Skip to content

Commit

Permalink
Deref and assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-M-Lucas committed Jun 21, 2024
1 parent d896fef commit 4215348
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 66 deletions.
33 changes: 24 additions & 9 deletions .idea/workspace.xml

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

40 changes: 12 additions & 28 deletions build/out.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,25 @@ section .text
main:
push rbp
mov rbp, rsp
mov qword [rbp-8], 0
main_0:
mov rax, qword [rbp-8]
mov qword [rbp-17], rax
mov qword [rbp-25], 1000000000
mov rax, qword [rbp-25]
cmp rax, qword [rbp-17]
jg __18_2
mov byte [rbp-9], 0
jmp __18_3
__18_2:
mov byte [rbp-9], 1
__18_3:
cmp byte [rbp-9], 0
jz main_1
mov qword [rbp-8], 13
mov rax, rbp
add rax, -8
mov qword [rbp-33], rax
mov qword [rbp-41], 1
mov rax, qword [rbp-33]
mov rdx, qword [rbp-41]
add qword [rax], rdx
jmp main_0
main_1:
mov qword [rbp-16], rax
mov qword [rbp-24], 12
mov rdx, qword [rbp-16]
mov rax, qword [rbp-24]
mov qword [rdx+0], rax
mov rax, qword [rbp-8]
mov qword [rbp-33], rax
mov qword [rbp-32], rax
mov rdi, __8_fstr
mov rsi, [rbp-33]
mov rsi, [rbp-32]
mov al, 0
sub rsp, 33
sub rsp, 32
extern printf
call printf
add rsp, 33
mov qword [rbp-41], 1
mov rax, qword [rbp-41]
add rsp, 32
mov qword [rbp-40], 1
mov rax, qword [rbp-40]
leave
ret

Expand Down
Binary file modified build/out.o
Binary file not shown.
Binary file modified build/out.out
Binary file not shown.
10 changes: 3 additions & 7 deletions main.why
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
fn main() -> int {
let a: int = 0;
while (a < 1000000000) {
&a += 1;
}
let a: int = 13;
&a = 12;
printi(a);

return 1;
}

// 149563
// 116356
2 changes: 2 additions & 0 deletions src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::root::name_resolver::resolve::resolve;
use shared::common::ByteSize;
use crate::root::errors::WErr;
use crate::root::runner::{assemble, link_gcc, run};
#[cfg(debug_assertions)]
pub const DEBUG_ON_ERROR: bool = true;

// #[cfg(target_os = "windows")]
// use crate::root::runner::run;
Expand Down
78 changes: 78 additions & 0 deletions src/root/compiler/assembly/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,84 @@ pub fn align_16_bytes_plus_8(bytes: ByteSize) -> ByteSize {
// }
// }

pub fn copy_from_indirect(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
if amount == ByteSize(0) { return String::new(); }

let to = to.0;
let mut written = 0;
let mut output = AssemblyBuilder::new();

output.line(&format!("mov rdx, qword {from}"));

loop {
let to_write = amount.0 - written;
if to_write >= 8 {
output.line(&format!("mov rax, qword [rdx+{written}]", ));
output.line(&format!("mov qword {}, rax", &LocalAddress(to + written as isize)));
written += 8;
}
else if to_write >= 4 {
output.line(&format!("mov eax, dword [rdx+{written}]", ));
output.line(&format!("mov dword {}, eax", &LocalAddress(to + written as isize)));
written += 4;
}
else if to_write >= 2 {
output.line(&format!("mov ax, word [rdx+{written}]", ));
output.line(&format!("mov word {}, ax", &LocalAddress(to + written as isize)));
written += 2;
}
else if to_write >= 1 {
output.line(&format!("mov al, byte [rdx+{written}]", ));
output.line(&format!("mov byte {}, al", &LocalAddress(to + written as isize)));
written += 1;
}
else {
break;
}
if written == amount.0 { break; }
}
output.finish()
}

pub fn copy_to_indirect(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
if amount == ByteSize(0) { return String::new(); }

let from = from.0;
let mut written = 0;

let mut output = AssemblyBuilder::new();
output.line(&format!("mov rdx, qword {to}"));

loop {
let to_write = amount.0 - written;
if to_write >= 8 {
output.line(&format!("mov rax, qword {}", LocalAddress(from + written as isize)));
output.line(&format!("mov qword [rdx+{written}], rax"));
written += 8;
}
else if to_write >= 4 {
output.line(&format!("mov eax, dword {}", LocalAddress(from + written as isize)));
output.line(&format!("mov dword [rdx+{written}], eax"));
written += 4;
}
else if to_write >= 2 {
output.line(&format!("mov ax, word {}", LocalAddress(from + written as isize)));
output.line(&format!("mov word [rdx+{written}], ax"));
written += 2;
}
else if to_write >= 1 {
output.line(&format!("mov al, byte {}", LocalAddress(from + written as isize)));
output.line(&format!("mov byte [rdx+{written}], byte"));
written += 1;
}
else {
break;
}
if written == amount.0 { break; }
}
output.finish()
}

pub fn copy(from: LocalAddress, to: LocalAddress, amount: ByteSize) -> String {
if amount == ByteSize(0) { return String::new(); }

Expand Down
Loading

0 comments on commit 4215348

Please sign in to comment.