| 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 |
#include <sys/types.h> |
| 36 |
|
| 37 |
#ifndef MKEXT |
| 38 |
#include "external.h" |
| 39 |
#else |
| 40 |
typedef unsigned int bittype; |
| 41 |
#define BIT2BYTE(a) (((a) + 31) / 32) |
| 42 |
#endif |
| 43 |
#include "manifest.h" |
| 44 |
|
| 45 |
|
| 46 |
#define FOREFF 01 |
| 47 |
#define INAREG 02 |
| 48 |
#define INBREG 04 |
| 49 |
#define INCREG 010 |
| 50 |
#define INDREG 020 |
| 51 |
#define INREGS (INAREG|INBREG|INCREG|INDREG) |
| 52 |
#define FORCC 040 |
| 53 |
#define QUIET 0100 |
| 54 |
#define INTEMP 010000 |
| 55 |
#define FORREW 040000 |
| 56 |
#define INEREG 0x10000 |
| 57 |
#define INFREG 0x20000 |
| 58 |
#define INGREG 0x40000 |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#define OPSIMP 010000 |
| 65 |
#define OPCOMM 010002 |
| 66 |
#define OPMUL 010004 |
| 67 |
#define OPDIV 010006 |
| 68 |
#define OPUNARY 010010 |
| 69 |
#define OPLEAF 010012 |
| 70 |
#define OPANY 010014 |
| 71 |
#define OPLOG 010016 |
| 72 |
#define OPFLOAT 010020 |
| 73 |
#define OPSHFT 010022 |
| 74 |
#define OPLTYPE 010024 |
| 75 |
|
| 76 |
|
| 77 |
#define SANY 01 |
| 78 |
#define SAREG 02 |
| 79 |
#define SBREG 04 |
| 80 |
#define SCREG 010 |
| 81 |
#define SDREG 020 |
| 82 |
#define SCC 040 |
| 83 |
#define SNAME 0100 |
| 84 |
#define SCON 0200 |
| 85 |
#define SFLD 0400 |
| 86 |
#define SOREG 01000 |
| 87 |
#define STARNM 02000 |
| 88 |
#define STARREG 04000 |
| 89 |
#define SWADD 040000 |
| 90 |
#define SPECIAL 0100000 |
| 91 |
#define SZERO SPECIAL |
| 92 |
#define SONE (SPECIAL|1) |
| 93 |
#define SMONE (SPECIAL|2) |
| 94 |
#define SCCON (SPECIAL|3) |
| 95 |
#define SSCON (SPECIAL|4) |
| 96 |
#define SSOREG (SPECIAL|5) |
| 97 |
#define MAXSPECIAL (SPECIAL|5) |
| 98 |
#define SEREG 0x10000 |
| 99 |
#define SFREG 0x20000 |
| 100 |
#define SGREG 0x40000 |
| 101 |
|
| 102 |
|
| 103 |
#define TEMPREG 01000 |
| 104 |
#define PERMREG 02000 |
| 105 |
|
| 106 |
|
| 107 |
#define SRNOPE 0 |
| 108 |
#define SRDIR 1 |
| 109 |
#define SROREG 2 |
| 110 |
#define SRREG 3 |
| 111 |
|
| 112 |
|
| 113 |
#define FRETRY -2 |
| 114 |
#define FFAIL -1 |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#define TCHAR 01 |
| 120 |
#define TSHORT 02 |
| 121 |
#define TINT 04 |
| 122 |
#define TLONG 010 |
| 123 |
#define TFLOAT 020 |
| 124 |
#define TDOUBLE 040 |
| 125 |
#define TPOINT 0100 |
| 126 |
#define TUCHAR 0200 |
| 127 |
#define TUSHORT 0400 |
| 128 |
#define TUNSIGNED 01000 |
| 129 |
#define TULONG 02000 |
| 130 |
#define TPTRTO 04000 |
| 131 |
#define TANY 010000 |
| 132 |
#define TSTRUCT 020000 |
| 133 |
#define TLONGLONG 040000 |
| 134 |
#define TULONGLONG 0100000 |
| 135 |
#define TLDOUBLE 0200000 |
| 136 |
#define TFTN 0400000 |
| 137 |
|
| 138 |
|
| 139 |
#define RNULL 0 |
| 140 |
#define RLEFT 01 |
| 141 |
#define RRIGHT 02 |
| 142 |
#define RESC1 04 |
| 143 |
#define RESC2 010 |
| 144 |
#define RESC3 020 |
| 145 |
#define RDEST 040 |
| 146 |
#define RESCC 04000 |
| 147 |
#define RNOP 010000 |
| 148 |
|
| 149 |
#ifdef NEWNEED |
| 150 |
enum { cNREG = 1, cNTL, cNTR, cNSL, cNSR, cNL, cNR, cNOL, cNOR, |
| 151 |
cNEVER, cNRES, cNTEMP, cNREW }; |
| 152 |
#define NEEDS(...) (char []){ __VA_ARGS__, 0 } |
| 153 |
#define NEEDADD(x) (x == cNREG ? 3 : x < cNSL ? 1 : 2) |
| 154 |
#define NREG(cl,num) cNREG, CLASS ## cl, num |
| 155 |
#define NTL cNTL |
| 156 |
#define NTR cNTR |
| 157 |
#define NSL(cl) cNSL, CLASS ## cl |
| 158 |
#define NSR(cl) cNSR, CLASS ## cl |
| 159 |
#define NLEFT(reg) cNL, reg |
| 160 |
#define NRIGHT(reg) cNR, reg |
| 161 |
#define NOLEFT(reg) cNOL, reg |
| 162 |
#define NORIGHT(reg) cNOR, reg |
| 163 |
#define NEVER(reg) cNEVER, reg |
| 164 |
#define NRES(reg) cNRES, reg |
| 165 |
#define NTEMP(num) cNTEMP, num |
| 166 |
#define NREWRITE cNREW, 0 |
| 167 |
#else |
| 168 |
|
| 169 |
#define NASL 0x0001 |
| 170 |
#define NASR 0x0002 |
| 171 |
#define NAREG 0x0004 |
| 172 |
#define NACOUNT 0x000c |
| 173 |
#define NBSL 0x0010 |
| 174 |
#define NBSR 0x0020 |
| 175 |
#define NBREG 0x0040 |
| 176 |
#define NBCOUNT 0x00c0 |
| 177 |
#define NCSL 0x0100 |
| 178 |
#define NCSR 0x0200 |
| 179 |
#define NCREG 0x0400 |
| 180 |
#define NCCOUNT 0x0c00 |
| 181 |
#define NTEMP 0x1000 |
| 182 |
#define NTMASK 0x3000 |
| 183 |
#define NSPECIAL 0x4000 |
| 184 |
#define REWRITE 0x8000 |
| 185 |
#define NDSL 0x00010000 |
| 186 |
#define NDSR 0x00020000 |
| 187 |
#define NDREG 0x00040000 |
| 188 |
#define NDCOUNT 0x000c0000 |
| 189 |
#define NESL 0x00100000 |
| 190 |
#define NESR 0x00200000 |
| 191 |
#define NEREG 0x00400000 |
| 192 |
#define NECOUNT 0x00c00000 |
| 193 |
#define NFSL 0x01000000 |
| 194 |
#define NFSR 0x02000000 |
| 195 |
#define NFREG 0x04000000 |
| 196 |
#define NFCOUNT 0x0c000000 |
| 197 |
#define NGSL 0x10000000 |
| 198 |
#define NGSR 0x20000000 |
| 199 |
#undef NGREG |
| 200 |
#define NGREG 0x40000000 |
| 201 |
#define NGCOUNT 0xc0000000 |
| 202 |
|
| 203 |
|
| 204 |
#define NLEFT (0001) |
| 205 |
#define NOLEFT (0002) |
| 206 |
#define NRIGHT (0004) |
| 207 |
#define NORIGHT (0010) |
| 208 |
#define NEVER (0020) |
| 209 |
#define NRES (0040) |
| 210 |
#define NMOVTO (0100) |
| 211 |
#endif |
| 212 |
|
| 213 |
#define MUSTDO 010000 |
| 214 |
#define NOPREF 020000 |
| 215 |
|
| 216 |
#define isreg(p) (p->n_op == REG || p->n_op == TEMP) |
| 217 |
|
| 218 |
extern int fregs; |
| 219 |
|
| 220 |
|
| 221 |
extern struct optab { |
| 222 |
int op; |
| 223 |
int visit; |
| 224 |
int lshape; |
| 225 |
int ltype; |
| 226 |
int rshape; |
| 227 |
int rtype; |
| 228 |
#ifdef NEWNEED |
| 229 |
char *needs; |
| 230 |
#else |
| 231 |
int needs; |
| 232 |
#endif |
| 233 |
int rewrite; |
| 234 |
char *cstring; |
| 235 |
} table[]; |
| 236 |
|
| 237 |
#ifndef NEWNEED |
| 238 |
|
| 239 |
struct rspecial { |
| 240 |
int op, num; |
| 241 |
#if 0 |
| 242 |
int left; |
| 243 |
int noleft; |
| 244 |
int right; |
| 245 |
int noright; |
| 246 |
int *rmask; |
| 247 |
int res; |
| 248 |
|
| 249 |
#endif |
| 250 |
}; |
| 251 |
#endif |
| 252 |
|
| 253 |
struct p2env; |
| 254 |
#define NRESC 4 |
| 255 |
extern NODE resc[]; |
| 256 |
extern int p2autooff, p2maxautooff; |
| 257 |
|
| 258 |
extern NODE |
| 259 |
*talloc(void), |
| 260 |
*eread(void), |
| 261 |
*mklnode(int, CONSZ, int, TWORD), |
| 262 |
*mkbinode(int, NODE *, NODE *, TWORD), |
| 263 |
*mkunode(int, NODE *, int, TWORD), |
| 264 |
*getlr(NODE *p, int); |
| 265 |
|
| 266 |
void eoftn(struct interpass_prolog *); |
| 267 |
void prologue(struct interpass_prolog *); |
| 268 |
void e2print(NODE *p, int down, int *a, int *b); |
| 269 |
void myoptim(struct interpass *); |
| 270 |
void cbgen(int op, int label); |
| 271 |
int match(NODE *p, int cookie); |
| 272 |
int acceptable(struct optab *); |
| 273 |
int special(NODE *, int); |
| 274 |
int setasg(NODE *, int); |
| 275 |
int setuni(NODE *, int); |
| 276 |
int sucomp(NODE *); |
| 277 |
int nsucomp(NODE *); |
| 278 |
int setorder(NODE *); |
| 279 |
int geninsn(NODE *, int cookie); |
| 280 |
void adrput(FILE *, NODE *); |
| 281 |
void comperr(char *str, ...); |
| 282 |
void genregs(NODE *p); |
| 283 |
void ngenregs(struct p2env *); |
| 284 |
NODE *store(NODE *); |
| 285 |
struct interpass *ipnode(NODE *); |
| 286 |
void deflab(int); |
| 287 |
void rmove(int, int, TWORD); |
| 288 |
int rspecial(struct optab *, int); |
| 289 |
void printip(struct interpass *pole); |
| 290 |
int findops(NODE *p, int); |
| 291 |
int findasg(NODE *p, int); |
| 292 |
int finduni(NODE *p, int); |
| 293 |
int findumul(NODE *p, int); |
| 294 |
int findleaf(NODE *p, int); |
| 295 |
int relops(NODE *p); |
| 296 |
#ifdef FINDMOPS |
| 297 |
int findmops(NODE *p, int); |
| 298 |
int treecmp(NODE *p1, NODE *p2); |
| 299 |
#endif |
| 300 |
void offstar(NODE *p, int shape); |
| 301 |
int gclass(TWORD); |
| 302 |
void lastcall(NODE *); |
| 303 |
void myreader(struct interpass *pole); |
| 304 |
int oregok(NODE *p, int sharp); |
| 305 |
void myormake(NODE *); |
| 306 |
int *livecall(NODE *); |
| 307 |
void prtreg(NODE *); |
| 308 |
char *prcook(int); |
| 309 |
int myxasm(struct interpass *ip, NODE *p); |
| 310 |
int xasmcode(char *s); |
| 311 |
int freetemp(int k); |
| 312 |
NODE *storenode(TWORD t, int k); |
| 313 |
void storemod(NODE *, int k, int reg); |
| 314 |
int rewfld(NODE *p); |
| 315 |
void canon(NODE *); |
| 316 |
void mycanon(NODE *); |
| 317 |
void oreg2(NODE *p, void *); |
| 318 |
int shumul(NODE *p, int); |
| 319 |
NODE *deluseless(NODE *p); |
| 320 |
int getlab2(void); |
| 321 |
int tshape(NODE *, int); |
| 322 |
void conput(FILE *, NODE *); |
| 323 |
int shtemp(NODE *p); |
| 324 |
int ttype(TWORD t, int tword); |
| 325 |
void expand(NODE *, int, char *); |
| 326 |
void hopcode(int, int); |
| 327 |
void adrcon(CONSZ); |
| 328 |
void zzzcode(NODE *, int); |
| 329 |
void insput(NODE *); |
| 330 |
void upput(NODE *, int); |
| 331 |
int tlen(NODE *p); |
| 332 |
int setbin(NODE *); |
| 333 |
int notoff(TWORD, int, CONSZ, char *); |
| 334 |
int fldexpand(NODE *, int, char **); |
| 335 |
int flshape(NODE *p); |
| 336 |
#ifdef NEWNEED |
| 337 |
int ncnt(char *); |
| 338 |
char *hasneed(char *, int); |
| 339 |
char *hasneed2(char *, int, int); |
| 340 |
#else |
| 341 |
struct rspecial *nspecial(struct optab *q); |
| 342 |
int ncnt(int needs); |
| 343 |
#endif |
| 344 |
void mainp2(void); |
| 345 |
|
| 346 |
extern char *rnames[]; |
| 347 |
|
| 348 |
extern int classmask(int), tclassmask(int); |
| 349 |
extern void cmapinit(void); |
| 350 |
extern int aliasmap(int adjclass, int regnum); |
| 351 |
extern int regK[]; |
| 352 |
#define CLASSA 1 |
| 353 |
#define CLASSB 2 |
| 354 |
#define CLASSC 3 |
| 355 |
#define CLASSD 4 |
| 356 |
#define CLASSE 5 |
| 357 |
#define CLASSF 6 |
| 358 |
#define CLASSG 7 |
| 359 |
|
| 360 |
|
| 361 |
#define XASMVAL(x) ((x) & 0377) |
| 362 |
#define XASMVAL1(x) (((x) >> 16) & 0377) |
| 363 |
#define XASMVAL2(x) (((x) >> 24) & 0377) |
| 364 |
#define XASMASG 0x100 |
| 365 |
#define XASMCONSTR 0x200 |
| 366 |
#define XASMINOUT 0x400 |
| 367 |
#define XASMALL (XASMASG|XASMCONSTR|XASMINOUT) |
| 368 |
#define XASMISINP(cw) (((cw) & XASMASG) == 0) |
| 369 |
#define XASMISOUT(cw) ((cw) & (XASMASG|XASMINOUT)) |
| 370 |
|
| 371 |
|
| 372 |
#ifdef R2REGS |
| 373 |
void makeor2(NODE *p, NODE *q, int, int); |
| 374 |
int base(NODE *); |
| 375 |
int offset(NODE *p, int); |
| 376 |
#endif |
| 377 |
|
| 378 |
extern int lineno; |
| 379 |
extern int ndebug; |
| 380 |
extern int b2debug, c2debug, e2debug, f2debug, g2debug, o2debug; |
| 381 |
extern int r2debug, s2debug, t2debug, u2debug, x2debug; |
| 382 |
|
| 383 |
extern int dope[]; |
| 384 |
extern char *opst[]; |
| 385 |
|
| 386 |
#ifdef PCC_DEBUG |
| 387 |
|
| 388 |
static inline int |
| 389 |
optype(int o) |
| 390 |
{ |
| 391 |
if (o >= MAXOP+1) |
| 392 |
cerror("optype"); |
| 393 |
return (dope[o]&TYFLG); |
| 394 |
} |
| 395 |
|
| 396 |
static inline int |
| 397 |
asgop(int o) |
| 398 |
{ |
| 399 |
if (o >= MAXOP+1) |
| 400 |
cerror("asgop"); |
| 401 |
return (dope[o]&ASGFLG); |
| 402 |
} |
| 403 |
|
| 404 |
static inline int |
| 405 |
logop(int o) |
| 406 |
{ |
| 407 |
if (o >= MAXOP+1) |
| 408 |
cerror("logop"); |
| 409 |
return (dope[o]&LOGFLG); |
| 410 |
} |
| 411 |
|
| 412 |
static inline int |
| 413 |
callop(int o) |
| 414 |
{ |
| 415 |
if (o >= MAXOP+1) |
| 416 |
cerror("callop"); |
| 417 |
return (dope[o]&CALLFLG); |
| 418 |
} |
| 419 |
|
| 420 |
#else |
| 421 |
|
| 422 |
#define optype(o) (dope[o]&TYFLG) |
| 423 |
#define asgop(o) (dope[o]&ASGFLG) |
| 424 |
#define logop(o) (dope[o]&LOGFLG) |
| 425 |
#define callop(o) (dope[o]&CALLFLG) |
| 426 |
|
| 427 |
#endif |
| 428 |
|
| 429 |
|
| 430 |
#define R2PACK(x,y,z) (0200*((x)+1)+y+040000*z) |
| 431 |
#define R2UPK1(x) ((((x)>>7)-1)&0177) |
| 432 |
#define R2UPK2(x) ((x)&0177) |
| 433 |
#define R2UPK3(x) (x>>14) |
| 434 |
#define R2TEST(x) ((x)>=0200) |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
#define ISMOPS 001 |
| 446 |
#define RREG 002 |
| 447 |
#define RVEFF 004 |
| 448 |
#define RVCC 010 |
| 449 |
#define DORIGHT 020 |
| 450 |
#define SCLASS(v,x) ((v) |= ((x) << 5)) |
| 451 |
#define TCLASS(x) (((x) >> 5) & 7) |
| 452 |
#define TBSH 8 |
| 453 |
#define TBLIDX(idx) ((idx) >> TBSH) |
| 454 |
#define MKIDX(tbl,mod) (((tbl) << TBSH) | (mod)) |
| 455 |
|
| 456 |
#ifndef BREGS |
| 457 |
#define BREGS 0 |
| 458 |
#define TBREGS 0 |
| 459 |
#endif |
| 460 |
#define REGBIT(x) (1 << (x)) |
| 461 |
#ifndef PERMTYPE |
| 462 |
#define PERMTYPE(a) (INT) |
| 463 |
#endif |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
#define DO_LIVEDEAD 0x01 |
| 468 |
|
| 469 |
#define DO_AVAILEXPR 0x02 |
| 470 |
|
| 471 |
#define DO_UPDATELD 0x04 |
| 472 |
|
| 473 |
#define DO_UPDATEEX 0x08 |
| 474 |
|
| 475 |
void emit(struct interpass *); |
| 476 |
void optimize(struct p2env *); |
| 477 |
|
| 478 |
struct basicblock { |
| 479 |
DLIST_ENTRY(basicblock) bbelem; |
| 480 |
SLIST_HEAD(, cfgnode) parents; |
| 481 |
SLIST_HEAD(, cfgnode) child; |
| 482 |
int bbnum; |
| 483 |
unsigned int dfnum; |
| 484 |
unsigned int dfparent; |
| 485 |
unsigned int semi; |
| 486 |
unsigned int ancestor; |
| 487 |
unsigned int idom; |
| 488 |
unsigned int samedom; |
| 489 |
bittype *bucket; |
| 490 |
bittype *df; |
| 491 |
bittype *dfchildren; |
| 492 |
bittype *Aorig; |
| 493 |
bittype *Aphi; |
| 494 |
SLIST_HEAD(, phiinfo) phi; |
| 495 |
|
| 496 |
bittype *gen, *killed, *in, *out; |
| 497 |
|
| 498 |
struct interpass *first; |
| 499 |
struct interpass *last; |
| 500 |
}; |
| 501 |
|
| 502 |
struct labelinfo { |
| 503 |
struct basicblock **arr; |
| 504 |
int size; |
| 505 |
unsigned int low; |
| 506 |
}; |
| 507 |
|
| 508 |
struct bblockinfo { |
| 509 |
int size; |
| 510 |
struct basicblock **arr; |
| 511 |
}; |
| 512 |
|
| 513 |
struct varinfo { |
| 514 |
struct pvarinfo **arr; |
| 515 |
SLIST_HEAD(, varstack) *stack; |
| 516 |
int size; |
| 517 |
int low; |
| 518 |
}; |
| 519 |
|
| 520 |
struct pvarinfo { |
| 521 |
struct pvarinfo *next; |
| 522 |
struct basicblock *bb; |
| 523 |
TWORD n_type; |
| 524 |
}; |
| 525 |
|
| 526 |
struct varstack { |
| 527 |
SLIST_ENTRY(varstack) varstackelem; |
| 528 |
int tmpregno; |
| 529 |
}; |
| 530 |
|
| 531 |
|
| 532 |
struct cfgnode { |
| 533 |
SLIST_ENTRY(cfgnode) cfgelem; |
| 534 |
SLIST_ENTRY(cfgnode) chld; |
| 535 |
struct basicblock *bblock; |
| 536 |
}; |
| 537 |
|
| 538 |
struct phiinfo { |
| 539 |
SLIST_ENTRY(phiinfo) phielem; |
| 540 |
int tmpregno; |
| 541 |
int newtmpregno; |
| 542 |
TWORD n_type; |
| 543 |
int size; |
| 544 |
int *intmpregno; |
| 545 |
}; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
struct p2env { |
| 554 |
struct interpass ipole; |
| 555 |
struct interpass_prolog *ipp, *epp; |
| 556 |
struct bblockinfo bbinfo; |
| 557 |
struct labelinfo labinfo; |
| 558 |
struct basicblock bblocks; |
| 559 |
int nbblocks; |
| 560 |
#define NIPPREGS BIT2BYTE(MAXREGS)/sizeof(bittype) |
| 561 |
bittype p_regs[NIPPREGS]; |
| 562 |
}; |
| 563 |
|
| 564 |
extern struct p2env p2env; |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
#define PHI (MAXOP + 1) |
| 570 |
|
| 571 |
enum { |
| 572 |
ATTR_P2_FIRST = ATTR_MI_MAX + 1, |
| 573 |
ATTR_P2STRUCT, |
| 574 |
#ifdef ATTR_P2_TARGET |
| 575 |
ATTR_P2_TARGET, |
| 576 |
#endif |
| 577 |
ATTR_P2_MAX |
| 578 |
}; |