6 #define TMP_ADDR 0x0001
10 #define LOC_SYM 0x0800
11 #define LOC_LOCAL 0x1000
12 #define LOC_MASK 0xff00
51 #define MIN(a, b) ((a) < (b) ? (a) : (b))
53 #define TMP_BT(t) ((t)->flags & TMP_ADDR ? 8 : (t)->bt)
54 #define TMP_REG(t) ((t)->flags & LOC_REG ? (t)->addr : reg_get(~0))
55 #define TMP_REG2(t, r) ((t)->flags & LOC_REG && (t)->addr != r ? \
56 (t)->addr : reg_get(~(1 << r)))
57 #define BT_TMPBT(bt) (BT_SZ(bt) >= 4 ? (bt) : (bt) & BT_SIGNED | 4)
59 static char buf
[SECSIZE
];
63 static long spsub_addr
;
66 #define TMP(i) (&tmps[ntmp - 1 - (i)])
70 long off
; /* used for LOC_SYM; offset from a symbol address */
78 static struct tmp
*regs
[NREGS
];
79 static int tmpregs
[] = {R_RAX
, R_RDI
, R_RSI
, R_RDX
, R_RCX
, R_R8
, R_R9
};
81 #define MAXRET (1 << 8)
83 static long ret
[MAXRET
];
89 static void putint(char *s
, long n
, int l
)
97 static void os(char *s
, int n
)
105 static void oi(long n
, int l
)
115 static long codeaddr(void)
120 #define OP2(o2, o1) (0x010000 | ((o2) << 8) | (o1))
121 #define O2(op) (((op) >> 8) & 0xff)
122 #define O1(op) ((op) & 0xff)
123 #define MODRM(m, r1, r2) ((m) << 6 | (r1) << 3 | (r2))
125 static void o_op(int op
, int r1
, int r2
, unsigned bt
)
135 if (sz
== 1 && (r1
== R_RSI
|| r1
== R_RDI
||
136 r2
== R_RSI
|| r2
== R_RDI
))
138 /* hack: for movxx ops, bt does not represent the second arg */
139 if (op
& 0x10000 && O2(op
) == 0x0f && (O1(op
) & 0xf7) == 0xb6 &&
140 (r2
== R_RSI
|| r2
== R_RDI
))
148 oi(sz
== 1 ? O1(op
) & ~0x1 : O1(op
), 1);
151 static void memop(int op
, int src
, int base
, int off
, unsigned bt
)
153 int dis
= off
== (char) off
? 1 : 4;
154 int mod
= dis
== 4 ? 2 : 1;
155 o_op(op
, src
, base
, bt
);
158 oi(MODRM(mod
, src
& 0x07, base
& 0x07), 1);
163 static void regop(int op
, int src
, int dst
, unsigned bt
)
165 o_op(op
, src
, dst
, bt
);
166 oi(MODRM(3, src
& 0x07, dst
& 0x07), 1);
169 static long sp_push(int size
)
177 #define LOC_NEW(f, l) (((f) & ~LOC_MASK) | (l))
179 static void tmp_mem(struct tmp
*tmp
)
182 if (!(tmp
->flags
& LOC_REG
))
186 tmp
->addr
= -sp_push(8);
187 memop(MOV_R2X
, src
, R_RBP
, tmp
->addr
, BT_TMPBT(TMP_BT(tmp
)));
189 tmp
->flags
= LOC_NEW(tmp
->flags
, LOC_MEM
);
192 static int movxx_x2r(int bt
)
196 o2
= bt
& BT_SIGNED
? 0xbe : 0xb6;
198 o2
= bt
& BT_SIGNED
? 0xbf : 0xb7;
199 return OP2(0x0f, o2
);
204 static void mov_r2r(int r1
, int r2
, unsigned bt1
, unsigned bt2
)
206 int s1
= bt1
& BT_SIGNED
;
207 int s2
= bt2
& BT_SIGNED
;
208 int sz1
= BT_SZ(bt1
);
209 int sz2
= BT_SZ(bt2
);
210 if (sz2
< 4 && (sz1
> sz2
|| s1
!= s2
)) {
211 regop(movxx_x2r(bt2
), r1
, r2
, 4);
214 if (sz1
== 4 && sz2
== 8 && s1
) {
215 regop(MOVSXD
, r2
, r1
, sz2
);
218 if (r1
!= r2
|| sz1
> sz2
)
219 regop(MOV_R2X
, r1
, r2
, BT_TMPBT(bt2
));
222 static void mov_m2r(int dst
, int base
, int off
, int bt1
, int bt2
)
224 if (BT_SZ(bt1
) < 4) {
225 memop(movxx_x2r(bt1
), dst
, base
, off
,
226 bt1
& BT_SIGNED
&& BT_SZ(bt2
) == 8 ? 8 : 4);
227 mov_r2r(dst
, dst
, bt1
, bt2
);
229 memop(MOV_M2R
, dst
, base
, off
, bt1
);
230 mov_r2r(dst
, dst
, bt1
, bt2
);
234 static void num_cast(struct tmp
*t
, unsigned bt
)
236 if (!(bt
& BT_SIGNED
) && BT_SZ(bt
) != 8)
237 t
->addr
&= ((1l << (long) (BT_SZ(bt
) * 8)) - 1);
238 if (bt
& BT_SIGNED
&& BT_SZ(bt
) != 8 &&
239 t
->addr
> (1l << (BT_SZ(bt
) * 8 - 1)))
240 t
->addr
= -((1l << (BT_SZ(bt
) * 8)) - t
->addr
);
244 static void num_reg(int reg
, unsigned bt
, long num
)
246 int op
= MOV_I2R
+ (reg
& 7);
247 if (BT_SZ(bt
) == 8 && num
>= 0 && num
== (unsigned) num
)
249 o_op(op
, 0, reg
, bt
);
253 static void tmp_reg(struct tmp
*tmp
, int dst
, unsigned bt
, int deref
)
255 if (deref
&& tmp
->flags
& TMP_ADDR
)
256 tmp
->flags
&= ~TMP_ADDR
;
259 if (tmp
->flags
& LOC_NUM
) {
261 tmp
->bt
= BT_TMPBT(bt
);
262 num_reg(dst
, tmp
->bt
, tmp
->addr
);
265 tmp
->flags
= LOC_NEW(tmp
->flags
, LOC_REG
);
267 if (tmp
->flags
& LOC_SYM
) {
268 regop(MOV_I2X
, 0, dst
, 4);
270 out_rela(tmp
->addr
, codeaddr(), 0);
274 tmp
->flags
= LOC_NEW(tmp
->flags
, LOC_REG
);
276 if (tmp
->flags
& LOC_REG
) {
278 mov_m2r(dst
, tmp
->addr
, 0, tmp
->bt
, bt
);
280 mov_r2r(tmp
->addr
, dst
, TMP_BT(tmp
),
281 tmp
->flags
& TMP_ADDR
? 8 : bt
);
282 regs
[tmp
->addr
] = NULL
;
284 if (tmp
->flags
& LOC_LOCAL
) {
286 mov_m2r(dst
, R_RBP
, tmp
->addr
, tmp
->bt
, bt
);
288 memop(LEA_M2R
, dst
, R_RBP
, tmp
->addr
, 8);
290 if (tmp
->flags
& LOC_MEM
) {
291 int nbt
= deref
? 8 : TMP_BT(tmp
);
292 mov_m2r(dst
, R_RBP
, tmp
->addr
, nbt
, nbt
);
294 mov_m2r(dst
, dst
, 0, tmp
->bt
, bt
);
297 tmp
->bt
= tmp
->flags
& TMP_ADDR
? bt
: BT_TMPBT(bt
);
299 tmp
->flags
= LOC_NEW(tmp
->flags
, LOC_REG
);
302 static void reg_free(int reg
)
307 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
308 if (!regs
[tmpregs
[i
]]) {
309 tmp_reg(regs
[reg
], tmpregs
[i
], regs
[reg
]->bt
, 0);
315 static void reg_for(int reg
, struct tmp
*t
)
317 if (regs
[reg
] && regs
[reg
] != t
)
321 static void tmp_mv(struct tmp
*t
, int reg
)
324 tmp_reg(t
, reg
, t
->bt
, 0);
327 static void tmp_to(struct tmp
*t
, int reg
, int bt
)
330 tmp_reg(t
, reg
, bt
? bt
: t
->bt
, 1);
333 static void tmp_drop(int n
)
336 for (i
= ntmp
- n
; i
< ntmp
; i
++)
337 if (tmps
[i
].flags
& LOC_REG
)
338 regs
[tmps
[i
].addr
] = NULL
;
343 static int tmp_pop(int reg
, int bt
)
345 struct tmp
*t
= TMP(0);
351 static struct tmp
*tmp_new(void)
354 return &tmps
[ntmp
++];
357 static void tmp_push(int reg
, unsigned bt
)
359 struct tmp
*t
= tmp_new();
366 void o_local(long addr
, unsigned bt
)
368 struct tmp
*t
= tmp_new();
371 t
->flags
= LOC_LOCAL
| TMP_ADDR
;
374 void o_num(long num
, unsigned bt
)
376 struct tmp
*t
= tmp_new();
382 void o_symaddr(long addr
, unsigned bt
)
384 struct tmp
*t
= tmp_new();
387 t
->flags
= LOC_SYM
| TMP_ADDR
;
391 void o_tmpdrop(int n
)
393 if (n
== -1 || n
> ntmp
)
403 #define FORK_REG R_RAX
405 /* make sure tmps remain intact after a conditional expression */
409 for (i
= 0; i
< ntmp
- 1; i
++)
413 void o_forkpush(void)
418 void o_forkjoin(void)
420 tmp_push(FORK_REG
, 0);
425 struct tmp
*t1
= TMP(0);
426 struct tmp
*t2
= TMP(1);
428 memcpy(&t
, t1
, sizeof(t
));
429 memcpy(t1
, t2
, sizeof(t
));
430 memcpy(t2
, &t
, sizeof(t
));
431 if (t1
->flags
& LOC_REG
)
433 if (t2
->flags
& LOC_REG
)
437 static int reg_get(int mask
)
440 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
441 if ((1 << tmpregs
[i
]) & mask
&& !regs
[tmpregs
[i
]])
443 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
444 if ((1 << tmpregs
[i
]) & mask
) {
445 reg_free(tmpregs
[i
]);
451 void tmp_copy(struct tmp
*t1
)
453 struct tmp
*t2
= tmp_new();
454 memcpy(t2
, t1
, sizeof(*t1
));
455 if (!(t1
->flags
& (LOC_REG
| LOC_MEM
)))
457 if (t1
->flags
& LOC_MEM
) {
458 tmp_reg(t2
, reg_get(~0), t2
->bt
, 0);
459 } else if (t1
->flags
& LOC_REG
) {
460 t2
->addr
= reg_get(~(1 << t1
->addr
));
461 regop(MOV_R2X
, t1
->addr
, t2
->addr
, BT_TMPBT(TMP_BT(t1
)));
471 void o_cast(unsigned bt
)
473 struct tmp
*t
= TMP(0);
477 if (t
->flags
& LOC_NUM
) {
485 long o_func_beg(char *name
, int global
)
487 long addr
= out_func_beg(name
, global
);
489 os("\x55", 1); /* push %rbp */
490 os("\x48\x89\xe5", 3); /* mov %rsp, %rbp */
497 memset(regs
, 0, sizeof(regs
));
498 os("\x48\x81\xec", 3); /* sub $xxx, %rsp */
499 spsub_addr
= codeaddr();
504 void o_deref(unsigned bt
)
506 struct tmp
*t
= TMP(0);
507 if (t
->flags
& TMP_ADDR
)
508 tmp_to(t
, TMP_REG(t
), t
->bt
);
510 t
->flags
|= TMP_ADDR
;
515 struct tmp
*t
= TMP(0);
516 tmp_to(t
, TMP_REG(t
), t
->bt
);
519 static unsigned bt_op(unsigned bt1
, unsigned bt2
)
521 unsigned s1
= BT_SZ(bt1
);
522 unsigned s2
= BT_SZ(bt2
);
523 unsigned bt
= (bt1
| bt2
) & BT_SIGNED
| (s1
> s2
? s1
: s2
);
527 #define TMP_CONST(t) ((t)->flags & LOC_NUM && !((t)->flags & TMP_ADDR))
528 #define LOCAL_PTR(t) ((t)->flags & LOC_LOCAL && !((t)->flags & TMP_ADDR))
529 #define SYM_PTR(t) ((t)->flags & LOC_SYM && !((t)->flags & TMP_ADDR))
531 int o_popnum(long *c
)
533 struct tmp
*t
= TMP(0);
541 static int c_op(long (*cop
)(long a
, unsigned bt
), unsigned bt
)
543 struct tmp
*t1
= TMP(0);
549 ret
= cop(t1
->addr
, bt
);
555 static void shx(int uop
, int sop
)
557 struct tmp
*t2
= TMP(1);
558 int r2
= TMP_REG2(t2
, R_RCX
);
562 regop(SHX_REG
, bt
& BT_SIGNED
? sop
: uop
, r2
, BT_TMPBT(bt
));
566 static int mulop(int uop
, int sop
, int reg
)
568 struct tmp
*t1
= TMP(0);
569 struct tmp
*t2
= TMP(1);
570 int bt
= bt_op(t1
->bt
, t2
->bt
);
571 if (t1
->flags
& LOC_REG
&& t1
->addr
!= R_RAX
&& t1
->addr
!= R_RDX
)
574 tmp_to(t2
, R_RAX
, bt
);
578 o_op(0x99, R_RAX
, R_RDX
, bt
);
580 regop(XOR_R2X
, R_RDX
, R_RDX
, bt
);
583 regop(MUL_A2X
, bt
& BT_SIGNED
? sop
: uop
, reg
, BT_TMPBT(t2
->bt
));
589 tmps
[ntmp
- 1].flags
&= ~TMP_ADDR
;
590 tmps
[ntmp
- 1].bt
= 8;
593 void o_ret(unsigned bt
)
598 os("\x31\xc0", 2); /* xor %eax, %eax */
599 ret
[nret
++] = o_jmp(0);
602 static void inc(int op
)
604 struct tmp
*t
= TMP(0);
607 if (t
->flags
& LOC_LOCAL
) {
615 memop(INC_X
, op
, reg
, off
, t
->bt
);
620 if (cmp_last
== codeaddr()) {
621 buf
[cmp_setl
+ 1] ^= 0x01;
623 o_num(0, 4 | BT_SIGNED
);
630 struct tmp
*t
= TMP(0);
632 unsigned bt
= BT_TMPBT(t
->bt
);
635 regop(NOT_REG
, id
, reg
, bt
);
638 void o_func_end(void)
641 for (i
= 0; i
< nret
; i
++)
643 os("\xc9\xc3", 2); /* leave; ret; */
644 putint(buf
+ spsub_addr
, (maxsp
+ 7) & ~0x07, 4);
645 out_func_end(buf
, cur
- buf
);
648 long o_mklocal(int size
)
650 return sp_push((size
+ 7) & ~0x07);
653 void o_rmlocal(long addr
, int sz
)
658 static int arg_regs
[] = {R_RDI
, R_RSI
, R_RDX
, R_RCX
, R_R8
, R_R9
};
660 #define R_NARGS ARRAY_SIZE(arg_regs)
662 long o_arg(int i
, unsigned bt
)
666 addr
= o_mklocal(BT_SZ(bt
));
667 memop(MOV_R2X
, arg_regs
[i
], R_RBP
, -addr
, BT_TMPBT(bt
));
669 addr
= -8 * (i
- R_NARGS
+ 2);
674 void o_assign(unsigned bt
)
676 struct tmp
*t1
= TMP(0);
677 struct tmp
*t2
= TMP(1);
678 int r1
= TMP_REG(t1
);
681 tmp_to(t1
, r1
, BT_TMPBT(bt
));
682 if (t2
->flags
& LOC_LOCAL
) {
686 reg
= TMP_REG2(t2
, r1
);
691 memop(MOV_R2X
, r1
, reg
, off
, bt
);
695 static long cu(int op
, long i
)
707 static int c_uop(int op
)
709 struct tmp
*t1
= TMP(0);
713 o_num(cu(op
, t1
->addr
), t1
->bt
);
717 static long cb(int op
, long a
, long b
, int *bt
)
742 return (unsigned long) a
>> b
;
764 static int c_bop(int op
)
766 struct tmp
*t1
= TMP(0);
767 struct tmp
*t2
= TMP(1);
768 int locals
= LOCAL_PTR(t1
) + LOCAL_PTR(t2
);
769 int syms
= SYM_PTR(t1
) + SYM_PTR(t2
);
770 int nums
= TMP_CONST(t1
) + TMP_CONST(t2
);
772 if (syms
== 2 || syms
&& locals
|| syms
+ nums
+ locals
!= 2)
775 bt
= syms
? 8 : bt_op(t1
->bt
, t2
->bt
);
781 long o1
= SYM_PTR(t1
) ? t1
->off
: t1
->addr
;
782 long o2
= SYM_PTR(t2
) ? t2
->off
: t2
->addr
;
783 long ret
= cb(op
, o2
, o1
, &bt
);
789 long ret
= cb(op
, t2
->addr
, t1
->addr
, &bt
);
808 o_neg(op
== O_NEG
? 3 : 2);
820 static int binop(int op
, int *reg
)
822 struct tmp
*t1
= TMP(0);
823 struct tmp
*t2
= TMP(1);
825 unsigned bt
= bt_op(t1
->bt
, t2
->bt
);
827 r2
= TMP_REG2(t2
, r1
);
830 regop(op
, r2
, r1
, bt
);
835 static void bin_add(int op
)
837 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
838 static int rx
[] = {0x03, 0x2b, 0x23, 0x0b, 0x33};
840 int bt
= binop(rx
[op
& 0x0f], ®
);
844 static void bin_shx(int op
)
846 if ((op
& 0xff) == O_SHL
)
852 static void bin_mul(int op
)
854 if ((op
& 0xff) == O_MUL
)
855 tmp_push(R_RAX
, mulop(4, 5, R_RDX
));
856 if ((op
& 0xff) == O_DIV
)
857 tmp_push(R_RAX
, mulop(6, 7, R_RCX
));
858 if ((op
& 0xff) == O_MOD
)
859 tmp_push(R_RDX
, mulop(6, 7, R_RCX
));
862 static void o_cmp(int uop
, int sop
)
864 struct tmp
*t1
= TMP(0);
865 struct tmp
*t2
= TMP(1);
866 char set
[] = "\x0f\x00\xc0";
869 if (regs
[R_RAX
] && regs
[R_RAX
] != t1
&& regs
[R_RAX
] != t2
)
871 bt
= binop(CMP_R2X
, ®
);
872 set
[1] = bt
& BT_SIGNED
? sop
: uop
;
873 cmp_setl
= codeaddr();
874 os(set
, 3); /* setl %al */
875 os("\x0f\xb6\xc0", 3); /* movzbl %al, %eax */
876 tmp_push(R_RAX
, 4 | BT_SIGNED
);
877 cmp_last
= codeaddr();
880 static void bin_cmp(int op
)
904 static void o_bopset(int op
)
909 o_assign(TMP(1)->bt
);
914 if (!(op
& O_SET
) && !c_bop(op
))
920 if ((op
& 0xf0) == 0x00)
922 if ((op
& 0xf0) == 0x10)
924 if ((op
& 0xf0) == 0x20)
926 if ((op
& 0xf0) == 0x30)
930 void o_memcpy(int sz
)
932 struct tmp
*t0
= TMP(-1);
933 struct tmp
*t1
= TMP(0);
934 struct tmp
*t2
= TMP(1);
936 tmp_to(t0
, R_RCX
, 0);
939 os("\xf3\xa4", 2); /* rep movs */
943 void o_memset(int x
, int sz
)
945 struct tmp
*t0
= TMP(-2);
946 struct tmp
*t1
= TMP(-1);
947 struct tmp
*t2
= TMP(0);
950 tmp_to(t0
, R_RAX
, 0);
951 tmp_to(t1
, R_RCX
, 0);
953 os("\xf3\xaa", 2); /* rep stosb */
962 static long jx(int x
, long addr
)
966 os(op
, 2); /* jx $addr */
967 oi(addr
- codeaddr() - 4, 4);
968 return codeaddr() - 4;
971 static long jxtest(int x
, long addr
)
973 int bt
= tmp_pop(R_RAX
, 0);
974 regop(TEST_R2R
, R_RAX
, R_RAX
, bt
);
978 static long jxcmp(long addr
, int inv
)
981 if (codeaddr() != cmp_last
)
984 cur
= buf
+ cmp_setl
;
985 x
= (unsigned char) buf
[cmp_setl
+ 1];
986 return jx((inv
? x
: x
^ 0x01) & ~0x10, addr
);
991 long ret
= jxcmp(addr
, 0);
992 return ret
!= -1 ? ret
: jxtest(0x84, addr
);
995 long o_jnz(long addr
)
997 long ret
= jxcmp(addr
, 1);
998 return ret
!= -1 ? ret
: jxtest(0x85, addr
);
1001 long o_jmp(long addr
)
1003 os("\xe9", 1); /* jmp $addr */
1004 oi(addr
- codeaddr() - 4, 4);
1005 return codeaddr() - 4;
1008 void o_filljmp2(long addr
, long jmpdst
)
1010 putint(buf
+ addr
, jmpdst
- addr
- 4, 4);
1013 void o_filljmp(long addr
)
1015 o_filljmp2(addr
, codeaddr());
1018 void o_call(int argc
, unsigned *bt
, unsigned ret_bt
)
1022 for (i
= 0; i
< ARRAY_SIZE(tmpregs
); i
++)
1023 if (regs
[tmpregs
[i
]] && regs
[tmpregs
[i
]] - tmps
< ntmp
- argc
)
1024 tmp_mem(regs
[tmpregs
[i
]]);
1025 if (argc
> R_NARGS
) {
1026 long addr
= sp_push(8 * (argc
- R_NARGS
));
1027 for (i
= argc
- 1; i
>= R_NARGS
; --i
) {
1028 int reg
= TMP_REG(TMP(0));
1029 tmp_pop(reg
, bt
[i
]);
1030 memop(MOV_R2X
, reg
, R_RBP
,
1031 -(addr
- (i
- R_NARGS
) * 8), BT_TMPBT(bt
[i
]));
1034 for (i
= MIN(argc
, R_NARGS
) - 1; i
>= 0; i
--)
1035 tmp_pop(arg_regs
[i
], BT_TMPBT(bt
[i
]));
1037 if (t
->flags
& LOC_SYM
) {
1038 os("\x31\xc0", 2); /* xor %eax, %eax */
1039 os("\xe8", 1); /* call $x */
1041 out_rela(t
->addr
, codeaddr(), 1);
1045 tmp_mv(TMP(0), R_RAX
);
1047 regop(CALL_REG
, 2, R_RAX
, 4);
1050 tmp_push(R_RAX
, ret_bt
);
1063 void o_datset(long addr
, int off
, unsigned bt
)
1065 struct tmp
*t
= TMP(0);
1066 if (t
->flags
& LOC_NUM
&& !(t
->flags
& TMP_ADDR
)) {
1068 out_datcpy(addr
, off
, (void *) &t
->addr
, BT_SZ(bt
));
1070 if (t
->flags
& LOC_SYM
&& !(t
->flags
& TMP_ADDR
)) {
1071 out_datrela(t
->addr
, addr
, off
);
1072 out_datcpy(addr
, off
, (void *) &t
->off
, BT_SZ(bt
));