Debian Codegraph @

stable/ppc32_x86_trans.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13
14 #include "cpu.h"
15 #include "jit_op.h"
16 #include "ppc32_jit.h"
17 #include "ppc32_x86_trans.h"
18 #include "memory.h"
19
20 /* %esp adjustment (for MacOS X) */
21 #define STACK_ADJUST 12
22
23 /* ======================================================================= */
24
25 /* Macros for CPU structure access */
26 #define REG_OFFSET(reg) (OFFSET(cpu_ppc_t,gpr[(reg)]))
27 #define MEMOP_OFFSET(op) (OFFSET(cpu_ppc_t,mem_op_fn[(op)]))
28
29 #define DECLARE_INSN(name) \
30 static int ppc32_emit_##name(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b, \
31 ppc_insn_t insn)
32
33 /* EFLAGS to Condition Register (CR) field - signed */
34 static m_uint32_t eflags_to_cr_signed[64] = {
35 0x04, 0x02, 0x08, 0x02, 0x04, 0x02, 0x08, 0x02,
36 0x04, 0x02, 0x08, 0x02, 0x04, 0x02, 0x08, 0x02,
37 0x04, 0x02, 0x08, 0x02, 0x04, 0x02, 0x08, 0x02,
38 0x04, 0x02, 0x08, 0x02, 0x04, 0x02, 0x08, 0x02,
39 0x08, 0x02, 0x04, 0x02, 0x08, 0x02, 0x04, 0x02,
40 0x08, 0x02, 0x04, 0x02, 0x08, 0x02, 0x04, 0x02,
41 0x08, 0x02, 0x04, 0x02, 0x08, 0x02, 0x04, 0x02,
42 0x08, 0x02, 0x04, 0x02, 0x08, 0x02, 0x04, 0x02,
43 };
44
45 /* EFLAGS to Condition Register (CR) field - unsigned */
46 static m_uint32_t eflags_to_cr_unsigned[256] = {
47 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
48 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
49 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
50 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
51 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
52 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
53 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
54 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
55 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
56 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
57 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
58 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
59 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
60 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
61 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
62 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
63 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
64 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
65 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
66 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
67 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
68 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
69 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
70 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08,
71 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
72 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
73 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
74 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
75 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
76 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
77 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
78 0x02, 0x08, 0x02, 0x08, 0x02, 0x08, 0x02, 0x08,
79 };
80
81 /* Emit unhandled instruction code */
82 static int ppc32_emit_unknown(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,
83 ppc_insn_t opcode);
84
85 /* Load a 32 bit immediate value */
86 static forced_inline void ppc32_load_imm(u_char **ptr,u_int reg,m_uint32_t val)
87 {
88 if (val)
89 x86_mov_reg_imm(*ptr,reg,val);
90 else
91 x86_alu_reg_reg(*ptr,X86_XOR,reg,reg);
92 }
93
94 /* Set the Instruction Address (IA) register */
95 void ppc32_set_ia(u_char **ptr,m_uint32_t new_ia)
96 {
97 x86_mov_membase_imm(*ptr,X86_EDI,OFFSET(cpu_ppc_t,ia),new_ia,4);
98 }
99
100 /* Set the Link Register (LR) */
101 static void ppc32_set_lr(jit_op_t *iop,m_uint32_t new_lr)
102 {
103 x86_mov_membase_imm(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,lr),new_lr,4);
104 }
105
106 /*
107 * Try to branch directly to the specified JIT block without returning to
108 * main loop.
109 */
110 static void ppc32_try_direct_far_jump(cpu_ppc_t *cpu,jit_op_t *iop,
111 m_uint32_t new_ia)
112 {
113 m_uint32_t new_page,ia_hash,ia_offset;
114 u_char *test1,*test2,*test3;
115
116 /* Indicate that we throw %esi, %edx */
117 ppc32_op_emit_alter_host_reg(cpu,X86_ESI);
118 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
119
120 new_page = new_ia & PPC32_MIN_PAGE_MASK;
121 ia_offset = (new_ia & PPC32_MIN_PAGE_IMASK) >> 2;
122 ia_hash = ppc32_jit_get_ia_hash(new_ia);
123
124 /* Get JIT block info in %edx */
125 x86_mov_reg_membase(iop->ob_ptr,X86_EBX,
126 X86_EDI,OFFSET(cpu_ppc_t,exec_blk_map),4);
127 x86_mov_reg_membase(iop->ob_ptr,X86_EDX,X86_EBX,ia_hash*sizeof(void *),4);
128
129 /* no JIT block found ? */
130 x86_test_reg_reg(iop->ob_ptr,X86_EDX,X86_EDX);
131 test1 = iop->ob_ptr;
132 x86_branch8(iop->ob_ptr, X86_CC_Z, 0, 1);
133
134 /* Check block IA */
135 x86_mov_reg_imm(iop->ob_ptr,X86_ESI,new_page);
136 x86_alu_reg_membase(iop->ob_ptr,X86_CMP,X86_ESI,X86_EDX,
137 OFFSET(ppc32_jit_tcb_t,start_ia));
138 test2 = iop->ob_ptr;
139 x86_branch8(iop->ob_ptr, X86_CC_NE, 0, 1);
140
141 /* Jump to the code */
142 x86_mov_reg_membase(iop->ob_ptr,X86_ESI,
143 X86_EDX,OFFSET(ppc32_jit_tcb_t,jit_insn_ptr),4);
144 x86_mov_reg_membase(iop->ob_ptr,X86_EBX,
145 X86_ESI,ia_offset * sizeof(void *),4);
146
147 x86_test_reg_reg(iop->ob_ptr,X86_EBX,X86_EBX);
148 test3 = iop->ob_ptr;
149 x86_branch8(iop->ob_ptr, X86_CC_Z, 0, 1);
150 x86_jump_reg(iop->ob_ptr,X86_EBX);
151
152 /* Returns to caller... */
153 x86_patch(test1,iop->ob_ptr);
154 x86_patch(test2,iop->ob_ptr);
155 x86_patch(test3,iop->ob_ptr);
156
157 ppc32_set_ia(&iop->ob_ptr,new_ia);
158 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
159 }
160
161 /* Set Jump */
162 static void ppc32_set_jump(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,jit_op_t *iop,
163 m_uint32_t new_ia,int local_jump)
164 {
165 int return_to_caller = FALSE;
166 u_char *jump_ptr;
167
168 #if 0
169 if (cpu->sym_trace && !local_jump)
170 return_to_caller = TRUE;
171 #endif
172
173 if (!return_to_caller && ppc32_jit_tcb_local_addr(b,new_ia,&jump_ptr)) {
174 ppc32_jit_tcb_record_patch(b,iop,iop->ob_ptr,new_ia);
175 x86_jump32(iop->ob_ptr,0);
176 } else {
177 if (cpu->exec_blk_direct_jump) {
178 /* Block lookup optimization */
179 ppc32_try_direct_far_jump(cpu,iop,new_ia);
180 } else {
181 ppc32_set_ia(&iop->ob_ptr,new_ia);
182 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
183 }
184 }
185 }
186
187 /* Jump to the next page */
188 void ppc32_set_page_jump(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
189 {
190 jit_op_t *iop,*op_list = NULL;
191
192 cpu->gen->jit_op_current = &op_list;
193
194 iop = ppc32_op_emit_insn_output(cpu,4,"set_page_jump");
195 ppc32_set_jump(cpu,b,iop,b->start_ia + PPC32_MIN_PAGE_SIZE,FALSE);
196 ppc32_op_insn_output(b,iop);
197
198 jit_op_free_list(cpu->gen,op_list);
199 cpu->gen->jit_op_current = NULL;
200 }
201
202 /* Load a GPR into the specified host register */
203 static forced_inline void ppc32_load_gpr(u_char **ptr,u_int host_reg,
204 u_int ppc_reg)
205 {
206 x86_mov_reg_membase(*ptr,host_reg,X86_EDI,REG_OFFSET(ppc_reg),4);
207 }
208
209 /* Store contents for a host register into a GPR register */
210 static forced_inline void ppc32_store_gpr(u_char **ptr,u_int ppc_reg,
211 u_int host_reg)
212 {
213 x86_mov_membase_reg(*ptr,X86_EDI,REG_OFFSET(ppc_reg),host_reg,4);
214 }
215
216 /* Apply an ALU operation on a GPR register and a host register */
217 static forced_inline void ppc32_alu_gpr(u_char **ptr,u_int op,
218 u_int host_reg,u_int ppc_reg)
219 {
220 x86_alu_reg_membase(*ptr,op,host_reg,X86_EDI,REG_OFFSET(ppc_reg));
221 }
222
223 /*
224 * Update CR from %eflags
225 * %eax, %edx, %esi are modified.
226 */
227 static void ppc32_update_cr(ppc32_jit_tcb_t *b,int field,int is_signed)
228 {
229 /* Get status bits from EFLAGS */
230 if (!is_signed) {
231 x86_mov_reg_imm(b->jit_ptr,X86_EAX,0);
232 x86_lahf(b->jit_ptr);
233 x86_xchg_ah_al(b->jit_ptr);
234
235 x86_mov_reg_imm(b->jit_ptr,X86_EDX,eflags_to_cr_unsigned);
236 } else {
237 x86_pushfd(b->jit_ptr);
238 x86_pop_reg(b->jit_ptr,X86_EAX);
239 x86_shift_reg_imm(b->jit_ptr,X86_SHR,X86_EAX,6);
240 x86_alu_reg_imm(b->jit_ptr,X86_AND,X86_EAX,0x3F);
241
242 x86_mov_reg_imm(b->jit_ptr,X86_EDX,eflags_to_cr_signed);
243 }
244
245 x86_mov_reg_memindex(b->jit_ptr,X86_EAX,X86_EDX,0,X86_EAX,2,4);
246
247 /* Check XER Summary of Overflow and report it */
248 //x86_mov_reg_membase(b->jit_ptr,X86_EDX,X86_EDI,OFFSET(cpu_ppc_t,xer),4);
249 //x86_alu_reg_imm(b->jit_ptr,X86_AND,X86_ESI,PPC32_XER_SO);
250 //x86_shift_reg_imm(b->jit_ptr,X86_SHR,X86_ESI,PPC32_XER_SO_BIT);
251 //x86_alu_reg_reg(b->jit_ptr,X86_OR,X86_EAX,X86_ESI);
252
253 /* Store modified CR field */
254 x86_mov_membase_reg(b->jit_ptr,X86_EDI,PPC32_CR_FIELD_OFFSET(field),
255 X86_EAX,4);
256 }
257
258 /*
259 * Update CR0 from %eflags
260 * %eax, %edx, %esi are modified.
261 */
262 static void ppc32_update_cr0(ppc32_jit_tcb_t *b)
263 {
264 ppc32_update_cr(b,0,TRUE);
265 }
266
267 /* Indicate registers modified by ppc32_update_cr() functions */
268 void ppc32_update_cr_set_altered_hreg(cpu_ppc_t *cpu)
269 {
270 /* Throw %eax and %edx, which are modifed by ppc32_update_cr() */
271 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
272 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
273 }
274
275 /* Basic C call */
276 static forced_inline
277 void ppc32_emit_basic_c_call(u_char **ptr,void *f)
278 {
279 x86_mov_reg_imm(*ptr,X86_EBX,f);
280 x86_call_reg(*ptr,X86_EBX);
281 }
282
283 /* Emit a simple call to a C function without any parameter */
284 static void ppc32_emit_c_call(ppc32_jit_tcb_t *b,jit_op_t *iop,void *f)
285 {
286 ppc32_set_ia(&iop->ob_ptr,b->start_ia+(b->ppc_trans_pos << 2));
287 ppc32_emit_basic_c_call(&iop->ob_ptr,f);
288 }
289
290 /* ======================================================================== */
291
292 /* Initialize register mapping */
293 void ppc32_jit_init_hreg_mapping(cpu_ppc_t *cpu)
294 {
295 int avail_hregs[] = { X86_ESI, X86_EAX, X86_ECX, X86_EDX, -1 };
296 struct hreg_map *map;
297 int i,hreg;
298
299 cpu->hreg_map_list = cpu->hreg_lru = NULL;
300
301 /* Add the available registers to the map list */
302 for(i=0;avail_hregs[i]!=-1;i++) {
303 hreg = avail_hregs[i];
304 map = &cpu->hreg_map[hreg];
305
306 /* Initialize mapping. At the beginning, no PPC reg is mapped */
307 map->flags = 0;
308 map->hreg = hreg;
309 map->vreg = -1;
310 ppc32_jit_insert_hreg_mru(cpu,map);
311 }
312
313 /* Clear PPC registers mapping */
314 for(i=0;i<PPC32_GPR_NR;i++)
315 cpu->ppc_reg_map[i] = -1;
316 }
317
318 /* Allocate a specific temp register */
319 static int ppc32_jit_get_tmp_hreg(cpu_ppc_t *cpu)
320 {
321 return(X86_EBX);
322 }
323
324 /* ======================================================================== */
325 /* JIT operations (specific to target CPU). */
326 /* ======================================================================== */
327
328 /* INSN_OUTPUT */
329 void ppc32_op_insn_output(ppc32_jit_tcb_t *b,jit_op_t *op)
330 {
331 op->ob_final = b->jit_ptr;
332 memcpy(b->jit_ptr,op->ob_data,op->ob_ptr - op->ob_data);
333 b->jit_ptr += op->ob_ptr - op->ob_data;
334
335 if ((op->ob_ptr - op->ob_data) >= jit_op_blk_sizes[op->ob_size_index]) {
336 printf("ppc32_op_insn_output: FAILURE: count=%d, size=%d\n",
337 op->ob_ptr - op->ob_data, jit_op_blk_sizes[op->ob_size_index]);
338 }
339 }
340
341 /* LOAD_GPR: p[0] = %host_reg, p[1] = %ppc_reg */
342 void ppc32_op_load_gpr(ppc32_jit_tcb_t *b,jit_op_t *op)
343 {
344 if (op->param[0] != JIT_OP_INV_REG)
345 ppc32_load_gpr(&b->jit_ptr,op->param[0],op->param[1]);
346 }
347
348 /* STORE_GPR: p[0] = %host_reg, p[1] = %ppc_reg */
349 void ppc32_op_store_gpr(ppc32_jit_tcb_t *b,jit_op_t *op)
350 {
351 if (op->param[0] != JIT_OP_INV_REG)
352 ppc32_store_gpr(&b->jit_ptr,op->param[1],op->param[0]);
353 }
354
355 /* UPDATE_FLAGS: p[0] = cr_field, p[1] = is_signed */
356 void ppc32_op_update_flags(ppc32_jit_tcb_t *b,jit_op_t *op)
357 {
358 if (op->param[0] != JIT_OP_INV_REG)
359 ppc32_update_cr(b,op->param[0],op->param[1]);
360 }
361
362 /* MOVE_HOST_REG: p[0] = %host_dst_reg, p[1] = %host_src_reg */
363 void ppc32_op_move_host_reg(ppc32_jit_tcb_t *b,jit_op_t *op)
364 {
365 if ((op->param[0] != JIT_OP_INV_REG) && (op->param[1] != JIT_OP_INV_REG))
366 x86_mov_reg_reg(b->jit_ptr,op->param[0],op->param[1],4);
367 }
368
369 /* SET_HOST_REG_IMM32: p[0] = %host_reg, p[1] = imm32 */
370 void ppc32_op_set_host_reg_imm32(ppc32_jit_tcb_t *b,jit_op_t *op)
371 {
372 if (op->param[0] != JIT_OP_INV_REG)
373 ppc32_load_imm(&b->jit_ptr,op->param[0],op->param[1]);
374 }
375
376 /* ======================================================================== */
377
378 /* Memory operation */
379 static void ppc32_emit_memop(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,
380 int op,int base,int offset,int target,int update)
381 {
382 m_uint32_t val = sign_extend(offset,16);
383 jit_op_t *iop;
384
385 /*
386 * Since an exception can be triggered, clear JIT state. This allows
387 * to use branch target tag (we can directly branch on this instruction).
388 */
389 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_TARGET);
390 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
391
392 iop = ppc32_op_emit_insn_output(cpu,5,"memop");
393
394 /* Save PC for exception handling */
395 ppc32_set_ia(&iop->ob_ptr,b->start_ia+(b->ppc_trans_pos << 2));
396
397 /* EDX = sign-extended offset */
398 ppc32_load_imm(&iop->ob_ptr,X86_EDX,val);
399
400 /* EDX = GPR[base] + sign-extended offset */
401 if (update || (base != 0))
402 ppc32_alu_gpr(&iop->ob_ptr,X86_ADD,X86_EDX,base);
403
404 if (update)
405 x86_mov_reg_reg(iop->ob_ptr,X86_ESI,X86_EDX,4);
406
407 /* ECX = target register */
408 x86_mov_reg_imm(iop->ob_ptr,X86_ECX,target);
409
410 /* EAX = CPU instance pointer */
411 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EDI,4);
412
413 /* Call memory function */
414 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST);
415 x86_call_membase(iop->ob_ptr,X86_EDI,MEMOP_OFFSET(op));
416 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
417
418 if (update)
419 ppc32_store_gpr(&iop->ob_ptr,base,X86_ESI);
420 }
421
422 /* Memory operation (indexed) */
423 static void ppc32_emit_memop_idx(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,
424 int op,int ra,int rb,int target,int update)
425 {
426 jit_op_t *iop;
427
428 /*
429 * Since an exception can be triggered, clear JIT state. This allows
430 * to use branch target tag (we can directly branch on this instruction).
431 */
432 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_TARGET);
433 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
434
435 iop = ppc32_op_emit_insn_output(cpu,5,"memop_idx");
436
437 /* Save PC for exception handling */
438 ppc32_set_ia(&iop->ob_ptr,b->start_ia+(b->ppc_trans_pos << 2));
439
440 /* EDX = $rb */
441 ppc32_load_gpr(&iop->ob_ptr,X86_EDX,rb);
442
443 /* EDX = $rb + $ra */
444 if (update || (ra != 0))
445 ppc32_alu_gpr(&iop->ob_ptr,X86_ADD,X86_EDX,ra);
446
447 if (update)
448 x86_mov_reg_reg(iop->ob_ptr,X86_ESI,X86_EDX,4);
449
450 /* ECX = target register */
451 x86_mov_reg_imm(iop->ob_ptr,X86_ECX,target);
452
453 /* EAX = CPU instance pointer */
454 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EDI,4);
455
456 /* Call memory function */
457 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST);
458 x86_call_membase(iop->ob_ptr,X86_EDI,MEMOP_OFFSET(op));
459 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
460
461 if (update)
462 ppc32_store_gpr(&iop->ob_ptr,ra,X86_ESI);
463 }
464
465 typedef void (*memop_fast_access)(jit_op_t *iop,int target);
466
467 /* Fast LBZ */
468 static void ppc32_memop_fast_lbz(jit_op_t *iop,int target)
469 {
470 x86_clear_reg(iop->ob_ptr,X86_ECX);
471 x86_mov_reg_memindex(iop->ob_ptr,X86_ECX,X86_EAX,0,X86_EBX,0,1);
472 ppc32_store_gpr(&iop->ob_ptr,target,X86_ECX);
473 }
474
475 /* Fast STB */
476 static void ppc32_memop_fast_stb(jit_op_t *iop,int target)
477 {
478 ppc32_load_gpr(&iop->ob_ptr,X86_EDX,target);
479 x86_mov_memindex_reg(iop->ob_ptr,X86_EAX,0,X86_EBX,0,X86_EDX,1);
480 }
481
482 /* Fast LWZ */
483 static void ppc32_memop_fast_lwz(jit_op_t *iop,int target)
484 {
485 x86_mov_reg_memindex(iop->ob_ptr,X86_EAX,X86_EAX,0,X86_EBX,0,4);
486 x86_bswap(iop->ob_ptr,X86_EAX);
487 ppc32_store_gpr(&iop->ob_ptr,target,X86_EAX);
488 }
489
490 /* Fast STW */
491 static void ppc32_memop_fast_stw(jit_op_t *iop,int target)
492 {
493 ppc32_load_gpr(&iop->ob_ptr,X86_EDX,target);
494 x86_bswap(iop->ob_ptr,X86_EDX);
495 x86_mov_memindex_reg(iop->ob_ptr,X86_EAX,0,X86_EBX,0,X86_EDX,4);
496 }
497
498 /* Fast memory operation */
499 static void ppc32_emit_memop_fast(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,
500 int write_op,int opcode,
501 int base,int offset,int target,
502 memop_fast_access op_handler)
503 {
504 m_uint32_t val = sign_extend(offset,16);
505 u_char *test1,*test2,*p_exit;
506 _maybe_used u_char *p_fast_exit;
507 jit_op_t *iop;
508
509 /*
510 * Since an exception can be triggered, clear JIT state. This allows
511 * to use branch target tag (we can directly branch on this instruction).
512 */
513 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_TARGET);
514 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
515
516 iop = ppc32_op_emit_insn_output(cpu,5,"memop_fast");
517
518 test2 = NULL;
519
520 if (val != 0) {
521 /* EBX = sign-extended offset */
522 ppc32_load_imm(&iop->ob_ptr,X86_EBX,val);
523
524 /* EBX = GPR[base] + sign-extended offset */
525 if (base != 0)
526 ppc32_alu_gpr(&iop->ob_ptr,X86_ADD,X86_EBX,base);
527 } else {
528 if (base != 0)
529 ppc32_load_gpr(&iop->ob_ptr,X86_EBX,base);
530 else
531 ppc32_load_imm(&iop->ob_ptr,X86_EBX,0);
532 }
533
534 #if 0
535 /* ======= zzz ======= */
536 {
537 u_char *testZ;
538
539 x86_mov_reg_reg(iop->ob_ptr,X86_ESI,X86_EBX,4);
540 x86_alu_reg_imm(iop->ob_ptr,X86_AND,X86_ESI,PPC32_MIN_PAGE_MASK);
541 x86_alu_reg_membase(iop->ob_ptr,X86_CMP,X86_ESI,X86_EDI,
542 OFFSET(cpu_ppc_t,vtlb[base].vaddr));
543 testZ = iop->ob_ptr;
544 x86_branch8(iop->ob_ptr, X86_CC_NZ, 0, 1);
545
546 x86_alu_reg_imm(iop->ob_ptr,X86_AND,X86_EBX,PPC32_MIN_PAGE_IMASK);
547 x86_mov_reg_membase(iop->ob_ptr,X86_EAX,
548 X86_EDI,OFFSET(cpu_ppc_t,vtlb[base].haddr),4);
549
550 /* Memory access */
551 op_handler(iop,target);
552
553 p_fast_exit = iop->ob_ptr;
554 x86_jump8(iop->ob_ptr,0);
555
556 x86_patch(testZ,iop->ob_ptr);
557 }
558 #endif
559
560 /* EAX = mts32_entry index */
561 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EBX,4);
562 x86_shift_reg_imm(iop->ob_ptr,X86_SHR,X86_EAX,MTS32_HASH_SHIFT);
563 x86_alu_reg_imm(iop->ob_ptr,X86_AND,X86_EAX,MTS32_HASH_MASK);
564
565 /* EDX = mts32_entry */
566 x86_mov_reg_membase(iop->ob_ptr,X86_EDX,
567 X86_EDI,OFFSET(cpu_ppc_t,mts_cache[PPC32_MTS_DCACHE]),
568 4);
569 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,X86_EAX,4);
570 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,X86_EDX,X86_EAX);
571
572 /* Compare virtual page address (ESI = vpage) */
573 x86_mov_reg_reg(iop->ob_ptr,X86_ESI,X86_EBX,4);
574 x86_alu_reg_imm(iop->ob_ptr,X86_AND,X86_ESI,PPC32_MIN_PAGE_MASK);
575
576 x86_alu_reg_membase(iop->ob_ptr,X86_CMP,X86_ESI,X86_EDX,
577 OFFSET(mts32_entry_t,gvpa));
578 test1 = iop->ob_ptr;
579 x86_branch8(iop->ob_ptr, X86_CC_NZ, 0, 1);
580
581 /* Test if we are writing to a COW page */
582 if (write_op) {
583 x86_test_membase_imm(iop->ob_ptr,X86_EDX,OFFSET(mts32_entry_t,flags),
584 MTS_FLAG_COW|MTS_FLAG_EXEC);
585 test2 = iop->ob_ptr;
586 x86_branch8(iop->ob_ptr, X86_CC_NZ, 0, 1);
587 }
588
589 /* EBX = offset in page, EAX = Host Page Address */
590 x86_alu_reg_imm(iop->ob_ptr,X86_AND,X86_EBX,PPC32_MIN_PAGE_IMASK);
591 x86_mov_reg_membase(iop->ob_ptr,X86_EAX,
592 X86_EDX,OFFSET(mts32_entry_t,hpa),4);
593
594 #if 0
595 /* zzz */
596 {
597 x86_mov_membase_reg(iop->ob_ptr,
598 X86_EDI,OFFSET(cpu_ppc_t,vtlb[base].vaddr),
599 X86_ESI,4);
600 x86_mov_membase_reg(iop->ob_ptr,
601 X86_EDI,OFFSET(cpu_ppc_t,vtlb[base].haddr),
602 X86_EAX,4);
603 }
604 #endif
605
606 /* Memory access */
607 op_handler(iop,target);
608
609 p_exit = iop->ob_ptr;
610 x86_jump8(iop->ob_ptr,0);
611
612 /* === Slow lookup === */
613 x86_patch(test1,iop->ob_ptr);
614 if (test2)
615 x86_patch(test2,iop->ob_ptr);
616
617 /* Update IA (EBX = vaddr) */
618 ppc32_set_ia(&iop->ob_ptr,b->start_ia+(b->ppc_trans_pos << 2));
619
620 /* EDX = virtual address */
621 x86_mov_reg_reg(iop->ob_ptr,X86_EDX,X86_EBX,4);
622
623 /* ECX = target register */
624 x86_mov_reg_imm(iop->ob_ptr,X86_ECX,target);
625
626 /* EAX = CPU instance pointer */
627 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EDI,4);
628
629 /* Call memory function */
630 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST);
631 x86_call_membase(iop->ob_ptr,X86_EDI,MEMOP_OFFSET(opcode));
632 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
633
634 x86_patch(p_exit,iop->ob_ptr);
635
636 /* zzz */
637 #if 0
638 x86_patch(p_fast_exit,iop->ob_ptr);
639 #endif
640 }
641
642 /* Emit unhandled instruction code */
643 static int ppc32_emit_unknown(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b,
644 ppc_insn_t opcode)
645 {
646 u_char *test1;
647 jit_op_t *iop;
648
649 iop = ppc32_op_emit_insn_output(cpu,3,"unknown");
650
651 /* Update IA */
652 ppc32_set_ia(&iop->ob_ptr,b->start_ia+(b->ppc_trans_pos << 2));
653
654 /* Fallback to non-JIT mode */
655 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST);
656 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EDI,4);
657 x86_mov_reg_imm(iop->ob_ptr,X86_EDX,opcode);
658
659 ppc32_emit_basic_c_call(&iop->ob_ptr,ppc32_exec_single_insn_ext);
660 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
661
662 x86_test_reg_reg(iop->ob_ptr,X86_EAX,X86_EAX);
663 test1 = iop->ob_ptr;
664 x86_branch8(iop->ob_ptr, X86_CC_Z, 0, 1);
665 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
666
667 x86_patch(test1,iop->ob_ptr);
668
669 /* Signal this as an EOB to reset JIT state */
670 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
671 return(0);
672 }
673
674 /* Virtual Breakpoint */
675 void ppc32_emit_breakpoint(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
676 {
677 jit_op_t *iop;
678
679 iop = ppc32_op_emit_insn_output(cpu,2,"breakpoint");
680
681 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST);
682 x86_mov_reg_reg(iop->ob_ptr,X86_EAX,X86_EDI,4);
683 ppc32_emit_c_call(b,iop,ppc32_run_breakpoint);
684 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
685
686 /* Signal this as an EOB to to reset JIT state */
687 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
688 }
689
690 /* Dump regs */
691 _Unused static void ppc32_emit_dump_regs(cpu_ppc_t *cpu,ppc32_jit_tcb_t *b)
692 {
693 jit_op_t *iop;
694
695 iop = ppc32_op_emit_insn_output(cpu,2,"dump_regs");
696
697 x86_mov_reg_membase(iop->ob_ptr,X86_EAX,X86_EDI,OFFSET(cpu_ppc_t,gen),4);
698
699 x86_alu_reg_imm(iop->ob_ptr,X86_SUB,X86_ESP,STACK_ADJUST-4);
700 x86_push_reg(iop->ob_ptr,X86_EAX);
701 ppc32_emit_c_call(b,iop,ppc32_dump_regs);
702 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,X86_ESP,STACK_ADJUST);
703
704 /* Signal this as an EOB to to reset JIT state */
705 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
706 }
707
708 /* Increment the number of executed instructions (performance debugging) */
709 void ppc32_inc_perf_counter(cpu_ppc_t *cpu)
710 {
711 jit_op_t *iop;
712
713 iop = ppc32_op_emit_insn_output(cpu,1,"perf_cnt");
714 x86_inc_membase(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,perf_counter));
715 }
716
717 /* ======================================================================== */
718
719 /* BLR - Branch to Link Register */
720 DECLARE_INSN(BLR)
721 {
722 jit_op_t *iop;
723 int hreg;
724
725 ppc32_jit_start_hreg_seq(cpu,"blr");
726 hreg = ppc32_jit_alloc_hreg(cpu,-1);
727 ppc32_op_emit_alter_host_reg(cpu,hreg);
728
729 iop = ppc32_op_emit_insn_output(cpu,2,"blr");
730
731 x86_mov_reg_membase(iop->ob_ptr,hreg,X86_EDI,OFFSET(cpu_ppc_t,lr),4);
732 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ia),hreg,4);
733
734 /* set the return address */
735 if (insn & 1)
736 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
737
738 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
739 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
740 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
741
742 ppc32_jit_close_hreg_seq(cpu);
743 return(0);
744 }
745
746 /* BCTR - Branch to Count Register */
747 DECLARE_INSN(BCTR)
748 {
749 jit_op_t *iop;
750 int hreg;
751
752 ppc32_jit_start_hreg_seq(cpu,"bctr");
753 hreg = ppc32_jit_alloc_hreg(cpu,-1);
754 ppc32_op_emit_alter_host_reg(cpu,hreg);
755
756 iop = ppc32_op_emit_insn_output(cpu,2,"bctr");
757
758 x86_mov_reg_membase(iop->ob_ptr,hreg,X86_EDI,OFFSET(cpu_ppc_t,ctr),4);
759 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ia),hreg,4);
760
761 /* set the return address */
762 if (insn & 1)
763 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
764
765 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
766 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
767 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
768
769 ppc32_jit_close_hreg_seq(cpu);
770 return(0);
771 }
772
773 /* MFLR - Move From Link Register */
774 DECLARE_INSN(MFLR)
775 {
776 int rd = bits(insn,21,25);
777 int hreg_rd;
778 jit_op_t *iop;
779
780 ppc32_jit_start_hreg_seq(cpu,"mflr");
781 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
782 iop = ppc32_op_emit_insn_output(cpu,1,"mflr");
783
784 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,lr),4);
785 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
786
787 ppc32_jit_close_hreg_seq(cpu);
788 return(0);
789 }
790
791 /* MTLR - Move To Link Register */
792 DECLARE_INSN(MTLR)
793 {
794 int rs = bits(insn,21,25);
795 int hreg_rs;
796 jit_op_t *iop;
797
798 ppc32_jit_start_hreg_seq(cpu,"mtlr");
799 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
800 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
801
802 iop = ppc32_op_emit_insn_output(cpu,1,"mtlr");
803 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,lr),hreg_rs,4);
804
805 ppc32_jit_close_hreg_seq(cpu);
806 return(0);
807 }
808
809 /* MFCTR - Move From Counter Register */
810 DECLARE_INSN(MFCTR)
811 {
812 int rd = bits(insn,21,25);
813 int hreg_rd;
814 jit_op_t *iop;
815
816 ppc32_jit_start_hreg_seq(cpu,"mfctr");
817 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
818
819 iop = ppc32_op_emit_insn_output(cpu,1,"mfctr");
820
821 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,ctr),4);
822 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
823
824 ppc32_jit_close_hreg_seq(cpu);
825 return(0);
826 }
827
828 /* MTCTR - Move To Counter Register */
829 DECLARE_INSN(MTCTR)
830 {
831 int rs = bits(insn,21,25);
832 int hreg_rs;
833 jit_op_t *iop;
834
835 ppc32_jit_start_hreg_seq(cpu,"mtctr");
836 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
837 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
838
839 iop = ppc32_op_emit_insn_output(cpu,1,"mtctr");
840 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ctr),hreg_rs,4);
841
842 ppc32_jit_close_hreg_seq(cpu);
843 return(0);
844 }
845
846 /* MFTBU - Move from Time Base (Up) */
847 DECLARE_INSN(MFTBU)
848 {
849 int rd = bits(insn,21,25);
850 int hreg_rd;
851 jit_op_t *iop;
852
853 ppc32_jit_start_hreg_seq(cpu,"mftbu");
854 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
855
856 iop = ppc32_op_emit_insn_output(cpu,1,"mftbu");
857
858 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,tb)+4,4);
859 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
860
861 ppc32_jit_close_hreg_seq(cpu);
862 return(0);
863 }
864
865 #define PPC32_TB_INCREMENT 50
866
867 /* MFTBL - Move from Time Base (Lo) */
868 DECLARE_INSN(MFTBL)
869 {
870 int rd = bits(insn,21,25);
871 int hreg_rd,hreg_t0;
872 jit_op_t *iop;
873
874 ppc32_jit_start_hreg_seq(cpu,"mftbl");
875 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
876 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
877
878 iop = ppc32_op_emit_insn_output(cpu,3,"mftbl");
879
880 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,tb),4);
881
882 /* Increment the time base register */
883 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,tb),4);
884 x86_mov_reg_membase(iop->ob_ptr,hreg_t0,X86_EDI,OFFSET(cpu_ppc_t,tb)+4,4);
885 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_rd,PPC32_TB_INCREMENT);
886 x86_alu_reg_imm(iop->ob_ptr,X86_ADC,hreg_t0,0);
887 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,tb),hreg_rd,4);
888 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,tb)+4,hreg_t0,4);
889
890 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
891
892 ppc32_jit_close_hreg_seq(cpu);
893 return(0);
894 }
895
896 /* ADD */
897 DECLARE_INSN(ADD)
898 {
899 int rd = bits(insn,21,25);
900 int ra = bits(insn,16,20);
901 int rb = bits(insn,11,15);
902 int hreg_rd,hreg_ra,hreg_rb;
903 jit_op_t *iop;
904
905 /* $rd = $ra + $rb */
906 ppc32_jit_start_hreg_seq(cpu,"add");
907 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
908 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
909 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
910
911 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
912 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
913
914 iop = ppc32_op_emit_insn_output(cpu,2,"add");
915
916 if (rd == ra)
917 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_rb);
918 else if (rd == rb)
919 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_ra);
920 else {
921 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
922 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_rb);
923 }
924
925 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
926
927 if (insn & 1)
928 ppc32_op_emit_update_flags(cpu,0,TRUE);
929
930 ppc32_jit_close_hreg_seq(cpu);
931 return(0);
932 }
933
934 /* ADDC */
935 DECLARE_INSN(ADDC)
936 {
937 int rd = bits(insn,21,25);
938 int ra = bits(insn,16,20);
939 int rb = bits(insn,11,15);
940 int hreg_rd,hreg_ra,hreg_rb,hreg_t0;
941 jit_op_t *iop;
942
943 /* $rd = $ra + $rb */
944 ppc32_jit_start_hreg_seq(cpu,"addc");
945 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
946 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
947 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
948
949 /* store the carry flag */
950 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
951
952 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
953 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
954
955 iop = ppc32_op_emit_insn_output(cpu,2,"addc");
956
957 if (rd == ra)
958 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_rb);
959 else if (rd == rb)
960 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_ra);
961 else {
962 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
963 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_rd,hreg_rb);
964 }
965
966 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
967
968 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t0,FALSE);
969 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x1);
970 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),
971 hreg_t0,4);
972
973 if (insn & 1) {
974 x86_test_reg_reg(iop->ob_ptr,hreg_rd,hreg_rd);
975 ppc32_op_emit_update_flags(cpu,0,TRUE);
976 }
977
978 ppc32_jit_close_hreg_seq(cpu);
979 return(0);
980 }
981
982 /* ADDE - Add Extended */
983 DECLARE_INSN(ADDE)
984 {
985 int rd = bits(insn,21,25);
986 int ra = bits(insn,16,20);
987 int rb = bits(insn,11,15);
988 int hreg_ra,hreg_rb,hreg_rd,hreg_t0,hreg_t1;
989 jit_op_t *iop;
990
991 ppc32_jit_start_hreg_seq(cpu,"adde");
992 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
993 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
994 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
995
996 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
997 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
998
999 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
1000 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1001 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
1002
1003 iop = ppc32_op_emit_insn_output(cpu,3,"adde");
1004
1005 /* $t0 = $ra + carry */
1006 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t1,hreg_t1);
1007 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_ra,4);
1008
1009 x86_alu_reg_membase(iop->ob_ptr,X86_ADD,hreg_t0,
1010 X86_EDI,OFFSET(cpu_ppc_t,xer_ca));
1011 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
1012 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t1,4);
1013
1014 /* $t0 += $rb */
1015 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_t0,hreg_rb);
1016 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
1017 x86_alu_membase_reg(iop->ob_ptr,X86_OR,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),
1018 hreg_t1);
1019
1020 /* update cr0 */
1021 if (insn & 1)
1022 x86_test_reg_reg(iop->ob_ptr,hreg_t0,hreg_t0);
1023
1024 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_t0,4);
1025 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1026
1027 if (insn & 1)
1028 ppc32_op_emit_update_flags(cpu,0,TRUE);
1029
1030 ppc32_jit_close_hreg_seq(cpu);
1031 return(0);
1032 }
1033
1034 /* ADDI - ADD Immediate */
1035 DECLARE_INSN(ADDI)
1036 {
1037 int rd = bits(insn,21,25);
1038 int ra = bits(insn,16,20);
1039 int imm = bits(insn,0,15);
1040 m_uint32_t tmp = sign_extend_32(imm,16);
1041 int hreg_rd,hreg_ra;
1042 jit_op_t *iop;
1043
1044 /* $rd = $ra + imm */
1045 ppc32_jit_start_hreg_seq(cpu,"addi");
1046 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
1047
1048 if (ra != 0) {
1049 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1050 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1051
1052 iop = ppc32_op_emit_insn_output(cpu,2,"addi");
1053
1054 if (rd != ra)
1055 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
1056
1057 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_rd,tmp);
1058 } else {
1059 iop = ppc32_op_emit_insn_output(cpu,1,"addi");
1060 ppc32_load_imm(&iop->ob_ptr,hreg_rd,tmp);
1061 }
1062
1063 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1064
1065 ppc32_jit_close_hreg_seq(cpu);
1066 return(0);
1067 }
1068
1069 /* ADDIC - ADD Immediate with Carry */
1070 DECLARE_INSN(ADDIC)
1071 {
1072 int rd = bits(insn,21,25);
1073 int ra = bits(insn,16,20);
1074 int imm = bits(insn,0,15);
1075 m_uint32_t tmp = sign_extend_32(imm,16);
1076 int hreg_rd,hreg_ra;
1077 jit_op_t *iop;
1078
1079 /* $rd = $ra + imm */
1080 ppc32_jit_start_hreg_seq(cpu,"addic");
1081 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
1082 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1083
1084 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1085
1086 iop = ppc32_op_emit_insn_output(cpu,1,"addic");
1087
1088 if (rd != ra)
1089 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
1090
1091 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_rd,tmp);
1092 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1093
1094 x86_set_membase(iop->ob_ptr,X86_CC_C,
1095 X86_EDI,OFFSET(cpu_ppc_t,xer_ca),FALSE);
1096
1097 ppc32_jit_close_hreg_seq(cpu);
1098 return(0);
1099 }
1100
1101 /* ADDIC. */
1102 DECLARE_INSN(ADDIC_dot)
1103 {
1104 int rd = bits(insn,21,25);
1105 int ra = bits(insn,16,20);
1106 int imm = bits(insn,0,15);
1107 m_uint32_t tmp = sign_extend_32(imm,16);
1108 int hreg_rd,hreg_ra;
1109 jit_op_t *iop;
1110
1111 /* $rd = $ra + imm */
1112 ppc32_jit_start_hreg_seq(cpu,"addic.");
1113 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
1114 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1115
1116 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1117
1118 iop = ppc32_op_emit_insn_output(cpu,1,"addic.");
1119
1120 if (rd != ra)
1121 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
1122
1123 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_rd,tmp);
1124 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1125
1126 x86_set_membase(iop->ob_ptr,X86_CC_C,
1127 X86_EDI,OFFSET(cpu_ppc_t,xer_ca),FALSE);
1128
1129 ppc32_op_emit_update_flags(cpu,0,TRUE);
1130
1131 ppc32_jit_close_hreg_seq(cpu);
1132 return(0);
1133 }
1134
1135 /* ADDIS - ADD Immediate Shifted */
1136 DECLARE_INSN(ADDIS)
1137 {
1138 int rd = bits(insn,21,25);
1139 int ra = bits(insn,16,20);
1140 m_uint32_t imm = bits(insn,0,15);
1141 m_uint32_t tmp = imm << 16;
1142 int hreg_rd,hreg_ra;
1143 jit_op_t *iop;
1144
1145 /* $rd = $ra + (imm << 16) */
1146 ppc32_jit_start_hreg_seq(cpu,"addis");
1147 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
1148
1149 if (ra != 0) {
1150 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1151 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1152
1153 iop = ppc32_op_emit_insn_output(cpu,1,"addis");
1154
1155 if (rd != ra)
1156 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
1157
1158 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_rd,tmp);
1159 } else {
1160 //iop = ppc32_op_emit_insn_output(cpu,1,"addis");
1161 //x86_mov_reg_imm(iop->ob_ptr,hreg_rd,tmp);
1162 ppc32_op_emit_set_host_reg_imm32(cpu,hreg_rd,tmp);
1163 }
1164
1165 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1166
1167 ppc32_jit_close_hreg_seq(cpu);
1168 return(0);
1169 }
1170
1171 /* ADDZE */
1172 DECLARE_INSN(ADDZE)
1173 {
1174 int rd = bits(insn,21,25);
1175 int ra = bits(insn,16,20);
1176 int hreg_rd,hreg_ra,hreg_t0;
1177 jit_op_t *iop;
1178
1179 /* $rd = $ra + xer_ca + set_carry */
1180 ppc32_jit_start_hreg_seq(cpu,"addze");
1181 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
1182 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1183 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1184
1185 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1186
1187 iop = ppc32_op_emit_insn_output(cpu,2,"addze");
1188
1189 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t0,hreg_t0);
1190
1191 if (rd != ra)
1192 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
1193
1194 x86_alu_reg_membase(iop->ob_ptr,X86_ADD,hreg_rd,
1195 X86_EDI,OFFSET(cpu_ppc_t,xer_ca));
1196
1197 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t0,FALSE);
1198 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t0,4);
1199
1200 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
1201
1202 if (insn & 1)
1203 ppc32_op_emit_update_flags(cpu,0,TRUE);
1204
1205 ppc32_jit_close_hreg_seq(cpu);
1206 return(0);
1207 }
1208
1209 /* AND */
1210 DECLARE_INSN(AND)
1211 {
1212 int rs = bits(insn,21,25);
1213 int ra = bits(insn,16,20);
1214 int rb = bits(insn,11,15);
1215 int hreg_rs,hreg_ra,hreg_rb;
1216 jit_op_t *iop;
1217
1218 /* $ra = $rs & $rb */
1219 ppc32_jit_start_hreg_seq(cpu,"and");
1220 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
1221 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1222 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
1223
1224 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
1225 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
1226
1227 iop = ppc32_op_emit_insn_output(cpu,1,"and");
1228
1229 if (ra == rs)
1230 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rb);
1231 else if (ra == rb)
1232 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rs);
1233 else {
1234 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
1235 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rb);
1236 }
1237
1238 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
1239
1240 if (insn & 1)
1241 ppc32_op_emit_update_flags(cpu,0,TRUE);
1242
1243 ppc32_jit_close_hreg_seq(cpu);
1244 return(0);
1245 }
1246
1247 /* ANDC */
1248 DECLARE_INSN(ANDC)
1249 {
1250 int rs = bits(insn,21,25);
1251 int ra = bits(insn,16,20);
1252 int rb = bits(insn,11,15);
1253 int hreg_rs,hreg_ra,hreg_rb,hreg_t0;
1254 jit_op_t *iop;
1255
1256 /* $ra = $rs & ~$rb */
1257 ppc32_jit_start_hreg_seq(cpu,"andc");
1258 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
1259 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1260 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
1261
1262 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
1263 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
1264
1265 iop = ppc32_op_emit_insn_output(cpu,1,"andc");
1266
1267 /* $t0 = ~$rb */
1268 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1269 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rb,4);
1270 x86_not_reg(iop->ob_ptr,hreg_t0);
1271
1272 /* $ra = $rs & $t0 */
1273 if (ra == rs)
1274 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_t0);
1275 else {
1276 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,hreg_rs);
1277 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_t0,4);
1278 }
1279
1280 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
1281
1282 if (insn & 1)
1283 ppc32_op_emit_update_flags(cpu,0,TRUE);
1284
1285 ppc32_jit_close_hreg_seq(cpu);
1286 return(0);
1287 }
1288
1289 /* AND Immediate */
1290 DECLARE_INSN(ANDI)
1291 {
1292 int rs = bits(insn,21,25);
1293 int ra = bits(insn,16,20);
1294 m_uint16_t imm = bits(insn,0,15);
1295 m_uint32_t tmp = imm;
1296 int hreg_rs,hreg_ra;
1297 jit_op_t *iop;
1298
1299 /* $ra = $rs & imm */
1300 ppc32_jit_start_hreg_seq(cpu,"andi");
1301 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
1302 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1303
1304 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
1305
1306 iop = ppc32_op_emit_insn_output(cpu,2,"andi");
1307
1308 if (ra != rs)
1309 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
1310
1311 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_ra,tmp);
1312 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
1313
1314 ppc32_op_emit_update_flags(cpu,0,TRUE);
1315
1316 ppc32_jit_close_hreg_seq(cpu);
1317 return(0);
1318 }
1319
1320 /* AND Immediate Shifted */
1321 DECLARE_INSN(ANDIS)
1322 {
1323 int rs = bits(insn,21,25);
1324 int ra = bits(insn,16,20);
1325 m_uint32_t imm = bits(insn,0,15);
1326 m_uint32_t tmp = imm << 16;
1327 int hreg_rs,hreg_ra;
1328 jit_op_t *iop;
1329
1330 /* $ra = $rs & imm */
1331 ppc32_jit_start_hreg_seq(cpu,"andis");
1332 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
1333 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1334
1335 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
1336
1337 iop = ppc32_op_emit_insn_output(cpu,2,"andis");
1338
1339 if (ra != rs)
1340 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
1341
1342 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_ra,tmp);
1343 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
1344
1345 ppc32_op_emit_update_flags(cpu,0,TRUE);
1346
1347 ppc32_jit_close_hreg_seq(cpu);
1348 return(0);
1349 }
1350
1351 /* B - Branch */
1352 DECLARE_INSN(B)
1353 {
1354 m_uint32_t offset = bits(insn,2,25);
1355 m_uint32_t new_ia;
1356 jit_op_t *iop;
1357
1358 iop = ppc32_op_emit_insn_output(cpu,4,"b");
1359
1360 /* compute the new ia */
1361 new_ia = b->start_ia + (b->ppc_trans_pos << 2);
1362 new_ia += sign_extend(offset << 2,26);
1363 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1364
1365 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
1366 ppc32_op_emit_branch_target(cpu,b,new_ia);
1367 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
1368 return(0);
1369 }
1370
1371 /* BA - Branch Absolute */
1372 DECLARE_INSN(BA)
1373 {
1374 m_uint32_t offset = bits(insn,2,25);
1375 m_uint32_t new_ia;
1376 jit_op_t *iop;
1377
1378 iop = ppc32_op_emit_insn_output(cpu,4,"ba");
1379
1380 /* compute the new ia */
1381 new_ia = sign_extend(offset << 2,26);
1382 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1383
1384 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
1385 ppc32_op_emit_branch_target(cpu,b,new_ia);
1386 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
1387 return(0);
1388 }
1389
1390 /* BL - Branch and Link */
1391 DECLARE_INSN(BL)
1392 {
1393 m_uint32_t offset = bits(insn,2,25);
1394 m_uint32_t new_ia;
1395 jit_op_t *iop;
1396
1397 iop = ppc32_op_emit_insn_output(cpu,4,"bl");
1398
1399 /* compute the new ia */
1400 new_ia = b->start_ia + (b->ppc_trans_pos << 2);
1401 new_ia += sign_extend(offset << 2,26);
1402
1403 /* set the return address */
1404 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
1405 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1406
1407 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
1408 ppc32_op_emit_branch_target(cpu,b,new_ia);
1409 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
1410 return(0);
1411 }
1412
1413 /* BLA - Branch and Link Absolute */
1414 DECLARE_INSN(BLA)
1415 {
1416 m_uint32_t offset = bits(insn,2,25);
1417 m_uint32_t new_ia;
1418 jit_op_t *iop;
1419
1420 iop = ppc32_op_emit_insn_output(cpu,4,"bla");
1421
1422 /* compute the new ia */
1423 new_ia = sign_extend(offset << 2,26);
1424
1425 /* set the return address */
1426 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
1427 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1428
1429 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
1430 ppc32_op_emit_branch_target(cpu,b,new_ia);
1431 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1) << 2));
1432 return(0);
1433 }
1434
1435 /* BC - Branch Conditional (Condition Check only) */
1436 DECLARE_INSN(BCC)
1437 {
1438 int bo = bits(insn,21,25);
1439 int bi = bits(insn,16,20);
1440 int bd = bits(insn,2,15);
1441 jit_op_t *iop;
1442 u_int cr_field,cr_bit;
1443 m_uint32_t new_ia;
1444 u_char *jump_ptr;
1445 int local_jump;
1446 int cond;
1447
1448 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_JUMP);
1449
1450 iop = ppc32_op_emit_insn_output(cpu,5,"bcc");
1451
1452 /* Get the wanted value for the condition bit */
1453 cond = (bo >> 3) & 0x1;
1454
1455 /* Set the return address */
1456 if (insn & 1) {
1457 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
1458 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1)<<2));
1459 }
1460
1461 /* Compute the new ia */
1462 new_ia = sign_extend_32(bd << 2,16);
1463 if (!(insn & 0x02))
1464 new_ia += b->start_ia + (b->ppc_trans_pos << 2);
1465
1466 /* Test the condition bit */
1467 cr_field = ppc32_get_cr_field(bi);
1468 cr_bit = ppc32_get_cr_bit(bi);
1469
1470 ppc32_op_emit_require_flags(cpu,cr_field);
1471
1472 x86_test_membase_imm(iop->ob_ptr,
1473 X86_EDI,PPC32_CR_FIELD_OFFSET(cr_field),
1474 (1 << cr_bit));
1475
1476 local_jump = ppc32_jit_tcb_local_addr(b,new_ia,&jump_ptr);
1477
1478 /*
1479 * Optimize the jump, depending if the destination is in the same
1480 * page or not.
1481 */
1482 if (local_jump) {
1483 ppc32_jit_tcb_record_patch(b,iop,iop->ob_ptr,new_ia);
1484 x86_branch32(iop->ob_ptr,(cond) ? X86_CC_NZ : X86_CC_Z,0,FALSE);
1485 } else {
1486 jump_ptr = iop->ob_ptr;
1487 x86_branch32(iop->ob_ptr,(cond) ? X86_CC_Z : X86_CC_NZ,0,FALSE);
1488 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1489 x86_patch(jump_ptr,iop->ob_ptr);
1490 }
1491
1492 ppc32_op_emit_branch_target(cpu,b,new_ia);
1493 return(0);
1494 }
1495
1496 /* BC - Branch Conditional */
1497 DECLARE_INSN(BC)
1498 {
1499 int bo = bits(insn,21,25);
1500 int bi = bits(insn,16,20);
1501 int bd = bits(insn,2,15);
1502 int hreg_t0,hreg_t1;
1503 jit_op_t *iop;
1504 u_int cr_field,cr_bit;
1505 m_uint32_t new_ia;
1506 u_char *jump_ptr;
1507 int local_jump;
1508 int cond,ctr;
1509
1510 ppc32_op_emit_basic_opcode(cpu,JIT_OP_BRANCH_JUMP);
1511
1512 iop = ppc32_op_emit_insn_output(cpu,5,"bc");
1513
1514 ppc32_jit_start_hreg_seq(cpu,"bc");
1515 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
1516 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
1517
1518 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
1519
1520 /* Get the wanted value for the condition bit and CTR value */
1521 cond = (bo >> 3) & 0x1;
1522 ctr = (bo >> 1) & 0x1;
1523
1524 /* Set the return address */
1525 if (insn & 1) {
1526 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
1527 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1)<<2));
1528 }
1529
1530 /* Compute the new ia */
1531 new_ia = sign_extend_32(bd << 2,16);
1532 if (!(insn & 0x02))
1533 new_ia += b->start_ia + (b->ppc_trans_pos << 2);
1534
1535 x86_mov_reg_imm(iop->ob_ptr,hreg_t0,1);
1536
1537 /* Decrement the count register */
1538 if (!(bo & 0x04)) {
1539 x86_dec_membase(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ctr));
1540 x86_set_reg(iop->ob_ptr,(ctr) ? X86_CC_Z : X86_CC_NZ,hreg_t1,FALSE);
1541 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,hreg_t1);
1542 }
1543
1544 /* Test the condition bit */
1545 if (!((bo >> 4) & 0x01)) {
1546 cr_field = ppc32_get_cr_field(bi);
1547 cr_bit = ppc32_get_cr_bit(bi);
1548
1549 ppc32_op_emit_require_flags(cpu,cr_field);
1550
1551 x86_test_membase_imm(iop->ob_ptr,
1552 X86_EDI,PPC32_CR_FIELD_OFFSET(cr_field),
1553 (1 << cr_bit));
1554
1555 x86_set_reg(iop->ob_ptr,(cond) ? X86_CC_NZ : X86_CC_Z,hreg_t1,FALSE);
1556 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,hreg_t1);
1557 }
1558
1559 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1560
1561 local_jump = ppc32_jit_tcb_local_addr(b,new_ia,&jump_ptr);
1562
1563 /*
1564 * Optimize the jump, depending if the destination is in the same
1565 * page or not.
1566 */
1567 if (local_jump) {
1568 ppc32_jit_tcb_record_patch(b,iop,iop->ob_ptr,new_ia);
1569 x86_branch32(iop->ob_ptr,X86_CC_NZ,0,FALSE);
1570 } else {
1571 jump_ptr = iop->ob_ptr;
1572 x86_branch32(iop->ob_ptr,X86_CC_Z,0,FALSE);
1573 ppc32_set_jump(cpu,b,iop,new_ia,TRUE);
1574 x86_patch(jump_ptr,iop->ob_ptr);
1575 }
1576
1577 ppc32_op_emit_branch_target(cpu,b,new_ia);
1578
1579 ppc32_jit_close_hreg_seq(cpu);
1580 return(0);
1581 }
1582
1583 /* BCLR - Branch Conditional to Link register */
1584 DECLARE_INSN(BCLR)
1585 {
1586 int bo = bits(insn,21,25);
1587 int bi = bits(insn,16,20);
1588 int bd = bits(insn,2,15);
1589 int hreg_t0,hreg_t1;
1590 jit_op_t *iop;
1591 u_int cr_field,cr_bit;
1592 m_uint32_t new_ia;
1593 u_char *jump_ptr;
1594 int cond,ctr;
1595
1596 ppc32_jit_start_hreg_seq(cpu,"bclr");
1597 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
1598 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
1599
1600 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
1601
1602 iop = ppc32_op_emit_insn_output(cpu,5,"bclr");
1603
1604 /* Get the wanted value for the condition bit and CTR value */
1605 cond = (bo >> 3) & 0x1;
1606 ctr = (bo >> 1) & 0x1;
1607
1608 /* Compute the new ia */
1609 new_ia = sign_extend_32(bd << 2,16);
1610 if (!(insn & 0x02))
1611 new_ia += b->start_ia + (b->ppc_trans_pos << 2);
1612
1613 x86_mov_reg_imm(iop->ob_ptr,hreg_t0,1);
1614
1615 /* Decrement the count register */
1616 if (!(bo & 0x04)) {
1617 x86_dec_membase(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ctr));
1618 x86_set_reg(iop->ob_ptr,(ctr) ? X86_CC_Z : X86_CC_NZ,hreg_t1,FALSE);
1619 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,hreg_t1);
1620 }
1621
1622 /* Test the condition bit */
1623 if (!((bo >> 4) & 0x01)) {
1624 cr_field = ppc32_get_cr_field(bi);
1625 cr_bit = ppc32_get_cr_bit(bi);
1626
1627 ppc32_op_emit_require_flags(cpu,cr_field);
1628
1629 x86_test_membase_imm(iop->ob_ptr,
1630 X86_EDI,PPC32_CR_FIELD_OFFSET(cr_field),
1631 (1 << cr_bit));
1632
1633 x86_set_reg(iop->ob_ptr,(cond) ? X86_CC_NZ : X86_CC_Z,hreg_t1,FALSE);
1634 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,hreg_t1);
1635 }
1636
1637 /* Set the return address */
1638 x86_mov_reg_membase(iop->ob_ptr,hreg_t1,X86_EDI,OFFSET(cpu_ppc_t,lr),4);
1639
1640 if (insn & 1) {
1641 ppc32_set_lr(iop,b->start_ia + ((b->ppc_trans_pos+1) << 2));
1642 ppc32_op_emit_branch_target(cpu,b,b->start_ia+((b->ppc_trans_pos+1)<<2));
1643 }
1644
1645 /* Branching */
1646 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1647
1648 jump_ptr = iop->ob_ptr;
1649 x86_branch32(iop->ob_ptr,X86_CC_Z,0,FALSE);
1650
1651 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t1,0xFFFFFFFC);
1652 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,ia),hreg_t1,4);
1653 ppc32_jit_tcb_push_epilog(&iop->ob_ptr);
1654
1655 x86_patch(jump_ptr,iop->ob_ptr);
1656
1657 ppc32_op_emit_basic_opcode(cpu,JIT_OP_EOB);
1658
1659 ppc32_jit_close_hreg_seq(cpu);
1660 return(0);
1661 }
1662
1663 /* CMP - Compare */
1664 DECLARE_INSN(CMP)
1665 {
1666 int rd = bits(insn,23,25);
1667 int ra = bits(insn,16,20);
1668 int rb = bits(insn,11,15);
1669 int hreg_ra,hreg_rb;
1670 jit_op_t *iop;
1671
1672 ppc32_jit_start_hreg_seq(cpu,"cmp");
1673 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1674 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
1675
1676 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1677 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
1678
1679 iop = ppc32_op_emit_insn_output(cpu,1,"cmp");
1680
1681 x86_alu_reg_reg(iop->ob_ptr,X86_CMP,hreg_ra,hreg_rb);
1682 ppc32_op_emit_update_flags(cpu,rd,TRUE);
1683
1684 ppc32_jit_close_hreg_seq(cpu);
1685 return(0);
1686 }
1687
1688 /* CMPI - Compare Immediate */
1689 DECLARE_INSN(CMPI)
1690 {
1691 int rd = bits(insn,23,25);
1692 int ra = bits(insn,16,20);
1693 m_uint16_t imm = bits(insn,0,15);
1694 m_uint32_t tmp = sign_extend_32(imm,16);
1695 int hreg_ra;
1696 jit_op_t *iop;
1697
1698 ppc32_jit_start_hreg_seq(cpu,"cmpi");
1699 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1700 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1701
1702 iop = ppc32_op_emit_insn_output(cpu,1,"cmpi");
1703
1704 x86_alu_reg_imm(iop->ob_ptr,X86_CMP,hreg_ra,tmp);
1705 ppc32_op_emit_update_flags(cpu,rd,TRUE);
1706
1707 ppc32_jit_close_hreg_seq(cpu);
1708 return(0);
1709 }
1710
1711 /* CMPL - Compare Logical */
1712 DECLARE_INSN(CMPL)
1713 {
1714 int rd = bits(insn,23,25);
1715 int ra = bits(insn,16,20);
1716 int rb = bits(insn,11,15);
1717 int hreg_ra,hreg_rb;
1718 jit_op_t *iop;
1719
1720 ppc32_jit_start_hreg_seq(cpu,"cmpl");
1721 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1722 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
1723
1724 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1725 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
1726
1727 iop = ppc32_op_emit_insn_output(cpu,1,"cmpl");
1728
1729 x86_alu_reg_reg(iop->ob_ptr,X86_CMP,hreg_ra,hreg_rb);
1730 ppc32_op_emit_update_flags(cpu,rd,FALSE);
1731
1732 ppc32_jit_close_hreg_seq(cpu);
1733 return(0);
1734 }
1735
1736 /* CMPLI - Compare Immediate */
1737 DECLARE_INSN(CMPLI)
1738 {
1739 int rd = bits(insn,23,25);
1740 int ra = bits(insn,16,20);
1741 m_uint32_t imm = bits(insn,0,15);
1742 int hreg_ra;
1743 jit_op_t *iop;
1744
1745 ppc32_jit_start_hreg_seq(cpu,"cmpli");
1746 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
1747 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
1748
1749 iop = ppc32_op_emit_insn_output(cpu,1,"cmpli");
1750
1751 x86_alu_reg_imm(iop->ob_ptr,X86_CMP,hreg_ra,imm);
1752 ppc32_op_emit_update_flags(cpu,rd,FALSE);
1753
1754 ppc32_jit_close_hreg_seq(cpu);
1755 return(0);
1756 }
1757
1758 /* CRAND - Condition Register AND */
1759 DECLARE_INSN(CRAND)
1760 {
1761 int bd = bits(insn,21,25);
1762 int bb = bits(insn,16,20);
1763 int ba = bits(insn,11,15);
1764 int hreg_t0;
1765 jit_op_t *iop;
1766
1767 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
1768
1769 ppc32_jit_start_hreg_seq(cpu,"crand");
1770 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1771 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
1772
1773 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
1774 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
1775 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
1776
1777 iop = ppc32_op_emit_insn_output(cpu,3,"crand");
1778
1779 /* test $ba bit */
1780 x86_test_membase_imm(iop->ob_ptr,
1781 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
1782 (1 << ppc32_get_cr_bit(ba)));
1783 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
1784
1785 /* test $bb bit */
1786 x86_test_membase_imm(iop->ob_ptr,
1787 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
1788 (1 << ppc32_get_cr_bit(bb)));
1789 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
1790
1791 /* result of AND between $ba and $bb */
1792 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,X86_EDX);
1793 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1794
1795 /* set/clear $bd bit depending on the result */
1796 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
1797 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1798 ~(1 << ppc32_get_cr_bit(bd)));
1799
1800 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
1801 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
1802 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1803 hreg_t0);
1804
1805 ppc32_jit_close_hreg_seq(cpu);
1806 return(0);
1807 }
1808
1809 /* CRANDC - Condition Register AND with Complement */
1810 DECLARE_INSN(CRANDC)
1811 {
1812 int bd = bits(insn,21,25);
1813 int bb = bits(insn,16,20);
1814 int ba = bits(insn,11,15);
1815 int hreg_t0;
1816 jit_op_t *iop;
1817
1818 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
1819
1820 ppc32_jit_start_hreg_seq(cpu,"crandc");
1821 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1822 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
1823
1824 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
1825 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
1826 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
1827
1828 iop = ppc32_op_emit_insn_output(cpu,3,"crandc");
1829
1830 /* test $ba bit */
1831 x86_test_membase_imm(iop->ob_ptr,
1832 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
1833 (1 << ppc32_get_cr_bit(ba)));
1834 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
1835
1836 /* test $bb bit */
1837 x86_test_membase_imm(iop->ob_ptr,
1838 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
1839 (1 << ppc32_get_cr_bit(bb)));
1840 x86_set_reg(iop->ob_ptr,X86_CC_Z,hreg_t0,FALSE);
1841
1842 /* result of AND between $ba and $bb */
1843 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,X86_EDX);
1844 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1845
1846 /* set/clear $bd bit depending on the result */
1847 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
1848 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1849 ~(1 << ppc32_get_cr_bit(bd)));
1850
1851 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
1852 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
1853 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1854 hreg_t0);
1855
1856 ppc32_jit_close_hreg_seq(cpu);
1857 return(0);
1858 }
1859
1860 /* CREQV - Condition Register EQV */
1861 DECLARE_INSN(CREQV)
1862 {
1863 int bd = bits(insn,21,25);
1864 int bb = bits(insn,16,20);
1865 int ba = bits(insn,11,15);
1866 int hreg_t0;
1867 jit_op_t *iop;
1868
1869 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
1870
1871 ppc32_jit_start_hreg_seq(cpu,"creqv");
1872 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1873 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
1874
1875 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
1876 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
1877 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
1878
1879 iop = ppc32_op_emit_insn_output(cpu,3,"creqv");
1880
1881 /* test $ba bit */
1882 x86_test_membase_imm(iop->ob_ptr,
1883 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
1884 (1 << ppc32_get_cr_bit(ba)));
1885 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
1886
1887 /* test $bb bit */
1888 x86_test_membase_imm(iop->ob_ptr,
1889 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
1890 (1 << ppc32_get_cr_bit(bb)));
1891 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
1892
1893 /* result of XOR between $ba and $bb */
1894 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t0,X86_EDX);
1895 x86_not_reg(iop->ob_ptr,hreg_t0);
1896 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1897
1898 /* set/clear $bd bit depending on the result */
1899 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
1900 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1901 ~(1 << ppc32_get_cr_bit(bd)));
1902
1903 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
1904 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
1905 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1906 hreg_t0);
1907
1908 ppc32_jit_close_hreg_seq(cpu);
1909 return(0);
1910 }
1911
1912 /* CRNAND - Condition Register NAND */
1913 DECLARE_INSN(CRNAND)
1914 {
1915 int bd = bits(insn,21,25);
1916 int bb = bits(insn,16,20);
1917 int ba = bits(insn,11,15);
1918 int hreg_t0;
1919 jit_op_t *iop;
1920
1921 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
1922
1923 ppc32_jit_start_hreg_seq(cpu,"crnand");
1924 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1925 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
1926
1927 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
1928 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
1929 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
1930
1931 iop = ppc32_op_emit_insn_output(cpu,3,"crnand");
1932
1933 /* test $ba bit */
1934 x86_test_membase_imm(iop->ob_ptr,
1935 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
1936 (1 << ppc32_get_cr_bit(ba)));
1937 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
1938
1939 /* test $bb bit */
1940 x86_test_membase_imm(iop->ob_ptr,
1941 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
1942 (1 << ppc32_get_cr_bit(bb)));
1943 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
1944
1945 /* result of NAND between $ba and $bb */
1946 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_t0,X86_EDX);
1947 x86_not_reg(iop->ob_ptr,hreg_t0);
1948 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
1949
1950 /* set/clear $bd bit depending on the result */
1951 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
1952 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1953 ~(1 << ppc32_get_cr_bit(bd)));
1954
1955 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
1956 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
1957 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
1958 hreg_t0);
1959
1960 ppc32_jit_close_hreg_seq(cpu);
1961 return(0);
1962 }
1963
1964 /* CRNOR - Condition Register NOR */
1965 DECLARE_INSN(CRNOR)
1966 {
1967 int bd = bits(insn,21,25);
1968 int bb = bits(insn,16,20);
1969 int ba = bits(insn,11,15);
1970 int hreg_t0;
1971 jit_op_t *iop;
1972
1973 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
1974
1975 ppc32_jit_start_hreg_seq(cpu,"crnor");
1976 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
1977 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
1978
1979 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
1980 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
1981 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
1982
1983 iop = ppc32_op_emit_insn_output(cpu,3,"crnor");
1984
1985 /* test $ba bit */
1986 x86_test_membase_imm(iop->ob_ptr,
1987 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
1988 (1 << ppc32_get_cr_bit(ba)));
1989 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
1990
1991 /* test $bb bit */
1992 x86_test_membase_imm(iop->ob_ptr,
1993 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
1994 (1 << ppc32_get_cr_bit(bb)));
1995 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
1996
1997 /* result of NOR between $ba and $bb */
1998 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_t0,X86_EDX);
1999 x86_not_reg(iop->ob_ptr,hreg_t0);
2000 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
2001
2002 /* set/clear $bd bit depending on the result */
2003 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
2004 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2005 ~(1 << ppc32_get_cr_bit(bd)));
2006
2007 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
2008 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
2009 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2010 hreg_t0);
2011
2012 ppc32_jit_close_hreg_seq(cpu);
2013 return(0);
2014 }
2015
2016 /* CROR - Condition Register OR */
2017 DECLARE_INSN(CROR)
2018 {
2019 int bd = bits(insn,21,25);
2020 int bb = bits(insn,16,20);
2021 int ba = bits(insn,11,15);
2022 int hreg_t0;
2023 jit_op_t *iop;
2024
2025 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2026
2027 ppc32_jit_start_hreg_seq(cpu,"cror");
2028 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2029 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2030
2031 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
2032 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
2033 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
2034
2035 iop = ppc32_op_emit_insn_output(cpu,3,"cror");
2036
2037 /* test $ba bit */
2038 x86_test_membase_imm(iop->ob_ptr,
2039 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
2040 (1 << ppc32_get_cr_bit(ba)));
2041 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
2042
2043 /* test $bb bit */
2044 x86_test_membase_imm(iop->ob_ptr,
2045 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
2046 (1 << ppc32_get_cr_bit(bb)));
2047 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
2048
2049 /* result of OR between $ba and $bb */
2050 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_t0,X86_EDX);
2051 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
2052
2053 /* set/clear $bd bit depending on the result */
2054 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
2055 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2056 ~(1 << ppc32_get_cr_bit(bd)));
2057
2058 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
2059 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
2060 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2061 hreg_t0);
2062
2063 ppc32_jit_close_hreg_seq(cpu);
2064 return(0);
2065 }
2066
2067 /* CRORC - Condition Register OR with Complement */
2068 DECLARE_INSN(CRORC)
2069 {
2070 int bd = bits(insn,21,25);
2071 int bb = bits(insn,16,20);
2072 int ba = bits(insn,11,15);
2073 int hreg_t0;
2074 jit_op_t *iop;
2075
2076 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2077
2078 ppc32_jit_start_hreg_seq(cpu,"crorc");
2079 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2080 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2081
2082 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
2083 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
2084 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
2085
2086 iop = ppc32_op_emit_insn_output(cpu,3,"crorc");
2087
2088 /* test $ba bit */
2089 x86_test_membase_imm(iop->ob_ptr,
2090 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
2091 (1 << ppc32_get_cr_bit(ba)));
2092 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
2093
2094 /* test $bb bit */
2095 x86_test_membase_imm(iop->ob_ptr,
2096 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
2097 (1 << ppc32_get_cr_bit(bb)));
2098 x86_set_reg(iop->ob_ptr,X86_CC_Z,hreg_t0,FALSE);
2099
2100 /* result of ORC between $ba and $bb */
2101 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_t0,X86_EDX);
2102 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
2103
2104 /* set/clear $bd bit depending on the result */
2105 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
2106 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2107 ~(1 << ppc32_get_cr_bit(bd)));
2108
2109 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
2110 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
2111 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2112 hreg_t0);
2113
2114 ppc32_jit_close_hreg_seq(cpu);
2115 return(0);
2116 }
2117
2118 /* CRXOR - Condition Register XOR */
2119 DECLARE_INSN(CRXOR)
2120 {
2121 int bd = bits(insn,21,25);
2122 int bb = bits(insn,16,20);
2123 int ba = bits(insn,11,15);
2124 int hreg_t0;
2125 jit_op_t *iop;
2126
2127 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2128
2129 ppc32_jit_start_hreg_seq(cpu,"crxor");
2130 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2131 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2132
2133 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(ba));
2134 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bb));
2135 ppc32_op_emit_require_flags(cpu,ppc32_get_cr_field(bd));
2136
2137 iop = ppc32_op_emit_insn_output(cpu,3,"crxor");
2138
2139 /* test $ba bit */
2140 x86_test_membase_imm(iop->ob_ptr,
2141 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(ba)),
2142 (1 << ppc32_get_cr_bit(ba)));
2143 x86_set_reg(iop->ob_ptr,X86_CC_NZ,X86_EDX,FALSE);
2144
2145 /* test $bb bit */
2146 x86_test_membase_imm(iop->ob_ptr,
2147 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bb)),
2148 (1 << ppc32_get_cr_bit(bb)));
2149 x86_set_reg(iop->ob_ptr,X86_CC_NZ,hreg_t0,FALSE);
2150
2151 /* result of XOR between $ba and $bb */
2152 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t0,X86_EDX);
2153 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x01);
2154
2155 /* set/clear $bd bit depending on the result */
2156 x86_alu_membase_imm(iop->ob_ptr,X86_AND,
2157 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2158 ~(1 << ppc32_get_cr_bit(bd)));
2159
2160 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_t0,ppc32_get_cr_bit(bd));
2161 x86_alu_membase_reg(iop->ob_ptr,X86_OR,
2162 X86_EDI,PPC32_CR_FIELD_OFFSET(ppc32_get_cr_field(bd)),
2163 hreg_t0);
2164
2165 ppc32_jit_close_hreg_seq(cpu);
2166 return(0);
2167 }
2168
2169 /* DIVWU - Divide Word Unsigned */
2170 DECLARE_INSN(DIVWU)
2171 {
2172 int rd = bits(insn,21,25);
2173 int ra = bits(insn,16,20);
2174 int rb = bits(insn,11,15);
2175 int hreg_rb;
2176 jit_op_t *iop;
2177
2178 ppc32_jit_start_hreg_seq(cpu,"divwu");
2179 ppc32_jit_alloc_hreg_forced(cpu,X86_EAX);
2180 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2181 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2182
2183 /* $rd = $ra / $rb */
2184 ppc32_op_emit_load_gpr(cpu,X86_EAX,ra);
2185 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2186
2187 iop = ppc32_op_emit_insn_output(cpu,2,"divwu");
2188 ppc32_load_imm(&iop->ob_ptr,X86_EDX,0);
2189
2190 x86_div_reg(iop->ob_ptr,hreg_rb,0);
2191
2192 if (insn & 1)
2193 x86_test_reg_reg(iop->ob_ptr,X86_EAX,X86_EAX);
2194
2195 ppc32_op_emit_store_gpr(cpu,rd,X86_EAX);
2196
2197 if (insn & 1)
2198 ppc32_op_emit_update_flags(cpu,0,TRUE);
2199
2200 /* edx:eax are directly modified: throw them */
2201 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
2202 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2203
2204 ppc32_jit_close_hreg_seq(cpu);
2205 return(0);
2206 }
2207
2208 /* EQV */
2209 DECLARE_INSN(EQV)
2210 {
2211 int rs = bits(insn,21,25);
2212 int ra = bits(insn,16,20);
2213 int rb = bits(insn,11,15);
2214 int hreg_rs,hreg_ra,hreg_rb;
2215 jit_op_t *iop;
2216
2217 /* $ra = ~($rs ^ $rb) */
2218 ppc32_jit_start_hreg_seq(cpu,"eqv");
2219 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2220 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2221 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2222
2223 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2224 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2225
2226 iop = ppc32_op_emit_insn_output(cpu,1,"eqv");
2227
2228 if (ra == rs)
2229 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rb);
2230 else if (ra == rb)
2231 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rs);
2232 else {
2233 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2234 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rb);
2235 }
2236
2237 x86_not_reg(iop->ob_ptr,hreg_ra);
2238
2239 if (insn & 1)
2240 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2241
2242 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2243
2244 if (insn & 1)
2245 ppc32_op_emit_update_flags(cpu,0,TRUE);
2246
2247 ppc32_jit_close_hreg_seq(cpu);
2248 return(0);
2249 }
2250
2251 /* EXTSB - Extend Sign Byte */
2252 DECLARE_INSN(EXTSB)
2253 {
2254 int rs = bits(insn,21,25);
2255 int ra = bits(insn,16,20);
2256 int hreg_rs,hreg_ra;
2257 jit_op_t *iop;
2258
2259 /* $ra = extsb($rs) */
2260 ppc32_jit_start_hreg_seq(cpu,"extsb");
2261 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2262 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2263
2264 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2265
2266 iop = ppc32_op_emit_insn_output(cpu,2,"extsb");
2267
2268 if (rs != ra)
2269 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2270
2271 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_ra,24);
2272 x86_shift_reg_imm(iop->ob_ptr,X86_SAR,hreg_ra,24);
2273
2274 if (insn & 1)
2275 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2276
2277 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2278
2279 if (insn & 1)
2280 ppc32_op_emit_update_flags(cpu,0,TRUE);
2281
2282 ppc32_jit_close_hreg_seq(cpu);
2283 return(0);
2284 }
2285
2286 /* EXTSH - Extend Sign Word */
2287 DECLARE_INSN(EXTSH)
2288 {
2289 int rs = bits(insn,21,25);
2290 int ra = bits(insn,16,20);
2291 int hreg_rs,hreg_ra;
2292 jit_op_t *iop;
2293
2294 /* $ra = extsh($rs) */
2295 ppc32_jit_start_hreg_seq(cpu,"extsh");
2296 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2297 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2298
2299 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2300
2301 iop = ppc32_op_emit_insn_output(cpu,2,"extsh");
2302
2303 if (rs != ra)
2304 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2305
2306 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_ra,16);
2307 x86_shift_reg_imm(iop->ob_ptr,X86_SAR,hreg_ra,16);
2308
2309 if (insn & 1)
2310 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2311
2312 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2313
2314 if (insn & 1)
2315 ppc32_op_emit_update_flags(cpu,0,TRUE);
2316
2317 ppc32_jit_close_hreg_seq(cpu);
2318 return(0);
2319 }
2320
2321 /* LBZ - Load Byte and Zero */
2322 DECLARE_INSN(LBZ)
2323 {
2324 int rs = bits(insn,21,25);
2325 int ra = bits(insn,16,20);
2326 m_uint16_t offset = bits(insn,0,15);
2327
2328 //ppc32_emit_memop(cpu,b,PPC_MEMOP_LBZ,ra,offset,rs,0);
2329 ppc32_emit_memop_fast(cpu,b,0,PPC_MEMOP_LBZ,ra,offset,rs,
2330 ppc32_memop_fast_lbz);
2331 return(0);
2332 }
2333
2334 /* LBZU - Load Byte and Zero with Update */
2335 DECLARE_INSN(LBZU)
2336 {
2337 int rs = bits(insn,21,25);
2338 int ra = bits(insn,16,20);
2339 m_uint16_t offset = bits(insn,0,15);
2340
2341 ppc32_emit_memop(cpu,b,PPC_MEMOP_LBZ,ra,offset,rs,1);
2342 return(0);
2343 }
2344
2345 /* LBZUX - Load Byte and Zero with Update Indexed */
2346 DECLARE_INSN(LBZUX)
2347 {
2348 int rs = bits(insn,21,25);
2349 int ra = bits(insn,16,20);
2350 int rb = bits(insn,11,15);
2351
2352 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LBZ,ra,rb,rs,1);
2353 return(0);
2354 }
2355
2356 /* LBZX - Load Byte and Zero Indexed */
2357 DECLARE_INSN(LBZX)
2358 {
2359 int rs = bits(insn,21,25);
2360 int ra = bits(insn,16,20);
2361 int rb = bits(insn,11,15);
2362
2363 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LBZ,ra,rb,rs,0);
2364 return(0);
2365 }
2366
2367 /* LHA - Load Half-Word Algebraic */
2368 DECLARE_INSN(LHA)
2369 {
2370 int rs = bits(insn,21,25);
2371 int ra = bits(insn,16,20);
2372 m_uint16_t offset = bits(insn,0,15);
2373
2374 ppc32_emit_memop(cpu,b,PPC_MEMOP_LHA,ra,offset,rs,0);
2375 return(0);
2376 }
2377
2378 /* LHAU - Load Half-Word Algebraic with Update */
2379 DECLARE_INSN(LHAU)
2380 {
2381 int rs = bits(insn,21,25);
2382 int ra = bits(insn,16,20);
2383 m_uint16_t offset = bits(insn,0,15);
2384
2385 ppc32_emit_memop(cpu,b,PPC_MEMOP_LHA,ra,offset,rs,1);
2386 return(0);
2387 }
2388
2389 /* LHAUX - Load Half-Word Algebraic with Update Indexed */
2390 DECLARE_INSN(LHAUX)
2391 {
2392 int rs = bits(insn,21,25);
2393 int ra = bits(insn,16,20);
2394 int rb = bits(insn,11,15);
2395
2396 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LHA,ra,rb,rs,1);
2397 return(0);
2398 }
2399
2400 /* LHAX - Load Half-Word Algebraic Indexed */
2401 DECLARE_INSN(LHAX)
2402 {
2403 int rs = bits(insn,21,25);
2404 int ra = bits(insn,16,20);
2405 int rb = bits(insn,11,15);
2406
2407 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LHA,ra,rb,rs,0);
2408 return(0);
2409 }
2410
2411 /* LHZ - Load Half-Word and Zero */
2412 DECLARE_INSN(LHZ)
2413 {
2414 int rs = bits(insn,21,25);
2415 int ra = bits(insn,16,20);
2416 m_uint16_t offset = bits(insn,0,15);
2417
2418 ppc32_emit_memop(cpu,b,PPC_MEMOP_LHZ,ra,offset,rs,0);
2419 return(0);
2420 }
2421
2422 /* LHZU - Load Half-Word and Zero with Update */
2423 DECLARE_INSN(LHZU)
2424 {
2425 int rs = bits(insn,21,25);
2426 int ra = bits(insn,16,20);
2427 m_uint16_t offset = bits(insn,0,15);
2428
2429 ppc32_emit_memop(cpu,b,PPC_MEMOP_LHZ,ra,offset,rs,1);
2430 return(0);
2431 }
2432
2433 /* LHZUX - Load Half-Word and Zero with Update Indexed */
2434 DECLARE_INSN(LHZUX)
2435 {
2436 int rs = bits(insn,21,25);
2437 int ra = bits(insn,16,20);
2438 int rb = bits(insn,11,15);
2439
2440 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LHZ,ra,rb,rs,1);
2441 return(0);
2442 }
2443
2444 /* LHZX - Load Half-Word and Zero Indexed */
2445 DECLARE_INSN(LHZX)
2446 {
2447 int rs = bits(insn,21,25);
2448 int ra = bits(insn,16,20);
2449 int rb = bits(insn,11,15);
2450
2451 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LHZ,ra,rb,rs,0);
2452 return(0);
2453 }
2454
2455 /* LWZ - Load Word and Zero */
2456 DECLARE_INSN(LWZ)
2457 {
2458 int rs = bits(insn,21,25);
2459 int ra = bits(insn,16,20);
2460 m_uint16_t offset = bits(insn,0,15);
2461
2462 //ppc32_emit_memop(b,PPC_MEMOP_LWZ,ra,offset,rs,0);
2463 ppc32_emit_memop_fast(cpu,b,0,PPC_MEMOP_LWZ,ra,offset,rs,
2464 ppc32_memop_fast_lwz);
2465 return(0);
2466 }
2467
2468 /* LWZU - Load Word and Zero with Update */
2469 DECLARE_INSN(LWZU)
2470 {
2471 int rs = bits(insn,21,25);
2472 int ra = bits(insn,16,20);
2473 m_uint16_t offset = bits(insn,0,15);
2474
2475 ppc32_emit_memop(cpu,b,PPC_MEMOP_LWZ,ra,offset,rs,1);
2476 return(0);
2477 }
2478
2479 /* LWZUX - Load Word and Zero with Update Indexed */
2480 DECLARE_INSN(LWZUX)
2481 {
2482 int rs = bits(insn,21,25);
2483 int ra = bits(insn,16,20);
2484 int rb = bits(insn,11,15);
2485
2486 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LWZ,ra,rb,rs,1);
2487 return(0);
2488 }
2489
2490 /* LWZX - Load Word and Zero Indexed */
2491 DECLARE_INSN(LWZX)
2492 {
2493 int rs = bits(insn,21,25);
2494 int ra = bits(insn,16,20);
2495 int rb = bits(insn,11,15);
2496
2497 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_LWZ,ra,rb,rs,0);
2498 return(0);
2499 }
2500
2501 /* MCRF - Move Condition Register Field */
2502 DECLARE_INSN(MCRF)
2503 {
2504 int rd = bits(insn,23,25);
2505 int rs = bits(insn,18,20);
2506 int hreg_t0;
2507 jit_op_t *iop;
2508
2509 ppc32_jit_start_hreg_seq(cpu,"mcrf");
2510 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2511 ppc32_op_emit_require_flags(cpu,rs);
2512
2513 iop = ppc32_op_emit_insn_output(cpu,1,"mcrf");
2514
2515 /* Load "rs" field in %edx */
2516 x86_mov_reg_membase(iop->ob_ptr,hreg_t0,
2517 X86_EDI,PPC32_CR_FIELD_OFFSET(rs),4);
2518
2519 /* Store it in "rd" field */
2520 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,PPC32_CR_FIELD_OFFSET(rd),
2521 hreg_t0,4);
2522
2523 ppc32_jit_close_hreg_seq(cpu);
2524 return(0);
2525 }
2526
2527 /* MFCR - Move from Condition Register */
2528 DECLARE_INSN(MFCR)
2529 {
2530 int rd = bits(insn,21,25);
2531 int hreg_rd,hreg_t0;
2532 jit_op_t *iop;
2533 int i;
2534
2535 ppc32_jit_start_hreg_seq(cpu,"mfcr");
2536 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
2537 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2538 ppc32_op_emit_require_flags(cpu,JIT_OP_PPC_ALL_FLAGS);
2539
2540 iop = ppc32_op_emit_insn_output(cpu,3,"mfcr");
2541
2542 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_rd,hreg_rd);
2543
2544 for(i=0;i<8;i++) {
2545 /* load field in %edx */
2546 x86_mov_reg_membase(iop->ob_ptr,hreg_t0,
2547 X86_EDI,PPC32_CR_FIELD_OFFSET(i),4);
2548 x86_shift_reg_imm(iop->ob_ptr,X86_SHL,hreg_rd,4);
2549 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_rd,hreg_t0);
2550 }
2551
2552 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
2553
2554 ppc32_jit_close_hreg_seq(cpu);
2555 return(0);
2556 }
2557
2558 /* MFMSR - Move from Machine State Register */
2559 DECLARE_INSN(MFMSR)
2560 {
2561 int rd = bits(insn,21,25);
2562 int hreg_rd;
2563 jit_op_t *iop;
2564
2565 ppc32_jit_start_hreg_seq(cpu,"mfmsr");
2566 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
2567
2568 iop = ppc32_op_emit_insn_output(cpu,1,"mfmsr");
2569 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,X86_EDI,OFFSET(cpu_ppc_t,msr),4);
2570 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
2571
2572 ppc32_jit_close_hreg_seq(cpu);
2573 return(0);
2574 }
2575
2576 /* MFSR - Move From Segment Register */
2577 DECLARE_INSN(MFSR)
2578 {
2579 int rd = bits(insn,21,25);
2580 int sr = bits(insn,16,19);
2581 int hreg_rd;
2582 jit_op_t *iop;
2583
2584 ppc32_jit_start_hreg_seq(cpu,"mfsr");
2585 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
2586
2587 iop = ppc32_op_emit_insn_output(cpu,1,"mfsr");
2588
2589 x86_mov_reg_membase(iop->ob_ptr,hreg_rd,
2590 X86_EDI,(OFFSET(cpu_ppc_t,sr) + (sr << 2)),4);
2591 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
2592
2593 ppc32_jit_close_hreg_seq(cpu);
2594 return(0);
2595 }
2596
2597 /* MTCRF - Move to Condition Register Fields */
2598 DECLARE_INSN(MTCRF)
2599 {
2600 int rs = bits(insn,21,25);
2601 int crm = bits(insn,12,19);
2602 int hreg_rs,hreg_t0;
2603 jit_op_t *iop;
2604 int i;
2605
2606 ppc32_jit_start_hreg_seq(cpu,"mtcrf");
2607 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2608 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2609
2610 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2611
2612 iop = ppc32_op_emit_insn_output(cpu,3,"mtcrf");
2613
2614 for(i=0;i<8;i++)
2615 if (crm & (1 << (7 - i))) {
2616 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
2617
2618 if (i != 7)
2619 x86_shift_reg_imm(iop->ob_ptr,X86_SHR,hreg_t0,28 - (i << 2));
2620
2621 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x0F);
2622 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,PPC32_CR_FIELD_OFFSET(i),
2623 hreg_t0,4);
2624 }
2625
2626 ppc32_op_emit_basic_opcode(cpu,JIT_OP_TRASH_FLAGS);
2627
2628 ppc32_jit_close_hreg_seq(cpu);
2629 return(0);
2630 }
2631
2632 /* MULHW - Multiply High Word */
2633 DECLARE_INSN(MULHW)
2634 {
2635 int rd = bits(insn,21,25);
2636 int ra = bits(insn,16,20);
2637 int rb = bits(insn,11,15);
2638 int hreg_rb;
2639 jit_op_t *iop;
2640
2641 ppc32_jit_start_hreg_seq(cpu,"mulhw");
2642 ppc32_jit_alloc_hreg_forced(cpu,X86_EAX);
2643 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2644 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2645
2646 ppc32_op_emit_load_gpr(cpu,X86_EAX,ra);
2647 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2648
2649 /* rd = hi(ra * rb) */
2650 iop = ppc32_op_emit_insn_output(cpu,2,"mulhw");
2651 x86_mul_reg(iop->ob_ptr,hreg_rb,1);
2652
2653 if (insn & 1)
2654 x86_test_reg_reg(iop->ob_ptr,X86_EDX,X86_EDX);
2655
2656 ppc32_op_emit_store_gpr(cpu,rd,X86_EDX);
2657
2658 if (insn & 1)
2659 ppc32_op_emit_update_flags(cpu,0,TRUE);
2660
2661 /* edx:eax are directly modified: throw them */
2662 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
2663 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2664
2665 ppc32_jit_close_hreg_seq(cpu);
2666 return(0);
2667 }
2668
2669 /* MULHWU - Multiply High Word Unsigned */
2670 DECLARE_INSN(MULHWU)
2671 {
2672 int rd = bits(insn,21,25);
2673 int ra = bits(insn,16,20);
2674 int rb = bits(insn,11,15);
2675 int hreg_rb;
2676 jit_op_t *iop;
2677
2678 ppc32_jit_start_hreg_seq(cpu,"mulhwu");
2679 ppc32_jit_alloc_hreg_forced(cpu,X86_EAX);
2680 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2681 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2682
2683 ppc32_op_emit_load_gpr(cpu,X86_EAX,ra);
2684 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2685
2686 /* rd = hi(ra * rb) */
2687 iop = ppc32_op_emit_insn_output(cpu,2,"mulhwu");
2688 x86_mul_reg(iop->ob_ptr,hreg_rb,0);
2689
2690 if (insn & 1)
2691 x86_test_reg_reg(iop->ob_ptr,X86_EDX,X86_EDX);
2692
2693 ppc32_op_emit_store_gpr(cpu,rd,X86_EDX);
2694
2695 if (insn & 1)
2696 ppc32_op_emit_update_flags(cpu,0,TRUE);
2697
2698 /* edx:eax are directly modified: throw them */
2699 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
2700 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2701
2702 ppc32_jit_close_hreg_seq(cpu);
2703 return(0);
2704 }
2705
2706 /* MULLI - Multiply Low Immediate */
2707 DECLARE_INSN(MULLI)
2708 {
2709 int rd = bits(insn,21,25);
2710 int ra = bits(insn,16,20);
2711 m_uint32_t imm = bits(insn,0,15);
2712 int hreg_t0;
2713 jit_op_t *iop;
2714
2715 ppc32_jit_start_hreg_seq(cpu,"mulli");
2716 ppc32_jit_alloc_hreg_forced(cpu,X86_EAX);
2717 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2718 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2719
2720 ppc32_op_emit_load_gpr(cpu,X86_EAX,ra);
2721
2722 /* rd = lo(ra * imm) */
2723 iop = ppc32_op_emit_insn_output(cpu,2,"mulli");
2724
2725 ppc32_load_imm(&iop->ob_ptr,hreg_t0,sign_extend_32(imm,16));
2726 x86_mul_reg(iop->ob_ptr,hreg_t0,1);
2727 ppc32_op_emit_store_gpr(cpu,rd,X86_EAX);
2728
2729 /* edx:eax are directly modified: throw them */
2730 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
2731 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2732
2733 ppc32_jit_close_hreg_seq(cpu);
2734 return(0);
2735 }
2736
2737 /* MULLW - Multiply Low Word */
2738 DECLARE_INSN(MULLW)
2739 {
2740 int rd = bits(insn,21,25);
2741 int ra = bits(insn,16,20);
2742 int rb = bits(insn,11,15);
2743 int hreg_rb;
2744 jit_op_t *iop;
2745
2746 ppc32_jit_start_hreg_seq(cpu,"mullw");
2747 ppc32_jit_alloc_hreg_forced(cpu,X86_EAX);
2748 ppc32_jit_alloc_hreg_forced(cpu,X86_EDX);
2749 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2750
2751 ppc32_op_emit_load_gpr(cpu,X86_EAX,ra);
2752 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2753
2754 /* rd = lo(ra * rb) */
2755 iop = ppc32_op_emit_insn_output(cpu,2,"mullw");
2756 x86_mul_reg(iop->ob_ptr,hreg_rb,1);
2757
2758 if (insn & 1)
2759 x86_test_reg_reg(iop->ob_ptr,X86_EAX,X86_EAX);
2760
2761 ppc32_op_emit_store_gpr(cpu,rd,X86_EAX);
2762
2763 if (insn & 1)
2764 ppc32_op_emit_update_flags(cpu,0,TRUE);
2765
2766 /* edx:eax are directly modified: throw them */
2767 ppc32_op_emit_alter_host_reg(cpu,X86_EAX);
2768 ppc32_op_emit_alter_host_reg(cpu,X86_EDX);
2769
2770 ppc32_jit_close_hreg_seq(cpu);
2771 return(0);
2772 }
2773
2774 /* NAND */
2775 DECLARE_INSN(NAND)
2776 {
2777 int rs = bits(insn,21,25);
2778 int ra = bits(insn,16,20);
2779 int rb = bits(insn,11,15);
2780 int hreg_rs,hreg_ra,hreg_rb;
2781 jit_op_t *iop;
2782
2783 /* $ra = ~($rs & $rb) */
2784 ppc32_jit_start_hreg_seq(cpu,"nand");
2785 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2786 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2787 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2788
2789 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2790 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2791
2792 iop = ppc32_op_emit_insn_output(cpu,2,"nand");
2793
2794 if (ra == rs)
2795 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rb);
2796 else if (ra == rb)
2797 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rs);
2798 else {
2799 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2800 x86_alu_reg_reg(iop->ob_ptr,X86_AND,hreg_ra,hreg_rb);
2801 }
2802
2803 x86_not_reg(iop->ob_ptr,hreg_ra);
2804
2805 if (insn & 1)
2806 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2807
2808 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2809
2810 if (insn & 1)
2811 ppc32_op_emit_update_flags(cpu,0,TRUE);
2812
2813 ppc32_jit_close_hreg_seq(cpu);
2814 return(0);
2815 }
2816
2817 /* NEG */
2818 DECLARE_INSN(NEG)
2819 {
2820 int rd = bits(insn,21,25);
2821 int ra = bits(insn,16,20);
2822 int hreg_rd,hreg_ra;
2823 jit_op_t *iop;
2824
2825 /* $rd = neg($ra) */
2826 ppc32_jit_start_hreg_seq(cpu,"neg");
2827 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2828 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
2829
2830 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
2831
2832 iop = ppc32_op_emit_insn_output(cpu,1,"neg");
2833
2834 if (rd != ra)
2835 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_ra,4);
2836
2837 x86_neg_reg(iop->ob_ptr,hreg_rd);
2838
2839 if (insn & 1)
2840 x86_test_reg_reg(iop->ob_ptr,hreg_rd,hreg_rd);
2841
2842 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
2843
2844 if (insn & 1)
2845 ppc32_op_emit_update_flags(cpu,0,TRUE);
2846
2847 ppc32_jit_close_hreg_seq(cpu);
2848 return(0);
2849 }
2850
2851 /* NOR */
2852 DECLARE_INSN(NOR)
2853 {
2854 int rs = bits(insn,21,25);
2855 int ra = bits(insn,16,20);
2856 int rb = bits(insn,11,15);
2857 int hreg_rs,hreg_ra,hreg_rb;
2858 jit_op_t *iop;
2859
2860 /* $ra = ~($rs | $rb) */
2861 ppc32_jit_start_hreg_seq(cpu,"nor");
2862 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2863 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2864 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2865
2866 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2867 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2868
2869 iop = ppc32_op_emit_insn_output(cpu,2,"nor");
2870
2871 if (ra == rs)
2872 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rb);
2873 else if (ra == rb)
2874 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rs);
2875 else {
2876 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2877 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rb);
2878 }
2879
2880 x86_not_reg(iop->ob_ptr,hreg_ra);
2881
2882 if (insn & 1)
2883 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2884
2885 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2886
2887 if (insn & 1)
2888 ppc32_op_emit_update_flags(cpu,0,TRUE);
2889
2890 ppc32_jit_close_hreg_seq(cpu);
2891 return(0);
2892 }
2893
2894 /* OR */
2895 DECLARE_INSN(OR)
2896 {
2897 int rs = bits(insn,21,25);
2898 int ra = bits(insn,16,20);
2899 int rb = bits(insn,11,15);
2900 int hreg_rs,hreg_ra,hreg_rb;
2901 jit_op_t *iop;
2902
2903 /* $ra = $rs | $rb */
2904 ppc32_jit_start_hreg_seq(cpu,"or");
2905 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2906 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2907 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2908
2909 /* special optimization for move/nop operation */
2910 if (rs == rb) {
2911 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2912 iop = ppc32_op_emit_insn_output(cpu,2,"or");
2913
2914 if (ra != rs)
2915 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2916
2917 if (insn & 1)
2918 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
2919
2920 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2921
2922 if (insn & 1)
2923 ppc32_op_emit_update_flags(cpu,0,TRUE);
2924
2925 ppc32_jit_close_hreg_seq(cpu);
2926 return(0);
2927 }
2928
2929 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2930 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2931
2932 iop = ppc32_op_emit_insn_output(cpu,2,"or");
2933
2934 if (ra == rs) {
2935 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rb);
2936 } else if (ra == rb)
2937 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rs);
2938 else {
2939 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
2940 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_rb);
2941 }
2942
2943 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2944
2945 if (insn & 1)
2946 ppc32_op_emit_update_flags(cpu,0,TRUE);
2947
2948 ppc32_jit_close_hreg_seq(cpu);
2949 return(0);
2950 }
2951
2952 /* OR with Complement */
2953 DECLARE_INSN(ORC)
2954 {
2955 int rs = bits(insn,21,25);
2956 int ra = bits(insn,16,20);
2957 int rb = bits(insn,11,15);
2958 int hreg_rs,hreg_ra,hreg_rb,hreg_t0;
2959 jit_op_t *iop;
2960
2961 /* $ra = $rs & ~$rb */
2962 ppc32_jit_start_hreg_seq(cpu,"orc");
2963 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
2964 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
2965 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
2966
2967 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
2968 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
2969
2970 iop = ppc32_op_emit_insn_output(cpu,1,"orc");
2971
2972 /* $t0 = ~$rb */
2973 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
2974 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rb,4);
2975 x86_not_reg(iop->ob_ptr,hreg_t0);
2976
2977 /* $ra = $rs | $t0 */
2978 if (ra == rs)
2979 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_t0);
2980 else {
2981 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_t0,hreg_rs);
2982 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_t0,4);
2983 }
2984
2985 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
2986
2987 if (insn & 1)
2988 ppc32_op_emit_update_flags(cpu,0,TRUE);
2989
2990 ppc32_jit_close_hreg_seq(cpu);
2991 return(0);
2992 }
2993
2994 /* OR Immediate */
2995 DECLARE_INSN(ORI)
2996 {
2997 int rs = bits(insn,21,25);
2998 int ra = bits(insn,16,20);
2999 m_uint16_t imm = bits(insn,0,15);
3000 m_uint32_t tmp = imm;
3001 int hreg_rs,hreg_ra;
3002 jit_op_t *iop;
3003
3004 /* $ra = $rs | imm */
3005 ppc32_jit_start_hreg_seq(cpu,"ori");
3006 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3007 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3008
3009 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3010
3011 iop = ppc32_op_emit_insn_output(cpu,1,"ori");
3012
3013 if (ra != rs)
3014 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3015
3016 x86_alu_reg_imm(iop->ob_ptr,X86_OR,hreg_ra,tmp);
3017 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3018
3019 ppc32_jit_close_hreg_seq(cpu);
3020 return(0);
3021 }
3022
3023 /* OR Immediate Shifted */
3024 DECLARE_INSN(ORIS)
3025 {
3026 int rs = bits(insn,21,25);
3027 int ra = bits(insn,16,20);
3028 m_uint16_t imm = bits(insn,0,15);
3029 m_uint32_t tmp = imm << 16;
3030 int hreg_rs,hreg_ra;
3031 jit_op_t *iop;
3032
3033 /* $ra = $rs | imm */
3034 ppc32_jit_start_hreg_seq(cpu,"oris");
3035 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3036 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3037
3038 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3039
3040 iop = ppc32_op_emit_insn_output(cpu,1,"oris");
3041
3042 if (ra != rs)
3043 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3044
3045 x86_alu_reg_imm(iop->ob_ptr,X86_OR,hreg_ra,tmp);
3046 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3047
3048 ppc32_jit_close_hreg_seq(cpu);
3049 return(0);
3050 }
3051
3052 /* RLWIMI - Rotate Left Word Immediate then Mask Insert */
3053 DECLARE_INSN(RLWIMI)
3054 {
3055 int rs = bits(insn,21,25);
3056 int ra = bits(insn,16,20);
3057 int sh = bits(insn,11,15);
3058 int mb = bits(insn,6,10);
3059 int me = bits(insn,1,5);
3060 register m_uint32_t mask;
3061 int hreg_rs,hreg_ra,hreg_t0;
3062 jit_op_t *iop;
3063
3064 ppc32_jit_start_hreg_seq(cpu,"rlwimi");
3065 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3066 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3067 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3068
3069 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3070 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3071
3072 mask = ppc32_rotate_mask(mb,me);
3073
3074 iop = ppc32_op_emit_insn_output(cpu,2,"rlwimi");
3075
3076 /* Apply inverse mask to $ra */
3077 if (mask != 0)
3078 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_ra,~mask);
3079
3080 /* Rotate $rs of "sh" bits and apply the mask */
3081 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
3082
3083 if (sh != 0)
3084 x86_shift_reg_imm(iop->ob_ptr,X86_ROL,hreg_t0,sh);
3085
3086 if (mask != 0xFFFFFFFF)
3087 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,mask);
3088
3089 /* Store the result */
3090 x86_alu_reg_reg(iop->ob_ptr,X86_OR,hreg_ra,hreg_t0);
3091 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3092
3093 if (insn & 1)
3094 ppc32_op_emit_update_flags(cpu,0,TRUE);
3095
3096 ppc32_jit_close_hreg_seq(cpu);
3097 return(0);
3098 }
3099
3100 /* RLWINM - Rotate Left Word Immediate AND with Mask */
3101 DECLARE_INSN(RLWINM)
3102 {
3103 int rs = bits(insn,21,25);
3104 int ra = bits(insn,16,20);
3105 int sh = bits(insn,11,15);
3106 int mb = bits(insn,6,10);
3107 int me = bits(insn,1,5);
3108 register m_uint32_t mask;
3109 int hreg_rs,hreg_ra;
3110 jit_op_t *iop;
3111
3112 ppc32_jit_start_hreg_seq(cpu,"rlwinm");
3113 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3114 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3115
3116 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3117
3118 iop = ppc32_op_emit_insn_output(cpu,2,"rlwinm");
3119
3120 /* Rotate $rs of "sh" bits and apply the mask */
3121 mask = ppc32_rotate_mask(mb,me);
3122
3123 if (rs != ra)
3124 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3125
3126 if (sh != 0)
3127 x86_shift_reg_imm(iop->ob_ptr,X86_ROL,hreg_ra,sh);
3128
3129 if (mask != 0xFFFFFFFF)
3130 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_ra,mask);
3131
3132 if (insn & 1)
3133 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
3134
3135 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3136
3137 if (insn & 1)
3138 ppc32_op_emit_update_flags(cpu,0,TRUE);
3139
3140 ppc32_jit_close_hreg_seq(cpu);
3141 return(0);
3142 }
3143
3144 /* RLWNM - Rotate Left Word then Mask Insert */
3145 DECLARE_INSN(RLWNM)
3146 {
3147 int rs = bits(insn,21,25);
3148 int ra = bits(insn,16,20);
3149 int rb = bits(insn,11,15);
3150 int mb = bits(insn,6,10);
3151 int me = bits(insn,1,5);
3152 register m_uint32_t mask;
3153 int hreg_rs,hreg_ra,hreg_t0;
3154 jit_op_t *iop;
3155
3156 /* ecx is directly modified: throw it */
3157 ppc32_op_emit_alter_host_reg(cpu,X86_ECX);
3158
3159 ppc32_jit_start_hreg_seq(cpu,"rlwnm");
3160 ppc32_jit_alloc_hreg_forced(cpu,X86_ECX);
3161
3162 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3163 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3164 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3165
3166 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3167 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3168 ppc32_op_emit_load_gpr(cpu,X86_ECX,rb);
3169
3170 iop = ppc32_op_emit_insn_output(cpu,2,"rlwnm");
3171
3172 /* Load the shift register ("sh") */
3173 mask = ppc32_rotate_mask(mb,me);
3174
3175 /* Rotate $rs and apply the mask */
3176 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
3177
3178 x86_shift_reg(iop->ob_ptr,X86_ROL,hreg_t0);
3179
3180 if (mask != 0xFFFFFFFF)
3181 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,mask);
3182
3183 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_t0,4);
3184
3185 if (insn & 1)
3186 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
3187
3188 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3189
3190 if (insn & 1)
3191 ppc32_op_emit_update_flags(cpu,0,TRUE);
3192
3193 ppc32_jit_close_hreg_seq(cpu);
3194 return(0);
3195 }
3196
3197 /* Shift Left Word */
3198 DECLARE_INSN(SLW)
3199 {
3200 int rs = bits(insn,21,25);
3201 int ra = bits(insn,16,20);
3202 int rb = bits(insn,11,15);
3203 u_char *test1;
3204 int hreg_rs,hreg_ra,hreg_t0;
3205 jit_op_t *iop;
3206
3207 /* ecx is directly modified: throw it */
3208 ppc32_op_emit_alter_host_reg(cpu,X86_ECX);
3209
3210 ppc32_jit_start_hreg_seq(cpu,"slw");
3211 ppc32_jit_alloc_hreg_forced(cpu,X86_ECX);
3212 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3213 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3214 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3215
3216 /* $ra = $rs << $rb. If count >= 32, then null result */
3217 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3218 ppc32_op_emit_load_gpr(cpu,X86_ECX,rb);
3219
3220 iop = ppc32_op_emit_insn_output(cpu,3,"slw");
3221
3222 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t0,hreg_t0);
3223 x86_test_reg_imm(iop->ob_ptr,X86_ECX,0x20);
3224 test1 = iop->ob_ptr;
3225 x86_branch8(iop->ob_ptr, X86_CC_NZ, 0, 1);
3226
3227 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
3228 x86_shift_reg(iop->ob_ptr,X86_SHL,hreg_t0);
3229
3230 /* store the result */
3231 x86_patch(test1,iop->ob_ptr);
3232 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_t0,4);
3233
3234 if (insn & 1)
3235 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
3236
3237 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3238
3239 if (insn & 1)
3240 ppc32_op_emit_update_flags(cpu,0,TRUE);
3241
3242 ppc32_jit_close_hreg_seq(cpu);
3243 return(0);
3244 }
3245
3246 /* SRAWI - Shift Right Algebraic Word Immediate */
3247 DECLARE_INSN(SRAWI)
3248 {
3249 int rs = bits(insn,21,25);
3250 int ra = bits(insn,16,20);
3251 int sh = bits(insn,11,15);
3252 register m_uint32_t mask;
3253 int hreg_rs,hreg_ra,hreg_t0;
3254 jit_op_t *iop;
3255
3256 ppc32_jit_start_hreg_seq(cpu,"srawi");
3257 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3258 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3259 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3260
3261 /* $ra = (int32)$rs >> sh */
3262 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3263
3264 iop = ppc32_op_emit_insn_output(cpu,3,"srawi");
3265 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
3266
3267 if (ra != rs)
3268 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3269 x86_shift_reg_imm(iop->ob_ptr,X86_SAR,hreg_ra,sh);
3270
3271 /* set XER_CA depending on the result */
3272 mask = ~(0xFFFFFFFFU << sh) | 0x80000000;
3273
3274 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,mask);
3275 x86_alu_reg_imm(iop->ob_ptr,X86_CMP,hreg_t0,0x80000000);
3276 x86_set_reg(iop->ob_ptr,X86_CC_A,hreg_t0,FALSE);
3277 x86_alu_reg_imm(iop->ob_ptr,X86_AND,hreg_t0,0x1);
3278 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t0,4);
3279
3280 if (insn & 1)
3281 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
3282
3283 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3284
3285 if (insn & 1)
3286 ppc32_op_emit_update_flags(cpu,0,TRUE);
3287
3288 ppc32_jit_close_hreg_seq(cpu);
3289 return(0);
3290 }
3291
3292 /* Shift Right Word */
3293 DECLARE_INSN(SRW)
3294 {
3295 int rs = bits(insn,21,25);
3296 int ra = bits(insn,16,20);
3297 int rb = bits(insn,11,15);
3298 u_char *test1;
3299 int hreg_rs,hreg_ra,hreg_t0;
3300 jit_op_t *iop;
3301
3302 /* ecx is directly modified: throw it */
3303 ppc32_op_emit_alter_host_reg(cpu,X86_ECX);
3304
3305 ppc32_jit_start_hreg_seq(cpu,"srw");
3306 ppc32_jit_alloc_hreg_forced(cpu,X86_ECX);
3307 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3308 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3309 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3310
3311 /* $ra = $rs >> $rb. If count >= 32, then null result */
3312 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3313 ppc32_op_emit_load_gpr(cpu,X86_ECX,rb);
3314
3315 iop = ppc32_op_emit_insn_output(cpu,3,"srw");
3316
3317 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t0,hreg_t0);
3318 x86_test_reg_imm(iop->ob_ptr,X86_ECX,0x20);
3319 test1 = iop->ob_ptr;
3320 x86_branch8(iop->ob_ptr, X86_CC_NZ, 0, 1);
3321
3322 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rs,4);
3323 x86_shift_reg(iop->ob_ptr,X86_SHR,hreg_t0);
3324
3325 /* store the result */
3326 x86_patch(test1,iop->ob_ptr);
3327 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_t0,4);
3328
3329 if (insn & 1)
3330 x86_test_reg_reg(iop->ob_ptr,hreg_ra,hreg_ra);
3331
3332 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3333
3334 if (insn & 1)
3335 ppc32_op_emit_update_flags(cpu,0,TRUE);
3336
3337 ppc32_jit_close_hreg_seq(cpu);
3338 return(0);
3339 }
3340
3341 /* STB - Store Byte */
3342 DECLARE_INSN(STB)
3343 {
3344 int rs = bits(insn,21,25);
3345 int ra = bits(insn,16,20);
3346 m_uint16_t offset = bits(insn,0,15);
3347
3348 //ppc32_emit_memop(b,PPC_MEMOP_STB,ra,offset,rs,0);
3349 ppc32_emit_memop_fast(cpu,b,1,PPC_MEMOP_STB,ra,offset,rs,
3350 ppc32_memop_fast_stb);
3351 return(0);
3352 }
3353
3354 /* STBU - Store Byte with Update */
3355 DECLARE_INSN(STBU)
3356 {
3357 int rs = bits(insn,21,25);
3358 int ra = bits(insn,16,20);
3359 m_uint16_t offset = bits(insn,0,15);
3360
3361 ppc32_emit_memop(cpu,b,PPC_MEMOP_STB,ra,offset,rs,1);
3362 return(0);
3363 }
3364
3365 /* STBUX - Store Byte with Update Indexed */
3366 DECLARE_INSN(STBUX)
3367 {
3368 int rs = bits(insn,21,25);
3369 int ra = bits(insn,16,20);
3370 int rb = bits(insn,11,15);
3371
3372 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STB,ra,rb,rs,1);
3373 return(0);
3374 }
3375
3376 /* STBUX - Store Byte Indexed */
3377 DECLARE_INSN(STBX)
3378 {
3379 int rs = bits(insn,21,25);
3380 int ra = bits(insn,16,20);
3381 int rb = bits(insn,11,15);
3382
3383 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STB,ra,rb,rs,0);
3384 return(0);
3385 }
3386
3387 /* STH - Store Half-Word */
3388 DECLARE_INSN(STH)
3389 {
3390 int rs = bits(insn,21,25);
3391 int ra = bits(insn,16,20);
3392 m_uint16_t offset = bits(insn,0,15);
3393
3394 ppc32_emit_memop(cpu,b,PPC_MEMOP_STH,ra,offset,rs,0);
3395 return(0);
3396 }
3397
3398 /* STHU - Store Half-Word with Update */
3399 DECLARE_INSN(STHU)
3400 {
3401 int rs = bits(insn,21,25);
3402 int ra = bits(insn,16,20);
3403 m_uint16_t offset = bits(insn,0,15);
3404
3405 ppc32_emit_memop(cpu,b,PPC_MEMOP_STH,ra,offset,rs,1);
3406 return(0);
3407 }
3408
3409 /* STHUX - Store Half-Word with Update Indexed */
3410 DECLARE_INSN(STHUX)
3411 {
3412 int rs = bits(insn,21,25);
3413 int ra = bits(insn,16,20);
3414 int rb = bits(insn,11,15);
3415
3416 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STH,ra,rb,rs,1);
3417 return(0);
3418 }
3419
3420 /* STHUX - Store Half-Word Indexed */
3421 DECLARE_INSN(STHX)
3422 {
3423 int rs = bits(insn,21,25);
3424 int ra = bits(insn,16,20);
3425 int rb = bits(insn,11,15);
3426
3427 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STH,ra,rb,rs,0);
3428 return(0);
3429 }
3430
3431 /* STW - Store Word */
3432 DECLARE_INSN(STW)
3433 {
3434 int rs = bits(insn,21,25);
3435 int ra = bits(insn,16,20);
3436 m_uint16_t offset = bits(insn,0,15);
3437
3438 //ppc32_emit_memop(b,PPC_MEMOP_STW,ra,offset,rs,0);
3439 ppc32_emit_memop_fast(cpu,b,1,PPC_MEMOP_STW,ra,offset,rs,
3440 ppc32_memop_fast_stw);
3441 return(0);
3442 }
3443
3444 /* STWU - Store Word with Update */
3445 DECLARE_INSN(STWU)
3446 {
3447 int rs = bits(insn,21,25);
3448 int ra = bits(insn,16,20);
3449 m_uint16_t offset = bits(insn,0,15);
3450
3451 ppc32_emit_memop(cpu,b,PPC_MEMOP_STW,ra,offset,rs,1);
3452 return(0);
3453 }
3454
3455 /* STWUX - Store Word with Update Indexed */
3456 DECLARE_INSN(STWUX)
3457 {
3458 int rs = bits(insn,21,25);
3459 int ra = bits(insn,16,20);
3460 int rb = bits(insn,11,15);
3461
3462 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STW,ra,rb,rs,1);
3463 return(0);
3464 }
3465
3466 /* STWUX - Store Word Indexed */
3467 DECLARE_INSN(STWX)
3468 {
3469 int rs = bits(insn,21,25);
3470 int ra = bits(insn,16,20);
3471 int rb = bits(insn,11,15);
3472
3473 ppc32_emit_memop_idx(cpu,b,PPC_MEMOP_STW,ra,rb,rs,0);
3474 return(0);
3475 }
3476
3477 /* SUBF - Subtract From */
3478 DECLARE_INSN(SUBF)
3479 {
3480 int rd = bits(insn,21,25);
3481 int ra = bits(insn,16,20);
3482 int rb = bits(insn,11,15);
3483 int hreg_rd,hreg_ra,hreg_rb,hreg_t0;
3484 jit_op_t *iop;
3485
3486 /* $rd = $rb - $ra */
3487 ppc32_jit_start_hreg_seq(cpu,"subf");
3488 hreg_t0 = ppc32_jit_get_tmp_hreg(cpu);
3489
3490 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
3491 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3492 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
3493
3494 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3495 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
3496
3497 iop = ppc32_op_emit_insn_output(cpu,2,"subf");
3498
3499 if (rd == rb)
3500 x86_alu_reg_reg(iop->ob_ptr,X86_SUB,hreg_rd,hreg_ra);
3501 else if (rd == ra) {
3502 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_rb,4);
3503 x86_alu_reg_reg(iop->ob_ptr,X86_SUB,hreg_t0,hreg_ra);
3504 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_t0,4);
3505 } else {
3506 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_rb,4);
3507 x86_alu_reg_reg(iop->ob_ptr,X86_SUB,hreg_rd,hreg_ra);
3508 }
3509
3510 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
3511
3512 if (insn & 1)
3513 ppc32_op_emit_update_flags(cpu,0,TRUE);
3514
3515 ppc32_jit_close_hreg_seq(cpu);
3516 return(0);
3517 }
3518
3519 /* SUBFC - Subtract From Carrying */
3520 DECLARE_INSN(SUBFC)
3521 {
3522 int rd = bits(insn,21,25);
3523 int ra = bits(insn,16,20);
3524 int rb = bits(insn,11,15);
3525 int hreg_ra,hreg_rb,hreg_rd,hreg_t0,hreg_t1;
3526 jit_op_t *iop;
3527
3528 /* $rd = ~$ra + 1 + $rb */
3529 ppc32_jit_start_hreg_seq(cpu,"subfc");
3530 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3531 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
3532 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
3533
3534 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
3535 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
3536
3537 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
3538 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3539 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
3540
3541 iop = ppc32_op_emit_insn_output(cpu,3,"subfc");
3542
3543 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t1,hreg_t1);
3544
3545 /* $t0 = ~$ra + 1 */
3546 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_ra,4);
3547 x86_not_reg(iop->ob_ptr,hreg_t0);
3548 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_t0,1);
3549 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3550 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t1,4);
3551
3552 /* $t0 += $rb */
3553 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_t0,hreg_rb);
3554 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3555 x86_alu_membase_reg(iop->ob_ptr,X86_OR,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),
3556 hreg_t1);
3557
3558 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_t0,4);
3559
3560 if (insn & 1)
3561 x86_test_reg_reg(iop->ob_ptr,hreg_rd,hreg_rd);
3562
3563 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
3564
3565 /* update cr0 */
3566 if (insn & 1)
3567 ppc32_update_cr0(b);
3568
3569 ppc32_jit_close_hreg_seq(cpu);
3570 return(0);
3571 }
3572
3573 /* SUBFE - Subtract From Extended */
3574 DECLARE_INSN(SUBFE)
3575 {
3576 int rd = bits(insn,21,25);
3577 int ra = bits(insn,16,20);
3578 int rb = bits(insn,11,15);
3579 int hreg_ra,hreg_rb,hreg_rd,hreg_t0,hreg_t1;
3580 jit_op_t *iop;
3581
3582 /* $rd = ~$ra + $carry (xer_ca) + $rb */
3583 ppc32_jit_start_hreg_seq(cpu,"subfe");
3584 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3585 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
3586 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
3587
3588 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
3589 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
3590
3591 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
3592 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3593 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
3594
3595 iop = ppc32_op_emit_insn_output(cpu,3,"subfe");
3596
3597 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t1,hreg_t1);
3598
3599 /* $t0 = ~$ra + $carry */
3600 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_ra,4);
3601 x86_not_reg(iop->ob_ptr,hreg_t0);
3602 x86_alu_reg_membase(iop->ob_ptr,X86_ADD,hreg_t0,
3603 X86_EDI,OFFSET(cpu_ppc_t,xer_ca));
3604
3605 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3606 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t1,4);
3607
3608 /* $t0 += $rb */
3609 x86_alu_reg_reg(iop->ob_ptr,X86_ADD,hreg_t0,hreg_rb);
3610 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3611 x86_alu_membase_reg(iop->ob_ptr,X86_OR,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),
3612 hreg_t1);
3613
3614 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_t0,4);
3615
3616 if (insn & 1)
3617 x86_test_reg_reg(iop->ob_ptr,hreg_rd,hreg_rd);
3618
3619 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
3620
3621 /* update cr0 */
3622 if (insn & 1)
3623 ppc32_update_cr0(b);
3624
3625 ppc32_jit_close_hreg_seq(cpu);
3626 return(0);
3627 }
3628
3629 /* SUBFIC - Subtract From Immediate Carrying */
3630 DECLARE_INSN(SUBFIC)
3631 {
3632 int rd = bits(insn,21,25);
3633 int ra = bits(insn,16,20);
3634 m_uint16_t imm = bits(insn,0,15);
3635 m_uint32_t tmp = sign_extend_32(imm,16);
3636 int hreg_ra,hreg_rd,hreg_t0,hreg_t1;
3637 jit_op_t *iop;
3638
3639 /* $rd = ~$ra + 1 + sign_extend(imm,16) */
3640 ppc32_jit_start_hreg_seq(cpu,"subfic");
3641 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3642 hreg_rd = ppc32_jit_alloc_hreg(cpu,rd);
3643
3644 hreg_t0 = ppc32_jit_alloc_hreg(cpu,-1);
3645 hreg_t1 = ppc32_jit_get_tmp_hreg(cpu);
3646
3647 ppc32_op_emit_alter_host_reg(cpu,hreg_t0);
3648 ppc32_op_emit_load_gpr(cpu,hreg_ra,ra);
3649
3650 iop = ppc32_op_emit_insn_output(cpu,3,"subfic");
3651
3652 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_t1,hreg_t1);
3653
3654 /* $t0 = ~$ra + 1 */
3655 x86_mov_reg_reg(iop->ob_ptr,hreg_t0,hreg_ra,4);
3656 x86_not_reg(iop->ob_ptr,hreg_t0);
3657 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_t0,1);
3658
3659 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3660 x86_mov_membase_reg(iop->ob_ptr,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),hreg_t1,4);
3661
3662 /* $t0 += sign_extend(imm,16) */
3663 x86_alu_reg_imm(iop->ob_ptr,X86_ADD,hreg_t0,tmp);
3664 x86_set_reg(iop->ob_ptr,X86_CC_C,hreg_t1,FALSE);
3665 x86_alu_membase_reg(iop->ob_ptr,X86_OR,X86_EDI,OFFSET(cpu_ppc_t,xer_ca),
3666 hreg_t1);
3667
3668 x86_mov_reg_reg(iop->ob_ptr,hreg_rd,hreg_t0,4);
3669 ppc32_op_emit_store_gpr(cpu,rd,hreg_rd);
3670
3671 ppc32_jit_close_hreg_seq(cpu);
3672 return(0);
3673 }
3674
3675 /* SYNC - Synchronize */
3676 DECLARE_INSN(SYNC)
3677 {
3678 return(0);
3679 }
3680
3681 /* XOR */
3682 DECLARE_INSN(XOR)
3683 {
3684 int rs = bits(insn,21,25);
3685 int ra = bits(insn,16,20);
3686 int rb = bits(insn,11,15);
3687 int hreg_rs,hreg_ra,hreg_rb;
3688 jit_op_t *iop;
3689
3690 /* $ra = $rs ^ $rb */
3691 ppc32_jit_start_hreg_seq(cpu,"xor");
3692 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3693 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3694 hreg_rb = ppc32_jit_alloc_hreg(cpu,rb);
3695
3696 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3697 ppc32_op_emit_load_gpr(cpu,hreg_rb,rb);
3698
3699 iop = ppc32_op_emit_insn_output(cpu,1,"xor");
3700
3701 if (ra == rs)
3702 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rb);
3703 else if (ra == rb)
3704 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rs);
3705 else {
3706 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3707 x86_alu_reg_reg(iop->ob_ptr,X86_XOR,hreg_ra,hreg_rb);
3708 }
3709
3710 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3711
3712 if (insn & 1)
3713 ppc32_op_emit_update_flags(cpu,0,TRUE);
3714
3715 ppc32_jit_close_hreg_seq(cpu);
3716 return(0);
3717 }
3718
3719 /* XORI - XOR Immediate */
3720 DECLARE_INSN(XORI)
3721 {
3722 int rs = bits(insn,21,25);
3723 int ra = bits(insn,16,20);
3724 m_uint32_t imm = bits(insn,0,15);
3725 int hreg_rs,hreg_ra;
3726 jit_op_t *iop;
3727
3728 /* $ra = $rs ^ imm */
3729 ppc32_jit_start_hreg_seq(cpu,"xori");
3730 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3731 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3732
3733 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3734
3735 iop = ppc32_op_emit_insn_output(cpu,1,"xori");
3736
3737 if (ra != rs)
3738 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3739
3740 x86_alu_reg_imm(iop->ob_ptr,X86_XOR,hreg_ra,imm);
3741 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3742
3743 ppc32_jit_close_hreg_seq(cpu);
3744 return(0);
3745 }
3746
3747 /* XORIS - XOR Immediate Shifted */
3748 DECLARE_INSN(XORIS)
3749 {
3750 int rs = bits(insn,21,25);
3751 int ra = bits(insn,16,20);
3752 m_uint16_t imm = bits(insn,0,15);
3753 m_uint32_t tmp = imm << 16;
3754 int hreg_rs,hreg_ra;
3755 jit_op_t *iop;
3756
3757 /* $ra = $rs ^ (imm << 16) */
3758 ppc32_jit_start_hreg_seq(cpu,"xoris");
3759 hreg_rs = ppc32_jit_alloc_hreg(cpu,rs);
3760 hreg_ra = ppc32_jit_alloc_hreg(cpu,ra);
3761
3762 ppc32_op_emit_load_gpr(cpu,hreg_rs,rs);
3763
3764 iop = ppc32_op_emit_insn_output(cpu,1,"xoris");
3765
3766 if (ra != rs)
3767 x86_mov_reg_reg(iop->ob_ptr,hreg_ra,hreg_rs,4);
3768
3769 x86_alu_reg_imm(iop->ob_ptr,X86_XOR,hreg_ra,tmp);
3770 ppc32_op_emit_store_gpr(cpu,ra,hreg_ra);
3771
3772 ppc32_jit_close_hreg_seq(cpu);
3773 return(0);
3774 }
3775
3776 /* PPC instruction array */
3777 struct ppc32_insn_tag ppc32_insn_tags[] = {
3778 { ppc32_emit_BLR , 0xfffffffe , 0x4e800020 },
3779 { ppc32_emit_BCTR , 0xfffffffe , 0x4e800420 },
3780 { ppc32_emit_MFLR , 0xfc1fffff , 0x7c0802a6 },
3781 { ppc32_emit_MTLR , 0xfc1fffff , 0x7c0803a6 },
3782 { ppc32_emit_MFCTR , 0xfc1fffff , 0x7c0902a6 },
3783 { ppc32_emit_MTCTR , 0xfc1fffff , 0x7c0903a6 },
3784 { ppc32_emit_MFTBL , 0xfc1ff7ff , 0x7c0c42e6 },
3785 { ppc32_emit_MFTBU , 0xfc1ff7ff , 0x7c0d42e6 },
3786 { ppc32_emit_ADD , 0xfc0007fe , 0x7c000214 },
3787 { ppc32_emit_ADDC , 0xfc0007fe , 0x7c000014 },
3788 { ppc32_emit_ADDE , 0xfc0007fe , 0x7c000114 },
3789 { ppc32_emit_ADDI , 0xfc000000 , 0x38000000 },
3790 { ppc32_emit_ADDIC , 0xfc000000 , 0x30000000 },
3791 { ppc32_emit_ADDIC_dot , 0xfc000000 , 0x34000000 },
3792 { ppc32_emit_ADDIS , 0xfc000000 , 0x3c000000 },
3793 { ppc32_emit_ADDZE , 0xfc00fffe , 0x7c000194 },
3794 { ppc32_emit_AND , 0xfc0007fe , 0x7c000038 },
3795 { ppc32_emit_ANDC , 0xfc0007fe , 0x7c000078 },
3796 { ppc32_emit_ANDI , 0xfc000000 , 0x70000000 },
3797 { ppc32_emit_ANDIS , 0xfc000000 , 0x74000000 },
3798 { ppc32_emit_B , 0xfc000003 , 0x48000000 },
3799 { ppc32_emit_BA , 0xfc000003 , 0x48000002 },
3800 { ppc32_emit_BL , 0xfc000003 , 0x48000001 },
3801 { ppc32_emit_BLA , 0xfc000003 , 0x48000003 },
3802 { ppc32_emit_BCC , 0xfe800000 , 0x40800000 },
3803 { ppc32_emit_BC , 0xfc000000 , 0x40000000 },
3804 { ppc32_emit_BCLR , 0xfc00fffe , 0x4c000020 },
3805 { ppc32_emit_CMP , 0xfc6007ff , 0x7c000000 },
3806 { ppc32_emit_CMPI , 0xfc600000 , 0x2c000000 },
3807 { ppc32_emit_CMPL , 0xfc6007ff , 0x7c000040 },
3808 { ppc32_emit_CMPLI , 0xfc600000 , 0x28000000 },
3809 { ppc32_emit_CRAND , 0xfc0007ff , 0x4c000202 },
3810 { ppc32_emit_CRANDC , 0xfc0007ff , 0x4c000102 },
3811 { ppc32_emit_CREQV , 0xfc0007ff , 0x4c000242 },
3812 { ppc32_emit_CRNAND , 0xfc0007ff , 0x4c0001c2 },
3813 { ppc32_emit_CRNOR , 0xfc0007ff , 0x4c000042 },
3814 { ppc32_emit_CROR , 0xfc0007ff , 0x4c000382 },
3815 { ppc32_emit_CRORC , 0xfc0007ff , 0x4c000342 },
3816 { ppc32_emit_CRXOR , 0xfc0007ff , 0x4c000182 },
3817 { ppc32_emit_DIVWU , 0xfc0007fe , 0x7c000396 },
3818 { ppc32_emit_EQV , 0xfc0007fe , 0x7c000238 },
3819 { ppc32_emit_EXTSB , 0xfc00fffe , 0x7c000774 },
3820 { ppc32_emit_EXTSH , 0xfc00fffe , 0x7c000734 },
3821 { ppc32_emit_LBZ , 0xfc000000 , 0x88000000 },
3822 { ppc32_emit_LBZU , 0xfc000000 , 0x8c000000 },
3823 { ppc32_emit_LBZUX , 0xfc0007ff , 0x7c0000ee },
3824 { ppc32_emit_LBZX , 0xfc0007ff , 0x7c0000ae },
3825 { ppc32_emit_LHA , 0xfc000000 , 0xa8000000 },
3826 { ppc32_emit_LHAU , 0xfc000000 , 0xac000000 },
3827 { ppc32_emit_LHAUX , 0xfc0007ff , 0x7c0002ee },
3828 { ppc32_emit_LHAX , 0xfc0007ff , 0x7c0002ae },
3829 { ppc32_emit_LHZ , 0xfc000000 , 0xa0000000 },
3830 { ppc32_emit_LHZU , 0xfc000000 , 0xa4000000 },
3831 { ppc32_emit_LHZUX , 0xfc0007ff , 0x7c00026e },
3832 { ppc32_emit_LHZX , 0xfc0007ff , 0x7c00022e },
3833 { ppc32_emit_LWZ , 0xfc000000 , 0x80000000 },
3834 { ppc32_emit_LWZU , 0xfc000000 , 0x84000000 },
3835 { ppc32_emit_LWZUX , 0xfc0007ff , 0x7c00006e },
3836 { ppc32_emit_LWZX , 0xfc0007ff , 0x7c00002e },
3837 { ppc32_emit_MCRF , 0xfc63ffff , 0x4c000000 },
3838 { ppc32_emit_MFCR , 0xfc1fffff , 0x7c000026 },
3839 { ppc32_emit_MFMSR , 0xfc1fffff , 0x7c0000a6 },
3840 { ppc32_emit_MFSR , 0xfc10ffff , 0x7c0004a6 },
3841 { ppc32_emit_MTCRF , 0xfc100fff , 0x7c000120 },
3842 { ppc32_emit_MULHW , 0xfc0007fe , 0x7c000096 },
3843 { ppc32_emit_MULHWU , 0xfc0007fe , 0x7c000016 },
3844 { ppc32_emit_MULLI , 0xfc000000 , 0x1c000000 },
3845 { ppc32_emit_MULLW , 0xfc0007fe , 0x7c0001d6 },
3846 { ppc32_emit_NAND , 0xfc0007fe , 0x7c0003b8 },
3847 { ppc32_emit_NEG , 0xfc00fffe , 0x7c0000d0 },
3848 { ppc32_emit_NOR , 0xfc0007fe , 0x7c0000f8 },
3849 { ppc32_emit_OR , 0xfc0007fe , 0x7c000378 },
3850 { ppc32_emit_ORC , 0xfc0007fe , 0x7c000338 },
3851 { ppc32_emit_ORI , 0xfc000000 , 0x60000000 },
3852 { ppc32_emit_ORIS , 0xfc000000 , 0x64000000 },
3853 { ppc32_emit_RLWIMI , 0xfc000000 , 0x50000000 },
3854 { ppc32_emit_RLWINM , 0xfc000000 , 0x54000000 },
3855 { ppc32_emit_RLWNM , 0xfc000000 , 0x5c000000 },
3856 { ppc32_emit_SLW , 0xfc0007fe , 0x7c000030 },
3857 { ppc32_emit_SRAWI , 0xfc0007fe , 0x7c000670 },
3858 { ppc32_emit_SRW , 0xfc0007fe , 0x7c000430 },
3859 { ppc32_emit_STB , 0xfc000000 , 0x98000000 },
3860 { ppc32_emit_STBU , 0xfc000000 , 0x9c000000 },
3861 { ppc32_emit_STBUX , 0xfc0007ff , 0x7c0001ee },
3862 { ppc32_emit_STBX , 0xfc0007ff , 0x7c0001ae },
3863 { ppc32_emit_STH , 0xfc000000 , 0xb0000000 },
3864 { ppc32_emit_STHU , 0xfc000000 , 0xb4000000 },
3865 { ppc32_emit_STHUX , 0xfc0007ff , 0x7c00036e },
3866 { ppc32_emit_STHX , 0xfc0007ff , 0x7c00032e },
3867 { ppc32_emit_STW , 0xfc000000 , 0x90000000 },
3868 { ppc32_emit_STWU , 0xfc000000 , 0x94000000 },
3869 { ppc32_emit_STWUX , 0xfc0007ff , 0x7c00016e },
3870 { ppc32_emit_STWX , 0xfc0007ff , 0x7c00012e },
3871 { ppc32_emit_SUBF , 0xfc0007fe , 0x7c000050 },
3872 { ppc32_emit_SUBFC , 0xfc0007fe , 0x7c000010 },
3873 { ppc32_emit_SUBFE , 0xfc0007fe , 0x7c000110 },
3874 { ppc32_emit_SUBFIC , 0xfc000000 , 0x20000000 },
3875 { ppc32_emit_SYNC , 0xffffffff , 0x7c0004ac },
3876 { ppc32_emit_XOR , 0xfc0007fe , 0x7c000278 },
3877 { ppc32_emit_XORI , 0xfc000000 , 0x68000000 },
3878 { ppc32_emit_XORIS , 0xfc000000 , 0x6c000000 },
3879 { ppc32_emit_unknown , 0x00000000 , 0x00000000 },
3880 { NULL , 0x00000000 , 0x00000000 },
3881 };