Skip to content

Commit

Permalink
More boolean operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 19, 2024
1 parent add622f commit fb9bb67
Show file tree
Hide file tree
Showing 16 changed files with 332 additions and 85 deletions.
29 changes: 24 additions & 5 deletions .idea/workspace.xml

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

33 changes: 25 additions & 8 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,41 @@ section .text
main:
push rbp
mov rbp, rsp
mov byte [rbp-1], 1

mov byte [rbp-1], 0
mov rax, rbp
add rax, -1
mov qword [rbp-9], rax
mov al, byte [rbp-1]
mov byte [rbp-11], al
mov al, byte [rbp-11]
cmp al, 0
jz __7_0
mov rdi, __7_t_fstr
mov byte [rbp-10], 0
jmp __7_1
__7_0:
mov rdi, __7_f_fstr
mov byte [rbp-10], 1
__7_1:
mov rdx, qword [rbp-9]
mov al, byte [rbp-10]
mov byte [rdx], al
mov al, byte [rbp-1]
mov byte [rbp-12], al
mov al, byte [rbp-12]
cmp al, 0
jz __7_2
mov rdi, __7_t_fstr
jmp __7_3
__7_2:
mov rdi, __7_f_fstr
__7_3:
mov rsi, 0
mov al, 0
sub rsp, 1
sub rsp, 12
extern printf
call printf
add rsp, 1
mov qword [rbp-9], 2
mov rax, qword [rbp-9]
add rsp, 12
mov qword [rbp-20], 2
mov rax, qword [rbp-20]
leave
ret

Expand Down
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
5 changes: 4 additions & 1 deletion main.why
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
fn main() -> int {
printb(true);
let a: bool = true;
&a = !a;

printb(a);

return 2;
}
50 changes: 34 additions & 16 deletions src/root/builtin/types/bool/and.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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::bool::printb::PrintB;
use crate::root::builtin::types::int::IntType;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;

Expand Down Expand Up @@ -28,56 +29,73 @@ impl BuiltinInlineFunction for BoolAnd {
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_into: Option<LocalAddress>, _, _| -> String {
|args: &[LocalAddress], return_into, gt, sz| -> String {

let jmp_false = gt.get_unique_tag(PrintB::id());
let jmp_end = gt.get_unique_tag(PrintB::id());

let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
format!(
" mov rax, qword {lhs}
mov rbx, qword {rhs}
or rax, rbx
mov qword {return_into}, rax\n")
" mov al, byte {lhs}
cmp al, 0
jz {jmp_false}
mov al, byte {rhs}
mov byte {return_into}, al
jmp {jmp_end}
{jmp_false}:
mov byte {return_into}, 0
{jmp_end}:
")
}
}

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

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

impl BuiltinInlineFunction for IntAsAdd {
impl BuiltinInlineFunction for BoolAsAnd {
fn id(&self) -> FunctionID {
f_id(IntAsAdd::unique_type_id().0)
f_id(BoolAsAnd::unique_type_id().0)
}

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
&[("lhs", IntType::id().with_indirection(1)), ("rhs", IntType::id().immediate())],
&[("lhs", BoolType::id().with_indirection(1)), ("rhs", BoolType::id().immediate())],
None
)
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_into: Option<LocalAddress>, _, _| -> String {
|args: &[LocalAddress], _, gt, sz| -> String {

let jmp_true = gt.get_unique_tag(PrintB::id());

let lhs = args[0];
let rhs = args[1];
format!(
" mov rax, qword {lhs}
mov rdx, qword {rhs}
add qword [rax], rdx\n")
" mov al, byte {rhs}
cmp al, 0
jnz {jmp_true}
mov rax, qword {lhs}
mov byte [rax], 0
{jmp_true}:
")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(IntType::id())
Some(BoolType::id())
}
}
54 changes: 52 additions & 2 deletions src/root/builtin/types/bool/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
mod printb;
mod and;
mod or;
mod not;

use b_box::b;
use unique_type_id::UniqueTypeId;
use crate::root::builtin::t_id;
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::not::BoolNot;
use crate::root::builtin::types::bool::or::{BoolAsOr, BoolOr};
use crate::root::builtin::types::bool::printb::PrintB;
use crate::root::builtin::types::int::IntType;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::name_resolver::resolve_function_signatures::FunctionSignature;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
use crate::root::shared::common::{ByteSize, LocalAddress, TypeID};
use crate::root::shared::common::{ByteSize, FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;

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(&BoolAnd);
global_table.register_inline_function(&BoolAsAnd);
global_table.register_inline_function(&BoolOr);
global_table.register_inline_function(&BoolAsOr);
global_table.register_inline_function(&BoolNot);
}

#[derive(UniqueTypeId)]
Expand Down Expand Up @@ -58,3 +71,40 @@ impl Type for BoolType {
})
}
}

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

impl BuiltinInlineFunction for BoolAssign {
fn id(&self) -> FunctionID {
f_id(BoolAssign::unique_type_id().0)
}

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
&[("lhs", BoolType::id().with_indirection(1)), ("rhs", BoolType::id().immediate())],
None
)
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_into: Option<LocalAddress>, _, _| -> String {
let lhs = args[0];
let rhs = args[1];
format!(
" mov rdx, qword {lhs}
mov al, byte {rhs}
mov byte [rdx], al\n")
}
}

fn parent_type(&self) -> Option<TypeID> {
Some(BoolType::id())
}
}
55 changes: 55 additions & 0 deletions src/root/builtin/types/bool/not.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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::bool::printb::PrintB;
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 BoolNot;

impl BuiltinInlineFunction for BoolNot {
fn id(&self) -> FunctionID {
f_id(BoolNot::unique_type_id().0)
}

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

fn signature(&self) -> FunctionSignature {
FunctionSignature::new_inline_builtin(
true,
&[("lhs", BoolType::id().immediate())],
Some(BoolType::id().immediate())
)
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_into, gt, sz| -> String {

let jmp_false = gt.get_unique_tag(PrintB::id());
let jmp_end = gt.get_unique_tag(PrintB::id());

let lhs = args[0];
let return_into = return_into.unwrap();
format!(
" mov al, byte {lhs}
cmp al, 0
jz {jmp_false}
mov byte {return_into}, 0
jmp {jmp_end}
{jmp_false}:
mov byte {return_into}, 1
{jmp_end}:
")
}
}

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

0 comments on commit fb9bb67

Please sign in to comment.