8 #define MAKESURE(what, x) typedef char make_sure_##what[(x)?1:-1]
9 #define die(...) die_(__FILE__, __VA_ARGS__)
11 typedef unsigned char uchar
;
12 typedef unsigned int uint
;
13 typedef unsigned long ulong
;
14 typedef unsigned long long bits
;
16 typedef struct BSet BSet
;
17 typedef struct Ref Ref
;
19 typedef struct Ins Ins
;
20 typedef struct Phi Phi
;
21 typedef struct Blk Blk
;
22 typedef struct Use Use
;
23 typedef struct Alias Alias
;
24 typedef struct Tmp Tmp
;
25 typedef struct Con Con
;
26 typedef struct Addr Mem
;
28 typedef struct Typ Typ
;
29 typedef struct Field Field
;
30 typedef struct Dat Dat
;
31 typedef struct Lnk Lnk
;
32 typedef struct Target Target
;
39 NBit
= CHAR_BIT
* sizeof(bits
),
45 int gpr0
; /* first general purpose reg */
47 int fpr0
; /* first floating point reg */
49 bits rglob
; /* globally live regs (e.g., sp, fp) */
51 int *rsave
; /* caller-save */
53 bits (*retregs
)(Ref
, int[2]);
54 bits (*argregs
)(Ref
, int[2]);
59 void (*emitfn
)(Fn
*, FILE *);
60 void (*emitfin
)(FILE *);
65 #define BIT(n) ((bits)1 << (n))
69 Tmp0
= NBit
, /* first non-reg temporary */
92 #define TMP(x) (Ref){RTmp, x}
93 #define CON(x) (Ref){RCon, x}
94 #define CON_Z CON(0) /* reserved zero constant */
95 #define SLOT(x) (Ref){RSlot, (x)&0x1fffffff}
96 #define TYPE(x) (Ref){RType, x}
97 #define CALL(x) (Ref){RCall, x}
98 #define MEM(x) (Ref){RMem, x}
100 static inline int req(Ref a
, Ref b
)
102 return a
.type
== b
.type
&& a
.val
== b
.val
;
105 static inline int rtype(Ref r
)
136 NCmp
= NCmpI
+ NCmpF
,
141 #define O(op, x, y) O##op,
149 X(retw) X(retl) X(rets) X(retd) \
150 X(retsb) X(retub) X(retsh) X(retuh) \
151 X(retc) X(ret0) X(jmp) X(jnz) \
152 X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
153 X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
154 X(jfiule) X(jfiult) X(jffeq) X(jffge) \
155 X(jffgt) X(jffle) X(jfflt) X(jffne) \
181 #define INRANGE(x, l, u) ((unsigned)(x) - l <= u - l) /* linear in x */
182 #define isstore(o) INRANGE(o, Ostoreb, Ostored)
183 #define isload(o) INRANGE(o, Oloadsb, Oload)
184 #define isext(o) INRANGE(o, Oextsb, Oextuw)
185 #define ispar(o) INRANGE(o, Opar, Opare)
186 #define isarg(o) INRANGE(o, Oarg, Oargv)
187 #define isret(j) INRANGE(j, Jretw, Jret0)
188 #define isparbh(o) INRANGE(o, Oparsb, Oparuh)
189 #define isargbh(o) INRANGE(o, Oargsb, Oarguh)
190 #define isretbh(j) INRANGE(j, Jretsb, Jretuh)
193 Kx
= -1, /* "top" class (see usecheck() and clsmerge()) */
200 #define KWIDE(k) ((k)&1)
201 #define KBASE(k) ((k)>>1)
247 BSet in
[1], out
[1], gen
[1];
276 ALoc
= 1, /* stack local */
278 AEsc
= 3, /* stack escaping */
281 #define astack(t) ((t) & 1)
291 int sz
; /* -1 if > NBit */
303 uint bid
; /* id of a defining block */
305 int slot
; /* -1 for unset */
308 int r
; /* register or -1 */
310 bits m
; /* avoid these registers */
316 Wsb
, /* must match Oload/Oext order */
342 char flt
; /* 1 to print as s, 2 to print as d */
345 typedef struct Addr Addr
;
347 struct Addr
{ /* amd64 addressing */
371 int retty
; /* index in typ[], -1 if no aggregate return */
401 uint len
; /* or index in typ[] for FTyp */
402 } (*fields
)[NField
+1];
433 extern char debug
['Z'+1];
437 PHeap
, /* free() necessary */
438 PFn
, /* discarded after processing the function */
442 extern Ins insb
[NIns
], *curi
;
443 uint32_t hash(char *);
444 void die_(char *, char *, ...) __attribute__((noreturn
));
445 void *emalloc(size_t);
448 void *vnew(ulong
, size_t, Pool
);
450 void vgrow(void *, ulong
);
451 uint32_t intern(char *);
453 int argcls(Ins
*, int);
455 int iscmp(int, int *, int *);
456 void emit(int, int, Ref
, Ref
, Ref
);
458 void idup(Ins
**, Ins
*, ulong
);
459 Ins
*icpy(Ins
*, Ins
*, ulong
);
462 int clsmerge(short *, short);
463 int phicls(int, Tmp
*);
464 Ref
newtmp(char *, int, Fn
*);
465 void chuse(Ref
, int, Fn
*);
466 Ref
newcon(Con
*, Fn
*);
467 Ref
getcon(int64_t, Fn
*);
468 int addcon(Con
*, Con
*);
469 void blit(Ref
, uint
, Ref
, uint
, uint
, Fn
*);
470 void blit0(Ref
, Ref
, uint
, Fn
*);
471 void salloc(Ref
, Ref
, Fn
*);
472 void dumpts(BSet
*, Tmp
*, FILE *);
474 void bsinit(BSet
*, uint
);
476 uint
bscount(BSet
*);
477 void bsset(BSet
*, uint
);
478 void bsclr(BSet
*, uint
);
479 void bscopy(BSet
*, BSet
*);
480 void bsunion(BSet
*, BSet
*);
481 void bsinter(BSet
*, BSet
*);
482 void bsdiff(BSet
*, BSet
*);
483 int bsequal(BSet
*, BSet
*);
484 int bsiter(BSet
*, int *);
487 bshas(BSet
*bs
, uint elt
)
489 assert(elt
< bs
->nt
* NBit
);
490 return (bs
->t
[elt
/NBit
] & BIT(elt
%NBit
)) != 0;
494 extern Op optab
[NOp
];
495 void parse(FILE *, char *, void (Dat
*), void (Fn
*));
496 void printfn(Fn
*, FILE *);
497 void printref(Ref
, Fn
*, FILE *);
498 void err(char *, ...) __attribute__((noreturn
));
505 void edgedel(Blk
*, Blk
**);
506 void fillpreds(Fn
*);
509 int sdom(Blk
*, Blk
*);
510 int dom(Blk
*, Blk
*);
512 void loopiter(Fn
*, void (*)(Blk
*, Blk
*));
521 void fillalias(Fn
*);
522 void getalias(Alias
*, Ref
, Fn
*);
523 int alias(Ref
, int, Ref
, int, int *, Fn
*);
524 int escapes(Ref
, Fn
*);
533 void fillpreds(Fn
*);
545 void liveon(BSet
*, Blk
*, Blk
*);
556 void emitfnlnk(char *, Lnk
*, FILE *);
557 void emitdat(Dat
*, FILE *);
558 int stashbits(void *, int);
559 void elf_emitfnfin(char *, FILE *);
560 void elf_emitfin(FILE *);
561 void macho_emitfin(FILE *);