MGen.def 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. DEFINITION MODULE MGen;
  2. (* MC64 image emitter for the M2c scalar subset (see M2c.atg).
  3. Buffers a single-module MC64 image and writes "<Module>.MCD"
  4. (current directory) at EndModule. The image satisfies the
  5. reference loader (Loader2): magic "MC64", descriptor with name,
  6. TOINIT flag, global sizes (one 8-byte slot per variable), two
  7. procedures (0 = user code, 1 = decimal print helper), relative
  8. procedure-table cells, and the checksum rule.
  9. Lowered: scalar globals (INTEGER/CARDINAL/CHAR/BOOLEAN/enums as
  10. 32-bit ops on slots, REAL as binary64), CONST initializers that
  11. are plain literals (emitted as startup stores), full scalar
  12. expressions on the stack machine, IF/CASE/WHILE/REPEAT/LOOP+EXIT/
  13. FOR via E0/E1 jumps with fixups. Everything else is reported by
  14. the parser (error 230); this module only needs structurally
  15. valid placeholders for those cases.
  16. Test convention (no I/O in the language): if the source declares
  17. VAR ExitCode : INTEGER; (int family, VAR kind)
  18. the epilogue prints its value as decimal + CRLF through the
  19. embedded print procedure; otherwise the program just ends. *)
  20. TYPE
  21. LitStr = ARRAY [0 .. 63] OF CHAR;
  22. (* Literal text: "42", "-5", "0FFH", "3.14", "'a'", "TRUE". ""
  23. means "not a plain literal". *)
  24. PROCEDURE OpenModule (name: ARRAY OF CHAR);
  25. (* Resets all state; reserves slots 0..3 for the print helper. *)
  26. PROCEDURE DeclVar (name: ARRAY OF CHAR);
  27. (* Assigns the next global slot to name (any type; one slot). *)
  28. PROCEDURE DeclConst (name: ARRAY OF CHAR; lit: LitStr; t: INTEGER);
  29. (* Assigns a slot and buffers a startup initializer from the literal
  30. text (t is a SymTab.TypeIndex). Empty lit buffers a 0. *)
  31. PROCEDURE DeclConstInt (name: ARRAY OF CHAR; v: INTEGER);
  32. (* Assigns a slot with an INTEGER startup value (enum literals). *)
  33. PROCEDURE BeginBody;
  34. (* Flushes buffered CONST initializers as startup stores. *)
  35. PROCEDURE EndModule;
  36. (* Emits the epilogue, the print procedure, the procedure table,
  37. the descriptor, the checksum, and writes the .MCD file. *)
  38. PROCEDURE LoadVar (name: ARRAY OF CHAR);
  39. (* Pushes the global (placeholder push-0 if unknown). *)
  40. PROCEDURE StoreVar (name: ARRAY OF CHAR);
  41. (* Pops into the global (dropped if unknown). *)
  42. PROCEDURE LoadTemp (t: INTEGER);
  43. PROCEDURE StoreTemp (t: INTEGER);
  44. (* Direct slot access for compiler temporaries. *)
  45. PROCEDURE TempGlobal (): INTEGER;
  46. (* Allocates a fresh hidden global slot, returns its index. *)
  47. PROCEDURE PushInt (v: INTEGER);
  48. (* Small encodings for 0..15; negatives via 0,mag,sub. *)
  49. PROCEDURE PushBits (b: LONGCARD);
  50. (* 8E + 8 bytes: CARDINAL values and REAL bit patterns. *)
  51. PROCEDURE ParseInt (s: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN;
  52. (* Decimal, H-suffixed hex, optional leading '-'. *)
  53. PROCEDURE ParseCard (s: ARRAY OF CHAR; VAR v: CARDINAL): BOOLEAN;
  54. (* Decimal or H-suffixed hex (for literals past MAXINT). *)
  55. PROCEDURE ParseReal (s: ARRAY OF CHAR; VAR b: LONGCARD): BOOLEAN;
  56. (* Decimal with optional fraction/exponent/sign to IEEE bits. *)
  57. PROCEDURE CharOrd (s: ARRAY OF CHAR): INTEGER;
  58. (* ORD of a 1-character literal such as 'a'. *)
  59. PROCEDURE NegFold (a: ARRAY OF CHAR; VAR q: LitStr);
  60. (* Folds a unary minus into literal text ("5" -> "-5"). *)
  61. PROCEDURE IsLit (s: ARRAY OF CHAR): BOOLEAN;
  62. (* TRUE for non-empty literal text. *)
  63. (* ---- stack operators (one opcode each, see mc64-spec.md §11) ---- *)
  64. PROCEDURE Add;
  65. PROCEDURE Sub;
  66. PROCEDURE MulU;
  67. PROCEDURE DivU;
  68. PROCEDURE ModU;
  69. PROCEDURE MulI;
  70. PROCEDURE DivI;
  71. PROCEDURE ModI (t: INTEGER);
  72. (* Signed a MOD b with truncation semantics via temp slot t:
  73. a - (a DIV b) * b. Caller passes TempGlobal(). *)
  74. PROCEDURE RealAdd;
  75. PROCEDURE RealSub;
  76. PROCEDURE RealMul;
  77. PROCEDURE RealDiv;
  78. PROCEDURE And;
  79. PROCEDURE Or;
  80. PROCEDURE Not;
  81. PROCEDURE Power2;
  82. (* Push (1 << pop) for set element masks. *)
  83. PROCEDURE FieldMask;
  84. (* (1<<hi)-(1<<lo) for set ranges. *)
  85. PROCEDURE BitIn;
  86. (* Push (pop in bitset(pop)). *)
  87. PROCEDURE Eq;
  88. PROCEDURE Neq;
  89. PROCEDURE ULt;
  90. PROCEDURE ULe;
  91. PROCEDURE UGt;
  92. PROCEDURE UGe;
  93. PROCEDURE ILt;
  94. PROCEDURE ILe;
  95. PROCEDURE IGt;
  96. PROCEDURE IGe;
  97. PROCEDURE RealEq;
  98. PROCEDURE RealNe;
  99. PROCEDURE RealLt;
  100. PROCEDURE RealLe;
  101. PROCEDURE RealGt;
  102. PROCEDURE RealGe;
  103. PROCEDURE NegInt;
  104. PROCEDURE NegReal;
  105. PROCEDURE IntToReal;
  106. (* BD int_to_long + BE long_to_real. *)
  107. PROCEDURE Dup;
  108. PROCEDURE Drop;
  109. (* ---- control flow (E0/E1 with fixups) ---- *)
  110. PROCEDURE NewLabel (): INTEGER;
  111. PROCEDURE DefLabel (id: INTEGER);
  112. PROCEDURE Jmp (id: INTEGER);
  113. PROCEDURE Jz (id: INTEGER);
  114. (* Jump if false (pops the condition). *)
  115. PROCEDURE PushLoop (exit: INTEGER);
  116. PROCEDURE PopLoop;
  117. PROCEDURE TopLoop (VAR exit: INTEGER): BOOLEAN;
  118. (* Innermost LOOP exit label; FALSE outside any LOOP. *)
  119. PROCEDURE NoSupEnter;
  120. PROCEDURE NoSupExit;
  121. PROCEDURE CopyName (s: ARRAY OF CHAR; VAR d: ARRAY OF CHAR);
  122. (* Copies zero-terminated strings between any char arrays. *)
  123. PROCEDURE NoEmitEnter;
  124. PROCEDURE NoEmitExit;
  125. (* Suppresses code emission (not literal capture) in declarative
  126. contexts such as CONST initializers and subrange bounds. *)
  127. PROCEDURE NoSup (): BOOLEAN;
  128. (* Guard depth for unsupported constructs: while > 0 the grammar
  129. suppresses follow-on 230 errors to avoid cascades. *)
  130. END MGen.