Skip to content

Commit

Permalink
Added first inline function
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 6, 2024
1 parent aad9441 commit f415fc4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 74 deletions.
31 changes: 17 additions & 14 deletions .idea/workspace.xml

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

10 changes: 8 additions & 2 deletions src/root/builtin/int/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ impl BuiltinInlineFunction for IntAdd {
}

fn inline(&self) -> InlineFunctionGenerator {
|args: &[LocalAddress], return_addr: Option<LocalAddress>| -> Result<String, WErr> {
todo!()
|args: &[LocalAddress], return_into: Option<LocalAddress>| -> String {
let lhs = args[0];
let rhs = args[1];
let return_into = return_into.unwrap();
format!(
" mov rax, qword {lhs}
add rax, qword {rhs}
mov qword {return_into}, rax")
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/root/builtin/int/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod add;
use b_box::b;
use unique_type_id::UniqueTypeId;
use crate::root::builtin::int::add::IntAdd;
use crate::root::compiler::assembly::utils::get_qword_stack_pointer;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse_function::parse_literal::{LiteralToken, LiteralTokens};
Expand Down Expand Up @@ -33,18 +32,17 @@ impl Type for IntType {
}

fn instantiate_from_literal(&self, location: &LocalAddress, literal: &LiteralToken) -> Result<String, WErr> {
let location = get_qword_stack_pointer(location);
Ok(match literal.literal() {
LiteralTokens::Bool(value) => {
if *value {
format!("\tmov {location}, 0")
format!(" mov qword {location}, 0")
}
else {
format!("\tmov {location}, 1")
format!(" mov qword {location}, 1")
}
}
LiteralTokens::Int(value) => {
format!("\tmov {location}, {value}")
format!(" mov qword {location}, {value}")
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/root/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn register_builtin(global_table: &mut GlobalDefinitionTable) {
register_int(global_table);
}

pub type InlineFunctionGenerator = fn(&[LocalAddress], Option<LocalAddress>) -> Result<String, WErr>;
pub type InlineFunctionGenerator = fn(&[LocalAddress], Option<LocalAddress>) -> String;

pub trait BuiltinInlineFunction {
fn id(&self) -> FunctionID;
Expand Down
94 changes: 47 additions & 47 deletions src/root/compiler/assembly/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,45 @@ pub fn align_16_bytes_plus_8(bytes: ByteSize) -> ByteSize {
}
}

pub fn get_qword_stack_pointer(address: &LocalAddress) -> String {
let address = address.0;

if address >= 0 {
format!("qword [rbp+{address}]")
} else {
format!("qword [rbp{address}]")
}
}

pub fn get_dword_stack_pointer(address: &LocalAddress) -> String {
let address = address.0;

if address >= 0 {
format!("dword [rbp+{address}]")
} else {
format!("dword [rbp{address}]")
}
}

pub fn get_word_stack_pointer(address: &LocalAddress) -> String {
let address = address.0;

if address >= 0 {
format!("word [rbp+{address}]")
} else {
format!("word [rbp{address}]")
}
}

pub fn get_byte_stack_pointer(address: &LocalAddress) -> String {
let address = address.0;

if address >= 0 {
format!("byte [rbp+{address}]")
} else {
format!("byte [rbp{address}]")
}
}
// pub fn get_qword_stack_pointer(address: &LocalAddress) -> String {
// let address = address.0;
//
// if address >= 0 {
// format!("qword [rbp+{address}]")
// } else {
// format!("qword [rbp{address}]")
// }
// }
//
// pub fn get_dword_stack_pointer(address: &LocalAddress) -> String {
// let address = address.0;
//
// if address >= 0 {
// format!("dword [rbp+{address}]")
// } else {
// format!("dword [rbp{address}]")
// }
// }
//
// pub fn get_word_stack_pointer(address: &LocalAddress) -> String {
// let address = address.0;
//
// if address >= 0 {
// format!("word [rbp+{address}]")
// } else {
// format!("word [rbp{address}]")
// }
// }
//
// pub fn get_byte_stack_pointer(address: &LocalAddress) -> String {
// let address = address.0;
//
// if address >= 0 {
// format!("byte [rbp+{address}]")
// } else {
// format!("byte [rbp{address}]")
// }
// }

pub fn copy(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
if amount == ByteSize(0) { return String::new(); }
Expand All @@ -99,23 +99,23 @@ pub fn copy(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
loop {
let to_write = amount.0 - written;
if to_write >= 8 {
output += &format!(" mov rax, {}\n", get_qword_stack_pointer(&LocalAddress(from + written as isize)));
output += &format!(" mov {}, rax", get_qword_stack_pointer(&LocalAddress(to + written as isize)));
output += &format!(" mov rax, qword {}\n", LocalAddress(from + written as isize));
output += &format!(" mov qword {}, rax", &LocalAddress(to + written as isize));
written += 8;
}
else if to_write >= 4 {
output += &format!(" mov eax, {}\n", get_dword_stack_pointer(&LocalAddress(from + written as isize)));
output += &format!(" mov {}, eax", get_dword_stack_pointer(&LocalAddress(to + written as isize)));
output += &format!(" mov rax, dword {}\n", LocalAddress(from + written as isize));
output += &format!(" mov dword {}, rax", &LocalAddress(to + written as isize));
written += 4;
}
else if to_write >= 2 {
output += &format!(" mov ax, {}\n", get_word_stack_pointer(&LocalAddress(from + written as isize)));
output += &format!(" mov {}, ax", get_word_stack_pointer(&LocalAddress(to + written as isize)));
output += &format!(" mov rax, word {}\n", LocalAddress(from + written as isize));
output += &format!(" mov word {}, rax", &LocalAddress(to + written as isize));
written += 2;
}
else if to_write >= 1 {
output += &format!(" mov ah, {}\n", get_byte_stack_pointer(&LocalAddress(from + written as isize)));
output += &format!(" mov {}, ah", get_byte_stack_pointer(&LocalAddress(to + written as isize)));
output += &format!(" mov rax, byte {}\n", LocalAddress(from + written as isize));
output += &format!(" mov byte {}, rax", &LocalAddress(to + written as isize));
written += 1;
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/root/compiler/compile_function.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use crate::root::builtin::int::IntType;
use crate::root::compiler::assembly::utils::{align_16_bytes, align_16_bytes_plus_8, get_function_tag, get_qword_stack_pointer};
use crate::root::compiler::assembly::utils::{align_16_bytes, align_16_bytes_plus_8, get_function_tag};
use crate::root::compiler::compile_evaluable::{compile_evaluable, compile_evaluable_into, compile_evaluable_reference};
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::WErr;
Expand Down Expand Up @@ -86,7 +86,7 @@ fn recursively_compile_lines(fid: FunctionID, lines: &[LineTokens], return_varia
let code = compile_evaluable_into(fid, rt.return_value(), address.clone(), &mut local_variables, global_table, function_calls)?;
contents += "\n";
contents += &code;
contents += &format!("\n\tmov rax, {}", get_qword_stack_pointer(address.local_address()));
contents += &format!("\n\tmov rax, qword {}", address.local_address());
}
else {
todo!()
Expand Down
2 changes: 1 addition & 1 deletion src/root/name_resolver/name_resolvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl GlobalDefinitionTable {

pub fn call_function(&self, function: FunctionID, arguments: &[LocalAddress], return_address: Option<LocalAddress>) -> Result<String, WErr> {
if let Some(inline) = self.inline_functions.get(&function) {
return inline(arguments, return_address);
return Ok(inline(arguments, return_address));
}

call_function(function, arguments, return_address)
Expand Down
15 changes: 13 additions & 2 deletions src/root/shared/common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter, write};
use derive_more::{Add, AddAssign, Display, Sub, SubAssign};
use derive_getters::{Dissolve, Getters};

Expand Down Expand Up @@ -41,11 +42,21 @@ impl Indirection {
#[display(fmt = "ByteSize: {}", .0)]
pub struct ByteSize(pub usize);

#[derive(Debug, PartialEq, Eq, Hash, Display, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
#[derive(Add, AddAssign, Sub, SubAssign)]
#[display(fmt = "LocalAddress: {}", .0)]
pub struct LocalAddress(pub isize);

impl Display for LocalAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.0 >= 0 {
write!(f, "[rbp+{}]", self.0)
}
else {
write!(f, "[rbp{}]", self.0)
}
}
}

#[derive(Getters, Clone, PartialEq)]
pub struct TypeRef {
type_id: TypeID,
Expand Down

0 comments on commit f415fc4

Please sign in to comment.