1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)lab.c 1.11 07/16/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 #ifdef PC 24 char extname[ BUFSIZ ]; 25 #endif PC 26 #ifndef PI0 27 register *ll; 28 register struct nl *p, *lp; 29 30 lp = NIL; 31 #else 32 send(REVLAB, r); 33 #endif 34 if ( ! progseen ) { 35 level1(); 36 } 37 line = l; 38 #ifndef PI1 39 if (parts[ cbn ] & (CPRT|TPRT|VPRT|RPRT)){ 40 if ( opt( 's' ) ) { 41 standard(); 42 } else { 43 warning(); 44 } 45 error("Label declarations should precede const, type, var and routine declarations"); 46 } 47 if (parts[ cbn ] & LPRT) { 48 if ( opt( 's' ) ) { 49 standard(); 50 } else { 51 warning(); 52 } 53 error("All labels should be declared in one label part"); 54 } 55 parts[ cbn ] |= LPRT; 56 #endif 57 #ifndef PI0 58 for (ll = r; ll != NIL; ll = ll[2]) { 59 l = getlab(); 60 p = enter(defnl(ll[1], LABEL, 0, l)); 61 /* 62 * Get the label for the eventual target 63 */ 64 p->value[1] = getlab(); 65 p->chain = lp; 66 p->nl_flags |= (NFORWD|NMOD); 67 p->value[NL_GOLEV] = NOTYET; 68 p->entloc = l; 69 lp = p; 70 # ifdef OBJ 71 /* 72 * This operator is between 73 * the bodies of two procedures 74 * and provides a target for 75 * gotos for this label via TRA. 76 */ 77 putlab(l); 78 /* put(2, O_GOTO | cbn<<8+INDX, (long)p->value[1]); */ 79 put(2, O_GOTO | cbn<<8, (long)p->value[1]); 80 # endif OBJ 81 # ifdef PC 82 /* 83 * labels have to be .globl otherwise /lib/c2 may 84 * throw them away if they aren't used in the function 85 * which defines them. 86 */ 87 extlabname( extname , p -> symbol , cbn ); 88 putprintf( " .globl %s" , 0 , extname ); 89 if ( cbn == 1 ) { 90 stabglabel( extname , line ); 91 } 92 # endif PC 93 } 94 gotos[cbn] = lp; 95 # ifdef PTREE 96 { 97 pPointer Labels = LabelDCopy( r ); 98 99 pDEF( PorFHeader[ nesting ] ).PorFLabels = Labels; 100 } 101 # endif PTREE 102 #endif 103 } 104 105 #ifndef PI0 106 /* 107 * Gotoop is called when 108 * we get a statement "goto label" 109 * and generates the needed tra. 110 */ 111 gotoop(s) 112 char *s; 113 { 114 register struct nl *p; 115 #ifdef PC 116 char extname[ BUFSIZ ]; 117 #endif PC 118 119 gocnt++; 120 p = lookup(s); 121 if (p == NIL) 122 return (NIL); 123 # ifdef OBJ 124 put(2, O_TRA4, (long)p->entloc); 125 # endif OBJ 126 # ifdef PC 127 if ( cbn != bn ) { 128 /* 129 * call goto to unwind the stack to the destination level 130 */ 131 putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 132 , "_GOTO" ); 133 putLV( DISPLAYNAME , 0 , bn * sizeof( struct dispsave ) , 134 NGLOBAL , P2PTR | P2INT ); 135 putop( P2CALL , P2INT ); 136 putdot( filename , line ); 137 } 138 extlabname( extname , p -> symbol , bn ); 139 /* 140 * this is a jmp because it's a jump to a global 141 * and the assembler doesn't change jbr's into jmp's 142 * if the destination is a global symbol. 143 */ 144 putprintf( " jmp %s" , 0 , extname ); 145 # endif PC 146 if (bn == cbn) 147 if (p->nl_flags & NFORWD) { 148 if (p->value[NL_GOLEV] == NOTYET) { 149 p->value[NL_GOLEV] = level; 150 p->value[NL_GOLINE] = line; 151 } 152 } else 153 if (p->value[NL_GOLEV] == DEAD) { 154 recovered(); 155 error("Goto %s is into a structured statement", p->symbol); 156 } 157 } 158 159 /* 160 * Labeled is called when a label 161 * definition is encountered, and 162 * marks that it has been found and 163 * patches the associated GOTO generated 164 * by gotoop. 165 */ 166 labeled(s) 167 char *s; 168 { 169 register struct nl *p; 170 #ifdef PC 171 char extname[ BUFSIZ ]; 172 #endif PC 173 174 p = lookup(s); 175 if (p == NIL) 176 return (NIL); 177 if (bn != cbn) { 178 error("Label %s not defined in correct block", s); 179 return; 180 } 181 if ((p->nl_flags & NFORWD) == 0) { 182 error("Label %s redefined", s); 183 return; 184 } 185 p->nl_flags &= ~NFORWD; 186 # ifdef OBJ 187 patch4(p->entloc); 188 # endif OBJ 189 # ifdef PC 190 extlabname( extname , p -> symbol , bn ); 191 putprintf( "%s:" , 0 , extname ); 192 # endif PC 193 if (p->value[NL_GOLEV] != NOTYET) 194 if (p->value[NL_GOLEV] < level) { 195 recovered(); 196 error("Goto %s from line %d is into a structured statement", s, p->value[NL_GOLINE]); 197 } 198 p->value[NL_GOLEV] = level; 199 } 200 #endif 201 202 #ifdef PC 203 /* 204 * construct the long name of a label based on it's static nesting. 205 * into a caller-supplied buffer (that should be about BUFSIZ big). 206 */ 207 extlabname( buffer , name , level ) 208 char buffer[]; 209 char *name; 210 int level; 211 { 212 char *starthere; 213 int i; 214 215 starthere = &buffer[0]; 216 for ( i = 1 ; i < level ; i++ ) { 217 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 218 starthere += strlen( enclosing[ i ] ) + 1; 219 } 220 sprintf( starthere , EXTFORMAT , "" ); 221 starthere += 1; 222 sprintf( starthere , LABELFORMAT , name ); 223 starthere += strlen( name ) + 1; 224 if ( starthere >= &buffer[ BUFSIZ ] ) { 225 panic( "extlabname" ); 226 } 227 } 228 #endif PC 229