Skip to content

Commit

Permalink
Added exit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 17, 2024
1 parent e572b1f commit a671c3e
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 39 deletions.
57 changes: 36 additions & 21 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 28 additions & 10 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ main:
push rbp
mov rbp, rsp
mov qword [rbp-8], 1
mov qword [rbp-8], 0
mov qword [rbp-16], 1
mov rax, qword [rbp-16]
mov qword [rbp-40], rax
mov rax, qword [rbp-8]
mov qword [rbp-16], rax
sub rsp, 16
mov qword [rbp-48], rax
sub rsp, 48
call _1
add rsp, 16
mov qword [rbp-16], 255
mov rax, qword [rbp-16]
add rsp, 48
mov qword [rbp-40], 255
mov rax, qword [rbp-40]
leave
ret

Expand All @@ -34,15 +37,30 @@ _1:
mov rax, qword [rbp+16]
mov qword [rbp-24], rax
mov qword [rbp-32], 1
mov rax, qword [rbp+24]
mov qword [rbp-32], rax
mov rax, qword [rbp-24]
add rax, qword [rbp-32]
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
mov rax, qword [rbp+24]
mov qword [rbp-40], rax
sub rsp, 40
mov qword [rbp-48], 12
mov rax, 60
mov rdi, [rbp-48]
syscall
mov rax, qword [rbp-40]
mov qword [rbp-56], rax
mov rax, qword [rbp-16]
mov qword [rbp-64], rax
mov rax, qword [rbp-64]
mov qword [rbp-88], rax
mov rax, qword [rbp-56]
mov qword [rbp-96], rax
sub rsp, 96
call _1
add rsp, 40
add rsp, 96
leave
ret

section .data
__10_fstr db `Integer: %d\n`,0
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
9 changes: 6 additions & 3 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
fn r(a: int) {
fn fib(a: int, b: int) {
printi(a);
r(a + 1);
let d: int = a + b;
let c: int = b;
exit(12);
fib(c, d);
}

fn main() -> int {
r(1);
fib(0, 1);
return 255;
}
51 changes: 51 additions & 0 deletions src/root/builtin/functions/exit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use unique_type_id::UniqueTypeId;
use crate::root::builtin::{BuiltinInlineFunction, f_id, InlineFunctionGenerator};
use crate::root::builtin::types::int::IntType;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::NameResult::Function;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;

use crate::root::shared::common::{FunctionID, Indirection, LocalAddress, TypeID, TypeRef};

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct ExitFunction;

impl ExitFunction {
pub const fn id() -> FunctionID {
f_id(ExitFunction::unique_type_id().0)
}
}

impl BuiltinInlineFunction for ExitFunction {
fn id(&self) -> FunctionID {
Self::id()
}

fn name(&self) -> &'static str {
"exit"
}

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
false,
&[("lhs", IntType::id().immediate())],
None
)
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], _, gt, sz| -> String {
let lhs = &args[0];

// 0 us exit syscall
format!(" mov rax, 60
mov rdi, {lhs}
syscall")
}
}

fn parent_type(&self) -> Option<TypeID> {
None
}
}
9 changes: 9 additions & 0 deletions src/root/builtin/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod exit;

use b_box::b;
use crate::root::builtin::functions::exit::ExitFunction;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;

pub fn register_functions(global_table: &mut GlobalDefinitionTable) {
global_table.register_inline_function(&ExitFunction);
}
2 changes: 2 additions & 0 deletions src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod types;
pub mod functions;

use crate::root::builtin::functions::register_functions;
use crate::root::builtin::types::int::register_int;
use crate::root::compiler::global_tracker::GlobalTracker;
use crate::root::errors::WErr;
Expand All @@ -10,6 +11,7 @@ use crate::root::shared::common::{ByteSize, FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;

pub fn register_builtin(global_table: &mut GlobalDefinitionTable) {
register_functions(global_table);
register_int(global_table);
}

Expand Down
2 changes: 1 addition & 1 deletion src/root/builtin/types/int/printi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl BuiltinInlineFunction for PrintI {

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
false,
&[("lhs", IntType::id().immediate())],
None
)
Expand Down
6 changes: 3 additions & 3 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ pub fn compile_function(fid: FunctionID, function: FunctionToken, global_table:
full_contents
);

if fid.is_main() {
final_contents += "\n\tleave\n\tret"
}
// if fid.is_main() {
final_contents += "\n\tleave\n\tret";
// }

Ok(final_contents)
}
Expand Down
2 changes: 1 addition & 1 deletion src/root/compiler/compile_function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn call_function(
local_variables.enter_block();

// ? Arguments
for arg in args {
for arg in args.iter().rev() {
let into = global_table.add_local_variable_unnamed_base(arg.type_ref().clone(), local_variables);
code += "\n";
code += &copy(*arg.local_address(), *into.local_address(), global_table.get_size(into.type_ref()));
Expand Down
10 changes: 10 additions & 0 deletions types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ Pri=6
Prin=7
Print=8
PrintI=9
E=10
Ex=11
Exi=12
Exit=13
ExitF=14
ExitFu=15
ExitFun=16
ExitFunctio=17
ExitFunction=18
ExitFunctions=19

0 comments on commit a671c3e

Please sign in to comment.