| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
# include "pass1.h" |
| 37 |
|
| 38 |
#ifndef LANG_CXX |
| 39 |
#define NODE P1ND |
| 40 |
#undef NIL |
| 41 |
#define NIL NULL |
| 42 |
#define tfree p1tfree |
| 43 |
#define talloc p1alloc |
| 44 |
#define ccopy p1tcopy |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
void |
| 51 |
setseg(int seg, char *name) |
| 52 |
{ |
| 53 |
switch (seg) { |
| 54 |
case PROG: name = ".text"; break; |
| 55 |
|
| 56 |
case DATA: |
| 57 |
case LDATA: name = ".data"; break; |
| 58 |
|
| 59 |
case STRNG: |
| 60 |
case RDATA: name = ".section .rodata"; break; |
| 61 |
|
| 62 |
case UDATA: break; |
| 63 |
|
| 64 |
case DTORS: |
| 65 |
name = ".section .dtors,\"aw\",@progbits"; |
| 66 |
break; |
| 67 |
case CTORS: |
| 68 |
name = ".section .ctors,\"aw\",@progbits"; |
| 69 |
break; |
| 70 |
|
| 71 |
case TLSDATA: |
| 72 |
case TLSUDATA: |
| 73 |
uerror("FIXME: unsupported segment %d", seg); |
| 74 |
break; |
| 75 |
|
| 76 |
case PICRDATA: |
| 77 |
name = ".section .data.rel.ro.local,\"aw\",@progbits"; |
| 78 |
break; |
| 79 |
|
| 80 |
case PICDATA: |
| 81 |
name = ".section .data.rel,\"aw\",@progbits"; |
| 82 |
break; |
| 83 |
case PICLDATA: |
| 84 |
name = ".section .data.rel.local,\"aw\",@progbits"; |
| 85 |
break; |
| 86 |
|
| 87 |
case NMSEG: |
| 88 |
printf("\t.section %s,\"a%c\",@progbits\n", name, |
| 89 |
cftnsp ? 'x' : 'w'); |
| 90 |
return; |
| 91 |
} |
| 92 |
printf("\t%s\n", name); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void |
| 100 |
defloc(struct symtab *sp) |
| 101 |
{ |
| 102 |
char *name; |
| 103 |
|
| 104 |
name = getexname(sp); |
| 105 |
|
| 106 |
if (sp->sclass == EXTDEF) { |
| 107 |
printf("\t.globl %s\n", name); |
| 108 |
if (ISFTN(sp->stype)) { |
| 109 |
printf("\t.type %s,@function\n", name); |
| 110 |
} else { |
| 111 |
printf("\t.type %s,@object\n", name); |
| 112 |
printf("\t.size %s,%d\n", name, |
| 113 |
(int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR); |
| 114 |
} |
| 115 |
} |
| 116 |
if (sp->slevel == 0) |
| 117 |
printf("%s:\n", name); |
| 118 |
else |
| 119 |
printf(LABFMT ":\n", sp->soffset); |
| 120 |
} |
| 121 |
|
| 122 |
static int strtemp; |
| 123 |
|
| 124 |
void |
| 125 |
efcode(void) |
| 126 |
{ |
| 127 |
TWORD t; |
| 128 |
NODE *p, *q; |
| 129 |
|
| 130 |
|
| 131 |
if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN) |
| 132 |
return; |
| 133 |
|
| 134 |
t = PTR+BTYPE(cftnsp->stype); |
| 135 |
|
| 136 |
q = tempnode(strtemp, t, 0, cftnsp->sap); |
| 137 |
q = buildtree(UMUL, q, NIL); |
| 138 |
p = block(REG, NIL, NIL, t, 0, cftnsp->sap); |
| 139 |
regno(p) = R0; |
| 140 |
p = buildtree(UMUL, p, NIL); |
| 141 |
p = buildtree(ASSIGN, q, p); |
| 142 |
ecomp(p); |
| 143 |
|
| 144 |
|
| 145 |
q = tempnode(strtemp, INT, 0, 0); |
| 146 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 147 |
regno(p) = R0; |
| 148 |
ecomp(buildtree(ASSIGN, p, q)); |
| 149 |
} |
| 150 |
|
| 151 |
void |
| 152 |
bfcode(struct symtab **sp, int n) |
| 153 |
{ |
| 154 |
struct symtab *sp2; |
| 155 |
NODE *p, *q; |
| 156 |
int i, argbase, sz; |
| 157 |
|
| 158 |
if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) { |
| 159 |
|
| 160 |
p = tempnode(0, INT, 0, 0); |
| 161 |
strtemp = regno(p); |
| 162 |
q = block(REG, 0, 0, INT, 0, 0); |
| 163 |
regno(q) = R1; |
| 164 |
ecomp(buildtree(ASSIGN, p, q)); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
argbase = ARGINIT; |
| 169 |
for (i = 0; i < n; i++) { |
| 170 |
sp2 = sp[i]; |
| 171 |
sz = tsize(sp2->stype, sp2->sdf, sp2->sap); |
| 172 |
|
| 173 |
SETOFF(sz, SZINT); |
| 174 |
sp2->soffset = argbase; |
| 175 |
argbase += sz; |
| 176 |
} |
| 177 |
|
| 178 |
if (xtemps == 0) |
| 179 |
return; |
| 180 |
|
| 181 |
|
| 182 |
for (i = 0; i < n; i++) { |
| 183 |
if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY || |
| 184 |
cisreg(sp[i]->stype) == 0) |
| 185 |
continue; |
| 186 |
if (cqual(sp[i]->stype, sp[i]->squal) & VOL) |
| 187 |
continue; |
| 188 |
sp2 = sp[i]; |
| 189 |
p = tempnode(0, sp[i]->stype, sp[i]->sdf, sp[i]->sap); |
| 190 |
p = buildtree(ASSIGN, p, nametree(sp2)); |
| 191 |
sp[i]->soffset = regno(p->n_left); |
| 192 |
sp[i]->sflags |= STNODE; |
| 193 |
ecomp(p); |
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
void |
| 199 |
ejobcode(int flag) |
| 200 |
{ |
| 201 |
|
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
void |
| 206 |
bjobcode(void) |
| 207 |
{ |
| 208 |
astypnames[INT] = astypnames[UNSIGNED] = "\t.long"; |
| 209 |
astypnames[SHORT] = astypnames[USHORT] = "\t.word"; |
| 210 |
} |
| 211 |
|
| 212 |
#if 0 |
| 213 |
aobeg(void) |
| 214 |
{ |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
aocode(struct symtab *p) |
| 219 |
{ |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
aoend(void) |
| 224 |
{ |
| 225 |
|
| 226 |
} |
| 227 |
#endif |
| 228 |
|
| 229 |
void |
| 230 |
fldty(struct symtab *p) |
| 231 |
{ |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
int |
| 239 |
mygenswitch(int num, TWORD type, struct swents **p, int n) |
| 240 |
{ |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
#ifdef notyet |
| 245 |
struct sw heapsw[SWITSZ]; |
| 246 |
|
| 247 |
genswitch(register struct sw *p, int n) |
| 248 |
{ |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
register i; |
| 256 |
register CONSZ j, range; |
| 257 |
register dlab, swlab; |
| 258 |
|
| 259 |
range = p[n].sval-p[1].sval; |
| 260 |
|
| 261 |
if( range>0 && range <= 3*n && n>=4 ){ |
| 262 |
|
| 263 |
swlab = getlab(); |
| 264 |
dlab = p->slab >= 0 ? p->slab : getlab(); |
| 265 |
|
| 266 |
|
| 267 |
printf(" casel r0,$%ld,$%ld\n", p[1].sval, range); |
| 268 |
deflab1(swlab); |
| 269 |
for( i=1,j=p[1].sval; i<=n; j++) { |
| 270 |
printf(" .word " LABFMT "-" LABFMT "\n", |
| 271 |
(j == p[i].sval ? ((j=p[i++].sval), p[i-1].slab) : dlab), |
| 272 |
swlab); |
| 273 |
} |
| 274 |
|
| 275 |
if( p->slab >= 0 ) branch( dlab ); |
| 276 |
else deflab1(dlab); |
| 277 |
return; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
if( n>8 ) { |
| 282 |
|
| 283 |
heapsw[0].slab = dlab = p->slab >= 0 ? p->slab : getlab(); |
| 284 |
makeheap(p, n, 1); |
| 285 |
|
| 286 |
walkheap(1, n); |
| 287 |
|
| 288 |
if( p->slab >= 0 ) |
| 289 |
branch( dlab ); |
| 290 |
else |
| 291 |
deflab1(dlab); |
| 292 |
return; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
for( i=1; i<=n; ++i ){ |
| 304 |
|
| 305 |
|
| 306 |
printf( " cmpl r0,$" ); |
| 307 |
printf( CONFMT, p[i].sval ); |
| 308 |
printf( "\n jeql " LBLFMT "\n", p[i].slab ); |
| 309 |
} |
| 310 |
|
| 311 |
if( p->slab>=0 ) branch( p->slab ); |
| 312 |
} |
| 313 |
|
| 314 |
makeheap(register struct sw *p, int m, int n) |
| 315 |
{ |
| 316 |
register int q; |
| 317 |
|
| 318 |
q = select(m); |
| 319 |
heapsw[n] = p[q]; |
| 320 |
if( q>1 ) makeheap(p, q-1, 2*n); |
| 321 |
if( q<m ) makeheap(p+q, m-q, 2*n+1); |
| 322 |
} |
| 323 |
|
| 324 |
select(int m) |
| 325 |
{ |
| 326 |
register int l,i,k; |
| 327 |
|
| 328 |
for(i=1; ; i*=2) |
| 329 |
if( (i-1) > m ) break; |
| 330 |
l = ((k = i/2 - 1) + 1)/2; |
| 331 |
return( l + (m-k < l ? m-k : l)); |
| 332 |
} |
| 333 |
|
| 334 |
walkheap(int start, int limit) |
| 335 |
{ |
| 336 |
int label; |
| 337 |
|
| 338 |
if( start > limit ) return; |
| 339 |
printf(" cmpl r0,$%d\n", heapsw[start].sval); |
| 340 |
printf(" jeql " LBLFMT "\n", heapsw[start].slab); |
| 341 |
if( (2*start) > limit ) { |
| 342 |
printf(" jbr " LBLFMT "\n", heapsw[0].slab); |
| 343 |
return; |
| 344 |
} |
| 345 |
if( (2*start+1) <= limit ) { |
| 346 |
label = getlab(); |
| 347 |
printf(" jgtr " LBLFMT "\n", label); |
| 348 |
} else |
| 349 |
printf(" jgtr " LBLFMT "\n", heapsw[0].slab); |
| 350 |
walkheap( 2*start, limit); |
| 351 |
if( (2*start+1) <= limit ) { |
| 352 |
deflab1(label); |
| 353 |
walkheap( 2*start+1, limit); |
| 354 |
} |
| 355 |
} |
| 356 |
#endif |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
NODE * |
| 363 |
funcode(NODE *p) |
| 364 |
{ |
| 365 |
NODE *r, *l; |
| 366 |
|
| 367 |
|
| 368 |
for (r = p->n_right; r->n_op == CM; r = r->n_left) { |
| 369 |
if (r->n_right->n_op != STARG) { |
| 370 |
r->n_right = intprom(r->n_right); |
| 371 |
r->n_right = block(FUNARG, r->n_right, NIL, |
| 372 |
r->n_right->n_type, r->n_right->n_df, |
| 373 |
r->n_right->n_ap); |
| 374 |
} |
| 375 |
} |
| 376 |
if (r->n_op != STARG) { |
| 377 |
l = talloc(); |
| 378 |
*l = *r; |
| 379 |
r->n_op = FUNARG; |
| 380 |
r->n_left = l; |
| 381 |
r->n_left = intprom(r->n_left); |
| 382 |
r->n_type = r->n_left->n_type; |
| 383 |
} |
| 384 |
return p; |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
NODE * |
| 391 |
builtin_ffs(const struct bitable *bt, NODE *a) |
| 392 |
{ |
| 393 |
NODE *p, *q, *r; |
| 394 |
|
| 395 |
p = tempnode(0, bt->rt, 0, 0); |
| 396 |
r = block(XARG, ccopy(p), NIL, INT, 0, 0); |
| 397 |
r->n_name = "=&r"; |
| 398 |
q = block(XARG, a, NIL, INT, 0, 0); |
| 399 |
q->n_name = "g"; |
| 400 |
q = block(CM, r, q, INT, 0, 0); |
| 401 |
q = block(XASM, q, block(ICON, 0, 0, STRTY, 0, 0), INT, 0, 0); |
| 402 |
q->n_name = "ffs $0,$32,%1,%0;bneq 1f;mnegl $1,%0;1:;incl %0"; |
| 403 |
p = block(COMOP, q, p, bt->rt, 0, 0); |
| 404 |
return p; |
| 405 |
} |
| 406 |
|
| 407 |
NODE * |
| 408 |
builtin_ffsl(const struct bitable *bt, NODE *a) |
| 409 |
{ |
| 410 |
return builtin_ffs(bt, a); |
| 411 |
} |
| 412 |
|
| 413 |
NODE * |
| 414 |
builtin_ffsll(const struct bitable *bt, NODE *a) |
| 415 |
{ |
| 416 |
cerror("builtin_ffsll unimplemented"); |
| 417 |
return NIL; |
| 418 |
} |
| 419 |
|
| 420 |
NODE * |
| 421 |
builtin_return_address(const struct bitable *bt, NODE *a) |
| 422 |
{ |
| 423 |
NODE *f; |
| 424 |
int v; |
| 425 |
|
| 426 |
if (a->n_op != ICON) |
| 427 |
goto bad; |
| 428 |
v = glval(a); |
| 429 |
tfree(a); |
| 430 |
|
| 431 |
if (v != 0) { |
| 432 |
werror("unsupported argument"); |
| 433 |
return xbcon(0, NULL, VOID|PTR); |
| 434 |
} |
| 435 |
|
| 436 |
f = block(REG, NIL, NIL, INCREF(PTR+CHAR), 0, 0); |
| 437 |
regno(f) = FPREG; |
| 438 |
f = block(UMUL, |
| 439 |
block(PLUS, f, |
| 440 |
bcon(16), INCREF(PTR+CHAR), 0, 0), NIL, PTR+CHAR, 0, 0); |
| 441 |
f = makety(f, PTR+VOID, 0, 0, 0); |
| 442 |
|
| 443 |
return f; |
| 444 |
bad: |
| 445 |
uerror("bad argument to __builtin_return_address"); |
| 446 |
return bcon(0); |
| 447 |
} |
| 448 |
|
| 449 |
NODE * |
| 450 |
builtin_frame_address(const struct bitable *bt, NODE *a) |
| 451 |
{ |
| 452 |
int nframes; |
| 453 |
NODE *f; |
| 454 |
|
| 455 |
if (a->n_op != ICON) |
| 456 |
goto bad; |
| 457 |
|
| 458 |
nframes = glval(a); |
| 459 |
|
| 460 |
tfree(a); |
| 461 |
|
| 462 |
f = block(REG, NIL, NIL, PTR+CHAR, 0, 0); |
| 463 |
regno(f) = FPREG; |
| 464 |
|
| 465 |
while (nframes--) { |
| 466 |
f = block(UMUL, |
| 467 |
block(PLUS, f, |
| 468 |
bcon(12), INCREF(PTR+CHAR), 0, 0), |
| 469 |
NIL, PTR+CHAR, 0, 0); |
| 470 |
f = makety(f, PTR+CHAR, 0, 0, 0); |
| 471 |
} |
| 472 |
|
| 473 |
return f; |
| 474 |
bad: |
| 475 |
uerror("bad argument to __builtin_frame_address"); |
| 476 |
return bcon(0); |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
NODE * |
| 483 |
builtin_cfa(const struct bitable *bt, NODE *a) |
| 484 |
{ |
| 485 |
uerror("missing builtin_cfa"); |
| 486 |
return bcon(0); |
| 487 |
} |
| 488 |
|