Initial commit - building
This commit is contained in:
commit
5f28f80c4e
28
.vscode/launch.json
vendored
Normal file
28
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Main",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "main.Main",
|
||||||
|
"projectName": "p5-compiler_25e45799"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Main",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "Main",
|
||||||
|
"projectName": "p5-compiler_25e45799"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Current File",
|
||||||
|
"request": "launch",
|
||||||
|
"mainClass": "${file}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
8
.vscode/settings.json
vendored
Normal file
8
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"java.project.referencedLibraries": [
|
||||||
|
"lib/**/*.jar",
|
||||||
|
"antlr-4.9.1-complete.jar"
|
||||||
|
],
|
||||||
|
"rpc.enabled": false,
|
||||||
|
"java.debug.settings.onBuildFailureProceed": true
|
||||||
|
}
|
BIN
antlr-4.9.1-complete.jar
Normal file
BIN
antlr-4.9.1-complete.jar
Normal file
Binary file not shown.
BIN
data/.DS_Store
vendored
Normal file
BIN
data/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
data/examples/.DS_Store
vendored
Normal file
BIN
data/examples/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
data/examples/Mars4_5.jar
Normal file
BIN
data/examples/Mars4_5.jar
Normal file
Binary file not shown.
35
data/examples/test1.asm
Normal file
35
data/examples/test1.asm
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "Hello world"
|
434
data/examples/test10.asm
Normal file
434
data/examples/test10.asm
Normal file
@ -0,0 +1,434 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for fib
|
||||||
|
fib:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 0
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel0
|
||||||
|
li $t0 1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
j datalabel1
|
||||||
|
datalabel0:
|
||||||
|
datalabel1:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 1
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel2
|
||||||
|
li $t0 1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
j datalabel3
|
||||||
|
datalabel2:
|
||||||
|
datalabel3:
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
sub $t1 $t1 $t2
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -12
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 12
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -20($sp)
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t1 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t3 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
li $t3 2
|
||||||
|
sub $t2 $t2 $t3
|
||||||
|
sw $t2 -20($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -16
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 16
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
lw $t1 -16($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t1
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t1 -24($sp)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel4
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 0
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 1
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 2
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 3
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 4
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 5
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 6
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 7
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 8
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 9
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 10
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel4: .asciiz "This program prints the first 11 numbers of the Fibonacci sequence"
|
92
data/examples/test11.asm
Normal file
92
data/examples/test11.asm
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
datalabel1:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 10
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel2
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel1
|
||||||
|
datalabel2:
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 0 through 9."
|
213
data/examples/test12.asm
Normal file
213
data/examples/test12.asm
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for fib
|
||||||
|
fib:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 0
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel0
|
||||||
|
li $t0 1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
j datalabel1
|
||||||
|
datalabel0:
|
||||||
|
datalabel1:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 1
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel2
|
||||||
|
li $t0 1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
j datalabel3
|
||||||
|
datalabel2:
|
||||||
|
datalabel3:
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
sub $t1 $t1 $t2
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -12
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 12
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -20($sp)
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t1 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t3 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
li $t3 2
|
||||||
|
sub $t2 $t2 $t3
|
||||||
|
sw $t2 -20($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -16
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 16
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
lw $t1 -16($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t1
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t1 -24($sp)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
la $a0 datalabel4
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
datalabel5:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 12
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel6
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
# Calling function fib
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fib
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel5
|
||||||
|
datalabel6:
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel4: .asciiz "This program prints the first 12 numbers of the Fibonacci sequence."
|
296
data/examples/test13.asm
Normal file
296
data/examples/test13.asm
Normal file
@ -0,0 +1,296 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t1 0
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t2 4
|
||||||
|
mul $t1 $t1 $t2
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t2 0
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t1 2
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t2 4
|
||||||
|
mul $t1 $t1 $t2
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t2 2
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t2 4
|
||||||
|
mul $t1 $t1 $t2
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t3 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
datalabel1:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 10
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel2
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -44
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t2 4
|
||||||
|
mul $t1 $t1 $t2
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 44
|
||||||
|
j datalabel1
|
||||||
|
datalabel2:
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t2 3
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t2 6
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
li $t2 6
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t3 4
|
||||||
|
mul $t2 $t2 $t3
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 6
|
||||||
|
mult $t0 $t1
|
||||||
|
mflo $t0
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This should print 0, 2, 2, 3, 6 and 36"
|
289
data/examples/test14.asm
Normal file
289
data/examples/test14.asm
Normal file
@ -0,0 +1,289 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for sum
|
||||||
|
sum:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# i
|
||||||
|
# sum
|
||||||
|
# n
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -16
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later.
|
||||||
|
li $t0 -20
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
datalabel0:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -16
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get n's offset from $sp from the symbol table and initialize n's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of n.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel1
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -20
|
||||||
|
# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of sum.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t3 16
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Get x's base address from the stack.
|
||||||
|
lw $t3 0($t3)
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t5 4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t5 $t5 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t4 0($t5)
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t5 4
|
||||||
|
mul $t4 $t4 $t5
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t3 $t3 $t4
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 20
|
||||||
|
j datalabel0
|
||||||
|
datalabel1:
|
||||||
|
# Get sum's offset from $sp from the symbol table and initialize sum's address with it. We'll add $sp later.
|
||||||
|
li $t1 -20
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of sum.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# i
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel2
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 0
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
datalabel3:
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t1 -44
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 10
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel4
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -44
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Evaluate the index expression and store in a register.
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# Multiply the index by -4.
|
||||||
|
li $t2 4
|
||||||
|
mul $t1 $t1 $t2
|
||||||
|
# Add the index offset to the offset.
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
# Get i's offset from $sp from the symbol table and initialize i's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of i.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
li $t2 1
|
||||||
|
add $t1 $t1 $t2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 44
|
||||||
|
j datalabel3
|
||||||
|
datalabel4:
|
||||||
|
# println
|
||||||
|
# Calling function sum
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -48($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t2 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Put the address of the array into the register.
|
||||||
|
# Use the address of a.
|
||||||
|
move $t1 $t2
|
||||||
|
sw $t1 -52($sp)
|
||||||
|
li $t1 4
|
||||||
|
sw $t1 -56($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -48
|
||||||
|
# Call the function
|
||||||
|
jal sum
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 48
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -48($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -60($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function sum
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -48($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t2 -40
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Put the address of the array into the register.
|
||||||
|
# Use the address of a.
|
||||||
|
move $t1 $t2
|
||||||
|
sw $t1 -52($sp)
|
||||||
|
li $t1 8
|
||||||
|
sw $t1 -56($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -48
|
||||||
|
# Call the function
|
||||||
|
jal sum
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 48
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -48($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -60($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel2: .asciiz "This should print 6 and 28"
|
89
data/examples/test2.asm
Normal file
89
data/examples/test2.asm
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
li $t0 7
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
li $t0 3
|
||||||
|
li $t1 4
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
li $t0 14
|
||||||
|
li $t1 2
|
||||||
|
div $t0 $t1
|
||||||
|
mflo $t0
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
li $t0 7
|
||||||
|
li $t1 1
|
||||||
|
mult $t0 $t1
|
||||||
|
mflo $t0
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
li $t0 7
|
||||||
|
li $t1 2
|
||||||
|
mult $t0 $t1
|
||||||
|
mflo $t0
|
||||||
|
li $t1 2
|
||||||
|
div $t0 $t1
|
||||||
|
mflo $t0
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7 7 7 7 7 (separated by newlines)"
|
73
data/examples/test3.asm
Normal file
73
data/examples/test3.asm
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# b
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 3
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 4
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints the number 7"
|
162
data/examples/test4.asm
Normal file
162
data/examples/test4.asm
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# b
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 3
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -8
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 5
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t2 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# b
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 9
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 2
|
||||||
|
sub $t1 $zero $t1
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t2 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 0
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 4
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 8
|
||||||
|
# println
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7 7 7"
|
155
data/examples/test5.asm
Normal file
155
data/examples/test5.asm
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for foo
|
||||||
|
foo:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
li $t0 7
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for fum
|
||||||
|
fum:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# b
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 9
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 12
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t1 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t2 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
li $t1 4
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Calling function foo
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -12
|
||||||
|
# Call the function
|
||||||
|
jal foo
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 12
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Calling function foo
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal foo
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Calling function fum
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal fum
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7 7 7"
|
138
data/examples/test6.asm
Normal file
138
data/examples/test6.asm
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for add
|
||||||
|
add:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# i
|
||||||
|
# y
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of y.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# b
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Calling function add
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 3
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
li $t1 4
|
||||||
|
sw $t1 -20($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -12
|
||||||
|
# Call the function
|
||||||
|
jal add
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 12
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 5
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t0 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 2
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# Calling function add
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t2 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
sw $t1 -16($sp)
|
||||||
|
# Get b's offset from $sp from the symbol table and initialize b's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of b.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
sw $t1 -20($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -12
|
||||||
|
# Call the function
|
||||||
|
jal add
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 12
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7 7"
|
141
data/examples/test7.asm
Normal file
141
data/examples/test7.asm
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for identity
|
||||||
|
identity:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
sw $t0 -8($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for add
|
||||||
|
add:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# y
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of y.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function identity
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 7
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal identity
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -12($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function add
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 3
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
li $t1 4
|
||||||
|
sw $t1 -12($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal add
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -16($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7 7"
|
164
data/examples/test8.asm
Normal file
164
data/examples/test8.asm
Normal file
@ -0,0 +1,164 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for add
|
||||||
|
add:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# y
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later.
|
||||||
|
li $t2 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t2 $t2 $sp
|
||||||
|
# Load the value of y.
|
||||||
|
lw $t1 0($t2)
|
||||||
|
add $t0 $t0 $t1
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for add2
|
||||||
|
add2:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# x
|
||||||
|
# y
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Calling function add
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -16($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Calling function add
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t1 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -16($sp)
|
||||||
|
sw $t1 -20($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
# Get x's offset from $sp from the symbol table and initialize x's address with it. We'll add $sp later.
|
||||||
|
li $t3 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Load the value of x.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
sw $t2 -24($sp)
|
||||||
|
# Get y's offset from $sp from the symbol table and initialize y's address with it. We'll add $sp later.
|
||||||
|
li $t3 -8
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t3 $t3 $sp
|
||||||
|
# Load the value of y.
|
||||||
|
lw $t2 0($t3)
|
||||||
|
sw $t2 -28($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -20
|
||||||
|
# Call the function
|
||||||
|
jal add
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 20
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -16($sp)
|
||||||
|
lw $t1 -20($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t1
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t1 -32($sp)
|
||||||
|
sw $t1 -20($sp)
|
||||||
|
li $t1 1
|
||||||
|
sw $t1 -24($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -16
|
||||||
|
# Call the function
|
||||||
|
jal add
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 16
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -16($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -28($sp)
|
||||||
|
sw $t0 -12($sp)
|
||||||
|
jr $ra
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
jr $ra
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# println
|
||||||
|
# Calling function add2
|
||||||
|
# Save $ra to a register
|
||||||
|
move $t0 $ra
|
||||||
|
# Save $t0-9 registers
|
||||||
|
sw $t0 -4($sp)
|
||||||
|
# Evaluate parameters and save to stack
|
||||||
|
li $t1 2
|
||||||
|
sw $t1 -8($sp)
|
||||||
|
li $t1 4
|
||||||
|
sw $t1 -12($sp)
|
||||||
|
# Update the stack pointer
|
||||||
|
add $sp $sp -4
|
||||||
|
# Call the function
|
||||||
|
jal add2
|
||||||
|
# Restore the stack pointer
|
||||||
|
add $sp $sp 4
|
||||||
|
# Restore $t0-9 registers
|
||||||
|
lw $t0 -4($sp)
|
||||||
|
# Restore $ra
|
||||||
|
move $ra $t0
|
||||||
|
# Get return value off stack
|
||||||
|
lw $t0 -16($sp)
|
||||||
|
move $a0 $t0
|
||||||
|
li $v0 1
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints 7"
|
239
data/examples/test9.asm
Normal file
239
data/examples/test9.asm
Normal file
@ -0,0 +1,239 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# code for main
|
||||||
|
main:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# a
|
||||||
|
# println
|
||||||
|
# return
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -0
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t0 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t0 $t0 $sp
|
||||||
|
# Compute rhs for assignment =
|
||||||
|
li $t1 3
|
||||||
|
# complete assignment statement with store
|
||||||
|
sw $t1 0($t0)
|
||||||
|
# println
|
||||||
|
la $a0 datalabel0
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 4
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel1
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel3
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel2
|
||||||
|
datalabel1:
|
||||||
|
datalabel2:
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 4
|
||||||
|
slt $t0 $t1 $t0
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel4
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel6
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel5
|
||||||
|
datalabel4:
|
||||||
|
datalabel5:
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 4
|
||||||
|
slt $t0 $t1 $t0
|
||||||
|
subi $t0 $t0 1
|
||||||
|
bne $t0 $zero datalabel7
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel9
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel8
|
||||||
|
datalabel7:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel10
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
datalabel8:
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 3
|
||||||
|
slt $t0 $t1 $t0
|
||||||
|
bne $t0 $zero datalabel11
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel13
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel12
|
||||||
|
datalabel11:
|
||||||
|
datalabel12:
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 3
|
||||||
|
sub $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel14
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel16
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel15
|
||||||
|
datalabel14:
|
||||||
|
datalabel15:
|
||||||
|
# Get a's offset from $sp from the symbol table and initialize a's address with it. We'll add $sp later.
|
||||||
|
li $t1 -4
|
||||||
|
# Add the stack pointer address to the offset.
|
||||||
|
add $t1 $t1 $sp
|
||||||
|
# Load the value of a.
|
||||||
|
lw $t0 0($t1)
|
||||||
|
li $t1 4
|
||||||
|
slt $t0 $t0 $t1
|
||||||
|
bne $t0 $zero datalabel17
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel19
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
j datalabel18
|
||||||
|
datalabel17:
|
||||||
|
# Entering a new scope.
|
||||||
|
# Symbols in symbol table:
|
||||||
|
# println
|
||||||
|
# Update the stack pointer.
|
||||||
|
addi $sp $sp -4
|
||||||
|
# println
|
||||||
|
la $a0 datalabel20
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
la $a0 newline
|
||||||
|
li $v0 4
|
||||||
|
syscall
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 4
|
||||||
|
datalabel18:
|
||||||
|
# Exiting scope.
|
||||||
|
addi $sp $sp 0
|
||||||
|
li $v0 10
|
||||||
|
syscall
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
||||||
|
newline: .asciiz "\n"
|
||||||
|
datalabel0: .asciiz "This program prints [1..5] correct."
|
||||||
|
datalabel3: .asciiz "1 correct"
|
||||||
|
datalabel6: .asciiz "2 not correct"
|
||||||
|
datalabel9: .asciiz "2 not correct"
|
||||||
|
datalabel10: .asciiz "2 correct"
|
||||||
|
datalabel13: .asciiz "3 correct"
|
||||||
|
datalabel16: .asciiz "4 correct"
|
||||||
|
datalabel19: .asciiz "5 not correct"
|
||||||
|
datalabel20: .asciiz "5 correct"
|
23
data/test
Executable file
23
data/test
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
#! /bin/bash
|
||||||
|
|
||||||
|
java -jar ./Mars4_5.jar test1.asm
|
||||||
|
java -jar ./Mars4_5.jar test2.asm
|
||||||
|
java -jar ./Mars4_5.jar test3.asm
|
||||||
|
java -jar ./Mars4_5.jar test4.asm
|
||||||
|
java -jar ./Mars4_5.jar test5.asm
|
||||||
|
java -jar ./Mars4_5.jar test6.asm
|
||||||
|
java -jar ./Mars4_5.jar test7.asm
|
||||||
|
java -jar ./Mars4_5.jar test8.asm
|
||||||
|
java -jar ./Mars4_5.jar test9.asm
|
||||||
|
java -jar ./Mars4_5.jar test10.asm
|
||||||
|
java -jar ./Mars4_5.jar test11.asm
|
||||||
|
java -jar ./Mars4_5.jar test12.asm
|
||||||
|
java -jar ./Mars4_5.jar test13.asm
|
||||||
|
java -jar ./Mars4_5.jar test14.asm
|
||||||
|
|
||||||
|
# This loop runs the Java code to create the .asm then runs the .asm.
|
||||||
|
#for i in {1..14}
|
||||||
|
#do
|
||||||
|
# java -cp ../../out/production/p5-compiler:../../antlr-4.9.1-complete.jar main.Main ../test$i.c
|
||||||
|
# java -jar ./Mars4_5.jar ../test$i.asm
|
||||||
|
#done
|
13
data/test1.asm
Normal file
13
data/test1.asm
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# All program code is placed after the
|
||||||
|
# .text assembler directive
|
||||||
|
.text
|
||||||
|
|
||||||
|
# Declare main as a global function
|
||||||
|
.globl main
|
||||||
|
|
||||||
|
j main
|
||||||
|
|
||||||
|
# All memory structures are placed after the
|
||||||
|
# .data assembler directive
|
||||||
|
.data
|
||||||
|
|
3
data/test1.c
Normal file
3
data/test1.c
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
void main() {
|
||||||
|
println("Hello world");
|
||||||
|
}
|
20
data/test10.c
Normal file
20
data/test10.c
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
int fib(int i) {
|
||||||
|
if (i == 0) return 1;
|
||||||
|
if (i == 1) return 1;
|
||||||
|
return fib(i-1) + fib(i-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
println("This program prints the first 11 numbers of the Fibonacci sequence");
|
||||||
|
println(fib(0)); // 1
|
||||||
|
println(fib(1)); // 1
|
||||||
|
println(fib(2)); // 2
|
||||||
|
println(fib(3)); // 3
|
||||||
|
println(fib(4)); // 5
|
||||||
|
println(fib(5)); // 8
|
||||||
|
println(fib(6)); // 13
|
||||||
|
println(fib(7)); // 21
|
||||||
|
println(fib(8)); // 34
|
||||||
|
println(fib(9)); // 55
|
||||||
|
println(fib(10)); // 89
|
||||||
|
}
|
9
data/test11.c
Normal file
9
data/test11.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
void main() {
|
||||||
|
int i;
|
||||||
|
i = 0;
|
||||||
|
println("This program prints 0 through 9.");
|
||||||
|
while (i < 10) {
|
||||||
|
println(i);
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
}
|
15
data/test12.c
Normal file
15
data/test12.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
int fib(int i) {
|
||||||
|
if (i == 0) return 1;
|
||||||
|
if (i == 1) return 1;
|
||||||
|
return fib(i-1) + fib(i-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int i;
|
||||||
|
i = 0;
|
||||||
|
println("This program prints the first 12 numbers of the Fibonacci sequence.");
|
||||||
|
while (i < 12) {
|
||||||
|
println(fib(i));
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
}
|
23
data/test13.c
Normal file
23
data/test13.c
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
void main() {
|
||||||
|
int a[10];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
println("This should print 0, 2, 2, 3, 6 and 36");
|
||||||
|
a[0] = 0;
|
||||||
|
println(a[0]);
|
||||||
|
a[2] = 2;
|
||||||
|
println(a[2]);
|
||||||
|
|
||||||
|
i = 2;
|
||||||
|
a[i] = i;
|
||||||
|
println(a[i]);
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < 10) {
|
||||||
|
a[i] = i;
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
println(a[3]);
|
||||||
|
println(a[6]);
|
||||||
|
println(a[6]*6);
|
||||||
|
}
|
27
data/test14.c
Normal file
27
data/test14.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// Sum the first n elements of the array
|
||||||
|
int sum(int x[], int n) {
|
||||||
|
int i;
|
||||||
|
int sum;
|
||||||
|
i = 0;
|
||||||
|
sum = 0;
|
||||||
|
while (i < n) {
|
||||||
|
sum = sum + x[i];
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int a[10];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
println("This should print 6 and 28");
|
||||||
|
i = 0;
|
||||||
|
while (i < 10) {
|
||||||
|
a[i] = i;
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println(sum(a, 4));
|
||||||
|
println(sum(a, 8));
|
||||||
|
}
|
8
data/test2.c
Normal file
8
data/test2.c
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
void main() {
|
||||||
|
println("This program prints 7 7 7 7 7 (separated by newlines)");
|
||||||
|
println(7);
|
||||||
|
println(3+4);
|
||||||
|
println(14/2);
|
||||||
|
println(7*1);
|
||||||
|
println((7*2)/2);
|
||||||
|
}
|
9
data/test3.c
Normal file
9
data/test3.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
void main() {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
|
||||||
|
println("This program prints the number 7");
|
||||||
|
a = 3;
|
||||||
|
b = 4;
|
||||||
|
println(a+b);
|
||||||
|
}
|
21
data/test4.c
Normal file
21
data/test4.c
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
void main() {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
|
||||||
|
println("This program prints 7 7 7");
|
||||||
|
a = 3;
|
||||||
|
b = 2;
|
||||||
|
{
|
||||||
|
int a;
|
||||||
|
a = 5;
|
||||||
|
println(a+b);
|
||||||
|
{
|
||||||
|
int b;
|
||||||
|
b = 9;
|
||||||
|
a = -2;
|
||||||
|
println(a+b);
|
||||||
|
}
|
||||||
|
b = 4;
|
||||||
|
}
|
||||||
|
println(a+b);
|
||||||
|
}
|
19
data/test5.c
Normal file
19
data/test5.c
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
void foo() {
|
||||||
|
println(7);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fum() {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
a = 9;
|
||||||
|
b = 12;
|
||||||
|
println(b-a+4);
|
||||||
|
|
||||||
|
foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
println("This program prints 7 7 7");
|
||||||
|
foo();
|
||||||
|
fum();
|
||||||
|
}
|
15
data/test6.c
Normal file
15
data/test6.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
void add(int x, int y) {
|
||||||
|
int i;
|
||||||
|
println(x+y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
int a;
|
||||||
|
int b;
|
||||||
|
println("This program prints 7 7");
|
||||||
|
add(3, 4);
|
||||||
|
|
||||||
|
a = 5;
|
||||||
|
b = 2;
|
||||||
|
add(a, b);
|
||||||
|
}
|
14
data/test7.c
Normal file
14
data/test7.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
int identity(int x) {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add(int x, int y) {
|
||||||
|
return x+y;
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
println("This program prints 7 7");
|
||||||
|
println(identity(7));
|
||||||
|
println(add(3, 4));
|
||||||
|
}
|
||||||
|
|
14
data/test8.c
Normal file
14
data/test8.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
|
||||||
|
int add(int x, int y) {
|
||||||
|
return x+y;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add2(int x, int y) {
|
||||||
|
return add(add(x,y),1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
println("This program prints 7");
|
||||||
|
println(add2(2, 4));
|
||||||
|
}
|
||||||
|
|
27
data/test9.c
Normal file
27
data/test9.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
void main() {
|
||||||
|
int a;
|
||||||
|
a = 3;
|
||||||
|
println("This program prints [1..5] correct.");
|
||||||
|
if (a < 4) {
|
||||||
|
println("1 correct");
|
||||||
|
}
|
||||||
|
if (a > 4) {
|
||||||
|
println("2 not correct");
|
||||||
|
}
|
||||||
|
if (a > 4) {
|
||||||
|
println("2 not correct");
|
||||||
|
} else {
|
||||||
|
println("2 correct");
|
||||||
|
}
|
||||||
|
if (a <= 3) {
|
||||||
|
println("3 correct");
|
||||||
|
}
|
||||||
|
if (a == 3) {
|
||||||
|
println("4 correct");
|
||||||
|
}
|
||||||
|
if (a >= 4) {
|
||||||
|
println("5 not correct");
|
||||||
|
} else {
|
||||||
|
println("5 correct");
|
||||||
|
}
|
||||||
|
}
|
109
main/Main.java
Normal file
109
main/Main.java
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
package main;
|
||||||
|
|
||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
|
import parser.CminusLexer;
|
||||||
|
import parser.CminusParser;
|
||||||
|
import submit.ASTVisitor;
|
||||||
|
import org.antlr.v4.runtime.Parser;
|
||||||
|
import submit.RegisterAllocator;
|
||||||
|
import submit.SymbolTable;
|
||||||
|
import submit.ast.Node;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
private static Logger LOGGER;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args the command line arguments
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
// Logging setup
|
||||||
|
Level level = Level.INFO;
|
||||||
|
|
||||||
|
// TODO Enable trace-level code as needed. When true, LOGGER.fine() statements
|
||||||
|
// will be visible.
|
||||||
|
final boolean trace = true;
|
||||||
|
if (trace) {
|
||||||
|
level = Level.ALL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Properties props = System.getProperties();
|
||||||
|
props.setProperty("java.util.logging.SimpleFormatter.format", "%5$s%6$s%n");
|
||||||
|
Logger.getLogger("").setLevel(level);
|
||||||
|
for (Handler handler : Logger.getLogger("").getHandlers()) {
|
||||||
|
handler.setLevel(level);
|
||||||
|
}
|
||||||
|
LOGGER = Logger.getLogger(Parser.class.getName());
|
||||||
|
|
||||||
|
if (args.length < 1) {
|
||||||
|
throw new RuntimeException("Be sure to add your test C- file as a command-line parameter.");
|
||||||
|
}
|
||||||
|
final String filename = args[0];
|
||||||
|
|
||||||
|
LOGGER.info("");
|
||||||
|
LOGGER.info("Parsing " + filename + "\n");
|
||||||
|
LOGGER.info("");
|
||||||
|
final CharStream charStream = CharStreams.fromFileName(filename);
|
||||||
|
CminusLexer lexer = new CminusLexer(charStream);
|
||||||
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
|
CminusParser parser = new CminusParser(tokens);
|
||||||
|
parser.setBuildParseTree(true);
|
||||||
|
CminusParser.ProgramContext programCtx = parser.program();
|
||||||
|
|
||||||
|
LOGGER.info("");
|
||||||
|
LOGGER.info("Building abstract syntax tree");
|
||||||
|
LOGGER.info("");
|
||||||
|
ASTVisitor v = new ASTVisitor(LOGGER);
|
||||||
|
Node ast = v.visitProgram(programCtx);
|
||||||
|
SymbolTable symbolTable = v.getSymbolTable();
|
||||||
|
|
||||||
|
LOGGER.info("");
|
||||||
|
LOGGER.info("MIPS code:");
|
||||||
|
LOGGER.info("");
|
||||||
|
StringBuilder code = new StringBuilder();
|
||||||
|
StringBuilder data = new StringBuilder();
|
||||||
|
RegisterAllocator regAllocator = new RegisterAllocator();
|
||||||
|
try {
|
||||||
|
ast.toMIPS(code, data, symbolTable, regAllocator);
|
||||||
|
} finally {
|
||||||
|
StringBuilder mips = new StringBuilder();
|
||||||
|
mips.append("# All program code is placed after the\n" +
|
||||||
|
"# .text assembler directive\n" +
|
||||||
|
".text\n" +
|
||||||
|
"\n" +
|
||||||
|
"# Declare main as a global function\n" +
|
||||||
|
".globl\tmain\n\nj main\n");
|
||||||
|
mips.append(code.toString());
|
||||||
|
mips.append("\n# All memory structures are placed after the\n" +
|
||||||
|
"# .data assembler directive\n" +
|
||||||
|
".data\n" +
|
||||||
|
"\n");
|
||||||
|
mips.append(data.toString());
|
||||||
|
|
||||||
|
// Write MIPS code to stdout
|
||||||
|
LOGGER.info(mips.toString());
|
||||||
|
|
||||||
|
// Write MIPS code to output file for easy loading into MARS
|
||||||
|
final String fn = filename.substring(0, filename.length() - 1) + "asm";
|
||||||
|
try {
|
||||||
|
FileWriter myWriter = new FileWriter(fn);
|
||||||
|
myWriter.write(mips.toString());
|
||||||
|
myWriter.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.severe("Error writing to file " + fn + "\n");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
140
parser/Cminus.interp
Normal file
140
parser/Cminus.interp
Normal file
File diff suppressed because one or more lines are too long
90
parser/Cminus.tokens
Normal file
90
parser/Cminus.tokens
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
T__0=1
|
||||||
|
T__1=2
|
||||||
|
T__2=3
|
||||||
|
T__3=4
|
||||||
|
T__4=5
|
||||||
|
T__5=6
|
||||||
|
T__6=7
|
||||||
|
T__7=8
|
||||||
|
T__8=9
|
||||||
|
T__9=10
|
||||||
|
T__10=11
|
||||||
|
T__11=12
|
||||||
|
T__12=13
|
||||||
|
T__13=14
|
||||||
|
T__14=15
|
||||||
|
T__15=16
|
||||||
|
T__16=17
|
||||||
|
T__17=18
|
||||||
|
T__18=19
|
||||||
|
T__19=20
|
||||||
|
T__20=21
|
||||||
|
T__21=22
|
||||||
|
T__22=23
|
||||||
|
T__23=24
|
||||||
|
T__24=25
|
||||||
|
T__25=26
|
||||||
|
T__26=27
|
||||||
|
T__27=28
|
||||||
|
T__28=29
|
||||||
|
T__29=30
|
||||||
|
T__30=31
|
||||||
|
T__31=32
|
||||||
|
T__32=33
|
||||||
|
T__33=34
|
||||||
|
T__34=35
|
||||||
|
T__35=36
|
||||||
|
T__36=37
|
||||||
|
T__37=38
|
||||||
|
T__38=39
|
||||||
|
T__39=40
|
||||||
|
T__40=41
|
||||||
|
ID=42
|
||||||
|
NUMCONST=43
|
||||||
|
STRINGCONST=44
|
||||||
|
CHARCONST=45
|
||||||
|
BANG=46
|
||||||
|
WS=47
|
||||||
|
COMMENT=48
|
||||||
|
','=1
|
||||||
|
';'=2
|
||||||
|
'['=3
|
||||||
|
']'=4
|
||||||
|
'void'=5
|
||||||
|
'('=6
|
||||||
|
')'=7
|
||||||
|
'int'=8
|
||||||
|
'bool'=9
|
||||||
|
'char'=10
|
||||||
|
'[]'=11
|
||||||
|
'{'=12
|
||||||
|
'}'=13
|
||||||
|
'if'=14
|
||||||
|
'else'=15
|
||||||
|
'while'=16
|
||||||
|
'return'=17
|
||||||
|
'break'=18
|
||||||
|
'='=19
|
||||||
|
'+='=20
|
||||||
|
'-='=21
|
||||||
|
'*='=22
|
||||||
|
'/='=23
|
||||||
|
'++'=24
|
||||||
|
'--'=25
|
||||||
|
'||'=26
|
||||||
|
'&&'=27
|
||||||
|
'<='=28
|
||||||
|
'<'=29
|
||||||
|
'>'=30
|
||||||
|
'>='=31
|
||||||
|
'=='=32
|
||||||
|
'!='=33
|
||||||
|
'+'=34
|
||||||
|
'-'=35
|
||||||
|
'*'=36
|
||||||
|
'/'=37
|
||||||
|
'%'=38
|
||||||
|
'?'=39
|
||||||
|
'true'=40
|
||||||
|
'false'=41
|
||||||
|
'!'=46
|
435
parser/CminusBaseListener.java
Normal file
435
parser/CminusBaseListener.java
Normal file
@ -0,0 +1,435 @@
|
|||||||
|
// Generated from Cminus.g4 by ANTLR 4.9.1
|
||||||
|
package parser;
|
||||||
|
|
||||||
|
import org.antlr.v4.runtime.ParserRuleContext;
|
||||||
|
import org.antlr.v4.runtime.tree.ErrorNode;
|
||||||
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides an empty implementation of {@link CminusListener},
|
||||||
|
* which can be extended to create a listener which only needs to handle a subset
|
||||||
|
* of the available methods.
|
||||||
|
*/
|
||||||
|
public class CminusBaseListener implements CminusListener {
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterProgram(CminusParser.ProgramContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitProgram(CminusParser.ProgramContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterDeclaration(CminusParser.DeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitDeclaration(CminusParser.DeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterVarDeclaration(CminusParser.VarDeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitVarDeclaration(CminusParser.VarDeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterVarDeclId(CminusParser.VarDeclIdContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitVarDeclId(CminusParser.VarDeclIdContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterFunDeclaration(CminusParser.FunDeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitFunDeclaration(CminusParser.FunDeclarationContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterParam(CminusParser.ParamContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitParam(CminusParser.ParamContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterParamId(CminusParser.ParamIdContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitParamId(CminusParser.ParamIdContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterStatement(CminusParser.StatementContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitStatement(CminusParser.StatementContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterCompoundStmt(CminusParser.CompoundStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitCompoundStmt(CminusParser.CompoundStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterIfStmt(CminusParser.IfStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitIfStmt(CminusParser.IfStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterWhileStmt(CminusParser.WhileStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitWhileStmt(CminusParser.WhileStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterReturnStmt(CminusParser.ReturnStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitReturnStmt(CminusParser.ReturnStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterBreakStmt(CminusParser.BreakStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitBreakStmt(CminusParser.BreakStmtContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterExpression(CminusParser.ExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitExpression(CminusParser.ExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterOrExpression(CminusParser.OrExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitOrExpression(CminusParser.OrExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterAndExpression(CminusParser.AndExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitAndExpression(CminusParser.AndExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterRelExpression(CminusParser.RelExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitRelExpression(CminusParser.RelExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterRelop(CminusParser.RelopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitRelop(CminusParser.RelopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterSumExpression(CminusParser.SumExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitSumExpression(CminusParser.SumExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterSumop(CminusParser.SumopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitSumop(CminusParser.SumopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterTermExpression(CminusParser.TermExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitTermExpression(CminusParser.TermExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterMulop(CminusParser.MulopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitMulop(CminusParser.MulopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterUnaryop(CminusParser.UnaryopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitUnaryop(CminusParser.UnaryopContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterFactor(CminusParser.FactorContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitFactor(CminusParser.FactorContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterMutable(CminusParser.MutableContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitMutable(CminusParser.MutableContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterImmutable(CminusParser.ImmutableContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitImmutable(CminusParser.ImmutableContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterCall(CminusParser.CallContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitCall(CminusParser.CallContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterConstant(CminusParser.ConstantContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitConstant(CminusParser.ConstantContext ctx) { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void enterEveryRule(ParserRuleContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void exitEveryRule(ParserRuleContext ctx) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void visitTerminal(TerminalNode node) { }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation does nothing.</p>
|
||||||
|
*/
|
||||||
|
@Override public void visitErrorNode(ErrorNode node) { }
|
||||||
|
}
|
245
parser/CminusBaseVisitor.java
Normal file
245
parser/CminusBaseVisitor.java
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
// Generated from Cminus.g4 by ANTLR 4.9.1
|
||||||
|
package parser;
|
||||||
|
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class provides an empty implementation of {@link CminusVisitor},
|
||||||
|
* which can be extended to create a visitor which only needs to handle a subset
|
||||||
|
* of the available methods.
|
||||||
|
*
|
||||||
|
* @param <T> The return type of the visit operation. Use {@link Void} for
|
||||||
|
* operations with no return type.
|
||||||
|
*/
|
||||||
|
public class CminusBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements CminusVisitor<T> {
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitProgram(CminusParser.ProgramContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitDeclaration(CminusParser.DeclarationContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitVarDeclaration(CminusParser.VarDeclarationContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitVarDeclId(CminusParser.VarDeclIdContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitFunDeclaration(CminusParser.FunDeclarationContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitParam(CminusParser.ParamContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitParamId(CminusParser.ParamIdContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitStatement(CminusParser.StatementContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitCompoundStmt(CminusParser.CompoundStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitIfStmt(CminusParser.IfStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitWhileStmt(CminusParser.WhileStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitReturnStmt(CminusParser.ReturnStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitBreakStmt(CminusParser.BreakStmtContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitExpression(CminusParser.ExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitOrExpression(CminusParser.OrExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitAndExpression(CminusParser.AndExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitRelExpression(CminusParser.RelExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitRelop(CminusParser.RelopContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitSumExpression(CminusParser.SumExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitSumop(CminusParser.SumopContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitTermExpression(CminusParser.TermExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitMulop(CminusParser.MulopContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitUnaryop(CminusParser.UnaryopContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitFactor(CminusParser.FactorContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitMutable(CminusParser.MutableContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitImmutable(CminusParser.ImmutableContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitCall(CminusParser.CallContext ctx) { return visitChildren(ctx); }
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*
|
||||||
|
* <p>The default implementation returns the result of calling
|
||||||
|
* {@link #visitChildren} on {@code ctx}.</p>
|
||||||
|
*/
|
||||||
|
@Override public T visitConstant(CminusParser.ConstantContext ctx) { return visitChildren(ctx); }
|
||||||
|
}
|
163
parser/CminusLexer.interp
Normal file
163
parser/CminusLexer.interp
Normal file
File diff suppressed because one or more lines are too long
232
parser/CminusLexer.java
Normal file
232
parser/CminusLexer.java
Normal file
@ -0,0 +1,232 @@
|
|||||||
|
// Generated from Cminus.g4 by ANTLR 4.9.1
|
||||||
|
package parser;
|
||||||
|
import org.antlr.v4.runtime.Lexer;
|
||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.antlr.v4.runtime.Token;
|
||||||
|
import org.antlr.v4.runtime.TokenStream;
|
||||||
|
import org.antlr.v4.runtime.*;
|
||||||
|
import org.antlr.v4.runtime.atn.*;
|
||||||
|
import org.antlr.v4.runtime.dfa.DFA;
|
||||||
|
import org.antlr.v4.runtime.misc.*;
|
||||||
|
|
||||||
|
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
|
||||||
|
public class CminusLexer extends Lexer {
|
||||||
|
static { RuntimeMetaData.checkVersion("4.9.1", RuntimeMetaData.VERSION); }
|
||||||
|
|
||||||
|
protected static final DFA[] _decisionToDFA;
|
||||||
|
protected static final PredictionContextCache _sharedContextCache =
|
||||||
|
new PredictionContextCache();
|
||||||
|
public static final int
|
||||||
|
T__0=1, T__1=2, T__2=3, T__3=4, T__4=5, T__5=6, T__6=7, T__7=8, T__8=9,
|
||||||
|
T__9=10, T__10=11, T__11=12, T__12=13, T__13=14, T__14=15, T__15=16, T__16=17,
|
||||||
|
T__17=18, T__18=19, T__19=20, T__20=21, T__21=22, T__22=23, T__23=24,
|
||||||
|
T__24=25, T__25=26, T__26=27, T__27=28, T__28=29, T__29=30, T__30=31,
|
||||||
|
T__31=32, T__32=33, T__33=34, T__34=35, T__35=36, T__36=37, T__37=38,
|
||||||
|
T__38=39, T__39=40, T__40=41, ID=42, NUMCONST=43, STRINGCONST=44, CHARCONST=45,
|
||||||
|
BANG=46, WS=47, COMMENT=48;
|
||||||
|
public static String[] channelNames = {
|
||||||
|
"DEFAULT_TOKEN_CHANNEL", "HIDDEN"
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String[] modeNames = {
|
||||||
|
"DEFAULT_MODE"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static String[] makeRuleNames() {
|
||||||
|
return new String[] {
|
||||||
|
"T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "T__8",
|
||||||
|
"T__9", "T__10", "T__11", "T__12", "T__13", "T__14", "T__15", "T__16",
|
||||||
|
"T__17", "T__18", "T__19", "T__20", "T__21", "T__22", "T__23", "T__24",
|
||||||
|
"T__25", "T__26", "T__27", "T__28", "T__29", "T__30", "T__31", "T__32",
|
||||||
|
"T__33", "T__34", "T__35", "T__36", "T__37", "T__38", "T__39", "T__40",
|
||||||
|
"ID", "NUMCONST", "STRINGCONST", "CHARCONST", "BANG", "WS", "COMMENT",
|
||||||
|
"LETTER", "DIGIT"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public static final String[] ruleNames = makeRuleNames();
|
||||||
|
|
||||||
|
private static String[] makeLiteralNames() {
|
||||||
|
return new String[] {
|
||||||
|
null, "','", "';'", "'['", "']'", "'void'", "'('", "')'", "'int'", "'bool'",
|
||||||
|
"'char'", "'[]'", "'{'", "'}'", "'if'", "'else'", "'while'", "'return'",
|
||||||
|
"'break'", "'='", "'+='", "'-='", "'*='", "'/='", "'++'", "'--'", "'||'",
|
||||||
|
"'&&'", "'<='", "'<'", "'>'", "'>='", "'=='", "'!='", "'+'", "'-'", "'*'",
|
||||||
|
"'/'", "'%'", "'?'", "'true'", "'false'", null, null, null, null, "'!'"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
private static final String[] _LITERAL_NAMES = makeLiteralNames();
|
||||||
|
private static String[] makeSymbolicNames() {
|
||||||
|
return new String[] {
|
||||||
|
null, null, null, null, null, null, null, null, null, null, null, null,
|
||||||
|
null, null, null, null, null, null, null, null, null, null, null, null,
|
||||||
|
null, null, null, null, null, null, null, null, null, null, null, null,
|
||||||
|
null, null, null, null, null, null, "ID", "NUMCONST", "STRINGCONST",
|
||||||
|
"CHARCONST", "BANG", "WS", "COMMENT"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
private static final String[] _SYMBOLIC_NAMES = makeSymbolicNames();
|
||||||
|
public static final Vocabulary VOCABULARY = new VocabularyImpl(_LITERAL_NAMES, _SYMBOLIC_NAMES);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link #VOCABULARY} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static final String[] tokenNames;
|
||||||
|
static {
|
||||||
|
tokenNames = new String[_SYMBOLIC_NAMES.length];
|
||||||
|
for (int i = 0; i < tokenNames.length; i++) {
|
||||||
|
tokenNames[i] = VOCABULARY.getLiteralName(i);
|
||||||
|
if (tokenNames[i] == null) {
|
||||||
|
tokenNames[i] = VOCABULARY.getSymbolicName(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tokenNames[i] == null) {
|
||||||
|
tokenNames[i] = "<INVALID>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public String[] getTokenNames() {
|
||||||
|
return tokenNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
|
||||||
|
public Vocabulary getVocabulary() {
|
||||||
|
return VOCABULARY;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public CminusLexer(CharStream input) {
|
||||||
|
super(input);
|
||||||
|
_interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getGrammarFileName() { return "Cminus.g4"; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getRuleNames() { return ruleNames; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSerializedATN() { return _serializedATN; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getChannelNames() { return channelNames; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] getModeNames() { return modeNames; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ATN getATN() { return _ATN; }
|
||||||
|
|
||||||
|
public static final String _serializedATN =
|
||||||
|
"\3\u608b\ua72a\u8133\ub9ed\u417c\u3be7\u7786\u5964\2\62\u0132\b\1\4\2"+
|
||||||
|
"\t\2\4\3\t\3\4\4\t\4\4\5\t\5\4\6\t\6\4\7\t\7\4\b\t\b\4\t\t\t\4\n\t\n\4"+
|
||||||
|
"\13\t\13\4\f\t\f\4\r\t\r\4\16\t\16\4\17\t\17\4\20\t\20\4\21\t\21\4\22"+
|
||||||
|
"\t\22\4\23\t\23\4\24\t\24\4\25\t\25\4\26\t\26\4\27\t\27\4\30\t\30\4\31"+
|
||||||
|
"\t\31\4\32\t\32\4\33\t\33\4\34\t\34\4\35\t\35\4\36\t\36\4\37\t\37\4 \t"+
|
||||||
|
" \4!\t!\4\"\t\"\4#\t#\4$\t$\4%\t%\4&\t&\4\'\t\'\4(\t(\4)\t)\4*\t*\4+\t"+
|
||||||
|
"+\4,\t,\4-\t-\4.\t.\4/\t/\4\60\t\60\4\61\t\61\4\62\t\62\4\63\t\63\3\2"+
|
||||||
|
"\3\2\3\3\3\3\3\4\3\4\3\5\3\5\3\6\3\6\3\6\3\6\3\6\3\7\3\7\3\b\3\b\3\t\3"+
|
||||||
|
"\t\3\t\3\t\3\n\3\n\3\n\3\n\3\n\3\13\3\13\3\13\3\13\3\13\3\f\3\f\3\f\3"+
|
||||||
|
"\r\3\r\3\16\3\16\3\17\3\17\3\17\3\20\3\20\3\20\3\20\3\20\3\21\3\21\3\21"+
|
||||||
|
"\3\21\3\21\3\21\3\22\3\22\3\22\3\22\3\22\3\22\3\22\3\23\3\23\3\23\3\23"+
|
||||||
|
"\3\23\3\23\3\24\3\24\3\25\3\25\3\25\3\26\3\26\3\26\3\27\3\27\3\27\3\30"+
|
||||||
|
"\3\30\3\30\3\31\3\31\3\31\3\32\3\32\3\32\3\33\3\33\3\33\3\34\3\34\3\34"+
|
||||||
|
"\3\35\3\35\3\35\3\36\3\36\3\37\3\37\3 \3 \3 \3!\3!\3!\3\"\3\"\3\"\3#\3"+
|
||||||
|
"#\3$\3$\3%\3%\3&\3&\3\'\3\'\3(\3(\3)\3)\3)\3)\3)\3*\3*\3*\3*\3*\3*\3+"+
|
||||||
|
"\3+\3+\7+\u00ed\n+\f+\16+\u00f0\13+\3,\6,\u00f3\n,\r,\16,\u00f4\3-\3-"+
|
||||||
|
"\3-\3-\7-\u00fb\n-\f-\16-\u00fe\13-\3-\3-\3.\3.\3.\3.\5.\u0106\n.\3.\3"+
|
||||||
|
".\3/\3/\3\60\6\60\u010d\n\60\r\60\16\60\u010e\3\60\3\60\3\61\3\61\3\61"+
|
||||||
|
"\3\61\7\61\u0117\n\61\f\61\16\61\u011a\13\61\3\61\5\61\u011d\n\61\3\61"+
|
||||||
|
"\3\61\3\61\3\61\3\61\7\61\u0124\n\61\f\61\16\61\u0127\13\61\3\61\3\61"+
|
||||||
|
"\5\61\u012b\n\61\3\61\3\61\3\62\3\62\3\63\3\63\4\u00fc\u0125\2\64\3\3"+
|
||||||
|
"\5\4\7\5\t\6\13\7\r\b\17\t\21\n\23\13\25\f\27\r\31\16\33\17\35\20\37\21"+
|
||||||
|
"!\22#\23%\24\'\25)\26+\27-\30/\31\61\32\63\33\65\34\67\359\36;\37= ?!"+
|
||||||
|
"A\"C#E$G%I&K\'M(O)Q*S+U,W-Y.[/]\60_\61a\62c\2e\2\3\2\6\3\2$$\5\2\13\f"+
|
||||||
|
"\16\17\"\"\4\2\f\f\17\17\4\2C\\c|\2\u013a\2\3\3\2\2\2\2\5\3\2\2\2\2\7"+
|
||||||
|
"\3\2\2\2\2\t\3\2\2\2\2\13\3\2\2\2\2\r\3\2\2\2\2\17\3\2\2\2\2\21\3\2\2"+
|
||||||
|
"\2\2\23\3\2\2\2\2\25\3\2\2\2\2\27\3\2\2\2\2\31\3\2\2\2\2\33\3\2\2\2\2"+
|
||||||
|
"\35\3\2\2\2\2\37\3\2\2\2\2!\3\2\2\2\2#\3\2\2\2\2%\3\2\2\2\2\'\3\2\2\2"+
|
||||||
|
"\2)\3\2\2\2\2+\3\2\2\2\2-\3\2\2\2\2/\3\2\2\2\2\61\3\2\2\2\2\63\3\2\2\2"+
|
||||||
|
"\2\65\3\2\2\2\2\67\3\2\2\2\29\3\2\2\2\2;\3\2\2\2\2=\3\2\2\2\2?\3\2\2\2"+
|
||||||
|
"\2A\3\2\2\2\2C\3\2\2\2\2E\3\2\2\2\2G\3\2\2\2\2I\3\2\2\2\2K\3\2\2\2\2M"+
|
||||||
|
"\3\2\2\2\2O\3\2\2\2\2Q\3\2\2\2\2S\3\2\2\2\2U\3\2\2\2\2W\3\2\2\2\2Y\3\2"+
|
||||||
|
"\2\2\2[\3\2\2\2\2]\3\2\2\2\2_\3\2\2\2\2a\3\2\2\2\3g\3\2\2\2\5i\3\2\2\2"+
|
||||||
|
"\7k\3\2\2\2\tm\3\2\2\2\13o\3\2\2\2\rt\3\2\2\2\17v\3\2\2\2\21x\3\2\2\2"+
|
||||||
|
"\23|\3\2\2\2\25\u0081\3\2\2\2\27\u0086\3\2\2\2\31\u0089\3\2\2\2\33\u008b"+
|
||||||
|
"\3\2\2\2\35\u008d\3\2\2\2\37\u0090\3\2\2\2!\u0095\3\2\2\2#\u009b\3\2\2"+
|
||||||
|
"\2%\u00a2\3\2\2\2\'\u00a8\3\2\2\2)\u00aa\3\2\2\2+\u00ad\3\2\2\2-\u00b0"+
|
||||||
|
"\3\2\2\2/\u00b3\3\2\2\2\61\u00b6\3\2\2\2\63\u00b9\3\2\2\2\65\u00bc\3\2"+
|
||||||
|
"\2\2\67\u00bf\3\2\2\29\u00c2\3\2\2\2;\u00c5\3\2\2\2=\u00c7\3\2\2\2?\u00c9"+
|
||||||
|
"\3\2\2\2A\u00cc\3\2\2\2C\u00cf\3\2\2\2E\u00d2\3\2\2\2G\u00d4\3\2\2\2I"+
|
||||||
|
"\u00d6\3\2\2\2K\u00d8\3\2\2\2M\u00da\3\2\2\2O\u00dc\3\2\2\2Q\u00de\3\2"+
|
||||||
|
"\2\2S\u00e3\3\2\2\2U\u00e9\3\2\2\2W\u00f2\3\2\2\2Y\u00f6\3\2\2\2[\u0101"+
|
||||||
|
"\3\2\2\2]\u0109\3\2\2\2_\u010c\3\2\2\2a\u012a\3\2\2\2c\u012e\3\2\2\2e"+
|
||||||
|
"\u0130\3\2\2\2gh\7.\2\2h\4\3\2\2\2ij\7=\2\2j\6\3\2\2\2kl\7]\2\2l\b\3\2"+
|
||||||
|
"\2\2mn\7_\2\2n\n\3\2\2\2op\7x\2\2pq\7q\2\2qr\7k\2\2rs\7f\2\2s\f\3\2\2"+
|
||||||
|
"\2tu\7*\2\2u\16\3\2\2\2vw\7+\2\2w\20\3\2\2\2xy\7k\2\2yz\7p\2\2z{\7v\2"+
|
||||||
|
"\2{\22\3\2\2\2|}\7d\2\2}~\7q\2\2~\177\7q\2\2\177\u0080\7n\2\2\u0080\24"+
|
||||||
|
"\3\2\2\2\u0081\u0082\7e\2\2\u0082\u0083\7j\2\2\u0083\u0084\7c\2\2\u0084"+
|
||||||
|
"\u0085\7t\2\2\u0085\26\3\2\2\2\u0086\u0087\7]\2\2\u0087\u0088\7_\2\2\u0088"+
|
||||||
|
"\30\3\2\2\2\u0089\u008a\7}\2\2\u008a\32\3\2\2\2\u008b\u008c\7\177\2\2"+
|
||||||
|
"\u008c\34\3\2\2\2\u008d\u008e\7k\2\2\u008e\u008f\7h\2\2\u008f\36\3\2\2"+
|
||||||
|
"\2\u0090\u0091\7g\2\2\u0091\u0092\7n\2\2\u0092\u0093\7u\2\2\u0093\u0094"+
|
||||||
|
"\7g\2\2\u0094 \3\2\2\2\u0095\u0096\7y\2\2\u0096\u0097\7j\2\2\u0097\u0098"+
|
||||||
|
"\7k\2\2\u0098\u0099\7n\2\2\u0099\u009a\7g\2\2\u009a\"\3\2\2\2\u009b\u009c"+
|
||||||
|
"\7t\2\2\u009c\u009d\7g\2\2\u009d\u009e\7v\2\2\u009e\u009f\7w\2\2\u009f"+
|
||||||
|
"\u00a0\7t\2\2\u00a0\u00a1\7p\2\2\u00a1$\3\2\2\2\u00a2\u00a3\7d\2\2\u00a3"+
|
||||||
|
"\u00a4\7t\2\2\u00a4\u00a5\7g\2\2\u00a5\u00a6\7c\2\2\u00a6\u00a7\7m\2\2"+
|
||||||
|
"\u00a7&\3\2\2\2\u00a8\u00a9\7?\2\2\u00a9(\3\2\2\2\u00aa\u00ab\7-\2\2\u00ab"+
|
||||||
|
"\u00ac\7?\2\2\u00ac*\3\2\2\2\u00ad\u00ae\7/\2\2\u00ae\u00af\7?\2\2\u00af"+
|
||||||
|
",\3\2\2\2\u00b0\u00b1\7,\2\2\u00b1\u00b2\7?\2\2\u00b2.\3\2\2\2\u00b3\u00b4"+
|
||||||
|
"\7\61\2\2\u00b4\u00b5\7?\2\2\u00b5\60\3\2\2\2\u00b6\u00b7\7-\2\2\u00b7"+
|
||||||
|
"\u00b8\7-\2\2\u00b8\62\3\2\2\2\u00b9\u00ba\7/\2\2\u00ba\u00bb\7/\2\2\u00bb"+
|
||||||
|
"\64\3\2\2\2\u00bc\u00bd\7~\2\2\u00bd\u00be\7~\2\2\u00be\66\3\2\2\2\u00bf"+
|
||||||
|
"\u00c0\7(\2\2\u00c0\u00c1\7(\2\2\u00c18\3\2\2\2\u00c2\u00c3\7>\2\2\u00c3"+
|
||||||
|
"\u00c4\7?\2\2\u00c4:\3\2\2\2\u00c5\u00c6\7>\2\2\u00c6<\3\2\2\2\u00c7\u00c8"+
|
||||||
|
"\7@\2\2\u00c8>\3\2\2\2\u00c9\u00ca\7@\2\2\u00ca\u00cb\7?\2\2\u00cb@\3"+
|
||||||
|
"\2\2\2\u00cc\u00cd\7?\2\2\u00cd\u00ce\7?\2\2\u00ceB\3\2\2\2\u00cf\u00d0"+
|
||||||
|
"\7#\2\2\u00d0\u00d1\7?\2\2\u00d1D\3\2\2\2\u00d2\u00d3\7-\2\2\u00d3F\3"+
|
||||||
|
"\2\2\2\u00d4\u00d5\7/\2\2\u00d5H\3\2\2\2\u00d6\u00d7\7,\2\2\u00d7J\3\2"+
|
||||||
|
"\2\2\u00d8\u00d9\7\61\2\2\u00d9L\3\2\2\2\u00da\u00db\7\'\2\2\u00dbN\3"+
|
||||||
|
"\2\2\2\u00dc\u00dd\7A\2\2\u00ddP\3\2\2\2\u00de\u00df\7v\2\2\u00df\u00e0"+
|
||||||
|
"\7t\2\2\u00e0\u00e1\7w\2\2\u00e1\u00e2\7g\2\2\u00e2R\3\2\2\2\u00e3\u00e4"+
|
||||||
|
"\7h\2\2\u00e4\u00e5\7c\2\2\u00e5\u00e6\7n\2\2\u00e6\u00e7\7u\2\2\u00e7"+
|
||||||
|
"\u00e8\7g\2\2\u00e8T\3\2\2\2\u00e9\u00ee\5c\62\2\u00ea\u00ed\5c\62\2\u00eb"+
|
||||||
|
"\u00ed\5e\63\2\u00ec\u00ea\3\2\2\2\u00ec\u00eb\3\2\2\2\u00ed\u00f0\3\2"+
|
||||||
|
"\2\2\u00ee\u00ec\3\2\2\2\u00ee\u00ef\3\2\2\2\u00efV\3\2\2\2\u00f0\u00ee"+
|
||||||
|
"\3\2\2\2\u00f1\u00f3\5e\63\2\u00f2\u00f1\3\2\2\2\u00f3\u00f4\3\2\2\2\u00f4"+
|
||||||
|
"\u00f2\3\2\2\2\u00f4\u00f5\3\2\2\2\u00f5X\3\2\2\2\u00f6\u00fc\7$\2\2\u00f7"+
|
||||||
|
"\u00f8\7^\2\2\u00f8\u00fb\7$\2\2\u00f9\u00fb\n\2\2\2\u00fa\u00f7\3\2\2"+
|
||||||
|
"\2\u00fa\u00f9\3\2\2\2\u00fb\u00fe\3\2\2\2\u00fc\u00fd\3\2\2\2\u00fc\u00fa"+
|
||||||
|
"\3\2\2\2\u00fd\u00ff\3\2\2\2\u00fe\u00fc\3\2\2\2\u00ff\u0100\7$\2\2\u0100"+
|
||||||
|
"Z\3\2\2\2\u0101\u0105\7$\2\2\u0102\u0103\7^\2\2\u0103\u0106\7$\2\2\u0104"+
|
||||||
|
"\u0106\n\2\2\2\u0105\u0102\3\2\2\2\u0105\u0104\3\2\2\2\u0106\u0107\3\2"+
|
||||||
|
"\2\2\u0107\u0108\7$\2\2\u0108\\\3\2\2\2\u0109\u010a\7#\2\2\u010a^\3\2"+
|
||||||
|
"\2\2\u010b\u010d\t\3\2\2\u010c\u010b\3\2\2\2\u010d\u010e\3\2\2\2\u010e"+
|
||||||
|
"\u010c\3\2\2\2\u010e\u010f\3\2\2\2\u010f\u0110\3\2\2\2\u0110\u0111\b\60"+
|
||||||
|
"\2\2\u0111`\3\2\2\2\u0112\u0113\7\61\2\2\u0113\u0114\7\61\2\2\u0114\u0118"+
|
||||||
|
"\3\2\2\2\u0115\u0117\n\4\2\2\u0116\u0115\3\2\2\2\u0117\u011a\3\2\2\2\u0118"+
|
||||||
|
"\u0116\3\2\2\2\u0118\u0119\3\2\2\2\u0119\u011c\3\2\2\2\u011a\u0118\3\2"+
|
||||||
|
"\2\2\u011b\u011d\7\17\2\2\u011c\u011b\3\2\2\2\u011c\u011d\3\2\2\2\u011d"+
|
||||||
|
"\u011e\3\2\2\2\u011e\u012b\7\f\2\2\u011f\u0120\7\61\2\2\u0120\u0121\7"+
|
||||||
|
",\2\2\u0121\u0125\3\2\2\2\u0122\u0124\13\2\2\2\u0123\u0122\3\2\2\2\u0124"+
|
||||||
|
"\u0127\3\2\2\2\u0125\u0126\3\2\2\2\u0125\u0123\3\2\2\2\u0126\u0128\3\2"+
|
||||||
|
"\2\2\u0127\u0125\3\2\2\2\u0128\u0129\7,\2\2\u0129\u012b\7\61\2\2\u012a"+
|
||||||
|
"\u0112\3\2\2\2\u012a\u011f\3\2\2\2\u012b\u012c\3\2\2\2\u012c\u012d\b\61"+
|
||||||
|
"\2\2\u012db\3\2\2\2\u012e\u012f\t\5\2\2\u012fd\3\2\2\2\u0130\u0131\4\62"+
|
||||||
|
";\2\u0131f\3\2\2\2\16\2\u00ec\u00ee\u00f4\u00fa\u00fc\u0105\u010e\u0118"+
|
||||||
|
"\u011c\u0125\u012a\3\b\2\2";
|
||||||
|
public static final ATN _ATN =
|
||||||
|
new ATNDeserializer().deserialize(_serializedATN.toCharArray());
|
||||||
|
static {
|
||||||
|
_decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
|
||||||
|
for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
|
||||||
|
_decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
90
parser/CminusLexer.tokens
Normal file
90
parser/CminusLexer.tokens
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
T__0=1
|
||||||
|
T__1=2
|
||||||
|
T__2=3
|
||||||
|
T__3=4
|
||||||
|
T__4=5
|
||||||
|
T__5=6
|
||||||
|
T__6=7
|
||||||
|
T__7=8
|
||||||
|
T__8=9
|
||||||
|
T__9=10
|
||||||
|
T__10=11
|
||||||
|
T__11=12
|
||||||
|
T__12=13
|
||||||
|
T__13=14
|
||||||
|
T__14=15
|
||||||
|
T__15=16
|
||||||
|
T__16=17
|
||||||
|
T__17=18
|
||||||
|
T__18=19
|
||||||
|
T__19=20
|
||||||
|
T__20=21
|
||||||
|
T__21=22
|
||||||
|
T__22=23
|
||||||
|
T__23=24
|
||||||
|
T__24=25
|
||||||
|
T__25=26
|
||||||
|
T__26=27
|
||||||
|
T__27=28
|
||||||
|
T__28=29
|
||||||
|
T__29=30
|
||||||
|
T__30=31
|
||||||
|
T__31=32
|
||||||
|
T__32=33
|
||||||
|
T__33=34
|
||||||
|
T__34=35
|
||||||
|
T__35=36
|
||||||
|
T__36=37
|
||||||
|
T__37=38
|
||||||
|
T__38=39
|
||||||
|
T__39=40
|
||||||
|
T__40=41
|
||||||
|
ID=42
|
||||||
|
NUMCONST=43
|
||||||
|
STRINGCONST=44
|
||||||
|
CHARCONST=45
|
||||||
|
BANG=46
|
||||||
|
WS=47
|
||||||
|
COMMENT=48
|
||||||
|
','=1
|
||||||
|
';'=2
|
||||||
|
'['=3
|
||||||
|
']'=4
|
||||||
|
'void'=5
|
||||||
|
'('=6
|
||||||
|
')'=7
|
||||||
|
'int'=8
|
||||||
|
'bool'=9
|
||||||
|
'char'=10
|
||||||
|
'[]'=11
|
||||||
|
'{'=12
|
||||||
|
'}'=13
|
||||||
|
'if'=14
|
||||||
|
'else'=15
|
||||||
|
'while'=16
|
||||||
|
'return'=17
|
||||||
|
'break'=18
|
||||||
|
'='=19
|
||||||
|
'+='=20
|
||||||
|
'-='=21
|
||||||
|
'*='=22
|
||||||
|
'/='=23
|
||||||
|
'++'=24
|
||||||
|
'--'=25
|
||||||
|
'||'=26
|
||||||
|
'&&'=27
|
||||||
|
'<='=28
|
||||||
|
'<'=29
|
||||||
|
'>'=30
|
||||||
|
'>='=31
|
||||||
|
'=='=32
|
||||||
|
'!='=33
|
||||||
|
'+'=34
|
||||||
|
'-'=35
|
||||||
|
'*'=36
|
||||||
|
'/'=37
|
||||||
|
'%'=38
|
||||||
|
'?'=39
|
||||||
|
'true'=40
|
||||||
|
'false'=41
|
||||||
|
'!'=46
|
340
parser/CminusListener.java
Normal file
340
parser/CminusListener.java
Normal file
@ -0,0 +1,340 @@
|
|||||||
|
// Generated from Cminus.g4 by ANTLR 4.9.1
|
||||||
|
package parser;
|
||||||
|
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines a complete listener for a parse tree produced by
|
||||||
|
* {@link CminusParser}.
|
||||||
|
*/
|
||||||
|
public interface CminusListener extends ParseTreeListener {
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#program}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterProgram(CminusParser.ProgramContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#program}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitProgram(CminusParser.ProgramContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#declaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterDeclaration(CminusParser.DeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#declaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitDeclaration(CminusParser.DeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#varDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterVarDeclaration(CminusParser.VarDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#varDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitVarDeclaration(CminusParser.VarDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#varDeclId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterVarDeclId(CminusParser.VarDeclIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#varDeclId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitVarDeclId(CminusParser.VarDeclIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#funDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterFunDeclaration(CminusParser.FunDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#funDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitFunDeclaration(CminusParser.FunDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#typeSpecifier}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterTypeSpecifier(CminusParser.TypeSpecifierContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#typeSpecifier}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitTypeSpecifier(CminusParser.TypeSpecifierContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#param}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterParam(CminusParser.ParamContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#param}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitParam(CminusParser.ParamContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#paramId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterParamId(CminusParser.ParamIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#paramId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitParamId(CminusParser.ParamIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#statement}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterStatement(CminusParser.StatementContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#statement}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitStatement(CminusParser.StatementContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#compoundStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterCompoundStmt(CminusParser.CompoundStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#compoundStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitCompoundStmt(CminusParser.CompoundStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#expressionStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterExpressionStmt(CminusParser.ExpressionStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#expressionStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitExpressionStmt(CminusParser.ExpressionStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#ifStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterIfStmt(CminusParser.IfStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#ifStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitIfStmt(CminusParser.IfStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#whileStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterWhileStmt(CminusParser.WhileStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#whileStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitWhileStmt(CminusParser.WhileStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#returnStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterReturnStmt(CminusParser.ReturnStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#returnStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitReturnStmt(CminusParser.ReturnStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#breakStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterBreakStmt(CminusParser.BreakStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#breakStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitBreakStmt(CminusParser.BreakStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#expression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterExpression(CminusParser.ExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#expression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitExpression(CminusParser.ExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#simpleExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterSimpleExpression(CminusParser.SimpleExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#simpleExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitSimpleExpression(CminusParser.SimpleExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#orExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterOrExpression(CminusParser.OrExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#orExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitOrExpression(CminusParser.OrExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#andExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterAndExpression(CminusParser.AndExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#andExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitAndExpression(CminusParser.AndExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#unaryRelExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#unaryRelExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#relExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterRelExpression(CminusParser.RelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#relExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitRelExpression(CminusParser.RelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#relop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterRelop(CminusParser.RelopContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#relop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitRelop(CminusParser.RelopContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#sumExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterSumExpression(CminusParser.SumExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#sumExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitSumExpression(CminusParser.SumExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#sumop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterSumop(CminusParser.SumopContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#sumop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitSumop(CminusParser.SumopContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#termExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterTermExpression(CminusParser.TermExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#termExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitTermExpression(CminusParser.TermExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#mulop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterMulop(CminusParser.MulopContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#mulop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitMulop(CminusParser.MulopContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#unaryExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterUnaryExpression(CminusParser.UnaryExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#unaryExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitUnaryExpression(CminusParser.UnaryExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#unaryop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterUnaryop(CminusParser.UnaryopContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#unaryop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitUnaryop(CminusParser.UnaryopContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#factor}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterFactor(CminusParser.FactorContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#factor}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitFactor(CminusParser.FactorContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#mutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterMutable(CminusParser.MutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#mutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitMutable(CminusParser.MutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#immutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterImmutable(CminusParser.ImmutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#immutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitImmutable(CminusParser.ImmutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#call}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterCall(CminusParser.CallContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#call}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitCall(CminusParser.CallContext ctx);
|
||||||
|
/**
|
||||||
|
* Enter a parse tree produced by {@link CminusParser#constant}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void enterConstant(CminusParser.ConstantContext ctx);
|
||||||
|
/**
|
||||||
|
* Exit a parse tree produced by {@link CminusParser#constant}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
*/
|
||||||
|
void exitConstant(CminusParser.ConstantContext ctx);
|
||||||
|
}
|
2454
parser/CminusParser.java
Normal file
2454
parser/CminusParser.java
Normal file
File diff suppressed because it is too large
Load Diff
211
parser/CminusVisitor.java
Normal file
211
parser/CminusVisitor.java
Normal file
@ -0,0 +1,211 @@
|
|||||||
|
// Generated from Cminus.g4 by ANTLR 4.9.1
|
||||||
|
package parser;
|
||||||
|
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This interface defines a complete generic visitor for a parse tree produced
|
||||||
|
* by {@link CminusParser}.
|
||||||
|
*
|
||||||
|
* @param <T> The return type of the visit operation. Use {@link Void} for
|
||||||
|
* operations with no return type.
|
||||||
|
*/
|
||||||
|
public interface CminusVisitor<T> extends ParseTreeVisitor<T> {
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#program}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitProgram(CminusParser.ProgramContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#declaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitDeclaration(CminusParser.DeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#varDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitVarDeclaration(CminusParser.VarDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#varDeclId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitVarDeclId(CminusParser.VarDeclIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#funDeclaration}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitFunDeclaration(CminusParser.FunDeclarationContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#typeSpecifier}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitTypeSpecifier(CminusParser.TypeSpecifierContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#param}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitParam(CminusParser.ParamContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#paramId}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitParamId(CminusParser.ParamIdContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#statement}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitStatement(CminusParser.StatementContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#compoundStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitCompoundStmt(CminusParser.CompoundStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#expressionStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitExpressionStmt(CminusParser.ExpressionStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#ifStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitIfStmt(CminusParser.IfStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#whileStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitWhileStmt(CminusParser.WhileStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#returnStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitReturnStmt(CminusParser.ReturnStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#breakStmt}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitBreakStmt(CminusParser.BreakStmtContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#expression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitExpression(CminusParser.ExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#simpleExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitSimpleExpression(CminusParser.SimpleExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#orExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitOrExpression(CminusParser.OrExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#andExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitAndExpression(CminusParser.AndExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#unaryRelExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#relExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitRelExpression(CminusParser.RelExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#relop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitRelop(CminusParser.RelopContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#sumExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitSumExpression(CminusParser.SumExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#sumop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitSumop(CminusParser.SumopContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#termExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitTermExpression(CminusParser.TermExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#mulop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitMulop(CminusParser.MulopContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#unaryExpression}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitUnaryExpression(CminusParser.UnaryExpressionContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#unaryop}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitUnaryop(CminusParser.UnaryopContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#factor}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitFactor(CminusParser.FactorContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#mutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitMutable(CminusParser.MutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#immutable}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitImmutable(CminusParser.ImmutableContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#call}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitCall(CminusParser.CallContext ctx);
|
||||||
|
/**
|
||||||
|
* Visit a parse tree produced by {@link CminusParser#constant}.
|
||||||
|
* @param ctx the parse tree
|
||||||
|
* @return the visitor result
|
||||||
|
*/
|
||||||
|
T visitConstant(CminusParser.ConstantContext ctx);
|
||||||
|
}
|
304
submit/ASTVisitor.java
Normal file
304
submit/ASTVisitor.java
Normal file
@ -0,0 +1,304 @@
|
|||||||
|
package submit;
|
||||||
|
|
||||||
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
|
import parser.CminusBaseVisitor;
|
||||||
|
import parser.CminusParser;
|
||||||
|
import submit.ast.*;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class ASTVisitor extends CminusBaseVisitor<Node> {
|
||||||
|
private final Logger LOGGER;
|
||||||
|
private SymbolTable symbolTable;
|
||||||
|
|
||||||
|
public ASTVisitor(Logger LOGGER) {
|
||||||
|
this.LOGGER = LOGGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SymbolTable getSymbolTable() {
|
||||||
|
return symbolTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
private VarType getVarType(CminusParser.TypeSpecifierContext ctx) {
|
||||||
|
final String t = ctx.getText();
|
||||||
|
return (t.equals("int")) ? VarType.INT : (t.equals("bool")) ? VarType.BOOL : VarType.CHAR;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitProgram(CminusParser.ProgramContext ctx) {
|
||||||
|
symbolTable = new SymbolTable();
|
||||||
|
List<Declaration> decls = new ArrayList<>();
|
||||||
|
for (CminusParser.DeclarationContext d : ctx.declaration()) {
|
||||||
|
decls.add((Declaration) visitDeclaration(d));
|
||||||
|
}
|
||||||
|
return new Program(decls);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitVarDeclaration(CminusParser.VarDeclarationContext ctx) {
|
||||||
|
VarType type = getVarType(ctx.typeSpecifier());
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
|
List<Integer> arraySizes = new ArrayList<>();
|
||||||
|
for (CminusParser.VarDeclIdContext v : ctx.varDeclId()) {
|
||||||
|
String id = v.ID().getText();
|
||||||
|
ids.add(id);
|
||||||
|
symbolTable.addSymbol(id, new SymbolInfo(id, type, false));
|
||||||
|
if (v.NUMCONST() != null) {
|
||||||
|
arraySizes.add(Integer.parseInt(v.NUMCONST().getText()));
|
||||||
|
} else {
|
||||||
|
arraySizes.add(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final boolean isStatic = false;
|
||||||
|
return new VarDeclaration(type, ids, arraySizes, isStatic);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitFunDeclaration(CminusParser.FunDeclarationContext ctx) {
|
||||||
|
VarType returnType = null;
|
||||||
|
if (ctx.typeSpecifier() != null) {
|
||||||
|
returnType = getVarType(ctx.typeSpecifier());
|
||||||
|
}
|
||||||
|
String id = ctx.ID().getText();
|
||||||
|
List<Param> params = new ArrayList<>();
|
||||||
|
for (CminusParser.ParamContext p : ctx.param()) {
|
||||||
|
params.add((Param) visitParam(p));
|
||||||
|
}
|
||||||
|
Statement statement = (Statement) visitStatement(ctx.statement());
|
||||||
|
symbolTable.addSymbol(id, new SymbolInfo(id, returnType, true));
|
||||||
|
return new FunDeclaration(returnType, id, params, statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitParam(CminusParser.ParamContext ctx) {
|
||||||
|
VarType type = getVarType(ctx.typeSpecifier());
|
||||||
|
String id = ctx.paramId().ID().getText();
|
||||||
|
symbolTable.addSymbol(id, new SymbolInfo(id, type, false));
|
||||||
|
return new Param(type, id, ctx.paramId().children.size() > 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitCompoundStmt(CminusParser.CompoundStmtContext ctx) {
|
||||||
|
symbolTable = symbolTable.createChild();
|
||||||
|
List<Statement> statements = new ArrayList<>();
|
||||||
|
for (CminusParser.VarDeclarationContext d : ctx.varDeclaration()) {
|
||||||
|
statements.add((VarDeclaration) visitVarDeclaration(d));
|
||||||
|
}
|
||||||
|
for (CminusParser.StatementContext d : ctx.statement()) {
|
||||||
|
statements.add((Statement) visitStatement(d));
|
||||||
|
}
|
||||||
|
symbolTable = symbolTable.getParent();
|
||||||
|
return new CompoundStatement(statements);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitExpressionStmt(CminusParser.ExpressionStmtContext ctx) {
|
||||||
|
if (ctx.expression() == null) {
|
||||||
|
return Statement.empty();
|
||||||
|
}
|
||||||
|
return new ExpressionStatement((Expression) visitExpression(ctx.expression()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitIfStmt(CminusParser.IfStmtContext ctx) {
|
||||||
|
Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression());
|
||||||
|
Statement trueStatement = (Statement) visitStatement(ctx.statement(0));
|
||||||
|
Statement falseStatement = null;
|
||||||
|
if (ctx.statement().size() > 1) {
|
||||||
|
falseStatement = (Statement) visitStatement(ctx.statement(1));
|
||||||
|
}
|
||||||
|
return new If(expression, trueStatement, falseStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitWhileStmt(CminusParser.WhileStmtContext ctx) {
|
||||||
|
Expression expression = (Expression) visitSimpleExpression(ctx.simpleExpression());
|
||||||
|
Statement statement = (Statement) visitStatement(ctx.statement());
|
||||||
|
return new While(expression, statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitReturnStmt(CminusParser.ReturnStmtContext ctx) {
|
||||||
|
if (ctx.expression() != null) {
|
||||||
|
return new Return((Expression) visitExpression(ctx.expression()));
|
||||||
|
}
|
||||||
|
return new Return(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitBreakStmt(CminusParser.BreakStmtContext ctx) {
|
||||||
|
return new Break();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitExpression(CminusParser.ExpressionContext ctx) {
|
||||||
|
final Node ret;
|
||||||
|
CminusParser.MutableContext mutable = ctx.mutable();
|
||||||
|
CminusParser.ExpressionContext expression = ctx.expression();
|
||||||
|
if (mutable != null) {
|
||||||
|
// Assignment
|
||||||
|
ParseTree operator = ctx.getChild(1);
|
||||||
|
Mutable lhs = (Mutable) visitMutable(mutable);// new Mutable(mutable.ID().getText(), (Expression)
|
||||||
|
// visitExpression(mutable.expression()));
|
||||||
|
Expression rhs = null;
|
||||||
|
if (expression != null) {
|
||||||
|
rhs = (Expression) visitExpression(expression);
|
||||||
|
}
|
||||||
|
ret = new Assignment(lhs, operator.getText(), rhs);
|
||||||
|
} else {
|
||||||
|
ret = visitSimpleExpression(ctx.simpleExpression());
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitOrExpression(CminusParser.OrExpressionContext ctx) {
|
||||||
|
List<Node> ands = new ArrayList<>();
|
||||||
|
for (CminusParser.AndExpressionContext and : ctx.andExpression()) {
|
||||||
|
ands.add(visitAndExpression(and));
|
||||||
|
}
|
||||||
|
if (ands.size() == 1) {
|
||||||
|
return ands.get(0);
|
||||||
|
}
|
||||||
|
BinaryOperator op = new BinaryOperator((Expression) ands.get(0), "||", (Expression) ands.get(1));
|
||||||
|
for (int i = 2; i < ands.size(); ++i) {
|
||||||
|
op = new BinaryOperator(op, "||", (Expression) ands.get(i));
|
||||||
|
}
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitAndExpression(CminusParser.AndExpressionContext ctx) {
|
||||||
|
List<Node> uns = new ArrayList<>();
|
||||||
|
for (CminusParser.UnaryRelExpressionContext un : ctx.unaryRelExpression()) {
|
||||||
|
uns.add(visitUnaryRelExpression(un));
|
||||||
|
}
|
||||||
|
if (uns.size() == 1) {
|
||||||
|
return uns.get(0);
|
||||||
|
}
|
||||||
|
BinaryOperator op = new BinaryOperator((Expression) uns.get(0), "&&", (Expression) uns.get(1));
|
||||||
|
for (int i = 2; i < uns.size(); ++i) {
|
||||||
|
op = new BinaryOperator(op, "&&", (Expression) uns.get(i));
|
||||||
|
}
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitUnaryRelExpression(CminusParser.UnaryRelExpressionContext ctx) {
|
||||||
|
Expression e = (Expression) visitRelExpression(ctx.relExpression());
|
||||||
|
for (TerminalNode n : ctx.BANG()) {
|
||||||
|
e = new UnaryOperator("!", e);
|
||||||
|
}
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitRelExpression(CminusParser.RelExpressionContext ctx) {
|
||||||
|
List<Node> uns = new ArrayList<>();
|
||||||
|
for (CminusParser.SumExpressionContext un : ctx.sumExpression()) {
|
||||||
|
uns.add(visitSumExpression(un));
|
||||||
|
}
|
||||||
|
if (uns.size() == 1) {
|
||||||
|
return uns.get(0);
|
||||||
|
}
|
||||||
|
BinaryOperator op = new BinaryOperator((Expression) uns.get(0), ctx.relop(0).getText(),
|
||||||
|
(Expression) uns.get(1));
|
||||||
|
for (int i = 2; i < uns.size(); ++i) {
|
||||||
|
op = new BinaryOperator(op, ctx.relop(i - 1).getText(), (Expression) uns.get(i));
|
||||||
|
}
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitSumExpression(CminusParser.SumExpressionContext ctx) {
|
||||||
|
List<Node> es = new ArrayList<>();
|
||||||
|
for (CminusParser.TermExpressionContext e : ctx.termExpression()) {
|
||||||
|
es.add(visitTermExpression(e));
|
||||||
|
}
|
||||||
|
if (es.size() == 1) {
|
||||||
|
return es.get(0);
|
||||||
|
}
|
||||||
|
BinaryOperator op = new BinaryOperator((Expression) es.get(0), ctx.sumop(0).getText(), (Expression) es.get(1));
|
||||||
|
for (int i = 2; i < es.size(); ++i) {
|
||||||
|
op = new BinaryOperator(op, ctx.sumop(i - 1).getText(), (Expression) es.get(i));
|
||||||
|
}
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitTermExpression(CminusParser.TermExpressionContext ctx) {
|
||||||
|
List<Node> es = new ArrayList<>();
|
||||||
|
for (CminusParser.UnaryExpressionContext e : ctx.unaryExpression()) {
|
||||||
|
es.add(visitUnaryExpression(e));
|
||||||
|
}
|
||||||
|
if (es.size() == 1) {
|
||||||
|
return es.get(0);
|
||||||
|
}
|
||||||
|
BinaryOperator op = new BinaryOperator((Expression) es.get(0), ctx.mulop(0).getText(), (Expression) es.get(1));
|
||||||
|
for (int i = 2; i < es.size(); ++i) {
|
||||||
|
op = new BinaryOperator(op, ctx.mulop(i - 1).getText(), (Expression) es.get(i));
|
||||||
|
}
|
||||||
|
return op;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitUnaryExpression(CminusParser.UnaryExpressionContext ctx) {
|
||||||
|
Node ret = visitFactor(ctx.factor());
|
||||||
|
for (int i = ctx.unaryop().size() - 1; i >= 0; i--) {
|
||||||
|
ret = new UnaryOperator(ctx.unaryop(i).getText(), (Expression) ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitMutable(CminusParser.MutableContext ctx) {
|
||||||
|
Expression e = null;
|
||||||
|
if (ctx.expression() != null) {
|
||||||
|
e = (Expression) visitExpression(ctx.expression());
|
||||||
|
}
|
||||||
|
String id = ctx.ID().getText();
|
||||||
|
if (symbolTable.find(id) == null) {
|
||||||
|
LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id);
|
||||||
|
}
|
||||||
|
return new Mutable(id, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitImmutable(CminusParser.ImmutableContext ctx) {
|
||||||
|
if (ctx.expression() != null) {
|
||||||
|
return new ParenExpression((Expression) visitExpression(ctx.expression()));
|
||||||
|
}
|
||||||
|
return visitChildren(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitCall(CminusParser.CallContext ctx) {
|
||||||
|
final String id = ctx.ID().getText();
|
||||||
|
final List<Expression> args = new ArrayList<>();
|
||||||
|
for (CminusParser.ExpressionContext e : ctx.expression()) {
|
||||||
|
args.add((Expression) visitExpression(e));
|
||||||
|
}
|
||||||
|
if (symbolTable.find(id) == null) {
|
||||||
|
LOGGER.warning("Undefined symbol on line " + ctx.getStart().getLine() + ": " + id);
|
||||||
|
}
|
||||||
|
return new Call(id, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node visitConstant(CminusParser.ConstantContext ctx) {
|
||||||
|
final Node node;
|
||||||
|
if (ctx.NUMCONST() != null) {
|
||||||
|
node = new NumConstant(Integer.parseInt(ctx.NUMCONST().getText()));
|
||||||
|
} else if (ctx.CHARCONST() != null) {
|
||||||
|
node = new CharConstant(ctx.CHARCONST().getText().charAt(0));
|
||||||
|
} else if (ctx.STRINGCONST() != null) {
|
||||||
|
node = new StringConstant(ctx.STRINGCONST().getText());
|
||||||
|
} else {
|
||||||
|
node = new BoolConstant(ctx.getText().equals("true"));
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
}
|
61
submit/MIPSResult.java
Normal file
61
submit/MIPSResult.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
*/
|
||||||
|
package submit;
|
||||||
|
|
||||||
|
import submit.ast.VarType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class represents the result of generating MIPS code. There are various
|
||||||
|
* forms this result can take: 1) void, for cases where the calling node doesn't
|
||||||
|
* need any information returned, such as a return statement. 2) register, for
|
||||||
|
* cases where the called node needs to inform the calling node what register a
|
||||||
|
* result is placed in, such as BinaryOperator. 3) address, for cases where the
|
||||||
|
* returning result is in memory, such as StringConstant.
|
||||||
|
*
|
||||||
|
* To instantiate a MIPSResult object use the factory methods create...().
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MIPSResult {
|
||||||
|
|
||||||
|
private final String register;
|
||||||
|
private final String address;
|
||||||
|
private final VarType type;
|
||||||
|
|
||||||
|
public static MIPSResult createVoidResult() {
|
||||||
|
return new MIPSResult(null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MIPSResult createRegisterResult(String register, VarType type) {
|
||||||
|
return new MIPSResult(register, null, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MIPSResult createAddressResult(String address, VarType type) {
|
||||||
|
return new MIPSResult(null, address, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private MIPSResult(String register, String address, VarType type) {
|
||||||
|
this.register = register;
|
||||||
|
this.address = address;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Anytime you get a register from a result you should seriously consider
|
||||||
|
* calling regAllocator.clear(reg) after using the register to minimize
|
||||||
|
* register usage.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getRegister() {
|
||||||
|
return register;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VarType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
126
submit/RegisterAllocator.java
Normal file
126
submit/RegisterAllocator.java
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
*/
|
||||||
|
package submit;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public final class RegisterAllocator {
|
||||||
|
|
||||||
|
// True if t is used
|
||||||
|
private final boolean[] t = new boolean[10];
|
||||||
|
private final boolean[] s = new boolean[8];
|
||||||
|
private final Set<String> used = new HashSet<>();
|
||||||
|
|
||||||
|
public RegisterAllocator() {
|
||||||
|
clearAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getT() {
|
||||||
|
for (int i = 0; i < t.length; ++i) {
|
||||||
|
if (!t[i]) {
|
||||||
|
t[i] = true;
|
||||||
|
String str = "$t" + i;
|
||||||
|
used.add(str);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public String getS() {
|
||||||
|
// for (int i = 0; i < s.length; ++i) {
|
||||||
|
// if (!s[i]) {
|
||||||
|
// s[i] = true;
|
||||||
|
// String str = "$s" + i;
|
||||||
|
// used.add(str);
|
||||||
|
// return str;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Returns the number of bytes used to save the registers
|
||||||
|
public int saveRestore(StringBuilder code, int baseOffset, boolean s_or_t, boolean save) {
|
||||||
|
boolean[] r = s;
|
||||||
|
String prefix = "$s";
|
||||||
|
if (!s_or_t) {
|
||||||
|
r = t;
|
||||||
|
prefix = "$t";
|
||||||
|
}
|
||||||
|
String instruction = "sw";
|
||||||
|
if (!save) {
|
||||||
|
instruction = "lw";
|
||||||
|
}
|
||||||
|
int offset = 0;
|
||||||
|
for (int i = 0; i < r.length; ++i) {
|
||||||
|
if (r[i]) {
|
||||||
|
offset -= 4;
|
||||||
|
String str = prefix + i;
|
||||||
|
code.append(instruction).append(" ").append(str).append(" ").append(offset-baseOffset).append("($sp)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
// public int saveS(StringBuilder code, int baseOffset) {
|
||||||
|
// return saveRestore(code, baseOffset, true, true);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public int saveT(StringBuilder code, int baseOffset) {
|
||||||
|
return saveRestore(code, baseOffset, false, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// public int restoreS(StringBuilder code, int baseOffset) {
|
||||||
|
// return saveRestore(code, baseOffset, true, false);
|
||||||
|
// }
|
||||||
|
|
||||||
|
public int restoreT(StringBuilder code, int baseOffset) {
|
||||||
|
return saveRestore(code, baseOffset, false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getUsed() {
|
||||||
|
return new ArrayList<>(used);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Any time you call this method you should seriously consider adding a
|
||||||
|
* corresponding clear() call.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getAny() {
|
||||||
|
// String availS = getS();
|
||||||
|
// if (availS != null) {
|
||||||
|
// return availS;
|
||||||
|
// }
|
||||||
|
String t = getT();
|
||||||
|
if (t == null) {
|
||||||
|
throw new RuntimeException("Out of registers");
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear(String reg) {
|
||||||
|
if (reg.charAt(1) == 's') {
|
||||||
|
s[Integer.parseInt(reg.substring(2))] = false;
|
||||||
|
} else if (reg.charAt(1) == 't') {
|
||||||
|
t[Integer.parseInt(reg.substring(2))] = false;
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Unexpected register in clear");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearAll() {
|
||||||
|
Arrays.fill(t, false);
|
||||||
|
Arrays.fill(s, false);
|
||||||
|
}
|
||||||
|
}
|
31
submit/SymbolInfo.java
Normal file
31
submit/SymbolInfo.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit;
|
||||||
|
|
||||||
|
import submit.ast.VarType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class SymbolInfo {
|
||||||
|
|
||||||
|
private final String id;
|
||||||
|
// In the case of a function, type is the return type
|
||||||
|
private final VarType type;
|
||||||
|
private final boolean function;
|
||||||
|
|
||||||
|
public SymbolInfo(String id, VarType type, boolean function) {
|
||||||
|
this.id = id;
|
||||||
|
this.type = type;
|
||||||
|
this.function = function;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "<" + id + ", " + type + '>';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
63
submit/SymbolTable.java
Normal file
63
submit/SymbolTable.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
package submit;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class SymbolTable {
|
||||||
|
|
||||||
|
private final HashMap<String, SymbolInfo> table;
|
||||||
|
private SymbolTable parent;
|
||||||
|
private final List<SymbolTable> children;
|
||||||
|
|
||||||
|
public SymbolTable() {
|
||||||
|
table = new HashMap<>();
|
||||||
|
parent = null;
|
||||||
|
children = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addSymbol(String id, SymbolInfo symbol) {
|
||||||
|
table.put(id, symbol);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns null if no symbol with that id is in this symbol table or an
|
||||||
|
* ancestor table.
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public SymbolInfo find(String id) {
|
||||||
|
if (table.containsKey(id)) {
|
||||||
|
return table.get(id);
|
||||||
|
}
|
||||||
|
if (parent != null) {
|
||||||
|
return parent.find(id);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the new child.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public SymbolTable createChild() {
|
||||||
|
SymbolTable child = new SymbolTable();
|
||||||
|
children.add(child);
|
||||||
|
child.parent = this;
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SymbolTable getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
submit/ast/AbstractNode.java
Normal file
15
submit/ast/AbstractNode.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import submit.MIPSResult;
|
||||||
|
import submit.RegisterAllocator;
|
||||||
|
import submit.SymbolTable;
|
||||||
|
|
||||||
|
public class AbstractNode implements Node {
|
||||||
|
public void toCminus(StringBuilder sb, String prefix) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public MIPSResult toMIPS(StringBuilder code, StringBuilder data, SymbolTable symbolTable,
|
||||||
|
RegisterAllocator regAllocator) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
33
submit/ast/Assignment.java
Normal file
33
submit/ast/Assignment.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Assignment extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final Mutable mutable;
|
||||||
|
private final AssignmentType type;
|
||||||
|
private final Expression rhs;
|
||||||
|
|
||||||
|
public Assignment(Mutable mutable, String assign, Expression rhs) {
|
||||||
|
this.mutable = mutable;
|
||||||
|
this.type = AssignmentType.fromString(assign);
|
||||||
|
this.rhs = rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
mutable.toCminus(builder, prefix);
|
||||||
|
if (rhs != null) {
|
||||||
|
builder.append(" ").append(type.toString()).append(" ");
|
||||||
|
rhs.toCminus(builder, prefix);
|
||||||
|
} else {
|
||||||
|
builder.append(type.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
submit/ast/AssignmentType.java
Normal file
35
submit/ast/AssignmentType.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public enum AssignmentType {
|
||||||
|
|
||||||
|
EQUALS("="), PLUS("+="), MINUS("-="), TIMES("*="), DIVIDE("/="), INC("++"), DEC("--");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private AssignmentType(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AssignmentType fromString(String s) {
|
||||||
|
for (AssignmentType at : AssignmentType.values()) {
|
||||||
|
if (at.value.equals(s)) {
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Illegal string in AssignType.fromString()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
submit/ast/BinaryOperator.java
Normal file
35
submit/ast/BinaryOperator.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class BinaryOperator extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final Expression lhs, rhs;
|
||||||
|
private final BinaryOperatorType type;
|
||||||
|
|
||||||
|
public BinaryOperator(Expression lhs, BinaryOperatorType type, Expression rhs) {
|
||||||
|
this.lhs = lhs;
|
||||||
|
this.type = type;
|
||||||
|
this.rhs = rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BinaryOperator(Expression lhs, String type, Expression rhs) {
|
||||||
|
this.lhs = lhs;
|
||||||
|
this.type = BinaryOperatorType.fromString(type);
|
||||||
|
this.rhs = rhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
lhs.toCminus(builder, prefix);
|
||||||
|
builder.append(" ").append(type).append(" ");
|
||||||
|
rhs.toCminus(builder, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
37
submit/ast/BinaryOperatorType.java
Normal file
37
submit/ast/BinaryOperatorType.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public enum BinaryOperatorType {
|
||||||
|
|
||||||
|
OR("||"), AND("&&"),
|
||||||
|
LE("<="), LT("<"), GT(">"), GE(">="), EQ("=="), NE("!="),
|
||||||
|
PLUS("+"), MINUS("-"), TIMES("*"), DIVIDE("/"), MOD("%");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private BinaryOperatorType(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BinaryOperatorType fromString(String s) {
|
||||||
|
for (BinaryOperatorType at : BinaryOperatorType.values()) {
|
||||||
|
if (at.value.equals(s)) {
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Illegal string in OperatorType.fromString(): " + s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
submit/ast/BoolConstant.java
Normal file
27
submit/ast/BoolConstant.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class BoolConstant extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final boolean value;
|
||||||
|
|
||||||
|
public BoolConstant(boolean value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
if (value) {
|
||||||
|
builder.append("true");
|
||||||
|
} else {
|
||||||
|
builder.append("false");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
18
submit/ast/Break.java
Normal file
18
submit/ast/Break.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Break extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix).append("break;\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
37
submit/ast/Call.java
Normal file
37
submit/ast/Call.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Call extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final String id;
|
||||||
|
private final List<Expression> args;
|
||||||
|
|
||||||
|
public Call(String id, List<Expression> args) {
|
||||||
|
this.id = id;
|
||||||
|
this.args = new ArrayList<>(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(id).append("(");
|
||||||
|
for (Expression arg : args) {
|
||||||
|
arg.toCminus(builder, prefix);
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
if (!args.isEmpty()) {
|
||||||
|
builder.setLength(builder.length() - 2);
|
||||||
|
}
|
||||||
|
builder.append(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
submit/ast/CharConstant.java
Normal file
23
submit/ast/CharConstant.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class CharConstant extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final char value;
|
||||||
|
|
||||||
|
public CharConstant(char value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
builder.append("'").append(value).append("'");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
30
submit/ast/CompoundStatement.java
Normal file
30
submit/ast/CompoundStatement.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class CompoundStatement extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
private final List<Statement> statements;
|
||||||
|
|
||||||
|
public CompoundStatement(List<Statement> statements) {
|
||||||
|
this.statements = statements;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix).append("{\n");
|
||||||
|
for (Statement s : statements) {
|
||||||
|
s.toCminus(builder, prefix + " ");
|
||||||
|
}
|
||||||
|
builder.append(prefix).append("}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
submit/ast/Declaration.java
Normal file
13
submit/ast/Declaration.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public interface Declaration extends Statement {
|
||||||
|
|
||||||
|
}
|
13
submit/ast/Expression.java
Normal file
13
submit/ast/Expression.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public interface Expression extends Node {
|
||||||
|
|
||||||
|
}
|
26
submit/ast/ExpressionStatement.java
Normal file
26
submit/ast/ExpressionStatement.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class ExpressionStatement extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
private final Expression expression;
|
||||||
|
|
||||||
|
public ExpressionStatement(Expression expression) {
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix);
|
||||||
|
expression.toCminus(builder, prefix);
|
||||||
|
builder.append(";\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
46
submit/ast/FunDeclaration.java
Normal file
46
submit/ast/FunDeclaration.java
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class FunDeclaration extends AbstractNode implements Declaration {
|
||||||
|
|
||||||
|
private final VarType returnType;
|
||||||
|
private final String id;
|
||||||
|
private final ArrayList<Param> params;
|
||||||
|
private final Statement statement;
|
||||||
|
|
||||||
|
public FunDeclaration(VarType returnType, String id, List<Param> params,
|
||||||
|
Statement statement) {
|
||||||
|
this.returnType = returnType;
|
||||||
|
this.id = id;
|
||||||
|
this.params = new ArrayList<>(params);
|
||||||
|
this.statement = statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
String rt = (returnType != null) ? returnType.toString() : "void";
|
||||||
|
builder.append("\n").append(rt).append(" ");
|
||||||
|
builder.append(id);
|
||||||
|
builder.append("(");
|
||||||
|
|
||||||
|
for (Param param : params) {
|
||||||
|
param.toCminus(builder, prefix);
|
||||||
|
builder.append(", ");
|
||||||
|
}
|
||||||
|
if (!params.isEmpty()) {
|
||||||
|
builder.delete(builder.length() - 2, builder.length());
|
||||||
|
}
|
||||||
|
builder.append(")\n");
|
||||||
|
statement.toCminus(builder, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
44
submit/ast/If.java
Normal file
44
submit/ast/If.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class If extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
private final Expression expression;
|
||||||
|
private final Statement trueStatement;
|
||||||
|
private final Statement falseStatement;
|
||||||
|
|
||||||
|
public If(Expression expression, Statement trueStatement, Statement falseStatement) {
|
||||||
|
this.expression = expression;
|
||||||
|
this.trueStatement = trueStatement;
|
||||||
|
this.falseStatement = falseStatement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix).append("if (");
|
||||||
|
expression.toCminus(builder, prefix);
|
||||||
|
builder.append(")\n");
|
||||||
|
if (trueStatement instanceof CompoundStatement) {
|
||||||
|
trueStatement.toCminus(builder, prefix);
|
||||||
|
} else {
|
||||||
|
trueStatement.toCminus(builder, prefix + " ");
|
||||||
|
}
|
||||||
|
if (falseStatement != null) {
|
||||||
|
builder.append(prefix).append("else\n");
|
||||||
|
// falseStatement.toCminus(builder, prefix);
|
||||||
|
if (falseStatement instanceof CompoundStatement) {
|
||||||
|
falseStatement.toCminus(builder, prefix);
|
||||||
|
} else {
|
||||||
|
falseStatement.toCminus(builder, prefix + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// builder.append(prefix).append("}");
|
||||||
|
}
|
||||||
|
}
|
31
submit/ast/Mutable.java
Normal file
31
submit/ast/Mutable.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Mutable extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final String id;
|
||||||
|
private final Expression index;
|
||||||
|
|
||||||
|
public Mutable(String id, Expression index) {
|
||||||
|
this.id = id;
|
||||||
|
this.index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(id);
|
||||||
|
if (index != null) {
|
||||||
|
builder.append("[");
|
||||||
|
index.toCminus(builder, prefix);
|
||||||
|
builder.append("]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
11
submit/ast/Node.java
Normal file
11
submit/ast/Node.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import submit.MIPSResult;
|
||||||
|
import submit.RegisterAllocator;
|
||||||
|
import submit.SymbolTable;
|
||||||
|
|
||||||
|
public interface Node {
|
||||||
|
void toCminus(StringBuilder builder, final String prefix);
|
||||||
|
|
||||||
|
MIPSResult toMIPS(StringBuilder code, StringBuilder data, SymbolTable symbolTable, RegisterAllocator regAllocator);
|
||||||
|
}
|
23
submit/ast/NumConstant.java
Normal file
23
submit/ast/NumConstant.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class NumConstant extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final int value;
|
||||||
|
|
||||||
|
public NumConstant(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
builder.append(Integer.toString(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
43
submit/ast/Param.java
Normal file
43
submit/ast/Param.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Param extends AbstractNode implements Node {
|
||||||
|
|
||||||
|
private final VarType type;
|
||||||
|
private final String id;
|
||||||
|
private final boolean array;
|
||||||
|
|
||||||
|
public Param(VarType type, String id, boolean array) {
|
||||||
|
this.type = type;
|
||||||
|
this.id = id;
|
||||||
|
this.array = array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public VarType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isArray() {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
if (isArray()) {
|
||||||
|
builder.append(type).append(" ").append(id).append("[]");
|
||||||
|
} else {
|
||||||
|
builder.append(type).append(" ").append(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
26
submit/ast/ParenExpression.java
Normal file
26
submit/ast/ParenExpression.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class ParenExpression extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final Expression expression;
|
||||||
|
|
||||||
|
public ParenExpression(Expression expression) {
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append("(");
|
||||||
|
expression.toCminus(builder, prefix);
|
||||||
|
builder.append(")");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
submit/ast/Program.java
Normal file
36
submit/ast/Program.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Program extends AbstractNode implements Node {
|
||||||
|
|
||||||
|
private ArrayList<Declaration> declarations;
|
||||||
|
|
||||||
|
public Program(List<Declaration> declarations) {
|
||||||
|
this.declarations = new ArrayList<>(declarations);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
toCminus(builder, "");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
for (Declaration declaration : declarations) {
|
||||||
|
declaration.toCminus(builder, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
31
submit/ast/Return.java
Normal file
31
submit/ast/Return.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class Return extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
private final Expression expr;
|
||||||
|
|
||||||
|
public Return(Expression expr) {
|
||||||
|
this.expr = expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix);
|
||||||
|
if (expr == null) {
|
||||||
|
builder.append("return;\n");
|
||||||
|
} else {
|
||||||
|
builder.append("return ");
|
||||||
|
expr.toCminus(builder, prefix);
|
||||||
|
builder.append(";\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
16
submit/ast/Statement.java
Normal file
16
submit/ast/Statement.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public interface Statement extends Node {
|
||||||
|
public static CompoundStatement empty() { return new CompoundStatement(new ArrayList<>()); }
|
||||||
|
|
||||||
|
}
|
23
submit/ast/StringConstant.java
Normal file
23
submit/ast/StringConstant.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class StringConstant extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public StringConstant(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
builder.append("\"").append(value).append("\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
27
submit/ast/UnaryOperator.java
Normal file
27
submit/ast/UnaryOperator.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class UnaryOperator extends AbstractNode implements Expression {
|
||||||
|
|
||||||
|
private final UnaryOperatorType type;
|
||||||
|
private final Expression expression;
|
||||||
|
|
||||||
|
public UnaryOperator(String type, Expression expression) {
|
||||||
|
this.type = UnaryOperatorType.fromString(type);
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(type);
|
||||||
|
expression.toCminus(builder, prefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
submit/ast/UnaryOperatorType.java
Normal file
35
submit/ast/UnaryOperatorType.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public enum UnaryOperatorType {
|
||||||
|
|
||||||
|
NOT("!"), NEG("-"), DEREF("*"), QUESTION("?");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private UnaryOperatorType(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UnaryOperatorType fromString(String s) {
|
||||||
|
for (UnaryOperatorType at : UnaryOperatorType.values()) {
|
||||||
|
if (at.value.equals(s)) {
|
||||||
|
return at;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Illegal string in UnaryOperatorType.fromString()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
submit/ast/VarDeclaration.java
Normal file
47
submit/ast/VarDeclaration.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class VarDeclaration extends AbstractNode implements Declaration {
|
||||||
|
|
||||||
|
private final VarType type;
|
||||||
|
private final List<String> ids;
|
||||||
|
private final List<Integer> arraySizes;
|
||||||
|
private final boolean isStatic;
|
||||||
|
|
||||||
|
public VarDeclaration(VarType type, List<String> ids, List<Integer> arraySizes, boolean isStatic) {
|
||||||
|
this.type = type;
|
||||||
|
this.ids = new ArrayList<>(ids);
|
||||||
|
this.arraySizes = arraySizes;
|
||||||
|
this.isStatic = isStatic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void toCminus(StringBuilder builder, final String prefix) {
|
||||||
|
builder.append(prefix);
|
||||||
|
if (isStatic) {
|
||||||
|
builder.append("static ");
|
||||||
|
}
|
||||||
|
builder.append(type).append(" ");
|
||||||
|
for (int i = 0; i < ids.size(); ++i) {
|
||||||
|
final String id = ids.get(i);
|
||||||
|
final int arraySize = arraySizes.get(i);
|
||||||
|
if (arraySize >= 0) {
|
||||||
|
builder.append(id).append("[").append(arraySize).append("]").append(", ");
|
||||||
|
} else {
|
||||||
|
builder.append(id).append(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.delete(builder.length() - 2, builder.length());
|
||||||
|
builder.append(";\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
35
submit/ast/VarType.java
Normal file
35
submit/ast/VarType.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public enum VarType {
|
||||||
|
|
||||||
|
INT("int"), BOOL("bool"), CHAR("char");
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
private VarType(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static VarType fromString(String s) {
|
||||||
|
for (VarType vt : VarType.values()) {
|
||||||
|
if (vt.value.equals(s)) {
|
||||||
|
return vt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Illegal string in VarType.fromString()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
33
submit/ast/While.java
Normal file
33
submit/ast/While.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Code formatter project
|
||||||
|
* CS 4481
|
||||||
|
*/
|
||||||
|
package submit.ast;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author edwajohn
|
||||||
|
*/
|
||||||
|
public class While extends AbstractNode implements Statement {
|
||||||
|
|
||||||
|
private final Expression expression;
|
||||||
|
private final Statement statement;
|
||||||
|
|
||||||
|
public While(Expression expression, Statement statement) {
|
||||||
|
this.expression = expression;
|
||||||
|
this.statement = statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toCminus(StringBuilder builder, String prefix) {
|
||||||
|
builder.append(prefix).append("while (");
|
||||||
|
expression.toCminus(builder, prefix);
|
||||||
|
builder.append(")\n");
|
||||||
|
if (statement instanceof CompoundStatement) {
|
||||||
|
statement.toCminus(builder, prefix);
|
||||||
|
} else {
|
||||||
|
statement.toCminus(builder, prefix + " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user