xref: /qemu/target/openrisc/translate.c (revision 7c1f51bf)
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     TCGv spr = tcg_temp_new();
823 
824     check_r0_write(dc, a->d);
825 
826     if (tb_cflags(dc->base.tb) & CF_USE_ICOUNT) {
827         gen_io_start();
828         if (dc->delayed_branch) {
829             tcg_gen_mov_tl(cpu_pc, jmp_pc);
830             tcg_gen_discard_tl(jmp_pc);
831         } else {
832             tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);
833         }
834         dc->base.is_jmp = DISAS_EXIT;
835     }
836 
837     tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
838     gen_helper_mfspr(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d), spr);
839     return true;
840 }
841 
842 static bool trans_l_mtspr(DisasContext *dc, arg_l_mtspr *a)
843 {
844     TCGv spr = tcg_temp_new();
845 
846     if (tb_cflags(dc->base.tb) & CF_USE_ICOUNT) {
847         gen_io_start();
848     }
849     /*
850      * For SR, we will need to exit the TB to recognize the new
851      * exception state.  For NPC, in theory this counts as a branch
852      * (although the SPR only exists for use by an ICE).  Save all
853      * of the cpu state first, allowing it to be overwritten.
854      */
855     if (dc->delayed_branch) {
856         tcg_gen_mov_tl(cpu_pc, jmp_pc);
857         tcg_gen_discard_tl(jmp_pc);
858     } else {
859         tcg_gen_movi_tl(cpu_pc, dc->base.pc_next + 4);
860     }
861     dc->base.is_jmp = DISAS_EXIT;
862 
863     tcg_gen_ori_tl(spr, cpu_R(dc, a->a), a->k);
864     gen_helper_mtspr(cpu_env, spr, cpu_R(dc, a->b));
865     return true;
866 }
867 
868 static bool trans_l_mac(DisasContext *dc, arg_ab *a)
869 {
870     gen_mac(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
871     return true;
872 }
873 
874 static bool trans_l_msb(DisasContext *dc, arg_ab *a)
875 {
876     gen_msb(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
877     return true;
878 }
879 
880 static bool trans_l_macu(DisasContext *dc, arg_ab *a)
881 {
882     gen_macu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
883     return true;
884 }
885 
886 static bool trans_l_msbu(DisasContext *dc, arg_ab *a)
887 {
888     gen_msbu(dc, cpu_R(dc, a->a), cpu_R(dc, a->b));
889     return true;
890 }
891 
892 static bool trans_l_slli(DisasContext *dc, arg_dal *a)
893 {
894     check_r0_write(dc, a->d);
895     tcg_gen_shli_tl(cpu_R(dc, a->d), cpu_R(dc, a->a),
896                     a->l & (TARGET_LONG_BITS - 1));
897     return true;
898 }
899 
900 static bool trans_l_srli(DisasContext *dc, arg_dal *a)
901 {
902     check_r0_write(dc, a->d);
903     tcg_gen_shri_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_srai(DisasContext *dc, arg_dal *a)
909 {
910     check_r0_write(dc, a->d);
911     tcg_gen_sari_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_rori(DisasContext *dc, arg_dal *a)
917 {
918     check_r0_write(dc, a->d);
919     tcg_gen_rotri_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_movhi(DisasContext *dc, arg_l_movhi *a)
925 {
926     check_r0_write(dc, a->d);
927     tcg_gen_movi_tl(cpu_R(dc, a->d), a->k << 16);
928     return true;
929 }
930 
931 static bool trans_l_macrc(DisasContext *dc, arg_l_macrc *a)
932 {
933     check_r0_write(dc, a->d);
934     tcg_gen_trunc_i64_tl(cpu_R(dc, a->d), cpu_mac);
935     tcg_gen_movi_i64(cpu_mac, 0);
936     return true;
937 }
938 
939 static bool trans_l_sfeq(DisasContext *dc, arg_ab *a)
940 {
941     tcg_gen_setcond_tl(TCG_COND_EQ, cpu_sr_f,
942                        cpu_R(dc, a->a), cpu_R(dc, a->b));
943     return true;
944 }
945 
946 static bool trans_l_sfne(DisasContext *dc, arg_ab *a)
947 {
948     tcg_gen_setcond_tl(TCG_COND_NE, cpu_sr_f,
949                        cpu_R(dc, a->a), cpu_R(dc, a->b));
950     return true;
951 }
952 
953 static bool trans_l_sfgtu(DisasContext *dc, arg_ab *a)
954 {
955     tcg_gen_setcond_tl(TCG_COND_GTU, cpu_sr_f,
956                        cpu_R(dc, a->a), cpu_R(dc, a->b));
957     return true;
958 }
959 
960 static bool trans_l_sfgeu(DisasContext *dc, arg_ab *a)
961 {
962     tcg_gen_setcond_tl(TCG_COND_GEU, cpu_sr_f,
963                        cpu_R(dc, a->a), cpu_R(dc, a->b));
964     return true;
965 }
966 
967 static bool trans_l_sfltu(DisasContext *dc, arg_ab *a)
968 {
969     tcg_gen_setcond_tl(TCG_COND_LTU, cpu_sr_f,
970                        cpu_R(dc, a->a), cpu_R(dc, a->b));
971     return true;
972 }
973 
974 static bool trans_l_sfleu(DisasContext *dc, arg_ab *a)
975 {
976     tcg_gen_setcond_tl(TCG_COND_LEU, cpu_sr_f,
977                        cpu_R(dc, a->a), cpu_R(dc, a->b));
978     return true;
979 }
980 
981 static bool trans_l_sfgts(DisasContext *dc, arg_ab *a)
982 {
983     tcg_gen_setcond_tl(TCG_COND_GT, cpu_sr_f,
984                        cpu_R(dc, a->a), cpu_R(dc, a->b));
985     return true;
986 }
987 
988 static bool trans_l_sfges(DisasContext *dc, arg_ab *a)
989 {
990     tcg_gen_setcond_tl(TCG_COND_GE, cpu_sr_f,
991                        cpu_R(dc, a->a), cpu_R(dc, a->b));
992     return true;
993 }
994 
995 static bool trans_l_sflts(DisasContext *dc, arg_ab *a)
996 {
997     tcg_gen_setcond_tl(TCG_COND_LT, cpu_sr_f,
998                        cpu_R(dc, a->a), cpu_R(dc, a->b));
999     return true;
1000 }
1001 
1002 static bool trans_l_sfles(DisasContext *dc, arg_ab *a)
1003 {
1004     tcg_gen_setcond_tl(TCG_COND_LE,
1005                        cpu_sr_f, cpu_R(dc, a->a), cpu_R(dc, a->b));
1006     return true;
1007 }
1008 
1009 static bool trans_l_sfeqi(DisasContext *dc, arg_ai *a)
1010 {
1011     tcg_gen_setcondi_tl(TCG_COND_EQ, cpu_sr_f, cpu_R(dc, a->a), a->i);
1012     return true;
1013 }
1014 
1015 static bool trans_l_sfnei(DisasContext *dc, arg_ai *a)
1016 {
1017     tcg_gen_setcondi_tl(TCG_COND_NE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1018     return true;
1019 }
1020 
1021 static bool trans_l_sfgtui(DisasContext *dc, arg_ai *a)
1022 {
1023     tcg_gen_setcondi_tl(TCG_COND_GTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1024     return true;
1025 }
1026 
1027 static bool trans_l_sfgeui(DisasContext *dc, arg_ai *a)
1028 {
1029     tcg_gen_setcondi_tl(TCG_COND_GEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1030     return true;
1031 }
1032 
1033 static bool trans_l_sfltui(DisasContext *dc, arg_ai *a)
1034 {
1035     tcg_gen_setcondi_tl(TCG_COND_LTU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1036     return true;
1037 }
1038 
1039 static bool trans_l_sfleui(DisasContext *dc, arg_ai *a)
1040 {
1041     tcg_gen_setcondi_tl(TCG_COND_LEU, cpu_sr_f, cpu_R(dc, a->a), a->i);
1042     return true;
1043 }
1044 
1045 static bool trans_l_sfgtsi(DisasContext *dc, arg_ai *a)
1046 {
1047     tcg_gen_setcondi_tl(TCG_COND_GT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1048     return true;
1049 }
1050 
1051 static bool trans_l_sfgesi(DisasContext *dc, arg_ai *a)
1052 {
1053     tcg_gen_setcondi_tl(TCG_COND_GE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1054     return true;
1055 }
1056 
1057 static bool trans_l_sfltsi(DisasContext *dc, arg_ai *a)
1058 {
1059     tcg_gen_setcondi_tl(TCG_COND_LT, cpu_sr_f, cpu_R(dc, a->a), a->i);
1060     return true;
1061 }
1062 
1063 static bool trans_l_sflesi(DisasContext *dc, arg_ai *a)
1064 {
1065     tcg_gen_setcondi_tl(TCG_COND_LE, cpu_sr_f, cpu_R(dc, a->a), a->i);
1066     return true;
1067 }
1068 
1069 static bool trans_l_sys(DisasContext *dc, arg_l_sys *a)
1070 {
1071     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1072     gen_exception(dc, EXCP_SYSCALL);
1073     dc->base.is_jmp = DISAS_NORETURN;
1074     return true;
1075 }
1076 
1077 static bool trans_l_trap(DisasContext *dc, arg_l_trap *a)
1078 {
1079     tcg_gen_movi_tl(cpu_pc, dc->base.pc_next);
1080     gen_exception(dc, EXCP_TRAP);
1081     dc->base.is_jmp = DISAS_NORETURN;
1082     return true;
1083 }
1084 
1085 static bool trans_l_msync(DisasContext *dc, arg_l_msync *a)
1086 {
1087     tcg_gen_mb(TCG_MO_ALL);
1088     return true;
1089 }
1090 
1091 static bool trans_l_psync(DisasContext *dc, arg_l_psync *a)
1092 {
1093     return true;
1094 }
1095 
1096 static bool trans_l_csync(DisasContext *dc, arg_l_csync *a)
1097 {
1098     return true;
1099 }
1100 
1101 static bool trans_l_rfe(DisasContext *dc, arg_l_rfe *a)
1102 {
1103     if (is_user(dc)) {
1104         gen_illegal_exception(dc);
1105     } else {
1106         gen_helper_rfe(cpu_env);
1107         dc->base.is_jmp = DISAS_EXIT;
1108     }
1109     return true;
1110 }
1111 
1112 static bool do_fp2(DisasContext *dc, arg_da *a,
1113                    void (*fn)(TCGv, TCGv_env, TCGv))
1114 {
1115     if (!check_of32s(dc)) {
1116         return false;
1117     }
1118     check_r0_write(dc, a->d);
1119     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a));
1120     gen_helper_update_fpcsr(cpu_env);
1121     return true;
1122 }
1123 
1124 static bool do_fp3(DisasContext *dc, arg_dab *a,
1125                    void (*fn)(TCGv, TCGv_env, TCGv, TCGv))
1126 {
1127     if (!check_of32s(dc)) {
1128         return false;
1129     }
1130     check_r0_write(dc, a->d);
1131     fn(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1132     gen_helper_update_fpcsr(cpu_env);
1133     return true;
1134 }
1135 
1136 static bool do_fpcmp(DisasContext *dc, arg_ab *a,
1137                      void (*fn)(TCGv, TCGv_env, TCGv, TCGv),
1138                      bool inv, bool swap)
1139 {
1140     if (!check_of32s(dc)) {
1141         return false;
1142     }
1143     if (swap) {
1144         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->b), cpu_R(dc, a->a));
1145     } else {
1146         fn(cpu_sr_f, cpu_env, cpu_R(dc, a->a), cpu_R(dc, a->b));
1147     }
1148     if (inv) {
1149         tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1);
1150     }
1151     gen_helper_update_fpcsr(cpu_env);
1152     return true;
1153 }
1154 
1155 static bool trans_lf_add_s(DisasContext *dc, arg_dab *a)
1156 {
1157     return do_fp3(dc, a, gen_helper_float_add_s);
1158 }
1159 
1160 static bool trans_lf_sub_s(DisasContext *dc, arg_dab *a)
1161 {
1162     return do_fp3(dc, a, gen_helper_float_sub_s);
1163 }
1164 
1165 static bool trans_lf_mul_s(DisasContext *dc, arg_dab *a)
1166 {
1167     return do_fp3(dc, a, gen_helper_float_mul_s);
1168 }
1169 
1170 static bool trans_lf_div_s(DisasContext *dc, arg_dab *a)
1171 {
1172     return do_fp3(dc, a, gen_helper_float_div_s);
1173 }
1174 
1175 static bool trans_lf_rem_s(DisasContext *dc, arg_dab *a)
1176 {
1177     return do_fp3(dc, a, gen_helper_float_rem_s);
1178     return true;
1179 }
1180 
1181 static bool trans_lf_itof_s(DisasContext *dc, arg_da *a)
1182 {
1183     return do_fp2(dc, a, gen_helper_itofs);
1184 }
1185 
1186 static bool trans_lf_ftoi_s(DisasContext *dc, arg_da *a)
1187 {
1188     return do_fp2(dc, a, gen_helper_ftois);
1189 }
1190 
1191 static bool trans_lf_madd_s(DisasContext *dc, arg_dab *a)
1192 {
1193     if (!check_of32s(dc)) {
1194         return false;
1195     }
1196     check_r0_write(dc, a->d);
1197     gen_helper_float_madd_s(cpu_R(dc, a->d), cpu_env, cpu_R(dc, a->d),
1198                             cpu_R(dc, a->a), cpu_R(dc, a->b));
1199     gen_helper_update_fpcsr(cpu_env);
1200     return true;
1201 }
1202 
1203 static bool trans_lf_sfeq_s(DisasContext *dc, arg_ab *a)
1204 {
1205     return do_fpcmp(dc, a, gen_helper_float_eq_s, false, false);
1206 }
1207 
1208 static bool trans_lf_sfne_s(DisasContext *dc, arg_ab *a)
1209 {
1210     return do_fpcmp(dc, a, gen_helper_float_eq_s, true, false);
1211 }
1212 
1213 static bool trans_lf_sfgt_s(DisasContext *dc, arg_ab *a)
1214 {
1215     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, true);
1216 }
1217 
1218 static bool trans_lf_sfge_s(DisasContext *dc, arg_ab *a)
1219 {
1220     return do_fpcmp(dc, a, gen_helper_float_le_s, false, true);
1221 }
1222 
1223 static bool trans_lf_sflt_s(DisasContext *dc, arg_ab *a)
1224 {
1225     return do_fpcmp(dc, a, gen_helper_float_lt_s, false, false);
1226 }
1227 
1228 static bool trans_lf_sfle_s(DisasContext *dc, arg_ab *a)
1229 {
1230     return do_fpcmp(dc, a, gen_helper_float_le_s, false, false);
1231 }
1232 
1233 static bool trans_lf_sfueq_s(DisasContext *dc, arg_ab *a)
1234 {
1235     if (!check_v1_3(dc)) {
1236         return false;
1237     }
1238     return do_fpcmp(dc, a, gen_helper_float_ueq_s, false, false);
1239 }
1240 
1241 static bool trans_lf_sfult_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_ult_s, false, false);
1247 }
1248 
1249 static bool trans_lf_sfugt_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, true);
1255 }
1256 
1257 static bool trans_lf_sfule_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_ule_s, false, false);
1263 }
1264 
1265 static bool trans_lf_sfuge_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, true);
1271 }
1272 
1273 static bool trans_lf_sfun_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_un_s, false, false);
1279 }
1280 
1281 static bool check_pair(DisasContext *dc, int r, int p)
1282 {
1283     return r + 1 + p < 32;
1284 }
1285 
1286 static void load_pair(DisasContext *dc, TCGv_i64 t, int r, int p)
1287 {
1288     tcg_gen_concat_i32_i64(t, cpu_R(dc, r + 1 + p), cpu_R(dc, r));
1289 }
1290 
1291 static void save_pair(DisasContext *dc, TCGv_i64 t, int r, int p)
1292 {
1293     tcg_gen_extr_i64_i32(cpu_R(dc, r + 1 + p), cpu_R(dc, r), t);
1294 }
1295 
1296 static bool do_dp3(DisasContext *dc, arg_dab_pair *a,
1297                    void (*fn)(TCGv_i64, TCGv_env, TCGv_i64, TCGv_i64))
1298 {
1299     TCGv_i64 t0, t1;
1300 
1301     if (!check_of64a32s(dc) ||
1302         !check_pair(dc, a->a, a->ap) ||
1303         !check_pair(dc, a->b, a->bp) ||
1304         !check_pair(dc, a->d, a->dp)) {
1305         return false;
1306     }
1307     check_r0_write(dc, a->d);
1308 
1309     t0 = tcg_temp_new_i64();
1310     t1 = tcg_temp_new_i64();
1311     load_pair(dc, t0, a->a, a->ap);
1312     load_pair(dc, t1, a->b, a->bp);
1313     fn(t0, cpu_env, t0, t1);
1314     save_pair(dc, t0, a->d, a->dp);
1315 
1316     gen_helper_update_fpcsr(cpu_env);
1317     return true;
1318 }
1319 
1320 static bool do_dp2(DisasContext *dc, arg_da_pair *a,
1321                    void (*fn)(TCGv_i64, TCGv_env, TCGv_i64))
1322 {
1323     TCGv_i64 t0;
1324 
1325     if (!check_of64a32s(dc) ||
1326         !check_pair(dc, a->a, a->ap) ||
1327         !check_pair(dc, a->d, a->dp)) {
1328         return false;
1329     }
1330     check_r0_write(dc, a->d);
1331 
1332     t0 = tcg_temp_new_i64();
1333     load_pair(dc, t0, a->a, a->ap);
1334     fn(t0, cpu_env, t0);
1335     save_pair(dc, t0, a->d, a->dp);
1336 
1337     gen_helper_update_fpcsr(cpu_env);
1338     return true;
1339 }
1340 
1341 static bool do_dpcmp(DisasContext *dc, arg_ab_pair *a,
1342                      void (*fn)(TCGv, TCGv_env, TCGv_i64, TCGv_i64),
1343                      bool inv, bool swap)
1344 {
1345     TCGv_i64 t0, t1;
1346 
1347     if (!check_of64a32s(dc) ||
1348         !check_pair(dc, a->a, a->ap) ||
1349         !check_pair(dc, a->b, a->bp)) {
1350         return false;
1351     }
1352 
1353     t0 = tcg_temp_new_i64();
1354     t1 = tcg_temp_new_i64();
1355     load_pair(dc, t0, a->a, a->ap);
1356     load_pair(dc, t1, a->b, a->bp);
1357     if (swap) {
1358         fn(cpu_sr_f, cpu_env, t1, t0);
1359     } else {
1360         fn(cpu_sr_f, cpu_env, t0, t1);
1361     }
1362 
1363     if (inv) {
1364         tcg_gen_xori_tl(cpu_sr_f, cpu_sr_f, 1);
1365     }
1366     gen_helper_update_fpcsr(cpu_env);
1367     return true;
1368 }
1369 
1370 static bool trans_lf_add_d(DisasContext *dc, arg_dab_pair *a)
1371 {
1372     return do_dp3(dc, a, gen_helper_float_add_d);
1373 }
1374 
1375 static bool trans_lf_sub_d(DisasContext *dc, arg_dab_pair *a)
1376 {
1377     return do_dp3(dc, a, gen_helper_float_sub_d);
1378 }
1379 
1380 static bool trans_lf_mul_d(DisasContext *dc, arg_dab_pair *a)
1381 {
1382     return do_dp3(dc, a, gen_helper_float_mul_d);
1383 }
1384 
1385 static bool trans_lf_div_d(DisasContext *dc, arg_dab_pair *a)
1386 {
1387     return do_dp3(dc, a, gen_helper_float_div_d);
1388 }
1389 
1390 static bool trans_lf_rem_d(DisasContext *dc, arg_dab_pair *a)
1391 {
1392     return do_dp3(dc, a, gen_helper_float_rem_d);
1393 }
1394 
1395 static bool trans_lf_itof_d(DisasContext *dc, arg_da_pair *a)
1396 {
1397     return do_dp2(dc, a, gen_helper_itofd);
1398 }
1399 
1400 static bool trans_lf_ftoi_d(DisasContext *dc, arg_da_pair *a)
1401 {
1402     return do_dp2(dc, a, gen_helper_ftoid);
1403 }
1404 
1405 static bool trans_lf_stod_d(DisasContext *dc, arg_lf_stod_d *a)
1406 {
1407     TCGv_i64 t0;
1408 
1409     if (!check_of64a32s(dc) ||
1410         !check_pair(dc, a->d, a->dp)) {
1411         return false;
1412     }
1413     check_r0_write(dc, a->d);
1414 
1415     t0 = tcg_temp_new_i64();
1416     gen_helper_stod(t0, cpu_env, cpu_R(dc, a->a));
1417     save_pair(dc, t0, a->d, a->dp);
1418 
1419     gen_helper_update_fpcsr(cpu_env);
1420     return true;
1421 }
1422 
1423 static bool trans_lf_dtos_d(DisasContext *dc, arg_lf_dtos_d *a)
1424 {
1425     TCGv_i64 t0;
1426 
1427     if (!check_of64a32s(dc) ||
1428         !check_pair(dc, a->a, a->ap)) {
1429         return false;
1430     }
1431     check_r0_write(dc, a->d);
1432 
1433     t0 = tcg_temp_new_i64();
1434     load_pair(dc, t0, a->a, a->ap);
1435     gen_helper_dtos(cpu_R(dc, a->d), cpu_env, t0);
1436 
1437     gen_helper_update_fpcsr(cpu_env);
1438     return true;
1439 }
1440 
1441 static bool trans_lf_madd_d(DisasContext *dc, arg_dab_pair *a)
1442 {
1443     TCGv_i64 t0, t1, t2;
1444 
1445     if (!check_of64a32s(dc) ||
1446         !check_pair(dc, a->a, a->ap) ||
1447         !check_pair(dc, a->b, a->bp) ||
1448         !check_pair(dc, a->d, a->dp)) {
1449         return false;
1450     }
1451     check_r0_write(dc, a->d);
1452 
1453     t0 = tcg_temp_new_i64();
1454     t1 = tcg_temp_new_i64();
1455     t2 = tcg_temp_new_i64();
1456     load_pair(dc, t0, a->d, a->dp);
1457     load_pair(dc, t1, a->a, a->ap);
1458     load_pair(dc, t2, a->b, a->bp);
1459     gen_helper_float_madd_d(t0, cpu_env, t0, t1, t2);
1460     save_pair(dc, t0, a->d, a->dp);
1461 
1462     gen_helper_update_fpcsr(cpu_env);
1463     return true;
1464 }
1465 
1466 static bool trans_lf_sfeq_d(DisasContext *dc, arg_ab_pair *a)
1467 {
1468     return do_dpcmp(dc, a, gen_helper_float_eq_d, false, false);
1469 }
1470 
1471 static bool trans_lf_sfne_d(DisasContext *dc, arg_ab_pair *a)
1472 {
1473     return do_dpcmp(dc, a, gen_helper_float_eq_d, true, false);
1474 }
1475 
1476 static bool trans_lf_sfgt_d(DisasContext *dc, arg_ab_pair *a)
1477 {
1478     return do_dpcmp(dc, a, gen_helper_float_lt_d, false, true);
1479 }
1480 
1481 static bool trans_lf_sfge_d(DisasContext *dc, arg_ab_pair *a)
1482 {
1483     return do_dpcmp(dc, a, gen_helper_float_le_d, false, true);
1484 }
1485 
1486 static bool trans_lf_sflt_d(DisasContext *dc, arg_ab_pair *a)
1487 {
1488     return do_dpcmp(dc, a, gen_helper_float_lt_d, false, false);
1489 }
1490 
1491 static bool trans_lf_sfle_d(DisasContext *dc, arg_ab_pair *a)
1492 {
1493     return do_dpcmp(dc, a, gen_helper_float_le_d, false, false);
1494 }
1495 
1496 static bool trans_lf_sfueq_d(DisasContext *dc, arg_ab_pair *a)
1497 {
1498     return do_dpcmp(dc, a, gen_helper_float_ueq_d, false, false);
1499 }
1500 
1501 static bool trans_lf_sfule_d(DisasContext *dc, arg_ab_pair *a)
1502 {
1503     return do_dpcmp(dc, a, gen_helper_float_ule_d, false, false);
1504 }
1505 
1506 static bool trans_lf_sfuge_d(DisasContext *dc, arg_ab_pair *a)
1507 {
1508     return do_dpcmp(dc, a, gen_helper_float_ule_d, false, true);
1509 }
1510 
1511 static bool trans_lf_sfult_d(DisasContext *dc, arg_ab_pair *a)
1512 {
1513     return do_dpcmp(dc, a, gen_helper_float_ult_d, false, false);
1514 }
1515 
1516 static bool trans_lf_sfugt_d(DisasContext *dc, arg_ab_pair *a)
1517 {
1518     return do_dpcmp(dc, a, gen_helper_float_ult_d, false, true);
1519 }
1520 
1521 static bool trans_lf_sfun_d(DisasContext *dc, arg_ab_pair *a)
1522 {
1523     return do_dpcmp(dc, a, gen_helper_float_un_d, false, false);
1524 }
1525 
1526 static void openrisc_tr_init_disas_context(DisasContextBase *dcb, CPUState *cs)
1527 {
1528     DisasContext *dc = container_of(dcb, DisasContext, base);
1529     CPUOpenRISCState *env = cs->env_ptr;
1530     int bound;
1531 
1532     dc->mem_idx = cpu_mmu_index(env, false);
1533     dc->tb_flags = dc->base.tb->flags;
1534     dc->delayed_branch = (dc->tb_flags & TB_FLAGS_DFLAG) != 0;
1535     dc->cpucfgr = env->cpucfgr;
1536     dc->avr = env->avr;
1537     dc->jmp_pc_imm = -1;
1538 
1539     bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
1540     dc->base.max_insns = MIN(dc->base.max_insns, bound);
1541 }
1542 
1543 static void openrisc_tr_tb_start(DisasContextBase *db, CPUState *cs)
1544 {
1545     DisasContext *dc = container_of(db, DisasContext, base);
1546 
1547     /* Allow the TCG optimizer to see that R0 == 0,
1548        when it's true, which is the common case.  */
1549     dc->zero = tcg_constant_tl(0);
1550     if (dc->tb_flags & TB_FLAGS_R0_0) {
1551         dc->R0 = dc->zero;
1552     } else {
1553         dc->R0 = cpu_regs[0];
1554     }
1555 }
1556 
1557 static void openrisc_tr_insn_start(DisasContextBase *dcbase, CPUState *cs)
1558 {
1559     DisasContext *dc = container_of(dcbase, DisasContext, base);
1560 
1561     tcg_gen_insn_start(dc->base.pc_next, (dc->delayed_branch ? 1 : 0)
1562                        | (dc->base.num_insns > 1 ? 2 : 0));
1563 }
1564 
1565 static void openrisc_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs)
1566 {
1567     DisasContext *dc = container_of(dcbase, DisasContext, base);
1568     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1569     uint32_t insn = translator_ldl(&cpu->env, &dc->base, dc->base.pc_next);
1570 
1571     if (!decode(dc, insn)) {
1572         gen_illegal_exception(dc);
1573     }
1574     dc->base.pc_next += 4;
1575 
1576     /* When exiting the delay slot normally, exit via jmp_pc.
1577      * For DISAS_NORETURN, we have raised an exception and already exited.
1578      * For DISAS_EXIT, we found l.rfe in a delay slot.  There's nothing
1579      * in the manual saying this is illegal, but it surely it should.
1580      * At least or1ksim overrides pcnext and ignores the branch.
1581      */
1582     if (dc->delayed_branch
1583         && --dc->delayed_branch == 0
1584         && dc->base.is_jmp == DISAS_NEXT) {
1585         dc->base.is_jmp = DISAS_JUMP;
1586     }
1587 }
1588 
1589 static void openrisc_tr_tb_stop(DisasContextBase *dcbase, CPUState *cs)
1590 {
1591     DisasContext *dc = container_of(dcbase, DisasContext, base);
1592     target_ulong jmp_dest;
1593 
1594     /* If we have already exited the TB, nothing following has effect.  */
1595     if (dc->base.is_jmp == DISAS_NORETURN) {
1596         return;
1597     }
1598 
1599     /* Adjust the delayed branch state for the next TB.  */
1600     if ((dc->tb_flags & TB_FLAGS_DFLAG ? 1 : 0) != (dc->delayed_branch != 0)) {
1601         tcg_gen_movi_i32(cpu_dflag, dc->delayed_branch != 0);
1602     }
1603 
1604     /* For DISAS_TOO_MANY, jump to the next insn.  */
1605     jmp_dest = dc->base.pc_next;
1606     tcg_gen_movi_tl(cpu_ppc, jmp_dest - 4);
1607 
1608     switch (dc->base.is_jmp) {
1609     case DISAS_JUMP:
1610         jmp_dest = dc->jmp_pc_imm;
1611         if (jmp_dest == -1) {
1612             /* The jump destination is indirect/computed; use jmp_pc.  */
1613             tcg_gen_mov_tl(cpu_pc, jmp_pc);
1614             tcg_gen_discard_tl(jmp_pc);
1615             tcg_gen_lookup_and_goto_ptr();
1616             break;
1617         }
1618         /* The jump destination is direct; use jmp_pc_imm.
1619            However, we will have stored into jmp_pc as well;
1620            we know now that it wasn't needed.  */
1621         tcg_gen_discard_tl(jmp_pc);
1622         /* fallthru */
1623 
1624     case DISAS_TOO_MANY:
1625         if (translator_use_goto_tb(&dc->base, jmp_dest)) {
1626             tcg_gen_goto_tb(0);
1627             tcg_gen_movi_tl(cpu_pc, jmp_dest);
1628             tcg_gen_exit_tb(dc->base.tb, 0);
1629             break;
1630         }
1631         tcg_gen_movi_tl(cpu_pc, jmp_dest);
1632         tcg_gen_lookup_and_goto_ptr();
1633         break;
1634 
1635     case DISAS_EXIT:
1636         tcg_gen_exit_tb(NULL, 0);
1637         break;
1638     default:
1639         g_assert_not_reached();
1640     }
1641 }
1642 
1643 static void openrisc_tr_disas_log(const DisasContextBase *dcbase,
1644                                   CPUState *cs, FILE *logfile)
1645 {
1646     DisasContext *s = container_of(dcbase, DisasContext, base);
1647 
1648     fprintf(logfile, "IN: %s\n", lookup_symbol(s->base.pc_first));
1649     target_disas(logfile, cs, s->base.pc_first, s->base.tb->size);
1650 }
1651 
1652 static const TranslatorOps openrisc_tr_ops = {
1653     .init_disas_context = openrisc_tr_init_disas_context,
1654     .tb_start           = openrisc_tr_tb_start,
1655     .insn_start         = openrisc_tr_insn_start,
1656     .translate_insn     = openrisc_tr_translate_insn,
1657     .tb_stop            = openrisc_tr_tb_stop,
1658     .disas_log          = openrisc_tr_disas_log,
1659 };
1660 
1661 void gen_intermediate_code(CPUState *cs, TranslationBlock *tb, int *max_insns,
1662                            target_ulong pc, void *host_pc)
1663 {
1664     DisasContext ctx;
1665 
1666     translator_loop(cs, tb, max_insns, pc, host_pc,
1667                     &openrisc_tr_ops, &ctx.base);
1668 }
1669 
1670 void openrisc_cpu_dump_state(CPUState *cs, FILE *f, int flags)
1671 {
1672     OpenRISCCPU *cpu = OPENRISC_CPU(cs);
1673     CPUOpenRISCState *env = &cpu->env;
1674     int i;
1675 
1676     qemu_fprintf(f, "PC=%08x\n", env->pc);
1677     for (i = 0; i < 32; ++i) {
1678         qemu_fprintf(f, "R%02d=%08x%c", i, cpu_get_gpr(env, i),
1679                      (i % 4) == 3 ? '\n' : ' ');
1680     }
1681 }
1682