flag bad vastart uses
[qbe.git] / all.h
blob0d0bee655b3dfa14855142ec0f2557e691c06a75
1 #include <assert.h>
2 #include <inttypes.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
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;
18 typedef struct Op Op;
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;
27 typedef struct Fn Fn;
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;
34 enum {
35 NString = 72,
36 NIns = 1 << 20,
37 NAlign = 3,
38 NField = 32,
39 NBit = CHAR_BIT * sizeof(bits),
42 struct Target {
43 char name[16];
44 int gpr0; /* first general purpose reg */
45 int ngpr;
46 int fpr0; /* first floating point reg */
47 int nfpr;
48 bits rglob; /* globally live regs (e.g., sp, fp) */
49 int nrglob;
50 int *rsave; /* caller-save */
51 int nrsave[2];
52 bits (*retregs)(Ref, int[2]);
53 bits (*argregs)(Ref, int[2]);
54 int (*memargs)(int);
55 void (*abi0)(Fn *);
56 void (*abi1)(Fn *);
57 void (*isel)(Fn *);
58 void (*emitfn)(Fn *, FILE *);
59 void (*emitfin)(FILE *);
60 char asloc[4];
61 char assym[4];
64 #define BIT(n) ((bits)1 << (n))
66 enum {
67 RXX = 0,
68 Tmp0 = NBit, /* first non-reg temporary */
71 struct BSet {
72 uint nt;
73 bits *t;
76 struct Ref {
77 uint type:3;
78 uint val:29;
81 enum {
82 RTmp,
83 RCon,
84 RType,
85 RSlot,
86 RCall,
87 RMem,
90 #define R (Ref){0, 0}
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)
106 if (req(r, R))
107 return -1;
108 return r.type;
111 enum CmpI {
112 Cieq,
113 Cine,
114 Cisge,
115 Cisgt,
116 Cisle,
117 Cislt,
118 Ciuge,
119 Ciugt,
120 Ciule,
121 Ciult,
122 NCmpI,
125 enum CmpF {
126 Cfeq,
127 Cfge,
128 Cfgt,
129 Cfle,
130 Cflt,
131 Cfne,
132 Cfo,
133 Cfuo,
134 NCmpF,
135 NCmp = NCmpI + NCmpF,
138 enum O {
139 Oxxx,
140 #define O(op, x, y) O##op,
141 #include "ops.h"
142 NOp,
145 enum J {
146 Jxxx,
147 #define JMPS(X) \
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) \
155 X(jffo) X(jffuo)
156 #define X(j) J##j,
157 JMPS(X)
158 #undef X
159 NJmp
162 enum {
163 Ocmpw = Oceqw,
164 Ocmpw1 = Ocultw,
165 Ocmpl = Oceql,
166 Ocmpl1 = Ocultl,
167 Ocmps = Oceqs,
168 Ocmps1 = Ocuos,
169 Ocmpd = Oceqd,
170 Ocmpd1 = Ocuod,
171 Oalloc = Oalloc4,
172 Oalloc1 = Oalloc16,
173 Oflag = Oflagieq,
174 Oflag1 = Oflagfuo,
175 NPubOp = Onop,
176 Jjf = Jjfieq,
177 Jjf1 = Jjffuo,
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)
191 enum {
192 Kx = -1, /* "top" class (see usecheck() and clsmerge()) */
199 #define KWIDE(k) ((k)&1)
200 #define KBASE(k) ((k)>>1)
202 struct Op {
203 char *name;
204 short argcls[2][4];
205 int canfold;
208 struct Ins {
209 uint op:30;
210 uint cls:2;
211 Ref to;
212 Ref arg[2];
215 struct Phi {
216 Ref to;
217 Ref *arg;
218 Blk **blk;
219 uint narg;
220 int cls;
221 Phi *link;
224 struct Blk {
225 Phi *phi;
226 Ins *ins;
227 uint nins;
228 struct {
229 short type;
230 Ref arg;
231 } jmp;
232 Blk *s1;
233 Blk *s2;
234 Blk *link;
236 uint id;
237 uint visit;
239 Blk *idom;
240 Blk *dom, *dlink;
241 Blk **fron;
242 uint nfron;
244 Blk **pred;
245 uint npred;
246 BSet in[1], out[1], gen[1];
247 int nlive[2];
248 int loop;
249 char name[NString];
252 struct Use {
253 enum {
254 UXXX,
255 UPhi,
256 UIns,
257 UJmp,
258 } type;
259 uint bid;
260 union {
261 Ins *ins;
262 Phi *phi;
263 } u;
266 enum {
267 NoAlias,
268 MayAlias,
269 MustAlias
272 struct Alias {
273 enum {
274 ABot = 0,
275 ALoc = 1, /* stack local */
276 ACon = 2,
277 AEsc = 3, /* stack escaping */
278 ASym = 4,
279 AUnk = 6,
280 #define astack(t) ((t) & 1)
281 } type;
282 Ref base;
283 uint32_t label;
284 int64_t offset;
285 Alias *slot;
288 struct Tmp {
289 char name[NString];
290 Use *use;
291 uint ndef, nuse;
292 uint bid; /* id of a defining block */
293 uint cost;
294 int slot; /* -1 for unset */
295 short cls;
296 struct {
297 int r; /* register or -1 */
298 int w; /* weight */
299 bits m; /* avoid these registers */
300 } hint;
301 int phi;
302 Alias alias;
303 enum {
304 WFull,
305 Wsb, /* must match Oload/Oext order */
306 Wub,
307 Wsh,
308 Wuh,
309 Wsw,
311 } width;
312 int visit;
315 struct Con {
316 enum {
317 CUndef,
318 CBits,
319 CAddr,
320 } type;
321 uint32_t label;
322 union {
323 int64_t i;
324 double d;
325 float s;
326 } bits;
327 char flt; /* 1 to print as s, 2 to print as d */
328 char local;
331 typedef struct Addr Addr;
333 struct Addr { /* amd64 addressing */
334 Con offset;
335 Ref base;
336 Ref index;
337 int scale;
340 struct Lnk {
341 char export;
342 char align;
343 char *sec;
344 char *secf;
347 struct Fn {
348 Blk *start;
349 Tmp *tmp;
350 Con *con;
351 Mem *mem;
352 int ntmp;
353 int ncon;
354 int nmem;
355 uint nblk;
356 int retty; /* index in typ[], -1 if no aggregate return */
357 Ref retr;
358 Blk **rpo;
359 bits reg;
360 int slot;
361 char vararg;
362 char dynalloc;
363 char name[NString];
364 Lnk lnk;
367 struct Typ {
368 char name[NString];
369 char isdark;
370 char isunion;
371 int align;
372 uint64_t size;
373 uint nunion;
374 struct Field {
375 enum {
376 FEnd,
383 FPad,
384 FTyp,
385 } type;
386 uint len; /* or index in typ[] for FTyp */
387 } (*fields)[NField+1];
390 struct Dat {
391 enum {
392 DStart,
393 DEnd,
399 } type;
400 char *name;
401 Lnk *lnk;
402 union {
403 int64_t num;
404 double fltd;
405 float flts;
406 char *str;
407 struct {
408 char *name;
409 int64_t off;
410 } ref;
411 } u;
412 char isref;
413 char isstr;
416 /* main.c */
417 extern Target T;
418 extern char debug['Z'+1];
420 /* util.c */
421 typedef enum {
422 PHeap, /* free() necessary */
423 PFn, /* discarded after processing the function */
424 } Pool;
426 extern Typ *typ;
427 extern Ins insb[NIns], *curi;
428 uint32_t hash(char *);
429 void die_(char *, char *, ...) __attribute__((noreturn));
430 void *emalloc(size_t);
431 void *alloc(size_t);
432 void freeall(void);
433 void *vnew(ulong, size_t, Pool);
434 void vfree(void *);
435 void vgrow(void *, ulong);
436 uint32_t intern(char *);
437 char *str(uint32_t);
438 int argcls(Ins *, int);
439 int isreg(Ref);
440 int iscmp(int, int *, int *);
441 void emit(int, int, Ref, Ref, Ref);
442 void emiti(Ins);
443 void idup(Ins **, Ins *, ulong);
444 Ins *icpy(Ins *, Ins *, ulong);
445 int cmpop(int);
446 int cmpneg(int);
447 int clsmerge(short *, short);
448 int phicls(int, Tmp *);
449 Ref newtmp(char *, int, Fn *);
450 void chuse(Ref, int, Fn *);
451 Ref newcon(Con *, Fn *);
452 Ref getcon(int64_t, Fn *);
453 int addcon(Con *, Con *);
454 void blit(Ref, uint, Ref, uint, uint, Fn *);
455 void blit0(Ref, Ref, uint, Fn *);
456 void salloc(Ref, Ref, Fn *);
457 void dumpts(BSet *, Tmp *, FILE *);
459 void bsinit(BSet *, uint);
460 void bszero(BSet *);
461 uint bscount(BSet *);
462 void bsset(BSet *, uint);
463 void bsclr(BSet *, uint);
464 void bscopy(BSet *, BSet *);
465 void bsunion(BSet *, BSet *);
466 void bsinter(BSet *, BSet *);
467 void bsdiff(BSet *, BSet *);
468 int bsequal(BSet *, BSet *);
469 int bsiter(BSet *, int *);
471 static inline int
472 bshas(BSet *bs, uint elt)
474 assert(elt < bs->nt * NBit);
475 return (bs->t[elt/NBit] & BIT(elt%NBit)) != 0;
478 /* parse.c */
479 extern Op optab[NOp];
480 void parse(FILE *, char *, void (Dat *), void (Fn *));
481 void printfn(Fn *, FILE *);
482 void printref(Ref, Fn *, FILE *);
483 void err(char *, ...) __attribute__((noreturn));
485 /* abi.c */
486 void elimsb(Fn *);
488 /* cfg.c */
489 Blk *blknew(void);
490 void edgedel(Blk *, Blk **);
491 void fillpreds(Fn *);
492 void fillrpo(Fn *);
493 void filldom(Fn *);
494 int sdom(Blk *, Blk *);
495 int dom(Blk *, Blk *);
496 void fillfron(Fn *);
497 void loopiter(Fn *, void (*)(Blk *, Blk *));
498 void fillloop(Fn *);
499 void simpljmp(Fn *);
501 /* mem.c */
502 void memopt(Fn *);
504 /* alias.c */
505 void fillalias(Fn *);
506 int alias(Ref, int, Ref, int, int *, Fn *);
507 int escapes(Ref, Fn *);
509 /* load.c */
510 int loadsz(Ins *);
511 int storesz(Ins *);
512 void loadopt(Fn *);
514 /* ssa.c */
515 void filluse(Fn *);
516 void fillpreds(Fn *);
517 void fillrpo(Fn *);
518 void ssa(Fn *);
519 void ssacheck(Fn *);
521 /* copy.c */
522 void copy(Fn *);
524 /* fold.c */
525 void fold(Fn *);
527 /* live.c */
528 void liveon(BSet *, Blk *, Blk *);
529 void filllive(Fn *);
531 /* spill.c */
532 void fillcost(Fn *);
533 void spill(Fn *);
535 /* rega.c */
536 void rega(Fn *);
538 /* emit.c */
539 void emitlnk(char *, Lnk *, char *, FILE *);
540 void emitdat(Dat *, FILE *);
541 int stashbits(void *, int);
542 void elf_emitfnfin(char *, FILE *);
543 void elf_emitfin(FILE *);
544 void macho_emitfin(FILE *);