gnu.expr
Class Compilation

java.lang.Object
  extended by gnu.expr.Compilation
Direct Known Subclasses:
Translator

public class Compilation
extends java.lang.Object

State for a single expression or module. For each top-level thing (expression or file) we compile or evaluate we create a new Compilation.


Field Summary
static Type[] apply0args
           
static Method apply0method
           
static Type[] apply1args
           
static Method apply1method
           
static Type[] apply2args
           
static Method apply2method
           
static Method apply3method
           
static Method apply4method
           
static Method applyCpsMethod
           
static Method[] applymethods
           
static Type[] applyNargs
           
static Method applyNmethod
           
static Field argsCallContextField
           
static int CALL_WITH_CONSUMER
          Function results are written to the current CallContext's Consumer.
static int CALL_WITH_CONTINUATIONS
          Support for full continuations.
static int CALL_WITH_RETURN
          Plain calling convention, using regular Java parameters and returns.
static int CALL_WITH_TAILCALLS
          Like CALL_WITH_CONSUMER, but handle full on-stack-growing tail-calls.
static int CALL_WITH_UNSPECIFIED
           
 java.lang.String classPrefix
          If non-null: a prefix for generateClassName to prepend to names.
 ClassType curClass
           
 LambdaExp curLambda
           
protected  ScopeExp current_scope
           
 Options currentOptions
           
static boolean debugPrintFinalExpr
          If true, print out final expressions after optimizations etc.
static int defaultCallConvention
          The default calling convention.
static boolean emitSourceDebugExtAttr
           
 java.util.Stack exprStack
          A help vector for building expressions.
static Field falseConstant
           
static boolean fewerClasses
          If true, minimize the number of classes generated.
 boolean generateApplet
          True if we should generate an Applet.
static boolean generateAppletDefault
           
 boolean generateMain
          True if we should generate a main(String[]) method.
static boolean generateMainDefault
           
 boolean generateServlet
          True if we should generate an Servlet.
static boolean generateServletDefault
           
static Method getCallContextInstanceMethod
           
static Method getCurrentEnvironmentMethod
           
static Method getLocation1EnvironmentMethod
           
static Method getLocation2EnvironmentMethod
           
static Method getLocationMethod
           
static Method getProcedureBindingMethod
           
static Method getSymbolProcedureMethod
           
static Method getSymbolValueMethod
           
 boolean immediate
          True if the compiled result will be immediately loaded.
static boolean inlineOk
           
static Type[] int1Args
           
static ClassType javaIntegerType
           
static ClassType javaStringType
           
protected  Language language
           
 NameLookup lexical
          Current lexical scope - map name to Declaration.
 ClassType mainClass
           
 ModuleExp mainLambda
           
protected  SourceMessages messages
           
 Method method
          The current method.
 ClassType moduleClass
          Generated class that extends ModuleBody.
static int moduleStatic
          If moduleStatic > 0, (module-static #t) is implied by default.
 boolean mustCompile
          True if the form is too complex to evaluate,and we must compile it.
static Field noArgsField
           
static ArrayType objArrayType
           
static Options options
           
static Field pcCallContextField
           
static Field procCallContextField
           
static ClassType scmBooleanType
           
static ClassType scmKeywordType
           
static ClassType scmListType
           
static ClassType scmNamedType
           
static ClassType scmPairType
           
static ClassType scmSequenceType
           
static ClassType scmUndefinedType
           
static Type[] string1Arg
           
static Type[] sym1Arg
           
 Variable thisDecl
           
static Field trueConstant
           
static ClassType typeApplet
           
static ClassType typeCallContext
           
static ClassType typeClass
           
static ClassType typeClassType
           
static ClassType typeConsumer
           
static ClassType typeEnvironment
           
static ClassType typeLanguage
           
static ClassType typeLocation
           
static ClassType typeMethodProc
           
static ClassType typeModuleBody
           
static ClassType typeModuleMethod
           
static ClassType typeModuleWithContext
           
static ClassType typeObject
           
static ClassType typeObjectType
           
static ClassType typePair
           
static ClassType typeProcedure
           
static ClassType typeProcedure0
           
static ClassType typeProcedure1
           
static ClassType typeProcedure2
           
static ClassType typeProcedure3
           
static ClassType typeProcedure4
           
static ClassType[] typeProcedureArray
           
static ClassType typeProcedureN
           
static ClassType typeRunnable
           
static ClassType typeServlet
           
static ClassType typeString
           
static ClassType typeSymbol
           
static ClassType typeType
           
static ClassType typeValues
           
 
Constructor Summary
Compilation(boolean immediate, SourceMessages messages)
           
Compilation(Language language, SourceMessages messages)
           
Compilation(Language language, SourceMessages messages, NameLookup lexical)
           
Compilation(SourceMessages messages)
           
 
Method Summary
 void addClass(ClassType new_class)
           
 ClassType addClass(ModuleExp module)
          Compiles a module to a class.
 Field allocLocalField(Type type, java.lang.String name)
           
 void compile(ModuleExp lexp, java.lang.String classname, java.lang.String prefix)
          Create a new Compilation environment.
 void compileConstant(java.lang.Object value)
          Emit code to "evaluate" a compile-time constant.
 void compileConstant(java.lang.Object value, Target target)
           
 Field compileConstantToField(java.lang.Object value)
           
 void compileToArchive(ModuleExp mexp, java.lang.String fname)
           
 void compileToFiles(ModuleExp mexp, java.lang.String topname, java.lang.String directory, java.lang.String prefix)
           
 LambdaExp currentLambda()
           
 ModuleExp currentModule()
          The same as getModule, until we allow nested modules.
 ScopeExp currentScope()
           
static char demangle2(char char1, char char2)
          Demangle a three-character mangling starting with '$'.
static java.lang.String demangleName(java.lang.String name)
           
static java.lang.String demangleName(java.lang.String name, boolean reversible)
           
 void error(char severity, Declaration decl, java.lang.String msg1, java.lang.String msg2)
           
 void error(char severity, java.lang.String message)
           
 ClassType findNamedClass(java.lang.String name)
          Search this Compilation for a ClassType with a given name.
 void freeLocalField(Field field)
           
 void generateApplyMethodsWithContext(LambdaExp lexp)
          Generate ModuleBody's apply(CallContext) method Use the applyMethods vector, which contains methods that implement the (public, readable) methods of the current module.
 void generateApplyMethodsWithoutContext(LambdaExp lexp)
          Generate ModuleBody's apply0...applyN methods.
 java.lang.String generateClassName(java.lang.String hint)
          Generate an unused class name.
 void generateConstructor(ClassType clas, LambdaExp lexp)
           
 void generateConstructor(LambdaExp lexp)
           
 void generateMatchMethods(LambdaExp lexp)
           
 boolean getBooleanOption(java.lang.String key)
          Get a named boolean option.
 boolean getBooleanOption(java.lang.String key, boolean defaultValue)
          Get a named boolean option.
 CodeAttr getCode()
           
 int getColumn()
           
static Method getConstructor(ClassType clas, LambdaExp lexp)
           
 Method getConstructor(LambdaExp lexp)
           
static Compilation getCurrent()
           
 java.lang.String getFile()
           
 Method getForNameHelper()
          Generate a method to find a named Class without initializing it.
 Language getLanguage()
           
 int getLine()
           
 SourceMessages getMessages()
           
 ModuleExp getModule()
           
 ClassType getModuleType()
           
 boolean inlineOk(Expression proc)
           
 boolean inlineOk(Procedure prcc)
           
 boolean isStatic()
           
static boolean isValidJavaName(java.lang.String name)
           
 LetExp letDone(Expression body)
           
 void letEnter()
           
 void letStart()
           
 Declaration letVariable(java.lang.Object name, Type type, Expression init)
           
 void loadCallContext()
          Generate code to push the current CallContext on the JVM stack.
 void loadClassRef(java.lang.String className)
          Generate code to load a named Class without initializing it.
 Declaration lookup(java.lang.Object name, int namespace)
           
 void loopBody(Expression body)
           
 void loopCond(Expression cond)
           
 void loopEnter()
          Done handling loop variables, and pushes them into the lexical scope.
 Expression loopRepeat()
           
 Expression loopRepeat(Expression exp)
           
 Expression loopRepeat(Expression[] exps)
           
 void loopStart()
          Start a new loop.
 Declaration loopVariable(java.lang.Object name, Type type, Expression init)
           
static java.lang.String mangleName(java.lang.String name)
           
static java.lang.String mangleName(java.lang.String name, boolean reversible)
          Convert a string to a safe Java identifier.
static java.lang.String mangleNameIfNeeded(java.lang.String name)
           
static java.lang.String mangleURI(java.lang.String name)
          Map a URI to a package/class name.
 void mustCompileHere()
          Note that we have seen a construct that must be compiled, not evaluated.
 void outputClass(java.lang.String directory)
           
 Expression parse(java.lang.Object input)
          This may not make sense, except for Lisp-like languages.
 void pop()
           
 void pop(ScopeExp scope)
           
 void push(Declaration decl)
           
 void push(ScopeExp scope)
           
 void pushScope(ScopeExp scope)
           
 java.lang.Object resolve(java.lang.Object name, boolean function)
           
 void setColumn(int column)
           
static void setCurrent(Compilation comp)
           
 void setCurrentScope(ScopeExp scope)
          Set currentScope().
 void setFile(java.lang.String filename)
           
 void setLine(Expression position)
           
 void setLine(int line)
           
 void setLine(java.lang.String filename, int line, int column)
           
 void setMessages(SourceMessages messages)
           
 void setModule(ModuleExp mexp)
           
 Expression syntaxError(java.lang.String message)
          Handle syntax errors (at rewrite time).
 void usedClass(Type type)
          Called for classes referenced in bytecode.
 boolean usingCPStyle()
           
 boolean usingTailCalls()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mustCompile

public boolean mustCompile
True if the form is too complex to evaluate,and we must compile it. This is because it contains a construct we know how to compile, but not evaluate, and it it outside a function (which we always compile). This can be a let scope, or primitive procedure.


curClass

public ClassType curClass

mainClass

public ClassType mainClass

moduleClass

public ClassType moduleClass
Generated class that extends ModuleBody. Normally same as mainClass.


curLambda

public LambdaExp curLambda

mainLambda

public ModuleExp mainLambda

thisDecl

public Variable thisDecl

fewerClasses

public static boolean fewerClasses
If true, minimize the number of classes generated. Do this even if it makes things a little slower.


debugPrintFinalExpr

public static boolean debugPrintFinalExpr
If true, print out final expressions after optimizations etc.


options

public static Options options

currentOptions

public Options currentOptions

defaultCallConvention

public static int defaultCallConvention
The default calling convention. One of the following CALL_WITHG_xxx values.


CALL_WITH_UNSPECIFIED

public static final int CALL_WITH_UNSPECIFIED
See Also:
Constant Field Values

CALL_WITH_RETURN

public static final int CALL_WITH_RETURN
Plain calling convention, using regular Java parameters and returns.

See Also:
Constant Field Values

CALL_WITH_CONSUMER

public static final int CALL_WITH_CONSUMER
Function results are written to the current CallContext's Consumer.

See Also:
Constant Field Values

CALL_WITH_TAILCALLS

public static final int CALL_WITH_TAILCALLS
Like CALL_WITH_CONSUMER, but handle full on-stack-growing tail-calls.

See Also:
Constant Field Values

CALL_WITH_CONTINUATIONS

public static final int CALL_WITH_CONTINUATIONS
Support for full continuations. Not implemented.

See Also:
Constant Field Values

moduleStatic

public static int moduleStatic
If moduleStatic > 0, (module-static #t) is implied by default. If moduleStatic == 2, calls run. If moduleStatic < 0, (module-static #f) is implied by default.


immediate

public boolean immediate
True if the compiled result will be immediately loaded.


method

public Method method
The current method.


typeObject

public static ClassType typeObject

scmBooleanType

public static ClassType scmBooleanType

typeString

public static ClassType typeString

javaStringType

public static ClassType javaStringType

scmKeywordType

public static ClassType scmKeywordType

scmSequenceType

public static ClassType scmSequenceType

javaIntegerType

public static ClassType javaIntegerType

scmListType

public static ClassType scmListType

typePair

public static ClassType typePair

scmPairType

public static ClassType scmPairType

scmUndefinedType

public static ClassType scmUndefinedType

objArrayType

public static final ArrayType objArrayType

scmNamedType

public static ClassType scmNamedType

typeRunnable

public static ClassType typeRunnable

typeType

public static ClassType typeType

typeObjectType

public static ClassType typeObjectType

typeClass

public static ClassType typeClass

typeClassType

public static ClassType typeClassType

typeProcedure

public static ClassType typeProcedure

typeLanguage

public static ClassType typeLanguage

typeEnvironment

public static ClassType typeEnvironment

typeLocation

public static ClassType typeLocation

typeSymbol

public static ClassType typeSymbol

getSymbolValueMethod

public static final Method getSymbolValueMethod

getSymbolProcedureMethod

public static final Method getSymbolProcedureMethod

getLocationMethod

public static final Method getLocationMethod

getProcedureBindingMethod

public static final Method getProcedureBindingMethod

trueConstant

public static final Field trueConstant

falseConstant

public static final Field falseConstant

int1Args

public static final Type[] int1Args

string1Arg

public static final Type[] string1Arg

sym1Arg

public static final Type[] sym1Arg

getLocation1EnvironmentMethod

public static final Method getLocation1EnvironmentMethod

getLocation2EnvironmentMethod

public static final Method getLocation2EnvironmentMethod

getCurrentEnvironmentMethod

public static Method getCurrentEnvironmentMethod

apply0args

public static Type[] apply0args

apply1args

public static Type[] apply1args

apply2args

public static Type[] apply2args

applyNargs

public static Type[] applyNargs

apply0method

public static Method apply0method

apply1method

public static Method apply1method

apply2method

public static Method apply2method

apply3method

public static Method apply3method

apply4method

public static Method apply4method

applyNmethod

public static Method applyNmethod

applymethods

public static Method[] applymethods

typeProcedure0

public static ClassType typeProcedure0

typeProcedure1

public static ClassType typeProcedure1

typeProcedure2

public static ClassType typeProcedure2

typeProcedure3

public static ClassType typeProcedure3

typeProcedure4

public static ClassType typeProcedure4

typeProcedureN

public static ClassType typeProcedureN

typeModuleBody

public static ClassType typeModuleBody

typeModuleWithContext

public static ClassType typeModuleWithContext

typeApplet

public static ClassType typeApplet

typeServlet

public static ClassType typeServlet

typeCallContext

public static ClassType typeCallContext

typeConsumer

public static final ClassType typeConsumer

getCallContextInstanceMethod

public static Method getCallContextInstanceMethod

typeValues

public static ClassType typeValues

noArgsField

public static Field noArgsField

pcCallContextField

public static Field pcCallContextField

typeMethodProc

public static ClassType typeMethodProc

typeModuleMethod

public static ClassType typeModuleMethod

argsCallContextField

public static Field argsCallContextField

procCallContextField

public static Field procCallContextField

applyCpsMethod

public static Method applyCpsMethod

typeProcedureArray

public static ClassType[] typeProcedureArray

generateMainDefault

public static boolean generateMainDefault

generateMain

public boolean generateMain
True if we should generate a main(String[]) method.


generateAppletDefault

public static boolean generateAppletDefault

generateApplet

public boolean generateApplet
True if we should generate an Applet.


generateServletDefault

public static boolean generateServletDefault

generateServlet

public boolean generateServlet
True if we should generate an Servlet.


inlineOk

public static boolean inlineOk

classPrefix

public java.lang.String classPrefix
If non-null: a prefix for generateClassName to prepend to names.


emitSourceDebugExtAttr

public static boolean emitSourceDebugExtAttr

language

protected Language language

exprStack

public java.util.Stack exprStack
A help vector for building expressions.


lexical

public NameLookup lexical
Current lexical scope - map name to Declaration.


current_scope

protected ScopeExp current_scope

messages

protected SourceMessages messages
Constructor Detail

Compilation

public Compilation(boolean immediate,
                   SourceMessages messages)

Compilation

public Compilation(SourceMessages messages)

Compilation

public Compilation(Language language,
                   SourceMessages messages)

Compilation

public Compilation(Language language,
                   SourceMessages messages,
                   NameLookup lexical)
Method Detail

getBooleanOption

public final boolean getBooleanOption(java.lang.String key,
                                      boolean defaultValue)
Get a named boolean option.


getBooleanOption

public final boolean getBooleanOption(java.lang.String key)
Get a named boolean option.


usingCPStyle

public boolean usingCPStyle()

usingTailCalls

public boolean usingTailCalls()

getCode

public final CodeAttr getCode()

getModuleType

public final ClassType getModuleType()

compileConstant

public void compileConstant(java.lang.Object value)
Emit code to "evaluate" a compile-time constant. This is the normal external interface.

Parameters:
value - the value to be compiled

compileConstantToField

public Field compileConstantToField(java.lang.Object value)

inlineOk

public boolean inlineOk(Expression proc)

inlineOk

public boolean inlineOk(Procedure prcc)

compileConstant

public void compileConstant(java.lang.Object value,
                            Target target)

findNamedClass

public ClassType findNamedClass(java.lang.String name)
Search this Compilation for a ClassType with a given name.

Parameters:
name - the name of the class desired
Returns:
the matching ClassType, or null if none is found

mangleURI

public static java.lang.String mangleURI(java.lang.String name)
Map a URI to a package/class name. Similar to the JAXB mangling, and that in the Java language spec.


mangleName

public static java.lang.String mangleName(java.lang.String name)

mangleNameIfNeeded

public static java.lang.String mangleNameIfNeeded(java.lang.String name)

isValidJavaName

public static boolean isValidJavaName(java.lang.String name)

mangleName

public static java.lang.String mangleName(java.lang.String name,
                                          boolean reversible)
Convert a string to a safe Java identifier.

Parameters:
reversible - if we should use an invertible mapping.

demangle2

public static char demangle2(char char1,
                             char char2)
Demangle a three-character mangling starting with '$'. UNFINISHED!


demangleName

public static java.lang.String demangleName(java.lang.String name)

demangleName

public static java.lang.String demangleName(java.lang.String name,
                                            boolean reversible)

generateClassName

public java.lang.String generateClassName(java.lang.String hint)
Generate an unused class name.

Parameters:
hint - the requested name (or prefix)
Returns:
a unique class name.

compile

public void compile(ModuleExp lexp,
                    java.lang.String classname,
                    java.lang.String prefix)
Create a new Compilation environment.

Parameters:
lexp - top-level function
classname - name of top-level class to generate
prefix - prefix to pre-pend to the names of other (non-top) classes

compileToFiles

public void compileToFiles(ModuleExp mexp,
                           java.lang.String topname,
                           java.lang.String directory,
                           java.lang.String prefix)
                    throws java.io.IOException
Throws:
java.io.IOException

outputClass

public void outputClass(java.lang.String directory)
                 throws java.io.IOException
Throws:
java.io.IOException

compileToArchive

public void compileToArchive(ModuleExp mexp,
                             java.lang.String fname)
                      throws java.io.IOException
Throws:
java.io.IOException

addClass

public void addClass(ClassType new_class)

getConstructor

public final Method getConstructor(LambdaExp lexp)

getConstructor

public static final Method getConstructor(ClassType clas,
                                          LambdaExp lexp)

generateConstructor

public final void generateConstructor(LambdaExp lexp)

generateConstructor

public final void generateConstructor(ClassType clas,
                                      LambdaExp lexp)

generateMatchMethods

public void generateMatchMethods(LambdaExp lexp)

generateApplyMethodsWithContext

public void generateApplyMethodsWithContext(LambdaExp lexp)
Generate ModuleBody's apply(CallContext) method Use the applyMethods vector, which contains methods that implement the (public, readable) methods of the current module.


generateApplyMethodsWithoutContext

public void generateApplyMethodsWithoutContext(LambdaExp lexp)
Generate ModuleBody's apply0...applyN methods. Use the applyMethods vector, which contains methods that implement the (public, readable) methods of the current module.


addClass

public final ClassType addClass(ModuleExp module)
Compiles a module to a class.


allocLocalField

public Field allocLocalField(Type type,
                             java.lang.String name)

loadCallContext

public final void loadCallContext()
Generate code to push the current CallContext on the JVM stack.


freeLocalField

public void freeLocalField(Field field)

parse

public Expression parse(java.lang.Object input)
This may not make sense, except for Lisp-like languages. For those, 'input' an s-expression from the reader.


getLanguage

public Language getLanguage()

currentLambda

public LambdaExp currentLambda()

getModule

public final ModuleExp getModule()

setModule

public void setModule(ModuleExp mexp)

isStatic

public boolean isStatic()

currentModule

public ModuleExp currentModule()
The same as getModule, until we allow nested modules.


mustCompileHere

public void mustCompileHere()
Note that we have seen a construct that must be compiled, not evaluated. If we are not inside a lambda (which is always compiled), but only inside the outer-most ModuleExp, note that it must be compiled.


currentScope

public ScopeExp currentScope()

setCurrentScope

public void setCurrentScope(ScopeExp scope)
Set currentScope(). Also update the nesting object.


push

public void push(ScopeExp scope)

pushScope

public final void pushScope(ScopeExp scope)

pop

public void pop(ScopeExp scope)

pop

public final void pop()

push

public void push(Declaration decl)

lookup

public Declaration lookup(java.lang.Object name,
                          int namespace)

usedClass

public void usedClass(Type type)
Called for classes referenced in bytecode. Since this only does something when immediate, we only care about classes referenced in the bytecode when immediate. It is used to ensure that we can inherit from classes defines when in immediate mode (in Scheme using define-class or similar).


getMessages

public SourceMessages getMessages()

setMessages

public void setMessages(SourceMessages messages)

error

public void error(char severity,
                  java.lang.String message)

error

public void error(char severity,
                  Declaration decl,
                  java.lang.String msg1,
                  java.lang.String msg2)

syntaxError

public Expression syntaxError(java.lang.String message)
Handle syntax errors (at rewrite time).

Parameters:
message - an error message to print out
Returns:
an ErrorExp

getFile

public final java.lang.String getFile()

getLine

public final int getLine()

getColumn

public final int getColumn()

setFile

public void setFile(java.lang.String filename)

setLine

public void setLine(int line)

setColumn

public void setColumn(int column)

setLine

public final void setLine(Expression position)

setLine

public void setLine(java.lang.String filename,
                    int line,
                    int column)

letStart

public void letStart()

letVariable

public Declaration letVariable(java.lang.Object name,
                               Type type,
                               Expression init)

letEnter

public void letEnter()

letDone

public LetExp letDone(Expression body)

loopStart

public void loopStart()
Start a new loop. (We could make this implied by the first loopVaribale call ???)


loopVariable

public Declaration loopVariable(java.lang.Object name,
                                Type type,
                                Expression init)

loopEnter

public void loopEnter()
Done handling loop variables, and pushes them into the lexical scope. Ready to parse the loop condition.


loopCond

public void loopCond(Expression cond)

loopBody

public void loopBody(Expression body)

loopRepeat

public Expression loopRepeat(Expression[] exps)

loopRepeat

public Expression loopRepeat()

loopRepeat

public Expression loopRepeat(Expression exp)

loadClassRef

public void loadClassRef(java.lang.String className)
Generate code to load a named Class without initializing it.


getForNameHelper

public Method getForNameHelper()
Generate a method to find a named Class without initializing it. Generate a static helper method "class$" like javac generates for 'CLASS.class', but does not initialize CLASS. Also, we don't bother catching exceptions, since the JVM doesn't require us to. I.e. generates: public static class $(String name) { return Class.forName(name, false, Class.forName(THISCLASSNAME).getClassLoader()); } Note that we want the result to use the same ClassLoader as the caller, which is why we generate a static helper method.


resolve

public java.lang.Object resolve(java.lang.Object name,
                                boolean function)

getCurrent

public static Compilation getCurrent()

setCurrent

public static void setCurrent(Compilation comp)