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
),
44 int gpr0
; /* first general purpose reg */
46 int fpr0
; /* first floating point reg */
48 bits rglob
; /* globally live regs (e.g., sp, fp) */
50 int *rsave
; /* caller-save */
52 bits (*retregs
)(Ref
, int[2]);
53 bits (*argregs
)(Ref
, int[2]);
58 void (*emitfn
)(Fn
*, FILE *);
59 void (*emitfin
)(FILE *);
64 #define BIT(n) ((bits)1 << (n))
68 Tmp0
= NBit
, /* first non-reg temporary */
91 #define TMP(x) (Ref){RTmp, x}
92 #define CON(x) (Ref){RCon, x}
93 #define CON_Z CON(0) /* reserved zero constant */
94 #define SLOT(x) (Ref){RSlot, (x)&0x1fffffff}
95 #define TYPE(x) (Ref){RType, x}
96 #define CALL(x) (Ref){RCall, x}
97 #define MEM(x) (Ref){RMem, x}
99 static inline int req(Ref a
, Ref b
)
101 return a
.type
== b
.type
&& a
.val
== b
.val
;
104 static inline int rtype(Ref r
)
135 NCmp
= NCmpI
+ NCmpF
,
140 #define O(op, x, y) O##op,
148 X(retw) X(retl) X(rets) X(retd) \
149 X(retsb) X(retub) X(retsh) X(retuh) \
150 X(retc) X(ret0) X(jmp) X(jnz) \
151 X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
152 X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
153 X(jfiule) X(jfiult) X(jffeq) X(jffge) \
154 X(jffgt) X(jffle) X(jfflt) X(jffne) \
180 #define INRANGE(x, l, u) ((unsigned)(x) - l <= u - l) /* linear in x */
181 #define isstore(o) INRANGE(o, Ostoreb, Ostored)
182 #define isload(o) INRANGE(o, Oloadsb, Oload)
183 #define isext(o) INRANGE(o, Oextsb, Oextuw)
184 #define ispar(o) INRANGE(o, Opar, Opare)
185 #define isarg(o) INRANGE(o, Oarg, Oargv)
186 #define isret(j) INRANGE(j, Jretw, Jret0)
187 #define isparbh(o) INRANGE(o, Oparsb, Oparuh)
188 #define isargbh(o) INRANGE(o, Oargsb, Oarguh)
189 #define isretbh(j) INRANGE(j, Jretsb, Jretuh)
192 Kx
= -1, /* "top" class (see usecheck() and clsmerge()) */
199 #define KWIDE(k) ((k)&1)
200 #define KBASE(k) ((k)>>1)
246 BSet in
[1], out
[1], gen
[1];
275 ALoc
= 1, /* stack local */
277 AEsc
= 3, /* stack escaping */
280 #define astack(t) ((t) & 1)
293 uint bid
; /* id of a defining block */
295 int slot
; /* -1 for unset */
298 int r
; /* register or -1 */
300 bits m
; /* avoid these registers */
306 Wsb
, /* must match Oload/Oext order */
332 char flt
; /* 1 to print as s, 2 to print as d */
335 typedef struct Addr Addr
;
337 struct Addr
{ /* amd64 addressing */
361 int retty
; /* index in typ[], -1 if no aggregate return */
391 uint len
; /* or index in typ[] for FTyp */
392 } (*fields
)[NField
+1];
423 extern char debug
['Z'+1];
427 PHeap
, /* free() necessary */
428 PFn
, /* discarded after processing the function */
432 extern Ins insb
[NIns
], *curi
;
433 uint32_t hash(char *);
434 void die_(char *, char *, ...) __attribute__((noreturn
));
435 void *emalloc(size_t);
438 void *vnew(ulong
, size_t, Pool
);
440 void vgrow(void *, ulong
);
441 uint32_t intern(char *);
443 int argcls(Ins
*, int);
445 int iscmp(int, int *, int *);
446 void emit(int, int, Ref
, Ref
, Ref
);
448 void idup(Ins
**, Ins
*, ulong
);
449 Ins
*icpy(Ins
*, Ins
*, ulong
);
452 int clsmerge(short *, short);
453 int phicls(int, Tmp
*);
454 Ref
newtmp(char *, int, Fn
*);
455 void chuse(Ref
, int, Fn
*);
456 Ref
newcon(Con
*, Fn
*);
457 Ref
getcon(int64_t, Fn
*);
458 int addcon(Con
*, Con
*);
459 void blit(Ref
, uint
, Ref
, uint
, uint
, Fn
*);
460 void blit0(Ref
, Ref
, uint
, Fn
*);
461 void salloc(Ref
, Ref
, Fn
*);
462 void dumpts(BSet
*, Tmp
*, FILE *);
464 void bsinit(BSet
*, uint
);
466 uint
bscount(BSet
*);
467 void bsset(BSet
*, uint
);
468 void bsclr(BSet
*, uint
);
469 void bscopy(BSet
*, BSet
*);
470 void bsunion(BSet
*, BSet
*);
471 void bsinter(BSet
*, BSet
*);
472 void bsdiff(BSet
*, BSet
*);
473 int bsequal(BSet
*, BSet
*);
474 int bsiter(BSet
*, int *);
477 bshas(BSet
*bs
, uint elt
)
479 assert(elt
< bs
->nt
* NBit
);
480 return (bs
->t
[elt
/NBit
] & BIT(elt
%NBit
)) != 0;
484 extern Op optab
[NOp
];
485 void parse(FILE *, char *, void (Dat
*), void (Fn
*));
486 void printfn(Fn
*, FILE *);
487 void printref(Ref
, Fn
*, FILE *);
488 void err(char *, ...) __attribute__((noreturn
));
495 void edgedel(Blk
*, Blk
**);
496 void fillpreds(Fn
*);
499 int sdom(Blk
*, Blk
*);
500 int dom(Blk
*, Blk
*);
502 void loopiter(Fn
*, void (*)(Blk
*, Blk
*));
510 void fillalias(Fn
*);
511 int alias(Ref
, int, Ref
, int, int *, Fn
*);
512 int escapes(Ref
, Fn
*);
521 void fillpreds(Fn
*);
533 void liveon(BSet
*, Blk
*, Blk
*);
544 void emitfnlnk(char *, Lnk
*, FILE *);
545 void emitdat(Dat
*, FILE *);
546 int stashbits(void *, int);
547 void elf_emitfnfin(char *, FILE *);
548 void elf_emitfin(FILE *);
549 void macho_emitfin(FILE *);