From 6d805dc2ec4386da3b3aac304c8c030353bd98c6 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Sat, 28 Sep 2024 16:28:05 +0200 Subject: [PATCH] fixing the cli --- hblang/src/main.rs | 4 +- hblang/src/son.rs | 25 ++++++++- hblang/tests/son_tests_arithmetic.txt | 6 +-- hblang/tests/son_tests_branch_assignments.txt | 8 +-- hblang/tests/son_tests_comments.txt | 6 +-- .../son_tests_const_folding_with_arg.txt | 6 +-- .../son_tests_exhaustive_loop_testing.txt | 52 +++++++------------ hblang/tests/son_tests_fb_driver.txt | 14 ++--- hblang/tests/son_tests_functions.txt | 10 +--- .../son_tests_hex_octal_binary_literals.txt | 6 +-- hblang/tests/son_tests_loops.txt | 22 ++++---- hblang/tests/son_tests_pointer_opts.txt | 10 ++-- hblang/tests/son_tests_pointers.txt | 14 ++--- hblang/tests/son_tests_variables.txt | 6 +-- 14 files changed, 71 insertions(+), 118 deletions(-) diff --git a/hblang/src/main.rs b/hblang/src/main.rs index c1e0386..7eccd1f 100644 --- a/hblang/src/main.rs +++ b/hblang/src/main.rs @@ -11,10 +11,10 @@ fn main() -> std::io::Result<()> { } hblang::run_compiler( - args.get(1).copied().unwrap_or("main.hb"), + args.iter().filter(|a| !a.starts_with('-')).nth(1).copied().unwrap_or("main.hb"), hblang::Options { fmt: args.contains(&"--fmt"), - fmt_current: args.contains(&"--fmt-current"), + fmt_current: args.contains(&"--fmt-stdout"), dump_asm: args.contains(&"--dump-asm"), extra_threads: args .iter() diff --git a/hblang/src/son.rs b/hblang/src/son.rs index 0c12bf9..8da65fe 100644 --- a/hblang/src/son.rs +++ b/hblang/src/son.rs @@ -1994,18 +1994,39 @@ impl Codegen { ret_rel.apply_jump(&mut self.ci.code, end as _, 0); } + let mut stripped_prelude_size = 0; '_close_function: { - let pushed = (saved as i64 + 1) * 8; + let pushed = + (saved as i64 + (std::mem::take(&mut self.ci.call_count) != 0) as i64) * 8; let stack = std::mem::take(&mut self.ci.stack_size) as i64; + match (pushed, stack) { + (0, 0) => { + stripped_prelude_size = + instrs::addi64(0, 0, 0).0 + instrs::st(0, 0, 0, 0).0; + self.ci.code.drain(0..stripped_prelude_size); + break '_close_function; + } + (0, stack) => { + write_reloc(&mut self.ci.code, 3, -stack, 8); + stripped_prelude_size = instrs::addi64(0, 0, 0).0; + let end = stripped_prelude_size + instrs::st(0, 0, 0, 0).0; + self.ci.code.drain(stripped_prelude_size..end); + self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, stack as _)); + break '_close_function; + } + _ => {} + } + write_reloc(&mut self.ci.code, 3, -(pushed + stack), 8); write_reloc(&mut self.ci.code, 3 + 8 + 3, stack, 8); write_reloc(&mut self.ci.code, 3 + 8 + 3 + 8, pushed, 2); self.ci.emit(instrs::ld(reg::RET_ADDR, reg::STACK_PTR, stack as _, pushed as _)); self.ci.emit(instrs::addi64(reg::STACK_PTR, reg::STACK_PTR, (pushed + stack) as _)); - self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0)); } + self.ci.relocs.iter_mut().for_each(|r| r.reloc.offset -= stripped_prelude_size as u32); + self.ci.emit(instrs::jala(reg::ZERO, reg::RET_ADDR, 0)); } self.tys.funcs[id as usize].code.append(&mut self.ci.code); diff --git a/hblang/tests/son_tests_arithmetic.txt b/hblang/tests/son_tests_arithmetic.txt index 21cfc6c..e10b421 100644 --- a/hblang/tests/son_tests_arithmetic.txt +++ b/hblang/tests/son_tests_arithmetic.txt @@ -1,10 +1,6 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 0d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 77 +code size: 29 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_branch_assignments.txt b/hblang/tests/son_tests_branch_assignments.txt index d780a40..bec0bad 100644 --- a/hblang/tests/son_tests_branch_assignments.txt +++ b/hblang/tests/son_tests_branch_assignments.txt @@ -1,6 +1,4 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 1d JNE r2, r1, :0 JMP :1 @@ -9,9 +7,7 @@ main: LI64 r1, 2d JMP :1 2: LI64 r1, 3d - 1: LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d - JALA r0, r31, 0a -code size: 127 + 1: JALA r0, r31, 0a +code size: 79 ret: 2 status: Ok(()) diff --git a/hblang/tests/son_tests_comments.txt b/hblang/tests/son_tests_comments.txt index 33f3145..22a5c60 100644 --- a/hblang/tests/son_tests_comments.txt +++ b/hblang/tests/son_tests_comments.txt @@ -1,8 +1,4 @@ foo: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a main: ADDI64 r254, r254, -8d @@ -12,6 +8,6 @@ main: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 143 +code size: 95 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_const_folding_with_arg.txt b/hblang/tests/son_tests_const_folding_with_arg.txt index 21cfc6c..e10b421 100644 --- a/hblang/tests/son_tests_const_folding_with_arg.txt +++ b/hblang/tests/son_tests_const_folding_with_arg.txt @@ -1,10 +1,6 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 0d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 77 +code size: 29 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_exhaustive_loop_testing.txt b/hblang/tests/son_tests_exhaustive_loop_testing.txt index 3895fb6..9670e99 100644 --- a/hblang/tests/son_tests_exhaustive_loop_testing.txt +++ b/hblang/tests/son_tests_exhaustive_loop_testing.txt @@ -1,24 +1,20 @@ continue_and_state_change: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h - LI64 r32, 3d - LI64 r33, 4d - LI64 r34, 2d - LI64 r35, 10d - 6: JLTU r2, r35, :0 + LI64 r7, 3d + LI64 r8, 4d + LI64 r9, 2d + LI64 r10, 10d + 6: JLTU r2, r10, :0 CP r1, r2 JMP :1 - 0: JNE r2, r34, :2 - CP r2, r33 + 0: JNE r2, r9, :2 + CP r2, r8 JMP :3 - 2: JNE r2, r32, :4 + 2: JNE r2, r7, :4 LI64 r1, 0d 1: JMP :5 4: ADDI64 r2, r2, 1d 3: JMP :6 - 5: LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d - JALA r0, r31, 0a + 5: JALA r0, r31, 0a main: ADDI64 r254, r254, -80d ST r31, r254, 0a, 80h @@ -70,37 +66,29 @@ main: ADDI64 r254, r254, 80d JALA r0, r31, 0a multiple_breaks: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h - LI64 r32, 3d - LI64 r33, 10d - 4: JLTU r2, r33, :0 + LI64 r6, 3d + LI64 r5, 10d + 4: JLTU r2, r5, :0 CP r1, r2 JMP :1 0: ADDI64 r1, r2, 1d - JNE r1, r32, :2 + JNE r1, r6, :2 1: JMP :3 2: CP r2, r1 JMP :4 - 3: LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d - JALA r0, r31, 0a + 3: JALA r0, r31, 0a state_change_in_break: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h - LI64 r32, 3d - LI64 r33, 10d - 4: JLTU r2, r33, :0 + LI64 r5, 3d + LI64 r6, 10d + 4: JLTU r2, r6, :0 CP r1, r2 JMP :1 - 0: JNE r2, r32, :2 + 0: JNE r2, r5, :2 LI64 r1, 0d 1: JMP :3 2: ADDI64 r2, r2, 1d JMP :4 - 3: LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d - JALA r0, r31, 0a -code size: 713 + 3: JALA r0, r31, 0a +code size: 569 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_fb_driver.txt b/hblang/tests/son_tests_fb_driver.txt index 1fbe635..6d3b629 100644 --- a/hblang/tests/son_tests_fb_driver.txt +++ b/hblang/tests/son_tests_fb_driver.txt @@ -42,20 +42,12 @@ main: ADDI64 r254, r254, 64d JALA r0, r31, 0a set_pixel: - ADDI64 r254, r254, -16d - ST r31, r254, 0a, 16h - MUL64 r32, r4, r3 - ADD64 r1, r32, r2 - LD r31, r254, 0a, 16h - ADDI64 r254, r254, 16d + MUL64 r8, r4, r3 + ADD64 r1, r8, r2 JALA r0, r31, 0a x86_fb_ptr: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 100d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 426 +code size: 330 ret: 3000 status: Ok(()) diff --git a/hblang/tests/son_tests_functions.txt b/hblang/tests/son_tests_functions.txt index e89665a..686e680 100644 --- a/hblang/tests/son_tests_functions.txt +++ b/hblang/tests/son_tests_functions.txt @@ -1,16 +1,8 @@ add_one: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h ADDI64 r1, r2, 1d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a add_two: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h ADDI64 r1, r2, 2d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a main: ADDI64 r254, r254, -16d @@ -24,6 +16,6 @@ main: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 248 +code size: 152 ret: 33 status: Ok(()) diff --git a/hblang/tests/son_tests_hex_octal_binary_literals.txt b/hblang/tests/son_tests_hex_octal_binary_literals.txt index 21cfc6c..e10b421 100644 --- a/hblang/tests/son_tests_hex_octal_binary_literals.txt +++ b/hblang/tests/son_tests_hex_octal_binary_literals.txt @@ -1,10 +1,6 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 0d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 77 +code size: 29 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_loops.txt b/hblang/tests/son_tests_loops.txt index 95c64c7..f07df75 100644 --- a/hblang/tests/son_tests_loops.txt +++ b/hblang/tests/son_tests_loops.txt @@ -1,19 +1,15 @@ fib: - ADDI64 r254, r254, -32d - ST r31, r254, 0a, 32h - LI64 r32, 1d - LI64 r33, 0d - CP r1, r33 - 2: JNE r2, r33, :0 + LI64 r7, 1d + LI64 r4, 0d + CP r1, r4 + 2: JNE r2, r4, :0 JMP :1 - 0: ADD64 r34, r32, r1 + 0: ADD64 r3, r7, r1 ADDI64 r2, r2, -1d - CP r1, r32 - CP r32, r34 + CP r1, r7 + CP r7, r3 JMP :2 - 1: LD r31, r254, 0a, 32h - ADDI64 r254, r254, 32d - JALA r0, r31, 0a + 1: JALA r0, r31, 0a main: ADDI64 r254, r254, -8d ST r31, r254, 0a, 8h @@ -22,6 +18,6 @@ main: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 202 +code size: 154 ret: 55 status: Ok(()) diff --git a/hblang/tests/son_tests_pointer_opts.txt b/hblang/tests/son_tests_pointer_opts.txt index fb6ad34..a478596 100644 --- a/hblang/tests/son_tests_pointer_opts.txt +++ b/hblang/tests/son_tests_pointer_opts.txt @@ -1,10 +1,6 @@ clobber: - ADDI64 r254, r254, -16d - ST r31, r254, 0a, 16h - LI64 r32, 4d - ST r32, r2, 0a, 8h - LD r31, r254, 0a, 16h - ADDI64 r254, r254, 16d + LI64 r3, 4d + ST r3, r2, 0a, 8h JALA r0, r31, 0a main: ADDI64 r254, r254, -32d @@ -18,6 +14,6 @@ main: LD r31, r254, 8a, 24h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 214 +code size: 166 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_pointers.txt b/hblang/tests/son_tests_pointers.txt index 05763eb..6a62ee7 100644 --- a/hblang/tests/son_tests_pointers.txt +++ b/hblang/tests/son_tests_pointers.txt @@ -1,8 +1,4 @@ drop: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a main: ADDI64 r254, r254, -32d @@ -19,13 +15,9 @@ main: ADDI64 r254, r254, 32d JALA r0, r31, 0a modify: - ADDI64 r254, r254, -16d - ST r31, r254, 0a, 16h - LI64 r32, 2d - ST r32, r2, 0a, 8h - LD r31, r254, 0a, 16h - ADDI64 r254, r254, 16d + LI64 r3, 2d + ST r3, r2, 0a, 8h JALA r0, r31, 0a -code size: 283 +code size: 187 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_variables.txt b/hblang/tests/son_tests_variables.txt index 21cfc6c..e10b421 100644 --- a/hblang/tests/son_tests_variables.txt +++ b/hblang/tests/son_tests_variables.txt @@ -1,10 +1,6 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h LI64 r1, 0d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 77 +code size: 29 ret: 0 status: Ok(())