Statistics
| Branch: | Revision:

root / tcg / hppa / tcg-target.c @ f0da3757

History | View | Annotate | Download (58.6 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
    "%r0", "%r1", "%rp", "%r3", "%r4", "%r5", "%r6", "%r7",
28
    "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
29
    "%r16", "%r17", "%r18", "%r19", "%r20", "%r21", "%r22", "%r23",
30
    "%r24", "%r25", "%r26", "%dp", "%ret0", "%ret1", "%sp", "%r31",
31
};
32
#endif
33

    
34
/* This is an 8 byte temp slot in the stack frame.  */
35
#define STACK_TEMP_OFS -16
36

    
37
#ifdef CONFIG_USE_GUEST_BASE
38
#define TCG_GUEST_BASE_REG TCG_REG_R16
39
#else
40
#define TCG_GUEST_BASE_REG TCG_REG_R0
41
#endif
42

    
43
static const int tcg_target_reg_alloc_order[] = {
44
    TCG_REG_R4,
45
    TCG_REG_R5,
46
    TCG_REG_R6,
47
    TCG_REG_R7,
48
    TCG_REG_R8,
49
    TCG_REG_R9,
50
    TCG_REG_R10,
51
    TCG_REG_R11,
52
    TCG_REG_R12,
53
    TCG_REG_R13,
54

    
55
    TCG_REG_R17,
56
    TCG_REG_R14,
57
    TCG_REG_R15,
58
    TCG_REG_R16,
59

    
60
    TCG_REG_R26,
61
    TCG_REG_R25,
62
    TCG_REG_R24,
63
    TCG_REG_R23,
64

    
65
    TCG_REG_RET0,
66
    TCG_REG_RET1,
67
};
68

    
69
static const int tcg_target_call_iarg_regs[4] = {
70
    TCG_REG_R26,
71
    TCG_REG_R25,
72
    TCG_REG_R24,
73
    TCG_REG_R23,
74
};
75

    
76
static const int tcg_target_call_oarg_regs[2] = {
77
    TCG_REG_RET0,
78
    TCG_REG_RET1,
79
};
80

    
81
/* True iff val fits a signed field of width BITS.  */
82
static inline int check_fit_tl(tcg_target_long val, unsigned int bits)
83
{
84
    return (val << ((sizeof(tcg_target_long) * 8 - bits))
85
            >> (sizeof(tcg_target_long) * 8 - bits)) == val;
86
}
87

    
88
/* True iff depi can be used to compute (reg | MASK).
89
   Accept a bit pattern like:
90
      0....01....1
91
      1....10....0
92
      0..01..10..0
93
   Copied from gcc sources.  */
94
static inline int or_mask_p(tcg_target_ulong mask)
95
{
96
    if (mask == 0 || mask == -1) {
97
        return 0;
98
    }
99
    mask += mask & -mask;
100
    return (mask & (mask - 1)) == 0;
101
}
102

    
103
/* True iff depi or extru can be used to compute (reg & mask).
104
   Accept a bit pattern like these:
105
      0....01....1
106
      1....10....0
107
      1..10..01..1
108
   Copied from gcc sources.  */
109
static inline int and_mask_p(tcg_target_ulong mask)
110
{
111
    return or_mask_p(~mask);
112
}
113

    
114
static int low_sign_ext(int val, int len)
115
{
116
    return (((val << 1) & ~(-1u << len)) | ((val >> (len - 1)) & 1));
117
}
118

    
119
static int reassemble_12(int as12)
120
{
121
    return (((as12 & 0x800) >> 11) |
122
            ((as12 & 0x400) >> 8) |
123
            ((as12 & 0x3ff) << 3));
124
}
125

    
126
static int reassemble_17(int as17)
127
{
128
    return (((as17 & 0x10000) >> 16) |
129
            ((as17 & 0x0f800) << 5) |
130
            ((as17 & 0x00400) >> 8) |
131
            ((as17 & 0x003ff) << 3));
132
}
133

    
134
static int reassemble_21(int as21)
135
{
136
    return (((as21 & 0x100000) >> 20) |
137
            ((as21 & 0x0ffe00) >> 8) |
138
            ((as21 & 0x000180) << 7) |
139
            ((as21 & 0x00007c) << 14) |
140
            ((as21 & 0x000003) << 12));
141
}
142

    
143
/* ??? Bizzarely, there is no PCREL12F relocation type.  I guess all
144
   such relocations are simply fully handled by the assembler.  */
145
#define R_PARISC_PCREL12F  R_PARISC_NONE
146

    
147
static void patch_reloc(uint8_t *code_ptr, int type,
148
                        tcg_target_long value, tcg_target_long addend)
149
{
150
    uint32_t *insn_ptr = (uint32_t *)code_ptr;
151
    uint32_t insn = *insn_ptr;
152
    tcg_target_long pcrel;
153

    
154
    value += addend;
155
    pcrel = (value - ((tcg_target_long)code_ptr + 8)) >> 2;
156

    
157
    switch (type) {
158
    case R_PARISC_PCREL12F:
159
        assert(check_fit_tl(pcrel, 12));
160
        /* ??? We assume all patches are forward.  See tcg_out_brcond
161
           re setting the NUL bit on the branch and eliding the nop.  */
162
        assert(pcrel >= 0);
163
        insn &= ~0x1ffdu;
164
        insn |= reassemble_12(pcrel);
165
        break;
166
    case R_PARISC_PCREL17F:
167
        assert(check_fit_tl(pcrel, 17));
168
        insn &= ~0x1f1ffdu;
169
        insn |= reassemble_17(pcrel);
170
        break;
171
    default:
172
        tcg_abort();
173
    }
174

    
175
    *insn_ptr = insn;
176
}
177

    
178
/* maximum number of register used for input function arguments */
179
static inline int tcg_target_get_call_iarg_regs_count(int flags)
180
{
181
    return 4;
182
}
183

    
184
/* parse target specific constraints */
185
static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
186
{
187
    const char *ct_str;
188

    
189
    ct_str = *pct_str;
190
    switch (ct_str[0]) {
191
    case 'r':
192
        ct->ct |= TCG_CT_REG;
193
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
194
        break;
195
    case 'L': /* qemu_ld/st constraint */
196
        ct->ct |= TCG_CT_REG;
197
        tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
198
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R26);
199
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R25);
200
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R24);
201
        tcg_regset_reset_reg(ct->u.regs, TCG_REG_R23);
202
        break;
203
    case 'Z':
204
        ct->ct |= TCG_CT_CONST_0;
205
        break;
206
    case 'I':
207
        ct->ct |= TCG_CT_CONST_S11;
208
        break;
209
    case 'J':
210
        ct->ct |= TCG_CT_CONST_S5;
211
        break;
212
    case 'K':
213
        ct->ct |= TCG_CT_CONST_MS11;
214
        break;
215
    case 'M':
216
        ct->ct |= TCG_CT_CONST_AND;
217
        break;
218
    case 'O':
219
        ct->ct |= TCG_CT_CONST_OR;
220
        break;
221
    default:
222
        return -1;
223
    }
224
    ct_str++;
225
    *pct_str = ct_str;
226
    return 0;
227
}
228

    
229
/* test if a constant matches the constraint */
230
static int tcg_target_const_match(tcg_target_long val,
231
                                  const TCGArgConstraint *arg_ct)
232
{
233
    int ct = arg_ct->ct;
234
    if (ct & TCG_CT_CONST) {
235
        return 1;
236
    } else if (ct & TCG_CT_CONST_0) {
237
        return val == 0;
238
    } else if (ct & TCG_CT_CONST_S5) {
239
        return check_fit_tl(val, 5);
240
    } else if (ct & TCG_CT_CONST_S11) {
241
        return check_fit_tl(val, 11);
242
    } else if (ct & TCG_CT_CONST_MS11) {
243
        return check_fit_tl(-val, 11);
244
    } else if (ct & TCG_CT_CONST_AND) {
245
        return and_mask_p(val);
246
    } else if (ct & TCG_CT_CONST_OR) {
247
        return or_mask_p(val);
248
    }
249
    return 0;
250
}
251

    
252
#define INSN_OP(x)       ((x) << 26)
253
#define INSN_EXT3BR(x)   ((x) << 13)
254
#define INSN_EXT3SH(x)   ((x) << 10)
255
#define INSN_EXT4(x)     ((x) << 6)
256
#define INSN_EXT5(x)     (x)
257
#define INSN_EXT6(x)     ((x) << 6)
258
#define INSN_EXT7(x)     ((x) << 6)
259
#define INSN_EXT8A(x)    ((x) << 6)
260
#define INSN_EXT8B(x)    ((x) << 5)
261
#define INSN_T(x)        (x)
262
#define INSN_R1(x)       ((x) << 16)
263
#define INSN_R2(x)       ((x) << 21)
264
#define INSN_DEP_LEN(x)  (32 - (x))
265
#define INSN_SHDEP_CP(x) ((31 - (x)) << 5)
266
#define INSN_SHDEP_P(x)  ((x) << 5)
267
#define INSN_COND(x)     ((x) << 13)
268
#define INSN_IM11(x)     low_sign_ext(x, 11)
269
#define INSN_IM14(x)     low_sign_ext(x, 14)
270
#define INSN_IM5(x)      (low_sign_ext(x, 5) << 16)
271

    
272
#define COND_NEVER   0
273
#define COND_EQ      1
274
#define COND_LT      2
275
#define COND_LE      3
276
#define COND_LTU     4
277
#define COND_LEU     5
278
#define COND_SV      6
279
#define COND_OD      7
280
#define COND_FALSE   8
281

    
282
#define INSN_ADD        (INSN_OP(0x02) | INSN_EXT6(0x18))
283
#define INSN_ADDC        (INSN_OP(0x02) | INSN_EXT6(0x1c))
284
#define INSN_ADDI        (INSN_OP(0x2d))
285
#define INSN_ADDIL        (INSN_OP(0x0a))
286
#define INSN_ADDL        (INSN_OP(0x02) | INSN_EXT6(0x28))
287
#define INSN_AND        (INSN_OP(0x02) | INSN_EXT6(0x08))
288
#define INSN_ANDCM        (INSN_OP(0x02) | INSN_EXT6(0x00))
289
#define INSN_COMCLR        (INSN_OP(0x02) | INSN_EXT6(0x22))
290
#define INSN_COMICLR        (INSN_OP(0x24))
291
#define INSN_DEP        (INSN_OP(0x35) | INSN_EXT3SH(3))
292
#define INSN_DEPI        (INSN_OP(0x35) | INSN_EXT3SH(7))
293
#define INSN_EXTRS        (INSN_OP(0x34) | INSN_EXT3SH(7))
294
#define INSN_EXTRU        (INSN_OP(0x34) | INSN_EXT3SH(6))
295
#define INSN_LDIL        (INSN_OP(0x08))
296
#define INSN_LDO        (INSN_OP(0x0d))
297
#define INSN_MTCTL        (INSN_OP(0x00) | INSN_EXT8B(0xc2))
298
#define INSN_OR                (INSN_OP(0x02) | INSN_EXT6(0x09))
299
#define INSN_SHD        (INSN_OP(0x34) | INSN_EXT3SH(2))
300
#define INSN_SUB        (INSN_OP(0x02) | INSN_EXT6(0x10))
301
#define INSN_SUBB        (INSN_OP(0x02) | INSN_EXT6(0x14))
302
#define INSN_SUBI        (INSN_OP(0x25))
303
#define INSN_VEXTRS        (INSN_OP(0x34) | INSN_EXT3SH(5))
304
#define INSN_VEXTRU        (INSN_OP(0x34) | INSN_EXT3SH(4))
305
#define INSN_VSHD        (INSN_OP(0x34) | INSN_EXT3SH(0))
306
#define INSN_XOR        (INSN_OP(0x02) | INSN_EXT6(0x0a))
307
#define INSN_ZDEP        (INSN_OP(0x35) | INSN_EXT3SH(2))
308
#define INSN_ZVDEP        (INSN_OP(0x35) | INSN_EXT3SH(0))
309

    
310
#define INSN_BL         (INSN_OP(0x3a) | INSN_EXT3BR(0))
311
#define INSN_BL_N       (INSN_OP(0x3a) | INSN_EXT3BR(0) | 2)
312
#define INSN_BLR        (INSN_OP(0x3a) | INSN_EXT3BR(2))
313
#define INSN_BV         (INSN_OP(0x3a) | INSN_EXT3BR(6))
314
#define INSN_BV_N       (INSN_OP(0x3a) | INSN_EXT3BR(6) | 2)
315
#define INSN_BLE_SR4    (INSN_OP(0x39) | (1 << 13))
316

    
317
#define INSN_LDB        (INSN_OP(0x10))
318
#define INSN_LDH        (INSN_OP(0x11))
319
#define INSN_LDW        (INSN_OP(0x12))
320
#define INSN_LDWM       (INSN_OP(0x13))
321
#define INSN_FLDDS      (INSN_OP(0x0b) | INSN_EXT4(0) | (1 << 12))
322

    
323
#define INSN_LDBX        (INSN_OP(0x03) | INSN_EXT4(0))
324
#define INSN_LDHX        (INSN_OP(0x03) | INSN_EXT4(1))
325
#define INSN_LDWX       (INSN_OP(0x03) | INSN_EXT4(2))
326

    
327
#define INSN_STB        (INSN_OP(0x18))
328
#define INSN_STH        (INSN_OP(0x19))
329
#define INSN_STW        (INSN_OP(0x1a))
330
#define INSN_STWM       (INSN_OP(0x1b))
331
#define INSN_FSTDS      (INSN_OP(0x0b) | INSN_EXT4(8) | (1 << 12))
332

    
333
#define INSN_COMBT      (INSN_OP(0x20))
334
#define INSN_COMBF      (INSN_OP(0x22))
335
#define INSN_COMIBT     (INSN_OP(0x21))
336
#define INSN_COMIBF     (INSN_OP(0x23))
337

    
338
/* supplied by libgcc */
339
extern void *__canonicalize_funcptr_for_compare(const void *);
340

    
341
static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
342
{
343
    /* PA1.1 defines COPY as OR r,0,t; PA2.0 defines COPY as LDO 0(r),t
344
       but hppa-dis.c is unaware of this definition */
345
    if (ret != arg) {
346
        tcg_out32(s, INSN_OR | INSN_T(ret) | INSN_R1(arg)
347
                  | INSN_R2(TCG_REG_R0));
348
    }
349
}
350

    
351
static void tcg_out_movi(TCGContext *s, TCGType type,
352
                         TCGReg ret, tcg_target_long arg)
353
{
354
    if (check_fit_tl(arg, 14)) {
355
        tcg_out32(s, INSN_LDO | INSN_R1(ret)
356
                  | INSN_R2(TCG_REG_R0) | INSN_IM14(arg));
357
    } else {
358
        uint32_t hi, lo;
359
        hi = arg >> 11;
360
        lo = arg & 0x7ff;
361

    
362
        tcg_out32(s, INSN_LDIL | INSN_R2(ret) | reassemble_21(hi));
363
        if (lo) {
364
            tcg_out32(s, INSN_LDO | INSN_R1(ret)
365
                      | INSN_R2(ret) | INSN_IM14(lo));
366
        }
367
    }
368
}
369

    
370
static void tcg_out_ldst(TCGContext *s, int ret, int addr,
371
                         tcg_target_long offset, int op)
372
{
373
    if (!check_fit_tl(offset, 14)) {
374
        uint32_t hi, lo, op;
375

    
376
        hi = offset >> 11;
377
        lo = offset & 0x7ff;
378

    
379
        if (addr == TCG_REG_R0) {
380
            op = INSN_LDIL | INSN_R2(TCG_REG_R1);
381
        } else {
382
            op = INSN_ADDIL | INSN_R2(addr);
383
        }
384
        tcg_out32(s, op | reassemble_21(hi));
385

    
386
        addr = TCG_REG_R1;
387
        offset = lo;
388
    }
389

    
390
    if (ret != addr || offset != 0 || op != INSN_LDO) {
391
        tcg_out32(s, op | INSN_R1(ret) | INSN_R2(addr) | INSN_IM14(offset));
392
    }
393
}
394

    
395
/* This function is required by tcg.c.  */
396
static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
397
                              TCGReg arg1, tcg_target_long arg2)
398
{
399
    tcg_out_ldst(s, ret, arg1, arg2, INSN_LDW);
400
}
401

    
402
/* This function is required by tcg.c.  */
403
static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg ret,
404
                              TCGReg arg1, tcg_target_long arg2)
405
{
406
    tcg_out_ldst(s, ret, arg1, arg2, INSN_STW);
407
}
408

    
409
static void tcg_out_ldst_index(TCGContext *s, int data,
410
                               int base, int index, int op)
411
{
412
    tcg_out32(s, op | INSN_T(data) | INSN_R1(index) | INSN_R2(base));
413
}
414

    
415
static inline void tcg_out_addi2(TCGContext *s, int ret, int arg1,
416
                                 tcg_target_long val)
417
{
418
    tcg_out_ldst(s, ret, arg1, val, INSN_LDO);
419
}
420

    
421
/* This function is required by tcg.c.  */
422
static inline void tcg_out_addi(TCGContext *s, int reg, tcg_target_long val)
423
{
424
    tcg_out_addi2(s, reg, reg, val);
425
}
426

    
427
static inline void tcg_out_arith(TCGContext *s, int t, int r1, int r2, int op)
428
{
429
    tcg_out32(s, op | INSN_T(t) | INSN_R1(r1) | INSN_R2(r2));
430
}
431

    
432
static inline void tcg_out_arithi(TCGContext *s, int t, int r1,
433
                                  tcg_target_long val, int op)
434
{
435
    assert(check_fit_tl(val, 11));
436
    tcg_out32(s, op | INSN_R1(t) | INSN_R2(r1) | INSN_IM11(val));
437
}
438

    
439
static inline void tcg_out_nop(TCGContext *s)
440
{
441
    tcg_out_arith(s, TCG_REG_R0, TCG_REG_R0, TCG_REG_R0, INSN_OR);
442
}
443

    
444
static inline void tcg_out_mtctl_sar(TCGContext *s, int arg)
445
{
446
    tcg_out32(s, INSN_MTCTL | INSN_R2(11) | INSN_R1(arg));
447
}
448

    
449
/* Extract LEN bits at position OFS from ARG and place in RET.
450
   Note that here the bit ordering is reversed from the PA-RISC
451
   standard, such that the right-most bit is 0.  */
452
static inline void tcg_out_extr(TCGContext *s, int ret, int arg,
453
                                unsigned ofs, unsigned len, int sign)
454
{
455
    assert(ofs < 32 && len <= 32 - ofs);
456
    tcg_out32(s, (sign ? INSN_EXTRS : INSN_EXTRU)
457
              | INSN_R1(ret) | INSN_R2(arg)
458
              | INSN_SHDEP_P(31 - ofs) | INSN_DEP_LEN(len));
459
}
460

    
461
/* Likewise with OFS interpreted little-endian.  */
462
static inline void tcg_out_dep(TCGContext *s, int ret, int arg,
463
                               unsigned ofs, unsigned len)
464
{
465
    assert(ofs < 32 && len <= 32 - ofs);
466
    tcg_out32(s, INSN_DEP | INSN_R2(ret) | INSN_R1(arg)
467
              | INSN_SHDEP_CP(31 - ofs) | INSN_DEP_LEN(len));
468
}
469

    
470
static inline void tcg_out_depi(TCGContext *s, int ret, int arg,
471
                                unsigned ofs, unsigned len)
472
{
473
    assert(ofs < 32 && len <= 32 - ofs);
474
    tcg_out32(s, INSN_DEPI | INSN_R2(ret) | INSN_IM5(arg)
475
              | INSN_SHDEP_CP(31 - ofs) | INSN_DEP_LEN(len));
476
}
477

    
478
static inline void tcg_out_shd(TCGContext *s, int ret, int hi, int lo,
479
                               unsigned count)
480
{
481
    assert(count < 32);
482
    tcg_out32(s, INSN_SHD | INSN_R1(hi) | INSN_R2(lo) | INSN_T(ret)
483
              | INSN_SHDEP_CP(count));
484
}
485

    
486
static void tcg_out_vshd(TCGContext *s, int ret, int hi, int lo, int creg)
487
{
488
    tcg_out_mtctl_sar(s, creg);
489
    tcg_out32(s, INSN_VSHD | INSN_T(ret) | INSN_R1(hi) | INSN_R2(lo));
490
}
491

    
492
static void tcg_out_ori(TCGContext *s, int ret, int arg, tcg_target_ulong m)
493
{
494
    int bs0, bs1;
495

    
496
    /* Note that the argument is constrained to match or_mask_p.  */
497
    for (bs0 = 0; bs0 < 32; bs0++) {
498
        if ((m & (1u << bs0)) != 0) {
499
            break;
500
        }
501
    }
502
    for (bs1 = bs0; bs1 < 32; bs1++) {
503
        if ((m & (1u << bs1)) == 0) {
504
            break;
505
        }
506
    }
507
    assert(bs1 == 32 || (1ul << bs1) > m);
508

    
509
    tcg_out_mov(s, TCG_TYPE_I32, ret, arg);
510
    tcg_out_depi(s, ret, -1, bs0, bs1 - bs0);
511
}
512

    
513
static void tcg_out_andi(TCGContext *s, int ret, int arg, tcg_target_ulong m)
514
{
515
    int ls0, ls1, ms0;
516

    
517
    /* Note that the argument is constrained to match and_mask_p.  */
518
    for (ls0 = 0; ls0 < 32; ls0++) {
519
        if ((m & (1u << ls0)) == 0) {
520
            break;
521
        }
522
    }
523
    for (ls1 = ls0; ls1 < 32; ls1++) {
524
        if ((m & (1u << ls1)) != 0) {
525
            break;
526
        }
527
    }
528
    for (ms0 = ls1; ms0 < 32; ms0++) {
529
        if ((m & (1u << ms0)) == 0) {
530
            break;
531
        }
532
    }
533
    assert (ms0 == 32);
534

    
535
    if (ls1 == 32) {
536
        tcg_out_extr(s, ret, arg, 0, ls0, 0);
537
    } else {
538
        tcg_out_mov(s, TCG_TYPE_I32, ret, arg);
539
        tcg_out_depi(s, ret, 0, ls0, ls1 - ls0);
540
    }
541
}
542

    
543
static inline void tcg_out_ext8s(TCGContext *s, int ret, int arg)
544
{
545
    tcg_out_extr(s, ret, arg, 0, 8, 1);
546
}
547

    
548
static inline void tcg_out_ext16s(TCGContext *s, int ret, int arg)
549
{
550
    tcg_out_extr(s, ret, arg, 0, 16, 1);
551
}
552

    
553
static void tcg_out_shli(TCGContext *s, int ret, int arg, int count)
554
{
555
    count &= 31;
556
    tcg_out32(s, INSN_ZDEP | INSN_R2(ret) | INSN_R1(arg)
557
              | INSN_SHDEP_CP(31 - count) | INSN_DEP_LEN(32 - count));
558
}
559

    
560
static void tcg_out_shl(TCGContext *s, int ret, int arg, int creg)
561
{
562
    tcg_out_arithi(s, TCG_REG_R20, creg, 31, INSN_SUBI);
563
    tcg_out_mtctl_sar(s, TCG_REG_R20);
564
    tcg_out32(s, INSN_ZVDEP | INSN_R2(ret) | INSN_R1(arg) | INSN_DEP_LEN(32));
565
}
566

    
567
static void tcg_out_shri(TCGContext *s, int ret, int arg, int count)
568
{
569
    count &= 31;
570
    tcg_out_extr(s, ret, arg, count, 32 - count, 0);
571
}
572

    
573
static void tcg_out_shr(TCGContext *s, int ret, int arg, int creg)
574
{
575
    tcg_out_vshd(s, ret, TCG_REG_R0, arg, creg);
576
}
577

    
578
static void tcg_out_sari(TCGContext *s, int ret, int arg, int count)
579
{
580
    count &= 31;
581
    tcg_out_extr(s, ret, arg, count, 32 - count, 1);
582
}
583

    
584
static void tcg_out_sar(TCGContext *s, int ret, int arg, int creg)
585
{
586
    tcg_out_arithi(s, TCG_REG_R20, creg, 31, INSN_SUBI);
587
    tcg_out_mtctl_sar(s, TCG_REG_R20);
588
    tcg_out32(s, INSN_VEXTRS | INSN_R1(ret) | INSN_R2(arg) | INSN_DEP_LEN(32));
589
}
590

    
591
static void tcg_out_rotli(TCGContext *s, int ret, int arg, int count)
592
{
593
    count &= 31;
594
    tcg_out_shd(s, ret, arg, arg, 32 - count);
595
}
596

    
597
static void tcg_out_rotl(TCGContext *s, int ret, int arg, int creg)
598
{
599
    tcg_out_arithi(s, TCG_REG_R20, creg, 32, INSN_SUBI);
600
    tcg_out_vshd(s, ret, arg, arg, TCG_REG_R20);
601
}
602

    
603
static void tcg_out_rotri(TCGContext *s, int ret, int arg, int count)
604
{
605
    count &= 31;
606
    tcg_out_shd(s, ret, arg, arg, count);
607
}
608

    
609
static void tcg_out_rotr(TCGContext *s, int ret, int arg, int creg)
610
{
611
    tcg_out_vshd(s, ret, arg, arg, creg);
612
}
613

    
614
static void tcg_out_bswap16(TCGContext *s, int ret, int arg, int sign)
615
{
616
    if (ret != arg) {
617
        tcg_out_mov(s, TCG_TYPE_I32, ret, arg); /* arg =  xxAB */
618
    }
619
    tcg_out_dep(s, ret, ret, 16, 8);          /* ret =  xBAB */
620
    tcg_out_extr(s, ret, ret, 8, 16, sign);   /* ret =  ..BA */
621
}
622

    
623
static void tcg_out_bswap32(TCGContext *s, int ret, int arg, int temp)
624
{
625
                                          /* arg =  ABCD */
626
    tcg_out_rotri(s, temp, arg, 16);      /* temp = CDAB */
627
    tcg_out_dep(s, temp, temp, 16, 8);    /* temp = CBAB */
628
    tcg_out_shd(s, ret, arg, temp, 8);    /* ret =  DCBA */
629
}
630

    
631
static void tcg_out_call(TCGContext *s, const void *func)
632
{
633
    tcg_target_long val, hi, lo, disp;
634

    
635
    val = (uint32_t)__canonicalize_funcptr_for_compare(func);
636
    disp = (val - ((tcg_target_long)s->code_ptr + 8)) >> 2;
637

    
638
    if (check_fit_tl(disp, 17)) {
639
        tcg_out32(s, INSN_BL_N | INSN_R2(TCG_REG_RP) | reassemble_17(disp));
640
    } else {
641
        hi = val >> 11;
642
        lo = val & 0x7ff;
643

    
644
        tcg_out32(s, INSN_LDIL | INSN_R2(TCG_REG_R20) | reassemble_21(hi));
645
        tcg_out32(s, INSN_BLE_SR4 | INSN_R2(TCG_REG_R20)
646
                  | reassemble_17(lo >> 2));
647
        tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_RP, TCG_REG_R31);
648
    }
649
}
650

    
651
static void tcg_out_xmpyu(TCGContext *s, int retl, int reth,
652
                          int arg1, int arg2)
653
{
654
    /* Store both words into the stack for copy to the FPU.  */
655
    tcg_out_ldst(s, arg1, TCG_REG_CALL_STACK, STACK_TEMP_OFS, INSN_STW);
656
    tcg_out_ldst(s, arg2, TCG_REG_CALL_STACK, STACK_TEMP_OFS + 4, INSN_STW);
657

    
658
    /* Load both words into the FPU at the same time.  We get away
659
       with this because we can address the left and right half of the
660
       FPU registers individually once loaded.  */
661
    /* fldds stack_temp(sp),fr22 */
662
    tcg_out32(s, INSN_FLDDS | INSN_R2(TCG_REG_CALL_STACK)
663
              | INSN_IM5(STACK_TEMP_OFS) | INSN_T(22));
664

    
665
    /* xmpyu fr22r,fr22,fr22 */
666
    tcg_out32(s, 0x3ad64796);
667

    
668
    /* Store the 64-bit result back into the stack.  */
669
    /* fstds stack_temp(sp),fr22 */
670
    tcg_out32(s, INSN_FSTDS | INSN_R2(TCG_REG_CALL_STACK)
671
              | INSN_IM5(STACK_TEMP_OFS) | INSN_T(22));
672

    
673
    /* Load the pieces of the result that the caller requested.  */
674
    if (reth) {
675
        tcg_out_ldst(s, reth, TCG_REG_CALL_STACK, STACK_TEMP_OFS, INSN_LDW);
676
    }
677
    if (retl) {
678
        tcg_out_ldst(s, retl, TCG_REG_CALL_STACK, STACK_TEMP_OFS + 4,
679
                     INSN_LDW);
680
    }
681
}
682

    
683
static void tcg_out_add2(TCGContext *s, int destl, int desth,
684
                         int al, int ah, int bl, int bh, int blconst)
685
{
686
    int tmp = (destl == ah || destl == bh ? TCG_REG_R20 : destl);
687

    
688
    if (blconst) {
689
        tcg_out_arithi(s, tmp, al, bl, INSN_ADDI);
690
    } else {
691
        tcg_out_arith(s, tmp, al, bl, INSN_ADD);
692
    }
693
    tcg_out_arith(s, desth, ah, bh, INSN_ADDC);
694

    
695
    tcg_out_mov(s, TCG_TYPE_I32, destl, tmp);
696
}
697

    
698
static void tcg_out_sub2(TCGContext *s, int destl, int desth, int al, int ah,
699
                         int bl, int bh, int alconst, int blconst)
700
{
701
    int tmp = (destl == ah || destl == bh ? TCG_REG_R20 : destl);
702

    
703
    if (alconst) {
704
        if (blconst) {
705
            tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R20, bl);
706
            bl = TCG_REG_R20;
707
        }
708
        tcg_out_arithi(s, tmp, bl, al, INSN_SUBI);
709
    } else if (blconst) {
710
        tcg_out_arithi(s, tmp, al, -bl, INSN_ADDI);
711
    } else {
712
        tcg_out_arith(s, tmp, al, bl, INSN_SUB);
713
    }
714
    tcg_out_arith(s, desth, ah, bh, INSN_SUBB);
715

    
716
    tcg_out_mov(s, TCG_TYPE_I32, destl, tmp);
717
}
718

    
719
static void tcg_out_branch(TCGContext *s, int label_index, int nul)
720
{
721
    TCGLabel *l = &s->labels[label_index];
722
    uint32_t op = nul ? INSN_BL_N : INSN_BL;
723

    
724
    if (l->has_value) {
725
        tcg_target_long val = l->u.value;
726

    
727
        val -= (tcg_target_long)s->code_ptr + 8;
728
        val >>= 2;
729
        assert(check_fit_tl(val, 17));
730

    
731
        tcg_out32(s, op | reassemble_17(val));
732
    } else {
733
        /* We need to keep the offset unchanged for retranslation.  */
734
        uint32_t old_insn = *(uint32_t *)s->code_ptr;
735

    
736
        tcg_out_reloc(s, s->code_ptr, R_PARISC_PCREL17F, label_index, 0);
737
        tcg_out32(s, op | (old_insn & 0x1f1ffdu));
738
    }
739
}
740

    
741
static const uint8_t tcg_cond_to_cmp_cond[10] =
742
{
743
    [TCG_COND_EQ] = COND_EQ,
744
    [TCG_COND_NE] = COND_EQ | COND_FALSE,
745
    [TCG_COND_LT] = COND_LT,
746
    [TCG_COND_GE] = COND_LT | COND_FALSE,
747
    [TCG_COND_LE] = COND_LE,
748
    [TCG_COND_GT] = COND_LE | COND_FALSE,
749
    [TCG_COND_LTU] = COND_LTU,
750
    [TCG_COND_GEU] = COND_LTU | COND_FALSE,
751
    [TCG_COND_LEU] = COND_LEU,
752
    [TCG_COND_GTU] = COND_LEU | COND_FALSE,
753
};
754

    
755
static void tcg_out_brcond(TCGContext *s, int cond, TCGArg c1,
756
                           TCGArg c2, int c2const, int label_index)
757
{
758
    TCGLabel *l = &s->labels[label_index];
759
    int op, pacond;
760

    
761
    /* Note that COMIB operates as if the immediate is the first
762
       operand.  We model brcond with the immediate in the second
763
       to better match what targets are likely to give us.  For
764
       consistency, model COMB with reversed operands as well.  */
765
    pacond = tcg_cond_to_cmp_cond[tcg_swap_cond(cond)];
766

    
767
    if (c2const) {
768
        op = (pacond & COND_FALSE ? INSN_COMIBF : INSN_COMIBT);
769
        op |= INSN_IM5(c2);
770
    } else {
771
        op = (pacond & COND_FALSE ? INSN_COMBF : INSN_COMBT);
772
        op |= INSN_R1(c2);
773
    }
774
    op |= INSN_R2(c1);
775
    op |= INSN_COND(pacond & 7);
776

    
777
    if (l->has_value) {
778
        tcg_target_long val = l->u.value;
779

    
780
        val -= (tcg_target_long)s->code_ptr + 8;
781
        val >>= 2;
782
        assert(check_fit_tl(val, 12));
783

    
784
        /* ??? Assume that all branches to defined labels are backward.
785
           Which means that if the nul bit is set, the delay slot is
786
           executed if the branch is taken, and not executed in fallthru.  */
787
        tcg_out32(s, op | reassemble_12(val));
788
        tcg_out_nop(s);
789
    } else {
790
        /* We need to keep the offset unchanged for retranslation.  */
791
        uint32_t old_insn = *(uint32_t *)s->code_ptr;
792

    
793
        tcg_out_reloc(s, s->code_ptr, R_PARISC_PCREL12F, label_index, 0);
794
        /* ??? Assume that all branches to undefined labels are forward.
795
           Which means that if the nul bit is set, the delay slot is
796
           not executed if the branch is taken, which is what we want.  */
797
        tcg_out32(s, op | 2 | (old_insn & 0x1ffdu));
798
    }
799
}
800

    
801
static void tcg_out_comclr(TCGContext *s, int cond, TCGArg ret,
802
                           TCGArg c1, TCGArg c2, int c2const)
803
{
804
    int op, pacond;
805

    
806
    /* Note that COMICLR operates as if the immediate is the first
807
       operand.  We model setcond with the immediate in the second
808
       to better match what targets are likely to give us.  For
809
       consistency, model COMCLR with reversed operands as well.  */
810
    pacond = tcg_cond_to_cmp_cond[tcg_swap_cond(cond)];
811

    
812
    if (c2const) {
813
        op = INSN_COMICLR | INSN_R2(c1) | INSN_R1(ret) | INSN_IM11(c2);
814
    } else {
815
        op = INSN_COMCLR | INSN_R2(c1) | INSN_R1(c2) | INSN_T(ret);
816
    }
817
    op |= INSN_COND(pacond & 7);
818
    op |= pacond & COND_FALSE ? 1 << 12 : 0;
819

    
820
    tcg_out32(s, op);
821
}
822

    
823
static TCGCond const tcg_high_cond[] = {
824
    [TCG_COND_EQ] = TCG_COND_EQ,
825
    [TCG_COND_NE] = TCG_COND_NE,
826
    [TCG_COND_LT] = TCG_COND_LT,
827
    [TCG_COND_LE] = TCG_COND_LT,
828
    [TCG_COND_GT] = TCG_COND_GT,
829
    [TCG_COND_GE] = TCG_COND_GT,
830
    [TCG_COND_LTU] = TCG_COND_LTU,
831
    [TCG_COND_LEU] = TCG_COND_LTU,
832
    [TCG_COND_GTU] = TCG_COND_GTU,
833
    [TCG_COND_GEU] = TCG_COND_GTU
834
};
835

    
836
static void tcg_out_brcond2(TCGContext *s, int cond, TCGArg al, TCGArg ah,
837
                            TCGArg bl, int blconst, TCGArg bh, int bhconst,
838
                            int label_index)
839
{
840
    switch (cond) {
841
    case TCG_COND_EQ:
842
        tcg_out_comclr(s, TCG_COND_NE, TCG_REG_R0, al, bl, blconst);
843
        tcg_out_brcond(s, TCG_COND_EQ, ah, bh, bhconst, label_index);
844
        break;
845
    case TCG_COND_NE:
846
        tcg_out_brcond(s, TCG_COND_NE, al, bl, bhconst, label_index);
847
        tcg_out_brcond(s, TCG_COND_NE, ah, bh, bhconst, label_index);
848
        break;
849
    default:
850
        tcg_out_brcond(s, tcg_high_cond[cond], ah, bh, bhconst, label_index);
851
        tcg_out_comclr(s, TCG_COND_NE, TCG_REG_R0, ah, bh, bhconst);
852
        tcg_out_brcond(s, tcg_unsigned_cond(cond),
853
                       al, bl, blconst, label_index);
854
        break;
855
    }
856
}
857

    
858
static void tcg_out_setcond(TCGContext *s, int cond, TCGArg ret,
859
                            TCGArg c1, TCGArg c2, int c2const)
860
{
861
    tcg_out_comclr(s, tcg_invert_cond(cond), ret, c1, c2, c2const);
862
    tcg_out_movi(s, TCG_TYPE_I32, ret, 1);
863
}
864

    
865
static void tcg_out_setcond2(TCGContext *s, int cond, TCGArg ret,
866
                             TCGArg al, TCGArg ah, TCGArg bl, int blconst,
867
                             TCGArg bh, int bhconst)
868
{
869
    int scratch = TCG_REG_R20;
870

    
871
    /* Note that the low parts are fully consumed before scratch is set.  */
872
    if (ret != ah && (bhconst || ret != bh)) {
873
        scratch = ret;
874
    }
875

    
876
    switch (cond) {
877
    case TCG_COND_EQ:
878
    case TCG_COND_NE:
879
        tcg_out_setcond(s, cond, scratch, al, bl, blconst);
880
        tcg_out_comclr(s, TCG_COND_EQ, TCG_REG_R0, ah, bh, bhconst);
881
        tcg_out_movi(s, TCG_TYPE_I32, scratch, cond == TCG_COND_NE);
882
        break;
883

    
884
    case TCG_COND_GE:
885
    case TCG_COND_GEU:
886
    case TCG_COND_LT:
887
    case TCG_COND_LTU:
888
        /* Optimize compares with low part zero.  */
889
        if (bl == 0) {
890
            tcg_out_setcond(s, cond, ret, ah, bh, bhconst);
891
            return;
892
        }
893
        /* FALLTHRU */
894

    
895
    case TCG_COND_LE:
896
    case TCG_COND_LEU:
897
    case TCG_COND_GT:
898
    case TCG_COND_GTU:
899
        /* <= : ah < bh | (ah == bh && al <= bl) */
900
        tcg_out_setcond(s, tcg_unsigned_cond(cond), scratch, al, bl, blconst);
901
        tcg_out_comclr(s, TCG_COND_EQ, TCG_REG_R0, ah, bh, bhconst);
902
        tcg_out_movi(s, TCG_TYPE_I32, scratch, 0);
903
        tcg_out_comclr(s, tcg_invert_cond(tcg_high_cond[cond]),
904
                       TCG_REG_R0, ah, bh, bhconst);
905
        tcg_out_movi(s, TCG_TYPE_I32, scratch, 1);
906
        break;
907

    
908
    default:
909
        tcg_abort();
910
    }
911

    
912
    tcg_out_mov(s, TCG_TYPE_I32, ret, scratch);
913
}
914

    
915
static void tcg_out_movcond(TCGContext *s, int cond, TCGArg ret,
916
                            TCGArg c1, TCGArg c2, int c2const,
917
                            TCGArg v1, int v1const)
918
{
919
    tcg_out_comclr(s, tcg_invert_cond(cond), TCG_REG_R0, c1, c2, c2const);
920
    if (v1const) {
921
        tcg_out_movi(s, TCG_TYPE_I32, ret, v1);
922
    } else {
923
        tcg_out_mov(s, TCG_TYPE_I32, ret, v1);
924
    }
925
}
926

    
927
#if defined(CONFIG_SOFTMMU)
928
#include "../../softmmu_defs.h"
929

    
930
/* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
931
   int mmu_idx) */
932
static const void * const qemu_ld_helpers[4] = {
933
    helper_ldb_mmu,
934
    helper_ldw_mmu,
935
    helper_ldl_mmu,
936
    helper_ldq_mmu,
937
};
938

    
939
/* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
940
   uintxx_t val, int mmu_idx) */
941
static const void * const qemu_st_helpers[4] = {
942
    helper_stb_mmu,
943
    helper_stw_mmu,
944
    helper_stl_mmu,
945
    helper_stq_mmu,
946
};
947

    
948
/* Load and compare a TLB entry, and branch if TLB miss.  OFFSET is set to
949
   the offset of the first ADDR_READ or ADDR_WRITE member of the appropriate
950
   TLB for the memory index.  The return value is the offset from ENV
951
   contained in R1 afterward (to be used when loading ADDEND); if the
952
   return value is 0, R1 is not used.  */
953

    
954
static int tcg_out_tlb_read(TCGContext *s, int r0, int r1, int addrlo,
955
                            int addrhi, int s_bits, int lab_miss, int offset)
956
{
957
    int ret;
958

    
959
    /* Extracting the index into the TLB.  The "normal C operation" is
960
          r1 = addr_reg >> TARGET_PAGE_BITS;
961
          r1 &= CPU_TLB_SIZE - 1;
962
          r1 <<= CPU_TLB_ENTRY_BITS;
963
       What this does is extract CPU_TLB_BITS beginning at TARGET_PAGE_BITS
964
       and place them at CPU_TLB_ENTRY_BITS.  We can combine the first two
965
       operations with an EXTRU.  Unfortunately, the current value of
966
       CPU_TLB_ENTRY_BITS is > 3, so we can't merge that shift with the
967
       add that follows.  */
968
    tcg_out_extr(s, r1, addrlo, TARGET_PAGE_BITS, CPU_TLB_BITS, 0);
969
    tcg_out_shli(s, r1, r1, CPU_TLB_ENTRY_BITS);
970
    tcg_out_arith(s, r1, r1, TCG_AREG0, INSN_ADDL);
971

    
972
    /* Make sure that both the addr_{read,write} and addend can be
973
       read with a 14-bit offset from the same base register.  */
974
    if (check_fit_tl(offset + CPU_TLB_SIZE, 14)) {
975
        ret = 0;
976
    } else {
977
        ret = (offset + 0x400) & ~0x7ff;
978
        offset = ret - offset;
979
        tcg_out_addi2(s, TCG_REG_R1, r1, ret);
980
        r1 = TCG_REG_R1;
981
    }
982

    
983
    /* Load the entry from the computed slot.  */
984
    if (TARGET_LONG_BITS == 64) {
985
        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R23, r1, offset);
986
        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R20, r1, offset + 4);
987
    } else {
988
        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R20, r1, offset);
989
    }
990

    
991
    /* Compute the value that ought to appear in the TLB for a hit, namely,
992
       the page of the address.  We include the low N bits of the address
993
       to catch unaligned accesses and force them onto the slow path.  Do
994
       this computation after having issued the load from the TLB slot to
995
       give the load time to complete.  */
996
    tcg_out_andi(s, r0, addrlo, TARGET_PAGE_MASK | ((1 << s_bits) - 1));
997

    
998
    /* If not equal, jump to lab_miss. */
999
    if (TARGET_LONG_BITS == 64) {
1000
        tcg_out_brcond2(s, TCG_COND_NE, TCG_REG_R20, TCG_REG_R23,
1001
                        r0, 0, addrhi, 0, lab_miss);
1002
    } else {
1003
        tcg_out_brcond(s, TCG_COND_NE, TCG_REG_R20, r0, 0, lab_miss);
1004
    }
1005

    
1006
    return ret;
1007
}
1008

    
1009
static int tcg_out_arg_reg32(TCGContext *s, int argno, TCGArg v, bool vconst)
1010
{
1011
    if (argno < 4) {
1012
        if (vconst) {
1013
            tcg_out_movi(s, TCG_TYPE_I32, tcg_target_call_iarg_regs[argno], v);
1014
        } else {
1015
            tcg_out_mov(s, TCG_TYPE_I32, tcg_target_call_iarg_regs[argno], v);
1016
        }
1017
    } else {
1018
        if (vconst && v != 0) {
1019
            tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R20, v);
1020
            v = TCG_REG_R20;
1021
        }
1022
        tcg_out_st(s, TCG_TYPE_I32, v, TCG_REG_CALL_STACK,
1023
                   TCG_TARGET_CALL_STACK_OFFSET - ((argno - 3) * 4));
1024
    }
1025
    return argno + 1;
1026
}
1027

    
1028
static int tcg_out_arg_reg64(TCGContext *s, int argno, TCGArg vl, TCGArg vh)
1029
{
1030
    /* 64-bit arguments must go in even reg pairs and stack slots.  */
1031
    if (argno & 1) {
1032
        argno++;
1033
    }
1034
    argno = tcg_out_arg_reg32(s, argno, vl, false);
1035
    argno = tcg_out_arg_reg32(s, argno, vh, false);
1036
    return argno;
1037
}
1038
#endif
1039

    
1040
static void tcg_out_qemu_ld_direct(TCGContext *s, int datalo_reg, int datahi_reg,
1041
                                   int addr_reg, int addend_reg, int opc)
1042
{
1043
#ifdef TARGET_WORDS_BIGENDIAN
1044
    const int bswap = 0;
1045
#else
1046
    const int bswap = 1;
1047
#endif
1048

    
1049
    switch (opc) {
1050
    case 0:
1051
        tcg_out_ldst_index(s, datalo_reg, addr_reg, addend_reg, INSN_LDBX);
1052
        break;
1053
    case 0 | 4:
1054
        tcg_out_ldst_index(s, datalo_reg, addr_reg, addend_reg, INSN_LDBX);
1055
        tcg_out_ext8s(s, datalo_reg, datalo_reg);
1056
        break;
1057
    case 1:
1058
        tcg_out_ldst_index(s, datalo_reg, addr_reg, addend_reg, INSN_LDHX);
1059
        if (bswap) {
1060
            tcg_out_bswap16(s, datalo_reg, datalo_reg, 0);
1061
        }
1062
        break;
1063
    case 1 | 4:
1064
        tcg_out_ldst_index(s, datalo_reg, addr_reg, addend_reg, INSN_LDHX);
1065
        if (bswap) {
1066
            tcg_out_bswap16(s, datalo_reg, datalo_reg, 1);
1067
        } else {
1068
            tcg_out_ext16s(s, datalo_reg, datalo_reg);
1069
        }
1070
        break;
1071
    case 2:
1072
        tcg_out_ldst_index(s, datalo_reg, addr_reg, addend_reg, INSN_LDWX);
1073
        if (bswap) {
1074
            tcg_out_bswap32(s, datalo_reg, datalo_reg, TCG_REG_R20);
1075
        }
1076
        break;
1077
    case 3:
1078
        if (bswap) {
1079
            int t = datahi_reg;
1080
            datahi_reg = datalo_reg;
1081
            datalo_reg = t;
1082
        }
1083
        /* We can't access the low-part with a reg+reg addressing mode,
1084
           so perform the addition now and use reg_ofs addressing mode.  */
1085
        if (addend_reg != TCG_REG_R0) {
1086
            tcg_out_arith(s, TCG_REG_R20, addr_reg, addend_reg, INSN_ADD);
1087
            addr_reg = TCG_REG_R20;
1088
        }
1089
        /* Make sure not to clobber the base register.  */
1090
        if (datahi_reg == addr_reg) {
1091
            tcg_out_ldst(s, datalo_reg, addr_reg, 4, INSN_LDW);
1092
            tcg_out_ldst(s, datahi_reg, addr_reg, 0, INSN_LDW);
1093
        } else {
1094
            tcg_out_ldst(s, datahi_reg, addr_reg, 0, INSN_LDW);
1095
            tcg_out_ldst(s, datalo_reg, addr_reg, 4, INSN_LDW);
1096
        }
1097
        if (bswap) {
1098
            tcg_out_bswap32(s, datalo_reg, datalo_reg, TCG_REG_R20);
1099
            tcg_out_bswap32(s, datahi_reg, datahi_reg, TCG_REG_R20);
1100
        }
1101
        break;
1102
    default:
1103
        tcg_abort();
1104
    }
1105
}
1106

    
1107
static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, int opc)
1108
{
1109
    int datalo_reg = *args++;
1110
    /* Note that datahi_reg is only used for 64-bit loads.  */
1111
    int datahi_reg = (opc == 3 ? *args++ : TCG_REG_R0);
1112
    int addrlo_reg = *args++;
1113

    
1114
#if defined(CONFIG_SOFTMMU)
1115
    /* Note that addrhi_reg is only used for 64-bit guests.  */
1116
    int addrhi_reg = (TARGET_LONG_BITS == 64 ? *args++ : TCG_REG_R0);
1117
    int mem_index = *args;
1118
    int lab1, lab2, argno, offset;
1119

    
1120
    lab1 = gen_new_label();
1121
    lab2 = gen_new_label();
1122

    
1123
    offset = offsetof(CPUArchState, tlb_table[mem_index][0].addr_read);
1124
    offset = tcg_out_tlb_read(s, TCG_REG_R26, TCG_REG_R25, addrlo_reg,
1125
                              addrhi_reg, opc & 3, lab1, offset);
1126

    
1127
    /* TLB Hit.  */
1128
    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R20,
1129
               (offset ? TCG_REG_R1 : TCG_REG_R25),
1130
               offsetof(CPUArchState, tlb_table[mem_index][0].addend) - offset);
1131
    tcg_out_qemu_ld_direct(s, datalo_reg, datahi_reg, addrlo_reg,
1132
                           TCG_REG_R20, opc);
1133
    tcg_out_branch(s, lab2, 1);
1134

    
1135
    /* TLB Miss.  */
1136
    /* label1: */
1137
    tcg_out_label(s, lab1, s->code_ptr);
1138

    
1139
    argno = 0;
1140
    argno = tcg_out_arg_reg32(s, argno, TCG_AREG0, false);
1141
    if (TARGET_LONG_BITS == 64) {
1142
        argno = tcg_out_arg_reg64(s, argno, addrlo_reg, addrhi_reg);
1143
    } else {
1144
        argno = tcg_out_arg_reg32(s, argno, addrlo_reg, false);
1145
    }
1146
    argno = tcg_out_arg_reg32(s, argno, mem_index, true);
1147

    
1148
    tcg_out_call(s, qemu_ld_helpers[opc & 3]);
1149

    
1150
    switch (opc) {
1151
    case 0:
1152
        tcg_out_andi(s, datalo_reg, TCG_REG_RET0, 0xff);
1153
        break;
1154
    case 0 | 4:
1155
        tcg_out_ext8s(s, datalo_reg, TCG_REG_RET0);
1156
        break;
1157
    case 1:
1158
        tcg_out_andi(s, datalo_reg, TCG_REG_RET0, 0xffff);
1159
        break;
1160
    case 1 | 4:
1161
        tcg_out_ext16s(s, datalo_reg, TCG_REG_RET0);
1162
        break;
1163
    case 2:
1164
    case 2 | 4:
1165
        tcg_out_mov(s, TCG_TYPE_I32, datalo_reg, TCG_REG_RET0);
1166
        break;
1167
    case 3:
1168
        tcg_out_mov(s, TCG_TYPE_I32, datahi_reg, TCG_REG_RET0);
1169
        tcg_out_mov(s, TCG_TYPE_I32, datalo_reg, TCG_REG_RET1);
1170
        break;
1171
    default:
1172
        tcg_abort();
1173
    }
1174

    
1175
    /* label2: */
1176
    tcg_out_label(s, lab2, s->code_ptr);
1177
#else
1178
    tcg_out_qemu_ld_direct(s, datalo_reg, datahi_reg, addrlo_reg,
1179
                           (GUEST_BASE ? TCG_GUEST_BASE_REG : TCG_REG_R0), opc);
1180
#endif
1181
}
1182

    
1183
static void tcg_out_qemu_st_direct(TCGContext *s, int datalo_reg,
1184
                                   int datahi_reg, int addr_reg, int opc)
1185
{
1186
#ifdef TARGET_WORDS_BIGENDIAN
1187
    const int bswap = 0;
1188
#else
1189
    const int bswap = 1;
1190
#endif
1191

    
1192
    switch (opc) {
1193
    case 0:
1194
        tcg_out_ldst(s, datalo_reg, addr_reg, 0, INSN_STB);
1195
        break;
1196
    case 1:
1197
        if (bswap) {
1198
            tcg_out_bswap16(s, TCG_REG_R20, datalo_reg, 0);
1199
            datalo_reg = TCG_REG_R20;
1200
        }
1201
        tcg_out_ldst(s, datalo_reg, addr_reg, 0, INSN_STH);
1202
        break;
1203
    case 2:
1204
        if (bswap) {
1205
            tcg_out_bswap32(s, TCG_REG_R20, datalo_reg, TCG_REG_R20);
1206
            datalo_reg = TCG_REG_R20;
1207
        }
1208
        tcg_out_ldst(s, datalo_reg, addr_reg, 0, INSN_STW);
1209
        break;
1210
    case 3:
1211
        if (bswap) {
1212
            tcg_out_bswap32(s, TCG_REG_R20, datalo_reg, TCG_REG_R20);
1213
            tcg_out_bswap32(s, TCG_REG_R23, datahi_reg, TCG_REG_R23);
1214
            datahi_reg = TCG_REG_R20;
1215
            datalo_reg = TCG_REG_R23;
1216
        }
1217
        tcg_out_ldst(s, datahi_reg, addr_reg, 0, INSN_STW);
1218
        tcg_out_ldst(s, datalo_reg, addr_reg, 4, INSN_STW);
1219
        break;
1220
    default:
1221
        tcg_abort();
1222
    }
1223

    
1224
}
1225

    
1226
static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, int opc)
1227
{
1228
    int datalo_reg = *args++;
1229
    /* Note that datahi_reg is only used for 64-bit loads.  */
1230
    int datahi_reg = (opc == 3 ? *args++ : TCG_REG_R0);
1231
    int addrlo_reg = *args++;
1232

    
1233
#if defined(CONFIG_SOFTMMU)
1234
    /* Note that addrhi_reg is only used for 64-bit guests.  */
1235
    int addrhi_reg = (TARGET_LONG_BITS == 64 ? *args++ : TCG_REG_R0);
1236
    int mem_index = *args;
1237
    int lab1, lab2, argno, next, offset;
1238

    
1239
    lab1 = gen_new_label();
1240
    lab2 = gen_new_label();
1241

    
1242
    offset = offsetof(CPUArchState, tlb_table[mem_index][0].addr_write);
1243
    offset = tcg_out_tlb_read(s, TCG_REG_R26, TCG_REG_R25, addrlo_reg,
1244
                              addrhi_reg, opc, lab1, offset);
1245

    
1246
    /* TLB Hit.  */
1247
    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R20,
1248
               (offset ? TCG_REG_R1 : TCG_REG_R25),
1249
               offsetof(CPUArchState, tlb_table[mem_index][0].addend) - offset);
1250

    
1251
    /* There are no indexed stores, so we must do this addition explitly.
1252
       Careful to avoid R20, which is used for the bswaps to follow.  */
1253
    tcg_out_arith(s, TCG_REG_R31, addrlo_reg, TCG_REG_R20, INSN_ADDL);
1254
    tcg_out_qemu_st_direct(s, datalo_reg, datahi_reg, TCG_REG_R31, opc);
1255
    tcg_out_branch(s, lab2, 1);
1256

    
1257
    /* TLB Miss.  */
1258
    /* label1: */
1259
    tcg_out_label(s, lab1, s->code_ptr);
1260

    
1261
    argno = 0;
1262
    argno = tcg_out_arg_reg32(s, argno, TCG_AREG0, false);
1263
    if (TARGET_LONG_BITS == 64) {
1264
        argno = tcg_out_arg_reg64(s, argno, addrlo_reg, addrhi_reg);
1265
    } else {
1266
        argno = tcg_out_arg_reg32(s, argno, addrlo_reg, false);
1267
    }
1268

    
1269
    next = (argno < 4 ? tcg_target_call_iarg_regs[argno] : TCG_REG_R20);
1270
    switch(opc) {
1271
    case 0:
1272
        tcg_out_andi(s, next, datalo_reg, 0xff);
1273
        argno = tcg_out_arg_reg32(s, argno, next, false);
1274
        break;
1275
    case 1:
1276
        tcg_out_andi(s, next, datalo_reg, 0xffff);
1277
        argno = tcg_out_arg_reg32(s, argno, next, false);
1278
        break;
1279
    case 2:
1280
        argno = tcg_out_arg_reg32(s, argno, datalo_reg, false);
1281
        break;
1282
    case 3:
1283
        argno = tcg_out_arg_reg64(s, argno, datalo_reg, datahi_reg);
1284
        break;
1285
    default:
1286
        tcg_abort();
1287
    }
1288
    argno = tcg_out_arg_reg32(s, argno, mem_index, true);
1289

    
1290
    tcg_out_call(s, qemu_st_helpers[opc]);
1291

    
1292
    /* label2: */
1293
    tcg_out_label(s, lab2, s->code_ptr);
1294
#else
1295
    /* There are no indexed stores, so if GUEST_BASE is set we must do
1296
       the add explicitly.  Careful to avoid R20, which is used for the
1297
       bswaps to follow.  */
1298
    if (GUEST_BASE != 0) {
1299
        tcg_out_arith(s, TCG_REG_R31, addrlo_reg,
1300
                      TCG_GUEST_BASE_REG, INSN_ADDL);
1301
        addrlo_reg = TCG_REG_R31;
1302
    }
1303
    tcg_out_qemu_st_direct(s, datalo_reg, datahi_reg, addrlo_reg, opc);
1304
#endif
1305
}
1306

    
1307
static void tcg_out_exit_tb(TCGContext *s, TCGArg arg)
1308
{
1309
    if (!check_fit_tl(arg, 14)) {
1310
        uint32_t hi, lo;
1311
        hi = arg & ~0x7ff;
1312
        lo = arg & 0x7ff;
1313
        if (lo) {
1314
            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RET0, hi);
1315
            tcg_out32(s, INSN_BV | INSN_R2(TCG_REG_R18));
1316
            tcg_out_addi(s, TCG_REG_RET0, lo);
1317
            return;
1318
        }
1319
        arg = hi;
1320
    }
1321
    tcg_out32(s, INSN_BV | INSN_R2(TCG_REG_R18));
1322
    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RET0, arg);
1323
}
1324

    
1325
static void tcg_out_goto_tb(TCGContext *s, TCGArg arg)
1326
{
1327
    if (s->tb_jmp_offset) {
1328
        /* direct jump method */
1329
        fprintf(stderr, "goto_tb direct\n");
1330
        tcg_abort();
1331
    } else {
1332
        /* indirect jump method */
1333
        tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_R20, TCG_REG_R0,
1334
                   (tcg_target_long)(s->tb_next + arg));
1335
        tcg_out32(s, INSN_BV_N | INSN_R2(TCG_REG_R20));
1336
    }
1337
    s->tb_next_offset[arg] = s->code_ptr - s->code_buf;
1338
}
1339

    
1340
static inline void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
1341
                              const int *const_args)
1342
{
1343
    switch (opc) {
1344
    case INDEX_op_exit_tb:
1345
        tcg_out_exit_tb(s, args[0]);
1346
        break;
1347
    case INDEX_op_goto_tb:
1348
        tcg_out_goto_tb(s, args[0]);
1349
        break;
1350

    
1351
    case INDEX_op_call:
1352
        if (const_args[0]) {
1353
            tcg_out_call(s, (void *)args[0]);
1354
        } else {
1355
            /* ??? FIXME: the value in the register in args[0] is almost
1356
               certainly a procedure descriptor, not a code address.  We
1357
               probably need to use the millicode $$dyncall routine.  */
1358
            tcg_abort();
1359
        }
1360
        break;
1361

    
1362
    case INDEX_op_jmp:
1363
        fprintf(stderr, "unimplemented jmp\n");
1364
        tcg_abort();
1365
        break;
1366

    
1367
    case INDEX_op_br:
1368
        tcg_out_branch(s, args[0], 1);
1369
        break;
1370

    
1371
    case INDEX_op_movi_i32:
1372
        tcg_out_movi(s, TCG_TYPE_I32, args[0], (uint32_t)args[1]);
1373
        break;
1374

    
1375
    case INDEX_op_ld8u_i32:
1376
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_LDB);
1377
        break;
1378
    case INDEX_op_ld8s_i32:
1379
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_LDB);
1380
        tcg_out_ext8s(s, args[0], args[0]);
1381
        break;
1382
    case INDEX_op_ld16u_i32:
1383
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_LDH);
1384
        break;
1385
    case INDEX_op_ld16s_i32:
1386
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_LDH);
1387
        tcg_out_ext16s(s, args[0], args[0]);
1388
        break;
1389
    case INDEX_op_ld_i32:
1390
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_LDW);
1391
        break;
1392

    
1393
    case INDEX_op_st8_i32:
1394
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_STB);
1395
        break;
1396
    case INDEX_op_st16_i32:
1397
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_STH);
1398
        break;
1399
    case INDEX_op_st_i32:
1400
        tcg_out_ldst(s, args[0], args[1], args[2], INSN_STW);
1401
        break;
1402

    
1403
    case INDEX_op_add_i32:
1404
        if (const_args[2]) {
1405
            tcg_out_addi2(s, args[0], args[1], args[2]);
1406
        } else {
1407
            tcg_out_arith(s, args[0], args[1], args[2], INSN_ADDL);
1408
        }
1409
        break;
1410

    
1411
    case INDEX_op_sub_i32:
1412
        if (const_args[1]) {
1413
            if (const_args[2]) {
1414
                tcg_out_movi(s, TCG_TYPE_I32, args[0], args[1] - args[2]);
1415
            } else {
1416
                /* Recall that SUBI is a reversed subtract.  */
1417
                tcg_out_arithi(s, args[0], args[2], args[1], INSN_SUBI);
1418
            }
1419
        } else if (const_args[2]) {
1420
            tcg_out_addi2(s, args[0], args[1], -args[2]);
1421
        } else {
1422
            tcg_out_arith(s, args[0], args[1], args[2], INSN_SUB);
1423
        }
1424
        break;
1425

    
1426
    case INDEX_op_and_i32:
1427
        if (const_args[2]) {
1428
            tcg_out_andi(s, args[0], args[1], args[2]);
1429
        } else {
1430
            tcg_out_arith(s, args[0], args[1], args[2], INSN_AND);
1431
        }
1432
        break;
1433

    
1434
    case INDEX_op_or_i32:
1435
        if (const_args[2]) {
1436
            tcg_out_ori(s, args[0], args[1], args[2]);
1437
        } else {
1438
            tcg_out_arith(s, args[0], args[1], args[2], INSN_OR);
1439
        }
1440
        break;
1441

    
1442
    case INDEX_op_xor_i32:
1443
        tcg_out_arith(s, args[0], args[1], args[2], INSN_XOR);
1444
        break;
1445

    
1446
    case INDEX_op_andc_i32:
1447
        if (const_args[2]) {
1448
            tcg_out_andi(s, args[0], args[1], ~args[2]);
1449
        } else {
1450
            tcg_out_arith(s, args[0], args[1], args[2], INSN_ANDCM);
1451
        }
1452
        break;
1453

    
1454
    case INDEX_op_shl_i32:
1455
        if (const_args[2]) {
1456
            tcg_out_shli(s, args[0], args[1], args[2]);
1457
        } else {
1458
            tcg_out_shl(s, args[0], args[1], args[2]);
1459
        }
1460
        break;
1461

    
1462
    case INDEX_op_shr_i32:
1463
        if (const_args[2]) {
1464
            tcg_out_shri(s, args[0], args[1], args[2]);
1465
        } else {
1466
            tcg_out_shr(s, args[0], args[1], args[2]);
1467
        }
1468
        break;
1469

    
1470
    case INDEX_op_sar_i32:
1471
        if (const_args[2]) {
1472
            tcg_out_sari(s, args[0], args[1], args[2]);
1473
        } else {
1474
            tcg_out_sar(s, args[0], args[1], args[2]);
1475
        }
1476
        break;
1477

    
1478
    case INDEX_op_rotl_i32:
1479
        if (const_args[2]) {
1480
            tcg_out_rotli(s, args[0], args[1], args[2]);
1481
        } else {
1482
            tcg_out_rotl(s, args[0], args[1], args[2]);
1483
        }
1484
        break;
1485

    
1486
    case INDEX_op_rotr_i32:
1487
        if (const_args[2]) {
1488
            tcg_out_rotri(s, args[0], args[1], args[2]);
1489
        } else {
1490
            tcg_out_rotr(s, args[0], args[1], args[2]);
1491
        }
1492
        break;
1493

    
1494
    case INDEX_op_mul_i32:
1495
        tcg_out_xmpyu(s, args[0], TCG_REG_R0, args[1], args[2]);
1496
        break;
1497
    case INDEX_op_mulu2_i32:
1498
        tcg_out_xmpyu(s, args[0], args[1], args[2], args[3]);
1499
        break;
1500

    
1501
    case INDEX_op_bswap16_i32:
1502
        tcg_out_bswap16(s, args[0], args[1], 0);
1503
        break;
1504
    case INDEX_op_bswap32_i32:
1505
        tcg_out_bswap32(s, args[0], args[1], TCG_REG_R20);
1506
        break;
1507

    
1508
    case INDEX_op_not_i32:
1509
        tcg_out_arithi(s, args[0], args[1], -1, INSN_SUBI);
1510
        break;
1511
    case INDEX_op_ext8s_i32:
1512
        tcg_out_ext8s(s, args[0], args[1]);
1513
        break;
1514
    case INDEX_op_ext16s_i32:
1515
        tcg_out_ext16s(s, args[0], args[1]);
1516
        break;
1517

    
1518
    case INDEX_op_brcond_i32:
1519
        tcg_out_brcond(s, args[2], args[0], args[1], const_args[1], args[3]);
1520
        break;
1521
    case INDEX_op_brcond2_i32:
1522
        tcg_out_brcond2(s, args[4], args[0], args[1],
1523
                        args[2], const_args[2],
1524
                        args[3], const_args[3], args[5]);
1525
        break;
1526

    
1527
    case INDEX_op_setcond_i32:
1528
        tcg_out_setcond(s, args[3], args[0], args[1], args[2], const_args[2]);
1529
        break;
1530
    case INDEX_op_setcond2_i32:
1531
        tcg_out_setcond2(s, args[5], args[0], args[1], args[2],
1532
                         args[3], const_args[3], args[4], const_args[4]);
1533
        break;
1534

    
1535
    case INDEX_op_movcond_i32:
1536
        tcg_out_movcond(s, args[5], args[0], args[1], args[2], const_args[2],
1537
                        args[3], const_args[3]);
1538
        break;
1539

    
1540
    case INDEX_op_add2_i32:
1541
        tcg_out_add2(s, args[0], args[1], args[2], args[3],
1542
                     args[4], args[5], const_args[4]);
1543
        break;
1544

    
1545
    case INDEX_op_sub2_i32:
1546
        tcg_out_sub2(s, args[0], args[1], args[2], args[3],
1547
                     args[4], args[5], const_args[2], const_args[4]);
1548
        break;
1549

    
1550
    case INDEX_op_deposit_i32:
1551
        if (const_args[2]) {
1552
            tcg_out_depi(s, args[0], args[2], args[3], args[4]);
1553
        } else {
1554
            tcg_out_dep(s, args[0], args[2], args[3], args[4]);
1555
        }
1556
        break;
1557

    
1558
    case INDEX_op_qemu_ld8u:
1559
        tcg_out_qemu_ld(s, args, 0);
1560
        break;
1561
    case INDEX_op_qemu_ld8s:
1562
        tcg_out_qemu_ld(s, args, 0 | 4);
1563
        break;
1564
    case INDEX_op_qemu_ld16u:
1565
        tcg_out_qemu_ld(s, args, 1);
1566
        break;
1567
    case INDEX_op_qemu_ld16s:
1568
        tcg_out_qemu_ld(s, args, 1 | 4);
1569
        break;
1570
    case INDEX_op_qemu_ld32:
1571
        tcg_out_qemu_ld(s, args, 2);
1572
        break;
1573
    case INDEX_op_qemu_ld64:
1574
        tcg_out_qemu_ld(s, args, 3);
1575
        break;
1576

    
1577
    case INDEX_op_qemu_st8:
1578
        tcg_out_qemu_st(s, args, 0);
1579
        break;
1580
    case INDEX_op_qemu_st16:
1581
        tcg_out_qemu_st(s, args, 1);
1582
        break;
1583
    case INDEX_op_qemu_st32:
1584
        tcg_out_qemu_st(s, args, 2);
1585
        break;
1586
    case INDEX_op_qemu_st64:
1587
        tcg_out_qemu_st(s, args, 3);
1588
        break;
1589

    
1590
    default:
1591
        fprintf(stderr, "unknown opcode 0x%x\n", opc);
1592
        tcg_abort();
1593
    }
1594
}
1595

    
1596
static const TCGTargetOpDef hppa_op_defs[] = {
1597
    { INDEX_op_exit_tb, { } },
1598
    { INDEX_op_goto_tb, { } },
1599

    
1600
    { INDEX_op_call, { "ri" } },
1601
    { INDEX_op_jmp, { "r" } },
1602
    { INDEX_op_br, { } },
1603

    
1604
    { INDEX_op_mov_i32, { "r", "r" } },
1605
    { INDEX_op_movi_i32, { "r" } },
1606

    
1607
    { INDEX_op_ld8u_i32, { "r", "r" } },
1608
    { INDEX_op_ld8s_i32, { "r", "r" } },
1609
    { INDEX_op_ld16u_i32, { "r", "r" } },
1610
    { INDEX_op_ld16s_i32, { "r", "r" } },
1611
    { INDEX_op_ld_i32, { "r", "r" } },
1612
    { INDEX_op_st8_i32, { "rZ", "r" } },
1613
    { INDEX_op_st16_i32, { "rZ", "r" } },
1614
    { INDEX_op_st_i32, { "rZ", "r" } },
1615

    
1616
    { INDEX_op_add_i32, { "r", "rZ", "ri" } },
1617
    { INDEX_op_sub_i32, { "r", "rI", "ri" } },
1618
    { INDEX_op_and_i32, { "r", "rZ", "rM" } },
1619
    { INDEX_op_or_i32, { "r", "rZ", "rO" } },
1620
    { INDEX_op_xor_i32, { "r", "rZ", "rZ" } },
1621
    /* Note that the second argument will be inverted, which means
1622
       we want a constant whose inversion matches M, and that O = ~M.
1623
       See the implementation of and_mask_p.  */
1624
    { INDEX_op_andc_i32, { "r", "rZ", "rO" } },
1625

    
1626
    { INDEX_op_mul_i32, { "r", "r", "r" } },
1627
    { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } },
1628

    
1629
    { INDEX_op_shl_i32, { "r", "r", "ri" } },
1630
    { INDEX_op_shr_i32, { "r", "r", "ri" } },
1631
    { INDEX_op_sar_i32, { "r", "r", "ri" } },
1632
    { INDEX_op_rotl_i32, { "r", "r", "ri" } },
1633
    { INDEX_op_rotr_i32, { "r", "r", "ri" } },
1634

    
1635
    { INDEX_op_bswap16_i32, { "r", "r" } },
1636
    { INDEX_op_bswap32_i32, { "r", "r" } },
1637
    { INDEX_op_not_i32, { "r", "r" } },
1638

    
1639
    { INDEX_op_ext8s_i32, { "r", "r" } },
1640
    { INDEX_op_ext16s_i32, { "r", "r" } },
1641

    
1642
    { INDEX_op_brcond_i32, { "rZ", "rJ" } },
1643
    { INDEX_op_brcond2_i32,  { "rZ", "rZ", "rJ", "rJ" } },
1644

    
1645
    { INDEX_op_setcond_i32, { "r", "rZ", "rI" } },
1646
    { INDEX_op_setcond2_i32, { "r", "rZ", "rZ", "rI", "rI" } },
1647

    
1648
    /* ??? We can actually support a signed 14-bit arg3, but we
1649
       only have existing constraints for a signed 11-bit.  */
1650
    { INDEX_op_movcond_i32, { "r", "rZ", "rI", "rI", "0" } },
1651

    
1652
    { INDEX_op_add2_i32, { "r", "r", "rZ", "rZ", "rI", "rZ" } },
1653
    { INDEX_op_sub2_i32, { "r", "r", "rI", "rZ", "rK", "rZ" } },
1654

    
1655
    { INDEX_op_deposit_i32, { "r", "0", "rJ" } },
1656

    
1657
#if TARGET_LONG_BITS == 32
1658
    { INDEX_op_qemu_ld8u, { "r", "L" } },
1659
    { INDEX_op_qemu_ld8s, { "r", "L" } },
1660
    { INDEX_op_qemu_ld16u, { "r", "L" } },
1661
    { INDEX_op_qemu_ld16s, { "r", "L" } },
1662
    { INDEX_op_qemu_ld32, { "r", "L" } },
1663
    { INDEX_op_qemu_ld64, { "r", "r", "L" } },
1664

    
1665
    { INDEX_op_qemu_st8, { "LZ", "L" } },
1666
    { INDEX_op_qemu_st16, { "LZ", "L" } },
1667
    { INDEX_op_qemu_st32, { "LZ", "L" } },
1668
    { INDEX_op_qemu_st64, { "LZ", "LZ", "L" } },
1669
#else
1670
    { INDEX_op_qemu_ld8u, { "r", "L", "L" } },
1671
    { INDEX_op_qemu_ld8s, { "r", "L", "L" } },
1672
    { INDEX_op_qemu_ld16u, { "r", "L", "L" } },
1673
    { INDEX_op_qemu_ld16s, { "r", "L", "L" } },
1674
    { INDEX_op_qemu_ld32, { "r", "L", "L" } },
1675
    { INDEX_op_qemu_ld64, { "r", "r", "L", "L" } },
1676

    
1677
    { INDEX_op_qemu_st8, { "LZ", "L", "L" } },
1678
    { INDEX_op_qemu_st16, { "LZ", "L", "L" } },
1679
    { INDEX_op_qemu_st32, { "LZ", "L", "L" } },
1680
    { INDEX_op_qemu_st64, { "LZ", "LZ", "L", "L" } },
1681
#endif
1682
    { -1 },
1683
};
1684

    
1685
static int tcg_target_callee_save_regs[] = {
1686
    /* R2, the return address register, is saved specially
1687
       in the caller's frame.  */
1688
    /* R3, the frame pointer, is not currently modified.  */
1689
    TCG_REG_R4,
1690
    TCG_REG_R5,
1691
    TCG_REG_R6,
1692
    TCG_REG_R7,
1693
    TCG_REG_R8,
1694
    TCG_REG_R9,
1695
    TCG_REG_R10,
1696
    TCG_REG_R11,
1697
    TCG_REG_R12,
1698
    TCG_REG_R13,
1699
    TCG_REG_R14,
1700
    TCG_REG_R15,
1701
    TCG_REG_R16,
1702
    TCG_REG_R17, /* R17 is the global env.  */
1703
    TCG_REG_R18
1704
};
1705

    
1706
#define FRAME_SIZE ((-TCG_TARGET_CALL_STACK_OFFSET \
1707
                     + TCG_TARGET_STATIC_CALL_ARGS_SIZE \
1708
                     + ARRAY_SIZE(tcg_target_callee_save_regs) * 4 \
1709
                     + CPU_TEMP_BUF_NLONGS * sizeof(long) \
1710
                     + TCG_TARGET_STACK_ALIGN - 1) \
1711
                    & -TCG_TARGET_STACK_ALIGN)
1712

    
1713
static void tcg_target_qemu_prologue(TCGContext *s)
1714
{
1715
    int frame_size, i;
1716

    
1717
    frame_size = FRAME_SIZE;
1718

    
1719
    /* The return address is stored in the caller's frame.  */
1720
    tcg_out_st(s, TCG_TYPE_PTR, TCG_REG_RP, TCG_REG_CALL_STACK, -20);
1721

    
1722
    /* Allocate stack frame, saving the first register at the same time.  */
1723
    tcg_out_ldst(s, tcg_target_callee_save_regs[0],
1724
                 TCG_REG_CALL_STACK, frame_size, INSN_STWM);
1725

    
1726
    /* Save all callee saved registers.  */
1727
    for (i = 1; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1728
        tcg_out_st(s, TCG_TYPE_PTR, tcg_target_callee_save_regs[i],
1729
                   TCG_REG_CALL_STACK, -frame_size + i * 4);
1730
    }
1731

    
1732
    /* Record the location of the TCG temps.  */
1733
    tcg_set_frame(s, TCG_REG_CALL_STACK, -frame_size + i * 4,
1734
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
1735

    
1736
#ifdef CONFIG_USE_GUEST_BASE
1737
    if (GUEST_BASE != 0) {
1738
        tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, GUEST_BASE);
1739
        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1740
    }
1741
#endif
1742

    
1743
    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1744

    
1745
    /* Jump to TB, and adjust R18 to be the return address.  */
1746
    tcg_out32(s, INSN_BLE_SR4 | INSN_R2(tcg_target_call_iarg_regs[1]));
1747
    tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_R18, TCG_REG_R31);
1748

    
1749
    /* Restore callee saved registers.  */
1750
    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_RP, TCG_REG_CALL_STACK,
1751
               -frame_size - 20);
1752
    for (i = 1; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1753
        tcg_out_ld(s, TCG_TYPE_PTR, tcg_target_callee_save_regs[i],
1754
                   TCG_REG_CALL_STACK, -frame_size + i * 4);
1755
    }
1756

    
1757
    /* Deallocate stack frame and return.  */
1758
    tcg_out32(s, INSN_BV | INSN_R2(TCG_REG_RP));
1759
    tcg_out_ldst(s, tcg_target_callee_save_regs[0],
1760
                 TCG_REG_CALL_STACK, -frame_size, INSN_LDWM);
1761
}
1762

    
1763
static void tcg_target_init(TCGContext *s)
1764
{
1765
    tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
1766

    
1767
    tcg_regset_clear(tcg_target_call_clobber_regs);
1768
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R20);
1769
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R21);
1770
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R22);
1771
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R23);
1772
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R24);
1773
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R25);
1774
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R26);
1775
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RET0);
1776
    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_RET1);
1777

    
1778
    tcg_regset_clear(s->reserved_regs);
1779
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);  /* hardwired to zero */
1780
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R1);  /* addil target */
1781
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_RP);  /* link register */
1782
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R3);  /* frame pointer */
1783
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R18); /* return pointer */
1784
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R19); /* clobbered w/o pic */
1785
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R20); /* reserved */
1786
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_DP);  /* data pointer */
1787
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);  /* stack pointer */
1788
    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R31); /* ble link reg */
1789

    
1790
    tcg_add_target_add_op_defs(hppa_op_defs);
1791
}
1792

    
1793
typedef struct {
1794
    uint32_t len __attribute__((aligned((sizeof(void *)))));
1795
    uint32_t id;
1796
    uint8_t version;
1797
    char augmentation[1];
1798
    uint8_t code_align;
1799
    uint8_t data_align;
1800
    uint8_t return_column;
1801
} DebugFrameCIE;
1802

    
1803
typedef struct {
1804
    uint32_t len __attribute__((aligned((sizeof(void *)))));
1805
    uint32_t cie_offset;
1806
    tcg_target_long func_start __attribute__((packed));
1807
    tcg_target_long func_len __attribute__((packed));
1808
    uint8_t def_cfa[4];
1809
    uint8_t ret_ofs[3];
1810
    uint8_t reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1811
} DebugFrameFDE;
1812

    
1813
typedef struct {
1814
    DebugFrameCIE cie;
1815
    DebugFrameFDE fde;
1816
} DebugFrame;
1817

    
1818
#define ELF_HOST_MACHINE  EM_PARISC
1819
#define ELF_HOST_FLAGS    EFA_PARISC_1_1
1820

    
1821
/* ??? BFD (and thus GDB) wants very much to distinguish between HPUX
1822
   and other extensions.  We don't really care, but if we don't set this
1823
   to *something* then the object file won't be properly matched.  */
1824
#define ELF_OSABI         ELFOSABI_LINUX
1825

    
1826
static DebugFrame debug_frame = {
1827
    .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
1828
    .cie.id = -1,
1829
    .cie.version = 1,
1830
    .cie.code_align = 1,
1831
    .cie.data_align = 1,
1832
    .cie.return_column = 2,
1833

    
1834
    .fde.len = sizeof(DebugFrameFDE)-4, /* length after .len member */
1835
    .fde.def_cfa = {
1836
        0x12, 30,                       /* DW_CFA_def_cfa_sf sp, ... */
1837
        (-FRAME_SIZE & 0x7f) | 0x80,     /* ... sleb128 -FRAME_SIZE */
1838
        (-FRAME_SIZE >> 7) & 0x7f
1839
    },
1840
    .fde.ret_ofs = {
1841
        0x11, 2, (-20 / 4) & 0x7f       /* DW_CFA_offset_extended_sf r2, 20 */
1842
    },
1843
    .fde.reg_ofs = {
1844
        /* This must match the ordering in tcg_target_callee_save_regs.  */
1845
        0x80 + 4, 0,                    /* DW_CFA_offset r4, 0 */
1846
        0x80 + 5, 4,                    /* DW_CFA_offset r5, 4 */
1847
        0x80 + 6, 8,                    /* DW_CFA_offset r6, 8 */
1848
        0x80 + 7, 12,                    /* ... */
1849
        0x80 + 8, 16,
1850
        0x80 + 9, 20,
1851
        0x80 + 10, 24,
1852
        0x80 + 11, 28,
1853
        0x80 + 12, 32,
1854
        0x80 + 13, 36,
1855
        0x80 + 14, 40,
1856
        0x80 + 15, 44,
1857
        0x80 + 16, 48,
1858
        0x80 + 17, 52,
1859
        0x80 + 18, 56,
1860
    }
1861
};
1862

    
1863
void tcg_register_jit(void *buf, size_t buf_size)
1864
{
1865
    debug_frame.fde.func_start = (tcg_target_long) buf;
1866
    debug_frame.fde.func_len = buf_size;
1867

    
1868
    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1869
}