From 85c8593e298d6bbed35ca661fcd55f5f5c3216a8 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Tue, 27 Jan 2026 23:50:38 -0500 Subject: [PATCH] comments in asm --- hylo-lang/hyloc/src/riscv_asm_codegen.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/hylo-lang/hyloc/src/riscv_asm_codegen.rs b/hylo-lang/hyloc/src/riscv_asm_codegen.rs index ca3cd99..4fa4be3 100644 --- a/hylo-lang/hyloc/src/riscv_asm_codegen.rs +++ b/hylo-lang/hyloc/src/riscv_asm_codegen.rs @@ -118,7 +118,7 @@ impl<'a> CodeGen<'a> { 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"); @@ -136,20 +136,22 @@ impl<'a> CodeGen<'a> { 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"); @@ -171,17 +173,21 @@ impl<'a> CodeGen<'a> { 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 => { @@ -194,15 +200,18 @@ impl<'a> CodeGen<'a> { 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 => { @@ -211,6 +220,7 @@ impl<'a> CodeGen<'a> { 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 @@ -245,18 +255,21 @@ impl<'a> CodeGen<'a> { }, // 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) { -- 2.43.0