Skip to content

Commit

Permalink
Boolean comparators
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 20, 2024
1 parent 5c7d213 commit e494f6a
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 34 deletions.
23 changes: 16 additions & 7 deletions .idea/workspace.xml

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

32 changes: 16 additions & 16 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ section .text
main:
push rbp
mov rbp, rsp
mov qword [rbp-9], 2
mov qword [rbp-17], 3
mov rax, qword [rbp-17]
cmp rax, qword [rbp-9]
jg __5_0
mov qword [rbp-9], 1
mov qword [rbp-17], 2
mov rax, qword [rbp-9]
cmp rax, qword [rbp-17]
jnz __2_0
mov byte [rbp-1], 0
jmp __5_1
__5_0:
jmp __2_1
__2_0:
mov byte [rbp-1], 1
__5_1:
__2_1:
mov al, byte [rbp-1]
cmp al, 0
jz __7_2
mov rdi, __7_t_fstr
jmp __7_3
__7_2:
mov rdi, __7_f_fstr
__7_3:
jz __23_2
mov rdi, __23_t_fstr
jmp __23_3
__23_2:
mov rdi, __23_f_fstr
__23_3:
mov rsi, 0
mov al, 0
sub rsp, 17
Expand All @@ -36,5 +36,5 @@ main:


section .data_readonly
__7_f_fstr db `Boolean: False`,0
__7_t_fstr db `Boolean: True`,0
__23_f_fstr db `Boolean: False`,0
__23_t_fstr db `Boolean: True`,0
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
2 changes: 1 addition & 1 deletion main.why
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn main() -> int {
printb(2 < 3);
printb(1 != 2);

return 2;
}
122 changes: 122 additions & 0 deletions src/root/builtin/types/bool/comparators.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use unique_type_id::UniqueTypeId;
use crate::root::builtin::{BuiltinInlineFunction, f_id, InlineFunctionGenerator};
use crate::root::builtin::types::bool::BoolType;
use crate::root::builtin::types::int::IntType;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::shared::common::{FunctionID, LocalAddress, TypeID};

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

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

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

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
&[("lhs", BoolType::id().immediate()), ("rhs", BoolType::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 jmp_false = gt.get_unique_tag(BoolEq::id());
let jmp_true = gt.get_unique_tag(BoolEq::id());
let jmp_end = gt.get_unique_tag(BoolEq::id());

format!(
" cmp byte {lhs}, 0
jz {jmp_false}
mov al, byte {rhs}
mov byte {return_into}, al
jmp {jmp_end}
{jmp_false}:
cmp byte {rhs}, 0
jnz {jmp_true}
mov byte {return_into}, 1
jmp {jmp_end}
{jmp_true}:
mov byte {return_into}, 0
{jmp_end}:\n")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(BoolType::id())
}
}

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

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

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

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
&[("lhs", BoolType::id().immediate()), ("rhs", BoolType::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 jmp_false = gt.get_unique_tag(BoolNE::id());
let jmp_true = gt.get_unique_tag(BoolNE::id());
let jmp_end = gt.get_unique_tag(BoolNE::id());

format!(
" cmp byte {lhs}, 0
jnz {jmp_true}
mov al, byte {rhs}
mov byte {return_into}, al
jmp {jmp_end}
{jmp_true}:
cmp byte {rhs}, 0
jnz {jmp_false}
mov byte {return_into}, 1
jmp {jmp_end}
{jmp_false}:
mov byte {return_into}, 0
{jmp_end}:\n")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(BoolType::id())
}
}
4 changes: 4 additions & 0 deletions src/root/builtin/types/bool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ mod printb;
mod and;
mod or;
mod not;
mod comparators;

use b_box::b;
use unique_type_id::UniqueTypeId;
use crate::root::builtin::{BuiltinInlineFunction, f_id, InlineFunctionGenerator, t_id};
use crate::root::builtin::types::bool::and::{BoolAnd, BoolAsAnd};
use crate::root::builtin::types::bool::comparators::{BoolEq, BoolNE};
use crate::root::builtin::types::bool::not::BoolNot;
use crate::root::builtin::types::bool::or::{BoolAsOr, BoolOr};
use crate::root::builtin::types::bool::printb::PrintB;
Expand All @@ -22,6 +24,8 @@ pub fn register_bool(global_table: &mut GlobalDefinitionTable) {
global_table.register_builtin_type(b!(BoolType));
global_table.register_inline_function(&PrintB);
global_table.register_inline_function(&BoolAssign);
global_table.register_inline_function(&BoolEq);
global_table.register_inline_function(&BoolNE);
global_table.register_inline_function(&BoolAnd);
global_table.register_inline_function(&BoolAsAnd);
global_table.register_inline_function(&BoolOr);
Expand Down
68 changes: 60 additions & 8 deletions src/root/builtin/types/int/comparators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,58 @@ impl BuiltinInlineFunction for IntEq {
}
}

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

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

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

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

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 jmp_true = gt.get_unique_tag(IntNE::id());
let jmp_end = gt.get_unique_tag(IntNE::id());

format!(
" mov rax, qword {lhs}
cmp rax, qword {rhs}
jnz {jmp_true}
mov byte {return_into}, 0
jmp {jmp_end}
{jmp_true}:
mov byte {return_into}, 1
{jmp_end}:\n")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(IntType::id())
}
}

#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "u16"]
pub struct IntGT;
Expand Down Expand Up @@ -90,8 +142,8 @@ impl BuiltinInlineFunction for IntGT {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
let jmp_true = gt.get_unique_tag(IntEq::id());
let jmp_end = gt.get_unique_tag(IntEq::id());
let jmp_true = gt.get_unique_tag(IntGT::id());
let jmp_end = gt.get_unique_tag(IntGT::id());

format!(
" mov rax, qword {lhs}
Expand Down Expand Up @@ -142,8 +194,8 @@ impl BuiltinInlineFunction for IntLT {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
let jmp_true = gt.get_unique_tag(IntEq::id());
let jmp_end = gt.get_unique_tag(IntEq::id());
let jmp_true = gt.get_unique_tag(IntLT::id());
let jmp_end = gt.get_unique_tag(IntLT::id());

format!(
" mov rax, qword {rhs}
Expand Down Expand Up @@ -194,8 +246,8 @@ impl BuiltinInlineFunction for IntGE {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
let jmp_true = gt.get_unique_tag(IntEq::id());
let jmp_end = gt.get_unique_tag(IntEq::id());
let jmp_true = gt.get_unique_tag(IntGE::id());
let jmp_end = gt.get_unique_tag(IntGE::id());

format!(
" mov rax, qword {lhs}
Expand Down Expand Up @@ -246,8 +298,8 @@ impl BuiltinInlineFunction for IntLE {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
let jmp_true = gt.get_unique_tag(IntEq::id());
let jmp_end = gt.get_unique_tag(IntEq::id());
let jmp_true = gt.get_unique_tag(IntLE::id());
let jmp_end = gt.get_unique_tag(IntLE::id());

format!(
" mov rax, qword {rhs}
Expand Down
3 changes: 2 additions & 1 deletion src/root/builtin/types/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use unique_type_id::UniqueTypeId;

use crate::root::builtin::{BuiltinInlineFunction, f_id, InlineFunctionGenerator, t_id};
use crate::root::builtin::types::int::add::{IntAdd, IntAsAdd};
use crate::root::builtin::types::int::comparators::{IntEq, IntGE, IntGT, IntLE, IntLT};
use crate::root::builtin::types::int::comparators::{IntEq, IntGE, IntGT, IntLE, IntLT, IntNE};
use crate::root::builtin::types::int::div::{IntAsDiv, IntDiv};
use crate::root::builtin::types::int::modulo::{IntAsMod, IntMod};
use crate::root::builtin::types::int::mul::{IntAsMul, IntMul};
Expand Down Expand Up @@ -43,6 +43,7 @@ pub fn register_int(global_table: &mut GlobalDefinitionTable) {
global_table.register_inline_function(&IntMod);
global_table.register_inline_function(&IntAsMod);
global_table.register_inline_function(&IntEq);
global_table.register_inline_function(&IntNE);
global_table.register_inline_function(&IntGT);
global_table.register_inline_function(&IntLT);
global_table.register_inline_function(&IntGE);
Expand Down
Loading

0 comments on commit e494f6a

Please sign in to comment.