|
StaticJava
Project
In this
project you will work on a compiler for a subset of the Java
programming language that is static in
terms of its data types and
method invocation. Details on the deliverables and
milestones for the project
are available here
Specifically, StaticJava
has the following
features (or limitations):
- Only
two
variable types: boolean and int
- No
objects
(thus, no exception handling), arrays, or
threads
- Only
static
fields and methods
- Method
return type can be the above type and void
- No
method
overloading
- Can
call
Java library static methods (e.g.,
Integer.parseInt())
- Consists
of
one class that has a Java main method
(String array is allowed here)
- No
increment and decrement expressions in addition to other missing
operators
- Limited loop constructs
- No
package
declaration (i.e., the declared class
lives in the default package)
- All
code
must be in one file, etc.
Your job is to implement a compiler for an extended version
of this language based on the implementation given below.
<program>
::= <class-declaration>
<class-declaration>
::= "public" "class" ID
"{" <main-method-declaration>
<field-or-method-declaration>* "}"
<main-method-declaration>
::= "public" "static" "void"
"main" "(" "String" "["
"]" ID ")" "{"
<method-body> "}"
<field-or-method-declaration>
::= <field-declaration> | <method-declaration>
<field-declaration>
::= "static" <type>
ID ";"
<method-declaration>
::= "static" <return-type>
ID "(" <params>?
")" "{" <method-body>
"}"
<type>
::= "boolean" | "int"
<return-type>
::= <type> | "void"
<params>
::= <param> ( ","
<param> )*
<param>
::= <type> ID
<method-body>
::= <local-declaration>* <statement>*
<local-declaration>
::= <type> ID ";"
<statement>
::= <assign-statement> | <if-statement>
| <while-statement> | <invoke-exp-statement>
| <return-statement>
<assign-statement>
::= ID "=" <exp>
";"
<if-statement>
::= "if" "(" <exp>
")" "{" <statement>*
"}" ( "else" "{"
<statement>* "}" )?
<while-statement>
::= "while" "(" <exp>
")" "{" <statement>*
"}"
<invoke-exp-statement>
::= <invoke-exp> ";"
<return-statement>
::= "return" <exp> ";"
<exp>
::= <literal-exp> | <unary-exp>
| <binary-exp> | <paren-exp>
| <invoke-exp> | <var-ref>
<literal-exp>
::= <boolean-literal> | NUM
<boolean-literal>
::= "true" | "false"
<unary-exp>
::= <unary-op> <exp>
<unary-op>
::= "+" | "-" | "!"
<binary-exp>
::= <exp> <binary-op>
<exp>
<binary-op>
::= "+" | "-" | "*"
| "/" | "%" | ">"
| ">=" | "==" | "<"
| "<=" | "!=" | "&&"
| "||"
<paren-exp>
::= "(" <exp> ")"
<invoke-exp>
::= ( ID "." )? ID
"(" <args>? ")"
<args>
::= <exp> ( ","
<exp> )*
<var-ref>
::= ID
ID
= ( 'a'..'z' | 'A'..'Z' | '_' | '$' ) ( 'a'..'z' | 'A'..'Z' | '_' |
'0'..'9' | '$' )*
NUM
= '0' | ('1'..'9') ('0'..'9')*
public class Factorial { public static void main(String[] args) { StaticJavaLib.println(factorial(StaticJavaLib.getIntArgument(args, 0))); } static int factorial(int n) { int result; int i; StaticJavaLib.assertTrue(n >= 1); result = 1; i = 2; while (i <= n) { result = result * i; i = i + 1; } return result; } }
| Eclipse JDT, ASM,
and JVM Bytecode Representations |
| StaticJava
|
Eclipse
JDT (org.eclipse.jdt.core.dom.*) |
ASM
(org.objectweb.asm.tree.*) |
JVM
Bytecode |
| <program> |
CompilationUnit |
N/A |
N/A |
| <class-declaration>
|
TypeDeclaration |
ClassNode |
N/A |
| <main-method-declaration>
|
MethodDeclaration |
MethodNode |
N/A |
| <field-declaration>
|
FieldDeclaration |
FieldNode |
N/A |
| <method-declaration>
|
MethodDeclaration |
MethodNode |
N/A |
| <type> |
Type (PrimitiveType) |
N/A |
N/A |
| <return-type> |
PrimitiveType |
N/A |
N/A |
| <param> |
SingleVariableDeclaration |
N/A |
N/A |
| <method-body> |
Block |
N/A |
N/A |
| <local-declaration>
|
VariableDeclarationStatement |
LocalVariableNode |
N/A |
| <assign-statement>
|
ExpressionStatement (Assignment) |
VarInsnNode |
ISTORE, PUTSTATIC |
| <if-statement> |
IfStatement |
JumpInsnNode |
IF_xxx, GOTO |
| <while-statement>
|
WhileStatement |
JumpInsnNode |
IF_xxx, GOTO |
| <invoke-exp-statement>
|
ExpressionStatement (MethodInvocation) |
MethodInsnNode |
INVOKESTATIC |
| <return-statement>
|
ReturnStatement |
InsnNode |
RETURN, IRETURN |
| <literal-exp> |
BooleanLiteral, NumberLiteral |
InsnNode |
ICONST_M1, ICONST_0, ICONST_1, ICONST_2,
ICONST_3, ICONST_4, ICONST_5, BIPUSH, SIPUSH, LDC |
| <unary-exp> |
PrefixExpression |
IntInsnNode, JumpInsnNode |
INEG, IF_EQ |
| <binary-exp> |
BinaryExpression |
InsnNode, JumpInsnNode |
IADD, ISUB, IMUL, IDIV, IREM, INEG, IF_xxx, GOTO |
| <paren-exp> |
ParenthesizedExpression |
N/A |
N/A |
| <invoke-exp> |
MethodInvocation |
MethodInsnNode |
INVOKESTATIC |
| <var-ref> |
SimpleName |
VarInsnNode, FieldInsnNode |
ILOAD, GETSTATIC |
Miscellaneous: POP,
POP2, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, SWAP
| Implementation and API (javadoc) |
|