-- VarMove.mesa  Edited by Sweet, January 21, 1980  2:24 PM

DIRECTORY
  AltoDefs: FROM "altodefs" USING [BYTE, wordlength],
  Code: FROM "code" USING [CodeNotImplemented, curctxlvl, dStar, stking],
  CodeDefs: FROM "codedefs",
  ControlDefs: FROM "controldefs" USING [FieldDescriptor, localbase],
  FOpCodes: FROM "fopcodes" USING [qADD, qAMUL, qDESCB, qGADRB,
    qLADRB, qLCO, qLG, qLGD, qLI, qLL, qLLD, qLLK, qMUL, qPUSH,
    qR, qRD, qRDL, qRF, qRFL, qRFS, qRFSL, qRIG, qRIGL, qRIL,
    qRILL, qRL, qRSTR, qRSTRL, qRXGL, qRXL, qRXLL, qSG, qSGD,
    qSL, qSLD, qW, qWD, qWDL, qWF, qWFL, qWFS, qWFSL, qWIG,
    qWIGL, qWIL, qWILL, qWL, qWSTR, qWSTRL, qWXGL, qWXL, qWXLL],
  InlineDefs: FROM "inlinedefs" USING [BITAND, BITSHIFT, LongNumber],
  OpCodeParams: FROM "opcodeparams" USING [GlobalHB, HB, LocalHB],
  P5L: FROM "p5l",
  P5U: FROM "p5u" USING [Out0, Out1, Out2],
  Stack: FROM "stack" USING [Above, Also, Dump, Exchange, Forget, Load, Loc,
    Pop, TempStore, Top, VDepthOf],
  Symbols: FROM "symbols" USING [ContextLevel, ctxType, lG, lZ, seType],
  Table: FROM "table" USING [Base, Notifier],
  Tree: FROM "tree" USING [treeType];

VarMove: PROGRAM
  IMPORTS LCPtr: Code, InlineDefs, P5U, P5L, Stack
  EXPORTS P5L, CodeDefs =
  BEGIN OPEN FOpCodes, CodeDefs, Symbols;

  CPtr: POINTER TO FRAME [Code] = LCPtr;

  WontHappen: SIGNAL = CODE;

  wordlength: CARDINAL = AltoDefs.wordlength;
  BYTE: TYPE = AltoDefs.BYTE;

  cb, seb, ctxb: Table.Base;

  VarMoveNotify: PUBLIC Table.Notifier =
    BEGIN  -- called by allocator whenever table area is repacked
    seb ← base[Symbols.seType];
    ctxb ← base[Symbols.ctxType];
    cb ← base[Tree.treeType];
    RETURN
    END;
    
  LoadComponent: PUBLIC PROCEDURE [var: VarComponent] =
    BEGIN
    Mask: ARRAY [0..15] OF CARDINAL =
      [0B, 1B, 3B, 7B, 17B, 37B, 77B, 177B, 377B, 777B,
      1777B, 3777B, 7777B, 17777B, 37777B, 77777B];
    wS: CARDINAL = P5L.Words[var.wSize, var.bSize];
    WITH vv: var SELECT FROM
      faddr =>
	BEGIN
	w: CARDINAL ← vv.wd;
	lvl: ContextLevel = vv.level;
	
	SELECT lvl FROM
	  lZ => ERROR;
	  lG => P5U.Out1[qGADRB, w];
	  CPtr.curctxlvl => P5U.Out1[qLADRB, w];
	  ENDCASE =>
	    BEGIN
	    P5L.LoadComponent[P5L.BaseComponent[lvl]];
	    w ← w - ControlDefs.localbase;
	    IF w # 0 THEN P5L.GenAdd[w];
	    END;
	END;
      frame =>
	BEGIN
	w: CARDINAL ← vv.wd; 
	b: CARDINAL = vv.bd;
	lvl: ContextLevel = vv.level;
	delta: CARDINAL ← 0;
	
	IF b # 0 THEN
	  BEGIN
	  IF b + var.bSize = wordlength THEN
	    BEGIN
	    P5L.LoadComponent[[wSize: 1, space: frame[wd: w, level: lvl]]];
	    P5L.GenAnd[Mask[var.bSize]];
	    var.bSize ← 0;
	    END
	  ELSE
	    BEGIN
	    r: VarIndex;
	    r ← P5L.GenVarItem[bo];
	    IF var.wSize # 0 THEN ERROR;
	    cb[r] ← [body: bo[
	      base: P5L.AddrComponent[var],
	      offset: [bSize: var.bSize, space: frame[bd: b]]]];
	    MoveBo[r, load];
	    RETURN
	    END;
	  delta ← 1;
	  END;
	BEGIN -- to declare LFOp
	LFOp: ARRAY BOOLEAN OF ARRAY [1..2] OF BYTE =
	  [[qLL, qLLD], [qLG, qLGD]];
	IF var.wSize # 0 THEN SELECT lvl FROM
	  lZ => ERROR;
	  CPtr.curctxlvl, lG => 
	    BEGIN
	    IF var.wSize IN [1..2] THEN
	      P5U.Out1[LFOp[lvl=lG][var.wSize], w+delta]
	    ELSE
	      BEGIN
	      i: CARDINAL;
	      FOR i IN [0..var.wSize) DO
		P5U.Out1[LFOp[lvl=lG][1], w+delta+i];
		ENDLOOP;
	      END;
	    IF vv.immutable AND CPtr.stking THEN
	      Stack.Also[var.wSize, FALSE, w+delta, lvl];
	    END;
	  ENDCASE =>
	    BEGIN
	    r: VarIndex = P5L.GenVarItem[bo];
	    cb[r] ← [body: bo[
	      base: P5L.BaseComponent[lvl],
	      offset: [wSize: var.wSize, space:
		frame[wd: w+delta - ControlDefs.localbase]]]];
	    MoveBo[r, load];
	    END;
	END; -- scope of LFOp
	IF var.bSize # 0 THEN
	  BEGIN -- delta = 0 in this case
	  P5L.LoadComponent[[wSize: 1, space:
	    frame[wd: w+var.wSize, level: lvl]]];
	  P5L.GenShift[var.bSize - wordlength];
	  END;
	END;
      frameup =>
	BEGIN
	r: VarIndex = P5L.GenVarItem[bo];
	cb[r] ← [body: bo[
	  base: [wSize: vv.pwSize, space: frame[wd: vv.wd, level: vv.level]],
	  offset: [wSize: var.wSize, space: frame[wd: vv.delta]]]];
	MoveBo[r, load];
	END;
      linkup =>
	BEGIN
	P5U.Out1[qLLK, vv.wd];
	P5U.Out1[IF var.wSize = 2 THEN qRD ELSE qR, vv.delta];
	END;
      caddr => P5U.Out1[qLCO, vv.wd];
      code =>
	BEGIN
	w: CARDINAL ← vv.wd; 
	b: CARDINAL = vv.bd;
	i: CARDINAL;
	delta: CARDINAL ← 0;
	IF b # 0 THEN
	  BEGIN
	  IF var.wSize # 0 AND var.bSize + b # wordlength THEN ERROR;
	  P5U.Out1[qLCO, w];
	  P5L.GenRFC[0, b, var.bSize];
	  var.bSize ← 0; delta ← 1;
	  END;
	FOR i IN [0..var.wSize) DO
	  P5U.Out1[qLCO, w];
	  P5L.GenRFC[i+delta, 0, wordlength];
	  ENDLOOP;
	IF var.bSize # 0 THEN
	  BEGIN
	  P5U.Out1[qLCO, w];
	  P5L.GenRFC[var.wSize, 0, var.bSize];
	  END;
	END;
      link => P5U.Out1[qLLK, vv.wd];
      stack =>
	BEGIN
	w: CARDINAL = vv.wd;
	b: [0..wordlength) = vv.bd;
	IF w # 0 THEN
	  BEGIN
	  IF Stack.VDepthOf[vv.sti] # wS+w-1 THEN
	    BEGIN
	    nsti: StackIndex;
	    SIGNAL WontHappen;
	    Stack.Dump[];
	    nsti ← Stack.Above[vv.sti, w];
	    Stack.Forget[vv.sti, w];
	    vv.sti ← nsti;
	    GO TO notOnTop;
	    END;
	  IF wS > 1 THEN
	    BEGIN
	    fvar: VarComponent ← Stack.TempStore[wS];
	    THROUGH [0..w) DO Stack.Pop[]; ENDLOOP;
	    WITH vv: fvar SELECT FROM
	      frame =>
		BEGIN
		vv.bd ← b;
		IF var.bSize # 0 THEN
		  BEGIN
		  fvar.wSize ← fvar.wSize-1;
		  fvar.bSize ← var.bSize;
		  END;
		END;
	      ENDCASE;
	    P5L.LoadComponent[fvar];
	    RETURN;
	    END
	  ELSE THROUGH [0..w) DO
	    Stack.Exchange[];
	    Stack.Pop[];
	    ENDLOOP;
	  vv.sti ← Stack.Top[1];
	  EXITS
	    notOnTop => NULL;
	  END;
	IF b # 0 THEN
	  BEGIN
	  rest: StackIndex;
	  IF var.wSize # 0 THEN
	    BEGIN
	    rest ← Stack.Above[vv.sti, 1];
	    IF b+var.bSize # wordlength THEN ERROR;
	    END;
	  Stack.Load[vv.sti, 1];
	  SELECT b + var.bSize FROM
	    > wordlength => ERROR;
	    = wordlength => NULL;
	    ENDCASE => P5L.GenShift[b + var.bSize - wordlength];
	  P5L.GenAnd[Mask[var.bSize]];
	  var.bSize ← 0;
	  IF var.wSize # 0 THEN Stack.Load[rest, var.wSize];
	  END
	ELSE Stack.Load[vv.sti, wS];
	IF var.bSize # 0 THEN P5L.GenShift[var.bSize - wordlength];
	END;
      const => 
	BEGIN
	v1: CARDINAL ← vv.d1;
	b: [0..wordlength) = vv.bd;
	nw: CARDINAL ← P5L.Words[var.wSize, var.bSize];
	
	IF b # 0 THEN
	  BEGIN -- Field taking machinery takes care of less than word case
	  IF b + var.bSize # wordlength THEN ERROR;
	  v1 ← InlineDefs.BITAND[v1, Mask[var.bSize]];
	  var.bSize ← 0;
	  END;
	P5U.Out1[qLI, v1];
	IF nw = 2 THEN 
	  IF var.bSize # 0 THEN
	    P5U.Out1[qLI, InlineDefs.BITSHIFT[vv.d2, var.bSize - wordlength]]
	  ELSE P5U.Out1[qLI, vv.d2];
	END;
      pdesc => P5U.Out1[qDESCB, LOOPHOLE[vv.desc]];
      ENDCASE => ERROR;
    END;
    
  LoadVar: PUBLIC PROCEDURE [r: VarIndex] =
    BEGIN
    WITH cb[r] SELECT FROM
      o => 
	BEGIN
	P5L.LoadComponent[var];
	P5L.ReleaseVarItem[r];
	END;
      bo => MoveBo[r, load];
      bdo => MoveBdo[r, load];
      ind => MoveInd[r, load];
      ENDCASE => ERROR;
    END;
    
  MoveBo: PROCEDURE [r: VarIndex, dir: MoveDirection] =
    BEGIN
    base, offset: VarComponent;
    foffset: POINTER TO frame VarComponent;
    psize: CARDINAL;
    delta: CARDINAL ← 0;
    RWFOp: ARRAY MoveDirection OF ARRAY [1..2] OF BYTE =
      [[qRF, qRFL], [qWF, qWFL]];
    
    MoveInitialFragment: PROCEDURE =
      BEGIN
      SELECT foffset.bd+offset.bSize FROM
	< wordlength => IF offset.wSize # 0 THEN ERROR;
	> wordlength => ERROR;
	ENDCASE;
      P5L.LoadComponent[base];
      P5U.Out2[
	RWFOp[dir][psize],
	foffset.wd,
	LOOPHOLE[ControlDefs.FieldDescriptor[
	  offset: 0,
	  posn: foffset.bd,
	  size: offset.bSize]]];
      offset.bSize ← 0;
      END;
    
    MoveFinalFragment: PROCEDURE =
      BEGIN
      P5L.LoadComponent[base];
      P5U.Out2[
	RWFOp[dir][psize],
	foffset.wd,
	LOOPHOLE[ControlDefs.FieldDescriptor[
	  offset: 0,
	  posn: foffset.bd,
	  size: offset.bSize]]];
      END;
    
    WITH cc: cb[r] SELECT FROM
      bo =>
	BEGIN
	base ← cc.base;
	offset ← cc.offset;
	END;
      ENDCASE => ERROR;
    P5L.ReleaseVarItem[r];
    
    WITH oo: offset SELECT FROM
      frame => foffset ← @oo;
      code =>
	BEGIN
	i: CARDINAL;
	IF dir = store THEN ERROR;
	IF oo.bd # 0 THEN
	  BEGIN
	  IF offset.wSize # 0 THEN 
	    IF oo.bd + offset.bSize # wordlength THEN ERROR
	    ELSE base ← P5L.EasilyLoadable[base, dir];
	  P5L.LoadComponent[base];
	  P5L.GenRFC[oo.wd, oo.bd, offset.bSize];
	  offset.bSize ← 0;
	  END;
	IF offset.wSize > 1 OR offset.bSize # 0 THEN
	  base ← P5L.EasilyLoadable[base, dir];
	FOR i IN [0..offset.wSize) DO
	  P5L.LoadComponent[base];
	  P5L.GenRFC[oo.wd+i, 0, wordlength];
	  ENDLOOP;
	IF offset.bSize # 0 THEN
	  BEGIN
	  P5L.LoadComponent[base];
	  P5L.GenRFC[oo.wd+offset.wSize, 0, offset.bSize];
	  END;
	RETURN;
	END;
      ENDCASE => ERROR;
    
    psize ← P5L.Words[base.wSize, base.bSize];
    IF psize ~IN [1..2] THEN ERROR;
    IF foffset.level # lZ THEN
      BEGIN -- a field descriptor
      RWFSOp: ARRAY MoveDirection OF ARRAY [1..2] OF BYTE =
	[[qRFS, qRFSL], [qWFS, qWFSL]];
      SIGNAL CPtr.CodeNotImplemented;
      -- the following is only half baked, when field variables
      --  happen, need to work through in detail
      P5L.LoadComponent[base];
      P5L.LoadComponent[offset];
      P5U.Out0[ RWFSOp[dir][psize]];
      RETURN
      END;
    WITH bb: base SELECT FROM
      frame =>
	BEGIN
	RWilOp: ARRAY MoveDirection OF
	  ARRAY [1..2] OF ARRAY BOOLEAN OF BYTE =
	    [[[qRIL, qRIG], [qRILL, qRIGL]],
	    [[qWIL, qWIG], [qWILL, qWIGL]]];
    
	IF offset.wSize # 1 OR foffset.wd ~IN OpCodeParams.HB
	  OR foffset.bd # 0 OR offset.bSize # 0
	  OR base.bSize # 0 THEN GO TO notMagic;
	SELECT bb.level FROM
	  lG => IF bb.wd ~IN OpCodeParams.GlobalHB THEN
	    GO TO notMagic;
	  CPtr.curctxlvl => IF bb.wd ~IN OpCodeParams.LocalHB THEN
	    GO TO notMagic;
	  ENDCASE => GO TO notMagic;
	P5U.Out2[
	  RWilOp[dir][psize][bb.level = lG],
	  bb.wd,
	  foffset.wd];
	RETURN;
	EXITS
	  notMagic => NULL;
	END;
      faddr =>
	BEGIN
	SELECT bb.level FROM
	  lG, CPtr.curctxlvl =>
	    IF offset.wSize IN [1..2] AND offset.bSize = 0 
	      AND foffset.bd = 0 THEN
		BEGIN
		LSFOp: ARRAY MoveDirection OF 
		  ARRAY [1..2] OF ARRAY BOOLEAN OF BYTE =
		    [[[qLL, qLG], [qLLD, qLGD]],
		    [[qSL, qSG], [qSLD, qSGD]]];
		P5U.Out1[
		  LSFOp[dir][offset.wSize][bb.level = lG],
		  bb.wd + foffset.wd];
		RETURN
		END;
	  ENDCASE => 
	    BEGIN
	    tr: VarIndex = P5L.GenVarItem[bo];
	    cb[tr] ← [body: bo[base: P5L.BaseComponent[bb.level],
	      offset: [wSize: offset.wSize, bSize: offset.bSize,
		space: frame[
		  wd: bb.wd - ControlDefs.localbase + foffset.wd,
		  bd: foffset.bd]]]];
	    MoveBo[tr, dir];
	    RETURN
	    END;
	END;
      stack => IF base.bSize = 0 AND base.wSize IN [1..2] THEN
	BEGIN
	loc: StackLocRec ← Stack.Loc[bb.sti, base.wSize];
	WITH loc SELECT FROM
	  inTemp =>
	    BEGIN -- let recursive call check other criteria
	    tr: VarIndex ← P5L.GenVarItem[bo];
	    cb[tr] ← [body: bo[base: P5L.EasilyLoadable[base, dir],
	      offset: offset]];
	    MoveBo[tr, dir];
	    RETURN;
	    END;
	  ENDCASE;
	END;
      ENDCASE;
    IF foffset.bd # 0 OR offset.bSize # 0 THEN
      BEGIN
      IF offset.wSize # 0 THEN base ← P5L.EasilyLoadable[base, dir];
      IF foffset.bd # 0 THEN
	BEGIN
	delta ← 1;
	IF dir = load THEN MoveInitialFragment[];
	END
      ELSE IF dir = store THEN MoveFinalFragment[];
      END;
    
    SELECT offset.wSize FROM
      0 => NULL;
      > MaxParmsInStack => ERROR;
      1, 2 => 
	BEGIN
	RWOp: ARRAY MoveDirection OF
	  ARRAY [1..2] OF ARRAY [1..2] OF BYTE =
	    [[[qR, qRL], [qRD, qRDL]],
	    [[qW, qWL], [qWD, qWDL]]];
	P5L.LoadComponent[base];
	P5U.Out1[
	  RWOp[dir][offset.wSize][psize],
	  foffset.wd];
	END;
      ENDCASE =>
	BEGIN
	n, wo, s: CARDINAL;
	tr: VarIndex;
	base ← P5L.EasilyLoadable[base, dir];
	n ← offset.wSize;
        s ← MIN[n, 2];
	wo ← IF dir = load THEN foffset.wd
	  ELSE foffset.wd + n-s;
	WHILE n # 0 DO
	  tr ← P5L.GenVarItem[bo];
	  cb[tr] ← [body: bo[base: base,
	    offset: [wSize: s, space: frame[wd: wo]]]];
	  MoveBo[tr, dir];
	  n ← n - s;
	  IF dir = load THEN 
	    BEGIN
	    wo ← wo + s;
	    s ← MIN[n, 2];
	    END
	  ELSE
	    BEGIN
	    s ← MIN[n, 2];
	    wo ← wo - s;
	    END;
	  ENDLOOP;
	END;
    IF offset.bSize # 0 THEN
      BEGIN
      IF dir = load THEN MoveFinalFragment[]
      ELSE IF foffset.bd # 0 THEN MoveInitialFragment[];
      END;
    END;
    
  MoveBdo: PROCEDURE [r: VarIndex, dir: MoveDirection] =
    BEGIN
    base, disp, offset: VarComponent;
    dpSize, bpSize: [1..2];
    magicOffset: BOOLEAN;
    foffset: POINTER TO frame VarComponent;
    
    BDCommute: PROCEDURE =
      BEGIN
      t: VarComponent = base;
      i: CARDINAL ← bpSize;
      base ← disp; disp ← t;
      bpSize ← dpSize; dpSize ← i;
      END;
    
    MagicBase: PROCEDURE [b: VarComponent] RETURNS [BOOLEAN] =
      BEGIN
      WITH bb: b SELECT FROM
	frame =>
	  BEGIN
	  IF bb.bd # 0 OR b.bSize # 0 THEN RETURN [FALSE];
	  IF b.wSize = 1 AND bb.level = CPtr.curctxlvl THEN
	    RETURN [bb.wd IN OpCodeParams.LocalHB];
	  IF CPtr.dStar AND b.wSize = 2 THEN SELECT bb.level FROM
	    lG => RETURN [bb.wd IN OpCodeParams.GlobalHB];
	    CPtr.curctxlvl => RETURN [bb.wd IN OpCodeParams.LocalHB];
	    ENDCASE;
	  END;
	ENDCASE;
      RETURN[FALSE];
      END;
    
    WITH cc: cb[r] SELECT FROM
      bdo =>
	BEGIN
	base ← cc.base;
	disp ← cc.disp;
	offset ← cc.offset;
	END;
      ENDCASE => ERROR;
    P5L.ReleaseVarItem[r];
    
    -- N.B. some of the following code is copied in MakeBo.  Any bugs should
    --   be fixed in both places.

    WITH vv: disp SELECT FROM
      const =>
	BEGIN
	owd: CARDINAL;
	ld: InlineDefs.LongNumber;
        bor: VarIndex;
        
	IF disp.wSize > 1 OR disp.bSize # 0 THEN GO TO tooBig;
	WITH offset SELECT FROM
	  frame => owd ← wd;
	  code => owd ← wd;
	  ENDCASE => ERROR;
	ld.lc ← LONG[CARDINAL[vv.d1]] + LONG[owd];
	IF ld.highbits # 0 THEN GO TO tooBig;
	P5L.ModComponent[var: @offset, wd: vv.d1];
	bor ← P5L.GenVarItem[bo];
	cb[bor] ← [body: bo[base: base, offset: offset]];
	MoveBo[bor, dir];
	RETURN;
	EXITS
	 tooBig => NULL;
	END;
      ENDCASE;
    
    WITH oo: offset SELECT FROM
      frame => foffset ← @oo;
      code =>
	BEGIN
	tr: VarIndex = P5L.GenVarItem[bo];
	P5L.LoadComponent[disp]; --more likely to be already loaded
	P5L.LoadComponent[base];
	P5U.Out0[qADD];
	cb[tr] ← [body: bo[base: P5L.TOSComponent[1], offset: offset]];
	MoveBo[tr, dir];
	RETURN;
	END;
      ENDCASE => ERROR;
    
    magicOffset ← offset.bSize = 0 AND offset.wSize = 1 AND
	foffset.level = lZ AND foffset.wd IN OpCodeParams.HB;
    bpSize ← P5L.Words[base.wSize, base.bSize];
    dpSize ← P5L.Words[disp.wSize, disp.bSize];
    
    BEGIN -- to set up some exit labels
    SELECT bpSize FROM
      >dpSize => GO TO different;
      <dpSize => BEGIN BDCommute[]; GO TO different END;
      =1 =>
	BEGIN
	IF ~magicOffset THEN GO TO notMagic;
	IF MagicBase[base] THEN GO TO magic;
	IF MagicBase[disp] THEN BEGIN BDCommute[]; GO TO magic END;
	GO TO notMagic;
	EXITS
	  magic =>
	    BEGIN
	    P5L.LoadComponent[disp];
	    WITH bb: base SELECT FROM
	      frame =>
		P5U.Out2[
		  IF dir = load THEN qRXL ELSE qWXL, 
		  bb.wd, foffset.wd];
	      ENDCASE;
	    RETURN;
	    END;
	END;
      =2 => GO TO notMagic;
      ENDCASE => ERROR;
    EXITS
      different =>
	BEGIN
	IF magicOffset AND MagicBase[base] THEN
	  BEGIN
	  RWXFLOp: ARRAY MoveDirection OF ARRAY BOOLEAN OF BYTE =
	    [[qRXLL, qRXGL], [qWXLL, qWXGL]];
	  P5L.LoadComponent[disp];
	  WITH bb: base SELECT FROM
	    frame =>
	      P5U.Out2[
		RWXFLOp[dir][bb.level = lG],
		bb.wd, foffset.wd];
	    ENDCASE => ERROR;
	  RETURN;
	  END;
	END;
      notMagic => NULL;
    END;
    bpSize ← P5L.LoadSum[@disp, @base];
    BEGIN
    tr: VarIndex = P5L.GenVarItem[bo];
    cb[tr] ← [body: bo[base: P5L.TOSComponent[bpSize], offset: offset]];
    MoveBo[tr, dir];
    END;
    END;
    
  MoveInd: PROCEDURE [r: VarIndex, dir: MoveDirection] =
    BEGIN
    base, index, offset: VarComponent;
    packed, simple: BOOLEAN;
    eWords, grain: CARDINAL;
    
    WITH cc: cb[r] SELECT FROM
      ind =>
	BEGIN
	base ← cc.base;
	index ← cc.index;
	offset ← cc.offset;
	simple ← cc.simple;
	WITH pp: cc SELECT FROM
	  packed =>
	    BEGIN
	    packed ← TRUE;
	    grain ← pp.grain;
	    END;
	  notPacked => 
	    BEGIN
	    packed ← FALSE;
	    eWords ← pp.eWords;
	    END;
	  ENDCASE;
	END;
      ENDCASE => ERROR;
    P5L.ReleaseVarItem[r];
    
    
    IF ~packed THEN
      BEGIN
      tr: BdoVarIndex = LOOPHOLE[P5L.GenVarItem[bdo]];
      cb[tr] ← [body: bdo[base: base, disp: NULL, offset: offset]];
      IF eWords = 1 THEN
	cb[tr].disp ← index
      ELSE
	BEGIN
	WITH vv: index SELECT FROM
	  const =>
	    BEGIN
	    ld: InlineDefs.LongNumber;
	    ld.lc ← LONG[CARDINAL[vv.d1]] * LONG[eWords];
	    vv.d1 ← ld.lowbits;
	    IF ld.highbits # 0 THEN
		BEGIN vv.wSize ← 2; vv.d2 ← ld.highbits; END;
	    cb[tr].disp ← index;
	    GO TO const;
	    END;
	  ENDCASE;
	P5L.LoadComponent[index];
	P5U.Out1[qLI, eWords];
	IF simple THEN
	  BEGIN
	  P5U.Out0[qMUL];
	  cb[tr].disp ← P5L.TOSComponent[1];
	  END
	ELSE
	  BEGIN
	  P5U.Out0[qAMUL];
	  P5U.Out0[qPUSH];
	  cb[tr].disp ← P5L.TOSComponent[2];
	  END;
	EXITS
	  const => NULL;
	END;
      MoveBdo[tr, dir];
      END
    ELSE
      BEGIN
      shift: [4..7] ← (SELECT grain FROM
	1 => 4, 2 => 5, 4 => 6, 8 => 7, ENDCASE => ERROR);
      obd: [0..wordlength);
      owd: CARDINAL;
      fd: ControlDefs.FieldDescriptor;
      bpSize: CARDINAL;
      RWFSOp: ARRAY MoveDirection OF ARRAY [1..2] OF BYTE =
	[[qRFS, qRFSL], [qWFS, qWFSL]];
    
      WITH oo: offset SELECT FROM
	code => BEGIN obd ← oo.bd; owd ← oo.wd END;
	frame =>
	  BEGIN
	  IF oo.level # lZ THEN ERROR;
	  obd ← oo.bd; owd ← oo.wd;
	  END;
	ENDCASE => ERROR;
    
      fd ← [offset: 0, posn: obd, size: offset.bSize];
      bpSize ← P5L.Words[base.wSize, base.bSize];
      IF bpSize ~IN [1..2] OR offset.wSize # 0 OR offset.bSize = 0
	OR CARDINAL[offset.bSize + obd] > grain
	THEN ERROR;
    
      WITH oo: offset SELECT FROM
	code =>
	  BEGIN
	  Mask: ARRAY [4..7] OF CARDINAL = [17B, 7B, 3B, 1B];
	  tr: VarIndex = P5L.GenVarItem[bdo];
	  fr: VarIndex = P5L.GenVarItem[ind];
	  iscomp, bscomp: VarComponent;
	  IF dir = store THEN ERROR;
	  IF owd # 0 THEN
	    BEGIN
	    P5L.LoadComponent[index];
	    P5L.GenAdd[owd];
	    index ← Stack.TempStore[1];
	    END
	  ELSE index ← P5L.EasilyLoadable[index, load];
	  P5L.LoadComponent[index];
	  P5L.GenShift[shift-8];
	  cb[tr] ← [body: bdo[
	    base: base, 
	    disp: P5L.TOSComponent[1], 
	    offset: [wSize: 1, space: code[wd: 0]]]];
	  MoveBdo[tr, load]; -- get word containing field
	  P5L.LoadComponent[P5L.AddrComponent[Stack.TempStore[1]]];
	  bscomp ← P5L.TOSComponent[1];
	  P5L.LoadComponent[index];
	  P5L.GenAnd[Mask[shift]];
	  iscomp ← P5L.TOSComponent[1];
	  cb[fr] ← [body: ind[
	    base: bscomp, index: iscomp,
	    offset: [bSize: offset.bSize,
	      space: frame[bd: obd]],
	    simple: TRUE,
	    packinfo: packed[grain: grain]]];
	  MoveInd[fr, load];
	  RETURN
	  END;
	ENDCASE;
      IF fd = [offset: 0, posn: 0, size: 8] THEN
	BEGIN
	RWSTROp: ARRAY MoveDirection OF ARRAY [1..2] OF BYTE =
	  [[qRSTR, qRSTRL], [qWSTR, qWSTRL]];
	alpha: CARDINAL ← owd;
	P5L.LoadBoth[@base, @index, FALSE];
	IF alpha > LAST[BYTE] THEN
	  BEGIN
	  P5L.GenAdd[alpha-LAST[BYTE]];
	  alpha ← LAST[BYTE];
	  END;
	P5U.Out1[RWSTROp[dir][bpSize], alpha];
	RETURN
	END;
      IF simple THEN
	BEGIN
	IF bpSize = 2 AND index.tag = stack THEN
	  index ← P5L.EasilyLoadable[index, dir]; -- move to temp
	P5L.LoadBoth[@base, @index, FALSE];
	IF owd # 0 THEN P5L.GenAdd[owd];
	END
      ELSE
	BEGIN
	Mask: ARRAY [4..7] OF CARDINAL = [17B, 7B, 3B, 1B];
	IF owd # 0 THEN
	  BEGIN
	  P5L.LoadComponent[index];
	  P5L.GenAdd[owd];
	  index ← Stack.TempStore[1];
	  END
	ELSE index ← P5L.EasilyLoadable[index, load];
	P5L.LoadComponent[base];
	P5L.LoadComponent[index];
	P5L.GenShift[shift-8];
	P5U.Out0[FOpCodes.qADD];
	P5L.LoadComponent[index];
	P5L.GenAnd[Mask[shift]];
	END;
      P5L.GenShift[shift];
      IF fd # LOOPHOLE[0] THEN P5L.GenAdd[fd];
      P5U.Out0[RWFSOp[dir][bpSize]];
      END;
    
    END;
    
  StoreComponent: PUBLIC PROCEDURE [var: VarComponent] =
    BEGIN
    w,b: CARDINAL;
    lvl: ContextLevel;
    
    WITH vv: var SELECT FROM
      frame =>
	BEGIN
	w ← vv.wd; b ← vv.bd; lvl ← vv.level;
	END;
      frameup =>
	BEGIN
	r: VarIndex;
	r ← P5L.GenVarItem[bo];
	cb[r] ← [body: bo[
	  base: [wSize: vv.pwSize, space: frame[wd: vv.wd, level: vv.level]],
	  offset: [wSize: vv.wSize, space: frame[wd: vv.delta]]]];
	MoveBo[r, store];
	END;
      linkup =>
	BEGIN
	r: VarIndex;
	r ← P5L.GenVarItem[bo];
	cb[r] ← [body: bo[
	  base: [wSize: 1, space: link[wd: vv.wd]],
	  offset: [wSize: vv.wSize, space: frame[wd: vv.delta]]]];
	MoveBo[r, store];
	END;
      ENDCASE => ERROR;
    IF b # 0 OR var.bSize # 0 THEN
      BEGIN
      r: VarIndex;
      r ← P5L.GenVarItem[bo];
      cb[r] ← [body: bo[
	base: P5L.AddrComponent[var],
	offset: [bSize: var.bSize, space: frame[bd: b]]]];
      MoveBo[r, store];
      END
    ELSE
      BEGIN
      SFOp: ARRAY BOOLEAN OF ARRAY [1..2] OF BYTE =
	[[qSL, qSLD], [qSG, qSGD]];
      SELECT lvl FROM
	lZ => ERROR;
	CPtr.curctxlvl, lG => IF var.wSize IN [1..2] THEN
	  BEGIN
	  P5U.Out1[SFOp[lvl=lG][var.wSize], w];
	  END
	ELSE
	  BEGIN
	  i: CARDINAL;
	  FOR i DECREASING IN [0..var.wSize) DO
	    P5U.Out1[SFOp[lvl=lG][1], w+i];
	    ENDLOOP;
	  END;
	ENDCASE =>
	  BEGIN
	  r: VarIndex = P5L.GenVarItem[bo];
	  cb[r] ← [body: bo[base: P5L.BaseComponent[lvl],
	    offset: [wSize: var.wSize,
	      space: frame[wd: w - ControlDefs.localbase]]]];
	  MoveBo[r, store];
	  END;
      END;
    END;
    
  StoreVar: PUBLIC PROCEDURE [r: VarIndex] =
    BEGIN
    WITH cb[r] SELECT FROM
      o =>
	BEGIN
	P5L.StoreComponent[var];
	P5L.ReleaseVarItem[r];
	END;
      bo => MoveBo[r, store];
      bdo => MoveBdo[r, store];
      ind => MoveInd[r, store];
      ENDCASE => ERROR;
    END;
    
    
  END.