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
, vararg
;
459 while (peek() != Trparen
) {
460 if (curi
- insb
>= NIns
)
461 err("too many instructions (1)");
463 err("no parameters allowed after '...'");
467 err("only one '...' allowed");
470 *curi
= (Ins
){.op
= Oargv
};
477 err("only one environment allowed");
490 err("invalid argument");
491 if (!arg
&& rtype(r
) != RTmp
)
492 err("invalid function parameter");
495 *curi
= (Ins
){Oargc
, Kl
, R
, {TYPE(ty
), r
}};
497 *curi
= (Ins
){Oparc
, Kl
, r
, {TYPE(ty
)}};
500 *curi
= (Ins
){Oarge
, k
, R
, {r
}};
502 *curi
= (Ins
){Opare
, k
, r
, {R
}};
505 *curi
= (Ins
){Oarg
, k
, R
, {r
}};
507 *curi
= (Ins
){Opar
, k
, r
, {R
}};
510 if (peek() == Trparen
)
524 h
= hash(name
) & BMask
;
525 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
526 if (strcmp(b
->name
, name
) == 0)
530 strcpy(b
->name
, name
);
539 curb
->nins
= curi
- insb
;
540 idup(&curb
->ins
, insb
, curb
->nins
);
548 Ref arg
[NPred
] = {R
};
556 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
557 err("label or } expected");
563 /* operations without result */
569 err("label, instruction or jump expected");
575 b
= findblk(tokval
.str
);
576 if (curb
&& curb
->jmp
.type
== Jxxx
) {
578 curb
->jmp
.type
= Jjmp
;
581 if (b
->jmp
.type
!= Jxxx
)
582 err("multiple definitions of block @%s", b
->name
);
589 curb
->jmp
.type
= (int[]){
595 curb
->jmp
.type
= Jret0
;
599 err("invalid return value");
604 curb
->jmp
.type
= Jjmp
;
607 curb
->jmp
.type
= Jjnz
;
610 err("invalid argument for jnz jump");
615 curb
->s1
= findblk(tokval
.str
);
616 if (curb
->jmp
.type
!= Jjmp
) {
619 curb
->s2
= findblk(tokval
.str
);
621 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
622 err("invalid jump to the start node");
628 r
= tmpref(tokval
.str
);
634 if (ps
!= PPhi
|| curb
== curf
->start
)
635 err("unexpected phi instruction");
652 if (op
>= Tloadl
&& op
<= Tloadd
)
654 if (op
== Talloc1
|| op
== Talloc2
)
657 err("size class must be w, l, s, or d");
659 err("invalid instruction");
664 err("too many arguments");
667 blk
[i
] = findblk(tokval
.str
);
671 err("invalid instruction argument");
677 err(", or end of line expected");
683 if (curi
- insb
>= NIns
)
684 err("too many instructions (2)");
688 curi
->arg
[0] = arg
[0];
689 curi
->arg
[1] = arg
[1];
693 phi
= alloc(sizeof *phi
);
696 phi
->arg
= vnew(i
, sizeof arg
[0], Pfn
);
697 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
698 phi
->blk
= vnew(i
, sizeof blk
[0], Pfn
);
699 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
708 usecheck(Ref r
, int k
, Fn
*fn
)
710 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
711 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
727 bsinit(pb
, fn
->nblk
);
728 bsinit(ppb
, fn
->nblk
);
729 for (b
=fn
->start
; b
; b
=b
->link
) {
730 for (p
=b
->phi
; p
; p
=p
->link
)
731 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
732 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
733 if (rtype(i
->to
) == RTmp
) {
734 t
= &fn
->tmp
[i
->to
.val
];
735 if (clsmerge(&t
->cls
, i
->cls
))
736 err("temporary %%%s is assigned with"
737 " multiple types", t
->name
);
740 for (b
=fn
->start
; b
; b
=b
->link
) {
742 for (n
=0; n
<b
->npred
; n
++)
743 bsset(pb
, b
->pred
[n
]->id
);
744 for (p
=b
->phi
; p
; p
=p
->link
) {
746 t
= &fn
->tmp
[p
->to
.val
];
747 for (n
=0; n
<p
->narg
; n
++) {
749 if (bshas(ppb
, p
->blk
[n
]->id
))
750 err("multiple entries for @%s in phi %%%s",
751 p
->blk
[n
]->name
, t
->name
);
752 if (!usecheck(p
->arg
[n
], k
, fn
))
753 err("invalid type for operand %%%s in phi %%%s",
754 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
755 bsset(ppb
, p
->blk
[n
]->id
);
757 if (!bsequal(pb
, ppb
))
758 err("predecessors not matched in phi %%%s", t
->name
);
760 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++)
761 for (n
=0; n
<2; n
++) {
762 k
= optab
[i
->op
].argcls
[n
][i
->cls
];
766 err("invalid instruction type in %s",
768 if (rtype(r
) == RType
)
770 if (rtype(r
) != -1 && k
== Kx
)
771 err("no %s operand expected in %s",
772 n
== 1 ? "second" : "first",
774 if (rtype(r
) == -1 && k
!= Kx
)
775 err("missing %s operand in %s",
776 n
== 1 ? "second" : "first",
778 if (!usecheck(r
, k
, fn
))
779 err("invalid type for %s operand %%%s in %s",
780 n
== 1 ? "second" : "first",
781 t
->name
, optab
[i
->op
].name
);
784 if (isret(b
->jmp
.type
)) {
785 if (b
->jmp
.type
== Jretc
) {
786 if (!usecheck(r
, Kl
, fn
))
788 } else if (!usecheck(r
, b
->jmp
.type
-Jretw
, fn
))
791 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
793 err("invalid type for jump argument %%%s in block @%s",
794 fn
->tmp
[r
.val
].name
, b
->name
);
795 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
796 err("block @%s is used undefined", b
->s1
->name
);
797 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
798 err("block @%s is used undefined", b
->s2
->name
);
812 curf
= alloc(sizeof *curf
);
814 curf
->ncon
= 1; /* first constant must be 0 */
815 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], Pfn
);
816 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], Pfn
);
817 for (i
=0; i
<Tmp0
; ++i
)
818 if (T
.fpr0
<= i
&& i
< T
.fpr0
+ T
.nfpr
)
822 curf
->con
[0].type
= CBits
;
823 curf
->export
= export
;
824 blink
= &curf
->start
;
827 rcls
= parsecls(&curf
->retty
);
831 err("function name expected");
832 strncpy(curf
->name
, tokval
.str
, NString
-1);
833 curf
->vararg
= parserefl(0);
834 if (nextnl() != Tlbrace
)
835 err("function body must start with {");
841 err("empty function");
842 if (curb
->jmp
.type
== Jxxx
)
843 err("last block misses jump");
844 curf
->mem
= vnew(0, sizeof curf
->mem
[0], Pfn
);
848 for (b
=0; b
; b
=b
->link
)
849 b
->dlink
= 0; /* was trashed by findblk() */
850 for (i
=0; i
<BMask
+1; ++i
)
852 memset(tmph
, 0, sizeof tmph
);
858 parsefields(Field
*fld
, Typ
*ty
, int t
)
861 int n
, c
, a
, al
, type
;
867 while (t
!= Trbrace
) {
870 default: err("invalid type member specifier");
871 case Td
: type
= Fd
; s
= 8; a
= 3; break;
872 case Tl
: type
= Fl
; s
= 8; a
= 3; break;
873 case Ts
: type
= Fs
; s
= 4; a
= 2; break;
874 case Tw
: type
= Fw
; s
= 4; a
= 2; break;
875 case Th
: type
= Fh
; s
= 2; a
= 1; break;
876 case Tb
: type
= Fb
; s
= 1; a
= 0; break;
879 ty1
= &typ
[findtyp(ntyp
-1)];
887 a
= ((sz
+ a
) & ~a
) - sz
;
905 for (; c
>0 && n
<NField
; c
--, n
++) {
914 err(", or } expected");
919 ty
->size
= (sz
+ a
- 1) & -a
;
930 /* be careful if extending the syntax
931 * to handle nested types, any pointer
932 * held to typ[] might be invalidated!
939 if (nextnl() != Ttyp
|| nextnl() != Teq
)
940 err("type name and then = expected");
941 strcpy(ty
->name
, tokval
.str
);
944 if (nextnl() != Tint
)
945 err("alignment expected");
946 for (al
=0; tokval
.num
/= 2; al
++)
952 err("type body must start with {");
956 ty
->size
= tokval
.num
;
958 err("dark types need alignment");
959 if (nextnl() != Trbrace
)
964 ty
->fields
= vnew(1, sizeof ty
->fields
[0], Pheap
);
968 err("invalid union member");
969 vgrow(&ty
->fields
, n
+1);
970 parsefields(ty
->fields
[n
++], ty
, nextnl());
972 } while (t
!= Trbrace
);
974 parsefields(ty
->fields
[n
++], ty
, t
);
984 d
->u
.ref
.nam
= tokval
.str
;
990 err("invalid token after offset in ref");
991 d
->u
.ref
.off
= tokval
.num
;
999 d
->u
.str
= tokval
.str
;
1003 parsedat(void cb(Dat
*), int export
)
1005 char name
[NString
] = {0};
1009 if (nextnl() != Tglo
|| nextnl() != Teq
)
1010 err("data name, then = expected");
1011 strncpy(name
, tokval
.str
, NString
-1);
1014 if (t
== Tsection
) {
1015 if (nextnl() != Tstr
)
1016 err("section \"name\" expected");
1017 d
.u
.str
= tokval
.str
;
1023 if (nextnl() != Tint
)
1024 err("alignment expected");
1026 d
.u
.num
= tokval
.num
;
1038 err("expected data contents in { .. }");
1041 default: err("invalid size specifier %c in data", tokval
.chr
);
1042 case Trbrace
: goto Done
;
1043 case Tl
: d
.type
= DL
; break;
1044 case Tw
: d
.type
= DW
; break;
1045 case Th
: d
.type
= DH
; break;
1046 case Tb
: d
.type
= DB
; break;
1047 case Ts
: d
.type
= DW
; break;
1048 case Td
: d
.type
= DL
; break;
1049 case Tz
: d
.type
= DZ
; break;
1055 memset(&d
.u
, 0, sizeof d
.u
);
1057 d
.u
.flts
= tokval
.flts
;
1058 else if (t
== Tfltd
)
1059 d
.u
.fltd
= tokval
.fltd
;
1061 d
.u
.num
= tokval
.num
;
1067 err("constant literal expected");
1070 } while (t
== Tint
|| t
== Tflts
|| t
== Tfltd
);
1074 err(", or } expected");
1082 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1092 typ
= vnew(0, sizeof typ
[0], Pheap
);
1097 err("top-level definition expected");
1103 func(parsefn(export
));
1106 else if (t
== Tdata
) {
1108 parsedat(data
, export
);
1112 err("export can only qualify data and function");
1124 printcon(Con
*c
, FILE *f
)
1130 fprintf(f
, "$%s", str(c
->label
));
1132 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1136 fprintf(f
, "s_%f", c
->bits
.s
);
1137 else if (c
->flt
== 2)
1138 fprintf(f
, "d_%lf", c
->bits
.d
);
1140 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1146 printref(Ref r
, Fn
*fn
, FILE *f
)
1154 fprintf(f
, "R%d", r
.val
);
1156 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1159 printcon(&fn
->con
[r
.val
], f
);
1162 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1165 fprintf(f
, "%04x", r
.val
);
1168 fprintf(f
, ":%s", typ
[r
.val
].name
);
1172 m
= &fn
->mem
[r
.val
];
1174 if (m
->offset
.type
!= CUndef
) {
1175 printcon(&m
->offset
, f
);
1178 if (!req(m
->base
, R
)) {
1181 printref(m
->base
, fn
, f
);
1184 if (!req(m
->index
, R
)) {
1187 fprintf(f
, "%d * ", m
->scale
);
1188 printref(m
->index
, fn
, f
);
1196 printfn(Fn
*fn
, FILE *f
)
1198 static char ktoc
[] = "wlsd";
1199 static char *jtoa
[NJmp
] = {
1200 #define X(j) [J##j] = #j,
1210 fprintf(f
, "export ");
1211 fprintf(f
, "function $%s() {\n", fn
->name
);
1212 for (b
=fn
->start
; b
; b
=b
->link
) {
1213 fprintf(f
, "@%s\n", b
->name
);
1214 for (p
=b
->phi
; p
; p
=p
->link
) {
1216 printref(p
->to
, fn
, f
);
1217 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1220 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1221 printref(p
->arg
[n
], fn
, f
);
1222 if (n
== p
->narg
-1) {
1229 for (i
=b
->ins
; i
<&b
->ins
[b
->nins
]; i
++) {
1231 if (!req(i
->to
, R
)) {
1232 printref(i
->to
, fn
, f
);
1233 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1235 assert(optab
[i
->op
].name
);
1236 fprintf(f
, "%s", optab
[i
->op
].name
);
1248 fputc(ktoc
[i
->cls
], f
);
1250 if (!req(i
->arg
[0], R
)) {
1252 printref(i
->arg
[0], fn
, f
);
1254 if (!req(i
->arg
[1], R
)) {
1256 printref(i
->arg
[1], fn
, f
);
1260 switch (b
->jmp
.type
) {
1267 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1268 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1270 printref(b
->jmp
.arg
, fn
, f
);
1272 if (b
->jmp
.type
== Jretc
)
1273 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1277 if (b
->s1
!= b
->link
)
1278 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1281 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1282 if (b
->jmp
.type
== Jjnz
) {
1283 printref(b
->jmp
.arg
, fn
, f
);
1286 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);