};
}
+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 {
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" {
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);
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");
},
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");
;
"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
+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