From 36c1e09c16d92dacf54b37eadc6ba33983eed0f4 Mon Sep 17 00:00:00 2001 From: Bryan English Date: Tue, 3 Feb 2026 00:00:57 -0500 Subject: [PATCH] implement putn in language, instead of C --- rel-lang/relc/src/ir.rs | 3 +- rel-lang/relc/src/riscv_asm_codegen.rs | 33 +++++++++++++++------ rel-lang/stdlib/out.rel | 40 ++++++++++++++++++++++++++ rel-lang/stdlib/test.sh | 15 +++++++--- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/rel-lang/relc/src/ir.rs b/rel-lang/relc/src/ir.rs index 2330920..aa50b18 100644 --- a/rel-lang/relc/src/ir.rs +++ b/rel-lang/relc/src/ir.rs @@ -121,7 +121,7 @@ impl ImportTree { "drop" => IR::Drop, "over" => IR::Over, "rot" => IR::Rot, - "putn" => IR::PutN, + "sp" => IR::StackPointer, "if" => IR::If, "else" => IR::Else, "endif" => IR::EndIf, @@ -129,6 +129,7 @@ impl ImportTree { "endloop" => IR::EndLoop, "=" => IR::Equals, ">" => IR::GreaterThan, + "<" => IR::LessThan, "+" => IR::AddU64, "-" => IR::SubtractU64, "*" => IR::MultiplyU64, diff --git a/rel-lang/relc/src/riscv_asm_codegen.rs b/rel-lang/relc/src/riscv_asm_codegen.rs index f2e9936..5ade98e 100644 --- a/rel-lang/relc/src/riscv_asm_codegen.rs +++ b/rel-lang/relc/src/riscv_asm_codegen.rs @@ -46,6 +46,13 @@ macro_rules! asm_macro { }; } +fn mangle(input: &str) -> String { + input + .replace("<", "_LT_") + .replace(">", "_GT_") + .replace("-", "___") +} + impl<'a> CodeGen<'a> { pub fn new(ir_mod: &'a IRObject, data_stack_size: usize) -> Self { Self { @@ -140,16 +147,17 @@ impl<'a> CodeGen<'a> { self.label("_start:"); self.line("la s2, data_stack_end # set initial data stack pointer"); } else { - self.label(format!(".globl {}", name)); - self.label(format!("{}:", name)); + let mangled = mangle(name); + self.label(format!(".globl {}", mangled)); + self.label(format!("{}:", mangled)); } self.line("addi sp, sp, -16 # allocate 16 bytes on stack"); // allocate 16 bytes on stack self.line("sd 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)); + let mangled = mangle(name); + self.label(format!("# call {}", mangled)); + self.line(format!("call {}", mangled)); }, IR::Ret => { if last_label == "main" { @@ -236,6 +244,11 @@ impl<'a> CodeGen<'a> { self.push_from("t2"); self.push_from("t0"); }, + IR::StackPointer => { + self.label("# sp"); + self.line("addi t0, s2, 0"); + self.push_from("t0"); + }, IR::Drop => { self.label("# drop"); self.move_stack_ptr_by_cells(1); @@ -252,6 +265,13 @@ impl<'a> CodeGen<'a> { self.line("seqz t0, t0"); // remember, 0 is true, others are false self.push_from("t0"); }, + IR::LessThan => { + self.label("# <"); + self.pop_some_to("t0 t1"); + self.line("slt t0, t0, t1"); + self.line("seqz t0, t0"); // remember, 0 is true, others are false + self.push_from("t0"); + }, IR::BitwiseOr => { self.pop_call_push("t0 t1", "or t0, t0, t1", "t0"); }, @@ -276,9 +296,6 @@ impl<'a> CodeGen<'a> { IR::Sys6 => { self.pop_call_push("a0 a1 a2 a3 a4 a5 a7", "ecall", "a0"); }, - IR::PutN => { - self.line("call putn"); - }, // https://cmput229.github.io/229-labs-RISCV/RISC-V-Examples_Public/03-Conditionals/03b-If_Else.html IR::If => { self.label("# if"); diff --git a/rel-lang/stdlib/out.rel b/rel-lang/stdlib/out.rel index 28f745d..48dfd94 100644 --- a/rel-lang/stdlib/out.rel +++ b/rel-lang/stdlib/out.rel @@ -28,3 +28,43 @@ ; "Hello, World! \n" puts + +: ZERO_CHAR 48 ; +: NEWLINE_CHAR 10 ; + +export putn + +: putn ( num -- num ) + dup dup \ ( num num num ) + 10 rot rot \ ( num 10 num num ) // Mutltidigit stop point + 10 < \ ( num 10 num is<10 ) + loop \ ( num 10 num' ) + dup \ ( num 10 num' num' ) + 10 % \ ( num 10 num' digit ) + swap \ ( num 10 digit num' ) + 10 / \ ( num 10 digit num'/10 ) + dup 10 < \ ( num 10 digit num'/10 is<10 ) + endloop \ ( num 10 digitn ... digit1 ) + dup 9 > \ ( num 10 digitn ... digit1 digit1>9 ) + loop \ ( num 10 digitn ... digit1 ) + ZERO_CHAR + \ \ ( num 10 digitn ... digit1 ) + sp \ ( num 10 digitn ... digit1 ptr ) + 1 \ ( num 10 digitn ... digit1 ptr 1 ) + swap \ ( num 10 digitn ... digit1 1 ptr ) + 1 \ ( num 10 digitn ... digit1 1 ptr 1 ) + write \ ( num 10 digitn ... digit1 result ) + drop drop \ ( num 10 digitn ... digit2 ) + dup 9 > \ ( num 10 digitn ... digit2 digit2>9 ) + endloop \ ( num 10 ) + drop \ ( num ) + NEWLINE_CHAR \ ( num 10 ) + sp \ ( num 10 ptr ) + 1 \ ( num 10 ptr 1 ) + swap \ (num 10 1 ptr ) + 1 \ ( num 10 1 ptr 1 ) + write \ ( num 10 result ) + drop \ ( num 10 ) + drop \ ( num ) +; + +28 putn diff --git a/rel-lang/stdlib/test.sh b/rel-lang/stdlib/test.sh index 0d32765..0bef584 100644 --- a/rel-lang/stdlib/test.sh +++ b/rel-lang/stdlib/test.sh @@ -1,9 +1,16 @@ +UNAME=$(uname -m) +CMD_PREFIX=$([ "$UNAME" = "riscv64" ] && echo "" || echo "riscv64-unknown-linux-gnu-") +AS="${CMD_PREFIX}as" +CC="${CMD_PREFIX}cc" + + # ../target/debug/relc mem.rel -# riscv64-unknown-linux-gnu-as -o mem.o mem.asm -# riscv64-unknown-linux-gnu-cc -O1 -no-pie -o test-mem.out putn.c mem.o -nostartfiles +# $AS -o mem.o mem.asm +# $CC -O1 -no-pie -o test-mem.out putn.c mem.o -nostartfiles # ./test-mem.out + ../target/debug/relc out.rel -as -o out.o out.asm -cc -O1 -no-pie -o test-out.out out.o putn.c -nostartfiles +$AS -o out.o out.asm +$CC -O1 -no-pie -o test-out.out out.o putn.c -nostartfiles ./test-out.out -- 2.43.0