From 6bcafec150f496003931a85a16e69710d7ebb60e Mon Sep 17 00:00:00 2001 From: glyh Date: Tue, 22 Oct 2024 21:36:04 +0800 Subject: [PATCH] fix riscv codegen bug --- src/riscv/codegen.mbt | 31 +++++++++++++++++-------------- test/run_js | 1 + 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/riscv/codegen.mbt b/src/riscv/codegen.mbt index 2384be6..cfa1ff0 100644 --- a/src/riscv/codegen.mbt +++ b/src/riscv/codegen.mbt @@ -332,19 +332,6 @@ fn CodegenBlock::resolve_loop( } for item in args_regs_list { let (val, reg_detect) = item - guard let Var(_) = val else { - _ => { - self.cur_fn.val.body.push( - Comment("pull immediate \{val} on to \{reg_detect}"), - ) - let pulled_reg = match val.get_type() { - Double => F(self.pull_val_f(val)) - _ => I(self.pull_val_i(val)) - } - self.mov_reg(reg_detect, pulled_reg) - continue - } - } if in_place.contains(reg_detect) { continue } @@ -400,6 +387,22 @@ fn CodegenBlock::resolve_loop( } } } + // writing to cur reg still may affect other regs that needs value on us, so + // we do this after chain is done + guard let Var(_) = val else { + _ => { + self.cur_fn.val.body.push( + Comment("pull immediate \{val} on to \{reg_detect}"), + ) + let pulled_reg = match val.get_type() { + Double => F(self.pull_val_f(val)) + _ => I(self.pull_val_i(val)) + } + self.mov_reg(reg_detect, pulled_reg) + continue + } + } + } } @@ -810,7 +813,7 @@ fn CodegenBlock::codegen(self : CodegenBlock) -> Unit { [ Sub(reg, reg_lhs, reg_rhs), Addi(reg, reg, -1), - Srai(reg, reg, 63), // take sign bit + Srli(reg, reg, 63), // take sign bit ] }, ) diff --git a/test/run_js b/test/run_js index 7156e08..94de5ee 100755 --- a/test/run_js +++ b/test/run_js @@ -1,2 +1,3 @@ #!/usr/bin/env bash + node ../js_rt/runtime.mjs $1.js