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
= 5041217, /* found using tools/lexh.c */
116 static uchar 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
<= UCHAR_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
);
385 memset(&c
, 0, sizeof c
);
388 return tmpref(tokval
.str
);
391 c
.bits
.i
= tokval
.num
;
395 c
.bits
.s
= tokval
.flts
;
400 c
.bits
.d
= tokval
.fltd
;
405 c
.label
= intern(tokval
.str
);
407 return newcon(&c
, curf
);
417 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
419 err("undefined type :%s", tokval
.str
);
427 err("invalid class specifier");
429 *tyn
= findtyp(ntyp
);
445 int k
, ty
, env
, hasenv
, vararg
;
451 while (peek() != Trparen
) {
452 if (curi
- insb
>= NIns
)
453 err("too many instructions (1)");
455 err("no parameters allowed after '...'");
459 err("only one '...' allowed");
462 *curi
= (Ins
){.op
= Oargv
};
469 err("only one environment allowed");
482 err("invalid argument");
483 if (!arg
&& rtype(r
) != RTmp
)
484 err("invalid function parameter");
487 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
489 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
492 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
494 *curi
= (Ins
){Opare
, k
, r
, {R
}};
497 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
499 *curi
= (Ins
){Opar
, k
, r
, {R
}};
502 if (peek() == Trparen
)
516 h
= hash(name
) & BMask
;
517 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
518 if (strcmp(b
->name
, name
) == 0)
522 strcpy(b
->name
, name
);
531 curb
->nins
= curi
- insb
;
532 idup(&curb
->ins
, insb
, curb
->nins
);
540 Ref arg
[NPred
] = {R
};
548 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
549 err("label or } expected");
555 /* operations without result */
561 err("label, instruction or jump expected");
567 b
= findblk(tokval
.str
);
568 if (curb
&& curb
->jmp
.type
== Jxxx
) {
570 curb
->jmp
.type
= Jjmp
;
573 if (b
->jmp
.type
!= Jxxx
)
574 err("multiple definitions of block @%s", b
->name
);
581 curb
->jmp
.type
= (int[]){
587 curb
->jmp
.type
= Jret0
;
591 err("invalid return value");
596 curb
->jmp
.type
= Jjmp
;
599 curb
->jmp
.type
= Jjnz
;
602 err("invalid argument for jnz jump");
607 curb
->s1
= findblk(tokval
.str
);
608 if (curb
->jmp
.type
!= Jjmp
) {
611 curb
->s2
= findblk(tokval
.str
);
613 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
614 err("invalid jump to the start node");
620 r
= tmpref(tokval
.str
);
626 if (ps
!= PPhi
|| curb
== curf
->start
)
627 err("unexpected phi instruction");
644 if (op
>= Tloadl
&& op
<= Tloadd
)
646 if (op
== Talloc1
|| op
== Talloc2
)
649 err("size class must be w, l, s, or d");
651 err("invalid instruction");
656 err("too many arguments");
659 blk
[i
] = findblk(tokval
.str
);
663 err("invalid instruction argument");
669 err(", or end of line expected");
675 if (curi
- insb
>= NIns
)
676 err("too many instructions (2)");
680 curi
->arg
[0] = arg
[0];
681 curi
->arg
[1] = arg
[1];
685 phi
= alloc(sizeof *phi
);
688 phi
->arg
= vnew(i
, sizeof arg
[0], Pfn
);
689 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
690 phi
->blk
= vnew(i
, sizeof blk
[0], Pfn
);
691 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
700 usecheck(Ref r
, int k
, Fn
*fn
)
702 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
703 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
719 bsinit(pb
, fn
->nblk
);
720 bsinit(ppb
, fn
->nblk
);
721 for (b
=fn
->start
; b
; b
=b
->link
) {
722 for (p
=b
->phi
; p
; p
=p
->link
)
723 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
724 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
725 if (rtype(i
->to
) == RTmp
) {
726 t
= &fn
->tmp
[i
->to
.val
];
727 if (clsmerge(&t
->cls
, i
->cls
))
728 err("temporary %%%s is assigned with"
729 " multiple types", t
->name
);
732 for (b
=fn
->start
; b
; b
=b
->link
) {
734 for (n
=0; n
<b
->npred
; n
++)
735 bsset(pb
, b
->pred
[n
]->id
);
736 for (p
=b
->phi
; p
; p
=p
->link
) {
738 t
= &fn
->tmp
[p
->to
.val
];
739 for (n
=0; n
<p
->narg
; n
++) {
741 if (bshas(ppb
, p
->blk
[n
]->id
))
742 err("multiple entries for @%s in phi %%%s",
743 p
->blk
[n
]->name
, t
->name
);
744 if (!usecheck(p
->arg
[n
], k
, fn
))
745 err("invalid type for operand %%%s in phi %%%s",
746 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
747 bsset(ppb
, p
->blk
[n
]->id
);
749 if (!bsequal(pb
, ppb
))
750 err("predecessors not matched in phi %%%s", t
->name
);
752 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
753 for (n
=0; n
<2; n
++) {
754 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
758 err("invalid instruction type in %s",
760 if (rtype(r
) == RType
)
762 if (rtype(r
) != -1 && k
== Kx
)
763 err("no %s operand expected in %s",
764 n
== 1 ? "second" : "first",
766 if (rtype(r
) == -1 && k
!= Kx
)
767 err("missing %s operand in %s",
768 n
== 1 ? "second" : "first",
770 if (!usecheck(r
, k
, fn
))
771 err("invalid type for %s operand %%%s in %s",
772 n
== 1 ? "second" : "first",
773 t
->name
, optab
[i
->op
].name
);
776 if (isret(b
->jmp
.type
)) {
777 if (b
->jmp
.type
== Jretc
) {
778 if (!usecheck(r
, Kl
, fn
))
780 } else if (!usecheck(r
, b
->jmp
.type
-Jretw
, fn
))
783 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
785 err("invalid type for jump argument %%%s in block @%s",
786 fn
->tmp
[r
.val
].name
, b
->name
);
787 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
788 err("block @%s is used undefined", b
->s1
->name
);
789 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
790 err("block @%s is used undefined", b
->s2
->name
);
804 curf
= alloc(sizeof *curf
);
806 curf
->ncon
= 1; /* first constant must be 0 */
807 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], Pfn
);
808 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], Pfn
);
809 for (i
=0; i
<Tmp0
; ++i
)
810 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
814 curf
->con
[0].type
= CBits
;
816 blink
= &curf
->start
;
819 rcls
= parsecls(&curf
->retty
);
823 err("function name expected");
824 strncpy(curf
->name
, tokval
.str
, NString
-1);
825 curf
->vararg
= parserefl(0);
826 if (nextnl() != Tlbrace
)
827 err("function body must start with {");
833 err("empty function");
834 if (curb
->jmp
.type
== Jxxx
)
835 err("last block misses jump");
836 curf
->mem
= vnew(0, sizeof curf
->mem
[0], Pfn
);
840 for (b
=0; b
; b
=b
->link
)
841 b
->dlink
= 0; /* was trashed by findblk() */
842 for (i
=0; i
<BMask
+1; ++i
)
844 memset(tmph
, 0, sizeof tmph
);
850 parsefields(Field
*fld
, Typ
*ty
, int t
)
853 int n
, c
, a
, al
, type
;
859 while (t
!= Trbrace
) {
862 default: err("invalid type member specifier");
863 case Td
: type
= Fd
; s
= 8; a
= 3; break;
864 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
865 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
866 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
867 case Th
: type
= Fh
; s
= 2; a
= 1; break;
868 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
871 ty1
= &typ
[findtyp(ntyp
-1)];
879 a
= ((sz
+ a
) & ~a
) - sz
;
897 for (; c
>0 && n
<NField
; c
--, n
++) {
906 err(", or } expected");
911 ty
->size
= (sz
+ a
- 1) & -a
;
922 /* be careful if extending the syntax
923 * to handle nested types, any pointer
924 * held to typ[] might be invalidated!
932 if (nextnl() != Ttyp
|| nextnl() != Teq
)
933 err("type name and then = expected");
934 strcpy(ty
->name
, tokval
.str
);
937 if (nextnl() != Tint
)
938 err("alignment expected");
939 for (al
=0; tokval
.num
/= 2; al
++)
945 err("type body must start with {");
949 ty
->size
= tokval
.num
;
951 err("dark types need alignment");
952 if (nextnl() != Trbrace
)
957 ty
->fields
= vnew(1, sizeof ty
->fields
[0], Pheap
);
962 err("invalid union member");
963 vgrow(&ty
->fields
, n
+1);
964 parsefields(ty
->fields
[n
++], ty
, nextnl());
966 } while (t
!= Trbrace
);
968 parsefields(ty
->fields
[n
++], ty
, t
);
978 d
->u
.ref
.name
= tokval
.str
;
984 err("invalid token after offset in ref");
985 d
->u
.ref
.off
= tokval
.num
;
993 d
->u
.str
= tokval
.str
;
997 parsedat(void cb(Dat
*), Lnk
*lnk
)
999 char name
[NString
] = {0};
1003 if (nextnl() != Tglo
|| nextnl() != Teq
)
1004 err("data name, then = expected");
1005 strncpy(name
, tokval
.str
, NString
-1);
1009 if (nextnl() != Tint
)
1010 err("alignment expected");
1011 lnk
->align
= tokval
.num
;
1020 err("expected data contents in { .. }");
1023 default: err("invalid size specifier %c in data", tokval
.chr
);
1024 case Trbrace
: goto Done
;
1025 case Tl
: d
.type
= DL
; break;
1026 case Tw
: d
.type
= DW
; break;
1027 case Th
: d
.type
= DH
; break;
1028 case Tb
: d
.type
= DB
; break;
1029 case Ts
: d
.type
= DW
; break;
1030 case Td
: d
.type
= DL
; break;
1031 case Tz
: d
.type
= DZ
; break;
1037 memset(&d
.u
, 0, sizeof d
.u
);
1039 d
.u
.flts
= tokval
.flts
;
1040 else if (t
== Tfltd
)
1041 d
.u
.fltd
= tokval
.fltd
;
1043 d
.u
.num
= tokval
.num
;
1049 err("constant literal expected");
1052 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
|| t
== Tstr
);
1056 err(", or } expected");
1068 for (haslnk
=0;; haslnk
=1)
1069 switch ((t
=nextnl())) {
1075 err("section \"name\" expected");
1076 lnk
->sec
= tokval
.str
;
1077 if (peek() == Tstr
) {
1079 lnk
->secf
= tokval
.str
;
1086 err("only data and function have linkage");
1092 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1103 typ
= vnew(0, sizeof typ
[0], Pheap
);
1106 switch (parselnk(&lnk
)) {
1108 err("top-level definition expected");
1110 func(parsefn(&lnk
));
1113 parsedat(data
, &lnk
);
1119 for (n
=0; n
<ntyp
; n
++)
1121 vfree(typ
[n
].fields
);
1129 printcon(Con
*c
, FILE *f
)
1135 fprintf(f
, "$%s", str(c
->label
));
1137 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1141 fprintf(f
, "s_%f", c
->bits
.s
);
1142 else if (c
->flt
== 2)
1143 fprintf(f
, "d_%lf", c
->bits
.d
);
1145 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1151 printref(Ref r
, Fn
*fn
, FILE *f
)
1159 fprintf(f
, "R%d", r
.val
);
1161 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1164 printcon(&fn
->con
[r
.val
], f
);
1167 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1170 fprintf(f
, "%04x", r
.val
);
1173 fprintf(f
, ":%s", typ
[r
.val
].name
);
1177 m
= &fn
->mem
[r
.val
];
1179 if (m
->offset
.type
!= CUndef
) {
1180 printcon(&m
->offset
, f
);
1183 if (!req(m
->base
, R
)) {
1186 printref(m
->base
, fn
, f
);
1189 if (!req(m
->index
, R
)) {
1192 fprintf(f
, "%d * ", m
->scale
);
1193 printref(m
->index
, fn
, f
);
1201 printfn(Fn
*fn
, FILE *f
)
1203 static char ktoc
[] = "wlsd";
1204 static char *jtoa
[NJmp
] = {
1205 #define X(j) [J##j] = #j,
1215 fprintf(f
, "export ");
1216 fprintf(f
, "function $%s() {\n", fn
->name
);
1217 for (b
=fn
->start
; b
; b
=b
->link
) {
1218 fprintf(f
, "@%s\n", b
->name
);
1219 for (p
=b
->phi
; p
; p
=p
->link
) {
1221 printref(p
->to
, fn
, f
);
1222 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1225 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1226 printref(p
->arg
[n
], fn
, f
);
1227 if (n
== p
->narg
-1) {
1234 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1236 if (!req(i
->to
, R
)) {
1237 printref(i
->to
, fn
, f
);
1238 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1240 assert(optab
[i
->op
].name
);
1241 fprintf(f
, "%s", optab
[i
->op
].name
);
1253 fputc(ktoc
[i
->cls
], f
);
1255 if (!req(i
->arg
[0], R
)) {
1257 printref(i
->arg
[0], fn
, f
);
1259 if (!req(i
->arg
[1], R
)) {
1261 printref(i
->arg
[1], fn
, f
);
1265 switch (b
->jmp
.type
) {
1272 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1273 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1275 printref(b
->jmp
.arg
, fn
, f
);
1277 if (b
->jmp
.type
== Jretc
)
1278 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1282 if (b
->s1
!= b
->link
)
1283 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1286 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1287 if (b
->jmp
.type
== Jjnz
) {
1288 printref(b
->jmp
.arg
, fn
, f
);
1291 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);