Skip to content

Commit c2afdc2

Browse files
authored
Fix logical-AND operation (#138)
The current implementation of logical-and is incorrect. Fix it with the equivalent instruction combination. The short-circuit evaluation should be done later. Close #120
1 parent 0a830cc commit c2afdc2

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/arm-codegen.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
8080
case OP_bit_or:
8181
case OP_bit_xor:
8282
case OP_negate:
83-
case OP_log_and:
8483
case OP_bit_not:
8584
elf_offset += 4;
8685
return;
@@ -119,6 +118,9 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
119118
case OP_return:
120119
elf_offset += 24;
121120
return;
121+
case OP_log_and:
122+
elf_offset += 28;
123+
return;
122124
default:
123125
printf("Unknown opcode\n");
124126
abort();
@@ -439,8 +441,14 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
439441
emit(__mov_i(__EQ, rd, 1));
440442
return;
441443
case OP_log_and:
442-
/* FIXME: bad logical-and instruction */
443-
emit(__and_r(__AL, rd, rn, rm));
444+
/* TODO: short-circuit evaluation */
445+
emit(__teq(rn));
446+
emit(__mov_i(__NE, __r8, 1));
447+
emit(__mov_i(__EQ, __r8, 0));
448+
emit(__teq(rm));
449+
emit(__mov_i(__NE, rd, 1));
450+
emit(__mov_i(__EQ, rd, 0));
451+
emit(__and_r(__AL, rd, rd, __r8));
444452
return;
445453
case OP_log_or:
446454
emit(__or_r(__AL, rd, rn, rm));

src/riscv-codegen.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
5858
case OP_bit_or:
5959
case OP_bit_xor:
6060
case OP_negate:
61-
case OP_log_and:
6261
case OP_bit_not:
6362
elf_offset += 4;
6463
return;
@@ -90,6 +89,7 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
9089
case OP_branch:
9190
elf_offset += 20;
9291
return;
92+
case OP_log_and:
9393
case OP_return:
9494
elf_offset += 24;
9595
return;
@@ -413,8 +413,13 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
413413
emit(__xori(rd, rd, 1));
414414
return;
415415
case OP_log_and:
416-
/* FIXME: bad logical-and instruction */
417-
emit(__and(rd, rs1, rs2));
416+
/* TODO: short-circuit evaluation */
417+
emit(__xor(__t0, rs1, rs2));
418+
emit(__sub(rd, __t0, rs1));
419+
emit(__sub(__t0, __t0, rs2));
420+
emit(__sltu(rd, __zero, rd));
421+
emit(__sltu(__t0, __zero, __t0));
422+
emit(__and(rd, rd, __t0));
418423
return;
419424
case OP_log_or:
420425
emit(__or(rd, rs1, rs2));

0 commit comments

Comments
 (0)