tap-linux: Convert tap_open() to Error
[qemu.git] / target-moxie / translate.c
blobe3e9139061e03155a2a4b10e080c91701f055356
1 /*
2 * Moxie emulation for qemu: main translation routines.
4 * Copyright (c) 2009, 2013 Anthony Green
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* For information on the Moxie architecture, see
21 * http://moxielogic.org/wiki
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <inttypes.h>
29 #include <assert.h>
31 #include "cpu.h"
32 #include "exec/exec-all.h"
33 #include "disas/disas.h"
34 #include "tcg-op.h"
35 #include "exec/cpu_ldst.h"
37 #include "exec/helper-proto.h"
38 #include "exec/helper-gen.h"
40 /* This is the state at translation time. */
41 typedef struct DisasContext {
42 struct TranslationBlock *tb;
43 target_ulong pc, saved_pc;
44 uint32_t opcode;
45 uint32_t fp_status;
46 /* Routine used to access memory */
47 int memidx;
48 int bstate;
49 target_ulong btarget;
50 int singlestep_enabled;
51 } DisasContext;
53 enum {
54 BS_NONE = 0, /* We go out of the TB without reaching a branch or an
55 * exception condition */
56 BS_STOP = 1, /* We want to stop translation for any reason */
57 BS_BRANCH = 2, /* We reached a branch condition */
58 BS_EXCP = 3, /* We reached an exception condition */
61 static TCGv cpu_pc;
62 static TCGv cpu_gregs[16];
63 static TCGv_ptr cpu_env;
64 static TCGv cc_a, cc_b;
66 #include "exec/gen-icount.h"
68 #define REG(x) (cpu_gregs[x])
70 /* Extract the signed 10-bit offset from a 16-bit branch
71 instruction. */
72 static int extract_branch_offset(int opcode)
74 return (((signed short)((opcode & ((1 << 10) - 1)) << 6)) >> 6) << 1;
77 void moxie_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
78 int flags)
80 MoxieCPU *cpu = MOXIE_CPU(cs);
81 CPUMoxieState *env = &cpu->env;
82 int i;
83 cpu_fprintf(f, "pc=0x%08x\n", env->pc);
84 cpu_fprintf(f, "$fp=0x%08x $sp=0x%08x $r0=0x%08x $r1=0x%08x\n",
85 env->gregs[0], env->gregs[1], env->gregs[2], env->gregs[3]);
86 for (i = 4; i < 16; i += 4) {
87 cpu_fprintf(f, "$r%d=0x%08x $r%d=0x%08x $r%d=0x%08x $r%d=0x%08x\n",
88 i-2, env->gregs[i], i-1, env->gregs[i + 1],
89 i, env->gregs[i + 2], i+1, env->gregs[i + 3]);
91 for (i = 4; i < 16; i += 4) {
92 cpu_fprintf(f, "sr%d=0x%08x sr%d=0x%08x sr%d=0x%08x sr%d=0x%08x\n",
93 i-2, env->sregs[i], i-1, env->sregs[i + 1],
94 i, env->sregs[i + 2], i+1, env->sregs[i + 3]);
98 void moxie_translate_init(void)
100 int i;
101 static int done_init;
102 static const char * const gregnames[16] = {
103 "$fp", "$sp", "$r0", "$r1",
104 "$r2", "$r3", "$r4", "$r5",
105 "$r6", "$r7", "$r8", "$r9",
106 "$r10", "$r11", "$r12", "$r13"
109 if (done_init) {
110 return;
112 cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
113 cpu_pc = tcg_global_mem_new_i32(TCG_AREG0,
114 offsetof(CPUMoxieState, pc), "$pc");
115 for (i = 0; i < 16; i++)
116 cpu_gregs[i] = tcg_global_mem_new_i32(TCG_AREG0,
117 offsetof(CPUMoxieState, gregs[i]),
118 gregnames[i]);
120 cc_a = tcg_global_mem_new_i32(TCG_AREG0,
121 offsetof(CPUMoxieState, cc_a), "cc_a");
122 cc_b = tcg_global_mem_new_i32(TCG_AREG0,
123 offsetof(CPUMoxieState, cc_b), "cc_b");
125 done_init = 1;
128 static inline void gen_goto_tb(CPUMoxieState *env, DisasContext *ctx,
129 int n, target_ulong dest)
131 TranslationBlock *tb;
132 tb = ctx->tb;
134 if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) &&
135 !ctx->singlestep_enabled) {
136 tcg_gen_goto_tb(n);
137 tcg_gen_movi_i32(cpu_pc, dest);
138 tcg_gen_exit_tb((uintptr_t)tb + n);
139 } else {
140 tcg_gen_movi_i32(cpu_pc, dest);
141 if (ctx->singlestep_enabled) {
142 gen_helper_debug(cpu_env);
144 tcg_gen_exit_tb(0);
148 static int decode_opc(MoxieCPU *cpu, DisasContext *ctx)
150 CPUMoxieState *env = &cpu->env;
152 /* Local cache for the instruction opcode. */
153 int opcode;
154 /* Set the default instruction length. */
155 int length = 2;
157 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
158 tcg_gen_debug_insn_start(ctx->pc);
161 /* Examine the 16-bit opcode. */
162 opcode = ctx->opcode;
164 /* Decode instruction. */
165 if (opcode & (1 << 15)) {
166 if (opcode & (1 << 14)) {
167 /* This is a Form 3 instruction. */
168 int inst = (opcode >> 10 & 0xf);
170 #define BRANCH(cond) \
171 do { \
172 TCGLabel *l1 = gen_new_label(); \
173 tcg_gen_brcond_i32(cond, cc_a, cc_b, l1); \
174 gen_goto_tb(env, ctx, 1, ctx->pc+2); \
175 gen_set_label(l1); \
176 gen_goto_tb(env, ctx, 0, extract_branch_offset(opcode) + ctx->pc+2); \
177 ctx->bstate = BS_BRANCH; \
178 } while (0)
180 switch (inst) {
181 case 0x00: /* beq */
182 BRANCH(TCG_COND_EQ);
183 break;
184 case 0x01: /* bne */
185 BRANCH(TCG_COND_NE);
186 break;
187 case 0x02: /* blt */
188 BRANCH(TCG_COND_LT);
189 break;
190 case 0x03: /* bgt */
191 BRANCH(TCG_COND_GT);
192 break;
193 case 0x04: /* bltu */
194 BRANCH(TCG_COND_LTU);
195 break;
196 case 0x05: /* bgtu */
197 BRANCH(TCG_COND_GTU);
198 break;
199 case 0x06: /* bge */
200 BRANCH(TCG_COND_GE);
201 break;
202 case 0x07: /* ble */
203 BRANCH(TCG_COND_LE);
204 break;
205 case 0x08: /* bgeu */
206 BRANCH(TCG_COND_GEU);
207 break;
208 case 0x09: /* bleu */
209 BRANCH(TCG_COND_LEU);
210 break;
211 default:
213 TCGv temp = tcg_temp_new_i32();
214 tcg_gen_movi_i32(cpu_pc, ctx->pc);
215 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
216 gen_helper_raise_exception(cpu_env, temp);
217 tcg_temp_free_i32(temp);
219 break;
221 } else {
222 /* This is a Form 2 instruction. */
223 int inst = (opcode >> 12 & 0x3);
224 switch (inst) {
225 case 0x00: /* inc */
227 int a = (opcode >> 8) & 0xf;
228 unsigned int v = (opcode & 0xff);
229 tcg_gen_addi_i32(REG(a), REG(a), v);
231 break;
232 case 0x01: /* dec */
234 int a = (opcode >> 8) & 0xf;
235 unsigned int v = (opcode & 0xff);
236 tcg_gen_subi_i32(REG(a), REG(a), v);
238 break;
239 case 0x02: /* gsr */
241 int a = (opcode >> 8) & 0xf;
242 unsigned v = (opcode & 0xff);
243 tcg_gen_ld_i32(REG(a), cpu_env,
244 offsetof(CPUMoxieState, sregs[v]));
246 break;
247 case 0x03: /* ssr */
249 int a = (opcode >> 8) & 0xf;
250 unsigned v = (opcode & 0xff);
251 tcg_gen_st_i32(REG(a), cpu_env,
252 offsetof(CPUMoxieState, sregs[v]));
254 break;
255 default:
257 TCGv temp = tcg_temp_new_i32();
258 tcg_gen_movi_i32(cpu_pc, ctx->pc);
259 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
260 gen_helper_raise_exception(cpu_env, temp);
261 tcg_temp_free_i32(temp);
263 break;
266 } else {
267 /* This is a Form 1 instruction. */
268 int inst = opcode >> 8;
269 switch (inst) {
270 case 0x00: /* nop */
271 break;
272 case 0x01: /* ldi.l (immediate) */
274 int reg = (opcode >> 4) & 0xf;
275 int val = cpu_ldl_code(env, ctx->pc+2);
276 tcg_gen_movi_i32(REG(reg), val);
277 length = 6;
279 break;
280 case 0x02: /* mov (register-to-register) */
282 int dest = (opcode >> 4) & 0xf;
283 int src = opcode & 0xf;
284 tcg_gen_mov_i32(REG(dest), REG(src));
286 break;
287 case 0x03: /* jsra */
289 TCGv t1 = tcg_temp_new_i32();
290 TCGv t2 = tcg_temp_new_i32();
292 tcg_gen_movi_i32(t1, ctx->pc + 6);
294 /* Make space for the static chain and return address. */
295 tcg_gen_subi_i32(t2, REG(1), 8);
296 tcg_gen_mov_i32(REG(1), t2);
297 tcg_gen_qemu_st32(t1, REG(1), ctx->memidx);
299 /* Push the current frame pointer. */
300 tcg_gen_subi_i32(t2, REG(1), 4);
301 tcg_gen_mov_i32(REG(1), t2);
302 tcg_gen_qemu_st32(REG(0), REG(1), ctx->memidx);
304 /* Set the pc and $fp. */
305 tcg_gen_mov_i32(REG(0), REG(1));
307 gen_goto_tb(env, ctx, 0, cpu_ldl_code(env, ctx->pc+2));
309 tcg_temp_free_i32(t1);
310 tcg_temp_free_i32(t2);
312 ctx->bstate = BS_BRANCH;
313 length = 6;
315 break;
316 case 0x04: /* ret */
318 TCGv t1 = tcg_temp_new_i32();
320 /* The new $sp is the old $fp. */
321 tcg_gen_mov_i32(REG(1), REG(0));
323 /* Pop the frame pointer. */
324 tcg_gen_qemu_ld32u(REG(0), REG(1), ctx->memidx);
325 tcg_gen_addi_i32(t1, REG(1), 4);
326 tcg_gen_mov_i32(REG(1), t1);
329 /* Pop the return address and skip over the static chain
330 slot. */
331 tcg_gen_qemu_ld32u(cpu_pc, REG(1), ctx->memidx);
332 tcg_gen_addi_i32(t1, REG(1), 8);
333 tcg_gen_mov_i32(REG(1), t1);
335 tcg_temp_free_i32(t1);
337 /* Jump... */
338 tcg_gen_exit_tb(0);
340 ctx->bstate = BS_BRANCH;
342 break;
343 case 0x05: /* add.l */
345 int a = (opcode >> 4) & 0xf;
346 int b = opcode & 0xf;
348 tcg_gen_add_i32(REG(a), REG(a), REG(b));
350 break;
351 case 0x06: /* push */
353 int a = (opcode >> 4) & 0xf;
354 int b = opcode & 0xf;
356 TCGv t1 = tcg_temp_new_i32();
357 tcg_gen_subi_i32(t1, REG(a), 4);
358 tcg_gen_mov_i32(REG(a), t1);
359 tcg_gen_qemu_st32(REG(b), REG(a), ctx->memidx);
360 tcg_temp_free_i32(t1);
362 break;
363 case 0x07: /* pop */
365 int a = (opcode >> 4) & 0xf;
366 int b = opcode & 0xf;
367 TCGv t1 = tcg_temp_new_i32();
369 tcg_gen_qemu_ld32u(REG(b), REG(a), ctx->memidx);
370 tcg_gen_addi_i32(t1, REG(a), 4);
371 tcg_gen_mov_i32(REG(a), t1);
372 tcg_temp_free_i32(t1);
374 break;
375 case 0x08: /* lda.l */
377 int reg = (opcode >> 4) & 0xf;
379 TCGv ptr = tcg_temp_new_i32();
380 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
381 tcg_gen_qemu_ld32u(REG(reg), ptr, ctx->memidx);
382 tcg_temp_free_i32(ptr);
384 length = 6;
386 break;
387 case 0x09: /* sta.l */
389 int val = (opcode >> 4) & 0xf;
391 TCGv ptr = tcg_temp_new_i32();
392 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
393 tcg_gen_qemu_st32(REG(val), ptr, ctx->memidx);
394 tcg_temp_free_i32(ptr);
396 length = 6;
398 break;
399 case 0x0a: /* ld.l (register indirect) */
401 int src = opcode & 0xf;
402 int dest = (opcode >> 4) & 0xf;
404 tcg_gen_qemu_ld32u(REG(dest), REG(src), ctx->memidx);
406 break;
407 case 0x0b: /* st.l */
409 int dest = (opcode >> 4) & 0xf;
410 int val = opcode & 0xf;
412 tcg_gen_qemu_st32(REG(val), REG(dest), ctx->memidx);
414 break;
415 case 0x0c: /* ldo.l */
417 int a = (opcode >> 4) & 0xf;
418 int b = opcode & 0xf;
420 TCGv t1 = tcg_temp_new_i32();
421 TCGv t2 = tcg_temp_new_i32();
422 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
423 tcg_gen_qemu_ld32u(t2, t1, ctx->memidx);
424 tcg_gen_mov_i32(REG(a), t2);
426 tcg_temp_free_i32(t1);
427 tcg_temp_free_i32(t2);
429 length = 6;
431 break;
432 case 0x0d: /* sto.l */
434 int a = (opcode >> 4) & 0xf;
435 int b = opcode & 0xf;
437 TCGv t1 = tcg_temp_new_i32();
438 TCGv t2 = tcg_temp_new_i32();
439 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
440 tcg_gen_qemu_st32(REG(b), t1, ctx->memidx);
442 tcg_temp_free_i32(t1);
443 tcg_temp_free_i32(t2);
445 length = 6;
447 break;
448 case 0x0e: /* cmp */
450 int a = (opcode >> 4) & 0xf;
451 int b = opcode & 0xf;
453 tcg_gen_mov_i32(cc_a, REG(a));
454 tcg_gen_mov_i32(cc_b, REG(b));
456 break;
457 case 0x19: /* jsr */
459 int fnreg = (opcode >> 4) & 0xf;
461 /* Load the stack pointer into T0. */
462 TCGv t1 = tcg_temp_new_i32();
463 TCGv t2 = tcg_temp_new_i32();
465 tcg_gen_movi_i32(t1, ctx->pc+2);
467 /* Make space for the static chain and return address. */
468 tcg_gen_subi_i32(t2, REG(1), 8);
469 tcg_gen_mov_i32(REG(1), t2);
470 tcg_gen_qemu_st32(t1, REG(1), ctx->memidx);
472 /* Push the current frame pointer. */
473 tcg_gen_subi_i32(t2, REG(1), 4);
474 tcg_gen_mov_i32(REG(1), t2);
475 tcg_gen_qemu_st32(REG(0), REG(1), ctx->memidx);
477 /* Set the pc and $fp. */
478 tcg_gen_mov_i32(REG(0), REG(1));
479 tcg_gen_mov_i32(cpu_pc, REG(fnreg));
480 tcg_temp_free_i32(t1);
481 tcg_temp_free_i32(t2);
482 tcg_gen_exit_tb(0);
483 ctx->bstate = BS_BRANCH;
485 break;
486 case 0x1a: /* jmpa */
488 tcg_gen_movi_i32(cpu_pc, cpu_ldl_code(env, ctx->pc+2));
489 tcg_gen_exit_tb(0);
490 ctx->bstate = BS_BRANCH;
491 length = 6;
493 break;
494 case 0x1b: /* ldi.b (immediate) */
496 int reg = (opcode >> 4) & 0xf;
497 int val = cpu_ldl_code(env, ctx->pc+2);
498 tcg_gen_movi_i32(REG(reg), val);
499 length = 6;
501 break;
502 case 0x1c: /* ld.b (register indirect) */
504 int src = opcode & 0xf;
505 int dest = (opcode >> 4) & 0xf;
507 tcg_gen_qemu_ld8u(REG(dest), REG(src), ctx->memidx);
509 break;
510 case 0x1d: /* lda.b */
512 int reg = (opcode >> 4) & 0xf;
514 TCGv ptr = tcg_temp_new_i32();
515 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
516 tcg_gen_qemu_ld8u(REG(reg), ptr, ctx->memidx);
517 tcg_temp_free_i32(ptr);
519 length = 6;
521 break;
522 case 0x1e: /* st.b */
524 int dest = (opcode >> 4) & 0xf;
525 int val = opcode & 0xf;
527 tcg_gen_qemu_st8(REG(val), REG(dest), ctx->memidx);
529 break;
530 case 0x1f: /* sta.b */
532 int val = (opcode >> 4) & 0xf;
534 TCGv ptr = tcg_temp_new_i32();
535 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
536 tcg_gen_qemu_st8(REG(val), ptr, ctx->memidx);
537 tcg_temp_free_i32(ptr);
539 length = 6;
541 break;
542 case 0x20: /* ldi.s (immediate) */
544 int reg = (opcode >> 4) & 0xf;
545 int val = cpu_ldl_code(env, ctx->pc+2);
546 tcg_gen_movi_i32(REG(reg), val);
547 length = 6;
549 break;
550 case 0x21: /* ld.s (register indirect) */
552 int src = opcode & 0xf;
553 int dest = (opcode >> 4) & 0xf;
555 tcg_gen_qemu_ld16u(REG(dest), REG(src), ctx->memidx);
557 break;
558 case 0x22: /* lda.s */
560 int reg = (opcode >> 4) & 0xf;
562 TCGv ptr = tcg_temp_new_i32();
563 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
564 tcg_gen_qemu_ld16u(REG(reg), ptr, ctx->memidx);
565 tcg_temp_free_i32(ptr);
567 length = 6;
569 break;
570 case 0x23: /* st.s */
572 int dest = (opcode >> 4) & 0xf;
573 int val = opcode & 0xf;
575 tcg_gen_qemu_st16(REG(val), REG(dest), ctx->memidx);
577 break;
578 case 0x24: /* sta.s */
580 int val = (opcode >> 4) & 0xf;
582 TCGv ptr = tcg_temp_new_i32();
583 tcg_gen_movi_i32(ptr, cpu_ldl_code(env, ctx->pc+2));
584 tcg_gen_qemu_st16(REG(val), ptr, ctx->memidx);
585 tcg_temp_free_i32(ptr);
587 length = 6;
589 break;
590 case 0x25: /* jmp */
592 int reg = (opcode >> 4) & 0xf;
593 tcg_gen_mov_i32(cpu_pc, REG(reg));
594 tcg_gen_exit_tb(0);
595 ctx->bstate = BS_BRANCH;
597 break;
598 case 0x26: /* and */
600 int a = (opcode >> 4) & 0xf;
601 int b = opcode & 0xf;
603 tcg_gen_and_i32(REG(a), REG(a), REG(b));
605 break;
606 case 0x27: /* lshr */
608 int a = (opcode >> 4) & 0xf;
609 int b = opcode & 0xf;
611 TCGv sv = tcg_temp_new_i32();
612 tcg_gen_andi_i32(sv, REG(b), 0x1f);
613 tcg_gen_shr_i32(REG(a), REG(a), sv);
614 tcg_temp_free_i32(sv);
616 break;
617 case 0x28: /* ashl */
619 int a = (opcode >> 4) & 0xf;
620 int b = opcode & 0xf;
622 TCGv sv = tcg_temp_new_i32();
623 tcg_gen_andi_i32(sv, REG(b), 0x1f);
624 tcg_gen_shl_i32(REG(a), REG(a), sv);
625 tcg_temp_free_i32(sv);
627 break;
628 case 0x29: /* sub.l */
630 int a = (opcode >> 4) & 0xf;
631 int b = opcode & 0xf;
633 tcg_gen_sub_i32(REG(a), REG(a), REG(b));
635 break;
636 case 0x2a: /* neg */
638 int a = (opcode >> 4) & 0xf;
639 int b = opcode & 0xf;
641 tcg_gen_neg_i32(REG(a), REG(b));
643 break;
644 case 0x2b: /* or */
646 int a = (opcode >> 4) & 0xf;
647 int b = opcode & 0xf;
649 tcg_gen_or_i32(REG(a), REG(a), REG(b));
651 break;
652 case 0x2c: /* not */
654 int a = (opcode >> 4) & 0xf;
655 int b = opcode & 0xf;
657 tcg_gen_not_i32(REG(a), REG(b));
659 break;
660 case 0x2d: /* ashr */
662 int a = (opcode >> 4) & 0xf;
663 int b = opcode & 0xf;
665 TCGv sv = tcg_temp_new_i32();
666 tcg_gen_andi_i32(sv, REG(b), 0x1f);
667 tcg_gen_sar_i32(REG(a), REG(a), sv);
668 tcg_temp_free_i32(sv);
670 break;
671 case 0x2e: /* xor */
673 int a = (opcode >> 4) & 0xf;
674 int b = opcode & 0xf;
676 tcg_gen_xor_i32(REG(a), REG(a), REG(b));
678 break;
679 case 0x2f: /* mul.l */
681 int a = (opcode >> 4) & 0xf;
682 int b = opcode & 0xf;
684 tcg_gen_mul_i32(REG(a), REG(a), REG(b));
686 break;
687 case 0x30: /* swi */
689 int val = cpu_ldl_code(env, ctx->pc+2);
691 TCGv temp = tcg_temp_new_i32();
692 tcg_gen_movi_i32(temp, val);
693 tcg_gen_st_i32(temp, cpu_env,
694 offsetof(CPUMoxieState, sregs[3]));
695 tcg_gen_movi_i32(cpu_pc, ctx->pc);
696 tcg_gen_movi_i32(temp, MOXIE_EX_SWI);
697 gen_helper_raise_exception(cpu_env, temp);
698 tcg_temp_free_i32(temp);
700 length = 6;
702 break;
703 case 0x31: /* div.l */
705 int a = (opcode >> 4) & 0xf;
706 int b = opcode & 0xf;
707 tcg_gen_movi_i32(cpu_pc, ctx->pc);
708 gen_helper_div(REG(a), cpu_env, REG(a), REG(b));
710 break;
711 case 0x32: /* udiv.l */
713 int a = (opcode >> 4) & 0xf;
714 int b = opcode & 0xf;
715 tcg_gen_movi_i32(cpu_pc, ctx->pc);
716 gen_helper_udiv(REG(a), cpu_env, REG(a), REG(b));
718 break;
719 case 0x33: /* mod.l */
721 int a = (opcode >> 4) & 0xf;
722 int b = opcode & 0xf;
723 tcg_gen_rem_i32(REG(a), REG(a), REG(b));
725 break;
726 case 0x34: /* umod.l */
728 int a = (opcode >> 4) & 0xf;
729 int b = opcode & 0xf;
730 tcg_gen_remu_i32(REG(a), REG(a), REG(b));
732 break;
733 case 0x35: /* brk */
735 TCGv temp = tcg_temp_new_i32();
736 tcg_gen_movi_i32(cpu_pc, ctx->pc);
737 tcg_gen_movi_i32(temp, MOXIE_EX_BREAK);
738 gen_helper_raise_exception(cpu_env, temp);
739 tcg_temp_free_i32(temp);
741 break;
742 case 0x36: /* ldo.b */
744 int a = (opcode >> 4) & 0xf;
745 int b = opcode & 0xf;
747 TCGv t1 = tcg_temp_new_i32();
748 TCGv t2 = tcg_temp_new_i32();
749 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
750 tcg_gen_qemu_ld8u(t2, t1, ctx->memidx);
751 tcg_gen_mov_i32(REG(a), t2);
753 tcg_temp_free_i32(t1);
754 tcg_temp_free_i32(t2);
756 length = 6;
758 break;
759 case 0x37: /* sto.b */
761 int a = (opcode >> 4) & 0xf;
762 int b = opcode & 0xf;
764 TCGv t1 = tcg_temp_new_i32();
765 TCGv t2 = tcg_temp_new_i32();
766 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
767 tcg_gen_qemu_st8(REG(b), t1, ctx->memidx);
769 tcg_temp_free_i32(t1);
770 tcg_temp_free_i32(t2);
772 length = 6;
774 break;
775 case 0x38: /* ldo.s */
777 int a = (opcode >> 4) & 0xf;
778 int b = opcode & 0xf;
780 TCGv t1 = tcg_temp_new_i32();
781 TCGv t2 = tcg_temp_new_i32();
782 tcg_gen_addi_i32(t1, REG(b), cpu_ldl_code(env, ctx->pc+2));
783 tcg_gen_qemu_ld16u(t2, t1, ctx->memidx);
784 tcg_gen_mov_i32(REG(a), t2);
786 tcg_temp_free_i32(t1);
787 tcg_temp_free_i32(t2);
789 length = 6;
791 break;
792 case 0x39: /* sto.s */
794 int a = (opcode >> 4) & 0xf;
795 int b = opcode & 0xf;
797 TCGv t1 = tcg_temp_new_i32();
798 TCGv t2 = tcg_temp_new_i32();
799 tcg_gen_addi_i32(t1, REG(a), cpu_ldl_code(env, ctx->pc+2));
800 tcg_gen_qemu_st16(REG(b), t1, ctx->memidx);
801 tcg_temp_free_i32(t1);
802 tcg_temp_free_i32(t2);
804 length = 6;
806 break;
807 default:
809 TCGv temp = tcg_temp_new_i32();
810 tcg_gen_movi_i32(cpu_pc, ctx->pc);
811 tcg_gen_movi_i32(temp, MOXIE_EX_BAD);
812 gen_helper_raise_exception(cpu_env, temp);
813 tcg_temp_free_i32(temp);
815 break;
819 return length;
822 /* generate intermediate code for basic block 'tb'. */
823 static inline void
824 gen_intermediate_code_internal(MoxieCPU *cpu, TranslationBlock *tb,
825 bool search_pc)
827 CPUState *cs = CPU(cpu);
828 DisasContext ctx;
829 target_ulong pc_start;
830 CPUBreakpoint *bp;
831 int j, lj = -1;
832 CPUMoxieState *env = &cpu->env;
833 int num_insns;
835 pc_start = tb->pc;
836 ctx.pc = pc_start;
837 ctx.saved_pc = -1;
838 ctx.tb = tb;
839 ctx.memidx = 0;
840 ctx.singlestep_enabled = 0;
841 ctx.bstate = BS_NONE;
842 num_insns = 0;
844 gen_tb_start(tb);
845 do {
846 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
847 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
848 if (ctx.pc == bp->pc) {
849 tcg_gen_movi_i32(cpu_pc, ctx.pc);
850 gen_helper_debug(cpu_env);
851 ctx.bstate = BS_EXCP;
852 goto done_generating;
857 if (search_pc) {
858 j = tcg_op_buf_count();
859 if (lj < j) {
860 lj++;
861 while (lj < j) {
862 tcg_ctx.gen_opc_instr_start[lj++] = 0;
865 tcg_ctx.gen_opc_pc[lj] = ctx.pc;
866 tcg_ctx.gen_opc_instr_start[lj] = 1;
867 tcg_ctx.gen_opc_icount[lj] = num_insns;
869 ctx.opcode = cpu_lduw_code(env, ctx.pc);
870 ctx.pc += decode_opc(cpu, &ctx);
871 num_insns++;
873 if (cs->singlestep_enabled) {
874 break;
877 if ((ctx.pc & (TARGET_PAGE_SIZE - 1)) == 0) {
878 break;
880 } while (ctx.bstate == BS_NONE && !tcg_op_buf_full());
882 if (cs->singlestep_enabled) {
883 tcg_gen_movi_tl(cpu_pc, ctx.pc);
884 gen_helper_debug(cpu_env);
885 } else {
886 switch (ctx.bstate) {
887 case BS_STOP:
888 case BS_NONE:
889 gen_goto_tb(env, &ctx, 0, ctx.pc);
890 break;
891 case BS_EXCP:
892 tcg_gen_exit_tb(0);
893 break;
894 case BS_BRANCH:
895 default:
896 break;
899 done_generating:
900 gen_tb_end(tb, num_insns);
902 if (search_pc) {
903 j = tcg_op_buf_count();
904 lj++;
905 while (lj <= j) {
906 tcg_ctx.gen_opc_instr_start[lj++] = 0;
908 } else {
909 tb->size = ctx.pc - pc_start;
910 tb->icount = num_insns;
914 void gen_intermediate_code(CPUMoxieState *env, struct TranslationBlock *tb)
916 gen_intermediate_code_internal(moxie_env_get_cpu(env), tb, false);
919 void gen_intermediate_code_pc(CPUMoxieState *env, struct TranslationBlock *tb)
921 gen_intermediate_code_internal(moxie_env_get_cpu(env), tb, true);
924 void restore_state_to_opc(CPUMoxieState *env, TranslationBlock *tb, int pc_pos)
926 env->pc = tcg_ctx.gen_opc_pc[pc_pos];