@@ -4,14 +4,34 @@ import decaf.frontend.annot._
44import decaf .frontend .tree .TreeNode
55import org .objectweb .asm .{Label , MethodVisitor , Opcodes , Type => ASMType }
66
7+ /**
8+ * Utilities for emitting JVM bytecode.
9+ */
710trait Util {
811
12+ /**
13+ * Internal name of Java's super class -- the well-known `java.lang.Object`.
14+ */
915 val JAVA_SUPER_INTERNAL_NAME = ASMType .getInternalName(classOf [java.lang.Object ])
16+
17+ /**
18+ * Default name of constructors.
19+ */
1020 val CONSTRUCTOR_NAME = " <init>"
21+
22+ /**
23+ * Type descriptor of constructors.
24+ */
1125 val CONSTRUCTOR_DESC = ASMType .getMethodDescriptor(ASMType .VOID_TYPE )
26+
27+ /**
28+ * Type descriptor of main method.
29+ */
1230 val MAIN_DESCRIPTOR : String = " ([Ljava/lang/String;)V"
1331
1432 /**
33+ * Emit bytecode for if-then-else branching. The boolean value of condition shall now be on the stack top.
34+ * Pseudo code:
1535 * {{{
1636 * if != 0 goto true
1737 * falseBranch
@@ -20,6 +40,10 @@ trait Util {
2040 * trueBranch
2141 * exit:
2242 * }}}
43+ *
44+ * @param trueBranch code (to be generated) of the true branch
45+ * @param falseBranch code (to be generated) of the true branch
46+ * @param mv method visitor
2347 */
2448 def ifThenElse (trueBranch : => Unit , falseBranch : => Unit )(implicit mv : MethodVisitor ): Unit = {
2549 val trueLabel = new Label
@@ -34,6 +58,8 @@ trait Util {
3458 }
3559
3660 /**
61+ * Emit bytecode for while-loop.
62+ * Pseudo code:
3763 * {{{
3864 * enter:
3965 * cond
@@ -43,9 +69,10 @@ trait Util {
4369 * exit:
4470 * }}}
4571 *
46- * @param body
47- * @param exit
48- * @param mv
72+ * @param cond code (to be generated) for evaluating the condition
73+ * @param body code (to be generated) of the loop body
74+ * @param exit label of loop exit
75+ * @param mv method visitor
4976 */
5077 def loop (cond : => Unit , exit : Label )(body : => Unit )(implicit mv : MethodVisitor ): Unit = {
5178 val enter = new Label
@@ -114,6 +141,7 @@ trait Util {
114141 ASMType .getMethodDescriptor(system_out.getDeclaredMethod(" print" , string)), false )
115142 }
116143
144+ /** Translate a Decaf type to JVM assembly type. */
117145 def toASMType (typ : Type ): ASMType = typ match {
118146 case IntType => ASMType .INT_TYPE
119147 case BoolType => ASMType .BOOLEAN_TYPE
@@ -125,15 +153,15 @@ trait Util {
125153 }
126154
127155 /**
128- * Get the ASM internal name of a class symbol.
156+ * Get the internal name of a class symbol.
129157 *
130158 * @param clazz the class symbol
131159 * @return its internal name
132160 */
133161 def internalName (clazz : ClassSymbol ): String = toASMType(clazz.typ).getInternalName
134162
135163 /**
136- * Get the JVM (type) descriptor of a field symbol.
164+ * Get the (type) descriptor of a field symbol.
137165 * See https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3 for descriptor syntax.
138166 *
139167 * @param field the field symbol, i.e. member var/method
0 commit comments