Skip to content

Commit 1769401

Browse files
committed
nucode: fix DJNZ_FAST to store final value back into memory
1 parent 9869470 commit 1769401

File tree

3 files changed

+138
-124
lines changed

3 files changed

+138
-124
lines changed

backends/nucode/nupeep.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ int NuConvertDjnz(NuIrList *irl)
691691

692692
for (ir = irl->head; ir; ir = ir->next) {
693693
if (ir->op == NU_OP_DJNZ) {
694-
if (ir->prev->op == NU_OP_ADD_DBASE && ir->prev->prev->op == NU_OP_PUSHI) {
694+
if (ir->prev->op == NU_OP_ADD_DBASE && ir->prev->prev->op == NU_OP_PUSHI && ir->next) {
695695
int offset = ir->prev->prev->val;
696696
label = ir->label;
697697
// look back to find the label
@@ -716,6 +716,17 @@ int NuConvertDjnz(NuIrList *irl)
716716
NuIrInsertBefore(irl, labelir, newIr);
717717
newIr = NuCreateIrOp(NU_OP_LDL);
718718
NuIrInsertBefore(irl, labelir, newIr);
719+
720+
// remember to store the final value back
721+
// into memory, too
722+
ir = ir->next;
723+
newIr = NuCreateIrOp(NU_OP_PUSHI);
724+
newIr->val = offset;
725+
NuIrInsertBefore(irl, ir, newIr);
726+
newIr = NuCreateIrOp(NU_OP_ADD_DBASE);
727+
NuIrInsertBefore(irl, ir, newIr);
728+
newIr = NuCreateIrOp(NU_OP_STL);
729+
NuIrInsertBefore(irl, ir, newIr);
719730
changes++;
720731
}
721732
}

sys/nuinterp.spin

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ impl_DJNZ_FAST
11681168
#endif
11691169
sub tos, #1 wz
11701170
if_nz jmp #\do_relbranch
1171-
jmp #\impl_DROP
1171+
ret ' leave final value on stack so we can write it back
11721172

11731173
'
11741174
' GOSUB:

sys/nuinterp.spin.h

Lines changed: 125 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)