Runtime.def 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. DEFINITION MODULE Runtime ;
  2. (* 8086 runtime library for TP3-compiled programs.
  3. The original embeds its runtime in the compiler's own code segment and
  4. copies it to the front of the generated code buffer (TPSRC7 "copyrt":
  5. "MOV CX,#start / REPZ MOVS.B" with SI=DI=0, then "MOV pc,#$2D7C" so the
  6. generated program starts past it; the .COM writer emits the runtime as a
  7. block ahead of the program). Same shape here: RT_Build assembles the
  8. library into rt[0..RT_Size-1], the compiler copies it to the front of its
  9. code buffer and starts pc/dc past it, so the runtime's own data lives at
  10. low, link-time-constant addresses and needs no relocation.
  11. Entry offsets are *derived* from where the code actually lands, not
  12. hardcoded - see RT_Entry. Everything the compiler calls is reached by a
  13. relative CALL, and both sides shift by the same runtime size, so the
  14. displacement arithmetic in EmCall is unaffected by the runtime being
  15. prepended. *)
  16. FROM SYSTEM IMPORT BYTE ;
  17. CONST
  18. (* entry selectors for RT_Entry *)
  19. E_InitMem = 0 ; E_ProgEnd = 1 ; E_StackChk = 2 ;
  20. E_WrInt = 3 ; E_WrChar = 4 ; E_WrBool = 5 ;
  21. E_WrReal = 6 ; E_WrLn = 7 ;
  22. E_RdInt = 8 ; E_RdChar = 9 ; E_RdBool = 10 ;
  23. E_RdLn = 11 ; E_Halt = 12 ; E_WrInl = 13 ;
  24. PROCEDURE RT_Build () ;
  25. (* assemble the runtime; idempotent, called once at Compile time *)
  26. PROCEDURE RT_Size () : CARDINAL ;
  27. (* size of the runtime in bytes - the offset at which the generated program
  28. starts inside the image *)
  29. PROCEDURE RT_Byte (i : CARDINAL) : BYTE ;
  30. (* i-th runtime byte, 0 past the end, for hex dumps *)
  31. PROCEDURE RT_Entry (i : CARDINAL) : CARDINAL ;
  32. (* offset of entry i (an E_* selector) inside the runtime; 0 if unknown *)
  33. PROCEDURE RT_CodeEnd () : CARDINAL ;
  34. (* offset where the code stops and the runtime's data block starts. Bytes
  35. from here on are strings and scratch, so disassembling them is
  36. meaningless; tests use this to sweep the code region only. *)
  37. END Runtime.