Skip to content

Commit

Permalink
fix: some shit I messed up
Browse files Browse the repository at this point in the history
  • Loading branch information
azaleacolburn committed May 6, 2024
1 parent ba65204 commit 667bfdd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
Binary file modified function
Binary file not shown.
36 changes: 24 additions & 12 deletions function.asm
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
mov x9, #121
str x9, [x15, #-8]!
ldr x9, [x29, #-8]
str x9, [x15, #-8]!
; putchar
mov x0, #1 ; stdout
mov x1, x15 ; put from TOS
mov x2, #1 ; print 1 char
mov x16, #4 ; write
svc #0x80
; unload the TOS
add x15, x15, #8
; variable declaration: t
mov x9, #9
Expand All @@ -33,8 +21,32 @@
mov x9, #111
str x9, [x15, #-8]!
; place old sfb
str x29, [x15, #-8]!
mov x10, x15
ldr x9, [x29, #-8]
str x9, [x15, #-8]!
ldr x9, [x29, #-16]
str x9, [x15, #-8]!
ldr x9, [x29, #-24]
str x9, [x15, #-8]!
mov x29, x10
bl .L2
; variable declaration: z
; place old sfb
str x29, [x15, #-8]!
mov x10, x15
mov x9, #8
str x9, [x15, #-8]!
mov x29, x10
bl .L3
; assume ret is TOS
ldr x9, [x29, #-80]
str x9, [x15, #-8]!
; putchar
mov x0, #1 ; stdout
Expand Down
19 changes: 9 additions & 10 deletions src/compiler/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ pub struct SymbolTable {
}

impl SymbolTable {
fn new(parent: Option<usize>, offset: i32) -> SymbolTable {
fn new(parent: Option<usize>) -> SymbolTable {
SymbolTable {
table: HashMap::new(),
function_table: HashMap::new(),
parent,
furthest_offset: offset, // stackframe_base(0), ret(4), a*. Measured in bytes (change if needed)
furthest_offset: 0,
}
}

Expand Down Expand Up @@ -74,7 +74,7 @@ impl Handler {
scopes: vec![String::from("\n.global .main\n.align 4\n")],
curr_scope: 0,
break_anchors: vec![],
sym_arena: vec![SymbolTable::new(None, 0)],
sym_arena: vec![SymbolTable::new(None)],
curr_frame: 0,
};

Expand Down Expand Up @@ -153,8 +153,7 @@ impl Handler {
/// This function must be breaking some borrow checker rule
fn new_stack_frame(&mut self) {
self.sym_arena
.push(SymbolTable::new(Some(self.curr_frame), 0));
self.sym_arena.push(SymbolTable::new(Some(self.curr_frame)));
self.curr_frame = self.sym_arena.len() - 1;
}

Expand Down Expand Up @@ -203,8 +202,8 @@ impl Handler {
.insert(name.to_string(), function_sig);

self.new_stack_frame();
self.new_expr_lit(); // represents the SFB

self.sym_arena[self.curr_frame].furthest_offset += 8;
for arg in args.into_iter() {
self.new_id(arg.0, arg.1);
}
Expand Down Expand Up @@ -323,7 +322,7 @@ pub fn assignment_code_gen(node: &TokenNode, handler: &mut Handler) {
};
}

handler.push_to_scope(format!("\nstr x9, [x29, #{relative_stack_position}]!"));
handler.push_to_scope(format!("\nstr x9, [x29], #{relative_stack_position}"));
}

// Leaves the result on TOS
Expand All @@ -339,12 +338,12 @@ fn expr_code_gen(node: &TokenNode, handler: &mut Handler, x: i32) {
handler.push_to_scope(format!(
"\nldr x{x}, [x29, #{offset}]\nstr x{x}, [x15, #-8]!",
));
//handler.new_expr_lit();
handler.new_expr_lit();
}
NodeType::FunctionCall(name) => {
function_call_code_gen(&node, handler, name.to_string());
handler.push_to_scope("\n; assume ret is TOS");
handler.new_expr_lit();
handler.push_to_scope("\n; assume ret is TOS")
}
_ => {
expr_code_gen(&node.children.as_ref().unwrap()[0], handler, 9);
Expand Down Expand Up @@ -418,7 +417,7 @@ pub fn function_call_code_gen(node: &TokenNode, handler: &mut Handler, name: Str
// Store the address of the current stack fb on the top of the stack
// Decrement the sp by 32
// load the address of the new stack frame base into the sfb register
handler.push_to_scope("\n\n; place old sfb\nmov x10, x15\n\nstr x29, [x15, #-8]!");
handler.push_to_scope("\n\n; place old sfb\nstr x29, [x15, #-8]!\nmov x10, x15");

handler.new_expr_lit();

Expand Down
5 changes: 3 additions & 2 deletions tests/core/function.rh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ int square(int i) {
return (i * i);
}
int y = 'y';
put(y);
int t = '\t';
int o = 'o';
put(t);
putchars(y, t, o);
int z = square(8);
put(z);

0 comments on commit 667bfdd

Please sign in to comment.