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},
89 static char *kwmap
[Ntok
] = {
102 [Texport
] = "export",
103 [Tthread
] = "thread",
104 [Tfunc
] = "function",
107 [Tsection
] = "section",
126 TMask
= 16383, /* for temps hash */
127 BMask
= 8191, /* for blocks hash */
129 K
= 9583425, /* found using tools/lexh.c */
133 static uchar lexh
[1 << (32-M
)];
147 static int tmph
[TMask
+1];
151 static Blk
*blkh
[BMask
+1];
162 fprintf(stderr
, "qbe:%s:%d: ", inpath
, lnum
);
163 vfprintf(stderr
, s
, ap
);
164 fprintf(stderr
, "\n");
178 for (i
=0; i
<NPubOp
; ++i
)
180 kwmap
[i
] = optab
[i
].name
;
181 assert(Ntok
<= UCHAR_MAX
);
182 for (i
=0; i
<Ntok
; ++i
)
184 h
= hash(kwmap
[i
])*K
>> M
;
185 assert(lexh
[h
] == Txxx
);
203 n
= 10*n
+ (c
- '0');
205 } while ('0' <= c
&& c
<= '9');
209 return *(int64_t *)&n
;
215 static char tok
[NString
];
242 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
246 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
259 if ((c
= fgetc(inf
)) == '"')
267 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
274 if (isdigit(c
) || c
== '-' || c
== '+') {
276 tokval
.num
= getint();
282 tokval
.str
= vnew(2, 1, PFn
);
288 err("unterminated string");
289 vgrow(&tokval
.str
, i
+2);
291 if (c
== '"' && !esc
) {
295 esc
= (c
== '\\' && !esc
);
299 if (!isalpha(c
) && c
!= '.' && c
!= '_')
300 err("invalid character %c (%d)", c
, c
);
304 err("identifier too long");
307 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
314 t
= lexh
[hash(tok
)*K
>> M
];
315 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
316 err("unknown keyword %s", tok
);
345 while ((t
= next()) == Tnl
)
353 static char *ttoa
[] = {
364 char buf
[128], *s1
, *s2
;
370 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
371 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
372 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
381 h
= &tmph
[hash(v
) & TMask
];
384 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
386 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
387 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
393 strcpy(curf
->tmp
[t
].name
, v
);
402 memset(&c
, 0, sizeof c
);
407 return tmpref(tokval
.str
);
410 c
.bits
.i
= tokval
.num
;
414 c
.bits
.s
= tokval
.flts
;
419 c
.bits
.d
= tokval
.fltd
;
428 c
.sym
.id
= intern(tokval
.str
);
431 return newcon(&c
, curf
);
438 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
440 err("undefined type :%s", tokval
.str
);
448 err("invalid class specifier");
450 *tyn
= findtyp(ntyp
);
474 int k
, ty
, env
, hasenv
, vararg
;
480 while (peek() != Trparen
) {
481 if (curi
- insb
>= NIns
)
482 err("too many instructions (1)");
484 err("no parameters allowed after '...'");
488 err("only one '...' allowed");
491 *curi
= (Ins
){.op
= Oargv
};
498 err("only one environment allowed");
511 err("invalid argument");
512 if (!arg
&& rtype(r
) != RTmp
)
513 err("invalid function parameter");
516 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
518 *curi
= (Ins
){Opare
, k
, r
, {R
}};
521 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
523 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
526 *curi
= (Ins
){Oargsb
+(k
-Ksb
), Kw
, R
, {r
}};
528 *curi
= (Ins
){Oparsb
+(k
-Ksb
), Kw
, r
, {R
}};
531 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
533 *curi
= (Ins
){Opar
, k
, r
, {R
}};
536 if (peek() == Trparen
)
550 h
= hash(name
) & BMask
;
551 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
552 if (strcmp(b
->name
, name
) == 0)
556 strcpy(b
->name
, name
);
565 curb
->nins
= curi
- insb
;
566 idup(&curb
->ins
, insb
, curb
->nins
);
574 Ref arg
[NPred
] = {R
};
582 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
583 err("label or } expected");
589 /* operations without result */
595 err("label, instruction or jump expected");
601 b
= findblk(tokval
.str
);
602 if (curb
&& curb
->jmp
.type
== Jxxx
) {
604 curb
->jmp
.type
= Jjmp
;
607 if (b
->jmp
.type
!= Jxxx
)
608 err("multiple definitions of block @%s", b
->name
);
615 curb
->jmp
.type
= Jretw
+ rcls
;
617 curb
->jmp
.type
= Jret0
;
618 else if (rcls
!= K0
) {
621 err("invalid return value");
626 curb
->jmp
.type
= Jjmp
;
629 curb
->jmp
.type
= Jjnz
;
632 err("invalid argument for jnz jump");
637 curb
->s1
= findblk(tokval
.str
);
638 if (curb
->jmp
.type
!= Jjmp
) {
641 curb
->s2
= findblk(tokval
.str
);
643 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
644 err("invalid jump to the start node");
650 r
= tmpref(tokval
.str
);
656 if (ps
!= PPhi
|| curb
== curf
->start
)
657 err("unexpected phi instruction");
676 if (op
>= Tloadl
&& op
<= Tloadd
)
678 if (op
== Talloc1
|| op
== Talloc2
)
680 if (op
== Ovastart
&& !curf
->vararg
)
681 err("cannot use vastart in non-variadic function");
683 err("size class must be w, l, s, or d");
685 err("invalid instruction");
690 err("too many arguments");
693 blk
[i
] = findblk(tokval
.str
);
697 err("invalid instruction argument");
703 err(", or end of line expected");
709 if (curi
- insb
>= NIns
)
710 err("too many instructions (2)");
714 curi
->arg
[0] = arg
[0];
715 curi
->arg
[1] = arg
[1];
719 phi
= alloc(sizeof *phi
);
722 phi
->arg
= vnew(i
, sizeof arg
[0], PFn
);
723 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
724 phi
->blk
= vnew(i
, sizeof blk
[0], PFn
);
725 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
734 usecheck(Ref r
, int k
, Fn
*fn
)
736 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
737 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
753 bsinit(pb
, fn
->nblk
);
754 bsinit(ppb
, fn
->nblk
);
755 for (b
=fn
->start
; b
; b
=b
->link
) {
756 for (p
=b
->phi
; p
; p
=p
->link
)
757 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
758 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
759 if (rtype(i
->to
) == RTmp
) {
760 t
= &fn
->tmp
[i
->to
.val
];
761 if (clsmerge(&t
->cls
, i
->cls
))
762 err("temporary %%%s is assigned with"
763 " multiple types", t
->name
);
766 for (b
=fn
->start
; b
; b
=b
->link
) {
768 for (n
=0; n
<b
->npred
; n
++)
769 bsset(pb
, b
->pred
[n
]->id
);
770 for (p
=b
->phi
; p
; p
=p
->link
) {
772 t
= &fn
->tmp
[p
->to
.val
];
773 for (n
=0; n
<p
->narg
; n
++) {
775 if (bshas(ppb
, p
->blk
[n
]->id
))
776 err("multiple entries for @%s in phi %%%s",
777 p
->blk
[n
]->name
, t
->name
);
778 if (!usecheck(p
->arg
[n
], k
, fn
))
779 err("invalid type for operand %%%s in phi %%%s",
780 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
781 bsset(ppb
, p
->blk
[n
]->id
);
783 if (!bsequal(pb
, ppb
))
784 err("predecessors not matched in phi %%%s", t
->name
);
786 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
787 for (n
=0; n
<2; n
++) {
788 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
792 err("invalid instruction type in %s",
794 if (rtype(r
) == RType
)
796 if (rtype(r
) != -1 && k
== Kx
)
797 err("no %s operand expected in %s",
798 n
== 1 ? "second" : "first",
800 if (rtype(r
) == -1 && k
!= Kx
)
801 err("missing %s operand in %s",
802 n
== 1 ? "second" : "first",
804 if (!usecheck(r
, k
, fn
))
805 err("invalid type for %s operand %%%s in %s",
806 n
== 1 ? "second" : "first",
807 t
->name
, optab
[i
->op
].name
);
810 if (isret(b
->jmp
.type
)) {
811 if (b
->jmp
.type
== Jretc
)
813 else if (b
->jmp
.type
>= Jretsb
)
816 k
= b
->jmp
.type
- Jretw
;
817 if (!usecheck(r
, k
, fn
))
820 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
822 err("invalid type for jump argument %%%s in block @%s",
823 fn
->tmp
[r
.val
].name
, b
->name
);
824 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
825 err("block @%s is used undefined", b
->s1
->name
);
826 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
827 err("block @%s is used undefined", b
->s2
->name
);
841 curf
= alloc(sizeof *curf
);
843 curf
->ncon
= 1; /* first constant must be 0 */
844 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], PFn
);
845 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], PFn
);
846 for (i
=0; i
<Tmp0
; ++i
)
847 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
851 curf
->con
[0].type
= CBits
;
853 blink
= &curf
->start
;
856 rcls
= parsecls(&curf
->retty
);
860 err("function name expected");
861 strncpy(curf
->name
, tokval
.str
, NString
-1);
862 curf
->vararg
= parserefl(0);
863 if (nextnl() != Tlbrace
)
864 err("function body must start with {");
870 err("empty function");
871 if (curb
->jmp
.type
== Jxxx
)
872 err("last block misses jump");
873 curf
->mem
= vnew(0, sizeof curf
->mem
[0], PFn
);
877 for (b
=0; b
; b
=b
->link
)
878 b
->dlink
= 0; /* was trashed by findblk() */
879 for (i
=0; i
<BMask
+1; ++i
)
881 memset(tmph
, 0, sizeof tmph
);
887 parsefields(Field
*fld
, Typ
*ty
, int t
)
890 int n
, c
, a
, al
, type
;
896 while (t
!= Trbrace
) {
899 default: err("invalid type member specifier");
900 case Td
: type
= Fd
; s
= 8; a
= 3; break;
901 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
902 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
903 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
904 case Th
: type
= Fh
; s
= 2; a
= 1; break;
905 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
908 ty1
= &typ
[findtyp(ntyp
-1)];
916 a
= ((sz
+ a
) & ~a
) - sz
;
934 for (; c
>0 && n
<NField
; c
--, n
++) {
943 err(", or } expected");
948 ty
->size
= (sz
+ a
- 1) & -a
;
959 /* be careful if extending the syntax
960 * to handle nested types, any pointer
961 * held to typ[] might be invalidated!
969 if (nextnl() != Ttyp
|| nextnl() != Teq
)
970 err("type name and then = expected");
971 strcpy(ty
->name
, tokval
.str
);
974 if (nextnl() != Tint
)
975 err("alignment expected");
976 for (al
=0; tokval
.num
/= 2; al
++)
982 err("type body must start with {");
986 ty
->size
= tokval
.num
;
988 err("dark types need alignment");
989 if (nextnl() != Trbrace
)
994 ty
->fields
= vnew(1, sizeof ty
->fields
[0], PHeap
);
999 err("invalid union member");
1000 vgrow(&ty
->fields
, n
+1);
1001 parsefields(ty
->fields
[n
++], ty
, nextnl());
1003 } while (t
!= Trbrace
);
1005 parsefields(ty
->fields
[n
++], ty
, t
);
1015 d
->u
.ref
.name
= tokval
.str
;
1021 err("invalid token after offset in ref");
1022 d
->u
.ref
.off
= tokval
.num
;
1030 d
->u
.str
= tokval
.str
;
1034 parsedat(void cb(Dat
*), Lnk
*lnk
)
1036 char name
[NString
] = {0};
1040 if (nextnl() != Tglo
|| nextnl() != Teq
)
1041 err("data name, then = expected");
1042 strncpy(name
, tokval
.str
, NString
-1);
1046 if (nextnl() != Tint
)
1047 err("alignment expected");
1048 lnk
->align
= tokval
.num
;
1057 err("expected data contents in { .. }");
1060 default: err("invalid size specifier %c in data", tokval
.chr
);
1061 case Trbrace
: goto Done
;
1062 case Tl
: d
.type
= DL
; break;
1063 case Tw
: d
.type
= DW
; break;
1064 case Th
: d
.type
= DH
; break;
1065 case Tb
: d
.type
= DB
; break;
1066 case Ts
: d
.type
= DW
; break;
1067 case Td
: d
.type
= DL
; break;
1068 case Tz
: d
.type
= DZ
; break;
1074 memset(&d
.u
, 0, sizeof d
.u
);
1076 d
.u
.flts
= tokval
.flts
;
1077 else if (t
== Tfltd
)
1078 d
.u
.fltd
= tokval
.fltd
;
1080 d
.u
.num
= tokval
.num
;
1086 err("constant literal expected");
1089 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
|| t
== Tglo
);
1093 err(", or } expected");
1105 for (haslnk
=0;; haslnk
=1)
1106 switch ((t
=nextnl())) {
1115 err("only one section allowed");
1117 err("section \"name\" expected");
1118 lnk
->sec
= tokval
.str
;
1119 if (peek() == Tstr
) {
1121 lnk
->secf
= tokval
.str
;
1125 if (t
== Tfunc
&& lnk
->thread
)
1126 err("only data may have thread linkage");
1127 if (haslnk
&& t
!= Tdata
&& t
!= Tfunc
)
1128 err("only data and function have linkage");
1134 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1145 typ
= vnew(0, sizeof typ
[0], PHeap
);
1148 switch (parselnk(&lnk
)) {
1150 err("top-level definition expected");
1152 func(parsefn(&lnk
));
1155 parsedat(data
, &lnk
);
1161 for (n
=0; n
<ntyp
; n
++)
1163 vfree(typ
[n
].fields
);
1171 printcon(Con
*c
, FILE *f
)
1177 fprintf(f
, "$%s", str(c
->sym
.id
));
1179 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1183 fprintf(f
, "s_%f", c
->bits
.s
);
1184 else if (c
->flt
== 2)
1185 fprintf(f
, "d_%lf", c
->bits
.d
);
1187 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1193 printref(Ref r
, Fn
*fn
, FILE *f
)
1201 fprintf(f
, "R%d", r
.val
);
1203 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1206 printcon(&fn
->con
[r
.val
], f
);
1209 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1212 fprintf(f
, "%04x", r
.val
);
1215 fprintf(f
, ":%s", typ
[r
.val
].name
);
1219 m
= &fn
->mem
[r
.val
];
1221 if (m
->offset
.type
!= CUndef
) {
1222 printcon(&m
->offset
, f
);
1225 if (!req(m
->base
, R
)) {
1228 printref(m
->base
, fn
, f
);
1231 if (!req(m
->index
, R
)) {
1234 fprintf(f
, "%d * ", m
->scale
);
1235 printref(m
->index
, fn
, f
);
1243 printfn(Fn
*fn
, FILE *f
)
1245 static char ktoc
[] = "wlsd";
1246 static char *jtoa
[NJmp
] = {
1247 #define X(j) [J##j] = #j,
1256 fprintf(f
, "function $%s() {\n", fn
->name
);
1257 for (b
=fn
->start
; b
; b
=b
->link
) {
1258 fprintf(f
, "@%s\n", b
->name
);
1259 for (p
=b
->phi
; p
; p
=p
->link
) {
1261 printref(p
->to
, fn
, f
);
1262 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1265 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1266 printref(p
->arg
[n
], fn
, f
);
1267 if (n
== p
->narg
-1) {
1274 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1276 if (!req(i
->to
, R
)) {
1277 printref(i
->to
, fn
, f
);
1278 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1280 assert(optab
[i
->op
].name
);
1281 fprintf(f
, "%s", optab
[i
->op
].name
);
1293 fputc(ktoc
[i
->cls
], f
);
1295 if (!req(i
->arg
[0], R
)) {
1297 printref(i
->arg
[0], fn
, f
);
1299 if (!req(i
->arg
[1], R
)) {
1301 printref(i
->arg
[1], fn
, f
);
1305 switch (b
->jmp
.type
) {
1316 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1317 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1319 printref(b
->jmp
.arg
, fn
, f
);
1321 if (b
->jmp
.type
== Jretc
)
1322 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1326 if (b
->s1
!= b
->link
)
1327 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1330 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1331 if (b
->jmp
.type
== Jjnz
) {
1332 printref(b
->jmp
.arg
, fn
, f
);
1335 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);