GenBigStack.mesa
Copyright Ó 1985, 1986, 1987 by Xerox Corporation. All rights reserved.
Russ Atkinson (RRA) May 7, 1987 9:08:01 pm PDT
GenBigStack.All[] generates code for stack saving and restoring.
DIRECTORY
DragonProcessOffsets,
DragOpsCross,
DragOpsCrossProcess,
DragOpsCrossUtils,
HandCoding,
HandCodingSupport,
HandCodingPseudos;
GenBigStack: CEDAR PROGRAM
IMPORTS DragOpsCrossUtils, HandCoding, HandCodingSupport, HandCodingPseudos
= BEGIN OPEN DragonProcessOffsets, HandCoding, HandCodingSupport, HandCodingPseudos;
StackedStatusWord: TYPE = DragOpsCross.StackedStatusWord;
Word: TYPE = DragOpsCross.Word;
useCST: BOOLFALSE;
For the current SoftCard the CST instruction does not really work. We can avoid it on single-processor systems by fancy coding.
bogusPadBits: NAT ← 2;
markPadBits: NAT ← 1;
bogusStatus: StackedStatusWord = [
padBits: bogusPadBits, userMode: FALSE, trapsEnabled: FALSE];
bogusStatusWord: Word = LOOPHOLE[bogusStatus];
Size parameters (the only difference between GenStack.mesa and GenBigStack.mesa)
initialFrames: NAT = 5*256;
Initial frames to allocate.
localFrameArraySize: NAT = 2*16;
# of frames in localArray
framesToTransfer: NAT = localFrameArraySize/2;
Number of frames to transfer between local and global frame pools.
(this # should be explored)
StackSize: NAT = 128;
StackLog: NAT = BITS[[0..StackSize)];
Note: StackSize = 2^StackLog
statusMask: StackedStatusWord = [userMode: TRUE, trapsEnabled: TRUE, lBase: StackSize-1];
statusMaskWord: Word = LOOPHOLE[statusMask];
The mask for the "interesting" bits in the stacked status word.
regsPerNacho: NAT = 16;
RegArray: TYPE = ARRAY [0..regsPerNacho) OF Word;
Nacho: TYPE = LONG POINTER TO NachoRep;
NachoRep: TYPE = RECORD [
link: Nacho, -- link to previous frame
lastPC: Word, -- continuation PC for this frame
nRegs: Word, -- number of regs used for this frame at last save
others: Nacho, -- if # NIL, then it is a pointer to an aux frame
regs: RegArray -- addressable by local regs
];
linkOffset: NAT = 0;
constLink: ConstSpec = const0;
lastPCOffset: NAT = 1;
constLastPC: ConstSpec = const1;
nRegsOffset: NAT = 2;
constNRegs: ConstSpec = const2;
othersOffset: NAT = 3;
constOthers: ConstSpec = const3;
regOff: CARDINAL = 4; -- offset of DumpBlock.regs
LocalAllocationRep: TYPE = RECORD [
lockPtr: GlobalAllocationPtr, -- pointer to global lock for frame allocation
ptrs: LocalArray -- start of frame pointers
];
LocalArray: TYPE = ARRAY [0..localFrameArraySize] OF Nacho;
type of holder for frames per processor, limit is quite arbitrary (need not be fixed)
NIL is stored at the very end to make test for empty easy
GlobalAllocationPtr: TYPE = LONG POINTER TO GlobalAllocationRep;
GlobalAllocationRep: TYPE = RECORD [
lock: Word, -- global spin lock for frame allocation
gFree: Word, -- pointer to the next free frame in frames
emergency: Word, -- pointer to the emergency limit for frames
frames: GlobalArray -- space for the global pool of nachos
];
GlobalArray: TYPE = ARRAY [0..initialFrames+1] OF Nacho;
type of holder for frames, limit is quite arbitrary (need not be fixed)
NIL is stored at the very end to make test for empty easy
StackMargin: NAT ← 17;
Number of words in EU stack to reserve to handle stack overflow. Will this really be enough? This has hardware implications as well (see Don Curry)
Global labels
stackUnderflowTrap: Label ← NIL;
Entry point for handling stack underflow (aka DragonStack.StackUnderflowTrap). Also used as the PC for the bogus frame.
nakedSaveFrame: Label ← NIL;
Entry point for the internal routine (aka DragonStack.NakedSaveFrame) that saves the eldest frame.
globalFreeNacho: Label ← NIL;
Entry point for the internal routine that frees up a nacho to the central pool.
globalAllocNacho: Label ← NIL;
Entry point for the internal routine that allocates up a nacho to the central pool.
StackUnderflowTrap: PROC [stackUnderflowTrap: Label] = {
This code is used both to handle stack underflow and to restore a frame.
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
dispatchData: Label = GenDataLabel[area, StackSize*DragOpsCross.bytesPerWord];
WordAlign[];
{
At the underflow point we have no frames on the IFU stack, provided that we are in kernel mode. No code in user mode can fake this entry in kernel mode, so we don't have to check. At this point the stack may contain return values from the procedure that just returned to us, so the words from [L] through [S] must be preserved.
underflowEntry1: Label = GenLabelHere[];
This is an internal routine, which is the way we dynamically set L. At the time this routine is called, the eldest frame in the IFU stack has the value for L that limits the restored register range. This can occur because the eldest frame is the only frame (the case for stack underflow) or because the eldest frame is a real frame (when we are calling NakedRestoreEldest). The idea is to calculate the new value for the L of the frame to restore, and to set L to that value on return from the call to underflowEntry1. This routine also sets the new stack limit. We assume that the hook register points at a valid nacho!
[S] = nRegs field of the restored frame (with status bits)
GetEldestStatus[];
drRVSUB[topDst, topSrc, belowSrc];
ExtractField[first: 32-StackLog, bits: StackLog];
drDUP[];
[S] = [S-1] = the new value for L, properly masked (no status bits)
[S-2] = nRegs field of the restored frame
SetYoungestStatus[];
Set the new youngest L, which will set L on return.
[S] = the new value for L (no status bits)
[S-1] holds the nRegs field of the restored frame
Calculate & set the new stack limit.
drDUP[];
[S] = [S-1] = new L (properly masked)
[S-2] = nRegs field of the restored frame
drSUBB[StackMargin];
ExtractField[first: 32-StackLog, bits: StackLog];
SetSPLimit[];
LRegI[hook, constLastPC];
[S] = return PC for restored frame
[S-1] = new L (masked)
[S-2] = nRegs field of the restored frame.
drRETN[];
WordAlign[];
{
MakeLabelGlobal["DragonStack.RestoreFrame", stackUnderflowTrap];
This entry is used to restore a frame. We always strip off the bogus frame, then enter the underflow handler, which restores the eldest frame and puts on a bogus frame. This entry is useful in trap handlers (like page fault) that want to make sure that a certain number of frames have been restored before returning from the trap.
On entry we assume that there is a valid hook, and that we are in kernel mode with traps disabled.
At the point of call there must be at least 4 available frames on the IFU stack.
{max: caller frame, restored frame, bogus frame, FreeNacho frame}
At the point of call there must be at least 7 available registers on the EU stack.
(plus the registers needed to restore the frame)
{max: 5 = 2 at use of FreeNacho, 3 for FreeNacho}
GetEldestPC[];
Flush the bogus frame (puts eldest PC on EU stack)
drDIS[];
Restore the EU stack to its entry point
};
WordAlign[];
SetLabel[stackUnderflowTrap];
MakeLabelGlobal["DragonStack.StackUnderflowTrap", stackUnderflowTrap];
This point is used in two ways: the PC of the bogus frame points here, and this is the proper point to enter when we want to restore the eldest frame. We assume that there is a valid hook, and that we are in kernel mode with traps disabled.
LRegI[hook, constNRegs];
[S] = nRegs field of the restored frame
drLIQB[statusMaskWord];
drAND[];
[S] = nRegs field & status bits for the restored frame
drLFC[UseLabel16[underflowEntry1]];
This call sets L and [S] to the new L.
[S] = return PC for restored frame
[S-1] = new L (no status bits)
[S-2] = nRegs field of the restored frame.
SetEldestPC[];
Creates the frame that will receive the restored information & sets the return PC.
[S] = new L (no status bits)
[S-1] = nRegs field of the restored frame (with status bits, masked)
Dupl2[];
Duplicate top two words on stack
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert: TRUE, mask: StackLog]]];
[S] = new L & status bits for the restored frame
[S-1] = new L (no status bits)
[S-2] = nRegs field of the restored frame
SetEldestStatus[];
Sets the proper status for the restored frame.
Create the bogus frame
[S] = new L (no status bits)
[S-1] = nRegs field of the restored frame
PushByteAddr[area, stackUnderflowTrap];
SetEldestPC[];
Create the bogus frame & set the PC
drADDQB[bogusStatusWord];
Set the bogus bits to mark the bogus frame
SetEldestStatus[];
the bogus frame is now correct
ExtractField[first: 32-StackLog, bits: StackLog];
[S] = #regs (no status bits) for the restored frame
At this point L is correctly set as well to the base of the frame to be restored. The hook register is pointing at the nacho chain to be used to restore the frame.
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
[S] = dispatch PC
[S-1] = #regs (no status bits)
MoveReg[temp, hook];
temp has the nacho to chase
MoveRegI[hook, hook, constLink];
hook now has the next frame's head nacho
drJSD[];
Dispatch to restore the proper # of regs; on arrival:
[S] = #regs (no status bits)
};
{
This section contains the various routines for restoring varying numbers of registers and the table for dispatching to those routines.
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
SetLabel[singleLabel];
At this point we execute a varying number of RAIs to restore the regs
[S] = #regs (no status bits)
FOR i: NAT DECREASING IN [0..regsPerNacho) DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
MoveReg[topDst, temp]; FreeNacho[];
free the nacho pointed to by temp (& flush the # regs)
MakeLabelGlobal["DragonStack.StackUnderflowReturn", GenLabelHere[]];
Now S is back at its initial value, hook is properly set
drRETN[];
Return from kernel, no change to S.
SetLabel[fatalLabel];
We should never have this happen! Control only gets here for a truly smashed nacho!
Pause[];
SetLabel[multiLabel];
Now do regsPerNacho reads into the stack registers from the nacho.
FOR i: NAT IN [0..regsPerNacho) DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
LReg[temp]; MoveRegI[temp, temp, constOthers];
push the nacho to free; point hook at the next nacho in the chain
[S] = nextNacho, [S-1] = #regs
FreeNacho[];
free the nacho pointed to by [S]
[S] = # regs
EU stack high water mark: 7 registers
drSUBB[regsPerNacho];
adjust #regs
[S] = #regs left
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
drAL[regsPerNacho];
adjust L to account for the registers restored
drJSD[];
Dispatch to restore the proper # of regs
[S] = # of regs to save
{
Generate the dispatch table
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
newPC: CARD ← dispatchData.offset;
HandCodingSupport.SetOutputPC[newPC];
FOR nr: NAT DECREASING IN [0..regsPerNacho] DO
OutputByteAddr[area, singleLabel, 3*nr];
ENDLOOP;
FOR nr: NAT IN (regsPerNacho..regsPerNacho*4] DO
OutputByteAddr[area, multiLabel, 0];
ENDLOOP;
FOR nr: NAT IN (4*regsPerNacho..StackSize) DO
OutputByteAddr[area, fatalLabel, 0];
ENDLOOP;
HandCodingSupport.SetOutputPC[oldPC];
};
};
};
StackOverflowTrap: PROC [underflowLabel: Label] = {
Note: we assume that there is sufficient reserve space on the EU stack and the IFU stack to complete the saving of one frame. Traps are automatically disabled on entry to this routine, and must be reenabled on the way out.
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
internalSaveSetup: Label ← GenLabel[];
exitLabel: Label ← GenLabel[];
dispatchLabel: Label = GenLabel[];
dispatchData: Label = GenDataLabel[area, StackSize*DragOpsCross.bytesPerWord];
{
DragonStack.IFUStackOverflowTrap: This part is for IFU stack overflow. Note that we are NOT assured of a non-empty frame on the IFU stack.
ifuEntryLabel: Label = GenLabel[];
ProcedureEntry[ifuEntryLabel, 0, TRUE];
We get no arguments.
MakeLabelGlobal["DragonStack.IFUStackOverflowTrap", ifuEntryLabel];
FillTrap[IFUStackOverflowTrap, ifuEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
AllocNacho[];
Allocate the next save block to use
([S] = newBlock, [S-1] = eldest status)
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook (and in temp).
L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
SetLabel[dispatchLabel];
We get here to do the dispatch to the appropriate register saving code.
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
Fetch the dispatch address
drSFC[];
Dispatch to restore the proper # of regs
On return: [S] = eldest status, [S-1] = sampled carry bit
This is the common exit stuff for the stack overflow handlers. The idea is to keep the stack limit register set with enough reserve space to be able to prevent wraparound when we execute code where traps are disabled. Also, the saved L for the bogus frame must be consistent with the number of registers in the youngest saved frame (referenced by hook).
SetLabel[exitLabel];
MakeLabelGlobal["DragonStack.StackOverflowExit", exitLabel];
At this point: [S] holds the eldest status, [S-1] holds the carry bit
PushByteAddr[area, underflowLabel];
SetEldestPC[];
Install the new bogus frame & set its PC (creating a new eldest frame)
ExtractField[first: 32-StackLog, bits: StackLog]; drDUP[];
drADDQB[bogusStatusWord];
SetEldestStatus[];
Calculate & set the L for the bogus frame. [S] still holds that L.
drSUBB[StackMargin];
ExtractField[first: 32-StackLog, bits: StackLog];
SetSPLimit[];
Calculate & set the new stack limit. S is now back to its original depth.
[S] holds the carry bit
MakeLabelGlobal["DragonStack.StackOverflowReturn", GenLabelHere[]];
drRUSUB[topDst, const0, popSrc];
Set the carry bit that was saved on the overflow
drRETN[];
At this point we go back to the code that overflowed.
};
{
DragonStack.EUStackOverflowTrap: If we save a frame, but don't get any more space in the EU stack, then we have to keep saving frames until a frame is saved that has at least one EU register saved with it. We know that there is a non-empty frame on the register stack, otherwise why would we get an EU stack overflow?
euEntryLabel: Label = GenLabel[];
ProcedureEntry[euEntryLabel, 0, TRUE];
We get no arguments.
MakeLabelGlobal["DragonStack.EUStackOverflowTrap", euEntryLabel];
FillTrap[EUStackOverflowTrap, euEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
{
loopLabel: Label = GenLabelHere[];
[S] = eldest status
AllocNacho[];
Allocate the next save block to use
[S] = newBlock, [S-1] = eldest status
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook (and in temp).
L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
drRJNEBJ[left: topSrc, right: const0, dist: UseLabel8B[dispatchLabel]];
If we have registers to save, then go do them.
drRJEBJ[popSrc, topSrc, UseLabel8B[loopLabel]];
Discard [S] and go loop (unconditionally, really).
[S] = eldest status
};
};
{
InstallBogusFrame is useful for external callers of DragonStack.NakedSaveFrame. Once the necessary frames have been saved, the bogus frame can be created using this routine.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0, TRUE];
drQADD[pushA0, const0];
Capture (& clear) the carry bit
GetEldestStatus[];
push the eldest status
[S] holds the eldest status, [S-1] holds the carry bit
MakeLabelGlobal["DragonStack.InstallBogusFrame", exitEntryLabel];
drJB[UseLabel8A[exitLabel]];
};
{
RemoveBogusFrame is useful for external callers of DragonStack.NakedSaveFrame. Before NakedSaveFrame can be called, the bogus frame must be removed, and the carry bit & current eldest status must be pushed.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0];
MakeLabelGlobal["DragonStack.RemoveBogusFrame", exitEntryLabel];
GetEldestPC[];
discard the bogus frame
[S] holds the PC for the bogus frame
drRUADD[topDst, const0, const0];
Capture (& clear) the carry bit
[S] holds the carry bit
GetEldestStatus[];
[S] holds the eldest status, [S-1] holds the carry bit
ProcedureExit[1];
return the new eldest status & carry bit
};
{
This is an internal routine that we use to setup stuff (especially L) prior to saving the frame. It needs to be a procedure because we have to set L (sigh).
before entry:
hook = frame chain
[S] = newBlock, [S-1] = eldest status
after exit:
L = L of frame to save
[S] = #regs, [S-1] = eldest status
At call requires at least 1 IFU frame & 4 EU stack regs.
eldestStatus: RegSpec = reg0;
localHook: RegSpec = reg1;
localStatus: RegSpec = reg2;
ProcedureEntry[internalSaveSetup, 2];
Make our local L good for a few locals, we get one sent to us on the stack
LReg[hook]; drPSB[linkOffset];
store the hook into the new dump block
[S] = localHook = newBlock; (localHook+linkOffset)^ = hook
PReg[hook]; PReg[temp];
store the new dump block addr into hook and temp
[S] = localHook = hook = temp = newBlock
LReg[eldestStatus];
ExtractField[first: 32-StackLog, bits: StackLog];
SetYoungestStatus[];
Get the L for the frame we wish to save, put it into localStatus and youngest L
[S] = localStatus = L of frame to save; [S-1] = localHook, [S-2] = eldest status
(note that the status bits must be clear in [S])
GetEldestPC[]; drSRIn[localHook, lastPCOffset];
Store the continuation PC and remove the eldest PC from the ifuStack.
((localHook+lastPCOffset)^ = PC of frame to save)
GetEldestStatus[];
Sample & save the new eldest status
[S] (aka localStatus) = eldest status (the new version)
[S-1] = localHook
[S-2] = eldest status (the old version)
drRVSUB[c: pushDst, a: localStatus, b: eldestStatus];
Calculate the number of regs in this frame
[S] = # of regs to save (with garbage in top bits)
[S-1] (aka localStatus) = status
[S-2] = localHook
[S-3] = eldest status
MoveReg[eldestStatus, localStatus];
[S] = # of regs to save (with garbage in top bits)
[S-1] (aka localStatus) = eldest status (the new version)
[S-2] = localHook
[S-3] = eldest status (the new version)
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert: TRUE, mask: StackLog]]];
insert # of regs into the saved status word (aka localStatus)
[S] (aka localStatus) = #regs (+ status bits)
[S-1] (aka localHook)
[S-2] = eldest status
drWRI[localStatus, localHook, nRegsOffset];
Save #regs into the save block & into eldestStatus
(localHook+nRegsOffset)^ ← localStatus
[S] = #regs, [S-1] = localHook, [S-2] = eldest status
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[mask: StackLog]]];
[S] = #regs, [S-1] = eldest status
ProcedureExit[2];
return the values
[S] = #regs, [S-1] = eldest status
};
{
DragonStack.NakedSaveFrame: This is the entry point for saving the eldest stack frame in the IFU stack. We assume that the bogus frame has been removed, and that we are in kernel mode with traps disabled. The top two words must be as they were set by RemoveBogusFrame.
At entry:
[S] = the sampled eldest status
[S-1] = the sampled carry bit
At the point of call there must be at least 2 available frames on the IFU stack.
{max: caller frame, internalSaveSetup/AllocNacho frame}
At the point of call there must be at least 6 available registers on the EU stack.
{max: 6 = 2 at first use of AllocNacho + 4 for AllocNacho}
At exit:
[S] = the sampled eldest status
[S-1] = the sampled carry bit
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
shortLabel: Label = GenLabel[];
SetLabel[nakedSaveFrame];
MakeLabelGlobal["DragonStack.NakedSaveFrame", nakedSaveFrame];
AllocNacho[];
Allocate the next save block to use
[S] = newBlock, [S-1] = eldest status, [S-2] = sampled carry bit
drLFC[UseLabel16[internalSaveSetup]];
Init the new save block. Its address is left in hook. L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status, [S-2] = sampled carry bit
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
Fetch the dispatch address
drJSD[];
Dispatch to save the proper # of regs
after JSD: [S] = #regs, [S-1] = eldest status, [S-2] = sampled carry bit
SetLabel[singleLabel];
FOR i: NAT DECREASING IN [0..regsPerNacho) DO
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
drDIS[];
Discard #regs
drRETN[];
This return does not enable traps, and does not disturb S, but does restore L, which was previously clobbered.
[S] = new eldest status, [S-1] = sampled carry bit
SetLabel[fatalLabel];
For now we just crash. Eventually we should be more robust, saving away the regs, then giving the offending process a "stack fault".
Pause[];
SetLabel[multiLabel];
FOR i: NAT IN [0..regsPerNacho) DO
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
At this point we have regsPerNacho more EU regs available than when we started, since we just saved that many. The EU high-water mark must therefore occur before registers are saved.
LReg[temp];
Push the address of the completed save block
[S] = temp = oldBlock; [S-1] = #regs; [S-2] = eldest status; [S-3] = sampled carry
AllocNacho[]; -- EU stack high water mark: 8 regs
Allocate the next save block to use (it gets left on the stack)
[S] = newBlock; [S-1] = temp = oldBlock; [S-2] = #regs
[S-3] = eldest status; [S-4] = sampled carry
PReg[temp]; drWSB[othersOffset];
set temp to the new dump block addr
also store the new block addr into the old block
(temp = (temp'+othersOffset)^ = newBlock; [S] = #regs)
drSUBB[regsPerNacho];
adjust #regs
[S] = #regs, [S-1] = eldest status
PushWordAddr[area, dispatchData];
drQRX[topAtop, belowSrc];
Fetch the dispatch address
drAL[regsPerNacho];
adjust L for the restored regs
drJSD[];
Dispatch to save the proper # of regs
[S] = #regs, [S-1] = eldest status, [S-2] = sampled carry bit
{
Generate the dispatch table
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
HandCodingSupport.SetOutputPC[dispatchData.offset];
FOR nr: NAT DECREASING IN [0..regsPerNacho] DO
OutputByteAddr[area, singleLabel, 3*nr];
ENDLOOP;
FOR nr: NAT IN (regsPerNacho..regsPerNacho*4] DO
OutputByteAddr[area, multiLabel, 0];
ENDLOOP;
FOR nr: NAT IN (4*regsPerNacho..StackSize) DO
OutputByteAddr[area, fatalLabel, 0];
ENDLOOP;
HandCodingSupport.SetOutputPC[oldPC];
};
};
};
AllocNacho: PROC = {
Allocate a nacho using the local array of nachos, defaulting to the global array of nachos if not immediately successful. The address of the allocated nacho is left on the stack. There is no provision for failure from the global array at this time.
Requires at least 1 available IFU frame & 4 available EU stack registers.
exitLabel: Label = GenLabel[];
enterLabel: Label = GenLabelHere[];
LRegI[free, const0];
Push the next nacho address. If none in our local cache, a 0 will be pushed.
drLC0[];
push 0 for comparison (this cycle is free due to the preceding fetch)
drRJNEBJ[left: topSrc, right: belowSrc, dist: UseLabel8B[exitLabel]];
Jump if we got the nacho; leave 0 on stack if not. Predict success.
drLFC[UseLabel16[globalAllocNacho]];
Call to do this the expensive way (S←S-2)
drJB[UseLabel8A[enterLabel]];
Go to retry the allocation from the start (stack is back to ground level).
SetLabel[exitLabel];
This is the successful exit point. The allocated nacho is on the stack.
AddReg[free, const1];
Adjust the free pointer, since we got a nacho
[S] = 0, [S-1] = new nacho
drPSB[othersOffset];
always clears the others field on allocation
[S] = new nacho
};
GlobalAllocNacho: PROC = {
Replenish the local nacho array from the global nacho array. There is no provision for failure from the global array at this time.
At entry, [S] = [S-1] = 0, at exit S←S-2, but the local array has nachos. AllocNacho is the only caller of this routine!
exitLabel: Label = GenLabel[];
globalAllocNacho ← GenLabelHere[];
MakeLabelGlobal["DragonStack.GlobalAllocNacho", globalAllocNacho];
drRRX[c: belowDst, a: base, b: popSrc];
Replace [S-1] (was 0) with @lock; also S←S-1
[S] = @lock
LReg[process];
Push the desired new value for the lock field
[S] = process; [S-1] = @lock
IF useCST
THEN {
drLC0[];
Push the assumed old value of the lock field (i.e. free)
[S] = 0; [S-1] = process; [S-2] = @lock
{retryLabel: Label = GenLabelHere[];
drCST[0];
Try for the global lock for the nacho allocator. We store our process ID into the lock if the lock was free, and return the sampled value of the lock at the start of the instruction in any case.
[S] = sample; [S-1] = 0; [S-2] = process; [S-3] = @lock
drRJNEB[left: popSrc, right: belowSrc, dist: UseLabel8B[retryLabel]];
Jump back to retry if we did not get it (predict success), and pop the sampled value. If this loop causes us to hold the bus too often, we can always insert a spin loop that does not use CST.
[S] = 0; [S-1] = process; [S-2] = @lock
drAS[256-2];
On success, flush the old & new values, since we don't need them.
[S] = @lock
};
}
ELSE {
No locking is OK for the single-processor SoftCard.
drPSB[0];
};
drRSB[1];
Get the global free pointer (gFree) on the stack
@base.gFree = @base.lock + 1
[S] = gFree; [S-1] = @lock
drLIB[framesToTransfer]; drRVSUB[free, free, popSrc];
Adjust the free pointer to accomodate the next transfers.
FOR i: NAT IN [0..framesToTransfer) DO
drRSB[i]; LReg[free];
EU stack high water mark: 8 regs
[S] = free, [S-1] = word from nacho, [S-2] = gFree, [S-3] = @lock
drWB[i];
(free+i)^ ← (gFree+i)^
ENDLOOP;
drADDB[framesToTransfer];
adjust gFree for the number of nachos allocated
[S] = new gFree; [S-1] = @lock
drPSB[1];
Write gFree back, leaving the addr of the lock on the stack.
[S] = @lock
drLC0[]; drWSB[0];
Clear out the lock, which lets other processors get their chance.
stack is now at original level
drRETN[];
};
FreeNacho: PROC [] = {
Free a nacho (in [S]) using the local array of nachos, defaulting to the global array of nachos if not immediately successful. There is no provision for failure from the global array at this time.
At usage, requires at least 1 available IFU frame (for globalFreeNacho call).
At usage, requires at least 3 available EU stack registers (not including argument).
exitLabel: Label = GenLabel[];
enterLabel: Label = GenLabelHere[];
drRVSUB[c: pushDst, a: free, b: const1];
[S] = free-1; [S-1] = nacho to free
drRJNEBJ[left: topSrc, right: base, dist: UseLabel8B[exitLabel]];
Determine if free-1 will collide with base, jump if not. Free-1 is left on the stack for use in storing away the freed frame. Predict success.
drLFC[UseLabel16[globalFreeNacho]];
In the hard case we call a routine to munch on the global table.
EU stack high water mark: 3 regs (not including argument)
SetLabel[exitLabel];
At this point the address of the slot to hold the nacho is on the stack. First we store that address to free, then we load the nacho to be freed and store it into the slot.
PReg[free]; drWB[0];
(free ← [S])^ ← [S-1]; S←S-2
};
GlobalFreeNacho: PROC = {
This routine ensures that there is enough room in the processor free nacho cache to free up a nacho, and we know that the nacho cache is currently full of free nachos. We have already tested for the fast case, and we call this when [S] = base. At exit, [S] = new free.
At entry: [S] = base = free - 1
At the call, we need at least 1 available IFU frame & 2 available EU stack registers.
(does not include the argument register)
At entry: [S] = new free = old free + framesToTransfer - 1
lockReg: RegSpec = reg0;
globalFreeNacho ← GenLabel[];
ProcedureEntry[globalFreeNacho, 1];
MakeLabelGlobal["DragonStack.GlobalFreeNacho", globalFreeNacho];
drRB[0];
Put the addr of the lock on the stack. We get to have a pop for free here.
[S] = @lock
LReg[process];
Push the desired new value for the lock field
[S] = process; [S-1] = @lock
IF useCST
THEN {
drLC0[];
Push the assumed old value of the lock field (i.e. free)
[S] = 0; [S-1] = process; [S-2] = @lock
EU stack high water mark: 2 registers (not including argument)
{retryLabel: Label = GenLabelHere[];
drCST[0];
Try for the global lock for the nacho allocator. We store our process ID into the lock if the lock was free, and return the sampled value of the lock at the start of the instruction in any case.
[S] = sample; [S-1] = 0; [S-2] = process; [S-3] = @lock
drRJNEB[left: popSrc, right: belowSrc, dist: UseLabel8B[retryLabel]];
Jump back to retry if we did not get it (predict success), and pop the sampled value. If this loop causes us to hold the bus too often, we can always insert a spin loop that does not use CST.
[S] = 0; [S-1] = process; [S-2] = @lock
drAS[256-2];
On success, flush the old & new values, since we don't need them. The lock address is left on the stack.
[S] = @lock
}
}
ELSE {
No locking is OK for the single-processor SoftCard.
drPSB[0];
};
drRSB[1];
get the global free pointer (gFree) on the stack (leaving the lock addr under it)
[S] = gFree; [S-1] = @lock
drLIB[framesToTransfer];
[S] = framesToTransfer; [S-1] = gFree; [S-2] = @lock
{
gFreeReg: RegSpec = reg1;
dataReg: RegSpec = reg2;
drRVSUB[gFreeReg, gFreeReg, topSrc];
gFree ← gFree - framesToTransfer
[S] = framesToTransfer; [S-1] = new gFree; [S-2] = @lock
FOR i: NAT IN [0..framesToTransfer) DO
drRAI[reg1: dataReg, reg2: free, disp: i];
drWRI[reg1: dataReg, reg2: gFreeReg, disp: i];
(gFree+i)^ ← (free+i)^
EU stack high water mark: 2 registers (not including argument)
ENDLOOP;
drDIS[];
[S] = gFree; [S-1] = @lock
};
drPSB[1];
write gFree back, leaving the lock addr on the stack
[S] = @lock
drLC0[]; drWSB[0];
clear out the lock, which lets other processors get theirs
stack is now at original level
drLIB[framesToTransfer-1]; drRVADD[c: topDst, a: topSrc, b: free];
[S] = free + (framesToTransfer-1)
This is the successful exit point. There is room between free and base.
ProcedureExit[1];
[S] = new free ptr
};
GenFramesInit: PROC = {
allocator: Label = HandCodingPseudos.GetGlobalLabel["Basics.AllocVector"];
rZero: RegSpec = reg0;
rLB: RegSpec = reg1; -- base of local frame table
rSB: RegSpec = reg2; -- base of shared frame table
tPtr: RegSpec = reg3; -- pointer to next slot in global table
drLC0[]; -- init rZero
drLIB[SIZE[LocalAllocationRep]/2];
drLFC[UseLabel16[allocator]]; drROR[base, topSrc, const0];
allocate the per processor frame base (LocalAllocationRep)
also put that frame into base and leave in rLB
drLIDB[framesToTransfer*2+initialFrames];
drLFC[UseLabel16[allocator]];
drWRI[rSB, rLB, 0];
allocate space for the shared frame base (GlobalAllocationRep)
also store that address into rLB.lockPtr and leave it in rSB
drWRI[rZero, rSB, 0]; drWRI[rZero, rSB, 2];
clear out the shared frame base
drRVADD[pushDst, rSB, const3]; drWRI[tPtr, rSB, 1];
init rSB.gFree and tPtr
{
Allocate the new frames from the basic system allocator. Each frame gets stuck into the shared frame base (GlobalAllocationRep) in a new slot.
loopLabel: Label = GenLabelHere[];
drLIB[20]; drLFC[UseLabel16[allocator]]; drSRIn[tPtr, 0];
Allocate and store the new frame
drRVADD[tPtr, tPtr, const1];
drLRn[tPtr]; drSUBDB[initialFrames];
drRJNEBJ[left: popSrc, right: rSB, dist: UseLabel8B[loopLabel]];
stop when tPtr gets far enough from the base
};
drLIB[17]; drRADD[free, base, popSrc]; drWRI[rZero, rLB, 17];
now make free point at the next local slot to fill
also, clear out that slot
drAS[256-4];
Finally, drop the junk from the stack
};
All: PROC = {
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
continueLabel: Label = GenLabel[];
toUserLabel: Label = GenLabel[];
underflowLabel: Label = GenLabel[];
nakedSaveFrame ← GenLabel[];
Entry point for the internal routine (aka DragonStack.NakedSaveFrame) that saves the eldest frame.
GenFramesInit[];
drLFC[UseLabel16[continueLabel]];
Order of generation is important!
GlobalAllocNacho[];
GlobalFreeNacho[];
StackUnderflowTrap[underflowLabel];
StackOverflowTrap[underflowLabel];
SetLabel[continueLabel];
Setup the bogus frame & enable traps (to detect stack overflow).
PushWordAddr[area, underflowLabel];
SetYoungestPC[];
GetYoungestStatus[];
drLIQB[LOOPHOLE[DragOpsCross.StackedStatusWord[trapsEnabled: TRUE]]];
drOR[];
drDFC[UseLabel32[HandCodingPseudos.GetGlobalLabel["Basics.SetStatus"]]];
drLFC[UseLabel16[toUserLabel]];
This is to ensure that there is a non-bogus frame at the bottom of the stack, which is more-or-less required for stack save/restore. Someday we may want to remove this restriction. RRA: April 21, 1987
Pause[];
SetLabel[toUserLabel];
drLC0[];
MakeLabelGlobal["DragonStack.ExitToUser", toUserLabel];
};
FillTrap: PROC [tx: DragOpsCross.TrapIndex, dest: Label] = {
area: HandCodingSupport.Area = HandCodingSupport.GetCurrentArea[];
oldPC: CARD = HandCodingSupport.GetOutputPC[area];
SetOutputPC[DragOpsCrossUtils.TrapIndexToBytePC[tx]];
drJDB[UseLabel16[dest]];
HandCodingSupport.SetOutputPC[oldPC];
};
Dupl2: PROC = {
Duplicates the top two words on the stack
before: [S] = X, [S-1] = Y
after: [S] = X, [S-1] = Y; [S-2] = X; [S-3] = Y
drQOR[pushA0, belowSrc];
drQOR[pushA0, belowSrc];
};
END.