DIRECTORY
Alloc: TYPE USING [Notifier],
CodeDefs:
TYPE
USING [
Base, BoVarIndex, BYTE, codeType, JumpType, LabelCCIndex, Lexeme, NullLex,
StoreOptions, VarComponent, VarIndex, VarNull],
ComData: TYPE USING [switches, typeRefANY],
Counting: TYPE USING [],
FOpCodes:
TYPE
USING [
qBLZL, qDUP, qKFCB, qLP, qNILCKL, qRL, qRD, qREC, qSFC, qSUB,
qWCDL, qWCIDL],
P5: TYPE USING [CallCatch, Exp, GenTempLex, PushLex, PushRhs, SAssign, SysCall],
P5L:
TYPE
USING [
AllLoaded, CopyToTemp, CopyVarItem, EasilyLoadable, InCode, GenAdd, LoadAddress,
LoadComponent, LoadVar, MakeBo, OVarItem, ReleaseVarItem, ReusableCopies,
StoreVar, TOSAddrLex, VarAlignment, VarVarAssign, VarForLex, Words],
P5U:
TYPE
USING [
InsertLabel, LabelAlloc, Out0, Out1, OutJump, PushLitVal, TreeLiteral, WordsForSei],
PrincOps: TYPE USING [SD],
RTSD: TYPE USING [sAssignComposite, sAssignCompositeNew, sSystemZone],
Stack: TYPE USING [Decr, DeleteToMark, Dump, Incr, Load, Mark, TempStore, Top],
SymbolOps:
TYPE
USING [
FirstCtxSe, NextSe, RCType, TypeRoot, UnderType, WordsForType],
Symbols:
TYPE
USING [
Base, CSEIndex, ISEIndex, lZ, RecordSEIndex, RefClass, SEIndex, seType],
SymLiteralOps: TYPE USING [TypeRef],
Tree: TYPE USING [Link, Null],
TreeOps: TYPE USING [FreeTree, ScanList];
CountingImpl:
PROGRAM
IMPORTS MPtr: ComData, P5, P5L, P5U, Stack, SymbolOps, SymLiteralOps, TreeOps
EXPORTS Counting, CodeDefs = {
OPEN CodeDefs, Counting, Symbols;
Allocate: PUBLIC PROC [zone: Tree.Link, type: SEIndex, catch: Tree.Link, pushSize: PROC] = {
Allocate generates code for space allocation from counted zones
first, generate code to perform the allocation
nwords: CARDINAL = SymbolOps.WordsForType[type];
sizeVar: VarIndex ← VarNull;
zoneVar, copyVar: VarIndex;
topVar: VarIndex ← VarNull;
initRC: RefClass = SymbolOps.RCType[SymbolOps.UnderType[type]];
typeTree: Tree.Link ← SymLiteralOps.TypeRef[SymbolOps.TypeRoot[type], FALSE];
Stack.Dump[]; Stack.Mark[];
IF zone = Tree.Null THEN LoadSystemZone[]
ELSE {
zoneVar ← P5L.VarForLex[P5.Exp[zone]];
[first: zoneVar, next: copyVar] ← P5L.ReusableCopies[zoneVar, load, FALSE];
P5L.LoadVar[zoneVar]};
IF pushSize #
NIL
THEN {
pushSize[]; -- (must save it for later)
P5U.Out0[FOpCodes.qDUP]; sizeVar ← StackTemp[1]}
ELSE P5U.PushLitVal[nwords];
P5.PushRhs[typeTree]; typeTree ← TreeOps.FreeTree[typeTree];
IF zone = Tree.Null THEN LoadSystemZone[]
ELSE {
P5L.LoadVar[copyVar];
IF ~MPtr.switches['a] THEN P5U.Out0[FOpCodes.qNILCKL]};
P5U.Out1[FOpCodes.qRL, 0];
Stack.DeleteToMark[]; Stack.Incr[1]; P5U.Out0[FOpCodes.qSFC];
P5.CallCatch[catch];
Stack.Incr[2];
if size is in a variable and we are RC, then clear the world to NIL
IF initRC # none
AND pushSize #
NIL
THEN {
IF nwords = 0 OR sizeVar = VarNull THEN ERROR;
CopyLoad[sizeVar]; P5U.Out0[FOpCodes.qBLZL]}
};
Free:
PUBLIC
PROC [var: VarIndex, counted:
BOOL, zone, catch: Tree.Link] = {
emit code for zone.FREE[@ var ! catch]
c0: VarIndex ← P5L.OVarItem[[wSize: 2, space: const[d1:0, d2:0]]];
bor: BoVarIndex ← P5L.MakeBo[var];
rr, zoneVar, copyVar: VarIndex;
cb[bor].base ← P5L.EasilyLoadable[cb[bor].base, load];
rr ← P5L.CopyVarItem[bor];
Stack.Dump[]; Stack.Mark[];
IF zone = Tree.Null THEN LoadSystemZone[]
ELSE {
zoneVar ← P5L.VarForLex[P5.Exp[zone]];
[first: zoneVar, next: copyVar] ← P5L.ReusableCopies[zoneVar, load, FALSE];
P5L.LoadVar[zoneVar]};
P5L.LoadVar[bor];
IF counted THEN [] ← VarVarAssignCounted[rr, c0, [counted: TRUE], MPtr.typeRefANY]
ELSE [] ← P5L.VarVarAssign[rr, c0, FALSE];
IF zone = Tree.Null THEN LoadSystemZone[]
ELSE {
P5L.LoadVar[copyVar];
IF ~MPtr.switches['a] THEN P5U.Out0[FOpCodes.qNILCKL]};
P5U.Out1[FOpCodes.qRL, 1];
Stack.DeleteToMark[]; Stack.Incr[1]; P5U.Out0[FOpCodes.qSFC];
P5.CallCatch[catch]};
StoreVarCounted:
PROC [r: VarIndex, options: StoreOptions] = {
WCDOp:
ARRAY
BOOL
OF CodeDefs.
BYTE = [
FALSE: FOpCodes.qWCDL, TRUE: FOpCodes.qWCIDL];
alpha: CARDINAL ← 0;
WITH cc: cb[r]
SELECT
FROM
bo => {
offset: VarComponent = cc.offset;
WITH oo: offset
SELECT
FROM
frame =>
IF oo.level = Symbols.lZ
AND oo.wSize = 2
AND oo.bSize = 0
AND oo.bd = 0
THEN {
P5L.LoadComponent[cc.base];
SELECT P5L.Words[cc.base.wSize, cc.base.bSize]
FROM
1 => P5U.Out0[FOpCodes.qLP];
2 => NULL;
ENDCASE => ERROR;
alpha ← oo.wd;
P5L.ReleaseVarItem[r]}
ELSE LoadLongAddress[r];
ENDCASE => LoadLongAddress[r]};
ENDCASE => LoadLongAddress[r];
Stack.Load[Stack.Top[4], 4]; -- REF + nWords
P5U.Out1[WCDOp[options.init], alpha]};
FillCounted:
PUBLIC
PROC [
space, source: VarIndex, options: StoreOptions, type: Symbols.SEIndex] = {
subType: CSEIndex = SymbolOps.UnderType[type];
wordsPerItem: CARDINAL = SymbolOps.WordsForType[subType];
nItems: CARDINAL = P5L.VarAlignment[space, load].wSize/wordsPerItem;
countLex: Lexeme.se = P5.GenTempLex[1];
ptrTemp: VarIndex;
loopLabel: CodeDefs.LabelCCIndex = P5U.LabelAlloc[];
testLabel: LabelCCIndex = P5U.LabelAlloc[];
LoadLongAddress[space]; ptrTemp ← StackTemp[2];
Stack.Dump[];
P5U.PushLitVal[nItems]; Stack.Decr[1];
P5U.OutJump[Jump, testLabel];
P5U.InsertLabel[loopLabel]; -- top of the assignment loop
IF SymbolOps.RCType[subType] = composite
THEN {
typeTree: Tree.Link ← SymLiteralOps.TypeRef[subType];
Stack.Mark[];
LoadLongVarAddress[source];
CopyLoad[ptrTemp];
P5.PushRhs[typeTree]; typeTree ← TreeOps.FreeTree[typeTree];
P5U.PushLitVal[wordsPerItem];
P5.SysCall[IF options.init THEN RTSD.sAssignCompositeNew ELSE RTSD.sAssignComposite]}
ELSE {
WCDOp:
ARRAY
BOOL
OF CodeDefs.
BYTE = [
FALSE: FOpCodes.qWCDL, TRUE: FOpCodes.qWCIDL];
P5L.LoadVar[source];
CopyLoad[ptrTemp];
P5U.Out1[WCDOp[options.init], 0]};
DoubleIncr[ptrTemp, wordsPerItem]; -- update pointer
P5L.StoreVar[ptrTemp];
P5.PushLex[countLex];
P5U.PushLitVal[1];
P5U.Out0[FOpCodes.qSUB];
P5U.InsertLabel[testLabel];
P5.SAssign[countLex.lexsei];
P5U.Out0[FOpCodes.qREC];
P5U.PushLitVal[0];
P5U.OutJump[JumpG, loopLabel]};