SymTab.def 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. DEFINITION MODULE SymTab;
  2. (* Symbol table with static type checking for the M2c compiler
  3. (Modula-2 program modules, procedures arrive in a later phase).
  4. - flat scopes with levels: globals at level 0, WITH statement
  5. bodies push the record's fields as an inner scope (shadowing
  6. allowed, duplicates within one level rejected)
  7. - every symbol carries a type descriptor index (InvalidType if
  8. unknown, e.g. imported names); unknown types suppress follow-on
  9. errors to avoid cascades
  10. - type descriptors: aliases (one per TYPE declaration, so
  11. self-references such as POINTER TO Person resolve), subranges,
  12. enumerations, arrays, records (with field lists), sets,
  13. pointers, predefined types, string literals
  14. - INTEGER, CARDINAL and subranges thereof form one lenient
  15. "integer family"; mixed INTEGER/REAL arithmetic is rejected *)
  16. CONST
  17. MaxSyms = 256;
  18. InvalidType = -1;
  19. (* symbol kinds *)
  20. KindConst = 0;
  21. KindType = 1;
  22. KindVar = 2;
  23. KindImport = 3;
  24. KindModule = 4;
  25. KindPredef = 5;
  26. KindField = 6;
  27. KindProc = 7;
  28. KindParam = 8;
  29. KindVarPar = 9;
  30. (* type classes returned by ClassOf *)
  31. ClInvalid = 0;
  32. ClInt = 1;
  33. ClReal = 2;
  34. ClChar = 3;
  35. ClBool = 4;
  36. ClEnum = 5;
  37. ClArray = 6;
  38. ClRecord = 7;
  39. ClSet = 8;
  40. ClPtr = 9;
  41. ClStr = 10;
  42. (* operator codes for RelCheck *)
  43. OpEq = 0; OpNeq1 = 1; OpNeq2 = 2;
  44. OpLt = 3; OpLe = 4; OpGt = 5; OpGe = 6;
  45. OpIn = 7;
  46. (* operator codes for AddOp / MulOp *)
  47. OpAdd = 0; OpSub = 1; OpOr = 2;
  48. OpTimes = 0; OpSlash = 1; OpDiv = 2; OpMod = 3; OpAnd = 4;
  49. TYPE
  50. Name = ARRAY [0 .. 63] OF CHAR;
  51. TypeIndex = INTEGER;
  52. (* ---------------- symbols and scopes ---------------- *)
  53. PROCEDURE Init;
  54. (* Clears the table and enters predefined identifiers
  55. (INTEGER, CARDINAL, SHORTINT, LONGINT, REAL, LONGREAL, CHAR,
  56. BOOLEAN, TRUE, FALSE, NIL). *)
  57. PROCEDURE Enter (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  58. (* Enters name at the current scope level (type InvalidType).
  59. Returns FALSE on duplicate within the same level. *)
  60. PROCEDURE EnterPending (name: ARRAY OF CHAR; kind: INTEGER): BOOLEAN;
  61. (* Like Enter, but remembers the entry for a later FixPending call
  62. (used for VAR identifier lists whose type is parsed afterwards).
  63. Returns FALSE on duplicate (entry not recorded). *)
  64. PROCEDURE FixPending (t: TypeIndex);
  65. (* Assigns type t to all pending entries, clears the buffer. *)
  66. PROCEDURE PendCount (): CARDINAL;
  67. (* Number of entries currently waiting for FixPending. *)
  68. PROCEDURE PendName (i: CARDINAL; VAR n: Name);
  69. (* Name of the i-th pending entry (empty string if out of range).
  70. Used by the backend to emit storage for VAR lists whose
  71. type is parsed after the identifiers. *)
  72. PROCEDURE FieldPending (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  73. (* Records a field name for record descriptor rec (type fixed later
  74. with FixPendingF). Returns FALSE on duplicate field. *)
  75. PROCEDURE FixPendingF (rec: TypeIndex; t: TypeIndex);
  76. (* Assigns type t to pending fields owned by rec. *)
  77. PROCEDURE Lookup (name: ARRAY OF CHAR): BOOLEAN;
  78. (* TRUE if name is visible (innermost scope wins). *)
  79. PROCEDURE SymType (name: ARRAY OF CHAR): TypeIndex;
  80. (* Type of innermost visible entry, InvalidType if absent. *)
  81. PROCEDURE SetSymType (name: ARRAY OF CHAR; t: TypeIndex);
  82. (* Sets type of innermost visible entry. *)
  83. PROCEDURE SymKind (name: ARRAY OF CHAR): INTEGER;
  84. (* Kind of innermost visible entry, -1 if absent. *)
  85. PROCEDURE Equal (a, b: ARRAY OF CHAR): BOOLEAN;
  86. PROCEDURE PushScope;
  87. PROCEDURE PopScope;
  88. (* WITH statement support: PushRecord pushes a record's fields. *)
  89. PROCEDURE SymLev (name: ARRAY OF CHAR): INTEGER;
  90. (* Scope level at declaration (-1 if absent). *)
  91. (* ---------------- local modules (single-file, step 6) *)
  92. (* Local MODULEs live at program level (no nesting, no bodies).
  93. EXPORT names are resolved at END (before PopScope) into an export
  94. table keyed by (module, name); MGen stores module vars qualified
  95. ("M.x") so FindVar has no collisions. *)
  96. PROCEDURE EnterModule (name: ARRAY OF CHAR): BOOLEAN;
  97. (* Enters KindModule + pushes body scope + module context.
  98. FALSE on duplicate. *)
  99. PROCEDURE ModuleAddExp (name: ARRAY OF CHAR): BOOLEAN;
  100. (* Records an export name for the current module (checked for
  101. duplicates in the export list). FALSE if not in a module. *)
  102. PROCEDURE ExitModule(): BOOLEAN;
  103. (* Resolves exports against the body scope, pops scope + context.
  104. TRUE if every EXPORT name was declared (FALSE = ghost export). *)
  105. PROCEDURE InModule (): BOOLEAN;
  106. (* TRUE inside a local MODULE body (including its procedures). *)
  107. PROCEDURE CurModName (VAR m: Name);
  108. (* Top module name, empty if none. *)
  109. PROCEDURE ExpKind (mod, exp: ARRAY OF CHAR): INTEGER;
  110. (* Exported kind, -1 if absent. *)
  111. PROCEDURE ExpType (mod, exp: ARRAY OF CHAR): TypeIndex;
  112. (* Exported type (InvalidType if absent / not a type-ish export). *)
  113. PROCEDURE ExpProc (mod, exp: ARRAY OF CHAR): INTEGER;
  114. (* Exported proc number, -1 if absent / not a procedure. *)
  115. PROCEDURE ExpQual (mod, exp: ARRAY OF CHAR; VAR qual: Name);
  116. (* Qualified MGen name ("M.x"), empty if absent / not a variable
  117. or constant. *)
  118. PROCEDURE SelfKind (mod, exp: ARRAY OF CHAR): INTEGER;
  119. (* Kind of exp as M.exp from inside M itself (live body scope,
  120. since exports resolve only at END). -1 if not applicable. *)
  121. PROCEDURE SelfQual (exp: ARRAY OF CHAR; VAR qual: Name);
  122. (* CurMod.exp for a self reference (call only when SelfKind # -1). *)
  123. (* ---------------- separate compilation units (step 11) *)
  124. (* DEFINITION modules publish every declared name into the export
  125. tables and record an in-memory interface (side table: kind, type,
  126. proc number); the body scope is popped. IMPLEMENTATION modules
  127. re-enter that interface into a fresh scope (no new MGen slots),
  128. match procedure headings via the forward machinery (231 on
  129. mismatch), and must supply every body (231). All units share one
  130. image: globals/procs accumulate, library inits run first.
  131. FROM-imported names materialize as real alias symbols (procs
  132. share the original number, so call checking is unchanged);
  133. VAR/CONST aliases map to the definition's global via GlobAlias. *)
  134. PROCEDURE ExitDefinition (): BOOLEAN;
  135. (* Publishes the current module scope, records the interface,
  136. pops scope + context. FALSE when a phase cap breaks (too many
  137. definitions, names, or exports). *)
  138. PROCEDURE OpenImplementation (name: ARRAY OF CHAR): BOOLEAN;
  139. (* Re-enters a recorded interface for its implementation body.
  140. FALSE when no definition is known (stale state untouched). *)
  141. PROCEDURE CloseImplementation (): BOOLEAN;
  142. (* 231-check (every definition proc got a body), pops scope +
  143. context. State is popped even on FALSE. *)
  144. PROCEDURE IsDefMod (name: ARRAY OF CHAR): BOOLEAN;
  145. (* TRUE for a module with a recorded definition interface. *)
  146. PROCEDURE ImpBind (mod, exp: ARRAY OF CHAR): BOOLEAN;
  147. (* Records a FROM-import alias target ("mod.exp"). FALSE when the
  148. export is unknown or the alias table is full. *)
  149. PROCEDURE ImpUnbind (name: ARRAY OF CHAR);
  150. (* Drops an alias binding (failed materialization cleanup). *)
  151. PROCEDURE EnterImpProc (name: ARRAY OF CHAR; pnum: INTEGER): BOOLEAN;
  152. (* Enters an imported procedure alias sharing pnum (signature and
  153. codegen resolve through the shared procs[] entry; the current
  154. procedure context is untouched). FALSE on duplicate or full. *)
  155. PROCEDURE GlobAlias (name: ARRAY OF CHAR; VAR q: Name): BOOLEAN;
  156. (* TRUE + qualified definition global ("M.x") for a VAR/CONST
  157. FROM-import alias (MGen slot lookup hook). *)
  158. PROCEDURE NoteProgram (): BOOLEAN;
  159. (* Claims the single program unit of the session (FALSE when one
  160. already exists). *)
  161. PROCEDURE PushRecord (t: TypeIndex): BOOLEAN;
  162. (* Pushes a scope containing t's fields (as KindField). FALSE if t
  163. is not a record type (or invalid). Caller must PopScope after. *)
  164. PROCEDURE PrintTable;
  165. (* ---------------- type descriptors ---------------- *)
  166. PROCEDURE NewAlias (): TypeIndex;
  167. PROCEDURE NewSub (base: TypeIndex): TypeIndex;
  168. PROCEDURE NewSubB (base: TypeIndex; lo, hi: INTEGER): TypeIndex;
  169. PROCEDURE NewEnum (): TypeIndex;
  170. PROCEDURE EnumAdd (t: TypeIndex);
  171. PROCEDURE NewArray (elem: TypeIndex): TypeIndex;
  172. PROCEDURE NewArrayB (elem: TypeIndex; lo, hi: INTEGER): TypeIndex;
  173. PROCEDURE NewOpen (elem: TypeIndex): TypeIndex;
  174. PROCEDURE IsOpen (t: TypeIndex): BOOLEAN;
  175. PROCEDURE NewRecord (): TypeIndex;
  176. PROCEDURE NewSet (base: TypeIndex): TypeIndex;
  177. PROCEDURE NewPtr (base: TypeIndex): TypeIndex;
  178. PROCEDURE NewStr (): TypeIndex;
  179. (* Fresh descriptors; base/elem may be InvalidType. InvalidType is
  180. returned when the table is full. *)
  181. PROCEDURE SetTarget (t, base: TypeIndex);
  182. (* Sets an alias target (TYPE declaration completion). *)
  183. PROCEDURE IntType (): TypeIndex;
  184. PROCEDURE RealType (): TypeIndex;
  185. PROCEDURE CharType (): TypeIndex;
  186. PROCEDURE BoolType (): TypeIndex;
  187. PROCEDURE ClassOf (t: TypeIndex): INTEGER;
  188. (* Resolves aliases; InvalidType maps to ClInvalid. *)
  189. PROCEDURE IsIntFamily (t: TypeIndex): BOOLEAN;
  190. (* INTEGER, CARDINAL or subrange thereof (Invalid suppresses). *)
  191. PROCEDURE SameType (a, b: TypeIndex): BOOLEAN;
  192. (* Same resolved descriptor (Invalid suppresses). *)
  193. PROCEDURE FieldExists (rec: TypeIndex; name: ARRAY OF CHAR): BOOLEAN;
  194. PROCEDURE FieldType (rec: TypeIndex; name: ARRAY OF CHAR): TypeIndex;
  195. PROCEDURE FieldOffset (rec: TypeIndex; name: ARRAY OF CHAR): INTEGER;
  196. PROCEDURE ArrayElem (t: TypeIndex): TypeIndex;
  197. PROCEDURE ArrayLo (t: TypeIndex): INTEGER;
  198. PROCEDURE ArrayHi (t: TypeIndex): INTEGER;
  199. PROCEDURE ArrayLen (t: TypeIndex): CARDINAL;
  200. PROCEDURE TypeSlots (t: TypeIndex): CARDINAL;
  201. PROCEDURE TypeLo (t: TypeIndex): INTEGER;
  202. PROCEDURE TypeHi (t: TypeIndex): INTEGER;
  203. PROCEDURE TypeLen (t: TypeIndex): CARDINAL;
  204. PROCEDURE PtrBase (t: TypeIndex): TypeIndex;
  205. (* ---------------- predicates used by grammar checks ---------------- *)
  206. (* All return TRUE if either operand is InvalidType (no cascades). *)
  207. PROCEDURE Assignable (src, dst: TypeIndex): BOOLEAN;
  208. (* Assignment compatibility (210). *)
  209. PROCEDURE ArithCheck (l, r: TypeIndex; divmod: BOOLEAN;
  210. VAR res: TypeIndex): BOOLEAN;
  211. (* + - * / (divmod FALSE) or DIV MOD (TRUE); res is result type (211). *)
  212. PROCEDURE UnaryCheck (t: TypeIndex; VAR res: TypeIndex): BOOLEAN;
  213. (* Unary + - (part of 211). *)
  214. PROCEDURE BoolCheck (t: TypeIndex): BOOLEAN;
  215. (* BOOLEAN required: NOT/AND/OR operands (212), conditions (214). *)
  216. PROCEDURE RelCheck (l, r: TypeIndex; op: INTEGER): BOOLEAN;
  217. (* = # <> < <= > >= IN (213/222 chosen by caller via op). *)
  218. PROCEDURE EqCheck (l, r: TypeIndex): BOOLEAN;
  219. (* = # compatibility, also reused for CASE label matching. *)
  220. PROCEDURE InCheck (l, set: TypeIndex): BOOLEAN;
  221. PROCEDURE SetElemCheck (first, elem: TypeIndex): BOOLEAN;
  222. PROCEDURE SetFor (elem: TypeIndex): TypeIndex;
  223. (* Fresh SET OF elem descriptor for set literals. *)
  224. PROCEDURE StrLen (s: ARRAY OF CHAR): CARDINAL;
  225. (* ---------------- procedures ---------------- *)
  226. (* Step 3: nested procedures with value and VAR parameters, forward
  227. declarations, static scoping. Proc numbers start at 1 (0 is the
  228. module body); at most MaxProcs user procedures. *)
  229. PROCEDURE EnterProc (name: ARRAY OF CHAR): BOOLEAN;
  230. (* Enters a procedure name (KindProc, InvalidType) and assigns the
  231. next proc number. FALSE on duplicate or table full. Sets the
  232. current-proc context for following EnterParam/FwdParam calls. *)
  233. PROCEDURE AllocInitNum (): INTEGER;
  234. (* Reserves the next proc number for a module BEGIN init body
  235. (step 10). Init bodies share the procedure number pool so an
  236. init emitted before a later procedure's body can never steal
  237. that procedure's number. The slot has no symbol entry and no
  238. signature; -1 when the pool is full. *)
  239. PROCEDURE IsForward (name: ARRAY OF CHAR): BOOLEAN;
  240. (* TRUE if the visible entry is a forward-declared procedure
  241. whose body is still pending. *)
  242. PROCEDURE ReuseProc (name: ARRAY OF CHAR);
  243. (* Starts the full definition of a forward-declared procedure:
  244. keeps its number, switches signature recording to define mode. *)
  245. PROCEDURE OpenProcScope;
  246. (* Pushes a scope for a procedure body, enters proc depth + 1,
  247. resets slot counters, pushes return-type context. *)
  248. PROCEDURE CloseProc;
  249. (* Pops a procedure body scope, restores the enclosing context. *)
  250. PROCEDURE EnterParam (name: ARRAY OF CHAR; isVar: BOOLEAN;
  251. t: TypeIndex): BOOLEAN;
  252. (* Enters a value (KindParam) or VAR (KindVarPar) parameter in the
  253. current procedure scope with its frame slot, appends it to the
  254. define signature. FALSE on duplicate. *)
  255. PROCEDURE SetProcRet (t: TypeIndex): BOOLEAN;
  256. (* Sets the return type (InvalidType = proper procedure). TRUE
  257. unless it mismatches a forward heading (error 231). *)
  258. PROCEDURE VerifyProc (): BOOLEAN;
  259. (* TRUE unless the define signature mismatches the forward
  260. heading (error 231). *)
  261. PROCEDURE SetForward;
  262. (* Marks the current procedure forward-declared (body pending). *)
  263. PROCEDURE AnyForward (): BOOLEAN;
  264. (* TRUE if some procedure body is still missing (error 231). *)
  265. PROCEDURE ProcNum (name: ARRAY OF CHAR): INTEGER;
  266. (* Proc number, or -1 if absent / not a procedure. *)
  267. PROCEDURE ProcRet (name: ARRAY OF CHAR): TypeIndex;
  268. (* Return type (InvalidType = proper procedure or unknown). *)
  269. PROCEDURE ProcNPar (name: ARRAY OF CHAR): CARDINAL;
  270. (* Arity from the define signature, else the forward one. *)
  271. PROCEDURE ParamType (name: ARRAY OF CHAR; i: CARDINAL): TypeIndex;
  272. PROCEDURE ParamIsVar (name: ARRAY OF CHAR; i: CARDINAL): BOOLEAN;
  273. (* i-th formal parameter (0-based), InvalidType/FALSE if absent. *)
  274. PROCEDURE ProcValid (num: INTEGER): BOOLEAN;
  275. (* TRUE if num is a declared procedure number (survives scope pops,
  276. so module procs stay callable after ExitModule). *)
  277. PROCEDURE ProcNParByNum (num: INTEGER): CARDINAL;
  278. PROCEDURE ParamTypeByNum (num: INTEGER; i: CARDINAL): TypeIndex;
  279. PROCEDURE ParamIsVarByNum (num: INTEGER; i: CARDINAL): BOOLEAN;
  280. PROCEDURE ProcRetByNum (num: INTEGER): TypeIndex;
  281. (* Same signature queries as above, keyed by procedure number. *)
  282. PROCEDURE SymSlot (name: ARRAY OF CHAR): INTEGER;
  283. (* Frame slot (params >= 3, locals negative). Only meaningful
  284. for variables at proc depth > 0. *)
  285. PROCEDURE SymDepth (name: ARRAY OF CHAR): INTEGER;
  286. (* Proc depth at declaration (0 = module level). *)
  287. PROCEDURE CurDepth (): INTEGER;
  288. (* Current proc depth (0 = module body). *)
  289. PROCEDURE CurProc (): INTEGER;
  290. (* Current proc number (0 = module body). *)
  291. PROCEDURE CurNPar (): CARDINAL;
  292. (* Arity of the current procedure (0 outside). *)
  293. PROCEDURE CurRet (): TypeIndex;
  294. (* Current return type (InvalidType = proper or outside). *)
  295. PROCEDURE InProc (): BOOLEAN;
  296. (* TRUE inside a procedure body. *)
  297. PROCEDURE InFunction (): BOOLEAN;
  298. (* TRUE inside a function (non-InvalidType return) body. *)
  299. PROCEDURE ProcNLocals (): CARDINAL;
  300. (* Local slot count of the current procedure (0 outside). *)
  301. END SymTab.