6 Ke
= -2, /* Erroneous mode */
7 Km
= Kl
, /* Memory pointer */
11 #define O(op, t, cf) [O##op]={#op, t, cf},
76 static char *kwmap
[Ntok
] = {
105 TMask
= 16383, /* for temps hash */
106 BMask
= 8191, /* for blocks hash */
108 K
= 3233235, /* found using tools/lexh.c */
112 static char lexh
[1 << (32-M
)];
126 static int tmph
[TMask
+1];
130 static Blk
*blkh
[BMask
+1];
141 fprintf(stderr
, "%s:%d: ", inpath
, lnum
);
142 vfprintf(stderr
, s
, ap
);
143 fprintf(stderr
, "\n");
157 for (i
=0; i
<NPubOp
; ++i
)
159 kwmap
[i
] = optab
[i
].name
;
160 assert(Ntok
<= CHAR_MAX
);
161 for (i
=0; i
<Ntok
; ++i
)
163 h
= hash(kwmap
[i
])*K
>> M
;
164 assert(lexh
[h
] == Txxx
);
182 n
= 10*n
+ (c
- '0');
184 } while ('0' <= c
&& c
<= '9');
188 return *(int64_t *)&n
;
194 static char tok
[NString
];
221 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
225 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
241 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
248 if (isdigit(c
) || c
== '-' || c
== '+') {
250 tokval
.num
= getint();
254 tokval
.str
= vnew(0, 1, Pfn
);
259 err("unterminated string");
260 vgrow(&tokval
.str
, i
+1);
261 if (c
== '"' && !esc
) {
265 esc
= (c
== '\\' && !esc
);
270 Alpha
: c
= fgetc(inf
);
271 if (!isalpha(c
) && c
!= '.' && c
!= '_')
272 err("invalid character %c (%d)", c
, c
);
276 err("identifier too long");
279 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
286 t
= lexh
[hash(tok
)*K
>> M
];
287 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
288 err("unknown keyword %s", tok
);
317 while ((t
= next()) == Tnl
)
325 static char *ttoa
[] = {
336 char buf
[128], *s1
, *s2
;
342 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
343 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
344 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
353 h
= &tmph
[hash(v
) & TMask
];
356 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
358 for (t
=curf
->ntmp
-1; t
>=Tmp0
; t
--)
359 if (strcmp(curf
->tmp
[t
].name
, v
) == 0)
365 strcpy(curf
->tmp
[t
].name
, v
);
375 memset(&c
, 0, sizeof c
);
378 return tmpref(tokval
.str
);
381 c
.bits
.i
= tokval
.num
;
385 c
.bits
.s
= tokval
.flts
;
390 c
.bits
.d
= tokval
.fltd
;
395 c
.label
= intern(tokval
.str
);
397 for (i
=0; i
<curf
->ncon
; i
++)
398 if (curf
->con
[i
].type
== c
.type
399 && curf
->con
[i
].bits
.i
== c
.bits
.i
400 && curf
->con
[i
].label
== c
.label
)
402 vgrow(&curf
->con
, ++curf
->ncon
);
414 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
416 err("undefined type :%s", tokval
.str
);
424 err("invalid class specifier");
426 *tyn
= findtyp(ntyp
);
442 int k
, ty
, env
, hasenv
;
447 while (peek() != Trparen
&& peek() != Tdots
) {
448 if (curi
- insb
>= NIns
)
449 err("too many instructions (1)");
450 env
= peek() == Tenv
;
458 err("invalid argument");
460 err("only one environment allowed");
461 if (!arg
&& rtype(r
) != RTmp
)
462 err("invalid function parameter");
465 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
467 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
470 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
472 *curi
= (Ins
){Opare
, k
, r
, {R
}};
475 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
477 *curi
= (Ins
){Opar
, k
, r
, {R
}};
480 if (peek() == Trparen
)
484 if (next() == Tdots
) {
497 h
= hash(name
) & BMask
;
498 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
499 if (strcmp(b
->name
, name
) == 0)
503 strcpy(b
->name
, name
);
512 curb
->nins
= curi
- insb
;
513 idup(&curb
->ins
, insb
, curb
->nins
);
521 Ref arg
[NPred
] = {R
};
529 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
530 err("label or } expected");
536 /* operations without result */
542 err("label, instruction or jump expected");
548 b
= findblk(tokval
.str
);
549 if (curb
&& curb
->jmp
.type
== Jxxx
) {
551 curb
->jmp
.type
= Jjmp
;
554 if (b
->jmp
.type
!= Jxxx
)
555 err("multiple definitions of block @%s", b
->name
);
562 curb
->jmp
.type
= (int[]){
568 curb
->jmp
.type
= Jret0
;
572 err("invalid return value");
577 curb
->jmp
.type
= Jjmp
;
580 curb
->jmp
.type
= Jjnz
;
583 err("invalid argument for jnz jump");
588 curb
->s1
= findblk(tokval
.str
);
589 if (curb
->jmp
.type
!= Jjmp
) {
592 curb
->s2
= findblk(tokval
.str
);
594 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
595 err("invalid jump to the start node");
601 r
= tmpref(tokval
.str
);
607 if (ps
!= PPhi
|| curb
== curf
->start
)
608 err("unexpected phi instruction");
625 if (op
>= Tloadw
&& op
<= Tloadd
)
627 if (op
== Talloc1
|| op
== Talloc2
)
630 err("size class must be w, l, s, or d");
632 err("invalid instruction");
637 err("too many arguments");
640 blk
[i
] = findblk(tokval
.str
);
644 err("invalid instruction argument");
650 err(", or end of line expected");
656 if (curi
- insb
>= NIns
)
657 err("too many instructions (2)");
661 curi
->arg
[0] = arg
[0];
662 curi
->arg
[1] = arg
[1];
666 phi
= alloc(sizeof *phi
);
669 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
670 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
679 usecheck(Ref r
, int k
, Fn
*fn
)
681 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
682 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
698 bsinit(pb
, fn
->nblk
);
699 bsinit(ppb
, fn
->nblk
);
700 for (b
=fn
->start
; b
; b
=b
->link
) {
701 for (p
=b
->phi
; p
; p
=p
->link
)
702 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
703 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
704 if (rtype(i
->to
) == RTmp
) {
705 t
= &fn
->tmp
[i
->to
.val
];
706 if (clsmerge(&t
->cls
, i
->cls
))
707 err("temporary %%%s is assigned with"
708 " multiple types", t
->name
);
711 for (b
=fn
->start
; b
; b
=b
->link
) {
713 for (n
=0; n
<b
->npred
; n
++)
714 bsset(pb
, b
->pred
[n
]->id
);
715 for (p
=b
->phi
; p
; p
=p
->link
) {
717 t
= &fn
->tmp
[p
->to
.val
];
718 for (n
=0; n
<p
->narg
; n
++) {
720 if (bshas(ppb
, p
->blk
[n
]->id
))
721 err("multiple entries for @%s in phi %%%s",
722 p
->blk
[n
]->name
, t
->name
);
723 if (!usecheck(p
->arg
[n
], k
, fn
))
724 err("invalid type for operand %%%s in phi %%%s",
725 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
726 bsset(ppb
, p
->blk
[n
]->id
);
728 if (!bsequal(pb
, ppb
))
729 err("predecessors not matched in phi %%%s", t
->name
);
731 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
732 for (n
=0; n
<2; n
++) {
733 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
737 err("invalid instruction type in %s",
739 if (rtype(r
) == RType
)
741 if (rtype(r
) != -1 && k
== Kx
)
742 err("no %s operand expected in %s",
743 n
== 1 ? "second" : "first",
745 if (rtype(r
) == -1 && k
!= Kx
)
746 err("missing %s operand in %s",
747 n
== 1 ? "second" : "first",
749 if (!usecheck(r
, k
, fn
))
750 err("invalid type for %s operand %%%s in %s",
751 n
== 1 ? "second" : "first",
752 t
->name
, optab
[i
->op
].name
);
755 if (isret(b
->jmp
.type
)) {
756 if (b
->jmp
.type
== Jretc
) {
757 if (!usecheck(r
, Kl
, fn
))
759 } else if (!usecheck(r
, b
->jmp
.type
-Jretw
, fn
))
762 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
764 err("invalid type for jump argument %%%s in block @%s",
765 fn
->tmp
[r
.val
].name
, b
->name
);
766 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
767 err("block @%s is used undefined", b
->s1
->name
);
768 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
769 err("block @%s is used undefined", b
->s2
->name
);
783 curf
= alloc(sizeof *curf
);
785 curf
->ncon
= 1; /* first constant must be 0 */
786 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], Pfn
);
787 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], Pfn
);
788 for (i
=0; i
<Tmp0
; ++i
)
789 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
793 curf
->con
[0].type
= CBits
;
794 curf
->export
= export
;
795 blink
= &curf
->start
;
798 rcls
= parsecls(&curf
->retty
);
802 err("function name expected");
803 strcpy(curf
->name
, tokval
.str
);
804 curf
->vararg
= parserefl(0);
805 if (nextnl() != Tlbrace
)
806 err("function body must start with {");
812 err("empty function");
813 if (curb
->jmp
.type
== Jxxx
)
814 err("last block misses jump");
815 curf
->mem
= vnew(0, sizeof curf
->mem
[0], Pfn
);
819 for (b
=0; b
; b
=b
->link
)
820 b
->dlink
= 0; /* was trashed by findblk() */
821 for (i
=0; i
<BMask
+1; ++i
)
823 memset(tmph
, 0, sizeof tmph
);
829 parsefields(Field
*fld
, Typ
*ty
, int t
)
832 int n
, c
, a
, al
, type
;
838 while (t
!= Trbrace
) {
841 default: err("invalid type member specifier");
842 case Td
: type
= Fd
; s
= 8; a
= 3; break;
843 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
844 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
845 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
846 case Th
: type
= Fh
; s
= 2; a
= 1; break;
847 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
850 ty1
= &typ
[findtyp(ntyp
-1)];
876 for (; c
>0 && n
<NField
; c
--, n
++) {
885 err(", or } expected");
890 ty
->size
= (sz
+ a
- 1) & -a
;
901 /* be careful if extending the syntax
902 * to handle nested types, any pointer
903 * held to typ[] might be invalidated!
910 if (nextnl() != Ttyp
|| nextnl() != Teq
)
911 err("type name and then = expected");
912 strcpy(ty
->name
, tokval
.str
);
915 if (nextnl() != Tint
)
916 err("alignment expected");
917 for (al
=0; tokval
.num
/= 2; al
++)
923 err("type body must start with {");
927 ty
->size
= tokval
.num
;
929 err("dark types need alignment");
930 if (nextnl() != Trbrace
)
935 ty
->fields
= vnew(1, sizeof ty
->fields
[0], Pheap
);
939 err("invalid union member");
940 vgrow(&ty
->fields
, n
+1);
941 parsefields(ty
->fields
[n
++], ty
, nextnl());
943 } while (t
!= Trbrace
);
945 parsefields(ty
->fields
[n
++], ty
, t
);
955 d
->u
.ref
.nam
= tokval
.str
;
961 err("invalid token after offset in ref");
962 d
->u
.ref
.off
= tokval
.num
;
970 d
->u
.str
= tokval
.str
;
974 parsedat(void cb(Dat
*), int export
)
985 if (nextnl() != Tglo
|| nextnl() != Teq
)
986 err("data name, then = expected");
987 strcpy(s
, tokval
.str
);
990 if (nextnl() != Tint
)
991 err("alignment expected");
993 d
.u
.num
= tokval
.num
;
1002 err("expected data contents in { .. }");
1005 default: err("invalid size specifier %c in data", tokval
.chr
);
1006 case Trbrace
: goto Done
;
1007 case Tl
: d
.type
= DL
; break;
1008 case Tw
: d
.type
= DW
; break;
1009 case Th
: d
.type
= DH
; break;
1010 case Tb
: d
.type
= DB
; break;
1011 case Ts
: d
.type
= DW
; break;
1012 case Td
: d
.type
= DL
; break;
1013 case Tz
: d
.type
= DZ
; break;
1019 memset(&d
.u
, 0, sizeof d
.u
);
1021 d
.u
.flts
= tokval
.flts
;
1022 else if (t
== Tfltd
)
1023 d
.u
.fltd
= tokval
.fltd
;
1025 d
.u
.num
= tokval
.num
;
1031 err("constant literal expected");
1034 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
);
1038 err(", or } expected");
1046 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1056 typ
= vnew(0, sizeof typ
[0], Pheap
);
1061 err("top-level definition expected");
1067 func(parsefn(export
));
1070 else if (t
== Tdata
) {
1072 parsedat(data
, export
);
1076 err("export can only qualify data and function");
1088 printcon(Con
*c
, FILE *f
)
1094 fprintf(f
, "$%s", str(c
->label
));
1096 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1100 fprintf(f
, "s_%f", c
->bits
.s
);
1101 else if (c
->flt
== 2)
1102 fprintf(f
, "d_%lf", c
->bits
.d
);
1104 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1110 printref(Ref r
, Fn
*fn
, FILE *f
)
1118 fprintf(f
, "R%d", r
.val
);
1120 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1123 printcon(&fn
->con
[r
.val
], f
);
1126 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1129 fprintf(f
, "%04x", r
.val
);
1132 fprintf(f
, ":%s", typ
[r
.val
].name
);
1136 m
= &fn
->mem
[r
.val
];
1138 if (m
->offset
.type
!= CUndef
) {
1139 printcon(&m
->offset
, f
);
1142 if (!req(m
->base
, R
)) {
1145 printref(m
->base
, fn
, f
);
1148 if (!req(m
->index
, R
)) {
1151 fprintf(f
, "%d * ", m
->scale
);
1152 printref(m
->index
, fn
, f
);
1160 printfn(Fn
*fn
, FILE *f
)
1162 static char ktoc
[] = "wlsd";
1163 static char *jtoa
[NJmp
] = {
1164 #define X(j) [J##j] = #j,
1174 fprintf(f
, "export ");
1175 fprintf(f
, "function $%s() {\n", fn
->name
);
1176 for (b
=fn
->start
; b
; b
=b
->link
) {
1177 fprintf(f
, "@%s\n", b
->name
);
1178 for (p
=b
->phi
; p
; p
=p
->link
) {
1180 printref(p
->to
, fn
, f
);
1181 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1184 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1185 printref(p
->arg
[n
], fn
, f
);
1186 if (n
== p
->narg
-1) {
1193 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1195 if (!req(i
->to
, R
)) {
1196 printref(i
->to
, fn
, f
);
1197 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1199 assert(optab
[i
->op
].name
);
1200 fprintf(f
, "%s", optab
[i
->op
].name
);
1212 fputc(ktoc
[i
->cls
], f
);
1214 if (!req(i
->arg
[0], R
)) {
1216 printref(i
->arg
[0], fn
, f
);
1218 if (!req(i
->arg
[1], R
)) {
1220 printref(i
->arg
[1], fn
, f
);
1224 switch (b
->jmp
.type
) {
1231 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1232 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1234 printref(b
->jmp
.arg
, fn
, f
);
1236 if (b
->jmp
.type
== Jretc
)
1237 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1241 if (b
->s1
!= b
->link
)
1242 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1245 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1246 if (b
->jmp
.type
== Jjnz
) {
1247 printref(b
->jmp
.arg
, fn
, f
);
1250 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);