diff --git a/build/out.asm b/build/out.asm index 06f1ecf..c69b3e7 100644 --- a/build/out.asm +++ b/build/out.asm @@ -5,54 +5,72 @@ section .text main: push rbp mov rbp, rsp - mov qword [rbp-8], 3 - mov qword [rbp-16], 4 - mov byte [rbp-17], 1 - mov al, byte [rbp-17] - mov byte [rbp-18], al - mov rdi, __6_fstr - mov rsi, 0 - mov sil, [rbp-18] - mov al, 0 - sub rsp, 18 - extern printf - call printf - add rsp, 18 - mov al, byte [rbp-17] - mov byte [rbp-19], al - cmp byte [rbp-19], 0 - jz main_0 + 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-27], rax - mov rdi, __4_fstr - mov rsi, [rbp-27] - mov al, 0 - sub rsp, 27 - extern printf - call printf - add rsp, 27 - mov qword [rbp-35], 13 - mov rax, qword [rbp-35] + mov qword [rbp-48], rax + sub rsp, 48 + call _1 + add rsp, 48 + mov qword [rbp-40], 1 + mov rax, qword [rbp-40] leave ret - jmp main_1 - main_0: - mov rax, qword [rbp-16] - mov qword [rbp-27], rax + + +_1: + push rbp + mov rbp, rsp + mov rax, qword [rbp+16] + mov qword [rbp-8], rax mov rdi, __4_fstr - mov rsi, [rbp-27] + mov rsi, [rbp-8] mov al, 0 - sub rsp, 27 + sub rsp, 8 extern printf call printf - add rsp, 27 - mov qword [rbp-35], 12 - mov rax, qword [rbp-35] - leave - ret - main_1: + add rsp, 8 + mov rax, qword [rbp+16] + mov qword [rbp-17], rax + mov qword [rbp-25], 233 + mov rax, qword [rbp-17] + cmp rax, qword [rbp-25] + jz __5_0 + mov byte [rbp-9], 0 + jmp __5_1 + __5_0: + mov byte [rbp-9], 1 + __5_1: + cmp byte [rbp-9], 0 + jz _1_2 + mov qword [rbp-33], 10 + mov rax, 60 + mov rdi, [rbp-33] + syscall + _1_2: + mov rax, qword [rbp+24] + mov qword [rbp-33], rax + mov rax, qword [rbp+16] + mov qword [rbp-49], rax + mov rax, qword [rbp+24] + mov qword [rbp-57], rax + mov rax, qword [rbp-49] + add rax, qword [rbp-57] + mov qword [rbp-41], rax + mov rax, qword [rbp-33] + mov qword [rbp-65], rax + mov rax, qword [rbp-41] + mov qword [rbp-73], rax + mov rax, qword [rbp-73] + mov qword [rbp-97], rax + mov rax, qword [rbp-65] + mov qword [rbp-105], rax + sub rsp, 105 + call _1 + add rsp, 105 section .data_readonly - __6_fstr db `Boolean: %d\n`,0 __4_fstr db `Integer: %d\n`,0 \ No newline at end of file diff --git a/build/out.o b/build/out.o index a350db6..8b84e4f 100644 Binary files a/build/out.o and b/build/out.o differ diff --git a/build/out.out b/build/out.out index a78d355..f93f857 100755 Binary files a/build/out.out and b/build/out.out differ diff --git a/main.why b/main.why index a88ebe4..79c2045 100644 --- a/main.why +++ b/main.why @@ -1,15 +1,18 @@ -fn main() -> int { - let i: int = 3; - let j: int = 4; - let x: bool = true; - printb(x); - - if (x) { - printi(i); - return 13; - } - else { - printi(j); - return 12; +fn fib(a: int, b: int) { + printi(a); + + if (a == 233) { + exit(10); } + + let c: int = b; + let d: int = a + b; + + fib(c, d); +} + +fn main() -> int { + fib(0, 1); + + return 1; } diff --git a/src/root/builtin/functions/exit.rs b/src/root/builtin/functions/exit.rs index a0ceae5..4355a54 100644 --- a/src/root/builtin/functions/exit.rs +++ b/src/root/builtin/functions/exit.rs @@ -41,7 +41,7 @@ impl BuiltinInlineFunction for ExitFunction { // 0 us exit syscall format!(" mov rax, 60 mov rdi, {lhs} - syscall") + syscall\n") } } diff --git a/src/root/builtin/types/int/add.rs b/src/root/builtin/types/int/add.rs index d9c751e..51257bb 100644 --- a/src/root/builtin/types/int/add.rs +++ b/src/root/builtin/types/int/add.rs @@ -36,7 +36,7 @@ impl BuiltinInlineFunction for IntAdd { format!( " mov rax, qword {lhs} add rax, qword {rhs} - mov qword {return_into}, rax") + mov qword {return_into}, rax\n") } } diff --git a/src/root/builtin/types/int/eq.rs b/src/root/builtin/types/int/eq.rs new file mode 100644 index 0000000..b1c5573 --- /dev/null +++ b/src/root/builtin/types/int/eq.rs @@ -0,0 +1,62 @@ +use unique_type_id::UniqueTypeId; +use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator, f_id}; +use crate::root::builtin::types::bool::BoolType; +use crate::root::builtin::types::int::IntType; +use crate::root::builtin::types::int::printi::PrintI; +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 IntEq; + +impl IntEq { + pub const fn id() -> FunctionID { + f_id(IntEq::unique_type_id().0) + } +} + +impl BuiltinInlineFunction for IntEq { + fn id(&self) -> FunctionID { + IntEq::id() + } + + fn name(&self) -> &'static str { + "eq" + } + + fn signature(&self) -> FunctionSignature { + FunctionSignature::new_inline_builtin( + true, + &[("lhs", IntType::id().immediate()), ("rhs", IntType::id().immediate())], + Some(BoolType::id().immediate()) + ) + } + + fn inline(&self) -> InlineFunctionGenerator { + |args: &[LocalAddress], return_into: Option, gt, _| -> String { + let lhs = args[0]; + let rhs = args[1]; + let return_into = return_into.unwrap(); + let j = gt.get_unique_tag(IntEq::id()); + let j2 = gt.get_unique_tag(IntEq::id()); + + format!( +" mov rax, qword {lhs} + cmp rax, qword {rhs} + jz {j} + mov byte {return_into}, 0 + jmp {j2} + {j}: + mov byte {return_into}, 1 + {j2}:\n") + } + } + + fn parent_type(&self) -> Option { + Some(IntType::id()) + } +} diff --git a/src/root/builtin/types/int/mod.rs b/src/root/builtin/types/int/mod.rs index 078ddf8..5b142cb 100644 --- a/src/root/builtin/types/int/mod.rs +++ b/src/root/builtin/types/int/mod.rs @@ -2,11 +2,13 @@ mod add; mod sub; mod p_sub; mod printi; +mod eq; use b_box::b; use unique_type_id::UniqueTypeId; use crate::root::builtin::t_id; use crate::root::builtin::types::int::add::IntAdd; +use crate::root::builtin::types::int::eq::IntEq; use crate::root::builtin::types::int::p_sub::IntPSub; use crate::root::builtin::types::int::printi::PrintI; use crate::root::builtin::types::int::sub::IntSub; @@ -21,6 +23,7 @@ pub fn register_int(global_table: &mut GlobalDefinitionTable) { global_table.register_inline_function(&IntAdd); global_table.register_inline_function(&IntSub); global_table.register_inline_function(&IntPSub); + global_table.register_inline_function(&IntEq); global_table.register_inline_function(&PrintI); } diff --git a/src/root/builtin/types/int/p_sub.rs b/src/root/builtin/types/int/p_sub.rs index ca67c16..70e6fdf 100644 --- a/src/root/builtin/types/int/p_sub.rs +++ b/src/root/builtin/types/int/p_sub.rs @@ -35,7 +35,7 @@ impl BuiltinInlineFunction for IntPSub { format!( " mov rax, qword {lhs} neg rax - mov qword {return_into}, rax") + mov qword {return_into}, rax\n") } } diff --git a/src/root/builtin/types/int/sub.rs b/src/root/builtin/types/int/sub.rs index 7cb1da5..9634cfa 100644 --- a/src/root/builtin/types/int/sub.rs +++ b/src/root/builtin/types/int/sub.rs @@ -36,7 +36,7 @@ impl BuiltinInlineFunction for IntSub { format!( " mov rax, qword {lhs} sub rax, qword {rhs} - mov qword {return_into}, rax") + mov qword {return_into}, rax\n") } } diff --git a/test/test.o b/test/test.o new file mode 100644 index 0000000..6aa11e9 Binary files /dev/null and b/test/test.o differ diff --git a/test/test.out b/test/test.out new file mode 100755 index 0000000..bec97dc Binary files /dev/null and b/test/test.out differ diff --git a/types.toml b/types.toml new file mode 100644 index 0000000..894a261 --- /dev/null +++ b/types.toml @@ -0,0 +1,9 @@ +IntAdd=0 +IntSub=1 +IntPSub=2 +PrintI=3 +IntEq=4 +IntType=5 +PrintB=6 +BoolType=7 +ExitFunction=8