@@ -342,9 +342,9 @@ def get_rel(arg):
342
342
if arg .type == IMM :
343
343
if arg .value & 3 != 0 : # bitwise version of: arg.value % 4 != 0
344
344
raise ValueError ('Relative offset must be a multiple of 4' )
345
- return arg .value >> 2 # bitwise version of: arg.value // 4
345
+ return IMM , arg .value >> 2 # bitwise version of: arg.value // 4
346
346
if arg .type == SYM :
347
- return symbols .resolve_relative (arg .value )
347
+ return SYM , symbols .resolve_relative (arg .value )
348
348
raise TypeError ('wanted: immediate, got: %s' % arg .raw )
349
349
350
350
@@ -449,7 +449,7 @@ def i_tsens(reg_dest, delay):
449
449
return _tsens .all
450
450
451
451
452
- def i_adc (reg_dest , adc_idx , mux ):
452
+ def i_adc (reg_dest , adc_idx , mux , _not_used = None ):
453
453
_adc .dreg = get_reg (reg_dest )
454
454
_adc .mux = get_imm (mux )
455
455
_adc .sar_sel = get_imm (adc_idx )
@@ -619,7 +619,8 @@ def i_jump(target, condition='--'):
619
619
raise ValueError ("invalid flags condition" )
620
620
if target .type == IMM or target .type == SYM :
621
621
_bx .dreg = 0
622
- _bx .addr = get_abs (target )
622
+ # we track label addresses in 32bit words, but immediate values are in bytes and need to get divided by 4.
623
+ _bx .addr = get_abs (target ) if target .type == SYM else get_abs (target ) >> 2 # bitwise version of "// 4"
623
624
_bx .unused = 0
624
625
_bx .reg = 0
625
626
_bx .type = jump_type
@@ -652,7 +653,7 @@ def _jump_relr(threshold, cond, offset):
652
653
653
654
654
655
def i_jumpr (offset , threshold , condition ):
655
- offset = get_rel (offset )
656
+ offset_type , offset = get_rel (offset )
656
657
threshold = get_imm (threshold )
657
658
condition = get_cond (condition )
658
659
if condition == 'lt' :
@@ -669,7 +670,11 @@ def i_jumpr(offset, threshold, condition):
669
670
# jump over next JUMPR
670
671
skip_ins = _jump_relr (threshold + 1 , BRCOND_GE , 2 )
671
672
# jump to target
672
- offset -= 1 # adjust for the additional JUMPR instruction
673
+ if (offset_type == IMM and offset < 0 ) or offset_type == SYM :
674
+ # adjust for the additional JUMPR instruction
675
+ # for IMM offsets, the offset is relative to the 2nd instruction, so only backwards jumps need adjusting
676
+ # for SYM offsets, label offsets already include the extra instruction, so both directions need adjusting
677
+ offset -= 1
673
678
jump_ins = _jump_relr (threshold , BRCOND_GE , offset )
674
679
return (skip_ins , jump_ins )
675
680
else :
@@ -691,7 +696,7 @@ def _jump_rels(threshold, cond, offset):
691
696
692
697
693
698
def i_jumps (offset , threshold , condition ):
694
- offset = get_rel (offset )
699
+ offset_type , offset = get_rel (offset )
695
700
threshold = get_imm (threshold )
696
701
condition = get_cond (condition )
697
702
if condition == 'lt' :
@@ -711,7 +716,11 @@ def i_jumps(offset, threshold, condition):
711
716
# jump over next JUMPS
712
717
skip_ins = _jump_rels (threshold , skip_cond , 2 )
713
718
# jump to target
714
- offset -= 1 # adjust for the additional JUMPS instruction
719
+ if (offset_type == IMM and offset < 0 ) or offset_type == SYM :
720
+ # adjust for the additional JUMPS instruction
721
+ # for IMM offsets, the offset is relative to the 2nd instruction, so only backwards jumps need adjusting
722
+ # for SYM offsets, label offsets already include the extra instruction, so both directions need adjusting
723
+ offset -= 1
715
724
jump_ins = _jump_rels (threshold , jump_cond , offset )
716
725
717
726
return (skip_ins , jump_ins )
0 commit comments