From 2c640a274f16bd9c219ab54b6658a9c481da651a Mon Sep 17 00:00:00 2001 From: Bryan English Date: Mon, 9 Feb 2026 09:32:20 -0500 Subject: [PATCH] add externs and putstack for testing --- rel-lang/sorelc/src/ir.rs | 11 ++++++++++- rel-lang/sorelc/src/parser.rs | 10 +++++++++- rel-lang/tests/putstack.c | 16 ++++++++++++++++ rel-lang/tests/test.sh | 4 +++- 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 rel-lang/tests/putstack.c diff --git a/rel-lang/sorelc/src/ir.rs b/rel-lang/sorelc/src/ir.rs index 324ca6d..61e6115 100644 --- a/rel-lang/sorelc/src/ir.rs +++ b/rel-lang/sorelc/src/ir.rs @@ -20,6 +20,7 @@ struct IRModule { text: Vec, imports: Vec>, exports: Vec, + externs: Vec, // TODO these next two should form an enum, not two options source_file: Option, std_specifier: Option, @@ -28,6 +29,9 @@ struct IRModule { impl IRModule { fn get_label_for_call(&self, name: &String) -> String { + if self.externs.contains(name) { + return name.clone(); + } let mut found: Option = None; for imported in &self.imports { if imported.exports.contains(name) { @@ -96,7 +100,10 @@ impl ImportTree { } let contents = &std::fs::read_to_string(&path)?; - let module = self.generate_internal(Some(path), None, &Module::parse(tokenize(&contents)?, is_entrypoint)?); + let tokens = tokenize(&contents)?; + let parsed = &Module::parse(tokens, is_entrypoint)?; + + let module = self.generate_internal(Some(path), None, parsed); let module = Rc::new(module); self.all_modules.insert(path_key, module.clone()); if is_entrypoint { @@ -147,6 +154,7 @@ impl ImportTree { s.to_string() }).collect(); + let externs = module.externs.iter().map(|s| s.to_string()).collect(); text.push(module.words.iter().map(|def| { let mut body = def.instructions.iter().map(|inst| { @@ -225,6 +233,7 @@ impl ImportTree { data, imports, exports, + externs, source_file: path, std_specifier, number, diff --git a/rel-lang/sorelc/src/parser.rs b/rel-lang/sorelc/src/parser.rs index 3214139..94b53e4 100644 --- a/rel-lang/sorelc/src/parser.rs +++ b/rel-lang/sorelc/src/parser.rs @@ -12,6 +12,7 @@ pub struct Module<'a> { pub words: Vec>, pub imports: Vec<&'a str>, pub exports: Vec<&'a str>, + pub externs: Vec<&'a str>, } impl<'a> Module<'a> { @@ -20,10 +21,12 @@ impl<'a> Module<'a> { let mut main = vec![]; let mut exports = vec![]; let mut imports = vec![]; + let mut externs = vec![]; let mut current_word: Option = None; let mut about_to_start_word_def = false; let mut last_was_import = false; let mut last_was_export = false; + let mut last_was_extern = false; for token in input { if about_to_start_word_def { @@ -64,10 +67,15 @@ impl<'a> Module<'a> { last_was_import = true; } else if word == "export" { last_was_export = true; + } else if word == "extern" { + last_was_extern = true; } else { if last_was_export { exports.push(word); last_was_export = false; + } else if last_was_extern { + externs.push(word); + last_was_extern = false; } else { main.push(token.clone()); } @@ -99,7 +107,7 @@ impl<'a> Module<'a> { }); } - Ok(Module { words: result, imports, exports }) + Ok(Module { words: result, imports, exports, externs }) } #[cfg(test)] diff --git a/rel-lang/tests/putstack.c b/rel-lang/tests/putstack.c new file mode 100644 index 0000000..a5200c4 --- /dev/null +++ b/rel-lang/tests/putstack.c @@ -0,0 +1,16 @@ +#include + +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); +} + + diff --git a/rel-lang/tests/test.sh b/rel-lang/tests/test.sh index e6f7b22..279addc 100644 --- a/rel-lang/tests/test.sh +++ b/rel-lang/tests/test.sh @@ -2,8 +2,10 @@ UNAME=$(uname -m) CMD_PREFIX=$([ "$UNAME" = "riscv64" ] && echo "" || echo "riscv64-unknown-linux-gnu-") AS="${CMD_PREFIX}as" LD="${CMD_PREFIX}ld" +CC="${CMD_PREFIX}cc" ../target/debug/sorelc test1.sorel $AS -g -o test1.o test1.asm -$LD -o test1.out test1.o +$CC -O1 -no-pie -o test1.out test1.o putstack.c -nostartfiles +# $LD -o test1.out test1.o ./test1.out -- 2.43.0