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},
93 static char *kwmap
[Ntok
] = {
108 [Texport
] = "export",
109 [Tthread
] = "thread",
110 [Tcommon
] = "common",
111 [Tfunc
] = "function",
114 [Tsection
] = "section",
116 [Tdbgfile
] = "dbgfile",
134 TMask
= 16383, /* for temps hash */
135 BMask
= 8191, /* for blocks hash */
137 K
= 11183273, /* found using tools/lexh.c */
141 static uchar lexh
[1 << (32-M
)];
155 static int tmph
[TMask
+1];
159 static Blk
*blkh
[BMask
+1];
170 fprintf(stderr
, "qbe:%s:%d: ", inpath
, lnum
);
171 vfprintf(stderr
, s
, ap
);
172 fprintf(stderr
, "\n");
186 for (i
=0; i
<NPubOp
; ++i
)
188 kwmap
[i
] = optab
[i
].name
;
189 assert(Ntok
<= UCHAR_MAX
);
190 for (i
=0; i
<Ntok
; ++i
)
192 h
= hash(kwmap
[i
])*K
>> M
;
193 assert(lexh
[h
] == Txxx
);
211 n
= 10*n
+ (c
- '0');
213 } while ('0' <= c
&& c
<= '9');
217 return *(int64_t *)&n
;
223 static char tok
[NString
];
250 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
254 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
267 if ((c
= fgetc(inf
)) == '"')
275 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
282 if (isdigit(c
) || c
== '-') {
284 tokval
.num
= getint();
290 tokval
.str
= vnew(2, 1, PFn
);
296 err("unterminated string");
297 vgrow(&tokval
.str
, i
+2);
299 if (c
== '"' && !esc
) {
303 esc
= (c
== '\\' && !esc
);
307 if (!isalpha(c
) && c
!= '.' && c
!= '_')
308 err("invalid character %c (%d)", c
, c
);
312 err("identifier too long");
315 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
322 t
= lexh
[hash(tok
)*K
>> M
];
323 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
324 err("unknown keyword %s", tok
);
353 while ((t
= next()) == Tnl
)
361 static char *ttoa
[] = {
372 char buf
[128], *s1
, *s2
;
378 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
379 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
380 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
389 h
= &tmph
[hash(v
) & TMask
];
392 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
394 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
395 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
401 strcpy(curf
->tmp
[t
].name
, v
);
410 memset(&c
, 0, sizeof c
);
415 return tmpref(tokval
.str
);
418 c
.bits
.i
= tokval
.num
;
422 c
.bits
.s
= tokval
.flts
;
427 c
.bits
.d
= tokval
.fltd
;
436 c
.sym
.id
= intern(tokval
.str
);
439 return newcon(&c
, curf
);
446 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
448 err("undefined type :%s", tokval
.str
);
456 err("invalid class specifier");
458 *tyn
= findtyp(ntyp
);
482 int k
, ty
, env
, hasenv
, vararg
;
488 while (peek() != Trparen
) {
489 if (curi
- insb
>= NIns
)
490 err("too many instructions");
492 err("no parameters allowed after '...'");
496 err("only one '...' allowed");
499 *curi
= (Ins
){.op
= Oargv
};
506 err("only one environment allowed");
519 err("invalid argument");
520 if (!arg
&& rtype(r
) != RTmp
)
521 err("invalid function parameter");
524 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
526 *curi
= (Ins
){Opare
, k
, r
, {R
}};
529 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
531 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
534 *curi
= (Ins
){Oargsb
+(k
-Ksb
), Kw
, R
, {r
}};
536 *curi
= (Ins
){Oparsb
+(k
-Ksb
), Kw
, r
, {R
}};
539 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
541 *curi
= (Ins
){Opar
, k
, r
, {R
}};
544 if (peek() == Trparen
)
558 h
= hash(name
) & BMask
;
559 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
560 if (strcmp(b
->name
, name
) == 0)
564 strcpy(b
->name
, name
);
573 curb
->nins
= curi
- insb
;
574 idup(&curb
->ins
, insb
, curb
->nins
);
582 Ref arg
[NPred
] = {R
};
591 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
592 err("label or } expected");
595 r
= tmpref(tokval
.str
);
605 /* operations without result */
611 err("label, instruction or jump expected");
615 b
= findblk(tokval
.str
);
616 if (curb
&& curb
->jmp
.type
== Jxxx
) {
618 curb
->jmp
.type
= Jjmp
;
621 if (b
->jmp
.type
!= Jxxx
)
622 err("multiple definitions of block @%s", b
->name
);
629 curb
->jmp
.type
= Jretw
+ rcls
;
631 curb
->jmp
.type
= Jret0
;
632 else if (rcls
!= K0
) {
635 err("invalid return value");
640 curb
->jmp
.type
= Jjmp
;
643 curb
->jmp
.type
= Jjnz
;
646 err("invalid argument for jnz jump");
651 curb
->s1
= findblk(tokval
.str
);
652 if (curb
->jmp
.type
!= Jjmp
) {
655 curb
->s2
= findblk(tokval
.str
);
657 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
658 err("invalid jump to the start block");
661 curb
->jmp
.type
= Jhlt
;
671 arg
[0] = INT(tokval
.num
);
672 if (arg
[0].val
!= tokval
.num
)
673 err("line number too big");
674 if (peek() == Tcomma
) {
677 arg
[1] = INT(tokval
.num
);
678 if (arg
[1].val
!= tokval
.num
)
679 err("column number too big");
699 if (op
>= Tloadl
&& op
<= Tloadd
)
701 if (op
== Talloc1
|| op
== Talloc2
)
703 if (op
== Ovastart
&& !curf
->vararg
)
704 err("cannot use vastart in non-variadic function");
706 err("size class must be w, l, s, or d");
711 err("too many arguments");
714 blk
[i
] = findblk(tokval
.str
);
718 err("invalid instruction argument");
724 err(", or end of line expected");
730 if (ps
!= PPhi
|| curb
== curf
->start
)
731 err("unexpected phi instruction");
732 phi
= alloc(sizeof *phi
);
735 phi
->arg
= vnew(i
, sizeof arg
[0], PFn
);
736 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
737 phi
->blk
= vnew(i
, sizeof blk
[0], PFn
);
738 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
744 if (curi
- insb
>= NIns
-1)
745 err("too many instructions");
746 memset(curi
, 0, 2 * sizeof(Ins
));
748 curi
->arg
[0] = arg
[0];
749 curi
->arg
[1] = arg
[1];
751 if (rtype(arg
[2]) != RCon
)
752 err("blit size must be constant");
753 c
= &curf
->con
[arg
[2].val
];
757 || rsval(r
) != c
->bits
.i
)
758 err("invalid blit size");
765 err("invalid instruction");
767 if (curi
- insb
>= NIns
)
768 err("too many instructions");
772 curi
->arg
[0] = arg
[0];
773 curi
->arg
[1] = arg
[1];
780 usecheck(Ref r
, int k
, Fn
*fn
)
782 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
783 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
799 bsinit(pb
, fn
->nblk
);
800 bsinit(ppb
, fn
->nblk
);
801 for (b
=fn
->start
; b
; b
=b
->link
) {
802 for (p
=b
->phi
; p
; p
=p
->link
)
803 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
804 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
805 if (rtype(i
->to
) == RTmp
) {
806 t
= &fn
->tmp
[i
->to
.val
];
807 if (clsmerge(&t
->cls
, i
->cls
))
808 err("temporary %%%s is assigned with"
809 " multiple types", t
->name
);
812 for (b
=fn
->start
; b
; b
=b
->link
) {
814 for (n
=0; n
<b
->npred
; n
++)
815 bsset(pb
, b
->pred
[n
]->id
);
816 for (p
=b
->phi
; p
; p
=p
->link
) {
818 t
= &fn
->tmp
[p
->to
.val
];
819 for (n
=0; n
<p
->narg
; n
++) {
821 if (bshas(ppb
, p
->blk
[n
]->id
))
822 err("multiple entries for @%s in phi %%%s",
823 p
->blk
[n
]->name
, t
->name
);
824 if (!usecheck(p
->arg
[n
], k
, fn
))
825 err("invalid type for operand %%%s in phi %%%s",
826 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
827 bsset(ppb
, p
->blk
[n
]->id
);
829 if (!bsequal(pb
, ppb
))
830 err("predecessors not matched in phi %%%s", t
->name
);
832 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
833 for (n
=0; n
<2; n
++) {
834 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
838 err("invalid instruction type in %s",
840 if (rtype(r
) == RType
)
842 if (rtype(r
) != -1 && k
== Kx
)
843 err("no %s operand expected in %s",
844 n
== 1 ? "second" : "first",
846 if (rtype(r
) == -1 && k
!= Kx
)
847 err("missing %s operand in %s",
848 n
== 1 ? "second" : "first",
850 if (!usecheck(r
, k
, fn
))
851 err("invalid type for %s operand %%%s in %s",
852 n
== 1 ? "second" : "first",
853 t
->name
, optab
[i
->op
].name
);
856 if (isret(b
->jmp
.type
)) {
857 if (b
->jmp
.type
== Jretc
)
859 else if (b
->jmp
.type
>= Jretsb
)
862 k
= b
->jmp
.type
- Jretw
;
863 if (!usecheck(r
, k
, fn
))
866 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
868 err("invalid type for jump argument %%%s in block @%s",
869 fn
->tmp
[r
.val
].name
, b
->name
);
870 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
871 err("block @%s is used undefined", b
->s1
->name
);
872 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
873 err("block @%s is used undefined", b
->s2
->name
);
887 curf
= alloc(sizeof *curf
);
890 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], PFn
);
891 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], PFn
);
892 for (i
=0; i
<Tmp0
; ++i
)
893 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
897 curf
->con
[0].type
= CBits
;
898 curf
->con
[0].bits
.i
= 0xdeaddead; /* UNDEF */
899 curf
->con
[1].type
= CBits
;
901 blink
= &curf
->start
;
904 rcls
= parsecls(&curf
->retty
);
908 err("function name expected");
909 strncpy(curf
->name
, tokval
.str
, NString
-1);
910 curf
->vararg
= parserefl(0);
911 if (nextnl() != Tlbrace
)
912 err("function body must start with {");
918 err("empty function");
919 if (curb
->jmp
.type
== Jxxx
)
920 err("last block misses jump");
921 curf
->mem
= vnew(0, sizeof curf
->mem
[0], PFn
);
925 for (b
=curf
->start
; b
; b
=b
->link
)
926 b
->dlink
= 0; /* was trashed by findblk() */
927 for (i
=0; i
<BMask
+1; ++i
)
929 memset(tmph
, 0, sizeof tmph
);
935 parsefields(Field
*fld
, Typ
*ty
, int t
)
938 int n
, c
, a
, al
, type
;
944 while (t
!= Trbrace
) {
947 default: err("invalid type member specifier");
948 case Td
: type
= Fd
; s
= 8; a
= 3; break;
949 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
950 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
951 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
952 case Th
: type
= Fh
; s
= 2; a
= 1; break;
953 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
956 ty1
= &typ
[findtyp(ntyp
-1)];
964 a
= ((sz
+ a
) & ~a
) - sz
;
982 for (; c
>0 && n
<NField
; c
--, n
++) {
991 err(", or } expected");
996 ty
->size
= (sz
+ a
- 1) & -a
;
1007 /* be careful if extending the syntax
1008 * to handle nested types, any pointer
1009 * held to typ[] might be invalidated!
1011 vgrow(&typ
, ntyp
+1);
1017 if (nextnl() != Ttyp
|| nextnl() != Teq
)
1018 err("type name and then = expected");
1019 strcpy(ty
->name
, tokval
.str
);
1022 if (nextnl() != Tint
)
1023 err("alignment expected");
1024 for (al
=0; tokval
.num
/= 2; al
++)
1030 err("type body must start with {");
1034 ty
->size
= tokval
.num
;
1035 if (ty
->align
== -1)
1036 err("dark types need alignment");
1037 if (nextnl() != Trbrace
)
1042 ty
->fields
= vnew(1, sizeof ty
->fields
[0], PHeap
);
1047 err("invalid union member");
1048 vgrow(&ty
->fields
, n
+1);
1049 parsefields(ty
->fields
[n
++], ty
, nextnl());
1051 } while (t
!= Trbrace
);
1053 parsefields(ty
->fields
[n
++], ty
, t
);
1063 d
->u
.ref
.name
= tokval
.str
;
1069 err("invalid token after offset in ref");
1070 d
->u
.ref
.off
= tokval
.num
;
1078 d
->u
.str
= tokval
.str
;
1082 parsedat(void cb(Dat
*), Lnk
*lnk
)
1084 char name
[NString
] = {0};
1088 if (nextnl() != Tglo
|| nextnl() != Teq
)
1089 err("data name, then = expected");
1090 strncpy(name
, tokval
.str
, NString
-1);
1094 if (nextnl() != Tint
)
1095 err("alignment expected");
1096 if (tokval
.num
<= 0 || tokval
.num
> CHAR_MAX
1097 || (tokval
.num
& (tokval
.num
-1)) != 0)
1098 err("invalid alignment");
1099 lnk
->align
= tokval
.num
;
1108 err("expected data contents in { .. }");
1111 default: err("invalid size specifier %c in data", tokval
.chr
);
1112 case Trbrace
: goto Done
;
1113 case Tl
: d
.type
= DL
; break;
1114 case Tw
: d
.type
= DW
; break;
1115 case Th
: d
.type
= DH
; break;
1116 case Tb
: d
.type
= DB
; break;
1117 case Ts
: d
.type
= DW
; break;
1118 case Td
: d
.type
= DL
; break;
1119 case Tz
: d
.type
= DZ
; break;
1125 memset(&d
.u
, 0, sizeof d
.u
);
1127 d
.u
.flts
= tokval
.flts
;
1128 else if (t
== Tfltd
)
1129 d
.u
.fltd
= tokval
.fltd
;
1131 d
.u
.num
= tokval
.num
;
1137 err("constant literal expected");
1140 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
|| t
== Tglo
);
1144 err(", or } expected");
1156 for (haslnk
=0;; haslnk
=1)
1157 switch ((t
=nextnl())) {
1169 err("only one section allowed");
1171 err("section \"name\" expected");
1172 lnk
->sec
= tokval
.str
;
1173 if (peek() == Tstr
) {
1175 lnk
->secf
= tokval
.str
;
1179 if (t
== Tfunc
&& lnk
->thread
)
1180 err("only data may have thread linkage");
1181 if (haslnk
&& t
!= Tdata
&& t
!= Tfunc
)
1182 err("only data and function have linkage");
1188 parse(FILE *f
, char *path
, void dbgfile(char *), void data(Dat
*), void func(Fn
*))
1199 typ
= vnew(0, sizeof typ
[0], PHeap
);
1202 switch (parselnk(&lnk
)) {
1204 err("top-level definition expected");
1207 dbgfile(tokval
.str
);
1210 func(parsefn(&lnk
));
1213 parsedat(data
, &lnk
);
1219 for (n
=0; n
<ntyp
; n
++)
1221 vfree(typ
[n
].fields
);
1229 printcon(Con
*c
, FILE *f
)
1235 if (c
->sym
.type
== SThr
)
1236 fprintf(f
, "thread ");
1237 fprintf(f
, "$%s", str(c
->sym
.id
));
1239 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1243 fprintf(f
, "s_%f", c
->bits
.s
);
1244 else if (c
->flt
== 2)
1245 fprintf(f
, "d_%lf", c
->bits
.d
);
1247 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1253 printref(Ref r
, Fn
*fn
, FILE *f
)
1261 fprintf(f
, "R%d", r
.val
);
1263 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1267 fprintf(f
, "UNDEF");
1269 printcon(&fn
->con
[r
.val
], f
);
1272 fprintf(f
, "S%d", rsval(r
));
1275 fprintf(f
, "%04x", r
.val
);
1278 fprintf(f
, ":%s", typ
[r
.val
].name
);
1282 m
= &fn
->mem
[r
.val
];
1284 if (m
->offset
.type
!= CUndef
) {
1285 printcon(&m
->offset
, f
);
1288 if (!req(m
->base
, R
)) {
1291 printref(m
->base
, fn
, f
);
1294 if (!req(m
->index
, R
)) {
1297 fprintf(f
, "%d * ", m
->scale
);
1298 printref(m
->index
, fn
, f
);
1303 fprintf(f
, "%d", rsval(r
));
1312 printfn(Fn
*fn
, FILE *f
)
1314 static char ktoc
[] = "wlsd";
1315 static char *jtoa
[NJmp
] = {
1316 #define X(j) [J##j] = #j,
1325 fprintf(f
, "function $%s() {\n", fn
->name
);
1326 for (b
=fn
->start
; b
; b
=b
->link
) {
1327 fprintf(f
, "@%s\n", b
->name
);
1328 for (p
=b
->phi
; p
; p
=p
->link
) {
1330 printref(p
->to
, fn
, f
);
1331 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1334 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1335 printref(p
->arg
[n
], fn
, f
);
1336 if (n
== p
->narg
-1) {
1343 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1345 if (!req(i
->to
, R
)) {
1346 printref(i
->to
, fn
, f
);
1347 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1349 assert(optab
[i
->op
].name
);
1350 fprintf(f
, "%s", optab
[i
->op
].name
);
1362 fputc(ktoc
[i
->cls
], f
);
1364 if (!req(i
->arg
[0], R
)) {
1366 printref(i
->arg
[0], fn
, f
);
1368 if (!req(i
->arg
[1], R
)) {
1370 printref(i
->arg
[1], fn
, f
);
1374 switch (b
->jmp
.type
) {
1385 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1386 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1388 printref(b
->jmp
.arg
, fn
, f
);
1390 if (b
->jmp
.type
== Jretc
)
1391 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1395 fprintf(f
, "\thlt\n");
1398 if (b
->s1
!= b
->link
)
1399 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1402 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1403 if (b
->jmp
.type
== Jjnz
) {
1404 printref(b
->jmp
.arg
, fn
, f
);
1407 assert(b
->s1
&& b
->s2
);
1408 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);