codegen: rework ALU_WRITES_FLAGS so that it takes four arguments: alu,
[ajla.git] / c2-x86.inc
blob64c066d7b8672920bc44c316567725a349c830f8
1 /*
2  * Copyright (C) 2024 Mikulas Patocka
3  *
4  * This file is part of Ajla.
5  *
6  * Ajla is free software: you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation, either version 3 of the License, or (at your option) any later
9  * version.
10  *
11  * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * Ajla. If not, see <https://www.gnu.org/licenses/>.
17  */
19 #define X86_ALU_RM8_R8                  0x00
20 #define X86_ALU_RM16_R16                0x01
21 #define X86_ALU_R8_RM8                  0x02
22 #define X86_ALU_R16_RM16                0x03
23 #define X86_ALU_AL_IMM8                 0x04
24 #define X86_ALU_AX_IMM16                0x05
25 #define X86_0F                          0x0f
26 #define X86_ES                          0x26
27 #define X86_CS                          0x2e
28 #define X86_SS                          0x36
29 #define X86_DS                          0x3e
30 #define X86_REX                         0x40
31 #define  X86_REX_B                              0x01
32 #define  X86_REX_X                              0x02
33 #define  X86_REX_R                              0x04
34 #define  X86_REX_W                              0x08
35 #define X86_INC_R16                     0x40
36 #define X86_DEC_R16                     0x48
37 #define X86_PUSH_R16                    0x50
38 #define X86_POP_R16                     0x58
39 #define X86_MOVSXD                      0x63
40 #define X86_FS                          0x64
41 #define X86_GS                          0x65
42 #define X86_OP_SIZE                     0x66
43 #define X86_PUSH_IMM16                  0x68
44 #define X86_IMUL_R16_RM16_IMM16         0x69
45 #define X86_PUSH_IMM8                   0x6a
46 #define X86_IMUL_R16_RM16_IMM8          0x6b
47 #define X86_JCC_8                       0x70
48 #define X86_ALU_RM8_IMM8                0x80
49 #define X86_ALU_RM16_IMM16              0x81
50 #define X86_ALU_RM16_IMM8               0x83
51 #define X86_TEST_RM8_R8                 0x84
52 #define X86_TEST_RM16_R16               0x85
53 #define X86_MOV_RM8_R8                  0x88
54 #define X86_MOV_RM16_R16                0x89
55 #define X86_MOV_R8_RM8                  0x8a
56 #define X86_MOV_R16_RM16                0x8b
57 #define X86_LEA_R16_RM16                0x8d
58 #define X86_CBW                         0x98
59 #define X86_CWD                         0x99
60 #define X86_MOV_AL_M16                  0xa0
61 #define X86_MOV_AX_M16                  0xa1
62 #define X86_MOV_M16_AL                  0xa2
63 #define X86_MOV_M16_AX                  0xa3
64 #define X86_MOVSB                       0xa4
65 #define X86_TEST_AL_IMM8                0xa8
66 #define X86_TEST_AX_IMM16               0xa9
67 #define X86_STOSB                       0xaa
68 #define X86_MOV_R16_IMM16               0xb8
69 #define X86_ROT_RM8_IMM8                0xc0
70 #define X86_ROT_RM16_IMM8               0xc1
71 #define X86_RET_IMM16                   0xc2
72 #define X86_RET                         0xc3
73 #define X86_VEX_3                       0xc4
74 #define X86_VEX_2                       0xc5
75 #define X86_MOV_RM8_IMM8                0xc6
76 #define X86_MOV_RM16_IMM16              0xc7
77 #define  X86_MOV_R16_IMM16_REG                  0x0
78 #define X86_8F                          0x8f
79 #define  X86_8F_POP                             0x0
80 #define X86_ROT_RM8_1                   0xd0
81 #define X86_ROT_RM16_1                  0xd1
82 #define X86_ROT_RM8_CL                  0xd2
83 #define X86_ROT_RM16_CL                 0xd3
84 #define X86_JMP_16                      0xe9
85 #define X86_JMP_8                       0xeb
86 #define X86_REPNE                       0xf2
87 #define X86_REPE                        0xf3
88 #define X86_F6                          0xf6
89 #define  X86_F6_TEST_RM8_IMM8                   0x0
90 #define  X86_F6_NOT_RM8                         0x2
91 #define  X86_F6_NEG_RM8                         0x3
92 #define  X86_F6_MUL_RM8                         0x4
93 #define  X86_F6_IMUL_RM8                        0x5
94 #define  X86_F6_DIV_RM8                         0x6
95 #define  X86_F6_IDIV_RM8                        0x7
96 #define X86_F7                          0xf7
97 #define  X86_F7_TEST_RM16_IMM16                 0x0
98 #define  X86_F7_NOT_RM16                        0x2
99 #define  X86_F7_NEG_RM16                        0x3
100 #define  X86_F7_MUL_RM16                        0x4
101 #define  X86_F7_IMUL_RM16                       0x5
102 #define  X86_F7_DIV_RM16                        0x6
103 #define  X86_F7_IDIV_RM16                       0x7
104 #define X86_FE                          0xfe
105 #define  X86_FE_INC_RM8                         0x0
106 #define  X86_FE_DEC_RM8                         0x1
107 #define X86_FF                          0xff
108 #define  X86_FF_INC_RM16                        0x0
109 #define  X86_FF_DEC_RM16                        0x1
110 #define  X86_FF_CALL_INDIRECT                   0x2
111 #define  X86_FF_JMP_INDIRECT                    0x4
112 #define  X86_FF_PUSH                            0x6
114 #define X86_0F_MOVSS_X128_M32           0x10
115 #define X86_0F_MOVSS_M32_X128           0x11
116 #define X86_0F_MOVAPS_X128_M128         0x28
117 #define X86_0F_MOVAPS_M128_X128         0x29
118 #define X86_0F_CVTSI2SS_X128_RM32       0x2a
119 #define X86_0F_CVTTSS2SI_X128_RM32      0x2c
120 #define X86_0F_UCOMISS_X128_RM32        0x2e
121 #define X86_0F_38                       0x38
122 #define X86_0F_3A                       0x3a
123 #define X86_0F_CMOVCC_R16_RM16          0x40
124 #define X86_0F_SQRTPS_X128_M32          0x51
125 #define X86_0F_ANDPS_X128_M128          0x54
126 #define X86_0F_ANDNPS_X128_M128         0x55
127 #define X86_0F_ORPS_X128_M128           0x56
128 #define X86_0F_XORPS_X128_M128          0x57
129 #define X86_0F_ADDPS_X128_M32           0x58
130 #define X86_0F_MULPS_X128_M32           0x59
131 #define X86_0F_SUBPS_X128_M32           0x5c
132 #define X86_0F_DIVPS_X128_M32           0x5e
133 #define X86_0F_MOVD_X128_RM32           0x6e
134 #define X86_0F_MOVD_RM32_X128           0x7e
135 #define X86_0F_JCC_16                   0x80
136 #define X86_0F_SETCC_RM8                0x90
137 #define X86_0F_BT_RM16_R16              0xa3
138 #define X86_0F_BTS_RM16_R16             0xab
139 #define X86_0F_BTR_RM16_R16             0xb3
140 #define X86_0F_BTX_RM16_IMM8            0xba
141 #define  X86_0F_BTX_BT_RM16_IMM8                0x4
142 #define  X86_0F_BTX_BTS_RM16_IMM8               0x5
143 #define  X86_0F_BTX_BTR_RM16_IMM8               0x6
144 #define  X86_0F_BTX_BTC_RM16_IMM8               0x7
145 #define X86_0F_BSWAP                    0xc8
146 #define X86_0F_BTC_RM16_R16             0xbb
147 #define X86_0F_IMUL_R16_RM16            0xaf
148 #define X86_0F_MOVZX_R16_RM8            0xb6
149 #define X86_0F_MOVZX_R16_RM16           0xb7
150 #define X86_0F_POPCNT_R16_RM16          0xb8
151 #define X86_0F_BSF_R16_RM16             0xbc
152 #define X86_0F_BSR_R16_RM16             0xbd
153 #define X86_0F_MOVSX_R16_RM8            0xbe
154 #define X86_0F_MOVSX_R16_RM16           0xbf
155 #define X86_0F_PINSRW_X128_RM16_IMM8    0xc4
157 #define X86_0F_38_CVTPH2PS_X128_RM64    0x13
158 #define X86_0F_38_ROTX                  0xf7
160 #define X86_0F_3A_ROUNDSS_X128_M32      0x0a
161 #define X86_0F_3A_ROUNDSD_X128_M64      0x0b
162 #define X86_0F_3A_PEXTRW_RM16_X128_IMM8 0x15
163 #define X86_0F_3A_CVTPS2PH_RM64_X128    0x1d
165 #define X87_FLD_RM32                    0xd9
166 #define  X87_FLD_RM32_X                         0x0
167 #define X87_FLDCW                       0xd9
168 #define  X87_FLDCW_X                            0x5
169 #define X87_FILD_M32                    0xdb
170 #define  X87_FILD_M32_X                         0x0
171 #define X87_FISTTP_M32                  0xdb
172 #define  X87_FISTTP_M32_X                       0x1
173 #define X87_FISTP_M32                   0xdb
174 #define  X87_FISTP_M32_X                        0x3
175 #define X87_FLD_M80                     0xdb
176 #define  X87_FLD_M80_X                          0x5
177 #define X87_FLD_M64                     0xdd
178 #define  X87_FLD_M64_X                          0x0
179 #define X87_FSTP_M32                    0xd9
180 #define  X87_FSTP_M32_X                         0x3
181 #define X87_FSTP_M80                    0xdb
182 #define  X87_FSTP_M80_X                         0x7
183 #define X87_FSTP_RM64                   0xdd
184 #define  X87_FSTP_RM64_X                        0x3
185 #define X87_FALU_ST_RM32                0xd8
186 #define X87_FCHS                        0xd9
187 #define  X87_FCHS_2                             0xe0
188 #define X87_FSQRT                       0xd9
189 #define  X87_FSQRT_2                            0xfa
190 #define X87_FRNDINT                     0xd9
191 #define  X87_FRNDINT_2                          0xfc
192 #define X87_FALU_ST_M64                 0xdc
193 #define X87_FALU_STi_ST                 0xdc
194 #define X87_FISTTP_M64                  0xdd
195 #define  X87_FISTTP_M64_X                       0x1
196 #define X87_FALUP_STi_ST0               0xde
197 #define  X87_ALU_ADD                            0x0
198 #define  X87_ALU_MUL                            0x1
199 #define  X87_ALU_FCOM                           0x2
200 #define  X87_ALU_FCOMP                          0x3
201 #define  X87_ALU_SUBR                           0x4
202 #define  X87_ALU_SUB                            0x5
203 #define  X87_ALU_DIVR                           0x6
204 #define  X87_ALU_DIV                            0x7
205 #define X87_FCOMPP                      0xde
206 #define  X87_FCOMPP_2                           0xd9
207 #define X87_FILD_M16                    0xdf
208 #define  X87_FILD_M16_X                         0x0
209 #define X87_FISTTP_M16                  0xdf
210 #define  X87_FISTTP_M16_X                       0x1
211 #define X87_FISTP_M16                   0xdf
212 #define  X87_FISTP_M16_X                        0x3
213 #define X87_FILD_M64                    0xdf
214 #define  X87_FILD_M64_X                         0x5
215 #define X87_FISTP_M64                   0xdf
216 #define  X87_FISTP_M64_X                        0x7
217 #define X87_FNSTSW                      0xdf
218 #define  X87_FNSTSW_2                           0xe0
219 #define X87_FCOMIP                      0xdf
220 #define  X87_FCOMIP_2                           0xf0
222 #define SSE_PREFIX_NONE                 0
223 #define SSE_PREFIX_66                   1
224 #define SSE_PREFIX_F3                   2
225 #define SSE_PREFIX_F2                   3
227 #define PREFIX_NONE                     0
228 #define PREFIX_0F                       1
229 #define PREFIX_0F_38                    2
230 #define PREFIX_0F_3A                    3
232 #ifdef ARCH_X86_32
233 #define cgen_rex(rex)           internal(file_line, "cgen_rex: attempting to generate rex in 32-bit mode: %02x", rex)
234 #else
235 #define cgen_rex(rex)           cgen_one(rex)
236 #endif
238 #define force_vex                       0x10000
240 static bool attr_w cgen_rm_insn(struct codegen_context *ctx, int32_t sse_prefix, uint8_t prefix, uint8_t opcode, unsigned size, bool reg_is_reg, uint8_t reg, uint8_t *arg)
242         uint8_t rex, mod, rm;
243         int16_t sib;
244         int64_t imm = 0;        /* avoid warning */
245         uint8_t addr_size;
246         uint8_t arg_reg;
247         bool need_rex;
249         if (unlikely(R_IS_XMM(reg)))
250                 reg -= R_XMM0;
251         if (unlikely(R_IS_XMM(arg[0]))) {
252                 arg_reg = arg[0] - R_XMM0;
253                 arg = &arg_reg;
254         }
255         if (unlikely(!R_IS_GPR(reg)))
256                 internal(file_line, "cgen_rm_insn: invalid register %02x", reg);
258         sib = -1;
259         rex = X86_REX;
260         if (size == OP_SIZE_8)
261                 rex |= X86_REX_W;
262         if (reg & 8)
263                 rex |= X86_REX_R;
264         if (arg[0] < 0x40) {
265                 if (arg[0] & 8)
266                         rex |= X86_REX_B;
267                 mod = 0xc0;
268                 rm = arg[0] & 7;
269                 addr_size = 0;
270         } else {
271                 uint8_t *imm_ptr = arg + arg_size(arg[0]) - 8;
272                 imm = get_imm(imm_ptr);
273                 if (unlikely(!imm_is_32bit(imm)))
274                         internal(file_line, "cgen_rm_insn: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
275                 if (arg[0] == ARG_ADDRESS_0) {
276 address_0:
277                         addr_size = 4;
278                         mod = 0x00;
279                         rm = 0x4;
280                         sib = 0x25;
281                         goto gen;
282                 }
283                 if (!imm) {
284                         addr_size = 0;
285                 } else if (imm >= -0x80 && imm <= 0x7f) {
286                         addr_size = 1;
287                 } else {
288                         addr_size = 4;
289                 }
290                 if ((arg[1] & 7) == 0x5 && addr_size == 0)
291                         addr_size = 1;
292                 if (addr_size == 0)
293                         mod = 0x00;
294                 else if (addr_size == 1)
295                         mod = 0x40;
296                 else
297                         mod = 0x80;
298                 if (arg[0] == ARG_ADDRESS_1) {
299                         if (reg_is_segment(arg[1])) {
300                                 static const uint8_t segments[6] = { X86_ES, X86_CS, X86_SS, X86_DS, X86_FS, X86_GS };
301                                 cgen_one(segments[arg[1] - R_ES]);
302                                 goto address_0;
303                         }
304                         if (arg[1] & 8)
305                                 rex |= X86_REX_B;
306                         if ((arg[1] & 7) == 0x4) {
307                                 rm = 0x4;
308                                 sib = 0x24;
309                                 goto gen;
310                         }
311                         rm = arg[1] & 7;
312                         goto gen;
313                 }
314                 if (arg[0] >= ARG_ADDRESS_1_2 && arg[0] <= ARG_ADDRESS_1_8) {
315                         if (unlikely(arg[1] == R_SP))
316                                 internal(file_line, "cgen_rm_insn: attemptint to scale SP");
317                         if (arg[1] & 8)
318                                 rex |= X86_REX_X;
319                         addr_size = 4;
320                         mod = 0x00;
321                         rm = 0x4;
322                         sib = ((arg[0] - ARG_ADDRESS_1) << 6) | ((arg[1] & 7) << 3) | 0x5;
323                         goto gen;
324                 }
325                 if (arg[0] >= ARG_ADDRESS_2 && arg[0] <= ARG_ADDRESS_2_8) {
326                         if (unlikely(arg[2] == R_SP))
327                                 internal(file_line, "cgen_rm_insn: attemptint to scale SP");
328                         if (arg[1] & 8)
329                                 rex |= X86_REX_B;
330                         if (arg[2] & 8)
331                                 rex |= X86_REX_X;
332                         rm = 0x4;
333                         sib = ((arg[0] - ARG_ADDRESS_2) << 6) | ((arg[2] & 7) << 3) | (arg[1] & 7);
334                         goto gen;
335                 }
336                 internal(file_line, "cgen_rm_insn: invalid argument %02x", arg[0]);
337         }
339 gen:
340         if (unlikely(sse_prefix >= 0)) {
341                 if (likely(cpu_test_feature(CPU_FEATURE_avx)) || (sse_prefix & force_vex)) {
342                         if ((rex & (X86_REX_X | X86_REX_B | X86_REX_W)) == 0 && prefix == PREFIX_0F) {
343                                 cgen_one(X86_VEX_2);
344                                 cgen_one((~rex & X86_REX_R) << 5 | (~(sse_prefix >> 8) & 0xf) << 3 | (sse_prefix & 3));
345                         } else {
346                                 cgen_one(X86_VEX_3);
347                                 cgen_one((~rex & (X86_REX_R | X86_REX_X | X86_REX_B)) << 5 | prefix);
348                                 cgen_one((rex & X86_REX_W) << 4 | (~(sse_prefix >> 8) & 0xf) << 3 | (sse_prefix & 3));
349                         }
350                         goto avx_done;
351                 }
352                 switch (sse_prefix & 3) {
353                         case SSE_PREFIX_66:     cgen_one(X86_OP_SIZE); break;
354                         case SSE_PREFIX_F3:     cgen_one(X86_REPE); break;
355                         case SSE_PREFIX_F2:     cgen_one(X86_REPNE); break;
356                 }
357         } else {
358                 if (size == OP_SIZE_2)
359                         cgen_one(X86_OP_SIZE);
360         }
361         need_rex = rex != X86_REX;
362         need_rex |= size == OP_SIZE_1 && ((reg_is_reg && !reg_is_fp(reg) && reg >= 4) || (mod == 0xc0 && !reg_is_fp(arg[0]) && arg[0] >= 4));
363         if (prefix == PREFIX_0F && (opcode == X86_0F_MOVZX_R16_RM8 || opcode == X86_0F_MOVSX_R16_RM8)) {
364                 need_rex |= mod == 0xc0 && arg[0] >= 4;
365         }
366         if (need_rex)
367                 cgen_rex(rex);
369         switch (prefix) {
370                 case PREFIX_NONE:
371                         break;
372                 case PREFIX_0F:
373                         cgen_one(X86_0F);
374                         break;
375                 case PREFIX_0F_38:
376                         cgen_one(X86_0F);
377                         cgen_one(X86_0F_38);
378                         break;
379                 case PREFIX_0F_3A:
380                         cgen_one(X86_0F);
381                         cgen_one(X86_0F_3A);
382                         break;
383                 default:
384                         internal(file_line, "cgen_rm_insn: invalid prefix %u", prefix);
385         }
386 avx_done:
387         cgen_one(opcode);
388         cgen_one(mod | ((reg & 7) << 3) | (rm & 7));
389         if (sib >= 0)
390                 cgen_one(sib);
391         switch (addr_size) {
392                 case 1:
393                         cgen_one(imm);
394                         break;
395                 case 4:
396                         cgen_four(imm);
397                         break;
398         }
399         return true;
402 static bool attr_w cgen_sse_insn(struct codegen_context *ctx, unsigned sse_prefix, unsigned sse_op_map, uint8_t opcode, bool wide, uint8_t reg, uint8_t reg2, uint8_t *arg)
404         g(cgen_rm_insn(ctx, sse_prefix + (reg2 << 8), sse_op_map, opcode, !wide ? OP_SIZE_4 : OP_SIZE_8, true, reg, arg));
405         return true;
408 static bool attr_w cgen_push(struct codegen_context *ctx)
410         uint8_t *arg1 = ctx->code_position;
411         ctx->code_position += arg_size(*arg1);
412         if (likely(R_IS_GPR(arg1[0]))) {
413                 if (arg1[0] & 8)
414                         cgen_rex(X86_REX | X86_REX_B);
415                 cgen_one(X86_PUSH_R16 + (arg1[0] & 7));
416                 return true;
417         }
418         if (arg1[0] == ARG_IMM) {
419                 int64_t imm;
420                 imm = get_imm(&arg1[1]);
421                 if (unlikely(!imm_is_32bit(imm)))
422                         internal(file_line, "cgen_push: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
423                 if (imm >= -0x80 && imm <= 0x7f) {
424                         cgen_one(X86_PUSH_IMM8);
425                         cgen_one(imm);
426                         return true;
427                 } else {
428                         cgen_one(X86_PUSH_IMM16);
429                         cgen_four(imm);
430                         return true;
431                 }
432         }
433         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_FF, OP_SIZE_4, false, X86_FF_PUSH, arg1));
434         return true;
437 static bool attr_w cgen_pop(struct codegen_context *ctx)
439         uint8_t *arg1 = ctx->code_position;
440         ctx->code_position += arg_size(*arg1);
441         if (likely(R_IS_GPR(arg1[0]))) {
442                 if (arg1[0] & 8)
443                         cgen_rex(X86_REX | X86_REX_B);
444                 cgen_one(X86_POP_R16 + (arg1[0] & 7));
445                 return true;
446         }
447         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_8F, OP_SIZE_4, false, X86_8F_POP, arg1));
448         return true;
451 static bool attr_w cgen_mov(struct codegen_context *ctx, unsigned size)
453         uint8_t *arg1 = ctx->code_position;
454         uint8_t *arg2 = arg1 + arg_size(*arg1);
455         ctx->code_position = arg2 + arg_size(*arg2);
456         if (arg2[0] == ARG_IMM) {
457                 int rex = X86_REX;
458                 int64_t imm;
459                 imm = get_imm(&arg2[1]);
460                 if (R_IS_GPR(arg1[0])) {
461                         if (arg1[0] & 8)
462                                 rex |= X86_REX_B;
463 #ifndef ARCH_X86_32
464                         if (imm >= 0 && imm < 0x100000000LL)
465 #endif
466                         {
467                                 if (rex != X86_REX)
468                                         cgen_rex(rex);
469                                 cgen_one(X86_MOV_R16_IMM16 + (arg1[0] & 7));
470                                 cgen_four(imm);
471                                 return true;
472                         }
473                         if (imm >= ~(int64_t)0x7fffffff && imm < 0) {
474                                 goto mov_rm;
475                         }
476                         rex |= X86_REX_W;
477                         cgen_rex(rex);
478                         cgen_one(X86_MOV_R16_IMM16 + (arg1[0] & 7));
479                         cgen_eight(imm);
480                         return true;
481                 } else {
482                         if (size < OP_SIZE_4) {
483                                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_MOV_RM8_IMM8 : X86_MOV_RM16_IMM16, size, false, X86_MOV_R16_IMM16_REG, arg1));
484                                 if (size == OP_SIZE_1)
485                                         cgen_one(imm);
486                                 else
487                                         cgen_two(imm);
488                                 return true;
489                         }
490 mov_rm:
491                         if (unlikely(!imm_is_32bit(imm)))
492                                 internal(file_line, "cgen_mov: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
493                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_MOV_RM16_IMM16, maximum(size, OP_SIZE_4), false, X86_MOV_R16_IMM16_REG, arg1));
494                         cgen_four(imm);
495                         return true;
496                 }
497         }
498         if (arg1[0] == R_AX && size >= OP_SIZE_4 && arg2[0] == ARG_ADDRESS_0) {
499                 int64_t imm;
500                 imm = get_imm(&arg2[1]);
501                 if (size == OP_SIZE_8)
502                         cgen_rex(X86_REX | X86_REX_W);
503                 cgen_one(X86_MOV_AX_M16);
504                 cgen_eight(imm);
505                 return true;
506         }
507         if (arg1[0] == ARG_ADDRESS_0 && arg2[0] == R_AX) {
508                 uint8_t code = size == OP_SIZE_1 ? X86_MOV_M16_AL : X86_MOV_M16_AX;
509                 int64_t imm;
510                 imm = get_imm(&arg1[1]);
511                 if (size == OP_SIZE_2)
512                         cgen_one(X86_OP_SIZE);
513                 if (size == OP_SIZE_8)
514                         cgen_rex(X86_REX | X86_REX_W);
515                 cgen_one(code);
516                 cgen_eight(imm);
517                 return true;
518         }
519         if (R_IS_XMM(arg1[0]) && ARG_IS_ADDRESS(arg2[0])) {
520                 if (size == OP_SIZE_2) {
521                         g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F, X86_0F_PINSRW_X128_RM16_IMM8, false, arg1[0], R_XMM7, arg2));
522                         cgen_one(0x00);
523                         return true;
524                 }
525                 if (size == OP_SIZE_16) {
526                         g(cgen_sse_insn(ctx, SSE_PREFIX_NONE, PREFIX_0F, X86_0F_MOVAPS_X128_M128, false, arg1[0], 0, arg2));
527                         return true;
528                 }
529                 g(cgen_sse_insn(ctx, size == OP_SIZE_4 ? SSE_PREFIX_F3 : SSE_PREFIX_F2, PREFIX_0F, X86_0F_MOVSS_X128_M32, false, arg1[0], 0, arg2));
530                 return true;
531         }
532         if (ARG_IS_ADDRESS(arg1[0]) && R_IS_XMM(arg2[0])) {
533                 if (size == OP_SIZE_2) {
534                         g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F_3A, X86_0F_3A_PEXTRW_RM16_X128_IMM8, false, arg2[0], 0, arg1));
535                         cgen_one(0x00);
536                         return true;
537                 }
538                 if (size == OP_SIZE_16) {
539                         g(cgen_sse_insn(ctx, SSE_PREFIX_NONE, PREFIX_0F, X86_0F_MOVAPS_M128_X128, false, arg2[0], 0, arg1));
540                         return true;
541                 }
542                 g(cgen_sse_insn(ctx, size == OP_SIZE_4 ? SSE_PREFIX_F3 : SSE_PREFIX_F2, PREFIX_0F, X86_0F_MOVSS_M32_X128, false, arg2[0], 0, arg1));
543                 return true;
544         }
545         if (R_IS_XMM(arg1[0]) && R_IS_XMM(arg2[0])) {
546                 g(cgen_sse_insn(ctx, SSE_PREFIX_NONE, PREFIX_0F, X86_0F_MOVAPS_X128_M128, false, arg1[0], 0, arg2));
547                 return true;
548         }
549         if (R_IS_XMM(arg1[0]) && R_IS_GPR(arg2[0])) {
550                 g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F, X86_0F_MOVD_X128_RM32, size == OP_SIZE_8, arg1[0], 0, arg2));
551                 return true;
552         }
553         if (R_IS_GPR(arg1[0]) && R_IS_XMM(arg2[0])) {
554                 g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F, X86_0F_MOVD_RM32_X128, size == OP_SIZE_8, arg2[0], 0, arg1));
555                 return true;
556         }
557         if (!R_IS_GPR(arg1[0]) && unlikely(!R_IS_GPR(arg2[0]))) {
558                 /*debug("%s", da(ctx->fn,function)->function_name);*/
559                 internal(file_line, "cgen_mov: two addresses not supported");
560         }
561         if (!R_IS_GPR(arg1[0])) {
562                 uint8_t code = size == OP_SIZE_1 ? X86_MOV_RM8_R8 : X86_MOV_RM16_R16;
563                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, code, size, true, arg2[0], arg1));
564                 return true;
565         } else if (size >= OP_SIZE_4) {
566                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_MOV_R16_RM16, size, true, arg1[0], arg2));
567                 return true;
568         } else {
569                 uint8_t code = size == OP_SIZE_1 ? X86_0F_MOVZX_R16_RM8 : X86_0F_MOVZX_R16_RM16;
570                 g(cgen_rm_insn(ctx, -1, PREFIX_0F, code, OP_SIZE_4, false, arg1[0], arg2));
571                 return true;
572         }
575 static bool attr_w cgen_movsx(struct codegen_context *ctx, unsigned size)
577         uint8_t *arg1, *arg2;
578         if (unlikely(size == OP_SIZE_NATIVE)) {
579                 g(cgen_mov(ctx, size));
580                 return true;
581         }
582         arg1 = ctx->code_position;
583         arg2 = arg1 + arg_size(*arg1);
584         ctx->code_position = arg2 + arg_size(*arg2);
585         if (size <= OP_SIZE_2) {
586                 g(cgen_rm_insn(ctx, -1, PREFIX_0F, size == OP_SIZE_1 ? X86_0F_MOVSX_R16_RM8 : X86_0F_MOVSX_R16_RM16, OP_SIZE_NATIVE, true, arg1[0], arg2));
587         } else {
588                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_MOVSXD, OP_SIZE_NATIVE, true, arg1[0], arg2));
589         }
590         return true;
593 static bool attr_w cgen_lea(struct codegen_context *ctx, unsigned size, bool neg)
595         uint8_t addr[11];
596         uint8_t *arg1, *arg2, *arg3;
598         arg1 = ctx->code_position;
599         arg2 = arg1 + arg_size(*arg1);
600         arg3 = arg2 + arg_size(*arg2);
601         ctx->code_position = arg3 + arg_size(*arg3);
603         if (arg3[0] == ARG_IMM) {
604                 int64_t imm;
605                 if (arg2[0] == ARG_SHIFTED_REGISTER) {
606                         if (unlikely((arg2[1] & ARG_SHIFT_MODE) != ARG_SHIFT_LSL) ||
607                             unlikely((arg2[1] & ARG_SHIFT_AMOUNT) > 3))
608                                 goto invalid;
609                         addr[0] = ARG_ADDRESS_1 + (arg2[1] & ARG_SHIFT_AMOUNT);
610                         addr[1] = arg2[2];
611                 } else {
612                         addr[0] = ARG_ADDRESS_1;
613                         addr[1] = arg2[0];
614                 }
615                 imm = get_imm(&arg3[1]);
616                 if (neg) {
617                         imm = -(uint64_t)imm;
618                 }
619                 memcpy(&addr[2], &imm, 8);
620         } else if (R_IS_GPR(arg3[0])) {
621                 if (unlikely(neg))
622                         goto invalid;
623                 addr[0] = ARG_ADDRESS_2;
624                 addr[1] = arg2[0];
625                 addr[2] = arg3[0];
626                 memset(&addr[3], 0, 8);
627         } else if (arg3[0] == ARG_SHIFTED_REGISTER) {
628                 if (unlikely(neg))
629                         goto invalid;
630                 if (unlikely((arg3[1] & ARG_SHIFT_MODE) != ARG_SHIFT_LSL) ||
631                     unlikely((arg3[1] & ARG_SHIFT_AMOUNT) > 3))
632                         goto invalid;
633                 addr[0] = ARG_ADDRESS_2 + (arg3[1] & ARG_SHIFT_AMOUNT);
634                 addr[1] = arg2[0];
635                 addr[2] = arg3[2];
636                 memset(&addr[3], 0, 8);
637         } else {
638 invalid:
639                 internal(file_line, "cgen_lea: invalid argument %u, %02x, %02x, %02x, %u", size, arg1[0], arg2[0], arg3[0], (unsigned)neg);
640         }
641         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_LEA_R16_RM16, size, true, arg1[0], addr));
642         return true;
645 static bool attr_w cgen_alu(struct codegen_context *ctx, unsigned size, unsigned alu, unsigned writes_flags)
647         uint8_t *arg1, *arg2, *arg3;
648         if (alu != 7) {
649                 arg1 = ctx->code_position;
650                 arg2 = arg1 + arg_size(*arg1);
651                 arg3 = arg2 + arg_size(*arg2);
652                 ctx->code_position = arg3 + arg_size(*arg3);
653                 if (unlikely(arg_size(*arg1) != arg_size(*arg2)))
654                         internal(file_line, "cgen_alu: three-operand mode not supported");
655                 if (unlikely(memcmp(arg1, arg2, arg_size(*arg1))))
656                         internal(file_line, "cgen_alu: three-operand mode not supported");
657         } else {
658                 arg1 = ctx->code_position;
659                 arg2 = arg1;
660                 arg3 = arg2 + arg_size(*arg2);
661                 ctx->code_position = arg3 + arg_size(*arg3);
662         }
664         if (unlikely(alu == ALU_MUL)) {
665                 if (unlikely(arg3[0] == ARG_IMM)) {
666                         uint8_t code;
667                         int64_t imm;
668                         imm = get_imm(&arg3[1]);
669                         if (unlikely(!imm_is_32bit(imm)))
670                                 internal(file_line, "cgen_alu: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
671                         code = imm_is_8bit(imm) ? X86_IMUL_R16_RM16_IMM8 : X86_IMUL_R16_RM16_IMM16;
672                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, code, size, true, arg1[0], arg2));
673                         if (code == X86_IMUL_R16_RM16_IMM8) {
674                                 cgen_one(imm);
675                         } else if (size == OP_SIZE_2) {
676                                 cgen_two(imm);
677                         } else {
678                                 cgen_four(imm);
679                         }
680                         return true;
681                 }
682                 if (unlikely(size == OP_SIZE_1)) {
683                         if (unlikely(arg1[0] != R_AX))
684                                 internal(file_line, "cgen_alu: imul with unsupported register");
685                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_F6, size, false, X86_F6_IMUL_RM8, arg3));
686                         return true;
687                 } else {
688                         if (unlikely(!R_IS_GPR(arg1[0])))
689                                 internal(file_line, "cgen_alu: invalid multiply args");
690                         g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_IMUL_R16_RM16, size, true, arg1[0], arg3));
691                         return true;
692                 }
693         }
695         if (arg3[0] == ARG_IMM) {
696                 uint8_t code;
697                 bool bit8;
698                 int64_t imm;
699                 imm = get_imm(&arg3[1]);
700                 if (unlikely(!imm_is_32bit(imm)))
701                         internal(file_line, "cgen_alu: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
703                 if (!(writes_flags & 2)) {
704                         if ((alu == ALU_ADD && imm == 1) || (alu == ALU_SUB && imm == -1)) {
705 #ifdef ARCH_X86_32
706                                 if (R_IS_GPR(arg1[0]) && size >= OP_SIZE_2) {
707                                         if (size == OP_SIZE_2)
708                                                 cgen_one(X86_OP_SIZE);
709                                         cgen_one(X86_INC_R16 + arg1[0]);
710                                         return true;
711                                 }
712 #endif
713                                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_FE : X86_FF, size, false, X86_FE_INC_RM8, arg1));
714                                 return true;
715                         }
716                         if ((alu == ALU_ADD && imm == -1) || (alu == ALU_SUB && imm == 1)) {
717 #ifdef ARCH_X86_32
718                                 if (R_IS_GPR(arg1[0]) && size >= OP_SIZE_2) {
719                                         if (size == OP_SIZE_2)
720                                                 cgen_one(X86_OP_SIZE);
721                                         cgen_one(X86_DEC_R16 + arg1[0]);
722                                         return true;
723                                 }
724 #endif
725                                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_FE : X86_FF, size, false, X86_FE_DEC_RM8, arg1));
726                                 return true;
727                         }
728                 }
730                 if (arg1[0] == R_AX) {
731                         if (imm_is_8bit(imm) && size >= OP_SIZE_4)
732                                 goto use_shorter;
733                         bit8 = false;
734                         code = size == OP_SIZE_1 ? X86_ALU_AL_IMM8 : X86_ALU_AX_IMM16;
735                         if (size == OP_SIZE_2)
736                                 cgen_one(X86_OP_SIZE);
737                         if (size == OP_SIZE_8)
738                                 cgen_rex(X86_REX | X86_REX_W);
739                         code += alu * 8;
740                         cgen_one(code);
741                 } else {
742 use_shorter:
743                         bit8 = imm_is_8bit(imm);
744                         code = size == OP_SIZE_1 ? X86_ALU_RM8_IMM8 : bit8 ? X86_ALU_RM16_IMM8 : X86_ALU_RM16_IMM16;
745                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, code, size, false, alu, arg1));
746                 }
747                 if (bit8 || size == OP_SIZE_1) {
748                         cgen_one(imm);
749                 } else if (size == OP_SIZE_2) {
750                         cgen_two(imm);
751                 } else {
752                         cgen_four(imm);
753                 }
754                 return true;
755         }
757         if (R_IS_XMM(arg1[0]) && size == OP_SIZE_16) {
758                 uint8_t code;
759                 switch (alu) {
760                         case ALU_AND:   code = X86_0F_ANDPS_X128_M128; break;
761                         case ALU_ANDN:  code = X86_0F_ANDNPS_X128_M128; break;
762                         case ALU_OR:    code = X86_0F_ORPS_X128_M128; break;
763                         case ALU_XOR:   code = X86_0F_XORPS_X128_M128; break;
764                         default:        internal(file_line, "invalid sse alu: %u", alu);
765                 }
766                 g(cgen_sse_insn(ctx, SSE_PREFIX_NONE, PREFIX_0F, code, false, arg1[0], arg2[0], arg3));
767                 return true;
768         }
770         if (!R_IS_GPR(arg1[0]) && unlikely(!R_IS_GPR(arg3[0])))
771                 internal(file_line, "cgen_alu: two addresses not supported");
773         if (!R_IS_GPR(arg1[0])) {
774                 uint8_t code = size == OP_SIZE_1 ? X86_ALU_RM8_R8 : X86_ALU_RM16_R16;
775                 code += alu * 8;
776                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, code, size, true, arg3[0], arg1));
777                 return true;
778         } else {
779                 uint8_t code = size == OP_SIZE_1 ? X86_ALU_R8_RM8 : X86_ALU_R16_RM16;
780                 code += alu * 8;
781                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, code, size, true, arg1[0], arg3));
782                 return true;
783         }
786 static bool attr_w cgen_alu1(struct codegen_context *ctx, unsigned size, unsigned alu)
788         uint8_t rex;
789         uint8_t *arg1 = ctx->code_position;
790         uint8_t *arg2 = arg1 + arg_size(*arg1);
791         ctx->code_position = arg2 + arg_size(*arg2);
792         if (alu == ALU1_NOT || alu == ALU1_NEG || alu == ALU1_BSWAP) {
793                 if (unlikely(arg1[0] != arg2[0]))
794                         internal(file_line, "cgen_alu1: arguments mismatch: %x, %x", arg1[0], arg2[0]);
795         }
796         switch (alu) {
797                 case ALU1_NOT:
798                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_F6 : X86_F7, size, false, X86_F6_NOT_RM8, arg1));
799                         return true;
800                 case ALU1_NEG:
801                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_F6 : X86_F7, size, false, X86_F6_NEG_RM8, arg1));
802                         return true;
803                 case ALU1_BSWAP:
804                         if (unlikely(size <= OP_SIZE_2))
805                                 internal(file_line, "cgen_alu1: bytes or words not supported with this operation");
806                         rex = X86_REX;
807                         if (!R_IS_GPR(arg1[0]))
808                                 internal(file_line, "cgen_alu1: bswap needs a register");
809                         if (arg1[0] & 8)
810                                 rex |= X86_REX_B;
811                         if (size == OP_SIZE_8)
812                                 rex |= X86_REX_W;
813                         if (rex != X86_REX)
814                                 cgen_rex(rex);
815                         cgen_one(X86_0F);
816                         cgen_one(X86_0F_BSWAP + (arg1[0] & 7));
817                         return true;
818                 case ALU1_BSF:
819                 case ALU1_BSR:
820                 case ALU1_LZCNT:
821                 case ALU1_POPCNT:
822                         if (unlikely(size == OP_SIZE_1))
823                                 internal(file_line, "cgen_alu1: bytes not supported with this operation");
824                         if (alu == ALU1_POPCNT || alu == ALU1_LZCNT)
825                                 cgen_one(X86_REPE);
826                         g(cgen_rm_insn(ctx, -1, PREFIX_0F, alu == ALU1_BSF ? X86_0F_BSF_R16_RM16 : alu == ALU1_BSR || alu == ALU1_LZCNT ? X86_0F_BSR_R16_RM16 : X86_0F_POPCNT_R16_RM16, size, true, arg1[0], arg2));
827                         return true;
828                 default:
829                         internal(file_line, "cgen_alu1: invalid operation %u", alu);
830                         return false;
831         }
834 static bool attr_w cgen_test(struct codegen_context *ctx, unsigned size)
836         uint8_t *arg1, *arg2;
837         arg1 = ctx->code_position;
838         arg2 = arg1 + arg_size(*arg1);
839         ctx->code_position = arg2 + arg_size(*arg2);
841         if (arg2[0] == ARG_IMM) {
842                 int64_t imm;
843                 imm = get_imm(&arg2[1]);
844                 if (arg1[0] == R_AX) {
845                         if (size == OP_SIZE_1) {
846                                 cgen_one(X86_TEST_AL_IMM8);
847                         } else if (size == OP_SIZE_2) {
848                                 cgen_one(X86_OP_SIZE);
849                                 cgen_one(X86_TEST_AX_IMM16);
850                         } else if (size == OP_SIZE_4) {
851                                 cgen_one(X86_TEST_AX_IMM16);
852                         } else {
853                                 if (unlikely(!imm_is_32bit(imm)))
854                                         internal(file_line, "cgen_Test: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
855                                 cgen_rex(X86_REX | X86_REX_W);
856                                 cgen_one(X86_TEST_AX_IMM16);
857                         }
858                 } else {
859                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_F6 : X86_F7, size, false, X86_F6_TEST_RM8_IMM8, arg1));
860                 }
861                 if (size == OP_SIZE_1) {
862                         cgen_one(imm);
863                 } else if (size == OP_SIZE_2) {
864                         cgen_two(imm);
865                 } else if (size == OP_SIZE_4) {
866                         cgen_four(imm);
867                 } else {
868                         if (unlikely(!imm_is_32bit(imm)))
869                                 internal(file_line, "cgen_Test: immediate out of range: %"PRIxMAX"", (uintmax_t)imm);
870                         cgen_four(imm);
871                 }
872                 return true;
873         }
875         if (!R_IS_GPR(arg1[0]) && unlikely(!R_IS_GPR(arg2[0])))
876                 internal(file_line, "cgen_test: two addresses not supported");
878         if (!R_IS_GPR(arg1[0])) {
879                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_TEST_RM8_R8 : X86_TEST_RM16_R16, size, true, arg2[0], arg1));
880         } else {
881                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_TEST_RM8_R8 : X86_TEST_RM16_R16, size, true, arg1[0], arg2));
882         }
883         return true;
886 static bool attr_w cgen_lea3(struct codegen_context *ctx, unsigned size, unsigned shift)
888         uint8_t addr[11];
889         uint8_t *arg1, *arg2, *arg3, *arg4;
891         arg1 = ctx->code_position;
892         arg2 = arg1 + arg_size(*arg1);
893         arg3 = arg2 + arg_size(*arg2);
894         arg4 = arg3 + arg_size(*arg3);
895         ctx->code_position = arg4 + arg_size(*arg4);
897         if (unlikely(!R_IS_GPR(arg1[0])) || unlikely(!R_IS_GPR(arg2[0])) || unlikely(!R_IS_GPR(arg3[0])) || unlikely(arg4[0] != ARG_IMM))
898                 internal(file_line, "cgen_lea3: invalid arguments");
900         addr[0] = ARG_ADDRESS_2 + shift;
901         addr[1] = arg2[0];
902         addr[2] = arg3[0];
903         memcpy(&addr[3], &arg4[1], 8);
905         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_LEA_R16_RM16, size, true, arg1[0], addr));
907         return true;
910 static bool attr_w cgen_rot(struct codegen_context *ctx, unsigned size, uint8_t rot, unsigned writes_flags)
912         uint8_t *arg1 = ctx->code_position;
913         uint8_t *arg2 = arg1 + arg_size(*arg1);
914         uint8_t *arg3 = arg2 + arg_size(*arg2);
915         ctx->code_position = arg3 + arg_size(*arg3);
917         if (cpu_test_feature(CPU_FEATURE_bmi2) && size >= OP_SIZE_4 && arg3[0] != ARG_IMM && !writes_flags && (rot == ROT_SHL || rot == ROT_SHR || rot == ROT_SAR)) {
918                 int32_t sse_prefix;
919                 switch (rot) {
920                         case ROT_SHL:   sse_prefix = SSE_PREFIX_66; break;
921                         case ROT_SAR:   sse_prefix = SSE_PREFIX_F3; break;
922                         case ROT_SHR:   sse_prefix = SSE_PREFIX_F2; break;
923                         default:        internal(file_line, "cgen_rot: invalid rotation %x", rot);
924                 }
925                 g(cgen_sse_insn(ctx, sse_prefix + force_vex, PREFIX_0F_38, X86_0F_38_ROTX, size == OP_SIZE_8, arg1[0], arg3[0], arg2));
926                 return true;
927         }
929         if (arg1[0] != arg2[0])
930                 internal(file_line, "cgen_rot: invalid arguments: %x, %02x, %02x, %02x", rot, arg1[0], arg2[0], arg3[0]);
932         if (arg3[0] == R_CX) {
933                 g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_ROT_RM8_CL : X86_ROT_RM16_CL, size, false, rot, arg1));
934         } else if (likely(arg3[0] == ARG_IMM)) {
935                 int64_t imm;
936                 imm = get_imm(&arg3[1]);
937                 if (imm == 1) {
938                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_ROT_RM8_1 : X86_ROT_RM16_1, size, false, rot, arg1));
939                 } else {
940                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_ROT_RM8_IMM8 : X86_ROT_RM16_IMM8, size, false, rot, arg1));
941                         cgen_one(imm);
942                 }
943         } else {
944                 internal(file_line, "cgen_rot: invalid argument %02x", arg3[0]);
945         }
946         return true;
949 static bool attr_w cgen_btxt(struct codegen_context *ctx, unsigned size, uint8_t bt, uint8_t *arg1, uint8_t *arg2)
951         if (arg2[0] == ARG_IMM) {
952                 g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_BTX_RM16_IMM8, size, false, X86_0F_BTX_BT_RM16_IMM8 + bt, arg1));
953                 cgen_one(arg2[1]);
954         } else {
955                 g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_BT_RM16_R16 + bt * 8, size, true, arg2[0], arg1));
956         }
957         return true;
960 static bool attr_w cgen_bt(struct codegen_context *ctx, unsigned size)
962         uint8_t *arg1 = ctx->code_position;
963         uint8_t *arg2 = arg1 + arg_size(*arg1);
964         ctx->code_position = arg2 + arg_size(*arg2);
966         return cgen_btxt(ctx, size, BTX_BT, arg1, arg2);
969 static bool attr_w cgen_btx(struct codegen_context *ctx, unsigned size, uint8_t bt)
971         uint8_t *arg1 = ctx->code_position;
972         uint8_t *arg2 = arg1 + arg_size(*arg1);
973         uint8_t *arg3 = arg2 + arg_size(*arg2);
974         ctx->code_position = arg3 + arg_size(*arg3);
976         if (arg1[0] != arg2[0])
977                 internal(file_line, "cgen_btx: invalid arguments");
979         return cgen_btxt(ctx, size, bt, arg1, arg3);
982 static bool attr_w cgen_mul_l(struct codegen_context *ctx, unsigned size, bool sgn)
984         uint8_t *arg1, *arg2, *arg3, *arg4;
985         uint32_t reg_up;
986         arg1 = ctx->code_position;
987         arg2 = arg1 + arg_size(*arg1);
988         arg3 = arg2 + arg_size(*arg2);
989         arg4 = arg3 + arg_size(*arg3);
990         ctx->code_position = arg4 + arg_size(*arg4);
991         reg_up = size == OP_SIZE_1 ? R_AX : R_DX;
992         if (unlikely(arg1[0] != R_AX) || unlikely(arg2[0] != reg_up) || unlikely(arg3[0] != R_AX) || unlikely(arg4[0] == ARG_IMM))
993                 internal(file_line, "cgen_mul_l: invalid mul arguments");
995         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_F6 : X86_F7, size, false, !sgn ? X86_F6_MUL_RM8 : X86_F6_IMUL_RM8, arg4));
996         return true;
999 static bool attr_w cgen_div_l(struct codegen_context *ctx, unsigned size, bool sgn)
1001         uint8_t *arg1, *arg2, *arg3, *arg4, *arg5;
1002         uint32_t reg_up;
1003         arg1 = ctx->code_position;
1004         arg2 = arg1 + arg_size(*arg1);
1005         arg3 = arg2 + arg_size(*arg2);
1006         arg4 = arg3 + arg_size(*arg3);
1007         arg5 = arg4 + arg_size(*arg4);
1008         ctx->code_position = arg5 + arg_size(*arg5);
1009         reg_up = size == OP_SIZE_1 ? R_AX : R_DX;
1010         if (unlikely(arg1[0] != R_AX) || unlikely(arg2[0] != reg_up) || unlikely(arg3[0] != R_AX) || unlikely(arg4[0] != reg_up) || unlikely(arg5[0] == ARG_IMM))
1011                 internal(file_line, "cgen_div_l: invalid div arguments");
1013         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, size == OP_SIZE_1 ? X86_F6 : X86_F7, size, false, !sgn ? X86_F6_DIV_RM8 : X86_F6_IDIV_RM8, arg5));
1014         return true;
1017 static bool attr_w cgen_cmov(struct codegen_context *ctx, unsigned size, unsigned cond)
1019         uint8_t *arg1 = ctx->code_position;
1020         uint8_t *arg2 = arg1 + arg_size(*arg1);
1021         uint8_t *arg3 = arg2 + arg_size(*arg2);
1022         ctx->code_position = arg3 + arg_size(*arg3);
1023         if (unlikely(arg1[0] != arg2[0]))
1024                 internal(file_line, "cgen_cmov: invalid arguments");
1025         g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_CMOVCC_R16_RM16 + cond, size, true, arg1[0], arg3));
1026         return true;
1029 static bool attr_w cgen_memcpy(struct codegen_context *ctx)
1031         int64_t disp_dest, disp_src;
1032         uint8_t *arg1, *arg2, *arg3;
1033         arg1 = ctx->code_position;
1034         arg2 = arg1 + arg_size(*arg1);
1035         arg3 = arg2 + arg_size(*arg2);
1036         ctx->code_position = arg3 + arg_size(*arg3);
1037         if (unlikely(arg1[0] != ARG_ADDRESS_1_POST_I) || unlikely(arg2[0] != ARG_ADDRESS_1_POST_I) || unlikely(arg3[0] != R_CX))
1038                 goto invl;
1039         if (unlikely(arg1[1] != R_DI) || unlikely(arg2[1] != R_SI))
1040                 goto invl;
1041         disp_dest = get_imm(&arg1[2]);
1042         disp_src = get_imm(&arg2[2]);
1043         if (unlikely(disp_dest != 0) || unlikely(disp_src != 0))
1044                 goto invl;
1046         cgen_one(X86_REPE);
1047         cgen_one(X86_MOVSB);
1048         return true;
1050 invl:
1051         internal(file_line, "cgen_memcpy: invalid arguments %02x, %02x, %02x", *arg1, *arg2, *arg3);
1052         return false;
1055 static bool attr_w cgen_memset(struct codegen_context *ctx)
1057         int64_t disp_dest;
1058         uint8_t *arg1, *arg2, *arg3;
1059         arg1 = ctx->code_position;
1060         arg2 = arg1 + arg_size(*arg1);
1061         arg3 = arg2 + arg_size(*arg2);
1062         ctx->code_position = arg3 + arg_size(*arg3);
1063         if (unlikely(arg1[0] != ARG_ADDRESS_1_POST_I) || unlikely(arg2[0] != R_CX) || unlikely(arg3[0] != R_AX))
1064                 goto invl;
1065         if (unlikely(arg1[1] != R_DI))
1066                 goto invl;
1067         disp_dest = get_imm(&arg1[2]);
1068         if (unlikely(disp_dest != 0))
1069                 goto invl;
1071         cgen_one(X86_REPE);
1072         cgen_one(X86_STOSB);
1073         return true;
1075 invl:
1076         internal(file_line, "cgen_memset: invalid arguments %02x, %02x, %02x", *arg1, *arg2, *arg3);
1077         return false;
1080 static bool attr_w cgen_sse_cmp(struct codegen_context *ctx, unsigned size)
1082         uint8_t *arg1 = ctx->code_position;
1083         uint8_t *arg2 = arg1 + arg_size(*arg1);
1084         ctx->code_position = arg2 + arg_size(*arg2);
1085         g(cgen_sse_insn(ctx, size == OP_SIZE_4 ? SSE_PREFIX_NONE: SSE_PREFIX_66, PREFIX_0F, X86_0F_UCOMISS_X128_RM32, false, arg1[0], 0, arg2));
1086         return true;
1089 static bool attr_w cgen_sse_alu(struct codegen_context *ctx, unsigned size, unsigned alu)
1091         uint8_t opcode;
1092         uint8_t *arg1 = ctx->code_position;
1093         uint8_t *arg2 = arg1 + arg_size(*arg1);
1094         uint8_t *arg3 = arg2 + arg_size(*arg2);
1095         ctx->code_position = arg3 + arg_size(*arg3);
1096         switch (alu) {
1097                 case FP_ALU_ADD:        opcode = X86_0F_ADDPS_X128_M32; break;
1098                 case FP_ALU_SUB:        opcode = X86_0F_SUBPS_X128_M32; break;
1099                 case FP_ALU_MUL:        opcode = X86_0F_MULPS_X128_M32; break;
1100                 case FP_ALU_DIV:        opcode = X86_0F_DIVPS_X128_M32; break;
1101                 default:                internal(file_line, "cgen_sse_alu: invalid alu %u", alu);
1102         }
1103         g(cgen_sse_insn(ctx, size == OP_SIZE_4 ? SSE_PREFIX_F3 : SSE_PREFIX_F2, PREFIX_0F, opcode, false, arg1[0], arg2[0], arg3));
1104         return true;
1107 static bool attr_w cgen_sse_alu1(struct codegen_context *ctx, unsigned size, unsigned alu)
1109         uint8_t opcode;
1110         unsigned sse_pfx, sse_op_map;
1111         uint8_t *arg1 = ctx->code_position;
1112         uint8_t *arg2 = arg1 + arg_size(*arg1);
1113         ctx->code_position = arg2 + arg_size(*arg2);
1114         switch (alu) {
1115                 case FP_ALU1_SQRT:      if (size == OP_SIZE_4) {
1116                                                 sse_pfx = SSE_PREFIX_F3;
1117                                         } else if (size == OP_SIZE_8) {
1118                                                 sse_pfx = SSE_PREFIX_F2;
1119                                         } else {
1120                                                 goto fail;
1121                                         }
1122                                         sse_op_map = PREFIX_0F;
1123                                         opcode = X86_0F_SQRTPS_X128_M32;
1124                                         break;
1125                 case FP_ALU1_ROUND:
1126                 case FP_ALU1_FLOOR:
1127                 case FP_ALU1_CEIL:
1128                 case FP_ALU1_TRUNC:     sse_pfx = SSE_PREFIX_66;
1129                                         sse_op_map = PREFIX_0F_3A;
1130                                         if (size == OP_SIZE_4) {
1131                                                 opcode = X86_0F_3A_ROUNDSS_X128_M32;
1132                                         } else if (size == OP_SIZE_8) {
1133                                                 opcode = X86_0F_3A_ROUNDSD_X128_M64;
1134                                         } else {
1135                                                 goto fail;
1136                                         }
1137                                         break;
1138                 fail:
1139                 default:                internal(file_line, "cgen_sse_alu1: invalid alu %u, %u", alu, size);
1140         }
1141         g(cgen_sse_insn(ctx, sse_pfx, sse_op_map, opcode, false, arg1[0], arg1[0], arg2));
1142         if (OP_IS_ROUND(alu))
1143                 cgen_one(alu - FP_ALU1_ROUND);
1144         return true;
1147 static bool attr_w cgen_sse_from_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1149         uint8_t *arg1 = ctx->code_position;
1150         uint8_t *arg2 = arg1 + arg_size(*arg1);
1151         ctx->code_position = arg2 + arg_size(*arg2);
1152         g(cgen_sse_insn(ctx, fp_op_size == OP_SIZE_4 ? SSE_PREFIX_F3 : SSE_PREFIX_F2, PREFIX_0F, X86_0F_CVTSI2SS_X128_RM32, int_op_size == OP_SIZE_8, arg1[0], R_XMM7, arg2));
1153         return true;
1156 static bool attr_w cgen_sse_to_int(struct codegen_context *ctx, unsigned int_op_size, unsigned fp_op_size)
1158         uint8_t *arg1 = ctx->code_position;
1159         uint8_t *arg2 = arg1 + arg_size(*arg1);
1160         ctx->code_position = arg2 + arg_size(*arg2);
1161         g(cgen_sse_insn(ctx, fp_op_size == OP_SIZE_4 ? SSE_PREFIX_F3 : SSE_PREFIX_F2, PREFIX_0F, X86_0F_CVTTSS2SI_X128_RM32, int_op_size == OP_SIZE_8, arg1[0], 0, arg2));
1162         return true;
1165 static bool attr_w cgen_sse_cvt(struct codegen_context *ctx, unsigned from_op_size, unsigned to_op_size)
1167         uint8_t *arg1 = ctx->code_position;
1168         uint8_t *arg2 = arg1 + arg_size(*arg1);
1169         ctx->code_position = arg2 + arg_size(*arg2);
1170         if (from_op_size == OP_SIZE_2 && to_op_size == OP_SIZE_4) {
1171                 g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F_38, X86_0F_38_CVTPH2PS_X128_RM64, false, arg1[0], 0, arg2));
1172                 return true;
1173         } else if (from_op_size == OP_SIZE_4 && to_op_size == OP_SIZE_2) {
1174                 g(cgen_sse_insn(ctx, SSE_PREFIX_66, PREFIX_0F_3A, X86_0F_3A_CVTPS2PH_RM64_X128, false, arg2[0], 0, arg1));
1175                 cgen_one(0x04);
1176                 return true;
1177         }
1178         internal(file_line, "cgen_sse_cvt: unsupported arguments %u, %u", from_op_size, to_op_size);
1179         return false;
1182 static bool attr_w cgen_x87_fld(struct codegen_context *ctx, unsigned size)
1184         unsigned c1, c2;
1185         uint8_t *arg1 = ctx->code_position;
1186         ctx->code_position = arg1 + arg_size(*arg1);
1187         if (arg1[0] >= R_ST0 && arg1[0] <= R_ST7)
1188                 size = OP_SIZE_4;
1189         switch (size) {
1190                 case OP_SIZE_4:
1191                         c1 = X87_FLD_RM32; c2 = X87_FLD_RM32_X; break;
1192                 case OP_SIZE_8:
1193                         c1 = X87_FLD_M64; c2 = X87_FLD_M64_X; break;
1194                 case OP_SIZE_10:
1195                         c1 = X87_FLD_M80; c2 = X87_FLD_M80_X; break;
1196                 default:
1197                         internal(file_line, "cgen_x87_fld: invalid size %u", size);
1198         }
1199         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1200         return true;
1203 static bool attr_w cgen_x87_fild(struct codegen_context *ctx, unsigned size)
1205         unsigned c1, c2;
1206         uint8_t *arg1 = ctx->code_position;
1207         ctx->code_position = arg1 + arg_size(*arg1);
1208         switch (size) {
1209                 case OP_SIZE_2:
1210                         c1 = X87_FILD_M16; c2 = X87_FILD_M16_X; break;
1211                 case OP_SIZE_4:
1212                         c1 = X87_FILD_M32; c2 = X87_FILD_M32_X; break;
1213                 case OP_SIZE_8:
1214                         c1 = X87_FILD_M64; c2 = X87_FILD_M64_X; break;
1215                 default:
1216                         internal(file_line, "cgen_x87_fild: invalid size %u", size);
1217         }
1218         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1219         return true;
1222 static bool attr_w cgen_x87_fstp(struct codegen_context *ctx, unsigned size)
1224         unsigned c1, c2;
1225         uint8_t *arg1 = ctx->code_position;
1226         ctx->code_position = arg1 + arg_size(*arg1);
1227         if (arg1[0] >= R_ST0 && arg1[0] <= R_ST7)
1228                 size = OP_SIZE_8;
1229         switch (size) {
1230                 case OP_SIZE_4:
1231                         c1 = X87_FSTP_M32; c2 = X87_FSTP_M32_X; break;
1232                 case OP_SIZE_8:
1233                         c1 = X87_FSTP_RM64; c2 = X87_FSTP_RM64_X; break;
1234                 case OP_SIZE_10:
1235                         c1 = X87_FSTP_M80; c2 = X87_FSTP_M80_X; break;
1236                 default:
1237                         internal(file_line, "cgen_x87_fstp: invalid size %u", size);
1238         }
1239         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1240         return true;
1243 static bool attr_w cgen_x87_fistp(struct codegen_context *ctx, unsigned size)
1245         unsigned c1, c2;
1246         uint8_t *arg1 = ctx->code_position;
1247         ctx->code_position = arg1 + arg_size(*arg1);
1248         switch (size) {
1249                 case OP_SIZE_2:
1250                         c1 = X87_FISTP_M16; c2 = X87_FISTP_M16_X; break;
1251                 case OP_SIZE_4:
1252                         c1 = X87_FISTP_M32; c2 = X87_FISTP_M32_X; break;
1253                 case OP_SIZE_8:
1254                         c1 = X87_FISTP_M64; c2 = X87_FISTP_M64_X; break;
1255                 default:
1256                         internal(file_line, "cgen_x87_fistp: invalid size %u", size);
1257         }
1258         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1259         return true;
1262 static bool attr_w cgen_x87_fisttp(struct codegen_context *ctx, unsigned size)
1264         unsigned c1, c2;
1265         uint8_t *arg1 = ctx->code_position;
1266         ctx->code_position = arg1 + arg_size(*arg1);
1267         switch (size) {
1268                 case OP_SIZE_2:
1269                         c1 = X87_FISTTP_M16; c2 = X87_FISTTP_M16_X; break;
1270                 case OP_SIZE_4:
1271                         c1 = X87_FISTTP_M32; c2 = X87_FISTTP_M32_X; break;
1272                 case OP_SIZE_8:
1273                         c1 = X87_FISTTP_M64; c2 = X87_FISTTP_M64_X; break;
1274                 default:
1275                         internal(file_line, "cgen_x87_fisttp: invalid size %u", size);
1276         }
1277         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1278         return true;
1281 static bool attr_w cgen_x87_fcomp(struct codegen_context *ctx, unsigned size)
1283         unsigned c1, c2;
1284         uint8_t *arg1 = ctx->code_position;
1285         ctx->code_position = arg1 + arg_size(*arg1);
1286         if (arg1[0] < ARG_REGS_MAX) {
1287                 c1 = X87_FALU_ST_RM32;
1288         } else switch (size) {
1289                 case OP_SIZE_4:
1290                         c1 = X87_FALU_ST_RM32; break;
1291                 case OP_SIZE_8:
1292                         c1 = X87_FALU_ST_M64; break;
1293                 default:
1294                         internal(file_line, "cgen_x87_fcomp: invalid size %u", size);
1295         }
1296         c2 = X87_ALU_FCOMP;
1297         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1298         return true;
1301 static bool attr_w cgen_x87_alu(struct codegen_context *ctx, unsigned size, unsigned aux)
1303         unsigned c1, c2;
1304         uint8_t *arg1 = ctx->code_position;
1305         ctx->code_position = arg1 + arg_size(*arg1);
1306         if (arg1[0] < ARG_REGS_MAX) {
1307                 c1 = X87_FALU_ST_RM32;
1308         } else switch (size) {
1309                 case OP_SIZE_4:
1310                         c1 = X87_FALU_ST_RM32; break;
1311                 case OP_SIZE_8:
1312                         c1 = X87_FALU_ST_M64; break;
1313                 default:
1314                         internal(file_line, "cgen_x87_alu: invalid size %u", size);
1315         }
1316         switch (aux) {
1317                 case FP_ALU_ADD:
1318                         c2 = X87_ALU_ADD; break;
1319                 case FP_ALU_SUB:
1320                         c2 = X87_ALU_SUB; break;
1321                 case FP_ALU_MUL:
1322                         c2 = X87_ALU_MUL; break;
1323                 case FP_ALU_DIV:
1324                         c2 = X87_ALU_DIV; break;
1325                 default:
1326                         internal(file_line, "cgen_x87_fst: invalid operation %u", aux);
1327         }
1328         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, c1, OP_SIZE_4, false, c2, arg1));
1329         return true;
1332 static bool attr_w cgen_x87_alup(struct codegen_context *ctx, unsigned aux)
1334         unsigned c2;
1335         uint8_t *arg1 = ctx->code_position;
1336         ctx->code_position = arg1 + arg_size(*arg1);
1337         switch (aux) {
1338                 case FP_ALU_ADD:
1339                         c2 = X87_ALU_ADD; break;
1340                 case FP_ALU_SUB:
1341                         c2 = X87_ALU_SUB; break;
1342                 case FP_ALU_MUL:
1343                         c2 = X87_ALU_MUL; break;
1344                 case FP_ALU_DIV:
1345                         c2 = X87_ALU_DIV; break;
1346                 default:
1347                         internal(file_line, "cgen_x87_fstp: invalid operation %u", aux);
1348         }
1349         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X87_FALUP_STi_ST0, OP_SIZE_4, false, c2, arg1));
1350         return true;
1353 static bool attr_w resolve_relocation(struct codegen_context *ctx, struct relocation *reloc)
1355         int64_t offs = (int64_t)ctx->label_to_pos[reloc->label_id] - (int64_t)(reloc->position + (reloc->length == JMP_SHORT ? 1 : 4));
1356         switch (reloc->length) {
1357                 case JMP_SHORT: {
1358                         int8_t i8;
1359                         if (!imm_is_8bit(offs))
1360                                 return false;
1361                         i8 = offs;
1362                         memcpy(ctx->mcode + reloc->position, &i8, 1);
1363                         return true;
1364                 }
1365                 case JMP_LONG: {
1366                         int32_t i32;
1367                         if (!imm_is_32bit(offs))
1368                                 return false;
1369                         i32 = offs;
1370                         memcpy(ctx->mcode + reloc->position, &i32, 4);
1371                         return true;
1372                 }
1373                 default: {
1374                         internal(file_line, "resolve_relocation: invalid relocation length %u", reloc->length);
1375                         return false;
1376                 }
1377         }
1380 static bool attr_w cgen_insn(struct codegen_context *ctx, uint32_t insn)
1382         uint8_t imm8;
1383         uint16_t imm16;
1384         /*debug("insn: %08x", insn);*/
1385         switch (insn_opcode(insn)) {
1386                 case INSN_ENTRY:
1387                         g(cgen_entry(ctx));
1388                         return true;
1389                 case INSN_LABEL:
1390                         g(cgen_label(ctx));
1391                         return true;
1392                 case INSN_RET:
1393                         cgen_one(X86_RET);
1394                         return true;
1395                 case INSN_RET_IMM:
1396                         imm16 = cget_two(ctx);
1397                         cgen_one(X86_RET_IMM16);
1398                         cgen_two(imm16);
1399                         return true;
1400                 case INSN_PUSH:
1401                         g(cgen_push(ctx));
1402                         return true;
1403                 case INSN_POP:
1404                         g(cgen_pop(ctx));
1405                         return true;
1406                 case INSN_CALL_INDIRECT:
1407                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_FF, OP_SIZE_4, false, X86_FF_CALL_INDIRECT, ctx->code_position));
1408                         ctx->code_position += arg_size(*ctx->code_position);
1409                         return true;
1410                 case INSN_MOV:
1411                         g(cgen_mov(ctx, insn_op_size(insn)));
1412                         return true;
1413                 case INSN_MOVSX:
1414                         g(cgen_movsx(ctx, insn_op_size(insn)));
1415                         return true;
1416                 case INSN_CMP:
1417                         g(cgen_alu(ctx, insn_op_size(insn), 7, insn_writes_flags(insn)));
1418                         return true;
1419                 case INSN_TEST:
1420                         g(cgen_test(ctx, insn_op_size(insn)));
1421                         return true;
1422                 case INSN_ALU:
1423                 case INSN_ALU_FLAGS:
1424                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1425                                 goto invalid_insn;
1426                         if (!insn_writes_flags(insn) && insn_op_size(insn) <= OP_SIZE_8) {
1427                                 if (unlikely(insn_aux(insn) != ALU_ADD) && unlikely(insn_aux(insn) != ALU_SUB))
1428                                         goto invalid_insn;
1429                                 g(cgen_lea(ctx, insn_op_size(insn), insn_aux(insn) == ALU_SUB));
1430                                 return true;
1431                         }
1432                         g(cgen_alu(ctx, insn_op_size(insn), insn_aux(insn), insn_writes_flags(insn)));
1433                         return true;
1434                 case INSN_ALU_PARTIAL:
1435                 case INSN_ALU_FLAGS_PARTIAL:
1436                         if (unlikely(insn_op_size(insn) >= OP_SIZE_4))
1437                                 goto invalid_insn;
1438                         g(cgen_alu(ctx, insn_op_size(insn), insn_aux(insn), insn_writes_flags(insn)));
1439                         return true;
1440                 case INSN_ALU1:
1441                 case INSN_ALU1_FLAGS:
1442                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1443                                 goto invalid_insn;
1444                         g(cgen_alu1(ctx, insn_op_size(insn), insn_aux(insn)));
1445                         return true;
1446                 case INSN_ALU1_PARTIAL:
1447                 case INSN_ALU1_FLAGS_PARTIAL:
1448                         if (unlikely(insn_op_size(insn) >= OP_SIZE_4))
1449                                 goto invalid_insn;
1450                         g(cgen_alu1(ctx, insn_op_size(insn), insn_aux(insn)));
1451                         return true;
1452                 case INSN_LEA3:
1453                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1454                                 goto invalid_insn;
1455                         g(cgen_lea3(ctx, insn_op_size(insn), insn_aux(insn)));
1456                         return true;
1457                 case INSN_ROT:
1458                         if (unlikely(insn_op_size(insn) < OP_SIZE_4))
1459                                 goto invalid_insn;
1460                         g(cgen_rot(ctx, insn_op_size(insn), insn_aux(insn), insn_writes_flags(insn)));
1461                         return true;
1462                 case INSN_ROT_PARTIAL:
1463                         if (unlikely(insn_op_size(insn) >= OP_SIZE_4))
1464                                 goto invalid_insn;
1465                         g(cgen_rot(ctx, insn_op_size(insn), insn_aux(insn), insn_writes_flags(insn)));
1466                         return true;
1467                 case INSN_BT:
1468                         if (unlikely(insn_op_size(insn) == OP_SIZE_1) || unlikely(!insn_writes_flags(insn)))
1469                                 goto invalid_insn;
1470                         g(cgen_bt(ctx, insn_op_size(insn)));
1471                         return true;
1472                 case INSN_BTX:
1473                         if (unlikely(insn_op_size(insn) == OP_SIZE_1) || unlikely(!insn_writes_flags(insn)))
1474                                 goto invalid_insn;
1475                         g(cgen_btx(ctx, insn_op_size(insn), insn_aux(insn)));
1476                         return true;
1477                 case INSN_MUL_L:
1478                         g(cgen_mul_l(ctx, insn_op_size(insn), insn_aux(insn)));
1479                         return true;
1480                 case INSN_DIV_L:
1481                         g(cgen_div_l(ctx, insn_op_size(insn), insn_aux(insn)));
1482                         return true;
1483                 case INSN_CBW:
1484                         if (unlikely(insn_op_size(insn) <= OP_SIZE_2))
1485                                 goto invalid_insn;
1486                         if (insn_op_size(insn) == OP_SIZE_8)
1487                                 cgen_rex(X86_REX | X86_REX_W);
1488                         if (unlikely(cget_one(ctx) != R_AX))
1489                                 goto invalid_insn;
1490                         if (unlikely(cget_one(ctx) != R_AX))
1491                                 goto invalid_insn;
1492                         cgen_one(X86_CBW);
1493                         return true;
1494                 case INSN_CBW_PARTIAL:
1495                         if (unlikely(insn_op_size(insn) != OP_SIZE_2))
1496                                 goto invalid_insn;
1497                         if (unlikely(cget_one(ctx) != R_AX))
1498                                 goto invalid_insn;
1499                         if (unlikely(cget_one(ctx) != R_AX))
1500                                 goto invalid_insn;
1501                         cgen_one(X86_OP_SIZE);
1502                         cgen_one(X86_CBW);
1503                         return true;
1504                 case INSN_CWD:
1505                         if (unlikely(insn_op_size(insn) <= OP_SIZE_2))
1506                                 goto invalid_insn;
1507                         if (unlikely(cget_one(ctx) != R_DX))
1508                                 goto invalid_insn;
1509                         if (unlikely(cget_one(ctx) != R_AX))
1510                                 goto invalid_insn;
1511                         if (insn_op_size(insn) == OP_SIZE_8)
1512                                 cgen_rex(X86_REX | X86_REX_W);
1513                         cgen_one(X86_CWD);
1514                         return true;
1515                 case INSN_CWD_PARTIAL:
1516                         if (unlikely(insn_op_size(insn) != OP_SIZE_2))
1517                                 goto invalid_insn;
1518                         if (unlikely(cget_one(ctx) != R_DX))
1519                                 goto invalid_insn;
1520                         if (unlikely(cget_one(ctx) != R_AX))
1521                                 goto invalid_insn;
1522                         if (unlikely(cget_one(ctx) != R_DX))
1523                                 goto invalid_insn;
1524                         cgen_one(X86_OP_SIZE);
1525                         cgen_one(X86_CWD);
1526                         return true;
1527                 case INSN_SET_COND:
1528                         if (unlikely(insn_op_size(insn) != OP_SIZE_1))
1529                                 goto invalid_insn;
1530                         g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_SETCC_RM8 + (insn_aux(insn) & 0xf), OP_SIZE_1, false, 0, ctx->code_position));
1531                         ctx->code_position += arg_size(*ctx->code_position);
1532                         return true;
1533                 case INSN_SET_COND_PARTIAL:
1534                         if (unlikely(insn_op_size(insn) != OP_SIZE_1))
1535                                 goto invalid_insn;
1536                         g(cgen_rm_insn(ctx, -1, PREFIX_0F, X86_0F_SETCC_RM8 + (insn_aux(insn) & 0xf), OP_SIZE_1, false, 0, ctx->code_position));
1537                         ctx->code_position += arg_size(*ctx->code_position);
1538                         ctx->code_position += arg_size(*ctx->code_position);
1539                         return true;
1540                 case INSN_CMOV:
1541                 case INSN_CMOV_XCC:
1542                         if (unlikely(insn_op_size(insn) == OP_SIZE_1))
1543                                 goto invalid_insn;
1544                         g(cgen_cmov(ctx, insn_op_size(insn), insn_aux(insn)));
1545                         return true;
1546                 case INSN_MEMCPY:
1547                         g(cgen_memcpy(ctx));
1548                         return true;
1549                 case INSN_MEMSET:
1550                         g(cgen_memset(ctx));
1551                         return true;
1552                 case INSN_FP_CMP:
1553                         g(cgen_sse_cmp(ctx, insn_op_size(insn)));
1554                         return true;
1555                 case INSN_FP_ALU:
1556                         g(cgen_sse_alu(ctx, insn_op_size(insn), insn_aux(insn)));
1557                         return true;
1558                 case INSN_FP_ALU1:
1559                         g(cgen_sse_alu1(ctx, insn_op_size(insn), insn_aux(insn)));
1560                         return true;
1561                 case INSN_FP_FROM_INT32:
1562                 case INSN_FP_FROM_INT64:
1563                         g(cgen_sse_from_int(ctx, insn_opcode(insn) == INSN_FP_FROM_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1564                         return true;
1565                 case INSN_FP_TO_INT32:
1566                 case INSN_FP_TO_INT64:
1567                         g(cgen_sse_to_int(ctx, insn_opcode(insn) == INSN_FP_TO_INT32 ? OP_SIZE_4 : OP_SIZE_8, insn_op_size(insn)));
1568                         return true;
1569                 case INSN_FP_CVT:
1570                         g(cgen_sse_cvt(ctx, insn_op_size(insn), insn_aux(insn)));
1571                         return true;
1572                 case INSN_X87_FLD:
1573                         g(cgen_x87_fld(ctx, insn_op_size(insn)));
1574                         return true;
1575                 case INSN_X87_FILD:
1576                         g(cgen_x87_fild(ctx, insn_op_size(insn)));
1577                         return true;
1578                 case INSN_X87_FSTP:
1579                         g(cgen_x87_fstp(ctx, insn_op_size(insn)));
1580                         return true;
1581                 case INSN_X87_FISTP:
1582                         g(cgen_x87_fistp(ctx, insn_op_size(insn)));
1583                         return true;
1584                 case INSN_X87_FISTTP:
1585                         g(cgen_x87_fisttp(ctx, insn_op_size(insn)));
1586                         return true;
1587                 case INSN_X87_FCOMP:
1588                         g(cgen_x87_fcomp(ctx, insn_op_size(insn)));
1589                         return true;
1590                 case INSN_X87_FCOMPP:
1591                         cgen_one(X87_FCOMPP);
1592                         cgen_one(X87_FCOMPP_2);
1593                         return true;
1594                 case INSN_X87_FCOMIP:
1595                         imm8 = cget_one(ctx);
1596                         cgen_one(X87_FCOMIP);
1597                         cgen_one(X87_FCOMIP_2 + (imm8 & 7));
1598                         return true;
1599                 case INSN_X87_ALU:
1600                         g(cgen_x87_alu(ctx, insn_op_size(insn), insn_aux(insn)));
1601                         return true;
1602                 case INSN_X87_ALUP:
1603                         g(cgen_x87_alup(ctx, insn_aux(insn)));
1604                         return true;
1605                 case INSN_X87_FCHS:
1606                         cgen_one(X87_FCHS);
1607                         cgen_one(X87_FCHS_2);
1608                         return true;
1609                 case INSN_X87_FSQRT:
1610                         cgen_one(X87_FSQRT);
1611                         cgen_one(X87_FSQRT_2);
1612                         return true;
1613                 case INSN_X87_FRNDINT:
1614                         cgen_one(X87_FRNDINT);
1615                         cgen_one(X87_FRNDINT_2);
1616                         return true;
1617                 case INSN_X87_FNSTSW:
1618                         if (unlikely(cget_one(ctx) != R_AX))
1619                                 goto invalid_insn;
1620                         if (unlikely(cget_one(ctx) != R_AX))
1621                                 goto invalid_insn;
1622                         cgen_one(X87_FNSTSW);
1623                         cgen_one(X87_FNSTSW_2);
1624                         return true;
1625                 case INSN_X87_FLDCW:
1626                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X87_FLDCW, OP_SIZE_4, false, X87_FLDCW_X, ctx->code_position));
1627                         ctx->code_position += arg_size(*ctx->code_position);
1628                         return true;
1629                 case INSN_JMP:
1630                         if (insn_jump_size(insn) == JMP_SHORT || insn_jump_size(insn) == JMP_SHORTEST) {
1631                                 cgen_one(X86_JMP_8);
1632                                 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1633                                 cgen_one(0);
1634                         } else if (likely(insn_jump_size(insn) == JMP_LONG)) {
1635                                 cgen_one(X86_JMP_16);
1636                                 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1637                                 cgen_four(0);
1638                         } else {
1639                                 goto invalid_insn;
1640                         }
1641                         return true;
1642                 case INSN_JMP_COND:
1643                         if (insn_jump_size(insn) == JMP_SHORT || insn_jump_size(insn) == JMP_SHORTEST) {
1644                                 cgen_one(X86_JCC_8 + (insn_aux(insn) & 0xf));
1645                                 g(add_relocation(ctx, JMP_SHORT, 0, NULL));
1646                                 cgen_one(0);
1647                         } else if (likely(insn_jump_size(insn) == JMP_LONG)) {
1648                                 cgen_one(X86_0F);
1649                                 cgen_one(X86_0F_JCC_16 + (insn_aux(insn) & 0xf));
1650                                 g(add_relocation(ctx, JMP_LONG, 0, NULL));
1651                                 cgen_four(0);
1652                         } else {
1653                                 goto invalid_insn;
1654                         }
1655                         return true;
1656                 case INSN_JMP_INDIRECT:
1657                         g(cgen_rm_insn(ctx, -1, PREFIX_NONE, X86_FF, OP_SIZE_4, false, X86_FF_JMP_INDIRECT, ctx->code_position));
1658                         ctx->code_position += arg_size(*ctx->code_position);
1659                         return true;
1660                 default:
1661                 invalid_insn:
1662                         internal(file_line, "cgen_insn: invalid insn %08lx", (unsigned long)insn);
1663                         return false;
1664         }