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