*        704728   SIGMA 5/7         BPM M:PASS2
***********************************************************************
*M*      PS2      DRIVER FOR THE LOADER'S SECOND PASS.
***********************************************************************
*P*      NAME:    PS2
*P*      PURPOSE: ENTRY/EXIT FROM LDR.
*P*               A DRIVER FOR THE SECOND PASS.
*P*               CALLS ALL,SQZ,EVL AND WRT.
*P*               READS CURRENT SEGMENT'S STACKS.
*P*      DESCRIPTION: PS2 IS REALLY A DRIVER FOR THE SECOND PASS. IT
*P*               CALLS ALL,EVL AND WRT AS IT PROCEEDS ALONG THE
*P*               SEGMENTS. IN ADDITION,FOR EXTENDED MEMORY MODE,PS2
*P*               CALLS SQZ FOR EXPRESSION EVALUATION AND TO SQUEEZE
*P*               THE ROOT'S EXPRESSION STACK.
*P*      REFERENCE: THE OVERLAY LOADER TECHNICAL MANUAL.
***********************************************************************
         SYSTEM   SIG7FDP
DEBUG    EQU      0
         CSECT    1
         DEF      MCP2
MCP2     EQU      %
         REF      M16
         REF      M17
         REF      MN16
         REF      MSKFTYPE
         REF      Y8
         REF      Y0005
         REF      RFDFSTK
         REF      EXPRSTK
         REF      EXPRBAS
         REF      CSEG1
         REF      RSEG00
         REF      CSEG00
         REF      DLOC
         REF      PLOC
         REF      TOPOMEM
         REF      EXPRDIS
         REF      RFDFDIS
         REF      SBLNKDIS
         REF      ROM1DIS
         REF      00DIS
         REF      01DIS
         REF      READLM
         REF      LOCWD
         DEF      PASS2
         REF      ALLOCATE,EVEXPRS,LOADSEG,WRITESEG
         REF      SEGLOAD
         REF      TRESIZ
         REF       CHKLM
         REF      MREFLAG
         REF      KICKOUT,SQUEEZ,EVEXSQZ
         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
         REF      IOMSG
*       QUIT     ERROR, CONDITION,OUTPUT
QUIT     CNAME
         PROC
LF       EQU      %
         DO       NUM(AF)=3
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+4
         FIN
         STW,AF(3) CODE
         ELSE
         DO       AF(2)~=0
         GEN,12,20  AF(2),%+3
         FIN
         FIN
         LI,R3    AF(1)
         DO       AF(3)=SR3
         B        IOMSG
         ELSE
         B        MESSAGE
         FIN
         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.
EVL      TEXTC    'EVL'
ALLL     RES
SQZ      RES
WRT      TEXTC    'WRT'
PASS2    PSW,SR4  *R0
         LI,R4    K0
*                                   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
X4I1     RES                        INSTRUCTION FOR CALCULATING R4+R1
         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 RES
         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
         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.
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
         LI,R5    ALLL
         CAL1,8   SEGLOAD
*                                   ALLOCATE CONTROL AND DUMMY SECTIONS.
         BAL,SR4  ALLOCATE
         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
         MTW,0    MREFLAG
         BNEZ     LOAD36
         LW,R5    LOCWD
         CI,R5    X'4000'           ARE WE IN XMEM?
         BAZ      LOAD36            NO
         LW,R5    KICKOUT
         BNEZ     LOAD36
         DO       SQZ-ALLL~=0
         LI,R5    SQZ
         CAL1,8   SEGLOAD
         FIN
         BAL,SR4  EVEXSQZ           USE SQZ'S EVEXPRS
         BAL,SR4  SQUEEZ            SQUEEZE ROOT' EXPR STK
         LI,R5    EVL
         CAL1,8   SEGLOAD           LOAD EVL SEG
         B        LOAD3
LOAD36   EQU      %
         LI,R5    EVL
         CAL1,8   SEGLOAD
*                                   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
         LW,R7    RSEG00-1,R4
         STW,R7   CSEG00-1,R4
         BDR,R4   %-2
LOAD1    EQU      %
         BAL,SR4  LOADSEG
         LI,R5    WRT
         CAL1,8   SEGLOAD
*                                   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
         ANLZ,R5  X4I1              ADD R1 AND R4 INTO R5
         INT,R5   EXPRDIS,R5        GET BASE OF STAC
         SLS,R5   1                 IN WDS
         AI,R5    -1                STACK POINTS TO LAST ENTRY
         SW,R5    EXPRSTK
         MSP,R5   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      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        LOAD36
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
         END

