// SwatSymInit.bcpl
// Copyright Xerox Corporation 1979, 1982
// Last modified March 24, 1982  4:29 PM by Boggs

get "AltoFileSys.d"
get "SwatSym.decl"
get "Swat.decl"

external
[
// outgoing procedures
InitSym

// incoming procedures
Allocate; SetBlock; Zero; Enqueue; CopyString

// incoming statics
cfaSym; faSymStrings; writeDate
stfQ; userStrings; builtInStrings
sysZone
]

static [ nextBuiltInSym; builtInType; numBuiltInSyms ]

//----------------------------------------------------------------------------
let InitSym() be
//----------------------------------------------------------------------------
[
// descriptors for the .Syms file
cfaSym = Allocate(sysZone, lCFA); SetBlock(cfaSym, 125252b, lCFA)
faSymStrings = Allocate(sysZone, lFA)
writeDate = Allocate(sysZone, 2)

// symbol table fragment queue
stfQ = Allocate(sysZone, 2); stfQ!0 = 0

// built-in symbols
MapBuiltInSyms(CountBuiltInSym)
let stf = Allocate(sysZone, lenSTFHeader+numBuiltInSyms*lenSym)
stf>>STF.numSyms = numBuiltInSyms
stf>>STF.builtIn = true
nextBuiltInSym = lv stf>>STF.firstSym
Enqueue(stfQ, stf)
builtInStrings = Allocate(sysZone, 2); builtInStrings!0 = 0
MapBuiltInSyms(EnterBuiltInSym)

// user string cache
userStrings = Allocate(sysZone, 2); userStrings!0 = 0
]

//----------------------------------------------------------------------------
and MapBuiltInSyms(Proc) be	// enumerates BuiltIn symbols
//----------------------------------------------------------------------------
[
builtInType = variable
Proc(300b, "LQ0"); Proc(315b, "SQ0")

builtInType = procedure
Proc(340b, "OR"); Proc(341b, "XOR"); Proc(342b, "EQV")
Proc(343b, "Multiply"); Proc(344b, "DivRem"); Proc(345b, "DivRem")
Proc(346b, "LShift"); Proc(347b, "RShift")
Proc(350b, "Branch"); Proc(351b, "Lookup")
Proc(353b, "Finish"); Proc(354b, "Abort")
Proc(355b, "LongJump"); Proc(357b, "MulPlus")
Proc(360b, "SNQ0"); Proc(361b, "SNQ1")
Proc(362b, "LY02"); Proc(363b, "LY10") 
Proc(364b, "SY01"); Proc(365b, "SY10")
Proc(366b, "Return"); Proc(367b, "StArgs"); Proc(370b, "GetFrame")

builtInType = variable
Proc(userPC, "PC"); Proc(userCry, "CRY"); Proc(userInt, "INT")
Proc(userAC0, "AC0"); Proc(userAC1, "AC1")
Proc(userAC2, "AC2"); Proc(userAC3, "AC3")

Proc(335b, "StackLimit")
Proc(420b, "DisplayListHead"); Proc(421b, "DisplayInterrupt")
Proc(424b, "MouseX"); Proc(425b, "MouseY")
Proc(426b, "CursorX"); Proc(427b, "CursorY")
Proc(430b, "RealTimeClock"); Proc(431b, "CursorBitMap")
Proc(452b, "WakeupsWaiting"); Proc(453b, "Active")
Proc(460b, "MaskTable")
Proc(500b, "InterruptPC"); Proc(501b, "InterruptVector")
Proc(521b, "KBlk"); Proc(522b, "KStat"); Proc(523b, "KAddr")
Proc(527b, "TrapPC"); Proc(530b, "TrapVector")
Proc(570b, "TimeParams")
Proc(600b, "EPLoc"); Proc(601b, "EBLoc"); Proc(602b, "EELoc")
Proc(603b, "ELLoc"); Proc(604b, "EICLoc"); Proc(605b, "EIPLoc")
Proc(606b, "EOCLoc"); Proc(607b, "EOPLoc"); Proc(610b, "EHLoc")
Proc(177016b, "UtilOut"); Proc(177020b, "XBus"); Proc(177030b, "UtilIn")
Proc(177024b, "MEAR"); Proc(177025b, "MESR"); Proc(177026b, "MECR")
Proc(177034b, "Keyboard"); Proc(177700b, "EIAOut"); Proc(177701b, "EIAIn")
Proc(177740b, "XMBankRegs")
]

//----------------------------------------------------------------------------
and CountBuiltInSym(addr, string) be numBuiltInSyms = numBuiltInSyms +1
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
and EnterBuiltInSym(addr, string) be
//----------------------------------------------------------------------------
[
let ce = Allocate(sysZone, lenCE + string>>String.length rshift 1 +1)
ce>>CE.sym = nextBuiltInSym
ce>>CE.string = ce + lenCE
CopyString(ce>>CE.string, string)
Enqueue(builtInStrings, ce)

Zero(nextBuiltInSym, lenSym)
nextBuiltInSym>>Sym.type = builtInType
nextBuiltInSym>>Sym.value = @addr //initially bind built-in symbols to this OS
nextBuiltInSym>>Sym.addr = addr
nextBuiltInSym>>Sym.builtIn = true
nextBuiltInSym>>Sym.namePos = ce
nextBuiltInSym = nextBuiltInSym + lenSym
]