| 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 |
#include <assert.h> |
| 30 |
#include <stdlib.h> |
| 31 |
|
| 32 |
#include "pass1.h" |
| 33 |
#include "pass2.h" |
| 34 |
|
| 35 |
#ifdef LANG_CXX |
| 36 |
#define p1listf listf |
| 37 |
#define p1tfree tfree |
| 38 |
#define p1nfree nfree |
| 39 |
#define p1tcopy tcopy |
| 40 |
#else |
| 41 |
#define NODE P1ND |
| 42 |
#define talloc p1alloc |
| 43 |
#endif |
| 44 |
|
| 45 |
static void genswitch_bintree(int num, TWORD ty, struct swents **p, int n); |
| 46 |
|
| 47 |
#if 0 |
| 48 |
static void genswitch_table(int num, struct swents **p, int n); |
| 49 |
static void genswitch_mrst(int num, struct swents **p, int n); |
| 50 |
#endif |
| 51 |
|
| 52 |
static int rvnr; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void |
| 58 |
setseg(int seg, char *name) |
| 59 |
{ |
| 60 |
switch (seg) { |
| 61 |
case PROG: name = ".text"; break; |
| 62 |
case DATA: |
| 63 |
case LDATA: name = ".data"; break; |
| 64 |
case UDATA: break; |
| 65 |
#ifdef MACHOABI |
| 66 |
case PICLDATA: |
| 67 |
case PICDATA: name = ".section .data.rel.rw,\"aw\""; break; |
| 68 |
case PICRDATA: name = ".section .data.rel.ro,\"aw\""; break; |
| 69 |
case STRNG: name = ".cstring"; break; |
| 70 |
case RDATA: name = ".const_data"; break; |
| 71 |
#else |
| 72 |
case PICLDATA: name = ".section .data.rel.local,\"aw\",@progbits";break; |
| 73 |
case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break; |
| 74 |
case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break; |
| 75 |
case STRNG: |
| 76 |
#ifdef AOUTABI |
| 77 |
case RDATA: name = ".data"; break; |
| 78 |
#else |
| 79 |
case RDATA: name = ".section .rodata"; break; |
| 80 |
#endif |
| 81 |
#endif |
| 82 |
case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break; |
| 83 |
case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break; |
| 84 |
case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break; |
| 85 |
case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break; |
| 86 |
case NMSEG: |
| 87 |
printf("\t.section %s,\"a%c\",@progbits\n", name, |
| 88 |
cftnsp ? 'x' : 'w'); |
| 89 |
return; |
| 90 |
} |
| 91 |
printf("\t%s\n", name); |
| 92 |
} |
| 93 |
|
| 94 |
void |
| 95 |
defalign(int al) |
| 96 |
{ |
| 97 |
if (ispow2(al/ALCHAR)) |
| 98 |
printf("\t.p2align %d\n", ispow2(al/ALCHAR)); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
void |
| 107 |
defloc(struct symtab *sp) |
| 108 |
{ |
| 109 |
char *name; |
| 110 |
|
| 111 |
name = getexname(sp); |
| 112 |
if (sp->sclass == EXTDEF) |
| 113 |
printf(" .globl %s\n", name); |
| 114 |
if (sp->slevel == 0) |
| 115 |
printf("%s:\n", name); |
| 116 |
else |
| 117 |
printf(LABFMT ":\n", sp->soffset); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static void |
| 124 |
putintemp(struct symtab *sym) |
| 125 |
{ |
| 126 |
NODE *p; |
| 127 |
|
| 128 |
p = tempnode(0, sym->stype, sym->sdf, sym->sap); |
| 129 |
p = buildtree(ASSIGN, p, nametree(sym)); |
| 130 |
sym->soffset = regno(p->n_left); |
| 131 |
sym->sflags |= STNODE; |
| 132 |
ecomp(p); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void |
| 138 |
param_64bit(struct symtab *sym, int *argofsp, int dotemps) |
| 139 |
{ |
| 140 |
int argofs = *argofsp; |
| 141 |
NODE *p, *q; |
| 142 |
int navail; |
| 143 |
|
| 144 |
#if ALLONGLONG == 64 |
| 145 |
|
| 146 |
++argofs; |
| 147 |
argofs &= ~1; |
| 148 |
#endif |
| 149 |
|
| 150 |
navail = NARGREGS - argofs; |
| 151 |
|
| 152 |
if (navail < 2) { |
| 153 |
|
| 154 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 155 |
regno(q) = R3 + argofs; |
| 156 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 157 |
regno(p) = FPREG; |
| 158 |
p = block(PLUS, p, bcon(sym->soffset/SZCHAR), PTR+INT, 0, 0); |
| 159 |
p = block(UMUL, p, NIL, INT, 0, 0); |
| 160 |
} else { |
| 161 |
q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap); |
| 162 |
regno(q) = R3R4 + argofs; |
| 163 |
if (dotemps) { |
| 164 |
p = tempnode(0, sym->stype, sym->sdf, sym->sap); |
| 165 |
sym->soffset = regno(p); |
| 166 |
sym->sflags |= STNODE; |
| 167 |
} else { |
| 168 |
p = nametree(sym); |
| 169 |
} |
| 170 |
} |
| 171 |
p = buildtree(ASSIGN, p, q); |
| 172 |
ecomp(p); |
| 173 |
*argofsp = argofs + 2; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static void |
| 179 |
param_32bit(struct symtab *sym, int *argofsp, int dotemps) |
| 180 |
{ |
| 181 |
NODE *p, *q; |
| 182 |
|
| 183 |
q = block(REG, NIL, NIL, sym->stype, sym->sdf, sym->sap); |
| 184 |
regno(q) = R3 + (*argofsp)++; |
| 185 |
if (dotemps) { |
| 186 |
p = tempnode(0, sym->stype, sym->sdf, sym->sap); |
| 187 |
sym->soffset = regno(p); |
| 188 |
sym->sflags |= STNODE; |
| 189 |
} else { |
| 190 |
p = nametree(sym); |
| 191 |
} |
| 192 |
p = buildtree(ASSIGN, p, q); |
| 193 |
ecomp(p); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static void |
| 199 |
param_double(struct symtab *sym, int *argofsp, int dotemps) |
| 200 |
{ |
| 201 |
NODE *p, *q, *t; |
| 202 |
int tmpnr; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
if (xtemps) { |
| 211 |
q = block(REG, NIL, NIL, ULONGLONG, 0, 0); |
| 212 |
regno(q) = R3R4 + *argofsp; |
| 213 |
p = block(REG, NIL, NIL, PTR+ULONGLONG, 0, 0); |
| 214 |
regno(p) = SPREG; |
| 215 |
p = block(PLUS, p, bcon(-8), INT, 0, 0); |
| 216 |
p = block(UMUL, p, NIL, ULONGLONG, 0, 0); |
| 217 |
p = buildtree(ASSIGN, p, q); |
| 218 |
ecomp(p); |
| 219 |
|
| 220 |
t = tempnode(0, sym->stype, sym->sdf, sym->sap); |
| 221 |
tmpnr = regno(t); |
| 222 |
p = block(REG, NIL, NIL, |
| 223 |
INCREF(sym->stype), sym->sdf, sym->sap); |
| 224 |
regno(p) = SPREG; |
| 225 |
p = block(PLUS, p, bcon(-8), INT, 0, 0); |
| 226 |
p = block(UMUL, p, NIL, sym->stype, sym->sdf, sym->sap); |
| 227 |
p = buildtree(ASSIGN, t, p); |
| 228 |
ecomp(p); |
| 229 |
} else { |
| 230 |
|
| 231 |
p = block(REG, NIL, NIL, ULONGLONG, 0, 0); |
| 232 |
regno(p) = R3R4 + *argofsp; |
| 233 |
t = tempnode(0, ULONGLONG, 0, 0); |
| 234 |
tmpnr = regno(t); |
| 235 |
p = buildtree(ASSIGN, t, p); |
| 236 |
ecomp(p); |
| 237 |
} |
| 238 |
|
| 239 |
(*argofsp) += 2; |
| 240 |
|
| 241 |
sym->soffset = tmpnr; |
| 242 |
sym->sflags |= STNODE; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static void |
| 248 |
param_float(struct symtab *sym, int *argofsp, int dotemps) |
| 249 |
{ |
| 250 |
NODE *p, *q, *t; |
| 251 |
int tmpnr; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if (xtemps) { |
| 260 |
|
| 261 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 262 |
regno(q) = R3 + (*argofsp); |
| 263 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 264 |
regno(p) = SPREG; |
| 265 |
p = block(PLUS, p, bcon(-4), INT, 0, 0); |
| 266 |
p = block(UMUL, p, NIL, INT, 0, 0); |
| 267 |
p = buildtree(ASSIGN, p, q); |
| 268 |
ecomp(p); |
| 269 |
|
| 270 |
t = tempnode(0, sym->stype, sym->sdf, sym->sap); |
| 271 |
tmpnr = regno(t); |
| 272 |
p = block(REG, NIL, NIL, INCREF(sym->stype), |
| 273 |
sym->sdf, sym->sap); |
| 274 |
regno(p) = SPREG; |
| 275 |
p = block(PLUS, p, bcon(-4), INT, 0, 0); |
| 276 |
p = block(UMUL, p, NIL, sym->stype, sym->sdf, sym->sap); |
| 277 |
p = buildtree(ASSIGN, t, p); |
| 278 |
ecomp(p); |
| 279 |
} else { |
| 280 |
|
| 281 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 282 |
regno(p) = R3 + (*argofsp); |
| 283 |
t = tempnode(0, INT, 0, 0); |
| 284 |
tmpnr = regno(t); |
| 285 |
p = buildtree(ASSIGN, t, p); |
| 286 |
ecomp(p); |
| 287 |
} |
| 288 |
|
| 289 |
(*argofsp)++; |
| 290 |
|
| 291 |
sym->soffset = tmpnr; |
| 292 |
sym->sflags |= STNODE; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
static void |
| 298 |
param_retstruct(void) |
| 299 |
{ |
| 300 |
NODE *p, *q; |
| 301 |
|
| 302 |
p = tempnode(0, INCREF(cftnsp->stype), 0, cftnsp->sap); |
| 303 |
rvnr = regno(p); |
| 304 |
q = block(REG, NIL, NIL, INCREF(cftnsp->stype), |
| 305 |
cftnsp->sdf, cftnsp->sap); |
| 306 |
regno(q) = R3; |
| 307 |
p = buildtree(ASSIGN, p, q); |
| 308 |
ecomp(p); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
static void |
| 316 |
param_struct(struct symtab *sym, int *argofsp) |
| 317 |
{ |
| 318 |
int argofs = *argofsp; |
| 319 |
NODE *p, *q; |
| 320 |
int navail; |
| 321 |
int sz; |
| 322 |
int off; |
| 323 |
int num; |
| 324 |
int i; |
| 325 |
|
| 326 |
navail = NARGREGS - argofs; |
| 327 |
sz = tsize(sym->stype, sym->sdf, sym->sap) / SZINT; |
| 328 |
off = ARGINIT/SZINT + argofs; |
| 329 |
num = sz > navail ? navail : sz; |
| 330 |
for (i = 0; i < num; i++) { |
| 331 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 332 |
regno(q) = R3 + argofs++; |
| 333 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 334 |
regno(p) = SPREG; |
| 335 |
p = block(PLUS, p, bcon(4*off++), INT, 0, 0); |
| 336 |
p = block(UMUL, p, NIL, INT, 0, 0); |
| 337 |
p = buildtree(ASSIGN, p, q); |
| 338 |
ecomp(p); |
| 339 |
} |
| 340 |
|
| 341 |
*argofsp = argofs; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
void |
| 350 |
bfcode(struct symtab **sp, int cnt) |
| 351 |
{ |
| 352 |
#ifdef USE_GOTNR |
| 353 |
extern int gotnr; |
| 354 |
#endif |
| 355 |
|
| 356 |
struct symtab *sp2; |
| 357 |
union arglist *usym; |
| 358 |
int saveallargs = 0; |
| 359 |
int i, argofs = 0; |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
usym = cftnsp->sdf->dfun; |
| 366 |
while (usym && usym->type != TNULL) { |
| 367 |
if (usym->type == TELLIPSIS) { |
| 368 |
saveallargs = 1; |
| 369 |
break; |
| 370 |
} |
| 371 |
++usym; |
| 372 |
} |
| 373 |
|
| 374 |
if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) { |
| 375 |
param_retstruct(); |
| 376 |
++argofs; |
| 377 |
} |
| 378 |
|
| 379 |
#ifdef USE_GOTNR |
| 380 |
if (kflag) { |
| 381 |
|
| 382 |
NODE *q, *p; |
| 383 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 384 |
regno(q) = GOTREG; |
| 385 |
p = tempnode(0, INT, 0, 0); |
| 386 |
gotnr = regno(p); |
| 387 |
ecomp(buildtree(ASSIGN, p, q)); |
| 388 |
} |
| 389 |
#endif |
| 390 |
|
| 391 |
|
| 392 |
for (i = 0; i < cnt; i++) { |
| 393 |
|
| 394 |
if (sp[i] == NULL) |
| 395 |
continue; |
| 396 |
|
| 397 |
if ((argofs >= NARGREGS) && !xtemps) |
| 398 |
break; |
| 399 |
|
| 400 |
if (argofs >= NARGREGS) { |
| 401 |
putintemp(sp[i]); |
| 402 |
} else if (sp[i]->stype == STRTY || sp[i]->stype == UNIONTY) { |
| 403 |
param_struct(sp[i], &argofs); |
| 404 |
} else if (DEUNSIGN(sp[i]->stype) == LONGLONG) { |
| 405 |
param_64bit(sp[i], &argofs, xtemps && !saveallargs); |
| 406 |
} else if (sp[i]->stype == DOUBLE || sp[i]->stype == LDOUBLE) { |
| 407 |
if (features(FEATURE_HARDFLOAT)) |
| 408 |
param_double(sp[i], &argofs, |
| 409 |
xtemps && !saveallargs); |
| 410 |
else |
| 411 |
param_64bit(sp[i], &argofs, |
| 412 |
xtemps && !saveallargs); |
| 413 |
} else if (sp[i]->stype == FLOAT) { |
| 414 |
if (features(FEATURE_HARDFLOAT)) |
| 415 |
param_float(sp[i], &argofs, |
| 416 |
xtemps && !saveallargs); |
| 417 |
else |
| 418 |
param_32bit(sp[i], &argofs, |
| 419 |
xtemps && !saveallargs); |
| 420 |
} else { |
| 421 |
param_32bit(sp[i], &argofs, xtemps && !saveallargs); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
while (saveallargs && argofs < NARGREGS) { |
| 427 |
NODE *p, *q; |
| 428 |
|
| 429 |
int off = ARGINIT/SZINT + argofs; |
| 430 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 431 |
regno(q) = R3 + argofs++; |
| 432 |
p = block(REG, NIL, NIL, INT, 0, 0); |
| 433 |
regno(p) = FPREG; |
| 434 |
p = block(PLUS, p, bcon(4*off), INT, 0, 0); |
| 435 |
p = block(UMUL, p, NIL, INT, 0, 0); |
| 436 |
p = buildtree(ASSIGN, p, q); |
| 437 |
ecomp(p); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
if (pflag) { |
| 442 |
NODE *p; |
| 443 |
|
| 444 |
#if defined(ELFABI) |
| 445 |
|
| 446 |
sp2 = lookup("_mcount", 0); |
| 447 |
sp2->stype = EXTERN; |
| 448 |
p = nametree(sp2); |
| 449 |
p->n_sp->sclass = EXTERN; |
| 450 |
p = clocal(p); |
| 451 |
p = buildtree(ADDROF, p, NIL); |
| 452 |
p = block(UCALL, p, NIL, INT, 0, 0); |
| 453 |
ecomp(funcode(p)); |
| 454 |
|
| 455 |
|
| 456 |
#elif defined(MACHOABI) |
| 457 |
|
| 458 |
NODE *q; |
| 459 |
int tmpnr; |
| 460 |
|
| 461 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 462 |
regno(q) = R0; |
| 463 |
p = tempnode(0, INT, 0, 0); |
| 464 |
tmpnr = regno(p); |
| 465 |
p = buildtree(ASSIGN, p, q); |
| 466 |
ecomp(p); |
| 467 |
|
| 468 |
q = tempnode(tmpnr, INT, 0, 0); |
| 469 |
|
| 470 |
sp2 = lookup("mcount", 0); |
| 471 |
sp2->stype = EXTERN; |
| 472 |
p = nametree(sp2); |
| 473 |
p->n_sp->sclass = EXTERN; |
| 474 |
p = clocal(p); |
| 475 |
p = buildtree(ADDROF, p, NIL); |
| 476 |
p = block(CALL, p, q, INT, 0, 0); |
| 477 |
ecomp(funcode(p)); |
| 478 |
|
| 479 |
#endif |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
void |
| 488 |
efcode(void) |
| 489 |
{ |
| 490 |
NODE *p, *q; |
| 491 |
int tempnr; |
| 492 |
int ty; |
| 493 |
|
| 494 |
#ifdef USE_GOTNR |
| 495 |
extern int gotnr; |
| 496 |
gotnr = 0; |
| 497 |
#endif |
| 498 |
|
| 499 |
if (cftnsp->stype != STRTY+FTN && cftnsp->stype != UNIONTY+FTN) |
| 500 |
return; |
| 501 |
|
| 502 |
ty = cftnsp->stype - FTN; |
| 503 |
|
| 504 |
q = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap); |
| 505 |
regno(q) = R3; |
| 506 |
p = tempnode(0, INCREF(ty), 0, cftnsp->sap); |
| 507 |
tempnr = regno(p); |
| 508 |
p = buildtree(ASSIGN, p, q); |
| 509 |
ecomp(p); |
| 510 |
|
| 511 |
q = tempnode(tempnr, INCREF(ty), 0, cftnsp->sap); |
| 512 |
q = buildtree(UMUL, q, NIL); |
| 513 |
|
| 514 |
p = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap); |
| 515 |
p = buildtree(UMUL, p, NIL); |
| 516 |
|
| 517 |
p = buildtree(ASSIGN, p, q); |
| 518 |
ecomp(p); |
| 519 |
|
| 520 |
q = tempnode(rvnr, INCREF(ty), 0, cftnsp->sap); |
| 521 |
p = block(REG, NIL, NIL, INCREF(ty), 0, cftnsp->sap); |
| 522 |
regno(p) = R3; |
| 523 |
p = buildtree(ASSIGN, p, q); |
| 524 |
ecomp(p); |
| 525 |
} |
| 526 |
|
| 527 |
struct stub stublist; |
| 528 |
struct stub nlplist; |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
void |
| 533 |
ejobcode(int flag) |
| 534 |
{ |
| 535 |
|
| 536 |
#if defined(MACHOABI) |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
if (kflag) { |
| 541 |
struct stub *p; |
| 542 |
|
| 543 |
DLIST_FOREACH(p, &stublist, link) { |
| 544 |
printf("\t.section __TEXT, __picsymbolstub1,symbol_stubs,pure_instructions,32\n"); |
| 545 |
printf("\t.align 5\n"); |
| 546 |
printf("L%s$stub:\n", p->name); |
| 547 |
if (strcmp(p->name, "mcount") == 0) |
| 548 |
printf("\t.indirect_symbol %s\n", p->name); |
| 549 |
else |
| 550 |
printf("\t.indirect_symbol %s\n", p->name); |
| 551 |
printf("\tmflr r0\n"); |
| 552 |
printf("\tbcl 20,31,L%s$spb\n", p->name); |
| 553 |
printf("L%s$spb:\n", p->name); |
| 554 |
printf("\tmflr r11\n"); |
| 555 |
printf("\taddis r11,r11,ha16(L%s$lazy_ptr-L%s$spb)\n", |
| 556 |
p->name, p->name); |
| 557 |
printf("\tmtlr r0\n"); |
| 558 |
printf("\tlwzu r12,lo16(L%s$lazy_ptr-L%s$spb)(r11)\n", |
| 559 |
p->name, p->name); |
| 560 |
printf("\tmtctr r12\n"); |
| 561 |
printf("\tbctr\n"); |
| 562 |
printf("\t.lazy_symbol_pointer\n"); |
| 563 |
printf("L%s$lazy_ptr:\n", p->name); |
| 564 |
if (strcmp(p->name, "mcount") == 0) |
| 565 |
printf("\t.indirect_symbol %s\n", p->name); |
| 566 |
else |
| 567 |
printf("\t.indirect_symbol %s\n", p->name); |
| 568 |
printf("\t.long dyld_stub_binding_helper\n"); |
| 569 |
printf("\t.subsections_via_symbols\n"); |
| 570 |
} |
| 571 |
|
| 572 |
printf("\t.non_lazy_symbol_pointer\n"); |
| 573 |
DLIST_FOREACH(p, &nlplist, link) { |
| 574 |
printf("L%s$non_lazy_ptr:\n", p->name); |
| 575 |
if (strcmp(p->name, "mcount") == 0) |
| 576 |
printf("\t.indirect_symbol %s\n", p->name); |
| 577 |
else |
| 578 |
printf("\t.indirect_symbol %s\n", p->name); |
| 579 |
printf("\t.long 0\n"); |
| 580 |
} |
| 581 |
|
| 582 |
} |
| 583 |
#endif |
| 584 |
|
| 585 |
#ifndef os_darwin |
| 586 |
printf("\t.ident \"PCC: %s\"\n", VERSSTR); |
| 587 |
#endif |
| 588 |
|
| 589 |
} |
| 590 |
|
| 591 |
void |
| 592 |
bjobcode(void) |
| 593 |
{ |
| 594 |
DLIST_INIT(&stublist, link); |
| 595 |
DLIST_INIT(&nlplist, link); |
| 596 |
} |
| 597 |
|
| 598 |
#ifdef notdef |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
void |
| 604 |
bycode(int t, int i) |
| 605 |
{ |
| 606 |
static int lastoctal = 0; |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
if (t < 0) { |
| 611 |
if (i != 0) |
| 612 |
puts("\""); |
| 613 |
} else { |
| 614 |
if (i == 0) |
| 615 |
printf("\t.ascii \""); |
| 616 |
if (t == '\\' || t == '"') { |
| 617 |
lastoctal = 0; |
| 618 |
putchar('\\'); |
| 619 |
putchar(t); |
| 620 |
} else if (t < 040 || t >= 0177) { |
| 621 |
lastoctal++; |
| 622 |
printf("\\%o",t); |
| 623 |
} else if (lastoctal && '0' <= t && t <= '9') { |
| 624 |
lastoctal = 0; |
| 625 |
printf("\"\n\t.ascii \"%c", t); |
| 626 |
} else { |
| 627 |
lastoctal = 0; |
| 628 |
putchar(t); |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
#endif |
| 633 |
|
| 634 |
|
| 635 |
void |
| 636 |
fldty(struct symtab *p) |
| 637 |
{ |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
int |
| 644 |
mygenswitch(int num, TWORD type, struct swents **p, int n) |
| 645 |
{ |
| 646 |
if (num < 0) { |
| 647 |
genswitch_bintree(num, type, p, n); |
| 648 |
return 1; |
| 649 |
} |
| 650 |
|
| 651 |
return 0; |
| 652 |
|
| 653 |
#if 0 |
| 654 |
if (0) |
| 655 |
genswitch_table(num, p, n); |
| 656 |
if (0) |
| 657 |
genswitch_bintree(num, p, n); |
| 658 |
genswitch_mrst(num, p, n); |
| 659 |
#endif |
| 660 |
} |
| 661 |
|
| 662 |
static void bintree_rec(TWORD ty, int num, |
| 663 |
struct swents **p, int n, int s, int e); |
| 664 |
|
| 665 |
static void |
| 666 |
genswitch_bintree(int num, TWORD ty, struct swents **p, int n) |
| 667 |
{ |
| 668 |
int lab = getlab(); |
| 669 |
|
| 670 |
if (p[0]->slab == 0) |
| 671 |
p[0]->slab = lab; |
| 672 |
|
| 673 |
bintree_rec(ty, num, p, n, 1, n); |
| 674 |
|
| 675 |
plabel(lab); |
| 676 |
} |
| 677 |
|
| 678 |
static void |
| 679 |
bintree_rec(TWORD ty, int num, struct swents **p, int n, int s, int e) |
| 680 |
{ |
| 681 |
NODE *r; |
| 682 |
int rlabel; |
| 683 |
int h; |
| 684 |
|
| 685 |
if (s == e) { |
| 686 |
r = tempnode(num, ty, 0, 0); |
| 687 |
r = buildtree(NE, r, bcon(p[s]->sval)); |
| 688 |
cbranch(buildtree(NOT, r, NIL), bcon(p[s]->slab)); |
| 689 |
branch(p[0]->slab); |
| 690 |
return; |
| 691 |
} |
| 692 |
|
| 693 |
rlabel = getlab(); |
| 694 |
|
| 695 |
h = s + (e - s) / 2; |
| 696 |
|
| 697 |
r = tempnode(num, ty, 0, 0); |
| 698 |
r = buildtree(GT, r, bcon(p[h]->sval)); |
| 699 |
cbranch(r, bcon(rlabel)); |
| 700 |
bintree_rec(ty, num, p, n, s, h); |
| 701 |
plabel(rlabel); |
| 702 |
bintree_rec(ty, num, p, n, h+1, e); |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
#if 0 |
| 707 |
|
| 708 |
static void |
| 709 |
genswitch_table(int num, struct swents **p, int n) |
| 710 |
{ |
| 711 |
NODE *r, *t; |
| 712 |
int tval; |
| 713 |
int minval, maxval, range; |
| 714 |
int deflabel, tbllabel; |
| 715 |
int i, j; |
| 716 |
|
| 717 |
minval = p[1]->sval; |
| 718 |
maxval = p[n]->sval; |
| 719 |
|
| 720 |
range = maxval - minval + 1; |
| 721 |
|
| 722 |
if (n < 10 || range > 3 * n) { |
| 723 |
|
| 724 |
genswitch_simple(num, p, n); |
| 725 |
return; |
| 726 |
} |
| 727 |
|
| 728 |
r = tempnode(num, UNSIGNED, 0, 0); |
| 729 |
r = buildtree(MINUS, r, bcon(minval)); |
| 730 |
t = tempnode(0, UNSIGNED, 0, 0); |
| 731 |
tval = regno(t); |
| 732 |
r = buildtree(ASSIGN, t, r); |
| 733 |
ecomp(r); |
| 734 |
|
| 735 |
deflabel = p[0]->slab; |
| 736 |
if (deflabel == 0) |
| 737 |
deflabel = getlab(); |
| 738 |
|
| 739 |
t = tempnode(tval, UNSIGNED, 0, 0); |
| 740 |
cbranch(buildtree(GT, t, bcon(maxval-minval)), bcon(deflabel)); |
| 741 |
|
| 742 |
tbllabel = getlab(); |
| 743 |
struct symtab *strtbl = lookup("__switch_table", SLBLNAME|STEMP); |
| 744 |
strtbl->soffset = tbllabel; |
| 745 |
strtbl->sclass = ILABEL; |
| 746 |
strtbl->stype = INCREF(UCHAR); |
| 747 |
|
| 748 |
t = block(NAME, NIL, NIL, UNSIGNED, 0, 0); |
| 749 |
t->n_sp = strtbl; |
| 750 |
t = buildtree(ADDROF, t, NIL); |
| 751 |
r = tempnode(tval, UNSIGNED, 0, 0); |
| 752 |
r = buildtree(PLUS, t, r); |
| 753 |
t = tempnode(0, INCREF(UNSIGNED), 0, 0); |
| 754 |
r = buildtree(ASSIGN, t, r); |
| 755 |
ecomp(r); |
| 756 |
|
| 757 |
r = tempnode(regno(t), INCREF(UNSIGNED), 0, 0); |
| 758 |
r = buildtree(UMUL, r, NIL); |
| 759 |
t = block(NAME, NIL, NIL, UCHAR, 0, 0); |
| 760 |
t->n_sp = strtbl; |
| 761 |
t = buildtree(ADDROF, t, NIL); |
| 762 |
r = buildtree(PLUS, t, r); |
| 763 |
r = block(GOTO, r, NIL, 0, 0, 0); |
| 764 |
ecomp(r); |
| 765 |
|
| 766 |
plabel(tbllabel); |
| 767 |
for (i = minval, j=1; i <= maxval; i++) { |
| 768 |
char *entry = tmpalloc(20); |
| 769 |
int lab = deflabel; |
| 770 |
|
| 771 |
if (p[j]->sval == i) { |
| 772 |
lab = p[j]->slab; |
| 773 |
j++; |
| 774 |
} |
| 775 |
snprintf(entry, 20, "\t.long " LABFMT "-" LABFMT "\n", lab, tbllabel); |
| 776 |
send_passt(IP_ASM, entry); |
| 777 |
} |
| 778 |
|
| 779 |
if (p[0]->slab <= 0) |
| 780 |
plabel(deflabel); |
| 781 |
} |
| 782 |
|
| 783 |
#define DPRINTF(x) if (xdebug) printf x |
| 784 |
|
| 785 |
|
| 786 |
#define MIN_TABLE_SIZE 8 |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
static void mrst_rec(int num, struct swents **p, int n, int *state, int lab); |
| 793 |
static unsigned long mrst_find_window(struct swents **p, int n, int *state, int lab, int *len, int *lowbit); |
| 794 |
void mrst_put_entry_and_recurse(int num, struct swents **p, int n, int *state, int tbllabel, int lab, unsigned long j, unsigned long tblsize, unsigned long Wmax, int lowbit); |
| 795 |
|
| 796 |
static void |
| 797 |
genswitch_mrst(int num, struct swents **p, int n) |
| 798 |
{ |
| 799 |
int *state; |
| 800 |
int i; |
| 801 |
int putlabel = 0; |
| 802 |
|
| 803 |
if (n < 10) { |
| 804 |
|
| 805 |
genswitch_simple(num, p, n); |
| 806 |
return; |
| 807 |
} |
| 808 |
|
| 809 |
state = tmpalloc((n+1)*sizeof(int)); |
| 810 |
for (i = 0; i <= n; i++) |
| 811 |
state[i] = 0; |
| 812 |
|
| 813 |
if (p[0]->slab == 0) { |
| 814 |
p[0]->slab = getlab(); |
| 815 |
putlabel = 1; |
| 816 |
} |
| 817 |
|
| 818 |
mrst_rec(num, p, n, state, 0); |
| 819 |
|
| 820 |
if (putlabel) |
| 821 |
plabel(p[0]->slab); |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
static void |
| 832 |
mrst_rec(int num, struct swents **p, int n, int *state, int lab) |
| 833 |
{ |
| 834 |
int len, lowbit; |
| 835 |
unsigned long Wmax; |
| 836 |
unsigned int tblsize; |
| 837 |
NODE *t; |
| 838 |
NODE *r; |
| 839 |
int tval; |
| 840 |
int i; |
| 841 |
|
| 842 |
DPRINTF(("mrst_rec: num=%d, n=%d, lab=%d\n", num, n, lab)); |
| 843 |
|
| 844 |
|
| 845 |
Wmax = mrst_find_window(p, n, state, lab, &len, &lowbit); |
| 846 |
tblsize = (1 << len); |
| 847 |
assert(len > 0 && tblsize > 0); |
| 848 |
|
| 849 |
DPRINTF(("mrst_rec: Wmax=%lu, lowbit=%d, tblsize=%u\n", |
| 850 |
Wmax, lowbit, tblsize)); |
| 851 |
|
| 852 |
if (lab) |
| 853 |
plabel(lab); |
| 854 |
|
| 855 |
if (tblsize <= MIN_TABLE_SIZE) { |
| 856 |
DPRINTF(("msrt_rec: break the recursion\n")); |
| 857 |
for (i = 1; i <= n; i++) { |
| 858 |
if (state[i] == lab) { |
| 859 |
t = tempnode(num, UNSIGNED, 0, 0); |
| 860 |
cbranch(buildtree(EQ, t, bcon(p[i]->sval)), |
| 861 |
bcon(p[i]->slab)); |
| 862 |
} |
| 863 |
} |
| 864 |
branch(p[0]->slab); |
| 865 |
return; |
| 866 |
} |
| 867 |
|
| 868 |
DPRINTF(("generating table with %d elements\n", tblsize)); |
| 869 |
|
| 870 |
|
| 871 |
t = tempnode(num, UNSIGNED, 0, 0); |
| 872 |
r = buildtree(AND, t, bcon(Wmax)); |
| 873 |
|
| 874 |
|
| 875 |
r = buildtree(RS, r, bcon(lowbit)); |
| 876 |
|
| 877 |
t = tempnode(0, UNSIGNED, 0, 0); |
| 878 |
tval = regno(t); |
| 879 |
r = buildtree(ASSIGN, t, r); |
| 880 |
ecomp(r); |
| 881 |
|
| 882 |
int tbllabel = getlab(); |
| 883 |
struct symtab *strtbl = lookup("__switch_table", SLBLNAME|STEMP); |
| 884 |
strtbl->sclass = STATIC; |
| 885 |
strtbl->sap = 0; |
| 886 |
strtbl->slevel = 1; |
| 887 |
strtbl->soffset = tbllabel; |
| 888 |
strtbl->stype = INCREF(UCHAR); |
| 889 |
strtbl->squal = (CON >> TSHIFT); |
| 890 |
|
| 891 |
t = block(NAME, NIL, NIL, UNSIGNED, 0, 0); |
| 892 |
t->n_sp = strtbl; |
| 893 |
t = buildtree(ADDROF, t, NIL); |
| 894 |
r = tempnode(tval, UNSIGNED, 0, 0); |
| 895 |
r = buildtree(PLUS, t, r); |
| 896 |
t = tempnode(0, INCREF(UNSIGNED), 0, 0); |
| 897 |
r = buildtree(ASSIGN, t, r); |
| 898 |
ecomp(r); |
| 899 |
|
| 900 |
r = tempnode(regno(t), INCREF(UNSIGNED), 0, 0); |
| 901 |
r = buildtree(UMUL, r, NIL); |
| 902 |
t = block(NAME, NIL, NIL, UCHAR, 0, 0); |
| 903 |
t->n_sp = strtbl; |
| 904 |
t = buildtree(ADDROF, t, NIL); |
| 905 |
r = buildtree(PLUS, t, r); |
| 906 |
r = block(GOTO, r, NIL, 0, 0, 0); |
| 907 |
ecomp(r); |
| 908 |
|
| 909 |
plabel(tbllabel); |
| 910 |
|
| 911 |
mrst_put_entry_and_recurse(num, p, n, state, tbllabel, lab, |
| 912 |
0, tblsize, Wmax, lowbit); |
| 913 |
} |
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
void |
| 923 |
mrst_put_entry_and_recurse(int num, struct swents **p, int n, int *state, |
| 924 |
int tbllabel, int labval, |
| 925 |
unsigned long j, unsigned long tblsize, unsigned long Wmax, int lowbit) |
| 926 |
{ |
| 927 |
int i; |
| 928 |
int found = 0; |
| 929 |
int lab = getlab(); |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
for (i = 1; i <= n; i++) { |
| 936 |
unsigned int val = (p[i]->sval & Wmax) >> lowbit; |
| 937 |
if (val == j && state[i] == labval) { |
| 938 |
found = 1; |
| 939 |
state[i] = lab; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
if (!found) |
| 945 |
lab = p[0]->slab; |
| 946 |
|
| 947 |
|
| 948 |
char *entry = tmpalloc(20); |
| 949 |
snprintf(entry, 20, "\t.long " LABFMT "-" LABFMT "\n", lab, tbllabel); |
| 950 |
send_passt(IP_ASM, entry); |
| 951 |
|
| 952 |
DPRINTF(("mrst_put_entry: table=%d, pos=%lu/%lu, label=%d\n", |
| 953 |
tbllabel, j, tblsize, lab)); |
| 954 |
|
| 955 |
|
| 956 |
if (j+1 < tblsize) { |
| 957 |
mrst_put_entry_and_recurse(num, p, n, state, tbllabel, labval, |
| 958 |
j+1, tblsize, Wmax, lowbit); |
| 959 |
} |
| 960 |
|
| 961 |
|
| 962 |
if (!found) |
| 963 |
return; |
| 964 |
|
| 965 |
#ifdef PCC_DEBUG |
| 966 |
if (xdebug) { |
| 967 |
printf("state: "); |
| 968 |
for (i = 1; i <= n; i++) |
| 969 |
printf("%d ", state[i]); |
| 970 |
printf("\n"); |
| 971 |
} |
| 972 |
#endif |
| 973 |
|
| 974 |
|
| 975 |
mrst_rec(num, p, n, state, lab); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
static unsigned int |
| 983 |
mrst_cardinality(struct swents **p, int n, int *state, int step, unsigned long W, int L, int lowbit) |
| 984 |
{ |
| 985 |
unsigned int count = 0; |
| 986 |
int i; |
| 987 |
|
| 988 |
if (W == 0) |
| 989 |
return 0; |
| 990 |
|
| 991 |
int *vals = (int *)calloc(1 << L, sizeof(int)); |
| 992 |
assert(vals); |
| 993 |
|
| 994 |
DPRINTF(("mrst_cardinality: ")); |
| 995 |
for (i = 1; i <= n; i++) { |
| 996 |
int idx; |
| 997 |
if (state[i] != step) |
| 998 |
continue; |
| 999 |
idx = (p[i]->sval & W) >> lowbit; |
| 1000 |
DPRINTF(("%llu->%d, ", p[i]->sval, idx)); |
| 1001 |
if (!vals[idx]) { |
| 1002 |
count++; |
| 1003 |
} |
| 1004 |
vals[idx] = 1; |
| 1005 |
} |
| 1006 |
DPRINTF((": found %d entries\n", count)); |
| 1007 |
free(vals); |
| 1008 |
|
| 1009 |
return count; |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
static unsigned long |
| 1022 |
mrst_find_window(struct swents **p, int n, int *state, int lab, int *len, int *lowbit) |
| 1023 |
{ |
| 1024 |
unsigned int tblsize; |
| 1025 |
unsigned long W = 0; |
| 1026 |
unsigned long Wmax = 0; |
| 1027 |
unsigned long Wleft = (1 << (SZLONG-1)); |
| 1028 |
unsigned int C = 0; |
| 1029 |
unsigned int Cmax = 0; |
| 1030 |
int L = 0; |
| 1031 |
int Lmax = 0; |
| 1032 |
int lowmax = 0; |
| 1033 |
int no_b = SZLONG-1; |
| 1034 |
unsigned long b = (1 << (SZLONG-1)); |
| 1035 |
|
| 1036 |
DPRINTF(("mrst_find_window: n=%d, lab=%d\n", n, lab)); |
| 1037 |
|
| 1038 |
for (; b > 0; b >>= 1, no_b--) { |
| 1039 |
|
| 1040 |
|
| 1041 |
W |= b; |
| 1042 |
L += 1; |
| 1043 |
|
| 1044 |
tblsize = 1 << L; |
| 1045 |
assert(tblsize > 0); |
| 1046 |
|
| 1047 |
DPRINTF(("no_b=%d, b=0x%lx, Wleft=0x%lx, W=0x%lx, Wmax=0x%lx, L=%d, Lmax=%d, Cmax=%u, lowmax=%d, tblsize=%u\n", no_b, b, Wleft, W, Wmax, L, Lmax, Cmax, lowmax, tblsize)); |
| 1048 |
|
| 1049 |
C = mrst_cardinality(p, n, state, lab, W, L, no_b); |
| 1050 |
DPRINTF((" -> cardinality is %d\n", C)); |
| 1051 |
|
| 1052 |
if (2*C >= tblsize) { |
| 1053 |
DPRINTF(("(found good match, keep adding to table)\n")); |
| 1054 |
Wmax = W; |
| 1055 |
Lmax = L; |
| 1056 |
lowmax = no_b; |
| 1057 |
Cmax = C; |
| 1058 |
} else { |
| 1059 |
DPRINTF(("(too sparse)\n")); |
| 1060 |
assert((W & Wleft) != 0); |
| 1061 |
|
| 1062 |
|
| 1063 |
W ^= Wleft; |
| 1064 |
Wleft >>= 1; |
| 1065 |
L -= 1; |
| 1066 |
|
| 1067 |
DPRINTF((" --> trying W=0x%lx and L=%d and Cmax=%u\n", W, L, Cmax)); |
| 1068 |
C = mrst_cardinality(p, n, state, lab, W, L, no_b); |
| 1069 |
DPRINTF((" --> C=%u\n", C)); |
| 1070 |
if (C > Cmax) { |
| 1071 |
Wmax = W; |
| 1072 |
Lmax = L; |
| 1073 |
lowmax = no_b; |
| 1074 |
Cmax = C; |
| 1075 |
DPRINTF((" --> better!\n")); |
| 1076 |
} else { |
| 1077 |
DPRINTF((" --> no better\n")); |
| 1078 |
} |
| 1079 |
} |
| 1080 |
|
| 1081 |
} |
| 1082 |
|
| 1083 |
#ifdef PCC_DEBUG |
| 1084 |
if (xdebug) { |
| 1085 |
int i; |
| 1086 |
int hibit = lowmax + Lmax; |
| 1087 |
printf("msrt_find_window: Wmax=0x%lx, lowbit=%d, result=", Wmax, lowmax); |
| 1088 |
for (i = 31; i >= 0; i--) { |
| 1089 |
int mask = (1 << i); |
| 1090 |
if (i == hibit) |
| 1091 |
printf("["); |
| 1092 |
if (Wmax & mask) |
| 1093 |
printf("1"); |
| 1094 |
else |
| 1095 |
printf("0"); |
| 1096 |
if (i == lowmax) |
| 1097 |
printf("]"); |
| 1098 |
} |
| 1099 |
printf("\n"); |
| 1100 |
} |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
assert(Lmax > 0); |
| 1104 |
*len = Lmax; |
| 1105 |
*lowbit = lowmax; |
| 1106 |
|
| 1107 |
DPRINTF(("msrt_find_window: returning Wmax=%lu, len=%d, lowbit=%d [tblsize=%u, entries=%u]\n", Wmax, Lmax, lowmax, tblsize, C)); |
| 1108 |
|
| 1109 |
return Wmax; |
| 1110 |
} |
| 1111 |
#endif |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
static NODE * |
| 1130 |
straighten(NODE *p) |
| 1131 |
{ |
| 1132 |
NODE *r = p->n_right; |
| 1133 |
|
| 1134 |
if (p->n_op != CM || r->n_op != CM) |
| 1135 |
return p; |
| 1136 |
|
| 1137 |
p->n_right = r->n_left; |
| 1138 |
r->n_left = straighten(p); |
| 1139 |
|
| 1140 |
return r; |
| 1141 |
} |
| 1142 |
|
| 1143 |
static NODE * |
| 1144 |
reverse1(NODE *p, NODE *a) |
| 1145 |
{ |
| 1146 |
NODE *l = p->n_left; |
| 1147 |
NODE *r = p->n_right; |
| 1148 |
|
| 1149 |
a->n_right = r; |
| 1150 |
p->n_left = a; |
| 1151 |
|
| 1152 |
if (l->n_op == CM) { |
| 1153 |
return reverse1(l, p); |
| 1154 |
} else { |
| 1155 |
p->n_right = l; |
| 1156 |
return p; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
static NODE * |
| 1164 |
reverse(NODE *p) |
| 1165 |
{ |
| 1166 |
NODE *l = p->n_left; |
| 1167 |
NODE *r = p->n_right; |
| 1168 |
|
| 1169 |
p->n_left = r; |
| 1170 |
|
| 1171 |
if (l->n_op == CM) |
| 1172 |
return reverse1(l, p); |
| 1173 |
|
| 1174 |
p->n_right = l; |
| 1175 |
|
| 1176 |
return p; |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
static NODE * |
| 1182 |
pusharg(NODE *p, int *regp) |
| 1183 |
{ |
| 1184 |
NODE *q; |
| 1185 |
int sz; |
| 1186 |
int off; |
| 1187 |
|
| 1188 |
|
| 1189 |
sz = tsize(p->n_type, p->n_df, p->n_ap); |
| 1190 |
if (sz < SZINT) |
| 1191 |
p = block(SCONV, p, NIL, INT, 0, 0); |
| 1192 |
|
| 1193 |
q = block(REG, NIL, NIL, INCREF(p->n_type), p->n_df, p->n_ap); |
| 1194 |
regno(q) = SPREG; |
| 1195 |
|
| 1196 |
off = ARGINIT/SZCHAR + 4 * (*regp - R3); |
| 1197 |
q = block(PLUS, q, bcon(off), INT, 0, 0); |
| 1198 |
q = block(UMUL, q, NIL, p->n_type, p->n_df, p->n_ap); |
| 1199 |
(*regp) += szty(p->n_type); |
| 1200 |
|
| 1201 |
return buildtree(ASSIGN, q, p); |
| 1202 |
} |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
static NODE * |
| 1207 |
movearg_32bit(NODE *p, int *regp) |
| 1208 |
{ |
| 1209 |
int reg = *regp; |
| 1210 |
NODE *q; |
| 1211 |
|
| 1212 |
q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap); |
| 1213 |
regno(q) = reg++; |
| 1214 |
q = buildtree(ASSIGN, q, p); |
| 1215 |
|
| 1216 |
*regp = reg; |
| 1217 |
return q; |
| 1218 |
} |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
static NODE * |
| 1223 |
movearg_64bit(NODE *p, int *regp) |
| 1224 |
{ |
| 1225 |
int reg = *regp; |
| 1226 |
NODE *q, *r; |
| 1227 |
|
| 1228 |
#if ALLONGLONG == 64 |
| 1229 |
|
| 1230 |
++reg; |
| 1231 |
reg &= ~1; |
| 1232 |
#endif |
| 1233 |
|
| 1234 |
if (reg > R10) { |
| 1235 |
*regp = reg; |
| 1236 |
q = pusharg(p, regp); |
| 1237 |
} else if (reg == R10) { |
| 1238 |
|
| 1239 |
r = p1tcopy(p); |
| 1240 |
if (!features(FEATURE_BIGENDIAN)) { |
| 1241 |
q = block(SCONV, p, NIL, INT, 0, 0); |
| 1242 |
q = movearg_32bit(q, regp); |
| 1243 |
r = buildtree(RS, r, bcon(32)); |
| 1244 |
r = block(SCONV, r, NIL, INT, 0, 0); |
| 1245 |
r = pusharg(r, regp); |
| 1246 |
} else { |
| 1247 |
q = buildtree(RS, p, bcon(32)); |
| 1248 |
q = block(SCONV, q, NIL, INT, 0, 0); |
| 1249 |
q = movearg_32bit(q, regp); |
| 1250 |
r = block(SCONV, r, NIL, INT, 0, 0); |
| 1251 |
r = pusharg(r, regp); |
| 1252 |
} |
| 1253 |
q = straighten(block(CM, q, r, p->n_type, p->n_df, p->n_ap)); |
| 1254 |
} else { |
| 1255 |
q = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap); |
| 1256 |
regno(q) = R3R4 + (reg - R3); |
| 1257 |
q = buildtree(ASSIGN, q, p); |
| 1258 |
*regp = reg + 2; |
| 1259 |
} |
| 1260 |
|
| 1261 |
return q; |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
static NODE * |
| 1267 |
movearg_float(NODE *p, int *fregp, int *regp) |
| 1268 |
{ |
| 1269 |
#if defined(MACHOABI) |
| 1270 |
NODE *q, *r; |
| 1271 |
TWORD ty = INCREF(p->n_type); |
| 1272 |
int tmpnr; |
| 1273 |
#endif |
| 1274 |
|
| 1275 |
p = movearg_32bit(p, fregp); |
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
#if defined(MACHOABI) |
| 1284 |
|
| 1285 |
if (xtemps) { |
| 1286 |
|
| 1287 |
r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap); |
| 1288 |
regno(r) = SPREG; |
| 1289 |
r = block(PLUS, r, bcon(-4), INT, 0, 0); |
| 1290 |
r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap); |
| 1291 |
r = buildtree(ASSIGN, r, p); |
| 1292 |
ecomp(r); |
| 1293 |
|
| 1294 |
|
| 1295 |
r = block(REG, NIL, NIL, PTR+INT, 0, 0); |
| 1296 |
regno(r) = SPREG; |
| 1297 |
r = block(PLUS, r, bcon(-4), INT, 0, 0); |
| 1298 |
r = block(UMUL, r, NIL, INT, 0, 0); |
| 1299 |
q = tempnode(0, INT, 0, 0); |
| 1300 |
tmpnr = regno(q); |
| 1301 |
r = buildtree(ASSIGN, q, r); |
| 1302 |
ecomp(r); |
| 1303 |
} else { |
| 1304 |
|
| 1305 |
q = tempnode(0, p->n_type, p->n_df, p->n_ap); |
| 1306 |
tmpnr = regno(q); |
| 1307 |
r = buildtree(ASSIGN, q, p); |
| 1308 |
ecomp(r); |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
r = tempnode(tmpnr, INT, 0, 0); |
| 1313 |
q = block(REG, NIL, NIL, INT, 0, 0); |
| 1314 |
regno(q) = (*regp)++; |
| 1315 |
p = buildtree(ASSIGN, q, r); |
| 1316 |
|
| 1317 |
#endif |
| 1318 |
return p; |
| 1319 |
|
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
static NODE * |
| 1325 |
movearg_double(NODE *p, int *fregp, int *regp) |
| 1326 |
{ |
| 1327 |
#if defined(MACHOABI) |
| 1328 |
NODE *q, *r; |
| 1329 |
TWORD ty = INCREF(p->n_type); |
| 1330 |
int tmpnr; |
| 1331 |
#endif |
| 1332 |
|
| 1333 |
|
| 1334 |
p = movearg_32bit(p, fregp); |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
#if defined(MACHOABI) |
| 1343 |
|
| 1344 |
if (xtemps) { |
| 1345 |
|
| 1346 |
r = block(REG, NIL, NIL, ty, p->n_df, p->n_ap); |
| 1347 |
regno(r) = SPREG; |
| 1348 |
r = block(PLUS, r, bcon(-8), ty, p->n_df, p->n_ap); |
| 1349 |
r = block(UMUL, r, NIL, p->n_type, p->n_df, p->n_ap); |
| 1350 |
r = buildtree(ASSIGN, r, p); |
| 1351 |
ecomp(r); |
| 1352 |
|
| 1353 |
|
| 1354 |
r = block(REG, NIL, NIL, PTR+LONGLONG, 0, 0); |
| 1355 |
regno(r) = SPREG; |
| 1356 |
r = block(PLUS, r, bcon(-8), PTR+LONGLONG, 0, 0); |
| 1357 |
r = block(UMUL, r, NIL, LONGLONG, 0, 0); |
| 1358 |
q = tempnode(0, LONGLONG, 0, 0); |
| 1359 |
tmpnr = regno(q); |
| 1360 |
r = buildtree(ASSIGN, q, r); |
| 1361 |
ecomp(r); |
| 1362 |
} else { |
| 1363 |
|
| 1364 |
q = tempnode(0, p->n_type, p->n_df, p->n_ap); |
| 1365 |
tmpnr = regno(q); |
| 1366 |
r = buildtree(ASSIGN, q, p); |
| 1367 |
ecomp(r); |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
r = tempnode(tmpnr, LONGLONG, 0, 0); |
| 1372 |
q = block(REG, NIL, NIL, LONGLONG, 0, 0); |
| 1373 |
regno(q) = R3R4 - R3 + (*regp); |
| 1374 |
p = buildtree(ASSIGN, q, r); |
| 1375 |
|
| 1376 |
(*regp) += 2; |
| 1377 |
|
| 1378 |
#endif |
| 1379 |
|
| 1380 |
return p; |
| 1381 |
} |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
static NODE * |
| 1386 |
movearg_struct(NODE *p, int *regp) |
| 1387 |
{ |
| 1388 |
int reg = *regp; |
| 1389 |
NODE *l, *q, *t, *r; |
| 1390 |
int tmpnr; |
| 1391 |
int navail; |
| 1392 |
int num; |
| 1393 |
int sz; |
| 1394 |
int ty; |
| 1395 |
int i; |
| 1396 |
|
| 1397 |
assert(p->n_op == STARG); |
| 1398 |
|
| 1399 |
navail = NARGREGS - (reg - R3); |
| 1400 |
navail = navail < 0 ? 0 : navail; |
| 1401 |
sz = tsize(p->n_type, p->n_df, p->n_ap) / SZINT; |
| 1402 |
num = sz > navail ? navail : sz; |
| 1403 |
|
| 1404 |
|
| 1405 |
l = p->n_left; |
| 1406 |
p1nfree(p); |
| 1407 |
ty = l->n_type; |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
t = tempnode(0, ty, l->n_df, l->n_ap); |
| 1414 |
tmpnr = regno(t); |
| 1415 |
q = buildtree(ASSIGN, t, l); |
| 1416 |
|
| 1417 |
|
| 1418 |
for (i = 0; i < num; i++) { |
| 1419 |
t = tempnode(tmpnr, ty, 0, 0); |
| 1420 |
t = block(SCONV, t, NIL, PTR+INT, 0, 0); |
| 1421 |
t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0); |
| 1422 |
t = buildtree(UMUL, t, NIL); |
| 1423 |
|
| 1424 |
r = block(REG, NIL, NIL, INT, 0, 0); |
| 1425 |
regno(r) = reg++; |
| 1426 |
r = buildtree(ASSIGN, r, t); |
| 1427 |
|
| 1428 |
q = block(CM, q, r, INT, 0, 0); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
for (i = num; i < sz; i++) { |
| 1433 |
t = tempnode(tmpnr, ty, 0, 0); |
| 1434 |
t = block(SCONV, t, NIL, PTR+INT, 0, 0); |
| 1435 |
t = block(PLUS, t, bcon(4*i), PTR+INT, 0, 0); |
| 1436 |
t = buildtree(UMUL, t, NIL); |
| 1437 |
r = pusharg(t, ®); |
| 1438 |
q = block(CM, q, r, INT, 0, 0); |
| 1439 |
} |
| 1440 |
|
| 1441 |
q = reverse(q); |
| 1442 |
|
| 1443 |
*regp = reg; |
| 1444 |
return q; |
| 1445 |
} |
| 1446 |
|
| 1447 |
|
| 1448 |
static NODE * |
| 1449 |
moveargs(NODE *p, int *regp, int *fregp) |
| 1450 |
{ |
| 1451 |
NODE *r, **rp; |
| 1452 |
int reg, freg; |
| 1453 |
|
| 1454 |
if (p->n_op == CM) { |
| 1455 |
p->n_left = moveargs(p->n_left, regp, fregp); |
| 1456 |
r = p->n_right; |
| 1457 |
rp = &p->n_right; |
| 1458 |
} else { |
| 1459 |
r = p; |
| 1460 |
rp = &p; |
| 1461 |
} |
| 1462 |
|
| 1463 |
reg = *regp; |
| 1464 |
freg = *fregp; |
| 1465 |
|
| 1466 |
#define ISFLOAT(p) (p->n_type == FLOAT || \ |
| 1467 |
p->n_type == DOUBLE || \ |
| 1468 |
p->n_type == LDOUBLE) |
| 1469 |
|
| 1470 |
if (reg > R10 && r->n_op != STARG) { |
| 1471 |
*rp = pusharg(r, regp); |
| 1472 |
} else if (r->n_op == STARG) { |
| 1473 |
*rp = movearg_struct(r, regp); |
| 1474 |
} else if (DEUNSIGN(r->n_type) == LONGLONG) { |
| 1475 |
*rp = movearg_64bit(r, regp); |
| 1476 |
} else if (r->n_type == DOUBLE || r->n_type == LDOUBLE) { |
| 1477 |
if (features(FEATURE_HARDFLOAT)) |
| 1478 |
*rp = movearg_double(r, fregp, regp); |
| 1479 |
else |
| 1480 |
*rp = movearg_64bit(r, regp); |
| 1481 |
} else if (r->n_type == FLOAT) { |
| 1482 |
if (features(FEATURE_HARDFLOAT)) |
| 1483 |
*rp = movearg_float(r, fregp, regp); |
| 1484 |
else |
| 1485 |
*rp = movearg_32bit(r, regp); |
| 1486 |
} else { |
| 1487 |
*rp = movearg_32bit(r, regp); |
| 1488 |
} |
| 1489 |
|
| 1490 |
return straighten(p); |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
static NODE * |
| 1499 |
retstruct(NODE *p) |
| 1500 |
{ |
| 1501 |
struct symtab s; |
| 1502 |
NODE *l, *r, *t, *q; |
| 1503 |
TWORD ty; |
| 1504 |
|
| 1505 |
l = p->n_left; |
| 1506 |
r = p->n_right; |
| 1507 |
|
| 1508 |
ty = DECREF(l->n_type) - FTN; |
| 1509 |
|
| 1510 |
s.sclass = AUTO; |
| 1511 |
s.stype = ty; |
| 1512 |
s.sdf = l->n_df; |
| 1513 |
s.sap = l->n_ap; |
| 1514 |
oalloc(&s, &autooff); |
| 1515 |
q = block(REG, NIL, NIL, INCREF(ty), l->n_df, l->n_ap); |
| 1516 |
regno(q) = FPREG; |
| 1517 |
q = block(MINUS, q, bcon(autooff/SZCHAR), INCREF(ty), |
| 1518 |
l->n_df, l->n_ap); |
| 1519 |
|
| 1520 |
|
| 1521 |
if (r->n_op != CM) { |
| 1522 |
p->n_right = block(CM, q, r, INCREF(ty), l->n_df, l->n_ap); |
| 1523 |
} else { |
| 1524 |
for (t = r; t->n_left->n_op == CM; t = t->n_left) |
| 1525 |
; |
| 1526 |
t->n_left = block(CM, q, t->n_left, INCREF(ty), |
| 1527 |
l->n_df, l->n_ap); |
| 1528 |
} |
| 1529 |
|
| 1530 |
return p; |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
NODE * |
| 1538 |
funcode(NODE *p) |
| 1539 |
{ |
| 1540 |
int regnum = R3; |
| 1541 |
int fregnum = F1; |
| 1542 |
|
| 1543 |
if (DECREF(p->n_left->n_type) == STRTY+FTN || |
| 1544 |
DECREF(p->n_left->n_type) == UNIONTY+FTN) |
| 1545 |
p = retstruct(p); |
| 1546 |
|
| 1547 |
p->n_right = moveargs(p->n_right, ®num, &fregnum); |
| 1548 |
|
| 1549 |
if (p->n_right == NULL) |
| 1550 |
p->n_op += (UCALL - CALL); |
| 1551 |
|
| 1552 |
return p; |
| 1553 |
} |