]> rethought.computer Git - sorel-lang.git/commitdiff
beginning of stdlib keep/ac1e35bc7443a2b38e3e92d4eb833c55636f30c1
authorBryan English <bryan@rethought.computer>
Sat, 31 Jan 2026 03:52:58 +0000 (22:52 -0500)
committerBryan English <bryan@rethought.computer>
Tue, 10 Feb 2026 04:08:54 +0000 (04:08 +0000)
rel-lang/stdlib/mem.rel [new file with mode: 0644]
rel-lang/stdlib/out.rel [new file with mode: 0644]
rel-lang/stdlib/putn.c [new file with mode: 0644]
rel-lang/stdlib/test.sh [new file with mode: 0644]

diff --git a/rel-lang/stdlib/mem.rel b/rel-lang/stdlib/mem.rel
new file mode 100644 (file)
index 0000000..49d14db
--- /dev/null
@@ -0,0 +1,46 @@
+\ vim: filetype=forth
+
+: mmap 222 sys6 ;
+: munmap 215 sys2 ;
+
+: PROT_READ 1 ;
+: PROT_WRITE 2 ;
+: MAP_PRIVATE 2 ;
+: MAP_ANONYMOUS 32 ;
+
+: ALLOC_PROT PROT_READ PROT_WRITE | ;
+: ALLOC_MAP MAP_PRIVATE MAP_ANONYMOUS | ;
+
+\ This is a raw way to allocate memory. Callers to this will need to know
+\ how much memory was allocated, to call dealloc later.
+: alloc_raw ( byte-count -- addr )
+  0 swap ALLOC_PROT ALLOC_MAP -1:i32 0 mmap
+;
+
+: dealloc ( addr byte-count -- )
+  munmap
+;
+
+export alloc
+: alloc ( byte-count -- addr )
+  dup \ we'll need the length again later
+  8 + \ add 64 bits to the front where we'll store the length
+  alloc_raw \ ( byte-count addr )
+  dup \ ( byte-count addr addr)
+  rot \ ( addr addr byte-count )
+  rot \ ( addr byte-count addr )
+  ! \ write the length to the addr, now ( addr )
+  8 + \ here's the address we'll return, 8 bytes forward to account for the length
+;
+
+export free
+: free ( addr -- )
+  8 - \ the real memory start is 8 bytes before
+  dup \ ( addr addr )
+  @ \ ( addr byte-length )
+  8 + \ add 8 to the byte length to refer to the whole region from alloc.
+  dealloc
+; 
+
+1024 alloc
+free
diff --git a/rel-lang/stdlib/out.rel b/rel-lang/stdlib/out.rel
new file mode 100644 (file)
index 0000000..381d690
--- /dev/null
@@ -0,0 +1,21 @@
+\ vim: filetype=forth
+
+: write ( fd ptr len -- bytes-written-or-err )
+  64
+  sys3
+;
+
+: strlen ( addr -- len )
+  dup dup \ ( addr addr addr )
+  @:8 \ ( addr addr byte )
+  loop \ ( addr addr )
+    1 + \ ( addr addr+1 )
+    dup \ ( addr addr+1 addr+1)
+    @:8 \ ( addr addr+1 byte )
+  endloop
+  drop \ ( addr addr+len )
+  swap \ ( addr+len addr )
+  - \ ( len )
+;
+
+"this is a string" strlen
diff --git a/rel-lang/stdlib/putn.c b/rel-lang/stdlib/putn.c
new file mode 100644 (file)
index 0000000..201050c
--- /dev/null
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+extern unsigned long data_stack_end;
+register unsigned long * stack_pointer asm("s2");
+
+void putn() {
+  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);
+}
+
+
diff --git a/rel-lang/stdlib/test.sh b/rel-lang/stdlib/test.sh
new file mode 100644 (file)
index 0000000..c446927
--- /dev/null
@@ -0,0 +1,9 @@
+# ../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 
+# ./test-mem.out
+
+../target/debug/relc out.rel
+riscv64-unknown-linux-gnu-as -o out.o out.asm
+riscv64-unknown-linux-gnu-cc -O1 -no-pie -o test-out.out putn.c out.o -nostartfiles 
+./test-out.out