]> rethought.computer Git - sorel-lang.git/commitdiff
implement putn in language, instead of C keep/36c1e09c16d92dacf54b37eadc6ba33983eed0f4
authorBryan English <bryan@rethought.computer>
Tue, 3 Feb 2026 05:00:57 +0000 (00:00 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:54 +0000 (04:08 +0000)
rel-lang/relc/src/ir.rs
rel-lang/relc/src/riscv_asm_codegen.rs
rel-lang/stdlib/out.rel
rel-lang/stdlib/test.sh

index 23309203943eb0ffbfa8ab17700e6e7419bce1a0..aa50b1819dbf5401b2dcb1ea63f2bef9d13d7dc9 100644 (file)
@@ -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,
index f2e99365f4ec1470456d5a0f3a081877e164d60d..5ade98e16dc5d78b41e2c4c89887e382f2d71006 100644 (file)
@@ -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");
index 28f745d4602cdd3eb830f2865c03b1df6afb603a..48dfd9464b77814e8ca20c31481536862cf5cb20 100644 (file)
 ;
 
 "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
index 0d32765545c864577aa96bdcebf0f49669f1905a..0bef584958affef1c29e27683040cfacf2885f9f 100644 (file)
@@ -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