6 Ksb
= 4, /* matches Oarg/Opar/Jret */
13 Ke
= -2, /* erroneous mode */
14 Km
= Kl
, /* memory pointer */
18 #define O(op, t, cf) [O##op]={#op, t, cf},
92 static char *kwmap
[Ntok
] = {
107 [Texport
] = "export",
108 [Tthread
] = "thread",
109 [Tfunc
] = "function",
112 [Tsection
] = "section",
114 [Tdbgfile
] = "dbgfile",
132 TMask
= 16383, /* for temps hash */
133 BMask
= 8191, /* for blocks hash */
135 K
= 9583425, /* found using tools/lexh.c */
139 static uchar lexh
[1 << (32-M
)];
153 static int tmph
[TMask
+1];
157 static Blk
*blkh
[BMask
+1];
168 fprintf(stderr
, "qbe:%s:%d: ", inpath
, lnum
);
169 vfprintf(stderr
, s
, ap
);
170 fprintf(stderr
, "\n");
184 for (i
=0; i
<NPubOp
; ++i
)
186 kwmap
[i
] = optab
[i
].name
;
187 assert(Ntok
<= UCHAR_MAX
);
188 for (i
=0; i
<Ntok
; ++i
)
190 h
= hash(kwmap
[i
])*K
>> M
;
191 assert(lexh
[h
] == Txxx
);
209 n
= 10*n
+ (c
- '0');
211 } while ('0' <= c
&& c
<= '9');
215 return *(int64_t *)&n
;
221 static char tok
[NString
];
248 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
252 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
265 if ((c
= fgetc(inf
)) == '"')
273 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
280 if (isdigit(c
) || c
== '-') {
282 tokval
.num
= getint();
288 tokval
.str
= vnew(2, 1, PFn
);
294 err("unterminated string");
295 vgrow(&tokval
.str
, i
+2);
297 if (c
== '"' && !esc
) {
301 esc
= (c
== '\\' && !esc
);
305 if (!isalpha(c
) && c
!= '.' && c
!= '_')
306 err("invalid character %c (%d)", c
, c
);
310 err("identifier too long");
313 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
320 t
= lexh
[hash(tok
)*K
>> M
];
321 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
322 err("unknown keyword %s", tok
);
351 while ((t
= next()) == Tnl
)
359 static char *ttoa
[] = {
370 char buf
[128], *s1
, *s2
;
376 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
377 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
378 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
387 h
= &tmph
[hash(v
) & TMask
];
390 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
392 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
393 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
399 strcpy(curf
->tmp
[t
].name
, v
);
408 memset(&c
, 0, sizeof c
);
413 return tmpref(tokval
.str
);
416 c
.bits
.i
= tokval
.num
;
420 c
.bits
.s
= tokval
.flts
;
425 c
.bits
.d
= tokval
.fltd
;
434 c
.sym
.id
= intern(tokval
.str
);
437 return newcon(&c
, curf
);
444 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
446 err("undefined type :%s", tokval
.str
);
454 err("invalid class specifier");
456 *tyn
= findtyp(ntyp
);
480 int k
, ty
, env
, hasenv
, vararg
;
486 while (peek() != Trparen
) {
487 if (curi
- insb
>= NIns
)
488 err("too many instructions");
490 err("no parameters allowed after '...'");
494 err("only one '...' allowed");
497 *curi
= (Ins
){.op
= Oargv
};
504 err("only one environment allowed");
517 err("invalid argument");
518 if (!arg
&& rtype(r
) != RTmp
)
519 err("invalid function parameter");
522 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
524 *curi
= (Ins
){Opare
, k
, r
, {R
}};
527 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
529 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
532 *curi
= (Ins
){Oargsb
+(k
-Ksb
), Kw
, R
, {r
}};
534 *curi
= (Ins
){Oparsb
+(k
-Ksb
), Kw
, r
, {R
}};
537 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
539 *curi
= (Ins
){Opar
, k
, r
, {R
}};
542 if (peek() == Trparen
)
556 h
= hash(name
) & BMask
;
557 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
558 if (strcmp(b
->name
, name
) == 0)
562 strcpy(b
->name
, name
);
571 curb
->nins
= curi
- insb
;
572 idup(&curb
->ins
, insb
, curb
->nins
);
580 Ref arg
[NPred
] = {R
};
589 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
590 err("label or } expected");
593 r
= tmpref(tokval
.str
);
603 /* operations without result */
609 err("label, instruction or jump expected");
613 b
= findblk(tokval
.str
);
614 if (curb
&& curb
->jmp
.type
== Jxxx
) {
616 curb
->jmp
.type
= Jjmp
;
619 if (b
->jmp
.type
!= Jxxx
)
620 err("multiple definitions of block @%s", b
->name
);
627 curb
->jmp
.type
= Jretw
+ rcls
;
629 curb
->jmp
.type
= Jret0
;
630 else if (rcls
!= K0
) {
633 err("invalid return value");
638 curb
->jmp
.type
= Jjmp
;
641 curb
->jmp
.type
= Jjnz
;
644 err("invalid argument for jnz jump");
649 curb
->s1
= findblk(tokval
.str
);
650 if (curb
->jmp
.type
!= Jjmp
) {
653 curb
->s2
= findblk(tokval
.str
);
655 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
656 err("invalid jump to the start block");
659 curb
->jmp
.type
= Jhlt
;
669 arg
[0] = INT(tokval
.num
);
670 if (arg
[0].val
!= tokval
.num
)
671 err("line number too big");
672 if (peek() == Tcomma
) {
675 arg
[1] = INT(tokval
.num
);
676 if (arg
[1].val
!= tokval
.num
)
677 err("column number too big");
697 if (op
>= Tloadl
&& op
<= Tloadd
)
699 if (op
== Talloc1
|| op
== Talloc2
)
701 if (op
== Ovastart
&& !curf
->vararg
)
702 err("cannot use vastart in non-variadic function");
704 err("size class must be w, l, s, or d");
709 err("too many arguments");
712 blk
[i
] = findblk(tokval
.str
);
716 err("invalid instruction argument");
722 err(", or end of line expected");
728 if (ps
!= PPhi
|| curb
== curf
->start
)
729 err("unexpected phi instruction");
730 phi
= alloc(sizeof *phi
);
733 phi
->arg
= vnew(i
, sizeof arg
[0], PFn
);
734 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
735 phi
->blk
= vnew(i
, sizeof blk
[0], PFn
);
736 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
742 if (curi
- insb
>= NIns
-1)
743 err("too many instructions");
744 memset(curi
, 0, 2 * sizeof(Ins
));
746 curi
->arg
[0] = arg
[0];
747 curi
->arg
[1] = arg
[1];
749 if (rtype(arg
[2]) != RCon
)
750 err("blit size must be constant");
751 c
= &curf
->con
[arg
[2].val
];
755 || rsval(r
) != c
->bits
.i
)
756 err("invalid blit size");
763 err("invalid instruction");
765 if (curi
- insb
>= NIns
)
766 err("too many instructions");
770 curi
->arg
[0] = arg
[0];
771 curi
->arg
[1] = arg
[1];
778 usecheck(Ref r
, int k
, Fn
*fn
)
780 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
781 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
797 bsinit(pb
, fn
->nblk
);
798 bsinit(ppb
, fn
->nblk
);
799 for (b
=fn
->start
; b
; b
=b
->link
) {
800 for (p
=b
->phi
; p
; p
=p
->link
)
801 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
802 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
803 if (rtype(i
->to
) == RTmp
) {
804 t
= &fn
->tmp
[i
->to
.val
];
805 if (clsmerge(&t
->cls
, i
->cls
))
806 err("temporary %%%s is assigned with"
807 " multiple types", t
->name
);
810 for (b
=fn
->start
; b
; b
=b
->link
) {
812 for (n
=0; n
<b
->npred
; n
++)
813 bsset(pb
, b
->pred
[n
]->id
);
814 for (p
=b
->phi
; p
; p
=p
->link
) {
816 t
= &fn
->tmp
[p
->to
.val
];
817 for (n
=0; n
<p
->narg
; n
++) {
819 if (bshas(ppb
, p
->blk
[n
]->id
))
820 err("multiple entries for @%s in phi %%%s",
821 p
->blk
[n
]->name
, t
->name
);
822 if (!usecheck(p
->arg
[n
], k
, fn
))
823 err("invalid type for operand %%%s in phi %%%s",
824 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
825 bsset(ppb
, p
->blk
[n
]->id
);
827 if (!bsequal(pb
, ppb
))
828 err("predecessors not matched in phi %%%s", t
->name
);
830 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
831 for (n
=0; n
<2; n
++) {
832 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
836 err("invalid instruction type in %s",
838 if (rtype(r
) == RType
)
840 if (rtype(r
) != -1 && k
== Kx
)
841 err("no %s operand expected in %s",
842 n
== 1 ? "second" : "first",
844 if (rtype(r
) == -1 && k
!= Kx
)
845 err("missing %s operand in %s",
846 n
== 1 ? "second" : "first",
848 if (!usecheck(r
, k
, fn
))
849 err("invalid type for %s operand %%%s in %s",
850 n
== 1 ? "second" : "first",
851 t
->name
, optab
[i
->op
].name
);
854 if (isret(b
->jmp
.type
)) {
855 if (b
->jmp
.type
== Jretc
)
857 else if (b
->jmp
.type
>= Jretsb
)
860 k
= b
->jmp
.type
- Jretw
;
861 if (!usecheck(r
, k
, fn
))
864 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
866 err("invalid type for jump argument %%%s in block @%s",
867 fn
->tmp
[r
.val
].name
, b
->name
);
868 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
869 err("block @%s is used undefined", b
->s1
->name
);
870 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
871 err("block @%s is used undefined", b
->s2
->name
);
885 curf
= alloc(sizeof *curf
);
888 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], PFn
);
889 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], PFn
);
890 for (i
=0; i
<Tmp0
; ++i
)
891 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
895 curf
->con
[0].type
= CBits
;
896 curf
->con
[0].bits
.i
= 0xdeaddead; /* UNDEF */
897 curf
->con
[1].type
= CBits
;
899 blink
= &curf
->start
;
902 rcls
= parsecls(&curf
->retty
);
906 err("function name expected");
907 strncpy(curf
->name
, tokval
.str
, NString
-1);
908 curf
->vararg
= parserefl(0);
909 if (nextnl() != Tlbrace
)
910 err("function body must start with {");
916 err("empty function");
917 if (curb
->jmp
.type
== Jxxx
)
918 err("last block misses jump");
919 curf
->mem
= vnew(0, sizeof curf
->mem
[0], PFn
);
923 for (b
=0; b
; b
=b
->link
)
924 b
->dlink
= 0; /* was trashed by findblk() */
925 for (i
=0; i
<BMask
+1; ++i
)
927 memset(tmph
, 0, sizeof tmph
);
933 parsefields(Field
*fld
, Typ
*ty
, int t
)
936 int n
, c
, a
, al
, type
;
942 while (t
!= Trbrace
) {
945 default: err("invalid type member specifier");
946 case Td
: type
= Fd
; s
= 8; a
= 3; break;
947 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
948 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
949 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
950 case Th
: type
= Fh
; s
= 2; a
= 1; break;
951 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
954 ty1
= &typ
[findtyp(ntyp
-1)];
962 a
= ((sz
+ a
) & ~a
) - sz
;
980 for (; c
>0 && n
<NField
; c
--, n
++) {
989 err(", or } expected");
994 ty
->size
= (sz
+ a
- 1) & -a
;
1005 /* be careful if extending the syntax
1006 * to handle nested types, any pointer
1007 * held to typ[] might be invalidated!
1009 vgrow(&typ
, ntyp
+1);
1015 if (nextnl() != Ttyp
|| nextnl() != Teq
)
1016 err("type name and then = expected");
1017 strcpy(ty
->name
, tokval
.str
);
1020 if (nextnl() != Tint
)
1021 err("alignment expected");
1022 for (al
=0; tokval
.num
/= 2; al
++)
1028 err("type body must start with {");
1032 ty
->size
= tokval
.num
;
1033 if (ty
->align
== -1)
1034 err("dark types need alignment");
1035 if (nextnl() != Trbrace
)
1040 ty
->fields
= vnew(1, sizeof ty
->fields
[0], PHeap
);
1045 err("invalid union member");
1046 vgrow(&ty
->fields
, n
+1);
1047 parsefields(ty
->fields
[n
++], ty
, nextnl());
1049 } while (t
!= Trbrace
);
1051 parsefields(ty
->fields
[n
++], ty
, t
);
1061 d
->u
.ref
.name
= tokval
.str
;
1067 err("invalid token after offset in ref");
1068 d
->u
.ref
.off
= tokval
.num
;
1076 d
->u
.str
= tokval
.str
;
1080 parsedat(void cb(Dat
*), Lnk
*lnk
)
1082 char name
[NString
] = {0};
1086 if (nextnl() != Tglo
|| nextnl() != Teq
)
1087 err("data name, then = expected");
1088 strncpy(name
, tokval
.str
, NString
-1);
1092 if (nextnl() != Tint
)
1093 err("alignment expected");
1094 if (tokval
.num
<= 0 || tokval
.num
> CHAR_MAX
1095 || (tokval
.num
& (tokval
.num
-1)) != 0)
1096 err("invalid alignment");
1097 lnk
->align
= tokval
.num
;
1106 err("expected data contents in { .. }");
1109 default: err("invalid size specifier %c in data", tokval
.chr
);
1110 case Trbrace
: goto Done
;
1111 case Tl
: d
.type
= DL
; break;
1112 case Tw
: d
.type
= DW
; break;
1113 case Th
: d
.type
= DH
; break;
1114 case Tb
: d
.type
= DB
; break;
1115 case Ts
: d
.type
= DW
; break;
1116 case Td
: d
.type
= DL
; break;
1117 case Tz
: d
.type
= DZ
; break;
1123 memset(&d
.u
, 0, sizeof d
.u
);
1125 d
.u
.flts
= tokval
.flts
;
1126 else if (t
== Tfltd
)
1127 d
.u
.fltd
= tokval
.fltd
;
1129 d
.u
.num
= tokval
.num
;
1135 err("constant literal expected");
1138 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
|| t
== Tglo
);
1142 err(", or } expected");
1154 for (haslnk
=0;; haslnk
=1)
1155 switch ((t
=nextnl())) {
1164 err("only one section allowed");
1166 err("section \"name\" expected");
1167 lnk
->sec
= tokval
.str
;
1168 if (peek() == Tstr
) {
1170 lnk
->secf
= tokval
.str
;
1174 if (t
== Tfunc
&& lnk
->thread
)
1175 err("only data may have thread linkage");
1176 if (haslnk
&& t
!= Tdata
&& t
!= Tfunc
)
1177 err("only data and function have linkage");
1183 parse(FILE *f
, char *path
, void dbgfile(char *), void data(Dat
*), void func(Fn
*))
1194 typ
= vnew(0, sizeof typ
[0], PHeap
);
1197 switch (parselnk(&lnk
)) {
1199 err("top-level definition expected");
1202 dbgfile(tokval
.str
);
1205 func(parsefn(&lnk
));
1208 parsedat(data
, &lnk
);
1214 for (n
=0; n
<ntyp
; n
++)
1216 vfree(typ
[n
].fields
);
1224 printcon(Con
*c
, FILE *f
)
1230 if (c
->sym
.type
== SThr
)
1231 fprintf(f
, "thread ");
1232 fprintf(f
, "$%s", str(c
->sym
.id
));
1234 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1238 fprintf(f
, "s_%f", c
->bits
.s
);
1239 else if (c
->flt
== 2)
1240 fprintf(f
, "d_%lf", c
->bits
.d
);
1242 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1248 printref(Ref r
, Fn
*fn
, FILE *f
)
1256 fprintf(f
, "R%d", r
.val
);
1258 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1262 fprintf(f
, "UNDEF");
1264 printcon(&fn
->con
[r
.val
], f
);
1267 fprintf(f
, "S%d", rsval(r
));
1270 fprintf(f
, "%04x", r
.val
);
1273 fprintf(f
, ":%s", typ
[r
.val
].name
);
1277 m
= &fn
->mem
[r
.val
];
1279 if (m
->offset
.type
!= CUndef
) {
1280 printcon(&m
->offset
, f
);
1283 if (!req(m
->base
, R
)) {
1286 printref(m
->base
, fn
, f
);
1289 if (!req(m
->index
, R
)) {
1292 fprintf(f
, "%d * ", m
->scale
);
1293 printref(m
->index
, fn
, f
);
1298 fprintf(f
, "%d", rsval(r
));
1304 printfn(Fn
*fn
, FILE *f
)
1306 static char ktoc
[] = "wlsd";
1307 static char *jtoa
[NJmp
] = {
1308 #define X(j) [J##j] = #j,
1317 fprintf(f
, "function $%s() {\n", fn
->name
);
1318 for (b
=fn
->start
; b
; b
=b
->link
) {
1319 fprintf(f
, "@%s\n", b
->name
);
1320 for (p
=b
->phi
; p
; p
=p
->link
) {
1322 printref(p
->to
, fn
, f
);
1323 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1326 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1327 printref(p
->arg
[n
], fn
, f
);
1328 if (n
== p
->narg
-1) {
1335 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1337 if (!req(i
->to
, R
)) {
1338 printref(i
->to
, fn
, f
);
1339 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1341 assert(optab
[i
->op
].name
);
1342 fprintf(f
, "%s", optab
[i
->op
].name
);
1354 fputc(ktoc
[i
->cls
], f
);
1356 if (!req(i
->arg
[0], R
)) {
1358 printref(i
->arg
[0], fn
, f
);
1360 if (!req(i
->arg
[1], R
)) {
1362 printref(i
->arg
[1], fn
, f
);
1366 switch (b
->jmp
.type
) {
1377 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1378 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1380 printref(b
->jmp
.arg
, fn
, f
);
1382 if (b
->jmp
.type
== Jretc
)
1383 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1387 fprintf(f
, "\thlt\n");
1390 if (b
->s1
!= b
->link
)
1391 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1394 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1395 if (b
->jmp
.type
== Jjnz
) {
1396 printref(b
->jmp
.arg
, fn
, f
);
1399 assert(b
->s1
&& b
->s2
);
1400 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);