Skip to content

Commit 1c37b30

Browse files
JaydeepIMGT-J-Teru
authored andcommitted
sim/riscv: fix JALR instruction simulation
Fix 32bit 'jalr rd,ra,imm' integer instruction, where RD was written before using it to calculate destination address. This commit also improves testutils.inc for riscv; make use of pushsection and popsection when adding things to .data, and setup the %gp global pointer register within the 'start' macro. Approved-By: Andrew Burgess <[email protected]>
1 parent 29736fc commit 1c37b30

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

sim/riscv/sim-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ execute_i (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op)
449449
break;
450450
case MATCH_JALR:
451451
TRACE_INSN (cpu, "jalr %s, %s, %" PRIiTW ";", rd_name, rs1_name, i_imm);
452-
store_rd (cpu, rd, riscv_cpu->pc + 4);
453452
pc = riscv_cpu->regs[rs1] + i_imm;
453+
store_rd (cpu, rd, riscv_cpu->pc + 4);
454454
TRACE_BRANCH (cpu, "to %#" PRIxTW, pc);
455455
break;
456456

sim/testsuite/riscv/jalr.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Basic jalr tests.
2+
# mach: riscv
3+
4+
.include "testutils.inc"
5+
6+
start
7+
8+
# Load desination into register a0.
9+
la a0, good_dest
10+
11+
# Jump to the destination in a0.
12+
jalr a0, a0, 0
13+
14+
# If we write destination into a0 before reading it in order
15+
# to jump, we might end up here.
16+
bad_dest:
17+
fail
18+
19+
# We should end up here.
20+
good_dest:
21+
pass
22+
fail

sim/testsuite/riscv/testutils.inc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
# Trigger OS trap.
2222
ecall;
2323
exit 0;
24-
.data
24+
.pushsection .data
2525
1: .asciz "pass\n"
26+
.popsection
2627
.endm
2728

2829
# MACRO: fail
@@ -33,14 +34,15 @@
3334
# Use stdout.
3435
li a0, 1;
3536
# Point to the string.
36-
lla a1, 1f;
37+
la a1, 1f;
3738
# Number of bytes to write.
3839
li a2, 5;
3940
# Trigger OS trap.
4041
ecall;
4142
exit 0;
42-
.data
43+
.pushsection .data
4344
1: .asciz "fail\n"
45+
.popsection
4446
.endm
4547

4648
# MACRO: start
@@ -49,4 +51,8 @@
4951
.text
5052
.global _start
5153
_start:
54+
.option push
55+
.option norelax
56+
lla gp, __global_pointer$
57+
.option pop
5258
.endm

0 commit comments

Comments
 (0)