bcg
Class Compiler

java.lang.Object
  extended by bcg.Compiler
All Implemented Interfaces:
Visitor, org.objectweb.asm.Opcodes

public class Compiler
extends java.lang.Object
implements org.objectweb.asm.Opcodes, Visitor

Compiles an AST to Java bytecode. All visit methods return null on failure and non-null on success. The generated class has all support functions as static and all user code in the object initializer.

See Also:
compile(String, Node)

Nested Class Summary
protected  class Compiler.And
          Joins two conditions with and logic.
protected  class Compiler.ArrayEqual
          Check two arrays for equality.
protected  class Compiler.Boolean
          Conditional for a boolean value
protected  class Compiler.BooleanField
          Conditional for a boolean field
protected  class Compiler.ChainedArray
           
protected  class Compiler.ChainedCondition
           
protected  class Compiler.ChainedStringCondition
           
protected  class Compiler.Comparison
          Comparison Condition.
protected  class Compiler.Condition
          Continuation based conditions.
protected  class Compiler.FunctionCondition
           
protected  class Compiler.Not
          Simple inverting condition
protected  class Compiler.Or
          Joins two conditions with or logic.
protected  class Compiler.Unconditional
           
 
Field Summary
protected  boolean usesRead
          Mark if we've used the read builtin.
 
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DOUBLE, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F_APPEND, F_CHOP, F_FULL, F_NEW, F_SAME, F_SAME1, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAT, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INTEGER, INVOKEDYNAMIC, INVOKEDYNAMIC_OWNER, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LONG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, NULL, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, TOP, UNINITIALIZED_THIS, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6, V1_7
 
Constructor Summary
Compiler(java.lang.String name)
          Creates a compiler that constructs a class with a given name
 
Method Summary
protected  java.lang.Object arithmetic(BinaryOperator.BinOp oper, Expression left, Expression right)
           
protected  java.lang.Object comparison(BinaryOperator.BinOp oper, Expression left, Expression right)
           
static byte[] compile(java.lang.String name, Node ast)
          Simple entry point to get a byte array.
static java.lang.Class<?> compileClass(java.lang.String name, Node ast)
          Simple entry point to get a class object.
protected  int[] compInsns(BinaryOperator.BinOp oper)
           
protected  java.lang.Object functionCall(FunctionCall node)
           
protected  java.lang.Object load(Variable var)
          Puts value from variable on top of stack.
protected  java.lang.Object logic(BinaryOperator.BinOp oper, Expression left, Expression right)
           
static void main(java.lang.String[] args)
          Parse a program from a file and compile it.
protected  void makeReadFunc()
          Create a function to do heavy lifting for read.
protected  java.lang.String signature(Expression[] args, Type ret)
           
protected  java.lang.String signature(Type[] types, Type ret)
           
protected  java.lang.Object store(Variable var)
          Stores top value on stack in variable.
protected  java.lang.Object stringOp(BinaryOperator.BinOp oper, Expression left, Expression right)
           
 byte[] toByteArray()
          Returns a constructed class's bytecode.
 java.lang.Class<?> toClass()
          Returns a constructed class.
 java.lang.Object visit(Assignment node)
           
 java.lang.Object visit(BinaryOperator node)
           
 java.lang.Object visit(Block node)
           
 java.lang.Object visit(BuiltIn node)
           
 java.lang.Object visit(Chained node)
           
 java.lang.Object visit(ChainedString node)
           
 java.lang.Object visit(Expression node)
           
 java.lang.Object visit(FunctionCall node)
           
 java.lang.Object visit(If node)
           
 java.lang.Object visit(Indexed node)
           
 java.lang.Object visit(Literal.Array node)
           
 java.lang.Object visit(Literal.Bool node)
           
 java.lang.Object visit(Literal.Int node)
           
 java.lang.Object visit(Literal.Str node)
           
 java.lang.Object visit(Node node)
          Generic fallback
 java.lang.Object visit(Program node)
           
 java.lang.Object visit(Statement node)
           
 java.lang.Object visit(Subroutine node)
           
 java.lang.Object visit(SubroutineCall node)
           
 java.lang.Object visit(UnaryOperator node)
           
 java.lang.Object visit(Variable node)
           
 java.lang.Object visit(While node)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

usesRead

protected boolean usesRead
Mark if we've used the read builtin. Allows the compiler to generate support code only when needed.

Constructor Detail

Compiler

public Compiler(java.lang.String name)
Creates a compiler that constructs a class with a given name

Method Detail

compile

public static byte[] compile(java.lang.String name,
                             Node ast)
Simple entry point to get a byte array. Creates a class named name from ast and returns it as a byte array.


compileClass

public static java.lang.Class<?> compileClass(java.lang.String name,
                                              Node ast)
Simple entry point to get a class object. Creates a class named name from ast and returns it as a class object. Initialize the class to run the code.

See Also:
Constructor

toByteArray

public byte[] toByteArray()
Returns a constructed class's bytecode. Only useful after the compiler has visited a program.


toClass

public java.lang.Class<?> toClass()
Returns a constructed class. Only useful after the compiler has visited a program.


visit

public java.lang.Object visit(Node node)
Description copied from interface: Visitor
Generic fallback

Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Statement node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Expression node)
Specified by:
visit in interface Visitor

makeReadFunc

protected void makeReadFunc()
Create a function to do heavy lifting for read.


store

protected java.lang.Object store(Variable var)
Stores top value on stack in variable.


load

protected java.lang.Object load(Variable var)
Puts value from variable on top of stack.


visit

public java.lang.Object visit(Program node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Block node)
Specified by:
visit in interface Visitor

signature

protected java.lang.String signature(Type[] types,
                                     Type ret)

signature

protected java.lang.String signature(Expression[] args,
                                     Type ret)

visit

public java.lang.Object visit(Subroutine node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(BuiltIn node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Literal.Int node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Literal.Str node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Literal.Array node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Indexed node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Literal.Bool node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(UnaryOperator node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(BinaryOperator node)
Specified by:
visit in interface Visitor

arithmetic

protected java.lang.Object arithmetic(BinaryOperator.BinOp oper,
                                      Expression left,
                                      Expression right)

compInsns

protected int[] compInsns(BinaryOperator.BinOp oper)

comparison

protected java.lang.Object comparison(BinaryOperator.BinOp oper,
                                      Expression left,
                                      Expression right)

logic

protected java.lang.Object logic(BinaryOperator.BinOp oper,
                                 Expression left,
                                 Expression right)

stringOp

protected java.lang.Object stringOp(BinaryOperator.BinOp oper,
                                    Expression left,
                                    Expression right)

visit

public java.lang.Object visit(If node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Variable node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Assignment node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(While node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(Chained node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(ChainedString node)
Specified by:
visit in interface Visitor

visit

public java.lang.Object visit(SubroutineCall node)
Specified by:
visit in interface Visitor

functionCall

protected java.lang.Object functionCall(FunctionCall node)

visit

public java.lang.Object visit(FunctionCall node)
Specified by:
visit in interface Visitor

main

public static void main(java.lang.String[] args)
Parse a program from a file and compile it. Needs the file name so it can determine the output filename.



Written by Brian Gernhardt for Compiler Construction at RIT, Winter 2010