Skip to content

Commit

Permalink
anchor: don't revert to
Browse files Browse the repository at this point in the history
  • Loading branch information
Azalea Colburn committed May 6, 2024
1 parent 24b0403 commit c1d5a25
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 51 deletions.
Binary file modified function
Binary file not shown.
77 changes: 59 additions & 18 deletions function.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,44 @@
mov x29, sp
mov x15, sp
; variable declaration
; variable declaration: y
mov x9, #121
str x9, [x15, #-8]!
ldr x9, [x29, #-8]
str x9, [x15, #-8]!
; variable declaration
; 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
str x9, [x15, #-8]!
; variable declaration
; variable declaration: o
mov x9, #111
str x9, [x15, #-8]!
; place old sfb
mov x10, x15
str x29, [x15, #-8]!
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
; 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
; exit program gracefully
mov x0, #0
Expand All @@ -41,7 +53,7 @@

.L2:
; function declaration
ldr x9, [x29, #-16]
ldr x9, [x29, #-8]
str x9, [x15, #-8]!
; putchar
Expand All @@ -53,7 +65,7 @@
; unload the TOS
add x15, x15, #8
ldr x9, [x29, #-24]
ldr x9, [x29, #-16]
str x9, [x15, #-8]!
; putchar
Expand All @@ -65,7 +77,7 @@
; unload the TOS
add x15, x15, #8
ldr x9, [x29, #-32]
ldr x9, [x29, #-24]
str x9, [x15, #-8]!
; putchar
Expand All @@ -79,8 +91,37 @@
; unload stack
; x15 <- x29
; x29 <- &old_sfb
mov x15, x29
add x15, x15, #8
ldr x29, [x29]
ret

.L3:
; function declaration
; evaluate return statement and place on stack
ldr x9, [x29, #-8]
str x9, [x15, #-8]!
ldr x10, [x29, #-8]
str x10, [x15, #-8]!
; load from stack
ldr x9, [x15], #8
ldr x10, [x15], #8
mul x9, x9, x10
str x9, [x15, #-8]!
; ldr expr into x9
ldr x9, [x15], #8
; reset sfb
mov x15, x29
ldr x29, [x29]
str x9, [x15, #-8]!
ret
; unload stack
mov x15, x29
add x15, x15, #8
ldr x29, [x29]
ret
60 changes: 38 additions & 22 deletions src/compiler/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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), 8));
.push(SymbolTable::new(Some(self.curr_frame), 0));
self.curr_frame = self.sym_arena.len() - 1;
}

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

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

for arg in args.into_iter() {
self.new_id(arg.0, arg.1);
Expand Down Expand Up @@ -253,13 +253,16 @@ pub fn scope_code_gen(node: &TokenNode, handler: &mut Handler, scope_type: Scope
NodeType::FunctionCall(id) => {
function_call_code_gen(&child_node, handler, id.to_string())
}
NodeType::FunctionDecaration(id) => {
function_declare_code_gen(&child_node, handler, id.to_string())
NodeType::FunctionDecaration((id, t)) => {
function_declare_code_gen(&child_node, handler, id.to_string(), t.clone())
}
NodeType::Break => handler.insert_break(),
NodeType::Return => {
if scope_type == ScopeType::Function {
if let ScopeType::Function(_t) = scope_type {
// return type shouldn't matter for now
return return_statement_code_gen(&child_node, handler);
} else {
panic!("Return statements only allowed in function scopes");
}
}
NodeType::Asm(str) => asm_code_gen(&child_node, handler, str.to_string()),
Expand All @@ -281,7 +284,7 @@ pub fn scope_code_gen(node: &TokenNode, handler: &mut Handler, scope_type: Scope
pub fn declare_code_gen(node: &TokenNode, handler: &mut Handler, name: String, _t: RhTypes) {
println!("Declare Node: {:?}", node.token);
println!("Node children: {:?}", node.children);
handler.push_to_scope("\n\n; variable declaration");
handler.push_to_scope(format!("\n\n; variable declaration: {}", name));
expr_code_gen(
&node.children.as_ref().expect("Node to have children")[0],
handler,
Expand Down Expand Up @@ -336,7 +339,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();
}
NodeType::FunctionCall(name) => {
function_call_code_gen(&node, handler, name.to_string());
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 All @@ -360,22 +368,29 @@ fn expr_code_gen(node: &TokenNode, handler: &mut Handler, x: i32) {
}
}

pub fn function_declare_code_gen(node: &TokenNode, handler: &mut Handler, name: String) {
if node.children.is_none() {
panic!("Function children must be some");
}

let children = node.children.as_ref().unwrap();
pub fn function_declare_code_gen(
node: &TokenNode,
handler: &mut Handler,
name: String,
t: RhTypes,
) {
let children = node
.children
.as_ref()
.expect("Function node must have children");
let orig_scope = handler.curr_scope;
handler.new_scope();
handler.push_to_scope("\n; function declaration");
let function_scope = handler.curr_scope;
println!("function_scope: {}", handler.curr_scope);
let mut args: Vec<(String, i32)> = vec![];

for child in 0..children.len() - 1 {
if let NodeType::Declaration((id, t)) = &children[child].token {
let size = match t {
RhTypes::Char => 8,
RhTypes::Int => 8,
RhTypes::Void => 8,
};
// TODO: figure out what other code needs to go here (id any)
args.push((id.clone(), size));
Expand All @@ -386,13 +401,13 @@ pub fn function_declare_code_gen(node: &TokenNode, handler: &mut Handler, name:

if let NodeType::Scope(_) = scope_child.token {
println!("Function Scope Time");
scope_code_gen(&scope_child, handler, ScopeType::Function);
scope_code_gen(&scope_child, handler, ScopeType::Function(t));
}

handler.push_to_scope(
"\n\n; unload stack\n; x15 <- x29\n; x29 <- &old_sfb\nmov x15, x29\nldr x29, [x29]\nret",
);
handler.curr_scope = function_scope - 1;
handler
.push_to_scope("\n\n; unload stack\nmov x15, x29\nadd x15, x15, #8\nldr x29, [x29]\nret");
handler.curr_scope = orig_scope;
println!("handler prev curr scope: {}", handler.curr_scope);
handler.curr_frame = handler.sym_arena[handler.curr_frame]
.parent
.expect("Function has no parent sym tree");
Expand All @@ -403,7 +418,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\nstr x29, [x15, #-8]!");
handler.push_to_scope("\n\n; place old sfb\nmov x10, x15\n\nstr x29, [x15, #-8]!");

handler.new_expr_lit();

Expand Down Expand Up @@ -531,17 +546,18 @@ fn return_statement_code_gen(node: &TokenNode, handler: &mut Handler) {
handler,
9,
);
handler.push_to_scope("\nldr x9, [28]");
handler.push_to_scope(
"\n\n; x15 <- x29\n; x29 <- &old_sfb\nmov x15, x29\nldr x29, [x29]\nstr x9, [x15, #-8]!\nret",
);
handler.push_to_scope("\n\n; ldr expr into x9\nldr x9, [x15], #8");
handler.push_to_scope("\n\n; reset sfb\nmov x15, x29\nldr x29, [x29]");
handler.push_to_scope("\nstr x9, [x15, #-8]!\nret");
}

fn asm_code_gen(_node: &TokenNode, handler: &mut Handler, str: String) {
handler.push_to_scope(format!("\n{}", str));
}

fn putchar_code_gen(node: &TokenNode, handler: &mut Handler) {
println!("sym_tree: {:?}", handler.sym_arena[0]);

let children = node
.children
.as_ref()
Expand Down
24 changes: 23 additions & 1 deletion src/compiler/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn string_to_tokens(
&& chars[i + 3] == 'u'
&& chars[i + 4] == 'r'
&& chars[i + 5] == 'n'
&& chars[i + 6] == '('
&& (chars[i + 6] == '(' || chars[i + 6] == ' ')
{
ret.push(Token::Return);
i += 5;
Expand Down Expand Up @@ -526,6 +526,27 @@ pub fn string_to_tokens(
curr = String::from("");
}
}
'v' => {
if chars[i + 1] == 'o'
&& chars[i + 2] == 'i'
&& chars[i + 3] == 'd'
&& (chars[i + 4] == ' ' || chars[i + 4] == '*')
{
ret.push(Token::Type(RhTypes::Void));
i += 3;
} else {
for j in i..chars.len() {
if !chars[j].is_alphabetic() && chars[j] != '_' {
break;
}
curr.push(chars[j]);
}
ret.push(Token::Id(curr.clone()));
println!("curr before overflow: {}", curr);
i += curr.len() - 1;
curr = String::from("");
}
}
'\n' => {
line_tracker.new_line();
}
Expand Down Expand Up @@ -652,4 +673,5 @@ pub enum Token {
pub enum RhTypes {
Char,
Int,
Void,
}
Loading

0 comments on commit c1d5a25

Please sign in to comment.