// IfsContextInit.bcpl
//	Code can be wiped out after initialization
// Copyright Xerox Corporation 1979, 1980

// Last modified by Taft, February 28, 1980  12:22 PM
// Last modified by Butterfield, September 7, 1979  3:35 PM
// - original ContextInit.bcpl came from 11/4/77 ContextSources.dm

external
[
// outgoing procedures
InitializeContext

// incoming procedures
SysErr
]

// This declaration matches one in Context.Asm
structure CTX:
[
Next word	// Pointer to a list of such CTX's
Stack word	// Current stack pointer
StackMin word	// Lowest stack can go.
]		// After here, extra for user.

manifest ecCtxStackTooSmall = 2700
manifest stackBottom = -1;


//-----------------------------------------------------------------
let InitializeContext(region, length, proc, extraSpace;
    numargs na) = valof
//-----------------------------------------------------------------
[
region>>CTX.Next=0		// Not on a list yet
if na ls 4 then extraSpace=0
region>>CTX.StackMin=region+(size CTX/16)+extraSpace
let s=region+length-4		// Spot to start stack.
s!0 = 0				// So you can tell where it starts
s!1=proc			// Address at which to begin.
region>>CTX.Stack=s		// Initial stack value
if lv s!stackBottom - region>>CTX.StackMin ls 0 then
 SysErr(nil, ecCtxStackTooSmall);
s!stackBottom = lv s!stackBottom;
resultis region
]