-- file PGSBcd.mesa -- last modified by Satterthwaite, August 1, 1983 1:13 pm DIRECTORY BcdDefs: TYPE, BcdOps: TYPE USING [EXPHandle, NameString], Environment: TYPE USING [bytesPerPage, bytesPerWord], FileSegment: TYPE USING [Pages], FileStream: TYPE USING [FileByteIndex, GetIndex, SetIndex], PGSConDefs: TYPE USING [ objectVersion, outeol, outstring, pgsVersion, resetoutstream, seterrstream, sourceName, sourceVersion, warningsLogged, WriteSymbols], Spaces: TYPE USING [Node, Free, FreeString, PagesForWords, String], Stream: TYPE USING [Handle, PutByte, PutBlock], Strings: TYPE USING [String, AppendChar, AppendString], TableCommand: TYPE USING [FindInterface, FindItem, BadInterface]; PGSBcd: PROGRAM IMPORTS FileStream, PGSConDefs, Spaces, Stream, Strings, TableCommand EXPORTS PGSConDefs = { StreamIndex: TYPE = FileStream.FileByteIndex; bytesPerWord: CARDINAL = Environment.bytesPerWord; -- BCD construction bcdHeader: BcdDefs.BCD; module: BcdDefs.MTRecord; export: BcdOps.EXPHandle; defsFile: BcdDefs.FTRecord; codeSeg, symbolSeg: BcdDefs.SGRecord; ssb: BcdOps.NameString _ NIL; out: Stream.Handle; moduleIndex, segIndex: StreamIndex; -- for fixup moduleId: STRING _ [40]; InitializePackedString: PROC = { ssb _ LOOPHOLE[Spaces.String[60]]; ssb.string.length _ 1; ssb.size[1] _ 0}; AddName: PROC [n: Strings.String] RETURNS [name: BcdDefs.NameRecord] = { IF n = NIL THEN name _ BcdDefs.NullName ELSE { Strings.AppendChar[@ssb.string, LOOPHOLE[n.length]]; name _ BcdDefs.NameRecord[ssb.string.length]; Strings.AppendString[@ssb.string, n]}; RETURN}; FillInModule: PROC [name: BcdDefs.NameRecord, altoCode: BOOL] = { OPEN BcdDefs; module _ MTRecord[ name: name, namedInstance: FALSE, initial: FALSE, file: FTSelf, linkLoc: frame, config: CTNull, code: [ sgi: SGIndex.FIRST, linkspace: FALSE, packed: FALSE, offset: 0, length: 0], sseg: SGIndex.FIRST+SGRecord.SIZE, spare: FALSE, framesize: 4, tableCompiled: TRUE, long: FALSE, frameRefs: FALSE, frameType: 0, residentFrame: FALSE, crossJumped: FALSE, packageable: TRUE, gfi: 1, variables: EVNull, ngfi: 1, boundsChecks: FALSE, nilChecks: FALSE, links: LFIndex.FIRST, refLiterals: RFNull, types: TFNull]; codeSeg _ [class: code, file: FTSelf, base: 2, pages: 0, extraPages: 0]; symbolSeg _ [class: symbols, file: FTNull, base: 0, pages: 0, extraPages: 0]}; FillInExport: PROC [name: BcdDefs.NameRecord, size, entry: CARDINAL] = { export _ Spaces.Node[BcdDefs.EXPRecord.SIZE+size]; export^ _ BcdDefs.EXPRecord[ name: name, size: size, port: interface, namedInstance: FALSE, typeExported: FALSE, file: BcdDefs.FTIndex.FIRST, links:]; FOR i: CARDINAL IN [0..size) DO export.links[i] _ BcdDefs.NullLink ENDLOOP; export.links[entry] _ BcdDefs.Link[variable[vgfi:1, var:0, vtag:var]]}; FillInHeader: PROC = { OPEN h: bcdHeader; -- clear all fields LOOPHOLE[bcdHeader, ARRAY [0..BcdDefs.BCD.SIZE) OF CARDINAL] _ ALL[0]; h.versionIdent _ BcdDefs.VersionID; h.version _ PGSConDefs.objectVersion; h.creator _ PGSConDefs.pgsVersion; h.sourceVersion _ PGSConDefs.sourceVersion; h.source _ IF PGSConDefs.sourceName = NIL THEN BcdDefs.NullName ELSE AddName[PGSConDefs.sourceName]; h.nPages _ 1; h.nConfigs _ 0; h.nModules _ 1; h.nImports _ 0; h.nExports _ IF export = NIL THEN 0 ELSE 1; h.definitions _ h.repackaged _ h.typeExported _ FALSE; h.tableCompiled _ TRUE; h.versions _ FALSE; h.spare1 _ h.spare2 _ h.spare3 _ FALSE; h.firstdummy _ 2; h.nDummies _ 0; h.ctOffset _ h.impOffset _ h.ntOffset _ BcdDefs.BCD.SIZE; h.ssOffset _ BcdDefs.BCD.SIZE; h.ssLimit _ StringBody[ssb.string.length].SIZE; -- all strings must be entered by now h.mtOffset _ h.ssOffset + LOOPHOLE[h.ssLimit, CARDINAL]; h.mtLimit _ BcdDefs.MTIndex.FIRST + BcdDefs.MTRecord.SIZE; h.lfOffset _ h.mtOffset + LOOPHOLE[h.mtLimit, CARDINAL]; h.lfLimit _ LOOPHOLE[BcdDefs.LinkFrag[0].SIZE]; h.sgOffset _ h.lfOffset + LOOPHOLE[h.lfLimit, CARDINAL]; h.sgLimit _ BcdDefs.SGIndex.FIRST + 2*BcdDefs.SGRecord.SIZE; IF export # NIL THEN { h.ftOffset _ h.sgOffset + LOOPHOLE[h.sgLimit, CARDINAL]; h.ftLimit _ BcdDefs.FTIndex.FIRST + BcdDefs.FTRecord.SIZE; h.expOffset _ h.ftOffset + LOOPHOLE[h.ftLimit, CARDINAL]; h.expLimit _ BcdDefs.EXPIndex.FIRST + BcdDefs.EXPRecord.SIZE+export.size}; h.rtPages _ [0, 0]}; WriteBcd: PROC [out: Stream.Handle] = { linkFrag: ARRAY [0..BcdDefs.LinkFrag[0].SIZE) OF CARDINAL _ ALL[0]; out.PutBlock[[@bcdHeader, 0, BcdDefs.BCD.SIZE*bytesPerWord]]; out.PutBlock[[ssb, 0, StringBody[ssb.string.length].SIZE*bytesPerWord]]; Spaces.FreeString[LOOPHOLE[ssb]]; ssb _ NIL; moduleIndex _ FileStream.GetIndex[out]; out.PutBlock[[@module, 0, BcdDefs.MTRecord.SIZE*bytesPerWord]]; out.PutBlock[[@linkFrag, 0, BcdDefs.LinkFrag[0].SIZE*bytesPerWord]]; segIndex _ FileStream.GetIndex[out]; out.PutBlock[[@codeSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]]; out.PutBlock[[@symbolSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]]; IF export # NIL THEN { out.PutBlock[[@defsFile, 0, BcdDefs.FTRecord.SIZE*bytesPerWord]]; out.PutBlock[[export, 0, (BcdDefs.EXPRecord.SIZE+export.size)*bytesPerWord]]; Spaces.Free[export]}}; -- overall control WriteBcdHeader: PUBLIC PROC [ outStream: Stream.Handle, tableId, binaryId: Strings.String, -- file being written interfaceId, fileId: Strings.String, -- interface being exported altoCode: BOOL _ FALSE] = { symbols: FileSegment.Pages; out _ outStream; moduleId.length _ 0; IF tableId # NIL THEN Strings.AppendString[moduleId, tableId] ELSE FOR i: CARDINAL IN [0 .. binaryId.length) DO IF binaryId[i] = '. THEN EXIT; Strings.AppendChar[moduleId, binaryId[i]]; ENDLOOP; InitializePackedString[]; FillInModule[AddName[moduleId], altoCode]; -- fill in interface info IF interfaceId = NIL THEN export _ NIL ELSE { dName: BcdDefs.NameRecord = AddName[interfaceId]; size, entry: CARDINAL; [defsFile.version, symbols] _ TableCommand.FindInterface[interfaceId, fileId ! TableCommand.BadInterface => { OPEN PGSConDefs; seterrstream[]; outeol[1]; outstring[id]; outstring[" cannot be opened"L]; GO TO fail}]; defsFile.name _ IF fileId = NIL THEN dName ELSE AddName[fileId]; [size, entry] _ TableCommand.FindItem[symbols, moduleId ! TableCommand.BadInterface => { OPEN PGSConDefs; seterrstream[]; outeol[1]; outstring[moduleId]; outstring[" not found"L]; GO TO fail}]; FillInExport[dName, size, entry]; EXITS fail => { OPEN PGSConDefs; outstring[" -- SELF used"L]; outeol[2]; resetoutstream[]; warningsLogged _ TRUE; export _ NIL}}; FillInHeader[]; -- Do this after all strings entered WriteBcd[out]; FileStream.SetIndex[out, Environment.bytesPerPage]}; FixupBcdHeader: PUBLIC PROC = { bytesPerPage: CARDINAL = Environment.bytesPerPage; endIndex: StreamIndex _ FileStream.GetIndex[out]; nBytes: CARDINAL = endIndex - bytesPerPage; -- IF export # NIL THEN RETURN; ** from ModuleMaker ?? module.code.length _ nBytes; codeSeg.pages _ Spaces.PagesForWords[(nBytes + (bytesPerWord-1))/bytesPerWord]; IF bcdHeader.nExports = 0 THEN { startIndex: StreamIndex; symbolBytes: CARDINAL; UNTIL (startIndex _ FileStream.GetIndex[out]) MOD bytesPerPage = 0 DO out.PutByte[0] ENDLOOP; symbolSeg _ [ class: symbols, file: BcdDefs.FTSelf, base: codeSeg.base+codeSeg.pages, pages: , extraPages: 0]; PGSConDefs.WriteSymbols[out, moduleId]; endIndex _ FileStream.GetIndex[out]; symbolBytes _ endIndex-startIndex; symbolSeg.pages _ Spaces.PagesForWords[(symbolBytes + (bytesPerWord-1))/bytesPerWord]}; FileStream.SetIndex[out, moduleIndex]; out.PutBlock[[@module, 0, BcdDefs.MTRecord.SIZE*bytesPerWord]]; FileStream.SetIndex[out, segIndex]; out.PutBlock[[@codeSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]]; out.PutBlock[[@symbolSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]]; FileStream.SetIndex[out, endIndex]}; }.