xref: /qemu/target/openrisc/translate.c (revision 370ed600)
1 /*
2  * OpenRISC translation
3  *
4  * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5  *                         Feng Gao <gf91597@gmail.com>
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/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 "exec/log.h"
37 
38 /* is_jmp field values */
39 #define DISAS_EXIT    DISAS_TARGET_0  /* force exit to main loop */
40 #define DISAS_JUMP    DISAS_TARGET_1  /* exit via jmp_pc/jmp_pc_imm */
41 
42 typedef struct DisasContext {
43     DisasContextBase base;
44     uint32_t mem_idx;
45     uint32_t tb_flags;
46     uint32_t delayed_branch;
47     uint32_t cpucfgr;
48     uint32_t avr;
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     /* The constant zero. */
56     TCGv zero;
57 } DisasContext;
58 
59 static inline bool is_user(DisasContext *dc)
60 {
61 #ifdef CONFIG_USER_ONLY
62     return true;
63 #else
64     return !(dc->tb_flags & TB_FLAGS_SM);
65 #endif
66 }
67 
68 /* Include the auto-generated decoder.  */
69 #include "decode-insns.c.inc"
70 
71 static TCGv cpu_sr;
72 static TCGv cpu_regs[32];
73 static TCGv cpu_pc;
74 static TCGv jmp_pc;            /* l.jr/l.jalr temp pc */
75 static TCGv cpu_ppc;
76 static TCGv cpu_sr_f;           /* bf/bnf, F flag taken */
77 static TCGv cpu_sr_cy;          /* carry (unsigned overflow) */
78 static TCGv cpu_sr_ov;          /* signed overflow */
79 static TCGv cpu_lock_addr;
80 static TCGv cpu_lock_value;
81 static TCGv_i32 fpcsr;
82 static TCGv_i64 cpu_mac;        /* MACHI:MACLO */
83 static TCGv_i32 cpu_dflag;
84 
85 void openrisc_translate_init(void)
86 {
87     static const char * const regnames[] = {
88         "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
89         "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
90         "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
91         "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
92     };
93     int i;
94 
95     cpu_sr = tcg_global_mem_new(cpu_env,
96                                 offsetof(CPUOpenRISCState, sr), "sr");
97     cpu_dflag = tcg_global_mem_new_i32(cpu_env,
98                                        offsetof(CPUOpenRISCState, dflag),
99                                        "dflag");
100     cpu_pc = tcg_global_mem_new(cpu_env,
101                                 offsetof(CPUOpenRISCState, pc), "pc");
102     cpu_ppc = tcg_global_mem_new(cpu_env,
103                                  offsetof(CPUOpenRISCState, ppc), "ppc");
104     jmp_pc = tcg_global_mem_new(cpu_env,
105                                 offsetof(CPUOpenRISCState, jmp_pc), "jmp_pc");
106     cpu_sr_f = tcg_global_mem_new(cpu_env,
107                                   offsetof(CPUOpenRISCState, sr_f), "sr_f");
108     cpu_sr_cy = tcg_global_mem_new(cpu_env,
109                                    offsetof(CPUOpenRISCState, sr_cy), "sr_cy");
110     cpu_sr_ov = tcg_global_mem_new(cpu_env,
111                                    offsetof(CPUOpenRISCState, sr_ov), "sr_ov");
112     cpu_lock_addr = tcg_global_mem_new(cpu_env,
113                                        offsetof(CPUOpenRISCState, lock_addr),
114                                        "lock_addr");
115     cpu_lock_value = tcg_global_mem_new(cpu_env,
116                                         offsetof(CPUOpenRISCState, lock_value),
117                                         "lock_value");
118     fpcsr = tcg_global_mem_new_i32(cpu_env,
119                                    offsetof(CPUOpenRISCState, fpcsr),
120                                    "fpcsr");
121     cpu_mac = tcg_global_mem_new_i64(cpu_env,
122                                      offsetof(CPUOpenRISCState, mac),
123                                      "mac");
124     for (i = 0; i < 32; i++) {
125         cpu_regs[i] = tcg_global_mem_new(cpu_env,
126                                          offsetof(CPUOpenRISCState,
127                                                   shadow_gpr[0][i]),
128                                          regnames[i]);
129     }
130 }
131 
132 static void gen_exception(DisasContext *dc, unsigned int excp)
133 {
134     gen_helper_exception(cpu_env, tcg_constant_i32(excp));
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_v1_3(DisasContext *dc)
145 {
146     return dc->avr >= 0x01030000;
147 }
148 
149 static bool check_of32s(DisasContext *dc)
150 {
151     return dc->cpucfgr & CPUCFGR_OF32S;
152 }
153 
154 static bool check_of64a32s(DisasContext *dc)
155 {
156     return dc->cpucfgr & CPUCFGR_OF64A32S;
157 }
158 
159 static TCGv cpu_R(DisasContext *dc, int reg)
160 {
161     if (reg == 0) {
162         return dc->R0;
163     } else {
164         return cpu_regs[reg];
165     }
166 }
167 
168 /*
169  * We're about to write to REG.  On the off-chance that the user is
170  * writing to R0, re-instate the architectural register.
171  */
172 static void check_r0_write(DisasContext *dc, int reg)
173 {
174     if (unlikely(reg == 0)) {
175         dc->R0 = cpu_regs[0];
176     }
177 }
178 
179 static void gen_ove_cy(DisasContext *dc)
180 {
181     if (dc->tb_flags & SR_OVE) {
182         gen_helper_ove_cy(cpu_env);
183     }
184 }
185 
186 static void gen_ove_ov(DisasContext *dc)
187 {
188     if (dc->tb_flags & SR_OVE) {
189         gen_helper_ove_ov(cpu_env);
190     }
191 }
192 
193 static void gen_ove_cyov(DisasContext *dc)
194 {
195     if (dc->tb_flags & SR_OVE) {
196         gen_helper_ove_cyov(cpu_env);
197     }
198 }
199 
200 static void gen_add(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
201 {
202     TCGv t0 = tcg_temp_new();
203     TCGv res = tcg_temp_new();
204 
205     tcg_gen_add2_tl(res, cpu_sr_cy, srca, dc->zero, srcb, dc->zero);
206     tcg_gen_xor_tl(cpu_sr_ov, srca, srcb);
207     tcg_gen_xor_tl(t0, res, srcb);
208     tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov);
209 
210     tcg_gen_mov_tl(dest, res);
211 
212     gen_ove_cyov(dc);
213 }
214 
215 static void gen_addc(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
216 {
217     TCGv t0 = tcg_temp_new();
218     TCGv res = tcg_temp_new();
219 
220     tcg_gen_add2_tl(res, cpu_sr_cy, srca, dc->zero, cpu_sr_cy, dc->zero);
221     tcg_gen_add2_tl(res, cpu_sr_cy, res, cpu_sr_cy, srcb, dc->zero);
222     tcg_gen_xor_tl(cpu_sr_ov, srca, srcb);
223     tcg_gen_xor_tl(t0, res, srcb);
224     tcg_gen_andc_tl(cpu_sr_ov, t0, cpu_sr_ov);
225 
226     tcg_gen_mov_tl(dest, res);
227 
228     gen_ove_cyov(dc);
229 }
230 
231 static void gen_sub(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
232 {
233     TCGv res = tcg_temp_new();
234 
235     tcg_gen_sub_tl(res, srca, srcb);
236     tcg_gen_xor_tl(cpu_sr_cy, srca, srcb);
237     tcg_gen_xor_tl(cpu_sr_ov, res, srcb);
238     tcg_gen_and_tl(cpu_sr_ov, cpu_sr_ov, cpu_sr_cy);
239     tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_cy, srca, srcb);
240 
241     tcg_gen_mov_tl(dest, res);
242 
243     gen_ove_cyov(dc);
244 }
245 
246 static void gen_mul(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
247 {
248     TCGv t0 = tcg_temp_new();
249 
250     tcg_gen_muls2_tl(dest, cpu_sr_ov, srca, srcb);
251     tcg_gen_sari_tl(t0, dest, TARGET_LONG_BITS - 1);
252     tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_ov, cpu_sr_ov, t0);
253 
254     tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
255     gen_ove_ov(dc);
256 }
257 
258 static void gen_mulu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
259 {
260     tcg_gen_muls2_tl(dest, cpu_sr_cy, srca, srcb);
261     tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_cy, cpu_sr_cy, 0);
262 
263     gen_ove_cy(dc);
264 }
265 
266 static void gen_div(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
267 {
268     TCGv t0 = tcg_temp_new();
269 
270     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_ov, srcb, 0);
271     /* The result of divide-by-zero is undefined.
272        Supress the host-side exception by dividing by 1.  */
273     tcg_gen_or_tl(t0, srcb, cpu_sr_ov);
274     tcg_gen_div_tl(dest, srca, t0);
275 
276     tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
277     gen_ove_ov(dc);
278 }
279 
280 static void gen_divu(DisasContext *dc, TCGv dest, TCGv srca, TCGv srcb)
281 {
282     TCGv t0 = tcg_temp_new();
283 
284     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_cy, srcb, 0);
285     /* The result of divide-by-zero is undefined.
286        Supress the host-side exception by dividing by 1.  */
287     tcg_gen_or_tl(t0, srcb, cpu_sr_cy);
288     tcg_gen_divu_tl(dest, srca, t0);
289 
290     gen_ove_cy(dc);
291 }
292 
293 static void gen_muld(DisasContext *dc, TCGv srca, TCGv srcb)
294 {
295     TCGv_i64 t1 = tcg_temp_new_i64();
296     TCGv_i64 t2 = tcg_temp_new_i64();
297 
298     tcg_gen_ext_tl_i64(t1, srca);
299     tcg_gen_ext_tl_i64(t2, srcb);
300     if (TARGET_LONG_BITS == 32) {
301         tcg_gen_mul_i64(cpu_mac, t1, t2);
302         tcg_gen_movi_tl(cpu_sr_ov, 0);
303     } else {
304         TCGv_i64 high = tcg_temp_new_i64();
305 
306         tcg_gen_muls2_i64(cpu_mac, high, t1, t2);
307         tcg_gen_sari_i64(t1, cpu_mac, 63);
308         tcg_gen_setcond_i64(TCG_COND_NE, t1, t1, high);
309         tcg_gen_trunc_i64_tl(cpu_sr_ov, t1);
310         tcg_gen_neg_tl(cpu_sr_ov, cpu_sr_ov);
311 
312         gen_ove_ov(dc);
313     }
314 }
315 
316 static void gen_muldu(DisasContext *dc, TCGv srca, TCGv srcb)
317 {
318     TCGv_i64 t1 = tcg_temp_new_i64();
319     TCGv_i64 t2 = tcg_temp_new_i64();
320 
321     tcg_gen_extu_tl_i64(t1, srca);
322     tcg_gen_extu_tl_i64(t2, srcb);
323     if (TARGET_LONG_BITS == 32) {
324         tcg_gen_mul_i64(cpu_mac, t1, t2);
325         tcg_gen_movi_tl(cpu_sr_cy, 0);
326     } else {
327         TCGv_i64 high = tcg_temp_new_i64();
328 
329         tcg_gen_mulu2_i64(cpu_mac, high, t1, t2);
330         tcg_gen_setcondi_i64(TCG_COND_NE, high, high, 0);
331         tcg_gen_trunc_i64_tl(cpu_sr_cy, high);
332 
333         gen_ove_cy(dc);
334     }
335 }
336 
337 static void gen_mac(DisasContext *dc, TCGv srca, TCGv srcb)
338 {
339     TCGv_i64 t1 = tcg_temp_new_i64();
340     TCGv_i64 t2 = tcg_temp_new_i64();
341 
342     tcg_gen_ext_tl_i64(t1, srca);
343     tcg_gen_ext_tl_i64(t2, srcb);
344     tcg_gen_mul_i64(t1, t1, t2);
345 
346     /* Note that overflow is only computed during addition stage.  */
347     tcg_gen_xor_i64(t2, cpu_mac, t1);
348     tcg_gen_add_i64(cpu_mac, cpu_mac, t1);
349     tcg_gen_xor_i64(t1, t1, cpu_mac);
350     tcg_gen_andc_i64(t1, t1, t2);
351 
352 #if TARGET_LONG_BITS == 32
353     tcg_gen_extrh_i64_i32(cpu_sr_ov, t1);
354 #else
355     tcg_gen_mov_i64(cpu_sr_ov, t1);
356 #endif
357 
358     gen_ove_ov(dc);
359 }
360 
361 static void gen_macu(DisasContext *dc, TCGv srca, TCGv srcb)
362 {
363     TCGv_i64 t1 = tcg_temp_new_i64();
364     TCGv_i64 t2 = tcg_temp_new_i64();
365 
366     tcg_gen_extu_tl_i64(t1, srca);
367     tcg_gen_extu_tl_i64(t2, srcb);
368     tcg_gen_mul_i64(t1, t1, t2);
369 
370     /* Note that overflow is only computed during addition stage.  */
371     tcg_gen_add_i64(cpu_mac, cpu_mac, t1);
372     tcg_gen_setcond_i64(TCG_COND_LTU, t1, cpu_mac, t1);
373     tcg_gen_trunc_i64_tl(cpu_sr_cy, t1);
374 
375     gen_ove_cy(dc);
376 }
377 
378 static void gen_msb(DisasContext *dc, TCGv srca, TCGv srcb)
379 {
380     TCGv_i64 t1 = tcg_temp_new_i64();
381     TCGv_i64 t2 = tcg_temp_new_i64();
382 
383     tcg_gen_ext_tl_i64(t1, srca);
384     tcg_gen_ext_tl_i64(t2, srcb);
385     tcg_gen_mul_i64(t1, t1, t2);
386 
387     /* Note that overflow is only computed during subtraction stage.  */
388     tcg_gen_xor_i64(t2, cpu_mac, t1);
389     tcg_gen_sub_i64(cpu_mac, cpu_mac, t1);
390     tcg_gen_xor_i64(t1, t1, cpu_mac);
391     tcg_gen_and_i64(t1, t1, t2);
392 
393 #if TARGET_LONG_BITS == 32
394     tcg_gen_extrh_i64_i32(cpu_sr_ov, t1);
395 #else
396     tcg_gen_mov_i64(cpu_sr_ov, t1);
397 #endif
398 
399     gen_ove_ov(dc);
400 }
401 
402 static void gen_msbu(DisasContext *dc, TCGv srca, TCGv srcb)
403 {
404     TCGv_i64 t1 = tcg_temp_new_i64();
405     TCGv_i64 t2 = tcg_temp_new_i64();
406 
407     tcg_gen_extu_tl_i64(t1, srca);
408     tcg_gen_extu_tl_i64(t2, srcb);
409     tcg_gen_mul_i64(t1, t1, t2);
410 
411     /* Note that overflow is only computed during subtraction stage.  */
412     tcg_gen_setcond_i64(TCG_COND_LTU, t2, cpu_mac, t1);
413     tcg_gen_sub_i64(cpu_mac, cpu_mac, t1);
414     tcg_gen_trunc_i64_tl(cpu_sr_cy, t2);
415 
416     gen_ove_cy(dc);
417 }
418 
419 static bool trans_l_add(DisasContext *dc, arg_dab *a)
420 {
421     check_r0_write(dc, a->d);
422     gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
423     return true;
424 }
425 
426 static bool trans_l_addc(DisasContext *dc, arg_dab *a)
427 {
428     check_r0_write(dc, a->d);
429     gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
430     return true;
431 }
432 
433 static bool trans_l_sub(DisasContext *dc, arg_dab *a)
434 {
435     check_r0_write(dc, a->d);
436     gen_sub(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
437     return true;
438 }
439 
440 static bool trans_l_and(DisasContext *dc, arg_dab *a)
441 {
442     check_r0_write(dc, a->d);
443     tcg_gen_and_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
444     return true;
445 }
446 
447 static bool trans_l_or(DisasContext *dc, arg_dab *a)
448 {
449     check_r0_write(dc, a->d);
450     tcg_gen_or_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
451     return true;
452 }
453 
454 static bool trans_l_xor(DisasContext *dc, arg_dab *a)
455 {
456     check_r0_write(dc, a->d);
457     tcg_gen_xor_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
458     return true;
459 }
460 
461 static bool trans_l_sll(DisasContext *dc, arg_dab *a)
462 {
463     check_r0_write(dc, a->d);
464     tcg_gen_shl_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
465     return true;
466 }
467 
468 static bool trans_l_srl(DisasContext *dc, arg_dab *a)
469 {
470     check_r0_write(dc, a->d);
471     tcg_gen_shr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
472     return true;
473 }
474 
475 static bool trans_l_sra(DisasContext *dc, arg_dab *a)
476 {
477     check_r0_write(dc, a->d);
478     tcg_gen_sar_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
479     return true;
480 }
481 
482 static bool trans_l_ror(DisasContext *dc, arg_dab *a)
483 {
484     check_r0_write(dc, a->d);
485     tcg_gen_rotr_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
486     return true;
487 }
488 
489 static bool trans_l_exths(DisasContext *dc, arg_da *a)
490 {
491     check_r0_write(dc, a->d);
492     tcg_gen_ext16s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
493     return true;
494 }
495 
496 static bool trans_l_extbs(DisasContext *dc, arg_da *a)
497 {
498     check_r0_write(dc, a->d);
499     tcg_gen_ext8s_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
500     return true;
501 }
502 
503 static bool trans_l_exthz(DisasContext *dc, arg_da *a)
504 {
505     check_r0_write(dc, a->d);
506     tcg_gen_ext16u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
507     return true;
508 }
509 
510 static bool trans_l_extbz(DisasContext *dc, arg_da *a)
511 {
512     check_r0_write(dc, a->d);
513     tcg_gen_ext8u_tl(cpu_R(dc, a->d), cpu_R(dc, a->a));
514     return true;
515 }
516 
517 static bool trans_l_cmov(DisasContext *dc, arg_dab *a)
518 {
519     check_r0_write(dc, a->d);
520     tcg_gen_movcond_tl(TCG_COND_NE, cpu_R(dc, a->d), cpu_sr_f, dc->zero,
521                        cpu_R(dc, a->a), cpu_R(dc, a->b));
522     return true;
523 }
524 
525 static bool trans_l_ff1(DisasContext *dc, arg_da *a)
526 {
527     check_r0_write(dc, a->d);
528     tcg_gen_ctzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), -1);
529     tcg_gen_addi_tl(cpu_R(dc, a->d), cpu_R(dc, a->d), 1);
530     return true;
531 }
532 
533 static bool trans_l_fl1(DisasContext *dc, arg_da *a)
534 {
535     check_r0_write(dc, a->d);
536     tcg_gen_clzi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), TARGET_LONG_BITS);
537     tcg_gen_subfi_tl(cpu_R(dc, a->d), TARGET_LONG_BITS, cpu_R(dc, a->d));
538     return true;
539 }
540 
541 static bool trans_l_mul(DisasContext *dc, arg_dab *a)
542 {
543     check_r0_write(dc, a->d);
544     gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
545     return true;
546 }
547 
548 static bool trans_l_mulu(DisasContext *dc, arg_dab *a)
549 {
550     check_r0_write(dc, a->d);
551     gen_mulu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
552     return true;
553 }
554 
555 static bool trans_l_div(DisasContext *dc, arg_dab *a)
556 {
557     check_r0_write(dc, a->d);
558     gen_div(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
559     return true;
560 }
561 
562 static bool trans_l_divu(DisasContext *dc, arg_dab *a)
563 {
564     check_r0_write(dc, a->d);
565     gen_divu(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), cpu_R(dc, a->b));
566     return true;
567 }
568 
569 static bool trans_l_muld(DisasContext *dc, arg_ab *a)
570 {
571     gen_muld(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
572     return true;
573 }
574 
575 static bool trans_l_muldu(DisasContext *dc, arg_ab *a)
576 {
577     gen_muldu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
578     return true;
579 }
580 
581 static bool trans_l_j(DisasContext *dc, arg_l_j *a)
582 {
583     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
584 
585     tcg_gen_movi_tl(jmp_pc, tmp_pc);
586     dc->jmp_pc_imm = tmp_pc;
587     dc->delayed_branch = 2;
588     return true;
589 }
590 
591 static bool trans_l_jal(DisasContext *dc, arg_l_jal *a)
592 {
593     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
594     target_ulong ret_pc = dc->base.pc_next + 8;
595 
596     tcg_gen_movi_tl(cpu_regs[9], ret_pc);
597     /* Optimize jal being used to load the PC for PIC.  */
598     if (tmp_pc != ret_pc) {
599         tcg_gen_movi_tl(jmp_pc, tmp_pc);
600         dc->jmp_pc_imm = tmp_pc;
601         dc->delayed_branch = 2;
602     }
603     return true;
604 }
605 
606 static void do_bf(DisasContext *dc, arg_l_bf *a, TCGCond cond)
607 {
608     target_ulong tmp_pc = dc->base.pc_next + a->n * 4;
609     TCGv t_next = tcg_constant_tl(dc->base.pc_next + 8);
610     TCGv t_true = tcg_constant_tl(tmp_pc);
611 
612     tcg_gen_movcond_tl(cond, jmp_pc, cpu_sr_f, dc->zero, t_true, t_next);
613     dc->delayed_branch = 2;
614 }
615 
616 static bool trans_l_bf(DisasContext *dc, arg_l_bf *a)
617 {
618     do_bf(dc, a, TCG_COND_NE);
619     return true;
620 }
621 
622 static bool trans_l_bnf(DisasContext *dc, arg_l_bf *a)
623 {
624     do_bf(dc, a, TCG_COND_EQ);
625     return true;
626 }
627 
628 static bool trans_l_jr(DisasContext *dc, arg_l_jr *a)
629 {
630     tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b));
631     dc->delayed_branch = 2;
632     return true;
633 }
634 
635 static bool trans_l_jalr(DisasContext *dc, arg_l_jalr *a)
636 {
637     tcg_gen_mov_tl(jmp_pc, cpu_R(dc, a->b));
638     tcg_gen_movi_tl(cpu_regs[9], dc->base.pc_next + 8);
639     dc->delayed_branch = 2;
640     return true;
641 }
642 
643 static bool trans_l_lwa(DisasContext *dc, arg_load *a)
644 {
645     TCGv ea;
646 
647     check_r0_write(dc, a->d);
648     ea = tcg_temp_new();
649     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
650     tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, MO_TEUL);
651     tcg_gen_mov_tl(cpu_lock_addr, ea);
652     tcg_gen_mov_tl(cpu_lock_value, cpu_R(dc, a->d));
653     return true;
654 }
655 
656 static void do_load(DisasContext *dc, arg_load *a, MemOp mop)
657 {
658     TCGv ea;
659 
660     check_r0_write(dc, a->d);
661     ea = tcg_temp_new();
662     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
663     tcg_gen_qemu_ld_tl(cpu_R(dc, a->d), ea, dc->mem_idx, mop);
664 }
665 
666 static bool trans_l_lwz(DisasContext *dc, arg_load *a)
667 {
668     do_load(dc, a, MO_TEUL);
669     return true;
670 }
671 
672 static bool trans_l_lws(DisasContext *dc, arg_load *a)
673 {
674     do_load(dc, a, MO_TESL);
675     return true;
676 }
677 
678 static bool trans_l_lbz(DisasContext *dc, arg_load *a)
679 {
680     do_load(dc, a, MO_UB);
681     return true;
682 }
683 
684 static bool trans_l_lbs(DisasContext *dc, arg_load *a)
685 {
686     do_load(dc, a, MO_SB);
687     return true;
688 }
689 
690 static bool trans_l_lhz(DisasContext *dc, arg_load *a)
691 {
692     do_load(dc, a, MO_TEUW);
693     return true;
694 }
695 
696 static bool trans_l_lhs(DisasContext *dc, arg_load *a)
697 {
698     do_load(dc, a, MO_TESW);
699     return true;
700 }
701 
702 static bool trans_l_swa(DisasContext *dc, arg_store *a)
703 {
704     TCGv ea, val;
705     TCGLabel *lab_fail, *lab_done;
706 
707     ea = tcg_temp_new();
708     tcg_gen_addi_tl(ea, cpu_R(dc, a->a), a->i);
709 
710     lab_fail = gen_new_label();
711     lab_done = gen_new_label();
712     tcg_gen_brcond_tl(TCG_COND_NE, ea, cpu_lock_addr, lab_fail);
713 
714     val = tcg_temp_new();
715     tcg_gen_atomic_cmpxchg_tl(val, cpu_lock_addr, cpu_lock_value,
716                               cpu_R(dc, a->b), dc->mem_idx, MO_TEUL);
717     tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f, val, cpu_lock_value);
718 
719     tcg_gen_br(lab_done);
720 
721     gen_set_label(lab_fail);
722     tcg_gen_movi_tl(cpu_sr_f, 0);
723 
724     gen_set_label(lab_done);
725     tcg_gen_movi_tl(cpu_lock_addr, -1);
726     return true;
727 }
728 
729 static void do_store(DisasContext *dc, arg_store *a, MemOp mop)
730 {
731     TCGv t0 = tcg_temp_new();
732     tcg_gen_addi_tl(t0, cpu_R(dc, a->a), a->i);
733     tcg_gen_qemu_st_tl(cpu_R(dc, a->b), t0, dc->mem_idx, mop);
734 }
735 
736 static bool trans_l_sw(DisasContext *dc, arg_store *a)
737 {
738     do_store(dc, a, MO_TEUL);
739     return true;
740 }
741 
742 static bool trans_l_sb(DisasContext *dc, arg_store *a)
743 {
744     do_store(dc, a, MO_UB);
745     return true;
746 }
747 
748 static bool trans_l_sh(DisasContext *dc, arg_store *a)
749 {
750     do_store(dc, a, MO_TEUW);
751     return true;
752 }
753 
754 static bool trans_l_nop(DisasContext *dc, arg_l_nop *a)
755 {
756     return true;
757 }
758 
759 static bool trans_l_adrp(DisasContext *dc, arg_l_adrp *a)
760 {
761     if (!check_v1_3(dc)) {
762         return false;
763     }
764     check_r0_write(dc, a->d);
765 
766     tcg_gen_movi_i32(cpu_R(dc, a->d),
767                      (dc->base.pc_next & TARGET_PAGE_MASK) +
768                      ((target_long)a->i << TARGET_PAGE_BITS));
769     return true;
770 }
771 
772 static bool trans_l_addi(DisasContext *dc, arg_rri *a)
773 {
774     check_r0_write(dc, a->d);
775     gen_add(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i));
776     return true;
777 }
778 
779 static bool trans_l_addic(DisasContext *dc, arg_rri *a)
780 {
781     check_r0_write(dc, a->d);
782     gen_addc(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i));
783     return true;
784 }
785 
786 static bool trans_l_muli(DisasContext *dc, arg_rri *a)
787 {
788     check_r0_write(dc, a->d);
789     gen_mul(dc, cpu_R(dc, a->d), cpu_R(dc, a->a), tcg_constant_tl(a->i));
790     return true;
791 }
792 
793 static bool trans_l_maci(DisasContext *dc, arg_l_maci *a)
794 {
795     gen_mac(dc, cpu_R(dc, a->a), tcg_constant_tl(a->i));
796     return true;
797 }
798 
799 static bool trans_l_andi(DisasContext *dc, arg_rrk *a)
800 {
801     check_r0_write(dc, a->d);
802     tcg_gen_andi_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k);
803     return true;
804 }
805 
806 static bool trans_l_ori(DisasContext *dc, arg_rrk *a)
807 {
808     check_r0_write(dc, a->d);
809     tcg_gen_ori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->k);
810     return true;
811 }
812 
813 static bool trans_l_xori(DisasContext *dc, arg_rri *a)
814 {
815     check_r0_write(dc, a->d);
816     tcg_gen_xori_tl(cpu_R(dc, a->d), cpu_R(dc, a->a), a->i);
817     return true;
818 }
819 
820 static bool trans_l_mfspr(DisasContext *dc, arg_l_mfspr *a)
821 {
822     check_r0_write(dc, a->d);
823 
824     if (is_user(dc)) {
825         gen_illegal_exception(dc);
826     } else {
827         TCGv spr = tcg_temp_new();
828 
829         if (tb_cflags(dc->base.tb) & CF_USE_ICOUNT) {
830             gen_io_start();
831             if (dc->delayed_branch) {
832                 tcg_gen_mov_tl(cpu_pc, jmp_pc);
833                 tcg_gen_discard_tl(jmp_pc);
834             } else {
835                 tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);
836             }
837             dc->base.is_jmp = DISAS_EXIT;
838         }
839 
840         tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
841         gen_helper_mfspr(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d), spr);
842     }
843     return true;
844 }
845 
846 static bool trans_l_mtspr(DisasContext *dc, arg_l_mtspr *a)
847 {
848     if (is_user(dc)) {
849         gen_illegal_exception(dc);
850     } else {
851         TCGv spr;
852 
853         if (tb_cflags(dc->base.tb) & CF_USE_ICOUNT) {
854             gen_io_start();
855         }
856         /* For SR, we will need to exit the TB to recognize the new
857          * exception state.  For NPC, in theory this counts as a branch
858          * (although the SPR only exists for use by an ICE).  Save all
859          * of the cpu state first, allowing it to be overwritten.
860          */
861         if (dc->delayed_branch) {
862             tcg_gen_mov_tl(cpu_pc, jmp_pc);
863             tcg_gen_discard_tl(jmp_pc);
864         } else {
865             tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);
866         }
867         dc->base.is_jmp = DISAS_EXIT;
868 
869         spr = tcg_temp_new();
870         tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
871         gen_helper_mtspr(cpu_env, spr, cpu_R(dc, a->b));
872     }
873     return true;
874 }
875 
876 static bool trans_l_mac(DisasContext *dc, arg_ab *a)
877 {
878     gen_mac(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
879     return true;
880 }
881 
882 static bool trans_l_msb(DisasContext *dc, arg_ab *a)
883 {
884     gen_msb(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
885     return true;
886 }
887 
888 static bool trans_l_macu(DisasContext *dc, arg_ab *a)
889 {
890     gen_macu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
891     return true;
892 }
893 
894 static bool trans_l_msbu(DisasContext *dc, arg_ab *a)
895 {
896     gen_msbu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
897     return true;
898 }
899 
900 static bool trans_l_slli(DisasContext *dc, arg_dal *a)
901 {
902     check_r0_write(dc, a->d);
903     tcg_gen_shli_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
904                     a->l & (TARGET_LONG_BITS - 1));
905     return true;
906 }
907 
908 static bool trans_l_srli(DisasContext *dc, arg_dal *a)
909 {
910     check_r0_write(dc, a->d);
911     tcg_gen_shri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
912                     a->l & (TARGET_LONG_BITS - 1));
913     return true;
914 }
915 
916 static bool trans_l_srai(DisasContext *dc, arg_dal *a)
917 {
918     check_r0_write(dc, a->d);
919     tcg_gen_sari_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
920                     a->l & (TARGET_LONG_BITS - 1));
921     return true;
922 }
923 
924 static bool trans_l_rori(DisasContext *dc, arg_dal *a)
925 {
926     check_r0_write(dc, a->d);
927     tcg_gen_rotri_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
928                      a->l & (TARGET_LONG_BITS - 1));
929     return true;
930 }
931 
932 static bool trans_l_movhi(DisasContext *dc, arg_l_movhi *a)
933 {
934     check_r0_write(dc, a->d);
935     tcg_gen_movi_tl(cpu_R(dc, a->d), a->k << 16);
936     return true;
937 }
938 
939 static bool trans_l_macrc(DisasContext *dc, arg_l_macrc *a)
940 {
941     check_r0_write(dc, a->d);
942     tcg_gen_trunc_i64_tl(cpu_R(dc, a->d), cpu_mac);
943     tcg_gen_movi_i64(cpu_mac, 0);
944     return true;
945 }
946 
947 static bool trans_l_sfeq(DisasContext *dc, arg_ab *a)
948 {
949     tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f,
950                        cpu_R(dc, a->a), cpu_R(dc, a->b));
951     return true;
952 }
953 
954 static bool trans_l_sfne(DisasContext *dc, arg_ab *a)
955 {
956     tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_f,
957                        cpu_R(dc, a->a), cpu_R(dc, a->b));
958     return true;
959 }
960 
961 static bool trans_l_sfgtu(DisasContext *dc, arg_ab *a)
962 {
963     tcg_gen_setcond_tl(TCG_COND_GTU, cpu_sr_f,
964                        cpu_R(dc, a->a), cpu_R(dc, a->b));
965     return true;
966 }
967 
968 static bool trans_l_sfgeu(DisasContext *dc, arg_ab *a)
969 {
970     tcg_gen_setcond_tl(TCG_COND_GEU, cpu_sr_f,
971                        cpu_R(dc, a->a), cpu_R(dc, a->b));
972     return true;
973 }
974 
975 static bool trans_l_sfltu(DisasContext *dc, arg_ab *a)
976 {
977     tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_f,
978                        cpu_R(dc, a->a), cpu_R(dc, a->b));
979     return true;
980 }
981 
982 static bool trans_l_sfleu(DisasContext *dc, arg_ab *a)
983 {
984     tcg_gen_setcond_tl(TCG_COND_LEU, cpu_sr_f,
985                        cpu_R(dc, a->a), cpu_R(dc, a->b));
986     return true;
987 }
988 
989 static bool trans_l_sfgts(DisasContext *dc, arg_ab *a)
990 {
991     tcg_gen_setcond_tl(TCG_COND_GT, cpu_sr_f,
992                        cpu_R(dc, a->a), cpu_R(dc, a->b));
993     return true;
994 }
995 
996 static bool trans_l_sfges(DisasContext *dc, arg_ab *a)
997 {
998     tcg_gen_setcond_tl(TCG_COND_GE, cpu_sr_f,
999                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1000     return true;
1001 }
1002 
1003 static bool trans_l_sflts(DisasContext *dc, arg_ab *a)
1004 {
1005     tcg_gen_setcond_tl(TCG_COND_LT, cpu_sr_f,
1006                        cpu_R(dc, a->a), cpu_R(dc, a->b));
1007     return true;
1008 }
1009 
1010 static bool trans_l_sfles(DisasContext *dc, arg_ab *a)
1011 {
1012     tcg_gen_setcond_tl(TCG_COND_LE,
1013                        cpu_sr_f, cpu_R(dc, a->a), cpu_R(dc, a->b));
1014     return true;
1015 }
1016 
1017 static bool trans_l_sfeqi(DisasContext *dc, arg_ai *a)
1018 {
1019     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_f, cpu_R(dc, a->a), a->i);
1020     return true;
1021 }
1022 
1023 static bool trans_l_sfnei(DisasContext *dc, arg_ai *a)
1024 {
1025     tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1026     return true;
1027 }
1028 
1029 static bool trans_l_sfgtui(DisasContext *dc, arg_ai *a)
1030 {
1031     tcg_gen_setcondi_tl(TCG_COND_GTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1032     return true;
1033 }
1034 
1035 static bool trans_l_sfgeui(DisasContext *dc, arg_ai *a)
1036 {
1037     tcg_gen_setcondi_tl(TCG_COND_GEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1038     return true;
1039 }
1040 
1041 static bool trans_l_sfltui(DisasContext *dc, arg_ai *a)
1042 {
1043     tcg_gen_setcondi_tl(TCG_COND_LTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1044     return true;
1045 }
1046 
1047 static bool trans_l_sfleui(DisasContext *dc, arg_ai *a)
1048 {
1049     tcg_gen_setcondi_tl(TCG_COND_LEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1050     return true;
1051 }
1052 
1053 static bool trans_l_sfgtsi(DisasContext *dc, arg_ai *a)
1054 {
1055     tcg_gen_setcondi_tl(TCG_COND_GT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1056     return true;
1057 }
1058 
1059 static bool trans_l_sfgesi(DisasContext *dc, arg_ai *a)
1060 {
1061     tcg_gen_setcondi_tl(TCG_COND_GE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1062     return true;
1063 }
1064 
1065 static bool trans_l_sfltsi(DisasContext *dc, arg_ai *a)
1066 {
1067     tcg_gen_setcondi_tl(TCG_COND_LT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1068     return true;
1069 }
1070 
1071 static bool trans_l_sflesi(DisasContext *dc, arg_ai *a)
1072 {
1073     tcg_gen_setcondi_tl(TCG_COND_LE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1074     return true;
1075 }
1076 
1077 static bool trans_l_sys(DisasContext *dc, arg_l_sys *a)
1078 {
1079     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1080     gen_exception(dc, EXCP_SYSCALL);
1081     dc->base.is_jmp = DISAS_NORETURN;
1082     return true;
1083 }
1084 
1085 static bool trans_l_trap(DisasContext *dc, arg_l_trap *a)
1086 {
1087     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1088     gen_exception(dc, EXCP_TRAP);
1089     dc->base.is_jmp = DISAS_NORETURN;
1090     return true;
1091 }
1092 
1093 static bool trans_l_msync(DisasContext *dc, arg_l_msync *a)
1094 {
1095     tcg_gen_mb(TCG_MO_ALL);
1096     return true;
1097 }
1098 
1099 static bool trans_l_psync(DisasContext *dc, arg_l_psync *a)
1100 {
1101     return true;
1102 }
1103 
1104 static bool trans_l_csync(DisasContext *dc, arg_l_csync *a)
1105 {
1106     return true;
1107 }
1108 
1109 static bool trans_l_rfe(DisasContext *dc, arg_l_rfe *a)
1110 {
1111     if (is_user(dc)) {
1112         gen_illegal_exception(dc);
1113     } else {
1114         gen_helper_rfe(cpu_env);
1115         dc->base.is_jmp = DISAS_EXIT;
1116     }
1117     return true;
1118 }
1119 
1120 static bool do_fp2(DisasContext *dc, arg_da *a,
1121                    void (*fn)(TCGv, TCGv_env, TCGv))
1122 {
1123     if (!check_of32s(dc)) {
1124         return false;
1125     }
1126     check_r0_write(dc, a->d);
1127     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a));
1128     gen_helper_update_fpcsr(cpu_env);
1129     return true;
1130 }
1131 
1132 static bool do_fp3(DisasContext *dc, arg_dab *a,
1133                    void (*fn)(TCGv, TCGv_env, TCGv, TCGv))
1134 {
1135     if (!check_of32s(dc)) {
1136         return false;
1137     }
1138     check_r0_write(dc, a->d);
1139     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1140     gen_helper_update_fpcsr(cpu_env);
1141     return true;
1142 }
1143 
1144 static bool do_fpcmp(DisasContext *dc, arg_ab *a,
1145                      void (*fn)(TCGv, TCGv_env, TCGv, TCGv),
1146                      bool inv, bool swap)
1147 {
1148     if (!check_of32s(dc)) {
1149         return false;
1150     }
1151     if (swap) {
1152         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->b), cpu_R(dc, a->a));
1153     } else {
1154         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1155     }
1156     if (inv) {
1157         tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1);
1158     }
1159     gen_helper_update_fpcsr(cpu_env);
1160     return true;
1161 }
1162 
1163 static bool trans_lf_add_s(DisasContext *dc, arg_dab *a)
1164 {
1165     return do_fp3(dc, a, gen_helper_float_add_s);
1166 }
1167 
1168 static bool trans_lf_sub_s(DisasContext *dc, arg_dab *a)
1169 {
1170     return do_fp3(dc, a, gen_helper_float_sub_s);
1171 }
1172 
1173 static bool trans_lf_mul_s(DisasContext *dc, arg_dab *a)
1174 {
1175     return do_fp3(dc, a, gen_helper_float_mul_s);
1176 }
1177 
1178 static bool trans_lf_div_s(DisasContext *dc, arg_dab *a)
1179 {
1180     return do_fp3(dc, a, gen_helper_float_div_s);
1181 }
1182 
1183 static bool trans_lf_rem_s(DisasContext *dc, arg_dab *a)
1184 {
1185     return do_fp3(dc, a, gen_helper_float_rem_s);
1186     return true;
1187 }
1188 
1189 static bool trans_lf_itof_s(DisasContext *dc, arg_da *a)
1190 {
1191     return do_fp2(dc, a, gen_helper_itofs);
1192 }
1193 
1194 static bool trans_lf_ftoi_s(DisasContext *dc, arg_da *a)
1195 {
1196     return do_fp2(dc, a, gen_helper_ftois);
1197 }
1198 
1199 static bool trans_lf_madd_s(DisasContext *dc, arg_dab *a)
1200 {
1201     if (!check_of32s(dc)) {
1202         return false;
1203     }
1204     check_r0_write(dc, a->d);
1205     gen_helper_float_madd_s(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d),
1206                             cpu_R(dc, a->a), cpu_R(dc, a->b));
1207     gen_helper_update_fpcsr(cpu_env);
1208     return true;
1209 }
1210 
1211 static bool trans_lf_sfeq_s(DisasContext *dc, arg_ab *a)
1212 {
1213     return do_fpcmp(dc, a, gen_helper_float_eq_s, false, false);
1214 }
1215 
1216 static bool trans_lf_sfne_s(DisasContext *dc, arg_ab *a)
1217 {
1218     return do_fpcmp(dc, a, gen_helper_float_eq_s, true, false);
1219 }
1220 
1221 static bool trans_lf_sfgt_s(DisasContext *dc, arg_ab *a)
1222 {
1223     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, true);
1224 }
1225 
1226 static bool trans_lf_sfge_s(DisasContext *dc, arg_ab *a)
1227 {
1228     return do_fpcmp(dc, a, gen_helper_float_le_s, false, true);
1229 }
1230 
1231 static bool trans_lf_sflt_s(DisasContext *dc, arg_ab *a)
1232 {
1233     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, false);
1234 }
1235 
1236 static bool trans_lf_sfle_s(DisasContext *dc, arg_ab *a)
1237 {
1238     return do_fpcmp(dc, a, gen_helper_float_le_s, false, false);
1239 }
1240 
1241 static bool trans_lf_sfueq_s(DisasContext *dc, arg_ab *a)
1242 {
1243     if (!check_v1_3(dc)) {
1244         return false;
1245     }
1246     return do_fpcmp(dc, a, gen_helper_float_ueq_s, false, false);
1247 }
1248 
1249 static bool trans_lf_sfult_s(DisasContext *dc, arg_ab *a)
1250 {
1251     if (!check_v1_3(dc)) {
1252         return false;
1253     }
1254     return do_fpcmp(dc, a, gen_helper_float_ult_s, false, false);
1255 }
1256 
1257 static bool trans_lf_sfugt_s(DisasContext *dc, arg_ab *a)
1258 {
1259     if (!check_v1_3(dc)) {
1260         return false;
1261     }
1262     return do_fpcmp(dc, a, gen_helper_float_ult_s, false, true);
1263 }
1264 
1265 static bool trans_lf_sfule_s(DisasContext *dc, arg_ab *a)
1266 {
1267     if (!check_v1_3(dc)) {
1268         return false;
1269     }
1270     return do_fpcmp(dc, a, gen_helper_float_ule_s, false, false);
1271 }
1272 
1273 static bool trans_lf_sfuge_s(DisasContext *dc, arg_ab *a)
1274 {
1275     if (!check_v1_3(dc)) {
1276         return false;
1277     }
1278     return do_fpcmp(dc, a, gen_helper_float_ule_s, false, true);
1279 }
1280 
1281 static bool trans_lf_sfun_s(DisasContext *dc, arg_ab *a)
1282 {
1283     if (!check_v1_3(dc)) {
1284         return false;
1285     }
1286     return do_fpcmp(dc, a, gen_helper_float_un_s, false, false);
1287 }
1288 
1289 static bool check_pair(DisasContext *dc, int r, int p)
1290 {
1291     return r + 1 + p < 32;
1292 }
1293 
1294 static void load_pair(DisasContext *dc, TCGv_i64 t, int r, int p)
1295 {
1296     tcg_gen_concat_i32_i64(t, cpu_R(dc, r + 1 + p), cpu_R(dc, r));
1297 }
1298 
1299 static void save_pair(DisasContext *dc, TCGv_i64 t, int r, int p)
1300 {
1301     tcg_gen_extr_i64_i32(cpu_R(dc, r + 1 + p), cpu_R(dc, r), t);
1302 }
1303 
1304 static bool do_dp3(DisasContext *dc, arg_dab_pair *a,
1305                    void (*fn)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1306 {
1307     TCGv_i64 t0, t1;
1308 
1309     if (!check_of64a32s(dc) ||
1310         !check_pair(dc, a->a, a->ap) ||
1311         !check_pair(dc, a->b, a->bp) ||
1312         !check_pair(dc, a->d, a->dp)) {
1313         return false;
1314     }
1315     check_r0_write(dc, a->d);
1316 
1317     t0 = tcg_temp_new_i64();
1318     t1 = tcg_temp_new_i64();
1319     load_pair(dc, t0, a->a, a->ap);
1320     load_pair(dc, t1, a->b, a->bp);
1321     fn(t0, cpu_env, t0, t1);
1322     save_pair(dc, t0, a->d, a->dp);
1323 
1324     gen_helper_update_fpcsr(cpu_env);
1325     return true;
1326 }
1327 
1328 static bool do_dp2(DisasContext *dc, arg_da_pair *a,
1329                    void (*fn)(TCGv_i64, TCGv_env, TCGv_i64))
1330 {
1331     TCGv_i64 t0;
1332 
1333     if (!check_of64a32s(dc) ||
1334         !check_pair(dc, a->a, a->ap) ||
1335         !check_pair(dc, a->d, a->dp)) {
1336         return false;
1337     }
1338     check_r0_write(dc, a->d);
1339 
1340     t0 = tcg_temp_new_i64();
1341     load_pair(dc, t0, a->a, a->ap);
1342     fn(t0, cpu_env, t0);
1343     save_pair(dc, t0, a->d, a->dp);
1344 
1345     gen_helper_update_fpcsr(cpu_env);
1346     return true;
1347 }
1348 
1349 static bool do_dpcmp(DisasContext *dc, arg_ab_pair *a,
1350                      void (*fn)(TCGv, TCGv_env, TCGv_i64, TCGv_i64),
1351                      bool inv, bool swap)
1352 {
1353     TCGv_i64 t0, t1;
1354 
1355     if (!check_of64a32s(dc) ||
1356         !check_pair(dc, a->a, a->ap) ||
1357         !check_pair(dc, a->b, a->bp)) {
1358         return false;
1359     }
1360 
1361     t0 = tcg_temp_new_i64();
1362     t1 = tcg_temp_new_i64();
1363     load_pair(dc, t0, a->a, a->ap);
1364     load_pair(dc, t1, a->b, a->bp);
1365     if (swap) {
1366         fn(cpu_sr_f, cpu_env, t1, t0);
1367     } else {
1368         fn(cpu_sr_f, cpu_env, t0, t1);
1369     }
1370 
1371     if (inv) {
1372         tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1);
1373     }
1374     gen_helper_update_fpcsr(cpu_env);
1375     return true;
1376 }
1377 
1378 static bool trans_lf_add_d(DisasContext *dc, arg_dab_pair *a)
1379 {
1380     return do_dp3(dc, a, gen_helper_float_add_d);
1381 }
1382 
1383 static bool trans_lf_sub_d(DisasContext *dc, arg_dab_pair *a)
1384 {
1385     return do_dp3(dc, a, gen_helper_float_sub_d);
1386 }
1387 
1388 static bool trans_lf_mul_d(DisasContext *dc, arg_dab_pair *a)
1389 {
1390     return do_dp3(dc, a, gen_helper_float_mul_d);
1391 }
1392 
1393 static bool trans_lf_div_d(DisasContext *dc, arg_dab_pair *a)
1394 {
1395     return do_dp3(dc, a, gen_helper_float_div_d);
1396 }
1397 
1398 static bool trans_lf_rem_d(DisasContext *dc, arg_dab_pair *a)
1399 {
1400     return do_dp3(dc, a, gen_helper_float_rem_d);
1401 }
1402 
1403 static bool trans_lf_itof_d(DisasContext *dc, arg_da_pair *a)
1404 {
1405     return do_dp2(dc, a, gen_helper_itofd);
1406 }
1407 
1408 static bool trans_lf_ftoi_d(DisasContext *dc, arg_da_pair *a)
1409 {
1410     return do_dp2(dc, a, gen_helper_ftoid);
1411 }
1412 
1413 static bool trans_lf_stod_d(DisasContext *dc, arg_lf_stod_d *a)
1414 {
1415     TCGv_i64 t0;
1416 
1417     if (!check_of64a32s(dc) ||
1418         !check_pair(dc, a->d, a->dp)) {
1419         return false;
1420     }
1421     check_r0_write(dc, a->d);
1422 
1423     t0 = tcg_temp_new_i64();
1424     gen_helper_stod(t0, cpu_env, cpu_R(dc, a->a));
1425     save_pair(dc, t0, a->d, a->dp);
1426 
1427     gen_helper_update_fpcsr(cpu_env);
1428     return true;
1429 }
1430 
1431 static bool trans_lf_dtos_d(DisasContext *dc, arg_lf_dtos_d *a)
1432 {
1433     TCGv_i64 t0;
1434 
1435     if (!check_of64a32s(dc) ||
1436         !check_pair(dc, a->a, a->ap)) {
1437         return false;
1438     }
1439     check_r0_write(dc, a->d);
1440 
1441     t0 = tcg_temp_new_i64();
1442     load_pair(dc, t0, a->a, a->ap);
1443     gen_helper_dtos(cpu_R(dc, a->d), cpu_env, t0);
1444 
1445     gen_helper_update_fpcsr(cpu_env);
1446     return true;
1447 }
1448 
1449 static bool trans_lf_madd_d(DisasContext *dc, arg_dab_pair *a)
1450 {
1451     TCGv_i64 t0, t1, t2;
1452 
1453     if (!check_of64a32s(dc) ||
1454         !check_pair(dc, a->a, a->ap) ||
1455         !check_pair(dc, a->b, a->bp) ||
1456         !check_pair(dc, a->d, a->dp)) {
1457         return false;
1458     }
1459     check_r0_write(dc, a->d);
1460 
1461     t0 = tcg_temp_new_i64();
1462     t1 = tcg_temp_new_i64();
1463     t2 = tcg_temp_new_i64();
1464     load_pair(dc, t0, a->d, a->dp);
1465     load_pair(dc, t1, a->a, a->ap);
1466     load_pair(dc, t2, a->b, a->bp);
1467     gen_helper_float_madd_d(t0, cpu_env, t0, t1, t2);
1468     save_pair(dc, t0, a->d, a->dp);
1469 
1470     gen_helper_update_fpcsr(cpu_env);
1471     return true;
1472 }
1473 
1474 static bool trans_lf_sfeq_d(DisasContext *dc, arg_ab_pair *a)
1475 {
1476     return do_dpcmp(dc, a, gen_helper_float_eq_d, false, false);
1477 }
1478 
1479 static bool trans_lf_sfne_d(DisasContext *dc, arg_ab_pair *a)
1480 {
1481     return do_dpcmp(dc, a, gen_helper_float_eq_d, true, false);
1482 }
1483 
1484 static bool trans_lf_sfgt_d(DisasContext *dc, arg_ab_pair *a)
1485 {
1486     return do_dpcmp(dc, a, gen_helper_float_lt_d, false, true);
1487 }
1488 
1489 static bool trans_lf_sfge_d(DisasContext *dc, arg_ab_pair *a)
1490 {
1491     return do_dpcmp(dc, a, gen_helper_float_le_d, false, true);
1492 }
1493 
1494 static bool trans_lf_sflt_d(DisasContext *dc, arg_ab_pair *a)
1495 {
1496     return do_dpcmp(dc, a, gen_helper_float_lt_d, false, false);
1497 }
1498 
1499 static bool trans_lf_sfle_d(DisasContext *dc, arg_ab_pair *a)
1500 {
1501     return do_dpcmp(dc, a, gen_helper_float_le_d, false, false);
1502 }
1503 
1504 static bool trans_lf_sfueq_d(DisasContext *dc, arg_ab_pair *a)
1505 {
1506     return do_dpcmp(dc, a, gen_helper_float_ueq_d, false, false);
1507 }
1508 
1509 static bool trans_lf_sfule_d(DisasContext *dc, arg_ab_pair *a)
1510 {
1511     return do_dpcmp(dc, a, gen_helper_float_ule_d, false, false);
1512 }
1513 
1514 static bool trans_lf_sfuge_d(DisasContext *dc, arg_ab_pair *a)
1515 {
1516     return do_dpcmp(dc, a, gen_helper_float_ule_d, false, true);
1517 }
1518 
1519 static bool trans_lf_sfult_d(DisasContext *dc, arg_ab_pair *a)
1520 {
1521     return do_dpcmp(dc, a, gen_helper_float_ult_d, false, false);
1522 }
1523 
1524 static bool trans_lf_sfugt_d(DisasContext *dc, arg_ab_pair *a)
1525 {
1526     return do_dpcmp(dc, a, gen_helper_float_ult_d, false, true);
1527 }
1528 
1529 static bool trans_lf_sfun_d(DisasContext *dc, arg_ab_pair *a)
1530 {
1531     return do_dpcmp(dc, a, gen_helper_float_un_d, false, false);
1532 }
1533 
1534 static void openrisc_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1535 {
1536     DisasContext *dc = container_of(dcb, DisasContext, base);
1537     CPUOpenRISCState *env = cs->env_ptr;
1538     int bound;
1539 
1540     dc->mem_idx = cpu_mmu_index(env, false);
1541     dc->tb_flags = dc->base.tb->flags;
1542     dc->delayed_branch = (dc->tb_flags & TB_FLAGS_DFLAG) != 0;
1543     dc->cpucfgr = env->cpucfgr;
1544     dc->avr = env->avr;
1545     dc->jmp_pc_imm = -1;
1546 
1547     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1548     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1549 }
1550 
1551 static void openrisc_tr_tb_start(DisasContextBase *db, CPUState *cs)
1552 {
1553     DisasContext *dc = container_of(db, DisasContext, base);
1554 
1555     /* Allow the TCG optimizer to see that R0 == 0,
1556        when it's true, which is the common case.  */
1557     dc->zero = tcg_constant_tl(0);
1558     if (dc->tb_flags & TB_FLAGS_R0_0) {
1559         dc->R0 = dc->zero;
1560     } else {
1561         dc->R0 = cpu_regs[0];
1562     }
1563 }
1564 
1565 static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
1566 {
1567     DisasContext *dc = container_of(dcbase, DisasContext, base);
1568 
1569     tcg_gen_insn_start(dc->base.pc_next, (dc->delayed_branch ? 1 : 0)
1570                        | (dc->base.num_insns > 1 ? 2 : 0));
1571 }
1572 
1573 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
1574 {
1575     DisasContext *dc = container_of(dcbase, DisasContext, base);
1576     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1577     uint32_t insn = translator_ldl(&cpu->env, &dc->base, dc->base.pc_next);
1578 
1579     if (!decode(dc, insn)) {
1580         gen_illegal_exception(dc);
1581     }
1582     dc->base.pc_next += 4;
1583 
1584     /* When exiting the delay slot normally, exit via jmp_pc.
1585      * For DISAS_NORETURN, we have raised an exception and already exited.
1586      * For DISAS_EXIT, we found l.rfe in a delay slot.  There's nothing
1587      * in the manual saying this is illegal, but it surely it should.
1588      * At least or1ksim overrides pcnext and ignores the branch.
1589      */
1590     if (dc->delayed_branch
1591         && --dc->delayed_branch == 0
1592         && dc->base.is_jmp == DISAS_NEXT) {
1593         dc->base.is_jmp = DISAS_JUMP;
1594     }
1595 }
1596 
1597 static void openrisc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
1598 {
1599     DisasContext *dc = container_of(dcbase, DisasContext, base);
1600     target_ulong jmp_dest;
1601 
1602     /* If we have already exited the TB, nothing following has effect.  */
1603     if (dc->base.is_jmp == DISAS_NORETURN) {
1604         return;
1605     }
1606 
1607     /* Adjust the delayed branch state for the next TB.  */
1608     if ((dc->tb_flags & TB_FLAGS_DFLAG ? 1 : 0) != (dc->delayed_branch != 0)) {
1609         tcg_gen_movi_i32(cpu_dflag, dc->delayed_branch != 0);
1610     }
1611 
1612     /* For DISAS_TOO_MANY, jump to the next insn.  */
1613     jmp_dest = dc->base.pc_next;
1614     tcg_gen_movi_tl(cpu_ppc, jmp_dest - 4);
1615 
1616     switch (dc->base.is_jmp) {
1617     case DISAS_JUMP:
1618         jmp_dest = dc->jmp_pc_imm;
1619         if (jmp_dest == -1) {
1620             /* The jump destination is indirect/computed; use jmp_pc.  */
1621             tcg_gen_mov_tl(cpu_pc, jmp_pc);
1622             tcg_gen_discard_tl(jmp_pc);
1623             tcg_gen_lookup_and_goto_ptr();
1624             break;
1625         }
1626         /* The jump destination is direct; use jmp_pc_imm.
1627            However, we will have stored into jmp_pc as well;
1628            we know now that it wasn't needed.  */
1629         tcg_gen_discard_tl(jmp_pc);
1630         /* fallthru */
1631 
1632     case DISAS_TOO_MANY:
1633         if (translator_use_goto_tb(&dc->base, jmp_dest)) {
1634             tcg_gen_goto_tb(0);
1635             tcg_gen_movi_tl(cpu_pc, jmp_dest);
1636             tcg_gen_exit_tb(dc->base.tb, 0);
1637             break;
1638         }
1639         tcg_gen_movi_tl(cpu_pc, jmp_dest);
1640         tcg_gen_lookup_and_goto_ptr();
1641         break;
1642 
1643     case DISAS_EXIT:
1644         tcg_gen_exit_tb(NULL, 0);
1645         break;
1646     default:
1647         g_assert_not_reached();
1648     }
1649 }
1650 
1651 static void openrisc_tr_disas_log(const DisasContextBase *dcbase,
1652                                   CPUState *cs, FILE *logfile)
1653 {
1654     DisasContext *s = container_of(dcbase, DisasContext, base);
1655 
1656     fprintf(logfile, "IN: %s\n", lookup_symbol(s->base.pc_first));
1657     target_disas(logfile, cs, s->base.pc_first, s->base.tb->size);
1658 }
1659 
1660 static const TranslatorOps openrisc_tr_ops = {
1661     .init_disas_context = openrisc_tr_init_disas_context,
1662     .tb_start           = openrisc_tr_tb_start,
1663     .insn_start         = openrisc_tr_insn_start,
1664     .translate_insn     = openrisc_tr_translate_insn,
1665     .tb_stop            = openrisc_tr_tb_stop,
1666     .disas_log          = openrisc_tr_disas_log,
1667 };
1668 
1669 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1670                            target_ulong pc, void *host_pc)
1671 {
1672     DisasContext ctx;
1673 
1674     translator_loop(cs, tb, max_insns, pc, host_pc,
1675                     &openrisc_tr_ops, &ctx.base);
1676 }
1677 
1678 void openrisc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1679 {
1680     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1681     CPUOpenRISCState *env = &cpu->env;
1682     int i;
1683 
1684     qemu_fprintf(f, "PC=%08x\n", env->pc);
1685     for (i = 0; i < 32; ++i) {
1686         qemu_fprintf(f, "R%02d=%08x%c", i, cpu_get_gpr(env, i),
1687                      (i % 4) == 3 ? '\n' : ' ');
1688     }
1689 }
1690