]> Git Repo - qemu.git/blob - target/openrisc/translate.c
target/openrisc: Check CPUCFG_OF32S for float insns
[qemu.git] / target / openrisc / translate.c
1 /*
2  * OpenRISC translation
3  *
4  * Copyright (c) 2011-2012 Jia Liu <[email protected]>
5  *                         Feng Gao <[email protected]>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "exec/exec-all.h"
24 #include "disas/disas.h"
25 #include "tcg-op.h"
26 #include "qemu/log.h"
27 #include "qemu/bitops.h"
28 #include "qemu/qemu-print.h"
29 #include "exec/cpu_ldst.h"
30 #include "exec/translator.h"
31
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 #include "exec/gen-icount.h"
35
36 #include "trace-tcg.h"
37 #include "exec/log.h"
38
39 /* is_jmp field values */
40 #define DISAS_EXIT    DISAS_TARGET_0  /* force exit to main loop */
41 #define DISAS_JUMP    DISAS_TARGET_1  /* exit via jmp_pc/jmp_pc_imm */
42
43 typedef struct DisasContext {
44     DisasContextBase base;
45     uint32_t mem_idx;
46     uint32_t tb_flags;
47     uint32_t delayed_branch;
48     uint32_t cpucfgr;
49
50     /* If not -1, jmp_pc contains this value and so is a direct jump.  */
51     target_ulong jmp_pc_imm;
52
53     /* The temporary corresponding to register 0 for this compilation.  */
54     TCGv R0;
55 } DisasContext;
56
57 static inline bool is_user(DisasContext *dc)
58 {
59 #ifdef CONFIG_USER_ONLY
60     return true;
61 #else
62     return !(dc->tb_flags & TB_FLAGS_SM);
63 #endif
64 }
65
66 /* Include the auto-generated decoder.  */
67 #include "decode.inc.c"
68
69 static TCGv cpu_sr;
70 static TCGv cpu_regs[32];
71 static TCGv cpu_pc;
72 static TCGv jmp_pc;            /* l.jr/l.jalr temp pc */
73 static TCGv cpu_ppc;
74 static TCGv cpu_sr_f;           /* bf/bnf, F flag taken */
75 static TCGv cpu_sr_cy;          /* carry (unsigned overflow) */
76 static TCGv cpu_sr_ov;          /* signed overflow */
77 static TCGv cpu_lock_addr;
78 static TCGv cpu_lock_value;
79 static TCGv_i32 fpcsr;
80 static TCGv_i64 cpu_mac;        /* MACHI:MACLO */
81 static TCGv_i32 cpu_dflag;
82
83 void openrisc_translate_init(void)
84 {
85     static const char * const regnames[] = {
86         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
87         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
88         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
89         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
90     };
91     int i;
92
93     cpu_sr = tcg_global_mem_new(cpu_env,
94                                 offsetof(CPUOpenRISCState, sr), "sr");
95     cpu_dflag = tcg_global_mem_new_i32(cpu_env,
96                                        offsetof(CPUOpenRISCState, dflag),
97                                        "dflag");
98     cpu_pc = tcg_global_mem_new(cpu_env,
99                                 offsetof(CPUOpenRISCState, pc), "pc");
100     cpu_ppc = tcg_global_mem_new(cpu_env,
101                                  offsetof(CPUOpenRISCState, ppc), "ppc");
102     jmp_pc = tcg_global_mem_new(cpu_env,
103                                 offsetof(CPUOpenRISCState, jmp_pc), "jmp_pc");
104     cpu_sr_f = tcg_global_mem_new(cpu_env,
105                                   offsetof(CPUOpenRISCState, sr_f), "sr_f");
106     cpu_sr_cy = tcg_global_mem_new(cpu_env,
107                                    offsetof(CPUOpenRISCState, sr_cy), "sr_cy");
108     cpu_sr_ov = tcg_global_mem_new(cpu_env,
109                                    offsetof(CPUOpenRISCState, sr_ov), "sr_ov");
110     cpu_lock_addr = tcg_global_mem_new(cpu_env,
111                                        offsetof(CPUOpenRISCState, lock_addr),
112                                        "lock_addr");
113     cpu_lock_value = tcg_global_mem_new(cpu_env,
114                                         offsetof(CPUOpenRISCState, lock_value),
115                                         "lock_value");
116     fpcsr = tcg_global_mem_new_i32(cpu_env,
117                                    offsetof(CPUOpenRISCState, fpcsr),
118                                    "fpcsr");
119     cpu_mac = tcg_global_mem_new_i64(cpu_env,
120                                      offsetof(CPUOpenRISCState, mac),
121                                      "mac");
122     for (i = 0; i < 32; i++) {
123         cpu_regs[i] = tcg_global_mem_new(cpu_env,
124                                          offsetof(CPUOpenRISCState,
125                                                   shadow_gpr[0][i]),
126                                          regnames[i]);
127     }
128 }
129
130 static void gen_exception(DisasContext *dc, unsigned int excp)
131 {
132     TCGv_i32 tmp = tcg_const_i32(excp);
133     gen_helper_exception(cpu_env, tmp);
134     tcg_temp_free_i32(tmp);
135 }
136
137 static void gen_illegal_exception(DisasContext *dc)
138 {
139     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
140     gen_exception(dc, EXCP_ILLEGAL);
141     dc->base.is_jmp = DISAS_NORETURN;
142 }
143
144 static bool check_of32s(DisasContext *dc)
145 {
146     return dc->cpucfgr & CPUCFGR_OF32S;
147 }
148
149 static TCGv cpu_R(DisasContext *dc, int reg)
150 {
151     if (reg == 0) {
152         return dc->R0;
153     } else {
154         return cpu_regs[reg];
155     }
156 }
157
158 /*
159  * We're about to write to REG.  On the off-chance that the user is
160  * writing to R0, re-instate the architectural register.
161  */
162 static void check_r0_write(DisasContext *dc, int reg)
163 {
164     if (unlikely(reg == 0)) {
165         dc->R0 = cpu_regs[0];
166     }
167 }
168
169 static void gen_ove_cy(DisasContext *dc)
170 {
171     if (dc->tb_flags & SR_OVE) {
172         gen_helper_ove_cy(cpu_env);
173     }
174 }
175
176 static void gen_ove_ov(DisasContext *dc)
177 {
178     if (dc->tb_flags & SR_OVE) {
179         gen_helper_ove_ov(cpu_env);
180     }
181 }
182
183 static void gen_ove_cyov(DisasContext *dc)
184 {
185     if (dc->tb_flags & SR_OVE) {
186         gen_helper_ove_cyov(cpu_env);
187     }
188 }
189
190 static void gen_add(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
191 {
192     TCGv t0 = tcg_const_tl(0);
193     TCGv res = tcg_temp_new();
194
195     tcg_gen_add2_tl(res, cpu_sr_cy, srca, t0, srcb, t0);
196     tcg_gen_xor_tl(cpu_sr_ov, srca, srcb);
197     tcg_gen_xor_tl(t0, res, srcb);
198     tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov);
199     tcg_temp_free(t0);
200
201     tcg_gen_mov_tl(dest, res);
202     tcg_temp_free(res);
203
204     gen_ove_cyov(dc);
205 }
206
207 static void gen_addc(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
208 {
209     TCGv t0 = tcg_const_tl(0);
210     TCGv res = tcg_temp_new();
211
212     tcg_gen_add2_tl(res, cpu_sr_cy, srca, t0, cpu_sr_cy, t0);
213     tcg_gen_add2_tl(res, cpu_sr_cy, res, cpu_sr_cy, srcb, t0);
214     tcg_gen_xor_tl(cpu_sr_ov, srca, srcb);
215     tcg_gen_xor_tl(t0, res, srcb);
216     tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov);
217     tcg_temp_free(t0);
218
219     tcg_gen_mov_tl(dest, res);
220     tcg_temp_free(res);
221
222     gen_ove_cyov(dc);
223 }
224
225 static void gen_sub(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
226 {
227     TCGv res = tcg_temp_new();
228
229     tcg_gen_sub_tl(res, srca, srcb);
230     tcg_gen_xor_tl(cpu_sr_cy, srca, srcb);
231     tcg_gen_xor_tl(cpu_sr_ov, res, srcb);
232     tcg_gen_and_tl(cpu_sr_ov, cpu_sr_ov, cpu_sr_cy);
233     tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_cy, srca, srcb);
234
235     tcg_gen_mov_tl(dest, res);
236     tcg_temp_free(res);
237
238     gen_ove_cyov(dc);
239 }
240
241 static void gen_mul(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
242 {
243     TCGv t0 = tcg_temp_new();
244
245     tcg_gen_muls2_tl(dest, cpu_sr_ov, srca, srcb);
246     tcg_gen_sari_tl(t0, dest, TARGET_LONG_BITS - 1);
247     tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_ov, cpu_sr_ov, t0);
248     tcg_temp_free(t0);
249
250     tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
251     gen_ove_ov(dc);
252 }
253
254 static void gen_mulu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
255 {
256     tcg_gen_muls2_tl(dest, cpu_sr_cy, srca, srcb);
257     tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_cy, cpu_sr_cy, 0);
258
259     gen_ove_cy(dc);
260 }
261
262 static void gen_div(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
263 {
264     TCGv t0 = tcg_temp_new();
265
266     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_ov, srcb, 0);
267     /* The result of divide-by-zero is undefined.
268        Supress the host-side exception by dividing by 1.  */
269     tcg_gen_or_tl(t0, srcb, cpu_sr_ov);
270     tcg_gen_div_tl(dest, srca, t0);
271     tcg_temp_free(t0);
272
273     tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
274     gen_ove_ov(dc);
275 }
276
277 static void gen_divu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
278 {
279     TCGv t0 = tcg_temp_new();
280
281     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_cy, srcb, 0);
282     /* The result of divide-by-zero is undefined.
283        Supress the host-side exception by dividing by 1.  */
284     tcg_gen_or_tl(t0, srcb, cpu_sr_cy);
285     tcg_gen_divu_tl(dest, srca, t0);
286     tcg_temp_free(t0);
287
288     gen_ove_cy(dc);
289 }
290
291 static void gen_muld(DisasContext *dc, TCGv srca, TCGv srcb)
292 {
293     TCGv_i64 t1 = tcg_temp_new_i64();
294     TCGv_i64 t2 = tcg_temp_new_i64();
295
296     tcg_gen_ext_tl_i64(t1, srca);
297     tcg_gen_ext_tl_i64(t2, srcb);
298     if (TARGET_LONG_BITS == 32) {
299         tcg_gen_mul_i64(cpu_mac, t1, t2);
300         tcg_gen_movi_tl(cpu_sr_ov, 0);
301     } else {
302         TCGv_i64 high = tcg_temp_new_i64();
303
304         tcg_gen_muls2_i64(cpu_mac, high, t1, t2);
305         tcg_gen_sari_i64(t1, cpu_mac, 63);
306         tcg_gen_setcond_i64(TCG_COND_NE, t1, t1, high);
307         tcg_temp_free_i64(high);
308         tcg_gen_trunc_i64_tl(cpu_sr_ov, t1);
309         tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
310
311         gen_ove_ov(dc);
312     }
313     tcg_temp_free_i64(t1);
314     tcg_temp_free_i64(t2);
315 }
316
317 static void gen_muldu(DisasContext *dc, TCGv srca, TCGv srcb)
318 {
319     TCGv_i64 t1 = tcg_temp_new_i64();
320     TCGv_i64 t2 = tcg_temp_new_i64();
321
322     tcg_gen_extu_tl_i64(t1, srca);
323     tcg_gen_extu_tl_i64(t2, srcb);
324     if (TARGET_LONG_BITS == 32) {
325         tcg_gen_mul_i64(cpu_mac, t1, t2);
326         tcg_gen_movi_tl(cpu_sr_cy, 0);
327     } else {
328         TCGv_i64 high = tcg_temp_new_i64();
329
330         tcg_gen_mulu2_i64(cpu_mac, high, t1, t2);
331         tcg_gen_setcondi_i64(TCG_COND_NE, high, high, 0);
332         tcg_gen_trunc_i64_tl(cpu_sr_cy, high);
333         tcg_temp_free_i64(high);
334
335         gen_ove_cy(dc);
336     }
337     tcg_temp_free_i64(t1);
338     tcg_temp_free_i64(t2);
339 }
340
341 static void gen_mac(DisasContext *dc, TCGv srca, TCGv srcb)
342 {
343     TCGv_i64 t1 = tcg_temp_new_i64();
344     TCGv_i64 t2 = tcg_temp_new_i64();
345
346     tcg_gen_ext_tl_i64(t1, srca);
347     tcg_gen_ext_tl_i64(t2, srcb);
348     tcg_gen_mul_i64(t1, t1, t2);
349
350     /* Note that overflow is only computed during addition stage.  */
351     tcg_gen_xor_i64(t2, cpu_mac, t1);
352     tcg_gen_add_i64(cpu_mac, cpu_mac, t1);
353     tcg_gen_xor_i64(t1, t1, cpu_mac);
354     tcg_gen_andc_i64(t1, t1, t2);
355     tcg_temp_free_i64(t2);
356
357 #if TARGET_LONG_BITS == 32
358     tcg_gen_extrh_i64_i32(cpu_sr_ov, t1);
359 #else
360     tcg_gen_mov_i64(cpu_sr_ov, t1);
361 #endif
362     tcg_temp_free_i64(t1);
363
364     gen_ove_ov(dc);
365 }
366
367 static void gen_macu(DisasContext *dc, TCGv srca, TCGv srcb)
368 {
369     TCGv_i64 t1 = tcg_temp_new_i64();
370     TCGv_i64 t2 = tcg_temp_new_i64();
371
372     tcg_gen_extu_tl_i64(t1, srca);
373     tcg_gen_extu_tl_i64(t2, srcb);
374     tcg_gen_mul_i64(t1, t1, t2);
375     tcg_temp_free_i64(t2);
376
377     /* Note that overflow is only computed during addition stage.  */
378     tcg_gen_add_i64(cpu_mac, cpu_mac, t1);
379     tcg_gen_setcond_i64(TCG_COND_LTU, t1, cpu_mac, t1);
380     tcg_gen_trunc_i64_tl(cpu_sr_cy, t1);
381     tcg_temp_free_i64(t1);
382
383     gen_ove_cy(dc);
384 }
385
386 static void gen_msb(DisasContext *dc, TCGv srca, TCGv srcb)
387 {
388     TCGv_i64 t1 = tcg_temp_new_i64();
389     TCGv_i64 t2 = tcg_temp_new_i64();
390
391     tcg_gen_ext_tl_i64(t1, srca);
392     tcg_gen_ext_tl_i64(t2, srcb);
393     tcg_gen_mul_i64(t1, t1, t2);
394
395     /* Note that overflow is only computed during subtraction stage.  */
396     tcg_gen_xor_i64(t2, cpu_mac, t1);
397     tcg_gen_sub_i64(cpu_mac, cpu_mac, t1);
398     tcg_gen_xor_i64(t1, t1, cpu_mac);
399     tcg_gen_and_i64(t1, t1, t2);
400     tcg_temp_free_i64(t2);
401
402 #if TARGET_LONG_BITS == 32
403     tcg_gen_extrh_i64_i32(cpu_sr_ov, t1);
404 #else
405     tcg_gen_mov_i64(cpu_sr_ov, t1);
406 #endif
407     tcg_temp_free_i64(t1);
408
409     gen_ove_ov(dc);
410 }
411
412 static void gen_msbu(DisasContext *dc, TCGv srca, TCGv srcb)
413 {
414     TCGv_i64 t1 = tcg_temp_new_i64();
415     TCGv_i64 t2 = tcg_temp_new_i64();
416
417     tcg_gen_extu_tl_i64(t1, srca);
418     tcg_gen_extu_tl_i64(t2, srcb);
419     tcg_gen_mul_i64(t1, t1, t2);
420
421     /* Note that overflow is only computed during subtraction stage.  */
422     tcg_gen_setcond_i64(TCG_COND_LTU, t2, cpu_mac, t1);
423     tcg_gen_sub_i64(cpu_mac, cpu_mac, t1);
424     tcg_gen_trunc_i64_tl(cpu_sr_cy, t2);
425     tcg_temp_free_i64(t2);
426     tcg_temp_free_i64(t1);
427
428     gen_ove_cy(dc);
429 }
430
431 static bool trans_l_add(DisasContext *dc, arg_dab *a)
432 {
433     check_r0_write(dc, a->d);
434     gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
435     return true;
436 }
437
438 static bool trans_l_addc(DisasContext *dc, arg_dab *a)
439 {
440     check_r0_write(dc, a->d);
441     gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
442     return true;
443 }
444
445 static bool trans_l_sub(DisasContext *dc, arg_dab *a)
446 {
447     check_r0_write(dc, a->d);
448     gen_sub(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
449     return true;
450 }
451
452 static bool trans_l_and(DisasContext *dc, arg_dab *a)
453 {
454     check_r0_write(dc, a->d);
455     tcg_gen_and_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
456     return true;
457 }
458
459 static bool trans_l_or(DisasContext *dc, arg_dab *a)
460 {
461     check_r0_write(dc, a->d);
462     tcg_gen_or_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
463     return true;
464 }
465
466 static bool trans_l_xor(DisasContext *dc, arg_dab *a)
467 {
468     check_r0_write(dc, a->d);
469     tcg_gen_xor_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
470     return true;
471 }
472
473 static bool trans_l_sll(DisasContext *dc, arg_dab *a)
474 {
475     check_r0_write(dc, a->d);
476     tcg_gen_shl_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
477     return true;
478 }
479
480 static bool trans_l_srl(DisasContext *dc, arg_dab *a)
481 {
482     check_r0_write(dc, a->d);
483     tcg_gen_shr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
484     return true;
485 }
486
487 static bool trans_l_sra(DisasContext *dc, arg_dab *a)
488 {
489     check_r0_write(dc, a->d);
490     tcg_gen_sar_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
491     return true;
492 }
493
494 static bool trans_l_ror(DisasContext *dc, arg_dab *a)
495 {
496     check_r0_write(dc, a->d);
497     tcg_gen_rotr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
498     return true;
499 }
500
501 static bool trans_l_exths(DisasContext *dc, arg_da *a)
502 {
503     check_r0_write(dc, a->d);
504     tcg_gen_ext16s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
505     return true;
506 }
507
508 static bool trans_l_extbs(DisasContext *dc, arg_da *a)
509 {
510     check_r0_write(dc, a->d);
511     tcg_gen_ext8s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
512     return true;
513 }
514
515 static bool trans_l_exthz(DisasContext *dc, arg_da *a)
516 {
517     check_r0_write(dc, a->d);
518     tcg_gen_ext16u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
519     return true;
520 }
521
522 static bool trans_l_extbz(DisasContext *dc, arg_da *a)
523 {
524     check_r0_write(dc, a->d);
525     tcg_gen_ext8u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
526     return true;
527 }
528
529 static bool trans_l_cmov(DisasContext *dc, arg_dab *a)
530 {
531     TCGv zero;
532
533     check_r0_write(dc, a->d);
534     zero = tcg_const_tl(0);
535     tcg_gen_movcond_tl(TCG_COND_NE, cpu_R(dc, a->d), cpu_sr_f, zero,
536                        cpu_R(dc, a->a), cpu_R(dc, a->b));
537     tcg_temp_free(zero);
538     return true;
539 }
540
541 static bool trans_l_ff1(DisasContext *dc, arg_da *a)
542 {
543     check_r0_write(dc, a->d);
544     tcg_gen_ctzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), -1);
545     tcg_gen_addi_tl(cpu_R(dc, a->d), cpu_R(dc, a->d), 1);
546     return true;
547 }
548
549 static bool trans_l_fl1(DisasContext *dc, arg_da *a)
550 {
551     check_r0_write(dc, a->d);
552     tcg_gen_clzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), TARGET_LONG_BITS);
553     tcg_gen_subfi_tl(cpu_R(dc, a->d), TARGET_LONG_BITS, cpu_R(dc, a->d));
554     return true;
555 }
556
557 static bool trans_l_mul(DisasContext *dc, arg_dab *a)
558 {
559     check_r0_write(dc, a->d);
560     gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
561     return true;
562 }
563
564 static bool trans_l_mulu(DisasContext *dc, arg_dab *a)
565 {
566     check_r0_write(dc, a->d);
567     gen_mulu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
568     return true;
569 }
570
571 static bool trans_l_div(DisasContext *dc, arg_dab *a)
572 {
573     check_r0_write(dc, a->d);
574     gen_div(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
575     return true;
576 }
577
578 static bool trans_l_divu(DisasContext *dc, arg_dab *a)
579 {
580     check_r0_write(dc, a->d);
581     gen_divu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
582     return true;
583 }
584
585 static bool trans_l_muld(DisasContext *dc, arg_ab *a)
586 {
587     gen_muld(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
588     return true;
589 }
590
591 static bool trans_l_muldu(DisasContext *dc, arg_ab *a)
592 {
593     gen_muldu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
594     return true;
595 }
596
597 static bool trans_l_j(DisasContext *dc, arg_l_j *a)
598 {
599     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
600
601     tcg_gen_movi_tl(jmp_pc, tmp_pc);
602     dc->jmp_pc_imm = tmp_pc;
603     dc->delayed_branch = 2;
604     return true;
605 }
606
607 static bool trans_l_jal(DisasContext *dc, arg_l_jal *a)
608 {
609     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
610     target_ulong ret_pc = dc->base.pc_next + 8;
611
612     tcg_gen_movi_tl(cpu_regs[9], ret_pc);
613     /* Optimize jal being used to load the PC for PIC.  */
614     if (tmp_pc != ret_pc) {
615         tcg_gen_movi_tl(jmp_pc, tmp_pc);
616         dc->jmp_pc_imm = tmp_pc;
617         dc->delayed_branch = 2;
618     }
619     return true;
620 }
621
622 static void do_bf(DisasContext *dc, arg_l_bf *a, TCGCond cond)
623 {
624     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
625     TCGv t_next = tcg_const_tl(dc->base.pc_next + 8);
626     TCGv t_true = tcg_const_tl(tmp_pc);
627     TCGv t_zero = tcg_const_tl(0);
628
629     tcg_gen_movcond_tl(cond, jmp_pc, cpu_sr_f, t_zero, t_true, t_next);
630
631     tcg_temp_free(t_next);
632     tcg_temp_free(t_true);
633     tcg_temp_free(t_zero);
634     dc->delayed_branch = 2;
635 }
636
637 static bool trans_l_bf(DisasContext *dc, arg_l_bf *a)
638 {
639     do_bf(dc, a, TCG_COND_NE);
640     return true;
641 }
642
643 static bool trans_l_bnf(DisasContext *dc, arg_l_bf *a)
644 {
645     do_bf(dc, a, TCG_COND_EQ);
646     return true;
647 }
648
649 static bool trans_l_jr(DisasContext *dc, arg_l_jr *a)
650 {
651     tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b));
652     dc->delayed_branch = 2;
653     return true;
654 }
655
656 static bool trans_l_jalr(DisasContext *dc, arg_l_jalr *a)
657 {
658     tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b));
659     tcg_gen_movi_tl(cpu_regs[9], dc->base.pc_next + 8);
660     dc->delayed_branch = 2;
661     return true;
662 }
663
664 static bool trans_l_lwa(DisasContext *dc, arg_load *a)
665 {
666     TCGv ea;
667
668     check_r0_write(dc, a->d);
669     ea = tcg_temp_new();
670     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
671     tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, MO_TEUL);
672     tcg_gen_mov_tl(cpu_lock_addr, ea);
673     tcg_gen_mov_tl(cpu_lock_value, cpu_R(dc, a->d));
674     tcg_temp_free(ea);
675     return true;
676 }
677
678 static void do_load(DisasContext *dc, arg_load *a, MemOp mop)
679 {
680     TCGv ea;
681
682     check_r0_write(dc, a->d);
683     ea = tcg_temp_new();
684     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
685     tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, mop);
686     tcg_temp_free(ea);
687 }
688
689 static bool trans_l_lwz(DisasContext *dc, arg_load *a)
690 {
691     do_load(dc, a, MO_TEUL);
692     return true;
693 }
694
695 static bool trans_l_lws(DisasContext *dc, arg_load *a)
696 {
697     do_load(dc, a, MO_TESL);
698     return true;
699 }
700
701 static bool trans_l_lbz(DisasContext *dc, arg_load *a)
702 {
703     do_load(dc, a, MO_UB);
704     return true;
705 }
706
707 static bool trans_l_lbs(DisasContext *dc, arg_load *a)
708 {
709     do_load(dc, a, MO_SB);
710     return true;
711 }
712
713 static bool trans_l_lhz(DisasContext *dc, arg_load *a)
714 {
715     do_load(dc, a, MO_TEUW);
716     return true;
717 }
718
719 static bool trans_l_lhs(DisasContext *dc, arg_load *a)
720 {
721     do_load(dc, a, MO_TESW);
722     return true;
723 }
724
725 static bool trans_l_swa(DisasContext *dc, arg_store *a)
726 {
727     TCGv ea, val;
728     TCGLabel *lab_fail, *lab_done;
729
730     ea = tcg_temp_new();
731     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
732
733     /* For TB_FLAGS_R0_0, the branch below invalidates the temporary assigned
734        to cpu_regs[0].  Since l.swa is quite often immediately followed by a
735        branch, don't bother reallocating; finish the TB using the "real" R0.
736        This also takes care of RB input across the branch.  */
737     dc->R0 = cpu_regs[0];
738
739     lab_fail = gen_new_label();
740     lab_done = gen_new_label();
741     tcg_gen_brcond_tl(TCG_COND_NE, ea, cpu_lock_addr, lab_fail);
742     tcg_temp_free(ea);
743
744     val = tcg_temp_new();
745     tcg_gen_atomic_cmpxchg_tl(val, cpu_lock_addr, cpu_lock_value,
746                               cpu_regs[a->b], dc->mem_idx, MO_TEUL);
747     tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f, val, cpu_lock_value);
748     tcg_temp_free(val);
749
750     tcg_gen_br(lab_done);
751
752     gen_set_label(lab_fail);
753     tcg_gen_movi_tl(cpu_sr_f, 0);
754
755     gen_set_label(lab_done);
756     tcg_gen_movi_tl(cpu_lock_addr, -1);
757     return true;
758 }
759
760 static void do_store(DisasContext *dc, arg_store *a, MemOp mop)
761 {
762     TCGv t0 = tcg_temp_new();
763     tcg_gen_addi_tl(t0, cpu_R(dc, a->a), a->i);
764     tcg_gen_qemu_st_tl(cpu_R(dc, a->b), t0, dc->mem_idx, mop);
765     tcg_temp_free(t0);
766 }
767
768 static bool trans_l_sw(DisasContext *dc, arg_store *a)
769 {
770     do_store(dc, a, MO_TEUL);
771     return true;
772 }
773
774 static bool trans_l_sb(DisasContext *dc, arg_store *a)
775 {
776     do_store(dc, a, MO_UB);
777     return true;
778 }
779
780 static bool trans_l_sh(DisasContext *dc, arg_store *a)
781 {
782     do_store(dc, a, MO_TEUW);
783     return true;
784 }
785
786 static bool trans_l_nop(DisasContext *dc, arg_l_nop *a)
787 {
788     return true;
789 }
790
791 static bool trans_l_addi(DisasContext *dc, arg_rri *a)
792 {
793     TCGv t0;
794
795     check_r0_write(dc, a->d);
796     t0 = tcg_const_tl(a->i);
797     gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), t0);
798     tcg_temp_free(t0);
799     return true;
800 }
801
802 static bool trans_l_addic(DisasContext *dc, arg_rri *a)
803 {
804     TCGv t0;
805
806     check_r0_write(dc, a->d);
807     t0 = tcg_const_tl(a->i);
808     gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), t0);
809     tcg_temp_free(t0);
810     return true;
811 }
812
813 static bool trans_l_muli(DisasContext *dc, arg_rri *a)
814 {
815     TCGv t0;
816
817     check_r0_write(dc, a->d);
818     t0 = tcg_const_tl(a->i);
819     gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), t0);
820     tcg_temp_free(t0);
821     return true;
822 }
823
824 static bool trans_l_maci(DisasContext *dc, arg_l_maci *a)
825 {
826     TCGv t0;
827
828     t0 = tcg_const_tl(a->i);
829     gen_mac(dc, cpu_R(dc, a->a), t0);
830     tcg_temp_free(t0);
831     return true;
832 }
833
834 static bool trans_l_andi(DisasContext *dc, arg_rrk *a)
835 {
836     check_r0_write(dc, a->d);
837     tcg_gen_andi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k);
838     return true;
839 }
840
841 static bool trans_l_ori(DisasContext *dc, arg_rrk *a)
842 {
843     check_r0_write(dc, a->d);
844     tcg_gen_ori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k);
845     return true;
846 }
847
848 static bool trans_l_xori(DisasContext *dc, arg_rri *a)
849 {
850     check_r0_write(dc, a->d);
851     tcg_gen_xori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->i);
852     return true;
853 }
854
855 static bool trans_l_mfspr(DisasContext *dc, arg_l_mfspr *a)
856 {
857     check_r0_write(dc, a->d);
858
859     if (is_user(dc)) {
860         gen_illegal_exception(dc);
861     } else {
862         TCGv spr = tcg_temp_new();
863         tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
864         gen_helper_mfspr(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d), spr);
865         tcg_temp_free(spr);
866     }
867     return true;
868 }
869
870 static bool trans_l_mtspr(DisasContext *dc, arg_l_mtspr *a)
871 {
872     if (is_user(dc)) {
873         gen_illegal_exception(dc);
874     } else {
875         TCGv spr;
876
877         /* For SR, we will need to exit the TB to recognize the new
878          * exception state.  For NPC, in theory this counts as a branch
879          * (although the SPR only exists for use by an ICE).  Save all
880          * of the cpu state first, allowing it to be overwritten.
881          */
882         if (dc->delayed_branch) {
883             tcg_gen_mov_tl(cpu_pc, jmp_pc);
884             tcg_gen_discard_tl(jmp_pc);
885         } else {
886             tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);
887         }
888         dc->base.is_jmp = DISAS_EXIT;
889
890         spr = tcg_temp_new();
891         tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
892         gen_helper_mtspr(cpu_env, spr, cpu_R(dc, a->b));
893         tcg_temp_free(spr);
894     }
895     return true;
896 }
897
898 static bool trans_l_mac(DisasContext *dc, arg_ab *a)
899 {
900     gen_mac(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
901     return true;
902 }
903
904 static bool trans_l_msb(DisasContext *dc, arg_ab *a)
905 {
906     gen_msb(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
907     return true;
908 }
909
910 static bool trans_l_macu(DisasContext *dc, arg_ab *a)
911 {
912     gen_macu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
913     return true;
914 }
915
916 static bool trans_l_msbu(DisasContext *dc, arg_ab *a)
917 {
918     gen_msbu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
919     return true;
920 }
921
922 static bool trans_l_slli(DisasContext *dc, arg_dal *a)
923 {
924     check_r0_write(dc, a->d);
925     tcg_gen_shli_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
926                     a->l & (TARGET_LONG_BITS - 1));
927     return true;
928 }
929
930 static bool trans_l_srli(DisasContext *dc, arg_dal *a)
931 {
932     check_r0_write(dc, a->d);
933     tcg_gen_shri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
934                     a->l & (TARGET_LONG_BITS - 1));
935     return true;
936 }
937
938 static bool trans_l_srai(DisasContext *dc, arg_dal *a)
939 {
940     check_r0_write(dc, a->d);
941     tcg_gen_sari_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
942                     a->l & (TARGET_LONG_BITS - 1));
943     return true;
944 }
945
946 static bool trans_l_rori(DisasContext *dc, arg_dal *a)
947 {
948     check_r0_write(dc, a->d);
949     tcg_gen_rotri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
950                      a->l & (TARGET_LONG_BITS - 1));
951     return true;
952 }
953
954 static bool trans_l_movhi(DisasContext *dc, arg_l_movhi *a)
955 {
956     check_r0_write(dc, a->d);
957     tcg_gen_movi_tl(cpu_R(dc, a->d), a->k << 16);
958     return true;
959 }
960
961 static bool trans_l_macrc(DisasContext *dc, arg_l_macrc *a)
962 {
963     check_r0_write(dc, a->d);
964     tcg_gen_trunc_i64_tl(cpu_R(dc, a->d), cpu_mac);
965     tcg_gen_movi_i64(cpu_mac, 0);
966     return true;
967 }
968
969 static bool trans_l_sfeq(DisasContext *dc, arg_ab *a)
970 {
971     tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f,
972                        cpu_R(dc, a->a), cpu_R(dc, a->b));
973     return true;
974 }
975
976 static bool trans_l_sfne(DisasContext *dc, arg_ab *a)
977 {
978     tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_f,
979                        cpu_R(dc, a->a), cpu_R(dc, a->b));
980     return true;
981 }
982
983 static bool trans_l_sfgtu(DisasContext *dc, arg_ab *a)
984 {
985     tcg_gen_setcond_tl(TCG_COND_GTU, cpu_sr_f,
986                        cpu_R(dc, a->a), cpu_R(dc, a->b));
987     return true;
988 }
989
990 static bool trans_l_sfgeu(DisasContext *dc, arg_ab *a)
991 {
992     tcg_gen_setcond_tl(TCG_COND_GEU, cpu_sr_f,
993                        cpu_R(dc, a->a), cpu_R(dc, a->b));
994     return true;
995 }
996
997 static bool trans_l_sfltu(DisasContext *dc, arg_ab *a)
998 {
999     tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_f,
1000                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1001     return true;
1002 }
1003
1004 static bool trans_l_sfleu(DisasContext *dc, arg_ab *a)
1005 {
1006     tcg_gen_setcond_tl(TCG_COND_LEU, cpu_sr_f,
1007                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1008     return true;
1009 }
1010
1011 static bool trans_l_sfgts(DisasContext *dc, arg_ab *a)
1012 {
1013     tcg_gen_setcond_tl(TCG_COND_GT, cpu_sr_f,
1014                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1015     return true;
1016 }
1017
1018 static bool trans_l_sfges(DisasContext *dc, arg_ab *a)
1019 {
1020     tcg_gen_setcond_tl(TCG_COND_GE, cpu_sr_f,
1021                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1022     return true;
1023 }
1024
1025 static bool trans_l_sflts(DisasContext *dc, arg_ab *a)
1026 {
1027     tcg_gen_setcond_tl(TCG_COND_LT, cpu_sr_f,
1028                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1029     return true;
1030 }
1031
1032 static bool trans_l_sfles(DisasContext *dc, arg_ab *a)
1033 {
1034     tcg_gen_setcond_tl(TCG_COND_LE,
1035                        cpu_sr_f, cpu_R(dc, a->a), cpu_R(dc, a->b));
1036     return true;
1037 }
1038
1039 static bool trans_l_sfeqi(DisasContext *dc, arg_ai *a)
1040 {
1041     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_f, cpu_R(dc, a->a), a->i);
1042     return true;
1043 }
1044
1045 static bool trans_l_sfnei(DisasContext *dc, arg_ai *a)
1046 {
1047     tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1048     return true;
1049 }
1050
1051 static bool trans_l_sfgtui(DisasContext *dc, arg_ai *a)
1052 {
1053     tcg_gen_setcondi_tl(TCG_COND_GTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1054     return true;
1055 }
1056
1057 static bool trans_l_sfgeui(DisasContext *dc, arg_ai *a)
1058 {
1059     tcg_gen_setcondi_tl(TCG_COND_GEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1060     return true;
1061 }
1062
1063 static bool trans_l_sfltui(DisasContext *dc, arg_ai *a)
1064 {
1065     tcg_gen_setcondi_tl(TCG_COND_LTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1066     return true;
1067 }
1068
1069 static bool trans_l_sfleui(DisasContext *dc, arg_ai *a)
1070 {
1071     tcg_gen_setcondi_tl(TCG_COND_LEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1072     return true;
1073 }
1074
1075 static bool trans_l_sfgtsi(DisasContext *dc, arg_ai *a)
1076 {
1077     tcg_gen_setcondi_tl(TCG_COND_GT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1078     return true;
1079 }
1080
1081 static bool trans_l_sfgesi(DisasContext *dc, arg_ai *a)
1082 {
1083     tcg_gen_setcondi_tl(TCG_COND_GE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1084     return true;
1085 }
1086
1087 static bool trans_l_sfltsi(DisasContext *dc, arg_ai *a)
1088 {
1089     tcg_gen_setcondi_tl(TCG_COND_LT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1090     return true;
1091 }
1092
1093 static bool trans_l_sflesi(DisasContext *dc, arg_ai *a)
1094 {
1095     tcg_gen_setcondi_tl(TCG_COND_LE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1096     return true;
1097 }
1098
1099 static bool trans_l_sys(DisasContext *dc, arg_l_sys *a)
1100 {
1101     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1102     gen_exception(dc, EXCP_SYSCALL);
1103     dc->base.is_jmp = DISAS_NORETURN;
1104     return true;
1105 }
1106
1107 static bool trans_l_trap(DisasContext *dc, arg_l_trap *a)
1108 {
1109     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1110     gen_exception(dc, EXCP_TRAP);
1111     dc->base.is_jmp = DISAS_NORETURN;
1112     return true;
1113 }
1114
1115 static bool trans_l_msync(DisasContext *dc, arg_l_msync *a)
1116 {
1117     tcg_gen_mb(TCG_MO_ALL);
1118     return true;
1119 }
1120
1121 static bool trans_l_psync(DisasContext *dc, arg_l_psync *a)
1122 {
1123     return true;
1124 }
1125
1126 static bool trans_l_csync(DisasContext *dc, arg_l_csync *a)
1127 {
1128     return true;
1129 }
1130
1131 static bool trans_l_rfe(DisasContext *dc, arg_l_rfe *a)
1132 {
1133     if (is_user(dc)) {
1134         gen_illegal_exception(dc);
1135     } else {
1136         gen_helper_rfe(cpu_env);
1137         dc->base.is_jmp = DISAS_EXIT;
1138     }
1139     return true;
1140 }
1141
1142 static bool do_fp2(DisasContext *dc, arg_da *a,
1143                    void (*fn)(TCGv, TCGv_env, TCGv))
1144 {
1145     if (!check_of32s(dc)) {
1146         return false;
1147     }
1148     check_r0_write(dc, a->d);
1149     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a));
1150     gen_helper_update_fpcsr(cpu_env);
1151     return true;
1152 }
1153
1154 static bool do_fp3(DisasContext *dc, arg_dab *a,
1155                    void (*fn)(TCGv, TCGv_env, TCGv, TCGv))
1156 {
1157     if (!check_of32s(dc)) {
1158         return false;
1159     }
1160     check_r0_write(dc, a->d);
1161     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1162     gen_helper_update_fpcsr(cpu_env);
1163     return true;
1164 }
1165
1166 static bool do_fpcmp(DisasContext *dc, arg_ab *a,
1167                      void (*fn)(TCGv, TCGv_env, TCGv, TCGv),
1168                      bool inv, bool swap)
1169 {
1170     if (!check_of32s(dc)) {
1171         return false;
1172     }
1173     if (swap) {
1174         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->b), cpu_R(dc, a->a));
1175     } else {
1176         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1177     }
1178     if (inv) {
1179         tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1);
1180     }
1181     gen_helper_update_fpcsr(cpu_env);
1182     return true;
1183 }
1184
1185 static bool trans_lf_add_s(DisasContext *dc, arg_dab *a)
1186 {
1187     return do_fp3(dc, a, gen_helper_float_add_s);
1188 }
1189
1190 static bool trans_lf_sub_s(DisasContext *dc, arg_dab *a)
1191 {
1192     return do_fp3(dc, a, gen_helper_float_sub_s);
1193 }
1194
1195 static bool trans_lf_mul_s(DisasContext *dc, arg_dab *a)
1196 {
1197     return do_fp3(dc, a, gen_helper_float_mul_s);
1198 }
1199
1200 static bool trans_lf_div_s(DisasContext *dc, arg_dab *a)
1201 {
1202     return do_fp3(dc, a, gen_helper_float_div_s);
1203 }
1204
1205 static bool trans_lf_rem_s(DisasContext *dc, arg_dab *a)
1206 {
1207     return do_fp3(dc, a, gen_helper_float_rem_s);
1208     return true;
1209 }
1210
1211 static bool trans_lf_itof_s(DisasContext *dc, arg_da *a)
1212 {
1213     return do_fp2(dc, a, gen_helper_itofs);
1214 }
1215
1216 static bool trans_lf_ftoi_s(DisasContext *dc, arg_da *a)
1217 {
1218     return do_fp2(dc, a, gen_helper_ftois);
1219 }
1220
1221 static bool trans_lf_madd_s(DisasContext *dc, arg_dab *a)
1222 {
1223     if (!check_of32s(dc)) {
1224         return false;
1225     }
1226     check_r0_write(dc, a->d);
1227     gen_helper_float_madd_s(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d),
1228                             cpu_R(dc, a->a), cpu_R(dc, a->b));
1229     gen_helper_update_fpcsr(cpu_env);
1230     return true;
1231 }
1232
1233 static bool trans_lf_sfeq_s(DisasContext *dc, arg_ab *a)
1234 {
1235     return do_fpcmp(dc, a, gen_helper_float_eq_s, false, false);
1236 }
1237
1238 static bool trans_lf_sfne_s(DisasContext *dc, arg_ab *a)
1239 {
1240     return do_fpcmp(dc, a, gen_helper_float_eq_s, true, false);
1241 }
1242
1243 static bool trans_lf_sfgt_s(DisasContext *dc, arg_ab *a)
1244 {
1245     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, true);
1246 }
1247
1248 static bool trans_lf_sfge_s(DisasContext *dc, arg_ab *a)
1249 {
1250     return do_fpcmp(dc, a, gen_helper_float_le_s, false, true);
1251 }
1252
1253 static bool trans_lf_sflt_s(DisasContext *dc, arg_ab *a)
1254 {
1255     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, false);
1256 }
1257
1258 static bool trans_lf_sfle_s(DisasContext *dc, arg_ab *a)
1259 {
1260     return do_fpcmp(dc, a, gen_helper_float_le_s, false, false);
1261 }
1262
1263 static void openrisc_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1264 {
1265     DisasContext *dc = container_of(dcb, DisasContext, base);
1266     CPUOpenRISCState *env = cs->env_ptr;
1267     int bound;
1268
1269     dc->mem_idx = cpu_mmu_index(env, false);
1270     dc->tb_flags = dc->base.tb->flags;
1271     dc->delayed_branch = (dc->tb_flags & TB_FLAGS_DFLAG) != 0;
1272     dc->cpucfgr = env->cpucfgr;
1273     dc->jmp_pc_imm = -1;
1274
1275     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1276     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1277 }
1278
1279 static void openrisc_tr_tb_start(DisasContextBase *db, CPUState *cs)
1280 {
1281     DisasContext *dc = container_of(db, DisasContext, base);
1282
1283     /* Allow the TCG optimizer to see that R0 == 0,
1284        when it's true, which is the common case.  */
1285     if (dc->tb_flags & TB_FLAGS_R0_0) {
1286         dc->R0 = tcg_const_tl(0);
1287     } else {
1288         dc->R0 = cpu_regs[0];
1289     }
1290 }
1291
1292 static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
1293 {
1294     DisasContext *dc = container_of(dcbase, DisasContext, base);
1295
1296     tcg_gen_insn_start(dc->base.pc_next, (dc->delayed_branch ? 1 : 0)
1297                        | (dc->base.num_insns > 1 ? 2 : 0));
1298 }
1299
1300 static bool openrisc_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cs,
1301                                          const CPUBreakpoint *bp)
1302 {
1303     DisasContext *dc = container_of(dcbase, DisasContext, base);
1304
1305     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1306     gen_exception(dc, EXCP_DEBUG);
1307     dc->base.is_jmp = DISAS_NORETURN;
1308     /* The address covered by the breakpoint must be included in
1309        [tb->pc, tb->pc + tb->size) in order to for it to be
1310        properly cleared -- thus we increment the PC here so that
1311        the logic setting tb->size below does the right thing.  */
1312     dc->base.pc_next += 4;
1313     return true;
1314 }
1315
1316 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
1317 {
1318     DisasContext *dc = container_of(dcbase, DisasContext, base);
1319     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1320     uint32_t insn = cpu_ldl_code(&cpu->env, dc->base.pc_next);
1321
1322     if (!decode(dc, insn)) {
1323         gen_illegal_exception(dc);
1324     }
1325     dc->base.pc_next += 4;
1326
1327     /* When exiting the delay slot normally, exit via jmp_pc.
1328      * For DISAS_NORETURN, we have raised an exception and already exited.
1329      * For DISAS_EXIT, we found l.rfe in a delay slot.  There's nothing
1330      * in the manual saying this is illegal, but it surely it should.
1331      * At least or1ksim overrides pcnext and ignores the branch.
1332      */
1333     if (dc->delayed_branch
1334         && --dc->delayed_branch == 0
1335         && dc->base.is_jmp == DISAS_NEXT) {
1336         dc->base.is_jmp = DISAS_JUMP;
1337     }
1338 }
1339
1340 static void openrisc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
1341 {
1342     DisasContext *dc = container_of(dcbase, DisasContext, base);
1343     target_ulong jmp_dest;
1344
1345     /* If we have already exited the TB, nothing following has effect.  */
1346     if (dc->base.is_jmp == DISAS_NORETURN) {
1347         return;
1348     }
1349
1350     /* Adjust the delayed branch state for the next TB.  */
1351     if ((dc->tb_flags & TB_FLAGS_DFLAG ? 1 : 0) != (dc->delayed_branch != 0)) {
1352         tcg_gen_movi_i32(cpu_dflag, dc->delayed_branch != 0);
1353     }
1354
1355     /* For DISAS_TOO_MANY, jump to the next insn.  */
1356     jmp_dest = dc->base.pc_next;
1357     tcg_gen_movi_tl(cpu_ppc, jmp_dest - 4);
1358
1359     switch (dc->base.is_jmp) {
1360     case DISAS_JUMP:
1361         jmp_dest = dc->jmp_pc_imm;
1362         if (jmp_dest == -1) {
1363             /* The jump destination is indirect/computed; use jmp_pc.  */
1364             tcg_gen_mov_tl(cpu_pc, jmp_pc);
1365             tcg_gen_discard_tl(jmp_pc);
1366             if (unlikely(dc->base.singlestep_enabled)) {
1367                 gen_exception(dc, EXCP_DEBUG);
1368             } else {
1369                 tcg_gen_lookup_and_goto_ptr();
1370             }
1371             break;
1372         }
1373         /* The jump destination is direct; use jmp_pc_imm.
1374            However, we will have stored into jmp_pc as well;
1375            we know now that it wasn't needed.  */
1376         tcg_gen_discard_tl(jmp_pc);
1377         /* fallthru */
1378
1379     case DISAS_TOO_MANY:
1380         if (unlikely(dc->base.singlestep_enabled)) {
1381             tcg_gen_movi_tl(cpu_pc, jmp_dest);
1382             gen_exception(dc, EXCP_DEBUG);
1383         } else if ((dc->base.pc_first ^ jmp_dest) & TARGET_PAGE_MASK) {
1384             tcg_gen_movi_tl(cpu_pc, jmp_dest);
1385             tcg_gen_lookup_and_goto_ptr();
1386         } else {
1387             tcg_gen_goto_tb(0);
1388             tcg_gen_movi_tl(cpu_pc, jmp_dest);
1389             tcg_gen_exit_tb(dc->base.tb, 0);
1390         }
1391         break;
1392
1393     case DISAS_EXIT:
1394         if (unlikely(dc->base.singlestep_enabled)) {
1395             gen_exception(dc, EXCP_DEBUG);
1396         } else {
1397             tcg_gen_exit_tb(NULL, 0);
1398         }
1399         break;
1400     default:
1401         g_assert_not_reached();
1402     }
1403 }
1404
1405 static void openrisc_tr_disas_log(const DisasContextBase *dcbase, CPUState *cs)
1406 {
1407     DisasContext *s = container_of(dcbase, DisasContext, base);
1408
1409     qemu_log("IN: %s\n", lookup_symbol(s->base.pc_first));
1410     log_target_disas(cs, s->base.pc_first, s->base.tb->size);
1411 }
1412
1413 static const TranslatorOps openrisc_tr_ops = {
1414     .init_disas_context = openrisc_tr_init_disas_context,
1415     .tb_start           = openrisc_tr_tb_start,
1416     .insn_start         = openrisc_tr_insn_start,
1417     .breakpoint_check   = openrisc_tr_breakpoint_check,
1418     .translate_insn     = openrisc_tr_translate_insn,
1419     .tb_stop            = openrisc_tr_tb_stop,
1420     .disas_log          = openrisc_tr_disas_log,
1421 };
1422
1423 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int max_insns)
1424 {
1425     DisasContext ctx;
1426
1427     translator_loop(&openrisc_tr_ops, &ctx.base, cs, tb, max_insns);
1428 }
1429
1430 void openrisc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1431 {
1432     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1433     CPUOpenRISCState *env = &cpu->env;
1434     int i;
1435
1436     qemu_fprintf(f, "PC=%08x\n", env->pc);
1437     for (i = 0; i < 32; ++i) {
1438         qemu_fprintf(f, "R%02d=%08x%c", i, cpu_get_gpr(env, i),
1439                      (i % 4) == 3 ? '\n' : ' ');
1440     }
1441 }
1442
1443 void restore_state_to_opc(CPUOpenRISCState *env, TranslationBlock *tb,
1444                           target_ulong *data)
1445 {
1446     env->pc = data[0];
1447     env->dflag = data[1] & 1;
1448     if (data[1] & 2) {
1449         env->ppc = env->pc - 4;
1450     }
1451 }
This page took 0.148627 seconds and 4 git commands to generate.