file PGSBcd.mesa
last modified by Satterthwaite, November 2, 1982 11:19 am
Last Edited by: Maxwell, August 8, 1983 2:34 pm
DIRECTORY
BcdDefs: TYPE,
FileSegment: TYPE USING [Pages],
IO: TYPE USING [GetIndex, SetIndex, STREAM, PutChar, UnsafePutBlock],
PGSConDefs: TYPE USING [
objectVersion, outeol, outstring, pgsVersion, resetoutstream, seterrstream,
sourceName, sourceVersion, warningsLogged, WriteSymbols],
PrincOps: TYPE USING [bytesPerPage, bytesPerWord, wordsPerPage],
PrincOpsUtils: TYPE USING [LowHalf],
Rope: TYPE USING [Cat, Find, Flatten, FromChar, Length, ROPE, Substr],
TableCommand: TYPE USING [FindInterface, FindItem, BadInterface],
UnsafeStorage: TYPE USING [GetSystemUZone, NewUObject];
PGSBcd: PROGRAM
IMPORTS IO, PGSConDefs, PrincOpsUtils, Rope, TableCommand, UnsafeStorage
EXPORTS PGSConDefs = {
StreamIndex: TYPE = INT; -- FileStream.FileByteIndex
bytesPerWord: CARDINAL = PrincOps.bytesPerWord;
BCD construction
bcdHeader: BcdDefs.BCD;
module: BcdDefs.MTRecord.direct;
export: BcdDefs.EXPHandle;
defsFile: BcdDefs.FTRecord;
codeSeg, symbolSeg: BcdDefs.SGRecord;
ssb: Rope.ROPE;
out: IO.STREAM;
moduleIndex, segIndex: StreamIndex; -- for fixup
moduleId: Rope.ROPE;
systemZone: UNCOUNTED ZONE ← UnsafeStorage.GetSystemUZone[];
InitializePackedString: PROC = {ssb ← Rope.FromChar[LOOPHOLE[0]]};
ssb.string.length ← 1; ssb.size[1] ← 0};
AddName: PROC [n: Rope.ROPE] RETURNS [name: BcdDefs.NameRecord] = {
IF n = NIL THEN name ← BcdDefs.NullName
ELSE {
lengthChar: CHAR ← PrincOpsUtils.LowHalf[n.Length[]];
name ← BcdDefs.NameRecord[ssb.Length[]+1];
ssb ← Rope.Cat[ssb, Rope.FromChar[lengthChar], 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,
frameRefs: FALSE, frameType: 0, framesize: 4,
tableCompiled: TRUE, altoCode: altoCode, long: FALSE,
residentFrame: FALSE, crossJumped: FALSE, packageable: TRUE,
gfi: 1, variables: EVNull, ngfi: 1,
boundsChecks: FALSE, nilChecks: FALSE,
extension: direct[length: 0, frag: ]];
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 ← UnsafeStorage.NewUObject[BcdDefs.EXPRecord.SIZE+size, systemZone];
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.extended ← TRUE;
h.spare1 ← TRUE; -- large eval stack
h.spare2 ← 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.Length[]].SIZE; -- all strings must be entered by now
h.mtOffset ← h.ssOffset + LOOPHOLE[h.ssLimit, CARDINAL];
h.mtLimit ← BcdDefs.MTIndex.FIRST + BcdDefs.MTRecord.direct.SIZE;
h.sgOffset ← h.mtOffset + LOOPHOLE[h.mtLimit, 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: IO.STREAM] = {
ssb ← Rope.Flatten[ssb]; -- so we can blt it out
out.UnsafePutBlock[[@bcdHeader, 0, BcdDefs.BCD.SIZE*bytesPerWord]];
out.UnsafePutBlock[[LOOPHOLE[ssb], 0, StringBody[ssb.Length[]].SIZE*bytesPerWord]];
moduleIndex ← IO.GetIndex[out];
out.UnsafePutBlock[[@module, 0, BcdDefs.MTRecord.direct.SIZE*bytesPerWord]];
segIndex ← IO.GetIndex[out];
out.UnsafePutBlock[[@codeSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]];
out.UnsafePutBlock[[@symbolSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]];
IF export # NIL THEN {
out.UnsafePutBlock[[@defsFile, 0, BcdDefs.FTRecord.SIZE*bytesPerWord]];
out.UnsafePutBlock[[export, 0, (BcdDefs.EXPRecord.SIZE+export.size)*bytesPerWord]];
systemZone.FREE[@export]}};
PagesForWords: PROC [nWords: CARDINAL] RETURNS [CARDINAL] = INLINE {
RETURN[(nWords + (PrincOps.wordsPerPage-1))/PrincOps.wordsPerPage]};
overall control
WriteBcdHeader: PUBLIC PROC [
outStream: IO.STREAM,
tableId, binaryId: Rope.ROPE, -- file being written
interfaceId, fileId: Rope.ROPE, -- interface being exported
altoCode: BOOLTRUE] = {
symbols: FileSegment.Pages;
out ← outStream;
IF tableId # NIL THEN moduleId ← tableId
ELSE {
dotIndex: INT ← Rope.Find[binaryId, "."];
IF dotIndex < 0 THEN moduleId ← binaryId
ELSE moduleId ← Rope.Substr[binaryId, 0, dotIndex]};
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"];
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"];
GO TO fail}];
FillInExport[dName, size, entry];
EXITS
fail => {
OPEN PGSConDefs;
outstring[" -- SELF used"];
outeol[2]; resetoutstream[]; warningsLogged ← TRUE;
export ← NIL}};
FillInHeader[]; -- Do this after all strings entered
WriteBcd[out];
IO.SetIndex[out, PrincOps.bytesPerPage]};
FixupBcdHeader: PUBLIC PROC = {
bytesPerPage: CARDINAL = PrincOps.bytesPerPage;
endIndex: StreamIndex ← IO.GetIndex[out];
nBytes: CARDINAL = endIndex - bytesPerPage;
IF export # NIL THEN RETURN; ** from ModuleMaker ??
module.code.length ← nBytes;
codeSeg.pages ← PagesForWords[(nBytes + (bytesPerWord-1))/bytesPerWord];
IF bcdHeader.nExports = 0 THEN {
startIndex: StreamIndex;
symbolBytes: CARDINAL;
UNTIL (startIndex ← IO.GetIndex[out]) MOD bytesPerPage = 0 DO
out.PutChar[000C]; ENDLOOP;
symbolSeg ← [
class: symbols, file: BcdDefs.FTSelf,
base: codeSeg.base+codeSeg.pages, pages: , extraPages: 0];
PGSConDefs.WriteSymbols[out, moduleId];
endIndex ← IO.GetIndex[out];
symbolBytes ← endIndex-startIndex;
symbolSeg.pages ← PagesForWords[(symbolBytes + (bytesPerWord-1))/bytesPerWord]};
IO.SetIndex[out, moduleIndex];
out.UnsafePutBlock[[@module, 0, BcdDefs.MTRecord.direct.SIZE*bytesPerWord]];
IO.SetIndex[out, segIndex];
out.UnsafePutBlock[[@codeSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]];
out.UnsafePutBlock[[@symbolSeg, 0, BcdDefs.SGRecord.SIZE*bytesPerWord]];
IO.SetIndex[out, endIndex]};
}.