Statistics
| Branch: | Revision:

root / target-mips / translate.c @ 9d1d106a

History | View | Annotate | Download (42.6 kB)

1
/*
2
 *  MIPS32 emulation for qemu: main translation routines.
3
 * 
4
 *  Copyright (c) 2004-2005 Jocelyn Mayer
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 */
20

    
21
#include <stdarg.h>
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <inttypes.h>
26

    
27
#include "cpu.h"
28
#include "exec-all.h"
29
#include "disas.h"
30

    
31
#define MIPS_DEBUG_DISAS
32
//#define MIPS_SINGLE_STEP
33

    
34
enum {
35
#define DEF(s, n, copy_size) INDEX_op_ ## s,
36
#include "opc.h"
37
#undef DEF
38
    NB_OPS,
39
};
40

    
41
static uint16_t *gen_opc_ptr;
42
static uint32_t *gen_opparam_ptr;
43

    
44
#include "gen-op.h"
45

    
46
const unsigned char *regnames[] =
47
    { "r0", "at", "v0", "v1", "a0", "a1", "a2", "a3",
48
      "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
49
      "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
50
      "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra", };
51

    
52
/* Warning: no function for r0 register (hard wired to zero) */
53
#define GEN32(func, NAME) \
54
static GenOpFunc *NAME ## _table [32] = {                                     \
55
NULL,       NAME ## 1, NAME ## 2, NAME ## 3,                                  \
56
NAME ## 4,  NAME ## 5, NAME ## 6, NAME ## 7,                                  \
57
NAME ## 8,  NAME ## 9, NAME ## 10, NAME ## 11,                                \
58
NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15,                               \
59
NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19,                               \
60
NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23,                               \
61
NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27,                               \
62
NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31,                               \
63
};                                                                            \
64
static inline void func(int n)                                                \
65
{                                                                             \
66
    NAME ## _table[n]();                                                      \
67
}
68

    
69
/* General purpose registers moves */
70
GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);
71
GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);
72
GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);
73

    
74
GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);
75
GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);
76

    
77
typedef struct DisasContext {
78
    struct TranslationBlock *tb;
79
    target_ulong pc, saved_pc;
80
    uint32_t opcode;
81
    /* Routine used to access memory */
82
    int mem_idx;
83
    uint32_t hflags, saved_hflags;
84
    uint32_t CP0_Status;
85
    int bstate;
86
    target_ulong btarget;
87
} DisasContext;
88

    
89
enum {
90
    BS_NONE     = 0, /* We go out of the TB without reaching a branch or an
91
                      * exception condition
92
                      */
93
    BS_STOP     = 1, /* We want to stop translation for any reason */
94
    BS_BRANCH   = 2, /* We reached a branch condition     */
95
    BS_EXCP     = 3, /* We reached an exception condition */
96
};
97

    
98
#if defined MIPS_DEBUG_DISAS
99
#define MIPS_DEBUG(fmt, args...)                                              \
100
do {                                                                          \
101
    if (loglevel & CPU_LOG_TB_IN_ASM) {                                       \
102
        fprintf(logfile, "%08x: %08x " fmt "\n",                              \
103
                ctx->pc, ctx->opcode , ##args);                               \
104
    }                                                                         \
105
} while (0)
106
#else
107
#define MIPS_DEBUG(fmt, args...) do { } while(0)
108
#endif
109

    
110
#define MIPS_INVAL(op)                                                        \
111
do {                                                                          \
112
    MIPS_DEBUG("Invalid %s %03x %03x %03x", op, ctx->opcode >> 26,            \
113
               ctx->opcode & 0x3F, ((ctx->opcode >> 16) & 0x1F));             \
114
} while (0)
115

    
116
#define GEN_LOAD_REG_TN(Tn, Rn)                                               \
117
do {                                                                          \
118
    if (Rn == 0) {                                                            \
119
        glue(gen_op_reset_, Tn)();                                            \
120
    } else {                                                                  \
121
        glue(gen_op_load_gpr_, Tn)(Rn);                                       \
122
    }                                                                         \
123
} while (0)
124

    
125
#define GEN_LOAD_IMM_TN(Tn, Imm)                                              \
126
do {                                                                          \
127
    if (Imm == 0) {                                                           \
128
        glue(gen_op_reset_, Tn)();                                            \
129
    } else {                                                                  \
130
        glue(gen_op_set_, Tn)(Imm);                                           \
131
    }                                                                         \
132
} while (0)
133

    
134
#define GEN_STORE_TN_REG(Rn, Tn)                                              \
135
do {                                                                          \
136
    if (Rn != 0) {                                                            \
137
        glue(glue(gen_op_store_, Tn),_gpr)(Rn);                               \
138
    }                                                                         \
139
} while (0)
140

    
141
static inline void save_cpu_state (DisasContext *ctx, int do_save_pc)
142
{
143
#if defined MIPS_DEBUG_DISAS
144
    if (loglevel & CPU_LOG_TB_IN_ASM) {
145
            fprintf(logfile, "hflags %08x saved %08x\n",
146
                    ctx->hflags, ctx->saved_hflags);
147
    }
148
#endif
149
    if (do_save_pc && ctx->pc != ctx->saved_pc) {
150
        gen_op_save_pc(ctx->pc);
151
        ctx->saved_pc = ctx->pc;
152
    }
153
    if (ctx->hflags != ctx->saved_hflags) {
154
        gen_op_save_state(ctx->hflags);
155
        ctx->saved_hflags = ctx->hflags;
156
        if (ctx->hflags & MIPS_HFLAG_BR) {
157
            gen_op_save_breg_target();
158
        } else if (ctx->hflags & MIPS_HFLAG_B) {
159
            gen_op_save_btarget(ctx->btarget);
160
        } else if (ctx->hflags & MIPS_HFLAG_BMASK) {
161
            gen_op_save_bcond();
162
            gen_op_save_btarget(ctx->btarget);
163
        }
164
    }
165
}
166

    
167
static inline void generate_exception (DisasContext *ctx, int excp)
168
{
169
#if defined MIPS_DEBUG_DISAS
170
    if (loglevel & CPU_LOG_TB_IN_ASM)
171
            fprintf(logfile, "%s: raise exception %d\n", __func__, excp);
172
#endif
173
    save_cpu_state(ctx, 1);
174
    gen_op_raise_exception(excp);
175
    ctx->bstate = BS_EXCP;
176
}
177

    
178
#if defined(CONFIG_USER_ONLY)
179
#define op_ldst(name)        gen_op_##name##_raw()
180
#define OP_LD_TABLE(width)
181
#define OP_ST_TABLE(width)
182
#else
183
#define op_ldst(name)        (*gen_op_##name[ctx->mem_idx])()
184
#define OP_LD_TABLE(width)                                                    \
185
static GenOpFunc *gen_op_l##width[] = {                                       \
186
    &gen_op_l##width##_user,                                                  \
187
    &gen_op_l##width##_kernel,                                                \
188
}
189
#define OP_ST_TABLE(width)                                                    \
190
static GenOpFunc *gen_op_s##width[] = {                                       \
191
    &gen_op_s##width##_user,                                                  \
192
    &gen_op_s##width##_kernel,                                                \
193
}
194
#endif
195

    
196
#ifdef TARGET_MIPS64
197
OP_LD_TABLE(d);
198
OP_LD_TABLE(dl);
199
OP_LD_TABLE(dr);
200
OP_ST_TABLE(d);
201
OP_ST_TABLE(dl);
202
OP_ST_TABLE(dr);
203
#endif
204
OP_LD_TABLE(w);
205
OP_LD_TABLE(wl);
206
OP_LD_TABLE(wr);
207
OP_ST_TABLE(w);
208
OP_ST_TABLE(wl);
209
OP_ST_TABLE(wr);
210
OP_LD_TABLE(h);
211
OP_LD_TABLE(hu);
212
OP_ST_TABLE(h);
213
OP_LD_TABLE(b);
214
OP_LD_TABLE(bu);
215
OP_ST_TABLE(b);
216
OP_LD_TABLE(l);
217
OP_ST_TABLE(c);
218

    
219
/* Load and store */
220
static void gen_ldst (DisasContext *ctx, uint16_t opc, int rt,
221
                      int base, int16_t offset)
222
{
223
    const unsigned char *opn = "unk";
224

    
225
    if (base == 0) {
226
        GEN_LOAD_IMM_TN(T0, offset);
227
    } else if (offset == 0) {
228
        gen_op_load_gpr_T0(base);
229
    } else {
230
        gen_op_load_gpr_T0(base);
231
        gen_op_set_T1(offset);
232
        gen_op_add();
233
    }
234
    /* Don't do NOP if destination is zero: we must perform the actual
235
     * memory access
236
     */
237
    switch (opc) {
238
#if defined(TARGET_MIPS64)
239
    case OPC_LD:
240
#if defined (MIPS_HAS_UNALIGNED_LS)
241
    case OPC_ULD:
242
#endif
243
        op_ldst(ld);
244
        GEN_STORE_TN_REG(rt, T0);
245
        opn = "ld";
246
        break;
247
    case OPC_SD:
248
#if defined (MIPS_HAS_UNALIGNED_LS)
249
    case OPC_USD:
250
#endif
251
        GEN_LOAD_REG_TN(T1, rt);
252
        op_ldst(sd);
253
        opn = "sd";
254
        break;
255
    case OPC_LDL:
256
        op_ldst(ldl);
257
        GEN_STORE_TN_REG(rt, T0);
258
        opn = "ldl";
259
        break;
260
    case OPC_SDL:
261
        GEN_LOAD_REG_TN(T1, rt);
262
        op_ldst(sdl);
263
        opn = "sdl";
264
        break;
265
    case OPC_LDR:
266
        op_ldst(ldr);
267
        GEN_STORE_TN_REG(rt, T0);
268
        opn = "ldr";
269
        break;
270
    case OPC_SDR:
271
        GEN_LOAD_REG_TN(T1, rt);
272
        op_ldst(sdr);
273
        opn = "sdr";
274
        break;
275
#endif
276
    case OPC_LW:
277
#if defined (MIPS_HAS_UNALIGNED_LS)
278
    case OPC_ULW:
279
#endif
280
        op_ldst(lw);
281
        GEN_STORE_TN_REG(rt, T0);
282
        opn = "lw";
283
        break;
284
    case OPC_SW:
285
#if defined (MIPS_HAS_UNALIGNED_LS)
286
    case OPC_USW:
287
#endif
288
        GEN_LOAD_REG_TN(T1, rt);
289
        op_ldst(sw);
290
        opn = "sw";
291
        break;
292
    case OPC_LH:
293
#if defined (MIPS_HAS_UNALIGNED_LS)
294
    case OPC_ULH:
295
#endif
296
        op_ldst(lh);
297
        GEN_STORE_TN_REG(rt, T0);
298
        opn = "lh";
299
        break;
300
    case OPC_SH:
301
#if defined (MIPS_HAS_UNALIGNED_LS)
302
    case OPC_USH:
303
#endif
304
        GEN_LOAD_REG_TN(T1, rt);
305
        op_ldst(sh);
306
        opn = "sh";
307
        break;
308
    case OPC_LHU:
309
#if defined (MIPS_HAS_UNALIGNED_LS)
310
    case OPC_ULHU:
311
#endif
312
        op_ldst(lhu);
313
        GEN_STORE_TN_REG(rt, T0);
314
        opn = "lhu";
315
        break;
316
    case OPC_LB:
317
        op_ldst(lb);
318
        GEN_STORE_TN_REG(rt, T0);
319
        opn = "lb";
320
        break;
321
    case OPC_SB:
322
        GEN_LOAD_REG_TN(T1, rt);
323
        op_ldst(sb);
324
        opn = "sb";
325
        break;
326
    case OPC_LBU:
327
        op_ldst(lbu);
328
        GEN_STORE_TN_REG(rt, T0);
329
        opn = "lbu";
330
        break;
331
    case OPC_LWL:
332
        GEN_LOAD_REG_TN(T1, rt);
333
        op_ldst(lwl);
334
        GEN_STORE_TN_REG(rt, T0);
335
        opn = "lwl";
336
        break;
337
    case OPC_SWL:
338
        GEN_LOAD_REG_TN(T1, rt);
339
        op_ldst(swl);
340
        opn = "swr";
341
        break;
342
    case OPC_LWR:
343
        GEN_LOAD_REG_TN(T1, rt);
344
        op_ldst(lwr);
345
        GEN_STORE_TN_REG(rt, T0);
346
        opn = "lwr";
347
        break;
348
    case OPC_SWR:
349
        GEN_LOAD_REG_TN(T1, rt);
350
        op_ldst(swr);
351
        opn = "swr";
352
        break;
353
    case OPC_LL:
354
        op_ldst(ll);
355
        GEN_STORE_TN_REG(rt, T0);
356
        opn = "ll";
357
        break;
358
    case OPC_SC:
359
        GEN_LOAD_REG_TN(T1, rt);
360
        op_ldst(sc);
361
        GEN_STORE_TN_REG(rt, T0);
362
        opn = "sc";
363
        break;
364
    default:
365
        MIPS_INVAL("load/store");
366
        generate_exception(ctx, EXCP_RI);
367
        return;
368
    }
369
    MIPS_DEBUG("%s %s, %d(%s)", opn, regnames[rt], offset, regnames[base]);
370
}
371

    
372
/* Arithmetic with immediate operand */
373
static void gen_arith_imm (DisasContext *ctx, uint16_t opc, int rt,
374
                           int rs, int16_t imm)
375
{
376
    uint32_t uimm;
377
    const unsigned char *opn = "unk";
378

    
379
    if (rt == 0 && opc != OPC_ADDI) {
380
        /* if no destination, treat it as a NOP 
381
         * For addi, we must generate the overflow exception when needed.
382
         */
383
        MIPS_DEBUG("NOP");
384
        return;
385
    }
386
    if (opc == OPC_ADDI || opc == OPC_ADDIU ||
387
        opc == OPC_SLTI || opc == OPC_SLTIU)
388
        uimm = (int32_t)imm; /* Sign extent to 32 bits */
389
    else
390
        uimm = (uint16_t)imm;
391
    if (opc != OPC_LUI) {
392
        GEN_LOAD_REG_TN(T0, rs);
393
        GEN_LOAD_IMM_TN(T1, uimm);
394
    } else {
395
        uimm = uimm << 16;
396
        GEN_LOAD_IMM_TN(T0, uimm);
397
    }
398
    switch (opc) {
399
    case OPC_ADDI:
400
        save_cpu_state(ctx, 1);
401
        gen_op_addo();
402
        opn = "addi";
403
        break;
404
    case OPC_ADDIU:
405
        gen_op_add();
406
        opn = "addiu";
407
        break;
408
    case OPC_SLTI:
409
        gen_op_lt();
410
        opn = "slti";
411
        break;
412
    case OPC_SLTIU:
413
        gen_op_ltu();
414
        opn = "sltiu";
415
        break;
416
    case OPC_ANDI:
417
        gen_op_and();
418
        opn = "andi";
419
        break;
420
    case OPC_ORI:
421
        gen_op_or();
422
        opn = "ori";
423
        break;
424
    case OPC_XORI:
425
        gen_op_xor();
426
        opn = "xori";
427
        break;
428
    case OPC_LUI:
429
        opn = "lui";
430
        break;
431
    case OPC_SLL:
432
        gen_op_sll();
433
        opn = "sll";
434
        break;
435
    case OPC_SRA:
436
        gen_op_sra();
437
        opn = "sra";
438
        break;
439
    case OPC_SRL:
440
        gen_op_srl();
441
        opn = "srl";
442
        break;
443
    default:
444
        MIPS_INVAL("imm arith");
445
        generate_exception(ctx, EXCP_RI);
446
        return;
447
    }
448
    GEN_STORE_TN_REG(rt, T0);
449
    MIPS_DEBUG("%s %s, %s, %x", opn, regnames[rt], regnames[rs], uimm);
450
}
451

    
452
/* Arithmetic */
453
static void gen_arith (DisasContext *ctx, uint16_t opc,
454
                       int rd, int rs, int rt)
455
{
456
    const unsigned char *opn = "unk";
457

    
458
    if (rd == 0 && opc != OPC_ADD && opc != OPC_SUB) {
459
        /* if no destination, treat it as a NOP 
460
         * For add & sub, we must generate the overflow exception when needed.
461
         */
462
        MIPS_DEBUG("NOP");
463
        return;
464
    }
465
    GEN_LOAD_REG_TN(T0, rs);
466
    GEN_LOAD_REG_TN(T1, rt);
467
    switch (opc) {
468
    case OPC_ADD:
469
        save_cpu_state(ctx, 1);
470
        gen_op_addo();
471
        opn = "add";
472
        break;
473
    case OPC_ADDU:
474
        gen_op_add();
475
        opn = "addu";
476
        break;
477
    case OPC_SUB:
478
        save_cpu_state(ctx, 1);
479
        gen_op_subo();
480
        opn = "sub";
481
        break;
482
    case OPC_SUBU:
483
        gen_op_sub();
484
        opn = "subu";
485
        break;
486
    case OPC_SLT:
487
        gen_op_lt();
488
        opn = "slt";
489
        break;
490
    case OPC_SLTU:
491
        gen_op_ltu();
492
        opn = "sltu";
493
        break;
494
    case OPC_AND:
495
        gen_op_and();
496
        opn = "and";
497
        break;
498
    case OPC_NOR:
499
        gen_op_nor();
500
        opn = "nor";
501
        break;
502
    case OPC_OR:
503
        gen_op_or();
504
        opn = "or";
505
        break;
506
    case OPC_XOR:
507
        gen_op_xor();
508
        opn = "xor";
509
        break;
510
    case OPC_MUL:
511
        gen_op_mul();
512
        opn = "mul";
513
        break;
514
    case OPC_MOVN:
515
        gen_op_movn(rd);
516
        opn = "movn";
517
        goto print;
518
    case OPC_MOVZ:
519
        gen_op_movz(rd);
520
        opn = "movz";
521
        goto print;
522
    case OPC_SLLV:
523
        gen_op_sllv();
524
        opn = "sllv";
525
        break;
526
    case OPC_SRAV:
527
        gen_op_srav();
528
        opn = "srav";
529
        break;
530
    case OPC_SRLV:
531
        gen_op_srlv();
532
        opn = "srlv";
533
        break;
534
    default:
535
        MIPS_INVAL("arith");
536
        generate_exception(ctx, EXCP_RI);
537
        return;
538
    }
539
    GEN_STORE_TN_REG(rd, T0);
540
 print:
541
    MIPS_DEBUG("%s %s, %s, %s", opn, regnames[rd], regnames[rs], regnames[rt]);
542
}
543

    
544
/* Arithmetic on HI/LO registers */
545
static void gen_HILO (DisasContext *ctx, uint16_t opc, int reg)
546
{
547
    const unsigned char *opn = "unk";
548

    
549
    if (reg == 0 && (opc == OPC_MFHI || opc == OPC_MFLO)) {
550
        /* Treat as a NOP */
551
        MIPS_DEBUG("NOP");
552
        return;
553
    }
554
    switch (opc) {
555
    case OPC_MFHI:
556
        gen_op_load_HI();
557
        GEN_STORE_TN_REG(reg, T0);
558
        opn = "mfhi";
559
        break;
560
    case OPC_MFLO:
561
        gen_op_load_LO();
562
        GEN_STORE_TN_REG(reg, T0);
563
        opn = "mflo";
564
        break;
565
    case OPC_MTHI:
566
        GEN_LOAD_REG_TN(T0, reg);
567
        gen_op_store_HI();
568
        opn = "mthi";
569
        break;
570
    case OPC_MTLO:
571
        GEN_LOAD_REG_TN(T0, reg);
572
        gen_op_store_LO();
573
        opn = "mtlo";
574
        break;
575
    default:
576
        MIPS_INVAL("HILO");
577
        generate_exception(ctx, EXCP_RI);
578
        return;
579
    }
580
    MIPS_DEBUG("%s %s", opn, regnames[reg]);
581
}
582

    
583
static void gen_muldiv (DisasContext *ctx, uint16_t opc,
584
                        int rs, int rt)
585
{
586
    const unsigned char *opn = "unk";
587

    
588
    GEN_LOAD_REG_TN(T0, rs);
589
    GEN_LOAD_REG_TN(T1, rt);
590
    switch (opc) {
591
    case OPC_DIV:
592
        gen_op_div();
593
        opn = "div";
594
        break;
595
    case OPC_DIVU:
596
        gen_op_divu();
597
        opn = "divu";
598
        break;
599
    case OPC_MULT:
600
        gen_op_mult();
601
        opn = "mult";
602
        break;
603
    case OPC_MULTU:
604
        gen_op_multu();
605
        opn = "multu";
606
        break;
607
    case OPC_MADD:
608
        gen_op_madd();
609
        opn = "madd";
610
        break;
611
    case OPC_MADDU:
612
        gen_op_maddu();
613
        opn = "maddu";
614
        break;
615
    case OPC_MSUB:
616
        gen_op_msub();
617
        opn = "msub";
618
        break;
619
    case OPC_MSUBU:
620
        gen_op_msubu();
621
        opn = "msubu";
622
        break;
623
    default:
624
        MIPS_INVAL("mul/div");
625
        generate_exception(ctx, EXCP_RI);
626
        return;
627
    }
628
    MIPS_DEBUG("%s %s %s", opn, regnames[rs], regnames[rt]);
629
}
630

    
631
static void gen_cl (DisasContext *ctx, uint16_t opc,
632
                    int rd, int rs)
633
{
634
    const unsigned char *opn = "unk";
635
    if (rd == 0) {
636
        /* Treat as a NOP */
637
        MIPS_DEBUG("NOP");
638
        return;
639
    }
640
    GEN_LOAD_REG_TN(T0, rs);
641
    switch (opc) {
642
    case OPC_CLO:
643
        /* CLO */
644
        gen_op_clo();
645
        opn = "clo";
646
        break;
647
    case OPC_CLZ:
648
        /* CLZ */
649
        gen_op_clz();
650
        opn = "clz";
651
        break;
652
    default:
653
        MIPS_INVAL("CLx");
654
        generate_exception(ctx, EXCP_RI);
655
        return;
656
    }
657
    gen_op_store_T0_gpr(rd);
658
    MIPS_DEBUG("%s %s, %s", opn, regnames[rd], regnames[rs]);
659
}
660

    
661
/* Traps */
662
static void gen_trap (DisasContext *ctx, uint16_t opc,
663
                      int rs, int rt, int16_t imm)
664
{
665
    int cond;
666

    
667
    cond = 0;
668
    /* Load needed operands */
669
    switch (opc) {
670
    case OPC_TEQ:
671
    case OPC_TGE:
672
    case OPC_TGEU:
673
    case OPC_TLT:
674
    case OPC_TLTU:
675
    case OPC_TNE:
676
        /* Compare two registers */
677
        if (rs != rt) {
678
            GEN_LOAD_REG_TN(T0, rs);
679
            GEN_LOAD_REG_TN(T1, rt);
680
            cond = 1;
681
        }
682
    case OPC_TEQI:
683
    case OPC_TGEI:
684
    case OPC_TGEIU:
685
    case OPC_TLTI:
686
    case OPC_TLTIU:
687
    case OPC_TNEI:
688
        /* Compare register to immediate */
689
        if (rs != 0 || imm != 0) {
690
            GEN_LOAD_REG_TN(T0, rs);
691
            GEN_LOAD_IMM_TN(T1, (int32_t)imm);
692
            cond = 1;
693
        }
694
        break;
695
    }
696
    if (cond == 0) {
697
        switch (opc) {
698
        case OPC_TEQ:   /* rs == rs */
699
        case OPC_TEQI:  /* r0 == 0  */
700
        case OPC_TGE:   /* rs >= rs */
701
        case OPC_TGEI:  /* r0 >= 0  */
702
        case OPC_TGEU:  /* rs >= rs unsigned */
703
        case OPC_TGEIU: /* r0 >= 0  unsigned */
704
            /* Always trap */
705
            gen_op_set_T0(1);
706
            break;
707
        case OPC_TLT:   /* rs < rs           */
708
        case OPC_TLTI:  /* r0 < 0            */
709
        case OPC_TLTU:  /* rs < rs unsigned  */
710
        case OPC_TLTIU: /* r0 < 0  unsigned  */
711
        case OPC_TNE:   /* rs != rs          */
712
        case OPC_TNEI:  /* r0 != 0           */
713
            /* Never trap: treat as NOP */
714
            return;
715
        default:
716
            MIPS_INVAL("TRAP");
717
            generate_exception(ctx, EXCP_RI);
718
            return;
719
        }
720
    } else {
721
        switch (opc) {
722
        case OPC_TEQ:
723
        case OPC_TEQI:
724
            gen_op_eq();
725
            break;
726
        case OPC_TGE:
727
        case OPC_TGEI:
728
            gen_op_ge();
729
            break;
730
        case OPC_TGEU:
731
        case OPC_TGEIU:
732
            gen_op_geu();
733
            break;
734
        case OPC_TLT:
735
        case OPC_TLTI:
736
            gen_op_lt();
737
            break;
738
        case OPC_TLTU:
739
        case OPC_TLTIU:
740
            gen_op_ltu();
741
            break;
742
        case OPC_TNE:
743
        case OPC_TNEI:
744
            gen_op_ne();
745
            break;
746
        default:
747
            MIPS_INVAL("TRAP");
748
            generate_exception(ctx, EXCP_RI);
749
            return;
750
        }
751
    }
752
    save_cpu_state(ctx, 1);
753
    gen_op_trap();
754
    ctx->bstate = BS_STOP;
755
}
756

    
757
/* Branches (before delay slot) */
758
static void gen_compute_branch (DisasContext *ctx, uint16_t opc,
759
                                int rs, int rt, int32_t offset)
760
{
761
    target_ulong btarget;
762
    int blink, bcond;
763

    
764
    btarget = -1;
765
    blink = 0;
766
    bcond = 0;
767
    /* Load needed operands */
768
    switch (opc) {
769
    case OPC_BEQ:
770
    case OPC_BEQL:
771
    case OPC_BNE:
772
    case OPC_BNEL:
773
        /* Compare two registers */
774
        if (rs != rt) {
775
            GEN_LOAD_REG_TN(T0, rs);
776
            GEN_LOAD_REG_TN(T1, rt);
777
            bcond = 1;
778
        }
779
        btarget = ctx->pc + 4 + offset;
780
        break;
781
    case OPC_BGEZ:
782
    case OPC_BGEZAL:
783
    case OPC_BGEZALL:
784
    case OPC_BGEZL:
785
    case OPC_BGTZ:
786
    case OPC_BGTZL:
787
    case OPC_BLEZ:
788
    case OPC_BLEZL:
789
    case OPC_BLTZ:
790
    case OPC_BLTZAL:
791
    case OPC_BLTZALL:
792
    case OPC_BLTZL:
793
        /* Compare to zero */
794
        if (rs != 0) {
795
            gen_op_load_gpr_T0(rs);
796
            bcond = 1;
797
        }
798
        btarget = ctx->pc + 4 + offset;
799
        break;
800
    case OPC_J:
801
    case OPC_JAL:
802
        /* Jump to immediate */
803
        btarget = ((ctx->pc + 4) & 0xF0000000) | offset;
804
        break;
805
    case OPC_JR:
806
    case OPC_JALR:
807
        /* Jump to register */
808
        if (offset != 0) {
809
            /* Only hint = 0 is valid */
810
            generate_exception(ctx, EXCP_RI);
811
            return;
812
        }
813
        GEN_LOAD_REG_TN(T2, rs);
814
        break;
815
    default:
816
        MIPS_INVAL("branch/jump");
817
        generate_exception(ctx, EXCP_RI);
818
        return;
819
    }
820
    if (bcond == 0) {
821
        /* No condition to be computed */
822
        switch (opc) {
823
        case OPC_BEQ:     /* rx == rx        */
824
        case OPC_BEQL:    /* rx == rx likely */
825
        case OPC_BGEZ:    /* 0 >= 0          */
826
        case OPC_BGEZL:   /* 0 >= 0 likely   */
827
        case OPC_BLEZ:    /* 0 <= 0          */
828
        case OPC_BLEZL:   /* 0 <= 0 likely   */
829
            /* Always take */
830
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
831
            MIPS_DEBUG("balways");
832
            break;
833
        case OPC_BGEZAL:  /* 0 >= 0          */
834
        case OPC_BGEZALL: /* 0 >= 0 likely   */
835
            /* Always take and link */
836
            blink = 31;
837
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
838
            MIPS_DEBUG("balways and link");
839
            break;
840
        case OPC_BNE:     /* rx != rx        */
841
        case OPC_BGTZ:    /* 0 > 0           */
842
        case OPC_BLTZ:    /* 0 < 0           */
843
        case OPC_BLTZAL:  /* 0 < 0           */
844
            /* Treated as NOP */
845
            MIPS_DEBUG("bnever (NOP)");
846
            return;
847
        case OPC_BNEL:    /* rx != rx likely */
848
        case OPC_BGTZL:   /* 0 > 0 likely */
849
        case OPC_BLTZALL: /* 0 < 0 likely */
850
        case OPC_BLTZL:   /* 0 < 0 likely */
851
            /* Skip the instruction in the delay slot */
852
            MIPS_DEBUG("bnever and skip");
853
            gen_op_branch((long)ctx->tb, ctx->pc + 4);
854
            return;
855
        case OPC_J:
856
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
857
            MIPS_DEBUG("j %08x", btarget);
858
            break;
859
        case OPC_JAL:
860
            blink = 31;
861
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_B;
862
            MIPS_DEBUG("jal %08x", btarget);
863
            break;
864
        case OPC_JR:
865
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
866
            MIPS_DEBUG("jr %s", regnames[rs]);
867
            break;
868
        case OPC_JALR:
869
            blink = rt;
870
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BR;
871
            MIPS_DEBUG("jalr %s, %s", regnames[rt], regnames[rs]);
872
            break;
873
        default:
874
            MIPS_INVAL("branch/jump");
875
            generate_exception(ctx, EXCP_RI);
876
            return;
877
        }
878
    } else {
879
        switch (opc) {
880
        case OPC_BEQ:
881
            gen_op_eq();
882
            MIPS_DEBUG("beq %s, %s, %08x",
883
                       regnames[rs], regnames[rt], btarget);
884
            goto not_likely;
885
        case OPC_BEQL:
886
            gen_op_eq();
887
            MIPS_DEBUG("beql %s, %s, %08x",
888
                       regnames[rs], regnames[rt], btarget);
889
            goto likely;
890
        case OPC_BNE:
891
            gen_op_ne();
892
            MIPS_DEBUG("bne %s, %s, %08x",
893
                       regnames[rs], regnames[rt], btarget);
894
            goto not_likely;
895
        case OPC_BNEL:
896
            gen_op_ne();
897
            MIPS_DEBUG("bnel %s, %s, %08x",
898
                       regnames[rs], regnames[rt], btarget);
899
            goto likely;
900
        case OPC_BGEZ:
901
            gen_op_gez();
902
            MIPS_DEBUG("bgez %s, %08x", regnames[rs], btarget);
903
            goto not_likely;
904
        case OPC_BGEZL:
905
            gen_op_gez();
906
            MIPS_DEBUG("bgezl %s, %08x", regnames[rs], btarget);
907
            goto likely;
908
        case OPC_BGEZAL:
909
            gen_op_gez();
910
            MIPS_DEBUG("bgezal %s, %08x", regnames[rs], btarget);
911
            blink = 31;
912
            goto not_likely;
913
        case OPC_BGEZALL:
914
            gen_op_gez();
915
            blink = 31;
916
            MIPS_DEBUG("bgezall %s, %08x", regnames[rs], btarget);
917
            goto likely;
918
        case OPC_BGTZ:
919
            gen_op_gtz();
920
            MIPS_DEBUG("bgtz %s, %08x", regnames[rs], btarget);
921
            goto not_likely;
922
        case OPC_BGTZL:
923
            gen_op_gtz();
924
            MIPS_DEBUG("bgtzl %s, %08x", regnames[rs], btarget);
925
            goto likely;
926
        case OPC_BLEZ:
927
            gen_op_lez();
928
            MIPS_DEBUG("blez %s, %08x", regnames[rs], btarget);
929
            goto not_likely;
930
        case OPC_BLEZL:
931
            gen_op_lez();
932
            MIPS_DEBUG("blezl %s, %08x", regnames[rs], btarget);
933
            goto likely;
934
        case OPC_BLTZ:
935
            gen_op_ltz();
936
            MIPS_DEBUG("bltz %s, %08x", regnames[rs], btarget);
937
            goto not_likely;
938
        case OPC_BLTZL:
939
            gen_op_ltz();
940
            MIPS_DEBUG("bltzl %s, %08x", regnames[rs], btarget);
941
            goto likely;
942
        case OPC_BLTZAL:
943
            gen_op_ltz();
944
            blink = 31;
945
            MIPS_DEBUG("bltzal %s, %08x", regnames[rs], btarget);
946
        not_likely:
947
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BC;
948
            break;
949
        case OPC_BLTZALL:
950
            gen_op_ltz();
951
            blink = 31;
952
            MIPS_DEBUG("bltzall %s, %08x", regnames[rs], btarget);
953
        likely:
954
            ctx->hflags |= MIPS_HFLAG_DS | MIPS_HFLAG_BL;
955
            break;
956
        }
957
        gen_op_set_bcond();
958
    }
959
    MIPS_DEBUG("enter ds: link %d cond %02x target %08x",
960
               blink, ctx->hflags, btarget);
961
    ctx->btarget = btarget;
962
    if (blink > 0) {
963
        gen_op_set_T0(ctx->pc + 8);
964
        gen_op_store_T0_gpr(blink);
965
    }
966
    return;
967
}
968

    
969
/* CP0 (MMU and control) */
970
static void gen_cp0 (DisasContext *ctx, uint16_t opc, int rt, int rd)
971
{
972
    const unsigned char *opn = "unk";
973

    
974
    if (!(ctx->CP0_Status & (1 << CP0St_CU0))) {
975
        if (loglevel & CPU_LOG_TB_IN_ASM) {
976
            fprintf(logfile, "CP0 is not usable\n");
977
        }
978
        gen_op_raise_exception_err(EXCP_CpU, 0);
979
        return;
980
    }
981
    switch (opc) {
982
    case OPC_MFC0:
983
        if (rt == 0) {
984
            /* Treat as NOP */
985
            return;
986
        }
987
        gen_op_mfc0(rd, ctx->opcode & 0x7);
988
        gen_op_store_T0_gpr(rt);
989
        opn = "mfc0";
990
        break;
991
    case OPC_MTC0:
992
        /* If we get an exception, we want to restart at next instruction */
993
        ctx->pc += 4;
994
        save_cpu_state(ctx, 1);
995
        ctx->pc -= 4;
996
        GEN_LOAD_REG_TN(T0, rt);
997
        gen_op_mtc0(rd, ctx->opcode & 0x7);
998
        /* Stop translation as we may have switched the execution mode */
999
        ctx->bstate = BS_STOP;
1000
        opn = "mtc0";
1001
        break;
1002
#if defined(MIPS_USES_R4K_TLB)
1003
    case OPC_TLBWI:
1004
        gen_op_tlbwi();
1005
        opn = "tlbwi";
1006
        break;
1007
    case OPC_TLBWR:
1008
        gen_op_tlbwr();
1009
        opn = "tlbwr";
1010
        break;
1011
    case OPC_TLBP:
1012
        gen_op_tlbp();
1013
        opn = "tlbp";
1014
        break;
1015
    case OPC_TLBR:
1016
        gen_op_tlbr();
1017
        opn = "tlbr";
1018
        break;
1019
#endif
1020
    case OPC_ERET:
1021
        opn = "eret";
1022
        save_cpu_state(ctx, 0);
1023
        gen_op_eret();
1024
        ctx->bstate = BS_EXCP;
1025
        break;
1026
    case OPC_DERET:
1027
        opn = "deret";
1028
        if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1029
            generate_exception(ctx, EXCP_RI);
1030
        } else {
1031
            save_cpu_state(ctx, 0);
1032
            gen_op_deret();
1033
            ctx->bstate = BS_EXCP;
1034
        }
1035
        break;
1036
    /* XXX: TODO: WAIT */
1037
    default:
1038
        if (loglevel & CPU_LOG_TB_IN_ASM) {
1039
            fprintf(logfile, "Invalid CP0 opcode: %08x %03x %03x %03x\n",
1040
                    ctx->opcode, ctx->opcode >> 26, ctx->opcode & 0x3F,
1041
                    ((ctx->opcode >> 16) & 0x1F));
1042
        }
1043
        generate_exception(ctx, EXCP_RI);
1044
        return;
1045
    }
1046
    MIPS_DEBUG("%s %s %d", opn, regnames[rt], rd);
1047
}
1048

    
1049
/* Coprocessor 1 (FPU) */
1050

    
1051
/* ISA extensions */
1052
/* MIPS16 extension to MIPS32 */
1053
/* SmartMIPS extension to MIPS32 */
1054

    
1055
#ifdef TARGET_MIPS64
1056
static void gen_arith64 (DisasContext *ctx, uint16_t opc)
1057
{
1058
    if (func == 0x02 && rd == 0) {
1059
        /* NOP */
1060
        return;
1061
    }
1062
    if (rs == 0 || rt == 0) {
1063
        gen_op_reset_T0();
1064
        gen_op_save64();
1065
    } else {
1066
        gen_op_load_gpr_T0(rs);
1067
        gen_op_load_gpr_T1(rt);
1068
        gen_op_save64();
1069
        if (func & 0x01)
1070
            gen_op_mul64u();
1071
        else
1072
            gen_op_mul64s();
1073
    }
1074
    if (func & 0x02)
1075
        gen_op_add64();
1076
    else
1077
        gen_op_sub64();
1078
}
1079

    
1080
/* Coprocessor 3 (FPU) */
1081

    
1082
/* MDMX extension to MIPS64 */
1083
/* MIPS-3D extension to MIPS64 */
1084

    
1085
#endif
1086

    
1087
static void decode_opc (DisasContext *ctx)
1088
{
1089
    int32_t offset;
1090
    int rs, rt, rd, sa;
1091
    uint16_t op, op1;
1092
    int16_t imm;
1093

    
1094
    if ((ctx->hflags & MIPS_HFLAG_DS) &&
1095
        (ctx->hflags & MIPS_HFLAG_BL)) {
1096
        /* Handle blikely not taken case */
1097
        MIPS_DEBUG("blikely condition (%08x)", ctx->pc + 4);
1098
        gen_op_blikely((long)ctx->tb, ctx->pc + 4,
1099
                       ctx->hflags & ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS));
1100
    }
1101
    op = ctx->opcode >> 26;
1102
    rs = ((ctx->opcode >> 21) & 0x1F);
1103
    rt = ((ctx->opcode >> 16) & 0x1F);
1104
    rd = ((ctx->opcode >> 11) & 0x1F);
1105
    sa = ((ctx->opcode >> 6) & 0x1F);
1106
    imm = (int16_t)ctx->opcode;
1107
    switch (op) {
1108
    case 0x00:          /* Special opcode */
1109
        op1 = ctx->opcode & 0x3F;
1110
        switch (op1) {
1111
        case 0x00:          /* Arithmetic with immediate */
1112
        case 0x02 ... 0x03:
1113
            gen_arith_imm(ctx, op1 | EXT_SPECIAL, rd, rt, sa);
1114
            break;
1115
        case 0x04:          /* Arithmetic */
1116
        case 0x06 ... 0x07:
1117
        case 0x0A ... 0x0B:
1118
        case 0x20 ... 0x27:
1119
        case 0x2A ... 0x2B:
1120
            gen_arith(ctx, op1 | EXT_SPECIAL, rd, rs, rt);
1121
            break;
1122
        case 0x18 ... 0x1B: /* MULT / DIV */
1123
            gen_muldiv(ctx, op1 | EXT_SPECIAL, rs, rt);
1124
            break;
1125
        case 0x08 ... 0x09: /* Jumps */
1126
            gen_compute_branch(ctx, op1 | EXT_SPECIAL, rs, rd, sa);
1127
            return;
1128
        case 0x30 ... 0x34: /* Traps */
1129
        case 0x36:
1130
            gen_trap(ctx, op1 | EXT_SPECIAL, rs, rt, -1);
1131
            break;
1132
        case 0x10:          /* Move from HI/LO */
1133
        case 0x12:
1134
            gen_HILO(ctx, op1 | EXT_SPECIAL, rd);
1135
            break;
1136
        case 0x11:
1137
        case 0x13:          /* Move to HI/LO */
1138
            gen_HILO(ctx, op1 | EXT_SPECIAL, rs);
1139
            break;
1140
        case 0x0C:          /* SYSCALL */
1141
            generate_exception(ctx, EXCP_SYSCALL);
1142
            break;
1143
        case 0x0D:          /* BREAK */
1144
            generate_exception(ctx, EXCP_BREAK);
1145
            break;
1146
        case 0x0F:          /* SYNC */
1147
            /* Treat as a noop */
1148
            break;
1149
        case 0x05:          /* Pmon entry point */
1150
            gen_op_pmon((ctx->opcode >> 6) & 0x1F);
1151
            break;
1152
#if defined (MIPS_HAS_MOVCI)
1153
        case 0x01:          /* MOVCI */
1154
#endif
1155
#if defined (TARGET_MIPS64)
1156
        case 0x14: /* MIPS64 specific opcodes */
1157
        case 0x16:
1158
        case 0x17:
1159
        case 0x1C ... 0x1F:
1160
        case 0x2C ... 0x2F:
1161
        case 0x37:
1162
        case 0x39 ... 0x3B:
1163
        case 0x3E ... 0x3F:
1164
#endif
1165
        default:            /* Invalid */
1166
            MIPS_INVAL("special");
1167
            generate_exception(ctx, EXCP_RI);
1168
            break;
1169
        }
1170
        break;
1171
    case 0x1C:          /* Special2 opcode */
1172
        op1 = ctx->opcode & 0x3F;
1173
        switch (op1) {
1174
#if defined (MIPS_USES_R4K_EXT)
1175
        /* Those instructions are not part of MIPS32 core */
1176
        case 0x00 ... 0x01: /* Multiply and add/sub */
1177
        case 0x04 ... 0x05:
1178
            gen_muldiv(ctx, op1 | EXT_SPECIAL2, rs, rt);
1179
            break;
1180
        case 0x02:          /* MUL */
1181
            gen_arith(ctx, op1 | EXT_SPECIAL2, rd, rs, rt);
1182
            break;
1183
        case 0x20 ... 0x21: /* CLO / CLZ */
1184
            gen_cl(ctx, op1 | EXT_SPECIAL2, rd, rs);
1185
            break;
1186
#endif
1187
        case 0x3F:          /* SDBBP */
1188
            /* XXX: not clear which exception should be raised
1189
             *      when in debug mode...
1190
             */
1191
            if (!(ctx->hflags & MIPS_HFLAG_DM)) {
1192
                generate_exception(ctx, EXCP_DBp);
1193
            } else {
1194
                generate_exception(ctx, EXCP_DBp);
1195
            }
1196
            /* Treat as a noop */
1197
            break;
1198
        default:            /* Invalid */
1199
            MIPS_INVAL("special2");
1200
            generate_exception(ctx, EXCP_RI);
1201
            break;
1202
        }
1203
        break;
1204
    case 0x01:          /* B REGIMM opcode */
1205
        op1 = ((ctx->opcode >> 16) & 0x1F);
1206
        switch (op1) {
1207
        case 0x00 ... 0x03: /* REGIMM branches */
1208
        case 0x10 ... 0x13:
1209
            gen_compute_branch(ctx, op1 | EXT_REGIMM, rs, -1, imm << 2);
1210
            return;
1211
        case 0x08 ... 0x0C: /* Traps */
1212
        case 0x0E:
1213
            gen_trap(ctx, op1 | EXT_REGIMM, rs, -1, imm);
1214
            break;
1215
        default:            /* Invalid */
1216
            MIPS_INVAL("REGIMM");
1217
            generate_exception(ctx, EXCP_RI);
1218
            break;
1219
        }
1220
        break;
1221
    case 0x10:          /* CP0 opcode */
1222
        op1 = ((ctx->opcode >> 21) & 0x1F);
1223
        switch (op1) {
1224
        case 0x00:
1225
        case 0x04:
1226
            gen_cp0(ctx, op1 | EXT_CP0, rt, rd);
1227
            break;
1228
        default:
1229
            gen_cp0(ctx, (ctx->opcode & 0x1F) | EXT_CP0, rt, rd);
1230
            break;
1231
        }
1232
        break;
1233
    case 0x08 ... 0x0F: /* Arithmetic with immediate opcode */
1234
        gen_arith_imm(ctx, op, rt, rs, imm);
1235
        break;
1236
    case 0x02 ... 0x03: /* Jump */
1237
        offset = (int32_t)(ctx->opcode & 0x03FFFFFF) << 2;
1238
        gen_compute_branch(ctx, op, rs, rt, offset);
1239
        return;
1240
    case 0x04 ... 0x07: /* Branch */
1241
    case 0x14 ... 0x17:
1242
        gen_compute_branch(ctx, op, rs, rt, imm << 2);
1243
        return;
1244
    case 0x20 ... 0x26: /* Load and stores */
1245
    case 0x28 ... 0x2E:
1246
    case 0x30:
1247
    case 0x38:
1248
        gen_ldst(ctx, op, rt, rs, imm);
1249
        break;
1250
    case 0x2F:          /* Cache operation */
1251
        /* Treat as a noop */
1252
        break;
1253
    case 0x33:          /* Prefetch */
1254
        /* Treat as a noop */
1255
        break;
1256
    case 0x3F: /* HACK */
1257
        break;
1258
#if defined(MIPS_USES_FPU)
1259
    case 0x31 ... 0x32: /* Floating point load/store */
1260
    case 0x35 ... 0x36:
1261
    case 0x3A ... 0x3B:
1262
    case 0x3D ... 0x3E:
1263
        /* Not implemented */
1264
        /* XXX: not correct */
1265
#endif
1266
    case 0x11:          /* CP1 opcode */
1267
        /* Not implemented */
1268
        /* XXX: not correct */
1269
    case 0x12:          /* CP2 opcode */
1270
        /* Not implemented */
1271
        /* XXX: not correct */
1272
    case 0x13:          /* CP3 opcode */
1273
        /* Not implemented */
1274
        /* XXX: not correct */
1275
#if defined (TARGET_MIPS64)
1276
    case 0x18 ... 0x1B:
1277
    case 0x27:
1278
    case 0x34:
1279
    case 0x37:
1280
        /* MIPS64 opcodes */
1281
#endif
1282
#if defined (MIPS_HAS_JALX)
1283
    case 0x1D:
1284
        /* JALX: not implemented */
1285
#endif
1286
    case 0x1E:
1287
        /* ASE specific */
1288
#if defined (MIPS_HAS_LSC)
1289
    case 0x31: /* LWC1 */
1290
    case 0x32: /* LWC2 */
1291
    case 0x35: /* SDC1 */
1292
    case 0x36: /* SDC2 */
1293
#endif
1294
    default:            /* Invalid */
1295
        MIPS_INVAL("");
1296
        generate_exception(ctx, EXCP_RI);
1297
        break;
1298
    }
1299
    if (ctx->hflags & MIPS_HFLAG_DS) {
1300
        int hflags = ctx->hflags;
1301
        /* Branches completion */
1302
        ctx->hflags &= ~(MIPS_HFLAG_BMASK | MIPS_HFLAG_DS);
1303
        ctx->bstate = BS_BRANCH;
1304
        save_cpu_state(ctx, 0);
1305
        switch (hflags & MIPS_HFLAG_BMASK) {
1306
        case MIPS_HFLAG_B:
1307
            /* unconditional branch */
1308
            MIPS_DEBUG("unconditional branch");
1309
            gen_op_branch((long)ctx->tb, ctx->btarget);
1310
            break;
1311
        case MIPS_HFLAG_BL:
1312
            /* blikely taken case */
1313
            MIPS_DEBUG("blikely branch taken");
1314
            gen_op_branch((long)ctx->tb, ctx->btarget);
1315
            break;
1316
        case MIPS_HFLAG_BC:
1317
            /* Conditional branch */
1318
            MIPS_DEBUG("conditional branch");
1319
            gen_op_bcond((long)ctx->tb, ctx->btarget, ctx->pc + 4);
1320
            break;
1321
        case MIPS_HFLAG_BR:
1322
            /* unconditional branch to register */
1323
            MIPS_DEBUG("branch to register");
1324
            gen_op_breg();
1325
            break;
1326
        default:
1327
            MIPS_DEBUG("unknown branch");
1328
            break;
1329
        }
1330
    }
1331
}
1332

    
1333
int gen_intermediate_code_internal (CPUState *env, TranslationBlock *tb,
1334
                                    int search_pc)
1335
{
1336
    DisasContext ctx, *ctxp = &ctx;
1337
    target_ulong pc_start;
1338
    uint16_t *gen_opc_end;
1339
    int j, lj = -1;
1340

    
1341
    pc_start = tb->pc;
1342
    gen_opc_ptr = gen_opc_buf;
1343
    gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
1344
    gen_opparam_ptr = gen_opparam_buf;
1345
    ctx.pc = pc_start;
1346
    ctx.tb = tb;
1347
    ctx.bstate = BS_NONE;
1348
    /* Restore delay slot state */
1349
    ctx.hflags = env->hflags;
1350
    ctx.saved_hflags = ctx.hflags;
1351
    if (ctx.hflags & MIPS_HFLAG_BR) {
1352
        gen_op_restore_breg_target();
1353
    } else if (ctx.hflags & MIPS_HFLAG_B) {
1354
        ctx.btarget = env->btarget;
1355
    } else if (ctx.hflags & MIPS_HFLAG_BMASK) {
1356
        /* If we are in the delay slot of a conditional branch,
1357
         * restore the branch condition from env->bcond to T2
1358
         */
1359
        ctx.btarget = env->btarget;
1360
        gen_op_restore_bcond();
1361
    }
1362
#if defined(CONFIG_USER_ONLY)
1363
    ctx.mem_idx = 0;
1364
#else
1365
    ctx.mem_idx = (ctx.hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM ? 0 : 1;
1366
#endif
1367
    ctx.CP0_Status = env->CP0_Status;
1368
#ifdef DEBUG_DISAS
1369
    if (loglevel & CPU_LOG_TB_CPU) {
1370
        fprintf(logfile, "------------------------------------------------\n");
1371
        cpu_dump_state(env, logfile, fprintf, 0);
1372
    }
1373
#endif
1374
#if defined MIPS_DEBUG_DISAS
1375
    if (loglevel & CPU_LOG_TB_IN_ASM)
1376
        fprintf(logfile, "\ntb %p super %d cond %04x %04x\n",
1377
                tb, ctx.mem_idx, ctx.hflags, env->hflags);
1378
#endif
1379
    while (ctx.bstate == BS_NONE && gen_opc_ptr < gen_opc_end) {
1380
        if (search_pc) {
1381
            j = gen_opc_ptr - gen_opc_buf;
1382
            save_cpu_state(ctxp, 1);
1383
            if (lj < j) {
1384
                lj++;
1385
                while (lj < j)
1386
                    gen_opc_instr_start[lj++] = 0;
1387
                gen_opc_pc[lj] = ctx.pc;
1388
                gen_opc_instr_start[lj] = 1;
1389
            }
1390
        }
1391
        ctx.opcode = ldl_code(ctx.pc);
1392
        decode_opc(&ctx);
1393
        ctx.pc += 4;
1394
        if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0)
1395
            break;
1396
#if defined (MIPS_SINGLE_STEP)
1397
        break;
1398
#endif
1399
    }
1400
    if (ctx.bstate != BS_BRANCH && ctx.bstate != BS_EXCP) {
1401
        save_cpu_state(ctxp, 0);
1402
        gen_op_branch((long)ctx.tb, ctx.pc);
1403
    }
1404
    gen_op_reset_T0();
1405
    /* Generate the return instruction */
1406
    gen_op_exit_tb();
1407
    *gen_opc_ptr = INDEX_op_end;
1408
    if (search_pc) {
1409
        j = gen_opc_ptr - gen_opc_buf;
1410
        lj++;
1411
        while (lj <= j)
1412
            gen_opc_instr_start[lj++] = 0;
1413
        tb->size = 0;
1414
    } else {
1415
        tb->size = ctx.pc - pc_start;
1416
    }
1417
#ifdef DEBUG_DISAS
1418
#if defined MIPS_DEBUG_DISAS
1419
    if (loglevel & CPU_LOG_TB_IN_ASM)
1420
        fprintf(logfile, "\n");
1421
#endif
1422
    if (loglevel & CPU_LOG_TB_IN_ASM) {
1423
        fprintf(logfile, "IN: %s\n", lookup_symbol(pc_start));
1424
        target_disas(logfile, pc_start, ctx.pc - pc_start, 0);
1425
        fprintf(logfile, "\n");
1426
    }
1427
    if (loglevel & CPU_LOG_TB_OP) {
1428
        fprintf(logfile, "OP:\n");
1429
        dump_ops(gen_opc_buf, gen_opparam_buf);
1430
        fprintf(logfile, "\n");
1431
    }
1432
    if (loglevel & CPU_LOG_TB_CPU) {
1433
        fprintf(logfile, "---------------- %d %08x\n", ctx.bstate, ctx.hflags);
1434
    }
1435
#endif
1436
    
1437
    return 0;
1438
}
1439

    
1440
int gen_intermediate_code (CPUState *env, struct TranslationBlock *tb)
1441
{
1442
    return gen_intermediate_code_internal(env, tb, 0);
1443
}
1444

    
1445
int gen_intermediate_code_pc (CPUState *env, struct TranslationBlock *tb)
1446
{
1447
    return gen_intermediate_code_internal(env, tb, 1);
1448
}
1449

    
1450
void cpu_dump_state (CPUState *env, FILE *f, 
1451
                     int (*cpu_fprintf)(FILE *f, const char *fmt, ...),
1452
                     int flags)
1453
{
1454
    int i;
1455
    
1456
    cpu_fprintf(f, "pc=0x%08x HI=0x%08x LO=0x%08x ds %04x %08x %d\n",
1457
                env->PC, env->HI, env->LO, env->hflags, env->btarget, env->bcond);
1458
    for (i = 0; i < 32; i++) {
1459
        if ((i & 3) == 0)
1460
            cpu_fprintf(f, "GPR%02d:", i);
1461
        cpu_fprintf(f, " %s %08x", regnames[i], env->gpr[i]);
1462
        if ((i & 3) == 3)
1463
            cpu_fprintf(f, "\n");
1464
    }
1465
    cpu_fprintf(f, "CP0 Status  0x%08x Cause   0x%08x EPC    0x%08x\n",
1466
                env->CP0_Status, env->CP0_Cause, env->CP0_EPC);
1467
    cpu_fprintf(f, "    Config0 0x%08x Config1 0x%08x LLAddr 0x%08x\n",
1468
                env->CP0_Config0, env->CP0_Config1, env->CP0_LLAddr);
1469
}
1470

    
1471
CPUMIPSState *cpu_mips_init (void)
1472
{
1473
    CPUMIPSState *env;
1474

    
1475
    cpu_exec_init();
1476
    env = qemu_mallocz(sizeof(CPUMIPSState));
1477
    if (!env)
1478
        return NULL;
1479
    tlb_flush(env, 1);
1480
    /* Minimal init */
1481
    env->PC = 0xBFC00000;
1482
#if defined (MIPS_USES_R4K_TLB)
1483
    env->CP0_random = MIPS_TLB_NB - 1;
1484
#endif
1485
    env->CP0_Wired = 0;
1486
    env->CP0_Config0 = MIPS_CONFIG0;
1487
#if defined (MIPS_CONFIG1)
1488
        env->CP0_Config1 = MIPS_CONFIG1;
1489
#endif
1490
#if defined (MIPS_CONFIG2)
1491
        env->CP0_Config2 = MIPS_CONFIG2;
1492
#endif
1493
#if defined (MIPS_CONFIG3)
1494
        env->CP0_Config3 = MIPS_CONFIG3;
1495
#endif
1496
    env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
1497
    env->CP0_WatchLo = 0;
1498
    env->hflags = MIPS_HFLAG_ERL;
1499
    /* Count register increments in debug mode, EJTAG version 1 */
1500
    env->CP0_Debug = (1 << CP0DB_CNT) | (0x1 << CP0DB_VER);
1501
    env->CP0_PRid = MIPS_CPU;
1502
    env->exception_index = EXCP_NONE;
1503

    
1504
    cpu_single_env = env;
1505

    
1506
    return env;
1507
}