GenBigStack.mesa
Copyright © 1985, 1986 by Xerox Corporation. All rights reserved.
Russ Atkinson (RRA) September 19, 1986 10:07:30 am 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;
Size parameters (the only important difference between this 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)
StackLog: NAT = 7;
StackSize: NAT = 128;
Note: StackSize = 2^StackLog
statusMask: StackedStatusWord = [userMode: TRUE, trapsEnabled: TRUE, lBase: 377B];
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.
internalSaveEldest: Label ← NIL;
Entry point for the internal routine (aka DragonStack.InternalSaveEldest) 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 called when we return to the bogus frame, which has an invalid PC. At entry, maskable traps are disabled, and the IFU stack has exactly 1 frame (if the world is valid). The top of stack has no words that we can depend on. The idea is to just bring in one frame from the hook, and then "return" to it, enabling traps as we go.
{
At the underflow point we have no frames on the IFU stack, provided that we are in kernel mode. If not in kernel mode, then we will soon trap. 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.
underflowEntry1: Label = GenLabel[];
underflowEntry2: Label = GenLabel[];
underflowEntry3: Label = GenLabel[];
WordAlign[];
drJ1[];
Make the LFC take up the remainder of the bytes, so the following instruction does not take straddle a word. This is believed to help the IFU.
SetLabel[stackUnderflowTrap];
MakeLabelGlobal["DragonStack.StackUnderflowTrap", stackUnderflowTrap];
This is the bogus frame PC when the bogus frame is created using the stack overflow code. The saved status will have traps disabled and be in kernel mode, and the saved L value will be the current L.
drLFC[UseLabel16[underflowEntry1]];
This call sets L and the temp register to the new L.
[S] = new L
drSUBB[StackMargin];
ExtractField[first: 32-StackLog, bits: StackLog];
SetSPLimit[];
Calculate & set the new stack limit. S is back to the point it had at entry.
drLFC[UseLabel16[underflowEntry2]];
This call creates a frame with return PC pointing at the alternate stack underflow entry point.
drJB[UseLabel8A[stackUnderflowTrap]];
This is the bogus frame PC when the bogus frame is created using the stack underflow code. It does the same thing (obviously) as the other entry point.
WordAlign[];
SetLabel[underflowEntry1];
At this point we have one frame on the IFU stack, which will become the bogus frame, but its saved value for L is not yet correct. So we fix it here, which sets L on return. This code also sets the temp reg to that new value of L, and pushes it onto the stack. We assume that the hook register points at a valid nacho!
LRegI[hook, constNRegs];
GetYoungestStatus[];
drRVSUB[belowDst, popSrc, belowSrc];
[S] holds the new value for L, but it needs masking.
ExtractField[first: 32-StackLog, bits: StackLog]; PReg[temp];
Both temp and [S] hold the new value for L, properly masked.
drDUP[];
SetYoungestStatus[];
Set the new youngest L, which will set L on return.
drRETN[];
WordAlign[];
SetLabel[underflowEntry2];
At this point we have created the bogus frame with a good values for the PC and status words. Also, L and the temp register have good values, and S is as it was at stack entry.
LRegI[hook, constLink];
Note: this assumes that hook # NIL!
[S] = next hook
drLFC[UseLabel16[underflowEntry3]];
This call creates a frame that will be used for a genuine restored frame, although both PC and status bits will need to be set.
[S] = next hook
WordAlign[];
SetLabel[underflowEntry3];
At this point we have two frames on the IFU stack, and the elder one (the bogus frame) is completely correct.
};
{
At this point we have two frames on the IFU stack. The elder (which is the bogus frame) is correct, but the younger one has nothing of interest (actually, the L part is correct, but we can't use that fact). The frame we want to restore may have an immediately elder frame (in the saved chain) that has zero words in it. If so, we have to restore that one first. Also, we have pushed the next hook:
[S] = next hook
noHookLabel: Label = GenLabel[];
underflowNoSplice: Label = GenLabel[];
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
dispatchPC: CARD = HandCodingSupport.ReserveData[StackSize];
dispatchWord: Word =
DragOpsCrossUtils.CardToWord[dispatchPC / DragOpsCross.bytesPerWord];
drRJEB[topSrc, const0, UseLabel8B[noHookLabel]];
Just in case there is no next hook!
drRSB[nRegsOffset];
drDUP[];
ExtractField[first: 32-StackLog, bits: StackLog];
drRJNEBJ[popSrc, const0, UseLabel8B[underflowNoSplice]];
At this point we have a zero-word frame to splice in. This involves setting the PC and status properly, then returning the nacho, then creating a new youngest frame.
[S] = #regs (+ status bits), but #regs = 0!
[S-1] = frame to splice
temp = current L (also L for the bogus frame, as well as for the restored frame)
drRRX[topDst, belowSrc, constNRegs];
[S] = #regs (+ status bits) for the frame to splice
[S-1] = frame to splice
LReg[temp];
push the new L
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert: TRUE, mask: StackLog]]];
insert the new L into the status word
SetYoungestStatus[];
[S] = frame to splice, youngest status is now correct
drDUP[];
drRRX[belowDst, topSrc, constLink];
Fetch the next elder frame to the spliced frame
[S] = frame to splice, [S-1] = next elder frame
drRSB[lastPCOffset];
SetYoungestPC[];
Set the new frame's PC
[S] = frame to splice, [S-1] = next elder frame
FreeNacho[];
free the frame we just spliced in
[S] = next elder frame (next hook)
LRegI[hook, constNRegs];
[S] = #regs (+ status bits) for the hook
[S-1] = next hook
drLFC[UseLabel16[underflowNoSplice]];
We will never return here!
SetLabel[noHookLabel];
drLC0[];
Show that we have no registers!
WordAlign[];
SetLabel[underflowNoSplice];
At this point we have a frame to splice in.
[S] = #regs (+ status bits) for the hook
[S-1] = next hook (may be 0)
temp holds the bogus frame L.
LReg[temp];
push the new L
drSHDR[DragOpsCrossUtils.FieldDescriptorToCard[[insert: TRUE, mask: StackLog]]];
insert the new L into the status word
SetYoungestStatus[];
set the youngest frame status word
[S] = next value for hook, youngest status is now correct
LRegI[hook, constLastPC];
SetYoungestPC[];
[S] = next value for hook, youngest status & PC is now correct
LRegI[hook, constNRegs]; ExtractField[first: 32-StackLog, bits: StackLog];
Push regsPerNacho, then push the # of registers to restore
[S] = #regs, [S-1] = next hook
drLIQB[dispatchWord];
drQRX[topAtop, belowSrc];
drJSD[];
Dispatch to save the proper # of regs
[S] = # of regs to save
[S-1] = next hook
SetLabel[singleLabel];
At this point we execute a varying number of RAIs to restore the regs
[S] = # of regs to restore
[S-1] = next hook
FOR i: NAT DECREASING IN [0..regsPerNacho) DO
drRAI[reg1: [reg[i]], reg2: hook, disp: regOff+i];
ENDLOOP;
MoveReg[topDst, hook]; FreeNacho[];
free the nacho pointed to by hook (& flush the # regs)
[S] = next hook
MakeLabelGlobal["DragonStack.StackUnderflowReturn", GenLabelHere[]];
SReg[hook];
Now S is at its initial value, hook is properly set, and the IFU stack is correct for the coming return.
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 dump block
FOR i: NAT IN [0..regsPerNacho) DO
drRAI[reg1: [reg[i]], reg2: hook, disp: regOff+i];
ENDLOOP;
LReg[hook]; MoveRegI[hook, hook, 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
drAL[regsPerNacho]; drSUBB[regsPerNacho];
adjust L and #regs
[S] = #regs left
drLIQB[dispatchWord];
drQRX[topAtop, belowSrc];
drJSD[];
Dispatch to save the proper # of regs
[S] = # of regs to save
[S-1] = next hook
{
Generate the dispatch table
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
HandCodingSupport.SetOutputPC[dispatchPC];
FOR nr: NAT DECREASING IN [0..regsPerNacho] DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[singleLabel.offset+3*nr]];
ENDLOOP;
FOR nr: NAT IN (regsPerNacho..regsPerNacho*4] DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[multiLabel.offset]];
ENDLOOP;
FOR nr: NAT IN (4*regsPerNacho..StackSize) DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[fatalLabel.offset]];
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.
internalSaveSetup: Label ← GenLabel[];
exitLabel: Label ← GenLabel[];
dispatchLabel: Label = GenLabel[];
dispatchPC: CARD = HandCodingSupport.ReserveData[StackSize];
dispatchWord: Word =
DragOpsCrossUtils.CardToWord[dispatchPC / 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[]; drDIS[]; GetEldestStatus[];
discard the bogus frame
[S] holds the 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
SetLabel[dispatchLabel];
We get here to do the dispatch to the appropriate register saving code.
drLIQB[dispatchWord];
drQRX[topAtop, belowSrc];
drSFC[];
Dispatch to restore the proper # of regs
On return: [S] = eldest status
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
drLIQB[UseLabel32[underflowLabel]];
SetEldestPC[];
Install the new bogus frame & set its PC (creating a new eldest frame)
ExtractField[first: 32-StackLog, bits: StackLog]; drDUP[];
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.
MakeLabelGlobal["DragonStack.StackOverflowReturn", GenLabelHere[]];
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[]; drDIS[]; GetEldestStatus[];
discard the bogus frame
[S] holds the eldest status
{
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, const0, UseLabel8B[loopLabel]];
Discard [S] and go loop (unconditionally, really).
[S] = eldest status
};
};
{
InstallBogusFrame is useful for external callers of DragonStack.InternalSaveEldest. Once the necessary frames have been saved, the bogus frame can be created using this routine.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0, TRUE];
GetEldestStatus[];
push the eldest status
MakeLabelGlobal["DragonStack.InstallBogusFrame", exitEntryLabel];
drJB[UseLabel8A[exitLabel]];
};
{
RemoveBogusFrame is useful for external callers of DragonStack.InternalSaveEldest. Before InternalSaveEldest can be called, the bogus frame must be removed, and the current eldest status must be pushed.
exitEntryLabel: Label = GenLabel[];
ProcedureEntry[exitEntryLabel, 0];
MakeLabelGlobal["DragonStack.RemoveBogusFrame", exitEntryLabel];
GetEldestPC[]; drDIS[]; GetEldestStatus[];
flush the eldest PC (removes the eldest frame)
push the new eldest status (as needed by InternalSaveEldest)
ProcedureExit[1];
return the new eldest status
};
{
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
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.InternalSaveEldest: This is the entry point for internally saving the eldest stack frame in the IFU stack. The bogus frame has been removed. [S] holds the sampled eldest status.
singleLabel: Label = GenLabel[];
multiLabel: Label = GenLabel[];
fatalLabel: Label = GenLabel[];
shortLabel: Label = GenLabel[];
SetLabel[internalSaveEldest];
MakeLabelGlobal["DragonStack.InternalSaveEldest", internalSaveEldest];
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. L gets set to the L for the frame we want to save.
[S] = #regs, [S-1] = eldest status
drLIQB[dispatchWord];
drQRX[topAtop, belowSrc];
drJSD[];
Dispatch to restore the proper # of regs
after JSD: [S] = #regs, [S-1] = eldest status
SetLabel[singleLabel];
FOR i: NAT DECREASING IN [0..regsPerNacho) DO
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
drDIS[];
Put the stack back to the original level
[S] = eldest status
drRETN[];
This return does not enable traps, and does not disturb S, but does restore L, which was previously clobbered. S must have the same value that it did at entry!
[S] = eldest status
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;
LReg[temp];
Push the address of the completed save block
([S] = temp = oldBlock; [S-1] = #regs; [S-2] = eldest status)
AllocNacho[];
Allocate the next save block to use (it gets left on the stack)
([S] = newBlock; [S-1] = temp = oldBlock; [S-2] = #regs)
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)
drAL[regsPerNacho]; drSUBB[regsPerNacho];
adjust L and #regs
[S] = #regs, [S-1] = eldest status
drLIQB[dispatchWord];
drQRX[topAtop, belowSrc];
drJSD[];
Dispatch to restore the proper # of regs
[S] = #regs, [S-1] = eldest status
{
Generate the dispatch table
area: HandCodingSupport.Area ← HandCodingSupport.GetCurrentArea[];
oldPC: CARD ← HandCodingSupport.GetOutputPC[];
HandCodingSupport.SetOutputPC[dispatchPC];
FOR nr: NAT DECREASING IN [0..regsPerNacho] DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[singleLabel.offset+3*nr]];
ENDLOOP;
FOR nr: NAT IN (regsPerNacho..regsPerNacho*4] DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[multiLabel.offset]];
ENDLOOP;
FOR nr: NAT IN (4*regsPerNacho..StackSize) DO
HandCodingSupport.OutputWord[area,
DragOpsCrossUtils.IntToWord[fatalLabel.offset]];
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.
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)
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)
};
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]; 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.
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.
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 = {
Free up a nacho (in [S-1]) to the central pool. We have already tested for the fast case, and we call this when [S] = free-1; [S-1] = nacho to free. At exit, [S] = new free, [S-1] = nacho to free.
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.
globalFreeNacho ← GenLabelHere[];
MakeLabelGlobal["DragonStack.GlobalFreeNacho", globalFreeNacho];
drRRX[c: topDst, a: base, b: const0];
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)
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. The lock address is left on the stack.
([S] = @lock)
};
drRSB[1];
get the global free pointer (gFree) on the stack (leaving the lock addr under it)
([S] = gFree; [S-1] = @lock)
drSUBB[framesToTransfer];
gFree ← gFree - framesToTransfer; leave gFree on stack
([S] = new gFree; [S-1] = @lock)
FOR i: NAT IN [0..framesToTransfer) DO
LReg[free]; drRB[i]; drPSB[i];
(gFree+i)^ ← (free+i)^
ENDLOOP;
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.
drRETN[];
};
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 = {
continueLabel: Label = GenLabel[];
underflowLabel: Label = GenLabel[];
internalSaveEldest ← GenLabel[];
Entry point for the internal routine (aka DragonStack.InternalSaveEldest) 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).
drLIQB[UseLabel32[underflowLabel]];
SetYoungestPC[];
GetYoungestStatus[];
drLIQB[LOOPHOLE[DragOpsCross.StackedStatusWord[trapsEnabled: TRUE]]];
drOR[];
drDFC[UseLabel32[HandCodingPseudos.GetGlobalLabel["Basics.SetStatus"]]];
MakeLabelGlobal["DragonStack.ExitToUser", GenLabelHere[]];
};
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];
};
END.