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)((int64_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 void strf(char[NString
], char *, ...);
462 uint32_t intern(char *);
464 int argcls(Ins
*, int);
466 int iscmp(int, int *, int *);
467 void emit(int, int, Ref
, Ref
, Ref
);
469 void idup(Ins
**, Ins
*, ulong
);
470 Ins
*icpy(Ins
*, Ins
*, ulong
);
473 int clsmerge(short *, short);
474 int phicls(int, Tmp
*);
475 Ref
newtmp(char *, int, Fn
*);
476 void chuse(Ref
, int, Fn
*);
478 Ref
newcon(Con
*, Fn
*);
479 Ref
getcon(int64_t, Fn
*);
480 int addcon(Con
*, Con
*);
481 void salloc(Ref
, Ref
, Fn
*);
482 void dumpts(BSet
*, Tmp
*, FILE *);
484 void bsinit(BSet
*, uint
);
486 uint
bscount(BSet
*);
487 void bsset(BSet
*, uint
);
488 void bsclr(BSet
*, uint
);
489 void bscopy(BSet
*, BSet
*);
490 void bsunion(BSet
*, BSet
*);
491 void bsinter(BSet
*, BSet
*);
492 void bsdiff(BSet
*, BSet
*);
493 int bsequal(BSet
*, BSet
*);
494 int bsiter(BSet
*, int *);
497 bshas(BSet
*bs
, uint elt
)
499 assert(elt
< bs
->nt
* NBit
);
500 return (bs
->t
[elt
/NBit
] & BIT(elt
%NBit
)) != 0;
504 extern Op optab
[NOp
];
505 void parse(FILE *, char *, void (char *), void (Dat
*), void (Fn
*));
506 void printfn(Fn
*, FILE *);
507 void printref(Ref
, Fn
*, FILE *);
508 void err(char *, ...) __attribute__((noreturn
));
515 void edgedel(Blk
*, Blk
**);
516 void fillpreds(Fn
*);
519 int sdom(Blk
*, Blk
*);
520 int dom(Blk
*, Blk
*);
522 void loopiter(Fn
*, void (*)(Blk
*, Blk
*));
531 void fillalias(Fn
*);
532 void getalias(Alias
*, Ref
, Fn
*);
533 int alias(Ref
, int, int, Ref
, int, int *, Fn
*);
534 int escapes(Ref
, Fn
*);
556 void liveon(BSet
*, Blk
*, Blk
*);
567 void emitfnlnk(char *, Lnk
*, FILE *);
568 void emitdat(Dat
*, FILE *);
569 void emitdbgfile(char *, FILE *);
570 void emitdbgloc(uint
, uint
, FILE *);
571 int stashbits(void *, int);
572 void elf_emitfnfin(char *, FILE *);
573 void elf_emitfin(FILE *);
574 void macho_emitfin(FILE *);