Skip to content

Commit

Permalink
Printing and function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 16, 2024
1 parent 6e668e4 commit 133922f
Show file tree
Hide file tree
Showing 23 changed files with 505 additions and 141 deletions.
68 changes: 39 additions & 29 deletions .idea/workspace.xml

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

145 changes: 128 additions & 17 deletions build/out.asm
Original file line number Diff line number Diff line change
@@ -1,35 +1,146 @@
global main

section .text
section .text

_1:
main:
push rbp
mov rbp, rsp
mov qword [rbp-8], 3
mov rax, qword [rbp-8]
mov qword [rbp-16], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-16]
mov al, 0
sub rsp, 16
extern printf
call printf
add rsp, 16
mov rax, qword [rbp-8]
mov qword [rbp-24], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-24]
mov al, 0
sub rsp, 24
extern printf
call printf
add rsp, 24
mov rax, qword [rbp-8]
mov qword [rbp-32], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-32]
mov al, 0
sub rsp, 32
extern printf
call printf
add rsp, 32
mov rax, qword [rbp-8]
mov qword [rbp-40], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-40]
mov al, 0
sub rsp, 40
extern printf
call printf
add rsp, 40
mov rax, qword [rbp-8]
mov qword [rbp-48], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-48]
mov al, 0
sub rsp, 48
extern printf
call printf
add rsp, 48
mov rax, qword [rbp-8]
mov qword [rbp-56], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-56]
mov al, 0
sub rsp, 56
extern printf
call printf
add rsp, 56
mov rax, qword [rbp-8]
mov qword [rbp-64], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-64]
mov al, 0
sub rsp, 64
extern printf
call printf
add rsp, 64
mov rax, qword [rbp-8]
mov qword [rbp-72], rax
push 0
mov rdi, __10_fstr
mov rsi, [rbp-72]
mov al, 0
sub rsp, 72
extern printf
call printf
add rsp, 72
mov rax, qword [rbp-8]
mov qword [rbp-88], rax
mov rax, qword [rbp-88]
mov qword [rbp-104], rax
sub rsp, 104
call _2
add rsp, 104
mov rax, qword [rbp-96]
mov qword [rbp-80], rax
mov rax, qword [rbp-80]
leave
ret

_2:
push rbp
mov rbp, rsp
mov qword [rbp-8], 0
mov rax, qword [rbp+16]
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
mov qword [rbp-32], rax
sub rsp, 32
call _1
add rsp, 32
mov rax, qword [rbp-24]
mov qword [rbp-8], rax
mov qword [rbp-32], 1
mov rax, qword [rbp-8]
sub rax, qword [rbp-16]
add rax, qword [rbp-32]
mov qword [rbp+24], rax
leave
ret

main:
_1:
push rbp
mov rbp, rsp
mov qword [rbp-8], -12
mov rax, qword [rbp-8]
mov qword [rbp-24], rax
mov rax, qword [rbp-24]
mov qword [rbp-40], rax
sub rsp, 40
call _1
add rsp, 40
mov rax, qword [rbp-32]
mov rax, qword [rbp+16]
mov qword [rbp-8], rax
mov rax, qword [rbp+16]
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
leave
ret
mov rax, qword [rbp-8]
add rax, qword [rbp-16]
mov qword [rbp+24], rax
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.
22 changes: 16 additions & 6 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
impl int {
fn p_sub(self) -> Self {
return 0 - self;
}
fn test_two(a: int) -> int {
return a + a;
}

fn test(a: int) -> int {
return test_two(a) + 1;
}

fn main() -> int {
let x: int = -12;
return -x;
let x: int = 3;
printi(x);
printi(x);
printi(x);
printi(x);
printi(x);
printi(x);
printi(x);
printi(x);
return test(x);
}

Empty file.
21 changes: 17 additions & 4 deletions src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
pub mod int;
pub mod types;
pub mod functions;

use crate::root::builtin::int::{IntType, register_int};
use crate::root::builtin::types::int::register_int;
use crate::root::compiler::global_tracker::GlobalTracker;
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::shared::common::{FunctionID, LocalAddress, TypeID};
use crate::root::shared::common::{ByteSize, FunctionID, LocalAddress, TypeID};
use crate::root::shared::types::Type;

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

pub type InlineFunctionGenerator = fn(&[LocalAddress], Option<LocalAddress>) -> String;
pub type InlineFunctionGenerator = fn(&[LocalAddress], Option<LocalAddress>, &mut GlobalTracker, ByteSize) -> String;

const fn f_id(id: u16) -> FunctionID {
FunctionID(-(id as isize) - 1)
}

const fn t_id(id: u16) -> TypeID {
TypeID(-(id as isize) - 1)
}

pub trait BuiltinInlineFunction {
fn id(&self) -> FunctionID;
fn name(&self) -> &'static str;
fn signature(&self) -> FunctionSignature;
fn inline(&self) -> InlineFunctionGenerator;
fn requirements(&self) -> Option<Vec<String>> {
None
}
fn parent_type(&self) -> Option<TypeID>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use unique_type_id::UniqueTypeId;
use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator};
use crate::root::builtin::int::IntType;
use crate::root::builtin::{BuiltinInlineFunction, InlineFunctionGenerator, f_id};
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;
Expand All @@ -13,7 +13,7 @@ pub struct IntAdd;

impl BuiltinInlineFunction for IntAdd {
fn id(&self) -> FunctionID {
FunctionID(-(IntAdd::unique_type_id().0 as isize) - 1)
f_id(IntAdd::unique_type_id().0)
}

fn name(&self) -> &'static str {
Expand All @@ -29,7 +29,7 @@ impl BuiltinInlineFunction for IntAdd {
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_into: Option<LocalAddress>| -> String {
|args: &[LocalAddress], return_into: Option<LocalAddress>, _, _| -> String {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
Expand Down
Loading

0 comments on commit 133922f

Please sign in to comment.