self.label(".data\n");
self.label("data_stack:");
self.line(format!(".space {}", self.data_stack_size));
- self.label("data_stack_end:");
+ self.label(".globl data_stack_end\ndata_stack_end:\n");
// Code
self.label(".text\n");
if name == "main" {
self.label(".globl _start"); // TODO is globl necessary?
self.label("_start:");
- self.line("la s2, data_stack_end"); // set stack pointer
+ self.line("la s2, data_stack_end # set initial data stack pointer");
} else {
self.label(format!(".globl {}", name));
self.label(format!("{}:", name));
}
- self.line("addi sp, sp, -16"); // allocate 16 bytes on stack
- self.line("sw ra, 8(sp)"); // store return address on stack
+ self.line("addi sp, sp, -16 # allocate 16 bytes on stack"); // allocate 16 bytes on stack
+ self.line("sw ra, 8(sp) # store return address on stack"); // store return address on stack
},
IR::Call(name) => {
+ self.label(format!("# call {}", name));
+
self.line(format!("call {}", name));
},
IR::Ret => {
if last_label == "main" {
- // exit 0 syscall
+ self.label("# exit 0 syscall");
self.line("li a7, 93");
self.line("mv a0, x0");
self.line("ecall");
self.move_stack_ptr_by_cells(2);
},
IR::StackPush(num) => {
+ self.label(format!("# stackpush {}", num));
self.line(format!("li t0, {}", num));
self.push_from("t0");
},
IR::StackPushString(name) => {
+ self.label(format!("# stackpushstring {}", name));
self.line(format!("li t0, {}", name));
self.push_from("t0");
},
IR::AddU64 => {
+ self.label("# add");
self.pop_call_push("t0 t1", "add t0, t0, t1", "t0");
},
IR::SubtractU64 => {
+ self.label("# sub");
self.pop_call_push("t0 t1", "sub t0, t0, t1", "t0");
},
IR::MultiplyU64 => {
self.pop_call_push("t0 t1", "rem t0, t0, t1", "t0");
},
IR::Dup => {
+ self.label("# dup");
self.copy_top_stack_value_to("t0");
self.push_from("t0");
},
IR::Swap => {
+ self.label("# swap");
self.pop_some_to("t1 t0");
self.push_from("t0");
self.push_from("t1");
},
IR::Drop => {
+ self.label("# drop");
self.move_stack_ptr_by_cells(1);
},
IR::Equals => {
self.pop_call_push("t0 t1", "sub t0, t0, t1", "t0");
},
IR::GreaterThan => {
+ self.label("# >");
self.pop_some_to("t0 t1");
self.line("sgt t0, t0, t1");
self.line("seqz t0, t0"); // remember, 0 is true, others are false
},
// https://cmput229.github.io/229-labs-RISCV/RISC-V-Examples_Public/03-Conditionals/03b-If_Else.html
IR::If => {
+ self.label("# if");
self.pop_to("t0");
self.line(format!("bnez t0, _else_{}", if_block_count));
if_stack.push(if_block_count);
if_block_count += 1;
},
IR::Else => {
+ self.label("# else");
let if_counter = if_stack.last().unwrap().clone();
self.line(format!("j _endif_{}", if_counter));
self.label(format!("_else_{}:", if_counter));
seen_else.insert(if_counter);
},
IR::EndIf => {
+ self.label("# endif");
let stack = &mut if_stack;
let if_counter = stack.last().unwrap().clone();
if !seen_else.contains(&if_counter) {