*        704728   SIGMA 5/7         BPM M:PASS2
         SYSTEM   SIG7FDP
MODE     EQU      1
DEBUG    EQU      0
         CSECT    1
         DEF      MCP2
MCP2     EQU      %
         REF      M2,M3,M4,M5,M6,M7,M8,M15,M16,M17,M20,M32,MN1,MN2,;
                  MN3,MN4,MN8,MN9,MN16,M826,MSKFTYPE,Y1,Y2,Y4,Y8,Y01,;
                  Y02,Y08,Y0C,Y0F,Y001,Y002,Y004,Y008,Y00C,;
                  Y001F,Y00FFFF,Y0001,Y0002,Y0003,Y0004,Y0005,Y0006,;
                  Y0007,Y0008,Y000B,;
                  Y0301,Y0303,Y0343,Y0304,Y0405,Y0502,;
                  Y00FF,YFF00FFFF,Y000F,YFFF0FFFF,TXF:,TXM:,;
                  TXLIB,TX6F4:,TXASTL,TXHEAD,;
                  HEADWD,TXTREE,STK2,RFDFSTK2,PASSCODE
         REF      Y00F1
         REF      READBILI,2BNUM,3BNUM,12BNUM,4BNUM,GBYTE,GETSEG
         REF      DECLSTK,DECLSTK1,DECLBAS,RFDFBAS,RFDFSTK,;
                  RFDFSTK1,EXPRSTK,EXPRSTK1,EXPRBAS,BSEG1,BSEG2,;
                  CSEG1,CSEG2,CROM1,CROM2,CRFDF1,CRFDF2,CURBYTE,;
                  RCDSIZE,SEQNUM,SEVLEV,XSL,LASTCARD,BUF,BUF2,;
                  BFR,TEMPPTR,TREEPTR,FCOUNT,FTABLE,;
                  ERRTAB,ERRSTK,TCBSIZE,TCBPTR,FTAB,RSEG00,;
                  RSEG01,RSEG10,RREL00,RREL01,RREL10,CSEG00,;
                  CREL00,CREL01,CREL10,MAX00,;
                  MAX01,MAX10,DLOC,PLOC,SLOC,LOC,START,LOCCT,;
                  LOADBAS,MODBAS,RELDBAS,MBIAS,FBIAS,BIAS,;
                  RDIG,MODSIZ,NOTLLM,MAXRFDF,MAXEXPR,TOPOMEM,;
                  OPENEF
         REF      LOCWD
         REF      ER11
         REF      TEMPSIZE,DECLSIZE,EXPRDIS,;
                  RFLOADIS,RFDFDIS,NXROMDIS,SBLNKDIS,ROM1DIS,;
                  TMPSZDIS,NUNSTDIS,00DIS,01DIS,10DIS,NRWACCT,;
                  TREESIZE,TREEDIS,BIASDIS,LOWLIMDS,FCOMDIS,;
                  ERRDIS,LMNDIS,USACTDIS,LMPASS,LMEXPDIS,;
                  RDACT1,ROMSIZE,TCBBLNK
         DEF      PASS2
         REF      BINTOHEX,BLANKER,PRINT
         REF      READLM,WRITELM,M:LM,M:LL,M:EF,M:DIC,CLOSE,READEF
         REF      ALLOCATE,EVEXPRS,LOADSEG,WRITESEG
         REF      SEGLOAD
         REF      TRESIZ
         DO       MODE=1
         REF       CHKLM
         REF      MREFLAG
         FIN
         REF      KICKOUT,SQUEEZ,EVEXSQZ
         DO       MODE=0
         REF      ER17
         FIN
         REF      CODE
         PAGE
*                 SYMBOLIC REGISTER DEFINITIONS.
R0       EQU      0
R1       EQU      1
R2       EQU      2
R3       EQU      3
R4       EQU      4
R5       EQU      5
R6       EQU      6
R7       EQU      7
SR1      EQU      8
SR2      EQU      9
SR3      EQU      10
SR4      EQU      11
D1       EQU      12
D2       EQU      13
D3       EQU      14
D4       EQU      15
         PAGE
*                 IMMEDIATE CONSTANTS FOR LOADER.
K0       EQU      0
K1       EQU      1
K2       EQU      2
K3       EQU      3
K4       EQU      4
K5       EQU      5
K6       EQU      6
K7       EQU      7
K8       EQU      8
K9       EQU      9
KA       EQU      X'A'
KB       EQU      X'B'
KC       EQU      X'C'
KD       EQU      X'D'
KE       EQU      X'E'
KF       EQU      X'F'
K10      EQU      X'10'
K11      EQU      X'11'
K12      EQU      X'12'
K16      EQU      X'16'
K17      EQU      X'17'
K19      EQU      X'19'
K1C      EQU      X'1C'
K1E      EQU      X'1E'
K1F      EQU      X'1F'
K20      EQU      X'20'
K22      EQU         X'22'
K24      EQU      X'24'
K26      EQU      X'26'
K39      EQU      X'39'
K3C      EQU      X'3C'
K3F      EQU      X'3F'
K40      EQU      X'40'
K43      EQU      X'43'
K6C      EQU      X'6C'
K78      EQU      X'78'
K80      EQU      X'80'
KF0      EQU      X'F0'
KF1      EQU      X'F1'
KF2      EQU      X'F2'
KFF      EQU      X'FF'
K100     EQU      X'100'
K120     EQU      X'120'
K1FF     EQU      X'1FF'
K200     EQU      X'200'
K202     EQU      X'202'
K256     EQU      X'256'
K800     EQU      X'800'
KC00     EQU      X'C00'
K2000    EQU      X'2000'
K3FFF    EQU      X'3FFF'
K7FFF    EQU      X'7FFF'
KFF00   EQU      X'FF00'
K8000    EQU      X'8000'
K1FFFF   EQU      X'1FFFF'
K20000   EQU      X'20000'
KN1      EQU      -1
KN2      EQU      -2
KN3      EQU      -3
KN4      EQU      -4
KN8      EQU      -8
KN16     EQU      -16
KN100    EQU      -100
KTE      EQU      'E'
KTR      EQU      'R'
         PAGE
*        QUIT     ERROR,CONDITION
BGEZ     EQU      X'681'
BE       EQU      X'683'
BNEZ     EQU      X'693'
BGE      EQU      X'681'
BL       EQU      X'691'
BAZ      EQU      X'684'
BCR8     EQU      X'688'
BCR10    EQU      X'68A'
BLE      EQU      X'682'
BEZ      EQU      X'683'
         REF      MESSAGE
*       QUIT     ERROR, CONDITION,OUTPUT
QUIT     CNAME
         PROC
LF       EQU      %
         DO       NUM(AF)=3
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+4+MODE
         FIN
         STW,AF(3) CODE
         ELSE
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+3+MODE
         FIN
         FIN
         LI,R3    AF(1)
         DO       MODE=1
         LI,R4    AF(3)=SR3
         FIN
         B        MESSAGE
         PEND
         PAGE
*                                   PASS 2 CONTROLS THE READING OF
*                                   EACH SEGMENTS REF/DEF AND EXPR
*                                   STACK ALLOCATES THE CONTROL AND
*                                   DUMMY SECTIONS, EVALUATING
*                                   EXPRESSIONS LOADING AND UNITING
*                                   THE SEGMENT.
PASS2    PSW,SR4  *R0
         DO       MODE=1
         LI,R4    K0
         MTW,0    MREFLAG
         BEZ      NEXTSEG3
         BAL,SR4  NRM               EXTRA HALF-PASS FOR MREF
         FIN
         LI,R4    K0
         DO       MODE=1
         STW,R4   CSEG1
         B        FORMREF           ROOT'S STACKS ARE SET UP
         FIN
*                                   ESTABLISH THE STACK BASES IN THE
*                                   TREE.
NEXTSEG3 STW,R4   CSEG1
         AI,R4    RFDFDIS
         LW,R5    RFDFSTK
         AND,R5   M17
         STW,R5   RFDFSTK
         AI,R5    K2
         SLS,R5   -1
         LW,R6    *R4,R1
         AND,R6   MN16
         AW,R6    R5
         STW,R6   *R4,R1
         AI,R4    8-6
         LW,R5    EXPRSTK
         AND,R5   M17
         AI,R5    K2
         SLS,R5   -1
         LW,R6    *R4,R1
         AND,R6   MN16
         AW,R6    R5
         STW,R6   *R4,R1
*                                   READ THE REF/DEF STACK (NOT
*                                   NECESSARY FOR THE ROOT). INSURE
*                                   AN EVEN WORD BOUNDARY.
         LW,R5    RFDFSTK
         AI,R5    K1
         CI,R5    K1
         BAZ      NEXTSEG5
         AI,R5    K1
         LI,R6    K1
         MSP,R6   RFDFSTK
NEXTSEG5 LI,R6    K1FFFF
         LW,R7    CSEG1
         AW,R7    R1
         MTB,1    *R7
         LI,D4    K0
         LB,R4    *R7
         STB,D4   *R7,R4
         LW,R6    CSEG1
         BEZ      NEXTSEG4
         AI,R6    RFDFDIS
         LW,R6     *R6,R1
          LH,R6    R6
          SLS,R6   2
         CAL1,1   READLM
*                                   READ THE EXPR STACK. INSURE
*                                   AN EVEN WORD BOUNDARY.
NEXTSEG4 LW,R5    EXPRSTK
         AI,R5    K1
         CI,R5    K1
         BAZ      NEXTSEG6
         AI,R5    K1
         LI,R4    K1
         MSP,R4   EXPRSTK
NEXTSEG6 LI,D4    K1
         LB,R4    *R7
         STB,D4   *R7,R4
         LW,R4    R1
         AW,R4    CSEG1
          LW,R6    EXPRDIS,R4
         LH,R6     R6
         SLS,R6    2
         DO       MODE=1
         BAL,11   CHKLM
         FIN
         CAL1,1   READLM
         MTB,-1   *R7
*                                   ADJUST THE STACK POINTER.
         AI,R4    RFDFDIS
         LH,R7    *R4
         LW,R6    RFDFSTK
         AI,R6    K1
         MSP,R7   RFDFSTK
         AI,R4    8-6
         LH,R7    *R4
         DO       MODE=0
         AWM,R7   KICKOUT
         FIN
         MSP,R7   EXPRSTK
         DO       MODE=1
         MTW,0    MREFLAG
         BNEZ     FORMREF
         FIN
*                                   CHANGE "CURRENT" SEG BUFFER ADDR'S
*                                   RESET THE X'FF' HIGH-ORDER "USED"
*                                   BYTES OF ALL FORWARD REFERENCES.
NEXTSEGB LW,R4    *R6
         AND,R4   MSKFTYPE
         CW,R4    Y0005
         BNE      NEXTSEG8
         AI,R6    K3
         LI,R4    K0
         STB,R4   *R6
NEXTSEG9 AI,R6    K1
         B        NEXTSEGA
NEXTSEG8 LB,R4    *R6
         AW,R6    R4
NEXTSEGA CW,R6    RFDFSTK
         BL       NEXTSEGB
         DO       DEBUG=0
         DO       MODE=0
         LW,R5    TRESIZ
         BEZ      %+3
         FIN
         LI,R5    ALL
         CAL1,8   SEGLOAD
         FIN
*                                   ALLOCATE CONTROL AND DUMMY SECTIONS.
         BAL,SR4  ALLOCATE
FORMREF  EQU      %
         LW,R4    CSEG1
         BNEZ     NOTROOT
         LW,R6    01DIS,R1
         LH,R7    R6
         AW,R6    R7
         AND,R6   M16
         SLS,R6   1
         STW,R6   PLOC
NOTROOT  EQU      %
         AI,R4    SBLNKDIS
         INT,R4   *R4,R1
         CI,R4    K0
*                                   IF THERE IS A SUBLINK GO GET ITS
*                                   STACKS AND ALLOCATE.
         BNE      NEXTSEG3
LOAD     EQU      %
         DO       MODE=1
         MTW,0    MREFLAG
         BNEZ     LOAD3
         FIN
         LW,R5    LOCWD
         CI,R5    X'4000'           ARE WE IN XMEM?
         BAZ      LOAD36            NO
         LW,R5    KICKOUT
         DO       MODE=0            ENTER SQUEEZ ONLY ONCE
         BLZ      LOAD36
         ELSE
         BNEZ     LOAD36
         FIN
         DO       DEBUG=0
         DO       MODE=0
         LW,R5    TRESIZ
         BEZ      %+3
         FIN
         LI,R5    SQZ
         CAL1,8   SEGLOAD
         FIN
         DO       MODE=0            EXPRS USING SQZ'S EVEXPRS
         LI,R4    K200
         LW,R6    TOPOMEM           MAKE SURE THERE'S ENOUGH
         SW,R6    EXPRBAS             ROOM TO ENTER EVEXSQZ.
         SW,R6    KICKOUT           KICKOUT NOW CONTAINS THIS
         LW,R5    LOCWD               PATH'S EXPR SIZE.
         CI,R5    K100
         BANZ     %+2
         AI,R4    K200              MUST HAVE 2 BUFS IF NOT ABS
         CW,R6    R4
         BG       LOADSQ
         SW,R4    R6                NOT ENOUGH ROOM FOR CORE
         QUIT     ER17,,R4            IMAGE BUF(S)
LOADSQ   EQU      %
         LI,R6    K0
         STW,R6   KICKOUT           INITIALIZE FOR SQUEEZE
         FIN
         BAL,SR4  EVEXSQZ           USE SQZ'S EVEXPRS
         BAL,SR4  SQUEEZ            SQUEEZE ROOT' EXPR STK
         DO       MODE=1
         B        LOAD3
         FIN
         DO       MODE=0
         LW,R6    KICKOUT
         CW,R6    R4                TEST TO SEE THAT ENOUGH
         BG       LOAD30              CORE WAS REGAINED BY
         SW,R4    R6                  BY SQUEEZ TO CONTINUE.
         QUIT     ER17,,R4          IF NOT, MUST STOP.
         FIN
LOAD30   EQU      %
         DO       MODE=0
         LW,R6    Y8                FLAG KICKOUT TO SHOW
         AWM,R6   KICKOUT             SQUEEZ ENTERED
         B        LOAD3
         FIN
LOAD36   EQU      %
         DO       DEBUG=0
         DO       MODE=0
         LW,R5    TRESIZ
         BEZ      %+3
         FIN
         LI,R5    EVL
         CAL1,8   SEGLOAD
         FIN
*                                   EVALUATE THE PATHS EXPRESSIONS.
*
         BAL,SR4  EVEXPRS
LOAD3    LW,R4    CSEG1
         BNEZ     LOAD1
*                                   TO POINT TO THE ROOT SEGMENT
*                                   BUFFERS WHEN LOADING THE ROOT (LAST)
         LI,R4    K6
         LI,R5    RSEG00-1
         LI,R6    CSEG00-1
         LW,R7    *R5,R4
         STW,R7   *R6,R4
         BDR,R4   %-2
LOAD1    EQU      %
         DO       DEBUG=0
         DO       MODE=0
         LW,R5    TRESIZ
         BEZ      %+3
         FIN
         LI,R5    EVL
*                                   FORM THE LOAD IMAGE.
         CAL1,8   SEGLOAD
         FIN
         BAL,SR4  LOADSEG
         DO       DEBUG=0
         DO       MODE=0
         LW,R5    TRESIZ
         BEZ      %+3
         FIN
         LI,R5    WRT
         CAL1,8   SEGLOAD
         FIN
*                                   WRITE THE LOAD IMAGE INTO THE LOAD
*                                   MODULE FILE.
         BAL,SR4  WRITESEG
*                                   PULL THE STACKS TO ELIMINATE THIS
*                                   SEGMENTS ENTRIES.
         LW,R4    CSEG1
         AI,R4    RFDFDIS
         LW,R4    *R4,R1
         LH,R4    R4
         LCW,R4   R4
         MSP,R4   RFDFSTK
         LW,R4    CSEG1
         AI,R4    EXPRDIS
         LW,R4    *R4,R1
         LH,R4    R4
         LCW,R4   R4
         MSP,R4   EXPRSTK
         LW,R4    CSEG1
         DO       MODE=1
         MTW,0    MREFLAG
         BNEZ     %+2
         FIN
*                                   PULL THE LOCATION COUNTERS BACK
*                                   DOWN TO THE START OF THIS SEGMENT.
         BAL,SR4  PULLOC
         AI,R4    SBLNKDIS
         LW,R4    *R4,R1
         AND,R4   M16
         BEZ      LOAD2
*                                   THIS SEGMENT HAS AN OVERLAY- PERSUE
*                                   THOSE PATHS.
         B        NEXTSEG3
*                                   IF THIS SEGMENT IS THE ROOT WE,RE
*                                   DONE.
LOAD2    LW,R4    CSEG1
         BEZ      EXIT
         AI,R4    ROM1DIS
         LW,R4    *R4,R1
         AND,R4   M16
         STW,R4   CSEG1
*                                   BACK UP TO THE BACKWARD LINK SEGMENT
*                                   AND PURSUE THE PATH FROM THERE.
         B        LOAD3
EXIT     EQU      %
*                                   ALL DONE!
         PLW,SR4  *R0
         B        *SR4
         PAGE
*                                   THIS ROUTINE PICKS UP THE SIZES OF
*                                   THE THREE PROTECTION TYPES OF THE
*                                   SEGMENT POINTED TO BY R4 AND SUB-
*                                   TRACTS THEM FROM THE CORRESPONDING
*                                   LOCATION COUNTERS.
PULLOC   EQU      %
         LW,R5    R4                SEG DISP IN TREE
         AI,R5    00DIS             00 SIZE DISP
         LI,R6    -3
NEXTLOC  LW,R7    *R1,R5            GET SIZE
         SLS,R7   -16
         SLS,R7   1                 IN WORDS.
         LCW,R7   R7                SUBSTRACT FROM TOP
         AWM,R7   DLOC+3,R6         AND DEPOSIT IN LOC
         AI,R5    2                 POINT TO NEXT SIZE
         BIR,R6   NEXTLOC
         B        *SR4
         PAGE
*                                   DRIVER FOR NEW REFERENCING
*                                   MODE FOR CP-V MONITOR.
*                                   CONTROLS EVEXPRS-TYPE
*                                   EVALUATION AND WRITING
*                                   OUT OF MONITOR'S R/D & EXPR
*                                   STKS FOR PRELIMINARY HALF-PASS
*                                   PRIOR TO NORMAL PASS2
*                                   PROCESSING.
NRM      EQU      %
         PSW,SR4  *R0
NRMNEXT  EQU      %
         STW,R4   CSEG1
         AI,R4    RFDFDIS
         LW,R5    RFDFSTK
         AND,R5   M17
         STW,R5   RFDFSTK
         AI,R5    K2
         SLS,R5   -1
         LW,R6    *R4,R1
         AND,R6   MN16
         AW,R6    R5
         STW,R6   *R4,R1
         AI,R4    8-6
         LW,R5    EXPRSTK
         AND,R5   M17
         AI,R5    K2
         SLS,R5   -1
         LW,R6    *R4,R1
         AND,R6   MN16
         AW,R6    R5
         STW,R6   *R4,R1
*                                   READ THE REF/DEF STACK (NOT
*                                   NECESSARY FOR THE ROOT). INSURE
*                                   AN EVEN WORD BOUNDARY.
         LW,R5    RFDFSTK
         AI,R5    K1
         CI,R5    K1
         BAZ      NRMSEG5
         AI,R5    K1
         LI,R6    K1
         MSP,R6   RFDFSTK
NRMSEG5  EQU      %
         LW,R7    CSEG1
         AW,R7    R1
         MTB,1    *R7
         LI,D4    K0
         LB,R4    *R7
         STB,D4   *R7,R4
         LW,R6    CSEG1
         BEZ      NRMSEG4
         AI,R6    RFDFDIS
         LW,R6     *R6,R1
          LH,R6    R6
          SLS,R6   2
         CAL1,1   READLM
*                                   READ THE EXPR STACK. INSURE
*                                   AN EVEN WORD BOUNDARY.
NRMSEG4  LW,R5    EXPRSTK
         AI,R5    K1
         CI,R5    K1
         BAZ      NRMSEG6
         AI,R5    K1
         LI,R4    K1
         MSP,R4   EXPRSTK
NRMSEG6  LI,D4    K1
         LB,R4    *R7
         STB,D4   *R7,R4
         LW,R4    R1
         AW,R4    CSEG1
          LW,R6    EXPRDIS,R4
         LH,R6     R6
         SLS,R6    2
         BAL,11   CHKLM
         CAL1,1   READLM
         MTB,-1   *R7
*                                   ADJUST THE STACK POINTER.
         AI,R4    RFDFDIS
         LH,R7    *R4
         LW,R6    RFDFSTK
         AI,R6    K1
         MSP,R7   RFDFSTK
         AI,R4    8-6
         LH,R7    *R4
         MSP,R7   EXPRSTK
*                                   CHANGE "CURRENT" SEG BUFFER ADDR'S
*                                   RESET THE X'FF' HIGH-ORDER "USED"
*                                   BYTES OF ALL FORWARD REFERENCES.
NRMSEGB  LW,R4    *R6
         AND,R4   MSKFTYPE
         CW,R4    Y0005
         BNE      NRMSEG8
         AI,R6    K3
         LI,R4    K0
         STB,R4   *R6
NRMSEG9  AI,R6    K1
         B        NRMSEGA
NRMSEG8  LB,R4    *R6
         AW,R6    R4
NRMSEGA  CW,R6    RFDFSTK
         BL       NRMSEGB
         LI,R5    ALL
         CAL1,8   SEGLOAD
*                                   ALLOCATE CONTROL AND DUMMY SECTIONS.
         BAL,SR4  ALLOCATE
NRMNROOT EQU      %
         LW,R4    CSEG1
         AI,R4    SBLNKDIS
         INT,R4   *R4,R1
         CI,R4    K0
*                                   IF THERE IS A SUBLINK GO GET ITS
*                                   STACKS AND ALLOCATE.
         BNE      NRMNEXT
         LI,R5    SQZ
         CAL1,8   SEGLOAD
         BAL,SR4  EVEXSQZ           USE SQZ'S EVEXPRS
         LW,D4    KICKOUT
         BNEZ     %+2
         BAL,SR4  SQUEEZ            SQUEEZE ROOT' EXPR STK
*                                   PULL THE STACKS TO ELIMINATE THIS
*                                   SEGMENTS ENTRIES.
         LI,D4    K0
         LW,R4    CSEG1
         AI,R4    RFDFDIS
         BAL,SR4  WRITESTK          WRITE OUT R/D STK
         LCW,R6   R6
         MSP,R6   RFDFSTK
         LI,D4    K1
         LW,R4    CSEG1
         AI,R4    EXPRDIS
         BAL,SR4  WRITESTK          WRITE OUT EXPR STK
         LCW,R6   R6
         MSP,R6   EXPRSTK
         LW,R4    CSEG1
*                                   PULL THE LOCATION COUNTERS BACK
*                                   DOWN TO THE START OF THIS SEGMENT.
         BAL,SR4  PULLOC
         AI,R4    SBLNKDIS
         LW,R4    *R4,R1
         AND,R4   M16
         BEZ      NRMEXIT
*                                   THIS SEGMENT HAS AN OVERLAY- PERSUE
*                                   THOSE PATHS.
         B        NRMNEXT
*                                   IF THIS SEGMENT IS THE ROOT WE,RE
*                                   DONE.
NRMEXIT  EQU      %
*                                   ALL DONE!
         PLW,SR4  *R0
         B        *SR4
*                                   WRITE OUT CSEG'S R/D OR
*                                   EXPR STACK
WRITESTK EQU      %
         LW,R5    *R4,R1
         LH,R6    R5
         SLS,R6   2                 BUF SIZE=SIZE OF SEG'S STK.
         AND,R5   M16
         SLS,R5   1                 BUF ADDR=START OF THIS
*                                   SEG'S R/D STK.
         LW,R7    CSEG1
         AW,R7    R1
         MTB,1    *R7
         LB,R3    *R7
         STB,D4   *R7,R3            KEY
         CAL1,1   WRITELM
         MTB,-1   *R7
         SLS,R6   -2
         B        *SR4
         PAGE
ALL      TEXTC    'ALL'
         TEXT     '    '
EVL      TEXTC    'EVL'
         TEXT     '    '
WRT      TEXTC    'WRT'
         TEXT     '    '
SQZ      TEXTC    'SQZ'
         TEXT     '    '
         END

