Skip to content

Commit

Permalink
Int eq + bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 18, 2024
1 parent 7fb49e0 commit 8bfee8f
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 57 deletions.
98 changes: 58 additions & 40 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
29 changes: 16 additions & 13 deletions main.why
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 1 addition & 1 deletion src/root/builtin/functions/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl BuiltinInlineFunction for ExitFunction {
// 0 us exit syscall
format!(" mov rax, 60
mov rdi, {lhs}
syscall")
syscall\n")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/root/builtin/types/int/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
62 changes: 62 additions & 0 deletions src/root/builtin/types/int/eq.rs
Original file line number Diff line number Diff line change
@@ -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<LocalAddress>, 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<TypeID> {
Some(IntType::id())
}
}
3 changes: 3 additions & 0 deletions src/root/builtin/types/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/root/builtin/types/int/p_sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/root/builtin/types/int/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
Binary file added test/test.o
Binary file not shown.
Binary file added test/test.out
Binary file not shown.
9 changes: 9 additions & 0 deletions types.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
IntAdd=0
IntSub=1
IntPSub=2
PrintI=3
IntEq=4
IntType=5
PrintB=6
BoolType=7
ExitFunction=8

0 comments on commit 8bfee8f

Please sign in to comment.