From b8ff503c14f63cfa30cb1403907501cddfa8098d Mon Sep 17 00:00:00 2001 From: mlokr Date: Fri, 20 Sep 2024 11:01:10 +0200 Subject: [PATCH] fixing report bug --- Cargo.lock | 2 +- hbbytecode/src/lib.rs | 8 +- hblang/src/parser.rs | 2 +- hblang/src/son.rs | 36 ++++- hblang/tests/son_tests_branch_assignments.txt | 12 +- hblang/tests/son_tests_comments.txt | 19 ++- .../son_tests_exhaustive_loop_testing.txt | 129 +++++++++--------- hblang/tests/son_tests_fb_driver.txt | 67 ++++----- hblang/tests/son_tests_functions.txt | 37 +++-- hblang/tests/son_tests_if_statements.txt | 39 +++--- hblang/tests/son_tests_loops.txt | 35 +++-- 11 files changed, 206 insertions(+), 180 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49297bc3..9b52caf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -58,7 +58,7 @@ dependencies = [ [[package]] name = "regalloc2" version = "0.10.2" -source = "git+https://github.com/jakubDoka/regalloc2#7e74b2fde4f022816cded93ab5685e46f8e3a159" +source = "git+https://github.com/jakubDoka/regalloc2#52b2bbe908e78af1715de88f562f62a83e36ca96" dependencies = [ "hashbrown", "rustc-hash", diff --git a/hbbytecode/src/lib.rs b/hbbytecode/src/lib.rs index 3a45841c..da1f4b53 100644 --- a/hbbytecode/src/lib.rs +++ b/hbbytecode/src/lib.rs @@ -116,7 +116,7 @@ pub fn disasm( let prev = *binary; - *binary = &binary[..off as usize]; + *binary = &binary[off as usize..]; let mut label_count = 0; while let Some(&byte) = binary.first() { @@ -168,7 +168,7 @@ pub fn disasm( writeln!(out, "{name}:")?; - *binary = &binary[..off as usize]; + *binary = &binary[off as usize..]; while let Some(&byte) = binary.first() { let offset: i32 = (prev.len() - binary.len()).try_into().unwrap(); if offset as u32 == off + len { @@ -219,7 +219,9 @@ pub fn disasm( } else { let local_has_oob = global_offset < off || global_offset > off + len - || instr_from_byte(prev[global_offset as usize]).is_err() + || prev + .get(global_offset as usize) + .map_or(true, |&b| instr_from_byte(b).is_err()) || prev[global_offset as usize] == 0; has_oob |= local_has_oob; let label = labels.get(&global_offset).unwrap(); diff --git a/hblang/src/parser.rs b/hblang/src/parser.rs index 82677d8e..0f5fd6ab 100644 --- a/hblang/src/parser.rs +++ b/hblang/src/parser.rs @@ -102,7 +102,7 @@ impl<'a, 'b> Parser<'a, 'b> { report_to( self.lexer.source(), self.path, - id.ident, + ident::pos(id.ident), format_args!("undeclared identifier: {}", self.lexer.slice(ident::range(id.ident))), &mut errors, ); diff --git a/hblang/src/son.rs b/hblang/src/son.rs index 51da797e..c146d7d5 100644 --- a/hblang/src/son.rs +++ b/hblang/src/son.rs @@ -1280,6 +1280,9 @@ impl Codegen { let inps = [VOID, lhs, rhs]; Some(self.ci.nodes.new_node(ty::bin_ret(ty, op), Kind::BinOp { op }, inps)) } + Expr::UnOp { pos, op: TokenKind::Band, val } => { + todo!() + } Expr::UnOp { pos, op, val } => { let val = self.expr_ctx(val, ctx)?; if !self.tof(val).is_integer() { @@ -1763,20 +1766,23 @@ impl Codegen { let mut nodes = std::mem::take(&mut self.ci.nodes); let func = Function::new(&mut nodes, &self.tys, sig); - let env = regalloc2::MachineEnv { + let mut env = regalloc2::MachineEnv { preferred_regs_by_class: [ - (1..13).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), + (1..12).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), vec![], vec![], ], non_preferred_regs_by_class: [ - (13..64).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), + (12..64).map(|i| regalloc2::PReg::new(i, regalloc2::RegClass::Int)).collect(), vec![], vec![], ], scratch_by_class: Default::default(), fixed_stack_slots: Default::default(), }; + if self.ci.call_count != 0 { + std::mem::swap(&mut env.preferred_regs_by_class, &mut env.non_preferred_regs_by_class); + }; let options = regalloc2::RegallocOptions { verbose_log: false, validate_ssa: true }; let output = regalloc2::run(&func, &env, &options).unwrap_or_else(|err| panic!("{err}")); @@ -2270,8 +2276,17 @@ impl<'a> Function<'a> { self.emit_node(node.outputs[0], nid); } Kind::CInt { .. } => { - let ops = vec![self.drg(nid)]; - self.add_instr(nid, ops); + let unused = node.outputs.into_iter().all(|o| { + let ond = &self.nodes[o]; + matches!(ond.kind, Kind::BinOp { op } + if op.imm_binop(ond.ty.is_signed(), 8).is_some() + && op.cond_op(ond.ty.is_signed()).is_none()) + }); + + if !unused { + let ops = vec![self.drg(nid)]; + self.add_instr(nid, ops); + } } Kind::Phi => {} Kind::Tuple { index } => { @@ -2747,7 +2762,7 @@ mod tests { if_statements => README; loops => README; fb_driver => README; - //pointers => README; + pointers => README; //structs => README; //different_types => README; //struct_operators => README; @@ -2768,8 +2783,15 @@ mod tests { //inline => README; //inline_test => README; const_folding_with_arg => README; - // FIXME: contains redundant copies branch_assignments => README; exhaustive_loop_testing => README; + //idk => README; + //comptime_min_reg_leak => README; + //some_generic_code => README; + //integer_inference_issues => README; + //writing_into_string => README; + //request_page => README; + //tests_ptr_to_ptr_copy => README; + //wide_ret => README; } } diff --git a/hblang/tests/son_tests_branch_assignments.txt b/hblang/tests/son_tests_branch_assignments.txt index 4ef23153..d780a408 100644 --- a/hblang/tests/son_tests_branch_assignments.txt +++ b/hblang/tests/son_tests_branch_assignments.txt @@ -1,16 +1,16 @@ main: - ADDI64 r254, r254, -16d - ST r31, r254, 0a, 16h + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h LI64 r1, 1d JNE r2, r1, :0 JMP :1 - 0: LI64 r32, 0d - JNE r2, r32, :2 + 0: LI64 r7, 0d + JNE r2, r7, :2 LI64 r1, 2d JMP :1 2: LI64 r1, 3d - 1: LD r31, r254, 0a, 16h - ADDI64 r254, r254, 16d + 1: LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d JALA r0, r31, 0a code size: 127 ret: 2 diff --git a/hblang/tests/son_tests_comments.txt b/hblang/tests/son_tests_comments.txt index d8a4e31e..33f3145a 100644 --- a/hblang/tests/son_tests_comments.txt +++ b/hblang/tests/son_tests_comments.txt @@ -1,18 +1,17 @@ -main: - ADDI64 r254, r254, -16d - ST r31, r254, 0a, 16h - JAL r31, r0, :foo - CP r32, r1 - LI64 r1, 0d - LD r31, r254, 0a, 16h - ADDI64 r254, r254, 16d - JALA r0, r31, 0a foo: ADDI64 r254, r254, -8d ST r31, r254, 0a, 8h LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 146 +main: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + JAL r31, r0, :foo + LI64 r1, 0d + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a +code size: 143 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 22839221..0e1c8f1c 100644 --- a/hblang/tests/son_tests_exhaustive_loop_testing.txt +++ b/hblang/tests/son_tests_exhaustive_loop_testing.txt @@ -1,103 +1,106 @@ +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 + CP r1, r2 + JMP :1 + 0: JNE r2, r34, :2 + CP r2, r33 + JMP :3 + 2: JNE r2, r32, :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 main: - ADDI64 r254, r254, -88d - ST r31, r254, 0a, 88h - LI64 r32, 0d - CP r2, r32 + ADDI64 r254, r254, -72d + ST r31, r254, 0a, 72h + LI64 r12, 0d + CP r2, r12 JAL r31, r0, :multiple_breaks + CP r32, r12 CP r33, r1 - LI64 r34, 3d - JEQ r33, r34, :0 + LI64 r1, 3d + JEQ r33, r1, :0 LI64 r1, 1d JMP :1 - 0: LI64 r35, 4d + 0: CP r34, r1 + LI64 r35, 4d CP r2, r35 JAL r31, r0, :multiple_breaks - CP r36, r1 + CP r36, r35 LI64 r37, 10d - JEQ r36, r37, :2 + JEQ r1, r37, :2 LI64 r1, 2d JMP :1 2: CP r2, r32 JAL r31, r0, :state_change_in_break CP r38, r1 - JEQ r38, r32, :3 + CP r1, r32 + JEQ r38, r1, :3 CP r1, r34 JMP :1 - 3: CP r2, r35 + 3: CP r32, r1 + CP r2, r36 JAL r31, r0, :state_change_in_break - CP r39, r1 - JEQ r39, r37, :4 - CP r1, r35 + JEQ r1, r37, :4 + CP r1, r36 JMP :1 4: CP r2, r37 JAL r31, r0, :continue_and_state_change - CP r40, r1 - JEQ r40, r37, :5 + JEQ r1, r37, :5 LI64 r1, 5d JMP :1 5: CP r2, r34 JAL r31, r0, :continue_and_state_change - CP r41, r1 - JEQ r41, r32, :6 + CP r39, r1 + CP r1, r32 + JEQ r39, r1, :6 LI64 r1, 6d JMP :1 6: CP r1, r32 - 1: LD r31, r254, 0a, 88h - ADDI64 r254, r254, 88d - JALA r0, r31, 0a -continue_and_state_change: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h - CP r1, r2 - LI64 r32, 3d - LI64 r33, 4d - LI64 r34, 2d - LI64 r35, 10d - 6: JLTU r1, r35, :0 - JMP :1 - 0: JNE r1, r34, :2 - CP r1, r33 - JMP :3 - 2: JNE r1, r32, :4 - LI64 r1, 0d - 1: JMP :5 - 4: ADDI64 r33, r1, 1d - CP r1, r33 - 3: JMP :6 - 5: LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d - JALA r0, r31, 0a -state_change_in_break: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h - CP r1, r2 - LI64 r32, 3d - LI64 r33, 10d - 4: JLTU r1, r33, :0 - JMP :1 - 0: JNE r1, r32, :2 - LI64 r1, 0d - 1: JMP :3 - 2: ADDI64 r1, r1, 1d - JMP :4 - 3: LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d + 1: LD r31, r254, 0a, 72h + ADDI64 r254, r254, 72d JALA r0, r31, 0a multiple_breaks: ADDI64 r254, r254, -24d ST r31, r254, 0a, 24h - CP r1, r2 LI64 r32, 3d LI64 r33, 10d - 4: JLTU r1, r33, :0 + 4: JLTU r2, r33, :0 + CP r1, r2 JMP :1 - 0: ADDI64 r1, r1, 1d + 0: ADDI64 r1, r2, 1d JNE r1, r32, :2 1: JMP :3 - 2: JMP :4 + 2: CP r2, r1 + JMP :4 3: LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 704 +state_change_in_break: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + LI64 r32, 3d + LI64 r33, 10d + 4: JLTU r2, r33, :0 + CP r1, r2 + JMP :1 + 0: JNE r2, r32, :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 ret: 0 status: Ok(()) diff --git a/hblang/tests/son_tests_fb_driver.txt b/hblang/tests/son_tests_fb_driver.txt index ee77c521..1fbe6358 100644 --- a/hblang/tests/son_tests_fb_driver.txt +++ b/hblang/tests/son_tests_fb_driver.txt @@ -1,37 +1,45 @@ +check_platform: + ADDI64 r254, r254, -8d + ST r31, r254, 0a, 8h + JAL r31, r0, :x86_fb_ptr + LD r31, r254, 0a, 8h + ADDI64 r254, r254, 8d + JALA r0, r31, 0a main: - ADDI64 r254, r254, -88d - ST r31, r254, 0a, 88h + ADDI64 r254, r254, -64d + ST r31, r254, 0a, 64h JAL r31, r0, :check_platform - CP r32, r1 - LI64 r33, 0d - LI64 r34, 30d - LI64 r35, 100d - CP r36, r33 - CP r37, r33 - CP r38, r33 - 5: JLTU r37, r34, :0 + LI64 r32, 0d + LI64 r33, 30d + LI64 r34, 100d + CP r35, r32 + CP r36, r32 + CP r37, r32 + 5: JLTU r35, r33, :0 ADDI64 r36, r36, 1d - CP r2, r33 + CP r2, r32 CP r3, r36 - CP r4, r34 + CP r4, r33 JAL r31, r0, :set_pixel - CP r39, r1 - JEQ r39, r38, :1 - CP r1, r33 + JEQ r1, r37, :1 + CP r1, r32 JMP :2 - 1: JNE r36, r35, :3 - CP r1, r38 + 1: CP r38, r32 + JNE r36, r34, :3 + CP r1, r37 JMP :2 - 3: CP r37, r33 - CP r40, r38 + 3: CP r1, r37 + CP r35, r38 JMP :4 - 0: ADDI64 r40, r38, 1d - ADDI64 r41, r37, 1d - CP r37, r41 - 4: CP r38, r40 + 0: CP r1, r37 + CP r38, r32 + ADDI64 r1, r1, 1d + ADDI64 r35, r35, 1d + 4: CP r32, r38 + CP r37, r1 JMP :5 - 2: LD r31, r254, 0a, 88h - ADDI64 r254, r254, 88d + 2: LD r31, r254, 0a, 64h + ADDI64 r254, r254, 64d JALA r0, r31, 0a set_pixel: ADDI64 r254, r254, -16d @@ -41,13 +49,6 @@ set_pixel: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -check_platform: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h - JAL r31, r0, :x86_fb_ptr - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d - JALA r0, r31, 0a x86_fb_ptr: ADDI64 r254, r254, -8d ST r31, r254, 0a, 8h @@ -55,6 +56,6 @@ x86_fb_ptr: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -code size: 423 +code size: 426 ret: 3000 status: Ok(()) diff --git a/hblang/tests/son_tests_functions.txt b/hblang/tests/son_tests_functions.txt index 36fd6489..e89665a0 100644 --- a/hblang/tests/son_tests_functions.txt +++ b/hblang/tests/son_tests_functions.txt @@ -1,15 +1,9 @@ -main: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h - LI64 r2, 10d - JAL r31, r0, :add_one - CP r32, r1 - LI64 r2, 20d - JAL r31, r0, :add_two - CP r33, r1 - ADD64 r1, r33, r32 - LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d +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 @@ -18,13 +12,18 @@ add_two: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -add_one: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h - ADDI64 r1, r2, 1d - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d +main: + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LI64 r2, 10d + JAL r31, r0, :add_one + CP r32, r1 + LI64 r2, 20d + JAL r31, r0, :add_two + ADD64 r1, r1, r32 + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 251 +code size: 248 ret: 33 status: Ok(()) diff --git a/hblang/tests/son_tests_if_statements.txt b/hblang/tests/son_tests_if_statements.txt index ee986ff7..0664f986 100644 --- a/hblang/tests/son_tests_if_statements.txt +++ b/hblang/tests/son_tests_if_statements.txt @@ -1,3 +1,22 @@ +fib: + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h + LI64 r1, 1d + LI64 r32, 2d + JGTU r2, r32, :0 + JMP :1 + 0: CP r33, r2 + ADDI64 r2, r33, -1d + CP r34, r33 + JAL r31, r0, :fib + CP r2, r34 + CP r35, r1 + ADDI64 r2, r2, -2d + JAL r31, r0, :fib + ADD64 r1, r1, r35 + 1: LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d + JALA r0, r31, 0a main: ADDI64 r254, r254, -8d ST r31, r254, 0a, 8h @@ -6,24 +25,6 @@ main: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -fib: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h - CP r32, r2 - LI64 r1, 1d - LI64 r33, 2d - JGTU r32, r33, :0 - JMP :1 - 0: SUB64 r2, r32, r1 - JAL r31, r0, :fib - CP r34, r1 - SUB64 r2, r32, r33 - JAL r31, r0, :fib - CP r35, r1 - ADD64 r1, r35, r34 - 1: LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d - JALA r0, r31, 0a -code size: 208 +code size: 225 ret: 55 status: Ok(()) diff --git a/hblang/tests/son_tests_loops.txt b/hblang/tests/son_tests_loops.txt index 4a88f24d..95c64c76 100644 --- a/hblang/tests/son_tests_loops.txt +++ b/hblang/tests/son_tests_loops.txt @@ -1,3 +1,19 @@ +fib: + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h + LI64 r32, 1d + LI64 r33, 0d + CP r1, r33 + 2: JNE r2, r33, :0 + JMP :1 + 0: ADD64 r34, r32, r1 + ADDI64 r2, r2, -1d + CP r1, r32 + CP r32, r34 + JMP :2 + 1: LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a main: ADDI64 r254, r254, -8d ST r31, r254, 0a, 8h @@ -6,23 +22,6 @@ main: LD r31, r254, 0a, 8h ADDI64 r254, r254, 8d JALA r0, r31, 0a -fib: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h - LI64 r32, 1d - LI64 r33, 0d - CP r1, r33 - CP r34, r32 - 2: JNE r2, r33, :0 - JMP :1 - 0: ADD64 r35, r34, r1 - SUB64 r2, r2, r32 - CP r1, r34 - CP r34, r35 - JMP :2 - 1: LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d - JALA r0, r31, 0a -code size: 198 +code size: 202 ret: 55 status: Ok(())