Press enter to see results or esc to cancel.

Optimizing Jumps

Turbo Pascal tries to convert all near jumps to short. This is only possible if jump displacement can be stored in a byte. This procedure is called just before the target code is generated. Since all changes reduce code size and therefore provide new opportunities for optimizations, the function is called in a loop until there are no more changes.

  Function ConvertNearJumpsToShort: Boolean;
  Var JumpRecord: PIntermediateCodeRecord;
      JumpRecordOfs: Word absolute JumpRecord;
      LabelRecord: PIntermediateCodeRecord;
      LabelRecordOfs: Word absolute LabelRecord;
      JumpDisplacement: Integer;
      NumberOfConvertedJumps: Word;
  begin
    NumberOfConvertedJumps := 0;
    JumpRecord := Ptr (SymbolTable [stIntermediateCode].Segment, Last_icJumpNear);
    LabelRecord := Ptr (SymbolTable [stIntermediateCode].Segment, 0);
    While JumpRecordOfs <> 0 do
      With JumpRecord^ do
        begin
          LabelRecordOfs := LabelRecordOffset - 1;
          JumpDisplacement := LabelRecord^.LabelAddress - JumpCodeOffset;
          If (JumpDisplacement <= 129) and (JumpDisplacement >= - 126) then
            begin
              RecordType := icJumpShort;
              Inc (NumberOfConvertedJumps);
            end;
          JumpRecordOfs := Previous_icJumpNear;
        end;
    ConvertNearJumpsToShort := NumberOfConvertedJumps <> 0;
  end;