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 Sym Sym
;
24 typedef struct Alias Alias
;
25 typedef struct Tmp Tmp
;
26 typedef struct Con Con
;
27 typedef struct Addr Mem
;
29 typedef struct Typ Typ
;
30 typedef struct Field Field
;
31 typedef struct Dat Dat
;
32 typedef struct Lnk Lnk
;
33 typedef struct Target Target
;
40 NBit
= CHAR_BIT
* sizeof(bits
),
46 int gpr0
; /* first general purpose reg */
48 int fpr0
; /* first floating point reg */
50 bits rglob
; /* globally live regs (e.g., sp, fp) */
52 int *rsave
; /* caller-save */
54 bits (*retregs
)(Ref
, int[2]);
55 bits (*argregs
)(Ref
, int[2]);
60 void (*emitfn
)(Fn
*, FILE *);
61 void (*emitfin
)(FILE *);
66 #define BIT(n) ((bits)1 << (n))
70 Tmp0
= NBit
, /* first non-reg temporary */
87 RType
, /* last kind to come out of the parser */
93 #define R (Ref){RTmp, 0}
94 #define UNDEF (Ref){RCon, 0} /* represents uninitialized data */
95 #define CON_Z (Ref){RCon, 1}
96 #define TMP(x) (Ref){RTmp, x}
97 #define CON(x) (Ref){RCon, x}
98 #define SLOT(x) (Ref){RSlot, (x)&0x1fffffff}
99 #define TYPE(x) (Ref){RType, x}
100 #define CALL(x) (Ref){RCall, x}
101 #define MEM(x) (Ref){RMem, x}
102 #define INT(x) (Ref){RInt, (x)&0x1fffffff}
104 static inline int req(Ref a
, Ref b
)
106 return a
.type
== b
.type
&& a
.val
== b
.val
;
109 static inline int rtype(Ref r
)
116 static inline int rsval(Ref r
)
118 return ((int32_t)r
.val
<< 3) >> 3;
145 NCmp
= NCmpI
+ NCmpF
,
150 #define O(op, x, y) O##op,
158 X(retw) X(retl) X(rets) X(retd) \
159 X(retsb) X(retub) X(retsh) X(retuh) \
160 X(retc) X(ret0) X(jmp) X(jnz) \
161 X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
162 X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
163 X(jfiule) X(jfiult) X(jffeq) X(jffge) \
164 X(jffgt) X(jffle) X(jfflt) X(jffne) \
165 X(jffo) X(jffuo) X(hlt)
190 #define INRANGE(x, l, u) ((unsigned)(x) - l <= u - l) /* linear in x */
191 #define isstore(o) INRANGE(o, Ostoreb, Ostored)
192 #define isload(o) INRANGE(o, Oloadsb, Oload)
193 #define isext(o) INRANGE(o, Oextsb, Oextuw)
194 #define ispar(o) INRANGE(o, Opar, Opare)
195 #define isarg(o) INRANGE(o, Oarg, Oargv)
196 #define isret(j) INRANGE(j, Jretw, Jret0)
197 #define isparbh(o) INRANGE(o, Oparsb, Oparuh)
198 #define isargbh(o) INRANGE(o, Oargsb, Oarguh)
199 #define isretbh(j) INRANGE(j, Jretsb, Jretuh)
202 Kx
= -1, /* "top" class (see usecheck() and clsmerge()) */
209 #define KWIDE(k) ((k)&1)
210 #define KBASE(k) ((k)>>1)
256 BSet in
[1], out
[1], gen
[1];
293 ALoc
= 1, /* stack local */
295 AEsc
= 3, /* stack escaping */
298 #define astack(t) ((t) & 1)
305 int sz
; /* -1 if > NBit */
317 uint bid
; /* id of a defining block */
319 int slot
; /* -1 for unset */
322 int r
; /* register or -1 */
324 bits m
; /* avoid these registers */
330 Wsb
, /* must match Oload/Oext order */
352 char flt
; /* 1 to print as s, 2 to print as d */
355 typedef struct Addr Addr
;
357 struct Addr
{ /* amd64 addressing */
381 int retty
; /* index in typ[], -1 if no aggregate return */
411 uint len
; /* or index in typ[] for FTyp */
412 } (*fields
)[NField
+1];
443 extern char debug
['Z'+1];
447 PHeap
, /* free() necessary */
448 PFn
, /* discarded after processing the function */
452 extern Ins insb
[NIns
], *curi
;
453 uint32_t hash(char *);
454 void die_(char *, char *, ...) __attribute__((noreturn
));
455 void *emalloc(size_t);
458 void *vnew(ulong
, size_t, Pool
);
460 void vgrow(void *, ulong
);
461 uint32_t intern(char *);
463 int argcls(Ins
*, int);
465 int iscmp(int, int *, int *);
466 void emit(int, int, Ref
, Ref
, Ref
);
468 void idup(Ins
**, Ins
*, ulong
);
469 Ins
*icpy(Ins
*, Ins
*, ulong
);
472 int clsmerge(short *, short);
473 int phicls(int, Tmp
*);
474 Ref
newtmp(char *, int, Fn
*);
475 void chuse(Ref
, int, Fn
*);
477 Ref
newcon(Con
*, Fn
*);
478 Ref
getcon(int64_t, Fn
*);
479 int addcon(Con
*, Con
*);
480 void salloc(Ref
, Ref
, Fn
*);
481 void dumpts(BSet
*, Tmp
*, FILE *);
483 void bsinit(BSet
*, uint
);
485 uint
bscount(BSet
*);
486 void bsset(BSet
*, uint
);
487 void bsclr(BSet
*, uint
);
488 void bscopy(BSet
*, BSet
*);
489 void bsunion(BSet
*, BSet
*);
490 void bsinter(BSet
*, BSet
*);
491 void bsdiff(BSet
*, BSet
*);
492 int bsequal(BSet
*, BSet
*);
493 int bsiter(BSet
*, int *);
496 bshas(BSet
*bs
, uint elt
)
498 assert(elt
< bs
->nt
* NBit
);
499 return (bs
->t
[elt
/NBit
] & BIT(elt
%NBit
)) != 0;
503 extern Op optab
[NOp
];
504 void parse(FILE *, char *, void (Dat
*), void (Fn
*));
505 void printfn(Fn
*, FILE *);
506 void printref(Ref
, Fn
*, FILE *);
507 void err(char *, ...) __attribute__((noreturn
));
514 void edgedel(Blk
*, Blk
**);
515 void fillpreds(Fn
*);
518 int sdom(Blk
*, Blk
*);
519 int dom(Blk
*, Blk
*);
521 void loopiter(Fn
*, void (*)(Blk
*, Blk
*));
530 void fillalias(Fn
*);
531 void getalias(Alias
*, Ref
, Fn
*);
532 int alias(Ref
, int, int, Ref
, int, int *, Fn
*);
533 int escapes(Ref
, Fn
*);
542 void fillpreds(Fn
*);
557 void liveon(BSet
*, Blk
*, Blk
*);
568 void emitfnlnk(char *, Lnk
*, FILE *);
569 void emitdat(Dat
*, FILE *);
570 int stashbits(void *, int);
571 void elf_emitfnfin(char *, FILE *);
572 void elf_emitfin(FILE *);
573 void macho_emitfin(FILE *);