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},
91 static char *kwmap
[Ntok
] = {
106 [Texport
] = "export",
107 [Tthread
] = "thread",
108 [Tfunc
] = "function",
111 [Tsection
] = "section",
130 TMask
= 16383, /* for temps hash */
131 BMask
= 8191, /* for blocks hash */
133 K
= 9583425, /* found using tools/lexh.c */
137 static uchar lexh
[1 << (32-M
)];
151 static int tmph
[TMask
+1];
155 static Blk
*blkh
[BMask
+1];
166 fprintf(stderr
, "qbe:%s:%d: ", inpath
, lnum
);
167 vfprintf(stderr
, s
, ap
);
168 fprintf(stderr
, "\n");
182 for (i
=0; i
<NPubOp
; ++i
)
184 kwmap
[i
] = optab
[i
].name
;
185 assert(Ntok
<= UCHAR_MAX
);
186 for (i
=0; i
<Ntok
; ++i
)
188 h
= hash(kwmap
[i
])*K
>> M
;
189 assert(lexh
[h
] == Txxx
);
207 n
= 10*n
+ (c
- '0');
209 } while ('0' <= c
&& c
<= '9');
213 return *(int64_t *)&n
;
219 static char tok
[NString
];
246 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
250 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
263 if ((c
= fgetc(inf
)) == '"')
271 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
278 if (isdigit(c
) || c
== '-' || c
== '+') {
280 tokval
.num
= getint();
286 tokval
.str
= vnew(2, 1, PFn
);
292 err("unterminated string");
293 vgrow(&tokval
.str
, i
+2);
295 if (c
== '"' && !esc
) {
299 esc
= (c
== '\\' && !esc
);
303 if (!isalpha(c
) && c
!= '.' && c
!= '_')
304 err("invalid character %c (%d)", c
, c
);
308 err("identifier too long");
311 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
318 t
= lexh
[hash(tok
)*K
>> M
];
319 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
320 err("unknown keyword %s", tok
);
349 while ((t
= next()) == Tnl
)
357 static char *ttoa
[] = {
368 char buf
[128], *s1
, *s2
;
374 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
375 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
376 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
385 h
= &tmph
[hash(v
) & TMask
];
388 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
390 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
391 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
397 strcpy(curf
->tmp
[t
].name
, v
);
406 memset(&c
, 0, sizeof c
);
411 return tmpref(tokval
.str
);
414 c
.bits
.i
= tokval
.num
;
418 c
.bits
.s
= tokval
.flts
;
423 c
.bits
.d
= tokval
.fltd
;
432 c
.sym
.id
= intern(tokval
.str
);
435 return newcon(&c
, curf
);
442 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
444 err("undefined type :%s", tokval
.str
);
452 err("invalid class specifier");
454 *tyn
= findtyp(ntyp
);
478 int k
, ty
, env
, hasenv
, vararg
;
484 while (peek() != Trparen
) {
485 if (curi
- insb
>= NIns
)
486 err("too many instructions");
488 err("no parameters allowed after '...'");
492 err("only one '...' allowed");
495 *curi
= (Ins
){.op
= Oargv
};
502 err("only one environment allowed");
515 err("invalid argument");
516 if (!arg
&& rtype(r
) != RTmp
)
517 err("invalid function parameter");
520 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
522 *curi
= (Ins
){Opare
, k
, r
, {R
}};
525 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
527 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
530 *curi
= (Ins
){Oargsb
+(k
-Ksb
), Kw
, R
, {r
}};
532 *curi
= (Ins
){Oparsb
+(k
-Ksb
), Kw
, r
, {R
}};
535 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
537 *curi
= (Ins
){Opar
, k
, r
, {R
}};
540 if (peek() == Trparen
)
554 h
= hash(name
) & BMask
;
555 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
556 if (strcmp(b
->name
, name
) == 0)
560 strcpy(b
->name
, name
);
569 curb
->nins
= curi
- insb
;
570 idup(&curb
->ins
, insb
, curb
->nins
);
578 Ref arg
[NPred
] = {R
};
587 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
588 err("label or } expected");
595 /* operations without result */
601 err("label, instruction or jump expected");
607 b
= findblk(tokval
.str
);
608 if (curb
&& curb
->jmp
.type
== Jxxx
) {
610 curb
->jmp
.type
= Jjmp
;
613 if (b
->jmp
.type
!= Jxxx
)
614 err("multiple definitions of block @%s", b
->name
);
621 curb
->jmp
.type
= Jretw
+ rcls
;
623 curb
->jmp
.type
= Jret0
;
624 else if (rcls
!= K0
) {
627 err("invalid return value");
632 curb
->jmp
.type
= Jjmp
;
635 curb
->jmp
.type
= Jjnz
;
638 err("invalid argument for jnz jump");
643 curb
->s1
= findblk(tokval
.str
);
644 if (curb
->jmp
.type
!= Jjmp
) {
647 curb
->s2
= findblk(tokval
.str
);
649 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
650 err("invalid jump to the start block");
653 curb
->jmp
.type
= Jhlt
;
659 r
= tmpref(tokval
.str
);
680 if (op
>= Tloadl
&& op
<= Tloadd
)
682 if (op
== Talloc1
|| op
== Talloc2
)
684 if (op
== Ovastart
&& !curf
->vararg
)
685 err("cannot use vastart in non-variadic function");
687 err("size class must be w, l, s, or d");
692 err("too many arguments");
695 blk
[i
] = findblk(tokval
.str
);
699 err("invalid instruction argument");
705 err(", or end of line expected");
711 if (ps
!= PPhi
|| curb
== curf
->start
)
712 err("unexpected phi instruction");
713 phi
= alloc(sizeof *phi
);
716 phi
->arg
= vnew(i
, sizeof arg
[0], PFn
);
717 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
718 phi
->blk
= vnew(i
, sizeof blk
[0], PFn
);
719 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
725 if (curi
- insb
>= NIns
-1)
726 err("too many instructions");
727 memset(curi
, 0, 2 * sizeof(Ins
));
729 curi
->arg
[0] = arg
[0];
730 curi
->arg
[1] = arg
[1];
732 if (rtype(arg
[2]) != RCon
)
733 err("blit size must be constant");
734 c
= &curf
->con
[arg
[2].val
];
738 || rsval(r
) != c
->bits
.i
)
739 err("invalid blit size");
746 err("invalid instruction");
748 if (curi
- insb
>= NIns
)
749 err("too many instructions");
753 curi
->arg
[0] = arg
[0];
754 curi
->arg
[1] = arg
[1];
761 usecheck(Ref r
, int k
, Fn
*fn
)
763 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
764 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
780 bsinit(pb
, fn
->nblk
);
781 bsinit(ppb
, fn
->nblk
);
782 for (b
=fn
->start
; b
; b
=b
->link
) {
783 for (p
=b
->phi
; p
; p
=p
->link
)
784 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
785 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
786 if (rtype(i
->to
) == RTmp
) {
787 t
= &fn
->tmp
[i
->to
.val
];
788 if (clsmerge(&t
->cls
, i
->cls
))
789 err("temporary %%%s is assigned with"
790 " multiple types", t
->name
);
793 for (b
=fn
->start
; b
; b
=b
->link
) {
795 for (n
=0; n
<b
->npred
; n
++)
796 bsset(pb
, b
->pred
[n
]->id
);
797 for (p
=b
->phi
; p
; p
=p
->link
) {
799 t
= &fn
->tmp
[p
->to
.val
];
800 for (n
=0; n
<p
->narg
; n
++) {
802 if (bshas(ppb
, p
->blk
[n
]->id
))
803 err("multiple entries for @%s in phi %%%s",
804 p
->blk
[n
]->name
, t
->name
);
805 if (!usecheck(p
->arg
[n
], k
, fn
))
806 err("invalid type for operand %%%s in phi %%%s",
807 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
808 bsset(ppb
, p
->blk
[n
]->id
);
810 if (!bsequal(pb
, ppb
))
811 err("predecessors not matched in phi %%%s", t
->name
);
813 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
814 for (n
=0; n
<2; n
++) {
815 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
819 err("invalid instruction type in %s",
821 if (rtype(r
) == RType
)
823 if (rtype(r
) != -1 && k
== Kx
)
824 err("no %s operand expected in %s",
825 n
== 1 ? "second" : "first",
827 if (rtype(r
) == -1 && k
!= Kx
)
828 err("missing %s operand in %s",
829 n
== 1 ? "second" : "first",
831 if (!usecheck(r
, k
, fn
))
832 err("invalid type for %s operand %%%s in %s",
833 n
== 1 ? "second" : "first",
834 t
->name
, optab
[i
->op
].name
);
837 if (isret(b
->jmp
.type
)) {
838 if (b
->jmp
.type
== Jretc
)
840 else if (b
->jmp
.type
>= Jretsb
)
843 k
= b
->jmp
.type
- Jretw
;
844 if (!usecheck(r
, k
, fn
))
847 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
849 err("invalid type for jump argument %%%s in block @%s",
850 fn
->tmp
[r
.val
].name
, b
->name
);
851 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
852 err("block @%s is used undefined", b
->s1
->name
);
853 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
854 err("block @%s is used undefined", b
->s2
->name
);
868 curf
= alloc(sizeof *curf
);
871 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], PFn
);
872 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], PFn
);
873 for (i
=0; i
<Tmp0
; ++i
)
874 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
878 curf
->con
[0].type
= CBits
;
879 curf
->con
[0].bits
.i
= 0xdeaddead; /* UNDEF */
880 curf
->con
[1].type
= CBits
;
882 blink
= &curf
->start
;
885 rcls
= parsecls(&curf
->retty
);
889 err("function name expected");
890 strncpy(curf
->name
, tokval
.str
, NString
-1);
891 curf
->vararg
= parserefl(0);
892 if (nextnl() != Tlbrace
)
893 err("function body must start with {");
899 err("empty function");
900 if (curb
->jmp
.type
== Jxxx
)
901 err("last block misses jump");
902 curf
->mem
= vnew(0, sizeof curf
->mem
[0], PFn
);
906 for (b
=0; b
; b
=b
->link
)
907 b
->dlink
= 0; /* was trashed by findblk() */
908 for (i
=0; i
<BMask
+1; ++i
)
910 memset(tmph
, 0, sizeof tmph
);
916 parsefields(Field
*fld
, Typ
*ty
, int t
)
919 int n
, c
, a
, al
, type
;
925 while (t
!= Trbrace
) {
928 default: err("invalid type member specifier");
929 case Td
: type
= Fd
; s
= 8; a
= 3; break;
930 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
931 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
932 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
933 case Th
: type
= Fh
; s
= 2; a
= 1; break;
934 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
937 ty1
= &typ
[findtyp(ntyp
-1)];
945 a
= ((sz
+ a
) & ~a
) - sz
;
963 for (; c
>0 && n
<NField
; c
--, n
++) {
972 err(", or } expected");
977 ty
->size
= (sz
+ a
- 1) & -a
;
988 /* be careful if extending the syntax
989 * to handle nested types, any pointer
990 * held to typ[] might be invalidated!
998 if (nextnl() != Ttyp
|| nextnl() != Teq
)
999 err("type name and then = expected");
1000 strcpy(ty
->name
, tokval
.str
);
1003 if (nextnl() != Tint
)
1004 err("alignment expected");
1005 for (al
=0; tokval
.num
/= 2; al
++)
1011 err("type body must start with {");
1015 ty
->size
= tokval
.num
;
1016 if (ty
->align
== -1)
1017 err("dark types need alignment");
1018 if (nextnl() != Trbrace
)
1023 ty
->fields
= vnew(1, sizeof ty
->fields
[0], PHeap
);
1028 err("invalid union member");
1029 vgrow(&ty
->fields
, n
+1);
1030 parsefields(ty
->fields
[n
++], ty
, nextnl());
1032 } while (t
!= Trbrace
);
1034 parsefields(ty
->fields
[n
++], ty
, t
);
1044 d
->u
.ref
.name
= tokval
.str
;
1050 err("invalid token after offset in ref");
1051 d
->u
.ref
.off
= tokval
.num
;
1059 d
->u
.str
= tokval
.str
;
1063 parsedat(void cb(Dat
*), Lnk
*lnk
)
1065 char name
[NString
] = {0};
1069 if (nextnl() != Tglo
|| nextnl() != Teq
)
1070 err("data name, then = expected");
1071 strncpy(name
, tokval
.str
, NString
-1);
1075 if (nextnl() != Tint
)
1076 err("alignment expected");
1077 lnk
->align
= tokval
.num
;
1086 err("expected data contents in { .. }");
1089 default: err("invalid size specifier %c in data", tokval
.chr
);
1090 case Trbrace
: goto Done
;
1091 case Tl
: d
.type
= DL
; break;
1092 case Tw
: d
.type
= DW
; break;
1093 case Th
: d
.type
= DH
; break;
1094 case Tb
: d
.type
= DB
; break;
1095 case Ts
: d
.type
= DW
; break;
1096 case Td
: d
.type
= DL
; break;
1097 case Tz
: d
.type
= DZ
; break;
1103 memset(&d
.u
, 0, sizeof d
.u
);
1105 d
.u
.flts
= tokval
.flts
;
1106 else if (t
== Tfltd
)
1107 d
.u
.fltd
= tokval
.fltd
;
1109 d
.u
.num
= tokval
.num
;
1115 err("constant literal expected");
1118 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
|| t
== Tglo
);
1122 err(", or } expected");
1134 for (haslnk
=0;; haslnk
=1)
1135 switch ((t
=nextnl())) {
1144 err("only one section allowed");
1146 err("section \"name\" expected");
1147 lnk
->sec
= tokval
.str
;
1148 if (peek() == Tstr
) {
1150 lnk
->secf
= tokval
.str
;
1154 if (t
== Tfunc
&& lnk
->thread
)
1155 err("only data may have thread linkage");
1156 if (haslnk
&& t
!= Tdata
&& t
!= Tfunc
)
1157 err("only data and function have linkage");
1163 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1174 typ
= vnew(0, sizeof typ
[0], PHeap
);
1177 switch (parselnk(&lnk
)) {
1179 err("top-level definition expected");
1181 func(parsefn(&lnk
));
1184 parsedat(data
, &lnk
);
1190 for (n
=0; n
<ntyp
; n
++)
1192 vfree(typ
[n
].fields
);
1200 printcon(Con
*c
, FILE *f
)
1206 if (c
->sym
.type
== SThr
)
1207 fprintf(f
, "thread ");
1208 fprintf(f
, "$%s", str(c
->sym
.id
));
1210 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1214 fprintf(f
, "s_%f", c
->bits
.s
);
1215 else if (c
->flt
== 2)
1216 fprintf(f
, "d_%lf", c
->bits
.d
);
1218 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1224 printref(Ref r
, Fn
*fn
, FILE *f
)
1232 fprintf(f
, "R%d", r
.val
);
1234 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1238 fprintf(f
, "UNDEF");
1240 printcon(&fn
->con
[r
.val
], f
);
1243 fprintf(f
, "S%d", rsval(r
));
1246 fprintf(f
, "%04x", r
.val
);
1249 fprintf(f
, ":%s", typ
[r
.val
].name
);
1253 m
= &fn
->mem
[r
.val
];
1255 if (m
->offset
.type
!= CUndef
) {
1256 printcon(&m
->offset
, f
);
1259 if (!req(m
->base
, R
)) {
1262 printref(m
->base
, fn
, f
);
1265 if (!req(m
->index
, R
)) {
1268 fprintf(f
, "%d * ", m
->scale
);
1269 printref(m
->index
, fn
, f
);
1274 fprintf(f
, "%d", rsval(r
));
1280 printfn(Fn
*fn
, FILE *f
)
1282 static char ktoc
[] = "wlsd";
1283 static char *jtoa
[NJmp
] = {
1284 #define X(j) [J##j] = #j,
1293 fprintf(f
, "function $%s() {\n", fn
->name
);
1294 for (b
=fn
->start
; b
; b
=b
->link
) {
1295 fprintf(f
, "@%s\n", b
->name
);
1296 for (p
=b
->phi
; p
; p
=p
->link
) {
1298 printref(p
->to
, fn
, f
);
1299 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1302 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1303 printref(p
->arg
[n
], fn
, f
);
1304 if (n
== p
->narg
-1) {
1311 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1313 if (!req(i
->to
, R
)) {
1314 printref(i
->to
, fn
, f
);
1315 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1317 assert(optab
[i
->op
].name
);
1318 fprintf(f
, "%s", optab
[i
->op
].name
);
1330 fputc(ktoc
[i
->cls
], f
);
1332 if (!req(i
->arg
[0], R
)) {
1334 printref(i
->arg
[0], fn
, f
);
1336 if (!req(i
->arg
[1], R
)) {
1338 printref(i
->arg
[1], fn
, f
);
1342 switch (b
->jmp
.type
) {
1353 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1354 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1356 printref(b
->jmp
.arg
, fn
, f
);
1358 if (b
->jmp
.type
== Jretc
)
1359 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1363 fprintf(f
, "\thlt\n");
1366 if (b
->s1
!= b
->link
)
1367 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1370 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1371 if (b
->jmp
.type
== Jjnz
) {
1372 printref(b
->jmp
.arg
, fn
, f
);
1375 assert(b
->s1
&& b
->s2
);
1376 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);