1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)lab.c 1.16 11/12/82"; 4 5 #include "whoami.h" 6 #include "0.h" 7 #include "tree.h" 8 #include "opcode.h" 9 #include "objfmt.h" 10 #ifdef PC 11 # include "pc.h" 12 # include "pcops.h" 13 #endif PC 14 15 /* 16 * Label enters the definitions 17 * of the label declaration part 18 * into the namelist. 19 */ 20 label(r, l) 21 int *r, l; 22 { 23 static bool label_order = FALSE; 24 static bool label_seen = FALSE; 25 #ifdef PC 26 char extname[ BUFSIZ ]; 27 #endif PC 28 #ifndef PI0 29 register *ll; 30 register struct nl *p, *lp; 31 32 lp = NIL; 33 #else 34 send(REVLAB, r); 35 #endif 36 if ( ! progseen ) { 37 level1(); 38 } 39 line = l; 40 #ifndef PI1 41 if (parts[ cbn ] & (CPRT|TPRT|VPRT|RPRT)){ 42 if ( opt( 's' ) ) { 43 standard(); 44 error("Label declarations should precede const, type, var and routine declarations"); 45 } else { 46 if ( !label_order ) { 47 label_order = TRUE; 48 warning(); 49 error("Label declarations should precede const, type, var and routine declarations"); 50 } 51 } 52 } 53 if (parts[ cbn ] & LPRT) { 54 if ( opt( 's' ) ) { 55 standard(); 56 error("All labels should be declared in one label part"); 57 } else { 58 if ( !label_seen ) { 59 label_seen = TRUE; 60 warning(); 61 error("All labels should be declared in one label part"); 62 } 63 } 64 } 65 parts[ cbn ] |= LPRT; 66 #endif 67 #ifndef PI0 68 for (ll = r; ll != NIL; ll = ll[2]) { 69 l = getlab(); 70 p = enter(defnl(ll[1], LABEL, 0, l)); 71 /* 72 * Get the label for the eventual target 73 */ 74 p->value[1] = getlab(); 75 p->chain = lp; 76 p->nl_flags |= (NFORWD|NMOD); 77 p->value[NL_GOLEV] = NOTYET; 78 p->value[NL_ENTLOC] = l; 79 lp = p; 80 # ifdef OBJ 81 /* 82 * This operator is between 83 * the bodies of two procedures 84 * and provides a target for 85 * gotos for this label via TRA. 86 */ 87 putlab(l); 88 put(2, O_GOTO | cbn<<8, (long)p->value[1]); 89 # endif OBJ 90 # ifdef PC 91 /* 92 * labels have to be .globl otherwise /lib/c2 may 93 * throw them away if they aren't used in the function 94 * which defines them. 95 */ 96 extlabname( extname , p -> symbol , cbn ); 97 putprintf( " .globl %s" , 0 , extname ); 98 if ( cbn == 1 ) { 99 stabglabel( extname , line ); 100 } 101 # endif PC 102 } 103 gotos[cbn] = lp; 104 # ifdef PTREE 105 { 106 pPointer Labels = LabelDCopy( r ); 107 108 pDEF( PorFHeader[ nesting ] ).PorFLabels = Labels; 109 } 110 # endif PTREE 111 #endif 112 } 113 114 #ifndef PI0 115 /* 116 * Gotoop is called when 117 * we get a statement "goto label" 118 * and generates the needed tra. 119 */ 120 gotoop(s) 121 char *s; 122 { 123 register struct nl *p; 124 #ifdef PC 125 char extname[ BUFSIZ ]; 126 #endif PC 127 128 gocnt++; 129 p = lookup(s); 130 if (p == NIL) 131 return (NIL); 132 # ifdef OBJ 133 put(2, O_TRA4, (long)p->value[NL_ENTLOC]); 134 # endif OBJ 135 # ifdef PC 136 if ( cbn == bn ) { 137 /* 138 * local goto. 139 */ 140 extlabname( extname , p -> symbol , bn ); 141 /* 142 * this is a jmp because it's a jump to a label that 143 * has been declared global. Although this branch is 144 * within this module the assembler will complain that 145 * the destination is a global symbol. The complaint 146 * arises because the assembler doesn't change jbr's 147 * into jmp's and consequently may cause a branch 148 * displacement overflow when the module is subsequently 149 * linked into the rest of the program. 150 */ 151 putprintf( " jmp %s" , 0 , extname ); 152 } else { 153 /* 154 * Non-local goto. 155 * 156 * Close all active files between top of stack and 157 * frame at the destination level. Then call longjmp 158 * to unwind the stack to the destination level. 159 * 160 * For nested routines the end is calculated as: 161 * __disply[ bn ] . ap + sizeof( local frame ) 162 * The size of the local frame is dumped out by 163 * the second pass as an assembler constant. 164 * The main routine may not be compiled in this 165 * module, so its size may not be available. 166 * However all of its variables will be globally 167 * declared, so only the known runtime temporaries 168 * will be in its stack frame. 169 */ 170 parts[ bn ] |= NONLOCALGOTO; 171 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 172 , "_PCLOSE" ); 173 if ( bn > 1 ) { 174 p = lookup( enclosing[ bn - 1 ] ); 175 sprintf( extname, "LF%d+%d", p -> value[ NL_ENTLOC ] 176 , sizeof( int ) ); 177 p = lookup(s); 178 putLV( extname , bn , 0 , NNLOCAL , P2PTR | P2CHAR ); 179 } else { 180 putLV( 0 , bn , -( DPOFF1 + sizeof( int ) ) , LOCALVAR , 181 P2PTR | P2CHAR ); 182 } 183 putop( P2CALL , P2INT ); 184 putdot( filename , line ); 185 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 186 , "_longjmp" ); 187 putLV( 0 , bn , GOTOENVOFFSET , NLOCAL , P2PTR|P2STRTY ); 188 extlabname( extname , p -> symbol , bn ); 189 putLV( extname , 0 , 0 , NGLOBAL , P2PTR|P2STRTY ); 190 putop( P2LISTOP , P2INT ); 191 putop( P2CALL , P2INT ); 192 putdot( filename , line ); 193 } 194 # endif PC 195 if (bn == cbn) 196 if (p->nl_flags & NFORWD) { 197 if (p->value[NL_GOLEV] == NOTYET) { 198 p->value[NL_GOLEV] = level; 199 p->value[NL_GOLINE] = line; 200 } 201 } else 202 if (p->value[NL_GOLEV] == DEAD) { 203 recovered(); 204 error("Goto %s is into a structured statement", p->symbol); 205 } 206 } 207 208 /* 209 * Labeled is called when a label 210 * definition is encountered, and 211 * marks that it has been found and 212 * patches the associated GOTO generated 213 * by gotoop. 214 */ 215 labeled(s) 216 char *s; 217 { 218 register struct nl *p; 219 #ifdef PC 220 char extname[ BUFSIZ ]; 221 #endif PC 222 223 p = lookup(s); 224 if (p == NIL) 225 return (NIL); 226 if (bn != cbn) { 227 error("Label %s not defined in correct block", s); 228 return; 229 } 230 if ((p->nl_flags & NFORWD) == 0) { 231 error("Label %s redefined", s); 232 return; 233 } 234 p->nl_flags &= ~NFORWD; 235 # ifdef OBJ 236 patch4(p->value[NL_ENTLOC]); 237 # endif OBJ 238 # ifdef PC 239 extlabname( extname , p -> symbol , bn ); 240 putprintf( "%s:" , 0 , extname ); 241 # endif PC 242 if (p->value[NL_GOLEV] != NOTYET) 243 if (p->value[NL_GOLEV] < level) { 244 recovered(); 245 error("Goto %s from line %d is into a structured statement", s, p->value[NL_GOLINE]); 246 } 247 p->value[NL_GOLEV] = level; 248 } 249 #endif 250 251 #ifdef PC 252 /* 253 * construct the long name of a label based on it's static nesting. 254 * into a caller-supplied buffer (that should be about BUFSIZ big). 255 */ 256 extlabname( buffer , name , level ) 257 char buffer[]; 258 char *name; 259 int level; 260 { 261 char *starthere; 262 int i; 263 264 starthere = &buffer[0]; 265 for ( i = 1 ; i < level ; i++ ) { 266 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 267 starthere += strlen( enclosing[ i ] ) + 1; 268 } 269 sprintf( starthere , EXTFORMAT , "" ); 270 starthere += 1; 271 sprintf( starthere , LABELFORMAT , name ); 272 starthere += strlen( name ) + 1; 273 if ( starthere >= &buffer[ BUFSIZ ] ) { 274 panic( "extlabname" ); 275 } 276 } 277 #endif PC 278