Skip to content

Commit 81b61a3

Browse files
committed
run script
1 parent 0950f30 commit 81b61a3

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

README.md

+81
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,84 @@ Once the live-in and live-out sets are computed. the live range for a variable a
5151

5252
## MIPS Assembly
5353
This is the last translation phase in the miniJAVA compiler. This translation phase takes in a vapor-M program and translates it into MIPS assembly using the vapor-M visitor to visit every node in the vapor-M abstract syntax tree and generate MIPS assembly code for that node. Here, the compiler must generate code that takes of stack frame allocations and deallocations during function calls. The compiler must also generate code for vapor and vapor-M builtins such as `HeapAllocZ`.
54+
55+
## Usage Example
56+
This project requires that gradle be installed. Before you can run the project, you need to install it. This is done
57+
by running the `make install` command. Once this command is issued, you will see a `install` directory. The
58+
`MiniJavaCompiler` executable is located in this directory. To compile, for instance, the following program:
59+
```java
60+
class Main {
61+
public static void main(String[] a){
62+
System.out.println(12 + 21);
63+
}
64+
}
65+
```
66+
which is saved as `add.java` navigate to the install directory and issue: `./MiniJavaCompiler --file add.java --o add
67+
.s`. This will generate a file with the `.s` extension containing the MIPS assembly code of `add.java`. To get
68+
verbose details on the assembly code generation process use the additional `--verbose` flag. The generated assembly
69+
code for the above program is laid out below:
70+
```mips
71+
.data
72+
73+
.text
74+
75+
jal Main
76+
li $v0 10
77+
syscall
78+
79+
Main:
80+
sw $fp -8($sp)
81+
move $fp $sp
82+
subu $sp $sp 76
83+
sw $ra -4($fp)
84+
li $t0 33 #2
85+
move $a0 $t0
86+
jal _print
87+
lw $ra -4($fp)
88+
lw $fp -8($fp)
89+
addu $sp $sp 76
90+
jr $ra
91+
92+
AllocArray:
93+
sw $fp -8($sp)
94+
move $fp $sp
95+
subu $sp $sp 76
96+
sw $ra -4($fp)
97+
move $t1 $a0
98+
mul $t0 $t1 4
99+
addu $t0 $t0 4
100+
move $a0 $t0
101+
jal _heapAlloc
102+
move $t2 $v0
103+
sw $t1 0($t2)
104+
move $v0 $t2
105+
lw $ra -4($fp)
106+
lw $fp -8($fp)
107+
addu $sp $sp 76
108+
jr $ra
109+
110+
_print:
111+
li $v0 1 # syscall: print integer
112+
syscall
113+
la $a0 _newline
114+
li $v0 4 # syscall: print string
115+
syscall
116+
jr $ra
117+
118+
_error:
119+
li $v0 4 # syscall: print string
120+
syscall
121+
li $v0 10 # syscall: exit
122+
syscall
123+
124+
_heapAlloc:
125+
li $v0 9 # syscall: sbrk
126+
syscall
127+
jr $ra
128+
129+
.data
130+
.align 0
131+
_newline: .asciiz "\n"
132+
_str0: .asciiz "null pointer\n"
133+
_str1: .asciiz "array index out of bounds\n"
134+
```

makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ install:
1010
@cp -r src/main/java/lib $(DESTDIR)
1111
@cp $(BUILD_LIBS)/MiniJavaCompiler.jar $(DESTDIR)
1212
@cp MiniJavaCompiler $(DESTDIR)
13-
@chmod +x $(DESTDIR)/MiniJavaCompiler
13+
@chmod +x $(DESTDIR)/MiniJavaCompiler
14+
@chmod +x run

run

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
java -jar src/main/java/lib/mars.jar nc $1

src/main/java/MiniJavaCompiler.java

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static void main(String[] args) throws Throwable {
3737
break;
3838
}
3939
}
40+
if (output_file == null) {
41+
output_file = filename.substring(0, filename.indexOf(".java"));
42+
}
4043

4144
boolean programTypeChecks = MiniJavaCompiler.typeCheck();
4245

File renamed without changes.

0 commit comments

Comments
 (0)