6 Ke
= -2, /* Erroneous mode */
7 Km
= Kl
, /* Memory pointer (for x64) */
10 OpDesc opdesc
[NOp
] = {
11 #define A(a,b,c,d) {[Kw]=K##a, [Kl]=K##b, [Ks]=K##c, [Kd]=K##d}
13 /* NAME NM ARGCLS0 ARGCLS1 SF LF FLD*/
14 [Oadd
] = { "add", 2, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 1, 0, 1 },
15 [Osub
] = { "sub", 2, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 1, 0, 1 },
16 [Odiv
] = { "div", 2, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 0, 0, 1 },
17 [Orem
] = { "rem", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 0, 0, 1 },
18 [Oudiv
] = { "udiv", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 0, 0, 1 },
19 [Ourem
] = { "urem", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 0, 0, 1 },
20 [Omul
] = { "mul", 2, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 0, 0, 1 },
21 [Oand
] = { "and", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 1, 0, 1 },
22 [Oor
] = { "or", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 1, 0, 1 },
23 [Oxor
] = { "xor", 2, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 1, 0, 1 },
24 [Osar
] = { "sar", 1, {A(w
,l
,e
,e
), A(w
,w
,e
,e
)}, 1, 0, 1 },
25 [Oshr
] = { "shr", 1, {A(w
,l
,e
,e
), A(w
,w
,e
,e
)}, 1, 0, 1 },
26 [Oshl
] = { "shl", 1, {A(w
,l
,e
,e
), A(w
,w
,e
,e
)}, 1, 0, 1 },
27 [Ostored
] = { "stored", 0, {A(d
,d
,d
,d
), A(m
,m
,m
,m
)}, 0, 1, 0 },
28 [Ostores
] = { "stores", 0, {A(s
,s
,s
,s
), A(m
,m
,m
,m
)}, 0, 1, 0 },
29 [Ostorel
] = { "storel", 0, {A(l
,l
,l
,l
), A(m
,m
,m
,m
)}, 0, 1, 0 },
30 [Ostorew
] = { "storew", 0, {A(w
,w
,w
,w
), A(m
,m
,m
,m
)}, 0, 1, 0 },
31 [Ostoreh
] = { "storeh", 0, {A(w
,w
,w
,w
), A(m
,m
,m
,m
)}, 0, 1, 0 },
32 [Ostoreb
] = { "storeb", 0, {A(w
,w
,w
,w
), A(m
,m
,m
,m
)}, 0, 1, 0 },
33 [Oload
] = { "load", 0, {A(m
,m
,m
,m
), A(x
,x
,x
,x
)}, 0, 1, 0 },
34 [Oloadsw
] = { "loadsw", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
35 [Oloaduw
] = { "loaduw", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
36 [Oloadsh
] = { "loadsh", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
37 [Oloaduh
] = { "loaduh", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
38 [Oloadsb
] = { "loadsb", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
39 [Oloadub
] = { "loadub", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
40 [Oextsw
] = { "extsw", 0, {A(e
,w
,e
,e
), A(e
,x
,e
,e
)}, 0, 1, 1 },
41 [Oextuw
] = { "extuw", 0, {A(e
,w
,e
,e
), A(e
,x
,e
,e
)}, 0, 1, 1 },
42 [Oextsh
] = { "extsh", 0, {A(w
,w
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
43 [Oextuh
] = { "extuh", 0, {A(w
,w
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
44 [Oextsb
] = { "extsb", 0, {A(w
,w
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
45 [Oextub
] = { "extub", 0, {A(w
,w
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
46 [Oexts
] = { "exts", 0, {A(e
,e
,e
,s
), A(e
,e
,e
,x
)}, 0, 1, 1 },
47 [Otruncd
] = { "truncd", 0, {A(e
,e
,d
,e
), A(e
,e
,x
,e
)}, 0, 1, 1 },
48 [Ostosi
] = { "stosi", 0, {A(s
,s
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
49 [Odtosi
] = { "dtosi", 0, {A(d
,d
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 1 },
50 [Oswtof
] = { "swtof", 0, {A(e
,e
,w
,w
), A(e
,e
,x
,x
)}, 0, 1, 1 },
51 [Osltof
] = { "sltof", 0, {A(e
,e
,l
,l
), A(e
,e
,x
,x
)}, 0, 1, 1 },
52 [Ocast
] = { "cast", 0, {A(s
,d
,w
,l
), A(x
,x
,x
,x
)}, 0, 1, 1 },
53 [Ocopy
] = { "copy", 1, {A(w
,l
,s
,d
), A(x
,x
,x
,x
)}, 0, 1, 0 },
54 [Onop
] = { "nop", 0, {A(x
,x
,x
,x
), A(x
,x
,x
,x
)}, 0, 1, 0 },
55 [Oswap
] = { "swap", 2, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 0, 0, 0 },
56 [Osign
] = { "sign", 0, {A(w
,l
,e
,e
), A(x
,x
,e
,e
)}, 0, 0, 0 },
57 [Osalloc
] = { "salloc", 0, {A(e
,l
,e
,e
), A(e
,x
,e
,e
)}, 0, 0, 0 },
58 [Oxidiv
] = { "xidiv", 1, {A(w
,l
,e
,e
), A(x
,x
,e
,e
)}, 0, 0, 0 },
59 [Oxdiv
] = { "xdiv", 1, {A(w
,l
,e
,e
), A(x
,x
,e
,e
)}, 0, 0, 0 },
60 [Oxcmp
] = { "xcmp", 1, {A(w
,l
,s
,d
), A(w
,l
,s
,d
)}, 1, 0, 0 },
61 [Oxtest
] = { "xtest", 1, {A(w
,l
,e
,e
), A(w
,l
,e
,e
)}, 1, 0, 0 },
62 [Oaddr
] = { "addr", 0, {A(m
,m
,e
,e
), A(x
,x
,e
,e
)}, 0, 1, 0 },
63 [Opar
] = { "parn", 0, {A(x
,x
,x
,x
), A(x
,x
,x
,x
)}, 0, 0, 0 },
64 [Oparc
] = { "parc", 0, {A(e
,x
,e
,e
), A(e
,x
,e
,e
)}, 0, 0, 0 },
65 [Oarg
] = { "arg", 0, {A(w
,l
,s
,d
), A(x
,x
,x
,x
)}, 0, 0, 0 },
66 [Oargc
] = { "argc", 0, {A(e
,x
,e
,e
), A(e
,l
,e
,e
)}, 0, 0, 0 },
67 [Ocall
] = { "call", 0, {A(m
,m
,m
,m
), A(x
,x
,x
,x
)}, 0, 0, 0 },
68 [Oxsetnp
] = { "xsetnp", 0, {A(x
,x
,e
,e
), A(x
,x
,e
,e
)}, 0, 0, 0 },
69 [Oxsetp
] = { "xsetp", 0, {A(x
,x
,e
,e
), A(x
,x
,e
,e
)}, 0, 0, 0 },
70 [Oalloc
] = { "alloc4", 1, {A(e
,l
,e
,e
), A(e
,x
,e
,e
)}, 0, 0, 0 },
71 [Oalloc
+1] = { "alloc8", 1, {A(e
,l
,e
,e
), A(e
,x
,e
,e
)}, 0, 0, 0 },
72 [Oalloc
+2] = { "alloc16", 1, {A(e
,l
,e
,e
), A(e
,x
,e
,e
)}, 0, 0, 0 },
74 [Ocmpw+IC##c] = { "c" #c "w", 0, {A(w,w,e,e), A(w,w,e,e)}, 1, 0, 1 }, \
75 [Ocmpl+IC##c] = { "c" #c "l", 0, {A(l,l,e,e), A(l,l,e,e)}, 1, 0, 1 }, \
76 [Oxset+IC##c] = { "xset" #c, 0, {A(x,x,e,e), A(x,x,e,e)}, 0, 1, 0 },
80 [Ocmps+FC##c] = { "c" #c "s", 0, {A(s,s,e,e), A(s,s,e,e)}, 1, 0, 1 }, \
81 [Ocmpd+FC##c] = { "c" #c "d", 0, {A(d,d,e,e), A(d,d,e,e)}, 1, 0, 1 },
147 static char *kwmap
[Ntok
] = {
152 [Talloc1
] = "alloc1",
153 [Talloc2
] = "alloc2",
159 [Texport
] = "export",
160 [Tfunc
] = "function",
174 BMask
= 8191, /* for blocks hash */
176 K
= 2047061843, /* found using tools/lexh.c */
180 static char lexh
[1 << (32-M
)];
197 static Blk
*blkh
[BMask
+1];
208 fprintf(stderr
, "%s:%d: ", inpath
, lnum
);
209 vfprintf(stderr
, s
, ap
);
210 fprintf(stderr
, "\n");
215 static inline uint32_t
235 for (i
=0; i
<NPubOp
; ++i
)
237 kwmap
[i
] = opdesc
[i
].name
;
238 assert(Ntok
<= CHAR_MAX
);
239 for (i
=0; i
<Ntok
; ++i
)
241 h
= hash(kwmap
[i
])*K
>> M
;
242 assert(lexh
[h
] == Txxx
);
251 static char tok
[NString
];
278 if (fscanf(inf
, "_%f", &tokval
.flts
) != 1)
282 if (fscanf(inf
, "_%lf", &tokval
.fltd
) != 1)
298 while ((c
=fgetc(inf
)) != '\n' && c
!= EOF
)
304 if (isdigit(c
) || c
== '-' || c
== '+') {
306 if (fscanf(inf
, "%"SCNd64
, &tokval
.num
) != 1)
307 err("invalid integer literal");
311 tokval
.str
= vnew(0, 1, alloc
);
316 err("unterminated string");
317 vgrow(&tokval
.str
, i
+1);
318 if (c
== '"' && !esc
) {
322 esc
= (c
== '\\' && !esc
);
327 Alpha
: c
= fgetc(inf
);
328 if (!isalpha(c
) && c
!= '.' && c
!= '_')
329 err("lexing failure: invalid character %c (%d)", c
, c
);
333 err("identifier too long");
336 } while (isalpha(c
) || c
== '$' || c
== '.' || c
== '_' || isdigit(c
));
343 t
= lexh
[hash(tok
)*K
>> M
];
344 if (t
== Txxx
|| strcmp(kwmap
[t
], tok
) != 0) {
345 err("unknown keyword %s", tok
);
374 while ((t
= next()) == Tnl
)
382 static char *ttoa
[] = {
393 char buf
[128], *s1
, *s2
;
399 s1
= ttoa
[t
] ? ttoa
[t
] : "??";
400 s2
= ttoa
[t1
] ? ttoa
[t1
] : "??";
401 sprintf(buf
, "%s expected, got %s instead", s1
, s2
);
410 for (t
=Tmp0
; t
<curf
->ntmp
; t
++)
411 if (strcmp(v
, curf
->tmp
[t
].name
) == 0)
414 strcpy(curf
->tmp
[t
].name
, v
);
424 memset(&c
, 0, sizeof c
);
427 return tmpref(tokval
.str
);
430 c
.bits
.i
= tokval
.num
;
434 c
.bits
.s
= tokval
.flts
;
439 c
.bits
.d
= tokval
.fltd
;
444 strcpy(c
.label
, tokval
.str
);
446 for (i
=0; i
<curf
->ncon
; i
++)
447 if (curf
->con
[i
].type
== c
.type
448 && curf
->con
[i
].bits
.i
== c
.bits
.i
449 && strcmp(curf
->con
[i
].label
, c
.label
) == 0)
451 vgrow(&curf
->con
, ++curf
->ncon
);
463 if (strcmp(tokval
.str
, typ
[i
].name
) == 0)
465 err("undefined type :%s", tokval
.str
);
473 err("invalid class specifier");
475 *tyn
= findtyp(ntyp
);
495 while (peek() != Trparen
) {
496 if (curi
- insb
>= NIns
)
497 err("too many instructions (1)");
501 err("invalid reference argument");
502 if (!arg
&& rtype(r
) != RTmp
)
503 err("invalid function parameter");
506 *curi
= (Ins
){Oargc
, R
, {TYPE(ty
), r
}, Kl
};
508 *curi
= (Ins
){Oparc
, r
, {TYPE(ty
)}, Kl
};
511 *curi
= (Ins
){Oarg
, R
, {r
}, k
};
513 *curi
= (Ins
){Opar
, r
, {R
}, k
};
515 if (peek() == Trparen
)
528 h
= hash(name
) & BMask
;
529 for (b
=blkh
[h
]; b
; b
=b
->dlink
)
530 if (strcmp(b
->name
, name
) == 0)
534 strcpy(b
->name
, name
);
543 curb
->nins
= curi
- insb
;
544 idup(&curb
->ins
, insb
, curb
->nins
);
552 Ref arg
[NPred
] = {R
};
560 if (ps
== PLbl
&& t
!= Tlbl
&& t
!= Trbrace
)
561 err("label or } expected");
564 if (isstore(t
) || t
== Tcall
) {
565 /* operations without result */
571 err("label, instruction or jump expected");
577 b
= findblk(tokval
.str
);
578 if (curb
&& curb
->jmp
.type
== Jxxx
) {
580 curb
->jmp
.type
= Jjmp
;
583 if (b
->jmp
.type
!= Jxxx
)
584 err("multiple definitions of block @%s", b
->name
);
591 curb
->jmp
.type
= (int[]){
597 curb
->jmp
.type
= Jret0
;
601 err("invalid return value");
606 curb
->jmp
.type
= Jjmp
;
609 curb
->jmp
.type
= Jjnz
;
612 err("invalid argument for jnz jump");
617 curb
->s1
= findblk(tokval
.str
);
618 if (curb
->jmp
.type
!= Jjmp
) {
621 curb
->s2
= findblk(tokval
.str
);
623 if (curb
->s1
== curf
->start
|| curb
->s2
== curf
->start
)
624 err("invalid jump to the start node");
630 r
= tmpref(tokval
.str
);
636 if (ps
!= PPhi
|| curb
== curf
->start
)
637 err("unexpected phi instruction");
652 if (op
>= Tloadw
&& 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 memcpy(phi
->arg
, arg
, i
* sizeof arg
[0]);
697 memcpy(phi
->blk
, blk
, i
* sizeof blk
[0]);
706 usecheck(Ref r
, int k
, Fn
*fn
)
708 return rtype(r
) != RTmp
|| fn
->tmp
[r
.val
].cls
== k
709 || (fn
->tmp
[r
.val
].cls
== Kl
&& k
== Kw
);
725 bsinit(pb
, fn
->nblk
);
726 bsinit(ppb
, fn
->nblk
);
727 for (b
=fn
->start
; b
; b
=b
->link
) {
728 for (p
=b
->phi
; p
; p
=p
->link
)
729 fn
->tmp
[p
->to
.val
].cls
= p
->cls
;
730 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++)
731 if (rtype(i
->to
) == RTmp
) {
732 t
= &fn
->tmp
[i
->to
.val
];
733 if (clsmerge(&t
->cls
, i
->cls
))
734 err("temporary %%%s is assigned with"
735 " multiple types", t
->name
);
738 for (b
=fn
->start
; b
; b
=b
->link
) {
740 for (n
=0; n
<b
->npred
; n
++)
741 bsset(pb
, b
->pred
[n
]->id
);
742 for (p
=b
->phi
; p
; p
=p
->link
) {
744 t
= &fn
->tmp
[p
->to
.val
];
745 for (n
=0; n
<p
->narg
; n
++) {
747 if (bshas(ppb
, p
->blk
[n
]->id
))
748 err("multiple entries for @%s in phi %%%s",
749 p
->blk
[n
]->name
, t
->name
);
750 if (!usecheck(p
->arg
[n
], k
, fn
))
751 err("invalid type for operand %%%s in phi %%%s",
752 fn
->tmp
[p
->arg
[n
].val
].name
, t
->name
);
753 bsset(ppb
, p
->blk
[n
]->id
);
755 if (!bsequal(pb
, ppb
))
756 err("predecessors not matched in phi %%%s", t
->name
);
758 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++)
759 for (n
=0; n
<2; n
++) {
760 k
= opdesc
[i
->op
].argcls
[n
][i
->cls
];
764 err("invalid instruction type in %s",
766 if (rtype(r
) == RType
)
768 if (rtype(r
) != -1 && k
== Kx
)
769 err("no %s operand expected in %s",
770 n
== 1 ? "second" : "first",
772 if (rtype(r
) == -1 && k
!= Kx
)
773 err("missing %s operand in %s",
774 n
== 1 ? "second" : "first",
776 if (!usecheck(r
, k
, fn
))
777 err("invalid type for %s operand %%%s in %s",
778 n
== 1 ? "second" : "first",
779 t
->name
, opdesc
[i
->op
].name
);
782 if (isret(b
->jmp
.type
)) {
783 if (b
->jmp
.type
== Jretc
) {
784 if (!usecheck(r
, Kl
, fn
))
786 } else if (!usecheck(r
, b
->jmp
.type
-Jretw
, fn
))
789 if (b
->jmp
.type
== Jjnz
&& !usecheck(r
, Kw
, fn
))
791 err("invalid type for jump argument %%%s in block @%s",
792 fn
->tmp
[r
.val
].name
, b
->name
);
793 if (b
->s1
&& b
->s1
->jmp
.type
== Jxxx
)
794 err("block @%s is used undefined", b
->s1
->name
);
795 if (b
->s2
&& b
->s2
->jmp
.type
== Jxxx
)
796 err("block @%s is used undefined", b
->s2
->name
);
810 curf
= alloc(sizeof *curf
);
812 curf
->ncon
= 1; /* first constant must be 0 */
813 curf
->tmp
= vnew(curf
->ntmp
, sizeof curf
->tmp
[0], alloc
);
814 curf
->con
= vnew(curf
->ncon
, sizeof curf
->con
[0], alloc
);
815 for (i
=0; i
<Tmp0
; ++i
)
816 newtmp(0, i
< XMM0
? Kl
: Kd
, curf
);
817 curf
->con
[0].type
= CBits
;
818 curf
->export
= export
;
819 blink
= &curf
->start
;
822 rcls
= parsecls(&curf
->retty
);
826 err("function name expected");
827 strcpy(curf
->name
, tokval
.str
);
829 if (nextnl() != Tlbrace
)
830 err("function body must start with {");
836 err("empty function");
837 if (curb
->jmp
.type
== Jxxx
)
838 err("last block misses jump");
839 curf
->mem
= vnew(0, sizeof curf
->mem
[0], alloc
);
843 for (b
=0; b
; b
=b
->link
)
844 b
->dlink
= 0; /* was trashed by findblk() */
845 for (i
=0; i
<BMask
+1; ++i
)
852 parseseg(Seg
*seg
, Typ
*ty
, int t
)
855 int n
, c
, a
, al
, type
;
861 while (t
!= Trbrace
) {
864 default: err("invalid type member specifier");
865 case Td
: type
= Sflt
;
866 case Tl
: s
= 8; a
= 3; break;
867 case Ts
: type
= Sflt
;
868 case Tw
: s
= 4; a
= 2; break;
869 case Th
: s
= 2; a
= 1; break;
870 case Tb
: s
= 1; a
= 0; break;
873 ty1
= &typ
[findtyp(ntyp
-1)];
880 if ((a
= sz
& (s
-1))) {
883 /* padding segment */
898 for (; c
>0 && n
<NSeg
; c
--, n
++) {
907 err(", or } expected");
910 sz
= (sz
+ a
- 1) & -a
;
923 err("too many type definitions");
928 if (nextnl() != Ttyp
|| nextnl() != Teq
)
929 err("type name and then = expected");
930 strcpy(ty
->name
, tokval
.str
);
933 if (nextnl() != Tint
)
934 err("alignment expected");
935 for (al
=0; tokval
.num
/= 2; al
++)
941 err("type body must start with {");
945 ty
->size
= tokval
.num
;
947 err("dark types need alignment");
948 if (nextnl() != Trbrace
)
953 ty
->seg
= vnew(1, sizeof ty
->seg
[0], emalloc
);
957 err("invalid union member");
958 vgrow(&ty
->seg
, n
+1);
959 parseseg(ty
->seg
[n
++], ty
, nextnl());
961 } while (t
!= Trbrace
);
963 parseseg(ty
->seg
[n
++], ty
, t
);
973 d
->u
.ref
.nam
= tokval
.str
;
979 err("invalid token after offset in ref");
980 d
->u
.ref
.off
= tokval
.num
;
988 d
->u
.str
= tokval
.str
;
992 parsedat(void cb(Dat
*), int export
)
1003 if (nextnl() != Tglo
|| nextnl() != Teq
)
1004 err("data name, then = expected");
1005 strcpy(s
, tokval
.str
);
1008 if (nextnl() != Tint
)
1009 err("alignment expected");
1011 d
.u
.num
= 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
);
1056 err(", or } expected");
1064 parse(FILE *f
, char *path
, void data(Dat
*), void func(Fn
*))
1078 err("top-level definition expected");
1084 func(parsefn(export
));
1087 else if (t
== Tdata
) {
1089 parsedat(data
, export
);
1093 err("export can only qualify data and function");
1104 printcon(Con
*c
, FILE *f
)
1110 fprintf(f
, "$%s", c
->label
);
1112 fprintf(f
, "%+"PRIi64
, c
->bits
.i
);
1116 fprintf(f
, "s_%f", c
->bits
.s
);
1117 else if (c
->flt
== 2)
1118 fprintf(f
, "d_%lf", c
->bits
.d
);
1120 fprintf(f
, "%"PRIi64
, c
->bits
.i
);
1126 printref(Ref r
, Fn
*fn
, FILE *f
)
1134 fprintf(f
, "R%d", r
.val
);
1136 fprintf(f
, "%%%s", fn
->tmp
[r
.val
].name
);
1139 printcon(&fn
->con
[r
.val
], f
);
1142 fprintf(f
, "S%d", (r
.val
&(1<<28)) ? r
.val
-(1<<29) : r
.val
);
1145 fprintf(f
, "%03x", r
.val
);
1148 fprintf(f
, ":%s", typ
[r
.val
].name
);
1152 m
= &fn
->mem
[r
.val
];
1154 if (m
->offset
.type
!= CUndef
) {
1155 printcon(&m
->offset
, f
);
1158 if (!req(m
->base
, R
)) {
1161 printref(m
->base
, fn
, f
);
1164 if (!req(m
->index
, R
)) {
1167 fprintf(f
, "%d * ", m
->scale
);
1168 printref(m
->index
, fn
, f
);
1176 printfn(Fn
*fn
, FILE *f
)
1178 static char *jtoa
[NJmp
] = {
1188 #define X(c) [Jxjc+IC##c] = "xj" #c,
1192 static char prcls
[NOp
] = {
1200 static char ktoc
[] = "wlsd";
1207 fprintf(f
, "export ");
1208 fprintf(f
, "function $%s() {\n", fn
->name
);
1209 for (b
=fn
->start
; b
; b
=b
->link
) {
1210 fprintf(f
, "@%s\n", b
->name
);
1211 for (p
=b
->phi
; p
; p
=p
->link
) {
1213 printref(p
->to
, fn
, f
);
1214 fprintf(f
, " =%c phi ", ktoc
[p
->cls
]);
1217 fprintf(f
, "@%s ", p
->blk
[n
]->name
);
1218 printref(p
->arg
[n
], fn
, f
);
1219 if (n
== p
->narg
-1) {
1226 for (i
=b
->ins
; i
-b
->ins
< b
->nins
; i
++) {
1228 if (!req(i
->to
, R
)) {
1229 printref(i
->to
, fn
, f
);
1230 fprintf(f
, " =%c ", ktoc
[i
->cls
]);
1232 assert(opdesc
[i
->op
].name
);
1233 fprintf(f
, "%s", opdesc
[i
->op
].name
);
1234 if (req(i
->to
, R
) && prcls
[i
->op
])
1235 fputc(ktoc
[i
->cls
], f
);
1236 if (!req(i
->arg
[0], R
)) {
1238 printref(i
->arg
[0], fn
, f
);
1240 if (!req(i
->arg
[1], R
)) {
1242 printref(i
->arg
[1], fn
, f
);
1246 switch (b
->jmp
.type
) {
1253 fprintf(f
, "\t%s", jtoa
[b
->jmp
.type
]);
1254 if (b
->jmp
.type
!= Jret0
|| !req(b
->jmp
.arg
, R
)) {
1256 printref(b
->jmp
.arg
, fn
, f
);
1258 if (b
->jmp
.type
== Jretc
)
1259 fprintf(f
, ", :%s", typ
[fn
->retty
].name
);
1263 if (b
->s1
!= b
->link
)
1264 fprintf(f
, "\tjmp @%s\n", b
->s1
->name
);
1267 fprintf(f
, "\t%s ", jtoa
[b
->jmp
.type
]);
1268 if (b
->jmp
.type
== Jjnz
) {
1269 printref(b
->jmp
.arg
, fn
, f
);
1272 fprintf(f
, "@%s, @%s\n", b
->s1
->name
, b
->s2
->name
);