GenStack:
CEDAR
PROGRAM
IMPORTS DragOpsCrossUtils, HandCoding, HandCodingSupport, HandCodingPseudos
= BEGIN OPEN DragonProcessOffsets, HandCoding, HandCodingSupport, HandCodingPseudos;
CARD: TYPE = LONG CARDINAL;
Word: TYPE = DragOpsCross.Word;
enableTrapsLit: DragOpsCross.Word = LOOPHOLE[
DragOpsCross.IFUStatusRec[trapsEnabled: TRUE]];
Size parameters (the only important difference between this file and GenBigStack.mesa)
initialFrames:
NAT = 256;
Initial frames to allocate.
localFrameArraySize:
NAT = 16;
# of frames in localArray
StackLog:
NAT = 7;
Note: StackSize = 2^StackLog
emptyFrameLimit:
NAT = 1;
The stack underflow routine is prepared to restore the youngest frame (in hook) and this many empty frames immediately elder than the youngest frame. This avoids the situation where we underflow into an "empty" frame, then execute an ALS instruction which effectively moves registers from the youngest frame to the next youngest. If we allowed that to happen we would lose those registers from the stack abstraction, which would be wrong!
RegArray: TYPE = ARRAY Reg 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 (not yet used)
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)
framesToTransfer:
NAT ← 4;
Number of frames to transfer between local and global frame pools.
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 = {
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 exactly one word, which is the saved status. The idea is to just bring in one frame from the hook, and then "return" to it, enabling traps as we go.
stackUnderflowTrap: Label = GenLabel[];
Entry point for handling stack underflow (aka DragonStack.StackUnderflowTrap).
setupLabel: Label = GenLabel[];
restoreLabel: Label = GenLabel[];
freeRoutineLabel: Label = GenLabel[];
badLabel: Label = GenLabel[];
notSingleLabel: Label = GenLabel[];
faultLabel: Label = GenLabel[];
flushLabel: Label = GenLabel[];
shortLabel: Label = GenLabel[];
framedLabel: Label = GenLabel[];
area: HandCodingSupport.Area = HandCodingSupport.GetCurrentArea[];
SetLabel[notSingleLabel];
At this point we have a case where the eldest PC # the youngest PC, and the stack has [S] = eldest PC, [S-1] = eldest L, [S-2] = status. The IFU stack is not empty.
SetEldestPC[];
SetEldestL[];
SetLabel[faultLabel];
At this point we a genuine page fault (or just maybe the bogus frame has been clobbered). [S] = status. The IFU stack is as at entry.
Pause[];
SetLabel[stackUnderflowTrap];
We enter this procedure with one argument, the saved status word. However, we don't want to set L, since that would make restoring the regs harder.
MakeLabelGlobal["DragonStack.StackUnderflowTrap", stackUnderflowTrap];
FillTrap[IFUPageFaultTrap, stackUnderflowTrap];
At this point we should determine if the IFU stack only has the bogus frame.
GetYoungestPC[];
drJNEBB[17, UseLabel8B[faultLabel]];
drLIB[19];
SetYoungestPC[];
GetEldestL[];
GetEldestPC[];
drDUP[]; -- so we don't lose it if things are bad
drJNEBB[19, UseLabel8B[notSingleLabel]];
At this point we had one frame on the stack, and it was the bogus frame. So we put on the new value for the eldest frame PC.
drRRX[topDst, hook, constLastPC]; SetEldestPC[];
Get the new eldest PC into the IFU stack
drDUP[]; SetEldestL[];
Set the L value for that frame.
drLIB[16]; LRegI[hook, constNRegs];
Push 16, then put the # of registers in [S]
([S] = #regs, [S-1] = 16)
MoveReg[temp, hook];
Put the first frame to restore in temp
MoveRegI[hook, hook, constLink];
Make the hook point to the next frame (hook ← (hook+0)^)
drRJLEBJ[left: topSrc, right: belowSrc, dist: UseLabel8B[shortLabel]];
If #regs <= 16 (the usual case), skip the code that dumps extra blocks.
{
At this point, nregs > 16, so dump the first 16.
loopLabel: Label = GenLabelHere[];
Now do 16 reads into the stack registers from the dump block
FOR i:
NAT
IN [0..15]
DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
LReg[temp]; MoveRegI[temp, temp, constOthers];
push the nacho to free; point temp at the next nacho in the chain
([S] = nextNacho; [S-1] = #regs; [S-2] = 16)
FreeNacho[];
free the nacho pointed to by [S]; S←S-1
drAL[16]; drSUBB[16];
adjust L and #regs ([S] = #regs left; [S-1] = 16)
drRJGB[left: topSrc, right: belowSrc, dist: UseLabel8B[loopLabel]];
go do another frame (if necessary), do not change S
};
SetLabel[shortLabel];
This is point where we restore frames without extensions, and also restore the remnant of frames with extensions. [S] = #regs; [S-1] = 16.
{
The best way to quickly restore a variable # of regs is to jump into a table of read instructions. We don't dare restore too much or we will clobber the potential return values.
jumpLabel: Label = GenLabel[];
drRVADD[c: belowDst, a: topSrc, b: topSrc];
([S] = #regs; [S-1] = 2*#regs)
drRVADD[c: belowDst, a: belowSrc, b: belowSrc];
([S] = #regs; [S-1] = 4*#regs)
drRVSUB[c: belowDst, a: popSrc, b: belowSrc];
([S] = -3*#regs)
IndexedJump[dest: jumpLabel];
jump into the table
FOR i:
NAT
DECREASING
IN [0..15]
DO
drRAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
SetLabel[jumpLabel];
LReg[temp]; FreeNacho[];
free the nacho pointed to by temp (no change to S)
};
{
At this point all of the registers have been restored, all of the dump blocks have been freed. [S] holds the L for the restored frame, and [S-1] holds the status word. The eldest frame is the restored frame, and there is no bogus frame.
noFrameLabel: Label = GenLabel[];
The following code restores some number of empty frames. This is an attempt to avoid the situation where we get a fault before performing the ALS instruction. Since the ALS instruction effectively moves ownership of registers from the youngest frame to the next youngest, we must be sure that there IS a next youngest frame to move the registers to!
THROUGH [0..emptyFrameLimit)
DO
HandCoding.drRJEB[left: const0, right: hook, dist: UseLabel8B[noFrameLabel]];
Jump for an empty hook (should be rare)
LRegI[hook, constNRegs];
HandCoding.drRJNEBJ[left: const0, right: popSrc, dist: UseLabel8B[noFrameLabel]];
Jump for a hook that is non-empty
drDUP[]; LRegI[hook, constLastPC];
Now the EU stack has the L and PC for the empty frame.
SetEldestPC[]; SetEldestL[];
This puts the empty frame on the IFU stack.
LReg[hook]; MoveRegI[hook, hook, constLink];
push the hook, and put the next eldest frame link in hook
FreeNacho[];
free the nacho pointed to by [S]; S←S-1
ENDLOOP;
SetLabel[noFrameLabel];
MakeLabelGlobal["DragonStack.StackUnderflowExit", noFrameLabel];
[S] has L for eldest frame on IFU stack, [S-1] has the saved status
drLIB[17]; SetEldestPC[];
Install the new bogus frame & set its PC
{
It is possible that there is no hook left, so don't blow it by dereferencing through it.
noHookLabel: Label = GenLabel[];
drDUP[];
[S] = [S-1] = L for eldest frame in IFU stack
drRJEB[left: const0, right: hook, dist: UseLabel8B[noHookLabel]];
LRegI[hook, constNRegs]; drSUB[];
SetLabel[noHookLabel];
ExtractField[first: 32-StackLog, bits: StackLog]; SetEldestL[];
Calculate & set the L for the bogus frame. [S] still holds the L for the last restored frame, [S-1] holds the status.
};
drSUBB[StackMargin]; ExtractField[first: 32-StackLog, bits: StackLog]; SetSPLimit[];
Calculate & set the new stack limit. [S] now holds only the status.
drALS[0];
We must finally set L to be the same as S so we can execute the RETK properly. [S] still holds the status.
MakeLabelGlobal["DragonStack.StackUnderflowReturn", GenLabelHere[]];
drRETK[377B];
Return from kernel, cutting back the stack appropriately.
};
};
StackOverflowTrap:
PROC = {
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.
exitLabel: Label ← GenLabel[];
{
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, 1];
We get one argument, which is the status word.
MakeLabelGlobal["DragonStack.IFUStackOverflowTrap", ifuEntryLabel];
FillTrap[IFUStackOverflowTrap, ifuEntryLabel];
GetEldestPC[]; drDIS[];
discard the bogus frame
drLFC[UseLabel16[internalSaveEldest]];
Call to save eldest frame
drJB[UseLabel8A[exitLabel]];
};
{
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, 1];
We get one argument, which is the status word.
MakeLabelGlobal["DragonStack.EUStackOverflowTrap", euEntryLabel];
FillTrap[EUStackOverflowTrap, euEntryLabel];
GetEldestPC[]; drDIS[];
discard the bogus frame
{
loopLabel: Label = GenLabelHere[];
drLFC[UseLabel16[internalSaveEldest]];
Call to save one frame
LRegI[hook, constNRegs];
drRJEB[left: popSrc, right: const0, dist: UseLabel8B[loopLabel]];
If nregs = 0, then loop to the start. We must get at least one word saved to terminate. Of course, at least one frame must have more than 0 words in order to get the EU stack overflow trap in the first place, so termination is assured.
};
};
{
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];
GetEldestL[];
Get the L for the restored frame on the stack. We use it copiously later.
drLIB[17]; SetEldestPC[];
Install the new bogus frame & set its PC
LRegI[hook, constNRegs]; drRVSUB[topDst, belowSrc, topSrc];
ExtractField[first: 32-StackLog, bits: StackLog]; SetEldestL[];
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[]];
drRETK[377B];
At this point we go back to the code that overflowed. The old status word is used to restore the user/kernel mode.
};
};
InternalSaveEldest:
PROC = {
This routine saves the eldest frame. This routine is intended for use ONLY by the various overflow and reschedule trap routines. We assume that the bogus frame has been removed by the caller.
setupLabel: Label = GenLabel[];
{
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:
[S] = newBlock; [S-1] = PC for dummy frame; hook = frame chain
after exit:
[S] = #regs; [S-1] = 16; L = L of frame to save
localHook: RegSpec = reg0;
localNRegs: RegSpec = reg1;
ProcedureEntry[setupLabel, 1];
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)
GetEldestL[]; drDUP[]; SetYoungestL[];
Get the L for the frame we wish to save, put it into localNRegs and youngest L
([S] = localNRegs = L of frame to save; [S-1] = localHook)
GetEldestPC[]; drSRIn[localHook, lastPCOffset];
Store the continuation PC and remove the eldest PC from the ifuStack.
((localHook+lastPCOffset)^ = PC of frame to save)
GetEldestL[]; drRVSUB[c: localNRegs, a: popSrc, b: localNRegs];
Calculate the number of regs in this frame
([S] = localNRegs = eldestL - localNRegs'; [S-1] = localHook)
ExtractField[first: 32-StackLog, bits: StackLog];
determine the # of regs to store, using modulo arithmetic
([S] = localNRegs = #regs = (eldestL - localNRegs') MOD StackSize)
drWRI[localNRegs, localHook, nRegsOffset];
Save #regs into the save block
(localHook+nRegsOffset)^ = localNRegs
drLIB[16]; SReg[localHook];
([S] = #regs; [S-1] = 16)
drRETN[];
return without S adjustment
};
{
DragonStack.InternalSaveEldest: This is the entry point for internally saving the eldest stack frame in the IFU stack. The bogus frame has been removed.
shortLabel: Label = GenLabel[];
SetLabel[internalSaveEldest];
MakeLabelGlobal["DragonStack.InternalSaveEldest", internalSaveEldest];
AllocNacho[];
Allocate the next save block to use
([S] = newBlock)
drLFC[UseLabel16[setupLabel]];
Init the new save block. Its address is left in hook. L gets set to the L for the frame we want to save. In addition:
([S] = #regs; [S-1] = 16)
drRJLEBJ[left: topSrc, right: belowSrc, dist: UseLabel8B[shortLabel]];
If #regs <= 16 (the usual case), skip the code that dumps extra blocks.
{loopLabel: Label = GenLabelHere[];
At this point, there nregs > 16, so dump the first 16. The save block to use has already been allocated, and its address is in temp.
FOR i:
NAT
IN [0..15]
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] = 16)
AllocNacho[];
Allocate the next save block to use (it gets left on the stack)
([S] = newBlock; [S-1] = temp = oldBlock; [S-2] = #regs; [S-3] = 16)
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; [S-1] = 16)
drAL[16]; drSUBB[16];
adjust L and #regs
([S] = #regs; [S-1] = 16)
drRJGB[left: topSrc, right: belowSrc, dist: UseLabel8B[loopLabel]];
go do another frame (if necessary)
};
SetLabel[shortLabel];
(#regs IN [0..16]; [S] = #regs; [S-1] = 16)
{
The best way to quickly restore a variable # of regs is to jump into a table of read instructions. This takes roughly 6 cycles to do the jump, and we stand to save at least one cycle for every store we save (not counting avoiding extra dirty cache entries).
jumpLabel: Label = GenLabel[];
drRVADD[c: belowDst, a: topSrc, b: topSrc];
([S] = #regs; [S-1] = 2*#regs)
drRVADD[c: belowDst, a: belowSrc, b: belowSrc];
([S] = #regs; [S-1] = 4*#regs)
drRVSUB[c: belowDst, a: popSrc, b: belowSrc];
([S] = -3*#regs)
IndexedJump[jumpLabel];
jump into the table
FOR i:
NAT
DECREASING
IN [0..15]
DO
Generate 16 stores of locals to the save block
drWAI[reg1: [reg[i]], reg2: temp, disp: regOff+i];
ENDLOOP;
SetLabel[jumpLabel];
};
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!
};
};
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.
drRJNEBJ[left: topSrc, right: const0, 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
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
drLC0[]; drPSB[othersOffset];
always clears the others field on allocation
};
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] = 0, at exit S←S-1, 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: topDst, a: base, b: const0];
Replace top of stack (was 0) with the addr of the lock.
([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)
};
SubReg[free, const4];
Adjust the free pointer to accomodate the next transfers.
drRSB[1];
Get the global free pointer (gFree) on the stack
(@base.gFree = @base.lock + 1)
([S] = gFree; [S-1] = @lock)
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[4];
gFree ← gFree - 4; leave gFree on stack
([S] = new gFree; [S-1] = @lock)
FOR i:
NAT
IN [0..4)
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)
drRVADD[c: pushDst, a: free, b: const3];
([S] = free + (4-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[4+4+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[];
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[];
InternalSaveEldest[];
StackUnderflowTrap[];
StackOverflowTrap[];
SetLabel[continueLabel];
Setup the bogus frame & enable traps (to detect stack overflow).
drLIB[17];
SetYoungestPC[];
MakeLabelGlobal["DragonStack.ExitToUser", GenLabelHere[]];
};
FillTrap:
PROC [tx: DragOpsCross.TrapIndex, dest: Label] = {
area: HandCodingSupport.Area = HandCodingSupport.GetCurrentArea[];
oldPC: INT = HandCodingSupport.GetOutputPC[area];
SetOutputPC[DragOpsCrossUtils.TrapIndexToBytePC[tx]];
drJDB[UseLabel16[dest]];
HandCodingSupport.SetOutputPC[oldPC];
};
END.