]> Git Repo - qemu.git/blame - tcg/i386/tcg-target.c
tcg-i386: Nuke trailing whitespace.
[qemu.git] / tcg / i386 / tcg-target.c
CommitLineData
c896fe29
FB
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
d4a9eb1f
BS
24
25#ifndef NDEBUG
26static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
c896fe29
FB
27 "%eax",
28 "%ecx",
29 "%edx",
30 "%ebx",
31 "%esp",
32 "%ebp",
33 "%esi",
34 "%edi",
35};
d4a9eb1f 36#endif
c896fe29 37
d4a9eb1f 38static const int tcg_target_reg_alloc_order[] = {
c896fe29
FB
39 TCG_REG_EBX,
40 TCG_REG_ESI,
41 TCG_REG_EDI,
42 TCG_REG_EBP,
6648e296
RH
43 TCG_REG_ECX,
44 TCG_REG_EDX,
45 TCG_REG_EAX,
c896fe29
FB
46};
47
d4a9eb1f
BS
48static const int tcg_target_call_iarg_regs[3] = { TCG_REG_EAX, TCG_REG_EDX, TCG_REG_ECX };
49static const int tcg_target_call_oarg_regs[2] = { TCG_REG_EAX, TCG_REG_EDX };
c896fe29 50
b03cce8e
FB
51static uint8_t *tb_ret_addr;
52
78686523 53static void patch_reloc(uint8_t *code_ptr, int type,
f54b3f92 54 tcg_target_long value, tcg_target_long addend)
c896fe29 55{
f54b3f92 56 value += addend;
c896fe29
FB
57 switch(type) {
58 case R_386_32:
59 *(uint32_t *)code_ptr = value;
60 break;
61 case R_386_PC32:
62 *(uint32_t *)code_ptr = value - (long)code_ptr;
63 break;
f75b56c1
RH
64 case R_386_PC8:
65 value -= (long)code_ptr;
66 if (value != (int8_t)value) {
67 tcg_abort();
68 }
69 *(uint8_t *)code_ptr = value;
70 break;
c896fe29
FB
71 default:
72 tcg_abort();
73 }
74}
75
76/* maximum number of register used for input function arguments */
77static inline int tcg_target_get_call_iarg_regs_count(int flags)
78{
79 flags &= TCG_CALL_TYPE_MASK;
80 switch(flags) {
81 case TCG_CALL_TYPE_STD:
82 return 0;
83 case TCG_CALL_TYPE_REGPARM_1:
84 case TCG_CALL_TYPE_REGPARM_2:
85 case TCG_CALL_TYPE_REGPARM:
86 return flags - TCG_CALL_TYPE_REGPARM_1 + 1;
87 default:
88 tcg_abort();
89 }
90}
91
92/* parse target specific constraints */
d4a9eb1f 93static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
c896fe29
FB
94{
95 const char *ct_str;
96
97 ct_str = *pct_str;
98 switch(ct_str[0]) {
99 case 'a':
100 ct->ct |= TCG_CT_REG;
101 tcg_regset_set_reg(ct->u.regs, TCG_REG_EAX);
102 break;
103 case 'b':
104 ct->ct |= TCG_CT_REG;
105 tcg_regset_set_reg(ct->u.regs, TCG_REG_EBX);
106 break;
107 case 'c':
108 ct->ct |= TCG_CT_REG;
109 tcg_regset_set_reg(ct->u.regs, TCG_REG_ECX);
110 break;
111 case 'd':
112 ct->ct |= TCG_CT_REG;
113 tcg_regset_set_reg(ct->u.regs, TCG_REG_EDX);
114 break;
115 case 'S':
116 ct->ct |= TCG_CT_REG;
117 tcg_regset_set_reg(ct->u.regs, TCG_REG_ESI);
118 break;
119 case 'D':
120 ct->ct |= TCG_CT_REG;
121 tcg_regset_set_reg(ct->u.regs, TCG_REG_EDI);
122 break;
123 case 'q':
124 ct->ct |= TCG_CT_REG;
125 tcg_regset_set32(ct->u.regs, 0, 0xf);
126 break;
127 case 'r':
128 ct->ct |= TCG_CT_REG;
129 tcg_regset_set32(ct->u.regs, 0, 0xff);
130 break;
131
132 /* qemu_ld/st address constraint */
133 case 'L':
134 ct->ct |= TCG_CT_REG;
135 tcg_regset_set32(ct->u.regs, 0, 0xff);
136 tcg_regset_reset_reg(ct->u.regs, TCG_REG_EAX);
137 tcg_regset_reset_reg(ct->u.regs, TCG_REG_EDX);
138 break;
139 default:
140 return -1;
141 }
142 ct_str++;
143 *pct_str = ct_str;
144 return 0;
145}
146
147/* test if a constant matches the constraint */
148static inline int tcg_target_const_match(tcg_target_long val,
149 const TCGArgConstraint *arg_ct)
150{
151 int ct;
152 ct = arg_ct->ct;
153 if (ct & TCG_CT_CONST)
154 return 1;
155 else
156 return 0;
157}
158
fcb5dac1
RH
159#define P_EXT 0x100 /* 0x0f opcode prefix */
160
a369a702
RH
161#define OPC_ARITH_EvIz (0x81)
162#define OPC_ARITH_EvIb (0x83)
81570a70
RH
163#define OPC_ARITH_GvEv (0x03) /* ... plus (ARITH_FOO << 3) */
164#define OPC_ADD_GvEv (OPC_ARITH_GvEv | (ARITH_ADD << 3))
fcb5dac1 165#define OPC_BSWAP (0xc8 | P_EXT)
aadb21a4 166#define OPC_CALL_Jz (0xe8)
81570a70
RH
167#define OPC_CMP_GvEv (OPC_ARITH_GvEv | (ARITH_CMP << 3))
168#define OPC_DEC_r32 (0x48)
0566d387
RH
169#define OPC_IMUL_GvEv (0xaf | P_EXT)
170#define OPC_IMUL_GvEvIb (0x6b)
171#define OPC_IMUL_GvEvIz (0x69)
81570a70 172#define OPC_INC_r32 (0x40)
da441cff
RH
173#define OPC_JCC_long (0x80 | P_EXT) /* ... plus condition code */
174#define OPC_JCC_short (0x70) /* ... plus condition code */
175#define OPC_JMP_long (0xe9)
176#define OPC_JMP_short (0xeb)
34a6d0b7 177#define OPC_LEA (0x8d)
af266089
RH
178#define OPC_MOVB_EvGv (0x88) /* stores, more or less */
179#define OPC_MOVL_EvGv (0x89) /* stores, more or less */
180#define OPC_MOVL_GvEv (0x8b) /* loads, more or less */
ef10b106 181#define OPC_MOVL_Iv (0xb8)
6817c355
RH
182#define OPC_MOVSBL (0xbe | P_EXT)
183#define OPC_MOVSWL (0xbf | P_EXT)
55e082a7
RH
184#define OPC_MOVZBL (0xb6 | P_EXT)
185#define OPC_MOVZWL (0xb7 | P_EXT)
6858614e
RH
186#define OPC_POP_r32 (0x58)
187#define OPC_PUSH_r32 (0x50)
188#define OPC_PUSH_Iv (0x68)
189#define OPC_PUSH_Ib (0x6a)
3c3accc6 190#define OPC_RET (0xc3)
32a8ffb9 191#define OPC_SETCC (0x90 | P_EXT) /* ... plus condition code */
f53dba01
RH
192#define OPC_SHIFT_1 (0xd1)
193#define OPC_SHIFT_Ib (0xc1)
194#define OPC_SHIFT_cl (0xd3)
81570a70 195#define OPC_TESTL (0x85)
b3e66df7 196#define OPC_XCHG_ax_r32 (0x90)
fcb5dac1 197
9363dedb
RH
198#define OPC_GRP3_Ev (0xf7)
199#define OPC_GRP5 (0xff)
200
201/* Group 1 opcode extensions for 0x80-0x83.
202 These are also used as modifiers for OPC_ARITH. */
c896fe29
FB
203#define ARITH_ADD 0
204#define ARITH_OR 1
205#define ARITH_ADC 2
206#define ARITH_SBB 3
207#define ARITH_AND 4
208#define ARITH_SUB 5
209#define ARITH_XOR 6
210#define ARITH_CMP 7
211
da441cff 212/* Group 2 opcode extensions for 0xc0, 0xc1, 0xd0-0xd3. */
9619376c
AJ
213#define SHIFT_ROL 0
214#define SHIFT_ROR 1
c896fe29
FB
215#define SHIFT_SHL 4
216#define SHIFT_SHR 5
217#define SHIFT_SAR 7
218
9363dedb
RH
219/* Group 3 opcode extensions for 0xf6, 0xf7. To be used with OPC_GRP3. */
220#define EXT3_NOT 2
221#define EXT3_NEG 3
222#define EXT3_MUL 4
223#define EXT3_IMUL 5
224#define EXT3_DIV 6
225#define EXT3_IDIV 7
226
227/* Group 5 opcode extensions for 0xff. To be used with OPC_GRP5. */
228#define EXT5_CALLN_Ev 2
229#define EXT5_JMPN_Ev 4
da441cff
RH
230
231/* Condition codes to be added to OPC_JCC_{long,short}. */
c896fe29
FB
232#define JCC_JMP (-1)
233#define JCC_JO 0x0
234#define JCC_JNO 0x1
235#define JCC_JB 0x2
236#define JCC_JAE 0x3
237#define JCC_JE 0x4
238#define JCC_JNE 0x5
239#define JCC_JBE 0x6
240#define JCC_JA 0x7
241#define JCC_JS 0x8
242#define JCC_JNS 0x9
243#define JCC_JP 0xa
244#define JCC_JNP 0xb
245#define JCC_JL 0xc
246#define JCC_JGE 0xd
247#define JCC_JLE 0xe
248#define JCC_JG 0xf
249
c896fe29
FB
250static const uint8_t tcg_cond_to_jcc[10] = {
251 [TCG_COND_EQ] = JCC_JE,
252 [TCG_COND_NE] = JCC_JNE,
253 [TCG_COND_LT] = JCC_JL,
254 [TCG_COND_GE] = JCC_JGE,
255 [TCG_COND_LE] = JCC_JLE,
256 [TCG_COND_GT] = JCC_JG,
257 [TCG_COND_LTU] = JCC_JB,
258 [TCG_COND_GEU] = JCC_JAE,
259 [TCG_COND_LEU] = JCC_JBE,
260 [TCG_COND_GTU] = JCC_JA,
261};
262
263static inline void tcg_out_opc(TCGContext *s, int opc)
264{
265 if (opc & P_EXT)
266 tcg_out8(s, 0x0f);
267 tcg_out8(s, opc);
268}
269
270static inline void tcg_out_modrm(TCGContext *s, int opc, int r, int rm)
271{
272 tcg_out_opc(s, opc);
273 tcg_out8(s, 0xc0 | (r << 3) | rm);
274}
275
34a6d0b7
RH
276/* Output an opcode with a full "rm + (index<<shift) + offset" address mode.
277 We handle either RM and INDEX missing with a -1 value. */
278
279static void tcg_out_modrm_sib_offset(TCGContext *s, int opc, int r, int rm,
280 int index, int shift, int32_t offset)
c896fe29 281{
34a6d0b7
RH
282 int mod, len;
283
284 if (index == -1 && rm == -1) {
285 /* Absolute address. */
286 tcg_out_opc(s, opc);
287 tcg_out8(s, (r << 3) | 5);
288 tcg_out32(s, offset);
289 return;
290 }
291
c896fe29 292 tcg_out_opc(s, opc);
34a6d0b7
RH
293
294 /* Find the length of the immediate addend. Note that the encoding
295 that would be used for (%ebp) indicates absolute addressing. */
c896fe29 296 if (rm == -1) {
34a6d0b7 297 mod = 0, len = 4, rm = 5;
c896fe29 298 } else if (offset == 0 && rm != TCG_REG_EBP) {
34a6d0b7
RH
299 mod = 0, len = 0;
300 } else if (offset == (int8_t)offset) {
301 mod = 0x40, len = 1;
c896fe29 302 } else {
34a6d0b7
RH
303 mod = 0x80, len = 4;
304 }
305
306 /* Use a single byte MODRM format if possible. Note that the encoding
307 that would be used for %esp is the escape to the two byte form. */
308 if (index == -1 && rm != TCG_REG_ESP) {
309 /* Single byte MODRM format. */
310 tcg_out8(s, mod | (r << 3) | rm);
311 } else {
312 /* Two byte MODRM+SIB format. */
313
314 /* Note that the encoding that would place %esp into the index
315 field indicates no index register. */
316 if (index == -1) {
317 index = 4;
c896fe29 318 } else {
34a6d0b7 319 assert(index != TCG_REG_ESP);
c896fe29 320 }
34a6d0b7
RH
321
322 tcg_out8(s, mod | (r << 3) | 4);
323 tcg_out8(s, (shift << 6) | (index << 3) | rm);
324 }
325
326 if (len == 1) {
327 tcg_out8(s, offset);
328 } else if (len == 4) {
c896fe29
FB
329 tcg_out32(s, offset);
330 }
331}
332
34a6d0b7
RH
333/* rm == -1 means no register index */
334static inline void tcg_out_modrm_offset(TCGContext *s, int opc, int r, int rm,
335 int32_t offset)
336{
337 tcg_out_modrm_sib_offset(s, opc, r, rm, -1, 0, offset);
338}
339
81570a70
RH
340/* Generate dest op= src. Uses the same ARITH_* codes as tgen_arithi. */
341static inline void tgen_arithr(TCGContext *s, int subop, int dest, int src)
342{
343 tcg_out_modrm(s, OPC_ARITH_GvEv + (subop << 3), dest, src);
344}
345
c896fe29
FB
346static inline void tcg_out_mov(TCGContext *s, int ret, int arg)
347{
af266089
RH
348 if (arg != ret) {
349 tcg_out_modrm(s, OPC_MOVL_GvEv, ret, arg);
350 }
c896fe29
FB
351}
352
353static inline void tcg_out_movi(TCGContext *s, TCGType type,
354 int ret, int32_t arg)
355{
356 if (arg == 0) {
81570a70 357 tgen_arithr(s, ARITH_XOR, ret, ret);
c896fe29 358 } else {
ef10b106 359 tcg_out8(s, OPC_MOVL_Iv + ret);
c896fe29
FB
360 tcg_out32(s, arg);
361 }
362}
363
6858614e
RH
364static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
365{
366 if (val == (int8_t)val) {
367 tcg_out_opc(s, OPC_PUSH_Ib);
368 tcg_out8(s, val);
369 } else {
370 tcg_out_opc(s, OPC_PUSH_Iv);
371 tcg_out32(s, val);
372 }
373}
374
375static inline void tcg_out_push(TCGContext *s, int reg)
376{
377 tcg_out_opc(s, OPC_PUSH_r32 + reg);
378}
379
380static inline void tcg_out_pop(TCGContext *s, int reg)
381{
382 tcg_out_opc(s, OPC_POP_r32 + reg);
383}
384
e4d5434c
BS
385static inline void tcg_out_ld(TCGContext *s, TCGType type, int ret,
386 int arg1, tcg_target_long arg2)
c896fe29 387{
af266089 388 tcg_out_modrm_offset(s, OPC_MOVL_GvEv, ret, arg1, arg2);
c896fe29
FB
389}
390
e4d5434c
BS
391static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,
392 int arg1, tcg_target_long arg2)
c896fe29 393{
af266089 394 tcg_out_modrm_offset(s, OPC_MOVL_EvGv, arg, arg1, arg2);
c896fe29
FB
395}
396
f53dba01
RH
397static void tcg_out_shifti(TCGContext *s, int subopc, int reg, int count)
398{
399 if (count == 1) {
400 tcg_out_modrm(s, OPC_SHIFT_1, subopc, reg);
401 } else {
402 tcg_out_modrm(s, OPC_SHIFT_Ib, subopc, reg);
403 tcg_out8(s, count);
404 }
405}
406
fcb5dac1
RH
407static inline void tcg_out_bswap32(TCGContext *s, int reg)
408{
409 tcg_out_opc(s, OPC_BSWAP + reg);
410}
411
412static inline void tcg_out_rolw_8(TCGContext *s, int reg)
413{
414 tcg_out8(s, 0x66);
f53dba01 415 tcg_out_shifti(s, SHIFT_ROL, reg, 8);
fcb5dac1
RH
416}
417
55e082a7
RH
418static inline void tcg_out_ext8u(TCGContext *s, int dest, int src)
419{
420 /* movzbl */
421 assert(src < 4);
422 tcg_out_modrm(s, OPC_MOVZBL, dest, src);
423}
424
6817c355
RH
425static void tcg_out_ext8s(TCGContext *s, int dest, int src)
426{
427 /* movsbl */
428 assert(src < 4);
429 tcg_out_modrm(s, OPC_MOVSBL, dest, src);
430}
431
55e082a7
RH
432static inline void tcg_out_ext16u(TCGContext *s, int dest, int src)
433{
434 /* movzwl */
435 tcg_out_modrm(s, OPC_MOVZWL, dest, src);
436}
437
6817c355
RH
438static inline void tcg_out_ext16s(TCGContext *s, int dest, int src)
439{
440 /* movswl */
441 tcg_out_modrm(s, OPC_MOVSWL, dest, src);
442}
443
81570a70
RH
444static inline void tgen_arithi(TCGContext *s, int c, int r0,
445 int32_t val, int cf)
c896fe29 446{
81570a70
RH
447 /* ??? While INC is 2 bytes shorter than ADDL $1, they also induce
448 partial flags update stalls on Pentium4 and are not recommended
449 by current Intel optimization manuals. */
450 if (!cf && (c == ARITH_ADD || c == ARITH_SUB) && (val == 1 || val == -1)) {
451 int opc = ((c == ARITH_ADD) ^ (val < 0) ? OPC_INC_r32 : OPC_DEC_r32);
452 tcg_out_opc(s, opc + r0);
17cf428f 453 } else if (val == (int8_t)val) {
a369a702 454 tcg_out_modrm(s, OPC_ARITH_EvIb, c, r0);
c896fe29 455 tcg_out8(s, val);
b70650cb 456 } else if (c == ARITH_AND && val == 0xffu && r0 < 4) {
55e082a7 457 tcg_out_ext8u(s, r0, r0);
b70650cb 458 } else if (c == ARITH_AND && val == 0xffffu) {
55e082a7 459 tcg_out_ext16u(s, r0, r0);
c896fe29 460 } else {
a369a702 461 tcg_out_modrm(s, OPC_ARITH_EvIz, c, r0);
c896fe29
FB
462 tcg_out32(s, val);
463 }
464}
465
3e9a474e 466static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
c896fe29
FB
467{
468 if (val != 0)
17cf428f 469 tgen_arithi(s, ARITH_ADD, reg, val, 0);
c896fe29
FB
470}
471
f75b56c1
RH
472/* Use SMALL != 0 to force a short forward branch. */
473static void tcg_out_jxx(TCGContext *s, int opc, int label_index, int small)
c896fe29
FB
474{
475 int32_t val, val1;
476 TCGLabel *l = &s->labels[label_index];
78686523 477
c896fe29
FB
478 if (l->has_value) {
479 val = l->u.value - (tcg_target_long)s->code_ptr;
480 val1 = val - 2;
481 if ((int8_t)val1 == val1) {
f75b56c1 482 if (opc == -1) {
da441cff 483 tcg_out8(s, OPC_JMP_short);
f75b56c1 484 } else {
da441cff 485 tcg_out8(s, OPC_JCC_short + opc);
f75b56c1 486 }
c896fe29
FB
487 tcg_out8(s, val1);
488 } else {
f75b56c1
RH
489 if (small) {
490 tcg_abort();
491 }
c896fe29 492 if (opc == -1) {
da441cff 493 tcg_out8(s, OPC_JMP_long);
c896fe29
FB
494 tcg_out32(s, val - 5);
495 } else {
da441cff 496 tcg_out_opc(s, OPC_JCC_long + opc);
c896fe29
FB
497 tcg_out32(s, val - 6);
498 }
499 }
f75b56c1
RH
500 } else if (small) {
501 if (opc == -1) {
da441cff 502 tcg_out8(s, OPC_JMP_short);
f75b56c1 503 } else {
da441cff 504 tcg_out8(s, OPC_JCC_short + opc);
f75b56c1
RH
505 }
506 tcg_out_reloc(s, s->code_ptr, R_386_PC8, label_index, -1);
507 s->code_ptr += 1;
c896fe29
FB
508 } else {
509 if (opc == -1) {
da441cff 510 tcg_out8(s, OPC_JMP_long);
c896fe29 511 } else {
da441cff 512 tcg_out_opc(s, OPC_JCC_long + opc);
c896fe29
FB
513 }
514 tcg_out_reloc(s, s->code_ptr, R_386_PC32, label_index, -4);
623e265c 515 s->code_ptr += 4;
c896fe29
FB
516 }
517}
518
1d2699ae
RH
519static void tcg_out_cmp(TCGContext *s, TCGArg arg1, TCGArg arg2,
520 int const_arg2)
c896fe29 521{
c896fe29
FB
522 if (const_arg2) {
523 if (arg2 == 0) {
c896fe29 524 /* test r, r */
81570a70 525 tcg_out_modrm(s, OPC_TESTL, arg1, arg1);
c896fe29 526 } else {
17cf428f 527 tgen_arithi(s, ARITH_CMP, arg1, arg2, 0);
c896fe29
FB
528 }
529 } else {
81570a70 530 tgen_arithr(s, ARITH_CMP, arg1, arg2);
c896fe29 531 }
1d2699ae
RH
532}
533
8a56e840 534static void tcg_out_brcond(TCGContext *s, TCGCond cond,
1d2699ae
RH
535 TCGArg arg1, TCGArg arg2, int const_arg2,
536 int label_index, int small)
537{
538 tcg_out_cmp(s, arg1, arg2, const_arg2);
f75b56c1 539 tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, small);
c896fe29
FB
540}
541
542/* XXX: we implement it at the target level to avoid having to
543 handle cross basic blocks temporaries */
f75b56c1
RH
544static void tcg_out_brcond2(TCGContext *s, const TCGArg *args,
545 const int *const_args, int small)
c896fe29
FB
546{
547 int label_next;
548 label_next = gen_new_label();
549 switch(args[4]) {
550 case TCG_COND_EQ:
f75b56c1
RH
551 tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2],
552 label_next, 1);
553 tcg_out_brcond(s, TCG_COND_EQ, args[1], args[3], const_args[3],
554 args[5], small);
c896fe29
FB
555 break;
556 case TCG_COND_NE:
f75b56c1
RH
557 tcg_out_brcond(s, TCG_COND_NE, args[0], args[2], const_args[2],
558 args[5], small);
559 tcg_out_brcond(s, TCG_COND_NE, args[1], args[3], const_args[3],
560 args[5], small);
c896fe29
FB
561 break;
562 case TCG_COND_LT:
f75b56c1
RH
563 tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3],
564 args[5], small);
565 tcg_out_jxx(s, JCC_JNE, label_next, 1);
566 tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2],
567 args[5], small);
c896fe29
FB
568 break;
569 case TCG_COND_LE:
f75b56c1
RH
570 tcg_out_brcond(s, TCG_COND_LT, args[1], args[3], const_args[3],
571 args[5], small);
572 tcg_out_jxx(s, JCC_JNE, label_next, 1);
573 tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2],
574 args[5], small);
c896fe29
FB
575 break;
576 case TCG_COND_GT:
f75b56c1
RH
577 tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3],
578 args[5], small);
579 tcg_out_jxx(s, JCC_JNE, label_next, 1);
580 tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2],
581 args[5], small);
c896fe29
FB
582 break;
583 case TCG_COND_GE:
f75b56c1
RH
584 tcg_out_brcond(s, TCG_COND_GT, args[1], args[3], const_args[3],
585 args[5], small);
586 tcg_out_jxx(s, JCC_JNE, label_next, 1);
587 tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2],
588 args[5], small);
c896fe29
FB
589 break;
590 case TCG_COND_LTU:
f75b56c1
RH
591 tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3],
592 args[5], small);
593 tcg_out_jxx(s, JCC_JNE, label_next, 1);
594 tcg_out_brcond(s, TCG_COND_LTU, args[0], args[2], const_args[2],
595 args[5], small);
c896fe29
FB
596 break;
597 case TCG_COND_LEU:
f75b56c1
RH
598 tcg_out_brcond(s, TCG_COND_LTU, args[1], args[3], const_args[3],
599 args[5], small);
600 tcg_out_jxx(s, JCC_JNE, label_next, 1);
601 tcg_out_brcond(s, TCG_COND_LEU, args[0], args[2], const_args[2],
602 args[5], small);
c896fe29
FB
603 break;
604 case TCG_COND_GTU:
f75b56c1
RH
605 tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3],
606 args[5], small);
607 tcg_out_jxx(s, JCC_JNE, label_next, 1);
608 tcg_out_brcond(s, TCG_COND_GTU, args[0], args[2], const_args[2],
609 args[5], small);
c896fe29
FB
610 break;
611 case TCG_COND_GEU:
f75b56c1
RH
612 tcg_out_brcond(s, TCG_COND_GTU, args[1], args[3], const_args[3],
613 args[5], small);
614 tcg_out_jxx(s, JCC_JNE, label_next, 1);
615 tcg_out_brcond(s, TCG_COND_GEU, args[0], args[2], const_args[2],
616 args[5], small);
c896fe29
FB
617 break;
618 default:
619 tcg_abort();
620 }
621 tcg_out_label(s, label_next, (tcg_target_long)s->code_ptr);
622}
623
8a56e840 624static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGArg dest,
1d2699ae
RH
625 TCGArg arg1, TCGArg arg2, int const_arg2)
626{
627 tcg_out_cmp(s, arg1, arg2, const_arg2);
32a8ffb9 628 tcg_out_modrm(s, OPC_SETCC | tcg_cond_to_jcc[cond], 0, dest);
a369a702 629 tcg_out_ext8u(s, dest, dest);
1d2699ae
RH
630}
631
632static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
633 const int *const_args)
634{
635 TCGArg new_args[6];
636 int label_true, label_over;
637
638 memcpy(new_args, args+1, 5*sizeof(TCGArg));
639
640 if (args[0] == args[1] || args[0] == args[2]
641 || (!const_args[3] && args[0] == args[3])
642 || (!const_args[4] && args[0] == args[4])) {
643 /* When the destination overlaps with one of the argument
644 registers, don't do anything tricky. */
645 label_true = gen_new_label();
646 label_over = gen_new_label();
647
648 new_args[5] = label_true;
649 tcg_out_brcond2(s, new_args, const_args+1, 1);
650
651 tcg_out_movi(s, TCG_TYPE_I32, args[0], 0);
652 tcg_out_jxx(s, JCC_JMP, label_over, 1);
653 tcg_out_label(s, label_true, (tcg_target_long)s->code_ptr);
654
655 tcg_out_movi(s, TCG_TYPE_I32, args[0], 1);
656 tcg_out_label(s, label_over, (tcg_target_long)s->code_ptr);
657 } else {
658 /* When the destination does not overlap one of the arguments,
659 clear the destination first, jump if cond false, and emit an
660 increment in the true case. This results in smaller code. */
661
662 tcg_out_movi(s, TCG_TYPE_I32, args[0], 0);
663
664 label_over = gen_new_label();
665 new_args[4] = tcg_invert_cond(new_args[4]);
666 new_args[5] = label_over;
667 tcg_out_brcond2(s, new_args, const_args+1, 1);
668
669 tgen_arithi(s, ARITH_ADD, args[0], 1, 0);
670 tcg_out_label(s, label_over, (tcg_target_long)s->code_ptr);
671 }
672}
673
aadb21a4
RH
674static void tcg_out_calli(TCGContext *s, tcg_target_long dest)
675{
676 tcg_out_opc(s, OPC_CALL_Jz);
677 tcg_out32(s, dest - (tcg_target_long)s->code_ptr - 4);
678}
679
c896fe29 680#if defined(CONFIG_SOFTMMU)
79383c9c
BS
681
682#include "../../softmmu_defs.h"
c896fe29
FB
683
684static void *qemu_ld_helpers[4] = {
685 __ldb_mmu,
686 __ldw_mmu,
687 __ldl_mmu,
688 __ldq_mmu,
689};
690
691static void *qemu_st_helpers[4] = {
692 __stb_mmu,
693 __stw_mmu,
694 __stl_mmu,
695 __stq_mmu,
696};
697#endif
698
379f6698
PB
699#ifndef CONFIG_USER_ONLY
700#define GUEST_BASE 0
701#endif
702
c896fe29
FB
703/* XXX: qemu_ld and qemu_st could be modified to clobber only EDX and
704 EAX. It will be useful once fixed registers globals are less
705 common. */
706static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
707 int opc)
708{
709 int addr_reg, data_reg, data_reg2, r0, r1, mem_index, s_bits, bswap;
710#if defined(CONFIG_SOFTMMU)
711 uint8_t *label1_ptr, *label2_ptr;
712#endif
713#if TARGET_LONG_BITS == 64
714#if defined(CONFIG_SOFTMMU)
715 uint8_t *label3_ptr;
716#endif
717 int addr_reg2;
718#endif
719
720 data_reg = *args++;
721 if (opc == 3)
722 data_reg2 = *args++;
723 else
724 data_reg2 = 0;
725 addr_reg = *args++;
726#if TARGET_LONG_BITS == 64
727 addr_reg2 = *args++;
728#endif
729 mem_index = *args;
730 s_bits = opc & 3;
731
732 r0 = TCG_REG_EAX;
733 r1 = TCG_REG_EDX;
734
735#if defined(CONFIG_SOFTMMU)
78686523
RH
736 tcg_out_mov(s, r1, addr_reg);
737 tcg_out_mov(s, r0, addr_reg);
a369a702 738
f53dba01
RH
739 tcg_out_shifti(s, SHIFT_SHR, r1, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
740
a369a702
RH
741 tgen_arithi(s, ARITH_AND, r0, TARGET_PAGE_MASK | ((1 << s_bits) - 1), 0);
742 tgen_arithi(s, ARITH_AND, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS, 0);
c896fe29 743
34a6d0b7
RH
744 tcg_out_modrm_sib_offset(s, OPC_LEA, r1, TCG_AREG0, r1, 0,
745 offsetof(CPUState,
746 tlb_table[mem_index][0].addr_read));
c896fe29
FB
747
748 /* cmp 0(r1), r0 */
81570a70 749 tcg_out_modrm_offset(s, OPC_CMP_GvEv, r0, r1, 0);
78686523 750
c896fe29 751 tcg_out_mov(s, r0, addr_reg);
78686523 752
c896fe29
FB
753#if TARGET_LONG_BITS == 32
754 /* je label1 */
da441cff 755 tcg_out8(s, OPC_JCC_short + JCC_JE);
c896fe29
FB
756 label1_ptr = s->code_ptr;
757 s->code_ptr++;
758#else
759 /* jne label3 */
da441cff 760 tcg_out8(s, OPC_JCC_short + JCC_JNE);
c896fe29
FB
761 label3_ptr = s->code_ptr;
762 s->code_ptr++;
78686523 763
c896fe29 764 /* cmp 4(r1), addr_reg2 */
81570a70 765 tcg_out_modrm_offset(s, OPC_CMP_GvEv, addr_reg2, r1, 4);
c896fe29
FB
766
767 /* je label1 */
da441cff 768 tcg_out8(s, OPC_JCC_short + JCC_JE);
c896fe29
FB
769 label1_ptr = s->code_ptr;
770 s->code_ptr++;
78686523 771
c896fe29
FB
772 /* label3: */
773 *label3_ptr = s->code_ptr - label3_ptr - 1;
774#endif
775
776 /* XXX: move that code at the end of the TB */
777#if TARGET_LONG_BITS == 32
778 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_EDX, mem_index);
779#else
780 tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
781 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
782#endif
aadb21a4 783 tcg_out_calli(s, (tcg_target_long)qemu_ld_helpers[s_bits]);
c896fe29
FB
784
785 switch(opc) {
786 case 0 | 4:
6817c355 787 tcg_out_ext8s(s, data_reg, TCG_REG_EAX);
c896fe29
FB
788 break;
789 case 1 | 4:
6817c355 790 tcg_out_ext16s(s, data_reg, TCG_REG_EAX);
c896fe29
FB
791 break;
792 case 0:
55e082a7 793 tcg_out_ext8u(s, data_reg, TCG_REG_EAX);
9db3ba4d 794 break;
c896fe29 795 case 1:
55e082a7 796 tcg_out_ext16u(s, data_reg, TCG_REG_EAX);
9db3ba4d 797 break;
c896fe29
FB
798 case 2:
799 default:
800 tcg_out_mov(s, data_reg, TCG_REG_EAX);
801 break;
802 case 3:
803 if (data_reg == TCG_REG_EDX) {
b3e66df7
RH
804 /* xchg %edx, %eax */
805 tcg_out_opc(s, OPC_XCHG_ax_r32 + TCG_REG_EDX);
c896fe29
FB
806 tcg_out_mov(s, data_reg2, TCG_REG_EAX);
807 } else {
808 tcg_out_mov(s, data_reg, TCG_REG_EAX);
809 tcg_out_mov(s, data_reg2, TCG_REG_EDX);
810 }
811 break;
812 }
813
814 /* jmp label2 */
da441cff 815 tcg_out8(s, OPC_JMP_short);
c896fe29
FB
816 label2_ptr = s->code_ptr;
817 s->code_ptr++;
78686523 818
c896fe29
FB
819 /* label1: */
820 *label1_ptr = s->code_ptr - label1_ptr - 1;
821
822 /* add x(r1), r0 */
81570a70
RH
823 tcg_out_modrm_offset(s, OPC_ADD_GvEv, r0, r1,
824 offsetof(CPUTLBEntry, addend) -
c896fe29
FB
825 offsetof(CPUTLBEntry, addr_read));
826#else
827 r0 = addr_reg;
828#endif
829
830#ifdef TARGET_WORDS_BIGENDIAN
831 bswap = 1;
832#else
833 bswap = 0;
834#endif
835 switch(opc) {
836 case 0:
837 /* movzbl */
55e082a7 838 tcg_out_modrm_offset(s, OPC_MOVZBL, data_reg, r0, GUEST_BASE);
c896fe29
FB
839 break;
840 case 0 | 4:
841 /* movsbl */
6817c355 842 tcg_out_modrm_offset(s, OPC_MOVSBL, data_reg, r0, GUEST_BASE);
c896fe29
FB
843 break;
844 case 1:
845 /* movzwl */
55e082a7 846 tcg_out_modrm_offset(s, OPC_MOVZWL, data_reg, r0, GUEST_BASE);
c896fe29 847 if (bswap) {
fcb5dac1 848 tcg_out_rolw_8(s, data_reg);
c896fe29
FB
849 }
850 break;
851 case 1 | 4:
852 /* movswl */
6817c355 853 tcg_out_modrm_offset(s, OPC_MOVSWL, data_reg, r0, GUEST_BASE);
c896fe29 854 if (bswap) {
fcb5dac1 855 tcg_out_rolw_8(s, data_reg);
c896fe29
FB
856
857 /* movswl data_reg, data_reg */
6817c355 858 tcg_out_modrm(s, OPC_MOVSWL, data_reg, data_reg);
c896fe29
FB
859 }
860 break;
861 case 2:
af266089 862 tcg_out_ld(s, TCG_TYPE_I32, data_reg, r0, GUEST_BASE);
c896fe29 863 if (bswap) {
fcb5dac1 864 tcg_out_bswap32(s, data_reg);
c896fe29
FB
865 }
866 break;
867 case 3:
a042ef94
RH
868 if (bswap) {
869 int t = data_reg;
870 data_reg = data_reg2;
871 data_reg2 = t;
c896fe29 872 }
a042ef94 873 if (r0 != data_reg) {
af266089
RH
874 tcg_out_ld(s, TCG_TYPE_I32, data_reg, r0, GUEST_BASE);
875 tcg_out_ld(s, TCG_TYPE_I32, data_reg2, r0, GUEST_BASE + 4);
c896fe29 876 } else {
a042ef94
RH
877 tcg_out_ld(s, TCG_TYPE_I32, data_reg2, r0, GUEST_BASE + 4);
878 tcg_out_ld(s, TCG_TYPE_I32, data_reg, r0, GUEST_BASE);
879 }
880 if (bswap) {
fcb5dac1 881 tcg_out_bswap32(s, data_reg);
fcb5dac1 882 tcg_out_bswap32(s, data_reg2);
c896fe29
FB
883 }
884 break;
885 default:
886 tcg_abort();
887 }
888
889#if defined(CONFIG_SOFTMMU)
890 /* label2: */
891 *label2_ptr = s->code_ptr - label2_ptr - 1;
892#endif
893}
894
895
896static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
897 int opc)
898{
899 int addr_reg, data_reg, data_reg2, r0, r1, mem_index, s_bits, bswap;
900#if defined(CONFIG_SOFTMMU)
aadb21a4 901 int stack_adjust;
c896fe29
FB
902 uint8_t *label1_ptr, *label2_ptr;
903#endif
904#if TARGET_LONG_BITS == 64
905#if defined(CONFIG_SOFTMMU)
906 uint8_t *label3_ptr;
907#endif
908 int addr_reg2;
909#endif
910
911 data_reg = *args++;
912 if (opc == 3)
913 data_reg2 = *args++;
914 else
915 data_reg2 = 0;
916 addr_reg = *args++;
917#if TARGET_LONG_BITS == 64
918 addr_reg2 = *args++;
919#endif
920 mem_index = *args;
921
922 s_bits = opc;
923
924 r0 = TCG_REG_EAX;
925 r1 = TCG_REG_EDX;
926
927#if defined(CONFIG_SOFTMMU)
78686523
RH
928 tcg_out_mov(s, r1, addr_reg);
929 tcg_out_mov(s, r0, addr_reg);
930
f53dba01
RH
931 tcg_out_shifti(s, SHIFT_SHR, r1, TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
932
a369a702
RH
933 tgen_arithi(s, ARITH_AND, r0, TARGET_PAGE_MASK | ((1 << s_bits) - 1), 0);
934 tgen_arithi(s, ARITH_AND, r1, (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS, 0);
c896fe29 935
34a6d0b7
RH
936 tcg_out_modrm_sib_offset(s, OPC_LEA, r1, TCG_AREG0, r1, 0,
937 offsetof(CPUState,
938 tlb_table[mem_index][0].addr_write));
c896fe29
FB
939
940 /* cmp 0(r1), r0 */
81570a70 941 tcg_out_modrm_offset(s, OPC_CMP_GvEv, r0, r1, 0);
78686523 942
c896fe29 943 tcg_out_mov(s, r0, addr_reg);
78686523 944
c896fe29
FB
945#if TARGET_LONG_BITS == 32
946 /* je label1 */
da441cff 947 tcg_out8(s, OPC_JCC_short + JCC_JE);
c896fe29
FB
948 label1_ptr = s->code_ptr;
949 s->code_ptr++;
950#else
951 /* jne label3 */
da441cff 952 tcg_out8(s, OPC_JCC_short + JCC_JNE);
c896fe29
FB
953 label3_ptr = s->code_ptr;
954 s->code_ptr++;
78686523 955
c896fe29 956 /* cmp 4(r1), addr_reg2 */
81570a70 957 tcg_out_modrm_offset(s, OPC_CMP_GvEv, addr_reg2, r1, 4);
c896fe29
FB
958
959 /* je label1 */
da441cff 960 tcg_out8(s, OPC_JCC_short + JCC_JE);
c896fe29
FB
961 label1_ptr = s->code_ptr;
962 s->code_ptr++;
78686523 963
c896fe29
FB
964 /* label3: */
965 *label3_ptr = s->code_ptr - label3_ptr - 1;
966#endif
967
968 /* XXX: move that code at the end of the TB */
969#if TARGET_LONG_BITS == 32
970 if (opc == 3) {
971 tcg_out_mov(s, TCG_REG_EDX, data_reg);
972 tcg_out_mov(s, TCG_REG_ECX, data_reg2);
6858614e 973 tcg_out_pushi(s, mem_index);
aadb21a4 974 stack_adjust = 4;
c896fe29
FB
975 } else {
976 switch(opc) {
977 case 0:
55e082a7 978 tcg_out_ext8u(s, TCG_REG_EDX, data_reg);
c896fe29
FB
979 break;
980 case 1:
55e082a7 981 tcg_out_ext16u(s, TCG_REG_EDX, data_reg);
c896fe29
FB
982 break;
983 case 2:
984 tcg_out_mov(s, TCG_REG_EDX, data_reg);
985 break;
986 }
987 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);
aadb21a4 988 stack_adjust = 0;
c896fe29
FB
989 }
990#else
991 if (opc == 3) {
992 tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
6858614e
RH
993 tcg_out_pushi(s, mem_index);
994 tcg_out_push(s, data_reg2);
995 tcg_out_push(s, data_reg);
aadb21a4 996 stack_adjust = 12;
c896fe29
FB
997 } else {
998 tcg_out_mov(s, TCG_REG_EDX, addr_reg2);
999 switch(opc) {
1000 case 0:
55e082a7 1001 tcg_out_ext8u(s, TCG_REG_ECX, data_reg);
c896fe29
FB
1002 break;
1003 case 1:
55e082a7 1004 tcg_out_ext16u(s, TCG_REG_ECX, data_reg);
c896fe29
FB
1005 break;
1006 case 2:
1007 tcg_out_mov(s, TCG_REG_ECX, data_reg);
1008 break;
1009 }
6858614e 1010 tcg_out_pushi(s, mem_index);
aadb21a4 1011 stack_adjust = 4;
c896fe29
FB
1012 }
1013#endif
aadb21a4
RH
1014
1015 tcg_out_calli(s, (tcg_target_long)qemu_st_helpers[s_bits]);
1016
1017 if (stack_adjust == 4) {
1018 /* Pop and discard. This is 2 bytes smaller than the add. */
1019 tcg_out_pop(s, TCG_REG_ECX);
1020 } else if (stack_adjust != 0) {
1021 tcg_out_addi(s, TCG_REG_ESP, stack_adjust);
1022 }
1023
c896fe29 1024 /* jmp label2 */
da441cff 1025 tcg_out8(s, OPC_JMP_short);
c896fe29
FB
1026 label2_ptr = s->code_ptr;
1027 s->code_ptr++;
78686523 1028
c896fe29
FB
1029 /* label1: */
1030 *label1_ptr = s->code_ptr - label1_ptr - 1;
1031
1032 /* add x(r1), r0 */
81570a70
RH
1033 tcg_out_modrm_offset(s, OPC_ADD_GvEv, r0, r1,
1034 offsetof(CPUTLBEntry, addend) -
c896fe29
FB
1035 offsetof(CPUTLBEntry, addr_write));
1036#else
1037 r0 = addr_reg;
1038#endif
1039
1040#ifdef TARGET_WORDS_BIGENDIAN
1041 bswap = 1;
1042#else
1043 bswap = 0;
1044#endif
1045 switch(opc) {
1046 case 0:
af266089 1047 tcg_out_modrm_offset(s, OPC_MOVB_EvGv, data_reg, r0, GUEST_BASE);
c896fe29
FB
1048 break;
1049 case 1:
1050 if (bswap) {
1051 tcg_out_mov(s, r1, data_reg);
fcb5dac1 1052 tcg_out_rolw_8(s, r1);
c896fe29
FB
1053 data_reg = r1;
1054 }
1055 /* movw */
1056 tcg_out8(s, 0x66);
af266089 1057 tcg_out_modrm_offset(s, OPC_MOVL_EvGv, data_reg, r0, GUEST_BASE);
c896fe29
FB
1058 break;
1059 case 2:
1060 if (bswap) {
1061 tcg_out_mov(s, r1, data_reg);
fcb5dac1 1062 tcg_out_bswap32(s, r1);
c896fe29
FB
1063 data_reg = r1;
1064 }
af266089 1065 tcg_out_st(s, TCG_TYPE_I32, data_reg, r0, GUEST_BASE);
c896fe29
FB
1066 break;
1067 case 3:
1068 if (bswap) {
1069 tcg_out_mov(s, r1, data_reg2);
fcb5dac1 1070 tcg_out_bswap32(s, r1);
af266089 1071 tcg_out_st(s, TCG_TYPE_I32, r1, r0, GUEST_BASE);
c896fe29 1072 tcg_out_mov(s, r1, data_reg);
fcb5dac1 1073 tcg_out_bswap32(s, r1);
af266089 1074 tcg_out_st(s, TCG_TYPE_I32, r1, r0, GUEST_BASE + 4);
c896fe29 1075 } else {
af266089
RH
1076 tcg_out_st(s, TCG_TYPE_I32, data_reg, r0, GUEST_BASE);
1077 tcg_out_st(s, TCG_TYPE_I32, data_reg2, r0, GUEST_BASE + 4);
c896fe29
FB
1078 }
1079 break;
1080 default:
1081 tcg_abort();
1082 }
1083
1084#if defined(CONFIG_SOFTMMU)
1085 /* label2: */
1086 *label2_ptr = s->code_ptr - label2_ptr - 1;
1087#endif
1088}
1089
a9751609 1090static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
c896fe29
FB
1091 const TCGArg *args, const int *const_args)
1092{
1093 int c;
78686523 1094
c896fe29
FB
1095 switch(opc) {
1096 case INDEX_op_exit_tb:
1097 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_EAX, args[0]);
da441cff 1098 tcg_out8(s, OPC_JMP_long); /* jmp tb_ret_addr */
b03cce8e 1099 tcg_out32(s, tb_ret_addr - s->code_ptr - 4);
c896fe29
FB
1100 break;
1101 case INDEX_op_goto_tb:
1102 if (s->tb_jmp_offset) {
1103 /* direct jump method */
da441cff 1104 tcg_out8(s, OPC_JMP_long); /* jmp im */
c896fe29
FB
1105 s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1106 tcg_out32(s, 0);
1107 } else {
1108 /* indirect jump method */
9363dedb 1109 tcg_out_modrm_offset(s, OPC_GRP5, EXT5_JMPN_Ev, -1,
c896fe29
FB
1110 (tcg_target_long)(s->tb_next + args[0]));
1111 }
1112 s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1113 break;
1114 case INDEX_op_call:
1115 if (const_args[0]) {
aadb21a4 1116 tcg_out_calli(s, args[0]);
c896fe29 1117 } else {
aadb21a4 1118 /* call *reg */
9363dedb 1119 tcg_out_modrm(s, OPC_GRP5, EXT5_CALLN_Ev, args[0]);
c896fe29
FB
1120 }
1121 break;
1122 case INDEX_op_jmp:
1123 if (const_args[0]) {
da441cff 1124 tcg_out8(s, OPC_JMP_long);
c896fe29
FB
1125 tcg_out32(s, args[0] - (tcg_target_long)s->code_ptr - 4);
1126 } else {
da441cff 1127 /* jmp *reg */
9363dedb 1128 tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, args[0]);
c896fe29
FB
1129 }
1130 break;
1131 case INDEX_op_br:
f75b56c1 1132 tcg_out_jxx(s, JCC_JMP, args[0], 0);
c896fe29
FB
1133 break;
1134 case INDEX_op_movi_i32:
1135 tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1]);
1136 break;
1137 case INDEX_op_ld8u_i32:
1138 /* movzbl */
55e082a7 1139 tcg_out_modrm_offset(s, OPC_MOVZBL, args[0], args[1], args[2]);
c896fe29
FB
1140 break;
1141 case INDEX_op_ld8s_i32:
1142 /* movsbl */
6817c355 1143 tcg_out_modrm_offset(s, OPC_MOVSBL, args[0], args[1], args[2]);
c896fe29
FB
1144 break;
1145 case INDEX_op_ld16u_i32:
1146 /* movzwl */
55e082a7 1147 tcg_out_modrm_offset(s, OPC_MOVZWL, args[0], args[1], args[2]);
c896fe29
FB
1148 break;
1149 case INDEX_op_ld16s_i32:
1150 /* movswl */
6817c355 1151 tcg_out_modrm_offset(s, OPC_MOVSWL, args[0], args[1], args[2]);
c896fe29
FB
1152 break;
1153 case INDEX_op_ld_i32:
af266089 1154 tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
c896fe29
FB
1155 break;
1156 case INDEX_op_st8_i32:
1157 /* movb */
af266089 1158 tcg_out_modrm_offset(s, OPC_MOVB_EvGv, args[0], args[1], args[2]);
c896fe29
FB
1159 break;
1160 case INDEX_op_st16_i32:
1161 /* movw */
1162 tcg_out8(s, 0x66);
af266089 1163 tcg_out_modrm_offset(s, OPC_MOVL_EvGv, args[0], args[1], args[2]);
c896fe29
FB
1164 break;
1165 case INDEX_op_st_i32:
af266089 1166 tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
c896fe29 1167 break;
5d1e4e85
RH
1168 case INDEX_op_add_i32:
1169 /* For 3-operand addition, use LEA. */
1170 if (args[0] != args[1]) {
1171 TCGArg a0 = args[0], a1 = args[1], a2 = args[2], c3 = 0;
1172
1173 if (const_args[2]) {
1174 c3 = a2, a2 = -1;
1175 } else if (a0 == a2) {
1176 /* Watch out for dest = src + dest, since we've removed
1177 the matching constraint on the add. */
1178 tgen_arithr(s, ARITH_ADD, a0, a1);
1179 break;
1180 }
1181
1182 tcg_out_modrm_sib_offset(s, OPC_LEA, a0, a1, a2, 0, c3);
1183 break;
1184 }
1185 c = ARITH_ADD;
1186 goto gen_arith;
c896fe29
FB
1187 case INDEX_op_sub_i32:
1188 c = ARITH_SUB;
1189 goto gen_arith;
1190 case INDEX_op_and_i32:
1191 c = ARITH_AND;
1192 goto gen_arith;
1193 case INDEX_op_or_i32:
1194 c = ARITH_OR;
1195 goto gen_arith;
1196 case INDEX_op_xor_i32:
1197 c = ARITH_XOR;
1198 goto gen_arith;
c896fe29
FB
1199 gen_arith:
1200 if (const_args[2]) {
17cf428f 1201 tgen_arithi(s, c, args[0], args[2], 0);
c896fe29 1202 } else {
81570a70 1203 tgen_arithr(s, c, args[0], args[2]);
c896fe29
FB
1204 }
1205 break;
1206 case INDEX_op_mul_i32:
1207 if (const_args[2]) {
1208 int32_t val;
1209 val = args[2];
1210 if (val == (int8_t)val) {
0566d387 1211 tcg_out_modrm(s, OPC_IMUL_GvEvIb, args[0], args[0]);
c896fe29
FB
1212 tcg_out8(s, val);
1213 } else {
0566d387 1214 tcg_out_modrm(s, OPC_IMUL_GvEvIz, args[0], args[0]);
c896fe29
FB
1215 tcg_out32(s, val);
1216 }
1217 } else {
0566d387 1218 tcg_out_modrm(s, OPC_IMUL_GvEv, args[0], args[2]);
c896fe29
FB
1219 }
1220 break;
1221 case INDEX_op_mulu2_i32:
9363dedb 1222 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_MUL, args[3]);
c896fe29
FB
1223 break;
1224 case INDEX_op_div2_i32:
9363dedb 1225 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_IDIV, args[4]);
c896fe29
FB
1226 break;
1227 case INDEX_op_divu2_i32:
9363dedb 1228 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_DIV, args[4]);
c896fe29
FB
1229 break;
1230 case INDEX_op_shl_i32:
1231 c = SHIFT_SHL;
1232 gen_shift32:
1233 if (const_args[2]) {
f53dba01 1234 tcg_out_shifti(s, c, args[0], args[2]);
c896fe29 1235 } else {
f53dba01 1236 tcg_out_modrm(s, OPC_SHIFT_cl, c, args[0]);
c896fe29
FB
1237 }
1238 break;
1239 case INDEX_op_shr_i32:
1240 c = SHIFT_SHR;
1241 goto gen_shift32;
1242 case INDEX_op_sar_i32:
1243 c = SHIFT_SAR;
1244 goto gen_shift32;
9619376c
AJ
1245 case INDEX_op_rotl_i32:
1246 c = SHIFT_ROL;
1247 goto gen_shift32;
1248 case INDEX_op_rotr_i32:
1249 c = SHIFT_ROR;
1250 goto gen_shift32;
1251
c896fe29 1252 case INDEX_op_add2_i32:
81570a70 1253 if (const_args[4]) {
17cf428f 1254 tgen_arithi(s, ARITH_ADD, args[0], args[4], 1);
81570a70
RH
1255 } else {
1256 tgen_arithr(s, ARITH_ADD, args[0], args[4]);
1257 }
1258 if (const_args[5]) {
17cf428f 1259 tgen_arithi(s, ARITH_ADC, args[1], args[5], 1);
81570a70
RH
1260 } else {
1261 tgen_arithr(s, ARITH_ADC, args[1], args[5]);
1262 }
c896fe29
FB
1263 break;
1264 case INDEX_op_sub2_i32:
81570a70 1265 if (const_args[4]) {
17cf428f 1266 tgen_arithi(s, ARITH_SUB, args[0], args[4], 1);
81570a70
RH
1267 } else {
1268 tgen_arithr(s, ARITH_SUB, args[0], args[4]);
1269 }
1270 if (const_args[5]) {
17cf428f 1271 tgen_arithi(s, ARITH_SBB, args[1], args[5], 1);
81570a70
RH
1272 } else {
1273 tgen_arithr(s, ARITH_SBB, args[1], args[5]);
1274 }
c896fe29
FB
1275 break;
1276 case INDEX_op_brcond_i32:
f75b56c1
RH
1277 tcg_out_brcond(s, args[2], args[0], args[1], const_args[1],
1278 args[3], 0);
c896fe29
FB
1279 break;
1280 case INDEX_op_brcond2_i32:
f75b56c1 1281 tcg_out_brcond2(s, args, const_args, 0);
c896fe29
FB
1282 break;
1283
5d40cd63 1284 case INDEX_op_bswap16_i32:
fcb5dac1 1285 tcg_out_rolw_8(s, args[0]);
5d40cd63 1286 break;
66896cb8 1287 case INDEX_op_bswap32_i32:
fcb5dac1 1288 tcg_out_bswap32(s, args[0]);
9619376c
AJ
1289 break;
1290
1291 case INDEX_op_neg_i32:
9363dedb 1292 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_NEG, args[0]);
9619376c
AJ
1293 break;
1294
1295 case INDEX_op_not_i32:
9363dedb 1296 tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_NOT, args[0]);
9619376c
AJ
1297 break;
1298
1299 case INDEX_op_ext8s_i32:
6817c355 1300 tcg_out_ext8s(s, args[0], args[1]);
9619376c
AJ
1301 break;
1302 case INDEX_op_ext16s_i32:
6817c355 1303 tcg_out_ext16s(s, args[0], args[1]);
9619376c 1304 break;
5f0ce17f 1305 case INDEX_op_ext8u_i32:
55e082a7 1306 tcg_out_ext8u(s, args[0], args[1]);
5f0ce17f
AJ
1307 break;
1308 case INDEX_op_ext16u_i32:
55e082a7 1309 tcg_out_ext16u(s, args[0], args[1]);
5f0ce17f 1310 break;
9619376c 1311
1d2699ae
RH
1312 case INDEX_op_setcond_i32:
1313 tcg_out_setcond(s, args[3], args[0], args[1], args[2], const_args[2]);
1314 break;
1315 case INDEX_op_setcond2_i32:
1316 tcg_out_setcond2(s, args, const_args);
1317 break;
1318
c896fe29
FB
1319 case INDEX_op_qemu_ld8u:
1320 tcg_out_qemu_ld(s, args, 0);
1321 break;
1322 case INDEX_op_qemu_ld8s:
1323 tcg_out_qemu_ld(s, args, 0 | 4);
1324 break;
1325 case INDEX_op_qemu_ld16u:
1326 tcg_out_qemu_ld(s, args, 1);
1327 break;
1328 case INDEX_op_qemu_ld16s:
1329 tcg_out_qemu_ld(s, args, 1 | 4);
1330 break;
86feb1c8 1331 case INDEX_op_qemu_ld32:
c896fe29
FB
1332 tcg_out_qemu_ld(s, args, 2);
1333 break;
1334 case INDEX_op_qemu_ld64:
1335 tcg_out_qemu_ld(s, args, 3);
1336 break;
78686523 1337
c896fe29
FB
1338 case INDEX_op_qemu_st8:
1339 tcg_out_qemu_st(s, args, 0);
1340 break;
1341 case INDEX_op_qemu_st16:
1342 tcg_out_qemu_st(s, args, 1);
1343 break;
1344 case INDEX_op_qemu_st32:
1345 tcg_out_qemu_st(s, args, 2);
1346 break;
1347 case INDEX_op_qemu_st64:
1348 tcg_out_qemu_st(s, args, 3);
1349 break;
1350
1351 default:
1352 tcg_abort();
1353 }
1354}
1355
1356static const TCGTargetOpDef x86_op_defs[] = {
1357 { INDEX_op_exit_tb, { } },
1358 { INDEX_op_goto_tb, { } },
1359 { INDEX_op_call, { "ri" } },
1360 { INDEX_op_jmp, { "ri" } },
1361 { INDEX_op_br, { } },
1362 { INDEX_op_mov_i32, { "r", "r" } },
1363 { INDEX_op_movi_i32, { "r" } },
1364 { INDEX_op_ld8u_i32, { "r", "r" } },
1365 { INDEX_op_ld8s_i32, { "r", "r" } },
1366 { INDEX_op_ld16u_i32, { "r", "r" } },
1367 { INDEX_op_ld16s_i32, { "r", "r" } },
1368 { INDEX_op_ld_i32, { "r", "r" } },
1369 { INDEX_op_st8_i32, { "q", "r" } },
1370 { INDEX_op_st16_i32, { "r", "r" } },
1371 { INDEX_op_st_i32, { "r", "r" } },
1372
5d1e4e85 1373 { INDEX_op_add_i32, { "r", "r", "ri" } },
c896fe29
FB
1374 { INDEX_op_sub_i32, { "r", "0", "ri" } },
1375 { INDEX_op_mul_i32, { "r", "0", "ri" } },
1376 { INDEX_op_mulu2_i32, { "a", "d", "a", "r" } },
1377 { INDEX_op_div2_i32, { "a", "d", "0", "1", "r" } },
1378 { INDEX_op_divu2_i32, { "a", "d", "0", "1", "r" } },
1379 { INDEX_op_and_i32, { "r", "0", "ri" } },
1380 { INDEX_op_or_i32, { "r", "0", "ri" } },
1381 { INDEX_op_xor_i32, { "r", "0", "ri" } },
1382
1383 { INDEX_op_shl_i32, { "r", "0", "ci" } },
1384 { INDEX_op_shr_i32, { "r", "0", "ci" } },
1385 { INDEX_op_sar_i32, { "r", "0", "ci" } },
9619376c
AJ
1386 { INDEX_op_rotl_i32, { "r", "0", "ci" } },
1387 { INDEX_op_rotr_i32, { "r", "0", "ci" } },
c896fe29
FB
1388
1389 { INDEX_op_brcond_i32, { "r", "ri" } },
1390
1391 { INDEX_op_add2_i32, { "r", "r", "0", "1", "ri", "ri" } },
1392 { INDEX_op_sub2_i32, { "r", "r", "0", "1", "ri", "ri" } },
1393 { INDEX_op_brcond2_i32, { "r", "r", "ri", "ri" } },
1394
5d40cd63 1395 { INDEX_op_bswap16_i32, { "r", "0" } },
66896cb8 1396 { INDEX_op_bswap32_i32, { "r", "0" } },
9619376c
AJ
1397
1398 { INDEX_op_neg_i32, { "r", "0" } },
1399
1400 { INDEX_op_not_i32, { "r", "0" } },
1401
1402 { INDEX_op_ext8s_i32, { "r", "q" } },
1403 { INDEX_op_ext16s_i32, { "r", "r" } },
55e082a7
RH
1404 { INDEX_op_ext8u_i32, { "r", "q" } },
1405 { INDEX_op_ext16u_i32, { "r", "r" } },
9619376c 1406
1d2699ae
RH
1407 { INDEX_op_setcond_i32, { "q", "r", "ri" } },
1408 { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
1409
c896fe29
FB
1410#if TARGET_LONG_BITS == 32
1411 { INDEX_op_qemu_ld8u, { "r", "L" } },
1412 { INDEX_op_qemu_ld8s, { "r", "L" } },
1413 { INDEX_op_qemu_ld16u, { "r", "L" } },
1414 { INDEX_op_qemu_ld16s, { "r", "L" } },
86feb1c8 1415 { INDEX_op_qemu_ld32, { "r", "L" } },
c896fe29
FB
1416 { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1417
1418 { INDEX_op_qemu_st8, { "cb", "L" } },
1419 { INDEX_op_qemu_st16, { "L", "L" } },
1420 { INDEX_op_qemu_st32, { "L", "L" } },
1421 { INDEX_op_qemu_st64, { "L", "L", "L" } },
1422#else
1423 { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1424 { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1425 { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1426 { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
86feb1c8 1427 { INDEX_op_qemu_ld32, { "r", "L", "L" } },
c896fe29
FB
1428 { INDEX_op_qemu_ld64, { "r", "r", "L", "L" } },
1429
1430 { INDEX_op_qemu_st8, { "cb", "L", "L" } },
1431 { INDEX_op_qemu_st16, { "L", "L", "L" } },
1432 { INDEX_op_qemu_st32, { "L", "L", "L" } },
1433 { INDEX_op_qemu_st64, { "L", "L", "L", "L" } },
1434#endif
1435 { -1 },
1436};
1437
b03cce8e
FB
1438static int tcg_target_callee_save_regs[] = {
1439 /* TCG_REG_EBP, */ /* currently used for the global env, so no
1440 need to save */
1441 TCG_REG_EBX,
1442 TCG_REG_ESI,
1443 TCG_REG_EDI,
1444};
1445
b03cce8e
FB
1446/* Generate global QEMU prologue and epilogue code */
1447void tcg_target_qemu_prologue(TCGContext *s)
1448{
1449 int i, frame_size, push_size, stack_addend;
78686523 1450
b03cce8e
FB
1451 /* TB prologue */
1452 /* save all callee saved registers */
1453 for(i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1454 tcg_out_push(s, tcg_target_callee_save_regs[i]);
1455 }
1456 /* reserve some stack space */
1457 push_size = 4 + ARRAY_SIZE(tcg_target_callee_save_regs) * 4;
1458 frame_size = push_size + TCG_STATIC_CALL_ARGS_SIZE;
78686523 1459 frame_size = (frame_size + TCG_TARGET_STACK_ALIGN - 1) &
b03cce8e
FB
1460 ~(TCG_TARGET_STACK_ALIGN - 1);
1461 stack_addend = frame_size - push_size;
1462 tcg_out_addi(s, TCG_REG_ESP, -stack_addend);
1463
9363dedb 1464 tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, TCG_REG_EAX); /* jmp *%eax */
78686523 1465
b03cce8e
FB
1466 /* TB epilogue */
1467 tb_ret_addr = s->code_ptr;
1468 tcg_out_addi(s, TCG_REG_ESP, stack_addend);
1469 for(i = ARRAY_SIZE(tcg_target_callee_save_regs) - 1; i >= 0; i--) {
1470 tcg_out_pop(s, tcg_target_callee_save_regs[i]);
1471 }
3c3accc6 1472 tcg_out_opc(s, OPC_RET);
b03cce8e
FB
1473}
1474
c896fe29
FB
1475void tcg_target_init(TCGContext *s)
1476{
20cb400d 1477#if !defined(CONFIG_USER_ONLY)
c896fe29
FB
1478 /* fail safe */
1479 if ((1 << CPU_TLB_ENTRY_BITS) != sizeof(CPUTLBEntry))
1480 tcg_abort();
20cb400d 1481#endif
c896fe29
FB
1482
1483 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xff);
4ab50ccf
RH
1484
1485 tcg_regset_clear(tcg_target_call_clobber_regs);
1486 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EAX);
1487 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EDX);
1488 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_ECX);
1489
c896fe29
FB
1490 tcg_regset_clear(s->reserved_regs);
1491 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ESP);
1492
1493 tcg_add_target_add_op_defs(x86_op_defs);
1494}
This page took 0.564487 seconds and 4 git commands to generate.