📄 translate.c
字号:
/* * PowerPC emulation for qemu: main translation routines. * * Copyright (c) 2003-2005 Jocelyn Mayer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <stdarg.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <inttypes.h>#include "cpu.h"#include "exec-all.h"#include "disas.h"//#define DO_SINGLE_STEP//#define PPC_DEBUG_DISAS#ifdef USE_DIRECT_JUMP#define TBPARAM(x)#else#define TBPARAM(x) (long)(x)#endifenum {#define DEF(s, n, copy_size) INDEX_op_ ## s,#include "opc.h"#undef DEF NB_OPS,};static uint16_t *gen_opc_ptr;static uint32_t *gen_opparam_ptr;#include "gen-op.h"#define GEN8(func, NAME) \static GenOpFunc *NAME ## _table [8] = { \NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3, \NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \}; \static inline void func(int n) \{ \ NAME ## _table[n](); \}#define GEN16(func, NAME) \static GenOpFunc *NAME ## _table [16] = { \NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3, \NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \}; \static inline void func(int n) \{ \ NAME ## _table[n](); \}#define GEN32(func, NAME) \static GenOpFunc *NAME ## _table [32] = { \NAME ## 0, NAME ## 1, NAME ## 2, NAME ## 3, \NAME ## 4, NAME ## 5, NAME ## 6, NAME ## 7, \NAME ## 8, NAME ## 9, NAME ## 10, NAME ## 11, \NAME ## 12, NAME ## 13, NAME ## 14, NAME ## 15, \NAME ## 16, NAME ## 17, NAME ## 18, NAME ## 19, \NAME ## 20, NAME ## 21, NAME ## 22, NAME ## 23, \NAME ## 24, NAME ## 25, NAME ## 26, NAME ## 27, \NAME ## 28, NAME ## 29, NAME ## 30, NAME ## 31, \}; \static inline void func(int n) \{ \ NAME ## _table[n](); \}/* Condition register moves */GEN8(gen_op_load_crf_T0, gen_op_load_crf_T0_crf);GEN8(gen_op_load_crf_T1, gen_op_load_crf_T1_crf);GEN8(gen_op_store_T0_crf, gen_op_store_T0_crf_crf);GEN8(gen_op_store_T1_crf, gen_op_store_T1_crf_crf);/* Floating point condition and status register moves */GEN8(gen_op_load_fpscr_T0, gen_op_load_fpscr_T0_fpscr);GEN8(gen_op_store_T0_fpscr, gen_op_store_T0_fpscr_fpscr);GEN8(gen_op_clear_fpscr, gen_op_clear_fpscr_fpscr);static GenOpFunc1 *gen_op_store_T0_fpscri_fpscr_table[8] = { &gen_op_store_T0_fpscri_fpscr0, &gen_op_store_T0_fpscri_fpscr1, &gen_op_store_T0_fpscri_fpscr2, &gen_op_store_T0_fpscri_fpscr3, &gen_op_store_T0_fpscri_fpscr4, &gen_op_store_T0_fpscri_fpscr5, &gen_op_store_T0_fpscri_fpscr6, &gen_op_store_T0_fpscri_fpscr7,};static inline void gen_op_store_T0_fpscri(int n, uint8_t param){ (*gen_op_store_T0_fpscri_fpscr_table[n])(param);}/* Segment register moves */GEN16(gen_op_load_sr, gen_op_load_sr);GEN16(gen_op_store_sr, gen_op_store_sr);/* General purpose registers moves */GEN32(gen_op_load_gpr_T0, gen_op_load_gpr_T0_gpr);GEN32(gen_op_load_gpr_T1, gen_op_load_gpr_T1_gpr);GEN32(gen_op_load_gpr_T2, gen_op_load_gpr_T2_gpr);GEN32(gen_op_store_T0_gpr, gen_op_store_T0_gpr_gpr);GEN32(gen_op_store_T1_gpr, gen_op_store_T1_gpr_gpr);GEN32(gen_op_store_T2_gpr, gen_op_store_T2_gpr_gpr);/* floating point registers moves */GEN32(gen_op_load_fpr_FT0, gen_op_load_fpr_FT0_fpr);GEN32(gen_op_load_fpr_FT1, gen_op_load_fpr_FT1_fpr);GEN32(gen_op_load_fpr_FT2, gen_op_load_fpr_FT2_fpr);GEN32(gen_op_store_FT0_fpr, gen_op_store_FT0_fpr_fpr);GEN32(gen_op_store_FT1_fpr, gen_op_store_FT1_fpr_fpr);GEN32(gen_op_store_FT2_fpr, gen_op_store_FT2_fpr_fpr);static uint8_t spr_access[1024 / 2];/* internal defines */typedef struct DisasContext { struct TranslationBlock *tb; target_ulong nip; uint32_t opcode; uint32_t exception; /* Routine used to access memory */ int mem_idx; /* Translation flags */#if !defined(CONFIG_USER_ONLY) int supervisor;#endif int fpu_enabled; ppc_spr_t *spr_cb; /* Needed to check rights for mfspr/mtspr */} DisasContext;struct opc_handler_t { /* invalid bits */ uint32_t inval; /* instruction type */ uint32_t type; /* handler */ void (*handler)(DisasContext *ctx);};#define RET_EXCP(ctx, excp, error) \do { \ if ((ctx)->exception == EXCP_NONE) { \ gen_op_update_nip((ctx)->nip); \ } \ gen_op_raise_exception_err((excp), (error)); \ ctx->exception = (excp); \} while (0)#define RET_INVAL(ctx) \RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_INVAL_INVAL)#define RET_PRIVOPC(ctx) \RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_OPC)#define RET_PRIVREG(ctx) \RET_EXCP((ctx), EXCP_PROGRAM, EXCP_INVAL | EXCP_PRIV_REG)/* Stop translation */static inline void RET_STOP (DisasContext *ctx){ gen_op_update_nip((ctx)->nip); ctx->exception = EXCP_MTMSR;}/* No need to update nip here, as execution flow will change */static inline void RET_CHG_FLOW (DisasContext *ctx){ ctx->exception = EXCP_MTMSR;}#define GEN_HANDLER(name, opc1, opc2, opc3, inval, type) \static void gen_##name (DisasContext *ctx); \GEN_OPCODE(name, opc1, opc2, opc3, inval, type); \static void gen_##name (DisasContext *ctx)typedef struct opcode_t { unsigned char opc1, opc2, opc3;#if HOST_LONG_BITS == 64 /* Explicitely align to 64 bits */ unsigned char pad[5];#else unsigned char pad[1];#endif opc_handler_t handler; const unsigned char *oname;} opcode_t;/*** Instruction decoding ***/#define EXTRACT_HELPER(name, shift, nb) \static inline uint32_t name (uint32_t opcode) \{ \ return (opcode >> (shift)) & ((1 << (nb)) - 1); \}#define EXTRACT_SHELPER(name, shift, nb) \static inline int32_t name (uint32_t opcode) \{ \ return (int16_t)((opcode >> (shift)) & ((1 << (nb)) - 1)); \}/* Opcode part 1 */EXTRACT_HELPER(opc1, 26, 6);/* Opcode part 2 */EXTRACT_HELPER(opc2, 1, 5);/* Opcode part 3 */EXTRACT_HELPER(opc3, 6, 5);/* Update Cr0 flags */EXTRACT_HELPER(Rc, 0, 1);/* Destination */EXTRACT_HELPER(rD, 21, 5);/* Source */EXTRACT_HELPER(rS, 21, 5);/* First operand */EXTRACT_HELPER(rA, 16, 5);/* Second operand */EXTRACT_HELPER(rB, 11, 5);/* Third operand */EXTRACT_HELPER(rC, 6, 5);/*** Get CRn ***/EXTRACT_HELPER(crfD, 23, 3);EXTRACT_HELPER(crfS, 18, 3);EXTRACT_HELPER(crbD, 21, 5);EXTRACT_HELPER(crbA, 16, 5);EXTRACT_HELPER(crbB, 11, 5);/* SPR / TBL */EXTRACT_HELPER(_SPR, 11, 10);static inline uint32_t SPR (uint32_t opcode){ uint32_t sprn = _SPR(opcode); return ((sprn >> 5) & 0x1F) | ((sprn & 0x1F) << 5);}/*** Get constants ***/EXTRACT_HELPER(IMM, 12, 8);/* 16 bits signed immediate value */EXTRACT_SHELPER(SIMM, 0, 16);/* 16 bits unsigned immediate value */EXTRACT_HELPER(UIMM, 0, 16);/* Bit count */EXTRACT_HELPER(NB, 11, 5);/* Shift count */EXTRACT_HELPER(SH, 11, 5);/* Mask start */EXTRACT_HELPER(MB, 6, 5);/* Mask end */EXTRACT_HELPER(ME, 1, 5);/* Trap operand */EXTRACT_HELPER(TO, 21, 5);EXTRACT_HELPER(CRM, 12, 8);EXTRACT_HELPER(FM, 17, 8);EXTRACT_HELPER(SR, 16, 4);EXTRACT_HELPER(FPIMM, 20, 4);/*** Jump target decoding ***//* Displacement */EXTRACT_SHELPER(d, 0, 16);/* Immediate address */static inline uint32_t LI (uint32_t opcode){ return (opcode >> 0) & 0x03FFFFFC;}static inline uint32_t BD (uint32_t opcode){ return (opcode >> 0) & 0xFFFC;}EXTRACT_HELPER(BO, 21, 5);EXTRACT_HELPER(BI, 16, 5);/* Absolute/relative address */EXTRACT_HELPER(AA, 1, 1);/* Link */EXTRACT_HELPER(LK, 0, 1);/* Create a mask between <start> and <end> bits */static inline uint32_t MASK (uint32_t start, uint32_t end){ uint32_t ret; ret = (((uint32_t)(-1)) >> (start)) ^ (((uint32_t)(-1) >> (end)) >> 1); if (start > end) return ~ret; return ret;}#if HOST_LONG_BITS == 64#define OPC_ALIGN 8#else#define OPC_ALIGN 4#endif#if defined(__APPLE__)#define OPCODES_SECTION \ __attribute__ ((section("__TEXT,__opcodes"), unused, aligned (OPC_ALIGN) ))#else#define OPCODES_SECTION \ __attribute__ ((section(".opcodes"), unused, aligned (OPC_ALIGN) ))#endif#define GEN_OPCODE(name, op1, op2, op3, invl, _typ) \OPCODES_SECTION opcode_t opc_##name = { \ .opc1 = op1, \ .opc2 = op2, \ .opc3 = op3, \ .pad = { 0, }, \ .handler = { \ .inval = invl, \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -