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 Num Num
;
25 typedef struct Alias Alias
;
26 typedef struct Tmp Tmp
;
27 typedef struct Con Con
;
28 typedef struct Addr Mem
;
30 typedef struct Typ Typ
;
31 typedef struct Field Field
;
32 typedef struct Dat Dat
;
33 typedef struct Lnk Lnk
;
34 typedef struct Target Target
;
41 NBit
= CHAR_BIT
* sizeof(bits
),
47 int gpr0
; /* first general purpose reg */
49 int fpr0
; /* first floating point reg */
51 bits rglob
; /* globally live regs (e.g., sp, fp) */
53 int *rsave
; /* caller-save */
55 bits (*retregs
)(Ref
, int[2]);
56 bits (*argregs
)(Ref
, int[2]);
61 void (*emitfn
)(Fn
*, FILE *);
62 void (*emitfin
)(FILE *);
67 #define BIT(n) ((bits)1 << (n))
71 Tmp0
= NBit
, /* first non-reg temporary */
88 RType
, /* last kind to come out of the parser */
94 #define R (Ref){RTmp, 0}
95 #define UNDEF (Ref){RCon, 0} /* represents uninitialized data */
96 #define CON_Z (Ref){RCon, 1}
97 #define TMP(x) (Ref){RTmp, x}
98 #define CON(x) (Ref){RCon, x}
99 #define SLOT(x) (Ref){RSlot, (x)&0x1fffffff}
100 #define TYPE(x) (Ref){RType, x}
101 #define CALL(x) (Ref){RCall, x}
102 #define MEM(x) (Ref){RMem, x}
103 #define INT(x) (Ref){RInt, (x)&0x1fffffff}
105 static inline int req(Ref a
, Ref b
)
107 return a
.type
== b
.type
&& a
.val
== b
.val
;
110 static inline int rtype(Ref r
)
117 static inline int rsval(Ref r
)
119 return ((int)r
.val
^ 0x10000000) - 0x10000000;
146 NCmp
= NCmpI
+ NCmpF
,
151 #define O(op, x, y) O##op,
159 X(retw) X(retl) X(rets) X(retd) \
160 X(retsb) X(retub) X(retsh) X(retuh) \
161 X(retc) X(ret0) X(jmp) X(jnz) \
162 X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
163 X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
164 X(jfiule) X(jfiult) X(jffeq) X(jffge) \
165 X(jffgt) X(jffle) X(jfflt) X(jffne) \
166 X(jffo) X(jffuo) X(hlt)
191 #define INRANGE(x, l, u) ((unsigned)(x) - l <= u - l) /* linear in x */
192 #define isstore(o) INRANGE(o, Ostoreb, Ostored)
193 #define isload(o) INRANGE(o, Oloadsb, Oload)
194 #define isext(o) INRANGE(o, Oextsb, Oextuw)
195 #define ispar(o) INRANGE(o, Opar, Opare)
196 #define isarg(o) INRANGE(o, Oarg, Oargv)
197 #define isret(j) INRANGE(j, Jretw, Jret0)
198 #define isparbh(o) INRANGE(o, Oparsb, Oparuh)
199 #define isargbh(o) INRANGE(o, Oargsb, Oarguh)
200 #define isretbh(j) INRANGE(j, Jretsb, Jretuh)
203 Kx
= -1, /* "top" class (see usecheck() and clsmerge()) */
210 #define KWIDE(k) ((k)&1)
211 #define KBASE(k) ((k)>>1)
218 uint idval
:1; /* identity value 0/1 */
259 BSet in
[1], out
[1], gen
[1];
302 ALoc
= 1, /* stack local */
304 AEsc
= 3, /* stack escaping */
307 #define astack(t) ((t) & 1)
314 int sz
; /* -1 if > NBit */
326 uint bid
; /* id of a defining block */
328 int slot
; /* -1 for unset */
331 int r
; /* register or -1 */
333 bits m
; /* avoid these registers */
339 Wsb
, /* must match Oload/Oext order */
361 char flt
; /* 1 to print as s, 2 to print as d */
364 typedef struct Addr Addr
;
366 struct Addr
{ /* amd64 addressing */
391 int retty
; /* index in typ[], -1 if no aggregate return */
423 uint len
; /* or index in typ[] for FTyp */
424 } (*fields
)[NField
+1];
455 extern char debug
['Z'+1];
459 PHeap
, /* free() necessary */
460 PFn
, /* discarded after processing the function */
464 extern Ins insb
[NIns
], *curi
;
465 uint32_t hash(char *);
466 void die_(char *, char *, ...) __attribute__((noreturn
));
467 void *emalloc(size_t);
470 void *vnew(ulong
, size_t, Pool
);
472 void vgrow(void *, ulong
);
473 void strf(char[NString
], char *, ...);
474 uint32_t intern(char *);
476 int argcls(Ins
*, int);
478 int iscmp(int, int *, int *);
479 void emit(int, int, Ref
, Ref
, Ref
);
481 void idup(Ins
**, Ins
*, ulong
);
482 Ins
*icpy(Ins
*, Ins
*, ulong
);
485 int clsmerge(short *, short);
486 int phicls(int, Tmp
*);
487 Ref
newtmp(char *, int, Fn
*);
488 void chuse(Ref
, int, Fn
*);
490 Ref
newcon(Con
*, Fn
*);
491 Ref
getcon(int64_t, Fn
*);
492 int addcon(Con
*, Con
*, int);
493 void salloc(Ref
, Ref
, Fn
*);
494 void dumpts(BSet
*, Tmp
*, FILE *);
495 void runmatch(uchar
*, Num
*, Ref
, Ref
*);
497 void bsinit(BSet
*, uint
);
499 uint
bscount(BSet
*);
500 void bsset(BSet
*, uint
);
501 void bsclr(BSet
*, uint
);
502 void bscopy(BSet
*, BSet
*);
503 void bsunion(BSet
*, BSet
*);
504 void bsinter(BSet
*, BSet
*);
505 void bsdiff(BSet
*, BSet
*);
506 int bsequal(BSet
*, BSet
*);
507 int bsiter(BSet
*, int *);
510 bshas(BSet
*bs
, uint elt
)
512 assert(elt
< bs
->nt
* NBit
);
513 return (bs
->t
[elt
/NBit
] & BIT(elt
%NBit
)) != 0;
517 extern Op optab
[NOp
];
518 void parse(FILE *, char *, void (char *), void (Dat
*), void (Fn
*));
519 void printfn(Fn
*, FILE *);
520 void printref(Ref
, Fn
*, FILE *);
521 void err(char *, ...) __attribute__((noreturn
));
528 void edgedel(Blk
*, Blk
**);
529 void fillpreds(Fn
*);
532 int sdom(Blk
*, Blk
*);
533 int dom(Blk
*, Blk
*);
535 void loopiter(Fn
*, void (*)(Blk
*, Blk
*));
544 void fillalias(Fn
*);
545 void getalias(Alias
*, Ref
, Fn
*);
546 int alias(Ref
, int, int, Ref
, int, int *, Fn
*);
547 int escapes(Ref
, Fn
*);
569 void liveon(BSet
*, Blk
*, Blk
*);
580 void emitfnlnk(char *, Lnk
*, FILE *);
581 void emitdat(Dat
*, FILE *);
582 void emitdbgfile(char *, FILE *);
583 void emitdbgloc(uint
, uint
, FILE *);
584 int stashbits(void *, int);
585 void elf_emitfnfin(char *, FILE *);
586 void elf_emitfin(FILE *);
587 void macho_emitfin(FILE *);