@@ -342,9 +342,9 @@ def get_rel(arg):
342342 if arg .type == IMM :
343343 if arg .value & 3 != 0 : # bitwise version of: arg.value % 4 != 0
344344 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
346346 if arg .type == SYM :
347- return symbols .resolve_relative (arg .value )
347+ return SYM , symbols .resolve_relative (arg .value )
348348 raise TypeError ('wanted: immediate, got: %s' % arg .raw )
349349
350350
@@ -449,7 +449,7 @@ def i_tsens(reg_dest, delay):
449449 return _tsens .all
450450
451451
452- def i_adc (reg_dest , adc_idx , mux ):
452+ def i_adc (reg_dest , adc_idx , mux , _not_used = None ):
453453 _adc .dreg = get_reg (reg_dest )
454454 _adc .mux = get_imm (mux )
455455 _adc .sar_sel = get_imm (adc_idx )
@@ -619,7 +619,8 @@ def i_jump(target, condition='--'):
619619 raise ValueError ("invalid flags condition" )
620620 if target .type == IMM or target .type == SYM :
621621 _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"
623624 _bx .unused = 0
624625 _bx .reg = 0
625626 _bx .type = jump_type
@@ -652,7 +653,7 @@ def _jump_relr(threshold, cond, offset):
652653
653654
654655def i_jumpr (offset , threshold , condition ):
655- offset = get_rel (offset )
656+ offset_type , offset = get_rel (offset )
656657 threshold = get_imm (threshold )
657658 condition = get_cond (condition )
658659 if condition == 'lt' :
@@ -669,7 +670,11 @@ def i_jumpr(offset, threshold, condition):
669670 # jump over next JUMPR
670671 skip_ins = _jump_relr (threshold + 1 , BRCOND_GE , 2 )
671672 # 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
673678 jump_ins = _jump_relr (threshold , BRCOND_GE , offset )
674679 return (skip_ins , jump_ins )
675680 else :
@@ -691,7 +696,7 @@ def _jump_rels(threshold, cond, offset):
691696
692697
693698def i_jumps (offset , threshold , condition ):
694- offset = get_rel (offset )
699+ offset_type , offset = get_rel (offset )
695700 threshold = get_imm (threshold )
696701 condition = get_cond (condition )
697702 if condition == 'lt' :
@@ -711,7 +716,11 @@ def i_jumps(offset, threshold, condition):
711716 # jump over next JUMPS
712717 skip_ins = _jump_rels (threshold , skip_cond , 2 )
713718 # 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
715724 jump_ins = _jump_rels (threshold , jump_cond , offset )
716725
717726 return (skip_ins , jump_ins )
0 commit comments