add asm diffing in test script
[qbe.git] / all.h
blobe53e2903b4cdd18f618512beb05f90c51eef3a7f
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 Target Target;
33 enum {
34 NString = 64,
35 NPred = 127,
36 NIns = 1 << 20,
37 NAlign = 3,
38 NField = 32,
39 NBit = CHAR_BIT * sizeof(bits),
42 struct Target {
43 int gpr0; /* first general purpose reg */
44 int ngpr;
45 int fpr0; /* first floating point reg */
46 int nfpr;
47 bits rglob; /* globally live regs (e.g., sp, fp) */
48 int nrglob;
49 int *rsave; /* caller-save */
50 int nrsave[2];
51 bits (*retregs)(Ref, int[2]);
52 bits (*argregs)(Ref, int[2]);
53 int (*memargs)(int);
54 void (*abi)(Fn *);
55 void (*isel)(Fn *);
56 void (*emitfn)(Fn *, FILE *);
59 #define BIT(n) ((bits)1 << (n))
61 enum {
62 RXX = 0,
63 Tmp0 = NBit, /* first non-reg temporary */
66 struct BSet {
67 uint nt;
68 bits *t;
71 struct Ref {
72 uint type:3;
73 uint val:29;
76 enum {
77 RTmp,
78 RCon,
79 RType,
80 RSlot,
81 RCall,
82 RMem,
85 #define R (Ref){0, 0}
86 #define TMP(x) (Ref){RTmp, x}
87 #define CON(x) (Ref){RCon, x}
88 #define CON_Z CON(0) /* reserved zero constant */
89 #define SLOT(x) (Ref){RSlot, (x)&0x1fffffff}
90 #define TYPE(x) (Ref){RType, x}
91 #define CALL(x) (Ref){RCall, x}
92 #define MEM(x) (Ref){RMem, x}
94 static inline int req(Ref a, Ref b)
96 return a.type == b.type && a.val == b.val;
99 static inline int rtype(Ref r)
101 if (req(r, R))
102 return -1;
103 return r.type;
106 enum CmpI {
107 Cieq,
108 Cine,
109 Cisge,
110 Cisgt,
111 Cisle,
112 Cislt,
113 Ciuge,
114 Ciugt,
115 Ciule,
116 Ciult,
117 NCmpI,
120 enum CmpF {
121 Cfeq,
122 Cfge,
123 Cfgt,
124 Cfle,
125 Cflt,
126 Cfne,
127 Cfo,
128 Cfuo,
129 NCmpF,
130 NCmp = NCmpI + NCmpF,
133 enum O {
134 Oxxx,
135 #define O(op, x, y) O##op,
136 #include "ops.h"
137 NOp,
140 enum J {
141 Jxxx,
142 #define JMPS(X) \
143 X(ret0) X(retw) X(retl) X(rets) \
144 X(retd) X(retc) X(jmp) X(jnz) \
145 X(jfieq) X(jfine) X(jfisge) X(jfisgt) \
146 X(jfisle) X(jfislt) X(jfiuge) X(jfiugt) \
147 X(jfiule) X(jfiult) X(jffeq) X(jffge) \
148 X(jffgt) X(jffle) X(jfflt) X(jffne) \
149 X(jffo) X(jffuo)
150 #define X(j) J##j,
151 JMPS(X)
152 #undef X
153 NJmp
156 enum {
157 Ocmpw = Oceqw,
158 Ocmpw1 = Ocultw,
159 Ocmpl = Oceql,
160 Ocmpl1 = Ocultl,
161 Ocmps = Oceqs,
162 Ocmps1 = Ocuos,
163 Ocmpd = Oceqd,
164 Ocmpd1 = Ocuod,
165 Oalloc = Oalloc4,
166 Oalloc1 = Oalloc16,
167 Oflag = Oflagieq,
168 Oflag1 = Oflagfuo,
169 NPubOp = Onop,
170 Jjf = Jjfieq,
171 Jjf1 = Jjffuo,
174 #define INRANGE(x, l, u) ((unsigned)(x) - l <= u - l) /* linear in x */
175 #define iscall(o) INRANGE(o, Ocall, Ovacall)
176 #define isstore(o) INRANGE(o, Ostoreb, Ostored)
177 #define isload(o) INRANGE(o, Oloadsb, Oload)
178 #define isext(o) INRANGE(o, Oextsb, Oextuw)
179 #define ispar(o) INRANGE(o, Opar, Opare)
180 #define isarg(o) INRANGE(o, Oarg, Oarge)
181 #define isret(j) INRANGE(j, Jret0, Jretc)
183 enum Class {
184 Kx = -1, /* "top" class (see usecheck() and clsmerge()) */
191 #define KWIDE(k) ((k)&1)
192 #define KBASE(k) ((k)>>1)
194 struct Op {
195 char *name;
196 short argcls[2][4];
197 int canfold;
200 struct Ins {
201 uint op:30;
202 uint cls:2;
203 Ref to;
204 Ref arg[2];
207 struct Phi {
208 Ref to;
209 Ref arg[NPred];
210 Blk *blk[NPred];
211 uint narg;
212 int cls;
213 Phi *link;
216 struct Blk {
217 Phi *phi;
218 Ins *ins;
219 uint nins;
220 struct {
221 short type;
222 Ref arg;
223 } jmp;
224 Blk *s1;
225 Blk *s2;
226 Blk *link;
228 uint id;
229 uint visit;
231 Blk *idom;
232 Blk *dom, *dlink;
233 Blk **fron;
234 uint nfron;
236 Blk **pred;
237 uint npred;
238 BSet in[1], out[1], gen[1];
239 int nlive[2];
240 int loop;
241 char name[NString];
244 struct Use {
245 enum {
246 UXXX,
247 UPhi,
248 UIns,
249 UJmp,
250 } type;
251 uint bid;
252 union {
253 Ins *ins;
254 Phi *phi;
255 } u;
258 enum {
259 NoAlias,
260 MayAlias,
261 MustAlias
264 struct Alias {
265 enum {
266 ABot = 0,
267 ALoc = 1, /* stack local */
268 ACon = 2,
269 AEsc = 3, /* stack escaping */
270 ASym = 4,
271 AUnk = 6,
272 #define astack(t) ((t) & 1)
273 } type;
274 Ref base;
275 uint32_t label;
276 int64_t offset;
277 Alias *slot;
280 struct Tmp {
281 char name[NString];
282 Use *use;
283 uint ndef, nuse;
284 uint bid; /* id of a defining block */
285 uint cost;
286 int slot; /* -1 for unset */
287 short cls;
288 struct {
289 int r; /* register or -1 */
290 int w; /* weight */
291 bits m; /* avoid these registers */
292 } hint;
293 int phi;
294 Alias alias;
295 enum {
296 WFull,
297 Wsb, /* must match Oload/Oext order */
298 Wub,
299 Wsh,
300 Wuh,
301 Wsw,
303 } width;
304 int visit;
307 struct Con {
308 enum {
309 CUndef,
310 CBits,
311 CAddr,
312 } type;
313 uint32_t label;
314 union {
315 int64_t i;
316 double d;
317 float s;
318 } bits;
319 char flt; /* 1 to print as s, 2 to print as d */
320 char local;
323 typedef struct Addr Addr;
325 struct Addr { /* amd64 addressing */
326 Con offset;
327 Ref base;
328 Ref index;
329 int scale;
332 struct Fn {
333 Blk *start;
334 Tmp *tmp;
335 Con *con;
336 Mem *mem;
337 int ntmp;
338 int ncon;
339 int nmem;
340 uint nblk;
341 int retty; /* index in typ[], -1 if no aggregate return */
342 Ref retr;
343 Blk **rpo;
344 bits reg;
345 int slot;
346 char export;
347 char vararg;
348 char dynalloc;
349 char name[NString];
352 struct Typ {
353 char name[NString];
354 int dark;
355 int align;
356 uint64_t size;
357 uint nunion;
358 struct Field {
359 enum {
360 FEnd,
367 FPad,
368 FTyp,
369 } type;
370 uint len; /* or index in typ[] for FTyp */
371 } (*fields)[NField+1];
374 struct Dat {
375 enum {
376 DStart,
377 DEnd,
378 DName,
379 DAlign,
385 } type;
386 union {
387 int64_t num;
388 double fltd;
389 float flts;
390 char *str;
391 struct {
392 char *nam;
393 int64_t off;
394 } ref;
395 } u;
396 char isref;
397 char isstr;
398 char export;
401 /* main.c */
402 extern Target T;
403 extern char debug['Z'+1];
405 /* util.c */
406 typedef enum {
407 Pheap, /* free() necessary */
408 Pfn, /* discarded after processing the function */
409 } Pool;
411 extern Typ *typ;
412 extern Ins insb[NIns], *curi;
413 uint32_t hash(char *);
414 void die_(char *, char *, ...) __attribute__((noreturn));
415 void *emalloc(size_t);
416 void *alloc(size_t);
417 void freeall(void);
418 void *vnew(ulong, size_t, Pool);
419 void vfree(void *);
420 void vgrow(void *, ulong);
421 uint32_t intern(char *);
422 char *str(uint32_t);
423 int argcls(Ins *, int);
424 int isreg(Ref);
425 int iscmp(int, int *, int *);
426 void emit(int, int, Ref, Ref, Ref);
427 void emiti(Ins);
428 void idup(Ins **, Ins *, ulong);
429 Ins *icpy(Ins *, Ins *, ulong);
430 int cmpop(int);
431 int cmpneg(int);
432 int clsmerge(short *, short);
433 int phicls(int, Tmp *);
434 Ref newtmp(char *, int, Fn *);
435 void chuse(Ref, int, Fn *);
436 Ref getcon(int64_t, Fn *);
437 void addcon(Con *, Con *);
438 void blit(Ref, uint, Ref, uint, Fn *);
439 void dumpts(BSet *, Tmp *, FILE *);
441 void bsinit(BSet *, uint);
442 void bszero(BSet *);
443 uint bscount(BSet *);
444 void bsset(BSet *, uint);
445 void bsclr(BSet *, uint);
446 void bscopy(BSet *, BSet *);
447 void bsunion(BSet *, BSet *);
448 void bsinter(BSet *, BSet *);
449 void bsdiff(BSet *, BSet *);
450 int bsequal(BSet *, BSet *);
451 int bsiter(BSet *, int *);
453 static inline int
454 bshas(BSet *bs, uint elt)
456 assert(elt < bs->nt * NBit);
457 return (bs->t[elt/NBit] & BIT(elt%NBit)) != 0;
460 /* parse.c */
461 extern Op optab[NOp];
462 void parse(FILE *, char *, void (Dat *), void (Fn *));
463 void printfn(Fn *, FILE *);
464 void printref(Ref, Fn *, FILE *);
465 void err(char *, ...) __attribute__((noreturn));
467 /* cfg.c */
468 Blk *blknew(void);
469 void edgedel(Blk *, Blk **);
470 void fillpreds(Fn *);
471 void fillrpo(Fn *);
472 void filldom(Fn *);
473 int sdom(Blk *, Blk *);
474 int dom(Blk *, Blk *);
475 void fillfron(Fn *);
476 void loopiter(Fn *, void (*)(Blk *, Blk *));
477 void fillloop(Fn *);
478 void simpljmp(Fn *);
480 /* mem.c */
481 void memopt(Fn *);
483 /* alias.c */
484 void fillalias(Fn *);
485 int alias(Ref, int, Ref, int, int *, Fn *);
486 int escapes(Ref, Fn *);
488 /* load.c */
489 int loadsz(Ins *);
490 int storesz(Ins *);
491 void loadopt(Fn *);
493 /* ssa.c */
494 void filluse(Fn *);
495 void fillpreds(Fn *);
496 void fillrpo(Fn *);
497 void ssa(Fn *);
498 void ssacheck(Fn *);
500 /* simpl.c */
501 void simpl(Fn *);
503 /* copy.c */
504 void copy(Fn *);
506 /* fold.c */
507 void fold(Fn *);
509 /* live.c */
510 void liveon(BSet *, Blk *, Blk *);
511 void filllive(Fn *);
513 /* spill.c */
514 void fillcost(Fn *);
515 void spill(Fn *);
517 /* rega.c */
518 void rega(Fn *);
520 /* gas.c */
521 extern char *gasloc;
522 extern char *gassym;
523 void gasemitdat(Dat *, FILE *);
524 int gasstash(void *, int);
525 void gasemitfin(FILE *);