amd64/emit.c: fix %x =k sub %x, %x
[qbe.git] / parse.c
blob43ae23b5b0a76f8a09f30ad899944feee3d32529
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 = 3233235, /* 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;
384 int i;
386 memset(&c, 0, sizeof c);
387 switch (next()) {
388 case Ttmp:
389 return tmpref(tokval.str);
390 case Tint:
391 c.type = CBits;
392 c.bits.i = tokval.num;
393 goto Look;
394 case Tflts:
395 c.type = CBits;
396 c.bits.s = tokval.flts;
397 c.flt = 1;
398 goto Look;
399 case Tfltd:
400 c.type = CBits;
401 c.bits.d = tokval.fltd;
402 c.flt = 2;
403 goto Look;
404 case Tglo:
405 c.type = CAddr;
406 c.label = intern(tokval.str);
407 Look:
408 for (i=0; i<curf->ncon; i++)
409 if (curf->con[i].type == c.type
410 && curf->con[i].bits.i == c.bits.i
411 && curf->con[i].label == c.label)
412 return CON(i);
413 vgrow(&curf->con, ++curf->ncon);
414 curf->con[i] = c;
415 return CON(i);
416 default:
417 return R;
421 static int
422 findtyp(int i)
424 while (--i >= 0)
425 if (strcmp(tokval.str, typ[i].name) == 0)
426 return i;
427 err("undefined type :%s", tokval.str);
430 static int
431 parsecls(int *tyn)
433 switch (next()) {
434 default:
435 err("invalid class specifier");
436 case Ttyp:
437 *tyn = findtyp(ntyp);
438 return 4;
439 case Tw:
440 return Kw;
441 case Tl:
442 return Kl;
443 case Ts:
444 return Ks;
445 case Td:
446 return Kd;
450 static int
451 parserefl(int arg)
453 int k, ty, env, hasenv;
454 Ref r;
456 hasenv = 0;
457 expect(Tlparen);
458 while (peek() != Trparen && peek() != Tdots) {
459 if (curi - insb >= NIns)
460 err("too many instructions (1)");
461 env = peek() == Tenv;
462 if (env) {
463 next();
464 k = Kl;
465 } else
466 k = parsecls(&ty);
467 r = parseref();
468 if (req(r, R))
469 err("invalid argument");
470 if (hasenv && env)
471 err("only one environment allowed");
472 if (!arg && rtype(r) != RTmp)
473 err("invalid function parameter");
474 if (k == 4)
475 if (arg)
476 *curi = (Ins){Oargc, Kl, R, {TYPE(ty), r}};
477 else
478 *curi = (Ins){Oparc, Kl, r, {TYPE(ty)}};
479 else if (env)
480 if (arg)
481 *curi = (Ins){Oarge, k, R, {r}};
482 else
483 *curi = (Ins){Opare, k, r, {R}};
484 else
485 if (arg)
486 *curi = (Ins){Oarg, k, R, {r}};
487 else
488 *curi = (Ins){Opar, k, r, {R}};
489 curi++;
490 hasenv |= env;
491 if (peek() == Trparen)
492 break;
493 expect(Tcomma);
495 if (next() == Tdots) {
496 expect(Trparen);
497 return 1;
499 return 0;
502 static Blk *
503 findblk(char *name)
505 Blk *b;
506 uint32_t h;
508 h = hash(name) & BMask;
509 for (b=blkh[h]; b; b=b->dlink)
510 if (strcmp(b->name, name) == 0)
511 return b;
512 b = blknew();
513 b->id = nblk++;
514 strcpy(b->name, name);
515 b->dlink = blkh[h];
516 blkh[h] = b;
517 return b;
520 static void
521 closeblk()
523 curb->nins = curi - insb;
524 idup(&curb->ins, insb, curb->nins);
525 blink = &curb->link;
526 curi = insb;
529 static PState
530 parseline(PState ps)
532 Ref arg[NPred] = {R};
533 Blk *blk[NPred];
534 Phi *phi;
535 Ref r;
536 Blk *b;
537 int t, op, i, k, ty;
539 t = nextnl();
540 if (ps == PLbl && t != Tlbl && t != Trbrace)
541 err("label or } expected");
542 switch (t) {
543 default:
544 if (isstore(t)) {
545 case Tcall:
546 case Ovastart:
547 /* operations without result */
548 r = R;
549 k = Kw;
550 op = t;
551 goto DoOp;
553 err("label, instruction or jump expected");
554 case Trbrace:
555 return PEnd;
556 case Ttmp:
557 break;
558 case Tlbl:
559 b = findblk(tokval.str);
560 if (curb && curb->jmp.type == Jxxx) {
561 closeblk();
562 curb->jmp.type = Jjmp;
563 curb->s1 = b;
565 if (b->jmp.type != Jxxx)
566 err("multiple definitions of block @%s", b->name);
567 *blink = b;
568 curb = b;
569 plink = &curb->phi;
570 expect(Tnl);
571 return PPhi;
572 case Tret:
573 curb->jmp.type = (int[]){
574 Jretw, Jretl,
575 Jrets, Jretd,
576 Jretc, Jret0
577 }[rcls];
578 if (peek() == Tnl)
579 curb->jmp.type = Jret0;
580 else if (rcls < 5) {
581 r = parseref();
582 if (req(r, R))
583 err("invalid return value");
584 curb->jmp.arg = r;
586 goto Close;
587 case Tjmp:
588 curb->jmp.type = Jjmp;
589 goto Jump;
590 case Tjnz:
591 curb->jmp.type = Jjnz;
592 r = parseref();
593 if (req(r, R))
594 err("invalid argument for jnz jump");
595 curb->jmp.arg = r;
596 expect(Tcomma);
597 Jump:
598 expect(Tlbl);
599 curb->s1 = findblk(tokval.str);
600 if (curb->jmp.type != Jjmp) {
601 expect(Tcomma);
602 expect(Tlbl);
603 curb->s2 = findblk(tokval.str);
605 if (curb->s1 == curf->start || curb->s2 == curf->start)
606 err("invalid jump to the start node");
607 Close:
608 expect(Tnl);
609 closeblk();
610 return PLbl;
612 r = tmpref(tokval.str);
613 expect(Teq);
614 k = parsecls(&ty);
615 op = next();
616 DoOp:
617 if (op == Tphi) {
618 if (ps != PPhi || curb == curf->start)
619 err("unexpected phi instruction");
620 op = -1;
622 if (op == Tcall) {
623 arg[0] = parseref();
624 if (parserefl(1))
625 op = Ovacall;
626 else
627 op = Ocall;
628 expect(Tnl);
629 if (k == 4) {
630 k = Kl;
631 arg[1] = TYPE(ty);
632 } else
633 arg[1] = R;
634 goto Ins;
636 if (op >= Tloadw && op <= Tloadd)
637 op = Oload;
638 if (op == Talloc1 || op == Talloc2)
639 op = Oalloc;
640 if (k == 4)
641 err("size class must be w, l, s, or d");
642 if (op >= NPubOp)
643 err("invalid instruction");
644 i = 0;
645 if (peek() != Tnl)
646 for (;;) {
647 if (i == NPred)
648 err("too many arguments");
649 if (op == -1) {
650 expect(Tlbl);
651 blk[i] = findblk(tokval.str);
653 arg[i] = parseref();
654 if (req(arg[i], R))
655 err("invalid instruction argument");
656 i++;
657 t = peek();
658 if (t == Tnl)
659 break;
660 if (t != Tcomma)
661 err(", or end of line expected");
662 next();
664 next();
665 Ins:
666 if (op != -1) {
667 if (curi - insb >= NIns)
668 err("too many instructions (2)");
669 curi->op = op;
670 curi->cls = k;
671 curi->to = r;
672 curi->arg[0] = arg[0];
673 curi->arg[1] = arg[1];
674 curi++;
675 return PIns;
676 } else {
677 phi = alloc(sizeof *phi);
678 phi->to = r;
679 phi->cls = k;
680 phi->arg = vnew(i, sizeof arg[0], Pfn);
681 memcpy(phi->arg, arg, i * sizeof arg[0]);
682 phi->blk = vnew(i, sizeof blk[0], Pfn);
683 memcpy(phi->blk, blk, i * sizeof blk[0]);
684 phi->narg = i;
685 *plink = phi;
686 plink = &phi->link;
687 return PPhi;
691 static int
692 usecheck(Ref r, int k, Fn *fn)
694 return rtype(r) != RTmp || fn->tmp[r.val].cls == k
695 || (fn->tmp[r.val].cls == Kl && k == Kw);
698 static void
699 typecheck(Fn *fn)
701 Blk *b;
702 Phi *p;
703 Ins *i;
704 uint n;
705 int k;
706 Tmp *t;
707 Ref r;
708 BSet pb[1], ppb[1];
710 fillpreds(fn);
711 bsinit(pb, fn->nblk);
712 bsinit(ppb, fn->nblk);
713 for (b=fn->start; b; b=b->link) {
714 for (p=b->phi; p; p=p->link)
715 fn->tmp[p->to.val].cls = p->cls;
716 for (i=b->ins; i<&b->ins[b->nins]; i++)
717 if (rtype(i->to) == RTmp) {
718 t = &fn->tmp[i->to.val];
719 if (clsmerge(&t->cls, i->cls))
720 err("temporary %%%s is assigned with"
721 " multiple types", t->name);
724 for (b=fn->start; b; b=b->link) {
725 bszero(pb);
726 for (n=0; n<b->npred; n++)
727 bsset(pb, b->pred[n]->id);
728 for (p=b->phi; p; p=p->link) {
729 bszero(ppb);
730 t = &fn->tmp[p->to.val];
731 for (n=0; n<p->narg; n++) {
732 k = t->cls;
733 if (bshas(ppb, p->blk[n]->id))
734 err("multiple entries for @%s in phi %%%s",
735 p->blk[n]->name, t->name);
736 if (!usecheck(p->arg[n], k, fn))
737 err("invalid type for operand %%%s in phi %%%s",
738 fn->tmp[p->arg[n].val].name, t->name);
739 bsset(ppb, p->blk[n]->id);
741 if (!bsequal(pb, ppb))
742 err("predecessors not matched in phi %%%s", t->name);
744 for (i=b->ins; i<&b->ins[b->nins]; i++)
745 for (n=0; n<2; n++) {
746 k = optab[i->op].argcls[n][i->cls];
747 r = i->arg[n];
748 t = &fn->tmp[r.val];
749 if (k == Ke)
750 err("invalid instruction type in %s",
751 optab[i->op].name);
752 if (rtype(r) == RType)
753 continue;
754 if (rtype(r) != -1 && k == Kx)
755 err("no %s operand expected in %s",
756 n == 1 ? "second" : "first",
757 optab[i->op].name);
758 if (rtype(r) == -1 && k != Kx)
759 err("missing %s operand in %s",
760 n == 1 ? "second" : "first",
761 optab[i->op].name);
762 if (!usecheck(r, k, fn))
763 err("invalid type for %s operand %%%s in %s",
764 n == 1 ? "second" : "first",
765 t->name, optab[i->op].name);
767 r = b->jmp.arg;
768 if (isret(b->jmp.type)) {
769 if (b->jmp.type == Jretc) {
770 if (!usecheck(r, Kl, fn))
771 goto JErr;
772 } else if (!usecheck(r, b->jmp.type-Jretw, fn))
773 goto JErr;
775 if (b->jmp.type == Jjnz && !usecheck(r, Kw, fn))
776 JErr:
777 err("invalid type for jump argument %%%s in block @%s",
778 fn->tmp[r.val].name, b->name);
779 if (b->s1 && b->s1->jmp.type == Jxxx)
780 err("block @%s is used undefined", b->s1->name);
781 if (b->s2 && b->s2->jmp.type == Jxxx)
782 err("block @%s is used undefined", b->s2->name);
786 static Fn *
787 parsefn(int export)
789 Blk *b;
790 int i;
791 PState ps;
793 curb = 0;
794 nblk = 0;
795 curi = insb;
796 curf = alloc(sizeof *curf);
797 curf->ntmp = 0;
798 curf->ncon = 1; /* first constant must be 0 */
799 curf->tmp = vnew(curf->ntmp, sizeof curf->tmp[0], Pfn);
800 curf->con = vnew(curf->ncon, sizeof curf->con[0], Pfn);
801 for (i=0; i<Tmp0; ++i)
802 if (T.fpr0 <= i && i < T.fpr0 + T.nfpr)
803 newtmp(0, Kd, curf);
804 else
805 newtmp(0, Kl, curf);
806 curf->con[0].type = CBits;
807 curf->export = export;
808 blink = &curf->start;
809 curf->retty = Kx;
810 if (peek() != Tglo)
811 rcls = parsecls(&curf->retty);
812 else
813 rcls = 5;
814 if (next() != Tglo)
815 err("function name expected");
816 strncpy(curf->name, tokval.str, NString-1);
817 curf->vararg = parserefl(0);
818 if (nextnl() != Tlbrace)
819 err("function body must start with {");
820 ps = PLbl;
822 ps = parseline(ps);
823 while (ps != PEnd);
824 if (!curb)
825 err("empty function");
826 if (curb->jmp.type == Jxxx)
827 err("last block misses jump");
828 curf->mem = vnew(0, sizeof curf->mem[0], Pfn);
829 curf->nmem = 0;
830 curf->nblk = nblk;
831 curf->rpo = 0;
832 for (b=0; b; b=b->link)
833 b->dlink = 0; /* was trashed by findblk() */
834 for (i=0; i<BMask+1; ++i)
835 blkh[i] = 0;
836 memset(tmph, 0, sizeof tmph);
837 typecheck(curf);
838 return curf;
841 static void
842 parsefields(Field *fld, Typ *ty, int t)
844 Typ *ty1;
845 int n, c, a, al, type;
846 uint64_t sz, s;
848 n = 0;
849 sz = 0;
850 al = ty->align;
851 while (t != Trbrace) {
852 ty1 = 0;
853 switch (t) {
854 default: err("invalid type member specifier");
855 case Td: type = Fd; s = 8; a = 3; break;
856 case Tl: type = Fl; s = 8; a = 3; break;
857 case Ts: type = Fs; s = 4; a = 2; break;
858 case Tw: type = Fw; s = 4; a = 2; break;
859 case Th: type = Fh; s = 2; a = 1; break;
860 case Tb: type = Fb; s = 1; a = 0; break;
861 case Ttyp:
862 type = FTyp;
863 ty1 = &typ[findtyp(ntyp-1)];
864 s = ty1->size;
865 a = ty1->align;
866 break;
868 if (a > al)
869 al = a;
870 a = (1 << a) - 1;
871 a = ((sz + a) & ~a) - sz;
872 if (a) {
873 if (n < NField) {
874 /* padding */
875 fld[n].type = FPad;
876 fld[n].len = a;
877 n++;
880 t = nextnl();
881 if (t == Tint) {
882 c = tokval.num;
883 t = nextnl();
884 } else
885 c = 1;
886 sz += a + c*s;
887 if (type == FTyp)
888 s = ty1 - typ;
889 for (; c>0 && n<NField; c--, n++) {
890 fld[n].type = type;
891 fld[n].len = s;
893 if (t != Tcomma)
894 break;
895 t = nextnl();
897 if (t != Trbrace)
898 err(", or } expected");
899 fld[n].type = FEnd;
900 a = 1 << al;
901 if (sz < ty->size)
902 sz = ty->size;
903 ty->size = (sz + a - 1) & -a;
904 ty->align = al;
907 static void
908 parsetyp()
910 Typ *ty;
911 int t, al;
912 uint n;
914 /* be careful if extending the syntax
915 * to handle nested types, any pointer
916 * held to typ[] might be invalidated!
918 vgrow(&typ, ntyp+1);
919 ty = &typ[ntyp++];
920 ty->dark = 0;
921 ty->align = -1;
922 ty->size = 0;
923 if (nextnl() != Ttyp || nextnl() != Teq)
924 err("type name and then = expected");
925 strcpy(ty->name, tokval.str);
926 t = nextnl();
927 if (t == Talign) {
928 if (nextnl() != Tint)
929 err("alignment expected");
930 for (al=0; tokval.num /= 2; al++)
932 ty->align = al;
933 t = nextnl();
935 if (t != Tlbrace)
936 err("type body must start with {");
937 t = nextnl();
938 if (t == Tint) {
939 ty->dark = 1;
940 ty->size = tokval.num;
941 if (ty->align == -1)
942 err("dark types need alignment");
943 if (nextnl() != Trbrace)
944 err("} expected");
945 return;
947 n = 0;
948 ty->fields = vnew(1, sizeof ty->fields[0], Pheap);
949 if (t == Tlbrace)
950 do {
951 if (t != Tlbrace)
952 err("invalid union member");
953 vgrow(&ty->fields, n+1);
954 parsefields(ty->fields[n++], ty, nextnl());
955 t = nextnl();
956 } while (t != Trbrace);
957 else
958 parsefields(ty->fields[n++], ty, t);
959 ty->nunion = n;
962 static void
963 parsedatref(Dat *d)
965 int t;
967 d->isref = 1;
968 d->u.ref.nam = tokval.str;
969 d->u.ref.off = 0;
970 t = peek();
971 if (t == Tplus) {
972 next();
973 if (next() != Tint)
974 err("invalid token after offset in ref");
975 d->u.ref.off = tokval.num;
979 static void
980 parsedatstr(Dat *d)
982 d->isstr = 1;
983 d->u.str = tokval.str;
986 static void
987 parsedat(void cb(Dat *), int export)
989 char name[NString] = {0};
990 int t;
991 Dat d;
993 if (nextnl() != Tglo || nextnl() != Teq)
994 err("data name, then = expected");
995 strncpy(name, tokval.str, NString-1);
996 t = nextnl();
997 d.u.str = 0;
998 if (t == Tsection) {
999 if (nextnl() != Tstr)
1000 err("section \"name\" expected");
1001 d.u.str = tokval.str;
1002 t = nextnl();
1004 d.type = DStart;
1005 cb(&d);
1006 if (t == Talign) {
1007 if (nextnl() != Tint)
1008 err("alignment expected");
1009 d.type = DAlign;
1010 d.u.num = tokval.num;
1011 d.isstr = 0;
1012 d.isref = 0;
1013 cb(&d);
1014 t = nextnl();
1016 d.type = DName;
1017 d.u.str = name;
1018 d.export = export;
1019 cb(&d);
1021 if (t != Tlbrace)
1022 err("expected data contents in { .. }");
1023 for (;;) {
1024 switch (nextnl()) {
1025 default: err("invalid size specifier %c in data", tokval.chr);
1026 case Trbrace: goto Done;
1027 case Tl: d.type = DL; break;
1028 case Tw: d.type = DW; break;
1029 case Th: d.type = DH; break;
1030 case Tb: d.type = DB; break;
1031 case Ts: d.type = DW; break;
1032 case Td: d.type = DL; break;
1033 case Tz: d.type = DZ; break;
1035 t = nextnl();
1036 do {
1037 d.isstr = 0;
1038 d.isref = 0;
1039 memset(&d.u, 0, sizeof d.u);
1040 if (t == Tflts)
1041 d.u.flts = tokval.flts;
1042 else if (t == Tfltd)
1043 d.u.fltd = tokval.fltd;
1044 else if (t == Tint)
1045 d.u.num = tokval.num;
1046 else if (t == Tglo)
1047 parsedatref(&d);
1048 else if (t == Tstr)
1049 parsedatstr(&d);
1050 else
1051 err("constant literal expected");
1052 cb(&d);
1053 t = nextnl();
1054 } while (t == Tint || t == Tflts || t == Tfltd);
1055 if (t == Trbrace)
1056 break;
1057 if (t != Tcomma)
1058 err(", or } expected");
1060 Done:
1061 d.type = DEnd;
1062 cb(&d);
1065 void
1066 parse(FILE *f, char *path, void data(Dat *), void func(Fn *))
1068 int t, export;
1070 lexinit();
1071 inf = f;
1072 inpath = path;
1073 lnum = 1;
1074 thead = Txxx;
1075 ntyp = 0;
1076 typ = vnew(0, sizeof typ[0], Pheap);
1077 for (;;) {
1078 export = 0;
1079 switch (nextnl()) {
1080 default:
1081 err("top-level definition expected");
1082 case Texport:
1083 export = 1;
1084 t = nextnl();
1085 if (t == Tfunc) {
1086 case Tfunc:
1087 func(parsefn(export));
1088 break;
1090 else if (t == Tdata) {
1091 case Tdata:
1092 parsedat(data, export);
1093 break;
1095 else
1096 err("export can only qualify data and function");
1097 case Ttype:
1098 parsetyp();
1099 break;
1100 case Teof:
1101 vfree(typ);
1102 return;
1107 static void
1108 printcon(Con *c, FILE *f)
1110 switch (c->type) {
1111 case CUndef:
1112 break;
1113 case CAddr:
1114 fprintf(f, "$%s", str(c->label));
1115 if (c->bits.i)
1116 fprintf(f, "%+"PRIi64, c->bits.i);
1117 break;
1118 case CBits:
1119 if (c->flt == 1)
1120 fprintf(f, "s_%f", c->bits.s);
1121 else if (c->flt == 2)
1122 fprintf(f, "d_%lf", c->bits.d);
1123 else
1124 fprintf(f, "%"PRIi64, c->bits.i);
1125 break;
1129 void
1130 printref(Ref r, Fn *fn, FILE *f)
1132 int i;
1133 Mem *m;
1135 switch (rtype(r)) {
1136 case RTmp:
1137 if (r.val < Tmp0)
1138 fprintf(f, "R%d", r.val);
1139 else
1140 fprintf(f, "%%%s", fn->tmp[r.val].name);
1141 break;
1142 case RCon:
1143 printcon(&fn->con[r.val], f);
1144 break;
1145 case RSlot:
1146 fprintf(f, "S%d", (r.val&(1<<28)) ? r.val-(1<<29) : r.val);
1147 break;
1148 case RCall:
1149 fprintf(f, "%04x", r.val);
1150 break;
1151 case RType:
1152 fprintf(f, ":%s", typ[r.val].name);
1153 break;
1154 case RMem:
1155 i = 0;
1156 m = &fn->mem[r.val];
1157 fputc('[', f);
1158 if (m->offset.type != CUndef) {
1159 printcon(&m->offset, f);
1160 i = 1;
1162 if (!req(m->base, R)) {
1163 if (i)
1164 fprintf(f, " + ");
1165 printref(m->base, fn, f);
1166 i = 1;
1168 if (!req(m->index, R)) {
1169 if (i)
1170 fprintf(f, " + ");
1171 fprintf(f, "%d * ", m->scale);
1172 printref(m->index, fn, f);
1174 fputc(']', f);
1175 break;
1179 void
1180 printfn(Fn *fn, FILE *f)
1182 static char ktoc[] = "wlsd";
1183 static char *jtoa[NJmp] = {
1184 #define X(j) [J##j] = #j,
1185 JMPS(X)
1186 #undef X
1188 Blk *b;
1189 Phi *p;
1190 Ins *i;
1191 uint n;
1193 if (fn->export)
1194 fprintf(f, "export ");
1195 fprintf(f, "function $%s() {\n", fn->name);
1196 for (b=fn->start; b; b=b->link) {
1197 fprintf(f, "@%s\n", b->name);
1198 for (p=b->phi; p; p=p->link) {
1199 fprintf(f, "\t");
1200 printref(p->to, fn, f);
1201 fprintf(f, " =%c phi ", ktoc[p->cls]);
1202 assert(p->narg);
1203 for (n=0;; n++) {
1204 fprintf(f, "@%s ", p->blk[n]->name);
1205 printref(p->arg[n], fn, f);
1206 if (n == p->narg-1) {
1207 fprintf(f, "\n");
1208 break;
1209 } else
1210 fprintf(f, ", ");
1213 for (i=b->ins; i<&b->ins[b->nins]; i++) {
1214 fprintf(f, "\t");
1215 if (!req(i->to, R)) {
1216 printref(i->to, fn, f);
1217 fprintf(f, " =%c ", ktoc[i->cls]);
1219 assert(optab[i->op].name);
1220 fprintf(f, "%s", optab[i->op].name);
1221 if (req(i->to, R))
1222 switch (i->op) {
1223 case Oarg:
1224 case Oswap:
1225 case Oxcmp:
1226 case Oacmp:
1227 case Oacmn:
1228 case Oafcmp:
1229 case Oxtest:
1230 case Oxdiv:
1231 case Oxidiv:
1232 fputc(ktoc[i->cls], f);
1234 if (!req(i->arg[0], R)) {
1235 fprintf(f, " ");
1236 printref(i->arg[0], fn, f);
1238 if (!req(i->arg[1], R)) {
1239 fprintf(f, ", ");
1240 printref(i->arg[1], fn, f);
1242 fprintf(f, "\n");
1244 switch (b->jmp.type) {
1245 case Jret0:
1246 case Jretw:
1247 case Jretl:
1248 case Jrets:
1249 case Jretd:
1250 case Jretc:
1251 fprintf(f, "\t%s", jtoa[b->jmp.type]);
1252 if (b->jmp.type != Jret0 || !req(b->jmp.arg, R)) {
1253 fprintf(f, " ");
1254 printref(b->jmp.arg, fn, f);
1256 if (b->jmp.type == Jretc)
1257 fprintf(f, ", :%s", typ[fn->retty].name);
1258 fprintf(f, "\n");
1259 break;
1260 case Jjmp:
1261 if (b->s1 != b->link)
1262 fprintf(f, "\tjmp @%s\n", b->s1->name);
1263 break;
1264 default:
1265 fprintf(f, "\t%s ", jtoa[b->jmp.type]);
1266 if (b->jmp.type == Jjnz) {
1267 printref(b->jmp.arg, fn, f);
1268 fprintf(f, ", ");
1270 fprintf(f, "@%s, @%s\n", b->s1->name, b->s2->name);
1271 break;
1274 fprintf(f, "}\n");