Browse Source

Change branch and jump destinations to byte based distances.

ccrause 5 months ago
parent
commit
a43f1bc1d0
1 changed files with 8 additions and 5 deletions
  1. 8 5
      compiler/avr/aasmcpu.pas

+ 8 - 5
compiler/avr/aasmcpu.pas

@@ -478,8 +478,8 @@ implementation
                         firstinstruction:=curtai;
                       case taicpu(curtai).opcode of
                         A_BRxx:
-                          if (taicpu(curtai).oper[0]^.typ=top_ref) and ((taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset>64) or
-                            (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset<-63)) then
+                          if (taicpu(curtai).oper[0]^.typ=top_ref) and ((taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset>63*2) or
+                            (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset<-64*2)) then
                             begin
                               if inasmblock then
                                 Message(asmw_e_brxx_out_of_range)
@@ -499,9 +499,12 @@ implementation
                             end;
                         A_JMP:
                           { replace JMP by RJMP? ...
-                            ... but do not mess with asm block }
-                          if not(inasmblock) and (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset<=2048) and
-                          (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset>=-2047) and
+                            ... but do not mess with asm block.
+                            Replacing JMP with RJMP shorten the distance to the destination
+                            in the reverse direction by 2 bytes, hence checking against a
+                            distance of -2049*2 bytes. }
+                          if not(inasmblock) and (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset<=2047*2) and
+                          (taicpu(curtai).InsOffset-taicpu(curtai).oper[0]^.ref^.symbol.offset>=-2049*2) and
                           { jmps to function go outside the currently considered scope, so do not mess with them.
                             Those are generated by the peephole optimizer from call/ret sequences }
                           not(taicpu(curtai).oper[0]^.ref^.symbol.typ=AT_FUNCTION) then