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