1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)lab.c 1.10 11/13/81"; 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 putprintf( " jbr %s" , 0 , extname ); 140 # endif PC 141 if (bn == cbn) 142 if (p->nl_flags & NFORWD) { 143 if (p->value[NL_GOLEV] == NOTYET) { 144 p->value[NL_GOLEV] = level; 145 p->value[NL_GOLINE] = line; 146 } 147 } else 148 if (p->value[NL_GOLEV] == DEAD) { 149 recovered(); 150 error("Goto %s is into a structured statement", p->symbol); 151 } 152 } 153 154 /* 155 * Labeled is called when a label 156 * definition is encountered, and 157 * marks that it has been found and 158 * patches the associated GOTO generated 159 * by gotoop. 160 */ 161 labeled(s) 162 char *s; 163 { 164 register struct nl *p; 165 #ifdef PC 166 char extname[ BUFSIZ ]; 167 #endif PC 168 169 p = lookup(s); 170 if (p == NIL) 171 return (NIL); 172 if (bn != cbn) { 173 error("Label %s not defined in correct block", s); 174 return; 175 } 176 if ((p->nl_flags & NFORWD) == 0) { 177 error("Label %s redefined", s); 178 return; 179 } 180 p->nl_flags &= ~NFORWD; 181 # ifdef OBJ 182 patch4(p->entloc); 183 # endif OBJ 184 # ifdef PC 185 extlabname( extname , p -> symbol , bn ); 186 putprintf( "%s:" , 0 , extname ); 187 # endif PC 188 if (p->value[NL_GOLEV] != NOTYET) 189 if (p->value[NL_GOLEV] < level) { 190 recovered(); 191 error("Goto %s from line %d is into a structured statement", s, p->value[NL_GOLINE]); 192 } 193 p->value[NL_GOLEV] = level; 194 } 195 #endif 196 197 #ifdef PC 198 /* 199 * construct the long name of a label based on it's static nesting. 200 * into a caller-supplied buffer (that should be about BUFSIZ big). 201 */ 202 extlabname( buffer , name , level ) 203 char buffer[]; 204 char *name; 205 int level; 206 { 207 char *starthere; 208 int i; 209 210 starthere = &buffer[0]; 211 for ( i = 1 ; i < level ; i++ ) { 212 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 213 starthere += strlen( enclosing[ i ] ) + 1; 214 } 215 sprintf( starthere , EXTFORMAT , "" ); 216 starthere += 1; 217 sprintf( starthere , LABELFORMAT , name ); 218 starthere += strlen( name ) + 1; 219 if ( starthere >= &buffer[ BUFSIZ ] ) { 220 panic( "extlabname" ); 221 } 222 } 223 #endif PC 224