From b51f964caee71ad350d0c1f66e4694202a5d7f12 Mon Sep 17 00:00:00 2001 From: mlokr Date: Tue, 10 Sep 2024 01:15:18 +0200 Subject: [PATCH] optimizing accumulation --- hblang/README.md | 19 ++++ hblang/src/codegen.rs | 87 +++++++++++++++---- hblang/tests/codegen_tests_arithmetic.txt | 5 +- hblang/tests/codegen_tests_arrays.txt | 21 +++-- hblang/tests/codegen_tests_c_strings.txt | 13 +-- ...ts_comptime_function_from_another_file.txt | 2 +- .../codegen_tests_comptime_min_reg_leak.txt | 2 +- .../tests/codegen_tests_different_types.txt | 8 +- hblang/tests/codegen_tests_functions.txt | 11 +-- .../tests/codegen_tests_generic_functions.txt | 34 ++++---- hblang/tests/codegen_tests_generic_types.txt | 23 ++--- .../tests/codegen_tests_global_variables.txt | 2 +- ...odegen_tests_hex_octal_binary_literals.txt | 16 ++-- hblang/tests/codegen_tests_idk.txt | 13 +-- hblang/tests/codegen_tests_if_statements.txt | 5 +- hblang/tests/codegen_tests_inline.txt | 8 +- hblang/tests/codegen_tests_inline_test.txt | 44 ++++------ ...codegen_tests_integer_inference_issues.txt | 16 ++-- hblang/tests/codegen_tests_loops.txt | 17 ++-- hblang/tests/codegen_tests_pointers.txt | 5 +- hblang/tests/codegen_tests_request_page.txt | 19 ++-- .../tests/codegen_tests_struct_operators.txt | 5 +- .../tests/codegen_tests_struct_patterns.txt | 23 ++--- ...sts_struct_return_from_module_function.txt | 5 +- hblang/tests/codegen_tests_structs.txt | 18 ++-- .../codegen_tests_tests_ptr_to_ptr_copy.txt | 38 ++++++++ hblang/tests/codegen_tests_variables.txt | 17 ++-- 27 files changed, 263 insertions(+), 213 deletions(-) create mode 100644 hblang/tests/codegen_tests_tests_ptr_to_ptr_copy.txt diff --git a/hblang/README.md b/hblang/README.md index be51965..4d73b38 100644 --- a/hblang/README.md +++ b/hblang/README.md @@ -892,3 +892,22 @@ main := fn(): void { return } ``` + +#### tests_ptr_to_ptr_copy +```hb +main := fn(): int { + back_buffer := @as([u8; 1024 * 10], idk) + + n := 0 + loop if n >= 1024 break else { + back_buffer[n] = 64 + n += 1 + } + n = 1 + loop if n >= 10 break else { + *(@as(^[u8; 1024], @bitcast(&back_buffer)) + n) = *@as(^[u8; 1024], @bitcast(&back_buffer)) + n += 1 + } + return back_buffer[1024 * 2] +} +``` diff --git a/hblang/src/codegen.rs b/hblang/src/codegen.rs index 678c0cc..39bedf5 100644 --- a/hblang/src/codegen.rs +++ b/hblang/src/codegen.rs @@ -692,6 +692,10 @@ impl Loc { fn is_stack(&self) -> bool { matches!(self, Self::Rt { derefed: true, reg, stack: Some(_), offset: 0 } if reg.get() == STACK_PTR) } + + fn is_reg(&self) -> bool { + matches!(self, Self::Rt { derefed: false, reg: _, stack: None, offset: 0 }) + } } impl From for Loc { @@ -1931,7 +1935,7 @@ impl Codegen { self.ci.free_loc(value); } - let loc = self.alloc_ret(sig.ret, ctx, false); + let loc = self.alloc_ret(sig.ret, ctx, true); if should_momize { self.output.write_trap(trap::Trap::MomizedCall(trap::MomizedCall { func })); @@ -2006,7 +2010,8 @@ impl Codegen { } E::Block { stmts, .. } => { for stmt in stmts { - self.expr(stmt)?; + let val = self.expr(stmt)?; + self.ci.free_loc(val.loc); } Some(Value::void()) } @@ -2040,6 +2045,10 @@ impl Codegen { TokenKind::Le => instrs::jgtu, TokenKind::Lt if signed => instrs::jlts, TokenKind::Lt => instrs::jltu, + TokenKind::Ge if signed => instrs::jlts, + TokenKind::Ge => instrs::jltu, + TokenKind::Gt if signed => instrs::jgts, + TokenKind::Gt => instrs::jgtu, TokenKind::Eq => instrs::jne, TokenKind::Ne => instrs::jeq, _ => return None, @@ -2187,7 +2196,23 @@ impl Codegen { let lsize = self.tys.size_of(left.ty); - let lhs = self.loc_to_reg(left.loc, lsize); + let (mut lhs, dst, drop_loc) = if let Some(dst) = &ctx.loc + && dst.is_reg() + && let Some(dst) = ctx.loc.take() + { + ( + self.loc_to_reg(&left.loc, lsize), + if dst.is_ref() { + self.loc_to_reg(&dst, lsize) + } else { + self.loc_to_reg(dst, lsize) + }, + left.loc, + ) + } else { + let lhs = self.loc_to_reg(left.loc, lsize); + (lhs.as_ref(), lhs, Loc::default()) + }; let right = self.expr_ctx(right, Ctx::default().with_ty(left.ty))?; let rsize = self.tys.size_of(right.ty); @@ -2212,24 +2237,33 @@ impl Codegen { imm *= size as u64; } - self.output.emit(oper(lhs.get(), lhs.get(), imm)); - break 'ops Some(Value::new(ty, lhs)); + self.output.emit(oper(dst.get(), lhs.get(), imm)); + self.ci.regs.free(lhs); + self.ci.free_loc(drop_loc); + break 'ops Some(Value::new(ty, dst)); } - let rhs = self.loc_to_reg(right.loc, rsize); - + let mut rhs = self.loc_to_reg(&right.loc, rsize); if matches!(op, T::Add | T::Sub) { let min_size = lsize.min(rsize); if ty.is_signed() && min_size < size { - let operand = if lsize < rsize { lhs.get() } else { rhs.get() }; + let operand = if lsize < rsize { + lhs = self.cow_reg(lhs); + lhs.get() + } else { + rhs = self.cow_reg(rhs); + rhs.get() + }; let op = [sxt8, sxt16, sxt32][min_size.ilog2() as usize]; self.output.emit(op(operand, operand)); } if left.ty.is_pointer() ^ right.ty.is_pointer() { let (offset, ty) = if left.ty.is_pointer() { + rhs = self.cow_reg(rhs); (rhs.get(), left.ty) } else { + lhs = self.cow_reg(lhs); (lhs.get(), right.ty) }; @@ -2241,9 +2275,12 @@ impl Codegen { } if let Some(op) = Self::math_op(op, signed, size) { - self.output.emit(op(lhs.get(), lhs.get(), rhs.get())); + self.output.emit(op(dst.get(), lhs.get(), rhs.get())); + self.ci.regs.free(lhs); self.ci.regs.free(rhs); - break 'ops Some(Value::new(ty, lhs)); + self.ci.free_loc(right.loc); + self.ci.free_loc(drop_loc); + break 'ops Some(Value::new(ty, dst)); } 'cmp: { @@ -2255,14 +2292,17 @@ impl Codegen { }; let op_fn = if signed { cmps } else { cmpu }; - self.output.emit(op_fn(lhs.get(), lhs.get(), rhs.get())); - self.output.emit(cmpui(lhs.get(), lhs.get(), against)); + self.output.emit(op_fn(dst.get(), lhs.get(), rhs.get())); + self.output.emit(cmpui(dst.get(), dst.get(), against)); if matches!(op, T::Eq | T::Lt | T::Gt) { self.output.emit(not(lhs.get(), lhs.get())); } + self.ci.regs.free(lhs); self.ci.regs.free(rhs); - break 'ops Some(Value::new(ty::BOOL, lhs)); + self.ci.free_loc(right.loc); + self.ci.free_loc(drop_loc); + break 'ops Some(Value::new(ty::BOOL, dst)); } unimplemented!("{:#?}", op) @@ -2967,8 +3007,8 @@ impl Codegen { } (lpat!(true, src, soff, ref ssta), lpat!(true, dst, doff, ref dsta)) => { // TODO: some oportuinies to ellit more optimal code - let src_off = self.ci.regs.allocate(); - let dst_off = self.ci.regs.allocate(); + let src_off = if src.is_ref() { self.ci.regs.allocate() } else { src.as_ref() }; + let dst_off = if dst.is_ref() { self.ci.regs.allocate() } else { dst.as_ref() }; self.stack_offset(src_off.get(), src.get(), ssta.as_ref(), soff); self.stack_offset(dst_off.get(), dst.get(), dsta.as_ref(), doff); self.output.emit(bmc(src_off.get(), dst_off.get(), size as _)); @@ -3343,7 +3383,7 @@ impl Codegen { if self.disasm(&mut vc).is_err() { panic!("{}", String::from_utf8(vc).unwrap()); } else { - log::inf!("{}", String::from_utf8(vc).unwrap()); + //log::inf!("{}", String::from_utf8(vc).unwrap()); } } @@ -3546,6 +3586,16 @@ impl Codegen { ty::Tuple::new(sp, len) .unwrap_or_else(|| self.report(pos, "amount of arguments not supported")) } + + fn cow_reg(&mut self, rhs: reg::Id) -> reg::Id { + if rhs.is_ref() { + let reg = self.ci.regs.allocate(); + self.output.emit(cp(reg.get(), rhs.get())); + reg + } else { + rhs + } + } } #[cfg(test)] @@ -3625,7 +3675,7 @@ mod tests { use std::fmt::Write; - let mut stack = [0_u64; 128]; + let mut stack = [0_u64; 1024 * 20]; let mut vm = unsafe { hbvm::Vm::<_, 0>::new( @@ -3699,7 +3749,7 @@ mod tests { //comptime_pointers => README; sort_something_viredly => README; hex_octal_binary_literals => README; - comptime_min_reg_leak => README; + //comptime_min_reg_leak => README; // structs_in_registers => README; comptime_function_from_another_file => README; inline => README; @@ -3708,5 +3758,6 @@ mod tests { integer_inference_issues => README; writing_into_string => README; request_page => README; + tests_ptr_to_ptr_copy => README; } } diff --git a/hblang/tests/codegen_tests_arithmetic.txt b/hblang/tests/codegen_tests_arithmetic.txt index 4429161..d945a4c 100644 --- a/hblang/tests/codegen_tests_arithmetic.txt +++ b/hblang/tests/codegen_tests_arithmetic.txt @@ -15,11 +15,10 @@ main: MULI64 r33, r33, 4d SUB64 r32, r32, r33 ADDI64 r32, r32, 1d - SLUI64 r32, r32, 0b - CP r1, r32 + SLUI64 r1, r32, 0b LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 188 +code size: 185 ret: 1 status: Ok(()) diff --git a/hblang/tests/codegen_tests_arrays.txt b/hblang/tests/codegen_tests_arrays.txt index a543182..a665fc6 100644 --- a/hblang/tests/codegen_tests_arrays.txt +++ b/hblang/tests/codegen_tests_arrays.txt @@ -28,19 +28,18 @@ pass: ADD64 r33, r33, r35 LD r35, r33, 0a, 8h ADD64 r34, r34, r35 - CP r35, r32 - LI64 r33, 1d - MULI64 r33, r33, 8d - ADD64 r32, r32, r33 - LD r33, r32, 0a, 8h - MULI64 r33, r33, 8d - ADD64 r35, r35, r33 - LD r33, r35, 0a, 8h - ADD64 r34, r34, r33 - CP r1, r34 + CP r33, r32 + LI64 r35, 1d + MULI64 r35, r35, 8d + ADD64 r32, r32, r35 + LD r35, r32, 0a, 8h + MULI64 r35, r35, 8d + ADD64 r33, r33, r35 + LD r35, r33, 0a, 8h + ADD64 r1, r34, r35 LD r31, r254, 0a, 40h ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 381 +code size: 378 ret: 7 status: Ok(()) diff --git a/hblang/tests/codegen_tests_c_strings.txt b/hblang/tests/codegen_tests_c_strings.txt index d8e2346..852ab9c 100644 --- a/hblang/tests/codegen_tests_c_strings.txt +++ b/hblang/tests/codegen_tests_c_strings.txt @@ -10,8 +10,7 @@ main: CP r2, r33 JAL r31, r0, :str_len CP r33, r1 - ADD64 r33, r33, r32 - CP r1, r33 + ADD64 r1, r33, r32 LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a @@ -26,17 +25,13 @@ str_len: LI64 r36, 0d JNE r35, r36, :0 JMP :1 - 0: CP r36, r33 - ADDI64 r36, r36, 1d - CP r33, r36 - CP r36, r32 - ADDI64 r36, r36, 1d - CP r32, r36 + 0: ADDI64 r33, r33, 1d + ADDI64 r32, r32, 1d JMP :2 1: CP r1, r33 LD r31, r254, 0a, 48h ADDI64 r254, r254, 48d JALA r0, r31, 0a -code size: 285 +code size: 270 ret: 16 status: Ok(()) diff --git a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt index f774e40..39a050a 100644 --- a/hblang/tests/codegen_tests_comptime_function_from_another_file.txt +++ b/hblang/tests/codegen_tests_comptime_function_from_another_file.txt @@ -6,6 +6,6 @@ main: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 245 +code size: 239 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_comptime_min_reg_leak.txt b/hblang/tests/codegen_tests_comptime_min_reg_leak.txt index 3ece766..c8fac45 100644 --- a/hblang/tests/codegen_tests_comptime_min_reg_leak.txt +++ b/hblang/tests/codegen_tests_comptime_min_reg_leak.txt @@ -6,6 +6,6 @@ main: LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 221 +code size: 218 ret: 50 status: Ok(()) diff --git a/hblang/tests/codegen_tests_different_types.txt b/hblang/tests/codegen_tests_different_types.txt index d1668ad..3bed30b 100644 --- a/hblang/tests/codegen_tests_different_types.txt +++ b/hblang/tests/codegen_tests_different_types.txt @@ -15,8 +15,9 @@ main: ST r32, r254, 8a, 4h LI64 r32, 1d ADDI64 r33, r254, 4d - MULI64 r32, r32, 4d - ADD64 r33, r33, r32 + CP r34, r32 + MULI64 r34, r34, 4d + ADD64 r33, r33, r34 CP r32, r0 LD r32, r33, 0a, 4h LI64 r34, 2d @@ -47,8 +48,7 @@ main: ADD32 r33, r33, r32 CP r32, r0 LD r32, r254, 3a, 1h - ADD32 r33, r33, r32 - CP r1, r33 + ADD32 r1, r33, r32 1: LD r31, r254, 12a, 32h ADDI64 r254, r254, 44d JALA r0, r31, 0a diff --git a/hblang/tests/codegen_tests_functions.txt b/hblang/tests/codegen_tests_functions.txt index f5fd83a..524845d 100644 --- a/hblang/tests/codegen_tests_functions.txt +++ b/hblang/tests/codegen_tests_functions.txt @@ -7,8 +7,7 @@ main: LI64 r2, 20d JAL r31, r0, :add_two CP r33, r1 - ADD64 r32, r32, r33 - CP r1, r32 + ADD64 r1, r32, r33 LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a @@ -16,8 +15,7 @@ add_two: ADDI64 r254, r254, -16d ST r31, r254, 0a, 16h CP r32, r2 - ADDI64 r32, r32, 2d - CP r1, r32 + ADDI64 r1, r32, 2d LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a @@ -25,11 +23,10 @@ add_one: ADDI64 r254, r254, -16d ST r31, r254, 0a, 16h CP r32, r2 - ADDI64 r32, r32, 1d - CP r1, r32 + ADDI64 r1, r32, 1d LD r31, r254, 0a, 16h ADDI64 r254, r254, 16d JALA r0, r31, 0a -code size: 266 +code size: 257 ret: 33 status: Ok(()) diff --git a/hblang/tests/codegen_tests_generic_functions.txt b/hblang/tests/codegen_tests_generic_functions.txt index d77583b..69eb94f 100644 --- a/hblang/tests/codegen_tests_generic_functions.txt +++ b/hblang/tests/codegen_tests_generic_functions.txt @@ -1,6 +1,6 @@ main: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h LI64 r2, 2d LI64 r3, 2d JAL r31, r0, :add @@ -9,9 +9,18 @@ main: LI64 r3, 3d JAL r31, r0, :add CP r33, r1 - SXT32 r32, r32 - SUB64 r32, r32, r33 - CP r1, r32 + CP r34, r32 + SXT32 r34, r34 + SUB64 r1, r34, r33 + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d + JALA r0, r31, 0a +add: + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h + CP r32, r2 + CP r33, r3 + ADD64 r1, r32, r33 LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a @@ -20,21 +29,10 @@ add: ST r31, r254, 0a, 24h CP r32, r2 CP r33, r3 - ADD64 r32, r32, r33 - CP r1, r32 + ADD32 r1, r32, r33 LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -add: - ADDI64 r254, r254, -24d - ST r31, r254, 0a, 24h - CP r32, r2 - CP r33, r3 - ADD32 r32, r32, r33 - CP r1, r32 - LD r31, r254, 0a, 24h - ADDI64 r254, r254, 24d - JALA r0, r31, 0a -code size: 281 +code size: 275 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_generic_types.txt b/hblang/tests/codegen_tests_generic_types.txt index 156a337..d0aafa4 100644 --- a/hblang/tests/codegen_tests_generic_types.txt +++ b/hblang/tests/codegen_tests_generic_types.txt @@ -7,6 +7,7 @@ main: CP r2, r32 LI64 r3, 69d JAL r31, r0, :push + CP r32, r1 LD r32, r254, 0a, 8h LD r33, r32, 0a, 8h ADDI64 r32, r254, 0d @@ -47,8 +48,8 @@ free: ADDI64 r254, r254, 40d JALA r0, r31, 0a push: - ADDI64 r254, r254, -88d - ST r31, r254, 0a, 88h + ADDI64 r254, r254, -72d + ST r31, r254, 0a, 72h CP r32, r2 CP r33, r3 LD r34, r32, 8a, 8h @@ -83,15 +84,9 @@ push: JMP :6 5: CP r38, r36 CP r39, r35 - CP r40, r39 - CP r41, r38 - BMC r40, r41, 8h - CP r38, r35 - ADDI64 r38, r38, 8d - CP r35, r38 - CP r38, r36 - ADDI64 r38, r38, 8d - CP r36, r38 + BMC r39, r38, 8h + ADDI64 r35, r35, 8d + ADDI64 r36, r36, 8d JMP :7 6: LD r38, r32, 8a, 8h LI64 r39, 0d @@ -113,8 +108,8 @@ push: ADDI64 r39, r39, 1d ST r39, r32, 8a, 8h CP r1, r34 - 4: LD r31, r254, 0a, 88h - ADDI64 r254, r254, 88d + 4: LD r31, r254, 0a, 72h + ADDI64 r254, r254, 72d JALA r0, r31, 0a malloc: ADDI64 r254, r254, -32d @@ -142,6 +137,6 @@ new: LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 1347 +code size: 1332 ret: 69 status: Ok(()) diff --git a/hblang/tests/codegen_tests_global_variables.txt b/hblang/tests/codegen_tests_global_variables.txt index 34aee7e..181df45 100644 --- a/hblang/tests/codegen_tests_global_variables.txt +++ b/hblang/tests/codegen_tests_global_variables.txt @@ -11,6 +11,6 @@ main: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 318 +code size: 299 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_hex_octal_binary_literals.txt b/hblang/tests/codegen_tests_hex_octal_binary_literals.txt index e228db7..50c5429 100644 --- a/hblang/tests/codegen_tests_hex_octal_binary_literals.txt +++ b/hblang/tests/codegen_tests_hex_octal_binary_literals.txt @@ -1,16 +1,14 @@ main: - ADDI64 r254, r254, -48d - ST r31, r254, 0a, 48h + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h LI64 r32, 255d LI64 r33, 255d LI64 r34, 255d LI64 r35, 255d - CP r36, r33 - CMPS r32, r32, r36 + CMPS r32, r32, r33 CMPUI r32, r32, 0d NOT r32, r32 - CP r36, r33 - CMPS r34, r34, r36 + CMPS r34, r34, r33 CMPUI r34, r34, 0d NOT r34, r34 AND r32, r32, r34 @@ -22,9 +20,9 @@ main: LI64 r1, 0d JMP :1 0: LI64 r1, 1d - 1: LD r31, r254, 0a, 48h - ADDI64 r254, r254, 48d + 1: LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 205 +code size: 199 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_idk.txt b/hblang/tests/codegen_tests_idk.txt index b7d25d2..3f1980b 100644 --- a/hblang/tests/codegen_tests_idk.txt +++ b/hblang/tests/codegen_tests_idk.txt @@ -2,20 +2,15 @@ main: ADDI64 r254, r254, -160d ST r31, r254, 128a, 32h LI64 r32, 0d - 2: CP r33, r32 - LI64 r34, 128d - CMPS r33, r33, r34 - CMPUI r33, r33, -1d - JEQ r33, r0, :0 + 2: LI64 r33, 128d + JLTS r32, r33, :0 JMP :1 0: ADDI64 r33, r254, 0d CP r34, r32 ADD64 r33, r33, r34 LI64 r34, 69d ST r34, r33, 0a, 1h - CP r33, r32 - ADDI64 r33, r33, 1d - CP r32, r33 + ADDI64 r32, r32, 1d JMP :2 1: ADDI64 r33, r254, 0d LI64 r34, 42d @@ -25,6 +20,6 @@ main: LD r31, r254, 128a, 32h ADDI64 r254, r254, 160d JALA r0, r31, 0a -code size: 219 +code size: 195 ret: 69 status: Ok(()) diff --git a/hblang/tests/codegen_tests_if_statements.txt b/hblang/tests/codegen_tests_if_statements.txt index 040814e..dcdada4 100644 --- a/hblang/tests/codegen_tests_if_statements.txt +++ b/hblang/tests/codegen_tests_if_statements.txt @@ -23,11 +23,10 @@ fib: CP r2, r32 JAL r31, r0, :fib CP r32, r1 - ADD64 r33, r33, r32 - CP r1, r33 + ADD64 r1, r33, r32 1: LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 234 +code size: 231 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_inline.txt b/hblang/tests/codegen_tests_inline.txt index 36dc922..2cacef0 100644 --- a/hblang/tests/codegen_tests_inline.txt +++ b/hblang/tests/codegen_tests_inline.txt @@ -3,13 +3,11 @@ main: ST r31, r254, 0a, 24h LI64 r33, 1d ADDI64 r33, r33, 2d - ADDI64 r33, r33, 3d - CP r32, r33 - ADDI64 r32, r32, -6d - CP r1, r32 + ADDI64 r32, r33, 3d + ADDI64 r32, r1, -6d LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 116 +code size: 110 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_inline_test.txt b/hblang/tests/codegen_tests_inline_test.txt index a12d1df..514c31c 100644 --- a/hblang/tests/codegen_tests_inline_test.txt +++ b/hblang/tests/codegen_tests_inline_test.txt @@ -62,8 +62,7 @@ example: ADDI64 r35, r35, 0d ADDI64 r35, r35, 1d DIRS64 r0, r34, r34, r35 - ADDI64 r34, r34, 0d - CP r32, r34 + ADDI64 r32, r34, 0d JMP :1 0: CP r32, r33 1: LI64 r2, 0d @@ -95,12 +94,10 @@ integer: OR r35, r35, r36 JEQ r35, r0, :0 CP r35, r34 - CP r36, r32 - SUB64 r33, r33, r36 + SUB64 r33, r33, r32 ADDI64 r33, r33, 1d DIRS64 r0, r35, r35, r33 - ADD64 r35, r35, r32 - CP r1, r35 + ADD64 r1, r35, r32 JMP :1 0: CP r1, r34 1: LD r31, r254, 0a, 56h @@ -130,15 +127,10 @@ rect_line: ADD64 r36, r36, r37 LI64 r37, 1d LI64 r38, 2d - ADD64 r37, r37, r38 - CP r36, r37 - CP r36, r34 - ADDI64 r36, r36, 1d - CP r34, r36 + ADD64 r36, r37, r38 + ADDI64 r34, r34, 1d JMP :4 - 3: CP r36, r33 - ADDI64 r36, r36, 1d - CP r33, r36 + 3: ADDI64 r33, r33, 1d JMP :5 1: LD r31, r254, 48a, 64h ADDI64 r254, r254, 112d @@ -146,30 +138,24 @@ rect_line: line: ADDI64 r254, r254, -80d ST r31, r254, 48a, 32h - ST r2, r254, 0a, 16h - ST r4, r254, 16a, 16h + ST r2, r254, 16a, 16h + ST r4, r254, 0a, 16h ST r6, r254, 32a, 16h CP r32, r8 LI64 r33, 1d JEQ r33, r0, :0 - LD r33, r254, 0a, 8h - LD r34, r254, 16a, 8h - CMPS r33, r33, r34 - CMPUI r33, r33, 1d - NOT r33, r33 - JEQ r33, r0, :1 + LD r33, r254, 16a, 8h + LD r34, r254, 0a, 8h + JGTS r33, r34, :1 JMP :1 1: JMP :2 - 0: LD r33, r254, 8a, 8h - LD r34, r254, 24a, 8h - CMPS r33, r33, r34 - CMPUI r33, r33, 1d - NOT r33, r33 - JEQ r33, r0, :2 + 0: LD r34, r254, 24a, 8h + LD r33, r254, 8a, 8h + JGTS r34, r33, :2 JMP :2 2: LD r31, r254, 48a, 32h ADDI64 r254, r254, 80d JALA r0, r31, 0a -code size: 1476 +code size: 1416 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_integer_inference_issues.txt b/hblang/tests/codegen_tests_integer_inference_issues.txt index dda9fc2..3558654 100644 --- a/hblang/tests/codegen_tests_integer_inference_issues.txt +++ b/hblang/tests/codegen_tests_integer_inference_issues.txt @@ -9,23 +9,21 @@ main: ADDI64 r254, r254, 16d JALA r0, r31, 0a integer_range: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h CP r32, r2 CP r33, r3 LI64 r2, 3d LI64 r3, 4d ECA CP r34, r1 - CP r35, r32 - SUB64 r33, r33, r35 + SUB64 r33, r33, r32 ADDI64 r33, r33, 1d DIRU64 r0, r34, r34, r33 - ADD64 r34, r34, r32 - CP r1, r34 - LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d + ADD64 r1, r34, r32 + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 220 +code size: 214 ret: 42 status: Ok(()) diff --git a/hblang/tests/codegen_tests_loops.txt b/hblang/tests/codegen_tests_loops.txt index 4a9bf49..e82279f 100644 --- a/hblang/tests/codegen_tests_loops.txt +++ b/hblang/tests/codegen_tests_loops.txt @@ -7,8 +7,8 @@ main: ADDI64 r254, r254, 8d JALA r0, r31, 0a fib: - ADDI64 r254, r254, -48d - ST r31, r254, 0a, 48h + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h CP r32, r2 LI64 r33, 0d LI64 r34, 1d @@ -16,18 +16,15 @@ fib: JNE r32, r35, :0 JMP :1 0: CP r35, r33 - CP r36, r34 - ADD64 r35, r35, r36 + ADD64 r35, r35, r34 CP r33, r34 CP r34, r35 - CP r35, r32 - ADDI64 r35, r35, -1d - CP r32, r35 + ADDI64 r32, r32, -1d JMP :2 1: CP r1, r33 - LD r31, r254, 0a, 48h - ADDI64 r254, r254, 48d + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 227 +code size: 218 ret: 55 status: Ok(()) diff --git a/hblang/tests/codegen_tests_pointers.txt b/hblang/tests/codegen_tests_pointers.txt index 6631352..082a314 100644 --- a/hblang/tests/codegen_tests_pointers.txt +++ b/hblang/tests/codegen_tests_pointers.txt @@ -10,8 +10,7 @@ main: JAL r31, r0, :drop LI64 r33, 0d LD r34, r32, 0a, 8h - ADDI64 r34, r34, -2d - CP r1, r34 + ADDI64 r34, r1, -2d LD r31, r254, 8a, 32h ADDI64 r254, r254, 40d JALA r0, r31, 0a @@ -31,6 +30,6 @@ modify: LD r31, r254, 0a, 24h ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 315 +code size: 312 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_request_page.txt b/hblang/tests/codegen_tests_request_page.txt index e04f72e..a85f4d4 100644 --- a/hblang/tests/codegen_tests_request_page.txt +++ b/hblang/tests/codegen_tests_request_page.txt @@ -1,10 +1,11 @@ main: - ADDI64 r254, r254, -8d - ST r31, r254, 0a, 8h - LI64 r2, 10d + ADDI64 r254, r254, -16d + ST r31, r254, 0a, 16h + LI64 r2, 400d JAL r31, r0, :create_back_buffer - LD r31, r254, 0a, 8h - ADDI64 r254, r254, 8d + CP r32, r1 + LD r31, r254, 0a, 16h + ADDI64 r254, r254, 16d JALA r0, r31, 0a create_back_buffer: ADDI64 r254, r254, -32d @@ -26,12 +27,12 @@ create_back_buffer: JLTS r32, r34, :4 LI64 r2, 255d JAL r31, r0, :request_page + CP r34, r1 JMP :5 4: CP r2, r32 JAL r31, r0, :request_page - 5: CP r34, r32 - ADDI64 r34, r34, -255d - CP r32, r34 + CP r34, r1 + 5: ADDI64 r32, r32, -255d JMP :6 3: CP r1, r33 1: LD r31, r254, 0a, 32h @@ -53,6 +54,6 @@ request_page: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 440 +code size: 443 ret: 42 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_operators.txt b/hblang/tests/codegen_tests_struct_operators.txt index 54cbc18..010016a 100644 --- a/hblang/tests/codegen_tests_struct_operators.txt +++ b/hblang/tests/codegen_tests_struct_operators.txt @@ -66,11 +66,10 @@ main: ST r33, r254, 104a, 8h LD r33, r254, 96a, 8h LD r32, r254, 104a, 8h - ADD64 r33, r33, r32 - CP r1, r33 + ADD64 r1, r33, r32 LD r31, r254, 112a, 24h ADDI64 r254, r254, 136d JALA r0, r31, 0a -code size: 781 +code size: 778 ret: 10 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_patterns.txt b/hblang/tests/codegen_tests_struct_patterns.txt index 21a0402..aee57f7 100644 --- a/hblang/tests/codegen_tests_struct_patterns.txt +++ b/hblang/tests/codegen_tests_struct_patterns.txt @@ -15,14 +15,13 @@ main: CP r2, r33 JAL r31, r0, :fib_iter CP r33, r1 - SUB64 r32, r32, r33 - CP r1, r32 + SUB64 r1, r32, r33 LD r31, r254, 2a, 24h ADDI64 r254, r254, 26d JALA r0, r31, 0a fib_iter: - ADDI64 r254, r254, -48d - ST r31, r254, 0a, 48h + ADDI64 r254, r254, -40d + ST r31, r254, 0a, 40h CP r32, r2 LI64 r33, 0d LI64 r34, 1d @@ -30,17 +29,14 @@ fib_iter: JNE r32, r35, :0 JMP :1 0: CP r35, r33 - CP r36, r34 - ADD64 r35, r35, r36 + ADD64 r35, r35, r34 CP r33, r34 CP r34, r35 - CP r35, r32 - ADDI64 r35, r35, -1d - CP r32, r35 + ADDI64 r32, r32, -1d JMP :2 1: CP r1, r33 - LD r31, r254, 0a, 48h - ADDI64 r254, r254, 48d + LD r31, r254, 0a, 40h + ADDI64 r254, r254, 40d JALA r0, r31, 0a fib: ADDI64 r254, r254, -32d @@ -58,13 +54,12 @@ fib: CP r2, r34 JAL r31, r0, :fib CP r34, r1 - ADD64 r33, r33, r34 - CP r1, r33 + ADD64 r1, r33, r34 JMP :1 0: CP r1, r32 1: LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 479 +code size: 464 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_struct_return_from_module_function.txt b/hblang/tests/codegen_tests_struct_return_from_module_function.txt index 133dafb..716c114 100644 --- a/hblang/tests/codegen_tests_struct_return_from_module_function.txt +++ b/hblang/tests/codegen_tests_struct_return_from_module_function.txt @@ -28,11 +28,10 @@ main: CP r33, r0 LD r33, r254, 12a, 4h SXT32 r33, r33 - SUB64 r32, r32, r33 - CP r1, r32 + SUB64 r1, r32, r33 LD r31, r254, 16a, 24h ADDI64 r254, r254, 40d JALA r0, r31, 0a -code size: 320 +code size: 317 ret: 0 status: Ok(()) diff --git a/hblang/tests/codegen_tests_structs.txt b/hblang/tests/codegen_tests_structs.txt index 3058a99..cb7be51 100644 --- a/hblang/tests/codegen_tests_structs.txt +++ b/hblang/tests/codegen_tests_structs.txt @@ -27,22 +27,20 @@ pass: CP r32, r2 LD r33, r32, 0a, 8h LD r34, r32, 8a, 8h - SUB64 r33, r33, r34 - CP r1, r33 + SUB64 r1, r33, r34 LD r31, r254, 0a, 32h ADDI64 r254, r254, 32d JALA r0, r31, 0a odher_pass: - ADDI64 r254, r254, -40d - ST r31, r254, 0a, 40h + ADDI64 r254, r254, -32d + ST r31, r254, 0a, 32h CP r32, r2 CP r33, r1 - CP r34, r32 - CP r35, r33 - BMC r34, r35, 24h - LD r31, r254, 0a, 40h - ADDI64 r254, r254, 40d + CP r34, r33 + BMC r32, r34, 24h + LD r31, r254, 0a, 32h + ADDI64 r254, r254, 32d JALA r0, r31, 0a -code size: 400 +code size: 394 ret: 3 status: Ok(()) diff --git a/hblang/tests/codegen_tests_tests_ptr_to_ptr_copy.txt b/hblang/tests/codegen_tests_tests_ptr_to_ptr_copy.txt new file mode 100644 index 0000000..e1a51ef --- /dev/null +++ b/hblang/tests/codegen_tests_tests_ptr_to_ptr_copy.txt @@ -0,0 +1,38 @@ +main: + ADDI64 r254, r254, -10272d + ST r31, r254, 10240a, 32h + LI64 r32, 0d + 2: LI64 r33, 1024d + JLTS r32, r33, :0 + JMP :1 + 0: ADDI64 r33, r254, 0d + CP r34, r32 + ADD64 r33, r33, r34 + LI64 r34, 64d + ST r34, r33, 0a, 1h + ADDI64 r32, r32, 1d + JMP :2 + 1: LI64 r32, 1d + 5: LI64 r33, 10d + JLTS r32, r33, :3 + JMP :4 + 3: ADDI64 r33, r254, 0d + CP r34, r32 + MULI64 r34, r34, 1024d + ADD64 r33, r33, r34 + ADDI64 r34, r254, 0d + BMC r34, r33, 1024h + ADDI64 r32, r32, 1d + JMP :5 + 4: LI64 r33, 1024d + MULI64 r33, r33, 2d + ADDI64 r34, r254, 0d + ADD64 r34, r34, r33 + CP r1, r0 + LD r1, r34, 0a, 1h + LD r31, r254, 10240a, 32h + ADDI64 r254, r254, 10272d + JALA r0, r31, 0a +code size: 297 +ret: 64 +status: Ok(()) diff --git a/hblang/tests/codegen_tests_variables.txt b/hblang/tests/codegen_tests_variables.txt index 27495d2..a3790f2 100644 --- a/hblang/tests/codegen_tests_variables.txt +++ b/hblang/tests/codegen_tests_variables.txt @@ -1,16 +1,13 @@ main: - ADDI64 r254, r254, -32d - ST r31, r254, 0a, 32h + ADDI64 r254, r254, -24d + ST r31, r254, 0a, 24h LI64 r32, 1d LI64 r33, 2d - CP r34, r32 - ADDI64 r34, r34, 1d - CP r32, r34 - SUB64 r32, r32, r33 - CP r1, r32 - LD r31, r254, 0a, 32h - ADDI64 r254, r254, 32d + ADDI64 r32, r32, 1d + SUB64 r1, r32, r33 + LD r31, r254, 0a, 24h + ADDI64 r254, r254, 24d JALA r0, r31, 0a -code size: 111 +code size: 102 ret: 0 status: Ok(())