Skip to content

Commit

Permalink
Improved self coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Aug 1, 2024
1 parent 029d513 commit 0c4e67c
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 62 deletions.
70 changes: 33 additions & 37 deletions .idea/workspace.xml

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

70 changes: 63 additions & 7 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,70 @@ section .text
main:
push rbp
mov rbp, rsp
mov byte [rbp-1], 1
cmp byte [rbp-1], 0
jz main_0
main_1:
main_0:
mov qword [rbp-9], 0
mov rax, qword [rbp-9]
mov qword [rbp-8], 3
mov rax, qword [rbp-8]
mov qword [rbp-16], rax
sub rsp, 16
call _9
add rsp, 16
mov rax, rbp
add rax, -8
mov qword [rbp-24], rax
mov rdx, qword [rbp-24]
mov rax, qword [rdx+0]
mov qword [rbp-16], rax
mov rdi, __8_fstr
mov rsi, [rbp-16]
mov al, 0
sub rsp, 24
extern printf
call printf
add rsp, 24
mov qword [rbp-32], 8
mov rax, qword [rbp-32]
leave
ret


_9:
push rbp
mov rbp, rsp
mov rax, rbp
add rax, 16
mov qword [rbp-16], rax
mov rdx, qword [rbp-16]
mov rax, qword [rdx+0]
mov qword [rbp-8], rax
mov rdi, __8_fstr
mov rsi, [rbp-8]
mov al, 0
sub rsp, 16
extern printf
call printf
add rsp, 16
mov rax, rbp
add rax, 16
mov qword [rbp-24], rax
mov qword [rbp-32], 1
mov rax, qword [rbp-24]
mov rdx, qword [rbp-32]
add qword [rax], rdx
mov rax, rbp
add rax, 16
mov qword [rbp-48], rax
mov rdx, qword [rbp-48]
mov rax, qword [rdx+0]
mov qword [rbp-40], rax
mov rdi, __8_fstr
mov rsi, [rbp-40]
mov al, 0
sub rsp, 48
extern printf
call printf
add rsp, 48

leave
ret

section .data_readonly
__8_fstr db `Integer: %ld\n`,0
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
8 changes: 4 additions & 4 deletions linked_list.why
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ impl LL {
if (Node::is_null(*self.first)) {
self.first = Node::new(val);
} else {
(*self.first).add(val);
self.first.add(val);
};
}

fn print(&self) {
if (!Node::is_null(*self.first)) {
(*self.first).print();
self.first.print();
};
}
}
Expand All @@ -41,14 +41,14 @@ impl Node {
if (Node::is_null(*self.next)) {
self.next = Node::new(val);
} else {
(*self.next).add(val);
self.next.add(val);
};
}

fn print(&self) {
printi(*self.val);
if (!Node::is_null(*self.next)) {
(*self.next).print();
self.next.print();
};
}
}
Expand Down
18 changes: 14 additions & 4 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
struct Leak {
struct Test {
a: int
}

impl Test {
fn test(self) {
printi(*self.a);
self.a += 1;
printi(*self.a);
}
}

fn main() -> int {
if (true) {};
let x: Test = Test { a: 3 };
x::test();
printi(*x.a);

return 0;
}
return 8;
}
3 changes: 2 additions & 1 deletion src/root/builtin/core/referencing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::parser::parse::Location;
use crate::root::shared::common::AddressedTypeRef;
use crate::root::shared::common::{AddressedTypeRef, Indirection};

/// Sets `into` to the address of `to_ref`
pub fn set_reference(
Expand Down Expand Up @@ -57,5 +57,6 @@ pub fn set_deref(
*to_deref.local_address(),
*into.local_address(),
global_table.get_size(into.type_ref()),
Indirection(1),
))
}
9 changes: 7 additions & 2 deletions src/root/compiler/assembly/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::root::assembler::assembly_builder::AssemblyBuilder;
use crate::root::shared::common::{ByteSize, LocalAddress};
use crate::root::shared::common::{ByteSize, Indirection, LocalAddress};

// pub fn get_jump_tag(id: FunctionID, jump_id: usize) -> String {
// if id.is_main() {
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn align_16_bytes_plus_8(bytes: ByteSize) -> ByteSize {

/// Copies data. Expects `from` to be the address of a pointer pointing to the data to move
/// and `to` to be the target
pub fn copy_from_indirect(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
pub fn copy_from_indirect(from: LocalAddress, to: LocalAddress, amount: ByteSize, indirectness: Indirection) -> String {
if amount == ByteSize(0) {
return String::new();
}
Expand All @@ -86,7 +86,12 @@ pub fn copy_from_indirect(from: LocalAddress, to: LocalAddress, amount: ByteSize
let mut written = 0;
let mut output = AssemblyBuilder::new();

debug_assert!(indirectness.0 >= 1);

output.line(&format!("mov rdx, qword {from}"));
for _ in 0..(indirectness.0 - 1) {
output.line("mov rdx, qword [rdx]");
}

loop {
let to_write = amount.0 - written;
Expand Down
29 changes: 26 additions & 3 deletions src/root/compiler/evaluation/coerce_self.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::root::builtin::core::referencing::set_reference;
use crate::root::compiler::assembly::utils::copy_from_indirect;
use crate::root::compiler::local_variable_table::LocalVariableTable;
use crate::root::errors::WErr;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
Expand All @@ -16,9 +17,21 @@ pub fn coerce_self(
SelfType::None => (String::new(), current_self),
SelfType::CopySelf => {
if current_self.type_ref().indirection().has_indirection() {
todo!()
let new_self = global_table.add_local_variable_unnamed_base(
current_self.type_ref().type_id().immediate(),
local_variables,
);

(copy_from_indirect(
*current_self.local_address(),
*new_self.local_address(),
global_table.get_size(new_self.type_ref()),
Indirection(current_self.type_ref().indirection().0)
), new_self)
}
else {
(String::new(), current_self)
}
(String::new(), current_self)
}
SelfType::RefSelf => {
if !current_self.type_ref().indirection().has_indirection() {
Expand All @@ -38,7 +51,17 @@ pub fn coerce_self(
} else if *current_self.type_ref().indirection() == Indirection(1) {
(String::new(), current_self)
} else {
todo!()
let new_self = global_table.add_local_variable_unnamed_base(
current_self.type_ref().type_id().with_indirection(1),
local_variables,
);

(copy_from_indirect(
*current_self.local_address(),
*new_self.local_address(),
global_table.get_size(new_self.type_ref()),
Indirection(current_self.type_ref().indirection().0 - 1) // Want a ref, not inner type
), new_self)
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/root/errors/evaluable_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ pub enum EvalErrs {
TypeDoesntHaveAttributes(String),
#[error("Type ({0}) cannot be initialised")]
TypeCannotBeInitialised(String),
#[error("Type ({0}) cannot be initialised from a literal")]
TypeCannotBeInitialisedByLiteral(String)
}

// return Err(WErr::n(OpWrongReturnType(global_table.get_type_name(into.type_ref()), global_table.get_type_name(&new_type)), location.clone()));
4 changes: 2 additions & 2 deletions src/root/name_resolver/resolve_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::root::errors::name_resolver_errors::NRErrs;
use crate::root::errors::WErr;
use derive_getters::Getters;
use itertools::Itertools;

use crate::root::errors::evaluable_errors::EvalErrs;
use crate::root::name_resolver::name_resolvers::GlobalDefinitionTable;
use crate::root::name_resolver::resolve_function_signatures::resolve_function_signature;
use crate::root::name_resolver::resolve_type_sizes::{resolve_type_sizes, UnsizedUserType};
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Type for UserType {
location: &LocalAddress,
literal: &LiteralToken,
) -> Result<String, WErr> {
todo!()
WErr::ne(EvalErrs::TypeCannotBeInitialisedByLiteral(self.name().clone()), literal.location().clone())
}
}

Expand Down

0 comments on commit 0c4e67c

Please sign in to comment.