1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)lab.c 1.13 08/29/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+INDX, (long)p->value[1]); */ 89 put(2, O_GOTO | cbn<<8, (long)p->value[1]); 90 # endif OBJ 91 # ifdef PC 92 /* 93 * labels have to be .globl otherwise /lib/c2 may 94 * throw them away if they aren't used in the function 95 * which defines them. 96 */ 97 extlabname( extname , p -> symbol , cbn ); 98 putprintf( " .globl %s" , 0 , extname ); 99 if ( cbn == 1 ) { 100 stabglabel( extname , line ); 101 } 102 # endif PC 103 } 104 gotos[cbn] = lp; 105 # ifdef PTREE 106 { 107 pPointer Labels = LabelDCopy( r ); 108 109 pDEF( PorFHeader[ nesting ] ).PorFLabels = Labels; 110 } 111 # endif PTREE 112 #endif 113 } 114 115 #ifndef PI0 116 /* 117 * Gotoop is called when 118 * we get a statement "goto label" 119 * and generates the needed tra. 120 */ 121 gotoop(s) 122 char *s; 123 { 124 register struct nl *p; 125 #ifdef PC 126 char extname[ BUFSIZ ]; 127 #endif PC 128 129 gocnt++; 130 p = lookup(s); 131 if (p == NIL) 132 return (NIL); 133 # ifdef OBJ 134 put(2, O_TRA4, (long)p->value[NL_ENTLOC]); 135 # endif OBJ 136 # ifdef PC 137 if ( cbn != bn ) { 138 /* 139 * call goto to unwind the stack to the destination level 140 */ 141 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 142 , "_GOTO" ); 143 putLV( DISPLAYNAME , 0 , bn * sizeof( struct dispsave ) , 144 NGLOBAL , P2PTR | P2INT ); 145 putop( P2CALL , P2INT ); 146 putdot( filename , line ); 147 } 148 extlabname( extname , p -> symbol , bn ); 149 /* 150 * this is a jmp because it's a jump to a global 151 * and the assembler doesn't change jbr's into jmp's 152 * if the destination is a global symbol. 153 */ 154 putprintf( " jmp %s" , 0 , extname ); 155 # endif PC 156 if (bn == cbn) 157 if (p->nl_flags & NFORWD) { 158 if (p->value[NL_GOLEV] == NOTYET) { 159 p->value[NL_GOLEV] = level; 160 p->value[NL_GOLINE] = line; 161 } 162 } else 163 if (p->value[NL_GOLEV] == DEAD) { 164 recovered(); 165 error("Goto %s is into a structured statement", p->symbol); 166 } 167 } 168 169 /* 170 * Labeled is called when a label 171 * definition is encountered, and 172 * marks that it has been found and 173 * patches the associated GOTO generated 174 * by gotoop. 175 */ 176 labeled(s) 177 char *s; 178 { 179 register struct nl *p; 180 #ifdef PC 181 char extname[ BUFSIZ ]; 182 #endif PC 183 184 p = lookup(s); 185 if (p == NIL) 186 return (NIL); 187 if (bn != cbn) { 188 error("Label %s not defined in correct block", s); 189 return; 190 } 191 if ((p->nl_flags & NFORWD) == 0) { 192 error("Label %s redefined", s); 193 return; 194 } 195 p->nl_flags &= ~NFORWD; 196 # ifdef OBJ 197 patch4(p->value[NL_ENTLOC]); 198 # endif OBJ 199 # ifdef PC 200 extlabname( extname , p -> symbol , bn ); 201 putprintf( "%s:" , 0 , extname ); 202 # endif PC 203 if (p->value[NL_GOLEV] != NOTYET) 204 if (p->value[NL_GOLEV] < level) { 205 recovered(); 206 error("Goto %s from line %d is into a structured statement", s, p->value[NL_GOLINE]); 207 } 208 p->value[NL_GOLEV] = level; 209 } 210 #endif 211 212 #ifdef PC 213 /* 214 * construct the long name of a label based on it's static nesting. 215 * into a caller-supplied buffer (that should be about BUFSIZ big). 216 */ 217 extlabname( buffer , name , level ) 218 char buffer[]; 219 char *name; 220 int level; 221 { 222 char *starthere; 223 int i; 224 225 starthere = &buffer[0]; 226 for ( i = 1 ; i < level ; i++ ) { 227 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 228 starthere += strlen( enclosing[ i ] ) + 1; 229 } 230 sprintf( starthere , EXTFORMAT , "" ); 231 starthere += 1; 232 sprintf( starthere , LABELFORMAT , name ); 233 starthere += strlen( name ) + 1; 234 if ( starthere >= &buffer[ BUFSIZ ] ) { 235 panic( "extlabname" ); 236 } 237 } 238 #endif PC 239