Add a negation instruction
[qbe.git] / parse.c
blobc05c370f5ab2fad600515a277086fae4d38cacb3
1 #include "all.h"
2 #include <ctype.h>
3 #include <stdarg.h>
5 enum {
6 Ke = -2, /* Erroneous mode */
7 Km = Kl, /* Memory pointer */
8 };
10 Op optab[NOp] = {
11 #define O(op, t, cf) [O##op]={#op, t, cf},
12 #include "ops.h"
15 typedef enum {
16 PXXX,
17 PLbl,
18 PPhi,
19 PIns,
20 PEnd,
21 } PState;
23 enum {
24 Txxx = 0,
26 /* aliases */
27 Tloadw = NPubOp,
28 Tloadl,
29 Tloads,
30 Tloadd,
31 Talloc1,
32 Talloc2,
34 Tcall,
35 Tenv,
36 Tphi,
37 Tjmp,
38 Tjnz,
39 Tret,
40 Texport,
41 Tfunc,
42 Ttype,
43 Tdata,
44 Tsection,
45 Talign,
46 Tl,
47 Tw,
48 Th,
49 Tb,
50 Td,
51 Ts,
52 Tz,
54 Tint,
55 Tflts,
56 Tfltd,
57 Ttmp,
58 Tlbl,
59 Tglo,
60 Ttyp,
61 Tstr,
63 Tplus,
64 Teq,
65 Tcomma,
66 Tlparen,
67 Trparen,
68 Tlbrace,
69 Trbrace,
70 Tnl,
71 Tdots,
72 Teof,
74 Ntok
77 static char *kwmap[Ntok] = {
78 [Tloadw] = "loadw",
79 [Tloadl] = "loadl",
80 [Tloads] = "loads",
81 [Tloadd] = "loadd",
82 [Talloc1] = "alloc1",
83 [Talloc2] = "alloc2",
84 [Tcall] = "call",
85 [Tenv] = "env",
86 [Tphi] = "phi",
87 [Tjmp] = "jmp",
88 [Tjnz] = "jnz",
89 [Tret] = "ret",
90 [Texport] = "export",
91 [Tfunc] = "function",
92 [Ttype] = "type",
93 [Tdata] = "data",
94 [Tsection] = "section",
95 [Talign] = "align",
96 [Tl] = "l",
97 [Tw] = "w",
98 [Th] = "h",
99 [Tb] = "b",
100 [Td] = "d",
101 [Ts] = "s",
102 [Tz] = "z",
103 [Tdots] = "...",
106 enum {
107 NPred = 63,
109 TMask = 16383, /* for temps hash */
110 BMask = 8191, /* for blocks hash */
112 K = 4331239, /* found using tools/lexh.c */
113 M = 23,
116 static char lexh[1 << (32-M)];
117 static FILE *inf;
118 static char *inpath;
119 static int thead;
120 static struct {
121 char chr;
122 double fltd;
123 float flts;
124 int64_t num;
125 char *str;
126 } tokval;
127 static int lnum;
129 static Fn *curf;
130 static int tmph[TMask+1];
131 static Phi **plink;
132 static Blk *curb;
133 static Blk **blink;
134 static Blk *blkh[BMask+1];
135 static int nblk;
136 static int rcls;
137 static uint ntyp;
139 void
140 err(char *s, ...)
142 va_list ap;
144 va_start(ap, s);
145 fprintf(stderr, "%s:%d: ", inpath, lnum);
146 vfprintf(stderr, s, ap);
147 fprintf(stderr, "\n");
148 va_end(ap);
149 exit(1);
152 static void
153 lexinit()
155 static int done;
156 int i;
157 long h;
159 if (done)
160 return;
161 for (i=0; i<NPubOp; ++i)
162 if (optab[i].name)
163 kwmap[i] = optab[i].name;
164 assert(Ntok <= CHAR_MAX);
165 for (i=0; i<Ntok; ++i)
166 if (kwmap[i]) {
167 h = hash(kwmap[i])*K >> M;
168 assert(lexh[h] == Txxx);
169 lexh[h] = i;
171 done = 1;
174 static int64_t
175 getint()
177 uint64_t n;
178 int c, m;
180 n = 0;
181 c = fgetc(inf);
182 m = (c == '-');
183 if (m || c == '+')
184 c = fgetc(inf);
185 do {
186 n = 10*n + (c - '0');
187 c = fgetc(inf);
188 } while ('0' <= c && c <= '9');
189 ungetc(c, inf);
190 if (m)
191 n = 1 + ~n;
192 return *(int64_t *)&n;
195 static int
196 lex()
198 static char tok[NString];
199 int c, i, esc;
200 int t;
203 c = fgetc(inf);
204 while (isblank(c));
205 t = Txxx;
206 tokval.chr = c;
207 switch (c) {
208 case EOF:
209 return Teof;
210 case ',':
211 return Tcomma;
212 case '(':
213 return Tlparen;
214 case ')':
215 return Trparen;
216 case '{':
217 return Tlbrace;
218 case '}':
219 return Trbrace;
220 case '=':
221 return Teq;
222 case '+':
223 return Tplus;
224 case 's':
225 if (fscanf(inf, "_%f", &tokval.flts) != 1)
226 break;
227 return Tflts;
228 case 'd':
229 if (fscanf(inf, "_%lf", &tokval.fltd) != 1)
230 break;
231 return Tfltd;
232 case '%':
233 t = Ttmp;
234 c = fgetc(inf);
235 goto Alpha;
236 case '@':
237 t = Tlbl;
238 c = fgetc(inf);
239 goto Alpha;
240 case '$':
241 t = Tglo;
242 if ((c = fgetc(inf)) == '"')
243 goto Quoted;
244 goto Alpha;
245 case ':':
246 t = Ttyp;
247 c = fgetc(inf);
248 goto Alpha;
249 case '#':
250 while ((c=fgetc(inf)) != '\n' && c != EOF)
252 /* fall through */
253 case '\n':
254 lnum++;
255 return Tnl;
257 if (isdigit(c) || c == '-' || c == '+') {
258 ungetc(c, inf);
259 tokval.num = getint();
260 return Tint;
262 if (c == '"') {
263 t = Tstr;
264 Quoted:
265 tokval.str = vnew(2, 1, Pfn);
266 tokval.str[0] = c;
267 esc = 0;
268 for (i=1;; i++) {
269 c = fgetc(inf);
270 if (c == EOF)
271 err("unterminated string");
272 vgrow(&tokval.str, i+2);
273 tokval.str[i] = c;
274 if (c == '"' && !esc) {
275 tokval.str[i+1] = 0;
276 return t;
278 esc = (c == '\\' && !esc);
281 Alpha:
282 if (!isalpha(c) && c != '.' && c != '_')
283 err("invalid character %c (%d)", c, c);
284 i = 0;
285 do {
286 if (i >= NString-1)
287 err("identifier too long");
288 tok[i++] = c;
289 c = fgetc(inf);
290 } while (isalpha(c) || c == '$' || c == '.' || c == '_' || isdigit(c));
291 tok[i] = 0;
292 ungetc(c, inf);
293 tokval.str = tok;
294 if (t != Txxx) {
295 return t;
297 t = lexh[hash(tok)*K >> M];
298 if (t == Txxx || strcmp(kwmap[t], tok) != 0) {
299 err("unknown keyword %s", tok);
300 return Txxx;
302 return t;
305 static int
306 peek()
308 if (thead == Txxx)
309 thead = lex();
310 return thead;
313 static int
314 next()
316 int t;
318 t = peek();
319 thead = Txxx;
320 return t;
323 static int
324 nextnl()
326 int t;
328 while ((t = next()) == Tnl)
330 return t;
333 static void
334 expect(int t)
336 static char *ttoa[] = {
337 [Tlbl] = "label",
338 [Tcomma] = ",",
339 [Teq] = "=",
340 [Tnl] = "newline",
341 [Tlparen] = "(",
342 [Trparen] = ")",
343 [Tlbrace] = "{",
344 [Trbrace] = "}",
345 [Teof] = 0,
347 char buf[128], *s1, *s2;
348 int t1;
350 t1 = next();
351 if (t == t1)
352 return;
353 s1 = ttoa[t] ? ttoa[t] : "??";
354 s2 = ttoa[t1] ? ttoa[t1] : "??";
355 sprintf(buf, "%s expected, got %s instead", s1, s2);
356 err(buf);
359 static Ref
360 tmpref(char *v)
362 int t, *h;
364 h = &tmph[hash(v) & TMask];
365 t = *h;
366 if (t) {
367 if (strcmp(curf->tmp[t].name, v) == 0)
368 return TMP(t);
369 for (t=curf->ntmp-1; t>=Tmp0; t--)
370 if (strcmp(curf->tmp[t].name, v) == 0)
371 return TMP(t);
373 t = curf->ntmp;
374 *h = t;
375 newtmp(0, Kx, curf);
376 strcpy(curf->tmp[t].name, v);
377 return TMP(t);
380 static Ref
381 parseref()
383 Con c;
385 memset(&c, 0, sizeof c);
386 switch (next()) {
387 case Ttmp:
388 return tmpref(tokval.str);
389 case Tint:
390 c.type = CBits;
391 c.bits.i = tokval.num;
392 goto Look;
393 case Tflts:
394 c.type = CBits;
395 c.bits.s = tokval.flts;
396 c.flt = 1;
397 goto Look;
398 case Tfltd:
399 c.type = CBits;
400 c.bits.d = tokval.fltd;
401 c.flt = 2;
402 goto Look;
403 case Tglo:
404 c.type = CAddr;
405 c.label = intern(tokval.str);
406 Look:
407 return newcon(&c, curf);
408 default:
409 return R;
413 static int
414 findtyp(int i)
416 while (--i >= 0)
417 if (strcmp(tokval.str, typ[i].name) == 0)
418 return i;
419 err("undefined type :%s", tokval.str);
422 static int
423 parsecls(int *tyn)
425 switch (next()) {
426 default:
427 err("invalid class specifier");
428 case Ttyp:
429 *tyn = findtyp(ntyp);
430 return 4;
431 case Tw:
432 return Kw;
433 case Tl:
434 return Kl;
435 case Ts:
436 return Ks;
437 case Td:
438 return Kd;
442 static int
443 parserefl(int arg)
445 int k, ty, env, hasenv, vararg;
446 Ref r;
448 hasenv = 0;
449 vararg = 0;
450 expect(Tlparen);
451 while (peek() != Trparen) {
452 if (curi - insb >= NIns)
453 err("too many instructions (1)");
454 if (!arg && vararg)
455 err("no parameters allowed after '...'");
456 switch (peek()) {
457 case Tdots:
458 if (vararg)
459 err("only one '...' allowed");
460 vararg = 1;
461 if (arg) {
462 *curi = (Ins){.op = Oargv};
463 curi++;
465 next();
466 goto Next;
467 case Tenv:
468 if (hasenv)
469 err("only one environment allowed");
470 hasenv = 1;
471 env = 1;
472 next();
473 k = Kl;
474 break;
475 default:
476 env = 0;
477 k = parsecls(&ty);
478 break;
480 r = parseref();
481 if (req(r, R))
482 err("invalid argument");
483 if (!arg && rtype(r) != RTmp)
484 err("invalid function parameter");
485 if (k == 4)
486 if (arg)
487 *curi = (Ins){Oargc, Kl, R, {TYPE(ty), r}};
488 else
489 *curi = (Ins){Oparc, Kl, r, {TYPE(ty)}};
490 else if (env)
491 if (arg)
492 *curi = (Ins){Oarge, k, R, {r}};
493 else
494 *curi = (Ins){Opare, k, r, {R}};
495 else
496 if (arg)
497 *curi = (Ins){Oarg, k, R, {r}};
498 else
499 *curi = (Ins){Opar, k, r, {R}};
500 curi++;
501 Next:
502 if (peek() == Trparen)
503 break;
504 expect(Tcomma);
506 expect(Trparen);
507 return vararg;
510 static Blk *
511 findblk(char *name)
513 Blk *b;
514 uint32_t h;
516 h = hash(name) & BMask;
517 for (b=blkh[h]; b; b=b->dlink)
518 if (strcmp(b->name, name) == 0)
519 return b;
520 b = blknew();
521 b->id = nblk++;
522 strcpy(b->name, name);
523 b->dlink = blkh[h];
524 blkh[h] = b;
525 return b;
528 static void
529 closeblk()
531 curb->nins = curi - insb;
532 idup(&curb->ins, insb, curb->nins);
533 blink = &curb->link;
534 curi = insb;
537 static PState
538 parseline(PState ps)
540 Ref arg[NPred] = {R};
541 Blk *blk[NPred];
542 Phi *phi;
543 Ref r;
544 Blk *b;
545 int t, op, i, k, ty;
547 t = nextnl();
548 if (ps == PLbl && t != Tlbl && t != Trbrace)
549 err("label or } expected");
550 switch (t) {
551 default:
552 if (isstore(t)) {
553 case Tcall:
554 case Ovastart:
555 /* operations without result */
556 r = R;
557 k = Kw;
558 op = t;
559 goto DoOp;
561 err("label, instruction or jump expected");
562 case Trbrace:
563 return PEnd;
564 case Ttmp:
565 break;
566 case Tlbl:
567 b = findblk(tokval.str);
568 if (curb && curb->jmp.type == Jxxx) {
569 closeblk();
570 curb->jmp.type = Jjmp;
571 curb->s1 = b;
573 if (b->jmp.type != Jxxx)
574 err("multiple definitions of block @%s", b->name);
575 *blink = b;
576 curb = b;
577 plink = &curb->phi;
578 expect(Tnl);
579 return PPhi;
580 case Tret:
581 curb->jmp.type = (int[]){
582 Jretw, Jretl,
583 Jrets, Jretd,
584 Jretc, Jret0
585 }[rcls];
586 if (peek() == Tnl)
587 curb->jmp.type = Jret0;
588 else if (rcls < 5) {
589 r = parseref();
590 if (req(r, R))
591 err("invalid return value");
592 curb->jmp.arg = r;
594 goto Close;
595 case Tjmp:
596 curb->jmp.type = Jjmp;
597 goto Jump;
598 case Tjnz:
599 curb->jmp.type = Jjnz;
600 r = parseref();
601 if (req(r, R))
602 err("invalid argument for jnz jump");
603 curb->jmp.arg = r;
604 expect(Tcomma);
605 Jump:
606 expect(Tlbl);
607 curb->s1 = findblk(tokval.str);
608 if (curb->jmp.type != Jjmp) {
609 expect(Tcomma);
610 expect(Tlbl);
611 curb->s2 = findblk(tokval.str);
613 if (curb->s1 == curf->start || curb->s2 == curf->start)
614 err("invalid jump to the start node");
615 Close:
616 expect(Tnl);
617 closeblk();
618 return PLbl;
620 r = tmpref(tokval.str);
621 expect(Teq);
622 k = parsecls(&ty);
623 op = next();
624 DoOp:
625 if (op == Tphi) {
626 if (ps != PPhi || curb == curf->start)
627 err("unexpected phi instruction");
628 op = -1;
630 if (op == Tcall) {
631 arg[0] = parseref();
632 parserefl(1);
633 op = Ocall;
634 expect(Tnl);
635 if (k == 4) {
636 k = Kl;
637 arg[1] = TYPE(ty);
638 } else
639 arg[1] = R;
640 goto Ins;
642 if (op == Tloadw)
643 op = Oloadsw;
644 if (op >= Tloadl && op <= Tloadd)
645 op = Oload;
646 if (op == Talloc1 || op == Talloc2)
647 op = Oalloc;
648 if (k == 4)
649 err("size class must be w, l, s, or d");
650 if (op >= NPubOp)
651 err("invalid instruction");
652 i = 0;
653 if (peek() != Tnl)
654 for (;;) {
655 if (i == NPred)
656 err("too many arguments");
657 if (op == -1) {
658 expect(Tlbl);
659 blk[i] = findblk(tokval.str);
661 arg[i] = parseref();
662 if (req(arg[i], R))
663 err("invalid instruction argument");
664 i++;
665 t = peek();
666 if (t == Tnl)
667 break;
668 if (t != Tcomma)
669 err(", or end of line expected");
670 next();
672 next();
673 Ins:
674 if (op != -1) {
675 if (curi - insb >= NIns)
676 err("too many instructions (2)");
677 curi->op = op;
678 curi->cls = k;
679 curi->to = r;
680 curi->arg[0] = arg[0];
681 curi->arg[1] = arg[1];
682 curi++;
683 return PIns;
684 } else {
685 phi = alloc(sizeof *phi);
686 phi->to = r;
687 phi->cls = k;
688 phi->arg = vnew(i, sizeof arg[0], Pfn);
689 memcpy(phi->arg, arg, i * sizeof arg[0]);
690 phi->blk = vnew(i, sizeof blk[0], Pfn);
691 memcpy(phi->blk, blk, i * sizeof blk[0]);
692 phi->narg = i;
693 *plink = phi;
694 plink = &phi->link;
695 return PPhi;
699 static int
700 usecheck(Ref r, int k, Fn *fn)
702 return rtype(r) != RTmp || fn->tmp[r.val].cls == k
703 || (fn->tmp[r.val].cls == Kl && k == Kw);
706 static void
707 typecheck(Fn *fn)
709 Blk *b;
710 Phi *p;
711 Ins *i;
712 uint n;
713 int k;
714 Tmp *t;
715 Ref r;
716 BSet pb[1], ppb[1];
718 fillpreds(fn);
719 bsinit(pb, fn->nblk);
720 bsinit(ppb, fn->nblk);
721 for (b=fn->start; b; b=b->link) {
722 for (p=b->phi; p; p=p->link)
723 fn->tmp[p->to.val].cls = p->cls;
724 for (i=b->ins; i<&b->ins[b->nins]; i++)
725 if (rtype(i->to) == RTmp) {
726 t = &fn->tmp[i->to.val];
727 if (clsmerge(&t->cls, i->cls))
728 err("temporary %%%s is assigned with"
729 " multiple types", t->name);
732 for (b=fn->start; b; b=b->link) {
733 bszero(pb);
734 for (n=0; n<b->npred; n++)
735 bsset(pb, b->pred[n]->id);
736 for (p=b->phi; p; p=p->link) {
737 bszero(ppb);
738 t = &fn->tmp[p->to.val];
739 for (n=0; n<p->narg; n++) {
740 k = t->cls;
741 if (bshas(ppb, p->blk[n]->id))
742 err("multiple entries for @%s in phi %%%s",
743 p->blk[n]->name, t->name);
744 if (!usecheck(p->arg[n], k, fn))
745 err("invalid type for operand %%%s in phi %%%s",
746 fn->tmp[p->arg[n].val].name, t->name);
747 bsset(ppb, p->blk[n]->id);
749 if (!bsequal(pb, ppb))
750 err("predecessors not matched in phi %%%s", t->name);
752 for (i=b->ins; i<&b->ins[b->nins]; i++)
753 for (n=0; n<2; n++) {
754 k = optab[i->op].argcls[n][i->cls];
755 r = i->arg[n];
756 t = &fn->tmp[r.val];
757 if (k == Ke)
758 err("invalid instruction type in %s",
759 optab[i->op].name);
760 if (rtype(r) == RType)
761 continue;
762 if (rtype(r) != -1 && k == Kx)
763 err("no %s operand expected in %s",
764 n == 1 ? "second" : "first",
765 optab[i->op].name);
766 if (rtype(r) == -1 && k != Kx)
767 err("missing %s operand in %s",
768 n == 1 ? "second" : "first",
769 optab[i->op].name);
770 if (!usecheck(r, k, fn))
771 err("invalid type for %s operand %%%s in %s",
772 n == 1 ? "second" : "first",
773 t->name, optab[i->op].name);
775 r = b->jmp.arg;
776 if (isret(b->jmp.type)) {
777 if (b->jmp.type == Jretc) {
778 if (!usecheck(r, Kl, fn))
779 goto JErr;
780 } else if (!usecheck(r, b->jmp.type-Jretw, fn))
781 goto JErr;
783 if (b->jmp.type == Jjnz && !usecheck(r, Kw, fn))
784 JErr:
785 err("invalid type for jump argument %%%s in block @%s",
786 fn->tmp[r.val].name, b->name);
787 if (b->s1 && b->s1->jmp.type == Jxxx)
788 err("block @%s is used undefined", b->s1->name);
789 if (b->s2 && b->s2->jmp.type == Jxxx)
790 err("block @%s is used undefined", b->s2->name);
794 static Fn *
795 parsefn(int export)
797 Blk *b;
798 int i;
799 PState ps;
801 curb = 0;
802 nblk = 0;
803 curi = insb;
804 curf = alloc(sizeof *curf);
805 curf->ntmp = 0;
806 curf->ncon = 1; /* first constant must be 0 */
807 curf->tmp = vnew(curf->ntmp, sizeof curf->tmp[0], Pfn);
808 curf->con = vnew(curf->ncon, sizeof curf->con[0], Pfn);
809 for (i=0; i<Tmp0; ++i)
810 if (T.fpr0 <= i && i < T.fpr0 + T.nfpr)
811 newtmp(0, Kd, curf);
812 else
813 newtmp(0, Kl, curf);
814 curf->con[0].type = CBits;
815 curf->export = export;
816 blink = &curf->start;
817 curf->retty = Kx;
818 if (peek() != Tglo)
819 rcls = parsecls(&curf->retty);
820 else
821 rcls = 5;
822 if (next() != Tglo)
823 err("function name expected");
824 strncpy(curf->name, tokval.str, NString-1);
825 curf->vararg = parserefl(0);
826 if (nextnl() != Tlbrace)
827 err("function body must start with {");
828 ps = PLbl;
830 ps = parseline(ps);
831 while (ps != PEnd);
832 if (!curb)
833 err("empty function");
834 if (curb->jmp.type == Jxxx)
835 err("last block misses jump");
836 curf->mem = vnew(0, sizeof curf->mem[0], Pfn);
837 curf->nmem = 0;
838 curf->nblk = nblk;
839 curf->rpo = 0;
840 for (b=0; b; b=b->link)
841 b->dlink = 0; /* was trashed by findblk() */
842 for (i=0; i<BMask+1; ++i)
843 blkh[i] = 0;
844 memset(tmph, 0, sizeof tmph);
845 typecheck(curf);
846 return curf;
849 static void
850 parsefields(Field *fld, Typ *ty, int t)
852 Typ *ty1;
853 int n, c, a, al, type;
854 uint64_t sz, s;
856 n = 0;
857 sz = 0;
858 al = ty->align;
859 while (t != Trbrace) {
860 ty1 = 0;
861 switch (t) {
862 default: err("invalid type member specifier");
863 case Td: type = Fd; s = 8; a = 3; break;
864 case Tl: type = Fl; s = 8; a = 3; break;
865 case Ts: type = Fs; s = 4; a = 2; break;
866 case Tw: type = Fw; s = 4; a = 2; break;
867 case Th: type = Fh; s = 2; a = 1; break;
868 case Tb: type = Fb; s = 1; a = 0; break;
869 case Ttyp:
870 type = FTyp;
871 ty1 = &typ[findtyp(ntyp-1)];
872 s = ty1->size;
873 a = ty1->align;
874 break;
876 if (a > al)
877 al = a;
878 a = (1 << a) - 1;
879 a = ((sz + a) & ~a) - sz;
880 if (a) {
881 if (n < NField) {
882 /* padding */
883 fld[n].type = FPad;
884 fld[n].len = a;
885 n++;
888 t = nextnl();
889 if (t == Tint) {
890 c = tokval.num;
891 t = nextnl();
892 } else
893 c = 1;
894 sz += a + c*s;
895 if (type == FTyp)
896 s = ty1 - typ;
897 for (; c>0 && n<NField; c--, n++) {
898 fld[n].type = type;
899 fld[n].len = s;
901 if (t != Tcomma)
902 break;
903 t = nextnl();
905 if (t != Trbrace)
906 err(", or } expected");
907 fld[n].type = FEnd;
908 a = 1 << al;
909 if (sz < ty->size)
910 sz = ty->size;
911 ty->size = (sz + a - 1) & -a;
912 ty->align = al;
915 static void
916 parsetyp()
918 Typ *ty;
919 int t, al;
920 uint n;
922 /* be careful if extending the syntax
923 * to handle nested types, any pointer
924 * held to typ[] might be invalidated!
926 vgrow(&typ, ntyp+1);
927 ty = &typ[ntyp++];
928 ty->dark = 0;
929 ty->align = -1;
930 ty->size = 0;
931 if (nextnl() != Ttyp || nextnl() != Teq)
932 err("type name and then = expected");
933 strcpy(ty->name, tokval.str);
934 t = nextnl();
935 if (t == Talign) {
936 if (nextnl() != Tint)
937 err("alignment expected");
938 for (al=0; tokval.num /= 2; al++)
940 ty->align = al;
941 t = nextnl();
943 if (t != Tlbrace)
944 err("type body must start with {");
945 t = nextnl();
946 if (t == Tint) {
947 ty->dark = 1;
948 ty->size = tokval.num;
949 if (ty->align == -1)
950 err("dark types need alignment");
951 if (nextnl() != Trbrace)
952 err("} expected");
953 return;
955 n = 0;
956 ty->fields = vnew(1, sizeof ty->fields[0], Pheap);
957 if (t == Tlbrace)
958 do {
959 if (t != Tlbrace)
960 err("invalid union member");
961 vgrow(&ty->fields, n+1);
962 parsefields(ty->fields[n++], ty, nextnl());
963 t = nextnl();
964 } while (t != Trbrace);
965 else
966 parsefields(ty->fields[n++], ty, t);
967 ty->nunion = n;
970 static void
971 parsedatref(Dat *d)
973 int t;
975 d->isref = 1;
976 d->u.ref.nam = tokval.str;
977 d->u.ref.off = 0;
978 t = peek();
979 if (t == Tplus) {
980 next();
981 if (next() != Tint)
982 err("invalid token after offset in ref");
983 d->u.ref.off = tokval.num;
987 static void
988 parsedatstr(Dat *d)
990 d->isstr = 1;
991 d->u.str = tokval.str;
994 static void
995 parsedat(void cb(Dat *), int export)
997 char name[NString] = {0};
998 int t;
999 Dat d;
1001 if (nextnl() != Tglo || nextnl() != Teq)
1002 err("data name, then = expected");
1003 strncpy(name, tokval.str, NString-1);
1004 t = nextnl();
1005 d.u.str = 0;
1006 if (t == Tsection) {
1007 if (nextnl() != Tstr)
1008 err("section \"name\" expected");
1009 d.u.str = tokval.str;
1010 t = nextnl();
1012 d.type = DStart;
1013 cb(&d);
1014 if (t == Talign) {
1015 if (nextnl() != Tint)
1016 err("alignment expected");
1017 d.type = DAlign;
1018 d.u.num = tokval.num;
1019 d.isstr = 0;
1020 d.isref = 0;
1021 cb(&d);
1022 t = nextnl();
1024 d.type = DName;
1025 d.u.str = name;
1026 d.export = export;
1027 cb(&d);
1029 if (t != Tlbrace)
1030 err("expected data contents in { .. }");
1031 for (;;) {
1032 switch (nextnl()) {
1033 default: err("invalid size specifier %c in data", tokval.chr);
1034 case Trbrace: goto Done;
1035 case Tl: d.type = DL; break;
1036 case Tw: d.type = DW; break;
1037 case Th: d.type = DH; break;
1038 case Tb: d.type = DB; break;
1039 case Ts: d.type = DW; break;
1040 case Td: d.type = DL; break;
1041 case Tz: d.type = DZ; break;
1043 t = nextnl();
1044 do {
1045 d.isstr = 0;
1046 d.isref = 0;
1047 memset(&d.u, 0, sizeof d.u);
1048 if (t == Tflts)
1049 d.u.flts = tokval.flts;
1050 else if (t == Tfltd)
1051 d.u.fltd = tokval.fltd;
1052 else if (t == Tint)
1053 d.u.num = tokval.num;
1054 else if (t == Tglo)
1055 parsedatref(&d);
1056 else if (t == Tstr)
1057 parsedatstr(&d);
1058 else
1059 err("constant literal expected");
1060 cb(&d);
1061 t = nextnl();
1062 } while (t == Tint || t == Tflts || t == Tfltd);
1063 if (t == Trbrace)
1064 break;
1065 if (t != Tcomma)
1066 err(", or } expected");
1068 Done:
1069 d.type = DEnd;
1070 cb(&d);
1073 void
1074 parse(FILE *f, char *path, void data(Dat *), void func(Fn *))
1076 int t, export;
1078 lexinit();
1079 inf = f;
1080 inpath = path;
1081 lnum = 1;
1082 thead = Txxx;
1083 ntyp = 0;
1084 typ = vnew(0, sizeof typ[0], Pheap);
1085 for (;;) {
1086 export = 0;
1087 switch (nextnl()) {
1088 default:
1089 err("top-level definition expected");
1090 case Texport:
1091 export = 1;
1092 t = nextnl();
1093 if (t == Tfunc) {
1094 case Tfunc:
1095 func(parsefn(export));
1096 break;
1098 else if (t == Tdata) {
1099 case Tdata:
1100 parsedat(data, export);
1101 break;
1103 else
1104 err("export can only qualify data and function");
1105 case Ttype:
1106 parsetyp();
1107 break;
1108 case Teof:
1109 vfree(typ);
1110 return;
1115 static void
1116 printcon(Con *c, FILE *f)
1118 switch (c->type) {
1119 case CUndef:
1120 break;
1121 case CAddr:
1122 fprintf(f, "$%s", str(c->label));
1123 if (c->bits.i)
1124 fprintf(f, "%+"PRIi64, c->bits.i);
1125 break;
1126 case CBits:
1127 if (c->flt == 1)
1128 fprintf(f, "s_%f", c->bits.s);
1129 else if (c->flt == 2)
1130 fprintf(f, "d_%lf", c->bits.d);
1131 else
1132 fprintf(f, "%"PRIi64, c->bits.i);
1133 break;
1137 void
1138 printref(Ref r, Fn *fn, FILE *f)
1140 int i;
1141 Mem *m;
1143 switch (rtype(r)) {
1144 case RTmp:
1145 if (r.val < Tmp0)
1146 fprintf(f, "R%d", r.val);
1147 else
1148 fprintf(f, "%%%s", fn->tmp[r.val].name);
1149 break;
1150 case RCon:
1151 printcon(&fn->con[r.val], f);
1152 break;
1153 case RSlot:
1154 fprintf(f, "S%d", (r.val&(1<<28)) ? r.val-(1<<29) : r.val);
1155 break;
1156 case RCall:
1157 fprintf(f, "%04x", r.val);
1158 break;
1159 case RType:
1160 fprintf(f, ":%s", typ[r.val].name);
1161 break;
1162 case RMem:
1163 i = 0;
1164 m = &fn->mem[r.val];
1165 fputc('[', f);
1166 if (m->offset.type != CUndef) {
1167 printcon(&m->offset, f);
1168 i = 1;
1170 if (!req(m->base, R)) {
1171 if (i)
1172 fprintf(f, " + ");
1173 printref(m->base, fn, f);
1174 i = 1;
1176 if (!req(m->index, R)) {
1177 if (i)
1178 fprintf(f, " + ");
1179 fprintf(f, "%d * ", m->scale);
1180 printref(m->index, fn, f);
1182 fputc(']', f);
1183 break;
1187 void
1188 printfn(Fn *fn, FILE *f)
1190 static char ktoc[] = "wlsd";
1191 static char *jtoa[NJmp] = {
1192 #define X(j) [J##j] = #j,
1193 JMPS(X)
1194 #undef X
1196 Blk *b;
1197 Phi *p;
1198 Ins *i;
1199 uint n;
1201 if (fn->export)
1202 fprintf(f, "export ");
1203 fprintf(f, "function $%s() {\n", fn->name);
1204 for (b=fn->start; b; b=b->link) {
1205 fprintf(f, "@%s\n", b->name);
1206 for (p=b->phi; p; p=p->link) {
1207 fprintf(f, "\t");
1208 printref(p->to, fn, f);
1209 fprintf(f, " =%c phi ", ktoc[p->cls]);
1210 assert(p->narg);
1211 for (n=0;; n++) {
1212 fprintf(f, "@%s ", p->blk[n]->name);
1213 printref(p->arg[n], fn, f);
1214 if (n == p->narg-1) {
1215 fprintf(f, "\n");
1216 break;
1217 } else
1218 fprintf(f, ", ");
1221 for (i=b->ins; i<&b->ins[b->nins]; i++) {
1222 fprintf(f, "\t");
1223 if (!req(i->to, R)) {
1224 printref(i->to, fn, f);
1225 fprintf(f, " =%c ", ktoc[i->cls]);
1227 assert(optab[i->op].name);
1228 fprintf(f, "%s", optab[i->op].name);
1229 if (req(i->to, R))
1230 switch (i->op) {
1231 case Oarg:
1232 case Oswap:
1233 case Oxcmp:
1234 case Oacmp:
1235 case Oacmn:
1236 case Oafcmp:
1237 case Oxtest:
1238 case Oxdiv:
1239 case Oxidiv:
1240 fputc(ktoc[i->cls], f);
1242 if (!req(i->arg[0], R)) {
1243 fprintf(f, " ");
1244 printref(i->arg[0], fn, f);
1246 if (!req(i->arg[1], R)) {
1247 fprintf(f, ", ");
1248 printref(i->arg[1], fn, f);
1250 fprintf(f, "\n");
1252 switch (b->jmp.type) {
1253 case Jret0:
1254 case Jretw:
1255 case Jretl:
1256 case Jrets:
1257 case Jretd:
1258 case Jretc:
1259 fprintf(f, "\t%s", jtoa[b->jmp.type]);
1260 if (b->jmp.type != Jret0 || !req(b->jmp.arg, R)) {
1261 fprintf(f, " ");
1262 printref(b->jmp.arg, fn, f);
1264 if (b->jmp.type == Jretc)
1265 fprintf(f, ", :%s", typ[fn->retty].name);
1266 fprintf(f, "\n");
1267 break;
1268 case Jjmp:
1269 if (b->s1 != b->link)
1270 fprintf(f, "\tjmp @%s\n", b->s1->name);
1271 break;
1272 default:
1273 fprintf(f, "\t%s ", jtoa[b->jmp.type]);
1274 if (b->jmp.type == Jjnz) {
1275 printref(b->jmp.arg, fn, f);
1276 fprintf(f, ", ");
1278 fprintf(f, "@%s, @%s\n", b->s1->name, b->s2->name);
1279 break;
1282 fprintf(f, "}\n");