]> rethought.computer Git - sorel-lang.git/commitdiff
implement putstack in sorel
authorBryan English <bryan@rethought.computer>
Thu, 12 Feb 2026 04:40:11 +0000 (23:40 -0500)
committerBryan English <bryan@rethought.computer>
Thu, 12 Feb 2026 04:41:28 +0000 (23:41 -0500)
docs/hacking.md
sorel-ir/src/lib.rs
sorelc/src/ir.rs
sorelc/src/riscv_asm_codegen.rs
stdlib/out.sorel
tests/putstack.c [deleted file]
tests/test.sh
tests/test1.sorel

index fe7323da6041ba39379cc8fb9cf33c6af370bf79..afd8f77168ff0be17d4d6172aac6d264c93ef105 100644 (file)
@@ -70,5 +70,3 @@ target remote :4567
 
 Then you can use normal gdb commands!
 The `tui layout asm` is highly recommended.
-Note that if you'l linking `putstack.c`, that will use libc, which drastically inflates the binary and hijack's init.
-This can make things quite a pain in `gdb`, so try to build without it (i.e. uncomment the LD line and comment out the CC line in `test.sh`, and remove all references to `putstack` in code).
index 1a3ec276942dc1be1c8f81dfc9c5be2005a12004..d4e8e8fecc4f6716c78f8b9e689b03b450109bd5 100644 (file)
@@ -38,6 +38,7 @@ pub enum IR {
     Over,
     Rot,
     StackPointer,
+    StackBottom,
     If,
     Else,
     EndIf,
index 61e61156c8f6c5bd3e6b07031319bec79a5a26de..7324af7b1ed0b5c61615d897b699c5e29a3dd75c 100644 (file)
@@ -175,6 +175,7 @@ impl ImportTree {
                             "over" => IR::Over,
                             "rot" => IR::Rot,
                             "sp" => IR::StackPointer,
+                            "stackbottom" => IR::StackBottom,
                             "if" => IR::If,
                             "else" => IR::Else,
                             "endif" => IR::EndIf,
index c382cdfc3e8114e5b75e47cc2672f245825741c1..23fba90984ed6a2b9b960137a99bc82221dda042 100644 (file)
@@ -71,8 +71,8 @@ impl<'a> CodeGen<'a> {
         self.lines.push(line.to_string());
     }
 
-    asm_macro!(copy_top_stack_value_to, "ld   {}, 0(s2)", &str);
-    asm_macro!(copy_offset_stack_value_to, "ld   {}, {}*8(s2)", &str, isize);
+    asm_macro!(copy_top_stack_value_to, "ld {}, 0(s2)", &str);
+    asm_macro!(copy_offset_stack_value_to, "ld {}, {}*8(s2)", &str, isize);
     asm_macro!(copy_to_top_of_stack, "sd {}, 0(s2)", &str);
     asm_macro!(move_stack_ptr_by_cells, "addi s2, s2, {}*8", isize);
 
@@ -251,6 +251,15 @@ impl<'a> CodeGen<'a> {
                     self.push_from("t0");
                     self.push_from("t1");
                 },
+                IR::Over => {
+                    // TODO this is super inefficient. There's no need to pop anything. Just read
+                    // from the second stack position and push it.
+                    self.label("# over");
+                    self.pop_some_to("t0 t1");
+                    self.push_from("t0");
+                    self.push_from("t1");
+                    self.push_from("t0");
+                },
                 IR::Rot => {
                     self.label("# rot");
                     self.pop_some_to("t0 t1 t2");
@@ -263,6 +272,11 @@ impl<'a> CodeGen<'a> {
                     self.line("addi t0, s2, 0");
                     self.push_from("t0");
                 },
+                IR::StackBottom => {
+                    self.label("# stackbottom");
+                    self.line("la t0, data_stack_end");
+                    self.push_from("t0");
+                }
                 IR::Drop => {
                     self.label("# drop");
                     self.move_stack_ptr_by_cells(1);
index 5704f999c9826acdf975d64eefcff4541be79d5a..efe769e5e85e9e3fbbc50b3a3059dcbeffd0929f 100644 (file)
@@ -57,3 +57,29 @@ export putn
   drop \ ( num )
 ;
 
+export putstack
+
+: putstack ( -- )
+  sp stackbottom \ ( sp stack_end )
+  swap \ ( stack_end sp )
+  - \ ( stack_height_in_bytes )
+  8 \ ( stack_height_in_bytes cell_length )
+  / \ ( stack_height_in_cells )
+  dup dup  \ ( stack_height_in_cells stack_height_in_cells stack_height_in_cells )
+  "\n________________________________ stack top\n" puts drop
+  loop \ ( stack_height_in_cells index )
+    over over \ ( stack_height_in_cells index stack_height_in_cells index )
+    - \ (stack_height_in_cells index offset )
+    sp 24 + \ ( stack_height_in_cells index offset sp ) \\\\ NOTE: We've added 3 cells to the stack, so need to rewind them here
+    swap 8 \ ( stack_height_in_cells index sp offset cell_length )
+    * \ ( stack_height_in_cells index sp true_offset )
+    + \ ( stack_height_in_cells index ptr_to_print )
+    @ \ ( stack_height_in_cells index val_to_print )
+    "   " puts drop putn drop \ ( stack_height_in_cells index )
+    1 - \ ( stack_height_in_cells index-1 )
+    dup \ ( stack_height_in_cells index-1 )
+  endloop \ ( stack_height_in_cells 0 )
+  drop drop \ ()
+  "-------------------------------- stack bottom\n\n" puts drop
+;
+
diff --git a/tests/putstack.c b/tests/putstack.c
deleted file mode 100644 (file)
index a5200c4..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-#include <stdio.h>
-
-extern unsigned long data_stack_end;
-register unsigned long * stack_pointer asm("s2");
-
-void putstack() {
-  unsigned long * stack_index = &data_stack_end;
-  printf("stack: ");
-  while (stack_index != stack_pointer) {
-         printf("%ld ", *stack_index);
-    stack_index -= 1;
-  }
-       printf("%ld\n", *stack_pointer);
-}
-
-
index 279addce6ec9d9cf07aebaa084bb74fd4804b4c8..ab7a14550a5e70d03885df622ec67cd65c8ea286 100644 (file)
@@ -6,6 +6,5 @@ CC="${CMD_PREFIX}cc"
 
 ../target/debug/sorelc test1.sorel
 $AS -g -o test1.o test1.asm
-$CC -O1 -no-pie -o test1.out test1.o putstack.c -nostartfiles 
-# $LD -o test1.out test1.o
+$LD -o test1.out test1.o
 ./test1.out
index 325662510e825c6478225a2c888c72f1264146d4..ca868a4c183c690eb4992728c282ffc05ab050c5 100644 (file)
@@ -20,11 +20,9 @@ import "std:string"
 import "std:mem"
 import "std:out"
 
-extern putstack
-
 16 alloc \ ( ptr )
 dup \ ( ptr ptr )
-"To be copied!" \ ( ptr ptr strptr )
+"To be copied!\n" \ ( ptr ptr strptr )
 dup \ ( ptr ptr strptr strptr )
 strlen 1 + \ ( ptr ptr strptr len )
 rot \ ( ptr strptr len ptr )
@@ -33,4 +31,4 @@ memcopy \ ( ptr )
 puts \ ( ptr )
 free \ ( )
 
-
+42 43 44 "soup" putstack