-- file Pass1.Mesa  For debugger
-- last modified by Bruce, October 17, 1980  11:32 AM
-- last modified by Satterthwaite, September 16, 1980  9:42 AM
-- last modified by Sweet, October 17, 1980  10:03 AM

DIRECTORY
  BcdDefs: TYPE USING [Link, EPIndex, GFTNull],
  ComData: TYPE USING [
    idANY, idATOM, idBOOL, idCARDINAL, idCHAR, idINT,
    idLOCK, idREAL, idSTRING, idUNWIND,
    outerCtx, seAnon, tC0, tC1,
    typeATOM, typeBOOL, typeCARDINAL, typeCHAR, typeCONDITION,
    typeINT, typeLOCK, typeREAL, typeSTRING, typeStringBody, typeTEXT],
  CompilerUtil USING [],
  LiteralOps: TYPE USING [Find],
  P1: TYPE USING [],
  Strings: TYPE USING [SubStringDescriptor],
  Symbols: TYPE USING [
    BitAddress, ByteLength, WordLength, SERecord,
    HTIndex, SEIndex, ISEIndex, CSEIndex, RecordSEIndex, CTXIndex,
    codeANY, codeINT, codeCHAR, typeANY, typeTYPE,
    HTNull, ISENull, RecordSENull, CTXNull, lZ, ctxType, seType],
  SymbolOps: TYPE USING [
    EnterExtension, EnterString, FillCtxSe, NewCtx, MakeNonCtxSe,
    MakeSeChain, NextSe],
  Table: TYPE USING [Base, Notifier, AddNotify, DropNotify],
  Tree: TYPE USING [Link, Null];
  
Pass1: PROGRAM
    IMPORTS
      LiteralOps, SymbolOps, Table, dataPtr: ComData
    EXPORTS CompilerUtil, P1 =
  BEGIN
  OPEN SymbolOps, Symbols;
  
-- symbol table bases
  seb: Table.Base;	-- semantic entry base
  ctxb: Table.Base;	-- context table base
  
  P1Notify: Table.Notifier = {seb ← base[seType]; ctxb ← base[ctxType]};
  
  
-- construction of predeclared symbols

  SubStringDescriptor: TYPE = Strings.SubStringDescriptor;
  
  MakeBasicType: PROC [code: [0..16), ordered: BOOLEAN, nBits: CARDINAL]
      RETURNS [sei: CSEIndex] = {
    sei ← MakeNonCtxSe[SIZE[basic cons SERecord]];
    seb[sei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[basic[ordered:ordered, code:code, length:nBits]]];
    RETURN};
    
  MakeRecordType: PROC [nBits: CARDINAL, default: BOOLEAN]
      RETURNS [rSei: RecordSEIndex] = {
    rSei ← LOOPHOLE[MakeNonCtxSe[SIZE[notLinked record cons SERecord]]];
    seb[rSei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[record[
		machineDep: TRUE,
		argument: FALSE,  monitored: FALSE, painted: TRUE,
		hints: [
		  comparable: FALSE, assignable: FALSE,
		  variant: FALSE, unifield: FALSE, privateFields: FALSE,
		  refField: FALSE, default: default, voidable: ~default],
		fieldCtx: NewCtx[lZ],
		length: nBits,
		linkPart: notLinked[]]]];
    RETURN};
    
  MakeRefType: PROC [refType: SEIndex] RETURNS [sei: CSEIndex] = {
    sei ← MakeNonCtxSe[SIZE[ref cons SERecord]];
    seb[sei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[ref[
		counted: FALSE, ordered: FALSE, list: FALSE, basing: FALSE,
		readOnly: FALSE,
		refType: refType]]];
    RETURN};
    
  MakeSubrangeType: PROC [origin: INTEGER, range: CARDINAL, empty: BOOLEAN]
      RETURNS [sei: CSEIndex] = {
    sei ← MakeNonCtxSe[SIZE[subrange cons SERecord]];
    seb[sei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[subrange[
		filled: TRUE, empty: empty,
		rangeType: dataPtr.idINT,
		origin: origin,  range: range]]];
    RETURN};
    
    
  SetIdAttr: PROC [sei: ISEIndex, const: BOOLEAN] = {
    seb[sei].immutable ← seb[sei].constant ← const;
    seb[sei].extended ← seb[sei].public ← seb[sei].linkSpace ← FALSE;
    seb[sei].mark3 ← seb[sei].mark4 ← TRUE};
    
  FillVariable: PROC [
      sei: ISEIndex, name: STRING, type: SEIndex, offset: BitAddress, nBits: CARDINAL] = {
    desc: SubStringDescriptor;
    hti: HTIndex;
    IF name # NIL
      THEN {desc ← [base:name, offset:0, length:name.length]; hti ← EnterString[@desc]}
      ELSE hti ← HTNull;
    FillCtxSe[sei, hti, FALSE];
    seb[sei].idType ← type;  seb[sei].idValue ← offset;  seb[sei].idInfo ← nBits;
    SetIdAttr[sei, FALSE]};
    
  FillConstant: PROC [sei: ISEIndex, name: STRING, type: SEIndex, value: WORD] = {
    desc: SubStringDescriptor ← [base:name, offset:0, length:name.length];
    FillCtxSe[sei, EnterString[@desc], FALSE];
    seb[sei].idType ← type;  seb[sei].idInfo ← 0;  seb[sei].idValue ← value;
    SetIdAttr[sei, TRUE]};
    
  FillNamedType: PROC [sei: ISEIndex, s: STRING, type: SEIndex] = {
    desc: SubStringDescriptor ← [base:s, offset:0, length:s.length];
    FillCtxSe[sei, EnterString[@desc], FALSE];  SetIdAttr[sei, TRUE];
    seb[sei].idType ← typeTYPE;  seb[sei].idInfo ← type;  seb[sei].idValue ← Tree.Null;
    SetIdAttr[sei, TRUE]};
    
  MakeTreeLiteral: PROC [val: WORD] RETURNS [Tree.Link] = {
    RETURN [[literal[info:[word[LiteralOps.Find[val]]]]]]};
    
    
  NOuterSymbols: CARDINAL = 20;	-- number of predeclared ids (outer level only)
  
  
  PrefillSymbols: PUBLIC PROC = { 
    OPEN dataPtr;
    tSei: CSEIndex;
    rSei: RecordSEIndex;
    tCtx: CTXIndex;
    sei, seChain: ISEIndex;
    
    outerChain: ISEIndex;
    NextOuterSe: PROC RETURNS [next: ISEIndex] = {
      IF outerChain = ISENull THEN ERROR;
      next ← outerChain;  outerChain ← NextSe[outerChain];  RETURN};
      
    idNAT: ISEIndex;
    Table.AddNotify[P1Notify];
    tSei ← MakeBasicType[codeANY, TRUE, WordLength];	-- guaranteed position
    outerCtx ← NewCtx[lZ];
    outerChain ← ctxb[outerCtx].seList ← MakeSeChain[outerCtx, NOuterSymbols, FALSE];
    -- make some constants
      {tC0 ← MakeTreeLiteral[0]; tC1 ← MakeTreeLiteral[1]};
      
    idANY ← NextOuterSe[];
      FillNamedType[idANY, "UNSPECIFIED"L, tSei];
      IF tSei # typeANY THEN ERROR;
    idINT ← NextOuterSe[];  typeINT ← MakeBasicType[codeINT, TRUE, WordLength];
      FillNamedType[idINT, "INTEGER"L, typeINT];
    idCHAR ← NextOuterSe[];  typeCHAR ← MakeBasicType[codeCHAR, TRUE, ByteLength];
      FillNamedType[idCHAR, "CHARACTER"L, typeCHAR];
    idBOOL ← NextOuterSe[];
      typeBOOL ← MakeNonCtxSe[SIZE[enumerated cons SERecord]];
      tCtx ← NewCtx[lZ];
      seb[typeBOOL] ← [mark3: TRUE, mark4: TRUE,
	body: cons[enumerated[
		ordered: TRUE, machineDep: TRUE, sparse: FALSE,
		valueCtx: tCtx, nValues: 2]]];
      ctxb[tCtx].seList ← seChain ← MakeSeChain[tCtx, 2, FALSE];
      FillConstant[seChain, "FALSE"L, idBOOL, 0];  seChain ← NextSe[seChain];
      FillConstant[seChain, "TRUE"L, idBOOL, 1];
      FillNamedType[idBOOL, "BOOLEAN"L, typeBOOL];
    idCARDINAL ← NextOuterSe[];
      typeCARDINAL ← MakeSubrangeType[0, 177777B, FALSE];
      FillNamedType[idCARDINAL, "CARDINAL"L, typeCARDINAL];
    FillNamedType[NextOuterSe[], "WORD"L, typeCARDINAL];
    idREAL ← NextOuterSe[];
      typeREAL ← MakeNonCtxSe[SIZE[real cons SERecord]];
      seb[typeREAL] ← [mark3:TRUE, mark4:TRUE, body:cons[real[rangeType:idINT]]];
      FillNamedType[idREAL, "REAL"L, typeREAL];
    idNAT ← sei ← NextOuterSe[];	-- NAT
      FillNamedType[sei, "***"L, MakeSubrangeType[0, 77777B, FALSE]];
    sei ← NextOuterSe[];	-- TEXT
      typeTEXT ← rSei ← MakeRecordType[nBits:2*WordLength, default:TRUE];
      seb[rSei].hints.variant ← TRUE;
      tCtx ← seb[rSei].fieldCtx; ctxb[tCtx].seList ← seChain ← MakeSeChain[tCtx, 2, FALSE];
      FillVariable[seChain, "length"L, idNAT, [wd:0, bd:0], WordLength];
	EnterExtension[seChain, default, tC0];
      seChain ← NextSe[seChain];
	BEGIN
	tag: ISEIndex = MakeSeChain[CTXNull, 1, FALSE];
	seqSei: CSEIndex = MakeNonCtxSe[SIZE[sequence cons SERecord]];
	FillVariable[tag, "maxLength"L, idNAT, [wd:1, bd:0], WordLength];
	  seb[tag].immutable ← TRUE;
	seb[seqSei] ← [mark3: TRUE, mark4: TRUE,
	    body: cons[sequence[
		packed: TRUE, machineDep: TRUE,
		controlled: TRUE, tagSei: tag,
		componentType: idCHAR]]];
	FillVariable[seChain, "text"L, seqSei, [wd:1, bd:0], WordLength];
	END;
      FillNamedType[sei, "TEXT"L, rSei];
    idSTRING ← NextOuterSe[];
    sei ← NextOuterSe[];	-- StringBody
      typeStringBody ← rSei ← MakeRecordType[nBits:2*WordLength, default:TRUE];
      seb[rSei].hints.assignable ← TRUE;	-- compatibility
      tCtx ← seb[rSei].fieldCtx; ctxb[tCtx].seList ← seChain ← MakeSeChain[tCtx, 3, FALSE];
      FillVariable[seChain, "length"L, idCARDINAL, [wd:0, bd:0], WordLength];
	EnterExtension[seChain, default, tC0];
      seChain ← NextSe[seChain];
      FillVariable[seChain, "maxlength"L, idCARDINAL, [wd:1, bd:0], WordLength];
	seb[seChain].immutable ← TRUE;
      seChain ← NextSe[seChain];
      tSei ← MakeNonCtxSe[SIZE[array cons SERecord]];
      seb[tSei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[array[
		packed: TRUE,
		indexType: MakeSubrangeType[0, 0, TRUE],
		componentType: idCHAR]]];
      FillVariable[seChain, "text"L, tSei, [wd:2, bd:0], 0];
      FillNamedType[sei, "StringBody"L, rSei];  typeSTRING ← MakeRefType[sei];
      FillNamedType[idSTRING, "STRING"L, typeSTRING];
    idLOCK ← NextOuterSe[];
      rSei ← MakeRecordType[nBits:WordLength, default:TRUE];
      tCtx ← seb[rSei].fieldCtx; ctxb[tCtx].seList ← seChain ← MakeSeChain[tCtx, 1, FALSE];
      FillVariable[seChain, NIL, idANY, [wd:0, bd:0], WordLength];
	EnterExtension[seChain, default, MakeTreeLiteral[100000B]];
      FillNamedType[idLOCK, "MONITORLOCK"L, rSei];  typeLOCK ← rSei;
    sei ← NextOuterSe[];	-- CONDITION
      rSei ← MakeRecordType[nBits:2*WordLength, default:TRUE];
      typeCONDITION ← rSei;
      tCtx ← seb[rSei].fieldCtx; ctxb[tCtx].seList ← seChain ← MakeSeChain[tCtx, 2, FALSE];
      FillVariable[seChain, "timeout"L, idCARDINAL, [wd:1, bd:0], WordLength];
	EnterExtension[seChain, default, MakeTreeLiteral[100]];
      seChain ← NextSe[seChain];
      FillVariable[seChain, NIL, idANY, [wd:0, bd:0], WordLength];
	EnterExtension[seChain, default, tC0];
      FillNamedType[sei, "CONDITION"L, rSei];  typeCONDITION ← rSei;
    sei ← NextOuterSe[];	-- MDSZone
      tSei ← MakeNonCtxSe[SIZE[zone cons SERecord]];
      seb[tSei] ← [mark3:TRUE, mark4:TRUE, body:cons[zone[counted:FALSE, mds:TRUE]]];
      FillNamedType[sei, "MDSZone"L, tSei];
    idATOM ← sei ← NextOuterSe[];
      tSei ← MakeNonCtxSe[SIZE[opaque cons SERecord]];
      seb[tSei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[opaque[
	  id: NextSe[sei],	-- seAnon
	  length: 0, lengthKnown: FALSE]]];
      tSei ← MakeRefType[tSei];
      typeATOM ← MakeNonCtxSe[SIZE[long cons SERecord]];
      seb[typeATOM] ← [mark3: TRUE, mark4: TRUE, body: cons[long[rangeType: tSei]]];
      FillNamedType[idATOM, "****"L, typeATOM]; -- so you can't say ATOM
    seAnon ← NextOuterSe[];
      FillVariable[seAnon, "?"L,  typeANY, [wd:0, bd:0], WordLength];
    FillConstant[NextOuterSe[], "TRUE"L, idBOOL, 1];	-- TRUE
    FillConstant[NextOuterSe[], "FALSE"L, idBOOL, 0];	-- FALSE
    idUNWIND ← NextOuterSe[];
      tSei ← MakeNonCtxSe[SIZE[transfer cons SERecord]];
      seb[tSei] ← [mark3: TRUE, mark4: TRUE,
	body: cons[transfer[mode:error, inRecord:RecordSENull, outRecord:RecordSENull]]];
      FillConstant[idUNWIND, "UNWIND"L, tSei,
	    LOOPHOLE[BcdDefs.Link[procedure[gfi:BcdDefs.GFTNull, ep:1, tag:TRUE]]]];
    FillConstant[NextOuterSe[], "ABORTED"L, tSei,
	    LOOPHOLE[BcdDefs.Link[procedure[gfi:BcdDefs.GFTNull, ep:2, tag:TRUE]]]];
	    
    IF outerChain # ISENull THEN ERROR;
    Table.DropNotify[P1Notify]};
    
    
  LockId: PUBLIC PROC RETURNS [HTIndex] = {
    desc: SubStringDescriptor ← [base:"LOCK"L, offset:0, length:("LOCK"L).length];
    RETURN [EnterString[@desc]]};
    
    
    
  EnterHashMark: PROC = INLINE {
    -- marks end of symbols from source file in hash table
    desc: SubStringDescriptor ← [base:"  "L, offset:1, length:0];
    [] ← EnterString[@desc]};
    
    
END.