34 #define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
37 int tmpregs
[] = {0, 1, 2, 6, 7, 3};
40 #define OP2(o2, o1) (0x010000 | ((o2) << 8) | (o1))
41 #define O2(op) (((op) >> 8) & 0xff)
42 #define O1(op) ((op) & 0xff)
43 #define MODRM(m, r1, r2) ((m) << 6 | (r1) << 3 | (r2))
45 static void putint(char *s
, long n
, int l
)
53 static void op_x(int op
, int r1
, int r2
, int bt
)
60 oi(sz
== 1 ? O1(op
) & ~0x1 : O1(op
), 1);
65 /* op_*(): r=reg, m=mem, i=imm, s=sym */
66 static void op_rm(int op
, int src
, int base
, int off
, int bt
)
68 int dis
= off
== (char) off
? 1 : 4;
69 int mod
= dis
== 4 ? 2 : 1;
70 if (!off
&& (base
& 7) != R_RBP
)
72 op_x(op
, src
, base
, bt
);
73 oi(MODRM(mod
, src
& 0x07, base
& 0x07), 1);
74 if ((base
& 7) == R_RSP
)
80 static void op_rr(int op
, int src
, int dst
, int bt
)
82 op_x(op
, src
, dst
, bt
);
83 oi(MODRM(3, src
& 0x07, dst
& 0x07), 1);
86 #define movrx_bt(bt) (LONGSZ)
88 static int movrx_op(int bt
, int mov
)
92 return OP2(0x0f, bt
& BT_SIGNED
? 0xbf : 0xb7);
94 return OP2(0x0f, bt
& BT_SIGNED
? 0xbe : 0xb6);
98 static void mov_r2r(int rd
, int r1
, unsigned bt
)
100 if (rd
!= r1
|| BT_SZ(bt
) != LONGSZ
)
101 op_rr(movrx_op(bt
, I_MOVR
), rd
, r1
, movrx_bt(bt
));
104 static void mov_m2r(int dst
, int base
, int off
, int bt
)
106 op_rm(movrx_op(bt
, I_MOVR
), dst
, base
, off
, movrx_bt(bt
));
109 int i_imm(int op
, long imm
)
111 if ((op
& 0xf0) == 0x20)
113 return imm
<= 127 && imm
>= -128;
116 static void i_push(int reg
)
118 op_x(I_PUSH
| (reg
& 0x7), 0, reg
, 4);
121 static void i_pop(int reg
)
123 op_x(I_POP
| (reg
& 0x7), 0, reg
, 4);
126 void i_mov(int rd
, int rn
)
128 op_rr(movrx_op(LONGSZ
, I_MOVR
), rd
, rn
, movrx_bt(LONGSZ
));
131 void i_load(int rd
, int rn
, int off
, int bt
)
133 mov_m2r(rd
, rn
, off
, bt
);
136 void i_save(int rd
, int rn
, int off
, int bt
)
138 op_rm(I_MOV
, rd
, rn
, off
, bt
);
141 void i_reg(int op
, int *rd
, int *r1
, int *r2
, int *tmp
)
145 *r2
= op
& O_IMM
? 0 : R_TMPS
;
147 if ((op
& 0xf0) == 0x00) /* add */
149 if ((op
& 0xf0) == 0x10) { /* shl */
156 if ((op
& 0xf0) == 0x20) { /* mul */
157 *rd
= (op
& 0xff) == O_MOD
? (1 << R_RDX
) : (1 << R_RAX
);
159 *r2
= R_TMPS
& ~*rd
& ~*r1
;
160 if ((op
& 0xff) == O_DIV
)
161 *r2
&= ~(1 << R_RDX
);
162 *tmp
= (1 << R_RDX
) | (1 << R_RAX
);
165 if ((op
& 0xf0) == 0x30) { /* cmp */
169 if ((op
& 0xf0) == 0x40) { /* uop */
171 if ((op
& 0xff) == O_LNOT
)
175 if ((op
& 0xf0) == 0x50) { /* etc */
186 if (op
== O_SX
|| op
== O_ZX
) {
199 static void i_add(int op
, int rd
, int r1
, int r2
)
201 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
202 static int rx
[] = {0003, 0053, 0043, 0013, 0063};
203 op_rr(rx
[op
& 0x0f], rd
, r2
, LONGSZ
);
206 static void i_add_imm(int op
, int rd
, int rn
, long n
)
208 /* opcode for O_ADD, O_SUB, O_AND, O_OR, O_XOR */
209 static int rx
[] = {0xc0, 0xe8, 0xe0, 0xc8, 0xf0};
210 unsigned char s
[3] = {0x83, rx
[op
& 0x0f] | rd
, n
& 0xff};
214 void i_num(int rd
, long n
)
217 op_rr(I_XOR
, rd
, rd
, 4);
220 op_x(I_MOVIR
+ (rd
& 7), 0, rd
, LONGSZ
);
225 static void i_mul(int rd
, int r1
, int r2
)
229 op_rr(I_MUL
, 4, r2
, LONGSZ
);
232 static void i_div(int op
, int rd
, int r1
, int r2
)
236 op_x(I_CQO
, R_RAX
, R_RDX
, LONGSZ
);
240 op_rr(I_MUL
, op
& O_SIGNED
? 7 : 6, r2
, LONGSZ
);
243 static void i_tst(int rn
, int rm
)
245 op_rr(I_TST
, rn
, rm
, LONGSZ
);
248 static void i_cmp(int rn
, int rm
)
250 op_rr(I_CMP
, rn
, rm
, LONGSZ
);
253 static void i_cmp_imm(int rn
, long n
)
255 unsigned char s
[3] = {0x83, 0xf8 | rn
, n
& 0xff};
259 static void i_shl(int op
, int rd
, int r1
, int rs
)
262 if ((op
& 0x0f) == 1)
263 sm
= op
& O_SIGNED
? 7 : 5;
264 op_rr(I_SHX
, sm
, rd
, LONGSZ
);
267 static void i_shl_imm(int op
, int rd
, int rn
, long n
)
269 int sm
= (op
& 0x1) ? (op
& O_SIGNED
? 0xf8 : 0xe8) : 0xe0;
270 char s
[3] = {0xc1, sm
| rn
, n
& 0xff};
274 void i_sym(int rd
, char *sym
, int off
)
276 op_x(I_MOVIR
+ (rd
& 7), 0, rd
, LONGSZ
);
278 out_rel(sym
, OUT_CS
, cslen
);
282 static void i_neg(int rd
)
284 op_rr(I_NOT
, 3, rd
, LONGSZ
);
287 static void i_not(int rd
)
289 op_rr(I_NOT
, 2, rd
, LONGSZ
);
292 /* for optimizing cmp + tst + jmp to cmp + jmp */
293 #define OPT_ISCMP() (last_set >= 0 && last_set + 6 == cslen)
294 #define OPT_CCOND() (cs[last_set + 1])
296 static long last_set
= -1;
298 static void i_set(int op
, int rd
)
300 /* lt, gt, le, ge, eq, neq */
301 static int ucond
[] = {0x92, 0x97, 0x96, 0x93, 0x94, 0x95};
302 static int scond
[] = {0x9c, 0x9f, 0x9e, 0x9d, 0x94, 0x95};
303 int cond
= op
& O_SIGNED
? scond
[op
& 0x0f] : ucond
[op
& 0x0f];
304 char set
[] = "\x0f\x00\xc0";
307 os(set
, 3); /* setl al */
308 os("\x0f\xb6\xc0", 3); /* movzx rax, al */
311 static void i_lnot(int rd
)
314 cs
[last_set
+ 1] ^= 0x01;
316 char cmp
[] = "\x83\xf8\x00";
318 os(cmp
, 3); /* cmp eax, 0 */
323 static void jx(int x
, int nbytes
)
327 op
[0] = 0x70 | (x
& 0x0f);
328 os(op
, 1); /* jx $addr */
331 os(op
, 2); /* jx $addr */
336 void i_jmp(int rn
, int z
, int nbytes
)
344 int cond
= OPT_CCOND();
346 jx((!z
? cond
: cond
^ 0x01) & ~0x10, nbytes
);
350 jx(z
? 0x84 : 0x85, nbytes
);
353 os(nbytes
== 1 ? "\xeb" : "\xe9", 1); /* jmp $addr */
358 long i_fill(long src
, long dst
, int nbytes
)
364 putint((void *) (cs
+ src
- nbytes
), dst
- src
, nbytes
);
368 static void i_zx(int rd
, int r1
, int bits
)
371 i_shl_imm(O_SHL
, rd
, rd
, LONGSZ
* 8 - bits
);
372 i_shl_imm(O_SHR
, rd
, rd
, LONGSZ
* 8 - bits
);
374 mov_r2r(rd
, r1
, bits
>> 3);
378 static void i_sx(int rd
, int r1
, int bits
)
380 mov_r2r(rd
, r1
, BT_SIGNED
| (bits
>> 3));
383 void i_op(int op
, int rd
, int r1
, int r2
)
385 if ((op
& 0xf0) == 0x00)
386 i_add(op
, r1
, r1
, r2
);
387 if ((op
& 0xf0) == 0x10)
388 i_shl(op
, r1
, r1
, r2
);
389 if ((op
& 0xf0) == 0x20) {
390 if ((op
& 0xff) == O_MUL
)
391 i_mul(R_RAX
, r1
, r2
);
392 if ((op
& 0xff) == O_DIV
)
393 i_div(op
, R_RAX
, r1
, r2
);
394 if ((op
& 0xff) == O_MOD
)
395 i_div(op
, R_RDX
, r1
, r2
);
398 if ((op
& 0xf0) == 0x30) {
403 if ((op
& 0xf0) == 0x40) { /* uop */
404 if ((op
& 0xff) == O_NEG
)
406 if ((op
& 0xff) == O_NOT
)
408 if ((op
& 0xff) == O_LNOT
)
414 static void i_add_anyimm(int rd
, int rn
, long n
)
416 op_rm(I_LEA
, rd
, rn
, n
, LONGSZ
);
419 void i_op_imm(int op
, int rd
, int r1
, long n
)
421 if ((op
& 0xf0) == 0x00) { /* add */
422 if (rd
== r1
&& i_imm(O_ADD
, n
))
423 i_add_imm(op
, rd
, r1
, n
);
425 i_add_anyimm(rd
, r1
, n
);
427 if ((op
& 0xf0) == 0x10) /* shl */
428 i_shl_imm(op
, rd
, r1
, n
);
429 if ((op
& 0xf0) == 0x20) /* mul */
430 die("mul/imm not implemented");
431 if ((op
& 0xf0) == 0x30) { /* imm */
435 if ((op
& 0xf0) == 0x50) { /* etc */
436 if ((op
& 0xff) == O_ZX
)
438 if ((op
& 0xff) == O_SX
)
440 if ((op
& 0xff) == O_MOV
)
445 void i_memcpy(int r0
, int r1
, int r2
)
447 os("\xfc\xf3\xa4", 3); /* cld; rep movs */
450 void i_memset(int r0
, int r1
, int r2
)
452 os("\xfc\xf3\xaa", 3); /* cld; rep stosb */
455 void i_call_reg(int rd
)
457 op_rr(I_CALL
, 2, rd
, LONGSZ
);
460 void i_call(char *sym
, int off
)
462 os("\xe8", 1); /* call $x */
464 out_rel(sym
, OUT_CS
| OUT_RLREL
, cslen
);
468 static int func_argc
;
469 static int func_varg
;
470 static int func_spsub
;
471 static int func_sargs
;
472 static int func_sregs
;
473 static int func_initfp
;
474 static int spsub_addr
;
485 for (i
= 0; i
< N_TMPS
; i
++)
486 if ((1 << tmpregs
[i
]) & func_sregs
)
491 void i_prolog(int argc
, int varg
, int sargs
, int sregs
, int initfp
, int subsp
)
499 func_initfp
= initfp
;
502 os("\x55", 1); /* push rbp */
503 os("\x89\xe5", 2); /* mov rbp, rsp */
506 for (i
= N_TMPS
- 1; i
>= 0; i
--)
507 if ((1 << tmpregs
[i
]) & func_sregs
)
511 os("\x81\xec", 2); /* sub rsp, $xxx */
517 void i_epilog(int sp_max
)
522 for (i
= 0; i
< N_TMPS
; i
++)
523 if ((1 << tmpregs
[i
]) & func_sregs
)
525 diff
= ALIGN(-sp_max
- nsregs
* LONGSZ
, 16);
526 /* forcing 16-byte alignment */
527 diff
= nsregs
& 1 ? diff
+ LONGSZ
: diff
;
528 if (func_spsub
&& diff
) {
529 i_add_anyimm(R_RSP
, R_RBP
, -nsregs
* LONGSZ
);
530 putint(cs
+ spsub_addr
, diff
, 4);
533 for (i
= 0; i
< N_TMPS
; i
++)
534 if ((1 << tmpregs
[i
]) & func_sregs
)
538 os("\xc9", 1); /* leave */
539 os("\xc3", 1); /* ret */