| 1 | #ifndef lint |
| 2 | static char sccsid[] = "@(#)local.c 1.1 (Berkeley) %G%"; |
| 3 | #endif |
| 4 | |
| 5 | # include "mfile1" |
| 6 | |
| 7 | /* this file contains code which is dependent on the target machine */ |
| 8 | |
| 9 | NODE * |
| 10 | clocal(p) register NODE *p; { |
| 11 | |
| 12 | /* this is called to do local transformations on |
| 13 | an expression tree preparitory to its being |
| 14 | written out in intermediate code. |
| 15 | */ |
| 16 | |
| 17 | /* the major essential job is rewriting the |
| 18 | automatic variables and arguments in terms of |
| 19 | REG and OREG nodes */ |
| 20 | /* conversion ops which are not necessary are also clobbered here */ |
| 21 | /* in addition, any special features (such as rewriting |
| 22 | exclusive or) are easily handled here as well */ |
| 23 | |
| 24 | register struct symtab *q; |
| 25 | register NODE *r; |
| 26 | register int o; |
| 27 | register int m, ml; |
| 28 | |
| 29 | switch( o = p->in.op ){ |
| 30 | |
| 31 | case NAME: |
| 32 | if( p->tn.rval < 0 ) { /* already processed; ignore... */ |
| 33 | return(p); |
| 34 | } |
| 35 | q = &stab[p->tn.rval]; |
| 36 | switch( q->sclass ){ |
| 37 | |
| 38 | case AUTO: |
| 39 | case PARAM: |
| 40 | /* fake up a structure reference */ |
| 41 | r = block( REG, NIL, NIL, PTR+STRTY, 0, 0 ); |
| 42 | r->tn.lval = 0; |
| 43 | r->tn.rval = STKREG; |
| 44 | p = stref( block( STREF, r, p, 0, 0, 0 ) ); |
| 45 | break; |
| 46 | |
| 47 | case ULABEL: |
| 48 | case LABEL: |
| 49 | case STATIC: |
| 50 | if( q->slevel == 0 ) break; |
| 51 | p->tn.lval = 0; |
| 52 | p->tn.rval = -q->offset; |
| 53 | break; |
| 54 | |
| 55 | case REGISTER: |
| 56 | p->in.op = REG; |
| 57 | p->tn.lval = 0; |
| 58 | p->tn.rval = q->offset; |
| 59 | #ifdef REG_CHAR |
| 60 | m = p->in.type; |
| 61 | if( m==CHAR || m==SHORT ) |
| 62 | p->in.type = INT; |
| 63 | else if( m==UCHAR || m==USHORT ) |
| 64 | p->in.type = UNSIGNED; |
| 65 | #endif |
| 66 | break; |
| 67 | |
| 68 | } |
| 69 | break; |
| 70 | |
| 71 | case LT: |
| 72 | case LE: |
| 73 | case GT: |
| 74 | case GE: |
| 75 | if( ISPTR( p->in.left->in.type ) || ISPTR( p->in.right->in.type ) ){ |
| 76 | p->in.op += (ULT-LT); |
| 77 | } |
| 78 | break; |
| 79 | |
| 80 | case PCONV: |
| 81 | /* do pointer conversions for char and longs */ |
| 82 | ml = p->in.left->in.type; |
| 83 | if( ( ml==CHAR || ml==UCHAR || ml==SHORT || ml==USHORT ) && p->in.left->in.op != ICON ) break; |
| 84 | |
| 85 | /* pointers all have the same representation; the type is inherited */ |
| 86 | |
| 87 | inherit: |
| 88 | p->in.left->in.type = p->in.type; |
| 89 | p->in.left->fn.cdim = p->fn.cdim; |
| 90 | p->in.left->fn.csiz = p->fn.csiz; |
| 91 | p->in.op = FREE; |
| 92 | return( p->in.left ); |
| 93 | |
| 94 | case SCONV: |
| 95 | m = p->in.type; |
| 96 | ml = p->in.left->in.type; |
| 97 | if(m == FLOAT || m == DOUBLE) { |
| 98 | if(p->in.left->in.op==SCONV && |
| 99 | (ml == FLOAT || ml == DOUBLE) && |
| 100 | p->in.left->in.left->in.type==m) { |
| 101 | p->in.op = p->in.left->in.op = FREE; |
| 102 | return(p->in.left->in.left); |
| 103 | } |
| 104 | if(p->in.left->in.op==FCON) |
| 105 | goto inherit; |
| 106 | break; |
| 107 | } |
| 108 | if(ml == FLOAT || ml == DOUBLE){ |
| 109 | if (p->in.left->in.op == FCON){ |
| 110 | p->in.left->in.op = FREE; |
| 111 | p->in.op = ICON; |
| 112 | p->tn.lval = p->in.left->fpn.dval; |
| 113 | p->tn.rval = NONAME; |
| 114 | return(p); |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | /* now, look for conversions downwards */ |
| 119 | |
| 120 | if( p->in.left->in.op == ICON ){ /* simulate the conversion here */ |
| 121 | CONSZ val; |
| 122 | val = p->in.left->tn.lval; |
| 123 | switch( m ){ |
| 124 | case CHAR: |
| 125 | p->in.left->tn.lval = (char) val; |
| 126 | break; |
| 127 | case UCHAR: |
| 128 | p->in.left->tn.lval = val & 0XFF; |
| 129 | break; |
| 130 | case USHORT: |
| 131 | p->in.left->tn.lval = val & 0XFFFFL; |
| 132 | break; |
| 133 | case SHORT: |
| 134 | p->in.left->tn.lval = (short)val; |
| 135 | break; |
| 136 | case UNSIGNED: |
| 137 | p->in.left->tn.lval = val & 0xFFFFFFFFL; |
| 138 | break; |
| 139 | case INT: |
| 140 | p->in.left->tn.lval = (int)val; |
| 141 | break; |
| 142 | } |
| 143 | p->in.left->in.type = m; |
| 144 | } |
| 145 | else { |
| 146 | /* meaningful ones are conversion of int to char, int to short, |
| 147 | and short to char, and unsigned versions thereof */ |
| 148 | if( m==CHAR || m==UCHAR ){ |
| 149 | if( ml!=CHAR && ml!= UCHAR ) break; |
| 150 | } |
| 151 | else if( m==SHORT || m==USHORT ){ |
| 152 | if( ml!=CHAR && ml!=UCHAR && ml!=SHORT && ml!=USHORT ) break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* clobber conversion */ |
| 157 | if( tlen(p) == tlen(p->in.left) ) goto inherit; |
| 158 | p->in.op = FREE; |
| 159 | return( p->in.left ); /* conversion gets clobbered */ |
| 160 | |
| 161 | case QUEST: /* the right side should be COLON */ |
| 162 | if((r = p->in.right)->in.op == SCONV) { |
| 163 | p->in.right = r->in.left; |
| 164 | p->in.type = r->in.left->in.type; |
| 165 | r->in.left = p; |
| 166 | return(r); |
| 167 | } |
| 168 | return(p); |
| 169 | |
| 170 | case PVCONV: |
| 171 | case PMCONV: |
| 172 | if( p->in.right->in.op != ICON ) cerror( "bad conversion", 0); |
| 173 | p->in.op = FREE; |
| 174 | return( buildtree( o==PMCONV?MUL:DIV, p->in.left, p->in.right ) ); |
| 175 | |
| 176 | case FLD: |
| 177 | /* make sure that the second pass does not make the |
| 178 | descendant of a FLD operator into a doubly indexed OREG */ |
| 179 | |
| 180 | if( p->in.left->in.op == UNARY MUL |
| 181 | && (r=p->in.left->in.left)->in.op == PCONV) |
| 182 | if( r->in.left->in.op == PLUS || r->in.left->in.op == MINUS ) |
| 183 | if( ISPTR(r->in.type) ) { |
| 184 | if( ISUNSIGNED(p->in.left->in.type) ) |
| 185 | p->in.left->in.type = UNSIGNED; |
| 186 | else |
| 187 | p->in.left->in.type = INT; |
| 188 | } |
| 189 | break; |
| 190 | case FORTCALL: /* arg must be FLOAT */ |
| 191 | if((r = p->in.right)->in.type != FLOAT) |
| 192 | p->in.right = clocal(makety(r, FLOAT, 0, FLOAT)); |
| 193 | return(p); |
| 194 | } |
| 195 | |
| 196 | /* if both sides are FLOAT, so is the op */ |
| 197 | if(optype(o)!=LTYPE && p->in.left->in.type==DOUBLE && |
| 198 | (o==UNARY MINUS || optype(o)==BITYPE && p->in.right->in.type==DOUBLE)) { |
| 199 | r = p->in.left; |
| 200 | if(r->in.op==SCONV && r->in.left->in.type==FLOAT) { |
| 201 | if(optype(o)==BITYPE) { |
| 202 | r = p->in.right; |
| 203 | if(r->in.op==SCONV && r->in.left->in.type==FLOAT) { |
| 204 | r->in.op = FREE; |
| 205 | p->in.right = r->in.left; |
| 206 | } else if(r->in.op==FCON) |
| 207 | r->in.type = FLOAT; |
| 208 | else |
| 209 | return(p); |
| 210 | } |
| 211 | r = p->in.left; |
| 212 | p->in.left = r->in.left; |
| 213 | } else if(optype(o)==BITYPE && r->in.op==FCON) { |
| 214 | r = p->in.right; |
| 215 | if(!(r->in.op==SCONV && r->in.left->in.type==FLOAT)) |
| 216 | return(p); |
| 217 | p->in.right = r->in.left; |
| 218 | p->in.left->in.type = FLOAT; |
| 219 | } else |
| 220 | return(p); |
| 221 | if(p->in.type==DOUBLE) { |
| 222 | p->in.type = FLOAT; |
| 223 | r->in.left = p; |
| 224 | return(r); |
| 225 | } else { /* usually logop */ |
| 226 | r->in.op = FREE; |
| 227 | return(p); |
| 228 | } |
| 229 | } |
| 230 | return(p); |
| 231 | } |
| 232 | |
| 233 | andable( p ) NODE *p; { |
| 234 | return(1); /* all names can have & taken on them */ |
| 235 | } |
| 236 | |
| 237 | cendarg(){ /* at the end of the arguments of a ftn, set the automatic offset */ |
| 238 | autooff = AUTOINIT; |
| 239 | } |
| 240 | |
| 241 | cisreg( t ) TWORD t; { /* is an automatic variable of type t OK for a register variable */ |
| 242 | |
| 243 | if( t==INT || t==UNSIGNED || t==LONG || t==ULONG /* tbl */ |
| 244 | #ifdef REG_CHAR |
| 245 | || t==CHAR || t==UCHAR || t==SHORT || t==USHORT /* tbl */ |
| 246 | #endif |
| 247 | || ISPTR(t)) return (1); /* wnj */ |
| 248 | return(0); |
| 249 | } |
| 250 | |
| 251 | NODE * |
| 252 | offcon( off, t, d, s ) OFFSZ off; TWORD t; { |
| 253 | |
| 254 | /* return a node, for structure references, which is suitable for |
| 255 | being added to a pointer of type t, in order to be off bits offset |
| 256 | into a structure */ |
| 257 | |
| 258 | register NODE *p; |
| 259 | |
| 260 | /* t, d, and s are the type, dimension offset, and sizeoffset */ |
| 261 | /* in general they are necessary for offcon, but not on H'well */ |
| 262 | |
| 263 | p = bcon(0); |
| 264 | p->tn.lval = off/SZCHAR; |
| 265 | return(p); |
| 266 | |
| 267 | } |
| 268 | |
| 269 | |
| 270 | static inwd /* current bit offsed in word */; |
| 271 | static CONSZ word /* word being built from fields */; |
| 272 | |
| 273 | incode( p, sz ) register NODE *p; { |
| 274 | |
| 275 | /* generate initialization code for assigning a constant c |
| 276 | to a field of width sz */ |
| 277 | /* we assume that the proper alignment has been obtained */ |
| 278 | /* inoff is updated to have the proper final value */ |
| 279 | /* we also assume sz < SZINT */ |
| 280 | |
| 281 | inwd += sz; |
| 282 | if(inwd > SZINT) cerror("incode: field > int"); |
| 283 | word |= (p->tn.lval&((1L<<sz)-1)) << (SZINT-inwd); |
| 284 | inoff += sz; |
| 285 | if(inoff%SZINT == 0) { |
| 286 | printf( " .long 0x%08X\n", word); |
| 287 | word = inwd = 0; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | # ifdef PRTDCON |
| 292 | prtdcon( p ) register NODE *p; { |
| 293 | int i; |
| 294 | |
| 295 | if( p->in.op == FCON ){ |
| 296 | if(p->fpn.dval == 0) { |
| 297 | p->in.op = ICON; |
| 298 | p->tn.rval = NONAME; |
| 299 | return; |
| 300 | } |
| 301 | locctr( DATA ); |
| 302 | defalign( ALDOUBLE ); |
| 303 | deflab( i = getlab() ); |
| 304 | # ifndef SFCON |
| 305 | fincode( p->fpn.dval, p->in.type==DOUBLE ? SZDOUBLE : SZFLOAT); |
| 306 | # else |
| 307 | p->in.type = fincode( p->fpn.dval, 0 ); |
| 308 | # endif |
| 309 | p->tn.lval = 0; |
| 310 | p->tn.rval = -i; |
| 311 | p->in.op = NAME; |
| 312 | } |
| 313 | } |
| 314 | # endif |
| 315 | |
| 316 | fincode( d, sz ) double d; register int sz; { |
| 317 | /* |
| 318 | * output code to initialize space of size sz to the value d |
| 319 | * the proper alignment has been obtained |
| 320 | * inoff is updated to have the proper final value |
| 321 | * this code should be the same for PDP, VAX and Tahoe |
| 322 | * SFCON makes use of value to determine type - only where |
| 323 | * float<->double conversions are ignored. |
| 324 | */ |
| 325 | |
| 326 | register struct sh4 { |
| 327 | unsigned short sh[4]; |
| 328 | } *x; |
| 329 | # ifdef SFCON |
| 330 | register int type; |
| 331 | # else |
| 332 | float f; |
| 333 | |
| 334 | if(sz == SZFLOAT) { /* force rounding */ |
| 335 | f = d; |
| 336 | d = f; |
| 337 | } |
| 338 | # endif |
| 339 | |
| 340 | x = (struct sh4 *)&d; |
| 341 | printf(" .long 0x%04x%04x", x->sh[0], x->sh[1]); |
| 342 | # ifdef SFCON |
| 343 | if(sz==0) |
| 344 | if(x->sh[2]==0 && x->sh[3]==0) { |
| 345 | sz = SZFLOAT; |
| 346 | type = FLOAT; |
| 347 | } else { |
| 348 | sz = SZDOUBLE; |
| 349 | type = DOUBLE; |
| 350 | } |
| 351 | # endif |
| 352 | if(sz == SZDOUBLE) { |
| 353 | printf(", 0x%04x%04x", x->sh[2], x->sh[3]); |
| 354 | printf(" # .double %.17g\n", d); |
| 355 | } else |
| 356 | printf(" # .float %.8g\n", d); |
| 357 | inoff += sz; |
| 358 | # ifdef SFCON |
| 359 | return type; |
| 360 | # endif |
| 361 | } |
| 362 | |
| 363 | cinit( p, sz ) NODE *p; { |
| 364 | /* arrange for the initialization of p into a space of |
| 365 | size sz */ |
| 366 | /* the proper alignment has been opbtained */ |
| 367 | /* inoff is updated to have the proper final value */ |
| 368 | ecode( p ); |
| 369 | inoff += sz; |
| 370 | } |
| 371 | |
| 372 | vfdzero( n ){ /* define n bits of zeros in a vfd */ |
| 373 | |
| 374 | if( n <= 0 ) return; |
| 375 | |
| 376 | inwd += n; |
| 377 | inoff += n; |
| 378 | if( inoff%ALINT ==0 ) { |
| 379 | printf( " .long 0x%08X\n", word ); |
| 380 | word = inwd = 0; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | char * |
| 385 | exname( p ) char *p; { |
| 386 | /* make a name look like an external name in the local machine */ |
| 387 | |
| 388 | #ifndef FLEXNAMES |
| 389 | static char text[NCHNAM+1]; |
| 390 | #else |
| 391 | static char text[BUFSIZ+1]; |
| 392 | #endif |
| 393 | |
| 394 | register int i; |
| 395 | |
| 396 | text[0] = '_'; |
| 397 | #ifndef FLEXNAMES |
| 398 | for( i=1; *p&&i<NCHNAM; ++i ){ |
| 399 | #else |
| 400 | for( i=1; *p; ++i ){ |
| 401 | #endif |
| 402 | text[i] = *p++; |
| 403 | } |
| 404 | |
| 405 | text[i] = '\0'; |
| 406 | #ifndef FLEXNAMES |
| 407 | text[NCHNAM] = '\0'; /* truncate */ |
| 408 | #endif |
| 409 | |
| 410 | return( text ); |
| 411 | } |
| 412 | |
| 413 | ctype( type )TWORD type;{ /* map types which are not defined on the local machine */ |
| 414 | switch( BTYPE(type) ){ |
| 415 | |
| 416 | case LONG: |
| 417 | MODTYPE(type,INT); |
| 418 | break; |
| 419 | |
| 420 | case ULONG: |
| 421 | MODTYPE(type,UNSIGNED); |
| 422 | } |
| 423 | return( type ); |
| 424 | } |
| 425 | |
| 426 | noinit() { /* curid is a variable which is defined but |
| 427 | is not initialized (and not a function ); |
| 428 | This routine returns the stroage class for an uninitialized declaration */ |
| 429 | |
| 430 | return(EXTERN); |
| 431 | |
| 432 | } |
| 433 | |
| 434 | commdec( id ){ /* make a common declaration for id, if reasonable */ |
| 435 | register struct symtab *q; |
| 436 | OFFSZ off, tsize(); |
| 437 | |
| 438 | q = &stab[id]; |
| 439 | printf( " .comm %s,", exname( q->sname ) ); |
| 440 | off = tsize( q->stype, q->dimoff, q->sizoff ); |
| 441 | printf( "%d" /*CONFMT*/, off/SZCHAR ); |
| 442 | printf( "\n" ); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | isitfloat( s ) char *s; { |
| 447 | double atof(); |
| 448 | dcon = atof(s); |
| 449 | return( FCON ); |
| 450 | } |
| 451 | |
| 452 | ecode( p ) NODE *p; { |
| 453 | |
| 454 | /* walk the tree and write out the nodes.. */ |
| 455 | |
| 456 | if( nerrors ) return; |
| 457 | p2tree( p ); |
| 458 | p2compile( p ); |
| 459 | } |
| 460 | |
| 461 | #ifndef ONEPASS |
| 462 | tlen(p) NODE *p; |
| 463 | { |
| 464 | switch(p->in.type) { |
| 465 | case CHAR: |
| 466 | case UCHAR: |
| 467 | return(1); |
| 468 | |
| 469 | case SHORT: |
| 470 | case USHORT: |
| 471 | return(2); |
| 472 | |
| 473 | case DOUBLE: |
| 474 | return(8); |
| 475 | |
| 476 | default: |
| 477 | return(4); |
| 478 | } |
| 479 | } |
| 480 | #endif |