| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- DEFINITION MODULE MGen;
- (* MC64 image emitter for the M2c scalar subset (see M2c.atg).
- Buffers a single-module MC64 image and writes "<Module>.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<<hi)-(1<<lo) for set ranges. *)
- PROCEDURE BitIn;
- (* Push (pop in bitset(pop)). *)
- PROCEDURE Eq;
- PROCEDURE Neq;
- PROCEDURE ULt;
- PROCEDURE ULe;
- PROCEDURE UGt;
- PROCEDURE UGe;
- PROCEDURE ILt;
- PROCEDURE ILe;
- PROCEDURE IGt;
- PROCEDURE IGe;
- PROCEDURE RealEq;
- PROCEDURE RealNe;
- PROCEDURE RealLt;
- PROCEDURE RealLe;
- PROCEDURE RealGt;
- PROCEDURE RealGe;
- PROCEDURE NegInt;
- PROCEDURE NegReal;
- PROCEDURE IntToReal;
- (* BD int_to_long + BE long_to_real. *)
- PROCEDURE Dup;
- PROCEDURE Drop;
- (* ---- control flow (E0/E1 with fixups) ---- *)
- PROCEDURE NewLabel (): INTEGER;
- PROCEDURE DefLabel (id: INTEGER);
- PROCEDURE Jmp (id: INTEGER);
- PROCEDURE Jz (id: INTEGER);
- (* Jump if false (pops the condition). *)
- PROCEDURE PushLoop (exit: INTEGER);
- PROCEDURE PopLoop;
- PROCEDURE TopLoop (VAR exit: INTEGER): BOOLEAN;
- (* Innermost LOOP exit label; FALSE outside any LOOP. *)
- PROCEDURE NoSupEnter;
- PROCEDURE NoSupExit;
- PROCEDURE CopyName (s: ARRAY OF CHAR; VAR d: ARRAY OF CHAR);
- (* Copies zero-terminated strings between any char arrays. *)
- PROCEDURE NoEmitEnter;
- PROCEDURE NoEmitExit;
- (* Suppresses code emission (not literal capture) in declarative
- contexts such as CONST initializers and subrange bounds. *)
- PROCEDURE NoSup (): BOOLEAN;
- (* Guard depth for unsupported constructs: while > 0 the grammar
- suppresses follow-on 230 errors to avoid cascades. *)
- END MGen.
|