DEFINITION MODULE MGen; (* MC64 image emitter for the M2c scalar subset (see M2c.atg). Buffers a single-module MC64 image and writes ".MCD" (current directory) at EndModule. The image satisfies the reference loader (Loader2): magic "MC64", descriptor with name, TOINIT flag, global sizes (one 8-byte slot per variable), two procedures (0 = user code, 1 = decimal print helper), relative procedure-table cells, and the checksum rule. Lowered: scalar globals (INTEGER/CARDINAL/CHAR/BOOLEAN/enums as 32-bit ops on slots, REAL as binary64), CONST initializers that are plain literals (emitted as startup stores), full scalar expressions on the stack machine, IF/CASE/WHILE/REPEAT/LOOP+EXIT/ FOR via E0/E1 jumps with fixups. Everything else is reported by the parser (error 230); this module only needs structurally valid placeholders for those cases. Test convention (no I/O in the language): if the source declares VAR ExitCode : INTEGER; (int family, VAR kind) the epilogue prints its value as decimal + CRLF through the embedded print procedure; otherwise the program just ends. *) TYPE LitStr = ARRAY [0 .. 63] OF CHAR; (* Literal text: "42", "-5", "0FFH", "3.14", "'a'", "TRUE". "" means "not a plain literal". *) PROCEDURE OpenModule (name: ARRAY OF CHAR); (* Resets all state; reserves slots 0..3 for the print helper. *) PROCEDURE DeclVar (name: ARRAY OF CHAR); (* Assigns the next global slot to name (any type; one slot). *) PROCEDURE DeclConst (name: ARRAY OF CHAR; lit: LitStr; t: INTEGER); (* Assigns a slot and buffers a startup initializer from the literal text (t is a SymTab.TypeIndex). Empty lit buffers a 0. *) PROCEDURE DeclConstInt (name: ARRAY OF CHAR; v: INTEGER); (* Assigns a slot with an INTEGER startup value (enum literals). *) PROCEDURE BeginBody; (* Flushes buffered CONST initializers as startup stores. *) PROCEDURE EndModule; (* Emits the epilogue, the print procedure, the procedure table, the descriptor, the checksum, and writes the .MCD file. *) PROCEDURE LoadVar (name: ARRAY OF CHAR); (* Pushes the global (placeholder push-0 if unknown). *) PROCEDURE StoreVar (name: ARRAY OF CHAR); (* Pops into the global (dropped if unknown). *) PROCEDURE LoadTemp (t: INTEGER); PROCEDURE StoreTemp (t: INTEGER); (* Direct slot access for compiler temporaries. *) PROCEDURE TempGlobal (): INTEGER; (* Allocates a fresh hidden global slot, returns its index. *) PROCEDURE PushInt (v: INTEGER); (* Small encodings for 0..15; negatives via 0,mag,sub. *) PROCEDURE PushBits (b: LONGCARD); (* 8E + 8 bytes: CARDINAL values and REAL bit patterns. *) PROCEDURE ParseInt (s: ARRAY OF CHAR; VAR v: INTEGER): BOOLEAN; (* Decimal, H-suffixed hex, optional leading '-'. *) PROCEDURE ParseCard (s: ARRAY OF CHAR; VAR v: CARDINAL): BOOLEAN; (* Decimal or H-suffixed hex (for literals past MAXINT). *) PROCEDURE ParseReal (s: ARRAY OF CHAR; VAR b: LONGCARD): BOOLEAN; (* Decimal with optional fraction/exponent/sign to IEEE bits. *) PROCEDURE CharOrd (s: ARRAY OF CHAR): INTEGER; (* ORD of a 1-character literal such as 'a'. *) PROCEDURE NegFold (a: ARRAY OF CHAR; VAR q: LitStr); (* Folds a unary minus into literal text ("5" -> "-5"). *) PROCEDURE IsLit (s: ARRAY OF CHAR): BOOLEAN; (* TRUE for non-empty literal text. *) (* ---- stack operators (one opcode each, see mc64-spec.md ยง11) ---- *) PROCEDURE Add; PROCEDURE Sub; PROCEDURE MulU; PROCEDURE DivU; PROCEDURE ModU; PROCEDURE MulI; PROCEDURE DivI; PROCEDURE ModI (t: INTEGER); (* Signed a MOD b with truncation semantics via temp slot t: a - (a DIV b) * b. Caller passes TempGlobal(). *) PROCEDURE RealAdd; PROCEDURE RealSub; PROCEDURE RealMul; PROCEDURE RealDiv; PROCEDURE And; PROCEDURE Or; PROCEDURE Not; PROCEDURE Power2; (* Push (1 << pop) for set element masks. *) PROCEDURE FieldMask; (* (1< 0 the grammar suppresses follow-on 230 errors to avoid cascades. *) END MGen.