Statistics
| Branch: | Revision:

root / tcg / i386 / tcg-target.c @ d73685e3

History | View | Annotate | Download (67.9 kB)

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
 */
24

    
25
#ifndef NDEBUG
26
static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
27
#if TCG_TARGET_REG_BITS == 64
28
    "%rax", "%rcx", "%rdx", "%rbx", "%rsp", "%rbp", "%rsi", "%rdi",
29
    "%r8",  "%r9",  "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
30
#else
31
    "%eax", "%ecx", "%edx", "%ebx", "%esp", "%ebp", "%esi", "%edi",
32
#endif
33
};
34
#endif
35

    
36
static const int tcg_target_reg_alloc_order[] = {
37
#if TCG_TARGET_REG_BITS == 64
38
    TCG_REG_RBP,
39
    TCG_REG_RBX,
40
    TCG_REG_R12,
41
    TCG_REG_R13,
42
    TCG_REG_R14,
43
    TCG_REG_R15,
44
    TCG_REG_R10,
45
    TCG_REG_R11,
46
    TCG_REG_R9,
47
    TCG_REG_R8,
48
    TCG_REG_RCX,
49
    TCG_REG_RDX,
50
    TCG_REG_RSI,
51
    TCG_REG_RDI,
52
    TCG_REG_RAX,
53
#else
54
    TCG_REG_EBX,
55
    TCG_REG_ESI,
56
    TCG_REG_EDI,
57
    TCG_REG_EBP,
58
    TCG_REG_ECX,
59
    TCG_REG_EDX,
60
    TCG_REG_EAX,
61
#endif
62
};
63

    
64
static const int tcg_target_call_iarg_regs[] = {
65
#if TCG_TARGET_REG_BITS == 64
66
#if defined(_WIN64)
67
    TCG_REG_RCX,
68
    TCG_REG_RDX,
69
#else
70
    TCG_REG_RDI,
71
    TCG_REG_RSI,
72
    TCG_REG_RDX,
73
    TCG_REG_RCX,
74
#endif
75
    TCG_REG_R8,
76
    TCG_REG_R9,
77
#else
78
    /* 32 bit mode uses stack based calling convention (GCC default). */
79
#endif
80
};
81

    
82
static const int tcg_target_call_oarg_regs[] = {
83
    TCG_REG_EAX,
84
#if TCG_TARGET_REG_BITS == 32
85
    TCG_REG_EDX
86
#endif
87
};
88

    
89
/* Registers used with L constraint, which are the first argument 
90
   registers on x86_64, and two random call clobbered registers on
91
   i386. */
92
#if TCG_TARGET_REG_BITS == 64
93
# define TCG_REG_L0 tcg_target_call_iarg_regs[0]
94
# define TCG_REG_L1 tcg_target_call_iarg_regs[1]
95
# define TCG_REG_L2 tcg_target_call_iarg_regs[2]
96
#else
97
# define TCG_REG_L0 TCG_REG_EAX
98
# define TCG_REG_L1 TCG_REG_EDX
99
#endif
100

    
101
static uint8_t *tb_ret_addr;
102

    
103
static void patch_reloc(uint8_t *code_ptr, int type,
104
                        tcg_target_long value, tcg_target_long addend)
105
{
106
    value += addend;
107
    switch(type) {
108
    case R_386_PC32:
109
        value -= (uintptr_t)code_ptr;
110
        if (value != (int32_t)value) {
111
            tcg_abort();
112
        }
113
        *(uint32_t *)code_ptr = value;
114
        break;
115
    case R_386_PC8:
116
        value -= (uintptr_t)code_ptr;
117
        if (value != (int8_t)value) {
118
            tcg_abort();
119
        }
120
        *(uint8_t *)code_ptr = value;
121
        break;
122
    default:
123
        tcg_abort();
124
    }
125
}
126

    
127
/* maximum number of register used for input function arguments */
128
static inline int tcg_target_get_call_iarg_regs_count(int flags)
129
{
130
    return ARRAY_SIZE(tcg_target_call_iarg_regs);
131
}
132

    
133
/* parse target specific constraints */
134
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
135
{
136
    const char *ct_str;
137

    
138
    ct_str = *pct_str;
139
    switch(ct_str[0]) {
140
    case 'a':
141
        ct->ct |= TCG_CT_REG;
142
        tcg_regset_set_reg(ct->u.regs, TCG_REG_EAX);
143
        break;
144
    case 'b':
145
        ct->ct |= TCG_CT_REG;
146
        tcg_regset_set_reg(ct->u.regs, TCG_REG_EBX);
147
        break;
148
    case 'c':
149
        ct->ct |= TCG_CT_REG;
150
        tcg_regset_set_reg(ct->u.regs, TCG_REG_ECX);
151
        break;
152
    case 'd':
153
        ct->ct |= TCG_CT_REG;
154
        tcg_regset_set_reg(ct->u.regs, TCG_REG_EDX);
155
        break;
156
    case 'S':
157
        ct->ct |= TCG_CT_REG;
158
        tcg_regset_set_reg(ct->u.regs, TCG_REG_ESI);
159
        break;
160
    case 'D':
161
        ct->ct |= TCG_CT_REG;
162
        tcg_regset_set_reg(ct->u.regs, TCG_REG_EDI);
163
        break;
164
    case 'q':
165
        ct->ct |= TCG_CT_REG;
166
        if (TCG_TARGET_REG_BITS == 64) {
167
            tcg_regset_set32(ct->u.regs, 0, 0xffff);
168
        } else {
169
            tcg_regset_set32(ct->u.regs, 0, 0xf);
170
        }
171
        break;
172
    case 'Q':
173
        ct->ct |= TCG_CT_REG;
174
        tcg_regset_set32(ct->u.regs, 0, 0xf);
175
        break;
176
    case 'r':
177
        ct->ct |= TCG_CT_REG;
178
        if (TCG_TARGET_REG_BITS == 64) {
179
            tcg_regset_set32(ct->u.regs, 0, 0xffff);
180
        } else {
181
            tcg_regset_set32(ct->u.regs, 0, 0xff);
182
        }
183
        break;
184

    
185
        /* qemu_ld/st address constraint */
186
    case 'L':
187
        ct->ct |= TCG_CT_REG;
188
#if TCG_TARGET_REG_BITS == 64
189
            tcg_regset_set32(ct->u.regs, 0, 0xffff);
190
            tcg_regset_reset_reg(ct->u.regs, TCG_REG_L0);
191
            tcg_regset_reset_reg(ct->u.regs, TCG_REG_L1);
192
            tcg_regset_reset_reg(ct->u.regs, TCG_REG_L2);
193
#else
194
            tcg_regset_set32(ct->u.regs, 0, 0xff);
195
            tcg_regset_reset_reg(ct->u.regs, TCG_REG_L0);
196
            tcg_regset_reset_reg(ct->u.regs, TCG_REG_L1);
197
#endif
198
        break;
199

    
200
    case 'e':
201
        ct->ct |= TCG_CT_CONST_S32;
202
        break;
203
    case 'Z':
204
        ct->ct |= TCG_CT_CONST_U32;
205
        break;
206

    
207
    default:
208
        return -1;
209
    }
210
    ct_str++;
211
    *pct_str = ct_str;
212
    return 0;
213
}
214

    
215
/* test if a constant matches the constraint */
216
static inline int tcg_target_const_match(tcg_target_long val,
217
                                         const TCGArgConstraint *arg_ct)
218
{
219
    int ct = arg_ct->ct;
220
    if (ct & TCG_CT_CONST) {
221
        return 1;
222
    }
223
    if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
224
        return 1;
225
    }
226
    if ((ct & TCG_CT_CONST_U32) && val == (uint32_t)val) {
227
        return 1;
228
    }
229
    return 0;
230
}
231

    
232
#if TCG_TARGET_REG_BITS == 64
233
# define LOWREGMASK(x)        ((x) & 7)
234
#else
235
# define LOWREGMASK(x)        (x)
236
#endif
237

    
238
#define P_EXT                0x100                /* 0x0f opcode prefix */
239
#define P_DATA16        0x200                /* 0x66 opcode prefix */
240
#if TCG_TARGET_REG_BITS == 64
241
# define P_ADDR32        0x400                /* 0x67 opcode prefix */
242
# define P_REXW                0x800                /* Set REX.W = 1 */
243
# define P_REXB_R        0x1000                /* REG field as byte register */
244
# define P_REXB_RM        0x2000                /* R/M field as byte register */
245
#else
246
# define P_ADDR32        0
247
# define P_REXW                0
248
# define P_REXB_R        0
249
# define P_REXB_RM        0
250
#endif
251

    
252
#define OPC_ARITH_EvIz        (0x81)
253
#define OPC_ARITH_EvIb        (0x83)
254
#define OPC_ARITH_GvEv        (0x03)                /* ... plus (ARITH_FOO << 3) */
255
#define OPC_ADD_GvEv        (OPC_ARITH_GvEv | (ARITH_ADD << 3))
256
#define OPC_BSWAP        (0xc8 | P_EXT)
257
#define OPC_CALL_Jz        (0xe8)
258
#define OPC_CMOVCC      (0x40 | P_EXT)  /* ... plus condition code */
259
#define OPC_CMP_GvEv        (OPC_ARITH_GvEv | (ARITH_CMP << 3))
260
#define OPC_DEC_r32        (0x48)
261
#define OPC_IMUL_GvEv        (0xaf | P_EXT)
262
#define OPC_IMUL_GvEvIb        (0x6b)
263
#define OPC_IMUL_GvEvIz        (0x69)
264
#define OPC_INC_r32        (0x40)
265
#define OPC_JCC_long        (0x80 | P_EXT)        /* ... plus condition code */
266
#define OPC_JCC_short        (0x70)                /* ... plus condition code */
267
#define OPC_JMP_long        (0xe9)
268
#define OPC_JMP_short        (0xeb)
269
#define OPC_LEA         (0x8d)
270
#define OPC_MOVB_EvGv        (0x88)                /* stores, more or less */
271
#define OPC_MOVL_EvGv        (0x89)                /* stores, more or less */
272
#define OPC_MOVL_GvEv        (0x8b)                /* loads, more or less */
273
#define OPC_MOVB_EvIz   (0xc6)
274
#define OPC_MOVL_EvIz        (0xc7)
275
#define OPC_MOVL_Iv     (0xb8)
276
#define OPC_MOVSBL        (0xbe | P_EXT)
277
#define OPC_MOVSWL        (0xbf | P_EXT)
278
#define OPC_MOVSLQ        (0x63 | P_REXW)
279
#define OPC_MOVZBL        (0xb6 | P_EXT)
280
#define OPC_MOVZWL        (0xb7 | P_EXT)
281
#define OPC_POP_r32        (0x58)
282
#define OPC_PUSH_r32        (0x50)
283
#define OPC_PUSH_Iv        (0x68)
284
#define OPC_PUSH_Ib        (0x6a)
285
#define OPC_RET                (0xc3)
286
#define OPC_SETCC        (0x90 | P_EXT | P_REXB_RM) /* ... plus cc */
287
#define OPC_SHIFT_1        (0xd1)
288
#define OPC_SHIFT_Ib        (0xc1)
289
#define OPC_SHIFT_cl        (0xd3)
290
#define OPC_TESTL        (0x85)
291
#define OPC_XCHG_ax_r32        (0x90)
292

    
293
#define OPC_GRP3_Ev        (0xf7)
294
#define OPC_GRP5        (0xff)
295

    
296
/* Group 1 opcode extensions for 0x80-0x83.
297
   These are also used as modifiers for OPC_ARITH.  */
298
#define ARITH_ADD 0
299
#define ARITH_OR  1
300
#define ARITH_ADC 2
301
#define ARITH_SBB 3
302
#define ARITH_AND 4
303
#define ARITH_SUB 5
304
#define ARITH_XOR 6
305
#define ARITH_CMP 7
306

    
307
/* Group 2 opcode extensions for 0xc0, 0xc1, 0xd0-0xd3.  */
308
#define SHIFT_ROL 0
309
#define SHIFT_ROR 1
310
#define SHIFT_SHL 4
311
#define SHIFT_SHR 5
312
#define SHIFT_SAR 7
313

    
314
/* Group 3 opcode extensions for 0xf6, 0xf7.  To be used with OPC_GRP3.  */
315
#define EXT3_NOT   2
316
#define EXT3_NEG   3
317
#define EXT3_MUL   4
318
#define EXT3_IMUL  5
319
#define EXT3_DIV   6
320
#define EXT3_IDIV  7
321

    
322
/* Group 5 opcode extensions for 0xff.  To be used with OPC_GRP5.  */
323
#define EXT5_INC_Ev        0
324
#define EXT5_DEC_Ev        1
325
#define EXT5_CALLN_Ev        2
326
#define EXT5_JMPN_Ev        4
327

    
328
/* Condition codes to be added to OPC_JCC_{long,short}.  */
329
#define JCC_JMP (-1)
330
#define JCC_JO  0x0
331
#define JCC_JNO 0x1
332
#define JCC_JB  0x2
333
#define JCC_JAE 0x3
334
#define JCC_JE  0x4
335
#define JCC_JNE 0x5
336
#define JCC_JBE 0x6
337
#define JCC_JA  0x7
338
#define JCC_JS  0x8
339
#define JCC_JNS 0x9
340
#define JCC_JP  0xa
341
#define JCC_JNP 0xb
342
#define JCC_JL  0xc
343
#define JCC_JGE 0xd
344
#define JCC_JLE 0xe
345
#define JCC_JG  0xf
346

    
347
static const uint8_t tcg_cond_to_jcc[10] = {
348
    [TCG_COND_EQ] = JCC_JE,
349
    [TCG_COND_NE] = JCC_JNE,
350
    [TCG_COND_LT] = JCC_JL,
351
    [TCG_COND_GE] = JCC_JGE,
352
    [TCG_COND_LE] = JCC_JLE,
353
    [TCG_COND_GT] = JCC_JG,
354
    [TCG_COND_LTU] = JCC_JB,
355
    [TCG_COND_GEU] = JCC_JAE,
356
    [TCG_COND_LEU] = JCC_JBE,
357
    [TCG_COND_GTU] = JCC_JA,
358
};
359

    
360
#if TCG_TARGET_REG_BITS == 64
361
static void tcg_out_opc(TCGContext *s, int opc, int r, int rm, int x)
362
{
363
    int rex;
364

    
365
    if (opc & P_DATA16) {
366
        /* We should never be asking for both 16 and 64-bit operation.  */
367
        assert((opc & P_REXW) == 0);
368
        tcg_out8(s, 0x66);
369
    }
370
    if (opc & P_ADDR32) {
371
        tcg_out8(s, 0x67);
372
    }
373

    
374
    rex = 0;
375
    rex |= (opc & P_REXW) >> 8;                /* REX.W */
376
    rex |= (r & 8) >> 1;                /* REX.R */
377
    rex |= (x & 8) >> 2;                /* REX.X */
378
    rex |= (rm & 8) >> 3;                /* REX.B */
379

    
380
    /* P_REXB_{R,RM} indicates that the given register is the low byte.
381
       For %[abcd]l we need no REX prefix, but for %{si,di,bp,sp}l we do,
382
       as otherwise the encoding indicates %[abcd]h.  Note that the values
383
       that are ORed in merely indicate that the REX byte must be present;
384
       those bits get discarded in output.  */
385
    rex |= opc & (r >= 4 ? P_REXB_R : 0);
386
    rex |= opc & (rm >= 4 ? P_REXB_RM : 0);
387

    
388
    if (rex) {
389
        tcg_out8(s, (uint8_t)(rex | 0x40));
390
    }
391

    
392
    if (opc & P_EXT) {
393
        tcg_out8(s, 0x0f);
394
    }
395
    tcg_out8(s, opc);
396
}
397
#else
398
static void tcg_out_opc(TCGContext *s, int opc)
399
{
400
    if (opc & P_DATA16) {
401
        tcg_out8(s, 0x66);
402
    }
403
    if (opc & P_EXT) {
404
        tcg_out8(s, 0x0f);
405
    }
406
    tcg_out8(s, opc);
407
}
408
/* Discard the register arguments to tcg_out_opc early, so as not to penalize
409
   the 32-bit compilation paths.  This method works with all versions of gcc,
410
   whereas relying on optimization may not be able to exclude them.  */
411
#define tcg_out_opc(s, opc, r, rm, x)  (tcg_out_opc)(s, opc)
412
#endif
413

    
414
static void tcg_out_modrm(TCGContext *s, int opc, int r, int rm)
415
{
416
    tcg_out_opc(s, opc, r, rm, 0);
417
    tcg_out8(s, 0xc0 | (LOWREGMASK(r) << 3) | LOWREGMASK(rm));
418
}
419

    
420
/* Output an opcode with a full "rm + (index<<shift) + offset" address mode.
421
   We handle either RM and INDEX missing with a negative value.  In 64-bit
422
   mode for absolute addresses, ~RM is the size of the immediate operand
423
   that will follow the instruction.  */
424

    
425
static void tcg_out_modrm_sib_offset(TCGContext *s, int opc, int r, int rm,
426
                                     int index, int shift,
427
                                     tcg_target_long offset)
428
{
429
    int mod, len;
430

    
431
    if (index < 0 && rm < 0) {
432
        if (TCG_TARGET_REG_BITS == 64) {
433
            /* Try for a rip-relative addressing mode.  This has replaced
434
               the 32-bit-mode absolute addressing encoding.  */
435
            tcg_target_long pc = (tcg_target_long)s->code_ptr + 5 + ~rm;
436
            tcg_target_long disp = offset - pc;
437
            if (disp == (int32_t)disp) {
438
                tcg_out_opc(s, opc, r, 0, 0);
439
                tcg_out8(s, (LOWREGMASK(r) << 3) | 5);
440
                tcg_out32(s, disp);
441
                return;
442
            }
443

    
444
            /* Try for an absolute address encoding.  This requires the
445
               use of the MODRM+SIB encoding and is therefore larger than
446
               rip-relative addressing.  */
447
            if (offset == (int32_t)offset) {
448
                tcg_out_opc(s, opc, r, 0, 0);
449
                tcg_out8(s, (LOWREGMASK(r) << 3) | 4);
450
                tcg_out8(s, (4 << 3) | 5);
451
                tcg_out32(s, offset);
452
                return;
453
            }
454

    
455
            /* ??? The memory isn't directly addressable.  */
456
            tcg_abort();
457
        } else {
458
            /* Absolute address.  */
459
            tcg_out_opc(s, opc, r, 0, 0);
460
            tcg_out8(s, (r << 3) | 5);
461
            tcg_out32(s, offset);
462
            return;
463
        }
464
    }
465

    
466
    /* Find the length of the immediate addend.  Note that the encoding
467
       that would be used for (%ebp) indicates absolute addressing.  */
468
    if (rm < 0) {
469
        mod = 0, len = 4, rm = 5;
470
    } else if (offset == 0 && LOWREGMASK(rm) != TCG_REG_EBP) {
471
        mod = 0, len = 0;
472
    } else if (offset == (int8_t)offset) {
473
        mod = 0x40, len = 1;
474
    } else {
475
        mod = 0x80, len = 4;
476
    }
477

    
478
    /* Use a single byte MODRM format if possible.  Note that the encoding
479
       that would be used for %esp is the escape to the two byte form.  */
480
    if (index < 0 && LOWREGMASK(rm) != TCG_REG_ESP) {
481
        /* Single byte MODRM format.  */
482
        tcg_out_opc(s, opc, r, rm, 0);
483
        tcg_out8(s, mod | (LOWREGMASK(r) << 3) | LOWREGMASK(rm));
484
    } else {
485
        /* Two byte MODRM+SIB format.  */
486

    
487
        /* Note that the encoding that would place %esp into the index
488
           field indicates no index register.  In 64-bit mode, the REX.X
489
           bit counts, so %r12 can be used as the index.  */
490
        if (index < 0) {
491
            index = 4;
492
        } else {
493
            assert(index != TCG_REG_ESP);
494
        }
495

    
496
        tcg_out_opc(s, opc, r, rm, index);
497
        tcg_out8(s, mod | (LOWREGMASK(r) << 3) | 4);
498
        tcg_out8(s, (shift << 6) | (LOWREGMASK(index) << 3) | LOWREGMASK(rm));
499
    }
500

    
501
    if (len == 1) {
502
        tcg_out8(s, offset);
503
    } else if (len == 4) {
504
        tcg_out32(s, offset);
505
    }
506
}
507

    
508
/* A simplification of the above with no index or shift.  */
509
static inline void tcg_out_modrm_offset(TCGContext *s, int opc, int r,
510
                                        int rm, tcg_target_long offset)
511
{
512
    tcg_out_modrm_sib_offset(s, opc, r, rm, -1, 0, offset);
513
}
514

    
515
/* Generate dest op= src.  Uses the same ARITH_* codes as tgen_arithi.  */
516
static inline void tgen_arithr(TCGContext *s, int subop, int dest, int src)
517
{
518
    /* Propagate an opcode prefix, such as P_REXW.  */
519
    int ext = subop & ~0x7;
520
    subop &= 0x7;
521

    
522
    tcg_out_modrm(s, OPC_ARITH_GvEv + (subop << 3) + ext, dest, src);
523
}
524

    
525
static inline void tcg_out_mov(TCGContext *s, TCGType type,
526
                               TCGReg ret, TCGReg arg)
527
{
528
    if (arg != ret) {
529
        int opc = OPC_MOVL_GvEv + (type == TCG_TYPE_I64 ? P_REXW : 0);
530
        tcg_out_modrm(s, opc, ret, arg);
531
    }
532
}
533

    
534
static void tcg_out_movi(TCGContext *s, TCGType type,
535
                         TCGReg ret, tcg_target_long arg)
536
{
537
    if (arg == 0) {
538
        tgen_arithr(s, ARITH_XOR, ret, ret);
539
        return;
540
    } else if (arg == (uint32_t)arg || type == TCG_TYPE_I32) {
541
        tcg_out_opc(s, OPC_MOVL_Iv + LOWREGMASK(ret), 0, ret, 0);
542
        tcg_out32(s, arg);
543
    } else if (arg == (int32_t)arg) {
544
        tcg_out_modrm(s, OPC_MOVL_EvIz + P_REXW, 0, ret);
545
        tcg_out32(s, arg);
546
    } else {
547
        tcg_out_opc(s, OPC_MOVL_Iv + P_REXW + LOWREGMASK(ret), 0, ret, 0);
548
        tcg_out32(s, arg);
549
        tcg_out32(s, arg >> 31 >> 1);
550
    }
551
}
552

    
553
static inline void tcg_out_pushi(TCGContext *s, tcg_target_long val)
554
{
555
    if (val == (int8_t)val) {
556
        tcg_out_opc(s, OPC_PUSH_Ib, 0, 0, 0);
557
        tcg_out8(s, val);
558
    } else if (val == (int32_t)val) {
559
        tcg_out_opc(s, OPC_PUSH_Iv, 0, 0, 0);
560
        tcg_out32(s, val);
561
    } else {
562
        tcg_abort();
563
    }
564
}
565

    
566
static inline void tcg_out_push(TCGContext *s, int reg)
567
{
568
    tcg_out_opc(s, OPC_PUSH_r32 + LOWREGMASK(reg), 0, reg, 0);
569
}
570

    
571
static inline void tcg_out_pop(TCGContext *s, int reg)
572
{
573
    tcg_out_opc(s, OPC_POP_r32 + LOWREGMASK(reg), 0, reg, 0);
574
}
575

    
576
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
577
                              TCGReg arg1, tcg_target_long arg2)
578
{
579
    int opc = OPC_MOVL_GvEv + (type == TCG_TYPE_I64 ? P_REXW : 0);
580
    tcg_out_modrm_offset(s, opc, ret, arg1, arg2);
581
}
582

    
583
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
584
                              TCGReg arg1, tcg_target_long arg2)
585
{
586
    int opc = OPC_MOVL_EvGv + (type == TCG_TYPE_I64 ? P_REXW : 0);
587
    tcg_out_modrm_offset(s, opc, arg, arg1, arg2);
588
}
589

    
590
static void tcg_out_shifti(TCGContext *s, int subopc, int reg, int count)
591
{
592
    /* Propagate an opcode prefix, such as P_DATA16.  */
593
    int ext = subopc & ~0x7;
594
    subopc &= 0x7;
595

    
596
    if (count == 1) {
597
        tcg_out_modrm(s, OPC_SHIFT_1 + ext, subopc, reg);
598
    } else {
599
        tcg_out_modrm(s, OPC_SHIFT_Ib + ext, subopc, reg);
600
        tcg_out8(s, count);
601
    }
602
}
603

    
604
static inline void tcg_out_bswap32(TCGContext *s, int reg)
605
{
606
    tcg_out_opc(s, OPC_BSWAP + LOWREGMASK(reg), 0, reg, 0);
607
}
608

    
609
static inline void tcg_out_rolw_8(TCGContext *s, int reg)
610
{
611
    tcg_out_shifti(s, SHIFT_ROL + P_DATA16, reg, 8);
612
}
613

    
614
static inline void tcg_out_ext8u(TCGContext *s, int dest, int src)
615
{
616
    /* movzbl */
617
    assert(src < 4 || TCG_TARGET_REG_BITS == 64);
618
    tcg_out_modrm(s, OPC_MOVZBL + P_REXB_RM, dest, src);
619
}
620

    
621
static void tcg_out_ext8s(TCGContext *s, int dest, int src, int rexw)
622
{
623
    /* movsbl */
624
    assert(src < 4 || TCG_TARGET_REG_BITS == 64);
625
    tcg_out_modrm(s, OPC_MOVSBL + P_REXB_RM + rexw, dest, src);
626
}
627

    
628
static inline void tcg_out_ext16u(TCGContext *s, int dest, int src)
629
{
630
    /* movzwl */
631
    tcg_out_modrm(s, OPC_MOVZWL, dest, src);
632
}
633

    
634
static inline void tcg_out_ext16s(TCGContext *s, int dest, int src, int rexw)
635
{
636
    /* movsw[lq] */
637
    tcg_out_modrm(s, OPC_MOVSWL + rexw, dest, src);
638
}
639

    
640
static inline void tcg_out_ext32u(TCGContext *s, int dest, int src)
641
{
642
    /* 32-bit mov zero extends.  */
643
    tcg_out_modrm(s, OPC_MOVL_GvEv, dest, src);
644
}
645

    
646
static inline void tcg_out_ext32s(TCGContext *s, int dest, int src)
647
{
648
    tcg_out_modrm(s, OPC_MOVSLQ, dest, src);
649
}
650

    
651
static inline void tcg_out_bswap64(TCGContext *s, int reg)
652
{
653
    tcg_out_opc(s, OPC_BSWAP + P_REXW + LOWREGMASK(reg), 0, reg, 0);
654
}
655

    
656
static void tgen_arithi(TCGContext *s, int c, int r0,
657
                        tcg_target_long val, int cf)
658
{
659
    int rexw = 0;
660

    
661
    if (TCG_TARGET_REG_BITS == 64) {
662
        rexw = c & -8;
663
        c &= 7;
664
    }
665

    
666
    /* ??? While INC is 2 bytes shorter than ADDL $1, they also induce
667
       partial flags update stalls on Pentium4 and are not recommended
668
       by current Intel optimization manuals.  */
669
    if (!cf && (c == ARITH_ADD || c == ARITH_SUB) && (val == 1 || val == -1)) {
670
        int is_inc = (c == ARITH_ADD) ^ (val < 0);
671
        if (TCG_TARGET_REG_BITS == 64) {
672
            /* The single-byte increment encodings are re-tasked as the
673
               REX prefixes.  Use the MODRM encoding.  */
674
            tcg_out_modrm(s, OPC_GRP5 + rexw,
675
                          (is_inc ? EXT5_INC_Ev : EXT5_DEC_Ev), r0);
676
        } else {
677
            tcg_out8(s, (is_inc ? OPC_INC_r32 : OPC_DEC_r32) + r0);
678
        }
679
        return;
680
    }
681

    
682
    if (c == ARITH_AND) {
683
        if (TCG_TARGET_REG_BITS == 64) {
684
            if (val == 0xffffffffu) {
685
                tcg_out_ext32u(s, r0, r0);
686
                return;
687
            }
688
            if (val == (uint32_t)val) {
689
                /* AND with no high bits set can use a 32-bit operation.  */
690
                rexw = 0;
691
            }
692
        }
693
        if (val == 0xffu && (r0 < 4 || TCG_TARGET_REG_BITS == 64)) {
694
            tcg_out_ext8u(s, r0, r0);
695
            return;
696
        }
697
        if (val == 0xffffu) {
698
            tcg_out_ext16u(s, r0, r0);
699
            return;
700
        }
701
    }
702

    
703
    if (val == (int8_t)val) {
704
        tcg_out_modrm(s, OPC_ARITH_EvIb + rexw, c, r0);
705
        tcg_out8(s, val);
706
        return;
707
    }
708
    if (rexw == 0 || val == (int32_t)val) {
709
        tcg_out_modrm(s, OPC_ARITH_EvIz + rexw, c, r0);
710
        tcg_out32(s, val);
711
        return;
712
    }
713

    
714
    tcg_abort();
715
}
716

    
717
static void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
718
{
719
    if (val != 0) {
720
        tgen_arithi(s, ARITH_ADD + P_REXW, reg, val, 0);
721
    }
722
}
723

    
724
/* Use SMALL != 0 to force a short forward branch.  */
725
static void tcg_out_jxx(TCGContext *s, int opc, int label_index, int small)
726
{
727
    int32_t val, val1;
728
    TCGLabel *l = &s->labels[label_index];
729

    
730
    if (l->has_value) {
731
        val = l->u.value - (tcg_target_long)s->code_ptr;
732
        val1 = val - 2;
733
        if ((int8_t)val1 == val1) {
734
            if (opc == -1) {
735
                tcg_out8(s, OPC_JMP_short);
736
            } else {
737
                tcg_out8(s, OPC_JCC_short + opc);
738
            }
739
            tcg_out8(s, val1);
740
        } else {
741
            if (small) {
742
                tcg_abort();
743
            }
744
            if (opc == -1) {
745
                tcg_out8(s, OPC_JMP_long);
746
                tcg_out32(s, val - 5);
747
            } else {
748
                tcg_out_opc(s, OPC_JCC_long + opc, 0, 0, 0);
749
                tcg_out32(s, val - 6);
750
            }
751
        }
752
    } else if (small) {
753
        if (opc == -1) {
754
            tcg_out8(s, OPC_JMP_short);
755
        } else {
756
            tcg_out8(s, OPC_JCC_short + opc);
757
        }
758
        tcg_out_reloc(s, s->code_ptr, R_386_PC8, label_index, -1);
759
        s->code_ptr += 1;
760
    } else {
761
        if (opc == -1) {
762
            tcg_out8(s, OPC_JMP_long);
763
        } else {
764
            tcg_out_opc(s, OPC_JCC_long + opc, 0, 0, 0);
765
        }
766
        tcg_out_reloc(s, s->code_ptr, R_386_PC32, label_index, -4);
767
        s->code_ptr += 4;
768
    }
769
}
770

    
771
static void tcg_out_cmp(TCGContext *s, TCGArg arg1, TCGArg arg2,
772
                        int const_arg2, int rexw)
773
{
774
    if (const_arg2) {
775
        if (arg2 == 0) {
776
            /* test r, r */
777
            tcg_out_modrm(s, OPC_TESTL + rexw, arg1, arg1);
778
        } else {
779
            tgen_arithi(s, ARITH_CMP + rexw, arg1, arg2, 0);
780
        }
781
    } else {
782
        tgen_arithr(s, ARITH_CMP + rexw, arg1, arg2);
783
    }
784
}
785

    
786
static void tcg_out_brcond32(TCGContext *s, TCGCond cond,
787
                             TCGArg arg1, TCGArg arg2, int const_arg2,
788
                             int label_index, int small)
789
{
790
    tcg_out_cmp(s, arg1, arg2, const_arg2, 0);
791
    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, small);
792
}
793

    
794
#if TCG_TARGET_REG_BITS == 64
795
static void tcg_out_brcond64(TCGContext *s, TCGCond cond,
796
                             TCGArg arg1, TCGArg arg2, int const_arg2,
797
                             int label_index, int small)
798
{
799
    tcg_out_cmp(s, arg1, arg2, const_arg2, P_REXW);
800
    tcg_out_jxx(s, tcg_cond_to_jcc[cond], label_index, small);
801
}
802
#else
803
/* XXX: we implement it at the target level to avoid having to
804
   handle cross basic blocks temporaries */
805
static void tcg_out_brcond2(TCGContext *s, const TCGArg *args,
806
                            const int *const_args, int small)
807
{
808
    int label_next;
809
    label_next = gen_new_label();
810
    switch(args[4]) {
811
    case TCG_COND_EQ:
812
        tcg_out_brcond32(s, TCG_COND_NE, args[0], args[2], const_args[2],
813
                         label_next, 1);
814
        tcg_out_brcond32(s, TCG_COND_EQ, args[1], args[3], const_args[3],
815
                         args[5], small);
816
        break;
817
    case TCG_COND_NE:
818
        tcg_out_brcond32(s, TCG_COND_NE, args[0], args[2], const_args[2],
819
                         args[5], small);
820
        tcg_out_brcond32(s, TCG_COND_NE, args[1], args[3], const_args[3],
821
                         args[5], small);
822
        break;
823
    case TCG_COND_LT:
824
        tcg_out_brcond32(s, TCG_COND_LT, args[1], args[3], const_args[3],
825
                         args[5], small);
826
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
827
        tcg_out_brcond32(s, TCG_COND_LTU, args[0], args[2], const_args[2],
828
                         args[5], small);
829
        break;
830
    case TCG_COND_LE:
831
        tcg_out_brcond32(s, TCG_COND_LT, args[1], args[3], const_args[3],
832
                         args[5], small);
833
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
834
        tcg_out_brcond32(s, TCG_COND_LEU, args[0], args[2], const_args[2],
835
                         args[5], small);
836
        break;
837
    case TCG_COND_GT:
838
        tcg_out_brcond32(s, TCG_COND_GT, args[1], args[3], const_args[3],
839
                         args[5], small);
840
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
841
        tcg_out_brcond32(s, TCG_COND_GTU, args[0], args[2], const_args[2],
842
                         args[5], small);
843
        break;
844
    case TCG_COND_GE:
845
        tcg_out_brcond32(s, TCG_COND_GT, args[1], args[3], const_args[3],
846
                         args[5], small);
847
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
848
        tcg_out_brcond32(s, TCG_COND_GEU, args[0], args[2], const_args[2],
849
                         args[5], small);
850
        break;
851
    case TCG_COND_LTU:
852
        tcg_out_brcond32(s, TCG_COND_LTU, args[1], args[3], const_args[3],
853
                         args[5], small);
854
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
855
        tcg_out_brcond32(s, TCG_COND_LTU, args[0], args[2], const_args[2],
856
                         args[5], small);
857
        break;
858
    case TCG_COND_LEU:
859
        tcg_out_brcond32(s, TCG_COND_LTU, args[1], args[3], const_args[3],
860
                         args[5], small);
861
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
862
        tcg_out_brcond32(s, TCG_COND_LEU, args[0], args[2], const_args[2],
863
                         args[5], small);
864
        break;
865
    case TCG_COND_GTU:
866
        tcg_out_brcond32(s, TCG_COND_GTU, args[1], args[3], const_args[3],
867
                         args[5], small);
868
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
869
        tcg_out_brcond32(s, TCG_COND_GTU, args[0], args[2], const_args[2],
870
                         args[5], small);
871
        break;
872
    case TCG_COND_GEU:
873
        tcg_out_brcond32(s, TCG_COND_GTU, args[1], args[3], const_args[3],
874
                         args[5], small);
875
        tcg_out_jxx(s, JCC_JNE, label_next, 1);
876
        tcg_out_brcond32(s, TCG_COND_GEU, args[0], args[2], const_args[2],
877
                         args[5], small);
878
        break;
879
    default:
880
        tcg_abort();
881
    }
882
    tcg_out_label(s, label_next, s->code_ptr);
883
}
884
#endif
885

    
886
static void tcg_out_setcond32(TCGContext *s, TCGCond cond, TCGArg dest,
887
                              TCGArg arg1, TCGArg arg2, int const_arg2)
888
{
889
    tcg_out_cmp(s, arg1, arg2, const_arg2, 0);
890
    tcg_out_modrm(s, OPC_SETCC | tcg_cond_to_jcc[cond], 0, dest);
891
    tcg_out_ext8u(s, dest, dest);
892
}
893

    
894
#if TCG_TARGET_REG_BITS == 64
895
static void tcg_out_setcond64(TCGContext *s, TCGCond cond, TCGArg dest,
896
                              TCGArg arg1, TCGArg arg2, int const_arg2)
897
{
898
    tcg_out_cmp(s, arg1, arg2, const_arg2, P_REXW);
899
    tcg_out_modrm(s, OPC_SETCC | tcg_cond_to_jcc[cond], 0, dest);
900
    tcg_out_ext8u(s, dest, dest);
901
}
902
#else
903
static void tcg_out_setcond2(TCGContext *s, const TCGArg *args,
904
                             const int *const_args)
905
{
906
    TCGArg new_args[6];
907
    int label_true, label_over;
908

    
909
    memcpy(new_args, args+1, 5*sizeof(TCGArg));
910

    
911
    if (args[0] == args[1] || args[0] == args[2]
912
        || (!const_args[3] && args[0] == args[3])
913
        || (!const_args[4] && args[0] == args[4])) {
914
        /* When the destination overlaps with one of the argument
915
           registers, don't do anything tricky.  */
916
        label_true = gen_new_label();
917
        label_over = gen_new_label();
918

    
919
        new_args[5] = label_true;
920
        tcg_out_brcond2(s, new_args, const_args+1, 1);
921

    
922
        tcg_out_movi(s, TCG_TYPE_I32, args[0], 0);
923
        tcg_out_jxx(s, JCC_JMP, label_over, 1);
924
        tcg_out_label(s, label_true, s->code_ptr);
925

    
926
        tcg_out_movi(s, TCG_TYPE_I32, args[0], 1);
927
        tcg_out_label(s, label_over, s->code_ptr);
928
    } else {
929
        /* When the destination does not overlap one of the arguments,
930
           clear the destination first, jump if cond false, and emit an
931
           increment in the true case.  This results in smaller code.  */
932

    
933
        tcg_out_movi(s, TCG_TYPE_I32, args[0], 0);
934

    
935
        label_over = gen_new_label();
936
        new_args[4] = tcg_invert_cond(new_args[4]);
937
        new_args[5] = label_over;
938
        tcg_out_brcond2(s, new_args, const_args+1, 1);
939

    
940
        tgen_arithi(s, ARITH_ADD, args[0], 1, 0);
941
        tcg_out_label(s, label_over, s->code_ptr);
942
    }
943
}
944
#endif
945

    
946
static void tcg_out_movcond32(TCGContext *s, TCGCond cond, TCGArg dest,
947
                              TCGArg c1, TCGArg c2, int const_c2,
948
                              TCGArg v1)
949
{
950
    tcg_out_cmp(s, c1, c2, const_c2, 0);
951
    tcg_out_modrm(s, OPC_CMOVCC | tcg_cond_to_jcc[cond], dest, v1);
952
}
953

    
954
#if TCG_TARGET_REG_BITS == 64
955
static void tcg_out_movcond64(TCGContext *s, TCGCond cond, TCGArg dest,
956
                              TCGArg c1, TCGArg c2, int const_c2,
957
                              TCGArg v1)
958
{
959
    tcg_out_cmp(s, c1, c2, const_c2, P_REXW);
960
    tcg_out_modrm(s, OPC_CMOVCC | tcg_cond_to_jcc[cond] | P_REXW, dest, v1);
961
}
962
#endif
963

    
964
static void tcg_out_branch(TCGContext *s, int call, tcg_target_long dest)
965
{
966
    tcg_target_long disp = dest - (tcg_target_long)s->code_ptr - 5;
967

    
968
    if (disp == (int32_t)disp) {
969
        tcg_out_opc(s, call ? OPC_CALL_Jz : OPC_JMP_long, 0, 0, 0);
970
        tcg_out32(s, disp);
971
    } else {
972
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R10, dest);
973
        tcg_out_modrm(s, OPC_GRP5,
974
                      call ? EXT5_CALLN_Ev : EXT5_JMPN_Ev, TCG_REG_R10);
975
    }
976
}
977

    
978
static inline void tcg_out_calli(TCGContext *s, tcg_target_long dest)
979
{
980
    tcg_out_branch(s, 1, dest);
981
}
982

    
983
static void tcg_out_jmp(TCGContext *s, tcg_target_long dest)
984
{
985
    tcg_out_branch(s, 0, dest);
986
}
987

    
988
#if defined(CONFIG_SOFTMMU)
989

    
990
#include "../../softmmu_defs.h"
991

    
992
/* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
993
   int mmu_idx) */
994
static const void *qemu_ld_helpers[4] = {
995
    helper_ldb_mmu,
996
    helper_ldw_mmu,
997
    helper_ldl_mmu,
998
    helper_ldq_mmu,
999
};
1000

    
1001
/* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
1002
   uintxx_t val, int mmu_idx) */
1003
static const void *qemu_st_helpers[4] = {
1004
    helper_stb_mmu,
1005
    helper_stw_mmu,
1006
    helper_stl_mmu,
1007
    helper_stq_mmu,
1008
};
1009

    
1010
/* Perform the TLB load and compare.
1011

1012
   Inputs:
1013
   ADDRLO_IDX contains the index into ARGS of the low part of the
1014
   address; the high part of the address is at ADDR_LOW_IDX+1.
1015

1016
   MEM_INDEX and S_BITS are the memory context and log2 size of the load.
1017

1018
   WHICH is the offset into the CPUTLBEntry structure of the slot to read.
1019
   This should be offsetof addr_read or addr_write.
1020

1021
   Outputs:
1022
   LABEL_PTRS is filled with 1 (32-bit addresses) or 2 (64-bit addresses)
1023
   positions of the displacements of forward jumps to the TLB miss case.
1024

1025
   First argument register is loaded with the low part of the address.
1026
   In the TLB hit case, it has been adjusted as indicated by the TLB
1027
   and so is a host address.  In the TLB miss case, it continues to
1028
   hold a guest address.
1029

1030
   Second argument register is clobbered.  */
1031

    
1032
static inline void tcg_out_tlb_load(TCGContext *s, int addrlo_idx,
1033
                                    int mem_index, int s_bits,
1034
                                    const TCGArg *args,
1035
                                    uint8_t **label_ptr, int which)
1036
{
1037
    const int addrlo = args[addrlo_idx];
1038
    const int r0 = TCG_REG_L0;
1039
    const int r1 = TCG_REG_L1;
1040
    TCGType type = TCG_TYPE_I32;
1041
    int rexw = 0;
1042

    
1043
    if (TCG_TARGET_REG_BITS == 64 && TARGET_LONG_BITS == 64) {
1044
        type = TCG_TYPE_I64;
1045
        rexw = P_REXW;
1046
    }
1047

    
1048
    tcg_out_mov(s, type, r1, addrlo);
1049
    tcg_out_mov(s, type, r0, addrlo);
1050

    
1051
    tcg_out_shifti(s, SHIFT_SHR + rexw, r1,
1052
                   TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1053

    
1054
    tgen_arithi(s, ARITH_AND + rexw, r0,
1055
                TARGET_PAGE_MASK | ((1 << s_bits) - 1), 0);
1056
    tgen_arithi(s, ARITH_AND + rexw, r1,
1057
                (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS, 0);
1058

    
1059
    tcg_out_modrm_sib_offset(s, OPC_LEA + P_REXW, r1, TCG_AREG0, r1, 0,
1060
                             offsetof(CPUArchState, tlb_table[mem_index][0])
1061
                             + which);
1062

    
1063
    /* cmp 0(r1), r0 */
1064
    tcg_out_modrm_offset(s, OPC_CMP_GvEv + rexw, r0, r1, 0);
1065

    
1066
    tcg_out_mov(s, type, r0, addrlo);
1067

    
1068
    /* jne label1 */
1069
    tcg_out8(s, OPC_JCC_short + JCC_JNE);
1070
    label_ptr[0] = s->code_ptr;
1071
    s->code_ptr++;
1072

    
1073
    if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
1074
        /* cmp 4(r1), addrhi */
1075
        tcg_out_modrm_offset(s, OPC_CMP_GvEv, args[addrlo_idx+1], r1, 4);
1076

    
1077
        /* jne label1 */
1078
        tcg_out8(s, OPC_JCC_short + JCC_JNE);
1079
        label_ptr[1] = s->code_ptr;
1080
        s->code_ptr++;
1081
    }
1082

    
1083
    /* TLB Hit.  */
1084

    
1085
    /* add addend(r1), r0 */
1086
    tcg_out_modrm_offset(s, OPC_ADD_GvEv + P_REXW, r0, r1,
1087
                         offsetof(CPUTLBEntry, addend) - which);
1088
}
1089
#endif
1090

    
1091
static void tcg_out_qemu_ld_direct(TCGContext *s, int datalo, int datahi,
1092
                                   int base, tcg_target_long ofs, int sizeop)
1093
{
1094
#ifdef TARGET_WORDS_BIGENDIAN
1095
    const int bswap = 1;
1096
#else
1097
    const int bswap = 0;
1098
#endif
1099
    switch (sizeop) {
1100
    case 0:
1101
        tcg_out_modrm_offset(s, OPC_MOVZBL, datalo, base, ofs);
1102
        break;
1103
    case 0 | 4:
1104
        tcg_out_modrm_offset(s, OPC_MOVSBL + P_REXW, datalo, base, ofs);
1105
        break;
1106
    case 1:
1107
        tcg_out_modrm_offset(s, OPC_MOVZWL, datalo, base, ofs);
1108
        if (bswap) {
1109
            tcg_out_rolw_8(s, datalo);
1110
        }
1111
        break;
1112
    case 1 | 4:
1113
        if (bswap) {
1114
            tcg_out_modrm_offset(s, OPC_MOVZWL, datalo, base, ofs);
1115
            tcg_out_rolw_8(s, datalo);
1116
            tcg_out_modrm(s, OPC_MOVSWL + P_REXW, datalo, datalo);
1117
        } else {
1118
            tcg_out_modrm_offset(s, OPC_MOVSWL + P_REXW, datalo, base, ofs);
1119
        }
1120
        break;
1121
    case 2:
1122
        tcg_out_ld(s, TCG_TYPE_I32, datalo, base, ofs);
1123
        if (bswap) {
1124
            tcg_out_bswap32(s, datalo);
1125
        }
1126
        break;
1127
#if TCG_TARGET_REG_BITS == 64
1128
    case 2 | 4:
1129
        if (bswap) {
1130
            tcg_out_ld(s, TCG_TYPE_I32, datalo, base, ofs);
1131
            tcg_out_bswap32(s, datalo);
1132
            tcg_out_ext32s(s, datalo, datalo);
1133
        } else {
1134
            tcg_out_modrm_offset(s, OPC_MOVSLQ, datalo, base, ofs);
1135
        }
1136
        break;
1137
#endif
1138
    case 3:
1139
        if (TCG_TARGET_REG_BITS == 64) {
1140
            tcg_out_ld(s, TCG_TYPE_I64, datalo, base, ofs);
1141
            if (bswap) {
1142
                tcg_out_bswap64(s, datalo);
1143
            }
1144
        } else {
1145
            if (bswap) {
1146
                int t = datalo;
1147
                datalo = datahi;
1148
                datahi = t;
1149
            }
1150
            if (base != datalo) {
1151
                tcg_out_ld(s, TCG_TYPE_I32, datalo, base, ofs);
1152
                tcg_out_ld(s, TCG_TYPE_I32, datahi, base, ofs + 4);
1153
            } else {
1154
                tcg_out_ld(s, TCG_TYPE_I32, datahi, base, ofs + 4);
1155
                tcg_out_ld(s, TCG_TYPE_I32, datalo, base, ofs);
1156
            }
1157
            if (bswap) {
1158
                tcg_out_bswap32(s, datalo);
1159
                tcg_out_bswap32(s, datahi);
1160
            }
1161
        }
1162
        break;
1163
    default:
1164
        tcg_abort();
1165
    }
1166
}
1167

    
1168
/* XXX: qemu_ld and qemu_st could be modified to clobber only EDX and
1169
   EAX. It will be useful once fixed registers globals are less
1170
   common. */
1171
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
1172
                            int opc)
1173
{
1174
    int data_reg, data_reg2 = 0;
1175
    int addrlo_idx;
1176
#if defined(CONFIG_SOFTMMU)
1177
    int mem_index, s_bits;
1178
#if TCG_TARGET_REG_BITS == 64
1179
    int arg_idx;
1180
#else
1181
    int stack_adjust;
1182
#endif
1183
    uint8_t *label_ptr[3];
1184
#endif
1185

    
1186
    data_reg = args[0];
1187
    addrlo_idx = 1;
1188
    if (TCG_TARGET_REG_BITS == 32 && opc == 3) {
1189
        data_reg2 = args[1];
1190
        addrlo_idx = 2;
1191
    }
1192

    
1193
#if defined(CONFIG_SOFTMMU)
1194
    mem_index = args[addrlo_idx + 1 + (TARGET_LONG_BITS > TCG_TARGET_REG_BITS)];
1195
    s_bits = opc & 3;
1196

    
1197
    tcg_out_tlb_load(s, addrlo_idx, mem_index, s_bits, args,
1198
                     label_ptr, offsetof(CPUTLBEntry, addr_read));
1199

    
1200
    /* TLB Hit.  */
1201
    tcg_out_qemu_ld_direct(s, data_reg, data_reg2, TCG_REG_L0, 0, opc);
1202

    
1203
    /* jmp label2 */
1204
    tcg_out8(s, OPC_JMP_short);
1205
    label_ptr[2] = s->code_ptr;
1206
    s->code_ptr++;
1207

    
1208
    /* TLB Miss.  */
1209

    
1210
    /* label1: */
1211
    *label_ptr[0] = s->code_ptr - label_ptr[0] - 1;
1212
    if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
1213
        *label_ptr[1] = s->code_ptr - label_ptr[1] - 1;
1214
    }
1215

    
1216
    /* XXX: move that code at the end of the TB */
1217
#if TCG_TARGET_REG_BITS == 32
1218
    tcg_out_pushi(s, mem_index);
1219
    stack_adjust = 4;
1220
    if (TARGET_LONG_BITS == 64) {
1221
        tcg_out_push(s, args[addrlo_idx + 1]);
1222
        stack_adjust += 4;
1223
    }
1224
    tcg_out_push(s, args[addrlo_idx]);
1225
    stack_adjust += 4;
1226
    tcg_out_push(s, TCG_AREG0);
1227
    stack_adjust += 4;
1228
#else
1229
    /* The first argument is already loaded with addrlo.  */
1230
    arg_idx = 1;
1231
    tcg_out_movi(s, TCG_TYPE_I32, tcg_target_call_iarg_regs[arg_idx],
1232
                 mem_index);
1233
    /* XXX/FIXME: suboptimal */
1234
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[3], TCG_REG_L2);
1235
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[2], TCG_REG_L1);
1236
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[1], TCG_REG_L0);
1237
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[0], TCG_AREG0);
1238
#endif
1239

    
1240
    tcg_out_calli(s, (tcg_target_long)qemu_ld_helpers[s_bits]);
1241

    
1242
#if TCG_TARGET_REG_BITS == 32
1243
    if (stack_adjust == (TCG_TARGET_REG_BITS / 8)) {
1244
        /* Pop and discard.  This is 2 bytes smaller than the add.  */
1245
        tcg_out_pop(s, TCG_REG_ECX);
1246
    } else if (stack_adjust != 0) {
1247
        tcg_out_addi(s, TCG_REG_CALL_STACK, stack_adjust);
1248
    }
1249
#endif
1250

    
1251
    switch(opc) {
1252
    case 0 | 4:
1253
        tcg_out_ext8s(s, data_reg, TCG_REG_EAX, P_REXW);
1254
        break;
1255
    case 1 | 4:
1256
        tcg_out_ext16s(s, data_reg, TCG_REG_EAX, P_REXW);
1257
        break;
1258
    case 0:
1259
        tcg_out_ext8u(s, data_reg, TCG_REG_EAX);
1260
        break;
1261
    case 1:
1262
        tcg_out_ext16u(s, data_reg, TCG_REG_EAX);
1263
        break;
1264
    case 2:
1265
        tcg_out_mov(s, TCG_TYPE_I32, data_reg, TCG_REG_EAX);
1266
        break;
1267
#if TCG_TARGET_REG_BITS == 64
1268
    case 2 | 4:
1269
        tcg_out_ext32s(s, data_reg, TCG_REG_EAX);
1270
        break;
1271
#endif
1272
    case 3:
1273
        if (TCG_TARGET_REG_BITS == 64) {
1274
            tcg_out_mov(s, TCG_TYPE_I64, data_reg, TCG_REG_RAX);
1275
        } else if (data_reg == TCG_REG_EDX) {
1276
            /* xchg %edx, %eax */
1277
            tcg_out_opc(s, OPC_XCHG_ax_r32 + TCG_REG_EDX, 0, 0, 0);
1278
            tcg_out_mov(s, TCG_TYPE_I32, data_reg2, TCG_REG_EAX);
1279
        } else {
1280
            tcg_out_mov(s, TCG_TYPE_I32, data_reg, TCG_REG_EAX);
1281
            tcg_out_mov(s, TCG_TYPE_I32, data_reg2, TCG_REG_EDX);
1282
        }
1283
        break;
1284
    default:
1285
        tcg_abort();
1286
    }
1287

    
1288
    /* label2: */
1289
    *label_ptr[2] = s->code_ptr - label_ptr[2] - 1;
1290
#else
1291
    {
1292
        int32_t offset = GUEST_BASE;
1293
        int base = args[addrlo_idx];
1294

    
1295
        if (TCG_TARGET_REG_BITS == 64) {
1296
            /* ??? We assume all operations have left us with register
1297
               contents that are zero extended.  So far this appears to
1298
               be true.  If we want to enforce this, we can either do
1299
               an explicit zero-extension here, or (if GUEST_BASE == 0)
1300
               use the ADDR32 prefix.  For now, do nothing.  */
1301

    
1302
            if (offset != GUEST_BASE) {
1303
                tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_L0, GUEST_BASE);
1304
                tgen_arithr(s, ARITH_ADD + P_REXW, TCG_REG_L0, base);
1305
                base = TCG_REG_L0;
1306
                offset = 0;
1307
            }
1308
        }
1309

    
1310
        tcg_out_qemu_ld_direct(s, data_reg, data_reg2, base, offset, opc);
1311
    }
1312
#endif
1313
}
1314

    
1315
static void tcg_out_qemu_st_direct(TCGContext *s, int datalo, int datahi,
1316
                                   int base, tcg_target_long ofs, int sizeop)
1317
{
1318
#ifdef TARGET_WORDS_BIGENDIAN
1319
    const int bswap = 1;
1320
#else
1321
    const int bswap = 0;
1322
#endif
1323
    /* ??? Ideally we wouldn't need a scratch register.  For user-only,
1324
       we could perform the bswap twice to restore the original value
1325
       instead of moving to the scratch.  But as it is, the L constraint
1326
       means that TCG_REG_L1 is definitely free here.  */
1327
    const int scratch = TCG_REG_L1;
1328

    
1329
    switch (sizeop) {
1330
    case 0:
1331
        tcg_out_modrm_offset(s, OPC_MOVB_EvGv + P_REXB_R, datalo, base, ofs);
1332
        break;
1333
    case 1:
1334
        if (bswap) {
1335
            tcg_out_mov(s, TCG_TYPE_I32, scratch, datalo);
1336
            tcg_out_rolw_8(s, scratch);
1337
            datalo = scratch;
1338
        }
1339
        tcg_out_modrm_offset(s, OPC_MOVL_EvGv + P_DATA16, datalo, base, ofs);
1340
        break;
1341
    case 2:
1342
        if (bswap) {
1343
            tcg_out_mov(s, TCG_TYPE_I32, scratch, datalo);
1344
            tcg_out_bswap32(s, scratch);
1345
            datalo = scratch;
1346
        }
1347
        tcg_out_st(s, TCG_TYPE_I32, datalo, base, ofs);
1348
        break;
1349
    case 3:
1350
        if (TCG_TARGET_REG_BITS == 64) {
1351
            if (bswap) {
1352
                tcg_out_mov(s, TCG_TYPE_I64, scratch, datalo);
1353
                tcg_out_bswap64(s, scratch);
1354
                datalo = scratch;
1355
            }
1356
            tcg_out_st(s, TCG_TYPE_I64, datalo, base, ofs);
1357
        } else if (bswap) {
1358
            tcg_out_mov(s, TCG_TYPE_I32, scratch, datahi);
1359
            tcg_out_bswap32(s, scratch);
1360
            tcg_out_st(s, TCG_TYPE_I32, scratch, base, ofs);
1361
            tcg_out_mov(s, TCG_TYPE_I32, scratch, datalo);
1362
            tcg_out_bswap32(s, scratch);
1363
            tcg_out_st(s, TCG_TYPE_I32, scratch, base, ofs + 4);
1364
        } else {
1365
            tcg_out_st(s, TCG_TYPE_I32, datalo, base, ofs);
1366
            tcg_out_st(s, TCG_TYPE_I32, datahi, base, ofs + 4);
1367
        }
1368
        break;
1369
    default:
1370
        tcg_abort();
1371
    }
1372
}
1373

    
1374
static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
1375
                            int opc)
1376
{
1377
    int data_reg, data_reg2 = 0;
1378
    int addrlo_idx;
1379
#if defined(CONFIG_SOFTMMU)
1380
    int mem_index, s_bits;
1381
    int stack_adjust;
1382
    uint8_t *label_ptr[3];
1383
#endif
1384

    
1385
    data_reg = args[0];
1386
    addrlo_idx = 1;
1387
    if (TCG_TARGET_REG_BITS == 32 && opc == 3) {
1388
        data_reg2 = args[1];
1389
        addrlo_idx = 2;
1390
    }
1391

    
1392
#if defined(CONFIG_SOFTMMU)
1393
    mem_index = args[addrlo_idx + 1 + (TARGET_LONG_BITS > TCG_TARGET_REG_BITS)];
1394
    s_bits = opc;
1395

    
1396
    tcg_out_tlb_load(s, addrlo_idx, mem_index, s_bits, args,
1397
                     label_ptr, offsetof(CPUTLBEntry, addr_write));
1398

    
1399
    /* TLB Hit.  */
1400
    tcg_out_qemu_st_direct(s, data_reg, data_reg2, TCG_REG_L0, 0, opc);
1401

    
1402
    /* jmp label2 */
1403
    tcg_out8(s, OPC_JMP_short);
1404
    label_ptr[2] = s->code_ptr;
1405
    s->code_ptr++;
1406

    
1407
    /* TLB Miss.  */
1408

    
1409
    /* label1: */
1410
    *label_ptr[0] = s->code_ptr - label_ptr[0] - 1;
1411
    if (TARGET_LONG_BITS > TCG_TARGET_REG_BITS) {
1412
        *label_ptr[1] = s->code_ptr - label_ptr[1] - 1;
1413
    }
1414

    
1415
    /* XXX: move that code at the end of the TB */
1416
#if TCG_TARGET_REG_BITS == 32
1417
    tcg_out_pushi(s, mem_index);
1418
    stack_adjust = 4;
1419
    if (opc == 3) {
1420
        tcg_out_push(s, data_reg2);
1421
        stack_adjust += 4;
1422
    }
1423
    tcg_out_push(s, data_reg);
1424
    stack_adjust += 4;
1425
    if (TARGET_LONG_BITS == 64) {
1426
        tcg_out_push(s, args[addrlo_idx + 1]);
1427
        stack_adjust += 4;
1428
    }
1429
    tcg_out_push(s, args[addrlo_idx]);
1430
    stack_adjust += 4;
1431
    tcg_out_push(s, TCG_AREG0);
1432
    stack_adjust += 4;
1433
#else
1434
    tcg_out_mov(s, (opc == 3 ? TCG_TYPE_I64 : TCG_TYPE_I32),
1435
                TCG_REG_L1, data_reg);
1436
    tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_L2, mem_index);
1437
    stack_adjust = 0;
1438
    /* XXX/FIXME: suboptimal */
1439
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[3], TCG_REG_L2);
1440
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[2], TCG_REG_L1);
1441
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[1], TCG_REG_L0);
1442
    tcg_out_mov(s, TCG_TYPE_I64, tcg_target_call_iarg_regs[0], TCG_AREG0);
1443
#endif
1444

    
1445
    tcg_out_calli(s, (tcg_target_long)qemu_st_helpers[s_bits]);
1446

    
1447
    if (stack_adjust == (TCG_TARGET_REG_BITS / 8)) {
1448
        /* Pop and discard.  This is 2 bytes smaller than the add.  */
1449
        tcg_out_pop(s, TCG_REG_ECX);
1450
    } else if (stack_adjust != 0) {
1451
        tcg_out_addi(s, TCG_REG_CALL_STACK, stack_adjust);
1452
    }
1453

    
1454
    /* label2: */
1455
    *label_ptr[2] = s->code_ptr - label_ptr[2] - 1;
1456
#else
1457
    {
1458
        int32_t offset = GUEST_BASE;
1459
        int base = args[addrlo_idx];
1460

    
1461
        if (TCG_TARGET_REG_BITS == 64) {
1462
            /* ??? We assume all operations have left us with register
1463
               contents that are zero extended.  So far this appears to
1464
               be true.  If we want to enforce this, we can either do
1465
               an explicit zero-extension here, or (if GUEST_BASE == 0)
1466
               use the ADDR32 prefix.  For now, do nothing.  */
1467

    
1468
            if (offset != GUEST_BASE) {
1469
                tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_L0, GUEST_BASE);
1470
                tgen_arithr(s, ARITH_ADD + P_REXW, TCG_REG_L0, base);
1471
                base = TCG_REG_L0;
1472
                offset = 0;
1473
            }
1474
        }
1475

    
1476
        tcg_out_qemu_st_direct(s, data_reg, data_reg2, base, offset, opc);
1477
    }
1478
#endif
1479
}
1480

    
1481
static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
1482
                              const TCGArg *args, const int *const_args)
1483
{
1484
    int c, rexw = 0;
1485

    
1486
#if TCG_TARGET_REG_BITS == 64
1487
# define OP_32_64(x) \
1488
        case glue(glue(INDEX_op_, x), _i64): \
1489
            rexw = P_REXW; /* FALLTHRU */    \
1490
        case glue(glue(INDEX_op_, x), _i32)
1491
#else
1492
# define OP_32_64(x) \
1493
        case glue(glue(INDEX_op_, x), _i32)
1494
#endif
1495

    
1496
    switch(opc) {
1497
    case INDEX_op_exit_tb:
1498
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_EAX, args[0]);
1499
        tcg_out_jmp(s, (tcg_target_long) tb_ret_addr);
1500
        break;
1501
    case INDEX_op_goto_tb:
1502
        if (s->tb_jmp_offset) {
1503
            /* direct jump method */
1504
            tcg_out8(s, OPC_JMP_long); /* jmp im */
1505
            s->tb_jmp_offset[args[0]] = s->code_ptr - s->code_buf;
1506
            tcg_out32(s, 0);
1507
        } else {
1508
            /* indirect jump method */
1509
            tcg_out_modrm_offset(s, OPC_GRP5, EXT5_JMPN_Ev, -1,
1510
                                 (tcg_target_long)(s->tb_next + args[0]));
1511
        }
1512
        s->tb_next_offset[args[0]] = s->code_ptr - s->code_buf;
1513
        break;
1514
    case INDEX_op_call:
1515
        if (const_args[0]) {
1516
            tcg_out_calli(s, args[0]);
1517
        } else {
1518
            /* call *reg */
1519
            tcg_out_modrm(s, OPC_GRP5, EXT5_CALLN_Ev, args[0]);
1520
        }
1521
        break;
1522
    case INDEX_op_jmp:
1523
        if (const_args[0]) {
1524
            tcg_out_jmp(s, args[0]);
1525
        } else {
1526
            /* jmp *reg */
1527
            tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, args[0]);
1528
        }
1529
        break;
1530
    case INDEX_op_br:
1531
        tcg_out_jxx(s, JCC_JMP, args[0], 0);
1532
        break;
1533
    case INDEX_op_movi_i32:
1534
        tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1]);
1535
        break;
1536
    OP_32_64(ld8u):
1537
        /* Note that we can ignore REXW for the zero-extend to 64-bit.  */
1538
        tcg_out_modrm_offset(s, OPC_MOVZBL, args[0], args[1], args[2]);
1539
        break;
1540
    OP_32_64(ld8s):
1541
        tcg_out_modrm_offset(s, OPC_MOVSBL + rexw, args[0], args[1], args[2]);
1542
        break;
1543
    OP_32_64(ld16u):
1544
        /* Note that we can ignore REXW for the zero-extend to 64-bit.  */
1545
        tcg_out_modrm_offset(s, OPC_MOVZWL, args[0], args[1], args[2]);
1546
        break;
1547
    OP_32_64(ld16s):
1548
        tcg_out_modrm_offset(s, OPC_MOVSWL + rexw, args[0], args[1], args[2]);
1549
        break;
1550
#if TCG_TARGET_REG_BITS == 64
1551
    case INDEX_op_ld32u_i64:
1552
#endif
1553
    case INDEX_op_ld_i32:
1554
        tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1555
        break;
1556

    
1557
    OP_32_64(st8):
1558
        if (const_args[0]) {
1559
            tcg_out_modrm_offset(s, OPC_MOVB_EvIz,
1560
                                 0, args[1], args[2]);
1561
            tcg_out8(s, args[0]);
1562
        } else {
1563
            tcg_out_modrm_offset(s, OPC_MOVB_EvGv | P_REXB_R,
1564
                                 args[0], args[1], args[2]);
1565
        }
1566
        break;
1567
    OP_32_64(st16):
1568
        if (const_args[0]) {
1569
            tcg_out_modrm_offset(s, OPC_MOVL_EvIz | P_DATA16,
1570
                                 0, args[1], args[2]);
1571
            tcg_out16(s, args[0]);
1572
        } else {
1573
            tcg_out_modrm_offset(s, OPC_MOVL_EvGv | P_DATA16,
1574
                                 args[0], args[1], args[2]);
1575
        }
1576
        break;
1577
#if TCG_TARGET_REG_BITS == 64
1578
    case INDEX_op_st32_i64:
1579
#endif
1580
    case INDEX_op_st_i32:
1581
        if (const_args[0]) {
1582
            tcg_out_modrm_offset(s, OPC_MOVL_EvIz, 0, args[1], args[2]);
1583
            tcg_out32(s, args[0]);
1584
        } else {
1585
            tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1586
        }
1587
        break;
1588

    
1589
    OP_32_64(add):
1590
        /* For 3-operand addition, use LEA.  */
1591
        if (args[0] != args[1]) {
1592
            TCGArg a0 = args[0], a1 = args[1], a2 = args[2], c3 = 0;
1593

    
1594
            if (const_args[2]) {
1595
                c3 = a2, a2 = -1;
1596
            } else if (a0 == a2) {
1597
                /* Watch out for dest = src + dest, since we've removed
1598
                   the matching constraint on the add.  */
1599
                tgen_arithr(s, ARITH_ADD + rexw, a0, a1);
1600
                break;
1601
            }
1602

    
1603
            tcg_out_modrm_sib_offset(s, OPC_LEA + rexw, a0, a1, a2, 0, c3);
1604
            break;
1605
        }
1606
        c = ARITH_ADD;
1607
        goto gen_arith;
1608
    OP_32_64(sub):
1609
        c = ARITH_SUB;
1610
        goto gen_arith;
1611
    OP_32_64(and):
1612
        c = ARITH_AND;
1613
        goto gen_arith;
1614
    OP_32_64(or):
1615
        c = ARITH_OR;
1616
        goto gen_arith;
1617
    OP_32_64(xor):
1618
        c = ARITH_XOR;
1619
        goto gen_arith;
1620
    gen_arith:
1621
        if (const_args[2]) {
1622
            tgen_arithi(s, c + rexw, args[0], args[2], 0);
1623
        } else {
1624
            tgen_arithr(s, c + rexw, args[0], args[2]);
1625
        }
1626
        break;
1627

    
1628
    OP_32_64(mul):
1629
        if (const_args[2]) {
1630
            int32_t val;
1631
            val = args[2];
1632
            if (val == (int8_t)val) {
1633
                tcg_out_modrm(s, OPC_IMUL_GvEvIb + rexw, args[0], args[0]);
1634
                tcg_out8(s, val);
1635
            } else {
1636
                tcg_out_modrm(s, OPC_IMUL_GvEvIz + rexw, args[0], args[0]);
1637
                tcg_out32(s, val);
1638
            }
1639
        } else {
1640
            tcg_out_modrm(s, OPC_IMUL_GvEv + rexw, args[0], args[2]);
1641
        }
1642
        break;
1643

    
1644
    OP_32_64(div2):
1645
        tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_IDIV, args[4]);
1646
        break;
1647
    OP_32_64(divu2):
1648
        tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_DIV, args[4]);
1649
        break;
1650

    
1651
    OP_32_64(shl):
1652
        c = SHIFT_SHL;
1653
        goto gen_shift;
1654
    OP_32_64(shr):
1655
        c = SHIFT_SHR;
1656
        goto gen_shift;
1657
    OP_32_64(sar):
1658
        c = SHIFT_SAR;
1659
        goto gen_shift;
1660
    OP_32_64(rotl):
1661
        c = SHIFT_ROL;
1662
        goto gen_shift;
1663
    OP_32_64(rotr):
1664
        c = SHIFT_ROR;
1665
        goto gen_shift;
1666
    gen_shift:
1667
        if (const_args[2]) {
1668
            tcg_out_shifti(s, c + rexw, args[0], args[2]);
1669
        } else {
1670
            tcg_out_modrm(s, OPC_SHIFT_cl + rexw, c, args[0]);
1671
        }
1672
        break;
1673

    
1674
    case INDEX_op_brcond_i32:
1675
        tcg_out_brcond32(s, args[2], args[0], args[1], const_args[1],
1676
                         args[3], 0);
1677
        break;
1678
    case INDEX_op_setcond_i32:
1679
        tcg_out_setcond32(s, args[3], args[0], args[1],
1680
                          args[2], const_args[2]);
1681
        break;
1682
    case INDEX_op_movcond_i32:
1683
        tcg_out_movcond32(s, args[5], args[0], args[1],
1684
                          args[2], const_args[2], args[3]);
1685
        break;
1686

    
1687
    OP_32_64(bswap16):
1688
        tcg_out_rolw_8(s, args[0]);
1689
        break;
1690
    OP_32_64(bswap32):
1691
        tcg_out_bswap32(s, args[0]);
1692
        break;
1693

    
1694
    OP_32_64(neg):
1695
        tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NEG, args[0]);
1696
        break;
1697
    OP_32_64(not):
1698
        tcg_out_modrm(s, OPC_GRP3_Ev + rexw, EXT3_NOT, args[0]);
1699
        break;
1700

    
1701
    OP_32_64(ext8s):
1702
        tcg_out_ext8s(s, args[0], args[1], rexw);
1703
        break;
1704
    OP_32_64(ext16s):
1705
        tcg_out_ext16s(s, args[0], args[1], rexw);
1706
        break;
1707
    OP_32_64(ext8u):
1708
        tcg_out_ext8u(s, args[0], args[1]);
1709
        break;
1710
    OP_32_64(ext16u):
1711
        tcg_out_ext16u(s, args[0], args[1]);
1712
        break;
1713

    
1714
    case INDEX_op_qemu_ld8u:
1715
        tcg_out_qemu_ld(s, args, 0);
1716
        break;
1717
    case INDEX_op_qemu_ld8s:
1718
        tcg_out_qemu_ld(s, args, 0 | 4);
1719
        break;
1720
    case INDEX_op_qemu_ld16u:
1721
        tcg_out_qemu_ld(s, args, 1);
1722
        break;
1723
    case INDEX_op_qemu_ld16s:
1724
        tcg_out_qemu_ld(s, args, 1 | 4);
1725
        break;
1726
#if TCG_TARGET_REG_BITS == 64
1727
    case INDEX_op_qemu_ld32u:
1728
#endif
1729
    case INDEX_op_qemu_ld32:
1730
        tcg_out_qemu_ld(s, args, 2);
1731
        break;
1732
    case INDEX_op_qemu_ld64:
1733
        tcg_out_qemu_ld(s, args, 3);
1734
        break;
1735

    
1736
    case INDEX_op_qemu_st8:
1737
        tcg_out_qemu_st(s, args, 0);
1738
        break;
1739
    case INDEX_op_qemu_st16:
1740
        tcg_out_qemu_st(s, args, 1);
1741
        break;
1742
    case INDEX_op_qemu_st32:
1743
        tcg_out_qemu_st(s, args, 2);
1744
        break;
1745
    case INDEX_op_qemu_st64:
1746
        tcg_out_qemu_st(s, args, 3);
1747
        break;
1748

    
1749
#if TCG_TARGET_REG_BITS == 32
1750
    case INDEX_op_brcond2_i32:
1751
        tcg_out_brcond2(s, args, const_args, 0);
1752
        break;
1753
    case INDEX_op_setcond2_i32:
1754
        tcg_out_setcond2(s, args, const_args);
1755
        break;
1756
    case INDEX_op_mulu2_i32:
1757
        tcg_out_modrm(s, OPC_GRP3_Ev, EXT3_MUL, args[3]);
1758
        break;
1759
    case INDEX_op_add2_i32:
1760
        if (const_args[4]) {
1761
            tgen_arithi(s, ARITH_ADD, args[0], args[4], 1);
1762
        } else {
1763
            tgen_arithr(s, ARITH_ADD, args[0], args[4]);
1764
        }
1765
        if (const_args[5]) {
1766
            tgen_arithi(s, ARITH_ADC, args[1], args[5], 1);
1767
        } else {
1768
            tgen_arithr(s, ARITH_ADC, args[1], args[5]);
1769
        }
1770
        break;
1771
    case INDEX_op_sub2_i32:
1772
        if (const_args[4]) {
1773
            tgen_arithi(s, ARITH_SUB, args[0], args[4], 1);
1774
        } else {
1775
            tgen_arithr(s, ARITH_SUB, args[0], args[4]);
1776
        }
1777
        if (const_args[5]) {
1778
            tgen_arithi(s, ARITH_SBB, args[1], args[5], 1);
1779
        } else {
1780
            tgen_arithr(s, ARITH_SBB, args[1], args[5]);
1781
        }
1782
        break;
1783
#else /* TCG_TARGET_REG_BITS == 64 */
1784
    case INDEX_op_movi_i64:
1785
        tcg_out_movi(s, TCG_TYPE_I64, args[0], args[1]);
1786
        break;
1787
    case INDEX_op_ld32s_i64:
1788
        tcg_out_modrm_offset(s, OPC_MOVSLQ, args[0], args[1], args[2]);
1789
        break;
1790
    case INDEX_op_ld_i64:
1791
        tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1792
        break;
1793
    case INDEX_op_st_i64:
1794
        if (const_args[0]) {
1795
            tcg_out_modrm_offset(s, OPC_MOVL_EvIz | P_REXW,
1796
                                 0, args[1], args[2]);
1797
            tcg_out32(s, args[0]);
1798
        } else {
1799
            tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1800
        }
1801
        break;
1802
    case INDEX_op_qemu_ld32s:
1803
        tcg_out_qemu_ld(s, args, 2 | 4);
1804
        break;
1805

    
1806
    case INDEX_op_brcond_i64:
1807
        tcg_out_brcond64(s, args[2], args[0], args[1], const_args[1],
1808
                         args[3], 0);
1809
        break;
1810
    case INDEX_op_setcond_i64:
1811
        tcg_out_setcond64(s, args[3], args[0], args[1],
1812
                          args[2], const_args[2]);
1813
        break;
1814
    case INDEX_op_movcond_i64:
1815
        tcg_out_movcond64(s, args[5], args[0], args[1],
1816
                          args[2], const_args[2], args[3]);
1817
        break;
1818

    
1819
    case INDEX_op_bswap64_i64:
1820
        tcg_out_bswap64(s, args[0]);
1821
        break;
1822
    case INDEX_op_ext32u_i64:
1823
        tcg_out_ext32u(s, args[0], args[1]);
1824
        break;
1825
    case INDEX_op_ext32s_i64:
1826
        tcg_out_ext32s(s, args[0], args[1]);
1827
        break;
1828
#endif
1829

    
1830
    OP_32_64(deposit):
1831
        if (args[3] == 0 && args[4] == 8) {
1832
            /* load bits 0..7 */
1833
            tcg_out_modrm(s, OPC_MOVB_EvGv | P_REXB_R | P_REXB_RM,
1834
                          args[2], args[0]);
1835
        } else if (args[3] == 8 && args[4] == 8) {
1836
            /* load bits 8..15 */
1837
            tcg_out_modrm(s, OPC_MOVB_EvGv, args[2], args[0] + 4);
1838
        } else if (args[3] == 0 && args[4] == 16) {
1839
            /* load bits 0..15 */
1840
            tcg_out_modrm(s, OPC_MOVL_EvGv | P_DATA16, args[2], args[0]);
1841
        } else {
1842
            tcg_abort();
1843
        }
1844
        break;
1845

    
1846
    default:
1847
        tcg_abort();
1848
    }
1849

    
1850
#undef OP_32_64
1851
}
1852

    
1853
static const TCGTargetOpDef x86_op_defs[] = {
1854
    { INDEX_op_exit_tb, { } },
1855
    { INDEX_op_goto_tb, { } },
1856
    { INDEX_op_call, { "ri" } },
1857
    { INDEX_op_jmp, { "ri" } },
1858
    { INDEX_op_br, { } },
1859
    { INDEX_op_mov_i32, { "r", "r" } },
1860
    { INDEX_op_movi_i32, { "r" } },
1861
    { INDEX_op_ld8u_i32, { "r", "r" } },
1862
    { INDEX_op_ld8s_i32, { "r", "r" } },
1863
    { INDEX_op_ld16u_i32, { "r", "r" } },
1864
    { INDEX_op_ld16s_i32, { "r", "r" } },
1865
    { INDEX_op_ld_i32, { "r", "r" } },
1866
    { INDEX_op_st8_i32, { "qi", "r" } },
1867
    { INDEX_op_st16_i32, { "ri", "r" } },
1868
    { INDEX_op_st_i32, { "ri", "r" } },
1869

    
1870
    { INDEX_op_add_i32, { "r", "r", "ri" } },
1871
    { INDEX_op_sub_i32, { "r", "0", "ri" } },
1872
    { INDEX_op_mul_i32, { "r", "0", "ri" } },
1873
    { INDEX_op_div2_i32, { "a", "d", "0", "1", "r" } },
1874
    { INDEX_op_divu2_i32, { "a", "d", "0", "1", "r" } },
1875
    { INDEX_op_and_i32, { "r", "0", "ri" } },
1876
    { INDEX_op_or_i32, { "r", "0", "ri" } },
1877
    { INDEX_op_xor_i32, { "r", "0", "ri" } },
1878

    
1879
    { INDEX_op_shl_i32, { "r", "0", "ci" } },
1880
    { INDEX_op_shr_i32, { "r", "0", "ci" } },
1881
    { INDEX_op_sar_i32, { "r", "0", "ci" } },
1882
    { INDEX_op_rotl_i32, { "r", "0", "ci" } },
1883
    { INDEX_op_rotr_i32, { "r", "0", "ci" } },
1884

    
1885
    { INDEX_op_brcond_i32, { "r", "ri" } },
1886

    
1887
    { INDEX_op_bswap16_i32, { "r", "0" } },
1888
    { INDEX_op_bswap32_i32, { "r", "0" } },
1889

    
1890
    { INDEX_op_neg_i32, { "r", "0" } },
1891

    
1892
    { INDEX_op_not_i32, { "r", "0" } },
1893

    
1894
    { INDEX_op_ext8s_i32, { "r", "q" } },
1895
    { INDEX_op_ext16s_i32, { "r", "r" } },
1896
    { INDEX_op_ext8u_i32, { "r", "q" } },
1897
    { INDEX_op_ext16u_i32, { "r", "r" } },
1898

    
1899
    { INDEX_op_setcond_i32, { "q", "r", "ri" } },
1900

    
1901
    { INDEX_op_deposit_i32, { "Q", "0", "Q" } },
1902
    { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } },
1903

    
1904
#if TCG_TARGET_REG_BITS == 32
1905
    { INDEX_op_mulu2_i32, { "a", "d", "a", "r" } },
1906
    { INDEX_op_add2_i32, { "r", "r", "0", "1", "ri", "ri" } },
1907
    { INDEX_op_sub2_i32, { "r", "r", "0", "1", "ri", "ri" } },
1908
    { INDEX_op_brcond2_i32, { "r", "r", "ri", "ri" } },
1909
    { INDEX_op_setcond2_i32, { "r", "r", "r", "ri", "ri" } },
1910
#else
1911
    { INDEX_op_mov_i64, { "r", "r" } },
1912
    { INDEX_op_movi_i64, { "r" } },
1913
    { INDEX_op_ld8u_i64, { "r", "r" } },
1914
    { INDEX_op_ld8s_i64, { "r", "r" } },
1915
    { INDEX_op_ld16u_i64, { "r", "r" } },
1916
    { INDEX_op_ld16s_i64, { "r", "r" } },
1917
    { INDEX_op_ld32u_i64, { "r", "r" } },
1918
    { INDEX_op_ld32s_i64, { "r", "r" } },
1919
    { INDEX_op_ld_i64, { "r", "r" } },
1920
    { INDEX_op_st8_i64, { "ri", "r" } },
1921
    { INDEX_op_st16_i64, { "ri", "r" } },
1922
    { INDEX_op_st32_i64, { "ri", "r" } },
1923
    { INDEX_op_st_i64, { "re", "r" } },
1924

    
1925
    { INDEX_op_add_i64, { "r", "0", "re" } },
1926
    { INDEX_op_mul_i64, { "r", "0", "re" } },
1927
    { INDEX_op_div2_i64, { "a", "d", "0", "1", "r" } },
1928
    { INDEX_op_divu2_i64, { "a", "d", "0", "1", "r" } },
1929
    { INDEX_op_sub_i64, { "r", "0", "re" } },
1930
    { INDEX_op_and_i64, { "r", "0", "reZ" } },
1931
    { INDEX_op_or_i64, { "r", "0", "re" } },
1932
    { INDEX_op_xor_i64, { "r", "0", "re" } },
1933

    
1934
    { INDEX_op_shl_i64, { "r", "0", "ci" } },
1935
    { INDEX_op_shr_i64, { "r", "0", "ci" } },
1936
    { INDEX_op_sar_i64, { "r", "0", "ci" } },
1937
    { INDEX_op_rotl_i64, { "r", "0", "ci" } },
1938
    { INDEX_op_rotr_i64, { "r", "0", "ci" } },
1939

    
1940
    { INDEX_op_brcond_i64, { "r", "re" } },
1941
    { INDEX_op_setcond_i64, { "r", "r", "re" } },
1942

    
1943
    { INDEX_op_bswap16_i64, { "r", "0" } },
1944
    { INDEX_op_bswap32_i64, { "r", "0" } },
1945
    { INDEX_op_bswap64_i64, { "r", "0" } },
1946
    { INDEX_op_neg_i64, { "r", "0" } },
1947
    { INDEX_op_not_i64, { "r", "0" } },
1948

    
1949
    { INDEX_op_ext8s_i64, { "r", "r" } },
1950
    { INDEX_op_ext16s_i64, { "r", "r" } },
1951
    { INDEX_op_ext32s_i64, { "r", "r" } },
1952
    { INDEX_op_ext8u_i64, { "r", "r" } },
1953
    { INDEX_op_ext16u_i64, { "r", "r" } },
1954
    { INDEX_op_ext32u_i64, { "r", "r" } },
1955

    
1956
    { INDEX_op_deposit_i64, { "Q", "0", "Q" } },
1957
    { INDEX_op_movcond_i64, { "r", "r", "re", "r", "0" } },
1958
#endif
1959

    
1960
#if TCG_TARGET_REG_BITS == 64
1961
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1962
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1963
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1964
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1965
    { INDEX_op_qemu_ld32, { "r", "L" } },
1966
    { INDEX_op_qemu_ld32u, { "r", "L" } },
1967
    { INDEX_op_qemu_ld32s, { "r", "L" } },
1968
    { INDEX_op_qemu_ld64, { "r", "L" } },
1969

    
1970
    { INDEX_op_qemu_st8, { "L", "L" } },
1971
    { INDEX_op_qemu_st16, { "L", "L" } },
1972
    { INDEX_op_qemu_st32, { "L", "L" } },
1973
    { INDEX_op_qemu_st64, { "L", "L" } },
1974
#elif TARGET_LONG_BITS <= TCG_TARGET_REG_BITS
1975
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1976
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1977
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1978
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1979
    { INDEX_op_qemu_ld32, { "r", "L" } },
1980
    { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1981

    
1982
    { INDEX_op_qemu_st8, { "cb", "L" } },
1983
    { INDEX_op_qemu_st16, { "L", "L" } },
1984
    { INDEX_op_qemu_st32, { "L", "L" } },
1985
    { INDEX_op_qemu_st64, { "L", "L", "L" } },
1986
#else
1987
    { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1988
    { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1989
    { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1990
    { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
1991
    { INDEX_op_qemu_ld32, { "r", "L", "L" } },
1992
    { INDEX_op_qemu_ld64, { "r", "r", "L", "L" } },
1993

    
1994
    { INDEX_op_qemu_st8, { "cb", "L", "L" } },
1995
    { INDEX_op_qemu_st16, { "L", "L", "L" } },
1996
    { INDEX_op_qemu_st32, { "L", "L", "L" } },
1997
    { INDEX_op_qemu_st64, { "L", "L", "L", "L" } },
1998
#endif
1999
    { -1 },
2000
};
2001

    
2002
static int tcg_target_callee_save_regs[] = {
2003
#if TCG_TARGET_REG_BITS == 64
2004
    TCG_REG_RBP,
2005
    TCG_REG_RBX,
2006
#if defined(_WIN64)
2007
    TCG_REG_RDI,
2008
    TCG_REG_RSI,
2009
#endif
2010
    TCG_REG_R12,
2011
    TCG_REG_R13,
2012
    TCG_REG_R14, /* Currently used for the global env. */
2013
    TCG_REG_R15,
2014
#else
2015
    TCG_REG_EBP, /* Currently used for the global env. */
2016
    TCG_REG_EBX,
2017
    TCG_REG_ESI,
2018
    TCG_REG_EDI,
2019
#endif
2020
};
2021

    
2022
/* Compute frame size via macros, to share between tcg_target_qemu_prologue
2023
   and tcg_register_jit.  */
2024

    
2025
#define PUSH_SIZE \
2026
    ((1 + ARRAY_SIZE(tcg_target_callee_save_regs)) \
2027
     * (TCG_TARGET_REG_BITS / 8))
2028

    
2029
#define FRAME_SIZE \
2030
    ((PUSH_SIZE \
2031
      + TCG_STATIC_CALL_ARGS_SIZE \
2032
      + CPU_TEMP_BUF_NLONGS * sizeof(long) \
2033
      + TCG_TARGET_STACK_ALIGN - 1) \
2034
     & ~(TCG_TARGET_STACK_ALIGN - 1))
2035

    
2036
/* Generate global QEMU prologue and epilogue code */
2037
static void tcg_target_qemu_prologue(TCGContext *s)
2038
{
2039
    int i, stack_addend;
2040

    
2041
    /* TB prologue */
2042

    
2043
    /* Reserve some stack space, also for TCG temps.  */
2044
    stack_addend = FRAME_SIZE - PUSH_SIZE;
2045
    tcg_set_frame(s, TCG_REG_CALL_STACK, TCG_STATIC_CALL_ARGS_SIZE,
2046
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
2047

    
2048
    /* Save all callee saved registers.  */
2049
    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2050
        tcg_out_push(s, tcg_target_callee_save_regs[i]);
2051
    }
2052

    
2053
#if TCG_TARGET_REG_BITS == 32
2054
    tcg_out_ld(s, TCG_TYPE_PTR, TCG_AREG0, TCG_REG_ESP,
2055
               (ARRAY_SIZE(tcg_target_callee_save_regs) + 1) * 4);
2056
    tcg_out_addi(s, TCG_REG_ESP, -stack_addend);
2057
    /* jmp *tb.  */
2058
    tcg_out_modrm_offset(s, OPC_GRP5, EXT5_JMPN_Ev, TCG_REG_ESP,
2059
                         (ARRAY_SIZE(tcg_target_callee_save_regs) + 2) * 4
2060
                         + stack_addend);
2061
#else
2062
    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2063
    tcg_out_addi(s, TCG_REG_ESP, -stack_addend);
2064
    /* jmp *tb.  */
2065
    tcg_out_modrm(s, OPC_GRP5, EXT5_JMPN_Ev, tcg_target_call_iarg_regs[1]);
2066
#endif
2067

    
2068
    /* TB epilogue */
2069
    tb_ret_addr = s->code_ptr;
2070

    
2071
    tcg_out_addi(s, TCG_REG_CALL_STACK, stack_addend);
2072

    
2073
    for (i = ARRAY_SIZE(tcg_target_callee_save_regs) - 1; i >= 0; i--) {
2074
        tcg_out_pop(s, tcg_target_callee_save_regs[i]);
2075
    }
2076
    tcg_out_opc(s, OPC_RET, 0, 0, 0);
2077
}
2078

    
2079
static void tcg_target_init(TCGContext *s)
2080
{
2081
#if !defined(CONFIG_USER_ONLY)
2082
    /* fail safe */
2083
    if ((1 << CPU_TLB_ENTRY_BITS) != sizeof(CPUTLBEntry))
2084
        tcg_abort();
2085
#endif
2086

    
2087
    if (TCG_TARGET_REG_BITS == 64) {
2088
        tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffff);
2089
        tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffff);
2090
    } else {
2091
        tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xff);
2092
    }
2093

    
2094
    tcg_regset_clear(tcg_target_call_clobber_regs);
2095
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EAX);
2096
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_EDX);
2097
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_ECX);
2098
    if (TCG_TARGET_REG_BITS == 64) {
2099
#if !defined(_WIN64)
2100
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RDI);
2101
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RSI);
2102
#endif
2103
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R8);
2104
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R9);
2105
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R10);
2106
        tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R11);
2107
    }
2108

    
2109
    tcg_regset_clear(s->reserved_regs);
2110
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2111

    
2112
    tcg_add_target_add_op_defs(x86_op_defs);
2113
}
2114

    
2115
typedef struct {
2116
    uint32_t len __attribute__((aligned((sizeof(void *)))));
2117
    uint32_t id;
2118
    uint8_t version;
2119
    char augmentation[1];
2120
    uint8_t code_align;
2121
    uint8_t data_align;
2122
    uint8_t return_column;
2123
} DebugFrameCIE;
2124

    
2125
typedef struct {
2126
    uint32_t len __attribute__((aligned((sizeof(void *)))));
2127
    uint32_t cie_offset;
2128
    tcg_target_long func_start __attribute__((packed));
2129
    tcg_target_long func_len __attribute__((packed));
2130
    uint8_t def_cfa[4];
2131
    uint8_t reg_ofs[14];
2132
} DebugFrameFDE;
2133

    
2134
typedef struct {
2135
    DebugFrameCIE cie;
2136
    DebugFrameFDE fde;
2137
} DebugFrame;
2138

    
2139
#if !defined(__ELF__)
2140
    /* Host machine without ELF. */
2141
#elif TCG_TARGET_REG_BITS == 64
2142
#define ELF_HOST_MACHINE EM_X86_64
2143
static DebugFrame debug_frame = {
2144
    .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2145
    .cie.id = -1,
2146
    .cie.version = 1,
2147
    .cie.code_align = 1,
2148
    .cie.data_align = 0x78,             /* sleb128 -8 */
2149
    .cie.return_column = 16,
2150

    
2151
    .fde.len = sizeof(DebugFrameFDE)-4, /* length after .len member */
2152
    .fde.def_cfa = {
2153
        12, 7,                          /* DW_CFA_def_cfa %rsp, ... */
2154
        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
2155
        (FRAME_SIZE >> 7)
2156
    },
2157
    .fde.reg_ofs = {
2158
        0x90, 1,                        /* DW_CFA_offset, %rip, -8 */
2159
        /* The following ordering must match tcg_target_callee_save_regs.  */
2160
        0x86, 2,                        /* DW_CFA_offset, %rbp, -16 */
2161
        0x83, 3,                        /* DW_CFA_offset, %rbx, -24 */
2162
        0x8c, 4,                        /* DW_CFA_offset, %r12, -32 */
2163
        0x8d, 5,                        /* DW_CFA_offset, %r13, -40 */
2164
        0x8e, 6,                        /* DW_CFA_offset, %r14, -48 */
2165
        0x8f, 7,                        /* DW_CFA_offset, %r15, -56 */
2166
    }
2167
};
2168
#else
2169
#define ELF_HOST_MACHINE EM_386
2170
static DebugFrame debug_frame = {
2171
    .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2172
    .cie.id = -1,
2173
    .cie.version = 1,
2174
    .cie.code_align = 1,
2175
    .cie.data_align = 0x7c,             /* sleb128 -4 */
2176
    .cie.return_column = 8,
2177

    
2178
    .fde.len = sizeof(DebugFrameFDE)-4, /* length after .len member */
2179
    .fde.def_cfa = {
2180
        12, 4,                          /* DW_CFA_def_cfa %esp, ... */
2181
        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
2182
        (FRAME_SIZE >> 7)
2183
    },
2184
    .fde.reg_ofs = {
2185
        0x88, 1,                        /* DW_CFA_offset, %eip, -4 */
2186
        /* The following ordering must match tcg_target_callee_save_regs.  */
2187
        0x85, 2,                        /* DW_CFA_offset, %ebp, -8 */
2188
        0x83, 3,                        /* DW_CFA_offset, %ebx, -12 */
2189
        0x86, 4,                        /* DW_CFA_offset, %esi, -16 */
2190
        0x87, 5,                        /* DW_CFA_offset, %edi, -20 */
2191
    }
2192
};
2193
#endif
2194

    
2195
#if defined(ELF_HOST_MACHINE)
2196
void tcg_register_jit(void *buf, size_t buf_size)
2197
{
2198
    /* We're expecting a 2 byte uleb128 encoded value.  */
2199
    assert(FRAME_SIZE >> 14 == 0);
2200

    
2201
    debug_frame.fde.func_start = (tcg_target_long) buf;
2202
    debug_frame.fde.func_len = buf_size;
2203

    
2204
    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
2205
}
2206
#endif