Press enter to see results or esc to cancel.

Jumps and Labels

Turbo Pascal generates jumps in an interesting way. Since most jumps will jump to the same place, Turbo Pascal keeps them in a linked list.

Each near jump (within segment) can be part of a jump list. This procedure creates intermediate code for a jump instruction and adds it to the list.

Procedure GenerateCodeForNearJump (Var JumpRecordList: Word; OpCode: Byte);
Var Saved_PreviousJumpRecord: Word;
  Saved_PreviousJumpRecord := JumpRecordList;
  JumpRecordList := SymbolTable [stIntermediateCode].UsedSize;
  With PIntermediateCodeRecord (IncreaseSymbolTable (stIntermediateCode, 8))^ do
      RecordType := icJumpNear;
      JumpOpCode := OpCode;
      PreviousJumpRecord := Saved_PreviousJumpRecord;

Once the jumps are generated, the compiler can generate a label. This procedure generates label intermediate code and sets all jumps in a list to point to it.

Procedure GenerateLabelAndSetJumpsToIt (Var JumpRecordList: Word);
Var LabelRecord: PIntermediateCodeRecord;
    JumpRecord: PIntermediateCodeRecord;
    JumpRecordOfs: Word absolute JumpRecord;
    CurrentJumpRecord: Word;
  CurrentJumpRecord := JumpRecordList;
  JumpRecordList := 0;
  If CurrentJumpRecord = 0 then Exit;
  LabelRecord := IncreaseSymbolTable (stIntermediateCode, 3);
  LabelRecord^.Recordtype := icLabel;
  JumpRecord := Ptr (Seg (LabelRecord^), 0);
    JumpRecordOfs := CurrentJumpRecord;
    CurrentJumpRecord := JumpRecord^.PreviousJumpRecord;
    JumpRecord^.LabelRecordOffset := Ofs (LabelRecord^) + 1;
  until CurrentJumpRecord = 0;