$30
This assignment is to generate Java assembly code (for Java Virtual Machines) of the given μGO program. The generated code will then be translated to the Java bytecode by the Java assembler, Jasmin. The generated Java bytecode should be run by the Java Virtual Machine (JVM) successfully.
Environmental Setup
Install dependencies: $ sudo apt install flex bison
Java Virtual Machine (JVM): $ sudo apt install default-jre Java Assembler (Jasmin) is included in the Compiler hw3 le.
Judgmental tool: $ pip3 install local-judge
In this assignment, you have to build a μGO compiler. Figure 1 shows the big picture of this assignment and the descriptions for the execution steps are as follows.
Build your μGO compiler by injecting the Java assembly code into your ex/bison code developed in the previous assignments.
Run the compiler with the given μGO program (e.g., test.go le) to generate the corresponding Java assembly code (e.g., test.j le).
Run the Java assembler, Jasmin, to convert the Java assembly code into the Java bytecode (e.g., test.class le).
Run the generated Java bytecode (e.g., test.class le) with JVM and display the results.
2. Java Assembly Language (Jasmin Instructions)
In this section, we list the Jasmin instructions that you may use in developing your compiler.
2.1 Literals (Constants)
The table below lists the constants de ned in μGO language. Also, the Jasmin instructions that we use to load the constants into the Java stack are given. More about the load instructions could be found in the course slides, Intermediate Representation.
Constant in μGO
Jasmin Instruction
94
ldc 94
8.7
ldc 8.7
"Hello world"
ldc "Hello world"
true / false
iconst_1 / iconst_0 (or idc 1 / ldc 0 )
2.2 Operations
The tables below lists the μGO operators and the corresponding assembly code de ned in Jasmin (i.e., Jasmin Instruction).
2.2.1 Unary Operators
μGO Operator
Jasmin Instruction ( int32 )
Jasmin Instruction ( float32 )
+
- (ignore or a blank) - (ignore or a blank)
-
ineg
fneg
2.2.2 Binary Operators
μGO Operator
Jasmin Instruction ( int32 )
Jasmin Instruction ( float32 )
+
iadd
fadd
-
isub
fsub
*
imul
fmul
/
idiv
fdiv
%
irem
-
The following example shows the standard unary and binary arithmetic operations in μGO and the corresponding Jasmin instructions.
μGO Operator
Jasmin Instruction
&&
iand
||
ior
!
ixor ( true xor b equals to not b )
You need to use subtraction and jump instruction to complete comparison operations. For int32, you can use isub . For oat32, there is an instruction fcmpl is used to compare two oatingpoint numbers. Note that the result should be bool type, i.e., 0 or 1. Jump instruction will be mentioned at section 2.6.
The following example shows how to load the constant at the top of the stack and store the value to the local variable ( x = 9 ). In addition, it then loads a constant to the Java stack, loads the content of the local variable, and adds the two values before the results are stored to the local variable ( y = 4 + x ). Furthermore, the example code exhibits how to store a string to the local variable ( z = "Hello" ). The contents of local variables after the execution of the Jasmin code are shown as below.
The following example shows how to print out the constants with the Jasmin code. Note that there is a little bit di erent for the actual parameters of the println functions invoked by the
invokevirtual instructions, i.e., int32 ( I ), oat32 ( F ), and string
( Ljava/lang/String; ). Note also that you need to treat bool type as string when encountering print statement, and the corresponding code segments are shown as below.
The following example shows the usage of the casting instructions, i2f and f2i , where x is int32 local variable 0, y is oat32 local variable 1.
2.6 Jump Instruction
The following example shows how to use jump instructions (both conditional and non-conditional branches). Jump instruction is used in if statement and for statement.
Jasmin Instruction
Description
goto <label>
direct jump
ifeq <label>
jump if zero
ifne <label>
jump if nonzero
iflt <label>
jump if less than zero
ifle <label>
jump if less than or equal to zero
ifgt <label>
jump if greater than zero
ifge <label>
jump if greater than or equal to zero
2.7 Method Invocation
There are several forms of method-calling instructions in the JVM. In this homework, methods are called using the invokestatic instruction. The usage can be shown by following example. A function foo has signature (II)I that you have implemented in hw2, and this information is used during the code generation. The invokestatic Main/foo(II)I is used to invoke the method foo after two actual argumants ( 3 , 4 ) are loaded to the stack, and then the result of the function output will be pushed to the stack.
μGO Code:
2.8 Setup Code
A valid Jasmin program should include the code segments for the execution environment setup. Your compiler should be able to generate the setup code, together with the translated Jasmin instructions (as shown in the previous paragraphs). The example code is listed as below.
Filename: hw3.j (generated by your compiler)
You are required to build a μGO compiler based on the previous two assignments. The execution steps are described as follows.
Build your compiler by make command and you will get an executable named mycompiler .
Run your compiler using the command $ ./mycompiler < input.go , which is built by lex and yacc, with the given μGO code ( .go le) to generate the corresponding Java assembly code ( .j le).
The Java assembly code can be converted into the Java Bytecode ( .class le) through the Java assembler, Jasmin, i.e., use $ java -jar jasmin.jar hw3.j to generate Main.class .
Run the Java program ( .class le) with Java Virtual Machine (JVM); the program should generate the execution results required by this assignment, i.e., use $ java Main to run the executable.
3. What Should Your Compiler Do?
In Assignment 3, the ex/bison le only need to print out the error messages, we score your assignment depending on the JVM execution result, i.e., the output of the command:
$ java Main .
When ERROR occurs during the parsing phase, we expect your compiler to print out ALL error messages (does not a ect your score), as Assignment 2 did, and DO NOT generate the Java assembly code (.j le).
There are 11 test cases (each test case is 10pt and the total score is 110pt) in the Compiler hw3 le, and you can check the correctness by local-judge (type judge command in your terminal) as hw1 and hw2.
!!! Incorrect format wi
l l lose 10pt. !!!
5. Online Demonstration of Your Assignment 3
Demonstration will be held in virtual. The form and schedule of demonstration will be announced on Moodle later. During the demonstration, you will be asked to demonstrate your assignment downloaded from Moodle and you need to answer the questions about the logics of your codes in 5 ~ 10 minutes. The scores that you get for your Assignment 3 depend totally on how good your answers are. By default, the demonstration should be performed on TA’s PC.
Java bytecode instruction listings: https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings
Java Language and Virtual Machine Speci cations: https://docs.oracle.com/javase/specs/
The Go (not μGo) Playground: https://go.dev/play/