6 Ke
= -2, /* Erroneous mode */
7 Km
= Kl
, /* Memory pointer */
11 #define O(op, t, cf) [O##op]={#op, t, cf},
77 static char *kwmap
[Ntok
] = {
94 [Tsection
] = "section",
109 TMask
= 16383, /* for temps hash */
110 BMask
= 8191, /* for blocks hash */
112 K
= 3233235, /* found using tools/lexh.c */
116 static char lexh
[1 << (32-M
)];
130 static int tmph
[TMask
+1];
134 static Blk
*blkh
[BMask
+1];
145 fprintf(stderr
, "%s:%d: ", inpath
, lnum
);
146 vfprintf(stderr
, s
, ap
);
147 fprintf(stderr
, "\n");
161 for (i
=0; i
<NPubOp
; ++i
)
163 kwmap
[i
] = optab
[i
].name
;
164 assert(Ntok
<= CHAR_MAX
);
165 for (i
=0; i
<Ntok
; ++i
)
167 h
= hash(kwmap
[i
])*K
>> M
;
168 assert(lexh
[h
] == Txxx
);
186 n
= 10*n
+ (c
- '0');
188 } while ('0' <= c
&& c
<= '9');
192 return *(int64_t *)&n
;
198 static char tok
[NString
];
225 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
229 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
242 if ((c
= fgetc(inf
)) == '"')
250 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
257 if (isdigit(c
) || c
== '-' || c
== '+') {
259 tokval
.num
= getint();
265 tokval
.str
= vnew(2, 1, Pfn
);
271 err("unterminated string");
272 vgrow(&tokval
.str
, i
+2);
274 if (c
== '"' && !esc
) {
278 esc
= (c
== '\\' && !esc
);
282 if (!isalpha(c
) && c
!= '.' && c
!= '_')
283 err("invalid character %c (%d)", c
, c
);
287 err("identifier too long");
290 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
297 t
= lexh
[hash(tok
)*K
>> M
];
298 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
299 err("unknown keyword %s", tok
);
328 while ((t
= next()) == Tnl
)
336 static char *ttoa
[] = {
347 char buf
[128], *s1
, *s2
;
353 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
354 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
355 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
364 h
= &tmph
[hash(v
) & TMask
];
367 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
369 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
370 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
376 strcpy(curf
->tmp
[t
].name
, v
);
386 memset(&c
, 0, sizeof c
);
389 return tmpref(tokval
.str
);
392 c
.bits
.i
= tokval
.num
;
396 c
.bits
.s
= tokval
.flts
;
401 c
.bits
.d
= tokval
.fltd
;
406 c
.label
= intern(tokval
.str
);
408 for (i
=0; i
<curf
->ncon
; i
++)
409 if (curf
->con
[i
].type
== c
.type
410 && curf
->con
[i
].bits
.i
== c
.bits
.i
411 && curf
->con
[i
].label
== c
.label
)
413 vgrow(&curf
->con
, ++curf
->ncon
);
425 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
427 err("undefined type :%s", tokval
.str
);
435 err("invalid class specifier");
437 *tyn
= findtyp(ntyp
);
453 int k
, ty
, env
, hasenv
;
458 while (peek() != Trparen
&& peek() != Tdots
) {
459 if (curi
- insb
>= NIns
)
460 err("too many instructions (1)");
461 env
= peek() == Tenv
;
469 err("invalid argument");
471 err("only one environment allowed");
472 if (!arg
&& rtype(r
) != RTmp
)
473 err("invalid function parameter");
476 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
478 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
481 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
483 *curi
= (Ins
){Opare
, k
, r
, {R
}};
486 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
488 *curi
= (Ins
){Opar
, k
, r
, {R
}};
491 if (peek() == Trparen
)
495 if (next() == Tdots
) {
508 h
= hash(name
) & BMask
;
509 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
510 if (strcmp(b
->name
, name
) == 0)
514 strcpy(b
->name
, name
);
523 curb
->nins
= curi
- insb
;
524 idup(&curb
->ins
, insb
, curb
->nins
);
532 Ref arg
[NPred
] = {R
};
540 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
541 err("label or } expected");
547 /* operations without result */
553 err("label, instruction or jump expected");
559 b
= findblk(tokval
.str
);
560 if (curb
&& curb
->jmp
.type
== Jxxx
) {
562 curb
->jmp
.type
= Jjmp
;
565 if (b
->jmp
.type
!= Jxxx
)
566 err("multiple definitions of block @%s", b
->name
);
573 curb
->jmp
.type
= (int[]){
579 curb
->jmp
.type
= Jret0
;
583 err("invalid return value");
588 curb
->jmp
.type
= Jjmp
;
591 curb
->jmp
.type
= Jjnz
;
594 err("invalid argument for jnz jump");
599 curb
->s1
= findblk(tokval
.str
);
600 if (curb
->jmp
.type
!= Jjmp
) {
603 curb
->s2
= findblk(tokval
.str
);
605 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
606 err("invalid jump to the start node");
612 r
= tmpref(tokval
.str
);
618 if (ps
!= PPhi
|| curb
== curf
->start
)
619 err("unexpected phi instruction");
636 if (op
>= Tloadw
&& op
<= Tloadd
)
638 if (op
== Talloc1
|| op
== Talloc2
)
641 err("size class must be w, l, s, or d");
643 err("invalid instruction");
648 err("too many arguments");
651 blk
[i
] = findblk(tokval
.str
);
655 err("invalid instruction argument");
661 err(", or end of line expected");
667 if (curi
- insb
>= NIns
)
668 err("too many instructions (2)");
672 curi
->arg
[0] = arg
[0];
673 curi
->arg
[1] = arg
[1];
677 phi
= alloc(sizeof *phi
);
680 phi
->arg
= vnew(i
, sizeof arg
[0], Pfn
);
681 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
682 phi
->blk
= vnew(i
, sizeof blk
[0], Pfn
);
683 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
692 usecheck(Ref r
, int k
, Fn
*fn
)
694 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
695 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
711 bsinit(pb
, fn
->nblk
);
712 bsinit(ppb
, fn
->nblk
);
713 for (b
=fn
->start
; b
; b
=b
->link
) {
714 for (p
=b
->phi
; p
; p
=p
->link
)
715 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
716 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
717 if (rtype(i
->to
) == RTmp
) {
718 t
= &fn
->tmp
[i
->to
.val
];
719 if (clsmerge(&t
->cls
, i
->cls
))
720 err("temporary %%%s is assigned with"
721 " multiple types", t
->name
);
724 for (b
=fn
->start
; b
; b
=b
->link
) {
726 for (n
=0; n
<b
->npred
; n
++)
727 bsset(pb
, b
->pred
[n
]->id
);
728 for (p
=b
->phi
; p
; p
=p
->link
) {
730 t
= &fn
->tmp
[p
->to
.val
];
731 for (n
=0; n
<p
->narg
; n
++) {
733 if (bshas(ppb
, p
->blk
[n
]->id
))
734 err("multiple entries for @%s in phi %%%s",
735 p
->blk
[n
]->name
, t
->name
);
736 if (!usecheck(p
->arg
[n
], k
, fn
))
737 err("invalid type for operand %%%s in phi %%%s",
738 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
739 bsset(ppb
, p
->blk
[n
]->id
);
741 if (!bsequal(pb
, ppb
))
742 err("predecessors not matched in phi %%%s", t
->name
);
744 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
745 for (n
=0; n
<2; n
++) {
746 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
750 err("invalid instruction type in %s",
752 if (rtype(r
) == RType
)
754 if (rtype(r
) != -1 && k
== Kx
)
755 err("no %s operand expected in %s",
756 n
== 1 ? "second" : "first",
758 if (rtype(r
) == -1 && k
!= Kx
)
759 err("missing %s operand in %s",
760 n
== 1 ? "second" : "first",
762 if (!usecheck(r
, k
, fn
))
763 err("invalid type for %s operand %%%s in %s",
764 n
== 1 ? "second" : "first",
765 t
->name
, optab
[i
->op
].name
);
768 if (isret(b
->jmp
.type
)) {
769 if (b
->jmp
.type
== Jretc
) {
770 if (!usecheck(r
, Kl
, fn
))
772 } else if (!usecheck(r
, b
->jmp
.type
-Jretw
, fn
))
775 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
777 err("invalid type for jump argument %%%s in block @%s",
778 fn
->tmp
[r
.val
].name
, b
->name
);
779 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
780 err("block @%s is used undefined", b
->s1
->name
);
781 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
782 err("block @%s is used undefined", b
->s2
->name
);
796 curf
= alloc(sizeof *curf
);
798 curf
->ncon
= 1; /* first constant must be 0 */
799 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], Pfn
);
800 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], Pfn
);
801 for (i
=0; i
<Tmp0
; ++i
)
802 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
806 curf
->con
[0].type
= CBits
;
807 curf
->export
= export
;
808 blink
= &curf
->start
;
811 rcls
= parsecls(&curf
->retty
);
815 err("function name expected");
816 strncpy(curf
->name
, tokval
.str
, NString
-1);
817 curf
->vararg
= parserefl(0);
818 if (nextnl() != Tlbrace
)
819 err("function body must start with {");
825 err("empty function");
826 if (curb
->jmp
.type
== Jxxx
)
827 err("last block misses jump");
828 curf
->mem
= vnew(0, sizeof curf
->mem
[0], Pfn
);
832 for (b
=0; b
; b
=b
->link
)
833 b
->dlink
= 0; /* was trashed by findblk() */
834 for (i
=0; i
<BMask
+1; ++i
)
836 memset(tmph
, 0, sizeof tmph
);
842 parsefields(Field
*fld
, Typ
*ty
, int t
)
845 int n
, c
, a
, al
, type
;
851 while (t
!= Trbrace
) {
854 default: err("invalid type member specifier");
855 case Td
: type
= Fd
; s
= 8; a
= 3; break;
856 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
857 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
858 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
859 case Th
: type
= Fh
; s
= 2; a
= 1; break;
860 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
863 ty1
= &typ
[findtyp(ntyp
-1)];
871 a
= ((sz
+ a
) & ~a
) - sz
;
889 for (; c
>0 && n
<NField
; c
--, n
++) {
898 err(", or } expected");
903 ty
->size
= (sz
+ a
- 1) & -a
;
914 /* be careful if extending the syntax
915 * to handle nested types, any pointer
916 * held to typ[] might be invalidated!
923 if (nextnl() != Ttyp
|| nextnl() != Teq
)
924 err("type name and then = expected");
925 strcpy(ty
->name
, tokval
.str
);
928 if (nextnl() != Tint
)
929 err("alignment expected");
930 for (al
=0; tokval
.num
/= 2; al
++)
936 err("type body must start with {");
940 ty
->size
= tokval
.num
;
942 err("dark types need alignment");
943 if (nextnl() != Trbrace
)
948 ty
->fields
= vnew(1, sizeof ty
->fields
[0], Pheap
);
952 err("invalid union member");
953 vgrow(&ty
->fields
, n
+1);
954 parsefields(ty
->fields
[n
++], ty
, nextnl());
956 } while (t
!= Trbrace
);
958 parsefields(ty
->fields
[n
++], ty
, t
);
968 d
->u
.ref
.nam
= tokval
.str
;
974 err("invalid token after offset in ref");
975 d
->u
.ref
.off
= tokval
.num
;
983 d
->u
.str
= tokval
.str
;
987 parsedat(void cb(Dat
*), int export
)
989 char name
[NString
] = {0};
993 if (nextnl() != Tglo
|| nextnl() != Teq
)
994 err("data name, then = expected");
995 strncpy(name
, tokval
.str
, NString
-1);
999 if (nextnl() != Tstr
)
1000 err("section \"name\" expected");
1001 d
.u
.str
= tokval
.str
;
1007 if (nextnl() != Tint
)
1008 err("alignment expected");
1010 d
.u
.num
= tokval
.num
;
1022 err("expected data contents in { .. }");
1025 default: err("invalid size specifier %c in data", tokval
.chr
);
1026 case Trbrace
: goto Done
;
1027 case Tl
: d
.type
= DL
; break;
1028 case Tw
: d
.type
= DW
; break;
1029 case Th
: d
.type
= DH
; break;
1030 case Tb
: d
.type
= DB
; break;
1031 case Ts
: d
.type
= DW
; break;
1032 case Td
: d
.type
= DL
; break;
1033 case Tz
: d
.type
= DZ
; break;
1039 memset(&d
.u
, 0, sizeof d
.u
);
1041 d
.u
.flts
= tokval
.flts
;
1042 else if (t
== Tfltd
)
1043 d
.u
.fltd
= tokval
.fltd
;
1045 d
.u
.num
= tokval
.num
;
1051 err("constant literal expected");
1054 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
);
1058 err(", or } expected");
1066 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1076 typ
= vnew(0, sizeof typ
[0], Pheap
);
1081 err("top-level definition expected");
1087 func(parsefn(export
));
1090 else if (t
== Tdata
) {
1092 parsedat(data
, export
);
1096 err("export can only qualify data and function");
1108 printcon(Con
*c
, FILE *f
)
1114 fprintf(f
, "$%s", str(c
->label
));
1116 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1120 fprintf(f
, "s_%f", c
->bits
.s
);
1121 else if (c
->flt
== 2)
1122 fprintf(f
, "d_%lf", c
->bits
.d
);
1124 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1130 printref(Ref r
, Fn
*fn
, FILE *f
)
1138 fprintf(f
, "R%d", r
.val
);
1140 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1143 printcon(&fn
->con
[r
.val
], f
);
1146 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1149 fprintf(f
, "%04x", r
.val
);
1152 fprintf(f
, ":%s", typ
[r
.val
].name
);
1156 m
= &fn
->mem
[r
.val
];
1158 if (m
->offset
.type
!= CUndef
) {
1159 printcon(&m
->offset
, f
);
1162 if (!req(m
->base
, R
)) {
1165 printref(m
->base
, fn
, f
);
1168 if (!req(m
->index
, R
)) {
1171 fprintf(f
, "%d * ", m
->scale
);
1172 printref(m
->index
, fn
, f
);
1180 printfn(Fn
*fn
, FILE *f
)
1182 static char ktoc
[] = "wlsd";
1183 static char *jtoa
[NJmp
] = {
1184 #define X(j) [J##j] = #j,
1194 fprintf(f
, "export ");
1195 fprintf(f
, "function $%s() {\n", fn
->name
);
1196 for (b
=fn
->start
; b
; b
=b
->link
) {
1197 fprintf(f
, "@%s\n", b
->name
);
1198 for (p
=b
->phi
; p
; p
=p
->link
) {
1200 printref(p
->to
, fn
, f
);
1201 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1204 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1205 printref(p
->arg
[n
], fn
, f
);
1206 if (n
== p
->narg
-1) {
1213 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1215 if (!req(i
->to
, R
)) {
1216 printref(i
->to
, fn
, f
);
1217 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1219 assert(optab
[i
->op
].name
);
1220 fprintf(f
, "%s", optab
[i
->op
].name
);
1232 fputc(ktoc
[i
->cls
], f
);
1234 if (!req(i
->arg
[0], R
)) {
1236 printref(i
->arg
[0], fn
, f
);
1238 if (!req(i
->arg
[1], R
)) {
1240 printref(i
->arg
[1], fn
, f
);
1244 switch (b
->jmp
.type
) {
1251 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1252 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1254 printref(b
->jmp
.arg
, fn
, f
);
1256 if (b
->jmp
.type
== Jretc
)
1257 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1261 if (b
->s1
!= b
->link
)
1262 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1265 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1266 if (b
->jmp
.type
== Jjnz
) {
1267 printref(b
->jmp
.arg
, fn
, f
);
1270 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);