1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)lab.c 1.15 11/11/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 putprintf( " jbr %s" , 0 , extname ); 142 } else { 143 /* 144 * Non-local goto. 145 * 146 * Close all active files between top of stack and 147 * frame at the destination level. Then call longjmp 148 * to unwind the stack to the destination level. 149 * 150 * For nested routines the end is calculated as: 151 * __disply[ bn ] . ap + sizeof( local frame ) 152 * The size of the local frame is dumped out by 153 * the second pass as an assembler constant. 154 * The main routine may not be compiled in this 155 * module, so its size may not be available. 156 * However all of its variables will be globally 157 * declared, so only the known runtime temporaries 158 * will be in its stack frame. 159 */ 160 parts[ bn ] |= NONLOCALGOTO; 161 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 162 , "_PCLOSE" ); 163 if ( bn > 1 ) { 164 p = lookup( enclosing[ bn - 1 ] ); 165 sprintf( extname, "LF%d+%d", p -> value[ NL_ENTLOC ] 166 , sizeof( int ) ); 167 p = lookup(s); 168 putLV( extname , bn , 0 , NNLOCAL , P2PTR | P2CHAR ); 169 } else { 170 putLV( 0 , bn , -( DPOFF1 + sizeof( int ) ) , LOCALVAR , 171 P2PTR | P2CHAR ); 172 } 173 putop( P2CALL , P2INT ); 174 putdot( filename , line ); 175 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 176 , "_longjmp" ); 177 putLV( 0 , bn , GOTOENVOFFSET , NLOCAL , P2PTR|P2STRTY ); 178 extlabname( extname , p -> symbol , bn ); 179 putLV( extname , 0 , 0 , NGLOBAL , P2PTR|P2STRTY ); 180 putop( P2LISTOP , P2INT ); 181 putop( P2CALL , P2INT ); 182 putdot( filename , line ); 183 } 184 # endif PC 185 if (bn == cbn) 186 if (p->nl_flags & NFORWD) { 187 if (p->value[NL_GOLEV] == NOTYET) { 188 p->value[NL_GOLEV] = level; 189 p->value[NL_GOLINE] = line; 190 } 191 } else 192 if (p->value[NL_GOLEV] == DEAD) { 193 recovered(); 194 error("Goto %s is into a structured statement", p->symbol); 195 } 196 } 197 198 /* 199 * Labeled is called when a label 200 * definition is encountered, and 201 * marks that it has been found and 202 * patches the associated GOTO generated 203 * by gotoop. 204 */ 205 labeled(s) 206 char *s; 207 { 208 register struct nl *p; 209 #ifdef PC 210 char extname[ BUFSIZ ]; 211 #endif PC 212 213 p = lookup(s); 214 if (p == NIL) 215 return (NIL); 216 if (bn != cbn) { 217 error("Label %s not defined in correct block", s); 218 return; 219 } 220 if ((p->nl_flags & NFORWD) == 0) { 221 error("Label %s redefined", s); 222 return; 223 } 224 p->nl_flags &= ~NFORWD; 225 # ifdef OBJ 226 patch4(p->value[NL_ENTLOC]); 227 # endif OBJ 228 # ifdef PC 229 extlabname( extname , p -> symbol , bn ); 230 putprintf( "%s:" , 0 , extname ); 231 # endif PC 232 if (p->value[NL_GOLEV] != NOTYET) 233 if (p->value[NL_GOLEV] < level) { 234 recovered(); 235 error("Goto %s from line %d is into a structured statement", s, p->value[NL_GOLINE]); 236 } 237 p->value[NL_GOLEV] = level; 238 } 239 #endif 240 241 #ifdef PC 242 /* 243 * construct the long name of a label based on it's static nesting. 244 * into a caller-supplied buffer (that should be about BUFSIZ big). 245 */ 246 extlabname( buffer , name , level ) 247 char buffer[]; 248 char *name; 249 int level; 250 { 251 char *starthere; 252 int i; 253 254 starthere = &buffer[0]; 255 for ( i = 1 ; i < level ; i++ ) { 256 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 257 starthere += strlen( enclosing[ i ] ) + 1; 258 } 259 sprintf( starthere , EXTFORMAT , "" ); 260 starthere += 1; 261 sprintf( starthere , LABELFORMAT , name ); 262 starthere += strlen( name ) + 1; 263 if ( starthere >= &buffer[ BUFSIZ ] ) { 264 panic( "extlabname" ); 265 } 266 } 267 #endif PC 268