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