6 Ksb
= 4, /* matches Oarg/Opar/Jret */
13 Ke
= -2, /* erroneous mode */
14 Km
= Kl
, /* memory pointer */
19 #define P(cf, hi, id) .canfold = cf, .hasid = hi, .idval = id
20 #define O(op, t, p) [O##op]={.name = #op, .argcls = t, p},
96 static char *kwmap
[Ntok
] = {
101 [Talloc1
] = "alloc1",
102 [Talloc2
] = "alloc2",
111 [Texport
] = "export",
112 [Tthread
] = "thread",
113 [Tcommon
] = "common",
114 [Tfunc
] = "function",
117 [Tsection
] = "section",
119 [Tdbgfile
] = "dbgfile",
137 TMask
= 16383, /* for temps hash */
138 BMask
= 8191, /* for blocks hash */
140 K
= 11183273, /* found using tools/lexh.c */
144 static uchar lexh
[1 << (32-M
)];
163 static Blk
*blkh
[BMask
+1];
174 fprintf(stderr
, "qbe:%s:%d: ", inpath
, lnum
);
175 vfprintf(stderr
, s
, ap
);
176 fprintf(stderr
, "\n");
190 for (i
=0; i
<NPubOp
; ++i
)
192 kwmap
[i
] = optab
[i
].name
;
193 assert(Ntok
<= UCHAR_MAX
);
194 for (i
=0; i
<Ntok
; ++i
)
196 h
= hash(kwmap
[i
])*K
>> M
;
197 assert(lexh
[h
] == Txxx
);
215 n
= 10*n
+ (c
- '0');
217 } while ('0' <= c
&& c
<= '9');
221 return *(int64_t *)&n
;
227 static char tok
[NString
];
254 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
258 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
271 if ((c
= fgetc(inf
)) == '"')
279 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
286 if (isdigit(c
) || c
== '-') {
288 tokval
.num
= getint();
294 tokval
.str
= vnew(2, 1, PFn
);
300 err("unterminated string");
301 vgrow(&tokval
.str
, i
+2);
303 if (c
== '"' && !esc
) {
307 esc
= (c
== '\\' && !esc
);
311 if (!isalpha(c
) && c
!= '.' && c
!= '_')
312 err("invalid character %c (%d)", c
, c
);
316 err("identifier too long");
319 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
326 t
= lexh
[hash(tok
)*K
>> M
];
327 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
328 err("unknown keyword %s", tok
);
357 while ((t
= next()) == Tnl
)
365 static char *ttoa
[] = {
376 char buf
[128], *s1
, *s2
;
382 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
383 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
384 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
393 if (tmphcap
/2 <= curf
->ntmp
-Tmp0
) {
395 tmphcap
= tmphcap
? tmphcap
*2 : TMask
+1;
396 tmph
= emalloc(tmphcap
* sizeof tmph
[0]);
397 for (t
=Tmp0
; t
<curf
->ntmp
; t
++) {
398 i
= hash(curf
->tmp
[t
].name
) & (tmphcap
-1);
399 for (; tmph
[i
]; i
=(i
+1) & (tmphcap
-1))
404 i
= hash(v
) & (tmphcap
-1);
405 for (; tmph
[i
]; i
=(i
+1) & (tmphcap
-1)) {
407 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
413 strcpy(curf
->tmp
[t
].name
, v
);
422 memset(&c
, 0, sizeof c
);
427 return tmpref(tokval
.str
);
430 c
.bits
.i
= tokval
.num
;
434 c
.bits
.s
= tokval
.flts
;
439 c
.bits
.d
= tokval
.fltd
;
448 c
.sym
.id
= intern(tokval
.str
);
451 return newcon(&c
, curf
);
458 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
460 err("undefined type :%s", tokval
.str
);
468 err("invalid class specifier");
470 *tyn
= findtyp(ntyp
);
494 int k
, ty
, env
, hasenv
, vararg
;
500 while (peek() != Trparen
) {
501 if (curi
- insb
>= NIns
)
502 err("too many instructions");
504 err("no parameters allowed after '...'");
508 err("only one '...' allowed");
511 *curi
= (Ins
){.op
= Oargv
};
518 err("only one environment allowed");
531 err("invalid argument");
532 if (!arg
&& rtype(r
) != RTmp
)
533 err("invalid function parameter");
536 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
538 *curi
= (Ins
){Opare
, k
, r
, {R
}};
541 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
543 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
546 *curi
= (Ins
){Oargsb
+(k
-Ksb
), Kw
, R
, {r
}};
548 *curi
= (Ins
){Oparsb
+(k
-Ksb
), Kw
, r
, {R
}};
551 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
553 *curi
= (Ins
){Opar
, k
, r
, {R
}};
556 if (peek() == Trparen
)
570 h
= hash(name
) & BMask
;
571 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
572 if (strcmp(b
->name
, name
) == 0)
576 strcpy(b
->name
, name
);
585 curb
->nins
= curi
- insb
;
586 idup(&curb
->ins
, insb
, curb
->nins
);
594 Ref arg
[NPred
] = {R
};
603 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
604 err("label or } expected");
607 r
= tmpref(tokval
.str
);
617 /* operations without result */
623 err("label, instruction or jump expected");
627 b
= findblk(tokval
.str
);
628 if (curb
&& curb
->jmp
.type
== Jxxx
) {
630 curb
->jmp
.type
= Jjmp
;
633 if (b
->jmp
.type
!= Jxxx
)
634 err("multiple definitions of block @%s", b
->name
);
641 curb
->jmp
.type
= Jretw
+ rcls
;
643 curb
->jmp
.type
= Jret0
;
644 else if (rcls
!= K0
) {
647 err("invalid return value");
652 curb
->jmp
.type
= Jjmp
;
655 curb
->jmp
.type
= Jjnz
;
658 err("invalid argument for jnz jump");
663 curb
->s1
= findblk(tokval
.str
);
664 if (curb
->jmp
.type
!= Jjmp
) {
667 curb
->s2
= findblk(tokval
.str
);
669 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
670 err("invalid jump to the start block");
673 curb
->jmp
.type
= Jhlt
;
683 arg
[0] = INT(tokval
.num
);
684 if (arg
[0].val
!= tokval
.num
)
685 err("line number too big");
686 if (peek() == Tcomma
) {
689 arg
[1] = INT(tokval
.num
);
690 if (arg
[1].val
!= tokval
.num
)
691 err("column number too big");
712 if (op
>= Tloadl
&& op
<= Tloadd
)
714 if (op
== Talloc1
|| op
== Talloc2
)
716 if (op
== Ovastart
&& !curf
->vararg
)
717 err("cannot use vastart in non-variadic function");
719 err("size class must be w, l, s, or d");
724 err("too many arguments");
727 blk
[i
] = findblk(tokval
.str
);
731 err("invalid instruction argument");
737 err(", or end of line expected");
743 if (ps
!= PPhi
|| curb
== curf
->start
)
744 err("unexpected phi instruction");
745 phi
= alloc(sizeof *phi
);
748 phi
->arg
= vnew(i
, sizeof arg
[0], PFn
);
749 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
750 phi
->blk
= vnew(i
, sizeof blk
[0], PFn
);
751 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
757 if (curi
- insb
>= NIns
-1)
758 err("too many instructions");
759 memset(curi
, 0, 2 * sizeof(Ins
));
761 curi
->arg
[0] = arg
[0];
762 curi
->arg
[1] = arg
[1];
764 if (rtype(arg
[2]) != RCon
)
765 err("blit size must be constant");
766 c
= &curf
->con
[arg
[2].val
];
770 || rsval(r
) != c
->bits
.i
)
771 err("invalid blit size");
778 err("invalid instruction");
780 if (curi
- insb
>= NIns
)
781 err("too many instructions");
785 curi
->arg
[0] = arg
[0];
786 curi
->arg
[1] = arg
[1];
793 usecheck(Ref r
, int k
, Fn
*fn
)
795 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
796 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
812 bsinit(pb
, fn
->nblk
);
813 bsinit(ppb
, fn
->nblk
);
814 for (b
=fn
->start
; b
; b
=b
->link
) {
815 for (p
=b
->phi
; p
; p
=p
->link
)
816 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
817 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
818 if (rtype(i
->to
) == RTmp
) {
819 t
= &fn
->tmp
[i
->to
.val
];
820 if (clsmerge(&t
->cls
, i
->cls
))
821 err("temporary %%%s is assigned with"
822 " multiple types", t
->name
);
825 for (b
=fn
->start
; b
; b
=b
->link
) {
827 for (n
=0; n
<b
->npred
; n
++)
828 bsset(pb
, b
->pred
[n
]->id
);
829 for (p
=b
->phi
; p
; p
=p
->link
) {
831 t
= &fn
->tmp
[p
->to
.val
];
832 for (n
=0; n
<p
->narg
; n
++) {
834 if (bshas(ppb
, p
->blk
[n
]->id
))
835 err("multiple entries for @%s in phi %%%s",
836 p
->blk
[n
]->name
, t
->name
);
837 if (!usecheck(p
->arg
[n
], k
, fn
))
838 err("invalid type for operand %%%s in phi %%%s",
839 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
840 bsset(ppb
, p
->blk
[n
]->id
);
842 if (!bsequal(pb
, ppb
))
843 err("predecessors not matched in phi %%%s", t
->name
);
845 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
846 for (n
=0; n
<2; n
++) {
847 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
851 err("invalid instruction type in %s",
853 if (rtype(r
) == RType
)
855 if (rtype(r
) != -1 && k
== Kx
)
856 err("no %s operand expected in %s",
857 n
== 1 ? "second" : "first",
859 if (rtype(r
) == -1 && k
!= Kx
)
860 err("missing %s operand in %s",
861 n
== 1 ? "second" : "first",
863 if (!usecheck(r
, k
, fn
))
864 err("invalid type for %s operand %%%s in %s",
865 n
== 1 ? "second" : "first",
866 t
->name
, optab
[i
->op
].name
);
869 if (isret(b
->jmp
.type
)) {
870 if (b
->jmp
.type
== Jretc
)
872 else if (b
->jmp
.type
>= Jretsb
)
875 k
= b
->jmp
.type
- Jretw
;
876 if (!usecheck(r
, k
, fn
))
879 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
881 err("invalid type for jump argument %%%s in block @%s",
882 fn
->tmp
[r
.val
].name
, b
->name
);
883 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
884 err("block @%s is used undefined", b
->s1
->name
);
885 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
886 err("block @%s is used undefined", b
->s2
->name
);
900 curf
= alloc(sizeof *curf
);
903 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], PFn
);
904 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], PFn
);
905 for (i
=0; i
<Tmp0
; ++i
)
906 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
910 curf
->con
[0].type
= CBits
;
911 curf
->con
[0].bits
.i
= 0xdeaddead; /* UNDEF */
912 curf
->con
[1].type
= CBits
;
915 blink
= &curf
->start
;
918 rcls
= parsecls(&curf
->retty
);
922 err("function name expected");
923 strncpy(curf
->name
, tokval
.str
, NString
-1);
924 curf
->vararg
= parserefl(0);
925 if (nextnl() != Tlbrace
)
926 err("function body must start with {");
932 err("empty function");
933 if (curb
->jmp
.type
== Jxxx
)
934 err("last block misses jump");
935 curf
->mem
= vnew(0, sizeof curf
->mem
[0], PFn
);
939 for (b
=curf
->start
; b
; b
=b
->link
)
940 b
->dlink
= 0; /* was trashed by findblk() */
941 for (i
=0; i
<BMask
+1; ++i
)
943 memset(tmph
, 0, tmphcap
* sizeof tmph
[0]);
949 parsefields(Field
*fld
, Typ
*ty
, int t
)
952 int n
, c
, a
, al
, type
;
958 while (t
!= Trbrace
) {
961 default: err("invalid type member specifier");
962 case Td
: type
= Fd
; s
= 8; a
= 3; break;
963 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
964 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
965 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
966 case Th
: type
= Fh
; s
= 2; a
= 1; break;
967 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
970 ty1
= &typ
[findtyp(ntyp
-1)];
978 a
= ((sz
+ a
) & ~a
) - sz
;
996 for (; c
>0 && n
<NField
; c
--, n
++) {
1005 err(", or } expected");
1010 ty
->size
= (sz
+ a
- 1) & -a
;
1021 /* be careful if extending the syntax
1022 * to handle nested types, any pointer
1023 * held to typ[] might be invalidated!
1025 vgrow(&typ
, ntyp
+1);
1031 if (nextnl() != Ttyp
|| nextnl() != Teq
)
1032 err("type name and then = expected");
1033 strcpy(ty
->name
, tokval
.str
);
1036 if (nextnl() != Tint
)
1037 err("alignment expected");
1038 for (al
=0; tokval
.num
/= 2; al
++)
1044 err("type body must start with {");
1048 ty
->size
= tokval
.num
;
1049 if (ty
->align
== -1)
1050 err("dark types need alignment");
1051 if (nextnl() != Trbrace
)
1056 ty
->fields
= vnew(1, sizeof ty
->fields
[0], PHeap
);
1061 err("invalid union member");
1062 vgrow(&ty
->fields
, n
+1);
1063 parsefields(ty
->fields
[n
++], ty
, nextnl());
1065 } while (t
!= Trbrace
);
1067 parsefields(ty
->fields
[n
++], ty
, t
);
1077 d
->u
.ref
.name
= tokval
.str
;
1083 err("invalid token after offset in ref");
1084 d
->u
.ref
.off
= tokval
.num
;
1092 d
->u
.str
= tokval
.str
;
1096 parsedat(void cb(Dat
*), Lnk
*lnk
)
1098 char name
[NString
] = {0};
1102 if (nextnl() != Tglo
|| nextnl() != Teq
)
1103 err("data name, then = expected");
1104 strncpy(name
, tokval
.str
, NString
-1);
1108 if (nextnl() != Tint
)
1109 err("alignment expected");
1110 if (tokval
.num
<= 0 || tokval
.num
> CHAR_MAX
1111 || (tokval
.num
& (tokval
.num
-1)) != 0)
1112 err("invalid alignment");
1113 lnk
->align
= tokval
.num
;
1122 err("expected data contents in { .. }");
1125 default: err("invalid size specifier %c in data", tokval
.chr
);
1126 case Trbrace
: goto Done
;
1127 case Tl
: d
.type
= DL
; break;
1128 case Tw
: d
.type
= DW
; break;
1129 case Th
: d
.type
= DH
; break;
1130 case Tb
: d
.type
= DB
; break;
1131 case Ts
: d
.type
= DW
; break;
1132 case Td
: d
.type
= DL
; break;
1133 case Tz
: d
.type
= DZ
; break;
1139 memset(&d
.u
, 0, sizeof d
.u
);
1141 d
.u
.flts
= tokval
.flts
;
1142 else if (t
== Tfltd
)
1143 d
.u
.fltd
= tokval
.fltd
;
1145 d
.u
.num
= tokval
.num
;
1151 err("constant literal expected");
1154 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
|| t
== Tglo
);
1158 err(", or } expected");
1170 for (haslnk
=0;; haslnk
=1)
1171 switch ((t
=nextnl())) {
1183 err("only one section allowed");
1185 err("section \"name\" expected");
1186 lnk
->sec
= tokval
.str
;
1187 if (peek() == Tstr
) {
1189 lnk
->secf
= tokval
.str
;
1193 if (t
== Tfunc
&& lnk
->thread
)
1194 err("only data may have thread linkage");
1195 if (haslnk
&& t
!= Tdata
&& t
!= Tfunc
)
1196 err("only data and function have linkage");
1202 parse(FILE *f
, char *path
, void dbgfile(char *), void data(Dat
*), void func(Fn
*))
1213 typ
= vnew(0, sizeof typ
[0], PHeap
);
1216 switch (parselnk(&lnk
)) {
1218 err("top-level definition expected");
1221 dbgfile(tokval
.str
);
1225 func(parsefn(&lnk
));
1228 parsedat(data
, &lnk
);
1234 for (n
=0; n
<ntyp
; n
++)
1236 vfree(typ
[n
].fields
);
1244 printcon(Con
*c
, FILE *f
)
1250 if (c
->sym
.type
== SThr
)
1251 fprintf(f
, "thread ");
1252 fprintf(f
, "$%s", str(c
->sym
.id
));
1254 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1258 fprintf(f
, "s_%f", c
->bits
.s
);
1259 else if (c
->flt
== 2)
1260 fprintf(f
, "d_%lf", c
->bits
.d
);
1262 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1268 printref(Ref r
, Fn
*fn
, FILE *f
)
1276 fprintf(f
, "R%d", r
.val
);
1278 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1282 fprintf(f
, "UNDEF");
1284 printcon(&fn
->con
[r
.val
], f
);
1287 fprintf(f
, "S%d", rsval(r
));
1290 fprintf(f
, "%04x", r
.val
);
1293 fprintf(f
, ":%s", typ
[r
.val
].name
);
1297 m
= &fn
->mem
[r
.val
];
1299 if (m
->offset
.type
!= CUndef
) {
1300 printcon(&m
->offset
, f
);
1303 if (!req(m
->base
, R
)) {
1306 printref(m
->base
, fn
, f
);
1309 if (!req(m
->index
, R
)) {
1312 fprintf(f
, "%d * ", m
->scale
);
1313 printref(m
->index
, fn
, f
);
1318 fprintf(f
, "%d", rsval(r
));
1327 printfn(Fn
*fn
, FILE *f
)
1329 static char ktoc
[] = "wlsd";
1330 static char *jtoa
[NJmp
] = {
1331 #define X(j) [J##j] = #j,
1340 fprintf(f
, "function $%s() {\n", fn
->name
);
1341 for (b
=fn
->start
; b
; b
=b
->link
) {
1342 fprintf(f
, "@%s\n", b
->name
);
1343 for (p
=b
->phi
; p
; p
=p
->link
) {
1345 printref(p
->to
, fn
, f
);
1346 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1349 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1350 printref(p
->arg
[n
], fn
, f
);
1351 if (n
== p
->narg
-1) {
1358 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1360 if (!req(i
->to
, R
)) {
1361 printref(i
->to
, fn
, f
);
1362 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1364 assert(optab
[i
->op
].name
);
1365 fprintf(f
, "%s", optab
[i
->op
].name
);
1377 fputc(ktoc
[i
->cls
], f
);
1379 if (!req(i
->arg
[0], R
)) {
1381 printref(i
->arg
[0], fn
, f
);
1383 if (!req(i
->arg
[1], R
)) {
1385 printref(i
->arg
[1], fn
, f
);
1389 switch (b
->jmp
.type
) {
1400 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1401 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1403 printref(b
->jmp
.arg
, fn
, f
);
1405 if (b
->jmp
.type
== Jretc
)
1406 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1410 fprintf(f
, "\thlt\n");
1413 if (b
->s1
!= b
->link
)
1414 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1417 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1418 if (b
->jmp
.type
== Jjnz
) {
1419 printref(b
->jmp
.arg
, fn
, f
);
1422 assert(b
->s1
&& b
->s2
);
1423 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);