-- XXDebugContext.mesa  Edited by Bruce,  October 13, 1980  3:02 PM

DIRECTORY
  CoreSwapDefs: FROM "coreswapdefs",
  DebugFormat: FROM "debugformat",
  DebugUsefulDefs: FROM "debugusefuldefs",
  IODefs: FROM "iodefs",
  PrincOps: FROM "princops",
  RESOut: FROM "resout",
  State: FROM "state",
  STDebugDefs: FROM "stdebugdefs",
  StringDefs: FROM "stringdefs",
  Symbols: FROM "symbols",
  XXDebugDefs: FROM "xxdebugdefs";

XXDebugContext: PROGRAM 
  IMPORTS DebugUsefulDefs, RESOut, STDebugDefs, StringDefs, XXDebugDefs
  EXPORTS XXDebugDefs =
  BEGIN OPEN RESOut, XXDebugDefs, State;

  PNil: PROCEDURE [p: POINTER] =
    BEGIN
    IF p = NIL THEN PString["NIL"L]
    ELSE POctal[p];
    END;

  PNextNil: PROCEDURE [lbl: STRING, p: POINTER, indent: CARDINAL ← 2] =
    BEGIN
    IF p = NIL THEN {PNext[lbl, 5, indent]; PString[": NIL"L]}
    ELSE PNextOctal[lbl, p, indent];
    END;

  PutTopCtx: PUBLIC PROCEDURE =
    BEGIN
    gsp: GSHandle = GS[];
    level: INTEGER;
    esv: CoreSwapDefs.ExternalStateVector;
    level ← DebugUsefulDefs.ShortREAD[@gsp.level];
    DebugUsefulDefs.ShortCopyREAD[
      to: @esv,
      nwords: SIZE[CoreSwapDefs.ExternalStateVector],
      from: @gsp.ESV];
    PCr[];
    PString["dLevel: "L]; PUnsigned[level];
    PNextUnsigned["uLevel"L, esv.level];
    PNextNil["state"L, esv.state];
    IF esv.state # NIL THEN 
       {PString[" ("L]; PrintUserState[esv.state]; PChar[')]};
    PutAsContext[TopCtx[]];
    END;

  PutTos: PUBLIC PROCEDURE =
    BEGIN
    handle: Handle = TopCtx[];
    sp: POINTER TO Stack = DebugUsefulDefs.ShortREAD[@handle.fooStack];
    IF sp = NIL THEN {PCr[]; PString["<empty>"]}
    ELSE PutAsStack[sp];
    END;

  PutAsContext: PUBLIC PROCEDURE [handle: State.Handle] =
    BEGIN
    context: Context;
    mop: STRING = [20];
    IF handle = NIL THEN RETURN;
    DebugUsefulDefs.ShortCopyREAD[to: @context, nwords: SIZE[Context], from: handle];
    PCr[];
    POctal[handle];
    BEGIN OPEN c: context;
    PString["↑ = prev: "L]; PNil[c.prev];
    PNextOctal["lContext"L, c.lContext];
    PNextOctal["gContext"L, c.gContext];
    PNextOctal["pContext"L, c.pContext];
    PNextOctal["interpretContext"L, c.interpretContext];
    PutAsContextType[c.howSet];
    PNextNil["fooStack"L, c.fooStack];
    END; -- of OPEN context
    END;

  PutAsContextType: PROC [how: State.ContextType] =
    BEGIN
    s: STRING ← [20];
    StringDefs.AppendString[s, ", howset: "L];
    StringDefs.AppendString[s, SELECT how FROM
      local => "local"L,
      psb => "psb"L,
      global => "global"L,
      state => "state"L,
      ENDCASE => "none"L];
    PString[s];
    END;

  PutAsStack: PUBLIC PROCEDURE [stack: POINTER TO Stack] =
    BEGIN
    f: DebugFormat.Foo;
    IF stack = NIL THEN RETURN;
    PCr[];
    POctal[stack];
    PString["↑ = link: "L];
    PNil[DebugUsefulDefs.ShortREAD[@stack.link]];
    PNextNil["foo"L, f ← DebugUsefulDefs.ShortREAD[@stack.foo]];
    PutAsFoo[f];
    END;

  PrintUserState: PUBLIC PROCEDURE [StatePtr: POINTER] =
    BEGIN
    mop: STRING = [20];
    state: PrincOps.StateVector;
    i, len: CARDINAL;
    first: BOOLEAN ← TRUE;
    UserCopyRead[
      from: StatePtr,
      nwords: SIZE[PrincOps.StateVector],
      to: @state];
    StringDefs.AppendString[mop, "inst: "L];
    STDebugDefs.AppendMopcodeName[mop, state.instbyte];
    [] ← MakeRoom[mop.length, 2];
    PString[mop];
    PNextNil["source"L, state.source];
    PNextNil["dest"L, state.dest];
    PNext["stk: ("L, 3]; PUnsigned[state.stkptr]; PString[")["L];
    FOR i IN [0..MIN[state.stkptr+2, 8]) DO
      len ← Log8[state.stk[i]] + (IF first THEN 0 ELSE 1);
      IF state.stk[i] > 7 THEN len ← len+1;
      IF ~first THEN PChar[',];
      IF MakeRoom[len, 2] AND ~first THEN PChar[IODefs.SP];
      first ← FALSE;
      POctal[state.stk[i]];
      ENDLOOP;
    PChar[']];
    END;


  PutAsFoo: PUBLIC PROCEDURE [foo: DebugFormat.Foo] =
    BEGIN OPEN DebugFormat;
    fob: Fob;
    IF foo = NIL THEN RETURN;
    DebugUsefulDefs.ShortCopyREAD[to: @fob, nwords: SIZE[Fob], from: foo];
    PCr[];
    POctal[foo];
    PString["↑ = hti: "L];
    BEGIN OPEN fob;
    PNull[hti, Symbols.HTNull];
    IF hti # Symbols.HTNull THEN
      {PString[" ("L]; STDebugDefs.PutHashString[hti]; PChar[')]};
    PNextNull["tsei"L, tsei, Symbols.SENull];
    PNext["addr: "L, LongLog8[LOOPHOLE[addr.base]]+2];
    PLongNumber[addr.base, [base: 8, zerofill: FALSE, unsigned: TRUE, columns: 0]];
    PString["B↑"L];
    IF addr.offset # 0 THEN PNextUnsigned["offset"L, addr.offset];
    IF words # 0 THEN PNextUnsigned["words"L, words];
    IF bits # 0 THEN PNextUnsigned["bits"L, bits];
    IF indent # 0 THEN PNextUnsigned["indent"L, indent];
    IF nesting # 0 THEN PNextUnsigned["nesting"L, nesting];
    IF xfer THEN PNext["xfer"L];
    IF there THEN PNext["there"L];
    IF typeOnly THEN PNext["typeOnly"L];
    IF addr.local THEN PNext["local"L];
    IF addr.useStack THEN PNext["useStack"L];
    END;
    END;

  PutAsBB: PUBLIC PROCEDURE [brk: DebugFormat.BBHandle] =
    BEGIN OPEN DebugFormat;
    bb: BreakBlock;
    mop: STRING = [16];
    exName: ARRAY EXOI OF STRING = ["entry"L, "exit"L, "octal"L, "in"L];
    btName: ARRAY BT OF STRING = ["break"L, "trace"L];
    IF brk = NIL THEN RETURN;
    DebugUsefulDefs.ShortCopyREAD[to: @bb, nwords: SIZE[BreakBlock], from: brk];
    PCr[];
    POctal[brk];
    PString["↑ = link: "L];
    BEGIN OPEN bb;
    POctal[link];
    PNextOctal["num"L, num];
    PNextOctal["gf"L, gf];
    IF condition # NIL THEN
      BEGIN
      PChar[',];
      IF MakeRoom[DebugUsefulDefs.ShortREAD[@condition.length]+6, 2] THEN
	PChar[IODefs.SP];
      PString["cond: "L]; STDebugDefs.PRemoteString[condition];
      END;
    IF exp # NIL THEN
      BEGIN
      PChar[',];
      IF MakeRoom[DebugUsefulDefs.ShortREAD[@exp.length]+5, 2] THEN
	PChar[IODefs.SP];
      PString["exp: "L]; STDebugDefs.PRemoteString[exp];
      END;
    PNextOctal["pc"L, pc];
    StringDefs.AppendString[mop, "inst: "L];
    STDebugDefs.AppendMopcodeName[mop, inst];
    PNext[mop];
    PNext[exName[bt.ex]];
    PNext[btName[bt.bt]];
    END; -- OPEN bb
    END;

  END.