src/emu/cpu/ds5002fp/ds5002fp.c
| r0 | r2628 | |
| 1 | /***************************************************************************** |
| 2 | * DS5002FP emulator by Manuel Abadia |
| 3 | * |
| 4 | * The emulator is just a modified version of the MCS-51 Family Emulator by Steve Ellenoff |
| 5 | * |
| 6 | * What has been added? |
| 7 | * - Extra SFRs |
| 8 | * - Bytewide Bus Support |
| 9 | * - Memory Partition and Memory Range |
| 10 | * - Bootstrap Configuration |
| 11 | * - Power Fail Interrupt |
| 12 | * - Timed Access |
| 13 | * - Stop Mode |
| 14 | * - Idle Mode |
| 15 | * |
| 16 | * What is not implemented? |
| 17 | * - Peripherals and Reprogrammable Peripheral Controller |
| 18 | * - CRC-16 |
| 19 | * - Watchdog timer |
| 20 | * |
| 21 | * The main features of the DS5002FP are: |
| 22 | * - 100% code-compatible with 8051 |
| 23 | * - Directly addresses 64kB program/64kB data memory |
| 24 | * - Nonvolatile memory control circuitry |
| 25 | * - 10-year data retention in the absence of power |
| 26 | * - In-system reprogramming via serial port |
| 27 | * - Dedicated memory bus, preserving four 8-bit ports for general purpose I/O |
| 28 | * - Power-fail reset |
| 29 | * - Early warning power-fail interrupt |
| 30 | * - Watchdog timer |
| 31 | * - Accesses up to 128kB on the bytewide bus |
| 32 | * - Decodes memory for 32kB x 8 or 128kB x 8 SRAMs |
| 33 | * - Four additional decoded peripheral-chip enables |
| 34 | * - CRC hardware for checking memory validity |
| 35 | * - Optionally emulates an 8042-style slave interface |
| 36 | * - Memory encryption using an 80-bit encryption key |
| 37 | * - Automatic random generation of encryption keys |
| 38 | * - Self-destruct input for tamper protection |
| 39 | * - Optional top-coating prevents microprobe |
| 40 | * |
| 41 | *****************************************************************************/ |
| 42 | |
| 43 | /***************************************************************************** |
| 44 | * |
| 45 | * i8051.c |
| 46 | * Portable MCS-51 Family Emulator |
| 47 | * |
| 48 | * Chips in the family: |
| 49 | * 8051 Product Line (8031,8051,8751) |
| 50 | * 8052 Product Line (8032,8052,8752) |
| 51 | * 8054 Product Line (8054) |
| 52 | * 8058 Product Line (8058) |
| 53 | * |
| 54 | * Copyright Steve Ellenoff, all rights reserved. |
| 55 | * |
| 56 | * - This source code is released as freeware for non-commercial purposes. |
| 57 | * - You are free to use and redistribute this code in modified or |
| 58 | * unmodified form, provided you list me in the credits. |
| 59 | * - If you modify this source code, you must add a notice to each modified |
| 60 | * source file that it has been changed. If you're a nice person, you |
| 61 | * will clearly mark each change too. :) |
| 62 | * - If you wish to use this for commercial purposes, please contact me at |
| 63 | * sellenoff@hotmail.com |
| 64 | * - The author of this copywritten work reserves the right to change the |
| 65 | * terms of its usage and license at any time, including retroactively |
| 66 | * - This entire notice must remain in the source code. |
| 67 | * |
| 68 | * This work is based on: |
| 69 | * #1) 'Intel(tm) MC51 Microcontroller Family Users Manual' and |
| 70 | * #2) 8051 simulator by Travis Marlatte |
| 71 | * #3) Portable UPI-41/8041/8741/8042/8742 emulator V0.1 by Juergen Buchmueller (MAME CORE) |
| 72 | * |
| 73 | *****************************************************************************/ |
| 74 | |
| 75 | /****************************************************************************** |
| 76 | * Notes: |
| 77 | * *Important*: Internal ROM needs to be treated the same as external rom by the programmer |
| 78 | * creating the driver (ie, use standard cpu rom region) |
| 79 | * |
| 80 | * The term cycles is used here to really refer to clock oscilations, because 1 machine cycle |
| 81 | * actually takes 12 oscilations. |
| 82 | * |
| 83 | * Read/Write/Modify Instruction - |
| 84 | * Data is read from the Port Latch (not the Port Pin!), possibly modified, and |
| 85 | * written back to (the pin? and) the latch! |
| 86 | * |
| 87 | * The following all perform this on a port address.. |
| 88 | * (anl, orl, xrl, jbc, cpl, inc, dec, djnz, mov px.y,c, clr px.y, setb px.y) |
| 89 | * |
| 90 | * Serial UART emulation is not really accurate, but faked enough to work as far as i can tell |
| 91 | * |
| 92 | * August 27,2003: Currently support for only 8031/8051/8751 chips (ie 128 RAM) |
| 93 | * October 14,2003: Added initial support for the 8752 (ie 256 RAM) |
| 94 | * October 22,2003: Full support for the 8752 (ie 256 RAM) |
| 95 | * July 28,2004: Fixed MOVX command and added External Ram Paging Support |
| 96 | * July 31,2004: Added Serial Mode 0 Support & Fixed Interrupt Flags for Serial Port |
| 97 | * |
| 98 | * Todo: Full Timer support (all modes) |
| 99 | * |
| 100 | * NOW Implemented: RAM paging using hardware configured addressing... |
| 101 | * (July 28,2004) the "MOVX a,@R0/R1" and "MOVX @R0/R1,a" commands can use any of the other ports |
| 102 | * to output a page offset into external ram, but it is totally based on the hardware setup. |
| 103 | * |
| 104 | * Timing needs to be implemented via MAME timers perhaps? |
| 105 | * |
| 106 | *****************************************************************************/ |
| 107 | |
| 108 | #include "debugger.h" |
| 109 | #include "deprecat.h" |
| 110 | #include "ds5002fp.h" |
| 111 | |
| 112 | #define VERBOSE 0 |
| 113 | |
| 114 | #define LOG(x) do { if (VERBOSE) logerror x; } while (0) |
| 115 | |
| 116 | |
| 117 | //Prototypes |
| 118 | INLINE void push_pc(void); |
| 119 | INLINE void pop_pc(void); |
| 120 | INLINE void set_parity(void); |
| 121 | INLINE void do_add_flags(UINT8 a, UINT8 data, UINT8 c); |
| 122 | INLINE void do_sub_flags(UINT8 a, UINT8 data, UINT8 c); |
| 123 | INLINE UINT8 check_interrupts(void); |
| 124 | INLINE void update_timer(int cyc); |
| 125 | INLINE void update_serial(int cyc); |
| 126 | INLINE void serial_transmit(UINT8 data); |
| 127 | static READ8_HANDLER(internal_ram_read); |
| 128 | static WRITE8_HANDLER(internal_ram_write); |
| 129 | static READ8_HANDLER(internal_ram_iread); |
| 130 | static READ32_HANDLER(external_ram_iaddr); |
| 131 | static WRITE8_HANDLER(internal_ram_iwrite); |
| 132 | static READ8_HANDLER(sfr_read); |
| 133 | static WRITE8_HANDLER(sfr_write); |
| 134 | static WRITE8_HANDLER( bit_address_w ); |
| 135 | static READ8_HANDLER( bit_address_r ); |
| 136 | |
| 137 | typedef struct { |
| 138 | UINT8 timerbaud; //Flag set if timer overflow is controlling baud |
| 139 | UINT8 sending; //Flag set when uart is sending |
| 140 | UINT8 data_out; //Data to send out |
| 141 | UINT8 bits_to_send; //How many bits left to send when transmitting out the serial port |
| 142 | UINT8 bitcycles; //# of bitcycles passed since last bit was sent |
| 143 | } DS5002FP_UART; |
| 144 | |
| 145 | typedef struct _DS5002FP { |
| 146 | |
| 147 | //Internal stuff |
| 148 | UINT16 ppc; //previous pc |
| 149 | UINT16 pc; //current pc |
| 150 | UINT16 subtype; //specific version of the cpu, ie 8031, or 8051 for example |
| 151 | UINT8 cur_irq; //Holds value of any current IRQ being serviced |
| 152 | UINT8 irq_priority; //Holds value of the current IRQ Priority Level |
| 153 | UINT8 rwm; //Signals that the current instruction is a read/write/modify instruction |
| 154 | UINT8 prev_used_cycles; //Track previous # of used cycles |
| 155 | UINT8 last_int0; //Store state of int0 |
| 156 | UINT8 last_int1; //Store state of int1 |
| 157 | UINT8 last_power_int; //Store state of last power interrupt |
| 158 | UINT8 int_vec; //Pending Interrupt Vector |
| 159 | UINT8 priority_request; //Priority level of incoming new irq |
| 160 | UINT8 previous_ta; //Previous Timed Access value |
| 161 | INT8 ta_window; //Timed Access window |
| 162 | UINT8 range; //Memory Range |
| 163 | const ds5002fp_config *config; //Bootstrap Configuration |
| 164 | //SFR Registers (Note: Appear in order as they do in memory) |
| 165 | UINT8 po; //Port 0 |
| 166 | UINT8 sp; //Stack Pointer |
| 167 | UINT8 dpl; //Data Pointer (DPTR) (Hi bit) |
| 168 | UINT8 dph; //Data Pointer (DPTR) (Lo bit) |
| 169 | UINT8 pcon; //Power Mode Control |
| 170 | UINT8 tcon; //Timer/Counter Control |
| 171 | UINT8 tmod; //Timer/Counter Mode Control |
| 172 | UINT8 tl0; //Timer 0 Lo |
| 173 | UINT8 tl1; //Timer 1 Lo |
| 174 | UINT8 th0; //Timer 0 Hi |
| 175 | UINT8 th1; //Timer 1 Hi |
| 176 | UINT8 p1; //Port 1 |
| 177 | UINT8 scon; //Serial Control |
| 178 | UINT8 sbuf; //Serial Data Buffer |
| 179 | UINT8 p2; //Port 2 |
| 180 | UINT8 ie; //Interrupt Enable |
| 181 | UINT8 p3; //Port 3 |
| 182 | UINT8 ip; //Interrupt Priority |
| 183 | UINT8 crc; //CRC |
| 184 | UINT8 crcl; //CRC Result (low nibble) |
| 185 | UINT8 crch; //CRC Result (high nibble) |
| 186 | UINT8 mcon; //Memory Control |
| 187 | UINT8 ta; //Timed Access |
| 188 | UINT8 rnr; //Random Number Generator |
| 189 | UINT8 psw; //Program Status Word |
| 190 | UINT8 rpctl; //RPC control |
| 191 | UINT8 rps; //RPC status |
| 192 | UINT8 acc; //Accumulator |
| 193 | UINT8 b; //Register B |
| 194 | |
| 195 | //Internal Ram |
| 196 | UINT8 IntRam[0xff+1]; //Max 256 Bytes of Internal RAM (8031/51 have 128, 8032/52 have 256) |
| 197 | |
| 198 | //Interrupt Callback |
| 199 | int (*irq_callback)(int irqline); |
| 200 | |
| 201 | //Serial Port TX/RX Call backs |
| 202 | void (*serial_tx_callback)(int data); //Call back funciton when sending data out of serial port |
| 203 | int (*serial_rx_callback)(void); //Call back function to retrieve data when receiving serial port data |
| 204 | |
| 205 | //Internal Indirect Read/Write Handlers |
| 206 | READ8_HANDLER((*iram_iread)); |
| 207 | WRITE8_HANDLER((*iram_iwrite)); |
| 208 | |
| 209 | //External/Bytewide Ram Address Callback for generating the hardware specific access to external ram |
| 210 | READ32_HANDLER((*ebram_iaddr_callback)); |
| 211 | |
| 212 | } DS5002FP; |
| 213 | |
| 214 | static int ds5002fp_icount; |
| 215 | |
| 216 | static DS5002FP ds5002fp; |
| 217 | static DS5002FP_UART uart; |
| 218 | |
| 219 | //Hold callback functions so they can be set by caller (before the cpu reset) |
| 220 | static void (*hold_serial_tx_callback)(int data); |
| 221 | static int (*hold_serial_rx_callback)(void); |
| 222 | static READ32_HANDLER((*hold_ebram_iaddr_callback)); |
| 223 | |
| 224 | /*Short cuts*/ |
| 225 | |
| 226 | /*************************************************************** |
| 227 | * Read Opcode/Opcode Arguments from Program Code |
| 228 | ***************************************************************/ |
| 229 | #define ROP(pc) cpu_readop(pc) |
| 230 | #define ROP_ARG(pc) cpu_readop_arg(pc) |
| 231 | /***************************************************************************** |
| 232 | * Read a byte from External Code Memory (Usually Program Rom(s) Space) |
| 233 | ***************************************************************************** |
| 234 | This area is mapped from 0-FFFF internally (64K) */ |
| 235 | #define CODEMEM_R(a) (UINT8)program_read_byte_8le(a) |
| 236 | /***************************************************************************** |
| 237 | * Read/Write a byte from/to Expanded/Byte-wide Data Memory (Usually RAM or other I/O) |
| 238 | ***************************************************************************** |
| 239 | This area is mapped from 0-FFFF internally (64K) */ |
| 240 | #define DATAMEM_R(a) (UINT8)data_read_byte_8le(a) |
| 241 | #define DATAMEM_W(a,v) data_write_byte_8le(a,v) |
| 242 | |
| 243 | /*************************************************************** |
| 244 | * Read/Write a byte from/to the Internal RAM |
| 245 | ***************************************************************/ |
| 246 | #define IRAM_R(a) internal_ram_read(Machine,a) |
| 247 | #define IRAM_W(a,v) internal_ram_write(Machine,a,v) |
| 248 | /*************************************************************** |
| 249 | * Read/Write a byte from/to the Internal RAM indirectly (called from indirect addressing) |
| 250 | ***************************************************************/ |
| 251 | #define IRAM_IR(a) internal_ram_iread(Machine,a) |
| 252 | #define IRAM_IW(a,v) internal_ram_iwrite(Machine,a,v) |
| 253 | /*************************************************************** |
| 254 | * Form an Address to Read/Write to External RAM indirectly (called from indirect addressing) |
| 255 | ***************************************************************/ |
| 256 | #define ERAM_ADDR(a,m) external_ram_iaddr(Machine,a,m) |
| 257 | /*************************************************************** |
| 258 | * Read/Write a byte from/to the SFR Registers |
| 259 | ***************************************************************/ |
| 260 | #define SFR_R(a) sfr_read(Machine,a) |
| 261 | #define SFR_W(a,v) sfr_write(Machine,a,v) |
| 262 | /*************************************************************** |
| 263 | * Read/Write a bit from Bit Addressable Memory |
| 264 | ***************************************************************/ |
| 265 | #define BIT_R(a) bit_address_r(Machine,a) |
| 266 | #define BIT_W(a,v) bit_address_w(Machine,a,v) |
| 267 | /*************************************************************** |
| 268 | * Input/Output a byte from given I/O port |
| 269 | ***************************************************************/ |
| 270 | #define IN(port) ((UINT8)io_read_byte(port)) |
| 271 | #define OUT(port,value) io_write_byte(port,value) |
| 272 | /*************************************************************** |
| 273 | * Access the 4 banks of R registers (R0...R7) |
| 274 | ***************************************************************/ |
| 275 | #define R_R(n) ds5002fp.IntRam[(GET_RS*8)+(n)] |
| 276 | /*************************************************************** |
| 277 | * Easy macro for working with 16 bit DPTR |
| 278 | ***************************************************************/ |
| 279 | #define R_DPTR ((R_DPH<<8) | R_DPL) |
| 280 | #define DPTR_W(n) SFR_W(DPH, ((n)>>8)&0xff);\ |
| 281 | SFR_W(DPL, ((n)&0xff)); |
| 282 | /*************************************************************** |
| 283 | * Easy macros for Setting Flags |
| 284 | ***************************************************************/ |
| 285 | /*PSW Flags*/ |
| 286 | #define SET_CY(n) R_PSW = (R_PSW & 0x7f) | (n<<7); //Carry Flag |
| 287 | #define SET_AC(n) R_PSW = (R_PSW & 0xbf) | (n<<6); //Aux.Carry Flag |
| 288 | #define SET_FO(n) R_PSW = (R_PSW & 0xdf) | (n<<5); //User Flag |
| 289 | #define SET_RS(n) R_PSW = (R_PSW & 0xe7) | (n<<3); //R Bank Select |
| 290 | #define SET_OV(n) R_PSW = (R_PSW & 0xfb) | (n<<2); //Overflow Flag |
| 291 | #define SET_P(n) R_PSW = (R_PSW & 0xfe) | (n<<0); //Parity Flag |
| 292 | /*IE Flags*/ |
| 293 | #define SET_EA(n) R_IE = (R_IE & 0x7f) | (n<<7); //Global Interrupt Enable/Disable |
| 294 | #define SET_ES(n) R_IE = (R_IE & 0xef) | (n<<4); //Serial Interrupt Enable/Disable |
| 295 | #define SET_ET1(n) R_IE = (R_IE & 0xf7) | (n<<3); //Timer 1 Interrupt Enable/Disable |
| 296 | #define SET_EX1(n) R_IE = (R_IE & 0xfb) | (n<<2); //External Int 1 Interrupt Enable/Disable |
| 297 | #define SET_ET0(n) R_IE = (R_IE & 0xfd) | (n<<1); //Timer 0 Interrupt Enable/Disable |
| 298 | #define SET_EX0(n) R_IE = (R_IE & 0xfe) | (n<<0); //External Int 0 Interrupt Enable/Disable |
| 299 | /*IP Flags*/ |
| 300 | #define SET_PS0(n) R_IP = (R_IP & 0xef) | (n<<4); //Set Serial Priority Level |
| 301 | #define SET_PT1(n) R_IP = (R_IP & 0xf7) | (n<<3); //Set Timer 1 Priority Level |
| 302 | #define SET_PX1(n) R_IP = (R_IP & 0xfb) | (n<<2); //Set External Int 1 Priority Level |
| 303 | #define SET_PT0(n) R_IP = (R_IP & 0xfd) | (n<<1); //Set Timer 0 Priority Level |
| 304 | #define SET_PX0(n) R_IP = (R_IP & 0xfe) | (n<<0); //Set External Int 0 Priority Level |
| 305 | /*TCON Flags*/ |
| 306 | #define SET_TF1(n) R_TCON = (R_TCON & 0x7f) | (n<<7); //Indicated Timer 1 Overflow Int Triggered |
| 307 | #define SET_TR1(n) R_TCON = (R_TCON & 0xbf) | (n<<6); //IndicateS Timer 1 is running |
| 308 | #define SET_TF0(n) R_TCON = (R_TCON & 0xdf) | (n<<5); //Indicated Timer 0 Overflow Int Triggered |
| 309 | #define SET_TR0(n) R_TCON = (R_TCON & 0xef) | (n<<4); //IndicateS Timer 0 is running |
| 310 | #define SET_IE1(n) R_TCON = (R_TCON & 0xf7) | (n<<3); //Indicated External Int 1 Triggered |
| 311 | #define SET_IT1(n) R_TCON = (R_TCON & 0xfb) | (n<<2); //Indicates how External Int 1 is Triggered |
| 312 | #define SET_IE0(n) R_TCON = (R_TCON & 0xfd) | (n<<1); //Indicated External Int 0 Triggered |
| 313 | #define SET_IT0(n) R_TCON = (R_TCON & 0xfe) | (n<<0); //Indicates how External Int 0 is Triggered |
| 314 | /*SCON Flags*/ |
| 315 | #define SET_SM0(n) R_SCON = (R_SCON & 0x7f) | (n<<7); //Sets Serial Port Mode |
| 316 | #define SET_SM1(n) R_SCON = (R_SCON & 0xbf) | (n<<6); //Sets Serial Port Mode |
| 317 | #define SET_SM2(n) R_SCON = (R_SCON & 0xdf) | (n<<5); //Sets Serial Port Mode (Multiprocesser mode) |
| 318 | #define SET_REN(n) R_SCON = (R_SCON & 0xef) | (n<<4); //Sets Serial Port Receive Enable |
| 319 | #define SET_TB8(n) R_SCON = (R_SCON & 0xf7) | (n<<3); //Transmit 8th Bit |
| 320 | #define SET_RB8(n) R_SCON = (R_SCON & 0xfb) | (n<<2); //Receive 8th Bit |
| 321 | #define SET_TI(n) R_SCON = (R_SCON & 0xfd) | (n<<1); //Indicates Transmit Interrupt Occurred |
| 322 | #define SET_RI(n) R_SCON = (R_SCON & 0xfe) | (n<<0); //Indicates Receive Interrupt Occurred |
| 323 | /*TMOD Flags*/ |
| 324 | #define SET_GATE1(n) R_TMOD = (R_TMOD & 0x7f) | (n<<7); //Timer 1 Gate Mode |
| 325 | #define SET_CT1(n) R_TMOD = (R_TMOD & 0xbf) | (n<<6); //Timer 1 Counter Mode |
| 326 | #define SET_M1_1(n) R_TMOD = (R_TMOD & 0xdf) | (n<<5); //Timer 1 Timer Mode Bit 1 |
| 327 | #define SET_M1_0(n) R_TMOD = (R_TMOD & 0xef) | (n<<4); //Timer 1 Timer Mode Bit 0 |
| 328 | #define SET_GATE0(n) R_TMOD = (R_TMOD & 0xf7) | (n<<3); //Timer 0 Gate Mode |
| 329 | #define SET_CT0(n) R_TMOD = (R_TMOD & 0xfb) | (n<<2); //Timer 0 Counter Mode |
| 330 | #define SET_M0_1(n) R_TMOD = (R_TMOD & 0xfd) | (n<<1); //Timer 0 Timer Mode Bit 1 |
| 331 | #define SET_M0_0(n) R_TMOD = (R_TMOD & 0xfe) | (n<<0); //Timer 0 Timer Mode Bit 0 |
| 332 | /*PCON Flags*/ |
| 333 | #define SET_POR(n) R_PCON = (R_PCON & 0xbf) | (n<<6); //Power On Reset |
| 334 | #define SET_PFW(n) R_PCON = (R_PCON & 0xdf) | (n<<5); //Power Fail Warning |
| 335 | #define SET_WTR(n) R_PCON = (R_PCON & 0xef) | (n<<4); //Watchdog Timer Reset |
| 336 | #define SET_EPFW(n) R_PCON = (R_PCON & 0xf7) | (n<<3); //Enable Power Fail Interrupt |
| 337 | #define SET_EWT(n) R_PCON = (R_PCON & 0xfb) | (n<<2); //Enable Watchdog Timer |
| 338 | #define SET_STOP(n) R_PCON = (R_PCON & 0xfd) | (n<<1); //Stop |
| 339 | #define SET_IDLE(n) R_PCON = (R_PCON & 0xfe) | (n<<0); //Idle |
| 340 | |
| 341 | /*************************************************************** |
| 342 | * Easy macros for Getting Flags |
| 343 | ***************************************************************/ |
| 344 | /*PSW Flags*/ |
| 345 | #define GET_CY ((R_PSW & 0x80)>>7) |
| 346 | #define GET_AC ((R_PSW & 0x40)>>6) |
| 347 | #define GET_FO ((R_PSW & 0x20)>>5) |
| 348 | #define GET_RS ((R_PSW & 0x18)>>3) |
| 349 | #define GET_OV ((R_PSW & 0x04)>>2) |
| 350 | #define GET_P ((R_PSW & 0x01)>>0) |
| 351 | /*IE Flags*/ |
| 352 | #define GET_EA ((R_IE & 0x80)>>7) |
| 353 | #define GET_ET2 ((R_IE & 0x20)>>5) |
| 354 | #define GET_ES ((R_IE & 0x10)>>4) |
| 355 | #define GET_ET1 ((R_IE & 0x08)>>3) |
| 356 | #define GET_EX1 ((R_IE & 0x04)>>2) |
| 357 | #define GET_ET0 ((R_IE & 0x02)>>1) |
| 358 | #define GET_EX0 ((R_IE & 0x01)>>0) |
| 359 | #define GET_PS ((R_IP & 0x10)>>4) |
| 360 | #define GET_PT1 ((R_IP & 0x08)>>3) |
| 361 | #define GET_PX1 ((R_IP & 0x04)>>2) |
| 362 | #define GET_PT0 ((R_IP & 0x02)>>1) |
| 363 | #define GET_PX0 ((R_IP & 0x01)>>0) |
| 364 | /*TCON Flags*/ |
| 365 | #define GET_TF1 ((R_TCON & 0x80)>>7) |
| 366 | #define GET_TR1 ((R_TCON & 0x40)>>6) |
| 367 | #define GET_TF0 ((R_TCON & 0x20)>>5) |
| 368 | #define GET_TR0 ((R_TCON & 0x10)>>4) |
| 369 | #define GET_IE1 ((R_TCON & 0x08)>>3) |
| 370 | #define GET_IT1 ((R_TCON & 0x04)>>2) |
| 371 | #define GET_IE0 ((R_TCON & 0x02)>>1) |
| 372 | #define GET_IT0 ((R_TCON & 0x01)>>0) |
| 373 | /*SCON Flags*/ |
| 374 | #define GET_SM0 ((R_SCON & 0x80)>>7) |
| 375 | #define GET_SM1 ((R_SCON & 0x40)>>6) |
| 376 | #define GET_SM2 ((R_SCON & 0x20)>>5) |
| 377 | #define GET_REN ((R_SCON & 0x10)>>4) |
| 378 | #define GET_TB8 ((R_SCON & 0x08)>>3) |
| 379 | #define GET_RB8 ((R_SCON & 0x04)>>2) |
| 380 | #define GET_TI ((R_SCON & 0x02)>>1) |
| 381 | #define GET_RI ((R_SCON & 0x01)>>0) |
| 382 | /*TMOD Flags*/ |
| 383 | #define GET_GATE1 ((R_TMOD & 0x80)>>7) |
| 384 | #define GET_CT1 ((R_TMOD & 0x40)>>6) |
| 385 | #define GET_M1_1 ((R_TMOD & 0x20)>>5) |
| 386 | #define GET_M1_0 ((R_TMOD & 0x10)>>4) |
| 387 | #define GET_GATE0 ((R_TMOD & 0x08)>>3) |
| 388 | #define GET_CT0 ((R_TMOD & 0x04)>>2) |
| 389 | #define GET_M0_1 ((R_TMOD & 0x02)>>1) |
| 390 | #define GET_M0_0 ((R_TMOD & 0x01)>>0) |
| 391 | /*PCON Flags*/ |
| 392 | #define GET_SMOD ((R_PCON & 0x80)>>7) |
| 393 | #define GET_POR ((R_PCON & 0x40)>>6) |
| 394 | #define GET_PFW ((R_PCON & 0x20)>>5) |
| 395 | #define GET_WTR ((R_PCON & 0x10)>>4) |
| 396 | #define GET_EPFW ((R_PCON & 0x08)>>3) |
| 397 | #define GET_EWT ((R_PCON & 0x04)>>2) |
| 398 | #define GET_STOP ((R_PCON & 0x02)>>1) |
| 399 | #define GET_IDLE ((R_PCON & 0x01)>>0) |
| 400 | /*MCON Flags*/ |
| 401 | #define GET_PA ((R_MCON & 0xf0)>>4) |
| 402 | #define GET_RG1 ((R_MCON & 0x08)>>3) |
| 403 | #define GET_PES ((R_MCON & 0x04)>>2) |
| 404 | #define GET_PM ((R_MCON & 0x02)>>1) |
| 405 | #define GET_SL ((R_MCON & 0x01)>>0) |
| 406 | /*RPCTL Flags*/ |
| 407 | #define GET_RNR ((R_RPCTL & 0x80)>>7) |
| 408 | #define GET_EXBS ((R_RPCTL & 0x20)>>5) |
| 409 | #define GET_AE ((R_RPCTL & 0x10)>>4) |
| 410 | #define GET_IBI ((R_RPCTL & 0x08)>>3) |
| 411 | #define GET_DMA ((R_RPCTL & 0x04)>>2) |
| 412 | #define GET_RPCON ((R_RPCTL & 0x02)>>1) |
| 413 | #define GET_RG0 ((R_RPCTL & 0x01)>>0) |
| 414 | |
| 415 | /*Add and Subtract Flag settings*/ |
| 416 | #define DO_ADD_FLAGS(a,d,c) do_add_flags(a,d,c); |
| 417 | #define DO_SUB_FLAGS(a,d,c) do_sub_flags(a,d,c); |
| 418 | |
| 419 | #define SET_PARITY set_parity(); |
| 420 | #define PUSH_PC push_pc(); |
| 421 | #define POP_PC pop_pc(); |
| 422 | |
| 423 | /* PC vectors */ |
| 424 | #define V_RESET 0x000 /* power on address */ |
| 425 | #define V_IE0 0x003 /* External Interrupt 0 */ |
| 426 | #define V_TF0 0x00b /* Timer 0 Overflow */ |
| 427 | #define V_IE1 0x013 /* External Interrupt 1 */ |
| 428 | #define V_TF1 0x01b /* Timer 1 Overflow */ |
| 429 | #define V_RITI 0x023 /* Serial Receive/Transmit */ |
| 430 | #define V_PFI 0x02b /* Power Failure Interrupt */ |
| 431 | |
| 432 | /* Any pending IRQ */ |
| 433 | #define SERIALPORT_IRQ ((R_SCON & 0x03) && GET_ES) |
| 434 | |
| 435 | /* Clear Current IRQ */ |
| 436 | #define CLEAR_CURRENT_IRQ ds5002fp.cur_irq = 0xff;\ |
| 437 | ds5002fp.irq_priority = 0; |
| 438 | |
| 439 | /* shorter names for the DS5002FP structure elements */ |
| 440 | |
| 441 | //Internal stuff |
| 442 | #define PPC ds5002fp.ppc |
| 443 | #define PC ds5002fp.pc |
| 444 | #define TYPE ds5002fp.subtype |
| 445 | #define RWM ds5002fp.rwm |
| 446 | |
| 447 | //SFR Registers |
| 448 | #define R_P0 ds5002fp.po |
| 449 | #define R_SP ds5002fp.sp |
| 450 | #define R_DPL ds5002fp.dpl |
| 451 | #define R_DPH ds5002fp.dph |
| 452 | #define R_PCON ds5002fp.pcon |
| 453 | #define R_TCON ds5002fp.tcon |
| 454 | #define R_TMOD ds5002fp.tmod |
| 455 | #define R_TL0 ds5002fp.tl0 |
| 456 | #define R_TL1 ds5002fp.tl1 |
| 457 | #define R_TH0 ds5002fp.th0 |
| 458 | #define R_TH1 ds5002fp.th1 |
| 459 | #define R_P1 ds5002fp.p1 |
| 460 | #define R_SCON ds5002fp.scon |
| 461 | #define R_SBUF ds5002fp.sbuf |
| 462 | #define R_P2 ds5002fp.p2 |
| 463 | #define R_IE ds5002fp.ie |
| 464 | #define R_P3 ds5002fp.p3 |
| 465 | #define R_IP ds5002fp.ip |
| 466 | #define R_CRC ds5002fp.crc |
| 467 | #define R_CRCL ds5002fp.crcl |
| 468 | #define R_CRCH ds5002fp.crch |
| 469 | #define R_MCON ds5002fp.mcon |
| 470 | #define R_TA ds5002fp.ta |
| 471 | #define R_RNR ds5002fp.rnr |
| 472 | #define R_PSW ds5002fp.psw |
| 473 | #define R_RPCTL ds5002fp.rpctl |
| 474 | #define R_RPS ds5002fp.rps |
| 475 | #define R_ACC ds5002fp.acc |
| 476 | #define R_B ds5002fp.b |
| 477 | |
| 478 | /* # of oscilations each opcode requires*/ |
| 479 | static const UINT8 ds5002fp_cycles[] = { |
| 480 | 12,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 481 | 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 482 | 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 483 | 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 484 | 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12, |
| 485 | 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12, |
| 486 | 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12, |
| 487 | 24,24,24,24,12,24,12,12,12,12,12,12,12,12,12,12, |
| 488 | 24,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24, |
| 489 | 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12, |
| 490 | 24,24,12,24,48,12,24,24,24,24,24,24,24,24,24,24, |
| 491 | 24,24,12,12,24,24,24,24,24,24,24,24,24,24,24,24, |
| 492 | 24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 493 | 24,24,12,12,12,24,12,12,24,24,24,24,24,24,24,24, |
| 494 | 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12, |
| 495 | 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12 |
| 496 | }; |
| 497 | |
| 498 | /* default configuration */ |
| 499 | static const ds5002fp_config default_config = |
| 500 | { |
| 501 | 0x00, |
| 502 | 0x00, |
| 503 | 0x00 |
| 504 | }; |
| 505 | |
| 506 | /* Memory Range (RG1 and RG0 @ MCON and RPCTL registers) */ |
| 507 | static const UINT16 ds5002fp_ranges[4] = { |
| 508 | 0x1fff, |
| 509 | 0x3fff, |
| 510 | 0x7fff, |
| 511 | 0xffff |
| 512 | }; |
| 513 | |
| 514 | /* Memory Partition Table (RG1 & RG0 @ MCON & RPCTL registers) */ |
| 515 | static const UINT32 ds5002fp_partitions[16] = { |
| 516 | 0x0000, |
| 517 | 0x1000, |
| 518 | 0x2000, |
| 519 | 0x3000, |
| 520 | 0x4000, |
| 521 | 0x5000, |
| 522 | 0x6000, |
| 523 | 0x7000, |
| 524 | 0x8000, |
| 525 | 0x9000, |
| 526 | 0xa000, |
| 527 | 0xb000, |
| 528 | 0xc000, |
| 529 | 0xd000, |
| 530 | 0xe000, |
| 531 | 0x10000 |
| 532 | }; |
| 533 | |
| 534 | /* Include Opcode functions */ |
| 535 | #include "ds5002fpops.c" |
| 536 | |
| 537 | void ds5002fp_init(int index, int clock, const void *_config, int (*irqcallback)(int)) |
| 538 | { |
| 539 | const ds5002fp_config *config = _config ? _config : &default_config; |
| 540 | |
| 541 | ds5002fp.config = config; |
| 542 | ds5002fp_set_irq_callback(irqcallback); |
| 543 | |
| 544 | //Internal stuff |
| 545 | state_save_register_item("ds5002fp", index, ds5002fp.ppc); |
| 546 | state_save_register_item("ds5002fp", index, ds5002fp.pc); |
| 547 | state_save_register_item("ds5002fp", index, ds5002fp.subtype); |
| 548 | state_save_register_item("ds5002fp", index, ds5002fp.rwm ); |
| 549 | state_save_register_item("ds5002fp", index, ds5002fp.cur_irq ); |
| 550 | state_save_register_item("ds5002fp", index, ds5002fp.irq_priority ); |
| 551 | state_save_register_item("ds5002fp", index, ds5002fp.prev_used_cycles ); |
| 552 | state_save_register_item("ds5002fp", index, ds5002fp.last_int0 ); |
| 553 | state_save_register_item("ds5002fp", index, ds5002fp.last_int1 ); |
| 554 | state_save_register_item("ds5002fp", index, ds5002fp.last_power_int ); |
| 555 | state_save_register_item("ds5002fp", index, ds5002fp.int_vec ); |
| 556 | state_save_register_item("ds5002fp", index, ds5002fp.priority_request ); |
| 557 | state_save_register_item("ds5002fp", index, ds5002fp.previous_ta ); |
| 558 | state_save_register_item("ds5002fp", index, ds5002fp.ta_window ); |
| 559 | state_save_register_item("ds5002fp", index, ds5002fp.range ); |
| 560 | //SFR Registers |
| 561 | state_save_register_item("ds5002fp", index, ds5002fp.po); |
| 562 | state_save_register_item("ds5002fp", index, ds5002fp.sp); |
| 563 | state_save_register_item("ds5002fp", index, ds5002fp.dpl); |
| 564 | state_save_register_item("ds5002fp", index, ds5002fp.dph); |
| 565 | state_save_register_item("ds5002fp", index, ds5002fp.pcon); |
| 566 | state_save_register_item("ds5002fp", index, ds5002fp.tcon); |
| 567 | state_save_register_item("ds5002fp", index, ds5002fp.tmod); |
| 568 | state_save_register_item("ds5002fp", index, ds5002fp.tl0); |
| 569 | state_save_register_item("ds5002fp", index, ds5002fp.tl1); |
| 570 | state_save_register_item("ds5002fp", index, ds5002fp.th0); |
| 571 | state_save_register_item("ds5002fp", index, ds5002fp.th1); |
| 572 | state_save_register_item("ds5002fp", index, ds5002fp.p1); |
| 573 | state_save_register_item("ds5002fp", index, ds5002fp.scon); |
| 574 | state_save_register_item("ds5002fp", index, ds5002fp.sbuf); |
| 575 | state_save_register_item("ds5002fp", index, ds5002fp.p2); |
| 576 | state_save_register_item("ds5002fp", index, ds5002fp.ie); |
| 577 | state_save_register_item("ds5002fp", index, ds5002fp.p3); |
| 578 | state_save_register_item("ds5002fp", index, ds5002fp.ip); |
| 579 | state_save_register_item("ds5002fp", index, ds5002fp.crc); |
| 580 | state_save_register_item("ds5002fp", index, ds5002fp.crcl); |
| 581 | state_save_register_item("ds5002fp", index, ds5002fp.crch); |
| 582 | state_save_register_item("ds5002fp", index, ds5002fp.mcon); |
| 583 | state_save_register_item("ds5002fp", index, ds5002fp.ta); |
| 584 | state_save_register_item("ds5002fp", index, ds5002fp.rnr); |
| 585 | state_save_register_item("ds5002fp", index, ds5002fp.psw); |
| 586 | state_save_register_item("ds5002fp", index, ds5002fp.rpctl); |
| 587 | state_save_register_item("ds5002fp", index, ds5002fp.rps); |
| 588 | state_save_register_item("ds5002fp", index, ds5002fp.acc); |
| 589 | state_save_register_item("ds5002fp", index, ds5002fp.b); |
| 590 | state_save_register_item_array("ds5002fp", index, ds5002fp.IntRam); |
| 591 | } |
| 592 | |
| 593 | /* Reset registers to the initial values */ |
| 594 | void ds5002fp_reset(void) |
| 595 | { |
| 596 | int (*save_irqcallback)(int); |
| 597 | const ds5002fp_config *save_config; |
| 598 | |
| 599 | save_irqcallback = ds5002fp.irq_callback; |
| 600 | save_config = ds5002fp.config; |
| 601 | memset(&ds5002fp, 0, sizeof(DS5002FP)); |
| 602 | ds5002fp.irq_callback = save_irqcallback; |
| 603 | ds5002fp.config = save_config; |
| 604 | |
| 605 | memset(&uart, 0, sizeof(DS5002FP_UART)); |
| 606 | ds5002fp.subtype = 8051; |
| 607 | |
| 608 | //Set up 8051 specific internal read/write (indirect) handlers.. |
| 609 | ds5002fp.iram_iread = internal_ram_read; //Indirect ram read/write handled the same as direct for 8051! |
| 610 | ds5002fp.iram_iwrite = internal_ram_write; //Indirect ram read/write handled the same as direct for 8051! |
| 611 | |
| 612 | //Set up serial call back handlers |
| 613 | ds5002fp.serial_tx_callback = hold_serial_tx_callback; |
| 614 | hold_serial_tx_callback = NULL; |
| 615 | ds5002fp.serial_rx_callback = hold_serial_rx_callback; |
| 616 | hold_serial_rx_callback = NULL; |
| 617 | |
| 618 | //Setup Expanded/Byte-wide ram callback handlers |
| 619 | ds5002fp.ebram_iaddr_callback = hold_ebram_iaddr_callback; |
| 620 | hold_ebram_iaddr_callback = NULL; |
| 621 | |
| 622 | //Clear Ram (w/0xff) |
| 623 | memset(&ds5002fp.IntRam, 0xff, sizeof(ds5002fp.IntRam)); |
| 624 | |
| 625 | /* these are all defined reset states */ |
| 626 | PC = 0; |
| 627 | SFR_W(ACC, 0); |
| 628 | SFR_W(B, 0); |
| 629 | SFR_W(PSW, 0); |
| 630 | SFR_W(SP, 0x7); |
| 631 | SFR_W(DPH, 0); |
| 632 | SFR_W(DPL, 0); |
| 633 | SFR_W(P3, 0xff); |
| 634 | SFR_W(P2, 0xff); |
| 635 | SFR_W(P1, 0xff); |
| 636 | SFR_W(P0, 0xff); |
| 637 | SFR_W(IP, 0); |
| 638 | SFR_W(IE, 0); |
| 639 | SFR_W(TMOD, 0); |
| 640 | SFR_W(TCON, 0); |
| 641 | SFR_W(TH1, 0); |
| 642 | SFR_W(TH0, 0); |
| 643 | SFR_W(TL1, 0); |
| 644 | SFR_W(TL0, 0); |
| 645 | SFR_W(SCON, 0); |
| 646 | |
| 647 | // set initial values (some of them are set using the bootstrap loader) |
| 648 | R_PCON = 0; |
| 649 | R_MCON = ds5002fp.config->mcon & 0xfb; |
| 650 | R_RPCTL = ds5002fp.config->rpctl & 0x01; |
| 651 | R_RPS = 0; |
| 652 | R_RNR = 0; |
| 653 | R_CRC = ds5002fp.config->crc & 0xf0; |
| 654 | R_CRCL = 0; |
| 655 | R_CRCH = 0; |
| 656 | R_TA = 0; |
| 657 | |
| 658 | /* Flag as NO IRQ in Progress */ |
| 659 | CLEAR_CURRENT_IRQ |
| 660 | |
| 661 | // set internal CPU state |
| 662 | ds5002fp.last_power_int = CLEAR_LINE; |
| 663 | ds5002fp.previous_ta = 0; |
| 664 | ds5002fp.ta_window = 0; |
| 665 | ds5002fp.range = (GET_RG1 << 1) | GET_RG0; |
| 666 | } |
| 667 | |
| 668 | /* Shut down CPU core */ |
| 669 | void ds5002fp_exit(void) |
| 670 | { |
| 671 | /* nothing to do */ |
| 672 | } |
| 673 | |
| 674 | /* Execute cycles - returns number of cycles actually run */ |
| 675 | int ds5002fp_execute(int cycles) |
| 676 | { |
| 677 | //In stop mode, no op takes place |
| 678 | if (R_PCON & 0x02) { |
| 679 | return cycles; |
| 680 | } |
| 681 | |
| 682 | ds5002fp_icount = cycles; |
| 683 | |
| 684 | //In idle mode, update timers and serial port (not sure if we need to do more things here) |
| 685 | if (R_PCON & 0x01) { |
| 686 | do { |
| 687 | //Update Timer (if any timers are running) |
| 688 | if((GET_TR0 && GET_ET0) || (GET_TR1 && GET_ET1)) |
| 689 | update_timer(12); |
| 690 | |
| 691 | //Update Serial (if serial port sending data) |
| 692 | if(uart.sending) |
| 693 | update_serial(12); |
| 694 | |
| 695 | ds5002fp_icount -= 12; |
| 696 | } while( ds5002fp_icount > 0 ); |
| 697 | |
| 698 | return cycles - ds5002fp_icount; |
| 699 | } |
| 700 | |
| 701 | do { |
| 702 | //Read next opcode |
| 703 | UINT8 op = cpu_readop(PC); |
| 704 | |
| 705 | //Store previous PC |
| 706 | PPC = PC; |
| 707 | |
| 708 | //Call Debugger |
| 709 | CALL_DEBUGGER(PC); |
| 710 | |
| 711 | //remove after testing |
| 712 | if(PC != PPC) op = cpu_readop(PC); |
| 713 | |
| 714 | //Update Timer (if any timers are running) |
| 715 | if((GET_TR0 && GET_ET0) || (GET_TR1 && GET_ET1)) |
| 716 | update_timer(ds5002fp.prev_used_cycles); |
| 717 | |
| 718 | //Update Serial (if serial port sending data) |
| 719 | if(uart.sending) |
| 720 | update_serial(ds5002fp.prev_used_cycles); |
| 721 | |
| 722 | //Update PC |
| 723 | PC += 1; |
| 724 | //Decrement total count by # of cycles used for this opcode |
| 725 | ds5002fp_icount -= (ds5002fp_cycles[op]); |
| 726 | |
| 727 | switch( op ) |
| 728 | { |
| 729 | //NOP |
| 730 | case 0x00: /* 1: 0000 0000 */ |
| 731 | nop(); |
| 732 | break; |
| 733 | //AJMP code addr /* 1: aaa0 0001 */ |
| 734 | case 0x01: |
| 735 | ajmp(); |
| 736 | break; |
| 737 | //LJMP code addr |
| 738 | case 0x02: /* 1: 0000 0010 */ |
| 739 | ljmp(); |
| 740 | break; |
| 741 | //RR A |
| 742 | case 0x03: /* 1: 0000 0011 */ |
| 743 | rr_a(); |
| 744 | break; |
| 745 | //INC A |
| 746 | case 0x04: /* 1: 0000 0100 */ |
| 747 | inc_a(); |
| 748 | break; |
| 749 | //INC data addr |
| 750 | case 0x05: /* 1: 0000 0101 */ |
| 751 | RWM=1; |
| 752 | inc_mem(); |
| 753 | RWM=0; |
| 754 | break; |
| 755 | //INC @R0/@R1 /* 1: 0000 011i */ |
| 756 | case 0x06: |
| 757 | case 0x07: |
| 758 | inc_ir(op&1); |
| 759 | break; |
| 760 | //INC R0 to R7 /* 1: 0000 1rrr */ |
| 761 | case 0x08: |
| 762 | case 0x09: |
| 763 | case 0x0a: |
| 764 | case 0x0b: |
| 765 | case 0x0c: |
| 766 | case 0x0d: |
| 767 | case 0x0e: |
| 768 | case 0x0f: |
| 769 | inc_r(op&7); |
| 770 | break; |
| 771 | //JBC bit addr, code addr |
| 772 | case 0x10: /* 1: 0001 0000 */ |
| 773 | RWM=1; |
| 774 | jbc(); |
| 775 | RWM=0; |
| 776 | break; |
| 777 | //ACALL code addr /* 1: aaa1 0001 */ |
| 778 | case 0x11: |
| 779 | acall(); |
| 780 | break; |
| 781 | //LCALL code addr |
| 782 | case 0x12: /* 1: 0001 0010 */ |
| 783 | lcall(); |
| 784 | break; |
| 785 | //RRC A |
| 786 | case 0x13: /* 1: 0001 0011 */ |
| 787 | rrc_a(); |
| 788 | break; |
| 789 | //DEC A |
| 790 | case 0x14: /* 1: 0001 0100 */ |
| 791 | dec_a(); |
| 792 | break; |
| 793 | //DEC data addr |
| 794 | case 0x15: /* 1: 0001 0101 */ |
| 795 | RWM=1; |
| 796 | dec_mem(); |
| 797 | RWM=0; |
| 798 | break; |
| 799 | //DEC @R0/@R1 /* 1: 0001 011i */ |
| 800 | case 0x16: |
| 801 | case 0x17: |
| 802 | dec_ir(op&1); |
| 803 | break; |
| 804 | //DEC R0 to R7 /* 1: 0001 1rrr */ |
| 805 | case 0x18: |
| 806 | case 0x19: |
| 807 | case 0x1a: |
| 808 | case 0x1b: |
| 809 | case 0x1c: |
| 810 | case 0x1d: |
| 811 | case 0x1e: |
| 812 | case 0x1f: |
| 813 | dec_r(op&7); |
| 814 | break; |
| 815 | //JB bit addr, code addr |
| 816 | case 0x20: /* 1: 0010 0000 */ |
| 817 | jb(); |
| 818 | break; |
| 819 | //AJMP code addr /* 1: aaa0 0001 */ |
| 820 | case 0x21: |
| 821 | ajmp(); |
| 822 | break; |
| 823 | //RET |
| 824 | case 0x22: /* 1: 0010 0010 */ |
| 825 | ret(); |
| 826 | break; |
| 827 | //RL A |
| 828 | case 0x23: /* 1: 0010 0011 */ |
| 829 | rl_a(); |
| 830 | break; |
| 831 | //ADD A, #data |
| 832 | case 0x24: /* 1: 0010 0100 */ |
| 833 | add_a_byte(); |
| 834 | break; |
| 835 | //ADD A, data addr |
| 836 | case 0x25: /* 1: 0010 0101 */ |
| 837 | add_a_mem(); |
| 838 | break; |
| 839 | //ADD A, @R0/@R1 /* 1: 0010 011i */ |
| 840 | case 0x26: |
| 841 | case 0x27: |
| 842 | add_a_ir(op&1); |
| 843 | break; |
| 844 | //ADD A, R0 to R7 /* 1: 0010 1rrr */ |
| 845 | case 0x28: |
| 846 | case 0x29: |
| 847 | case 0x2a: |
| 848 | case 0x2b: |
| 849 | case 0x2c: |
| 850 | case 0x2d: |
| 851 | case 0x2e: |
| 852 | case 0x2f: |
| 853 | add_a_r(op&7); |
| 854 | break; |
| 855 | //JNB bit addr, code addr |
| 856 | case 0x30: /* 1: 0011 0000 */ |
| 857 | jnb(); |
| 858 | break; |
| 859 | //ACALL code addr /* 1: aaa1 0001 */ |
| 860 | case 0x31: |
| 861 | acall(); |
| 862 | break; |
| 863 | //RETI |
| 864 | case 0x32: /* 1: 0011 0010 */ |
| 865 | reti(); |
| 866 | break; |
| 867 | //RLC A |
| 868 | case 0x33: /* 1: 0011 0011 */ |
| 869 | rlc_a(); |
| 870 | break; |
| 871 | //ADDC A, #data |
| 872 | case 0x34: /* 1: 0011 0100 */ |
| 873 | addc_a_byte(); |
| 874 | break; |
| 875 | //ADDC A, data addr |
| 876 | case 0x35: /* 1: 0011 0101 */ |
| 877 | addc_a_mem(); |
| 878 | break; |
| 879 | //ADDC A, @R0/@R1 /* 1: 0011 011i */ |
| 880 | case 0x36: |
| 881 | case 0x37: |
| 882 | addc_a_ir(op&1); |
| 883 | break; |
| 884 | //ADDC A, R0 to R7 /* 1: 0011 1rrr */ |
| 885 | case 0x38: |
| 886 | case 0x39: |
| 887 | case 0x3a: |
| 888 | case 0x3b: |
| 889 | case 0x3c: |
| 890 | case 0x3d: |
| 891 | case 0x3e: |
| 892 | case 0x3f: |
| 893 | addc_a_r(op&7); |
| 894 | break; |
| 895 | //JC code addr |
| 896 | case 0x40: /* 1: 0100 0000 */ |
| 897 | jc(); |
| 898 | break; |
| 899 | //AJMP code addr /* 1: aaa0 0001 */ |
| 900 | case 0x41: |
| 901 | ajmp(); |
| 902 | break; |
| 903 | //ORL data addr, A |
| 904 | case 0x42: /* 1: 0100 0010 */ |
| 905 | RWM=1; |
| 906 | orl_mem_a(); |
| 907 | RWM=0; |
| 908 | break; |
| 909 | //ORL data addr, #data |
| 910 | case 0x43: /* 1: 0100 0011 */ |
| 911 | RWM=1; |
| 912 | orl_mem_byte(); |
| 913 | RWM=0; |
| 914 | break; |
| 915 | //ORL A, #data |
| 916 | case 0x44: /* 1: 0100 0100 */ |
| 917 | orl_a_byte(); |
| 918 | break; |
| 919 | //ORL A, data addr |
| 920 | case 0x45: /* 1: 0100 0101 */ |
| 921 | orl_a_mem(); |
| 922 | break; |
| 923 | //ORL A, @RO/@R1 /* 1: 0100 011i */ |
| 924 | case 0x46: |
| 925 | case 0x47: |
| 926 | orl_a_ir(op&1); |
| 927 | break; |
| 928 | //ORL A, RO to R7 /* 1: 0100 1rrr */ |
| 929 | case 0x48: |
| 930 | case 0x49: |
| 931 | case 0x4a: |
| 932 | case 0x4b: |
| 933 | case 0x4c: |
| 934 | case 0x4d: |
| 935 | case 0x4e: |
| 936 | case 0x4f: |
| 937 | orl_a_r(op&7); |
| 938 | break; |
| 939 | //JNC code addr |
| 940 | case 0x50: /* 1: 0101 0000 */ |
| 941 | jnc(); |
| 942 | break; |
| 943 | //ACALL code addr /* 1: aaa1 0001 */ |
| 944 | case 0x51: |
| 945 | acall(); |
| 946 | break; |
| 947 | //ANL data addr, A |
| 948 | case 0x52: /* 1: 0101 0010 */ |
| 949 | RWM=1; |
| 950 | anl_mem_a(); |
| 951 | RWM=0; |
| 952 | break; |
| 953 | //ANL data addr, #data |
| 954 | case 0x53: /* 1: 0101 0011 */ |
| 955 | RWM=1; |
| 956 | anl_mem_byte(); |
| 957 | RWM=0; |
| 958 | break; |
| 959 | //ANL A, #data |
| 960 | case 0x54: /* 1: 0101 0100 */ |
| 961 | anl_a_byte(); |
| 962 | break; |
| 963 | //ANL A, data addr |
| 964 | case 0x55: /* 1: 0101 0101 */ |
| 965 | anl_a_mem(); |
| 966 | break; |
| 967 | //ANL A, @RO/@R1 /* 1: 0101 011i */ |
| 968 | case 0x56: |
| 969 | case 0x57: |
| 970 | anl_a_ir(op&1); |
| 971 | break; |
| 972 | //ANL A, RO to R7 /* 1: 0101 1rrr */ |
| 973 | case 0x58: |
| 974 | case 0x59: |
| 975 | case 0x5a: |
| 976 | case 0x5b: |
| 977 | case 0x5c: |
| 978 | case 0x5d: |
| 979 | case 0x5e: |
| 980 | case 0x5f: |
| 981 | anl_a_r(op&7); |
| 982 | break; |
| 983 | //JZ code addr |
| 984 | case 0x60: /* 1: 0110 0000 */ |
| 985 | jz(); |
| 986 | break; |
| 987 | //AJMP code addr /* 1: aaa0 0001 */ |
| 988 | case 0x61: |
| 989 | ajmp(); |
| 990 | break; |
| 991 | //XRL data addr, A |
| 992 | case 0x62: /* 1: 0110 0010 */ |
| 993 | RWM=1; |
| 994 | xrl_mem_a(); |
| 995 | RWM=0; |
| 996 | break; |
| 997 | //XRL data addr, #data |
| 998 | case 0x63: /* 1: 0110 0011 */ |
| 999 | RWM=1; |
| 1000 | xrl_mem_byte(); |
| 1001 | RWM=0; |
| 1002 | break; |
| 1003 | //XRL A, #data |
| 1004 | case 0x64: /* 1: 0110 0100 */ |
| 1005 | xrl_a_byte(); |
| 1006 | break; |
| 1007 | //XRL A, data addr |
| 1008 | case 0x65: /* 1: 0110 0101 */ |
| 1009 | xrl_a_mem(); |
| 1010 | break; |
| 1011 | //XRL A, @R0/@R1 /* 1: 0110 011i */ |
| 1012 | case 0x66: |
| 1013 | case 0x67: |
| 1014 | xrl_a_ir(op&1); |
| 1015 | break; |
| 1016 | //XRL A, R0 to R7 /* 1: 0110 1rrr */ |
| 1017 | case 0x68: |
| 1018 | case 0x69: |
| 1019 | case 0x6a: |
| 1020 | case 0x6b: |
| 1021 | case 0x6c: |
| 1022 | case 0x6d: |
| 1023 | case 0x6e: |
| 1024 | case 0x6f: |
| 1025 | xrl_a_r(op&7); |
| 1026 | break; |
| 1027 | //JNZ code addr |
| 1028 | case 0x70: /* 1: 0111 0000 */ |
| 1029 | jnz(); |
| 1030 | break; |
| 1031 | //ACALL code addr /* 1: aaa1 0001 */ |
| 1032 | case 0x71: |
| 1033 | acall(); |
| 1034 | break; |
| 1035 | //ORL C, bit addr |
| 1036 | case 0x72: /* 1: 0111 0010 */ |
| 1037 | orl_c_bitaddr(); |
| 1038 | break; |
| 1039 | //JMP @A+DPTR |
| 1040 | case 0x73: /* 1: 0111 0011 */ |
| 1041 | jmp_iadptr(); |
| 1042 | break; |
| 1043 | //MOV A, #data |
| 1044 | case 0x74: /* 1: 0111 0100 */ |
| 1045 | mov_a_byte(); |
| 1046 | break; |
| 1047 | //MOV data addr, #data |
| 1048 | case 0x75: /* 1: 0111 0101 */ |
| 1049 | mov_mem_byte(); |
| 1050 | break; |
| 1051 | //MOV @R0/@R1, #data /* 1: 0111 011i */ |
| 1052 | case 0x76: |
| 1053 | case 0x77: |
| 1054 | mov_ir_byte(op&1); |
| 1055 | break; |
| 1056 | //MOV R0 to R7, #data /* 1: 0111 1rrr */ |
| 1057 | case 0x78: |
| 1058 | case 0x79: |
| 1059 | case 0x7a: |
| 1060 | case 0x7b: |
| 1061 | case 0x7c: |
| 1062 | case 0x7d: |
| 1063 | case 0x7e: |
| 1064 | case 0x7f: |
| 1065 | mov_r_byte(op&7); |
| 1066 | break; |
| 1067 | //SJMP code addr |
| 1068 | case 0x80: /* 1: 1000 0000 */ |
| 1069 | sjmp(); |
| 1070 | break; |
| 1071 | //AJMP code addr /* 1: aaa0 0001 */ |
| 1072 | case 0x81: |
| 1073 | ajmp(); |
| 1074 | break; |
| 1075 | //ANL C, bit addr |
| 1076 | case 0x82: /* 1: 1000 0010 */ |
| 1077 | anl_c_bitaddr(); |
| 1078 | break; |
| 1079 | //MOVC A, @A + PC |
| 1080 | case 0x83: /* 1: 1000 0011 */ |
| 1081 | movc_a_iapc(); |
| 1082 | break; |
| 1083 | //DIV AB |
| 1084 | case 0x84: /* 1: 1000 0100 */ |
| 1085 | div_ab(); |
| 1086 | break; |
| 1087 | //MOV data addr, data addr |
| 1088 | case 0x85: /* 1: 1000 0101 */ |
| 1089 | mov_mem_mem(); |
| 1090 | break; |
| 1091 | //MOV data addr, @R0/@R1 /* 1: 1000 011i */ |
| 1092 | case 0x86: |
| 1093 | case 0x87: |
| 1094 | mov_mem_ir(op&1); |
| 1095 | break; |
| 1096 | //MOV data addr,R0 to R7 /* 1: 1000 1rrr */ |
| 1097 | case 0x88: |
| 1098 | case 0x89: |
| 1099 | case 0x8a: |
| 1100 | case 0x8b: |
| 1101 | case 0x8c: |
| 1102 | case 0x8d: |
| 1103 | case 0x8e: |
| 1104 | case 0x8f: |
| 1105 | mov_mem_r(op&7); |
| 1106 | break; |
| 1107 | //MOV DPTR, #data |
| 1108 | case 0x90: /* 1: 1001 0000 */ |
| 1109 | mov_dptr_byte(); |
| 1110 | break; |
| 1111 | //ACALL code addr /* 1: aaa1 0001 */ |
| 1112 | case 0x91: |
| 1113 | acall(); |
| 1114 | break; |
| 1115 | //MOV bit addr, C |
| 1116 | case 0x92: /* 1: 1001 0010 */ |
| 1117 | RWM = 1; |
| 1118 | mov_bitaddr_c(); |
| 1119 | RWM = 0; |
| 1120 | break; |
| 1121 | //MOVC A, @A + DPTR |
| 1122 | case 0x93: /* 1: 1001 0011 */ |
| 1123 | movc_a_iadptr(); |
| 1124 | break; |
| 1125 | //SUBB A, #data |
| 1126 | case 0x94: /* 1: 1001 0100 */ |
| 1127 | subb_a_byte(); |
| 1128 | break; |
| 1129 | //SUBB A, data addr |
| 1130 | case 0x95: /* 1: 1001 0101 */ |
| 1131 | subb_a_mem(); |
| 1132 | break; |
| 1133 | //SUBB A, @R0/@R1 /* 1: 1001 011i */ |
| 1134 | case 0x96: |
| 1135 | case 0x97: |
| 1136 | subb_a_ir(op&1); |
| 1137 | break; |
| 1138 | //SUBB A, R0 to R7 /* 1: 1001 1rrr */ |
| 1139 | case 0x98: |
| 1140 | case 0x99: |
| 1141 | case 0x9a: |
| 1142 | case 0x9b: |
| 1143 | case 0x9c: |
| 1144 | case 0x9d: |
| 1145 | case 0x9e: |
| 1146 | case 0x9f: |
| 1147 | subb_a_r(op&7); |
| 1148 | break; |
| 1149 | //ORL C, /bit addr |
| 1150 | case 0xa0: /* 1: 1010 0000 */ |
| 1151 | orl_c_nbitaddr(); |
| 1152 | break; |
| 1153 | //AJMP code addr /* 1: aaa0 0001 */ |
| 1154 | case 0xa1: |
| 1155 | ajmp(); |
| 1156 | break; |
| 1157 | //MOV C, bit addr |
| 1158 | case 0xa2: /* 1: 1010 0010 */ |
| 1159 | mov_c_bitaddr(); |
| 1160 | break; |
| 1161 | //INC DPTR |
| 1162 | case 0xa3: /* 1: 1010 0011 */ |
| 1163 | inc_dptr(); |
| 1164 | break; |
| 1165 | //MUL AB |
| 1166 | case 0xa4: /* 1: 1010 0100 */ |
| 1167 | mul_ab(); |
| 1168 | break; |
| 1169 | //reserved |
| 1170 | case 0xa5: /* 1: 1010 0101 */ |
| 1171 | illegal(); |
| 1172 | break; |
| 1173 | //MOV @R0/@R1, data addr /* 1: 1010 011i */ |
| 1174 | case 0xa6: |
| 1175 | case 0xa7: |
| 1176 | mov_ir_mem(op&1); |
| 1177 | break; |
| 1178 | //MOV R0 to R7, data addr /* 1: 1010 1rrr */ |
| 1179 | case 0xa8: |
| 1180 | case 0xa9: |
| 1181 | case 0xaa: |
| 1182 | case 0xab: |
| 1183 | case 0xac: |
| 1184 | case 0xad: |
| 1185 | case 0xae: |
| 1186 | case 0xaf: |
| 1187 | mov_r_mem(op&7); |
| 1188 | break; |
| 1189 | //ANL C,/bit addr |
| 1190 | case 0xb0: /* 1: 1011 0000 */ |
| 1191 | anl_c_nbitaddr(); |
| 1192 | break; |
| 1193 | //ACALL code addr /* 1: aaa1 0001 */ |
| 1194 | case 0xb1: |
| 1195 | acall(); |
| 1196 | break; |
| 1197 | //CPL bit addr |
| 1198 | case 0xb2: /* 1: 1011 0010 */ |
| 1199 | RWM=1; |
| 1200 | cpl_bitaddr(); |
| 1201 | RWM=0; |
| 1202 | break; |
| 1203 | //CPL C |
| 1204 | case 0xb3: /* 1: 1011 0011 */ |
| 1205 | cpl_c(); |
| 1206 | break; |
| 1207 | //CJNE A, #data, code addr |
| 1208 | case 0xb4: /* 1: 1011 0100 */ |
| 1209 | cjne_a_byte(); |
| 1210 | break; |
| 1211 | //CJNE A, data addr, code addr |
| 1212 | case 0xb5: /* 1: 1011 0101 */ |
| 1213 | cjne_a_mem(); |
| 1214 | break; |
| 1215 | //CJNE @R0/@R1, #data, code addr /* 1: 1011 011i */ |
| 1216 | case 0xb6: |
| 1217 | case 0xb7: |
| 1218 | cjne_ir_byte(op&1); |
| 1219 | break; |
| 1220 | //CJNE R0 to R7, #data, code addr/* 1: 1011 1rrr */ |
| 1221 | case 0xb8: |
| 1222 | case 0xb9: |
| 1223 | case 0xba: |
| 1224 | case 0xbb: |
| 1225 | case 0xbc: |
| 1226 | case 0xbd: |
| 1227 | case 0xbe: |
| 1228 | case 0xbf: |
| 1229 | cjne_r_byte(op&7); |
| 1230 | break; |
| 1231 | //PUSH data addr |
| 1232 | case 0xc0: /* 1: 1100 0000 */ |
| 1233 | push(); |
| 1234 | break; |
| 1235 | //AJMP code addr /* 1: aaa0 0001 */ |
| 1236 | case 0xc1: |
| 1237 | ajmp(); |
| 1238 | break; |
| 1239 | //CLR bit addr |
| 1240 | case 0xc2: /* 1: 1100 0010 */ |
| 1241 | RWM=1; |
| 1242 | clr_bitaddr(); |
| 1243 | RWM=0; |
| 1244 | break; |
| 1245 | //CLR C |
| 1246 | case 0xc3: /* 1: 1100 0011 */ |
| 1247 | clr_c(); |
| 1248 | break; |
| 1249 | //SWAP A |
| 1250 | case 0xc4: /* 1: 1100 0100 */ |
| 1251 | swap_a(); |
| 1252 | break; |
| 1253 | //XCH A, data addr |
| 1254 | case 0xc5: /* 1: 1100 0101 */ |
| 1255 | xch_a_mem(); |
| 1256 | break; |
| 1257 | //XCH A, @RO/@R1 /* 1: 1100 011i */ |
| 1258 | case 0xc6: |
| 1259 | case 0xc7: |
| 1260 | xch_a_ir(op&1); |
| 1261 | break; |
| 1262 | //XCH A, RO to R7 /* 1: 1100 1rrr */ |
| 1263 | case 0xc8: |
| 1264 | case 0xc9: |
| 1265 | case 0xca: |
| 1266 | case 0xcb: |
| 1267 | case 0xcc: |
| 1268 | case 0xcd: |
| 1269 | case 0xce: |
| 1270 | case 0xcf: |
| 1271 | xch_a_r(op&7); |
| 1272 | break; |
| 1273 | //POP data addr |
| 1274 | case 0xd0: /* 1: 1101 0000 */ |
| 1275 | pop(); |
| 1276 | break; |
| 1277 | //ACALL code addr /* 1: aaa1 0001 */ |
| 1278 | case 0xd1: |
| 1279 | acall(); |
| 1280 | break; |
| 1281 | //SETB bit addr |
| 1282 | case 0xd2: /* 1: 1101 0010 */ |
| 1283 | RWM=1; |
| 1284 | setb_bitaddr(); |
| 1285 | RWM=0; |
| 1286 | break; |
| 1287 | //SETB C |
| 1288 | case 0xd3: /* 1: 1101 0011 */ |
| 1289 | setb_c(); |
| 1290 | break; |
| 1291 | //DA A |
| 1292 | case 0xd4: /* 1: 1101 0100 */ |
| 1293 | da_a(); |
| 1294 | break; |
| 1295 | //DJNZ data addr, code addr |
| 1296 | case 0xd5: /* 1: 1101 0101 */ |
| 1297 | RWM=1; |
| 1298 | djnz_mem(); |
| 1299 | RWM=0; |
| 1300 | break; |
| 1301 | //XCHD A, @R0/@R1 /* 1: 1101 011i */ |
| 1302 | case 0xd6: |
| 1303 | case 0xd7: |
| 1304 | xchd_a_ir(op&1); |
| 1305 | break; |
| 1306 | //DJNZ R0 to R7,code addr /* 1: 1101 1rrr */ |
| 1307 | case 0xd8: |
| 1308 | case 0xd9: |
| 1309 | case 0xda: |
| 1310 | case 0xdb: |
| 1311 | case 0xdc: |
| 1312 | case 0xdd: |
| 1313 | case 0xde: |
| 1314 | case 0xdf: |
| 1315 | djnz_r(op&7); |
| 1316 | break; |
| 1317 | //MOVX A,@DPTR |
| 1318 | case 0xe0: /* 1: 1110 0000 */ |
| 1319 | movx_a_idptr(); |
| 1320 | break; |
| 1321 | //AJMP code addr /* 1: aaa0 0001 */ |
| 1322 | case 0xe1: |
| 1323 | ajmp(); |
| 1324 | break; |
| 1325 | //MOVX A, @R0/@R1 /* 1: 1110 001i */ |
| 1326 | case 0xe2: |
| 1327 | case 0xe3: |
| 1328 | movx_a_ir(op&1); |
| 1329 | break; |
| 1330 | //CLR A |
| 1331 | case 0xe4: /* 1: 1110 0100 */ |
| 1332 | clr_a(); |
| 1333 | break; |
| 1334 | //MOV A, data addr |
| 1335 | case 0xe5: /* 1: 1110 0101 */ |
| 1336 | mov_a_mem(); |
| 1337 | break; |
| 1338 | //MOV A,@RO/@R1 /* 1: 1110 011i */ |
| 1339 | case 0xe6: |
| 1340 | case 0xe7: |
| 1341 | mov_a_ir(op&1); |
| 1342 | break; |
| 1343 | //MOV A,R0 to R7 /* 1: 1110 1rrr */ |
| 1344 | case 0xe8: |
| 1345 | case 0xe9: |
| 1346 | case 0xea: |
| 1347 | case 0xeb: |
| 1348 | case 0xec: |
| 1349 | case 0xed: |
| 1350 | case 0xee: |
| 1351 | case 0xef: |
| 1352 | mov_a_r(op&7); |
| 1353 | break; |
| 1354 | //MOVX @DPTR,A |
| 1355 | case 0xf0: /* 1: 1111 0000 */ |
| 1356 | movx_idptr_a(); |
| 1357 | break; |
| 1358 | //ACALL code addr /* 1: aaa1 0001 */ |
| 1359 | case 0xf1: |
| 1360 | acall(); |
| 1361 | break; |
| 1362 | //MOVX @R0/@R1,A /* 1: 1111 001i */ |
| 1363 | case 0xf2: |
| 1364 | case 0xf3: |
| 1365 | movx_ir_a(op&1); |
| 1366 | break; |
| 1367 | //CPL A |
| 1368 | case 0xf4: /* 1: 1111 0100 */ |
| 1369 | cpl_a(); |
| 1370 | break; |
| 1371 | //MOV data addr, A |
| 1372 | case 0xf5: /* 1: 1111 0101 */ |
| 1373 | mov_mem_a(); |
| 1374 | break; |
| 1375 | //MOV @R0/@R1, A /* 1: 1111 011i */ |
| 1376 | case 0xf6: |
| 1377 | case 0xf7: |
| 1378 | mov_ir_a(op&1); |
| 1379 | break; |
| 1380 | //MOV R0 to R7, A /* 1: 1111 1rrr */ |
| 1381 | case 0xf8: |
| 1382 | case 0xf9: |
| 1383 | case 0xfa: |
| 1384 | case 0xfb: |
| 1385 | case 0xfc: |
| 1386 | case 0xfd: |
| 1387 | case 0xfe: |
| 1388 | case 0xff: |
| 1389 | mov_r_a(op&7); |
| 1390 | break; |
| 1391 | default: |
| 1392 | illegal(); |
| 1393 | } |
| 1394 | |
| 1395 | //If the chip entered in stop mode, end execution |
| 1396 | if (R_PCON & 0x02) { |
| 1397 | return cycles; |
| 1398 | } |
| 1399 | |
| 1400 | //Store # of used cycles for this opcode (for timer & serial check at top of code) |
| 1401 | ds5002fp.prev_used_cycles = ds5002fp_cycles[op]; |
| 1402 | |
| 1403 | // decrement the timed access window |
| 1404 | if (ds5002fp.ta_window > 0) { |
| 1405 | ds5002fp.ta_window -= ds5002fp_cycles[op]; |
| 1406 | if (ds5002fp.ta_window < 0) { |
| 1407 | ds5002fp.ta_window = 0; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | //Check for pending interrupts & handle - remove cycles used |
| 1412 | ds5002fp_icount-=check_interrupts(); |
| 1413 | |
| 1414 | //If the chip entered in idle mode, end the loop |
| 1415 | if (R_PCON & 0x01) { |
| 1416 | return cycles - ds5002fp_icount; |
| 1417 | } |
| 1418 | } while( ds5002fp_icount > 0 ); |
| 1419 | |
| 1420 | return cycles - ds5002fp_icount; |
| 1421 | } |
| 1422 | |
| 1423 | /* Get registers, return context size */ |
| 1424 | void ds5002fp_get_context(void *dst) |
| 1425 | { |
| 1426 | if( dst ) |
| 1427 | memcpy(dst, &ds5002fp, sizeof(DS5002FP)); |
| 1428 | } |
| 1429 | |
| 1430 | /* Set registers */ |
| 1431 | void ds5002fp_set_context(void *src) |
| 1432 | { |
| 1433 | if( src ) |
| 1434 | memcpy(&ds5002fp, src, sizeof(DS5002FP)); |
| 1435 | } |
| 1436 | |
| 1437 | /* Get Internal RAM value */ |
| 1438 | unsigned ds5002fp_get_intram(int offset) |
| 1439 | { |
| 1440 | UINT8 ram_ret = 0; |
| 1441 | RWM = 1; |
| 1442 | ram_ret = IRAM_R(offset); |
| 1443 | RWM = 0; |
| 1444 | return ram_ret; |
| 1445 | } |
| 1446 | |
| 1447 | /*NOTE: These are not used by the opcode functions, they are here only for MAME requirements*/ |
| 1448 | unsigned ds5002fp_get_reg(int regnum) |
| 1449 | { |
| 1450 | switch( regnum ) |
| 1451 | { |
| 1452 | case REG_PREVIOUSPC: return PPC; |
| 1453 | case REG_PC: |
| 1454 | case DS5002FP_PC: return PC; |
| 1455 | case REG_SP: |
| 1456 | case DS5002FP_SP: return R_SP; |
| 1457 | case DS5002FP_ACC: return R_ACC; |
| 1458 | case DS5002FP_PSW: return R_PSW; |
| 1459 | case DS5002FP_B: return R_B; |
| 1460 | case DS5002FP_DPH: return R_DPH; |
| 1461 | case DS5002FP_DPL: return R_DPL; |
| 1462 | case DS5002FP_IE: return R_IE; |
| 1463 | |
| 1464 | case DS5002FP_PCON: return R_PCON; |
| 1465 | case DS5002FP_MCON: return R_MCON; |
| 1466 | case DS5002FP_RPCTL:return R_RPCTL; |
| 1467 | case DS5002FP_CRC: return R_CRC; |
| 1468 | //Not real registers |
| 1469 | case DS5002FP_R0: return R_R(0); |
| 1470 | case DS5002FP_R1: return R_R(1); |
| 1471 | case DS5002FP_R2: return R_R(2); |
| 1472 | case DS5002FP_R3: return R_R(3); |
| 1473 | case DS5002FP_R4: return R_R(4); |
| 1474 | case DS5002FP_R5: return R_R(5); |
| 1475 | case DS5002FP_R6: return R_R(6); |
| 1476 | case DS5002FP_R7: return R_R(7); |
| 1477 | case DS5002FP_RB: return GET_RS; |
| 1478 | default: |
| 1479 | return 0; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /*NOTE: These are not used by the opcode functions, they are here only for MAME requirements*/ |
| 1484 | void ds5002fp_set_reg (int regnum, unsigned val) |
| 1485 | { |
| 1486 | switch( regnum ) |
| 1487 | { |
| 1488 | case REG_PC: |
| 1489 | case DS5002FP_PC: PC = val & 0xffff; break; |
| 1490 | case REG_SP: |
| 1491 | case DS5002FP_SP: SFR_W(SP,val); break; //Use SFR to handle error checking |
| 1492 | case DS5002FP_ACC: SFR_W(ACC,val); break; |
| 1493 | case DS5002FP_PSW: SFR_W(PSW,val); break; |
| 1494 | case DS5002FP_B: SFR_W(B,val); break; |
| 1495 | case DS5002FP_DPH: SFR_W(DPH,val); break; |
| 1496 | case DS5002FP_DPL: SFR_W(DPL,val); break; |
| 1497 | case DS5002FP_IE: SFR_W(IE,val); break; |
| 1498 | case DS5002FP_PCON: SFR_W(PCON,val); break; |
| 1499 | case DS5002FP_MCON: SFR_W(MCON,val); break; |
| 1500 | case DS5002FP_RPCTL: SFR_W(RPCTL,val); break; |
| 1501 | case DS5002FP_CRC: SFR_W(CRCR,val); break; |
| 1502 | //Not real registers |
| 1503 | case DS5002FP_R0: R_R(0) = val; break; |
| 1504 | case DS5002FP_R1: R_R(1) = val; break; |
| 1505 | case DS5002FP_R2: R_R(2) = val; break; |
| 1506 | case DS5002FP_R3: R_R(3) = val; break; |
| 1507 | case DS5002FP_R4: R_R(4) = val; break; |
| 1508 | case DS5002FP_R5: R_R(5) = val; break; |
| 1509 | case DS5002FP_R6: R_R(6) = val; break; |
| 1510 | case DS5002FP_R7: R_R(7) = val; break; |
| 1511 | case DS5002FP_RB: SET_RS( (val&3) ); break; |
| 1512 | |
| 1513 | default: |
| 1514 | return; |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | void ds5002fp_set_irq_line(int irqline, int state) |
| 1519 | { |
| 1520 | switch( irqline ) |
| 1521 | { |
| 1522 | //External Interrupt 0 |
| 1523 | case DS5002FP_INT0_LINE: |
| 1524 | //Line Asserted? |
| 1525 | if (state != CLEAR_LINE) { |
| 1526 | //Is the enable flag for this interrupt set? |
| 1527 | if(GET_EX0) { |
| 1528 | //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT0 active lo! |
| 1529 | if(GET_IT0){ |
| 1530 | if(ds5002fp.last_int0 == CLEAR_LINE) |
| 1531 | SET_IE0(1); |
| 1532 | } |
| 1533 | else |
| 1534 | SET_IE0(1); //Nope, just set it.. |
| 1535 | } |
| 1536 | } |
| 1537 | else |
| 1538 | SET_IE0(0); //Clear Int occurred flag |
| 1539 | ds5002fp.last_int0 = state; |
| 1540 | |
| 1541 | //Do the interrupt & handle - remove machine cycles used |
| 1542 | if(GET_IE0) |
| 1543 | ds5002fp_icount-=check_interrupts(); |
| 1544 | break; |
| 1545 | |
| 1546 | //External Interrupt 1 |
| 1547 | case DS5002FP_INT1_LINE: |
| 1548 | |
| 1549 | //Line Asserted? |
| 1550 | if (state != CLEAR_LINE) { |
| 1551 | if(GET_EX1) { |
| 1552 | //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT1 active lo! |
| 1553 | if(GET_IT1){ |
| 1554 | if(ds5002fp.last_int1 == CLEAR_LINE) |
| 1555 | SET_IE1(1); |
| 1556 | } |
| 1557 | else |
| 1558 | SET_IE1(1); //Nope, just set it.. |
| 1559 | } |
| 1560 | } |
| 1561 | else |
| 1562 | SET_IE1(0); //Clear Int occurred flag |
| 1563 | ds5002fp.last_int1 = state; |
| 1564 | |
| 1565 | //Do the interrupt & handle - remove machine cycles used |
| 1566 | if(GET_IE1) |
| 1567 | ds5002fp_icount-=check_interrupts(); |
| 1568 | break; |
| 1569 | |
| 1570 | //Serial Port Receive |
| 1571 | case DS5002FP_RX_LINE: |
| 1572 | //Is the enable flags for this interrupt set? |
| 1573 | if(GET_ES && GET_REN) { |
| 1574 | int data = 0; |
| 1575 | //Call our callball function to retrieve the data |
| 1576 | if(ds5002fp.serial_rx_callback) |
| 1577 | data = ds5002fp.serial_rx_callback(); |
| 1578 | //Update the register directly, since SFR_W() will trigger a serial transmit instead! |
| 1579 | R_SBUF=data; |
| 1580 | //Flag the IRQ |
| 1581 | SET_RI(1); |
| 1582 | } |
| 1583 | break; |
| 1584 | //Note: we won't call check interrupts, we'll let the main loop catch it |
| 1585 | |
| 1586 | //Power Fail Interrupt |
| 1587 | case DS5002FP_PFI_LINE: |
| 1588 | //Line Asserted? |
| 1589 | if (state != CLEAR_LINE) { |
| 1590 | if (GET_EPFW) { |
| 1591 | SET_PFW(1); |
| 1592 | //Need cleared->active line transition? (Logical 1-0 Pulse on the line) - CLEAR->ASSERT Transition since INT1 active lo! |
| 1593 | if (ds5002fp.last_power_int == CLEAR_LINE) { |
| 1594 | ds5002fp_icount-=check_interrupts(); |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | ds5002fp.last_power_int = state; |
| 1599 | break; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | /*********************************************************************************** |
| 1604 | Check for pending Interrupts and process - returns # of cycles used for the int |
| 1605 | |
| 1606 | Note about priority & interrupting interrupts.. |
| 1607 | 1) A high priority interrupt cannot be interrupted by anything! |
| 1608 | 2) A low priority interrupt can ONLY be interrupted by a high priority interrupt |
| 1609 | 3) If more than 1 Interrupt Flag is set (ie, 2 simultaneous requests occur), |
| 1610 | the following logic works as follows: |
| 1611 | 1) If two requests come in of different priority levels, the higher one is selected.. |
| 1612 | 2) If the requests are of the same level, an internal order is used: |
| 1613 | a) IEO |
| 1614 | b) TFO |
| 1615 | c) IE1 |
| 1616 | d) TF1 |
| 1617 | e) RI+TI |
| 1618 | f) TF2+EXF2 |
| 1619 | **********************************************************************************/ |
| 1620 | INLINE UINT8 check_interrupts(void) |
| 1621 | { |
| 1622 | //If All Inerrupts Disabled or no pending abort if no Power Fail Interrupt is pending |
| 1623 | if(!GET_EA && !GET_PFW) { |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | //Skip if current irq in progress is high priority and no Power Fail Interrupt is pending |
| 1628 | if(!GET_PFW && ds5002fp.irq_priority) { /* LOG(("high priority irq in progress, skipping irq request\n")); */ return 0; } |
| 1629 | |
| 1630 | //Check which interrupt(s) requests have occurred.. |
| 1631 | //NOTE: The order of checking is based on the internal/default priority levels when levels are the same |
| 1632 | |
| 1633 | // Power Fail Interrupt |
| 1634 | if(GET_PFW) { |
| 1635 | if (ds5002fp.last_power_int == CLEAR_LINE) { |
| 1636 | //Set vector & priority level request |
| 1637 | ds5002fp.int_vec = V_PFI; |
| 1638 | ds5002fp.priority_request = GET_PFW; |
| 1639 | } else if (!GET_EA) { |
| 1640 | return 0; |
| 1641 | } |
| 1642 | } |
| 1643 | //External Int 0 |
| 1644 | if(!ds5002fp.priority_request && GET_IE0) { |
| 1645 | //Set vector & priority level request |
| 1646 | ds5002fp.int_vec = V_IE0; |
| 1647 | ds5002fp.priority_request = GET_PX0; |
| 1648 | } |
| 1649 | //Timer 0 overflow |
| 1650 | if(!ds5002fp.priority_request && GET_TF0 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PT0))) { |
| 1651 | //Set vector & priority level request |
| 1652 | ds5002fp.int_vec = V_TF0; |
| 1653 | ds5002fp.priority_request = GET_PT0; |
| 1654 | } |
| 1655 | //External Int 1 |
| 1656 | if(!ds5002fp.priority_request && GET_IE1 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PX1))) { |
| 1657 | //Set vector & priority level request |
| 1658 | ds5002fp.int_vec = V_IE1; |
| 1659 | ds5002fp.priority_request = GET_PX1; |
| 1660 | } |
| 1661 | //Timer 1 overflow |
| 1662 | if(!ds5002fp.priority_request && GET_TF1 && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PT1))) { |
| 1663 | //Set vector & priority level request |
| 1664 | ds5002fp.int_vec = V_TF1; |
| 1665 | ds5002fp.priority_request = GET_PT1; |
| 1666 | } |
| 1667 | //Serial Interrupt Transmit/Receive Interrupts (Note: ES Bit - Serial Interrupts must be enabled) |
| 1668 | if(!ds5002fp.priority_request && GET_ES && (GET_TI || GET_RI) && (!ds5002fp.int_vec || (ds5002fp.int_vec && GET_PS))) { |
| 1669 | //Set vector & priority level request |
| 1670 | ds5002fp.int_vec = V_RITI; |
| 1671 | ds5002fp.priority_request = GET_PS; |
| 1672 | } |
| 1673 | |
| 1674 | //Skip the interrupt request if currently processing is lo priority, and the new request IS NOT HI PRIORITY! |
| 1675 | if(ds5002fp.cur_irq < 0xff && !ds5002fp.priority_request) |
| 1676 | { LOG(("low priority irq in progress already, skipping low irq request\n")); return 0; } |
| 1677 | |
| 1678 | /*** --- Perform the interrupt --- ***/ |
| 1679 | |
| 1680 | //Save current pc to stack, set pc to new interrupt vector |
| 1681 | push_pc(); |
| 1682 | PC = ds5002fp.int_vec; |
| 1683 | |
| 1684 | //Set current Irq & Priority being serviced |
| 1685 | ds5002fp.cur_irq = ds5002fp.int_vec; |
| 1686 | ds5002fp.irq_priority = ds5002fp.priority_request; |
| 1687 | |
| 1688 | //Clear any interrupt flags that should be cleared since we're servicing the irq! |
| 1689 | switch(ds5002fp.cur_irq) { |
| 1690 | case V_IE0: |
| 1691 | //External Int Flag only cleared when configured as Edge Triggered.. |
| 1692 | //if(GET_IT0) - for some reason having this, breaks alving dmd games |
| 1693 | SET_IE0(0); |
| 1694 | break; |
| 1695 | case V_TF0: |
| 1696 | //Timer 0 - Always clear Flag |
| 1697 | SET_TF0(0); |
| 1698 | break; |
| 1699 | case V_IE1: |
| 1700 | //External Int Flag only cleared when configured as Edge Triggered.. |
| 1701 | //if(GET_IT1) - for some reason having this, breaks alving dmd games |
| 1702 | SET_IE1(0); |
| 1703 | break; |
| 1704 | case V_TF1: |
| 1705 | //Timer 0 - Always clear Flag |
| 1706 | SET_TF1(0); |
| 1707 | break; |
| 1708 | case V_RITI: |
| 1709 | // no flags are cleared, TI and RI remain set until reset by software |
| 1710 | break; |
| 1711 | case V_PFI: |
| 1712 | // no flags are cleared, PFW is reset by software |
| 1713 | break; |
| 1714 | } |
| 1715 | |
| 1716 | //Clear vars.. (these are part of the 8051 structure for speed, so we don't have to dynamically allocate space each time) |
| 1717 | ds5002fp.int_vec = 0; |
| 1718 | ds5002fp.priority_request = 0; |
| 1719 | |
| 1720 | //All interrupts use 2 machine cycles |
| 1721 | return 24; |
| 1722 | } |
| 1723 | |
| 1724 | |
| 1725 | void ds5002fp_set_irq_callback(int (*callback)(int irqline)) |
| 1726 | { |
| 1727 | ds5002fp.irq_callback = callback; |
| 1728 | } |
| 1729 | |
| 1730 | void ds5002fp_set_serial_tx_callback(void (*callback)(int data)) |
| 1731 | { |
| 1732 | //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean |
| 1733 | hold_serial_tx_callback = callback; |
| 1734 | } |
| 1735 | void ds5002fp_set_serial_rx_callback(int (*callback)(void)) |
| 1736 | { |
| 1737 | //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean |
| 1738 | hold_serial_rx_callback = callback; |
| 1739 | } |
| 1740 | |
| 1741 | void ds5002fp_set_ebram_iaddr_callback(READ32_HANDLER((*callback))) |
| 1742 | { |
| 1743 | //Hold in static variable since this function can get called before reset has run, which wipes ds5002fp memory clean |
| 1744 | hold_ebram_iaddr_callback = callback; |
| 1745 | } |
| 1746 | |
| 1747 | |
| 1748 | void ds5002fp_state_save(void *file) |
| 1749 | { |
| 1750 | } |
| 1751 | |
| 1752 | void ds5002fp_state_load(void *file) |
| 1753 | { |
| 1754 | } |
| 1755 | |
| 1756 | /* HELPER FUNCTIONS */ |
| 1757 | |
| 1758 | /*All writes to SFR are handled here*/ |
| 1759 | static WRITE8_HANDLER(sfr_write) |
| 1760 | { |
| 1761 | UINT8 is_timed_access; |
| 1762 | UINT8 reg_mask1; |
| 1763 | UINT8 reg_mask2; |
| 1764 | |
| 1765 | data &= 0xff; //Ensure only 8 bits |
| 1766 | switch (offset) |
| 1767 | { |
| 1768 | case P0: |
| 1769 | R_P0 = data; |
| 1770 | OUT(0,data); |
| 1771 | break; |
| 1772 | |
| 1773 | case SP: |
| 1774 | if(offset > 0xff) |
| 1775 | LOG(("ds5002fp #%d: attemping to write value to SP past 256 bytes at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1776 | R_SP = data&0xff; //keep sp w/in 256 bytes |
| 1777 | break; |
| 1778 | |
| 1779 | case DPL: R_DPL = data; break; |
| 1780 | case DPH: R_DPH = data; break; |
| 1781 | case PCON: |
| 1782 | reg_mask1 = 0xb9; |
| 1783 | |
| 1784 | is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55); |
| 1785 | if (is_timed_access) { |
| 1786 | reg_mask1 = 0xff; |
| 1787 | } |
| 1788 | R_PCON = (R_PCON & (~reg_mask1)) | (data & reg_mask1); |
| 1789 | break; |
| 1790 | case TCON: R_TCON = data; break; |
| 1791 | case TMOD: R_TMOD = data; break; |
| 1792 | case TL0: R_TL0 = data; break; |
| 1793 | case TL1: R_TL1 = data; break; |
| 1794 | case TH0: R_TH0 = data; break; |
| 1795 | case TH1: R_TH1 = data; break; |
| 1796 | |
| 1797 | case P1: |
| 1798 | R_P1 = data; |
| 1799 | OUT(1,data); |
| 1800 | break; |
| 1801 | |
| 1802 | case SCON: { |
| 1803 | //Update register |
| 1804 | R_SCON = data; |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | case SBUF: |
| 1809 | //R_SBUF = data; //This register is used only for "Receiving data coming in!" |
| 1810 | serial_transmit(data); //Set up to transmit the data |
| 1811 | break; |
| 1812 | |
| 1813 | case P2: |
| 1814 | R_P2 = data; |
| 1815 | OUT(2,data); |
| 1816 | break; |
| 1817 | |
| 1818 | case IE: R_IE = data; break; |
| 1819 | |
| 1820 | case P3: |
| 1821 | R_P3 = data; |
| 1822 | OUT(3,data); |
| 1823 | break; |
| 1824 | |
| 1825 | case IP: |
| 1826 | reg_mask1 = 0x7f; |
| 1827 | |
| 1828 | is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55); |
| 1829 | if (is_timed_access) { |
| 1830 | reg_mask1 = 0xff; |
| 1831 | } |
| 1832 | R_IP = (R_IP & (~reg_mask1)) | (data & reg_mask1); |
| 1833 | break; |
| 1834 | |
| 1835 | case CRCR: reg_mask2 = 0x0f; |
| 1836 | R_CRC = (R_CRC & (~reg_mask2)) | (data & reg_mask2); |
| 1837 | LOG(("ds5002fp #%d: write to CRC register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1838 | break; |
| 1839 | case CRCL: R_CRCL = data; |
| 1840 | LOG(("ds5002fp #%d: write to CRCL register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1841 | break; |
| 1842 | case CRCH: R_CRCH = data; |
| 1843 | LOG(("ds5002fp #%d: write to CRCH register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1844 | break; |
| 1845 | case MCON: reg_mask1 = 0x0f; |
| 1846 | reg_mask2 = 0xf7; |
| 1847 | |
| 1848 | is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55); |
| 1849 | if (is_timed_access) { |
| 1850 | reg_mask1 = 0xff; |
| 1851 | } |
| 1852 | data = (R_MCON & (~reg_mask1)) | (data & reg_mask1); |
| 1853 | R_MCON = (R_MCON & (~reg_mask2)) | (data & reg_mask2); |
| 1854 | LOG(("ds5002fp #%d: write to MCON register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1855 | break; |
| 1856 | case TA: ds5002fp.previous_ta = R_TA; |
| 1857 | R_TA = data; |
| 1858 | |
| 1859 | // init the time window after having wrote 0xaa |
| 1860 | if ((data == 0xaa) && (ds5002fp.ta_window == 0)) { |
| 1861 | ds5002fp.ta_window = 4*12 + 2*12; |
| 1862 | LOG(("ds5002fp #%d: TA window initiated at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1863 | } |
| 1864 | //LOG(("ds5002fp #%d: write to TA register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1865 | break; |
| 1866 | case RNR: R_RNR = data; |
| 1867 | LOG(("ds5002fp #%d: write to RNR register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1868 | break; |
| 1869 | |
| 1870 | case PSW: |
| 1871 | R_PSW = data; |
| 1872 | SET_PARITY; |
| 1873 | break; |
| 1874 | |
| 1875 | case RPCTL: reg_mask1 = 0xef; |
| 1876 | reg_mask2 = 0xfe; |
| 1877 | |
| 1878 | is_timed_access = (ds5002fp.ta_window > 0) && (R_TA == 0x55); |
| 1879 | if (is_timed_access) { |
| 1880 | reg_mask1 = 0xff; |
| 1881 | } |
| 1882 | data = (R_RPCTL & (~reg_mask1)) | (data & reg_mask1); |
| 1883 | R_RPCTL = (R_RPCTL & (~reg_mask2)) | (data & reg_mask2); |
| 1884 | LOG(("ds5002fp #%d: write to RPCTL register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1885 | break; |
| 1886 | case RPS: R_RPS = data; |
| 1887 | LOG(("ds5002fp #%d: write to RPS register at 0x%04x, data=%x\n", cpu_getactivecpu(), PC, data)); |
| 1888 | break; |
| 1889 | |
| 1890 | case ACC: |
| 1891 | R_ACC = data; |
| 1892 | SET_PARITY; |
| 1893 | break; |
| 1894 | |
| 1895 | case B: R_B = data; break; |
| 1896 | |
| 1897 | /* Illegal or non-implemented sfr */ |
| 1898 | default: |
| 1899 | LOG(("ds5002fp #%d: attemping to write to an invalid/non-implemented SFR address: %x at 0x%04x, data=%x\n", cpu_getactivecpu(), offset, PC, data)); |
| 1900 | } |
| 1901 | } |
| 1902 | |
| 1903 | /*All reads to SFR are handled here*/ |
| 1904 | static READ8_HANDLER(sfr_read) |
| 1905 | { |
| 1906 | switch (offset) |
| 1907 | { |
| 1908 | case P0: |
| 1909 | if(RWM) |
| 1910 | return R_P0; //Read directly from port latch |
| 1911 | else |
| 1912 | return IN(0); //Read from actual port |
| 1913 | case SP: return R_SP; |
| 1914 | case DPL: return R_DPL; |
| 1915 | case DPH: return R_DPH; |
| 1916 | case PCON: return R_PCON; |
| 1917 | case TCON: return R_TCON; |
| 1918 | case TMOD: return R_TMOD; |
| 1919 | case TL0: return R_TL0; |
| 1920 | case TL1: return R_TL1; |
| 1921 | case TH0: return R_TH0; |
| 1922 | case TH1: return R_TH1; |
| 1923 | case P1: |
| 1924 | if(RWM) |
| 1925 | return R_P1; //Read directly from port latch |
| 1926 | else |
| 1927 | return IN(1); //Read from actual port |
| 1928 | case SCON: return R_SCON; |
| 1929 | case SBUF: return R_SBUF; |
| 1930 | case P2: |
| 1931 | if(RWM) |
| 1932 | return R_P2; //Read directly from port latch |
| 1933 | else |
| 1934 | return IN(2); //Read from actual port |
| 1935 | case IE: return R_IE; |
| 1936 | case P3: |
| 1937 | if(RWM) |
| 1938 | return R_P3; //Read directly from port latch |
| 1939 | else |
| 1940 | return IN(3); //Read from actual port |
| 1941 | case IP: return R_IP; |
| 1942 | |
| 1943 | case CRCR: LOG(("ds5002fp #%d: read CRC register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1944 | return R_CRC; |
| 1945 | case CRCL: LOG(("ds5002fp #%d: read CRCL register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1946 | return R_CRCL; |
| 1947 | case CRCH: LOG(("ds5002fp #%d: read CRCH register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1948 | return R_CRCH; |
| 1949 | case MCON: LOG(("ds5002fp #%d: read MCON register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1950 | return R_MCON; |
| 1951 | case TA: LOG(("ds5002fp #%d: read TA register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1952 | return R_TA; |
| 1953 | case RNR: LOG(("ds5002fp #%d: read RNR register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1954 | return R_RNR; |
| 1955 | |
| 1956 | case PSW: return R_PSW; |
| 1957 | |
| 1958 | case RPCTL: LOG(("ds5002fp #%d: read RPCTL register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1959 | return R_RPCTL; |
| 1960 | case RPS: LOG(("ds5002fp #%d: read RPS register at 0x%04x\n", cpu_getactivecpu(), PC)); |
| 1961 | return R_RPS; |
| 1962 | |
| 1963 | case ACC: return R_ACC; |
| 1964 | case B: return R_B; |
| 1965 | |
| 1966 | /* Illegal or non-implemented sfr */ |
| 1967 | default: |
| 1968 | LOG(("ds5002fp #%d: attemping to read an invalid/non-implemented SFR address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC)); |
| 1969 | } |
| 1970 | return 0xff; |
| 1971 | } |
| 1972 | |
| 1973 | /* Reads the contents of the Internal RAM memory */ |
| 1974 | /* Anything above 0x7f is a sfr/register */ |
| 1975 | static READ8_HANDLER(internal_ram_read) |
| 1976 | { |
| 1977 | if (offset < 0x80) |
| 1978 | return ds5002fp.IntRam[offset]; |
| 1979 | else { |
| 1980 | if (offset < 0x100) |
| 1981 | return SFR_R(offset); |
| 1982 | else |
| 1983 | LOG(("ds5002fp #%d: attemping to read from an invalid Internal Ram address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC)); |
| 1984 | } |
| 1985 | return 0xff; |
| 1986 | } |
| 1987 | |
| 1988 | /* Writes the contents of the Internal RAM memory */ |
| 1989 | /* Anything above 0x7f is a sfr/register */ |
| 1990 | static WRITE8_HANDLER(internal_ram_write) |
| 1991 | { |
| 1992 | data &= 0xff; //Ensure it's only 8 bits |
| 1993 | if (offset < 0x80) |
| 1994 | ds5002fp.IntRam[offset] = data; |
| 1995 | else { |
| 1996 | if (offset < 0x100) |
| 1997 | SFR_W(offset,data); |
| 1998 | else |
| 1999 | LOG(("ds5002fp #%d: attemping to write to invalid Internal Ram address: %x at 0x%04x\n", cpu_getactivecpu(), offset,PC)); |
| 2000 | } |
| 2001 | } |
| 2002 | |
| 2003 | /* Reads the contents of the Internal RAM memory (BUT CALLED FROM AN INDIRECT ADDRESSING MODE) */ |
| 2004 | /* Different chip types handle differently, for speed, simply call the chip's handler */ |
| 2005 | static READ8_HANDLER(internal_ram_iread) |
| 2006 | { |
| 2007 | return ds5002fp.iram_iread(machine,offset); |
| 2008 | } |
| 2009 | |
| 2010 | /* Writes the contents of the Internal RAM memory (BUT CALLED FROM AN INDIRECT ADDRESSING MODE) */ |
| 2011 | /* Different chip types handle differently, for speed, simply call the chip's handler */ |
| 2012 | static WRITE8_HANDLER(internal_ram_iwrite) |
| 2013 | { |
| 2014 | ds5002fp.iram_iwrite(machine,offset,data); |
| 2015 | } |
| 2016 | |
| 2017 | /* Generate an expanded bus/bytewide bus ram address for read/writing */ |
| 2018 | /* |
| 2019 | The DS5002FP has 2 16 bits data address buses (the byte-wide bus and the expanded bus). The exact memory position accessed depends on the |
| 2020 | partition mode, the memory range and the expanded bus select. The partition mode and the expanded bus select can be changed at any time. |
| 2021 | |
| 2022 | In order to simplify memory mapping to the data address bus, the following address map is assumed for partitioned mode: |
| 2023 | |
| 2024 | 0x00000-0x0ffff -> data memory on the expanded bus |
| 2025 | 0x10000-0x1ffff -> data memory on the byte-wide bus |
| 2026 | |
| 2027 | For non-partitioned mode the following memory map is assumed: |
| 2028 | |
| 2029 | 0x0000-0xffff -> data memory (the bus used to access it does not matter) |
| 2030 | */ |
| 2031 | static READ32_HANDLER(external_ram_iaddr) |
| 2032 | { |
| 2033 | // if partition mode is set, adjust offset based on the bus |
| 2034 | if (!GET_PM) { |
| 2035 | if (!GET_EXBS) { |
| 2036 | if ((offset >= ds5002fp_partitions[GET_PA]) && (offset <= ds5002fp_ranges[ds5002fp.range])) { |
| 2037 | offset += 0x10000; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | if(ds5002fp.ebram_iaddr_callback) |
| 2042 | return ds5002fp.ebram_iaddr_callback(machine,offset,mem_mask); |
| 2043 | else { |
| 2044 | //LOG(("ds5002fp #%d: external ram address requested (8 bit offset=%02x), but no callback available! at PC:%04x\n", cpu_getactivecpu(), offset, PC)); |
| 2045 | } |
| 2046 | |
| 2047 | return offset; |
| 2048 | } |
| 2049 | |
| 2050 | /*Push the current PC to the stack*/ |
| 2051 | INLINE void push_pc() |
| 2052 | { |
| 2053 | UINT8 tmpSP = R_SP; //Grab and Increment Stack Pointer |
| 2054 | tmpSP++; // "" |
| 2055 | SFR_W(SP,tmpSP); // "" |
| 2056 | if (tmpSP == R_SP) //Ensure it was able to write to new stack location |
| 2057 | IRAM_IW(tmpSP, (PC & 0xff)); //Store low byte of PC to Internal Ram (Use IRAM_IW to store stack above 128 bytes) |
| 2058 | tmpSP = R_SP; //Increment Stack Pointer |
| 2059 | tmpSP++; // "" |
| 2060 | SFR_W(SP,tmpSP); // "" |
| 2061 | if (tmpSP == R_SP) //Ensure it was able to write to new stack location |
| 2062 | IRAM_IW(tmpSP, ( (PC & 0xff00) >> 8)); //Store hi byte of PC to next address in Internal Ram (Use IRAM_IW to store stack above 128 bytes) |
| 2063 | } |
| 2064 | |
| 2065 | /*Pop the current PC off the stack and into the pc*/ |
| 2066 | INLINE void pop_pc() |
| 2067 | { |
| 2068 | UINT8 tmpSP = R_SP; //Grab Stack Pointer |
| 2069 | PC = (IRAM_IR(tmpSP) & 0xff) << 8; //Store hi byte to PC (must use IRAM_IR to access stack pointing above 128 bytes) |
| 2070 | tmpSP = R_SP-1; //Decrement Stack Pointer |
| 2071 | SFR_W(SP,tmpSP); // "" |
| 2072 | if (tmpSP == R_SP) //Ensure it was able to write to new stack location |
| 2073 | PC = PC | IRAM_IR(tmpSP); //Store lo byte to PC (must use IRAM_IR to access stack pointing above 128 bytes) |
| 2074 | SFR_W(SP,tmpSP-1); //Decrement Stack Pointer |
| 2075 | } |
| 2076 | |
| 2077 | //Set the PSW Parity Flag |
| 2078 | INLINE void set_parity() |
| 2079 | { |
| 2080 | //This flag will be set when the accumulator contains an odd # of bits set.. |
| 2081 | int i, |
| 2082 | p = 0; |
| 2083 | for (i=1; i<=128; i=i*2) { //Test for each of the 8 bits in the ACC! |
| 2084 | if ((R_ACC & i) != 0) |
| 2085 | p++; //Keep track of how many bits are set |
| 2086 | } |
| 2087 | |
| 2088 | //Update the PSW Pairty bit |
| 2089 | SET_P(p & 1); |
| 2090 | } |
| 2091 | |
| 2092 | static READ8_HANDLER(bit_address_r) |
| 2093 | { |
| 2094 | int word; |
| 2095 | int mask; |
| 2096 | int bit_pos; |
| 2097 | int base; /* base of bit space or sfr */ |
| 2098 | int distance; /* distance between bit addressable words */ |
| 2099 | /* 1 for normal bits, 8 for sfr bit addresses */ |
| 2100 | |
| 2101 | offset &= 0xff; |
| 2102 | |
| 2103 | //User defined bit addresses 0x20-0x2f (values are 0x0-0x7f) |
| 2104 | if (offset < 0x80) { |
| 2105 | base = 0x20; |
| 2106 | distance = 1; |
| 2107 | } |
| 2108 | //SFR bit addressable registers |
| 2109 | else { |
| 2110 | base = 0x80; |
| 2111 | distance = 8; |
| 2112 | } |
| 2113 | word = ( (offset & 0x78) >> 3) * distance + base; |
| 2114 | bit_pos = offset & 0x7; |
| 2115 | mask = 0x1 << bit_pos; |
| 2116 | return((IRAM_R(word) & mask) >> bit_pos); //Do not use IRAM_IR |
| 2117 | } |
| 2118 | |
| 2119 | |
| 2120 | static WRITE8_HANDLER(bit_address_w) |
| 2121 | { |
| 2122 | int word; |
| 2123 | int mask; |
| 2124 | int bit_pos; |
| 2125 | int result; |
| 2126 | int base; |
| 2127 | int distance; |
| 2128 | |
| 2129 | offset &= 0xff; |
| 2130 | |
| 2131 | //User defined bit addresses 0x20-0x2f (values are 0x0-0x7f) |
| 2132 | if (offset < 0x80) { |
| 2133 | base = 0x20; |
| 2134 | distance = 1; |
| 2135 | } |
| 2136 | //SFR bit addressable registers |
| 2137 | else { |
| 2138 | base = 0x80; |
| 2139 | distance = 8; |
| 2140 | } |
| 2141 | word = ((offset & 0x78) >> 3) * distance + base; |
| 2142 | bit_pos = offset & 0x7; |
| 2143 | data = (data & 0x1) << bit_pos; |
| 2144 | mask = ~(1 << bit_pos) & 0xff; |
| 2145 | result = IRAM_R(word) & mask; //Do not use IRAM_IR |
| 2146 | result = result | data; |
| 2147 | IRAM_W(word, result); //Do not use IRAM_IW |
| 2148 | } |
| 2149 | |
| 2150 | /* The following two handlers are used by the MAME Debugger Memory Window... |
| 2151 | By keeping these functions separate from the internally used IRAM_W/IRAM_R functions, |
| 2152 | we can manipulate and display internal memory in the debugger memory window in a layout |
| 2153 | that is not necessarily how the real memory is.. this will be especially useful for |
| 2154 | the 8052 chip where both the SFR and the upper 128 bytes of ram are mapped to the same |
| 2155 | address, so we can handle that here by mapping the sfr to a different address */ |
| 2156 | |
| 2157 | READ8_HANDLER(ds5002fp_internal_r) |
| 2158 | { |
| 2159 | //Restrict internal ram to 256 Bytes max |
| 2160 | if(offset < 0x100) |
| 2161 | return IRAM_R(offset); |
| 2162 | else |
| 2163 | return 0; |
| 2164 | } |
| 2165 | WRITE8_HANDLER(ds5002fp_internal_w) |
| 2166 | { |
| 2167 | //Restrict internal ram to 256 Bytes max |
| 2168 | if(offset < 0x100) |
| 2169 | IRAM_W(offset,data); |
| 2170 | } |
| 2171 | |
| 2172 | INLINE void do_add_flags(UINT8 a, UINT8 data, UINT8 c) |
| 2173 | { |
| 2174 | UINT16 result = a+data+c; |
| 2175 | INT16 result1 = (INT8)a+(INT8)data+c; |
| 2176 | int cy, ac, ov; |
| 2177 | |
| 2178 | cy = (result & 0x100) >> 8; |
| 2179 | result = (a&0x0f)+(data&0x0f)+c; |
| 2180 | ac = (result & 0x10) >> 4; |
| 2181 | ov = (result1 < -128 || result1 > 127); |
| 2182 | |
| 2183 | SET_CY(cy); |
| 2184 | SET_AC(ac); |
| 2185 | SET_OV(ov); |
| 2186 | |
| 2187 | #ifdef ENABLE_DEBUGGER |
| 2188 | // mame_printf_debug("add: result=%x, c=%x, ac=%x, ov=%x\n",a+data+c,cy,ac,ov); |
| 2189 | #endif |
| 2190 | } |
| 2191 | |
| 2192 | INLINE void do_sub_flags(UINT8 a, UINT8 data, UINT8 c) |
| 2193 | { |
| 2194 | UINT16 result = a-(data+c); |
| 2195 | INT16 result1 = (INT8)a-(INT8)(data+c); |
| 2196 | int cy, ac, ov; |
| 2197 | cy = (result & 0x100) >> 8; |
| 2198 | result = (a&0x0f)-((data&0x0f)+c); |
| 2199 | ac = (result & 0x10) >> 4; |
| 2200 | ov = (result1 < -128 || result1 > 127); |
| 2201 | SET_CY(cy); |
| 2202 | SET_AC(ac); |
| 2203 | SET_OV(ov); |
| 2204 | |
| 2205 | #ifdef ENABLE_DEBUGGER |
| 2206 | // mame_printf_debug("sub: a=%x, d=%x, c=%x, result=%x, cy=%x, ac=%x, ov=%x\n",a,data,c,a-data-c,cy,ac,ov); |
| 2207 | #endif |
| 2208 | } |
| 2209 | |
| 2210 | INLINE void update_timer(int cyc) |
| 2211 | { |
| 2212 | //This code sucks, needs to be rewritten SJE |
| 2213 | |
| 2214 | //Todo: Probably better to store the current mode of the timer on a write, so we don't waste time reading it. |
| 2215 | |
| 2216 | //Note: Counting modes increment on 1 machine cycle (12 oscilator periods) - except Timer 2 in certain modes |
| 2217 | |
| 2218 | //Update Timer 0 |
| 2219 | if(GET_TR0) { |
| 2220 | //Determine Mode |
| 2221 | int mode = (GET_M0_1<<1) | GET_M0_0; |
| 2222 | int overflow; |
| 2223 | UINT16 count = 0; |
| 2224 | switch(mode) { |
| 2225 | case 0: //13 Bit Timer Mode |
| 2226 | count = ((R_TH0<<8) | R_TL0); |
| 2227 | overflow = 0x3fff; |
| 2228 | //Todo - really, we update HI counter when LO counter hits 0x20 |
| 2229 | case 1: //16 Bit Timer Mode |
| 2230 | count = ((R_TH0<<8) | R_TL0); |
| 2231 | overflow = 0xffff; |
| 2232 | //Check for overflow |
| 2233 | if((UINT32)(count+(cyc/12))>overflow) { |
| 2234 | //Any overflow from cycles? |
| 2235 | cyc-= (overflow-count)*12; |
| 2236 | count = 0; |
| 2237 | SET_TF0(1); |
| 2238 | } |
| 2239 | //Update the timer |
| 2240 | if(cyc) { |
| 2241 | int inctimer = 0; |
| 2242 | //Gate Bit Set? Timer only incremented if Int0 is set! |
| 2243 | if(GET_GATE0 && GET_IE0) |
| 2244 | inctimer = (cyc/12); |
| 2245 | //Counter Mode? Only increment on 1-0 transition of the Port 3's T0 Line |
| 2246 | if(GET_CT0) { |
| 2247 | //Not supported |
| 2248 | } |
| 2249 | //Neither, regular timer mode |
| 2250 | if(!GET_GATE0 && !GET_CT0) |
| 2251 | inctimer = (cyc/12); |
| 2252 | |
| 2253 | count+=inctimer; //Increment counter |
| 2254 | } |
| 2255 | //Update new values of the counter |
| 2256 | R_TH0 = (count>>8) & 0xff; |
| 2257 | R_TL0 = count & 0xff; |
| 2258 | break; |
| 2259 | case 2: //8 Bit Autoreload |
| 2260 | overflow = 0xff; |
| 2261 | count = R_TL0; |
| 2262 | //Check for overflow |
| 2263 | if(count+(cyc/12)>overflow) { |
| 2264 | SET_TF0(1); |
| 2265 | //Reload |
| 2266 | count = R_TH0+(overflow-count); |
| 2267 | } |
| 2268 | else |
| 2269 | count+=(cyc/12); |
| 2270 | //Update new values of the counter |
| 2271 | R_TL0 = count & 0xff; |
| 2272 | break; |
| 2273 | case 3: //Split Timer |
| 2274 | //Split Timer 1 |
| 2275 | overflow = 0xff; |
| 2276 | count = R_TL0; |
| 2277 | //Check for overflow |
| 2278 | if(count+(cyc/12)>overflow) { |
| 2279 | count = overflow-count; |
| 2280 | SET_TF0(1); |
| 2281 | } |
| 2282 | else |
| 2283 | count+=(cyc/12); |
| 2284 | //Update new values of the counter |
| 2285 | R_TL0 = count & 0xff; |
| 2286 | |
| 2287 | //Split Timer 2 |
| 2288 | if(GET_TR1) { |
| 2289 | overflow = 0xff; |
| 2290 | count = R_TH0; |
| 2291 | //Check for overflow |
| 2292 | if(count+(cyc/12)>overflow) { |
| 2293 | count = overflow-count; |
| 2294 | SET_TF1(1); |
| 2295 | } |
| 2296 | else |
| 2297 | count+=(cyc/12); |
| 2298 | //Update new values of the counter |
| 2299 | R_TH0 = count & 0xff; |
| 2300 | } |
| 2301 | break; |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | //Update Timer 1 |
| 2306 | if(GET_TR1) { |
| 2307 | //Determine Mode |
| 2308 | int mode = (GET_M1_1<<1) | GET_M1_0; |
| 2309 | int overflow; |
| 2310 | UINT16 count = 0; |
| 2311 | switch(mode) { |
| 2312 | case 0: //13 Bit Timer Mode |
| 2313 | count = ((R_TH1<<8) | R_TL1); |
| 2314 | overflow = 0x3fff; |
| 2315 | //Todo - really, we update HI counter when LO counter hits 0x20 |
| 2316 | case 1: //16 Bit Timer Mode |
| 2317 | count = ((R_TH1<<8) | R_TL1); |
| 2318 | overflow = 0xffff; |
| 2319 | //Check for overflow |
| 2320 | if((UINT32)(count+(cyc/12))>overflow) { |
| 2321 | |
| 2322 | //TODO: Timer 1 can be set as Serial Baud Rate in the 8051 only... process bits here.. |
| 2323 | |
| 2324 | //Any overflow from cycles? |
| 2325 | cyc-= (overflow-count)*12; |
| 2326 | count = 0; |
| 2327 | SET_TF1(1); |
| 2328 | } |
| 2329 | //Update the timer |
| 2330 | if(cyc) { |
| 2331 | int inctimer = 0; |
| 2332 | //Gate Bit Set? Timer only incremented if Int0 is set! |
| 2333 | if(GET_GATE1 && GET_IE1) |
| 2334 | inctimer = (cyc/12); |
| 2335 | //Counter Mode? Only increment on 1-0 transition of the Port 3's T0 Line |
| 2336 | if(GET_CT1) { |
| 2337 | //Not supported |
| 2338 | } |
| 2339 | //Neither, regular timer mode |
| 2340 | if(!GET_GATE1 && !GET_CT1) |
| 2341 | inctimer = (cyc/12); |
| 2342 | |
| 2343 | count+=inctimer; //Increment counter |
| 2344 | } |
| 2345 | //Update new values of the counter |
| 2346 | R_TH1 = (count>>8) & 0xff; |
| 2347 | R_TL1 = count & 0xff; |
| 2348 | break; |
| 2349 | case 2: //8 Bit Autoreload |
| 2350 | overflow = 0xff; |
| 2351 | count = R_TL1; |
| 2352 | //Check for overflow |
| 2353 | if(count+(cyc/12)>overflow) { |
| 2354 | SET_TF1(1); |
| 2355 | //Reload |
| 2356 | count = R_TH1+(overflow-count); |
| 2357 | } |
| 2358 | else |
| 2359 | count+=(cyc/12); |
| 2360 | //Update new values of the counter |
| 2361 | R_TL1 = count & 0xff; |
| 2362 | break; |
| 2363 | case 3: //Split Timer |
| 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | } |
| 2368 | |
| 2369 | //Set up to transmit data out of serial port |
| 2370 | //NOTE: Enable Serial Port Interrupt bit is NOT required to send/receive data! |
| 2371 | INLINE void serial_transmit(UINT8 data) |
| 2372 | { |
| 2373 | int mode = (GET_SM0<<1) | GET_SM1; |
| 2374 | |
| 2375 | //Flag that we're sending data |
| 2376 | uart.sending = 1; |
| 2377 | uart.data_out = data; |
| 2378 | switch(mode) { |
| 2379 | //8 bit shifter ( + start,stop bit ) - baud set by clock freq / 12 |
| 2380 | case 0: |
| 2381 | uart.timerbaud = 0; |
| 2382 | uart.bitcycles = 0; |
| 2383 | uart.bits_to_send = 8+2; |
| 2384 | break; |
| 2385 | //8 bit uart ( + start,stop bit ) - baud set by timer1 or timer2 |
| 2386 | case 1: |
| 2387 | uart.timerbaud = 1; |
| 2388 | uart.bits_to_send = 8+2; |
| 2389 | break; |
| 2390 | //9 bit uart |
| 2391 | case 2: |
| 2392 | case 3: |
| 2393 | LOG(("Serial mode 2 & 3 not supported in ds5002fp!\n")); |
| 2394 | break; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | //Check and update status of serial port |
| 2399 | INLINE void update_serial(int cyc) |
| 2400 | { |
| 2401 | //Any bits left to send? |
| 2402 | if(uart.bits_to_send) { |
| 2403 | //Timer Generated baud? |
| 2404 | if(uart.timerbaud) { |
| 2405 | //Let Timer overflow handle removing bits |
| 2406 | } |
| 2407 | else { |
| 2408 | //Oscillator Based baud rate = Osc/12 baud rate, however it also means 1 bit = 12 cycles. |
| 2409 | uart.bitcycles+=cyc; |
| 2410 | if(uart.bitcycles > 11) { |
| 2411 | int bits_sent = uart.bitcycles / 12; |
| 2412 | int diff = uart.bitcycles % 12; |
| 2413 | //don't allow more bits sent than ready to send |
| 2414 | if(bits_sent > uart.bits_to_send) { |
| 2415 | bits_sent = uart.bits_to_send; |
| 2416 | diff = 0; |
| 2417 | } |
| 2418 | uart.bits_to_send-=bits_sent; |
| 2419 | uart.bitcycles = diff; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | //If no bits left to send - flag the interrupt & call the callback |
| 2424 | if(!uart.bits_to_send) { |
| 2425 | //Clear sending flag |
| 2426 | uart.sending = 0; |
| 2427 | uart.bitcycles = 0; |
| 2428 | //Call the callback function |
| 2429 | if(ds5002fp.serial_tx_callback) |
| 2430 | ds5002fp.serial_tx_callback(uart.data_out); |
| 2431 | //Set Interrupt Flag |
| 2432 | SET_TI(1); |
| 2433 | //Note: we'll let the main loop catch the interrupt |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | /************************************************************************** |
| 2438 | * Generic set_info |
| 2439 | **************************************************************************/ |
| 2440 | |
| 2441 | static void ds5002fp_set_info(UINT32 state, cpuinfo *info) |
| 2442 | { |
| 2443 | switch (state) |
| 2444 | { |
| 2445 | /* --- the following bits of info are set as 64-bit signed integers --- */ |
| 2446 | case CPUINFO_INT_PC: PC = info->i; break; |
| 2447 | case CPUINFO_INT_SP: ds5002fp.sp = info->i; break; |
| 2448 | |
| 2449 | case CPUINFO_INT_INPUT_STATE + DS5002FP_INT0_LINE: ds5002fp_set_irq_line(DS5002FP_INT0_LINE, info->i); break; |
| 2450 | case CPUINFO_INT_INPUT_STATE + DS5002FP_INT1_LINE: ds5002fp_set_irq_line(DS5002FP_INT1_LINE, info->i); break; |
| 2451 | case CPUINFO_INT_INPUT_STATE + DS5002FP_T0_LINE: ds5002fp_set_irq_line(DS5002FP_T0_LINE, info->i); break; |
| 2452 | case CPUINFO_INT_INPUT_STATE + DS5002FP_T1_LINE: ds5002fp_set_irq_line(DS5002FP_T1_LINE, info->i); break; |
| 2453 | case CPUINFO_INT_INPUT_STATE + DS5002FP_RX_LINE: ds5002fp_set_irq_line(DS5002FP_RX_LINE, info->i); break; |
| 2454 | case CPUINFO_INT_INPUT_STATE + DS5002FP_PFI_LINE: ds5002fp_set_irq_line(DS5002FP_PFI_LINE, info->i); break; |
| 2455 | |
| 2456 | case CPUINFO_INT_REGISTER + DS5002FP_PC: PC = info->i; break; |
| 2457 | case CPUINFO_INT_REGISTER + DS5002FP_SP: R_SP = info->i; break; |
| 2458 | case CPUINFO_INT_REGISTER + DS5002FP_PSW: ds5002fp.psw = info->i; break; |
| 2459 | case CPUINFO_INT_REGISTER + DS5002FP_ACC: ds5002fp.acc = info->i; break; |
| 2460 | case CPUINFO_INT_REGISTER + DS5002FP_B: ds5002fp.b = info->i; break; |
| 2461 | case CPUINFO_INT_REGISTER + DS5002FP_DPH: ds5002fp.dph = info->i; break; |
| 2462 | case CPUINFO_INT_REGISTER + DS5002FP_DPL: ds5002fp.dpl = info->i; break; |
| 2463 | case CPUINFO_INT_REGISTER + DS5002FP_IE: ds5002fp.ie = info->i; break; |
| 2464 | case CPUINFO_INT_REGISTER + DS5002FP_PCON: ds5002fp.pcon = info->i; break; |
| 2465 | case CPUINFO_INT_REGISTER + DS5002FP_MCON: ds5002fp.mcon = info->i; break; |
| 2466 | case CPUINFO_INT_REGISTER + DS5002FP_RPCTL: ds5002fp.rpctl = info->i; break; |
| 2467 | case CPUINFO_INT_REGISTER + DS5002FP_CRC: ds5002fp.crc = info->i; break; |
| 2468 | case CPUINFO_INT_REGISTER + DS5002FP_R0: ds5002fp.IntRam[0+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2469 | case CPUINFO_INT_REGISTER + DS5002FP_R1: ds5002fp.IntRam[1+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2470 | case CPUINFO_INT_REGISTER + DS5002FP_R2: ds5002fp.IntRam[2+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2471 | case CPUINFO_INT_REGISTER + DS5002FP_R3: ds5002fp.IntRam[3+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2472 | case CPUINFO_INT_REGISTER + DS5002FP_R4: ds5002fp.IntRam[4+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2473 | case CPUINFO_INT_REGISTER + DS5002FP_R5: ds5002fp.IntRam[5+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2474 | case CPUINFO_INT_REGISTER + DS5002FP_R6: ds5002fp.IntRam[6+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2475 | case CPUINFO_INT_REGISTER + DS5002FP_R7: ds5002fp.IntRam[7+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2476 | case CPUINFO_INT_REGISTER + DS5002FP_RB: ds5002fp.IntRam[8+(8*((ds5002fp.psw & 0x18)>>3))] = info->i; break; |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | /************************************************************************** |
| 2481 | * Generic get_info |
| 2482 | **************************************************************************/ |
| 2483 | |
| 2484 | void ds5002fp_get_info(UINT32 state, cpuinfo *info) |
| 2485 | { |
| 2486 | DS5002FP *r = &ds5002fp; |
| 2487 | |
| 2488 | switch (state) |
| 2489 | { |
| 2490 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 2491 | case CPUINFO_INT_CONTEXT_SIZE: info->i = sizeof(ds5002fp); break; |
| 2492 | case CPUINFO_INT_DEFAULT_IRQ_VECTOR: info->i = 0; break; |
| 2493 | case CPUINFO_INT_ENDIANNESS: info->i = CPU_IS_LE; break; |
| 2494 | case CPUINFO_INT_CLOCK_MULTIPLIER: info->i = 1; break; |
| 2495 | case CPUINFO_INT_CLOCK_DIVIDER: info->i = 1; break; |
| 2496 | case CPUINFO_INT_MIN_INSTRUCTION_BYTES: info->i = 1; break; |
| 2497 | case CPUINFO_INT_MAX_INSTRUCTION_BYTES: info->i = 5; break; |
| 2498 | case CPUINFO_INT_MIN_CYCLES: info->i = 1; break; |
| 2499 | case CPUINFO_INT_MAX_CYCLES: info->i = 20; /* rough guess */ break; |
| 2500 | case CPUINFO_INT_INPUT_LINES: info->i = 6; break; |
| 2501 | |
| 2502 | case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 8; break; |
| 2503 | case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_PROGRAM: info->i = 16; break; |
| 2504 | case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_PROGRAM: info->i = 0; break; |
| 2505 | case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_DATA: info->i = 8; break; |
| 2506 | case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_DATA: info->i = 17; /* really 16 bits, but it has 2 address bus */ break; |
| 2507 | case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_DATA: info->i = 0; break; |
| 2508 | case CPUINFO_INT_DATABUS_WIDTH + ADDRESS_SPACE_IO: info->i = 8; break; |
| 2509 | case CPUINFO_INT_ADDRBUS_WIDTH + ADDRESS_SPACE_IO: info->i = 16; break; |
| 2510 | case CPUINFO_INT_ADDRBUS_SHIFT + ADDRESS_SPACE_IO: info->i = 0; break; |
| 2511 | |
| 2512 | case CPUINFO_INT_PREVIOUSPC: info->i = PPC; break; |
| 2513 | case CPUINFO_INT_PC: info->i = PC; break; |
| 2514 | case CPUINFO_INT_SP: info->i = R_SP; break; |
| 2515 | |
| 2516 | case CPUINFO_INT_REGISTER + DS5002FP_PC: info->i = PC; break; |
| 2517 | case CPUINFO_INT_REGISTER + DS5002FP_SP: info->i = R_SP; break; |
| 2518 | case CPUINFO_INT_REGISTER + DS5002FP_PSW: info->i = ds5002fp.psw; break; |
| 2519 | case CPUINFO_INT_REGISTER + DS5002FP_ACC: info->i = ds5002fp.acc; break; |
| 2520 | case CPUINFO_INT_REGISTER + DS5002FP_B: info->i = ds5002fp.b; break; |
| 2521 | case CPUINFO_INT_REGISTER + DS5002FP_DPH: info->i = ds5002fp.dph; break; |
| 2522 | case CPUINFO_INT_REGISTER + DS5002FP_DPL: info->i = ds5002fp.dpl; break; |
| 2523 | case CPUINFO_INT_REGISTER + DS5002FP_IE: info->i = ds5002fp.ie; break; |
| 2524 | case CPUINFO_INT_REGISTER + DS5002FP_PCON: info->i = ds5002fp.pcon; break; |
| 2525 | case CPUINFO_INT_REGISTER + DS5002FP_MCON: info->i = ds5002fp.mcon; break; |
| 2526 | case CPUINFO_INT_REGISTER + DS5002FP_RPCTL: info->i = ds5002fp.rpctl; break; |
| 2527 | case CPUINFO_INT_REGISTER + DS5002FP_CRC: info->i = ds5002fp.crc; break; |
| 2528 | case CPUINFO_INT_REGISTER + DS5002FP_R0: info->i = ds5002fp.IntRam[0+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2529 | case CPUINFO_INT_REGISTER + DS5002FP_R1: info->i = ds5002fp.IntRam[1+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2530 | case CPUINFO_INT_REGISTER + DS5002FP_R2: info->i = ds5002fp.IntRam[2+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2531 | case CPUINFO_INT_REGISTER + DS5002FP_R3: info->i = ds5002fp.IntRam[3+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2532 | case CPUINFO_INT_REGISTER + DS5002FP_R4: info->i = ds5002fp.IntRam[4+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2533 | case CPUINFO_INT_REGISTER + DS5002FP_R5: info->i = ds5002fp.IntRam[5+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2534 | case CPUINFO_INT_REGISTER + DS5002FP_R6: info->i = ds5002fp.IntRam[6+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2535 | case CPUINFO_INT_REGISTER + DS5002FP_R7: info->i = ds5002fp.IntRam[7+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2536 | case CPUINFO_INT_REGISTER + DS5002FP_RB: info->i = ds5002fp.IntRam[8+(8*((ds5002fp.psw & 0x18)>>3))]; break; |
| 2537 | |
| 2538 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 2539 | case CPUINFO_PTR_SET_INFO: info->setinfo = ds5002fp_set_info; break; |
| 2540 | case CPUINFO_PTR_GET_CONTEXT: info->getcontext = ds5002fp_get_context; break; |
| 2541 | case CPUINFO_PTR_SET_CONTEXT: info->setcontext = ds5002fp_set_context; break; |
| 2542 | case CPUINFO_PTR_INIT: info->init = ds5002fp_init; break; |
| 2543 | case CPUINFO_PTR_RESET: info->reset = ds5002fp_reset; break; |
| 2544 | case CPUINFO_PTR_EXIT: info->exit = ds5002fp_exit; break; |
| 2545 | case CPUINFO_PTR_EXECUTE: info->execute = ds5002fp_execute; break; |
| 2546 | case CPUINFO_PTR_BURN: info->burn = NULL; break; |
| 2547 | #ifdef ENABLE_DEBUGGER |
| 2548 | case CPUINFO_PTR_DISASSEMBLE: info->disassemble = ds5002fp_dasm; break; |
| 2549 | #endif /* ENABLE_DEBUGGER */ |
| 2550 | case CPUINFO_PTR_INSTRUCTION_COUNTER: info->icount = &ds5002fp_icount; break; |
| 2551 | |
| 2552 | case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_PROGRAM: info->internal_map8 = NULL; break; |
| 2553 | case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_DATA: info->internal_map8 = NULL; break; |
| 2554 | case CPUINFO_PTR_INTERNAL_MEMORY_MAP + ADDRESS_SPACE_IO: info->internal_map8 = NULL; break; |
| 2555 | |
| 2556 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 2557 | case CPUINFO_STR_NAME: strcpy(info->s, "DS5002FP"); break; |
| 2558 | case CPUINFO_STR_CORE_FAMILY: strcpy(info->s, "Dallas"); break; |
| 2559 | case CPUINFO_STR_CORE_VERSION: strcpy(info->s, "1.0"); break; |
| 2560 | case CPUINFO_STR_CORE_FILE: strcpy(info->s, __FILE__); break; |
| 2561 | case CPUINFO_STR_CORE_CREDITS: strcpy(info->s, "Copyright Manuel Abadia"); break; |
| 2562 | |
| 2563 | case CPUINFO_STR_FLAGS: |
| 2564 | sprintf(info->s, "%c%c%c%c%c%c%c%c", |
| 2565 | r->psw & 0x80 ? 'C':'.', |
| 2566 | r->psw & 0x40 ? 'A':'.', |
| 2567 | r->psw & 0x20 ? 'F':'.', |
| 2568 | r->psw & 0x10 ? '0':'.', |
| 2569 | r->psw & 0x08 ? '1':'.', |
| 2570 | r->psw & 0x04 ? 'V':'.', |
| 2571 | r->psw & 0x02 ? '?':'.', |
| 2572 | r->psw & 0x01 ? 'P':'.'); |
| 2573 | break; |
| 2574 | |
| 2575 | case CPUINFO_STR_REGISTER + DS5002FP_PC: sprintf(info->s, "PC:%04X", r->pc); break; |
| 2576 | case CPUINFO_STR_REGISTER + DS5002FP_SP: sprintf(info->s, "SP:%02X", r->sp); break; |
| 2577 | case CPUINFO_STR_REGISTER + DS5002FP_PSW: sprintf(info->s, "PSW:%02X", r->psw); break; |
| 2578 | case CPUINFO_STR_REGISTER + DS5002FP_ACC: sprintf(info->s, "A:%02X", r->acc); break; |
| 2579 | case CPUINFO_STR_REGISTER + DS5002FP_B: sprintf(info->s, "B:%02X", r->b); break; |
| 2580 | case CPUINFO_STR_REGISTER + DS5002FP_DPH: sprintf(info->s, "DPH:%02X", r->dph); break; |
| 2581 | case CPUINFO_STR_REGISTER + DS5002FP_DPL: sprintf(info->s, "DPL:%02X", r->dpl); break; |
| 2582 | case CPUINFO_STR_REGISTER + DS5002FP_IE: sprintf(info->s, "IE:%02X", r->ie); break; |
| 2583 | case CPUINFO_STR_REGISTER + DS5002FP_PCON: sprintf(info->s, "PCON:%02X", r->pcon); break; |
| 2584 | case CPUINFO_STR_REGISTER + DS5002FP_MCON: sprintf(info->s, "MCON:%02X", r->mcon); break; |
| 2585 | case CPUINFO_STR_REGISTER + DS5002FP_RPCTL: sprintf(info->s, "RPCTL:%02X", r->rpctl);break; |
| 2586 | case CPUINFO_STR_REGISTER + DS5002FP_CRC: sprintf(info->s, "CRC:%02X", r->crc); break; |
| 2587 | case CPUINFO_STR_REGISTER + DS5002FP_R0: sprintf(info->s, "R0:%02X", r->IntRam[0+(8*((r->psw & 0x18)>>3))]); break; |
| 2588 | case CPUINFO_STR_REGISTER + DS5002FP_R1: sprintf(info->s, "R1:%02X", r->IntRam[1+(8*((r->psw & 0x18)>>3))]); break; |
| 2589 | case CPUINFO_STR_REGISTER + DS5002FP_R2: sprintf(info->s, "R2:%02X", r->IntRam[2+(8*((r->psw & 0x18)>>3))]); break; |
| 2590 | case CPUINFO_STR_REGISTER + DS5002FP_R3: sprintf(info->s, "R3:%02X", r->IntRam[3+(8*((r->psw & 0x18)>>3))]); break; |
| 2591 | case CPUINFO_STR_REGISTER + DS5002FP_R4: sprintf(info->s, "R4:%02X", r->IntRam[4+(8*((r->psw & 0x18)>>3))]); break; |
| 2592 | case CPUINFO_STR_REGISTER + DS5002FP_R5: sprintf(info->s, "R5:%02X", r->IntRam[5+(8*((r->psw & 0x18)>>3))]); break; |
| 2593 | case CPUINFO_STR_REGISTER + DS5002FP_R6: sprintf(info->s, "R6:%02X", r->IntRam[6+(8*((r->psw & 0x18)>>3))]); break; |
| 2594 | case CPUINFO_STR_REGISTER + DS5002FP_R7: sprintf(info->s, "R7:%02X", r->IntRam[7+(8*((r->psw & 0x18)>>3))]); break; |
| 2595 | case CPUINFO_STR_REGISTER + DS5002FP_RB: sprintf(info->s, "RB:%02X", ((r->psw & 0x18)>>3)); break; |
| 2596 | } |
| 2597 | } |
src/emu/cpu/drcbex64.c
| r0 | r2628 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | drcbex64.c |
| 4 | |
| 5 | 64-bit x64 back-end for the universal machine language. |
| 6 | |
| 7 | Copyright Aaron Giles |
| 8 | Released for general non-commercial use under the MAME license |
| 9 | Visit http://mamedev.org for licensing and usage restrictions. |
| 10 | |
| 11 | **************************************************************************** |
| 12 | |
| 13 | Future improvements/changes: |
| 14 | |
| 15 | * Add support for FP registers |
| 16 | |
| 17 | * Optimize to avoid unnecessary reloads |
| 18 | |
| 19 | * Identify common pairs and optimize output |
| 20 | |
| 21 | **************************************************************************** |
| 22 | |
| 23 | ------------------------- |
| 24 | ABI/conventions (Windows) |
| 25 | ------------------------- |
| 26 | |
| 27 | Registers: |
| 28 | RAX - volatile, function return value |
| 29 | RBX - non-volatile |
| 30 | RCX - volatile, integer function parameter 1 |
| 31 | RDX - volatile, integer function parameter 2 |
| 32 | RSI - non-volatile |
| 33 | RDI - non-volatile |
| 34 | RBP - non-volatile |
| 35 | R8 - volatile, integer function parameter 3 |
| 36 | R9 - volatile, integer function parameter 4 |
| 37 | R10 - volatile |
| 38 | R11 - volatile, scratch immediate storage |
| 39 | R12 - non-volatile |
| 40 | R13 - non-volatile |
| 41 | R14 - non-volatile |
| 42 | R15 - non-volatile |
| 43 | |
| 44 | XMM0 - volatile, FP function parameter 1 |
| 45 | XMM1 - volatile, FP function parameter 2 |
| 46 | XMM2 - volatile, FP function parameter 3 |
| 47 | XMM3 - volatile, FP function parameter 4 |
| 48 | XMM4 - volatile |
| 49 | XMM5 - volatile |
| 50 | XMM6 - non-volatile |
| 51 | XMM7 - non-volatile |
| 52 | XMM8 - non-volatile |
| 53 | XMM9 - non-volatile |
| 54 | XMM10 - non-volatile |
| 55 | XMM11 - non-volatile |
| 56 | XMM12 - non-volatile |
| 57 | XMM13 - non-volatile |
| 58 | XMM14 - non-volatile |
| 59 | XMM15 - non-volatile |
| 60 | |
| 61 | |
| 62 | ----------------------------- |
| 63 | ABI/conventions (Linux/MacOS) |
| 64 | ----------------------------- |
| 65 | |
| 66 | Registers: |
| 67 | RAX - volatile, function return value |
| 68 | RBX - non-volatile |
| 69 | RCX - volatile, integer function parameter 4 |
| 70 | RDX - volatile, integer function parameter 3 |
| 71 | RSI - volatile, integer function parameter 2 |
| 72 | RDI - volatile, integer function parameter 1 |
| 73 | RBP - non-volatile |
| 74 | R8 - volatile, integer function parameter 5 |
| 75 | R9 - volatile, integer function parameter 6 |
| 76 | R10 - volatile |
| 77 | R11 - volatile, scratch immediate storage |
| 78 | R12 - non-volatile |
| 79 | R13 - non-volatile |
| 80 | R14 - non-volatile |
| 81 | R15 - non-volatile |
| 82 | |
| 83 | XMM0 - volatile, FP function parameter 1 |
| 84 | XMM1 - volatile, FP function parameter 2 |
| 85 | XMM2 - volatile, FP function parameter 3 |
| 86 | XMM3 - volatile, FP function parameter 4 |
| 87 | XMM4 - volatile |
| 88 | XMM5 - volatile |
| 89 | XMM6 - volatile |
| 90 | XMM7 - volatile |
| 91 | XMM8 - volatile |
| 92 | XMM9 - volatile |
| 93 | XMM10 - volatile |
| 94 | XMM11 - volatile |
| 95 | XMM12 - volatile |
| 96 | XMM13 - volatile |
| 97 | XMM14 - volatile |
| 98 | XMM15 - volatile |
| 99 | |
| 100 | |
| 101 | --------------- |
| 102 | Execution model |
| 103 | --------------- |
| 104 | |
| 105 | Registers (Windows): |
| 106 | RAX - scratch register |
| 107 | RBX - maps to I0 |
| 108 | RCX - scratch register |
| 109 | RDX - scratch register |
| 110 | RSI - maps to I1 |
| 111 | RDI - maps to I2 |
| 112 | RBP - pointer to code cache |
| 113 | R8 - scratch register |
| 114 | R9 - scratch register |
| 115 | R10 - scratch register |
| 116 | R11 - scratch register |
| 117 | R12 - maps to I3 |
| 118 | R13 - maps to I4 |
| 119 | R14 - maps to I5 |
| 120 | R15 - maps to I6 |
| 121 | |
| 122 | Registers (Linux/MacOS): |
| 123 | RAX - scratch register |
| 124 | RBX - maps to I0 |
| 125 | RCX - scratch register |
| 126 | RDX - scratch register |
| 127 | RSI - unused |
| 128 | RDI - unused |
| 129 | RBP - pointer to code cache |
| 130 | R8 - scratch register |
| 131 | R9 - scratch register |
| 132 | R10 - scratch register |
| 133 | R11 - scratch register |
| 134 | R12 - maps to I1 |
| 135 | R13 - maps to I2 |
| 136 | R14 - maps to I3 |
| 137 | R15 - maps to I4 |
| 138 | |
| 139 | Entry point: |
| 140 | Assumes 1 parameter passed, which is the codeptr of the code |
| 141 | to execute once the environment is set up. |
| 142 | |
| 143 | Exit point: |
| 144 | Assumes exit value is in RAX. |
| 145 | |
| 146 | Entry stack: |
| 147 | [rsp] - return |
| 148 | |
| 149 | Runtime stack: |
| 150 | [rsp] - r9 home |
| 151 | [rsp+8] - r8 home |
| 152 | [rsp+16] - rdx home |
| 153 | [rsp+24] - rcx home |
| 154 | [rsp+40] - saved r15 |
| 155 | [rsp+48] - saved r14 |
| 156 | [rsp+56] - saved r13 |
| 157 | [rsp+64] - saved r12 |
| 158 | [rsp+72] - saved ebp |
| 159 | [rsp+80] - saved edi |
| 160 | [rsp+88] - saved esi |
| 161 | [rsp+96] - saved ebx |
| 162 | [rsp+104] - ret |
| 163 | |
| 164 | ***************************************************************************/ |
| 165 | |
| 166 | #include "drcuml.h" |
| 167 | #include "drcumld.h" |
| 168 | #include "drcbeut.h" |
| 169 | #include "debugger.h" |
| 170 | #include "x86emit.h" |
| 171 | #undef REG_SP |
| 172 | #include "x86log.h" |
| 173 | #include <math.h> |
| 174 | |
| 175 | |
| 176 | |
| 177 | /*************************************************************************** |
| 178 | DEBUGGING |
| 179 | ***************************************************************************/ |
| 180 | |
| 181 | #define LOG_HASHJMPS (0) |
| 182 | |
| 183 | |
| 184 | |
| 185 | /*************************************************************************** |
| 186 | CONSTANTS |
| 187 | ***************************************************************************/ |
| 188 | |
| 189 | #define PTYPE_M (1 << DRCUML_PTYPE_MEMORY) |
| 190 | #define PTYPE_I (1 << DRCUML_PTYPE_IMMEDIATE) |
| 191 | #define PTYPE_R (1 << DRCUML_PTYPE_INT_REGISTER) |
| 192 | #define PTYPE_F (1 << DRCUML_PTYPE_FLOAT_REGISTER) |
| 193 | #define PTYPE_MI (PTYPE_M | PTYPE_I) |
| 194 | #define PTYPE_RI (PTYPE_R | PTYPE_I) |
| 195 | #define PTYPE_MR (PTYPE_M | PTYPE_R) |
| 196 | #define PTYPE_MRI (PTYPE_M | PTYPE_R | PTYPE_I) |
| 197 | #define PTYPE_MF (PTYPE_M | PTYPE_F) |
| 198 | |
| 199 | #ifdef X64_WINDOWS_ABI |
| 200 | |
| 201 | #define REG_PARAM1 REG_RCX |
| 202 | #define REG_PARAM2 REG_RDX |
| 203 | #define REG_PARAM3 REG_R8 |
| 204 | #define REG_PARAM4 REG_R9 |
| 205 | |
| 206 | #else |
| 207 | |
| 208 | #define REG_PARAM1 REG_RDI |
| 209 | #define REG_PARAM2 REG_RSI |
| 210 | #define REG_PARAM3 REG_RDX |
| 211 | #define REG_PARAM4 REG_RCX |
| 212 | |
| 213 | #endif |
| 214 | |
| 215 | |
| 216 | |
| 217 | /*************************************************************************** |
| 218 | MACROS |
| 219 | ***************************************************************************/ |
| 220 | |
| 221 | #define X86_CONDITION(condflags) (condition_map[condflags - DRCUML_COND_Z]) |
| 222 | #define X86_NOT_CONDITION(condflags) (condition_map[condflags - DRCUML_COND_Z] ^ 1) |
| 223 | |
| 224 | #undef MABS |
| 225 | #define MABS(drcbe, ptr) MBD(REG_RBP, offset_from_rbp(drcbe, (FPTR)(ptr))) |
| 226 | |
| 227 | |
| 228 | |
| 229 | /*************************************************************************** |
| 230 | TYPE DEFINITIONS |
| 231 | ***************************************************************************/ |
| 232 | |
| 233 | /* entry point */ |
| 234 | typedef UINT32 (*x86_entry_point_func)(UINT8 *rbpvalue, x86code *entry); |
| 235 | |
| 236 | /* opcode handler */ |
| 237 | typedef x86code *(*opcode_generate_func)(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 238 | |
| 239 | |
| 240 | /* opcode table entry */ |
| 241 | typedef struct _opcode_table_entry opcode_table_entry; |
| 242 | struct _opcode_table_entry |
| 243 | { |
| 244 | drcuml_opcode opcode; /* opcode in question */ |
| 245 | opcode_generate_func func; /* function pointer to the work */ |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | /* internal backend-specific state */ |
| 250 | struct _drcbe_state |
| 251 | { |
| 252 | drcuml_state * drcuml; /* pointer back to our owner */ |
| 253 | drccache * cache; /* pointer to the cache */ |
| 254 | drcuml_machine_state state; /* state of the machine */ |
| 255 | drchash_state * hash; /* hash table state */ |
| 256 | drcmap_state * map; /* code map */ |
| 257 | drclabel_list * labels; /* label list */ |
| 258 | |
| 259 | x86_entry_point_func entry; /* entry point */ |
| 260 | x86code * exit; /* exit point */ |
| 261 | x86code * nocode; /* nocode handler */ |
| 262 | |
| 263 | x86code * mame_debug_hook; /* debugger callback */ |
| 264 | x86code * debug_log_hashjmp; /* hashjmp debugging */ |
| 265 | x86code * drcmap_get_value; /* map lookup helper */ |
| 266 | data_accessors accessors[ADDRESS_SPACES];/* memory accessors */ |
| 267 | |
| 268 | UINT32 ssemode; /* saved SSE mode */ |
| 269 | UINT32 ssemodesave; /* temporary location for saving */ |
| 270 | UINT32 ssecontrol[4]; /* copy of the sse_control array */ |
| 271 | UINT32 * absmask32; /* absolute value mask (32-bit) */ |
| 272 | UINT64 * absmask64; /* absolute value mask (32-bit) */ |
| 273 | |
| 274 | void * stacksave; /* saved stack pointer */ |
| 275 | void * hashstacksave; /* saved stack pointer for hashjmp */ |
| 276 | |
| 277 | UINT8 * rbpvalue; /* value of RBP */ |
| 278 | UINT8 flagsmap[0x1000]; /* flags map */ |
| 279 | |
| 280 | x86log_context * log; /* logging */ |
| 281 | }; |
| 282 | |
| 283 | |
| 284 | |
| 285 | /*************************************************************************** |
| 286 | FUNCTION PROTOTYPES |
| 287 | ***************************************************************************/ |
| 288 | |
| 289 | /* primary back-end callbacks */ |
| 290 | static drcbe_state *drcbex64_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits); |
| 291 | static void drcbex64_free(drcbe_state *drcbe); |
| 292 | static void drcbex64_reset(drcbe_state *drcbe); |
| 293 | static int drcbex64_execute(drcbe_state *drcbe, drcuml_codehandle *entry); |
| 294 | static void drcbex64_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst); |
| 295 | static int drcbex64_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc); |
| 296 | |
| 297 | /* private helper functions */ |
| 298 | static void fixup_label(void *parameter, drccodeptr labelcodeptr); |
| 299 | |
| 300 | |
| 301 | |
| 302 | /*************************************************************************** |
| 303 | GLOBAL VARIABLES |
| 304 | ***************************************************************************/ |
| 305 | |
| 306 | /* globally-accessible interface to the backend */ |
| 307 | const drcbe_interface drcbe_x64_be_interface = |
| 308 | { |
| 309 | drcbex64_alloc, |
| 310 | drcbex64_free, |
| 311 | drcbex64_reset, |
| 312 | drcbex64_execute, |
| 313 | drcbex64_generate, |
| 314 | drcbex64_hash_exists |
| 315 | }; |
| 316 | |
| 317 | /* opcode table */ |
| 318 | static opcode_generate_func opcode_table[DRCUML_OP_MAX]; |
| 319 | |
| 320 | /* size-to-mask table */ |
| 321 | static const UINT64 size_to_mask[] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0, U64(0xffffffffffffffff) }; |
| 322 | |
| 323 | /* register mapping tables */ |
| 324 | static UINT8 int_register_map[DRCUML_REG_I_END - DRCUML_REG_I0] = |
| 325 | { |
| 326 | #ifdef X64_WINDOWS_ABI |
| 327 | REG_RBX, REG_RSI, REG_RDI, REG_R12, REG_R13, REG_R14, REG_R15 |
| 328 | #else |
| 329 | REG_RBX, REG_R12, REG_R13, REG_R14, REG_R15 |
| 330 | #endif |
| 331 | }; |
| 332 | |
| 333 | static UINT8 float_register_map[DRCUML_REG_F_END - DRCUML_REG_F0] = |
| 334 | { |
| 335 | 0 |
| 336 | }; |
| 337 | |
| 338 | /* condition mapping table */ |
| 339 | static UINT8 condition_map[DRCUML_COND_MAX - DRCUML_COND_Z] = |
| 340 | { |
| 341 | COND_Z, /* DRCUML_COND_Z = 0x80, requires Z */ |
| 342 | COND_NZ, /* DRCUML_COND_NZ, requires Z */ |
| 343 | COND_S, /* DRCUML_COND_S, requires S */ |
| 344 | COND_NS, /* DRCUML_COND_NS, requires S */ |
| 345 | COND_C, /* DRCUML_COND_C, requires C */ |
| 346 | COND_NC, /* DRCUML_COND_NC, requires C */ |
| 347 | COND_O, /* DRCUML_COND_V, requires V */ |
| 348 | COND_NO, /* DRCUML_COND_NV, requires V */ |
| 349 | COND_P, /* DRCUML_COND_U, requires U */ |
| 350 | COND_NP, /* DRCUML_COND_NU, requires U */ |
| 351 | COND_A, /* DRCUML_COND_A, requires CZ */ |
| 352 | COND_BE, /* DRCUML_COND_BE, requires CZ */ |
| 353 | COND_G, /* DRCUML_COND_G, requires SVZ */ |
| 354 | COND_LE, /* DRCUML_COND_LE, requires SVZ */ |
| 355 | COND_L, /* DRCUML_COND_L, requires SV */ |
| 356 | COND_GE, /* DRCUML_COND_GE, requires SV */ |
| 357 | }; |
| 358 | |
| 359 | |
| 360 | |
| 361 | /*************************************************************************** |
| 362 | TABLES |
| 363 | ***************************************************************************/ |
| 364 | |
| 365 | static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 366 | static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 367 | static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 368 | static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 369 | static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 370 | |
| 371 | static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 372 | static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 373 | static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 374 | static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 375 | static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 376 | static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 377 | static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 378 | static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 379 | static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 380 | |
| 381 | static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 382 | static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 383 | static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 384 | |
| 385 | static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 386 | static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 387 | static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 388 | static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 389 | static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 390 | static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 391 | static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 392 | static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 393 | static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 394 | static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 395 | static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 396 | static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 397 | static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 398 | static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 399 | static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 400 | static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 401 | static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 402 | static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 403 | static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 404 | static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 405 | static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 406 | static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 407 | static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 408 | static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 409 | static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 410 | static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 411 | static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 412 | static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 413 | static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 414 | static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 415 | static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 416 | static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 417 | static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 418 | static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 419 | static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 420 | static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 421 | static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 422 | static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 423 | static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 424 | static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 425 | static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 426 | static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 427 | static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 428 | static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 429 | static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 430 | static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 431 | static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 432 | static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 433 | static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 434 | static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 435 | static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 436 | static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 437 | static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 438 | static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 439 | static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 440 | static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 441 | |
| 442 | static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 443 | static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 444 | static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 445 | static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 446 | static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 447 | static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 448 | static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 449 | static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 450 | static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 451 | static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 452 | static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 453 | static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 454 | static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 455 | static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 456 | static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 457 | static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 458 | static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 459 | static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 460 | static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 461 | static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 462 | static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 463 | static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 464 | static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 465 | static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 466 | static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 467 | static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 468 | static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 469 | static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 470 | static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 471 | |
| 472 | static const opcode_table_entry opcode_table_source[] = |
| 473 | { |
| 474 | /* Compile-time opcodes */ |
| 475 | { DRCUML_OP_HANDLE, op_handle }, /* HANDLE handle */ |
| 476 | { DRCUML_OP_HASH, op_hash }, /* HASH mode,pc */ |
| 477 | { DRCUML_OP_LABEL, op_label }, /* LABEL imm */ |
| 478 | { DRCUML_OP_COMMENT, op_comment }, /* COMMENT string */ |
| 479 | { DRCUML_OP_MAPVAR, op_mapvar }, /* MAPVAR mapvar,value */ |
| 480 | |
| 481 | /* Control Flow Operations */ |
| 482 | { DRCUML_OP_DEBUG, op_debug }, /* DEBUG pc */ |
| 483 | { DRCUML_OP_EXIT, op_exit }, /* EXIT src1[,c] */ |
| 484 | { DRCUML_OP_HASHJMP, op_hashjmp }, /* HASHJMP mode,pc,handle */ |
| 485 | { DRCUML_OP_JMP, op_jmp }, /* JMP imm[,c] */ |
| 486 | { DRCUML_OP_EXH, op_exh }, /* EXH handle,param[,c] */ |
| 487 | { DRCUML_OP_CALLH, op_callh }, /* CALLH handle[,c] */ |
| 488 | { DRCUML_OP_RET, op_ret }, /* RET [c] */ |
| 489 | { DRCUML_OP_CALLC, op_callc }, /* CALLC func,ptr[,c] */ |
| 490 | { DRCUML_OP_RECOVER, op_recover }, /* RECOVER dst,mapvar */ |
| 491 | |
| 492 | /* Internal Register Operations */ |
| 493 | { DRCUML_OP_SETFMOD, op_setfmod }, /* SETFMOD src */ |
| 494 | { DRCUML_OP_GETFMOD, op_getfmod }, /* GETFMOD dst */ |
| 495 | { DRCUML_OP_GETEXP, op_getexp }, /* GETEXP dst,index */ |
| 496 | |
| 497 | /* Integer Operations */ |
| 498 | { DRCUML_OP_LOAD1U, op_load1u }, /* LOAD1U dst,base,index */ |
| 499 | { DRCUML_OP_LOAD1S, op_load1s }, /* LOAD1S dst,base,index */ |
| 500 | { DRCUML_OP_LOAD2U, op_load2u }, /* LOAD2U dst,base,index */ |
| 501 | { DRCUML_OP_LOAD2S, op_load2s }, /* LOAD2S dst,base,index */ |
| 502 | { DRCUML_OP_LOAD4U, op_load4u }, /* LOAD4U dst,base,index */ |
| 503 | { DRCUML_OP_LOAD4S, op_load4s }, /* LOAD4S dst,base,index */ |
| 504 | { DRCUML_OP_LOAD8U, op_load8u }, /* LOAD8U dst,base,index */ |
| 505 | { DRCUML_OP_STORE1, op_store1 }, /* STORE1 base,index,src */ |
| 506 | { DRCUML_OP_STORE2, op_store2 }, /* STORE2 base,index,src */ |
| 507 | { DRCUML_OP_STORE4, op_store4 }, /* STORE4 base,index,src */ |
| 508 | { DRCUML_OP_STORE8, op_store8 }, /* STORE8 base,index,src */ |
| 509 | { DRCUML_OP_READ1U, op_read1u }, /* READ1U dst,space,src1 */ |
| 510 | { DRCUML_OP_READ1S, op_read1s }, /* READ1S dst,space,src1 */ |
| 511 | { DRCUML_OP_READ2U, op_read2u }, /* READ2U dst,space,src1 */ |
| 512 | { DRCUML_OP_READ2S, op_read2s }, /* READ2S dst,space,src1 */ |
| 513 | { DRCUML_OP_READ2M, op_read2m }, /* READ2M dst,space,src1,mask */ |
| 514 | { DRCUML_OP_READ4U, op_read4u }, /* READ4U dst,space,src1 */ |
| 515 | { DRCUML_OP_READ4S, op_read4s }, /* READ4S dst,space,src1 */ |
| 516 | { DRCUML_OP_READ4M, op_read4m }, /* READ4M dst,space,src1,mask */ |
| 517 | { DRCUML_OP_READ8U, op_read8u }, /* READ8U dst,space,src1 */ |
| 518 | { DRCUML_OP_READ8M, op_read8m }, /* READ8M dst,space,src1,mask */ |
| 519 | { DRCUML_OP_WRITE1, op_write1 }, /* WRITE1 space,dst,src1 */ |
| 520 | { DRCUML_OP_WRITE2, op_write2 }, /* WRITE2 space,dst,src1 */ |
| 521 | { DRCUML_OP_WRIT2M, op_writ2m }, /* WRIT2M space,dst,src1 */ |
| 522 | { DRCUML_OP_WRITE4, op_write4 }, /* WRITE4 space,dst,src1 */ |
| 523 | { DRCUML_OP_WRIT4M, op_writ4m }, /* WRIT4M space,dst,mask,src1 */ |
| 524 | { DRCUML_OP_WRITE8, op_write8 }, /* WRITE8 space,dst,src1 */ |
| 525 | { DRCUML_OP_WRIT8M, op_writ8m }, /* WRIT8M space,dst,mask,src1 */ |
| 526 | { DRCUML_OP_FLAGS, op_flags }, /* FLAGS dst,mask,table */ |
| 527 | { DRCUML_OP_MOV, op_mov }, /* MOV dst,src[,c] */ |
| 528 | { DRCUML_OP_ZEXT1, op_zext1 }, /* ZEXT1 dst,src */ |
| 529 | { DRCUML_OP_ZEXT2, op_zext2 }, /* ZEXT2 dst,src */ |
| 530 | { DRCUML_OP_ZEXT4, op_zext4 }, /* ZEXT4 dst,src */ |
| 531 | { DRCUML_OP_SEXT1, op_sext1 }, /* SEXT1 dst,src */ |
| 532 | { DRCUML_OP_SEXT2, op_sext2 }, /* SEXT2 dst,src */ |
| 533 | { DRCUML_OP_SEXT4, op_sext4 }, /* SEXT4 dst,src */ |
| 534 | { DRCUML_OP_ADD, op_add }, /* ADD dst,src1,src2[,f] */ |
| 535 | { DRCUML_OP_ADDC, op_addc }, /* ADDC dst,src1,src2[,f] */ |
| 536 | { DRCUML_OP_SUB, op_sub }, /* SUB dst,src1,src2[,f] */ |
| 537 | { DRCUML_OP_SUBB, op_subc }, /* SUBB dst,src1,src2[,f] */ |
| 538 | { DRCUML_OP_CMP, op_cmp }, /* CMP src1,src2[,f] */ |
| 539 | { DRCUML_OP_MULU, op_mulu }, /* MULU dst,edst,src1,src2[,f] */ |
| 540 | { DRCUML_OP_MULS, op_muls }, /* MULS dst,edst,src1,src2[,f] */ |
| 541 | { DRCUML_OP_DIVU, op_divu }, /* DIVU dst,edst,src1,src2[,f] */ |
| 542 | { DRCUML_OP_DIVS, op_divs }, /* DIVS dst,edst,src1,src2[,f] */ |
| 543 | { DRCUML_OP_AND, op_and }, /* AND dst,src1,src2[,f] */ |
| 544 | { DRCUML_OP_TEST, op_test }, /* TEST src1,src2[,f] */ |
| 545 | { DRCUML_OP_OR, op_or }, /* OR dst,src1,src2[,f] */ |
| 546 | { DRCUML_OP_XOR, op_xor }, /* XOR dst,src1,src2[,f] */ |
| 547 | { DRCUML_OP_SHL, op_shl }, /* SHL dst,src,count[,f] */ |
| 548 | { DRCUML_OP_SHR, op_shr }, /* SHR dst,src,count[,f] */ |
| 549 | { DRCUML_OP_SAR, op_sar }, /* SAR dst,src,count[,f] */ |
| 550 | { DRCUML_OP_ROL, op_rol }, /* ROL dst,src,count[,f] */ |
| 551 | { DRCUML_OP_ROLC, op_rolc }, /* ROLC dst,src,count[,f] */ |
| 552 | { DRCUML_OP_ROR, op_ror }, /* ROR dst,src,count[,f] */ |
| 553 | { DRCUML_OP_RORC, op_rorc }, /* RORC dst,src,count[,f] */ |
| 554 | |
| 555 | /* Floating Point Operations */ |
| 556 | { DRCUML_OP_FLOAD, op_fload }, /* FLOAD dst,base,index */ |
| 557 | { DRCUML_OP_FSTORE, op_fstore }, /* FSTORE base,index,src */ |
| 558 | { DRCUML_OP_FREAD, op_fread }, /* FREAD dst,space,src1 */ |
| 559 | { DRCUML_OP_FWRITE, op_fwrite }, /* FWRITE space,dst,src1 */ |
| 560 | { DRCUML_OP_FMOV, op_fmov }, /* FMOV dst,src1[,c] */ |
| 561 | { DRCUML_OP_FTOI4, op_ftoi4 }, /* FTOI4 dst,src1 */ |
| 562 | { DRCUML_OP_FTOI4T, op_ftoi4t }, /* FTOI4T dst,src1 */ |
| 563 | { DRCUML_OP_FTOI4R, op_ftoi4r }, /* FTOI4R dst,src1 */ |
| 564 | { DRCUML_OP_FTOI4F, op_ftoi4f }, /* FTOI4F dst,src1 */ |
| 565 | { DRCUML_OP_FTOI4C, op_ftoi4c }, /* FTOI4C dst,src1 */ |
| 566 | { DRCUML_OP_FTOI8, op_ftoi8 }, /* FTOI8 dst,src1 */ |
| 567 | { DRCUML_OP_FTOI8T, op_ftoi8t }, /* FTOI8T dst,src1 */ |
| 568 | { DRCUML_OP_FTOI8R, op_ftoi8r }, /* FTOI8R dst,src1 */ |
| 569 | { DRCUML_OP_FTOI8F, op_ftoi8f }, /* FTOI8F dst,src1 */ |
| 570 | { DRCUML_OP_FTOI8C, op_ftoi8c }, /* FTOI8C dst,src1 */ |
| 571 | { DRCUML_OP_FFRFS, op_ffrfs }, /* FFRFS dst,src1 */ |
| 572 | { DRCUML_OP_FFRFD, op_ffrfd }, /* FFRFD dst,src1 */ |
| 573 | { DRCUML_OP_FFRI4, op_ffri4 }, /* FFRI4 dst,src1 */ |
| 574 | { DRCUML_OP_FFRI8, op_ffri8 }, /* FFRI8 dst,src1 */ |
| 575 | { DRCUML_OP_FADD, op_fadd }, /* FADD dst,src1,src2 */ |
| 576 | { DRCUML_OP_FSUB, op_fsub }, /* FSUB dst,src1,src2 */ |
| 577 | { DRCUML_OP_FCMP, op_fcmp }, /* FCMP src1,src2 */ |
| 578 | { DRCUML_OP_FMUL, op_fmul }, /* FMUL dst,src1,src2 */ |
| 579 | { DRCUML_OP_FDIV, op_fdiv }, /* FDIV dst,src1,src2 */ |
| 580 | { DRCUML_OP_FNEG, op_fneg }, /* FNEG dst,src1 */ |
| 581 | { DRCUML_OP_FABS, op_fabs }, /* FABS dst,src1 */ |
| 582 | { DRCUML_OP_FSQRT, op_fsqrt }, /* FSQRT dst,src1 */ |
| 583 | { DRCUML_OP_FRECIP, op_frecip }, /* FRECIP dst,src1 */ |
| 584 | { DRCUML_OP_FRSQRT, op_frsqrt } /* FRSQRT dst,src1 */ |
| 585 | }; |
| 586 | |
| 587 | |
| 588 | |
| 589 | /*************************************************************************** |
| 590 | INLINE FUNCTIONS |
| 591 | ***************************************************************************/ |
| 592 | |
| 593 | /*------------------------------------------------- |
| 594 | param_select_register - select a register |
| 595 | to use, avoiding conflicts with the optional |
| 596 | checkparam |
| 597 | -------------------------------------------------*/ |
| 598 | |
| 599 | INLINE int param_select_register(int defreg, const drcuml_parameter *param, const drcuml_parameter *checkparam) |
| 600 | { |
| 601 | if ((param->type == DRCUML_PTYPE_INT_REGISTER || param->type == DRCUML_PTYPE_FLOAT_REGISTER) && (checkparam == NULL || param->type != checkparam->type || param->value != checkparam->value)) |
| 602 | return param->value; |
| 603 | return defreg; |
| 604 | } |
| 605 | |
| 606 | |
| 607 | /*------------------------------------------------- |
| 608 | offset_from_rbp - return the verified offset |
| 609 | from rbp |
| 610 | -------------------------------------------------*/ |
| 611 | |
| 612 | INLINE INT32 offset_from_rbp(drcbe_state *drcbe, FPTR ptr) |
| 613 | { |
| 614 | INT64 delta = (UINT8 *)ptr - drcbe->rbpvalue; |
| 615 | assert_always((INT32)delta == delta, "offset_from_rbp: delta out of range"); |
| 616 | return (INT32)delta; |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /*------------------------------------------------- |
| 621 | short_immediate - true if the given immediate |
| 622 | fits as a signed 32-bit value |
| 623 | -------------------------------------------------*/ |
| 624 | |
| 625 | INLINE int short_immediate(INT64 immediate) |
| 626 | { |
| 627 | return (INT32)immediate == immediate; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /*------------------------------------------------- |
| 632 | get_base_register_and_offset - determine right |
| 633 | base register and offset to access the given |
| 634 | target address |
| 635 | -------------------------------------------------*/ |
| 636 | |
| 637 | INLINE int get_base_register_and_offset(drcbe_state *drcbe, x86code **dst, FPTR target, UINT8 reg, INT32 *offset) |
| 638 | { |
| 639 | INT64 delta = (UINT8 *)target - drcbe->rbpvalue; |
| 640 | if (short_immediate(delta)) |
| 641 | { |
| 642 | *offset = delta; |
| 643 | return REG_RBP; |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | *offset = 0; |
| 648 | emit_mov_r64_imm(dst, reg, target); // mov reg,target |
| 649 | return reg; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /*------------------------------------------------- |
| 655 | emit_smart_call_r64 - generate a call either |
| 656 | directly or via a call through pointer |
| 657 | -------------------------------------------------*/ |
| 658 | |
| 659 | INLINE void emit_smart_call_r64(drcbe_state *drcbe, x86code **dst, x86code *target, UINT8 reg) |
| 660 | { |
| 661 | INT64 delta = target - (*dst + 5); |
| 662 | if (short_immediate(delta)) |
| 663 | emit_call(dst, target); // call target |
| 664 | else |
| 665 | { |
| 666 | emit_mov_r64_imm(dst, reg, (FPTR)target); // mov reg,target |
| 667 | emit_call_r64(dst, reg); // call reg |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | |
| 672 | /*------------------------------------------------- |
| 673 | emit_smart_call_m64 - generate a call either |
| 674 | directly or via a call through pointer |
| 675 | -------------------------------------------------*/ |
| 676 | |
| 677 | INLINE void emit_smart_call_m64(drcbe_state *drcbe, x86code **dst, x86code **target) |
| 678 | { |
| 679 | INT64 delta = *target - (*dst + 5); |
| 680 | if (short_immediate(delta)) |
| 681 | emit_call(dst, *target); // call *target |
| 682 | else |
| 683 | emit_call_m64(dst, MABS(drcbe, target)); // call [target] |
| 684 | } |
| 685 | |
| 686 | |
| 687 | |
| 688 | /*************************************************************************** |
| 689 | BACKEND CALLBACKS |
| 690 | ***************************************************************************/ |
| 691 | |
| 692 | /*------------------------------------------------- |
| 693 | drcbex64_alloc - allocate back-end-specific |
| 694 | state |
| 695 | -------------------------------------------------*/ |
| 696 | |
| 697 | static drcbe_state *drcbex64_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits) |
| 698 | { |
| 699 | /* SSE control register mapping */ |
| 700 | static const UINT32 sse_control[4] = |
| 701 | { |
| 702 | 0xffc0, /* DRCUML_FMOD_TRUNC */ |
| 703 | 0x9fc0, /* DRCUML_FMOD_ROUND */ |
| 704 | 0xdfc0, /* DRCUML_FMOD_CEIL */ |
| 705 | 0xbfc0 /* DRCUML_FMOD_FLOOR */ |
| 706 | }; |
| 707 | drcbe_state *drcbe; |
| 708 | int opnum, entry; |
| 709 | |
| 710 | /* allocate space in the cache for our state */ |
| 711 | drcbe = drccache_memory_alloc_near(cache, sizeof(*drcbe)); |
| 712 | if (drcbe == NULL) |
| 713 | return NULL; |
| 714 | memset(drcbe, 0, sizeof(*drcbe)); |
| 715 | |
| 716 | /* remember our pointers */ |
| 717 | drcbe->drcuml = drcuml; |
| 718 | drcbe->cache = cache; |
| 719 | drcbe->rbpvalue = drccache_near(cache) + 0x80; |
| 720 | |
| 721 | /* build up necessary arrays */ |
| 722 | memcpy(drcbe->ssecontrol, sse_control, sizeof(drcbe->ssecontrol)); |
| 723 | drcbe->absmask32 = drccache_memory_alloc_near(cache, 16*2 + 15); |
| 724 | drcbe->absmask32 = (void *)(((FPTR)drcbe->absmask32 + 15) & ~15); |
| 725 | drcbe->absmask32[0] = drcbe->absmask32[1] = drcbe->absmask32[2] = drcbe->absmask32[3] = 0x7fffffff; |
| 726 | drcbe->absmask64 = (UINT64 *)&drcbe->absmask32[4]; |
| 727 | drcbe->absmask64[0] = drcbe->absmask64[1] = U64(0x7fffffffffffffff); |
| 728 | |
| 729 | /* get pointers to C functions we need to call */ |
| 730 | #ifdef ENABLE_DEBUGGER |
| 731 | drcbe->mame_debug_hook = (x86code *)mame_debug_hook; |
| 732 | #endif |
| 733 | #if LOG_HASHJMPS |
| 734 | drcbe->debug_log_hashjmp = (x86code *)debug_log_hashjmp; |
| 735 | #endif |
| 736 | drcbe->drcmap_get_value = (x86code *)drcmap_get_value; |
| 737 | |
| 738 | /* allocate hash tables */ |
| 739 | drcbe->hash = drchash_alloc(cache, modes, addrbits, ignorebits); |
| 740 | if (drcbe->hash == NULL) |
| 741 | return NULL; |
| 742 | |
| 743 | /* allocate code map */ |
| 744 | drcbe->map = drcmap_alloc(cache, 0); |
| 745 | if (drcbe->map == NULL) |
| 746 | return NULL; |
| 747 | |
| 748 | /* allocate a label tracker */ |
| 749 | drcbe->labels = drclabel_list_alloc(cache); |
| 750 | if (drcbe->labels == NULL) |
| 751 | return NULL; |
| 752 | |
| 753 | /* build the opcode table (static but it doesn't hurt to regenerate it) */ |
| 754 | for (opnum = 0; opnum < ARRAY_LENGTH(opcode_table_source); opnum++) |
| 755 | opcode_table[opcode_table_source[opnum].opcode] = opcode_table_source[opnum].func; |
| 756 | |
| 757 | /* build the flags map */ |
| 758 | for (entry = 0; entry < ARRAY_LENGTH(drcbe->flagsmap); entry++) |
| 759 | { |
| 760 | UINT8 flags = 0; |
| 761 | if (entry & 0x001) flags |= DRCUML_FLAG_C; |
| 762 | if (entry & 0x004) flags |= DRCUML_FLAG_U; |
| 763 | if (entry & 0x040) flags |= DRCUML_FLAG_Z; |
| 764 | if (entry & 0x080) flags |= DRCUML_FLAG_S; |
| 765 | if (entry & 0x800) flags |= DRCUML_FLAG_V; |
| 766 | drcbe->flagsmap[entry] = flags; |
| 767 | } |
| 768 | |
| 769 | /* create the log */ |
| 770 | if (flags & DRCUML_OPTION_LOG_NATIVE) |
| 771 | drcbe->log = x86log_create_context("drcbex64.asm"); |
| 772 | |
| 773 | return drcbe; |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /*------------------------------------------------- |
| 778 | drcbex64_free - free back-end specific state |
| 779 | -------------------------------------------------*/ |
| 780 | |
| 781 | static void drcbex64_free(drcbe_state *drcbe) |
| 782 | { |
| 783 | /* free the log context */ |
| 784 | if (drcbe->log != NULL) |
| 785 | x86log_free_context(drcbe->log); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | /*------------------------------------------------- |
| 790 | drcbex64_reset - reset back-end specific state |
| 791 | -------------------------------------------------*/ |
| 792 | |
| 793 | static void drcbex64_reset(drcbe_state *drcbe) |
| 794 | { |
| 795 | x86code **dst; |
| 796 | int spacenum; |
| 797 | |
| 798 | /* output a note to the log */ |
| 799 | if (drcbe->log != NULL) |
| 800 | x86log_printf(drcbe->log, "\n\n===========\nCACHE RESET\n===========\n\n"); |
| 801 | |
| 802 | /* fetch the accessors now that things are ready to go */ |
| 803 | for (spacenum = 0; spacenum < ADDRESS_SPACES; spacenum++) |
| 804 | if (active_address_space[spacenum].accessors != NULL) |
| 805 | drcbe->accessors[spacenum] = *active_address_space[spacenum].accessors; |
| 806 | |
| 807 | /* generate a little bit of glue code to set up the environment */ |
| 808 | dst = (x86code **)drccache_begin_codegen(drcbe->cache, 500); |
| 809 | if (dst == NULL) |
| 810 | fatalerror("Out of cache space after a reset!"); |
| 811 | |
| 812 | /* generate an entry point */ |
| 813 | drcbe->entry = (x86_entry_point_func)*dst; |
| 814 | emit_push_r64(dst, REG_RBX); // push rbx |
| 815 | emit_push_r64(dst, REG_RSI); // push rsi |
| 816 | emit_push_r64(dst, REG_RDI); // push rdi |
| 817 | emit_push_r64(dst, REG_RBP); // push rbp |
| 818 | emit_push_r64(dst, REG_R12); // push r12 |
| 819 | emit_push_r64(dst, REG_R13); // push r13 |
| 820 | emit_push_r64(dst, REG_R14); // push r14 |
| 821 | emit_push_r64(dst, REG_R15); // push r15 |
| 822 | emit_mov_r64_r64(dst, REG_RBP, REG_PARAM1); // mov rbp,param1 |
| 823 | emit_sub_r64_imm(dst, REG_RSP, 32); // sub rsp,32 |
| 824 | emit_mov_m64_r64(dst, MABS(drcbe, &drcbe->hashstacksave), REG_RSP); // mov [hashstacksave],rsp |
| 825 | emit_sub_r64_imm(dst, REG_RSP, 8); // sub rsp,8 |
| 826 | emit_mov_m64_r64(dst, MABS(drcbe, &drcbe->stacksave), REG_RSP); // mov [stacksave],rsp |
| 827 | emit_stmxcsr_m32(dst, MABS(drcbe, &drcbe->ssemode)); // stmxcsr [ssemode] |
| 828 | emit_jmp_r64(dst, REG_PARAM2); // jmp param2 |
| 829 | if (drcbe->log != NULL) |
| 830 | x86log_disasm_code_range(drcbe->log, "entry_point", (x86code *)drcbe->entry, *dst); |
| 831 | |
| 832 | /* generate an exit point */ |
| 833 | drcbe->exit = *dst; |
| 834 | emit_ldmxcsr_m32(dst, MABS(drcbe, &drcbe->ssemode)); // ldmxcsr [ssemode] |
| 835 | emit_mov_r64_m64(dst, REG_RSP, MABS(drcbe, &drcbe->hashstacksave)); // mov rsp,[hashstacksave] |
| 836 | emit_add_r64_imm(dst, REG_RSP, 32); // add rsp,32 |
| 837 | emit_pop_r64(dst, REG_R15); // pop r15 |
| 838 | emit_pop_r64(dst, REG_R14); // pop r14 |
| 839 | emit_pop_r64(dst, REG_R13); // pop r13 |
| 840 | emit_pop_r64(dst, REG_R12); // pop r12 |
| 841 | emit_pop_r64(dst, REG_RBP); // pop rbp |
| 842 | emit_pop_r64(dst, REG_RDI); // pop rdi |
| 843 | emit_pop_r64(dst, REG_RSI); // pop rsi |
| 844 | emit_pop_r64(dst, REG_RBX); // pop rbx |
| 845 | emit_ret(dst); // ret |
| 846 | if (drcbe->log != NULL) |
| 847 | x86log_disasm_code_range(drcbe->log, "exit_point", drcbe->exit, *dst); |
| 848 | |
| 849 | /* generate a no code point */ |
| 850 | drcbe->nocode = *dst; |
| 851 | emit_ret(dst); // ret |
| 852 | if (drcbe->log != NULL) |
| 853 | x86log_disasm_code_range(drcbe->log, "nocode", drcbe->nocode, *dst); |
| 854 | |
| 855 | /* finish up codegen */ |
| 856 | drccache_end_codegen(drcbe->cache); |
| 857 | |
| 858 | /* reset our hash tables */ |
| 859 | drchash_reset(drcbe->hash); |
| 860 | drchash_set_default_codeptr(drcbe->hash, drcbe->nocode); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | /*------------------------------------------------- |
| 865 | drcbex64_generate - generate code |
| 866 | -------------------------------------------------*/ |
| 867 | |
| 868 | static void drcbex64_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst) |
| 869 | { |
| 870 | const char *blockname = NULL; |
| 871 | char blockbuffer[100]; |
| 872 | drccodeptr *cachetop; |
| 873 | x86code *base; |
| 874 | x86code *dst; |
| 875 | int inum; |
| 876 | |
| 877 | /* tell all of our utility objects that a block is beginning */ |
| 878 | drchash_block_begin(drcbe->hash, block, instlist, numinst); |
| 879 | drclabel_block_begin(drcbe->labels, block); |
| 880 | drcmap_block_begin(drcbe->map, block); |
| 881 | |
| 882 | /* begin codegen; fail if we can't */ |
| 883 | cachetop = drccache_begin_codegen(drcbe->cache, numinst * 8 * 4); |
| 884 | if (cachetop == NULL) |
| 885 | drcuml_block_abort(block); |
| 886 | |
| 887 | /* compute the base by aligning the cache top to a cache line (assumed to be 64 bytes) */ |
| 888 | base = (x86code *)(((FPTR)*cachetop + 63) & ~63); |
| 889 | dst = base; |
| 890 | |
| 891 | /* generate code */ |
| 892 | for (inum = 0; inum < numinst; inum++) |
| 893 | { |
| 894 | const drcuml_instruction *inst = &instlist[inum]; |
| 895 | assert(inst->opcode < ARRAY_LENGTH(opcode_table)); |
| 896 | |
| 897 | /* add a comment */ |
| 898 | if (drcbe->log != NULL) |
| 899 | { |
| 900 | char dasm[100]; |
| 901 | drcuml_disasm(inst, dasm); |
| 902 | x86log_add_comment(drcbe->log, dst, "%s", dasm); |
| 903 | } |
| 904 | |
| 905 | /* extract a blockname */ |
| 906 | if (blockname == NULL) |
| 907 | { |
| 908 | if (inst->opcode == DRCUML_OP_HANDLE) |
| 909 | blockname = drcuml_handle_name((drcuml_codehandle *)(FPTR)inst->param[0].value); |
| 910 | else if (inst->opcode == DRCUML_OP_HASH) |
| 911 | { |
| 912 | sprintf(blockbuffer, "Code: mode=%d PC=%08X", (UINT32)inst->param[0].value, (offs_t)inst->param[1].value); |
| 913 | blockname = blockbuffer; |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* generate code */ |
| 918 | dst = (*opcode_table[inst->opcode])(drcbe, dst, inst); |
| 919 | } |
| 920 | |
| 921 | /* complete codegen */ |
| 922 | *cachetop = (drccodeptr)dst; |
| 923 | drccache_end_codegen(drcbe->cache); |
| 924 | |
| 925 | /* log it */ |
| 926 | if (drcbe->log != NULL) |
| 927 | x86log_disasm_code_range(drcbe->log, (blockname == NULL) ? "Unknown block" : blockname, base, drccache_top(drcbe->cache)); |
| 928 | |
| 929 | /* tell all of our utility objects that the block is finished */ |
| 930 | drchash_block_end(drcbe->hash, block); |
| 931 | drclabel_block_end(drcbe->labels, block); |
| 932 | drcmap_block_end(drcbe->map, block); |
| 933 | } |
| 934 | |
| 935 | |
| 936 | /*------------------------------------------------- |
| 937 | drcbex64_hash_exists - return true if the |
| 938 | given mode/pc exists in the hash table |
| 939 | -------------------------------------------------*/ |
| 940 | |
| 941 | static int drcbex64_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc) |
| 942 | { |
| 943 | return drchash_code_exists(drcbe->hash, mode, pc); |
| 944 | } |
| 945 | |
| 946 | |
| 947 | /*------------------------------------------------- |
| 948 | drcbec_execute - execute a block of code |
| 949 | registered at the given mode/pc |
| 950 | -------------------------------------------------*/ |
| 951 | |
| 952 | static int drcbex64_execute(drcbe_state *drcbe, drcuml_codehandle *entry) |
| 953 | { |
| 954 | /* call our entry point which will jump to the destination */ |
| 955 | return (*drcbe->entry)(drcbe->rbpvalue, (x86code *)drcuml_handle_codeptr(entry)); |
| 956 | } |
| 957 | |
| 958 | |
| 959 | |
| 960 | /*************************************************************************** |
| 961 | COMPILE HELPERS |
| 962 | ***************************************************************************/ |
| 963 | |
| 964 | /*------------------------------------------------- |
| 965 | param_normalize - convert a full parameter |
| 966 | into a reduced set |
| 967 | -------------------------------------------------*/ |
| 968 | |
| 969 | static void param_normalize(drcbe_state *drcbe, const drcuml_parameter *src, drcuml_parameter *dest, UINT32 allowed) |
| 970 | { |
| 971 | int regnum; |
| 972 | |
| 973 | switch (src->type) |
| 974 | { |
| 975 | /* immediates pass through */ |
| 976 | case DRCUML_PTYPE_IMMEDIATE: |
| 977 | assert(allowed & PTYPE_I); |
| 978 | dest->type = DRCUML_PTYPE_IMMEDIATE; |
| 979 | dest->value = src->value; |
| 980 | break; |
| 981 | |
| 982 | /* mapvars are converted to immediates with their current value */ |
| 983 | case DRCUML_PTYPE_MAPVAR: |
| 984 | assert(allowed & PTYPE_I); |
| 985 | dest->type = DRCUML_PTYPE_IMMEDIATE; |
| 986 | dest->value = drcmap_get_last_value(drcbe->map, src->value); |
| 987 | break; |
| 988 | |
| 989 | /* memory passes through */ |
| 990 | case DRCUML_PTYPE_MEMORY: |
| 991 | assert(allowed & PTYPE_M); |
| 992 | dest->type = DRCUML_PTYPE_MEMORY; |
| 993 | dest->value = (FPTR)src->value; |
| 994 | break; |
| 995 | |
| 996 | /* if a register maps to a register, keep it as a register; otherwise map it to memory */ |
| 997 | case DRCUML_PTYPE_INT_REGISTER: |
| 998 | assert(allowed & PTYPE_R); |
| 999 | assert(allowed & PTYPE_M); |
| 1000 | regnum = int_register_map[src->value - DRCUML_REG_I0]; |
| 1001 | if (regnum != 0) |
| 1002 | { |
| 1003 | dest->type = DRCUML_PTYPE_INT_REGISTER; |
| 1004 | dest->value = regnum; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | dest->type = DRCUML_PTYPE_MEMORY; |
| 1009 | dest->value = (FPTR)&drcbe->state.r[src->value - DRCUML_REG_I0]; |
| 1010 | } |
| 1011 | break; |
| 1012 | |
| 1013 | /* if a register maps to a register, keep it as a register; otherwise map it to memory */ |
| 1014 | case DRCUML_PTYPE_FLOAT_REGISTER: |
| 1015 | assert(allowed & PTYPE_F); |
| 1016 | assert(allowed & PTYPE_M); |
| 1017 | regnum = float_register_map[src->value - DRCUML_REG_F0]; |
| 1018 | if (regnum != 0) |
| 1019 | { |
| 1020 | dest->type = DRCUML_PTYPE_FLOAT_REGISTER; |
| 1021 | dest->value = regnum; |
| 1022 | } |
| 1023 | else |
| 1024 | { |
| 1025 | dest->type = DRCUML_PTYPE_MEMORY; |
| 1026 | dest->value = (FPTR)&drcbe->state.f[src->value - DRCUML_REG_F0]; |
| 1027 | } |
| 1028 | break; |
| 1029 | |
| 1030 | /* everything else is unexpected */ |
| 1031 | default: |
| 1032 | fatalerror("Unexpected parameter type"); |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | /*------------------------------------------------- |
| 1039 | param_normalize_1 - normalize a single |
| 1040 | parameter instruction |
| 1041 | -------------------------------------------------*/ |
| 1042 | |
| 1043 | static void param_normalize_1(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0) |
| 1044 | { |
| 1045 | assert(inst->numparams == 1); |
| 1046 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | /*------------------------------------------------- |
| 1051 | param_normalize_2 - normalize a 2 |
| 1052 | parameter instruction |
| 1053 | -------------------------------------------------*/ |
| 1054 | |
| 1055 | static void param_normalize_2(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1) |
| 1056 | { |
| 1057 | assert(inst->numparams == 2); |
| 1058 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 1059 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 1060 | } |
| 1061 | |
| 1062 | |
| 1063 | /*------------------------------------------------- |
| 1064 | param_normalize_2_commutative - normalize a 2 |
| 1065 | parameter instruction, shuffling the |
| 1066 | parameters on the assumption that the two |
| 1067 | parameters can be swapped |
| 1068 | -------------------------------------------------*/ |
| 1069 | |
| 1070 | static void param_normalize_2_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1) |
| 1071 | { |
| 1072 | param_normalize_2(drcbe, inst, dest0, allowed0, dest1, allowed1); |
| 1073 | |
| 1074 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 1075 | if (dest0->type == DRCUML_PTYPE_MEMORY) |
| 1076 | { |
| 1077 | drcuml_parameter temp = *dest0; |
| 1078 | *dest0 = *dest1; |
| 1079 | *dest1 = temp; |
| 1080 | } |
| 1081 | |
| 1082 | /* if the inner parameter is an immediate, push it to the outer */ |
| 1083 | if (dest0->type == DRCUML_PTYPE_IMMEDIATE) |
| 1084 | { |
| 1085 | drcuml_parameter temp = *dest0; |
| 1086 | *dest0 = *dest1; |
| 1087 | *dest1 = temp; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /*------------------------------------------------- |
| 1093 | param_normalize_3 - normalize a 3 |
| 1094 | parameter instruction |
| 1095 | -------------------------------------------------*/ |
| 1096 | |
| 1097 | static void param_normalize_3(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2) |
| 1098 | { |
| 1099 | assert(inst->numparams == 3); |
| 1100 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 1101 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 1102 | param_normalize(drcbe, &inst->param[2], dest2, allowed2); |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | /*------------------------------------------------- |
| 1107 | param_normalize_3_commutative - normalize a 3 |
| 1108 | parameter instruction, shuffling the |
| 1109 | parameters on the assumption that the last |
| 1110 | 2 can be swapped |
| 1111 | -------------------------------------------------*/ |
| 1112 | |
| 1113 | static void param_normalize_3_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2) |
| 1114 | { |
| 1115 | param_normalize_3(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2); |
| 1116 | |
| 1117 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 1118 | if (dest1->type == DRCUML_PTYPE_MEMORY) |
| 1119 | { |
| 1120 | drcuml_parameter temp = *dest1; |
| 1121 | *dest1 = *dest2; |
| 1122 | *dest2 = temp; |
| 1123 | } |
| 1124 | |
| 1125 | /* if the inner parameter is an immediate, push it to the outer */ |
| 1126 | if (dest1->type == DRCUML_PTYPE_IMMEDIATE) |
| 1127 | { |
| 1128 | drcuml_parameter temp = *dest1; |
| 1129 | *dest1 = *dest2; |
| 1130 | *dest2 = temp; |
| 1131 | } |
| 1132 | |
| 1133 | /* if the destination and outer parameters are equal, move the outer to the inner */ |
| 1134 | if (dest0->type == dest2->type && dest0->value == dest2->value && dest0->type != DRCUML_PTYPE_IMMEDIATE) |
| 1135 | { |
| 1136 | drcuml_parameter temp = *dest1; |
| 1137 | *dest1 = *dest2; |
| 1138 | *dest2 = temp; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | /*------------------------------------------------- |
| 1144 | param_normalize_4 - normalize a 4 |
| 1145 | parameter instruction |
| 1146 | -------------------------------------------------*/ |
| 1147 | |
| 1148 | static void param_normalize_4(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3) |
| 1149 | { |
| 1150 | assert(inst->numparams == 4); |
| 1151 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 1152 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 1153 | param_normalize(drcbe, &inst->param[2], dest2, allowed2); |
| 1154 | param_normalize(drcbe, &inst->param[3], dest3, allowed3); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /*------------------------------------------------- |
| 1159 | param_normalize_4_commutative - normalize a 4 |
| 1160 | parameter instruction, shuffling the |
| 1161 | parameters on the assumption that the last |
| 1162 | 2 can be swapped |
| 1163 | -------------------------------------------------*/ |
| 1164 | |
| 1165 | static void param_normalize_4_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3) |
| 1166 | { |
| 1167 | param_normalize_4(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2, dest3, allowed3); |
| 1168 | |
| 1169 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 1170 | if (dest2->type == DRCUML_PTYPE_MEMORY) |
| 1171 | { |
| 1172 | drcuml_parameter temp = *dest2; |
| 1173 | *dest2 = *dest3; |
| 1174 | *dest3 = temp; |
| 1175 | } |
| 1176 | |
| 1177 | /* if the inner parameter is an immediate, push it to the outer */ |
| 1178 | if (dest2->type == DRCUML_PTYPE_IMMEDIATE) |
| 1179 | { |
| 1180 | drcuml_parameter temp = *dest2; |
| 1181 | *dest2 = *dest3; |
| 1182 | *dest3 = temp; |
| 1183 | } |
| 1184 | |
| 1185 | /* if the destination and outer parameters are equal, move the outer to the inner */ |
| 1186 | if (dest0->type == dest3->type && dest0->value == dest3->value && dest0->type != DRCUML_PTYPE_IMMEDIATE) |
| 1187 | { |
| 1188 | drcuml_parameter temp = *dest2; |
| 1189 | *dest2 = *dest3; |
| 1190 | *dest3 = temp; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | /*************************************************************************** |
| 1197 | EMITTERS FOR 32-BIT OPERATIONS WITH PARAMETERS |
| 1198 | ***************************************************************************/ |
| 1199 | |
| 1200 | /*------------------------------------------------- |
| 1201 | emit_mov_r32_p32 - move a 32-bit parameter |
| 1202 | into a register |
| 1203 | -------------------------------------------------*/ |
| 1204 | |
| 1205 | static void emit_mov_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param) |
| 1206 | { |
| 1207 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1208 | { |
| 1209 | if (param->value == 0) |
| 1210 | emit_xor_r32_r32(dst, reg, reg); // xor reg,reg |
| 1211 | else |
| 1212 | emit_mov_r32_imm(dst, reg, param->value); // mov reg,param |
| 1213 | } |
| 1214 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1215 | emit_mov_r32_m32(dst, reg, MABS(drcbe, param->value)); // mov reg,[param] |
| 1216 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1217 | { |
| 1218 | if (reg != param->value) |
| 1219 | emit_mov_r32_r32(dst, reg, param->value); // mov reg,param |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | /*------------------------------------------------- |
| 1225 | emit_mov_m32_p32 - move a 32-bit parameter |
| 1226 | into a memory location |
| 1227 | -------------------------------------------------*/ |
| 1228 | |
| 1229 | static void emit_mov_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param) |
| 1230 | { |
| 1231 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1232 | emit_mov_m32_imm(dst, MEMPARAMS, param->value); // mov [mem],param |
| 1233 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1234 | { |
| 1235 | emit_mov_r32_m32(dst, REG_EAX, MABS(drcbe, param->value)); // mov eax,[param] |
| 1236 | emit_mov_m32_r32(dst, MEMPARAMS, REG_EAX); // mov [mem],eax |
| 1237 | } |
| 1238 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1239 | emit_mov_m32_r32(dst, MEMPARAMS, param->value); // mov [mem],param |
| 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | /*------------------------------------------------- |
| 1244 | emit_mov_p32_r32 - move a register into a |
| 1245 | 32-bit parameter |
| 1246 | -------------------------------------------------*/ |
| 1247 | |
| 1248 | static void emit_mov_p32_r32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg) |
| 1249 | { |
| 1250 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 1251 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1252 | emit_mov_m32_r32(dst, MABS(drcbe, param->value), reg); // mov [param],reg |
| 1253 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1254 | { |
| 1255 | if (reg != param->value) |
| 1256 | emit_mov_r32_r32(dst, param->value, reg); // mov param,reg |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | /*------------------------------------------------- |
| 1262 | emit_add_r32_p32 - add operation to a 32-bit |
| 1263 | register from a 32-bit parameter |
| 1264 | -------------------------------------------------*/ |
| 1265 | |
| 1266 | static void emit_add_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1267 | { |
| 1268 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1269 | { |
| 1270 | if (inst->condflags != 0 || param->value != 0) |
| 1271 | emit_add_r32_imm(dst, reg, param->value); // add reg,param |
| 1272 | } |
| 1273 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1274 | emit_add_r32_m32(dst, reg, MABS(drcbe, param->value)); // add reg,[param] |
| 1275 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1276 | emit_add_r32_r32(dst, reg, param->value); // add reg,param |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | /*------------------------------------------------- |
| 1281 | emit_add_m32_p32 - add operation to a 32-bit |
| 1282 | memory location from a 32-bit parameter |
| 1283 | -------------------------------------------------*/ |
| 1284 | |
| 1285 | static void emit_add_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1286 | { |
| 1287 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1288 | { |
| 1289 | if (inst->condflags != 0 || param->value != 0) |
| 1290 | emit_add_m32_imm(dst, MEMPARAMS, param->value); // add [dest],param |
| 1291 | } |
| 1292 | else |
| 1293 | { |
| 1294 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1295 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1296 | emit_add_m32_r32(dst, MEMPARAMS, reg); // add [dest],reg |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | /*------------------------------------------------- |
| 1302 | emit_adc_r32_p32 - adc operation to a 32-bit |
| 1303 | register from a 32-bit parameter |
| 1304 | -------------------------------------------------*/ |
| 1305 | |
| 1306 | static void emit_adc_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1307 | { |
| 1308 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1309 | { |
| 1310 | if (inst->condflags != 0 || param->value != 0) |
| 1311 | emit_adc_r32_imm(dst, reg, param->value); // adc reg,param |
| 1312 | } |
| 1313 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1314 | emit_adc_r32_m32(dst, reg, MABS(drcbe, param->value)); // adc reg,[param] |
| 1315 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1316 | emit_adc_r32_r32(dst, reg, param->value); // adc reg,param |
| 1317 | } |
| 1318 | |
| 1319 | |
| 1320 | /*------------------------------------------------- |
| 1321 | emit_adc_m32_p32 - adc operation to a 32-bit |
| 1322 | memory location from a 32-bit parameter |
| 1323 | -------------------------------------------------*/ |
| 1324 | |
| 1325 | static void emit_adc_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1326 | { |
| 1327 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1328 | { |
| 1329 | if (inst->condflags != 0 || param->value != 0) |
| 1330 | emit_adc_m32_imm(dst, MEMPARAMS, param->value); // adc [dest],param |
| 1331 | } |
| 1332 | else |
| 1333 | { |
| 1334 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1335 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1336 | emit_adc_m32_r32(dst, MEMPARAMS, reg); // adc [dest],reg |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /*------------------------------------------------- |
| 1342 | emit_sub_r32_p32 - sub operation to a 32-bit |
| 1343 | register from a 32-bit parameter |
| 1344 | -------------------------------------------------*/ |
| 1345 | |
| 1346 | static void emit_sub_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1347 | { |
| 1348 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1349 | { |
| 1350 | if (inst->condflags != 0 || param->value != 0) |
| 1351 | emit_sub_r32_imm(dst, reg, param->value); // sub reg,param |
| 1352 | } |
| 1353 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1354 | emit_sub_r32_m32(dst, reg, MABS(drcbe, param->value)); // sub reg,[param] |
| 1355 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1356 | emit_sub_r32_r32(dst, reg, param->value); // sub reg,param |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | /*------------------------------------------------- |
| 1361 | emit_sub_m32_p32 - sub operation to a 32-bit |
| 1362 | memory location from a 32-bit parameter |
| 1363 | -------------------------------------------------*/ |
| 1364 | |
| 1365 | static void emit_sub_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1366 | { |
| 1367 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1368 | { |
| 1369 | if (inst->condflags != 0 || param->value != 0) |
| 1370 | emit_sub_m32_imm(dst, MEMPARAMS, param->value); // sub [dest],param |
| 1371 | } |
| 1372 | else |
| 1373 | { |
| 1374 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1375 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1376 | emit_sub_m32_r32(dst, MEMPARAMS, reg); // sub [dest],reg |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | /*------------------------------------------------- |
| 1382 | emit_sbb_r32_p32 - sbb operation to a 32-bit |
| 1383 | register from a 32-bit parameter |
| 1384 | -------------------------------------------------*/ |
| 1385 | |
| 1386 | static void emit_sbb_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1387 | { |
| 1388 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1389 | { |
| 1390 | if (inst->condflags != 0 || param->value != 0) |
| 1391 | emit_sbb_r32_imm(dst, reg, param->value); // sbb reg,param |
| 1392 | } |
| 1393 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1394 | emit_sbb_r32_m32(dst, reg, MABS(drcbe, param->value)); // sbb reg,[param] |
| 1395 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1396 | emit_sbb_r32_r32(dst, reg, param->value); // sbb reg,param |
| 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | /*------------------------------------------------- |
| 1401 | emit_sbb_m32_p32 - sbb operation to a 32-bit |
| 1402 | memory location from a 32-bit parameter |
| 1403 | -------------------------------------------------*/ |
| 1404 | |
| 1405 | static void emit_sbb_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1406 | { |
| 1407 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1408 | { |
| 1409 | if (inst->condflags != 0 || param->value != 0) |
| 1410 | emit_sbb_m32_imm(dst, MEMPARAMS, param->value); // sbb [dest],param |
| 1411 | } |
| 1412 | else |
| 1413 | { |
| 1414 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1415 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1416 | emit_sbb_m32_r32(dst, MEMPARAMS, reg); // sbb [dest],reg |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | |
| 1421 | /*------------------------------------------------- |
| 1422 | emit_cmp_r32_p32 - cmp operation to a 32-bit |
| 1423 | register from a 32-bit parameter |
| 1424 | -------------------------------------------------*/ |
| 1425 | |
| 1426 | static void emit_cmp_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1427 | { |
| 1428 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1429 | emit_cmp_r32_imm(dst, reg, param->value); // cmp reg,param |
| 1430 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1431 | emit_cmp_r32_m32(dst, reg, MABS(drcbe, param->value)); // cmp reg,[param] |
| 1432 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1433 | emit_cmp_r32_r32(dst, reg, param->value); // cmp reg,param |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | /*------------------------------------------------- |
| 1438 | emit_cmp_m32_p32 - cmp operation to a 32-bit |
| 1439 | memory location from a 32-bit parameter |
| 1440 | -------------------------------------------------*/ |
| 1441 | |
| 1442 | static void emit_cmp_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1443 | { |
| 1444 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1445 | emit_cmp_m32_imm(dst, MEMPARAMS, param->value); // cmp [dest],param |
| 1446 | else |
| 1447 | { |
| 1448 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1449 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1450 | emit_cmp_m32_r32(dst, MEMPARAMS, reg); // cmp [dest],reg |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | /*------------------------------------------------- |
| 1456 | emit_and_r32_p32 - and operation to a 32-bit |
| 1457 | register from a 32-bit parameter |
| 1458 | -------------------------------------------------*/ |
| 1459 | |
| 1460 | static void emit_and_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1461 | { |
| 1462 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1463 | { |
| 1464 | if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1465 | /* skip */; |
| 1466 | else if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1467 | emit_xor_r32_r32(dst, reg, reg); // xor reg,reg |
| 1468 | else |
| 1469 | emit_and_r32_imm(dst, reg, param->value); // and reg,param |
| 1470 | } |
| 1471 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1472 | emit_and_r32_m32(dst, reg, MABS(drcbe, param->value)); // and reg,[param] |
| 1473 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1474 | emit_and_r32_r32(dst, reg, param->value); // and reg,param |
| 1475 | } |
| 1476 | |
| 1477 | |
| 1478 | /*------------------------------------------------- |
| 1479 | emit_and_m32_p32 - and operation to a 32-bit |
| 1480 | memory location from a 32-bit parameter |
| 1481 | -------------------------------------------------*/ |
| 1482 | |
| 1483 | static void emit_and_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1484 | { |
| 1485 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1486 | { |
| 1487 | if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1488 | /* skip */; |
| 1489 | else if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1490 | emit_mov_m32_imm(dst, MEMPARAMS, 0); // mov [dest],0 |
| 1491 | else |
| 1492 | emit_and_m32_imm(dst, MEMPARAMS, param->value); // and [dest],param |
| 1493 | } |
| 1494 | else |
| 1495 | { |
| 1496 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1497 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1498 | emit_and_m32_r32(dst, MEMPARAMS, reg); // and [dest],reg |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /*------------------------------------------------- |
| 1504 | emit_test_r32_p32 - test operation to a 32-bit |
| 1505 | register from a 32-bit parameter |
| 1506 | -------------------------------------------------*/ |
| 1507 | |
| 1508 | static void emit_test_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1509 | { |
| 1510 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1511 | emit_test_r32_imm(dst, reg, param->value); // test reg,param |
| 1512 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1513 | emit_test_m32_r32(dst, MABS(drcbe, param->value), reg); // test [param],reg |
| 1514 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1515 | emit_test_r32_r32(dst, reg, param->value); // test reg,param |
| 1516 | } |
| 1517 | |
| 1518 | |
| 1519 | /*------------------------------------------------- |
| 1520 | emit_test_m32_p32 - test operation to a 32-bit |
| 1521 | memory location from a 32-bit parameter |
| 1522 | -------------------------------------------------*/ |
| 1523 | |
| 1524 | static void emit_test_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1525 | { |
| 1526 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1527 | emit_test_m32_imm(dst, MEMPARAMS, param->value); // test [dest],param |
| 1528 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1529 | { |
| 1530 | emit_mov_r32_p32(drcbe, dst, REG_EAX, param); // mov reg,param |
| 1531 | emit_test_m32_r32(dst, MEMPARAMS, REG_EAX); // test [dest],reg |
| 1532 | } |
| 1533 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1534 | emit_test_m32_r32(dst, MEMPARAMS, param->value); // test [dest],param |
| 1535 | } |
| 1536 | |
| 1537 | |
| 1538 | /*------------------------------------------------- |
| 1539 | emit_or_r32_p32 - or operation to a 32-bit |
| 1540 | register from a 32-bit parameter |
| 1541 | -------------------------------------------------*/ |
| 1542 | |
| 1543 | static void emit_or_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1544 | { |
| 1545 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1546 | { |
| 1547 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1548 | /* skip */; |
| 1549 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1550 | emit_mov_r32_imm(dst, reg, -1); // mov reg,-1 |
| 1551 | else |
| 1552 | emit_or_r32_imm(dst, reg, param->value); // or reg,param |
| 1553 | } |
| 1554 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1555 | emit_or_r32_m32(dst, reg, MABS(drcbe, param->value)); // or reg,[param] |
| 1556 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1557 | emit_or_r32_r32(dst, reg, param->value); // or reg,param |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | /*------------------------------------------------- |
| 1562 | emit_or_m32_p32 - or operation to a 32-bit |
| 1563 | memory location from a 32-bit parameter |
| 1564 | -------------------------------------------------*/ |
| 1565 | |
| 1566 | static void emit_or_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1567 | { |
| 1568 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1569 | { |
| 1570 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1571 | /* skip */; |
| 1572 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1573 | emit_mov_m32_imm(dst, MEMPARAMS, -1); // mov [dest],-1 |
| 1574 | else |
| 1575 | emit_or_m32_imm(dst, MEMPARAMS, param->value); // or [dest],param |
| 1576 | } |
| 1577 | else |
| 1578 | { |
| 1579 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1580 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1581 | emit_or_m32_r32(dst, MEMPARAMS, reg); // or [dest],reg |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | /*------------------------------------------------- |
| 1587 | emit_xor_r32_p32 - xor operation to a 32-bit |
| 1588 | register from a 32-bit parameter |
| 1589 | -------------------------------------------------*/ |
| 1590 | |
| 1591 | static void emit_xor_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1592 | { |
| 1593 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1594 | { |
| 1595 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1596 | /* skip */; |
| 1597 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1598 | emit_not_r32(dst, reg); // not reg |
| 1599 | else |
| 1600 | emit_xor_r32_imm(dst, reg, param->value); // xor reg,param |
| 1601 | } |
| 1602 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1603 | emit_xor_r32_m32(dst, reg, MABS(drcbe, param->value)); // xor reg,[param] |
| 1604 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1605 | emit_xor_r32_r32(dst, reg, param->value); // xor reg,param |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | /*------------------------------------------------- |
| 1610 | emit_xor_m32_p32 - xor operation to a 32-bit |
| 1611 | memory location from a 32-bit parameter |
| 1612 | -------------------------------------------------*/ |
| 1613 | |
| 1614 | static void emit_xor_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1615 | { |
| 1616 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1617 | { |
| 1618 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1619 | /* skip */; |
| 1620 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1621 | emit_not_m32(dst, MEMPARAMS); // not [dest] |
| 1622 | else |
| 1623 | emit_xor_m32_imm(dst, MEMPARAMS, param->value); // xor [dest],param |
| 1624 | } |
| 1625 | else |
| 1626 | { |
| 1627 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1628 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1629 | emit_xor_m32_r32(dst, MEMPARAMS, reg); // xor [dest],reg |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | |
| 1634 | /*------------------------------------------------- |
| 1635 | emit_shl_r32_p32 - shl operation to a 32-bit |
| 1636 | register from a 32-bit parameter |
| 1637 | -------------------------------------------------*/ |
| 1638 | |
| 1639 | static void emit_shl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1640 | { |
| 1641 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1642 | { |
| 1643 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1644 | /* skip */; |
| 1645 | else |
| 1646 | emit_shl_r32_imm(dst, reg, param->value); // shl reg,param |
| 1647 | } |
| 1648 | else |
| 1649 | { |
| 1650 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1651 | emit_shl_r32_cl(dst, reg); // shl reg,cl |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | |
| 1656 | /*------------------------------------------------- |
| 1657 | emit_shl_m32_p32 - shl operation to a 32-bit |
| 1658 | memory location from a 32-bit parameter |
| 1659 | -------------------------------------------------*/ |
| 1660 | |
| 1661 | static void emit_shl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1662 | { |
| 1663 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1664 | { |
| 1665 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1666 | /* skip */; |
| 1667 | else |
| 1668 | emit_shl_m32_imm(dst, MEMPARAMS, param->value); // shl [dest],param |
| 1669 | } |
| 1670 | else |
| 1671 | { |
| 1672 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1673 | emit_shl_m32_cl(dst, MEMPARAMS); // shl [dest],cl |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | |
| 1678 | /*------------------------------------------------- |
| 1679 | emit_shr_r32_p32 - shr operation to a 32-bit |
| 1680 | register from a 32-bit parameter |
| 1681 | -------------------------------------------------*/ |
| 1682 | |
| 1683 | static void emit_shr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1684 | { |
| 1685 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1686 | { |
| 1687 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1688 | /* skip */; |
| 1689 | else |
| 1690 | emit_shr_r32_imm(dst, reg, param->value); // shr reg,param |
| 1691 | } |
| 1692 | else |
| 1693 | { |
| 1694 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1695 | emit_shr_r32_cl(dst, reg); // shr reg,cl |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | /*------------------------------------------------- |
| 1701 | emit_shr_m32_p32 - shr operation to a 32-bit |
| 1702 | memory location from a 32-bit parameter |
| 1703 | -------------------------------------------------*/ |
| 1704 | |
| 1705 | static void emit_shr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1706 | { |
| 1707 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1708 | { |
| 1709 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1710 | /* skip */; |
| 1711 | else |
| 1712 | emit_shr_m32_imm(dst, MEMPARAMS, param->value); // shr [dest],param |
| 1713 | } |
| 1714 | else |
| 1715 | { |
| 1716 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1717 | emit_shr_m32_cl(dst, MEMPARAMS); // shr [dest],cl |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | |
| 1722 | /*------------------------------------------------- |
| 1723 | emit_sar_r32_p32 - sar operation to a 32-bit |
| 1724 | register from a 32-bit parameter |
| 1725 | -------------------------------------------------*/ |
| 1726 | |
| 1727 | static void emit_sar_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1728 | { |
| 1729 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1730 | { |
| 1731 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1732 | /* skip */; |
| 1733 | else |
| 1734 | emit_sar_r32_imm(dst, reg, param->value); // sar reg,param |
| 1735 | } |
| 1736 | else |
| 1737 | { |
| 1738 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1739 | emit_sar_r32_cl(dst, reg); // sar reg,cl |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | |
| 1744 | /*------------------------------------------------- |
| 1745 | emit_sar_m32_p32 - sar operation to a 32-bit |
| 1746 | memory location from a 32-bit parameter |
| 1747 | -------------------------------------------------*/ |
| 1748 | |
| 1749 | static void emit_sar_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1750 | { |
| 1751 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1752 | { |
| 1753 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1754 | /* skip */; |
| 1755 | else |
| 1756 | emit_sar_m32_imm(dst, MEMPARAMS, param->value); // sar [dest],param |
| 1757 | } |
| 1758 | else |
| 1759 | { |
| 1760 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1761 | emit_sar_m32_cl(dst, MEMPARAMS); // sar [dest],cl |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | |
| 1766 | /*------------------------------------------------- |
| 1767 | emit_rol_r32_p32 - rol operation to a 32-bit |
| 1768 | register from a 32-bit parameter |
| 1769 | -------------------------------------------------*/ |
| 1770 | |
| 1771 | static void emit_rol_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1772 | { |
| 1773 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1774 | { |
| 1775 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1776 | /* skip */; |
| 1777 | else |
| 1778 | emit_rol_r32_imm(dst, reg, param->value); // rol reg,param |
| 1779 | } |
| 1780 | else |
| 1781 | { |
| 1782 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1783 | emit_rol_r32_cl(dst, reg); // rol reg,cl |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | /*------------------------------------------------- |
| 1789 | emit_rol_m32_p32 - rol operation to a 32-bit |
| 1790 | memory location from a 32-bit parameter |
| 1791 | -------------------------------------------------*/ |
| 1792 | |
| 1793 | static void emit_rol_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1794 | { |
| 1795 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1796 | { |
| 1797 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1798 | /* skip */; |
| 1799 | else |
| 1800 | emit_rol_m32_imm(dst, MEMPARAMS, param->value); // rol [dest],param |
| 1801 | } |
| 1802 | else |
| 1803 | { |
| 1804 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1805 | emit_rol_m32_cl(dst, MEMPARAMS); // rol [dest],cl |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | |
| 1810 | /*------------------------------------------------- |
| 1811 | emit_ror_r32_p32 - ror operation to a 32-bit |
| 1812 | register from a 32-bit parameter |
| 1813 | -------------------------------------------------*/ |
| 1814 | |
| 1815 | static void emit_ror_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1816 | { |
| 1817 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1818 | { |
| 1819 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1820 | /* skip */; |
| 1821 | else |
| 1822 | emit_ror_r32_imm(dst, reg, param->value); // ror reg,param |
| 1823 | } |
| 1824 | else |
| 1825 | { |
| 1826 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1827 | emit_ror_r32_cl(dst, reg); // ror reg,cl |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | |
| 1832 | /*------------------------------------------------- |
| 1833 | emit_ror_m32_p32 - ror operation to a 32-bit |
| 1834 | memory location from a 32-bit parameter |
| 1835 | -------------------------------------------------*/ |
| 1836 | |
| 1837 | static void emit_ror_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1838 | { |
| 1839 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1840 | { |
| 1841 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1842 | /* skip */; |
| 1843 | else |
| 1844 | emit_ror_m32_imm(dst, MEMPARAMS, param->value); // ror [dest],param |
| 1845 | } |
| 1846 | else |
| 1847 | { |
| 1848 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1849 | emit_ror_m32_cl(dst, MEMPARAMS); // ror [dest],cl |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | /*------------------------------------------------- |
| 1855 | emit_rcl_r32_p32 - rcl operation to a 32-bit |
| 1856 | register from a 32-bit parameter |
| 1857 | -------------------------------------------------*/ |
| 1858 | |
| 1859 | static void emit_rcl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1860 | { |
| 1861 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1862 | { |
| 1863 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1864 | /* skip */; |
| 1865 | else |
| 1866 | emit_rcl_r32_imm(dst, reg, param->value); // rcl reg,param |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1871 | emit_rcl_r32_cl(dst, reg); // rcl reg,cl |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | |
| 1876 | /*------------------------------------------------- |
| 1877 | emit_rcl_m32_p32 - rcl operation to a 32-bit |
| 1878 | memory location from a 32-bit parameter |
| 1879 | -------------------------------------------------*/ |
| 1880 | |
| 1881 | static void emit_rcl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1882 | { |
| 1883 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1884 | { |
| 1885 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1886 | /* skip */; |
| 1887 | else |
| 1888 | emit_rcl_m32_imm(dst, MEMPARAMS, param->value); // rcl [dest],param |
| 1889 | } |
| 1890 | else |
| 1891 | { |
| 1892 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1893 | emit_rcl_m32_cl(dst, MEMPARAMS); // rcl [dest],cl |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | |
| 1898 | /*------------------------------------------------- |
| 1899 | emit_rcr_r32_p32 - rcr operation to a 32-bit |
| 1900 | register from a 32-bit parameter |
| 1901 | -------------------------------------------------*/ |
| 1902 | |
| 1903 | static void emit_rcr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1904 | { |
| 1905 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1906 | { |
| 1907 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1908 | /* skip */; |
| 1909 | else |
| 1910 | emit_rcr_r32_imm(dst, reg, param->value); // rcr reg,param |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1915 | emit_rcr_r32_cl(dst, reg); // rcr reg,cl |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | /*------------------------------------------------- |
| 1921 | emit_rcr_m32_p32 - rcr operation to a 32-bit |
| 1922 | memory location from a 32-bit parameter |
| 1923 | -------------------------------------------------*/ |
| 1924 | |
| 1925 | static void emit_rcr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1926 | { |
| 1927 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1928 | { |
| 1929 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1930 | /* skip */; |
| 1931 | else |
| 1932 | emit_rcr_m32_imm(dst, MEMPARAMS, param->value); // rcr [dest],param |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1937 | emit_rcr_m32_cl(dst, MEMPARAMS); // rcr [dest],cl |
| 1938 | } |
| 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | |
| 1943 | /*************************************************************************** |
| 1944 | EMITTERS FOR 64-BIT OPERATIONS WITH PARAMETERS |
| 1945 | ***************************************************************************/ |
| 1946 | |
| 1947 | /*------------------------------------------------- |
| 1948 | emit_mov_r64_p64 - move a 64-bit parameter |
| 1949 | into a register |
| 1950 | -------------------------------------------------*/ |
| 1951 | |
| 1952 | static void emit_mov_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param) |
| 1953 | { |
| 1954 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1955 | { |
| 1956 | if (param->value == 0) |
| 1957 | emit_xor_r32_r32(dst, reg, reg); // xor reg,reg |
| 1958 | else |
| 1959 | emit_mov_r64_imm(dst, reg, param->value); // mov reg,param |
| 1960 | } |
| 1961 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1962 | emit_mov_r64_m64(dst, reg, MABS(drcbe, param->value)); // mov reg,[param] |
| 1963 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1964 | { |
| 1965 | if (reg != param->value) |
| 1966 | emit_mov_r64_r64(dst, reg, param->value); // mov reg,param |
| 1967 | } |
| 1968 | } |
| 1969 | |
| 1970 | |
| 1971 | /*------------------------------------------------- |
| 1972 | emit_mov_p64_r64 - move a registers into a |
| 1973 | 64-bit parameter |
| 1974 | -------------------------------------------------*/ |
| 1975 | |
| 1976 | static void emit_mov_p64_r64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg) |
| 1977 | { |
| 1978 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 1979 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1980 | emit_mov_m64_r64(dst, MABS(drcbe, param->value), reg); // mov [param],reg |
| 1981 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1982 | { |
| 1983 | if (reg != param->value) |
| 1984 | emit_mov_r64_r64(dst, param->value, reg); // mov param,reg |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | |
| 1989 | /*------------------------------------------------- |
| 1990 | emit_add_r64_p64 - add operation to a 64-bit |
| 1991 | register from a 64-bit parameter |
| 1992 | -------------------------------------------------*/ |
| 1993 | |
| 1994 | static void emit_add_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1995 | { |
| 1996 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1997 | { |
| 1998 | if (inst->condflags != 0 || param->value != 0) |
| 1999 | { |
| 2000 | if (short_immediate(param->value)) |
| 2001 | emit_add_r64_imm(dst, reg, param->value); // add reg,param |
| 2002 | else |
| 2003 | { |
| 2004 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2005 | emit_add_r64_r64(dst, reg, REG_R11); // add reg,r11 |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2010 | emit_add_r64_m64(dst, reg, MABS(drcbe, param->value)); // add reg,[param] |
| 2011 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2012 | emit_add_r64_r64(dst, reg, param->value); // add reg,param |
| 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | /*------------------------------------------------- |
| 2017 | emit_add_m64_p64 - add operation to a 64-bit |
| 2018 | memory location from a 64-bit parameter |
| 2019 | -------------------------------------------------*/ |
| 2020 | |
| 2021 | static void emit_add_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2022 | { |
| 2023 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2024 | { |
| 2025 | if (inst->condflags != 0 || param->value != 0) |
| 2026 | { |
| 2027 | if (short_immediate(param->value)) |
| 2028 | emit_add_m64_imm(dst, MEMPARAMS, param->value); // add [mem],param |
| 2029 | else |
| 2030 | { |
| 2031 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2032 | emit_add_m64_r64(dst, MEMPARAMS, REG_R11); // add [mem],r11 |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | else |
| 2037 | { |
| 2038 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2039 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2040 | emit_add_m64_r64(dst, MEMPARAMS, reg); // add [dest],reg |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | |
| 2045 | /*------------------------------------------------- |
| 2046 | emit_adc_r64_p64 - adc operation to a 64-bit |
| 2047 | register from a 64-bit parameter |
| 2048 | -------------------------------------------------*/ |
| 2049 | |
| 2050 | static void emit_adc_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2051 | { |
| 2052 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2053 | { |
| 2054 | if (short_immediate(param->value)) |
| 2055 | emit_adc_r64_imm(dst, reg, param->value); // adc reg,param |
| 2056 | else |
| 2057 | { |
| 2058 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2059 | emit_adc_r64_r64(dst, reg, REG_R11); // adc reg,r11 |
| 2060 | } |
| 2061 | } |
| 2062 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2063 | emit_adc_r64_m64(dst, reg, MABS(drcbe, param->value)); // adc reg,[param] |
| 2064 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2065 | emit_adc_r64_r64(dst, reg, param->value); // adc reg,param |
| 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | /*------------------------------------------------- |
| 2070 | emit_adc_m64_p64 - adc operation to a 64-bit |
| 2071 | memory locaiton from a 64-bit parameter |
| 2072 | -------------------------------------------------*/ |
| 2073 | |
| 2074 | static void emit_adc_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2075 | { |
| 2076 | if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value)) |
| 2077 | emit_adc_m64_imm(dst, MEMPARAMS, param->value); // adc [mem],param |
| 2078 | else |
| 2079 | { |
| 2080 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2081 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2082 | emit_adc_m64_r64(dst, MEMPARAMS, reg); // adc [dest],reg |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | |
| 2087 | /*------------------------------------------------- |
| 2088 | emit_sub_r64_p64 - sub operation to a 64-bit |
| 2089 | register from a 64-bit parameter |
| 2090 | -------------------------------------------------*/ |
| 2091 | |
| 2092 | static void emit_sub_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2093 | { |
| 2094 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2095 | { |
| 2096 | if (inst->condflags != 0 || param->value != 0) |
| 2097 | { |
| 2098 | if (short_immediate(param->value)) |
| 2099 | emit_sub_r64_imm(dst, reg, param->value); // sub reg,param |
| 2100 | else |
| 2101 | { |
| 2102 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2103 | emit_sub_r64_r64(dst, reg, REG_R11); // sub reg,r11 |
| 2104 | } |
| 2105 | } |
| 2106 | } |
| 2107 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2108 | emit_sub_r64_m64(dst, reg, MABS(drcbe, param->value)); // sub reg,[param] |
| 2109 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2110 | emit_sub_r64_r64(dst, reg, param->value); // sub reg,param |
| 2111 | } |
| 2112 | |
| 2113 | |
| 2114 | /*------------------------------------------------- |
| 2115 | emit_sub_m64_p64 - sub operation to a 64-bit |
| 2116 | memory location from a 64-bit parameter |
| 2117 | -------------------------------------------------*/ |
| 2118 | |
| 2119 | static void emit_sub_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2120 | { |
| 2121 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2122 | { |
| 2123 | if (inst->condflags != 0 || param->value != 0) |
| 2124 | { |
| 2125 | if (short_immediate(param->value)) |
| 2126 | emit_sub_m64_imm(dst, MEMPARAMS, param->value); // sub [mem],param |
| 2127 | else |
| 2128 | { |
| 2129 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2130 | emit_sub_m64_r64(dst, MEMPARAMS, REG_R11); // sub [mem],r11 |
| 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | else |
| 2135 | { |
| 2136 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2137 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2138 | emit_sub_m64_r64(dst, MEMPARAMS, reg); // sub [dest],reg |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /*------------------------------------------------- |
| 2144 | emit_sbb_r64_p64 - sbb operation to a 64-bit |
| 2145 | register from a 64-bit parameter |
| 2146 | -------------------------------------------------*/ |
| 2147 | |
| 2148 | static void emit_sbb_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2149 | { |
| 2150 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2151 | { |
| 2152 | if (short_immediate(param->value)) |
| 2153 | emit_sbb_r64_imm(dst, reg, param->value); // sbb reg,param |
| 2154 | else |
| 2155 | { |
| 2156 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2157 | emit_sbb_r64_r64(dst, reg, REG_R11); // sbb reg,r11 |
| 2158 | } |
| 2159 | } |
| 2160 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2161 | emit_sbb_r64_m64(dst, reg, MABS(drcbe, param->value)); // sbb reg,[param] |
| 2162 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2163 | emit_sbb_r64_r64(dst, reg, param->value); // sbb reg,param |
| 2164 | } |
| 2165 | |
| 2166 | |
| 2167 | /*------------------------------------------------- |
| 2168 | emit_sbb_m64_p64 - sbb operation to a 64-bit |
| 2169 | memory location from a 64-bit parameter |
| 2170 | -------------------------------------------------*/ |
| 2171 | |
| 2172 | static void emit_sbb_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2173 | { |
| 2174 | if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value)) |
| 2175 | emit_sbb_m64_imm(dst, MEMPARAMS, param->value); // sbb [mem],param |
| 2176 | else |
| 2177 | { |
| 2178 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2179 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2180 | emit_sbb_m64_r64(dst, MEMPARAMS, reg); // sbb [dest],reg |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | |
| 2185 | /*------------------------------------------------- |
| 2186 | emit_cmp_r64_p64 - cmp operation to a 64-bit |
| 2187 | register from a 64-bit parameter |
| 2188 | -------------------------------------------------*/ |
| 2189 | |
| 2190 | static void emit_cmp_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2191 | { |
| 2192 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2193 | { |
| 2194 | if (short_immediate(param->value)) |
| 2195 | emit_cmp_r64_imm(dst, reg, param->value); // cmp reg,param |
| 2196 | else |
| 2197 | { |
| 2198 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2199 | emit_cmp_r64_r64(dst, reg, REG_R11); // cmp reg,r11 |
| 2200 | } |
| 2201 | } |
| 2202 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2203 | emit_cmp_r64_m64(dst, reg, MABS(drcbe, param->value)); // cmp reg,[param] |
| 2204 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2205 | emit_cmp_r64_r64(dst, reg, param->value); // cmp reg,param |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | /*------------------------------------------------- |
| 2210 | emit_cmp_m64_p64 - cmp operation to a 64-bit |
| 2211 | memory location from a 64-bit parameter |
| 2212 | -------------------------------------------------*/ |
| 2213 | |
| 2214 | static void emit_cmp_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2215 | { |
| 2216 | if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value)) |
| 2217 | emit_cmp_m64_imm(dst, MEMPARAMS, param->value); // cmp [dest],param |
| 2218 | else |
| 2219 | { |
| 2220 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2221 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2222 | emit_cmp_m64_r64(dst, MEMPARAMS, reg); // cmp [dest],reg |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | |
| 2227 | /*------------------------------------------------- |
| 2228 | emit_and_r64_p64 - and operation to a 64-bit |
| 2229 | register from a 64-bit parameter |
| 2230 | -------------------------------------------------*/ |
| 2231 | |
| 2232 | static void emit_and_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2233 | { |
| 2234 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2235 | { |
| 2236 | if (inst->condflags != 0 || param->value != U64(0xffffffffffffffff)) |
| 2237 | { |
| 2238 | if (short_immediate(param->value)) |
| 2239 | emit_and_r64_imm(dst, reg, param->value); // and reg,param |
| 2240 | else |
| 2241 | { |
| 2242 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2243 | emit_and_r64_r64(dst, reg, REG_R11); // and reg,r11 |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2248 | emit_and_r64_m64(dst, reg, MABS(drcbe, param->value)); // and reg,[param] |
| 2249 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2250 | emit_and_r64_r64(dst, reg, param->value); // and reg,param |
| 2251 | } |
| 2252 | |
| 2253 | |
| 2254 | /*------------------------------------------------- |
| 2255 | emit_and_m64_p64 - and operation to a 64-bit |
| 2256 | memory location from a 64-bit parameter |
| 2257 | -------------------------------------------------*/ |
| 2258 | |
| 2259 | static void emit_and_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2260 | { |
| 2261 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2262 | { |
| 2263 | if (inst->condflags != 0 || param->value != U64(0xffffffffffffffff)) |
| 2264 | { |
| 2265 | if (short_immediate(param->value)) |
| 2266 | emit_and_m64_imm(dst, MEMPARAMS, param->value); // and [mem],param |
| 2267 | else |
| 2268 | { |
| 2269 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2270 | emit_and_m64_r64(dst, MEMPARAMS, REG_R11); // and [mem],r11 |
| 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | else |
| 2275 | { |
| 2276 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2277 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2278 | emit_and_m64_r64(dst, MEMPARAMS, reg); // and [dest],reg |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | |
| 2283 | /*------------------------------------------------- |
| 2284 | emit_test_r64_p64 - test operation to a 64-bit |
| 2285 | register from a 64-bit parameter |
| 2286 | -------------------------------------------------*/ |
| 2287 | |
| 2288 | static void emit_test_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2289 | { |
| 2290 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2291 | { |
| 2292 | if (short_immediate(param->value)) |
| 2293 | emit_test_r64_imm(dst, reg, param->value); // test reg,param |
| 2294 | else |
| 2295 | { |
| 2296 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2297 | emit_test_r64_r64(dst, reg, REG_R11); // test reg,r11 |
| 2298 | } |
| 2299 | } |
| 2300 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2301 | emit_test_m64_r64(dst, MABS(drcbe, param->value), reg); // test [param],reg |
| 2302 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2303 | emit_test_r64_r64(dst, reg, param->value); // test reg,param |
| 2304 | } |
| 2305 | |
| 2306 | |
| 2307 | /*------------------------------------------------- |
| 2308 | emit_test_m64_p64 - test operation to a 64-bit |
| 2309 | memory location from a 64-bit parameter |
| 2310 | -------------------------------------------------*/ |
| 2311 | |
| 2312 | static void emit_test_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2313 | { |
| 2314 | if (param->type == DRCUML_PTYPE_IMMEDIATE && short_immediate(param->value)) |
| 2315 | emit_test_m64_imm(dst, MEMPARAMS, param->value); // test [dest],param |
| 2316 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2317 | { |
| 2318 | emit_mov_r64_p64(drcbe, dst, REG_EAX, param); // mov reg,param |
| 2319 | emit_test_m64_r64(dst, MEMPARAMS, REG_EAX); // test [dest],reg |
| 2320 | } |
| 2321 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2322 | emit_test_m64_r64(dst, MEMPARAMS, param->value); // test [dest],param |
| 2323 | } |
| 2324 | |
| 2325 | |
| 2326 | /*------------------------------------------------- |
| 2327 | emit_or_r64_p64 - or operation to a 64-bit |
| 2328 | register from a 64-bit parameter |
| 2329 | -------------------------------------------------*/ |
| 2330 | |
| 2331 | static void emit_or_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2332 | { |
| 2333 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2334 | { |
| 2335 | if (inst->condflags != 0 || param->value != 0) |
| 2336 | { |
| 2337 | if (short_immediate(param->value)) |
| 2338 | emit_or_r64_imm(dst, reg, param->value); // or reg,param |
| 2339 | else |
| 2340 | { |
| 2341 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2342 | emit_or_r64_r64(dst, reg, REG_R11); // or reg,r11 |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2347 | emit_or_r64_m64(dst, reg, MABS(drcbe, param->value)); // or reg,[param] |
| 2348 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2349 | emit_or_r64_r64(dst, reg, param->value); // or reg,param |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | /*------------------------------------------------- |
| 2354 | emit_or_m64_p64 - or operation to a 64-bit |
| 2355 | memory location from a 64-bit parameter |
| 2356 | -------------------------------------------------*/ |
| 2357 | |
| 2358 | static void emit_or_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2359 | { |
| 2360 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2361 | { |
| 2362 | if (inst->condflags != 0 || param->value != 0) |
| 2363 | { |
| 2364 | if (short_immediate(param->value)) |
| 2365 | emit_or_m64_imm(dst, MEMPARAMS, param->value); // or [mem],param |
| 2366 | else |
| 2367 | { |
| 2368 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2369 | emit_or_m64_r64(dst, MEMPARAMS, REG_R11); // or [mem],r11 |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | else |
| 2374 | { |
| 2375 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2376 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2377 | emit_or_m64_r64(dst, MEMPARAMS, reg); // or [dest],reg |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | /*------------------------------------------------- |
| 2383 | emit_xor_r64_p64 - xor operation to a 64-bit |
| 2384 | register from a 64-bit parameter |
| 2385 | -------------------------------------------------*/ |
| 2386 | |
| 2387 | static void emit_xor_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2388 | { |
| 2389 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2390 | { |
| 2391 | if (inst->condflags != 0 || param->value != 0) |
| 2392 | { |
| 2393 | if (param->value == U64(0xffffffffffffffff)) |
| 2394 | emit_not_r64(dst, reg); // not reg |
| 2395 | else if (short_immediate(param->value)) |
| 2396 | emit_xor_r64_imm(dst, reg, param->value); // xor reg,param |
| 2397 | else |
| 2398 | { |
| 2399 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2400 | emit_xor_r64_r64(dst, reg, REG_R11); // xor reg,r11 |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 2405 | emit_xor_r64_m64(dst, reg, MABS(drcbe, param->value)); // xor reg,[param] |
| 2406 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2407 | emit_xor_r64_r64(dst, reg, param->value); // xor reg,param |
| 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | /*------------------------------------------------- |
| 2412 | emit_xor_m64_p64 - xor operation to a 64-bit |
| 2413 | memory location from a 64-bit parameter |
| 2414 | -------------------------------------------------*/ |
| 2415 | |
| 2416 | static void emit_xor_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2417 | { |
| 2418 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2419 | { |
| 2420 | if (inst->condflags != 0 || param->value != 0) |
| 2421 | { |
| 2422 | if (param->value == U64(0xffffffffffffffff)) |
| 2423 | emit_not_m64(dst, MEMPARAMS); // not [mem] |
| 2424 | else if (short_immediate(param->value)) |
| 2425 | emit_xor_m64_imm(dst, MEMPARAMS, param->value); // xor [mem],param |
| 2426 | else |
| 2427 | { |
| 2428 | emit_mov_r64_imm(dst, REG_R11, param->value); // mov r11,param |
| 2429 | emit_xor_m64_r64(dst, MEMPARAMS, REG_R11); // xor [mem],r11 |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | else |
| 2434 | { |
| 2435 | int reg = param_select_register(REG_EAX, param, NULL); |
| 2436 | emit_mov_r64_p64(drcbe, dst, reg, param); // mov reg,param |
| 2437 | emit_xor_m64_r64(dst, MEMPARAMS, reg); // xor [dest],reg |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | |
| 2442 | /*------------------------------------------------- |
| 2443 | emit_shl_r64_p64 - shl operation to a 64-bit |
| 2444 | register from a 64-bit parameter |
| 2445 | -------------------------------------------------*/ |
| 2446 | |
| 2447 | static void emit_shl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2448 | { |
| 2449 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2450 | { |
| 2451 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2452 | /* skip */; |
| 2453 | else |
| 2454 | emit_shl_r64_imm(dst, reg, param->value); // shl reg,param |
| 2455 | } |
| 2456 | else |
| 2457 | { |
| 2458 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2459 | emit_shl_r64_cl(dst, reg); // shl reg,cl |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | |
| 2464 | /*------------------------------------------------- |
| 2465 | emit_shl_m64_p64 - shl operation to a 64-bit |
| 2466 | memory location from a 64-bit parameter |
| 2467 | -------------------------------------------------*/ |
| 2468 | |
| 2469 | static void emit_shl_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2470 | { |
| 2471 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2472 | { |
| 2473 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2474 | /* skip */; |
| 2475 | else |
| 2476 | emit_shl_m64_imm(dst, MEMPARAMS, param->value); // shl [dest],param |
| 2477 | } |
| 2478 | else |
| 2479 | { |
| 2480 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2481 | emit_shl_m64_cl(dst, MEMPARAMS); // shl [dest],cl |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | |
| 2486 | /*------------------------------------------------- |
| 2487 | emit_shr_r64_p64 - shr operation to a 64-bit |
| 2488 | register from a 64-bit parameter |
| 2489 | -------------------------------------------------*/ |
| 2490 | |
| 2491 | static void emit_shr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2492 | { |
| 2493 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2494 | { |
| 2495 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2496 | /* skip */; |
| 2497 | else |
| 2498 | emit_shr_r64_imm(dst, reg, param->value); // shr reg,param |
| 2499 | } |
| 2500 | else |
| 2501 | { |
| 2502 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2503 | emit_shr_r64_cl(dst, reg); // shr reg,cl |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | |
| 2508 | /*------------------------------------------------- |
| 2509 | emit_shr_m64_p64 - shr operation to a 64-bit |
| 2510 | memory location from a 64-bit parameter |
| 2511 | -------------------------------------------------*/ |
| 2512 | |
| 2513 | static void emit_shr_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2514 | { |
| 2515 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2516 | { |
| 2517 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2518 | /* skip */; |
| 2519 | else |
| 2520 | emit_shr_m64_imm(dst, MEMPARAMS, param->value); // shr [dest],param |
| 2521 | } |
| 2522 | else |
| 2523 | { |
| 2524 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2525 | emit_shr_m64_cl(dst, MEMPARAMS); // shr [dest],cl |
| 2526 | } |
| 2527 | } |
| 2528 | |
| 2529 | |
| 2530 | /*------------------------------------------------- |
| 2531 | emit_sar_r64_p64 - sar operation to a 64-bit |
| 2532 | register from a 64-bit parameter |
| 2533 | -------------------------------------------------*/ |
| 2534 | |
| 2535 | static void emit_sar_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2536 | { |
| 2537 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2538 | { |
| 2539 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2540 | /* skip */; |
| 2541 | else |
| 2542 | emit_sar_r64_imm(dst, reg, param->value); // sar reg,param |
| 2543 | } |
| 2544 | else |
| 2545 | { |
| 2546 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2547 | emit_sar_r64_cl(dst, reg); // sar reg,cl |
| 2548 | } |
| 2549 | } |
| 2550 | |
| 2551 | |
| 2552 | /*------------------------------------------------- |
| 2553 | emit_sar_m64_p64 - sar operation to a 64-bit |
| 2554 | memory location from a 64-bit parameter |
| 2555 | -------------------------------------------------*/ |
| 2556 | |
| 2557 | static void emit_sar_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2558 | { |
| 2559 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2560 | { |
| 2561 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2562 | /* skip */; |
| 2563 | else |
| 2564 | emit_sar_m64_imm(dst, MEMPARAMS, param->value); // sar [dest],param |
| 2565 | } |
| 2566 | else |
| 2567 | { |
| 2568 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2569 | emit_sar_m64_cl(dst, MEMPARAMS); // sar [dest],cl |
| 2570 | } |
| 2571 | } |
| 2572 | |
| 2573 | |
| 2574 | /*------------------------------------------------- |
| 2575 | emit_rol_r64_p64 - rol operation to a 64-bit |
| 2576 | register from a 64-bit parameter |
| 2577 | -------------------------------------------------*/ |
| 2578 | |
| 2579 | static void emit_rol_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2580 | { |
| 2581 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2582 | { |
| 2583 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2584 | /* skip */; |
| 2585 | else |
| 2586 | emit_rol_r64_imm(dst, reg, param->value); // rol reg,param |
| 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2591 | emit_rol_r64_cl(dst, reg); // rol reg,cl |
| 2592 | } |
| 2593 | } |
| 2594 | |
| 2595 | |
| 2596 | /*------------------------------------------------- |
| 2597 | emit_rol_m64_p64 - rol operation to a 64-bit |
| 2598 | memory location from a 64-bit parameter |
| 2599 | -------------------------------------------------*/ |
| 2600 | |
| 2601 | static void emit_rol_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2602 | { |
| 2603 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2604 | { |
| 2605 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2606 | /* skip */; |
| 2607 | else |
| 2608 | emit_rol_m64_imm(dst, MEMPARAMS, param->value); // rol [dest],param |
| 2609 | } |
| 2610 | else |
| 2611 | { |
| 2612 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2613 | emit_rol_m64_cl(dst, MEMPARAMS); // rol [dest],cl |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | |
| 2618 | /*------------------------------------------------- |
| 2619 | emit_ror_r64_p64 - ror operation to a 64-bit |
| 2620 | register from a 64-bit parameter |
| 2621 | -------------------------------------------------*/ |
| 2622 | |
| 2623 | static void emit_ror_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2624 | { |
| 2625 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2626 | { |
| 2627 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2628 | /* skip */; |
| 2629 | else |
| 2630 | emit_ror_r64_imm(dst, reg, param->value); // ror reg,param |
| 2631 | } |
| 2632 | else |
| 2633 | { |
| 2634 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2635 | emit_ror_r64_cl(dst, reg); // ror reg,cl |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | |
| 2640 | /*------------------------------------------------- |
| 2641 | emit_ror_m64_p64 - ror operation to a 64-bit |
| 2642 | memory location from a 64-bit parameter |
| 2643 | -------------------------------------------------*/ |
| 2644 | |
| 2645 | static void emit_ror_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2646 | { |
| 2647 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2648 | { |
| 2649 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2650 | /* skip */; |
| 2651 | else |
| 2652 | emit_ror_m64_imm(dst, MEMPARAMS, param->value); // ror [dest],param |
| 2653 | } |
| 2654 | else |
| 2655 | { |
| 2656 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2657 | emit_ror_m64_cl(dst, MEMPARAMS); // ror [dest],cl |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | |
| 2662 | /*------------------------------------------------- |
| 2663 | emit_rcl_r64_p64 - rcl operation to a 64-bit |
| 2664 | register from a 64-bit parameter |
| 2665 | -------------------------------------------------*/ |
| 2666 | |
| 2667 | static void emit_rcl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2668 | { |
| 2669 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2670 | { |
| 2671 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2672 | /* skip */; |
| 2673 | else |
| 2674 | emit_rcl_r64_imm(dst, reg, param->value); // rcl reg,param |
| 2675 | } |
| 2676 | else |
| 2677 | { |
| 2678 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2679 | emit_rcl_r64_cl(dst, reg); // rcl reg,cl |
| 2680 | } |
| 2681 | } |
| 2682 | |
| 2683 | |
| 2684 | /*------------------------------------------------- |
| 2685 | emit_rcl_m64_p64 - rcl operation to a 64-bit |
| 2686 | memory location from a 64-bit parameter |
| 2687 | -------------------------------------------------*/ |
| 2688 | |
| 2689 | static void emit_rcl_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2690 | { |
| 2691 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2692 | { |
| 2693 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2694 | /* skip */; |
| 2695 | else |
| 2696 | emit_rcl_m64_imm(dst, MEMPARAMS, param->value); // rcl [dest],param |
| 2697 | } |
| 2698 | else |
| 2699 | { |
| 2700 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2701 | emit_rcl_m64_cl(dst, MEMPARAMS); // rcl [dest],cl |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | /*------------------------------------------------- |
| 2707 | emit_rcr_r64_p64 - rcr operation to a 64-bit |
| 2708 | register from a 64-bit parameter |
| 2709 | -------------------------------------------------*/ |
| 2710 | |
| 2711 | static void emit_rcr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2712 | { |
| 2713 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2714 | { |
| 2715 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 2716 | /* skip */; |
| 2717 | else |
| 2718 | emit_rcr_r64_imm(dst, reg, param->value); // rcr reg,param |
| 2719 | } |
| 2720 | else |
| 2721 | { |
| 2722 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2723 | emit_rcr_r64_cl(dst, reg); // rcr reg,cl |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | |
| 2728 | /*------------------------------------------------- |
| 2729 | emit_rcr_m64_p64 - rcr operation to a 64-bit |
| 2730 | memory location from a 64-bit parameter |
| 2731 | -------------------------------------------------*/ |
| 2732 | |
| 2733 | static void emit_rcr_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2734 | { |
| 2735 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2736 | { |
| 2737 | if (inst->condflags != 0 && (UINT64)param->value == 0) |
| 2738 | /* skip */; |
| 2739 | else |
| 2740 | emit_rcr_m64_imm(dst, MEMPARAMS, param->value); // rcr [dest],param |
| 2741 | } |
| 2742 | else |
| 2743 | { |
| 2744 | emit_mov_r64_p64(drcbe, dst, REG_RCX, param); // mov rcx,param |
| 2745 | emit_rcr_m64_cl(dst, MEMPARAMS); // rcr [dest],cl |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | |
| 2750 | |
| 2751 | /*************************************************************************** |
| 2752 | EMITTERS FOR FLOATING POINT OPERATIONS WITH PARAMETERS |
| 2753 | ***************************************************************************/ |
| 2754 | |
| 2755 | /*------------------------------------------------- |
| 2756 | emit_movss_r128_p32 - move a 32-bit parameter |
| 2757 | into a register |
| 2758 | -------------------------------------------------*/ |
| 2759 | |
| 2760 | static void emit_movss_r128_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param) |
| 2761 | { |
| 2762 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 2763 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2764 | emit_movss_r128_m32(dst, reg, MABS(drcbe, param->value)); // movss reg,[param] |
| 2765 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2766 | { |
| 2767 | if (reg != param->value) |
| 2768 | emit_movss_r128_r128(dst, reg, param->value); // movss reg,param |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | |
| 2773 | /*------------------------------------------------- |
| 2774 | emit_movss_p32_r128 - move a register into a |
| 2775 | 32-bit parameter |
| 2776 | -------------------------------------------------*/ |
| 2777 | |
| 2778 | static void emit_movss_p32_r128(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg) |
| 2779 | { |
| 2780 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 2781 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2782 | emit_movss_m32_r128(dst, MABS(drcbe, param->value), reg); // movss [param],reg |
| 2783 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2784 | { |
| 2785 | if (reg != param->value) |
| 2786 | emit_movss_r128_r128(dst, param->value, reg); // movss param,reg |
| 2787 | } |
| 2788 | } |
| 2789 | |
| 2790 | |
| 2791 | /*------------------------------------------------- |
| 2792 | emit_movsd_r128_p64 - move a 64-bit parameter |
| 2793 | into a register |
| 2794 | -------------------------------------------------*/ |
| 2795 | |
| 2796 | static void emit_movsd_r128_p64(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param) |
| 2797 | { |
| 2798 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 2799 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2800 | emit_movsd_r128_m64(dst, reg, MABS(drcbe, param->value)); // movsd reg,[param] |
| 2801 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2802 | { |
| 2803 | if (reg != param->value) |
| 2804 | emit_movsd_r128_r128(dst, reg, param->value); // movsd reg,param |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | |
| 2809 | /*------------------------------------------------- |
| 2810 | emit_movsd_p64_r128 - move a register into a |
| 2811 | 64-bit parameter |
| 2812 | -------------------------------------------------*/ |
| 2813 | |
| 2814 | static void emit_movsd_p64_r128(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg) |
| 2815 | { |
| 2816 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 2817 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2818 | emit_movsd_m64_r128(dst, MABS(drcbe, param->value), reg); // movsd [param],reg |
| 2819 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2820 | { |
| 2821 | if (reg != param->value) |
| 2822 | emit_movsd_r128_r128(dst, param->value, reg); // movsd param,reg |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | |
| 2827 | |
| 2828 | /*************************************************************************** |
| 2829 | HELPERS TO CONVERT OTHER OPERATIONS TO MOVES |
| 2830 | ***************************************************************************/ |
| 2831 | |
| 2832 | /*------------------------------------------------- |
| 2833 | convert_to_mov_imm - convert an instruction |
| 2834 | into a mov immediate |
| 2835 | -------------------------------------------------*/ |
| 2836 | |
| 2837 | static x86code *convert_to_mov_imm(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, UINT64 imm) |
| 2838 | { |
| 2839 | drcuml_instruction temp = *inst; |
| 2840 | temp.numparams = 2; |
| 2841 | temp.param[0] = *dstp; |
| 2842 | temp.param[1].type = DRCUML_PTYPE_IMMEDIATE; |
| 2843 | temp.param[1].value = imm; |
| 2844 | return op_mov(drcbe, dst, &temp); |
| 2845 | } |
| 2846 | |
| 2847 | |
| 2848 | /*------------------------------------------------- |
| 2849 | convert_to_mov_src1 - convert an instruction |
| 2850 | into a mov src1 |
| 2851 | -------------------------------------------------*/ |
| 2852 | |
| 2853 | static x86code *convert_to_mov_src1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, const drcuml_parameter *srcp) |
| 2854 | { |
| 2855 | drcuml_instruction temp = *inst; |
| 2856 | temp.numparams = 2; |
| 2857 | temp.param[0] = *dstp; |
| 2858 | temp.param[1] = *srcp; |
| 2859 | return op_mov(drcbe, dst, &temp); |
| 2860 | } |
| 2861 | |
| 2862 | |
| 2863 | |
| 2864 | /*************************************************************************** |
| 2865 | OUT-OF-BAND CODE FIXUP CALLBACKS |
| 2866 | ***************************************************************************/ |
| 2867 | |
| 2868 | /*------------------------------------------------- |
| 2869 | fixup_label - callback to fixup forward- |
| 2870 | referenced labels |
| 2871 | -------------------------------------------------*/ |
| 2872 | |
| 2873 | static void fixup_label(void *parameter, drccodeptr labelcodeptr) |
| 2874 | { |
| 2875 | drccodeptr src = parameter; |
| 2876 | |
| 2877 | /* find the end of the instruction */ |
| 2878 | if (src[0] == 0xe3) |
| 2879 | { |
| 2880 | src += 1 + 1; |
| 2881 | src[-1] = labelcodeptr - src; |
| 2882 | } |
| 2883 | else if (src[0] == 0xe9) |
| 2884 | { |
| 2885 | src += 1 + 4; |
| 2886 | ((UINT32 *)src)[-1] = labelcodeptr - src; |
| 2887 | } |
| 2888 | else if (src[0] == 0x0f && (src[1] & 0xf0) == 0x80) |
| 2889 | { |
| 2890 | src += 2 + 4; |
| 2891 | ((UINT32 *)src)[-1] = labelcodeptr - src; |
| 2892 | } |
| 2893 | else |
| 2894 | fatalerror("fixup_label called with invalid jmp source!"); |
| 2895 | } |
| 2896 | |
| 2897 | |
| 2898 | /*------------------------------------------------- |
| 2899 | fixup_exception - callback to perform cleanup |
| 2900 | and jump to an exception handler |
| 2901 | -------------------------------------------------*/ |
| 2902 | |
| 2903 | static void fixup_exception(drccodeptr *codeptr, void *param1, void *param2, void *param3) |
| 2904 | { |
| 2905 | drcuml_parameter handp, exp; |
| 2906 | drcbe_state *drcbe = param1; |
| 2907 | drccodeptr src = param2; |
| 2908 | const drcuml_instruction *inst = param3; |
| 2909 | drccodeptr dst = *codeptr; |
| 2910 | drccodeptr *targetptr; |
| 2911 | |
| 2912 | /* normalize parameters */ |
| 2913 | param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI); |
| 2914 | |
| 2915 | /* look up the handle target */ |
| 2916 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value); |
| 2917 | |
| 2918 | /* first fixup the jump to get us here */ |
| 2919 | ((UINT32 *)src)[-1] = dst - src; |
| 2920 | |
| 2921 | /* then store the exception parameter */ |
| 2922 | emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp), &exp); // mov [exp],exp |
| 2923 | |
| 2924 | /* push the original return address on the stack */ |
| 2925 | emit_lea_r64_m64(&dst, REG_RAX, MABS(drcbe, src)); // lea rax,[return] |
| 2926 | emit_push_r64(&dst, REG_RAX); // push rax |
| 2927 | if (*targetptr != NULL) |
| 2928 | emit_jmp(&dst, *targetptr); // jmp *targetptr |
| 2929 | else |
| 2930 | emit_jmp_m64(&dst, MABS(drcbe, targetptr)); // jmp [targetptr] |
| 2931 | |
| 2932 | *codeptr = dst; |
| 2933 | } |
| 2934 | |
| 2935 | |
| 2936 | |
| 2937 | /*************************************************************************** |
| 2938 | DEBUG HELPERS |
| 2939 | ***************************************************************************/ |
| 2940 | |
| 2941 | /*------------------------------------------------- |
| 2942 | debug_log_hashjmp - callback to handle |
| 2943 | logging of hashjmps |
| 2944 | -------------------------------------------------*/ |
| 2945 | |
| 2946 | #if LOG_HASHJMPS |
| 2947 | static void debug_log_hashjmp(int mode, offs_t pc) |
| 2948 | { |
| 2949 | printf("mode=%d PC=%08X\n", mode, pc); |
| 2950 | } |
| 2951 | #endif |
| 2952 | |
| 2953 | |
| 2954 | |
| 2955 | /*************************************************************************** |
| 2956 | COMPILE-TIME OPCODES |
| 2957 | ***************************************************************************/ |
| 2958 | |
| 2959 | /*------------------------------------------------- |
| 2960 | op_handle - process a HANDLE opcode |
| 2961 | -------------------------------------------------*/ |
| 2962 | |
| 2963 | static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 2964 | { |
| 2965 | assert(inst->numparams == 1); |
| 2966 | assert(inst->param[0].type == DRCUML_PTYPE_MEMORY); |
| 2967 | |
| 2968 | /* register the current pointer for the handle */ |
| 2969 | drcuml_handle_set_codeptr((drcuml_codehandle *)(FPTR)inst->param[0].value, dst); |
| 2970 | |
| 2971 | /* by default, the handle points to prolog code that moves the stack pointer */ |
| 2972 | emit_sub_r64_imm(&dst, REG_RSP, 40); // sub rsp,40 |
| 2973 | return dst; |
| 2974 | } |
| 2975 | |
| 2976 | |
| 2977 | /*------------------------------------------------- |
| 2978 | op_hash - process a HASH opcode |
| 2979 | -------------------------------------------------*/ |
| 2980 | |
| 2981 | static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 2982 | { |
| 2983 | assert(inst->numparams == 2); |
| 2984 | assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE); |
| 2985 | assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE); |
| 2986 | |
| 2987 | /* register the current pointer for the mode/PC */ |
| 2988 | drchash_set_codeptr(drcbe->hash, inst->param[0].value, inst->param[1].value, dst); |
| 2989 | return dst; |
| 2990 | } |
| 2991 | |
| 2992 | |
| 2993 | /*------------------------------------------------- |
| 2994 | op_label - process a LABEL opcode |
| 2995 | -------------------------------------------------*/ |
| 2996 | |
| 2997 | static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 2998 | { |
| 2999 | assert(inst->numparams == 1); |
| 3000 | assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE); |
| 3001 | |
| 3002 | /* register the current pointer for the label */ |
| 3003 | drclabel_set_codeptr(drcbe->labels, inst->param[0].value, dst); |
| 3004 | return dst; |
| 3005 | } |
| 3006 | |
| 3007 | |
| 3008 | /*------------------------------------------------- |
| 3009 | op_comment - process a COMMENT opcode |
| 3010 | -------------------------------------------------*/ |
| 3011 | |
| 3012 | static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3013 | { |
| 3014 | assert(inst->numparams == 1); |
| 3015 | assert(inst->param[0].type == DRCUML_PTYPE_MEMORY); |
| 3016 | |
| 3017 | /* do nothing */ |
| 3018 | return dst; |
| 3019 | } |
| 3020 | |
| 3021 | |
| 3022 | /*------------------------------------------------- |
| 3023 | op_mapvar - process a MAPVAR opcode |
| 3024 | -------------------------------------------------*/ |
| 3025 | |
| 3026 | static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3027 | { |
| 3028 | assert(inst->numparams == 2); |
| 3029 | assert(inst->param[0].type == DRCUML_PTYPE_MAPVAR); |
| 3030 | assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE); |
| 3031 | |
| 3032 | /* set the value of the specified mapvar */ |
| 3033 | drcmap_set_value(drcbe->map, dst, inst->param[0].value, inst->param[1].value); |
| 3034 | return dst; |
| 3035 | } |
| 3036 | |
| 3037 | |
| 3038 | |
| 3039 | /*************************************************************************** |
| 3040 | CONTROL FLOW OPCODES |
| 3041 | ***************************************************************************/ |
| 3042 | |
| 3043 | /*------------------------------------------------- |
| 3044 | op_debug - process a DEBUG opcode |
| 3045 | -------------------------------------------------*/ |
| 3046 | |
| 3047 | static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3048 | { |
| 3049 | #ifdef ENABLE_DEBUGGER |
| 3050 | if (Machine->debug_mode) |
| 3051 | { |
| 3052 | drcuml_parameter pcp; |
| 3053 | |
| 3054 | /* validate instruction */ |
| 3055 | assert(inst->size == 4); |
| 3056 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3057 | |
| 3058 | /* normalize parameters */ |
| 3059 | param_normalize_1(drcbe, inst, &pcp, PTYPE_MRI); |
| 3060 | |
| 3061 | /* push the parameter */ |
| 3062 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &pcp); // mov param1,pcp |
| 3063 | emit_smart_call_m64(drcbe, &dst, &drcbe->mame_debug_hook); // call mame_debug_hook |
| 3064 | } |
| 3065 | #endif |
| 3066 | |
| 3067 | return dst; |
| 3068 | } |
| 3069 | |
| 3070 | |
| 3071 | /*------------------------------------------------- |
| 3072 | op_exit - process an EXIT opcode |
| 3073 | -------------------------------------------------*/ |
| 3074 | |
| 3075 | static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3076 | { |
| 3077 | drcuml_parameter retp; |
| 3078 | |
| 3079 | /* validate instruction */ |
| 3080 | assert(inst->size == 4); |
| 3081 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3082 | |
| 3083 | /* normalize parameters */ |
| 3084 | param_normalize_1(drcbe, inst, &retp, PTYPE_MRI); |
| 3085 | |
| 3086 | /* load the parameter into EAX */ |
| 3087 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &retp); // mov eax,retp |
| 3088 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3089 | emit_jmp(&dst, drcbe->exit); // jmp exit |
| 3090 | else |
| 3091 | emit_jcc(&dst, X86_CONDITION(inst->condflags), drcbe->exit); // jcc exit |
| 3092 | |
| 3093 | return dst; |
| 3094 | } |
| 3095 | |
| 3096 | |
| 3097 | /*------------------------------------------------- |
| 3098 | op_hashjmp - process a HASHJMP opcode |
| 3099 | -------------------------------------------------*/ |
| 3100 | |
| 3101 | static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3102 | { |
| 3103 | drcuml_parameter modep, pcp, exp; |
| 3104 | |
| 3105 | /* validate instruction */ |
| 3106 | assert(inst->size == 4); |
| 3107 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3108 | |
| 3109 | /* normalize parameters */ |
| 3110 | param_normalize_3(drcbe, inst, &modep, PTYPE_MRI, &pcp, PTYPE_MRI, &exp, PTYPE_M); |
| 3111 | |
| 3112 | #if LOG_HASHJMPS |
| 3113 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &pcp); |
| 3114 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &modep); |
| 3115 | emit_smart_call_m64(drcbe, &dst, &drcbe->debug_log_hashjmp); |
| 3116 | #endif |
| 3117 | |
| 3118 | /* load the stack base one word early so we end up at the right spot after our call below */ |
| 3119 | emit_mov_r64_m64(&dst, REG_RSP, MABS(drcbe, &drcbe->hashstacksave)); // mov rsp,[hashstacksave] |
| 3120 | |
| 3121 | /* fixed mode cases */ |
| 3122 | if (modep.type == DRCUML_PTYPE_IMMEDIATE) |
| 3123 | { |
| 3124 | /* a straight immediate jump is direct, though we need the PC in EAX in case of failure */ |
| 3125 | if (pcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3126 | { |
| 3127 | UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask; |
| 3128 | UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask; |
| 3129 | emit_call_m64(&dst, MABS(drcbe, &drcbe->hash->base[modep.value][l1val][l2val])); |
| 3130 | // call hash[modep][l1val][l2val] |
| 3131 | } |
| 3132 | |
| 3133 | /* a fixed mode but variable PC */ |
| 3134 | else |
| 3135 | { |
| 3136 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp); // mov eax,pcp |
| 3137 | emit_mov_r32_r32(&dst, REG_EDX, REG_EAX); // mov edx,eax |
| 3138 | emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift); // shr edx,l1shift |
| 3139 | emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and eax,l2mask << l2shift |
| 3140 | emit_mov_r64_m64(&dst, REG_RDX, MBISD(REG_RBP, REG_RDX, 8, offset_from_rbp(drcbe, (FPTR)&drcbe->hash->base[modep.value][0]))); |
| 3141 | // mov rdx,hash[modep+edx*8] |
| 3142 | emit_call_m64(&dst, MBISD(REG_RDX, REG_RAX, 8 >> drcbe->hash->l2shift, 0)); // call [rdx+rax*shift] |
| 3143 | } |
| 3144 | } |
| 3145 | else |
| 3146 | { |
| 3147 | /* variable mode */ |
| 3148 | int modereg = param_select_register(REG_ECX, &modep, NULL); |
| 3149 | emit_mov_r32_p32(drcbe, &dst, modereg, &modep); // mov modereg,modep |
| 3150 | emit_mov_r64_m64(&dst, REG_RCX, MBISD(REG_RBP, modereg, 8, offset_from_rbp(drcbe, (FPTR)&drcbe->hash->base[0]))); |
| 3151 | // mov rcx,hash[modereg*8] |
| 3152 | |
| 3153 | /* fixed PC */ |
| 3154 | if (pcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3155 | { |
| 3156 | UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask; |
| 3157 | UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask; |
| 3158 | emit_mov_r64_m64(&dst, REG_RDX, MBD(REG_RCX, l1val*8)); // mov rdx,[rcx+l1val*8] |
| 3159 | emit_call_m64(&dst, MBD(REG_RDX, l2val*8)); // call [l2val*8] |
| 3160 | } |
| 3161 | |
| 3162 | /* variable PC */ |
| 3163 | else |
| 3164 | { |
| 3165 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp); // mov eax,pcp |
| 3166 | emit_mov_r32_r32(&dst, REG_EDX, REG_EAX); // mov edx,eax |
| 3167 | emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift); // shr edx,l1shift |
| 3168 | emit_mov_r64_m64(&dst, REG_RDX, MBISD(REG_RCX, REG_RDX, 8, 0)); // mov rdx,[rcx+rdx*8] |
| 3169 | emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and eax,l2mask << l2shift |
| 3170 | emit_call_m64(&dst, MBISD(REG_RDX, REG_RAX, 8 >> drcbe->hash->l2shift, 0)); // call [rdx+rax*shift] |
| 3171 | } |
| 3172 | } |
| 3173 | |
| 3174 | /* in all cases, if there is no code, we return here to generate the exception */ |
| 3175 | emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp.l), &pcp); // mov [exp],param |
| 3176 | emit_sub_r64_imm(&dst, REG_RSP, 8); // sub rsp,8 |
| 3177 | emit_call_m64(&dst, MABS(drcbe, exp.value)); // call [exp] |
| 3178 | |
| 3179 | return dst; |
| 3180 | } |
| 3181 | |
| 3182 | |
| 3183 | /*------------------------------------------------- |
| 3184 | op_jmp - process a JMP opcode |
| 3185 | -------------------------------------------------*/ |
| 3186 | |
| 3187 | static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3188 | { |
| 3189 | drcuml_parameter labelp; |
| 3190 | x86code *jmptarget; |
| 3191 | |
| 3192 | /* validate instruction */ |
| 3193 | assert(inst->size == 4); |
| 3194 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3195 | |
| 3196 | /* normalize parameters */ |
| 3197 | param_normalize_1(drcbe, inst, &labelp, PTYPE_I); |
| 3198 | |
| 3199 | /* look up the jump target and jump there */ |
| 3200 | jmptarget = (x86code *)drclabel_get_codeptr(drcbe->labels, labelp.value, fixup_label, dst); |
| 3201 | if (jmptarget == NULL) |
| 3202 | jmptarget = dst + 0x7ffffff0; |
| 3203 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3204 | emit_jmp(&dst, jmptarget); // jmp target |
| 3205 | else |
| 3206 | emit_jcc(&dst, X86_CONDITION(inst->condflags), jmptarget); // jcc target |
| 3207 | |
| 3208 | return dst; |
| 3209 | } |
| 3210 | |
| 3211 | |
| 3212 | /*------------------------------------------------- |
| 3213 | op_exh - process an EXH opcode |
| 3214 | -------------------------------------------------*/ |
| 3215 | |
| 3216 | static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3217 | { |
| 3218 | drcuml_parameter handp, exp; |
| 3219 | drccodeptr *targetptr; |
| 3220 | |
| 3221 | /* validate instruction */ |
| 3222 | assert(inst->size == 4); |
| 3223 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3224 | |
| 3225 | /* normalize parameters */ |
| 3226 | param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI); |
| 3227 | |
| 3228 | /* look up the handle target */ |
| 3229 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value); |
| 3230 | |
| 3231 | /* perform the exception processing inline if unconditional */ |
| 3232 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3233 | { |
| 3234 | emit_mov_m32_p32(drcbe, &dst, MABS(drcbe, &drcbe->state.exp.l), &exp); // mov [exp],exp |
| 3235 | if (*targetptr != NULL) |
| 3236 | emit_call(&dst, *targetptr); // call *targetptr |
| 3237 | else |
| 3238 | emit_call_m64(&dst, MABS(drcbe, targetptr)); // call [targetptr] |
| 3239 | } |
| 3240 | |
| 3241 | /* otherwise, jump to an out-of-band handler */ |
| 3242 | else |
| 3243 | { |
| 3244 | emit_jcc(&dst, X86_CONDITION(inst->condflags), dst + 0x7ffffff0); // jcc exception |
| 3245 | drccache_request_oob_codegen(drcbe->cache, fixup_exception, drcbe, dst, (void *)inst); |
| 3246 | } |
| 3247 | return dst; |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | /*------------------------------------------------- |
| 3252 | op_callh - process a CALLH opcode |
| 3253 | -------------------------------------------------*/ |
| 3254 | |
| 3255 | static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3256 | { |
| 3257 | drcuml_parameter handp; |
| 3258 | drccodeptr *targetptr; |
| 3259 | emit_link skip = { 0 }; |
| 3260 | |
| 3261 | /* validate instruction */ |
| 3262 | assert(inst->size == 4); |
| 3263 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3264 | |
| 3265 | /* normalize parameters */ |
| 3266 | param_normalize_1(drcbe, inst, &handp, PTYPE_M); |
| 3267 | |
| 3268 | /* look up the handle target */ |
| 3269 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)handp.value); |
| 3270 | |
| 3271 | /* skip if conditional */ |
| 3272 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3273 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3274 | |
| 3275 | /* jump through the handle; directly if a normal jump */ |
| 3276 | if (*targetptr != NULL) |
| 3277 | emit_call(&dst, *targetptr); // call *targetptr |
| 3278 | else |
| 3279 | emit_call_m64(&dst, MABS(drcbe, targetptr)); // call [targetptr] |
| 3280 | |
| 3281 | /* resolve the conditional link */ |
| 3282 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3283 | resolve_link(&dst, &skip); // skip: |
| 3284 | return dst; |
| 3285 | } |
| 3286 | |
| 3287 | |
| 3288 | /*------------------------------------------------- |
| 3289 | op_ret - process a RET opcode |
| 3290 | -------------------------------------------------*/ |
| 3291 | |
| 3292 | static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3293 | { |
| 3294 | emit_link skip = { 0 }; |
| 3295 | |
| 3296 | /* validate instruction */ |
| 3297 | assert(inst->size == 4); |
| 3298 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3299 | assert(inst->numparams == 0); |
| 3300 | |
| 3301 | /* skip if conditional */ |
| 3302 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3303 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3304 | |
| 3305 | /* return */ |
| 3306 | emit_add_r64_imm(&dst, REG_RSP, 40); // add rsp,40 |
| 3307 | emit_ret(&dst); // ret |
| 3308 | |
| 3309 | /* resolve the conditional link */ |
| 3310 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3311 | resolve_link(&dst, &skip); // skip: |
| 3312 | return dst; |
| 3313 | } |
| 3314 | |
| 3315 | |
| 3316 | /*------------------------------------------------- |
| 3317 | op_callc - process a CALLC opcode |
| 3318 | -------------------------------------------------*/ |
| 3319 | |
| 3320 | static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3321 | { |
| 3322 | drcuml_parameter funcp, paramp; |
| 3323 | emit_link skip = { 0 }; |
| 3324 | |
| 3325 | /* validate instruction */ |
| 3326 | assert(inst->size == 4); |
| 3327 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3328 | |
| 3329 | /* normalize parameters */ |
| 3330 | param_normalize_2(drcbe, inst, &funcp, PTYPE_M, ¶mp, PTYPE_M); |
| 3331 | |
| 3332 | /* skip if conditional */ |
| 3333 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3334 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3335 | |
| 3336 | /* perform the call */ |
| 3337 | emit_mov_r64_imm(&dst, REG_PARAM1, paramp.value); // mov param1,paramp |
| 3338 | emit_smart_call_r64(drcbe, &dst, (x86code *)(FPTR)funcp.value, REG_RAX); // call funcp |
| 3339 | |
| 3340 | /* resolve the conditional link */ |
| 3341 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3342 | resolve_link(&dst, &skip); // skip: |
| 3343 | return dst; |
| 3344 | } |
| 3345 | |
| 3346 | |
| 3347 | /*------------------------------------------------- |
| 3348 | op_recover - process a RECOVER opcode |
| 3349 | -------------------------------------------------*/ |
| 3350 | |
| 3351 | static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3352 | { |
| 3353 | drcuml_parameter dstp, mvparam; |
| 3354 | |
| 3355 | /* validate instruction */ |
| 3356 | assert(inst->size == 4); |
| 3357 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3358 | |
| 3359 | /* normalize parameters */ |
| 3360 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &mvparam, PTYPE_I); |
| 3361 | |
| 3362 | /* call the recovery code */ |
| 3363 | emit_mov_r64_m64(&dst, REG_RAX, MABS(drcbe, &drcbe->stacksave)); // mov rax,stacksave |
| 3364 | emit_mov_r64_m64(&dst, REG_RAX, MBD(REG_RAX, -8)); // mov rax,[rax-4] |
| 3365 | emit_mov_r64_imm(&dst, REG_PARAM1, (FPTR)drcbe->map); // mov param1,drcbe->map |
| 3366 | emit_lea_r64_m64(&dst, REG_PARAM2, MBD(REG_RAX, -1)); // lea param2,[rax-1] |
| 3367 | emit_mov_r64_imm(&dst, REG_PARAM3, inst->param[1].value); // mov param3,param[1].value |
| 3368 | emit_smart_call_m64(drcbe, &dst, &drcbe->drcmap_get_value); // call drcmap_get_value |
| 3369 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 3370 | |
| 3371 | return dst; |
| 3372 | } |
| 3373 | |
| 3374 | |
| 3375 | |
| 3376 | /*************************************************************************** |
| 3377 | INTERNAL REGISTER OPCODES |
| 3378 | ***************************************************************************/ |
| 3379 | |
| 3380 | /*------------------------------------------------- |
| 3381 | op_setfmod - process a SETFMOD opcode |
| 3382 | -------------------------------------------------*/ |
| 3383 | |
| 3384 | static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3385 | { |
| 3386 | drcuml_parameter srcp; |
| 3387 | |
| 3388 | /* validate instruction */ |
| 3389 | assert(inst->size == 4); |
| 3390 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3391 | |
| 3392 | /* normalize parameters */ |
| 3393 | param_normalize_1(drcbe, inst, &srcp, PTYPE_MRI); |
| 3394 | |
| 3395 | /* immediate case */ |
| 3396 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3397 | { |
| 3398 | srcp.value &= 3; |
| 3399 | emit_mov_m8_imm(&dst, MABS(drcbe, &drcbe->state.fmod), srcp.value); // mov [fmod],srcp |
| 3400 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[srcp.value])); // ldmxcsr fp_control[srcp] |
| 3401 | } |
| 3402 | |
| 3403 | /* register/memory case */ |
| 3404 | else |
| 3405 | { |
| 3406 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp); // mov eax,srcp |
| 3407 | emit_and_r32_imm(&dst, REG_EAX, 3); // and eax,3 |
| 3408 | emit_mov_m8_r8(&dst, MABS(drcbe, &drcbe->state.fmod), REG_AL); // mov [fmod],al |
| 3409 | emit_ldmxcsr_m32(&dst, MBISD(REG_RBP, REG_RAX, 4, offset_from_rbp(drcbe, (FPTR)&drcbe->ssecontrol[0]))); |
| 3410 | // ldmxcsr fp_control[eax] |
| 3411 | } |
| 3412 | |
| 3413 | return dst; |
| 3414 | } |
| 3415 | |
| 3416 | |
| 3417 | /*------------------------------------------------- |
| 3418 | op_getfmod - process a GETFMOD opcode |
| 3419 | -------------------------------------------------*/ |
| 3420 | |
| 3421 | static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3422 | { |
| 3423 | drcuml_parameter dstp; |
| 3424 | |
| 3425 | /* validate instruction */ |
| 3426 | assert(inst->size == 4); |
| 3427 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3428 | |
| 3429 | /* normalize parameters */ |
| 3430 | param_normalize_1(drcbe, inst, &dstp, PTYPE_MR); |
| 3431 | |
| 3432 | /* fetch the current mode and store to the destination */ |
| 3433 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3434 | emit_movzx_r32_m8(&dst, dstp.value, MABS(drcbe, &drcbe->state.fmod)); // movzx reg,[fmod] |
| 3435 | else |
| 3436 | { |
| 3437 | emit_movzx_r32_m8(&dst, REG_EAX, MABS(drcbe, &drcbe->state.fmod)); // movzx eax,[fmod] |
| 3438 | emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX); // mov [dstp],eax |
| 3439 | } |
| 3440 | |
| 3441 | return dst; |
| 3442 | } |
| 3443 | |
| 3444 | |
| 3445 | /*------------------------------------------------- |
| 3446 | op_getexp - process a GETEXP opcode |
| 3447 | -------------------------------------------------*/ |
| 3448 | |
| 3449 | static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3450 | { |
| 3451 | drcuml_parameter dstp; |
| 3452 | |
| 3453 | /* validate instruction */ |
| 3454 | assert(inst->size == 4); |
| 3455 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3456 | |
| 3457 | /* normalize parameters */ |
| 3458 | param_normalize_1(drcbe, inst, &dstp, PTYPE_MR); |
| 3459 | |
| 3460 | /* fetch the exception parameter and store to the destination */ |
| 3461 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3462 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe, &drcbe->state.exp.l)); // mov reg,[exp] |
| 3463 | else |
| 3464 | { |
| 3465 | emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe, &drcbe->state.exp.l)); // mov eax,[exp] |
| 3466 | emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX); // mov [dstp],eax |
| 3467 | } |
| 3468 | |
| 3469 | return dst; |
| 3470 | } |
| 3471 | |
| 3472 | |
| 3473 | |
| 3474 | /*************************************************************************** |
| 3475 | INTEGER OPERATIONS |
| 3476 | ***************************************************************************/ |
| 3477 | |
| 3478 | /*------------------------------------------------- |
| 3479 | op_load1u - process a LOAD1U opcode |
| 3480 | -------------------------------------------------*/ |
| 3481 | |
| 3482 | static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3483 | { |
| 3484 | drcuml_parameter dstp, basep, indp; |
| 3485 | int basereg, dstreg; |
| 3486 | INT32 baseoffs; |
| 3487 | |
| 3488 | /* validate instruction */ |
| 3489 | assert(inst->size == 4 || inst->size == 8); |
| 3490 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3491 | |
| 3492 | /* normalize parameters */ |
| 3493 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3494 | |
| 3495 | /* pick a target register for the general case */ |
| 3496 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3497 | |
| 3498 | /* determine the pointer base */ |
| 3499 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3500 | |
| 3501 | /* immediate index */ |
| 3502 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3503 | emit_movzx_r32_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value)); // movzx dstreg,[basep + 1*indp] |
| 3504 | |
| 3505 | /* other index */ |
| 3506 | else |
| 3507 | { |
| 3508 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3509 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3510 | emit_movzx_r32_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs)); // movzx dstreg,[basep + 1*indp] |
| 3511 | } |
| 3512 | |
| 3513 | /* store to appropriate target size */ |
| 3514 | if (inst->size == 4) |
| 3515 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3516 | else if (inst->size == 8) |
| 3517 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3518 | |
| 3519 | return dst; |
| 3520 | } |
| 3521 | |
| 3522 | |
| 3523 | /*------------------------------------------------- |
| 3524 | op_load1s - process a LOAD1S opcode |
| 3525 | -------------------------------------------------*/ |
| 3526 | |
| 3527 | static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3528 | { |
| 3529 | drcuml_parameter dstp, basep, indp; |
| 3530 | int basereg, dstreg; |
| 3531 | INT32 baseoffs; |
| 3532 | |
| 3533 | /* validate instruction */ |
| 3534 | assert(inst->size == 4 || inst->size == 8); |
| 3535 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3536 | |
| 3537 | /* normalize parameters */ |
| 3538 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3539 | |
| 3540 | /* pick a target register for the general case */ |
| 3541 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3542 | |
| 3543 | /* determine the pointer base */ |
| 3544 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3545 | |
| 3546 | /* 32-bit form */ |
| 3547 | if (inst->size == 4) |
| 3548 | { |
| 3549 | /* immediate index */ |
| 3550 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3551 | emit_movsx_r32_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value)); // movsx eax,[basep + 1*indp] |
| 3552 | |
| 3553 | /* other index */ |
| 3554 | else |
| 3555 | { |
| 3556 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3557 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3558 | emit_movsx_r32_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs)); // movsx eax,[basep + 1*indp] |
| 3559 | } |
| 3560 | |
| 3561 | /* general case */ |
| 3562 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3563 | } |
| 3564 | |
| 3565 | /* 64-bit form */ |
| 3566 | else |
| 3567 | { |
| 3568 | /* immediate index */ |
| 3569 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3570 | emit_movsx_r64_m8(&dst, dstreg, MBD(basereg, baseoffs + 1*indp.value)); // movsx rax,[basep + 1*indp] |
| 3571 | |
| 3572 | /* other index */ |
| 3573 | else |
| 3574 | { |
| 3575 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3576 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3577 | emit_movsx_r64_m8(&dst, dstreg, MBISD(basereg, indreg, 1, baseoffs)); // movsx rax,[basep + 1*indp] |
| 3578 | } |
| 3579 | |
| 3580 | /* general case */ |
| 3581 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3582 | } |
| 3583 | return dst; |
| 3584 | } |
| 3585 | |
| 3586 | |
| 3587 | /*------------------------------------------------- |
| 3588 | op_load2u - process a LOAD2U opcode |
| 3589 | -------------------------------------------------*/ |
| 3590 | |
| 3591 | static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3592 | { |
| 3593 | drcuml_parameter dstp, basep, indp; |
| 3594 | int basereg, dstreg; |
| 3595 | INT32 baseoffs; |
| 3596 | |
| 3597 | /* validate instruction */ |
| 3598 | assert(inst->size == 4 || inst->size == 8); |
| 3599 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3600 | |
| 3601 | /* normalize parameters */ |
| 3602 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3603 | |
| 3604 | /* pick a target register for the general case */ |
| 3605 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3606 | |
| 3607 | /* determine the pointer base */ |
| 3608 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3609 | |
| 3610 | /* immediate index */ |
| 3611 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3612 | emit_movzx_r32_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value)); // movzx dstreg,[basep + 2*indp] |
| 3613 | |
| 3614 | /* other index */ |
| 3615 | else |
| 3616 | { |
| 3617 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3618 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3619 | emit_movzx_r32_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs)); // movzx dstreg,[basep + 2*indp] |
| 3620 | } |
| 3621 | |
| 3622 | /* store to appropriate target size */ |
| 3623 | if (inst->size == 4) |
| 3624 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3625 | else if (inst->size == 8) |
| 3626 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3627 | |
| 3628 | return dst; |
| 3629 | } |
| 3630 | |
| 3631 | |
| 3632 | /*------------------------------------------------- |
| 3633 | op_load2s - process a LOAD2S opcode |
| 3634 | -------------------------------------------------*/ |
| 3635 | |
| 3636 | static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3637 | { |
| 3638 | drcuml_parameter dstp, basep, indp; |
| 3639 | int basereg, dstreg; |
| 3640 | INT32 baseoffs; |
| 3641 | |
| 3642 | /* validate instruction */ |
| 3643 | assert(inst->size == 4 || inst->size == 8); |
| 3644 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3645 | |
| 3646 | /* normalize parameters */ |
| 3647 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3648 | |
| 3649 | /* pick a target register for the general case */ |
| 3650 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3651 | |
| 3652 | /* determine the pointer base */ |
| 3653 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3654 | |
| 3655 | /* 32-bit form */ |
| 3656 | if (inst->size == 4) |
| 3657 | { |
| 3658 | /* immediate index */ |
| 3659 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3660 | emit_movsx_r32_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value)); // movsx eax,[basep + 2*indp] |
| 3661 | |
| 3662 | /* other index */ |
| 3663 | else |
| 3664 | { |
| 3665 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3666 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3667 | emit_movsx_r32_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs)); // movsx eax,[basep + 2*indp] |
| 3668 | } |
| 3669 | |
| 3670 | /* general case */ |
| 3671 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3672 | } |
| 3673 | |
| 3674 | /* 64-bit form */ |
| 3675 | else |
| 3676 | { |
| 3677 | /* immediate index */ |
| 3678 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3679 | emit_movsx_r64_m16(&dst, dstreg, MBD(basereg, baseoffs + 2*indp.value)); // movsx rax,[basep + 2*indp] |
| 3680 | |
| 3681 | /* other index */ |
| 3682 | else |
| 3683 | { |
| 3684 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3685 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3686 | emit_movsx_r64_m16(&dst, dstreg, MBISD(basereg, indreg, 2, baseoffs)); // movsx rax,[basep + 2*indp] |
| 3687 | } |
| 3688 | |
| 3689 | /* general case */ |
| 3690 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3691 | } |
| 3692 | return dst; |
| 3693 | } |
| 3694 | |
| 3695 | |
| 3696 | /*------------------------------------------------- |
| 3697 | op_load4u - process a LOAD4U opcode |
| 3698 | -------------------------------------------------*/ |
| 3699 | |
| 3700 | static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3701 | { |
| 3702 | drcuml_parameter dstp, basep, indp; |
| 3703 | int basereg, dstreg; |
| 3704 | INT32 baseoffs; |
| 3705 | |
| 3706 | /* validate instruction */ |
| 3707 | assert(inst->size == 4 || inst->size == 8); |
| 3708 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3709 | |
| 3710 | /* normalize parameters */ |
| 3711 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3712 | |
| 3713 | /* pick a target register for the general case */ |
| 3714 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3715 | |
| 3716 | /* determine the pointer base */ |
| 3717 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3718 | |
| 3719 | /* immediate index */ |
| 3720 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3721 | emit_mov_r32_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value)); // mov dstreg,[basep + 4*indp] |
| 3722 | |
| 3723 | /* other index */ |
| 3724 | else |
| 3725 | { |
| 3726 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3727 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3728 | emit_mov_r32_m32(&dst, dstreg, MBISD(basereg, indreg, 4, baseoffs)); // mov dstreg,[basep + 4*indp] |
| 3729 | } |
| 3730 | |
| 3731 | /* store to appropriate target size */ |
| 3732 | if (inst->size == 4) |
| 3733 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3734 | else if (inst->size == 8) |
| 3735 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3736 | |
| 3737 | return dst; |
| 3738 | } |
| 3739 | |
| 3740 | |
| 3741 | /*------------------------------------------------- |
| 3742 | op_load4s - process a LOAD4S opcode |
| 3743 | -------------------------------------------------*/ |
| 3744 | |
| 3745 | static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3746 | { |
| 3747 | drcuml_parameter dstp, basep, indp; |
| 3748 | int basereg, dstreg; |
| 3749 | INT32 baseoffs; |
| 3750 | |
| 3751 | /* validate instruction */ |
| 3752 | assert(inst->size == 8); |
| 3753 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3754 | |
| 3755 | /* normalize parameters */ |
| 3756 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3757 | |
| 3758 | /* pick a target register for the general case */ |
| 3759 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3760 | |
| 3761 | /* determine the pointer base */ |
| 3762 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3763 | |
| 3764 | /* immediate index */ |
| 3765 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3766 | emit_movsxd_r64_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value)); // mov dstreg,[basep + 4*indp] |
| 3767 | |
| 3768 | /* other index */ |
| 3769 | else |
| 3770 | { |
| 3771 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3772 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3773 | emit_movsxd_r64_m32(&dst, dstreg, MISD(indreg, 4, basep.value)); // movsx dstreg,[basep + 4*indp] |
| 3774 | } |
| 3775 | |
| 3776 | /* general case */ |
| 3777 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3778 | |
| 3779 | return dst; |
| 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | /*------------------------------------------------- |
| 3784 | op_load8u - process a LOAD8U opcode |
| 3785 | -------------------------------------------------*/ |
| 3786 | |
| 3787 | static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3788 | { |
| 3789 | drcuml_parameter dstp, basep, indp; |
| 3790 | int basereg, dstreg; |
| 3791 | INT32 baseoffs; |
| 3792 | |
| 3793 | /* validate instruction */ |
| 3794 | assert(inst->size == 8); |
| 3795 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3796 | |
| 3797 | /* normalize parameters */ |
| 3798 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3799 | |
| 3800 | /* pick a target register for the general case */ |
| 3801 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3802 | |
| 3803 | /* determine the pointer base */ |
| 3804 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3805 | |
| 3806 | /* immediate index */ |
| 3807 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3808 | emit_mov_r64_m64(&dst, dstreg, MBD(basereg, baseoffs + 8*indp.value)); // mov dstreg,[basep + 8*indp] |
| 3809 | |
| 3810 | /* other index */ |
| 3811 | else |
| 3812 | { |
| 3813 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3814 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3815 | emit_mov_r64_m64(&dst, dstreg, MBISD(basereg, indreg, 8, baseoffs)); // mov dstreg,[basep + 8*indp] |
| 3816 | } |
| 3817 | |
| 3818 | /* general case */ |
| 3819 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,edx:dstreg |
| 3820 | |
| 3821 | return dst; |
| 3822 | } |
| 3823 | |
| 3824 | |
| 3825 | /*------------------------------------------------- |
| 3826 | op_store1 - process a STORE1 opcode |
| 3827 | -------------------------------------------------*/ |
| 3828 | |
| 3829 | static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3830 | { |
| 3831 | drcuml_parameter srcp, basep, indp; |
| 3832 | int basereg, srcreg; |
| 3833 | INT32 baseoffs; |
| 3834 | |
| 3835 | /* validate instruction */ |
| 3836 | assert(inst->size == 4 || inst->size == 8); |
| 3837 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3838 | |
| 3839 | /* normalize parameters */ |
| 3840 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3841 | |
| 3842 | /* pick a source register for the general case */ |
| 3843 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3844 | |
| 3845 | /* determine the pointer base */ |
| 3846 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3847 | |
| 3848 | /* degenerate case: constant index */ |
| 3849 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3850 | { |
| 3851 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3852 | emit_mov_m8_imm(&dst, MBD(basereg, baseoffs + 1*indp.value), srcp.value); // mov [basep + 1*indp],srcp |
| 3853 | else |
| 3854 | { |
| 3855 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3856 | emit_mov_m8_r8(&dst, MBD(basereg, baseoffs + 1*indp.value), srcreg); // mov [basep + 1*indp],srcreg |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | /* normal case: variable index */ |
| 3861 | else |
| 3862 | { |
| 3863 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3864 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3865 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3866 | emit_mov_m8_imm(&dst, MBISD(basereg, indreg, 1, baseoffs), srcp.value); // mov [basep + 1*ecx],srcp |
| 3867 | else |
| 3868 | { |
| 3869 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3870 | emit_mov_m8_r8(&dst, MBISD(basereg, indreg, 1, baseoffs), srcreg); // mov [basep + 1*ecx],srcreg |
| 3871 | } |
| 3872 | } |
| 3873 | return dst; |
| 3874 | } |
| 3875 | |
| 3876 | |
| 3877 | /*------------------------------------------------- |
| 3878 | op_store2 - process a STORE2 opcode |
| 3879 | -------------------------------------------------*/ |
| 3880 | |
| 3881 | static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3882 | { |
| 3883 | drcuml_parameter srcp, basep, indp; |
| 3884 | int basereg, srcreg; |
| 3885 | INT32 baseoffs; |
| 3886 | |
| 3887 | /* validate instruction */ |
| 3888 | assert(inst->size == 4 || inst->size == 8); |
| 3889 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3890 | |
| 3891 | /* normalize parameters */ |
| 3892 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3893 | |
| 3894 | /* pick a source register for the general case */ |
| 3895 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3896 | |
| 3897 | /* determine the pointer base */ |
| 3898 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3899 | |
| 3900 | /* degenerate case: constant index */ |
| 3901 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3902 | { |
| 3903 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3904 | emit_mov_m16_imm(&dst, MBD(basereg, baseoffs + 2*indp.value), srcp.value); // mov [basep + 2*indp],srcp |
| 3905 | else |
| 3906 | { |
| 3907 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3908 | emit_mov_m16_r16(&dst, MBD(basereg, baseoffs + 2*indp.value), srcreg); // mov [basep + 2*indp],srcreg |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | /* normal case: variable index */ |
| 3913 | else |
| 3914 | { |
| 3915 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3916 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3917 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3918 | emit_mov_m16_imm(&dst, MBISD(basereg, indreg, 2, baseoffs), srcp.value); // mov [basep + 2*ecx],srcp |
| 3919 | else |
| 3920 | { |
| 3921 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3922 | emit_mov_m16_r16(&dst, MBISD(basereg, indreg, 2, baseoffs), srcreg); // mov [basep + 2*ecx],srcreg |
| 3923 | } |
| 3924 | } |
| 3925 | return dst; |
| 3926 | } |
| 3927 | |
| 3928 | |
| 3929 | /*------------------------------------------------- |
| 3930 | op_store4 - process a STORE4 opcode |
| 3931 | -------------------------------------------------*/ |
| 3932 | |
| 3933 | static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3934 | { |
| 3935 | drcuml_parameter srcp, basep, indp; |
| 3936 | int basereg, srcreg; |
| 3937 | INT32 baseoffs; |
| 3938 | |
| 3939 | /* validate instruction */ |
| 3940 | assert(inst->size == 4 || inst->size == 8); |
| 3941 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3942 | |
| 3943 | /* normalize parameters */ |
| 3944 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3945 | |
| 3946 | /* pick a source register for the general case */ |
| 3947 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3948 | |
| 3949 | /* determine the pointer base */ |
| 3950 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 3951 | |
| 3952 | /* degenerate case: constant index */ |
| 3953 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3954 | { |
| 3955 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3956 | emit_mov_m32_imm(&dst, MBD(basereg, baseoffs + 4*indp.value), srcp.value); // mov [basep + 4*indp],srcp |
| 3957 | else |
| 3958 | { |
| 3959 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3960 | emit_mov_m32_r32(&dst, MBD(basereg, baseoffs + 4*indp.value), srcreg); // mov [basep + 4*indp],srcreg |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | /* normal case: variable index */ |
| 3965 | else |
| 3966 | { |
| 3967 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3968 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3969 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3970 | emit_mov_m32_imm(&dst, MBISD(basereg, indreg, 4, baseoffs), srcp.value); // mov [basep + 4*ecx],srcp |
| 3971 | else |
| 3972 | { |
| 3973 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3974 | emit_mov_m32_r32(&dst, MBISD(basereg, indreg, 4, baseoffs), srcreg); // mov [basep + 4*ecx],srcreg |
| 3975 | } |
| 3976 | } |
| 3977 | return dst; |
| 3978 | } |
| 3979 | |
| 3980 | |
| 3981 | /*------------------------------------------------- |
| 3982 | op_store8 - process a STORE8 opcode |
| 3983 | -------------------------------------------------*/ |
| 3984 | |
| 3985 | static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3986 | { |
| 3987 | drcuml_parameter srcp, basep, indp; |
| 3988 | int basereg, srcreg; |
| 3989 | INT32 baseoffs; |
| 3990 | |
| 3991 | /* validate instruction */ |
| 3992 | assert(inst->size == 8); |
| 3993 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3994 | |
| 3995 | /* normalize parameters */ |
| 3996 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3997 | |
| 3998 | /* pick a source register for the general case */ |
| 3999 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 4000 | |
| 4001 | /* determine the pointer base */ |
| 4002 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 4003 | |
| 4004 | /* degenerate case: constant index */ |
| 4005 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4006 | { |
| 4007 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value)) |
| 4008 | emit_mov_m64_imm(&dst, MBD(basereg, baseoffs + 8*indp.value), srcp.value); // mov [basep + 8*indp],srcp |
| 4009 | else |
| 4010 | { |
| 4011 | emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 4012 | emit_mov_m64_r64(&dst, MBD(basereg, baseoffs + 8*indp.value), srcreg); // mov [basep + 8*indp],srcreg |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | /* normal case: variable index */ |
| 4017 | else |
| 4018 | { |
| 4019 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 4020 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 4021 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value)) |
| 4022 | emit_mov_m64_imm(&dst, MBISD(basereg, indreg, 8, baseoffs), srcp.value); // mov [basep + 8*ecx],srcp |
| 4023 | else |
| 4024 | { |
| 4025 | emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 4026 | emit_mov_m64_r64(&dst, MBISD(basereg, indreg, 8, baseoffs), srcreg); // mov [basep + 8*ecx],srcreg |
| 4027 | } |
| 4028 | } |
| 4029 | return dst; |
| 4030 | } |
| 4031 | |
| 4032 | |
| 4033 | /*------------------------------------------------- |
| 4034 | op_read1u - process a READ1U opcode |
| 4035 | -------------------------------------------------*/ |
| 4036 | |
| 4037 | static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4038 | { |
| 4039 | drcuml_parameter dstp, spacep, addrp; |
| 4040 | int dstreg; |
| 4041 | |
| 4042 | /* validate instruction */ |
| 4043 | assert(inst->size == 4 || inst->size == 8); |
| 4044 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4045 | |
| 4046 | /* normalize parameters */ |
| 4047 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4048 | |
| 4049 | /* pick a target register for the general case */ |
| 4050 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4051 | |
| 4052 | /* set up a call to the read byte handler */ |
| 4053 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4054 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_byte);// call read_byte |
| 4055 | emit_movzx_r32_r8(&dst, dstreg, REG_AL); // movzx dstreg,al |
| 4056 | |
| 4057 | /* store to appropriate target size */ |
| 4058 | if (inst->size == 4) |
| 4059 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4060 | else if (inst->size == 8) |
| 4061 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4062 | |
| 4063 | return dst; |
| 4064 | } |
| 4065 | |
| 4066 | |
| 4067 | /*------------------------------------------------- |
| 4068 | op_read1s - process a READ1S opcode |
| 4069 | -------------------------------------------------*/ |
| 4070 | |
| 4071 | static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4072 | { |
| 4073 | drcuml_parameter dstp, spacep, addrp; |
| 4074 | int dstreg; |
| 4075 | |
| 4076 | /* validate instruction */ |
| 4077 | assert(inst->size == 4 || inst->size == 8); |
| 4078 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4079 | |
| 4080 | /* normalize parameters */ |
| 4081 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4082 | |
| 4083 | /* pick a target register for the general case */ |
| 4084 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4085 | |
| 4086 | /* set up a call to the read byte handler */ |
| 4087 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4088 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_byte);// call read_byte |
| 4089 | |
| 4090 | /* 32-bit form */ |
| 4091 | if (inst->size == 4) |
| 4092 | { |
| 4093 | /* general case */ |
| 4094 | emit_movsx_r32_r8(&dst, dstreg, REG_AL); // movsx dstreg,al |
| 4095 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4096 | } |
| 4097 | |
| 4098 | /* 64-bit form */ |
| 4099 | else |
| 4100 | { |
| 4101 | /* general case */ |
| 4102 | emit_movsx_r64_r8(&dst, dstreg, REG_AL); // movsx dstreg,al |
| 4103 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4104 | } |
| 4105 | return dst; |
| 4106 | } |
| 4107 | |
| 4108 | |
| 4109 | /*------------------------------------------------- |
| 4110 | op_read2u - process a READ2U opcode |
| 4111 | -------------------------------------------------*/ |
| 4112 | |
| 4113 | static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4114 | { |
| 4115 | drcuml_parameter dstp, spacep, addrp; |
| 4116 | int dstreg; |
| 4117 | |
| 4118 | /* validate instruction */ |
| 4119 | assert(inst->size == 4 || inst->size == 8); |
| 4120 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4121 | |
| 4122 | /* normalize parameters */ |
| 4123 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4124 | |
| 4125 | /* pick a target register for the general case */ |
| 4126 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4127 | |
| 4128 | /* set up a call to the read word handler */ |
| 4129 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4130 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word);// call read_word |
| 4131 | emit_movzx_r32_r16(&dst, dstreg, REG_AX); // movzx dstreg,ax |
| 4132 | |
| 4133 | /* store to appropriate target size */ |
| 4134 | if (inst->size == 4) |
| 4135 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4136 | else if (inst->size == 8) |
| 4137 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4138 | |
| 4139 | return dst; |
| 4140 | } |
| 4141 | |
| 4142 | |
| 4143 | /*------------------------------------------------- |
| 4144 | op_read2s - process a READ2S opcode |
| 4145 | -------------------------------------------------*/ |
| 4146 | |
| 4147 | static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4148 | { |
| 4149 | drcuml_parameter dstp, spacep, addrp; |
| 4150 | int dstreg; |
| 4151 | |
| 4152 | /* validate instruction */ |
| 4153 | assert(inst->size == 4 || inst->size == 8); |
| 4154 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4155 | |
| 4156 | /* normalize parameters */ |
| 4157 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4158 | |
| 4159 | /* pick a target register for the general case */ |
| 4160 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4161 | |
| 4162 | /* set up a call to the read word handler */ |
| 4163 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4164 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word);// call read_word |
| 4165 | |
| 4166 | /* 32-bit form */ |
| 4167 | if (inst->size == 4) |
| 4168 | { |
| 4169 | /* general case */ |
| 4170 | emit_movsx_r32_r16(&dst, dstreg, REG_AX); // movsx dstreg,ax |
| 4171 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4172 | } |
| 4173 | |
| 4174 | /* 64-bit form */ |
| 4175 | else |
| 4176 | { |
| 4177 | /* general case */ |
| 4178 | emit_movsx_r64_r16(&dst, dstreg, REG_AX); // movsx dstreg,ax |
| 4179 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4180 | } |
| 4181 | return dst; |
| 4182 | } |
| 4183 | |
| 4184 | |
| 4185 | /*------------------------------------------------- |
| 4186 | op_read2m - process a READ2M opcode |
| 4187 | -------------------------------------------------*/ |
| 4188 | |
| 4189 | static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4190 | { |
| 4191 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4192 | int dstreg; |
| 4193 | |
| 4194 | /* validate instruction */ |
| 4195 | assert(inst->size == 4 || inst->size == 8); |
| 4196 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4197 | |
| 4198 | /* normalize parameters */ |
| 4199 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4200 | |
| 4201 | /* pick a target register for the general case */ |
| 4202 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4203 | |
| 4204 | /* set up a call to the read dword masked handler */ |
| 4205 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4206 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &maskp); // mov param1,maskp |
| 4207 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_word_masked);// call read_word_masked |
| 4208 | emit_movzx_r32_r16(&dst, dstreg, REG_AX); // movzx dstreg,ax |
| 4209 | |
| 4210 | /* store to appropriate target size */ |
| 4211 | if (inst->size == 4) |
| 4212 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4213 | else if (inst->size == 8) |
| 4214 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4215 | |
| 4216 | return dst; |
| 4217 | } |
| 4218 | |
| 4219 | |
| 4220 | /*------------------------------------------------- |
| 4221 | op_read4u - process a READ4U opcode |
| 4222 | -------------------------------------------------*/ |
| 4223 | |
| 4224 | static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4225 | { |
| 4226 | drcuml_parameter dstp, spacep, addrp; |
| 4227 | |
| 4228 | /* validate instruction */ |
| 4229 | assert(inst->size == 4 || inst->size == 8); |
| 4230 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4231 | |
| 4232 | /* normalize parameters */ |
| 4233 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4234 | |
| 4235 | /* set up a call to the read dword handler */ |
| 4236 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4237 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call read_dword |
| 4238 | |
| 4239 | /* store to appropriate target size */ |
| 4240 | if (inst->size == 4) |
| 4241 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 4242 | else if (inst->size == 8) |
| 4243 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 4244 | |
| 4245 | return dst; |
| 4246 | } |
| 4247 | |
| 4248 | |
| 4249 | /*------------------------------------------------- |
| 4250 | op_read4s - process a READ4S opcode |
| 4251 | -------------------------------------------------*/ |
| 4252 | |
| 4253 | static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4254 | { |
| 4255 | drcuml_parameter dstp, spacep, addrp; |
| 4256 | int dstreg; |
| 4257 | |
| 4258 | /* validate instruction */ |
| 4259 | assert(inst->size == 8); |
| 4260 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4261 | |
| 4262 | /* normalize parameters */ |
| 4263 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4264 | |
| 4265 | /* pick a target register for the general case */ |
| 4266 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4267 | |
| 4268 | /* set up a call to the read dword handler */ |
| 4269 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4270 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call read_dword |
| 4271 | |
| 4272 | /* 64-bit form */ |
| 4273 | emit_movsxd_r64_r32(&dst, dstreg, REG_EAX); // movsxd dstreg,eax |
| 4274 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4275 | |
| 4276 | return dst; |
| 4277 | } |
| 4278 | |
| 4279 | |
| 4280 | /*------------------------------------------------- |
| 4281 | op_read4m - process a READ4M opcode |
| 4282 | -------------------------------------------------*/ |
| 4283 | |
| 4284 | static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4285 | { |
| 4286 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4287 | |
| 4288 | /* validate instruction */ |
| 4289 | assert(inst->size == 4 || inst->size == 8); |
| 4290 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4291 | |
| 4292 | /* normalize parameters */ |
| 4293 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4294 | |
| 4295 | /* set up a call to the read dword masked handler */ |
| 4296 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4297 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &maskp); // mov param2,maskp |
| 4298 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword_masked);// call read_dword_masked |
| 4299 | |
| 4300 | /* store to appropriate target size */ |
| 4301 | if (inst->size == 4) |
| 4302 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 4303 | else if (inst->size == 8) |
| 4304 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 4305 | |
| 4306 | return dst; |
| 4307 | } |
| 4308 | |
| 4309 | |
| 4310 | /*------------------------------------------------- |
| 4311 | op_read8u - process a READ8U opcode |
| 4312 | -------------------------------------------------*/ |
| 4313 | |
| 4314 | static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4315 | { |
| 4316 | drcuml_parameter dstp, spacep, addrp; |
| 4317 | |
| 4318 | /* validate instruction */ |
| 4319 | assert(inst->size == 8); |
| 4320 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4321 | |
| 4322 | /* normalize parameters */ |
| 4323 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4324 | |
| 4325 | /* set up a call to the read qword handler */ |
| 4326 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4327 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword);// call read_qword |
| 4328 | |
| 4329 | /* 64-bit form */ |
| 4330 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 4331 | |
| 4332 | return dst; |
| 4333 | } |
| 4334 | |
| 4335 | |
| 4336 | /*------------------------------------------------- |
| 4337 | op_read8m - process a READ8M opcode |
| 4338 | -------------------------------------------------*/ |
| 4339 | |
| 4340 | static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4341 | { |
| 4342 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4343 | |
| 4344 | /* validate instruction */ |
| 4345 | assert(inst->size == 8); |
| 4346 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4347 | |
| 4348 | /* normalize parameters */ |
| 4349 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4350 | |
| 4351 | /* set up a call to the read qword masked handler */ |
| 4352 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4353 | emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &maskp); // mov param2,maskp |
| 4354 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword_masked);// call read_qword_masked |
| 4355 | |
| 4356 | /* 64-bit form */ |
| 4357 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 4358 | |
| 4359 | return dst; |
| 4360 | } |
| 4361 | |
| 4362 | |
| 4363 | /*------------------------------------------------- |
| 4364 | op_write1 - process a WRITE1 opcode |
| 4365 | -------------------------------------------------*/ |
| 4366 | |
| 4367 | static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4368 | { |
| 4369 | drcuml_parameter spacep, addrp, srcp; |
| 4370 | |
| 4371 | /* validate instruction */ |
| 4372 | assert(inst->size == 4 || inst->size == 8); |
| 4373 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4374 | |
| 4375 | /* normalize parameters */ |
| 4376 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4377 | |
| 4378 | /* set up a call to the write byte handler */ |
| 4379 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4380 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4381 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_byte);// call write_byte |
| 4382 | |
| 4383 | return dst; |
| 4384 | } |
| 4385 | |
| 4386 | |
| 4387 | /*------------------------------------------------- |
| 4388 | op_write2 - process a WRITE2 opcode |
| 4389 | -------------------------------------------------*/ |
| 4390 | |
| 4391 | static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4392 | { |
| 4393 | drcuml_parameter spacep, addrp, srcp; |
| 4394 | |
| 4395 | /* validate instruction */ |
| 4396 | assert(inst->size == 4 || inst->size == 8); |
| 4397 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4398 | |
| 4399 | /* normalize parameters */ |
| 4400 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4401 | |
| 4402 | /* set up a call to the write word handler */ |
| 4403 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4404 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4405 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_word);// call write_word |
| 4406 | |
| 4407 | return dst; |
| 4408 | } |
| 4409 | |
| 4410 | |
| 4411 | /*------------------------------------------------- |
| 4412 | op_writ2m - process a WRIT2M opcode |
| 4413 | -------------------------------------------------*/ |
| 4414 | |
| 4415 | static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4416 | { |
| 4417 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4418 | |
| 4419 | /* validate instruction */ |
| 4420 | assert(inst->size == 4 || inst->size == 8); |
| 4421 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4422 | |
| 4423 | /* normalize parameters */ |
| 4424 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4425 | |
| 4426 | /* set up a call to the write word handler */ |
| 4427 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4428 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4429 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM3, &maskp); // mov param3,maskp |
| 4430 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_word_masked);// call write_word_masked |
| 4431 | return dst; |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | /*------------------------------------------------- |
| 4436 | op_write4 - process a WRITE4 opcode |
| 4437 | -------------------------------------------------*/ |
| 4438 | |
| 4439 | static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4440 | { |
| 4441 | drcuml_parameter spacep, addrp, srcp; |
| 4442 | |
| 4443 | /* validate instruction */ |
| 4444 | assert(inst->size == 4 || inst->size == 8); |
| 4445 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4446 | |
| 4447 | /* normalize parameters */ |
| 4448 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4449 | |
| 4450 | /* set up a call to the write dword handler */ |
| 4451 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4452 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4453 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword);// call write_dword |
| 4454 | |
| 4455 | return dst; |
| 4456 | } |
| 4457 | |
| 4458 | |
| 4459 | /*------------------------------------------------- |
| 4460 | op_writ4m - process a WRIT4M opcode |
| 4461 | -------------------------------------------------*/ |
| 4462 | |
| 4463 | static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4464 | { |
| 4465 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4466 | |
| 4467 | /* validate instruction */ |
| 4468 | assert(inst->size == 4 || inst->size == 8); |
| 4469 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4470 | |
| 4471 | /* normalize parameters */ |
| 4472 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4473 | |
| 4474 | /* set up a call to the write word handler */ |
| 4475 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4476 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4477 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM3, &maskp); // mov param3,maskp |
| 4478 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword_masked);// call write_dword_masked |
| 4479 | return dst; |
| 4480 | } |
| 4481 | |
| 4482 | |
| 4483 | /*------------------------------------------------- |
| 4484 | op_write8 - process a WRITE8 opcode |
| 4485 | -------------------------------------------------*/ |
| 4486 | |
| 4487 | static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4488 | { |
| 4489 | drcuml_parameter spacep, addrp, srcp; |
| 4490 | |
| 4491 | /* validate instruction */ |
| 4492 | assert(inst->size == 8); |
| 4493 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4494 | |
| 4495 | /* normalize parameters */ |
| 4496 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4497 | |
| 4498 | /* set up a call to the write qword handler */ |
| 4499 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4500 | emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4501 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword);// call write_qword |
| 4502 | |
| 4503 | return dst; |
| 4504 | } |
| 4505 | |
| 4506 | |
| 4507 | /*------------------------------------------------- |
| 4508 | op_writ8m - process a WRIT8M opcode |
| 4509 | -------------------------------------------------*/ |
| 4510 | |
| 4511 | static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4512 | { |
| 4513 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4514 | |
| 4515 | /* validate instruction */ |
| 4516 | assert(inst->size == 8); |
| 4517 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4518 | |
| 4519 | /* normalize parameters */ |
| 4520 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4521 | |
| 4522 | /* set up a call to the write word handler */ |
| 4523 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 4524 | emit_mov_r64_p64(drcbe, &dst, REG_PARAM2, &srcp); // mov param2,srcp |
| 4525 | emit_mov_r64_p64(drcbe, &dst, REG_PARAM3, &maskp); // mov param3,maskp |
| 4526 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword_masked);// call write_qword_masked |
| 4527 | return dst; |
| 4528 | } |
| 4529 | |
| 4530 | |
| 4531 | /*------------------------------------------------- |
| 4532 | op_flags - process a FLAGS opcode |
| 4533 | -------------------------------------------------*/ |
| 4534 | |
| 4535 | static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4536 | { |
| 4537 | drcuml_parameter dstp, maskp, tablep; |
| 4538 | int dstreg; |
| 4539 | |
| 4540 | /* validate instruction */ |
| 4541 | assert(inst->size == 4 || inst->size == 8); |
| 4542 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4543 | |
| 4544 | /* normalize parameters */ |
| 4545 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &maskp, PTYPE_I, &tablep, PTYPE_M); |
| 4546 | |
| 4547 | /* pick a target register for the general case */ |
| 4548 | dstreg = param_select_register(REG_ECX, &dstp, NULL); |
| 4549 | |
| 4550 | /* translate live flags into UML flags */ |
| 4551 | emit_pushf(&dst); // pushf |
| 4552 | emit_pop_r64(&dst, REG_RAX); // pop rax |
| 4553 | emit_and_r32_imm(&dst, REG_EAX, 0x8c5); // and eax,0x8c5 |
| 4554 | emit_movzx_r32_m8(&dst, REG_EAX, MBISD(REG_RBP, REG_RAX, 1, offset_from_rbp(drcbe, (FPTR)&drcbe->flagsmap[0]))); |
| 4555 | // movzx eax,[flags_map] |
| 4556 | |
| 4557 | /* 32-bit form */ |
| 4558 | if (inst->size == 4) |
| 4559 | { |
| 4560 | /* no masking */ |
| 4561 | if (maskp.value == 0xffffffff) |
| 4562 | { |
| 4563 | emit_mov_r32_m32(&dst, dstreg, MBISD(REG_RBP, REG_EAX, 4, offset_from_rbp(drcbe, tablep.value))); |
| 4564 | // mov dstreg,[eax*4+table] |
| 4565 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4566 | } |
| 4567 | |
| 4568 | /* general case */ |
| 4569 | else |
| 4570 | { |
| 4571 | emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp); // mov dstreg,dstp |
| 4572 | emit_mov_r32_m32(&dst, REG_EDX, MBISD(REG_RBP, REG_EAX, 4, offset_from_rbp(drcbe, tablep.value))); |
| 4573 | // mov edx,[eax*4+table] |
| 4574 | emit_and_r32_imm(&dst, dstreg, ~maskp.value); // and dstreg,~mask |
| 4575 | emit_and_r32_imm(&dst, REG_EDX, maskp.value); // and edx,mask |
| 4576 | emit_or_r32_r32(&dst, dstreg, REG_EDX); // or dstreg,edx |
| 4577 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | /* 64-bit form */ |
| 4582 | else |
| 4583 | { |
| 4584 | /* no masking */ |
| 4585 | if (maskp.value == U64(0xffffffffffffffff)) |
| 4586 | { |
| 4587 | emit_mov_r64_m64(&dst, dstreg, MBISD(REG_RBP, REG_EAX, 8, offset_from_rbp(drcbe, tablep.value))); |
| 4588 | // mov dstreg,[eax*8+table] |
| 4589 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4590 | } |
| 4591 | |
| 4592 | /* general case */ |
| 4593 | else |
| 4594 | { |
| 4595 | emit_mov_r64_p64(drcbe, &dst, dstreg, &dstp); // mov dstreg,dstp |
| 4596 | emit_mov_r64_m64(&dst, REG_EDX, MBISD(REG_RBP, REG_EAX, 8, offset_from_rbp(drcbe, tablep.value))); |
| 4597 | // mov edx,[eax*84+table] |
| 4598 | if (short_immediate(~maskp.value)) |
| 4599 | emit_and_r64_imm(&dst, dstreg, ~maskp.value); // and dstreg,~mask |
| 4600 | else |
| 4601 | { |
| 4602 | emit_mov_r64_imm(&dst, REG_R11, ~maskp.value); // mov r11,~mask |
| 4603 | emit_and_r64_r64(&dst, dstreg, REG_R11); // and dstreg,r11 |
| 4604 | } |
| 4605 | if (short_immediate(maskp.value)) |
| 4606 | emit_and_r64_imm(&dst, REG_EDX, maskp.value); // and edx,mask |
| 4607 | else |
| 4608 | { |
| 4609 | emit_mov_r64_imm(&dst, REG_R11, maskp.value); // mov r11,mask |
| 4610 | emit_and_r64_r64(&dst, REG_EDX, REG_R11); // and edx,r11 |
| 4611 | } |
| 4612 | emit_or_r64_r64(&dst, dstreg, REG_EDX); // or dstreg,edx |
| 4613 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4614 | } |
| 4615 | } |
| 4616 | return dst; |
| 4617 | } |
| 4618 | |
| 4619 | |
| 4620 | /*------------------------------------------------- |
| 4621 | op_mov - process a MOV opcode |
| 4622 | -------------------------------------------------*/ |
| 4623 | |
| 4624 | static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4625 | { |
| 4626 | drcuml_parameter dstp, srcp; |
| 4627 | x86code *savedst = dst; |
| 4628 | emit_link skip = { 0 }; |
| 4629 | int dstreg; |
| 4630 | |
| 4631 | /* validate instruction */ |
| 4632 | assert(inst->size == 4 || inst->size == 8); |
| 4633 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 4634 | |
| 4635 | /* normalize parameters, but only if we got here directly */ |
| 4636 | /* other opcodes call through here with pre-normalized parameters */ |
| 4637 | if (inst->opcode == DRCUML_OP_MOV) |
| 4638 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4639 | else |
| 4640 | { |
| 4641 | dstp = inst->param[0]; |
| 4642 | srcp = inst->param[1]; |
| 4643 | } |
| 4644 | |
| 4645 | /* degenerate case: dest and source are equal */ |
| 4646 | if (dstp.type == srcp.type && dstp.value == srcp.value) |
| 4647 | return dst; |
| 4648 | |
| 4649 | /* pick a target register for the general case */ |
| 4650 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4651 | |
| 4652 | /* always start with a jmp */ |
| 4653 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 4654 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 4655 | |
| 4656 | /* 32-bit form */ |
| 4657 | if (inst->size == 4) |
| 4658 | { |
| 4659 | /* register to memory */ |
| 4660 | if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4661 | emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), srcp.value); // mov [dstp],srcp |
| 4662 | |
| 4663 | /* immediate to memory */ |
| 4664 | else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4665 | emit_mov_m32_imm(&dst, MABS(drcbe, dstp.value), srcp.value); // mov [dstp],srcp |
| 4666 | |
| 4667 | /* conditional memory to register */ |
| 4668 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY) |
| 4669 | { |
| 4670 | dst = savedst; |
| 4671 | skip.target = NULL; |
| 4672 | emit_cmovcc_r32_m32(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(drcbe, srcp.value)); |
| 4673 | // cmovcc dstp,[srcp] |
| 4674 | } |
| 4675 | |
| 4676 | /* conditional register to register */ |
| 4677 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4678 | { |
| 4679 | dst = savedst; |
| 4680 | skip.target = NULL; |
| 4681 | emit_cmovcc_r32_r32(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value); |
| 4682 | // cmovcc dstp,srcp |
| 4683 | } |
| 4684 | |
| 4685 | /* general case */ |
| 4686 | else |
| 4687 | { |
| 4688 | emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp); // mov dstreg,srcp |
| 4689 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | /* 64-bit form */ |
| 4694 | else if (inst->size == 8) |
| 4695 | { |
| 4696 | /* register to memory */ |
| 4697 | if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4698 | emit_mov_m64_r64(&dst, MABS(drcbe, dstp.value), srcp.value); // mov [dstp],srcp |
| 4699 | |
| 4700 | /* immediate to memory */ |
| 4701 | else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(srcp.value)) |
| 4702 | emit_mov_m64_imm(&dst, MABS(drcbe, dstp.value), srcp.value); // mov [dstp],srcp |
| 4703 | |
| 4704 | /* conditional memory to register */ |
| 4705 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY) |
| 4706 | { |
| 4707 | dst = savedst; |
| 4708 | skip.target = NULL; |
| 4709 | emit_cmovcc_r64_m64(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(drcbe, srcp.value)); |
| 4710 | // cmovcc dstp,[srcp] |
| 4711 | } |
| 4712 | |
| 4713 | /* conditional register to register */ |
| 4714 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4715 | { |
| 4716 | dst = savedst; |
| 4717 | skip.target = NULL; |
| 4718 | emit_cmovcc_r64_r64(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value); |
| 4719 | // cmovcc dstp,srcp |
| 4720 | } |
| 4721 | |
| 4722 | /* general case */ |
| 4723 | else |
| 4724 | { |
| 4725 | emit_mov_r64_p64(drcbe, &dst, dstreg, &srcp); // mov dstreg,srcp |
| 4726 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | /* resolve the jump */ |
| 4731 | if (skip.target != NULL) |
| 4732 | resolve_link(&dst, &skip); |
| 4733 | return dst; |
| 4734 | } |
| 4735 | |
| 4736 | |
| 4737 | /*------------------------------------------------- |
| 4738 | op_zext1 - process a ZEXT1 opcode |
| 4739 | -------------------------------------------------*/ |
| 4740 | |
| 4741 | static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4742 | { |
| 4743 | drcuml_parameter dstp, srcp; |
| 4744 | int dstreg; |
| 4745 | |
| 4746 | /* validate instruction */ |
| 4747 | assert(inst->size == 4 || inst->size == 8); |
| 4748 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4749 | |
| 4750 | /* normalize parameters */ |
| 4751 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4752 | |
| 4753 | /* degenerate cases -- convert to a move */ |
| 4754 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4755 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT8)srcp.value); |
| 4756 | |
| 4757 | /* pick a target register for the general case */ |
| 4758 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4759 | |
| 4760 | /* general case */ |
| 4761 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4762 | emit_movzx_r32_m8(&dst, dstreg, MABS(drcbe, srcp.value)); // movzx dstreg,[srcp] |
| 4763 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4764 | emit_movzx_r32_r8(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4765 | |
| 4766 | /* 32-bit form */ |
| 4767 | if (inst->size == 4) |
| 4768 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4769 | |
| 4770 | /* 64-bit form */ |
| 4771 | else if (inst->size == 8) |
| 4772 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4773 | |
| 4774 | return dst; |
| 4775 | } |
| 4776 | |
| 4777 | |
| 4778 | /*------------------------------------------------- |
| 4779 | op_zext2 - process a ZEXT2 opcode |
| 4780 | -------------------------------------------------*/ |
| 4781 | |
| 4782 | static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4783 | { |
| 4784 | drcuml_parameter dstp, srcp; |
| 4785 | int dstreg; |
| 4786 | |
| 4787 | /* validate instruction */ |
| 4788 | assert(inst->size == 4 || inst->size == 8); |
| 4789 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4790 | |
| 4791 | /* normalize parameters */ |
| 4792 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4793 | |
| 4794 | /* degenerate cases -- convert to a move */ |
| 4795 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4796 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT16)srcp.value); |
| 4797 | |
| 4798 | /* pick a target register for the general case */ |
| 4799 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4800 | |
| 4801 | /* general case */ |
| 4802 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4803 | emit_movzx_r32_m16(&dst, dstreg, MABS(drcbe, srcp.value)); // movzx dstreg,[srcp] |
| 4804 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4805 | emit_movzx_r32_r16(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4806 | |
| 4807 | /* 32-bit form */ |
| 4808 | if (inst->size == 4) |
| 4809 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4810 | |
| 4811 | /* 64-bit form */ |
| 4812 | else if (inst->size == 8) |
| 4813 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4814 | |
| 4815 | return dst; |
| 4816 | } |
| 4817 | |
| 4818 | |
| 4819 | /*------------------------------------------------- |
| 4820 | op_zext4 - process a ZEXT4 opcode |
| 4821 | -------------------------------------------------*/ |
| 4822 | |
| 4823 | static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4824 | { |
| 4825 | drcuml_parameter dstp, srcp; |
| 4826 | int dstreg; |
| 4827 | |
| 4828 | /* validate instruction */ |
| 4829 | assert(inst->size == 8); |
| 4830 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4831 | |
| 4832 | /* normalize parameters */ |
| 4833 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4834 | |
| 4835 | /* degenerate cases -- convert to a move */ |
| 4836 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4837 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)srcp.value); |
| 4838 | |
| 4839 | /* pick a target register for the general case */ |
| 4840 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4841 | |
| 4842 | /* general case */ |
| 4843 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4844 | emit_mov_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // mov dstreg,[srcp] |
| 4845 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4846 | emit_mov_r32_r32(&dst, dstreg, srcp.value); // mov dstreg,srcp |
| 4847 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4848 | |
| 4849 | return dst; |
| 4850 | } |
| 4851 | |
| 4852 | |
| 4853 | /*------------------------------------------------- |
| 4854 | op_sext1 - process a SEXT1 opcode |
| 4855 | -------------------------------------------------*/ |
| 4856 | |
| 4857 | static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4858 | { |
| 4859 | drcuml_parameter dstp, srcp; |
| 4860 | int dstreg; |
| 4861 | |
| 4862 | /* validate instruction */ |
| 4863 | assert(inst->size == 4 || inst->size == 8); |
| 4864 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4865 | |
| 4866 | /* normalize parameters */ |
| 4867 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4868 | |
| 4869 | /* degenerate cases -- convert to a move */ |
| 4870 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4871 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT8)srcp.value); |
| 4872 | |
| 4873 | /* pick a target register for the general case */ |
| 4874 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4875 | |
| 4876 | /* convert 8-bit source registers */ |
| 4877 | if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4)) |
| 4878 | { |
| 4879 | emit_mov_r32_r32(&dst, REG_EAX, srcp.value); // mov eax,srcp |
| 4880 | srcp.value = REG_EAX; |
| 4881 | } |
| 4882 | |
| 4883 | /* 32-bit form */ |
| 4884 | if (inst->size == 4) |
| 4885 | { |
| 4886 | /* general case */ |
| 4887 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4888 | emit_movsx_r32_m8(&dst, dstreg, MABS(drcbe, srcp.value)); // movsx dstreg,[srcp] |
| 4889 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4890 | emit_movsx_r32_r8(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 4891 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4892 | } |
| 4893 | |
| 4894 | /* 64-bit form */ |
| 4895 | else if (inst->size == 8) |
| 4896 | { |
| 4897 | /* general case */ |
| 4898 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4899 | emit_movsx_r64_m8(&dst, dstreg, MABS(drcbe, srcp.value)); // movsx dstreg,[srcp] |
| 4900 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4901 | emit_movsx_r64_r8(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 4902 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4903 | } |
| 4904 | return dst; |
| 4905 | } |
| 4906 | |
| 4907 | |
| 4908 | /*------------------------------------------------- |
| 4909 | op_sext2 - process a SEXT2 opcode |
| 4910 | -------------------------------------------------*/ |
| 4911 | |
| 4912 | static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4913 | { |
| 4914 | drcuml_parameter dstp, srcp; |
| 4915 | int dstreg; |
| 4916 | |
| 4917 | /* validate instruction */ |
| 4918 | assert(inst->size == 4 || inst->size == 8); |
| 4919 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4920 | |
| 4921 | /* normalize parameters */ |
| 4922 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4923 | |
| 4924 | /* degenerate cases -- convert to a move */ |
| 4925 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4926 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT16)srcp.value); |
| 4927 | |
| 4928 | /* pick a target register for the general case */ |
| 4929 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4930 | |
| 4931 | /* 32-bit form */ |
| 4932 | if (inst->size == 4) |
| 4933 | { |
| 4934 | /* general case */ |
| 4935 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4936 | emit_movsx_r32_m16(&dst, dstreg, MABS(drcbe, srcp.value)); // movsx dstreg,[srcp] |
| 4937 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4938 | emit_movsx_r32_r16(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 4939 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4940 | } |
| 4941 | |
| 4942 | /* 64-bit form */ |
| 4943 | else if (inst->size == 8) |
| 4944 | { |
| 4945 | /* general case */ |
| 4946 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4947 | emit_movsx_r64_m16(&dst, dstreg, MABS(drcbe, srcp.value)); // movsx dstreg,[srcp] |
| 4948 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4949 | emit_movsx_r64_r16(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 4950 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4951 | } |
| 4952 | return dst; |
| 4953 | } |
| 4954 | |
| 4955 | |
| 4956 | /*------------------------------------------------- |
| 4957 | op_sext4 - process a SEXT4 opcode |
| 4958 | -------------------------------------------------*/ |
| 4959 | |
| 4960 | static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4961 | { |
| 4962 | drcuml_parameter dstp, srcp; |
| 4963 | int dstreg; |
| 4964 | |
| 4965 | /* validate instruction */ |
| 4966 | assert(inst->size == 8); |
| 4967 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4968 | |
| 4969 | /* normalize parameters */ |
| 4970 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4971 | |
| 4972 | /* degenerate cases -- convert to a move */ |
| 4973 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4974 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)srcp.value); |
| 4975 | |
| 4976 | /* pick a target register for the general case */ |
| 4977 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4978 | |
| 4979 | /* 64-bit form */ |
| 4980 | if (inst->size == 8) |
| 4981 | { |
| 4982 | /* general case */ |
| 4983 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4984 | emit_movsxd_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // movsxd dstreg,[srcp] |
| 4985 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4986 | emit_movsxd_r64_r32(&dst, dstreg, srcp.value); // movsxd dstreg,srcp |
| 4987 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4988 | } |
| 4989 | return dst; |
| 4990 | } |
| 4991 | |
| 4992 | |
| 4993 | /*------------------------------------------------- |
| 4994 | op_add - process a ADD opcode |
| 4995 | -------------------------------------------------*/ |
| 4996 | |
| 4997 | static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4998 | { |
| 4999 | drcuml_parameter dstp, src1p, src2p; |
| 5000 | int dstreg; |
| 5001 | |
| 5002 | /* validate instruction */ |
| 5003 | assert(inst->size == 4 || inst->size == 8); |
| 5004 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5005 | |
| 5006 | /* normalize parameters */ |
| 5007 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5008 | |
| 5009 | /* degenerate cases -- convert to a move */ |
| 5010 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5011 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value + src2p.value); |
| 5012 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5013 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5014 | |
| 5015 | /* pick a target register for the general case */ |
| 5016 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5017 | |
| 5018 | /* 32-bit form */ |
| 5019 | if (inst->size == 4) |
| 5020 | { |
| 5021 | /* dstp == src1p in memory */ |
| 5022 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5023 | emit_add_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // add [dstp],src2p |
| 5024 | |
| 5025 | /* reg = reg + imm */ |
| 5026 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5027 | emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, src2p.value)); // lea dstp,[src1p+src2p] |
| 5028 | |
| 5029 | /* reg = reg + reg */ |
| 5030 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0) |
| 5031 | emit_lea_r32_m32(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0)); // lea dstp,[src1p+src2p] |
| 5032 | |
| 5033 | /* general case */ |
| 5034 | else |
| 5035 | { |
| 5036 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5037 | emit_add_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // add dstreg,src2p |
| 5038 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5039 | } |
| 5040 | } |
| 5041 | |
| 5042 | /* 64-bit form */ |
| 5043 | else if (inst->size == 8) |
| 5044 | { |
| 5045 | /* dstp == src1p in memory */ |
| 5046 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5047 | emit_add_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // add [dstp],src2p |
| 5048 | |
| 5049 | /* reg = reg + imm */ |
| 5050 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.value) && inst->condflags == 0) |
| 5051 | emit_lea_r64_m64(&dst, dstp.value, MBD(src1p.value, src2p.value)); // lea dstp,[src1p+src2p] |
| 5052 | |
| 5053 | /* reg = reg + reg */ |
| 5054 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0) |
| 5055 | emit_lea_r64_m64(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0)); // lea dstp,[src1p+src2p] |
| 5056 | |
| 5057 | /* general case */ |
| 5058 | else |
| 5059 | { |
| 5060 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5061 | emit_add_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // add dstreg,src2p |
| 5062 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5063 | } |
| 5064 | } |
| 5065 | return dst; |
| 5066 | } |
| 5067 | |
| 5068 | |
| 5069 | /*------------------------------------------------- |
| 5070 | op_addc - process a ADDC opcode |
| 5071 | -------------------------------------------------*/ |
| 5072 | |
| 5073 | static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5074 | { |
| 5075 | drcuml_parameter dstp, src1p, src2p; |
| 5076 | int dstreg; |
| 5077 | |
| 5078 | /* validate instruction */ |
| 5079 | assert(inst->size == 4 || inst->size == 8); |
| 5080 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5081 | |
| 5082 | /* normalize parameters */ |
| 5083 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5084 | |
| 5085 | /* pick a target register for the general case */ |
| 5086 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5087 | |
| 5088 | /* 32-bit form */ |
| 5089 | if (inst->size == 4) |
| 5090 | { |
| 5091 | /* dstp == src1p in memory */ |
| 5092 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5093 | emit_adc_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // adc [dstp],src2p |
| 5094 | |
| 5095 | /* general case */ |
| 5096 | else |
| 5097 | { |
| 5098 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5099 | emit_adc_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // adc dstreg,src2p |
| 5100 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5101 | } |
| 5102 | } |
| 5103 | |
| 5104 | /* 64-bit form */ |
| 5105 | else if (inst->size == 8) |
| 5106 | { |
| 5107 | /* dstp == src1p in memory */ |
| 5108 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5109 | emit_adc_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // adc [dstp],src2p |
| 5110 | |
| 5111 | /* general case */ |
| 5112 | else |
| 5113 | { |
| 5114 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5115 | emit_adc_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // adc dstreg,src2p |
| 5116 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5117 | } |
| 5118 | } |
| 5119 | return dst; |
| 5120 | } |
| 5121 | |
| 5122 | |
| 5123 | /*------------------------------------------------- |
| 5124 | op_sub - process a SUB opcode |
| 5125 | -------------------------------------------------*/ |
| 5126 | |
| 5127 | static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5128 | { |
| 5129 | drcuml_parameter dstp, src1p, src2p; |
| 5130 | int dstreg; |
| 5131 | |
| 5132 | /* validate instruction */ |
| 5133 | assert(inst->size == 4 || inst->size == 8); |
| 5134 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5135 | |
| 5136 | /* normalize parameters */ |
| 5137 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5138 | |
| 5139 | /* degenerate cases -- convert to a move */ |
| 5140 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5141 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value - src2p.value); |
| 5142 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5143 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5144 | |
| 5145 | /* pick a target register for the general case */ |
| 5146 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5147 | |
| 5148 | /* 32-bit form */ |
| 5149 | if (inst->size == 4) |
| 5150 | { |
| 5151 | /* dstp == src1p in memory */ |
| 5152 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5153 | emit_sub_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sub [dstp],src2p |
| 5154 | |
| 5155 | /* reg = reg - imm */ |
| 5156 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5157 | emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, -src2p.value)); // lea dstp,[src1p-src2p] |
| 5158 | |
| 5159 | /* general case */ |
| 5160 | else |
| 5161 | { |
| 5162 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5163 | emit_sub_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sub dstreg,src2p |
| 5164 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5165 | } |
| 5166 | } |
| 5167 | |
| 5168 | /* 64-bit form */ |
| 5169 | else if (inst->size == 8) |
| 5170 | { |
| 5171 | /* dstp == src1p in memory */ |
| 5172 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5173 | emit_sub_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sub [dstp],src2p |
| 5174 | |
| 5175 | /* reg = reg - imm */ |
| 5176 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.type) && inst->condflags == 0) |
| 5177 | emit_lea_r64_m64(&dst, dstp.value, MBD(src1p.value, -src2p.value)); // lea dstp,[src1p-src2p] |
| 5178 | |
| 5179 | /* general case */ |
| 5180 | else |
| 5181 | { |
| 5182 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5183 | emit_sub_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // sub dstreg,src2p |
| 5184 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5185 | } |
| 5186 | } |
| 5187 | return dst; |
| 5188 | } |
| 5189 | |
| 5190 | |
| 5191 | /*------------------------------------------------- |
| 5192 | op_subc - process a SUBC opcode |
| 5193 | -------------------------------------------------*/ |
| 5194 | |
| 5195 | static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5196 | { |
| 5197 | drcuml_parameter dstp, src1p, src2p; |
| 5198 | int dstreg; |
| 5199 | |
| 5200 | /* validate instruction */ |
| 5201 | assert(inst->size == 4 || inst->size == 8); |
| 5202 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5203 | |
| 5204 | /* normalize parameters */ |
| 5205 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5206 | |
| 5207 | /* pick a target register for the general case */ |
| 5208 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5209 | |
| 5210 | /* 32-bit form */ |
| 5211 | if (inst->size == 4) |
| 5212 | { |
| 5213 | /* dstp == src1p in memory */ |
| 5214 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5215 | emit_sbb_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sbb [dstp],src2p |
| 5216 | |
| 5217 | /* general case */ |
| 5218 | else |
| 5219 | { |
| 5220 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5221 | emit_sbb_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sbb dstreg,src2p |
| 5222 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | /* 64-bit form */ |
| 5227 | else if (inst->size == 8) |
| 5228 | { |
| 5229 | /* dstp == src1p in memory */ |
| 5230 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5231 | emit_sbb_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sbb [dstp],src2p |
| 5232 | |
| 5233 | /* general case */ |
| 5234 | else |
| 5235 | { |
| 5236 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5237 | emit_sbb_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // sbb dstreg,src2p |
| 5238 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5239 | } |
| 5240 | } |
| 5241 | return dst; |
| 5242 | } |
| 5243 | |
| 5244 | |
| 5245 | /*------------------------------------------------- |
| 5246 | op_cmp - process a CMP opcode |
| 5247 | -------------------------------------------------*/ |
| 5248 | |
| 5249 | static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5250 | { |
| 5251 | drcuml_parameter src1p, src2p; |
| 5252 | int src1reg; |
| 5253 | |
| 5254 | /* validate instruction */ |
| 5255 | assert(inst->size == 4 || inst->size == 8); |
| 5256 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5257 | |
| 5258 | /* normalize parameters */ |
| 5259 | param_normalize_2(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5260 | |
| 5261 | /* degenerate cases */ |
| 5262 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && src1p.value == src2p.value) |
| 5263 | { |
| 5264 | emit_cmp_r32_r32(&dst, REG_EAX, REG_EAX); // cmp eax,eax |
| 5265 | return dst; |
| 5266 | } |
| 5267 | |
| 5268 | /* pick a target register for the general case */ |
| 5269 | src1reg = param_select_register(REG_EAX, &src1p, NULL); |
| 5270 | |
| 5271 | /* 32-bit form */ |
| 5272 | if (inst->size == 4) |
| 5273 | { |
| 5274 | /* memory versus anything */ |
| 5275 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5276 | emit_cmp_m32_p32(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst); // cmp [dstp],src2p |
| 5277 | |
| 5278 | /* general case */ |
| 5279 | else |
| 5280 | { |
| 5281 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5282 | emit_mov_r32_imm(&dst, src1reg, src1p.value); // mov src1reg,imm |
| 5283 | emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst); // cmp src1reg,src2p |
| 5284 | } |
| 5285 | } |
| 5286 | |
| 5287 | /* 64-bit form */ |
| 5288 | else |
| 5289 | { |
| 5290 | /* memory versus anything */ |
| 5291 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5292 | emit_cmp_m64_p64(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst); // cmp [dstp],src2p |
| 5293 | |
| 5294 | /* general case */ |
| 5295 | else |
| 5296 | { |
| 5297 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5298 | emit_mov_r64_imm(&dst, src1reg, src1p.value); // mov src1reg,imm |
| 5299 | emit_cmp_r64_p64(drcbe, &dst, src1reg, &src2p, inst); // cmp src1reg,src2p |
| 5300 | } |
| 5301 | } |
| 5302 | return dst; |
| 5303 | } |
| 5304 | |
| 5305 | |
| 5306 | /*------------------------------------------------- |
| 5307 | op_mulu - process a MULU opcode |
| 5308 | -------------------------------------------------*/ |
| 5309 | |
| 5310 | static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5311 | { |
| 5312 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5313 | int compute_hi; |
| 5314 | |
| 5315 | /* validate instruction */ |
| 5316 | assert(inst->size == 4 || inst->size == 8); |
| 5317 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5318 | |
| 5319 | /* normalize parameters */ |
| 5320 | param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5321 | compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5322 | |
| 5323 | /* degenerate cases -- convert to a move */ |
| 5324 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5325 | { |
| 5326 | if (inst->size == 4) |
| 5327 | { |
| 5328 | if (!compute_hi) |
| 5329 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value * (UINT32)src2p.value); |
| 5330 | else |
| 5331 | { |
| 5332 | UINT64 result = (UINT64)(UINT32)src1p.value * (UINT64)(UINT32)src2p.value; |
| 5333 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff); |
| 5334 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32); |
| 5335 | } |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | /* 32-bit form */ |
| 5340 | if (inst->size == 4) |
| 5341 | { |
| 5342 | /* general case */ |
| 5343 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5344 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5345 | emit_mul_m32(&dst, MABS(drcbe, src2p.value)); // mul [src2p] |
| 5346 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5347 | emit_mul_r32(&dst, src2p.value); // mul src2p |
| 5348 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5349 | { |
| 5350 | emit_mov_r32_imm(&dst, REG_EDX, src2p.value); // mov edx,src2p |
| 5351 | emit_mul_r32(&dst, REG_EDX); // mul edx |
| 5352 | } |
| 5353 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5354 | if (compute_hi) |
| 5355 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5356 | } |
| 5357 | |
| 5358 | /* 64-bit form */ |
| 5359 | else if (inst->size == 8) |
| 5360 | { |
| 5361 | /* general case */ |
| 5362 | emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p); // mov rax,src1p |
| 5363 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5364 | emit_mul_m64(&dst, MABS(drcbe, src2p.value)); // mul [src2p] |
| 5365 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5366 | emit_mul_r64(&dst, src2p.value); // mul src2p |
| 5367 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5368 | { |
| 5369 | emit_mov_r64_imm(&dst, REG_RDX, src2p.value); // mov rdx,src2p |
| 5370 | emit_mul_r64(&dst, REG_RDX); // mul rdx |
| 5371 | } |
| 5372 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5373 | if (compute_hi) |
| 5374 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX); // mov edstp,rdx |
| 5375 | } |
| 5376 | return dst; |
| 5377 | } |
| 5378 | |
| 5379 | |
| 5380 | /*------------------------------------------------- |
| 5381 | op_muls - process a MULS opcode |
| 5382 | -------------------------------------------------*/ |
| 5383 | |
| 5384 | static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5385 | { |
| 5386 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5387 | int compute_hi; |
| 5388 | |
| 5389 | /* validate instruction */ |
| 5390 | assert(inst->size == 4 || inst->size == 8); |
| 5391 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5392 | |
| 5393 | /* normalize parameters */ |
| 5394 | param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5395 | compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5396 | |
| 5397 | /* degenerate cases -- convert to a move */ |
| 5398 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5399 | { |
| 5400 | if (inst->size == 4) |
| 5401 | { |
| 5402 | if (!compute_hi) |
| 5403 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value * (INT32)src2p.value); |
| 5404 | else |
| 5405 | { |
| 5406 | UINT64 result = (INT64)(INT32)src1p.value * (INT64)(INT32)src2p.value; |
| 5407 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff); |
| 5408 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32); |
| 5409 | } |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | /* 32-bit form */ |
| 5414 | if (inst->size == 4) |
| 5415 | { |
| 5416 | /* 32-bit destination with memory/immediate or register/immediate */ |
| 5417 | if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5418 | { |
| 5419 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5420 | emit_imul_r32_m32_imm(&dst, REG_EAX, MABS(drcbe, src1p.value), src2p.value); // imul eax,[src1p],src2p |
| 5421 | else if (src1p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5422 | emit_imul_r32_r32_imm(&dst, REG_EAX, src1p.value, src2p.value); // imul eax,src1p,src2p |
| 5423 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5424 | } |
| 5425 | |
| 5426 | /* 32-bit destination, general case */ |
| 5427 | else if (!compute_hi) |
| 5428 | { |
| 5429 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5430 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5431 | emit_imul_r32_m32(&dst, REG_EAX, MABS(drcbe, src2p.value)); // imul eax,[src2p] |
| 5432 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5433 | emit_imul_r32_r32(&dst, REG_EAX, src2p.value); // imul eax,src2p |
| 5434 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5435 | } |
| 5436 | |
| 5437 | /* 64-bit destination, general case */ |
| 5438 | else |
| 5439 | { |
| 5440 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5441 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5442 | emit_imul_m32(&dst, MABS(drcbe, src2p.value)); // imul [src2p] |
| 5443 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5444 | emit_imul_r32(&dst, src2p.value); // imul src2p |
| 5445 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5446 | { |
| 5447 | emit_mov_r32_imm(&dst, REG_EDX, src2p.value); // mov edx,src2p |
| 5448 | emit_imul_r32(&dst, REG_EDX); // imul edx |
| 5449 | } |
| 5450 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5451 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5452 | } |
| 5453 | } |
| 5454 | |
| 5455 | /* 64-bit form */ |
| 5456 | else if (inst->size == 8) |
| 5457 | { |
| 5458 | /* 64-bit destination with memory/immediate or register/immediate */ |
| 5459 | if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && short_immediate(src2p.type)) |
| 5460 | { |
| 5461 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5462 | emit_imul_r64_m64_imm(&dst, REG_RAX, MABS(drcbe, src1p.value), src2p.value);// imul rax,[src1p],src2p |
| 5463 | else if (src1p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5464 | emit_imul_r64_r64_imm(&dst, REG_RAX, src1p.value, src2p.value); // imul rax,src1p,src2p |
| 5465 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5466 | } |
| 5467 | |
| 5468 | /* 64-bit destination, general case */ |
| 5469 | else if (!compute_hi) |
| 5470 | { |
| 5471 | emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p); // mov rax,src1p |
| 5472 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5473 | emit_imul_r64_m64(&dst, REG_RAX, MABS(drcbe, src2p.value)); // imul rax,[src2p] |
| 5474 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5475 | emit_imul_r64_r64(&dst, REG_RAX, src2p.value); // imul rax,src2p |
| 5476 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5477 | } |
| 5478 | |
| 5479 | /* 128-bit destination, general case */ |
| 5480 | else |
| 5481 | { |
| 5482 | emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p); // mov rax,src1p |
| 5483 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5484 | emit_imul_m64(&dst, MABS(drcbe, src2p.value)); // imul [src2p] |
| 5485 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5486 | emit_imul_r64(&dst, src2p.value); // imul src2p |
| 5487 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5488 | { |
| 5489 | emit_mov_r64_imm(&dst, REG_RDX, src2p.value); // mov rdx,src2p |
| 5490 | emit_imul_r64(&dst, REG_RDX); // imul rdx |
| 5491 | } |
| 5492 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5493 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX); // mov edstp,rdx |
| 5494 | } |
| 5495 | } |
| 5496 | return dst; |
| 5497 | } |
| 5498 | |
| 5499 | |
| 5500 | /*------------------------------------------------- |
| 5501 | op_divu - process a DIVU opcode |
| 5502 | -------------------------------------------------*/ |
| 5503 | |
| 5504 | static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5505 | { |
| 5506 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5507 | int compute_rem; |
| 5508 | emit_link skip; |
| 5509 | |
| 5510 | /* validate instruction */ |
| 5511 | assert(inst->size == 4 || inst->size == 8); |
| 5512 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5513 | |
| 5514 | /* normalize parameters */ |
| 5515 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5516 | compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5517 | |
| 5518 | /* degenerate cases -- convert to a move */ |
| 5519 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5520 | { |
| 5521 | if (inst->size == 4) |
| 5522 | { |
| 5523 | if ((UINT32)src2p.value == 0) |
| 5524 | return dst; |
| 5525 | if (!compute_rem) |
| 5526 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value); |
| 5527 | else |
| 5528 | { |
| 5529 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value); |
| 5530 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value % (UINT32)src2p.value); |
| 5531 | } |
| 5532 | } |
| 5533 | else if (inst->size == 8) |
| 5534 | { |
| 5535 | UINT64 reslo, reshi; |
| 5536 | if (src2p.value == 0) |
| 5537 | return dst; |
| 5538 | if (!compute_rem) |
| 5539 | { |
| 5540 | reslo = (UINT64)src1p.value / (UINT64)src2p.value; |
| 5541 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5542 | } |
| 5543 | else |
| 5544 | { |
| 5545 | reslo = (UINT64)src1p.value / (UINT64)src2p.value; |
| 5546 | reshi = (UINT64)src1p.value % (UINT64)src2p.value; |
| 5547 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5548 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5549 | } |
| 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | /* 32-bit form */ |
| 5554 | if (inst->size == 4) |
| 5555 | { |
| 5556 | /* general case */ |
| 5557 | emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p); // mov ecx,src2p |
| 5558 | emit_jecxz_link(&dst, &skip); // jecxz skip |
| 5559 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5560 | emit_xor_r32_r32(&dst, REG_EDX, REG_EDX); // xor edx,edx |
| 5561 | emit_div_r32(&dst, REG_ECX); // div ecx |
| 5562 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5563 | if (compute_rem) |
| 5564 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5565 | resolve_link(&dst, &skip); // skip: |
| 5566 | } |
| 5567 | |
| 5568 | /* 64-bit form */ |
| 5569 | else if (inst->size == 8) |
| 5570 | { |
| 5571 | /* general case */ |
| 5572 | emit_mov_r64_p64(drcbe, &dst, REG_RCX, &src2p); // mov rcx,src2p |
| 5573 | emit_jrcxz_link(&dst, &skip); // jrcxz skip |
| 5574 | emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p); // mov rax,src1p |
| 5575 | emit_xor_r32_r32(&dst, REG_EDX, REG_EDX); // xor edx,edx |
| 5576 | emit_div_r64(&dst, REG_RCX); // div rcx |
| 5577 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5578 | if (compute_rem) |
| 5579 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX); // mov edstp,rdx |
| 5580 | resolve_link(&dst, &skip); // skip: |
| 5581 | } |
| 5582 | return dst; |
| 5583 | } |
| 5584 | |
| 5585 | |
| 5586 | /*------------------------------------------------- |
| 5587 | op_divs - process a DIVS opcode |
| 5588 | -------------------------------------------------*/ |
| 5589 | |
| 5590 | static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5591 | { |
| 5592 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5593 | int compute_rem; |
| 5594 | emit_link skip; |
| 5595 | |
| 5596 | /* validate instruction */ |
| 5597 | assert(inst->size == 4 || inst->size == 8); |
| 5598 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5599 | |
| 5600 | /* normalize parameters */ |
| 5601 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5602 | compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5603 | |
| 5604 | /* degenerate cases -- convert to a move */ |
| 5605 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5606 | { |
| 5607 | if (inst->size == 4) |
| 5608 | { |
| 5609 | if ((INT32)src2p.value == 0) |
| 5610 | return dst; |
| 5611 | if (!compute_rem) |
| 5612 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value); |
| 5613 | else |
| 5614 | { |
| 5615 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value); |
| 5616 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value % (INT32)src2p.value); |
| 5617 | } |
| 5618 | } |
| 5619 | else if (inst->size == 8) |
| 5620 | { |
| 5621 | UINT64 reslo, reshi; |
| 5622 | if (src2p.value == 0) |
| 5623 | return dst; |
| 5624 | if (!compute_rem) |
| 5625 | { |
| 5626 | reslo = (INT64)src1p.value / (INT64)src2p.value; |
| 5627 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5628 | } |
| 5629 | else |
| 5630 | { |
| 5631 | reslo = (INT64)src1p.value / (INT64)src2p.value; |
| 5632 | reshi = (INT64)src1p.value % (INT64)src2p.value; |
| 5633 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5634 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5635 | } |
| 5636 | } |
| 5637 | } |
| 5638 | |
| 5639 | /* 32-bit form */ |
| 5640 | if (inst->size == 4) |
| 5641 | { |
| 5642 | /* general case */ |
| 5643 | emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p); // mov ecx,src2p |
| 5644 | emit_jecxz_link(&dst, &skip); // jecxz skip |
| 5645 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5646 | emit_cdq(&dst); // cdq |
| 5647 | emit_idiv_r32(&dst, REG_ECX); // idiv ecx |
| 5648 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5649 | if (compute_rem) |
| 5650 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5651 | resolve_link(&dst, &skip); // skip: |
| 5652 | } |
| 5653 | |
| 5654 | /* 64-bit form */ |
| 5655 | else if (inst->size == 8) |
| 5656 | { |
| 5657 | /* general case */ |
| 5658 | emit_mov_r64_p64(drcbe, &dst, REG_RCX, &src2p); // mov rcx,src2p |
| 5659 | emit_jrcxz_link(&dst, &skip); // jrcxz skip |
| 5660 | emit_mov_r64_p64(drcbe, &dst, REG_RAX, &src1p); // mov rax,src1p |
| 5661 | emit_cqo(&dst); // cqo |
| 5662 | emit_idiv_r64(&dst, REG_RCX); // idiv rcx |
| 5663 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_RAX); // mov dstp,rax |
| 5664 | if (compute_rem) |
| 5665 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_RDX); // mov edstp,rdx |
| 5666 | resolve_link(&dst, &skip); // skip: |
| 5667 | } |
| 5668 | return dst; |
| 5669 | } |
| 5670 | |
| 5671 | |
| 5672 | /*------------------------------------------------- |
| 5673 | op_and - process a AND opcode |
| 5674 | -------------------------------------------------*/ |
| 5675 | |
| 5676 | static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5677 | { |
| 5678 | drcuml_parameter dstp, src1p, src2p; |
| 5679 | int dstreg; |
| 5680 | |
| 5681 | /* validate instruction */ |
| 5682 | assert(inst->size == 4 || inst->size == 8); |
| 5683 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5684 | |
| 5685 | /* normalize parameters */ |
| 5686 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5687 | |
| 5688 | /* degenerate cases -- convert to a move */ |
| 5689 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5690 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value); |
| 5691 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && (src2p.value & size_to_mask[inst->size]) == size_to_mask[inst->size] && inst->condflags == 0) |
| 5692 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5693 | |
| 5694 | /* pick a target register for the general case */ |
| 5695 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5696 | |
| 5697 | /* 32-bit form */ |
| 5698 | if (inst->size == 4) |
| 5699 | { |
| 5700 | /* dstp == src1p in memory */ |
| 5701 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5702 | emit_and_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // and [dstp],src2p |
| 5703 | |
| 5704 | /* general case */ |
| 5705 | else |
| 5706 | { |
| 5707 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5708 | emit_and_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // and dstreg,src2p |
| 5709 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | /* 64-bit form */ |
| 5714 | else if (inst->size == 8) |
| 5715 | { |
| 5716 | /* dstp == src1p in memory */ |
| 5717 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5718 | emit_and_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // and [dstp],src2p |
| 5719 | |
| 5720 | /* general case */ |
| 5721 | else |
| 5722 | { |
| 5723 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5724 | emit_and_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // and dstreg,src2p |
| 5725 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5726 | } |
| 5727 | } |
| 5728 | return dst; |
| 5729 | } |
| 5730 | |
| 5731 | |
| 5732 | /*------------------------------------------------- |
| 5733 | op_test - process a TEST opcode |
| 5734 | -------------------------------------------------*/ |
| 5735 | |
| 5736 | static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5737 | { |
| 5738 | drcuml_parameter src1p, src2p; |
| 5739 | int src1reg; |
| 5740 | |
| 5741 | /* validate instruction */ |
| 5742 | assert(inst->size == 4 || inst->size == 8); |
| 5743 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5744 | |
| 5745 | /* normalize parameters */ |
| 5746 | param_normalize_2_commutative(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5747 | |
| 5748 | /* pick a target register for the general case */ |
| 5749 | src1reg = param_select_register(REG_EAX, &src1p, NULL); |
| 5750 | |
| 5751 | /* 32-bit form */ |
| 5752 | if (inst->size == 4) |
| 5753 | { |
| 5754 | /* src1p in memory */ |
| 5755 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5756 | emit_test_m32_p32(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst); // test [src1p],src2p |
| 5757 | |
| 5758 | /* general case */ |
| 5759 | else |
| 5760 | { |
| 5761 | emit_mov_r32_p32(drcbe, &dst, src1reg, &src1p); // mov src1reg,src1p |
| 5762 | emit_test_r32_p32(drcbe, &dst, src1reg, &src2p, inst); // test src1reg,src2p |
| 5763 | } |
| 5764 | } |
| 5765 | |
| 5766 | /* 64-bit form */ |
| 5767 | else if (inst->size == 8) |
| 5768 | { |
| 5769 | /* src1p in memory */ |
| 5770 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5771 | emit_test_m64_p64(drcbe, &dst, MABS(drcbe, src1p.value), &src2p, inst); // test [src1p],src2p |
| 5772 | |
| 5773 | /* general case */ |
| 5774 | else |
| 5775 | { |
| 5776 | emit_mov_r64_p64(drcbe, &dst, src1reg, &src1p); // mov src1reg,src1p |
| 5777 | emit_test_r64_p64(drcbe, &dst, src1reg, &src2p, inst); // test src1reg,src2p |
| 5778 | } |
| 5779 | } |
| 5780 | return dst; |
| 5781 | } |
| 5782 | |
| 5783 | |
| 5784 | /*------------------------------------------------- |
| 5785 | op_or - process a OR opcode |
| 5786 | -------------------------------------------------*/ |
| 5787 | |
| 5788 | static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5789 | { |
| 5790 | drcuml_parameter dstp, src1p, src2p; |
| 5791 | int dstreg; |
| 5792 | |
| 5793 | /* validate instruction */ |
| 5794 | assert(inst->size == 4 || inst->size == 8); |
| 5795 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5796 | |
| 5797 | /* normalize parameters */ |
| 5798 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5799 | |
| 5800 | /* degenerate cases -- convert to a move */ |
| 5801 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5802 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value | src2p.value); |
| 5803 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5804 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5805 | |
| 5806 | /* pick a target register for the general case */ |
| 5807 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5808 | |
| 5809 | /* 32-bit form */ |
| 5810 | if (inst->size == 4) |
| 5811 | { |
| 5812 | /* dstp == src1p in memory */ |
| 5813 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5814 | emit_or_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // or [dstp],src2p |
| 5815 | |
| 5816 | /* general case */ |
| 5817 | else |
| 5818 | { |
| 5819 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5820 | emit_or_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // or dstreg,src2p |
| 5821 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5822 | } |
| 5823 | } |
| 5824 | |
| 5825 | /* 64-bit form */ |
| 5826 | else if (inst->size == 8) |
| 5827 | { |
| 5828 | /* dstp == src1p in memory */ |
| 5829 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5830 | emit_or_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // or [dstp],src2p |
| 5831 | |
| 5832 | /* general case */ |
| 5833 | else |
| 5834 | { |
| 5835 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5836 | emit_or_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // or dstreg,src2p |
| 5837 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5838 | } |
| 5839 | } |
| 5840 | return dst; |
| 5841 | } |
| 5842 | |
| 5843 | |
| 5844 | /*------------------------------------------------- |
| 5845 | op_xor - process a XOR opcode |
| 5846 | -------------------------------------------------*/ |
| 5847 | |
| 5848 | static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5849 | { |
| 5850 | drcuml_parameter dstp, src1p, src2p; |
| 5851 | int dstreg; |
| 5852 | |
| 5853 | /* validate instruction */ |
| 5854 | assert(inst->size == 4 || inst->size == 8); |
| 5855 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5856 | |
| 5857 | /* normalize parameters */ |
| 5858 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5859 | |
| 5860 | /* degenerate cases -- convert to a move */ |
| 5861 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5862 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value); |
| 5863 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5864 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5865 | |
| 5866 | /* pick a target register for the general case */ |
| 5867 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5868 | |
| 5869 | /* 32-bit form */ |
| 5870 | if (inst->size == 4) |
| 5871 | { |
| 5872 | /* dstp == src1p in memory */ |
| 5873 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5874 | emit_xor_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // xor [dstp],src2p |
| 5875 | |
| 5876 | /* general case */ |
| 5877 | else |
| 5878 | { |
| 5879 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5880 | emit_xor_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // xor dstreg,src2p |
| 5881 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | /* 64-bit form */ |
| 5886 | else if (inst->size == 8) |
| 5887 | { |
| 5888 | /* dstp == src1p in memory */ |
| 5889 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5890 | emit_xor_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // xor [dstp],src2p |
| 5891 | |
| 5892 | /* general case */ |
| 5893 | else |
| 5894 | { |
| 5895 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5896 | emit_xor_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // xor dstreg,src2p |
| 5897 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5898 | } |
| 5899 | } |
| 5900 | return dst; |
| 5901 | } |
| 5902 | |
| 5903 | |
| 5904 | /*------------------------------------------------- |
| 5905 | op_shl - process a SHL opcode |
| 5906 | -------------------------------------------------*/ |
| 5907 | |
| 5908 | static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5909 | { |
| 5910 | drcuml_parameter dstp, src1p, src2p; |
| 5911 | int dstreg; |
| 5912 | |
| 5913 | /* validate instruction */ |
| 5914 | assert(inst->size == 4 || inst->size == 8); |
| 5915 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5916 | |
| 5917 | /* normalize parameters */ |
| 5918 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5919 | |
| 5920 | /* degenerate cases -- convert to a move */ |
| 5921 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5922 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value << src2p.value); |
| 5923 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5924 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5925 | |
| 5926 | /* pick a target register for the general case */ |
| 5927 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5928 | |
| 5929 | /* 32-bit form */ |
| 5930 | if (inst->size == 4) |
| 5931 | { |
| 5932 | /* dstp == src1p in memory */ |
| 5933 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5934 | emit_shl_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // shl [dstp],src2p |
| 5935 | |
| 5936 | /* general case */ |
| 5937 | else |
| 5938 | { |
| 5939 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5940 | emit_shl_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // shl dstreg,src2p |
| 5941 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5942 | } |
| 5943 | } |
| 5944 | |
| 5945 | /* 64-bit form */ |
| 5946 | else if (inst->size == 8) |
| 5947 | { |
| 5948 | /* dstp == src1p in memory */ |
| 5949 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5950 | emit_shl_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // shl [dstp],src2p |
| 5951 | |
| 5952 | /* general case */ |
| 5953 | else |
| 5954 | { |
| 5955 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5956 | emit_shl_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // shl dstreg,src2p |
| 5957 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5958 | } |
| 5959 | } |
| 5960 | return dst; |
| 5961 | } |
| 5962 | |
| 5963 | |
| 5964 | /*------------------------------------------------- |
| 5965 | op_shr - process a SHR opcode |
| 5966 | -------------------------------------------------*/ |
| 5967 | |
| 5968 | static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5969 | { |
| 5970 | drcuml_parameter dstp, src1p, src2p; |
| 5971 | int dstreg; |
| 5972 | |
| 5973 | /* validate instruction */ |
| 5974 | assert(inst->size == 4 || inst->size == 8); |
| 5975 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5976 | |
| 5977 | /* normalize parameters */ |
| 5978 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5979 | |
| 5980 | /* degenerate cases -- convert to a move */ |
| 5981 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5982 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 5983 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5984 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5985 | |
| 5986 | /* pick a target register for the general case */ |
| 5987 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5988 | |
| 5989 | /* 32-bit form */ |
| 5990 | if (inst->size == 4) |
| 5991 | { |
| 5992 | /* dstp == src1p in memory */ |
| 5993 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5994 | emit_shr_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // shr [dstp],src2p |
| 5995 | |
| 5996 | /* general case */ |
| 5997 | else |
| 5998 | { |
| 5999 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6000 | emit_shr_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // shr dstreg,src2p |
| 6001 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6002 | } |
| 6003 | } |
| 6004 | |
| 6005 | /* 64-bit form */ |
| 6006 | else if (inst->size == 8) |
| 6007 | { |
| 6008 | /* dstp == src1p in memory */ |
| 6009 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6010 | emit_shr_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // shr [dstp],src2p |
| 6011 | |
| 6012 | /* general case */ |
| 6013 | else |
| 6014 | { |
| 6015 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6016 | emit_shr_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // shr dstreg,src2p |
| 6017 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6018 | } |
| 6019 | } |
| 6020 | return dst; |
| 6021 | } |
| 6022 | |
| 6023 | |
| 6024 | /*------------------------------------------------- |
| 6025 | op_sar - process a SAR opcode |
| 6026 | -------------------------------------------------*/ |
| 6027 | |
| 6028 | static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6029 | { |
| 6030 | drcuml_parameter dstp, src1p, src2p; |
| 6031 | int dstreg; |
| 6032 | |
| 6033 | /* validate instruction */ |
| 6034 | assert(inst->size == 4 || inst->size == 8); |
| 6035 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6036 | |
| 6037 | /* normalize parameters */ |
| 6038 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6039 | |
| 6040 | /* degenerate cases -- convert to a move */ |
| 6041 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6042 | { |
| 6043 | if (inst->size == 4) |
| 6044 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value >> src2p.value); |
| 6045 | else if (inst->size == 8) |
| 6046 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT64)src1p.value >> src2p.value); |
| 6047 | } |
| 6048 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6049 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6050 | |
| 6051 | /* pick a target register for the general case */ |
| 6052 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6053 | |
| 6054 | /* 32-bit form */ |
| 6055 | if (inst->size == 4) |
| 6056 | { |
| 6057 | /* dstp == src1p in memory */ |
| 6058 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6059 | emit_sar_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sar [dstp],src2p |
| 6060 | |
| 6061 | /* general case */ |
| 6062 | else |
| 6063 | { |
| 6064 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6065 | emit_sar_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sar dstreg,src2p |
| 6066 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6067 | } |
| 6068 | } |
| 6069 | |
| 6070 | /* 64-bit form */ |
| 6071 | else if (inst->size == 8) |
| 6072 | { |
| 6073 | /* dstp == src1p in memory */ |
| 6074 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6075 | emit_sar_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // sar [dstp],src2p |
| 6076 | |
| 6077 | /* general case */ |
| 6078 | else |
| 6079 | { |
| 6080 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6081 | emit_sar_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // sar dstreg,src2p |
| 6082 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6083 | } |
| 6084 | } |
| 6085 | return dst; |
| 6086 | } |
| 6087 | |
| 6088 | |
| 6089 | /*------------------------------------------------- |
| 6090 | op_rol - process a rol opcode |
| 6091 | -------------------------------------------------*/ |
| 6092 | |
| 6093 | static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6094 | { |
| 6095 | drcuml_parameter dstp, src1p, src2p; |
| 6096 | int dstreg; |
| 6097 | |
| 6098 | /* validate instruction */ |
| 6099 | assert(inst->size == 4 || inst->size == 8); |
| 6100 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6101 | |
| 6102 | /* normalize parameters */ |
| 6103 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6104 | |
| 6105 | /* degenerate cases -- convert to a move */ |
| 6106 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6107 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6108 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6109 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6110 | |
| 6111 | /* pick a target register for the general case */ |
| 6112 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6113 | |
| 6114 | /* 32-bit form */ |
| 6115 | if (inst->size == 4) |
| 6116 | { |
| 6117 | /* dstp == src1p in memory */ |
| 6118 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6119 | emit_rol_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rol [dstp],src2p |
| 6120 | |
| 6121 | /* general case */ |
| 6122 | else |
| 6123 | { |
| 6124 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6125 | emit_rol_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rol dstreg,src2p |
| 6126 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | /* 64-bit form */ |
| 6131 | else if (inst->size == 8) |
| 6132 | { |
| 6133 | /* dstp == src1p in memory */ |
| 6134 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6135 | emit_rol_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rol [dstp],src2p |
| 6136 | |
| 6137 | /* general case */ |
| 6138 | else |
| 6139 | { |
| 6140 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6141 | emit_rol_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // rol dstreg,src2p |
| 6142 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6143 | } |
| 6144 | } |
| 6145 | return dst; |
| 6146 | } |
| 6147 | |
| 6148 | |
| 6149 | /*------------------------------------------------- |
| 6150 | op_ror - process a ROR opcode |
| 6151 | -------------------------------------------------*/ |
| 6152 | |
| 6153 | static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6154 | { |
| 6155 | drcuml_parameter dstp, src1p, src2p; |
| 6156 | int dstreg; |
| 6157 | |
| 6158 | /* validate instruction */ |
| 6159 | assert(inst->size == 4 || inst->size == 8); |
| 6160 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6161 | |
| 6162 | /* normalize parameters */ |
| 6163 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6164 | |
| 6165 | /* degenerate cases -- convert to a move */ |
| 6166 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6167 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6168 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6169 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6170 | |
| 6171 | /* pick a target register for the general case */ |
| 6172 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6173 | |
| 6174 | /* 32-bit form */ |
| 6175 | if (inst->size == 4) |
| 6176 | { |
| 6177 | /* dstp == src1p in memory */ |
| 6178 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6179 | emit_ror_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // ror [dstp],src2p |
| 6180 | |
| 6181 | /* general case */ |
| 6182 | else |
| 6183 | { |
| 6184 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6185 | emit_ror_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // ror dstreg,src2p |
| 6186 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6187 | } |
| 6188 | } |
| 6189 | |
| 6190 | /* 64-bit form */ |
| 6191 | else if (inst->size == 8) |
| 6192 | { |
| 6193 | /* dstp == src1p in memory */ |
| 6194 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6195 | emit_ror_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // ror [dstp],src2p |
| 6196 | |
| 6197 | /* general case */ |
| 6198 | else |
| 6199 | { |
| 6200 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6201 | emit_ror_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // ror dstreg,src2p |
| 6202 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6203 | } |
| 6204 | } |
| 6205 | return dst; |
| 6206 | } |
| 6207 | |
| 6208 | |
| 6209 | /*------------------------------------------------- |
| 6210 | op_rolc - process a ROLC opcode |
| 6211 | -------------------------------------------------*/ |
| 6212 | |
| 6213 | static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6214 | { |
| 6215 | drcuml_parameter dstp, src1p, src2p; |
| 6216 | int dstreg; |
| 6217 | |
| 6218 | /* validate instruction */ |
| 6219 | assert(inst->size == 4 || inst->size == 8); |
| 6220 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6221 | |
| 6222 | /* normalize parameters */ |
| 6223 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6224 | |
| 6225 | /* degenerate cases -- convert to a move */ |
| 6226 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6227 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6228 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6229 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6230 | |
| 6231 | /* pick a target register for the general case */ |
| 6232 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6233 | |
| 6234 | /* 32-bit form */ |
| 6235 | if (inst->size == 4) |
| 6236 | { |
| 6237 | /* dstp == src1p in memory */ |
| 6238 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6239 | emit_rcl_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rcl [dstp],src2p |
| 6240 | |
| 6241 | /* general case */ |
| 6242 | else |
| 6243 | { |
| 6244 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6245 | emit_rcl_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rcl dstreg,src2p |
| 6246 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6247 | } |
| 6248 | } |
| 6249 | |
| 6250 | /* 64-bit form */ |
| 6251 | else if (inst->size == 8) |
| 6252 | { |
| 6253 | /* dstp == src1p in memory */ |
| 6254 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6255 | emit_rcl_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rcl [dstp],src2p |
| 6256 | |
| 6257 | /* general case */ |
| 6258 | else |
| 6259 | { |
| 6260 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6261 | emit_rcl_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // rcl dstreg,src2p |
| 6262 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6263 | } |
| 6264 | } |
| 6265 | return dst; |
| 6266 | } |
| 6267 | |
| 6268 | |
| 6269 | /*------------------------------------------------- |
| 6270 | op_rorc - process a RORC opcode |
| 6271 | -------------------------------------------------*/ |
| 6272 | |
| 6273 | static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6274 | { |
| 6275 | drcuml_parameter dstp, src1p, src2p; |
| 6276 | int dstreg; |
| 6277 | |
| 6278 | /* validate instruction */ |
| 6279 | assert(inst->size == 4 || inst->size == 8); |
| 6280 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6281 | |
| 6282 | /* normalize parameters */ |
| 6283 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6284 | |
| 6285 | /* degenerate cases -- convert to a move */ |
| 6286 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6287 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6288 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6289 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6290 | |
| 6291 | /* pick a target register for the general case */ |
| 6292 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6293 | |
| 6294 | /* 32-bit form */ |
| 6295 | if (inst->size == 4) |
| 6296 | { |
| 6297 | /* dstp == src1p in memory */ |
| 6298 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6299 | emit_rcr_m32_p32(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rcr [dstp],src2p |
| 6300 | |
| 6301 | /* general case */ |
| 6302 | else |
| 6303 | { |
| 6304 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6305 | emit_rcr_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rcr dstreg,src2p |
| 6306 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6307 | } |
| 6308 | } |
| 6309 | |
| 6310 | /* 64-bit form */ |
| 6311 | else if (inst->size == 8) |
| 6312 | { |
| 6313 | /* dstp == src1p in memory */ |
| 6314 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6315 | emit_rcr_m64_p64(drcbe, &dst, MABS(drcbe, dstp.value), &src2p, inst); // rcr [dstp],src2p |
| 6316 | |
| 6317 | /* general case */ |
| 6318 | else |
| 6319 | { |
| 6320 | emit_mov_r64_p64(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6321 | emit_rcr_r64_p64(drcbe, &dst, dstreg, &src2p, inst); // rcr dstreg,src2p |
| 6322 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6323 | } |
| 6324 | } |
| 6325 | return dst; |
| 6326 | } |
| 6327 | |
| 6328 | |
| 6329 | |
| 6330 | /*************************************************************************** |
| 6331 | FLOATING POINT OPERATIONS |
| 6332 | ***************************************************************************/ |
| 6333 | |
| 6334 | /*------------------------------------------------- |
| 6335 | op_fload - process a FLOAD opcode |
| 6336 | -------------------------------------------------*/ |
| 6337 | |
| 6338 | static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6339 | { |
| 6340 | drcuml_parameter dstp, basep, indp; |
| 6341 | int basereg, dstreg; |
| 6342 | INT32 baseoffs; |
| 6343 | |
| 6344 | /* validate instruction */ |
| 6345 | assert(inst->size == 4 || inst->size == 8); |
| 6346 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6347 | |
| 6348 | /* normalize parameters */ |
| 6349 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 6350 | |
| 6351 | /* pick a target register for the general case */ |
| 6352 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 6353 | |
| 6354 | /* determine the pointer base */ |
| 6355 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 6356 | |
| 6357 | /* 32-bit form */ |
| 6358 | if (inst->size == 4) |
| 6359 | { |
| 6360 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6361 | emit_movss_r128_m32(&dst, dstreg, MBD(basereg, baseoffs + 4*indp.value)); // movss dstreg,[basep + 4*indp] |
| 6362 | else |
| 6363 | { |
| 6364 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6365 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 6366 | emit_movss_r128_m32(&dst, dstreg, MBISD(basereg, indreg, 4, baseoffs)); // movss dstreg,[basep + 4*indp] |
| 6367 | } |
| 6368 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 6369 | } |
| 6370 | |
| 6371 | /* 64-bit form */ |
| 6372 | else if (inst->size == 8) |
| 6373 | { |
| 6374 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6375 | emit_movsd_r128_m64(&dst, dstreg, MBD(basereg, baseoffs + 8*indp.value)); // movsd dstreg,[basep + 8*indp] |
| 6376 | else |
| 6377 | { |
| 6378 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6379 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 6380 | emit_movsd_r128_m64(&dst, dstreg, MBISD(basereg, indreg, 8, baseoffs)); // movsd dstreg,[basep + 8*indp] |
| 6381 | } |
| 6382 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 6383 | } |
| 6384 | return dst; |
| 6385 | } |
| 6386 | |
| 6387 | |
| 6388 | /*------------------------------------------------- |
| 6389 | op_fstore - process a FSTORE opcode |
| 6390 | -------------------------------------------------*/ |
| 6391 | |
| 6392 | static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6393 | { |
| 6394 | drcuml_parameter srcp, basep, indp; |
| 6395 | int basereg, srcreg; |
| 6396 | INT32 baseoffs; |
| 6397 | |
| 6398 | /* validate instruction */ |
| 6399 | assert(inst->size == 4 || inst->size == 8); |
| 6400 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6401 | |
| 6402 | /* normalize parameters */ |
| 6403 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MF); |
| 6404 | |
| 6405 | /* pick a target register for the general case */ |
| 6406 | srcreg = param_select_register(REG_XMM0, &srcp, NULL); |
| 6407 | |
| 6408 | /* determine the pointer base */ |
| 6409 | basereg = get_base_register_and_offset(drcbe, &dst, basep.value, REG_RDX, &baseoffs); |
| 6410 | |
| 6411 | /* 32-bit form */ |
| 6412 | if (inst->size == 4) |
| 6413 | { |
| 6414 | emit_movss_r128_p32(drcbe, &dst, srcreg, &srcp); // movss srcreg,srcp |
| 6415 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6416 | emit_movss_m32_r128(&dst, MBD(basereg, baseoffs + 4*indp.value), srcreg); // movss [basep + 4*indp],srcreg |
| 6417 | else |
| 6418 | { |
| 6419 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6420 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 6421 | emit_movss_m32_r128(&dst, MBISD(basereg, indreg, 4, baseoffs), srcreg); // movss [basep + 4*indp],srcreg |
| 6422 | } |
| 6423 | } |
| 6424 | |
| 6425 | /* 64-bit form */ |
| 6426 | else if (inst->size == 8) |
| 6427 | { |
| 6428 | emit_movsd_r128_p64(drcbe, &dst, srcreg, &srcp); // movsd srcreg,srcp |
| 6429 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6430 | emit_movsd_m64_r128(&dst, MBD(basereg, baseoffs + 8*indp.value), srcreg); // movsd [basep + 8*indp],srcreg |
| 6431 | else |
| 6432 | { |
| 6433 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6434 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 6435 | emit_movsd_m64_r128(&dst, MBISD(basereg, indreg, 8, baseoffs), srcreg); // movsd [basep + 8*indp],srcreg |
| 6436 | } |
| 6437 | } |
| 6438 | return dst; |
| 6439 | } |
| 6440 | |
| 6441 | |
| 6442 | /*------------------------------------------------- |
| 6443 | op_fread - process a FREAD opcode |
| 6444 | -------------------------------------------------*/ |
| 6445 | |
| 6446 | static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6447 | { |
| 6448 | drcuml_parameter dstp, spacep, addrp; |
| 6449 | |
| 6450 | /* validate instruction */ |
| 6451 | assert(inst->size == 4 || inst->size == 8); |
| 6452 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6453 | |
| 6454 | /* normalize parameters */ |
| 6455 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 6456 | |
| 6457 | /* set up a call to the read dword/qword handler */ |
| 6458 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 6459 | if (inst->size == 4) |
| 6460 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_dword);// call read_dword |
| 6461 | else if (inst->size == 8) |
| 6462 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].read_qword);// call read_qword |
| 6463 | |
| 6464 | /* store result */ |
| 6465 | if (inst->size == 4) |
| 6466 | { |
| 6467 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6468 | emit_mov_m32_r32(&dst, MABS(drcbe, dstp.value), REG_EAX); // mov [dstp],eax |
| 6469 | else if (dstp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6470 | emit_movd_r128_r32(&dst, dstp.value, REG_EAX); // movd dstp,eax |
| 6471 | } |
| 6472 | else if (inst->size == 8) |
| 6473 | { |
| 6474 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6475 | emit_mov_m64_r64(&dst, MABS(drcbe, dstp.value), REG_RAX); // mov [dstp],rax |
| 6476 | else if (dstp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6477 | emit_movq_r128_r64(&dst, dstp.value, REG_RAX); // movq dstp,rax |
| 6478 | } |
| 6479 | return dst; |
| 6480 | } |
| 6481 | |
| 6482 | |
| 6483 | /*------------------------------------------------- |
| 6484 | op_fwrite - process a FWRITE opcode |
| 6485 | -------------------------------------------------*/ |
| 6486 | |
| 6487 | static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6488 | { |
| 6489 | drcuml_parameter spacep, addrp, srcp; |
| 6490 | |
| 6491 | /* validate instruction */ |
| 6492 | assert(inst->size == 4 || inst->size == 8); |
| 6493 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6494 | |
| 6495 | /* normalize parameters */ |
| 6496 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MF); |
| 6497 | |
| 6498 | /* general case */ |
| 6499 | emit_mov_r32_p32(drcbe, &dst, REG_PARAM1, &addrp); // mov param1,addrp |
| 6500 | |
| 6501 | /* 32-bit form */ |
| 6502 | if (inst->size == 4) |
| 6503 | { |
| 6504 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6505 | emit_mov_r32_m32(&dst, REG_PARAM2, MABS(drcbe, srcp.value)); // mov param2,[srcp] |
| 6506 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6507 | emit_movd_r32_r128(&dst, REG_PARAM2, srcp.value); // movd param2,srcp |
| 6508 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_dword);// call write_dword |
| 6509 | } |
| 6510 | |
| 6511 | /* 64-bit form */ |
| 6512 | else if (inst->size == 8) |
| 6513 | { |
| 6514 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6515 | emit_mov_r64_m64(&dst, REG_PARAM2, MABS(drcbe, srcp.value)); // mov param2,[srcp] |
| 6516 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6517 | emit_movq_r64_r128(&dst, REG_PARAM2, srcp.value); // movq param2,srcp |
| 6518 | emit_smart_call_m64(drcbe, &dst, (x86code **)&drcbe->accessors[spacep.value].write_qword);// call write_qword |
| 6519 | } |
| 6520 | |
| 6521 | return dst; |
| 6522 | } |
| 6523 | |
| 6524 | |
| 6525 | /*------------------------------------------------- |
| 6526 | op_fmov - process a FMOV opcode |
| 6527 | -------------------------------------------------*/ |
| 6528 | |
| 6529 | static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6530 | { |
| 6531 | drcuml_parameter dstp, srcp; |
| 6532 | emit_link skip = { 0 }; |
| 6533 | int dstreg; |
| 6534 | |
| 6535 | /* validate instruction */ |
| 6536 | assert(inst->size == 4 || inst->size == 8); |
| 6537 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 6538 | |
| 6539 | /* normalize parameters */ |
| 6540 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 6541 | |
| 6542 | /* pick a target register for the general case */ |
| 6543 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 6544 | |
| 6545 | /* always start with a jmp */ |
| 6546 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 6547 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 6548 | |
| 6549 | /* 32-bit form */ |
| 6550 | if (inst->size == 4) |
| 6551 | { |
| 6552 | emit_movss_r128_p32(drcbe, &dst, dstreg, &srcp); // movss dstreg,srcp |
| 6553 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 6554 | } |
| 6555 | |
| 6556 | /* 64-bit form */ |
| 6557 | else if (inst->size == 8) |
| 6558 | { |
| 6559 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &srcp); // movsd dstreg,srcp |
| 6560 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 6561 | } |
| 6562 | |
| 6563 | /* resolve the jump */ |
| 6564 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 6565 | resolve_link(&dst, &skip); // skip: |
| 6566 | return dst; |
| 6567 | } |
| 6568 | |
| 6569 | |
| 6570 | /*------------------------------------------------- |
| 6571 | op_ftoi4 - process a FTOI4 opcode |
| 6572 | -------------------------------------------------*/ |
| 6573 | |
| 6574 | static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6575 | { |
| 6576 | drcuml_parameter dstp, srcp; |
| 6577 | int dstreg; |
| 6578 | |
| 6579 | /* validate instruction */ |
| 6580 | assert(inst->size == 4 || inst->size == 8); |
| 6581 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6582 | |
| 6583 | /* normalize parameters */ |
| 6584 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6585 | |
| 6586 | /* pick a target register for the general case */ |
| 6587 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6588 | |
| 6589 | /* 32-bit form */ |
| 6590 | if (inst->size == 4) |
| 6591 | { |
| 6592 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6593 | emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6594 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6595 | emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6596 | } |
| 6597 | |
| 6598 | /* 64-bit form */ |
| 6599 | else if (inst->size == 8) |
| 6600 | { |
| 6601 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6602 | emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6603 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6604 | emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6605 | } |
| 6606 | |
| 6607 | /* general case */ |
| 6608 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6609 | return dst; |
| 6610 | } |
| 6611 | |
| 6612 | |
| 6613 | /*------------------------------------------------- |
| 6614 | op_ftoi4t - process a FTOI4T opcode |
| 6615 | -------------------------------------------------*/ |
| 6616 | |
| 6617 | static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6618 | { |
| 6619 | drcuml_parameter dstp, srcp; |
| 6620 | int dstreg; |
| 6621 | |
| 6622 | /* validate instruction */ |
| 6623 | assert(inst->size == 4 || inst->size == 8); |
| 6624 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6625 | |
| 6626 | /* normalize parameters */ |
| 6627 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6628 | |
| 6629 | /* pick a target register for the general case */ |
| 6630 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6631 | |
| 6632 | /* 32-bit form */ |
| 6633 | if (inst->size == 4) |
| 6634 | { |
| 6635 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6636 | emit_cvttss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvttss2si dstreg,[srcp] |
| 6637 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6638 | emit_cvttss2si_r32_r128(&dst, dstreg, srcp.value); // cvttss2si dstreg,srcp |
| 6639 | } |
| 6640 | |
| 6641 | /* 64-bit form */ |
| 6642 | else if (inst->size == 8) |
| 6643 | { |
| 6644 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6645 | emit_cvttsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvttsd2si dstreg,[srcp] |
| 6646 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6647 | emit_cvttsd2si_r32_r128(&dst, dstreg, srcp.value); // cvttsd2si dstreg,srcp |
| 6648 | } |
| 6649 | |
| 6650 | /* general case */ |
| 6651 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6652 | return dst; |
| 6653 | } |
| 6654 | |
| 6655 | |
| 6656 | /*------------------------------------------------- |
| 6657 | op_ftoi4r - process a FTOI4R opcode |
| 6658 | -------------------------------------------------*/ |
| 6659 | |
| 6660 | static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6661 | { |
| 6662 | drcuml_parameter dstp, srcp; |
| 6663 | int dstreg; |
| 6664 | |
| 6665 | /* validate instruction */ |
| 6666 | assert(inst->size == 4 || inst->size == 8); |
| 6667 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6668 | |
| 6669 | /* normalize parameters */ |
| 6670 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6671 | |
| 6672 | /* pick a target register for the general case */ |
| 6673 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6674 | |
| 6675 | /* save and set the control word */ |
| 6676 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 6677 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_ROUND])); // ldmxcsr fpcontrol[DRCUML_FMOD_ROUND] |
| 6678 | |
| 6679 | /* 32-bit form */ |
| 6680 | if (inst->size == 4) |
| 6681 | { |
| 6682 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6683 | emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6684 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6685 | emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6686 | } |
| 6687 | |
| 6688 | /* 64-bit form */ |
| 6689 | else if (inst->size == 8) |
| 6690 | { |
| 6691 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6692 | emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6693 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6694 | emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6695 | } |
| 6696 | |
| 6697 | /* restore control word and proceed */ |
| 6698 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6699 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 6700 | return dst; |
| 6701 | } |
| 6702 | |
| 6703 | |
| 6704 | /*------------------------------------------------- |
| 6705 | op_ftoi4f - process a FTOI4F opcode |
| 6706 | -------------------------------------------------*/ |
| 6707 | |
| 6708 | static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6709 | { |
| 6710 | drcuml_parameter dstp, srcp; |
| 6711 | int dstreg; |
| 6712 | |
| 6713 | /* validate instruction */ |
| 6714 | assert(inst->size == 4 || inst->size == 8); |
| 6715 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6716 | |
| 6717 | /* normalize parameters */ |
| 6718 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6719 | |
| 6720 | /* pick a target register for the general case */ |
| 6721 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6722 | |
| 6723 | /* save and set the control word */ |
| 6724 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 6725 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_FLOOR])); // ldmxcsr fpcontrol[DRCUML_FMOD_FLOOR] |
| 6726 | |
| 6727 | /* 32-bit form */ |
| 6728 | if (inst->size == 4) |
| 6729 | { |
| 6730 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6731 | emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6732 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6733 | emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6734 | } |
| 6735 | |
| 6736 | /* 64-bit form */ |
| 6737 | else if (inst->size == 8) |
| 6738 | { |
| 6739 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6740 | emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6741 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6742 | emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6743 | } |
| 6744 | |
| 6745 | /* restore control word and proceed */ |
| 6746 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6747 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 6748 | return dst; |
| 6749 | } |
| 6750 | |
| 6751 | |
| 6752 | /*------------------------------------------------- |
| 6753 | op_ftoi4c - process a FTOI4C opcode |
| 6754 | -------------------------------------------------*/ |
| 6755 | |
| 6756 | static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6757 | { |
| 6758 | drcuml_parameter dstp, srcp; |
| 6759 | int dstreg; |
| 6760 | |
| 6761 | /* validate instruction */ |
| 6762 | assert(inst->size == 4 || inst->size == 8); |
| 6763 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6764 | |
| 6765 | /* normalize parameters */ |
| 6766 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6767 | |
| 6768 | /* pick a target register for the general case */ |
| 6769 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6770 | |
| 6771 | /* save and set the control word */ |
| 6772 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 6773 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_CEIL])); // ldmxcsr fpcontrol[DRCUML_FMOD_CEIL] |
| 6774 | |
| 6775 | /* 32-bit form */ |
| 6776 | if (inst->size == 4) |
| 6777 | { |
| 6778 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6779 | emit_cvtss2si_r32_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6780 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6781 | emit_cvtss2si_r32_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6782 | } |
| 6783 | |
| 6784 | /* 64-bit form */ |
| 6785 | else if (inst->size == 8) |
| 6786 | { |
| 6787 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6788 | emit_cvtsd2si_r32_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6789 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6790 | emit_cvtsd2si_r32_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6791 | } |
| 6792 | |
| 6793 | /* restore control word and proceed */ |
| 6794 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6795 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 6796 | return dst; |
| 6797 | } |
| 6798 | |
| 6799 | |
| 6800 | /*------------------------------------------------- |
| 6801 | op_ftoi8 - process a FTOI8 opcode |
| 6802 | -------------------------------------------------*/ |
| 6803 | |
| 6804 | static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6805 | { |
| 6806 | drcuml_parameter dstp, srcp; |
| 6807 | int dstreg; |
| 6808 | |
| 6809 | /* validate instruction */ |
| 6810 | assert(inst->size == 4 || inst->size == 8); |
| 6811 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6812 | |
| 6813 | /* normalize parameters */ |
| 6814 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6815 | |
| 6816 | /* pick a target register for the general case */ |
| 6817 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6818 | |
| 6819 | /* 32-bit form */ |
| 6820 | if (inst->size == 4) |
| 6821 | { |
| 6822 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6823 | emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6824 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6825 | emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6826 | } |
| 6827 | |
| 6828 | /* 64-bit form */ |
| 6829 | else if (inst->size == 8) |
| 6830 | { |
| 6831 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6832 | emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6833 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6834 | emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6835 | } |
| 6836 | |
| 6837 | /* general case */ |
| 6838 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6839 | return dst; |
| 6840 | } |
| 6841 | |
| 6842 | |
| 6843 | /*------------------------------------------------- |
| 6844 | op_ftoi8t - process a FTOI8T opcode |
| 6845 | -------------------------------------------------*/ |
| 6846 | |
| 6847 | static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6848 | { |
| 6849 | drcuml_parameter dstp, srcp; |
| 6850 | int dstreg; |
| 6851 | |
| 6852 | /* validate instruction */ |
| 6853 | assert(inst->size == 4 || inst->size == 8); |
| 6854 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6855 | |
| 6856 | /* normalize parameters */ |
| 6857 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6858 | |
| 6859 | /* pick a target register for the general case */ |
| 6860 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6861 | |
| 6862 | /* 32-bit form */ |
| 6863 | if (inst->size == 4) |
| 6864 | { |
| 6865 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6866 | emit_cvttss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvttss2si dstreg,[srcp] |
| 6867 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6868 | emit_cvttss2si_r64_r128(&dst, dstreg, srcp.value); // cvttss2si dstreg,srcp |
| 6869 | } |
| 6870 | |
| 6871 | /* 64-bit form */ |
| 6872 | else if (inst->size == 8) |
| 6873 | { |
| 6874 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6875 | emit_cvttsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvttsd2si dstreg,[srcp] |
| 6876 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6877 | emit_cvttsd2si_r64_r128(&dst, dstreg, srcp.value); // cvttsd2si dstreg,srcp |
| 6878 | } |
| 6879 | |
| 6880 | /* general case */ |
| 6881 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6882 | return dst; |
| 6883 | } |
| 6884 | |
| 6885 | |
| 6886 | /*------------------------------------------------- |
| 6887 | op_ftoi8r - process a FTOI8R opcode |
| 6888 | -------------------------------------------------*/ |
| 6889 | |
| 6890 | static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6891 | { |
| 6892 | drcuml_parameter dstp, srcp; |
| 6893 | int dstreg; |
| 6894 | |
| 6895 | /* validate instruction */ |
| 6896 | assert(inst->size == 4 || inst->size == 8); |
| 6897 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6898 | |
| 6899 | /* normalize parameters */ |
| 6900 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6901 | |
| 6902 | /* pick a target register for the general case */ |
| 6903 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6904 | |
| 6905 | /* save and set the control word */ |
| 6906 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 6907 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_ROUND])); // ldmxcsr fpcontrol[DRCUML_FMOD_ROUND] |
| 6908 | |
| 6909 | /* 32-bit form */ |
| 6910 | if (inst->size == 4) |
| 6911 | { |
| 6912 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6913 | emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6914 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6915 | emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6916 | } |
| 6917 | |
| 6918 | /* 64-bit form */ |
| 6919 | else if (inst->size == 8) |
| 6920 | { |
| 6921 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6922 | emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6923 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6924 | emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6925 | } |
| 6926 | |
| 6927 | /* restore control word and proceed */ |
| 6928 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6929 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 6930 | return dst; |
| 6931 | } |
| 6932 | |
| 6933 | |
| 6934 | /*------------------------------------------------- |
| 6935 | op_ftoi8f - process a FTOI8F opcode |
| 6936 | -------------------------------------------------*/ |
| 6937 | |
| 6938 | static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6939 | { |
| 6940 | drcuml_parameter dstp, srcp; |
| 6941 | int dstreg; |
| 6942 | |
| 6943 | /* validate instruction */ |
| 6944 | assert(inst->size == 4 || inst->size == 8); |
| 6945 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6946 | |
| 6947 | /* normalize parameters */ |
| 6948 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6949 | |
| 6950 | /* pick a target register for the general case */ |
| 6951 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 6952 | |
| 6953 | /* save and set the control word */ |
| 6954 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 6955 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_FLOOR])); // ldmxcsr fpcontrol[DRCUML_FMOD_FLOOR] |
| 6956 | |
| 6957 | /* 32-bit form */ |
| 6958 | if (inst->size == 4) |
| 6959 | { |
| 6960 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6961 | emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 6962 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6963 | emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 6964 | } |
| 6965 | |
| 6966 | /* 64-bit form */ |
| 6967 | else if (inst->size == 8) |
| 6968 | { |
| 6969 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6970 | emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 6971 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 6972 | emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 6973 | } |
| 6974 | |
| 6975 | /* restore control word and proceed */ |
| 6976 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6977 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 6978 | return dst; |
| 6979 | } |
| 6980 | |
| 6981 | |
| 6982 | /*------------------------------------------------- |
| 6983 | op_ftoi8c - process a FTOI8C opcode |
| 6984 | -------------------------------------------------*/ |
| 6985 | |
| 6986 | static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6987 | { |
| 6988 | drcuml_parameter dstp, srcp; |
| 6989 | int dstreg; |
| 6990 | |
| 6991 | /* validate instruction */ |
| 6992 | assert(inst->size == 4 || inst->size == 8); |
| 6993 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6994 | |
| 6995 | /* normalize parameters */ |
| 6996 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6997 | |
| 6998 | /* pick a target register for the general case */ |
| 6999 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 7000 | |
| 7001 | /* save and set the control word */ |
| 7002 | emit_stmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // stmxcsr [ssemodesave] |
| 7003 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssecontrol[DRCUML_FMOD_CEIL])); // ldmxcsr fpcontrol[DRCUML_FMOD_CEIL] |
| 7004 | |
| 7005 | /* 32-bit form */ |
| 7006 | if (inst->size == 4) |
| 7007 | { |
| 7008 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7009 | emit_cvtss2si_r64_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2si dstreg,[srcp] |
| 7010 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7011 | emit_cvtss2si_r64_r128(&dst, dstreg, srcp.value); // cvtss2si dstreg,srcp |
| 7012 | } |
| 7013 | |
| 7014 | /* 64-bit form */ |
| 7015 | else if (inst->size == 8) |
| 7016 | { |
| 7017 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7018 | emit_cvtsd2si_r64_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2si dstreg,[srcp] |
| 7019 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7020 | emit_cvtsd2si_r64_r128(&dst, dstreg, srcp.value); // cvtsd2si dstreg,srcp |
| 7021 | } |
| 7022 | |
| 7023 | /* restore control word and proceed */ |
| 7024 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 7025 | emit_ldmxcsr_m32(&dst, MABS(drcbe, &drcbe->ssemodesave)); // ldmxcsr [ssemodesave] |
| 7026 | return dst; |
| 7027 | } |
| 7028 | |
| 7029 | |
| 7030 | /*------------------------------------------------- |
| 7031 | op_ffrfs - process a FFRFS opcode |
| 7032 | -------------------------------------------------*/ |
| 7033 | |
| 7034 | static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7035 | { |
| 7036 | drcuml_parameter dstp, srcp; |
| 7037 | int dstreg; |
| 7038 | |
| 7039 | /* validate instruction */ |
| 7040 | assert(inst->size == 8); |
| 7041 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7042 | |
| 7043 | /* normalize parameters */ |
| 7044 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7045 | |
| 7046 | /* pick a target register for the general case */ |
| 7047 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7048 | |
| 7049 | /* general case */ |
| 7050 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7051 | emit_cvtss2sd_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtss2sd dstreg,[srcp] |
| 7052 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7053 | emit_cvtss2sd_r128_r128(&dst, dstreg, srcp.value); // cvtss2sd dstreg,srcp |
| 7054 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7055 | |
| 7056 | return dst; |
| 7057 | } |
| 7058 | |
| 7059 | |
| 7060 | /*------------------------------------------------- |
| 7061 | op_ffrfd - process a FFRFD opcode |
| 7062 | -------------------------------------------------*/ |
| 7063 | |
| 7064 | static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7065 | { |
| 7066 | drcuml_parameter dstp, srcp; |
| 7067 | int dstreg; |
| 7068 | |
| 7069 | /* validate instruction */ |
| 7070 | assert(inst->size == 4); |
| 7071 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7072 | |
| 7073 | /* normalize parameters */ |
| 7074 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7075 | |
| 7076 | /* pick a target register for the general case */ |
| 7077 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7078 | |
| 7079 | /* general case */ |
| 7080 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7081 | emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2ss dstreg,[srcp] |
| 7082 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7083 | emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value); // cvtsd2ss dstreg,srcp |
| 7084 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7085 | |
| 7086 | return dst; |
| 7087 | } |
| 7088 | |
| 7089 | |
| 7090 | /*------------------------------------------------- |
| 7091 | op_ffri4 - process a FFRI4 opcode |
| 7092 | -------------------------------------------------*/ |
| 7093 | |
| 7094 | static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7095 | { |
| 7096 | drcuml_parameter dstp, srcp; |
| 7097 | int dstreg; |
| 7098 | |
| 7099 | /* validate instruction */ |
| 7100 | assert(inst->size == 4 || inst->size == 8); |
| 7101 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7102 | |
| 7103 | /* normalize parameters */ |
| 7104 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7105 | |
| 7106 | /* pick a target register for the general case */ |
| 7107 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7108 | |
| 7109 | /* 32-bit form */ |
| 7110 | if (inst->size == 4) |
| 7111 | { |
| 7112 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7113 | emit_cvtsi2ss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsi2ss dstreg,[srcp] |
| 7114 | else |
| 7115 | { |
| 7116 | int srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 7117 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 7118 | emit_cvtsi2ss_r128_r32(&dst, dstreg, srcreg); // cvtsi2ss dstreg,srcreg |
| 7119 | } |
| 7120 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7121 | } |
| 7122 | |
| 7123 | /* 64-bit form */ |
| 7124 | else if (inst->size == 8) |
| 7125 | { |
| 7126 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7127 | emit_cvtsi2sd_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsi2sd dstreg,[srcp] |
| 7128 | else |
| 7129 | { |
| 7130 | int srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 7131 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 7132 | emit_cvtsi2sd_r128_r32(&dst, dstreg, srcreg); // cvtsi2sd dstreg,srcreg |
| 7133 | } |
| 7134 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7135 | } |
| 7136 | return dst; |
| 7137 | } |
| 7138 | |
| 7139 | |
| 7140 | /*------------------------------------------------- |
| 7141 | op_ffri8 - process a FFRI8 opcode |
| 7142 | -------------------------------------------------*/ |
| 7143 | |
| 7144 | static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7145 | { |
| 7146 | drcuml_parameter dstp, srcp; |
| 7147 | int dstreg; |
| 7148 | |
| 7149 | /* validate instruction */ |
| 7150 | assert(inst->size == 4 || inst->size == 8); |
| 7151 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7152 | |
| 7153 | /* normalize parameters */ |
| 7154 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7155 | |
| 7156 | /* pick a target register for the general case */ |
| 7157 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7158 | |
| 7159 | /* 32-bit form */ |
| 7160 | if (inst->size == 4) |
| 7161 | { |
| 7162 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7163 | emit_cvtsi2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsi2ss dstreg,[srcp] |
| 7164 | else |
| 7165 | { |
| 7166 | int srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 7167 | emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 7168 | emit_cvtsi2ss_r128_r64(&dst, dstreg, srcreg); // cvtsi2ss dstreg,srcreg |
| 7169 | } |
| 7170 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7171 | } |
| 7172 | |
| 7173 | /* 64-bit form */ |
| 7174 | else if (inst->size == 8) |
| 7175 | { |
| 7176 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7177 | emit_cvtsi2sd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsi2sd dstreg,[srcp] |
| 7178 | else |
| 7179 | { |
| 7180 | int srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 7181 | emit_mov_r64_p64(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 7182 | emit_cvtsi2sd_r128_r64(&dst, dstreg, srcreg); // cvtsi2sd dstreg,srcreg |
| 7183 | } |
| 7184 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7185 | } |
| 7186 | return dst; |
| 7187 | } |
| 7188 | |
| 7189 | |
| 7190 | /*------------------------------------------------- |
| 7191 | op_fadd - process a FADD opcode |
| 7192 | -------------------------------------------------*/ |
| 7193 | |
| 7194 | static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7195 | { |
| 7196 | drcuml_parameter dstp, src1p, src2p; |
| 7197 | int dstreg; |
| 7198 | |
| 7199 | /* validate instruction */ |
| 7200 | assert(inst->size == 4 || inst->size == 8); |
| 7201 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7202 | |
| 7203 | /* normalize parameters */ |
| 7204 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7205 | |
| 7206 | /* pick a target register for the general case */ |
| 7207 | dstreg = param_select_register(REG_XMM0, &dstp, &src2p); |
| 7208 | |
| 7209 | /* 32-bit form */ |
| 7210 | if (inst->size == 4) |
| 7211 | { |
| 7212 | emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p); // movss dstreg,src1p |
| 7213 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7214 | emit_addss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value)); // addss dstreg,[src2p] |
| 7215 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7216 | emit_addss_r128_r128(&dst, dstreg, src2p.value); // addss dstreg,src2p |
| 7217 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7218 | } |
| 7219 | |
| 7220 | /* 64-bit form */ |
| 7221 | else if (inst->size == 8) |
| 7222 | { |
| 7223 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p); // movsd dstreg,src1p |
| 7224 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7225 | emit_addsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value)); // addsd dstreg,[src2p] |
| 7226 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7227 | emit_addsd_r128_r128(&dst, dstreg, src2p.value); // addsd dstreg,src2p |
| 7228 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7229 | } |
| 7230 | return dst; |
| 7231 | } |
| 7232 | |
| 7233 | |
| 7234 | /*------------------------------------------------- |
| 7235 | op_fsub - process a FSUB opcode |
| 7236 | -------------------------------------------------*/ |
| 7237 | |
| 7238 | static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7239 | { |
| 7240 | drcuml_parameter dstp, src1p, src2p; |
| 7241 | int dstreg; |
| 7242 | |
| 7243 | /* validate instruction */ |
| 7244 | assert(inst->size == 4 || inst->size == 8); |
| 7245 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7246 | |
| 7247 | /* normalize parameters */ |
| 7248 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7249 | |
| 7250 | /* pick a target register for the general case */ |
| 7251 | dstreg = param_select_register(REG_XMM0, &dstp, &src2p); |
| 7252 | |
| 7253 | /* 32-bit form */ |
| 7254 | if (inst->size == 4) |
| 7255 | { |
| 7256 | emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p); // movss dstreg,src1p |
| 7257 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7258 | emit_subss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value)); // subss dstreg,[src2p] |
| 7259 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7260 | emit_subss_r128_r128(&dst, dstreg, src2p.value); // subss dstreg,src2p |
| 7261 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7262 | } |
| 7263 | |
| 7264 | /* 64-bit form */ |
| 7265 | else if (inst->size == 8) |
| 7266 | { |
| 7267 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p); // movsd dstreg,src1p |
| 7268 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7269 | emit_subsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value)); // subsd dstreg,[src2p] |
| 7270 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7271 | emit_subsd_r128_r128(&dst, dstreg, src2p.value); // subsd dstreg,src2p |
| 7272 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7273 | } |
| 7274 | return dst; |
| 7275 | } |
| 7276 | |
| 7277 | |
| 7278 | /*------------------------------------------------- |
| 7279 | op_fcmp - process a FCMP opcode |
| 7280 | -------------------------------------------------*/ |
| 7281 | |
| 7282 | static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7283 | { |
| 7284 | drcuml_parameter src1p, src2p; |
| 7285 | int src1reg; |
| 7286 | |
| 7287 | /* validate instruction */ |
| 7288 | assert(inst->size == 4 || inst->size == 8); |
| 7289 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_Z | DRCUML_FLAG_U)) == 0); |
| 7290 | |
| 7291 | /* normalize parameters */ |
| 7292 | param_normalize_2(drcbe, inst, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7293 | |
| 7294 | /* pick a target register for the general case */ |
| 7295 | src1reg = param_select_register(REG_XMM0, &src1p, NULL); |
| 7296 | |
| 7297 | /* 32-bit form */ |
| 7298 | if (inst->size == 4) |
| 7299 | { |
| 7300 | emit_movss_r128_p32(drcbe, &dst, src1reg, &src1p); // movss src1reg,src1p |
| 7301 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7302 | emit_comiss_r128_m32(&dst, src1reg, MABS(drcbe, src2p.value)); // comiss src1reg,[src2p] |
| 7303 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7304 | emit_comiss_r128_r128(&dst, src1reg, src2p.value); // comiss src1reg,src2p |
| 7305 | } |
| 7306 | |
| 7307 | /* 64-bit form */ |
| 7308 | else if (inst->size == 8) |
| 7309 | { |
| 7310 | emit_movsd_r128_p64(drcbe, &dst, src1reg, &src1p); // movsd src1reg,src1p |
| 7311 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7312 | emit_comisd_r128_m64(&dst, src1reg, MABS(drcbe, src2p.value)); // comisd src1reg,[src2p] |
| 7313 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7314 | emit_comisd_r128_r128(&dst, src1reg, src2p.value); // comisd src1reg,src2p |
| 7315 | } |
| 7316 | return dst; |
| 7317 | } |
| 7318 | |
| 7319 | |
| 7320 | /*------------------------------------------------- |
| 7321 | op_fmul - process a FMUL opcode |
| 7322 | -------------------------------------------------*/ |
| 7323 | |
| 7324 | static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7325 | { |
| 7326 | drcuml_parameter dstp, src1p, src2p; |
| 7327 | int dstreg; |
| 7328 | |
| 7329 | /* validate instruction */ |
| 7330 | assert(inst->size == 4 || inst->size == 8); |
| 7331 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7332 | |
| 7333 | /* normalize parameters */ |
| 7334 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7335 | |
| 7336 | /* pick a target register for the general case */ |
| 7337 | dstreg = param_select_register(REG_XMM0, &dstp, &src2p); |
| 7338 | |
| 7339 | /* 32-bit form */ |
| 7340 | if (inst->size == 4) |
| 7341 | { |
| 7342 | emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p); // movss dstreg,src1p |
| 7343 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7344 | emit_mulss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value)); // mulss dstreg,[src2p] |
| 7345 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7346 | emit_mulss_r128_r128(&dst, dstreg, src2p.value); // mulss dstreg,src2p |
| 7347 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7348 | } |
| 7349 | |
| 7350 | /* 64-bit form */ |
| 7351 | else if (inst->size == 8) |
| 7352 | { |
| 7353 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p); // movsd dstreg,src1p |
| 7354 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7355 | emit_mulsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value)); // mulsd dstreg,[src2p] |
| 7356 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7357 | emit_mulsd_r128_r128(&dst, dstreg, src2p.value); // mulsd dstreg,src2p |
| 7358 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7359 | } |
| 7360 | return dst; |
| 7361 | } |
| 7362 | |
| 7363 | |
| 7364 | /*------------------------------------------------- |
| 7365 | op_fdiv - process a FDIV opcode |
| 7366 | -------------------------------------------------*/ |
| 7367 | |
| 7368 | static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7369 | { |
| 7370 | drcuml_parameter dstp, src1p, src2p; |
| 7371 | int dstreg; |
| 7372 | |
| 7373 | /* validate instruction */ |
| 7374 | assert(inst->size == 4 || inst->size == 8); |
| 7375 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7376 | |
| 7377 | /* normalize parameters */ |
| 7378 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7379 | |
| 7380 | /* pick a target register for the general case */ |
| 7381 | dstreg = param_select_register(REG_XMM0, &dstp, &src2p); |
| 7382 | |
| 7383 | /* 32-bit form */ |
| 7384 | if (inst->size == 4) |
| 7385 | { |
| 7386 | emit_movss_r128_p32(drcbe, &dst, dstreg, &src1p); // movss dstreg,src1p |
| 7387 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7388 | emit_divss_r128_m32(&dst, dstreg, MABS(drcbe, src2p.value)); // divss dstreg,[src2p] |
| 7389 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7390 | emit_divss_r128_r128(&dst, dstreg, src2p.value); // divss dstreg,src2p |
| 7391 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7392 | } |
| 7393 | |
| 7394 | /* 64-bit form */ |
| 7395 | else if (inst->size == 8) |
| 7396 | { |
| 7397 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &src1p); // movsd dstreg,src1p |
| 7398 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 7399 | emit_divsd_r128_m64(&dst, dstreg, MABS(drcbe, src2p.value)); // divsd dstreg,[src2p] |
| 7400 | else if (src2p.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7401 | emit_divsd_r128_r128(&dst, dstreg, src2p.value); // divsd dstreg,src2p |
| 7402 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7403 | } |
| 7404 | return dst; |
| 7405 | } |
| 7406 | |
| 7407 | |
| 7408 | /*------------------------------------------------- |
| 7409 | op_fneg - process a FNEG opcode |
| 7410 | -------------------------------------------------*/ |
| 7411 | |
| 7412 | static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7413 | { |
| 7414 | drcuml_parameter dstp, srcp; |
| 7415 | int dstreg; |
| 7416 | |
| 7417 | /* validate instruction */ |
| 7418 | assert(inst->size == 4 || inst->size == 8); |
| 7419 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7420 | |
| 7421 | /* normalize parameters */ |
| 7422 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7423 | |
| 7424 | /* pick a target register for the general case */ |
| 7425 | dstreg = param_select_register(REG_XMM0, &dstp, &srcp); |
| 7426 | |
| 7427 | /* 32-bit form */ |
| 7428 | if (inst->size == 4) |
| 7429 | { |
| 7430 | emit_xorps_r128_r128(&dst, dstreg, dstreg); // xorps dstreg,dstreg |
| 7431 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7432 | emit_subss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // subss dstreg,[srcp] |
| 7433 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7434 | emit_subss_r128_r128(&dst, dstreg, srcp.value); // subss dstreg,srcp |
| 7435 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7436 | } |
| 7437 | |
| 7438 | /* 64-bit form */ |
| 7439 | else if (inst->size == 8) |
| 7440 | { |
| 7441 | emit_xorpd_r128_r128(&dst, dstreg, dstreg); // xorpd dstreg,dstreg |
| 7442 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7443 | emit_subsd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // subsd dstreg,[srcp] |
| 7444 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7445 | emit_subsd_r128_r128(&dst, dstreg, srcp.value); // subsd dstreg,srcp |
| 7446 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7447 | } |
| 7448 | return dst; |
| 7449 | } |
| 7450 | |
| 7451 | |
| 7452 | /*------------------------------------------------- |
| 7453 | op_fabs - process a FABS opcode |
| 7454 | -------------------------------------------------*/ |
| 7455 | |
| 7456 | static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7457 | { |
| 7458 | drcuml_parameter dstp, srcp; |
| 7459 | int dstreg; |
| 7460 | |
| 7461 | /* validate instruction */ |
| 7462 | assert(inst->size == 4 || inst->size == 8); |
| 7463 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7464 | |
| 7465 | /* normalize parameters */ |
| 7466 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7467 | |
| 7468 | /* pick a target register for the general case */ |
| 7469 | dstreg = param_select_register(REG_XMM0, &dstp, &srcp); |
| 7470 | |
| 7471 | /* 32-bit form */ |
| 7472 | if (inst->size == 4) |
| 7473 | { |
| 7474 | emit_movss_r128_p32(drcbe, &dst, dstreg, &srcp); // movss dstreg,srcp |
| 7475 | emit_andps_r128_m128(&dst, dstreg, MABS(drcbe, drcbe->absmask32)); // andps dstreg,[absmask32] |
| 7476 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7477 | } |
| 7478 | |
| 7479 | /* 64-bit form */ |
| 7480 | else if (inst->size == 8) |
| 7481 | { |
| 7482 | emit_movsd_r128_p64(drcbe, &dst, dstreg, &srcp); // movsd dstreg,srcp |
| 7483 | emit_andpd_r128_m128(&dst, dstreg, MABS(drcbe, drcbe->absmask64)); // andpd dstreg,[absmask64] |
| 7484 | emit_movsd_p64_r128(drcbe, &dst, &srcp, dstreg); // movsd dstp,dstreg |
| 7485 | } |
| 7486 | return dst; |
| 7487 | } |
| 7488 | |
| 7489 | |
| 7490 | /*------------------------------------------------- |
| 7491 | op_fsqrt - process a FSQRT opcode |
| 7492 | -------------------------------------------------*/ |
| 7493 | |
| 7494 | static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7495 | { |
| 7496 | drcuml_parameter dstp, srcp; |
| 7497 | int dstreg; |
| 7498 | |
| 7499 | /* validate instruction */ |
| 7500 | assert(inst->size == 4 || inst->size == 8); |
| 7501 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7502 | |
| 7503 | /* normalize parameters */ |
| 7504 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7505 | |
| 7506 | /* pick a target register for the general case */ |
| 7507 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7508 | |
| 7509 | /* 32-bit form */ |
| 7510 | if (inst->size == 4) |
| 7511 | { |
| 7512 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7513 | emit_sqrtss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // sqrtss dstreg,[srcp] |
| 7514 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7515 | emit_sqrtss_r128_r128(&dst, dstreg, srcp.value); // sqrtss dstreg,srcp |
| 7516 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7517 | } |
| 7518 | |
| 7519 | /* 64-bit form */ |
| 7520 | else if (inst->size == 8) |
| 7521 | { |
| 7522 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7523 | emit_sqrtsd_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // sqrtsd dstreg,[srcp] |
| 7524 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7525 | emit_sqrtsd_r128_r128(&dst, dstreg, srcp.value); // sqrtsd dstreg,srcp |
| 7526 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7527 | } |
| 7528 | return dst; |
| 7529 | } |
| 7530 | |
| 7531 | |
| 7532 | /*------------------------------------------------- |
| 7533 | op_frecip - process a FRECIP opcode |
| 7534 | -------------------------------------------------*/ |
| 7535 | |
| 7536 | static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7537 | { |
| 7538 | drcuml_parameter dstp, srcp; |
| 7539 | int dstreg; |
| 7540 | |
| 7541 | /* validate instruction */ |
| 7542 | assert(inst->size == 4 || inst->size == 8); |
| 7543 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7544 | |
| 7545 | /* normalize parameters */ |
| 7546 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7547 | |
| 7548 | /* pick a target register for the general case */ |
| 7549 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7550 | |
| 7551 | /* 32-bit form */ |
| 7552 | if (inst->size == 4) |
| 7553 | { |
| 7554 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7555 | emit_rcpss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // rcpss dstreg,[srcp] |
| 7556 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7557 | emit_rcpss_r128_r128(&dst, dstreg, srcp.value); // rcpss dstreg,srcp |
| 7558 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7559 | } |
| 7560 | |
| 7561 | /* 64-bit form */ |
| 7562 | else if (inst->size == 8) |
| 7563 | { |
| 7564 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7565 | emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2ss dstreg,[srcp] |
| 7566 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7567 | emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value); // cvtsd2ss dstreg,srcp |
| 7568 | emit_rcpss_r128_r128(&dst, dstreg, dstreg); // rcpss dstreg,dstreg |
| 7569 | emit_cvtss2sd_r128_r128(&dst, dstreg, dstreg); // cvtss2sd dstreg,dstreg |
| 7570 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7571 | } |
| 7572 | return dst; |
| 7573 | } |
| 7574 | |
| 7575 | |
| 7576 | /*------------------------------------------------- |
| 7577 | op_frsqrt - process a FRSQRT opcode |
| 7578 | -------------------------------------------------*/ |
| 7579 | |
| 7580 | static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7581 | { |
| 7582 | drcuml_parameter dstp, srcp; |
| 7583 | int dstreg; |
| 7584 | |
| 7585 | /* validate instruction */ |
| 7586 | assert(inst->size == 4 || inst->size == 8); |
| 7587 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7588 | |
| 7589 | /* normalize parameters */ |
| 7590 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7591 | |
| 7592 | /* pick a target register for the general case */ |
| 7593 | dstreg = param_select_register(REG_XMM0, &dstp, NULL); |
| 7594 | |
| 7595 | /* 32-bit form */ |
| 7596 | if (inst->size == 4) |
| 7597 | { |
| 7598 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7599 | emit_rsqrtss_r128_m32(&dst, dstreg, MABS(drcbe, srcp.value)); // rsqrtss dstreg,[srcp] |
| 7600 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7601 | emit_rsqrtss_r128_r128(&dst, dstreg, srcp.value); // rsqrtss dstreg,srcp |
| 7602 | emit_movss_p32_r128(drcbe, &dst, &dstp, dstreg); // movss dstp,dstreg |
| 7603 | } |
| 7604 | |
| 7605 | /* 64-bit form */ |
| 7606 | else if (inst->size == 8) |
| 7607 | { |
| 7608 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7609 | emit_cvtsd2ss_r128_m64(&dst, dstreg, MABS(drcbe, srcp.value)); // cvtsd2ss dstreg,[srcp] |
| 7610 | else if (srcp.type == DRCUML_PTYPE_FLOAT_REGISTER) |
| 7611 | emit_cvtsd2ss_r128_r128(&dst, dstreg, srcp.value); // cvtsd2ss dstreg,srcp |
| 7612 | emit_rsqrtss_r128_r128(&dst, dstreg, dstreg); // rsqrtss dstreg,dstreg |
| 7613 | emit_cvtss2sd_r128_r128(&dst, dstreg, dstreg); // cvtss2sd dstreg,dstreg |
| 7614 | emit_movsd_p64_r128(drcbe, &dst, &dstp, dstreg); // movsd dstp,dstreg |
| 7615 | } |
| 7616 | return dst; |
| 7617 | } |
src/emu/cpu/mips/mips3drc.c
| r2627 | r2628 | |
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | |
| 3 | 3 | mips3drc.c |
| 4 | | x86 Dynamic recompiler for MIPS III/IV emulator. |
| 5 | | Written by Aaron Giles |
| 6 | 4 | |
| 7 | | Philosophy: this is intended to be a very basic implementation of a |
| 8 | | dynamic compiler in order to keep things simple. There are certainly |
| 9 | | more optimizations that could be added but for now, we keep it |
| 10 | | strictly to NOP stripping and LUI optimizations. |
| 5 | Universal machine language-based MIPS III/IV emulator. |
| 11 | 6 | |
| 7 | Copyright Aaron Giles |
| 8 | Released for general non-commercial use under the MAME license |
| 9 | Visit http://mamedev.org for licensing and usage restrictions. |
| 10 | |
| 11 | **************************************************************************** |
| 12 | |
| 13 | Future improvements/changes: |
| 14 | |
| 15 | * Add DRC option to flush PC before calling memory handlers |
| 16 | |
| 17 | * Map MIPS registers to i5-i7 (or more) |
| 18 | - should rely on backend hints to know how many to assign |
| 19 | - add instrumentation to figure out which ones are most used |
| 20 | - on entry, load registers; on exit, flush them |
| 21 | - also flush before calling debugger |
| 22 | |
| 23 | * Constant tracking? (hasn't bought us much in the past) |
| 24 | |
| 25 | * Customized mapped/unmapped memory handlers |
| 26 | - create 3 sets of handlers: cached, uncached, general |
| 27 | - default to general |
| 28 | - in general case, if cached use RECALL to point to cached code |
| 29 | - (same for uncached) |
| 30 | - in cached/uncached case, fall back to general case |
| 31 | |
| 12 | 32 | ***************************************************************************/ |
| 13 | 33 | |
| 14 | 34 | #include <stddef.h> |
| r2627 | r2628 | |
| 17 | 37 | #include "mips3com.h" |
| 18 | 38 | #include "mips3fe.h" |
| 19 | 39 | #include "deprecat.h" |
| 20 | | #include "cpu/x86log.h" |
| 21 | 40 | #include "cpu/drcfe.h" |
| 41 | #include "cpu/drcuml.h" |
| 42 | #include "cpu/drcumlsh.h" |
| 22 | 43 | |
| 23 | 44 | extern unsigned dasmmips3(char *buffer, unsigned pc, UINT32 op); |
| 24 | 45 | |
| r2627 | r2628 | |
| 28 | 49 | DEBUGGING |
| 29 | 50 | ***************************************************************************/ |
| 30 | 51 | |
| 31 | | #define LOG_CODE (0) |
| 32 | | #define SINGLE_INSTRUCTION_MODE (0) |
| 52 | #define FORCE_C_BACKEND (0) |
| 53 | #define LOG_UML (0) |
| 54 | #define LOG_NATIVE (0) |
| 33 | 55 | |
| 34 | | #ifdef ENABLE_DEBUGGER |
| 35 | | #define COMPARE_AGAINST_C (0) |
| 36 | | #else |
| 37 | | #define COMPARE_AGAINST_C (0) |
| 38 | | #endif |
| 56 | #define SINGLE_INSTRUCTION_MODE (0) |
| 57 | #define PRINTF_EXCEPTIONS (0) |
| 39 | 58 | |
| 59 | #define PROBE_ADDRESS ~0 |
| 40 | 60 | |
| 41 | 61 | |
| 62 | |
| 42 | 63 | /*************************************************************************** |
| 43 | | CONFIGURATION |
| 64 | CONSTANTS |
| 44 | 65 | ***************************************************************************/ |
| 45 | 66 | |
| 67 | /* map variables */ |
| 68 | #define MAPVAR_PC MVAR(0) |
| 69 | #define MAPVAR_CYCLES MVAR(1) |
| 70 | |
| 46 | 71 | /* size of the execution code cache */ |
| 47 | 72 | #define CACHE_SIZE (32 * 1024 * 1024) |
| 48 | 73 | |
| r2627 | r2628 | |
| 52 | 77 | #define COMPILE_MAX_INSTRUCTIONS ((COMPILE_BACKWARDS_BYTES/4) + (COMPILE_FORWARDS_BYTES/4)) |
| 53 | 78 | #define COMPILE_MAX_SEQUENCE 64 |
| 54 | 79 | |
| 55 | | /* hack when running comparison against the C core */ |
| 56 | | #if COMPARE_AGAINST_C |
| 57 | | #undef MIPS3_COUNT_READ_CYCLES |
| 58 | | #undef MIPS3_CAUSE_READ_CYCLES |
| 59 | | #define MIPS3_COUNT_READ_CYCLES 0 |
| 60 | | #define MIPS3_CAUSE_READ_CYCLES 0 |
| 61 | | #endif |
| 80 | /* exit codes */ |
| 81 | #define EXECUTE_OUT_OF_CYCLES 0 |
| 82 | #define EXECUTE_MISSING_CODE 1 |
| 62 | 83 | |
| 63 | 84 | |
| 64 | 85 | |
| r2627 | r2628 | |
| 66 | 87 | MACROS |
| 67 | 88 | ***************************************************************************/ |
| 68 | 89 | |
| 69 | | #define SR mips3.core->cpr[0][COP0_Status] |
| 70 | | #define IS_FR0 (!(SR & SR_FR)) |
| 71 | | #define IS_FR1 (SR & SR_FR) |
| 90 | #ifdef LSB_FIRST |
| 91 | #define LOPTR(x) ((UINT32 *)(x)) |
| 92 | #else |
| 93 | #define LOPTR(x) ((UINT32 *)(x) + 1) |
| 94 | #endif |
| 72 | 95 | |
| 96 | #define R32(reg) ((reg == 0) ? DRCUML_PTYPE_IMMEDIATE : DRCUML_PTYPE_MEMORY), ((reg) == 0) ? 0 : (FPTR)LOPTR(&mips3.core->r[reg]) |
| 97 | #define LO32 R32(REG_LO) |
| 98 | #define HI32 R32(REG_HI) |
| 99 | #define CPR032(reg) MEM(LOPTR(&mips3.core->cpr[0][reg])) |
| 100 | #define CCR032(reg) MEM(LOPTR(&mips3.core->ccr[0][reg])) |
| 101 | #define FPR32(reg) MEM((mips3.cstate->mode == 0) ? &((float *)&mips3.core->cpr[1][0])[reg] : (float *)&mips3.core->cpr[1][reg]) |
| 102 | #define CCR132(reg) MEM(LOPTR(&mips3.core->ccr[1][reg])) |
| 103 | #define CPR232(reg) MEM(LOPTR(&mips3.core->cpr[2][reg])) |
| 104 | #define CCR232(reg) MEM(LOPTR(&mips3.core->ccr[2][reg])) |
| 73 | 105 | |
| 106 | #define R64(reg) ((reg == 0) ? DRCUML_PTYPE_IMMEDIATE : DRCUML_PTYPE_MEMORY), ((reg) == 0) ? 0 : (FPTR)&mips3.core->r[reg] |
| 107 | #define LO64 R64(REG_LO) |
| 108 | #define HI64 R64(REG_HI) |
| 109 | #define CPR064(reg) MEM(&mips3.core->cpr[0][reg]) |
| 110 | #define CCR064(reg) MEM(&mips3.core->ccr[0][reg]) |
| 111 | #define FPR64(reg) MEM((mips3.cstate->mode == 0) ? (double *)&mips3.core->cpr[1][(reg)/2] : (double *)&mips3.core->cpr[1][reg]) |
| 112 | #define CCR164(reg) MEM(&mips3.core->ccr[1][reg]) |
| 113 | #define CPR264(reg) MEM(&mips3.core->cpr[2][reg]) |
| 114 | #define CCR264(reg) MEM(&mips3.core->ccr[2][reg]) |
| 74 | 115 | |
| 116 | #define FCCMASK(which) fcc_mask[(mips3.core->flavor < MIPS3_TYPE_MIPS_IV) ? 0 : ((which) & 7)] |
| 117 | |
| 118 | |
| 119 | |
| 75 | 120 | /*************************************************************************** |
| 76 | 121 | STRUCTURES & TYPEDEFS |
| 77 | 122 | ***************************************************************************/ |
| r2627 | r2628 | |
| 80 | 125 | typedef struct _fast_ram_info fast_ram_info; |
| 81 | 126 | struct _fast_ram_info |
| 82 | 127 | { |
| 83 | | offs_t start; |
| 84 | | offs_t end; |
| 85 | | UINT8 readonly; |
| 86 | | void * base; |
| 128 | offs_t start; /* start of the RAM block */ |
| 129 | offs_t end; /* end of the RAM block */ |
| 130 | UINT8 readonly; /* TRUE if read-only */ |
| 131 | void * base; /* base in memory where the RAM lives */ |
| 87 | 132 | }; |
| 88 | 133 | |
| 89 | 134 | |
| r2627 | r2628 | |
| 91 | 136 | typedef struct _hotspot_info hotspot_info; |
| 92 | 137 | struct _hotspot_info |
| 93 | 138 | { |
| 94 | | offs_t pc; |
| 95 | | UINT32 opcode; |
| 96 | | UINT32 cycles; |
| 139 | offs_t pc; /* PC to consider */ |
| 140 | UINT32 opcode; /* required opcode at that PC */ |
| 141 | UINT32 cycles; /* number of cycles to eat when hit */ |
| 97 | 142 | }; |
| 98 | 143 | |
| 99 | 144 | |
| 100 | | /* opaque drc-specific info */ |
| 101 | | typedef struct _mips3drc_data mips3drc_data; |
| 145 | /* internal compiler state */ |
| 146 | typedef struct _compiler_state compiler_state; |
| 147 | struct _compiler_state |
| 148 | { |
| 149 | UINT32 cycles; /* accumulated cycles */ |
| 150 | UINT8 checkints; /* need to check interrupts before next instruction */ |
| 151 | UINT8 checksoftints; /* need to check software interrupts before next instruction */ |
| 152 | drcuml_codelabel labelnum; /* index for local labels */ |
| 153 | }; |
| 102 | 154 | |
| 103 | 155 | |
| 156 | /* cache-based state */ |
| 157 | typedef struct _mips3_cache_state mips3_cache_state; |
| 158 | struct _mips3_cache_state |
| 159 | { |
| 160 | /* parameters for subroutines */ |
| 161 | UINT64 numcycles; /* return value from gettotalcycles */ |
| 162 | UINT32 mode; /* current global mode */ |
| 163 | |
| 164 | /* tables */ |
| 165 | UINT8 fpmode[4]; /* FPU mode table */ |
| 166 | UINT64 slt_table[32]; /* FLAGS table for slt */ |
| 167 | UINT64 sltu_table[32]; /* FLAGS table for sltu */ |
| 168 | UINT32 c_un_table[32]; /* FLAGS table for c_un */ |
| 169 | UINT32 c_eq_table[32]; /* FLAGS table for c_eq */ |
| 170 | UINT32 c_ueq_table[32]; /* FLAGS table for c_ueq */ |
| 171 | UINT32 c_olt_table[32]; /* FLAGS table for c_olt */ |
| 172 | UINT32 c_ult_table[32]; /* FLAGS table for c_ult */ |
| 173 | UINT32 c_ole_table[32]; /* FLAGS table for c_ole */ |
| 174 | UINT32 c_ule_table[32]; /* FLAGS table for c_ule */ |
| 175 | }; |
| 176 | |
| 177 | |
| 104 | 178 | /* MIPS3 registers */ |
| 105 | 179 | typedef struct _mips3_regs mips3_regs; |
| 106 | 180 | struct _mips3_regs |
| 107 | 181 | { |
| 108 | 182 | /* core state */ |
| 109 | | UINT8 * cache; /* base of the cache */ |
| 110 | | mips3_state * core; /* pointer to the core MIPS3 state */ |
| 111 | | drcfe_state * drcfe; /* pointer to the DRC front-end state */ |
| 112 | | drc_core * drc; /* pointer to the DRC core */ |
| 113 | | mips3drc_data * drcdata; /* pointer to the DRC-specific data */ |
| 114 | | UINT32 drcoptions; /* configurable DRC options */ |
| 183 | drccache * cache; /* pointer to the DRC code cache */ |
| 184 | drcuml_state * drcuml; /* DRC UML generator state */ |
| 185 | drcfe_state * drcfe; /* pointer to the DRC front-end state */ |
| 186 | UINT32 drcoptions; /* configurable DRC options */ |
| 187 | mips3_state * core; /* pointer to the core MIPS3 state */ |
| 188 | mips3_cache_state * cstate; /* state that needs to live in the cache */ |
| 115 | 189 | |
| 116 | 190 | /* internal stuff */ |
| 117 | | UINT32 nextpc; |
| 118 | | UINT8 cache_dirty; |
| 191 | UINT8 cache_dirty; /* true if we need to flush the cache */ |
| 119 | 192 | |
| 193 | /* subroutines */ |
| 194 | drcuml_codehandle * entry; /* entry point */ |
| 195 | drcuml_codehandle * nocode; /* nocode exception handler */ |
| 196 | drcuml_codehandle * out_of_cycles; /* out of cycles exception handler */ |
| 197 | drcuml_codehandle * tlb_mismatch; /* tlb mismatch at current PC */ |
| 198 | drcuml_codehandle * read8; /* read byte */ |
| 199 | drcuml_codehandle * write8; /* write byte */ |
| 200 | drcuml_codehandle * read16; /* read half */ |
| 201 | drcuml_codehandle * write16; /* write half */ |
| 202 | drcuml_codehandle * read32; /* read word */ |
| 203 | drcuml_codehandle * read32mask; /* read word masked */ |
| 204 | drcuml_codehandle * write32; /* write word */ |
| 205 | drcuml_codehandle * write32mask; /* write word masked */ |
| 206 | drcuml_codehandle * read64; /* read double */ |
| 207 | drcuml_codehandle * read64mask; /* read double masked */ |
| 208 | drcuml_codehandle * write64; /* write double */ |
| 209 | drcuml_codehandle * write64mask; /* write double masked */ |
| 210 | drcuml_codehandle * exception[EXCEPTION_COUNT]; /* array of exception handlers */ |
| 211 | drcuml_codehandle * exception_norecover[EXCEPTION_COUNT]; /* array of no-recover exception handlers */ |
| 212 | |
| 120 | 213 | /* fast RAM */ |
| 121 | | UINT32 fastram_select; |
| 122 | | fast_ram_info fastram[MIPS3_MAX_FASTRAM]; |
| 214 | UINT32 fastram_select; |
| 215 | fast_ram_info fastram[MIPS3_MAX_FASTRAM]; |
| 123 | 216 | |
| 124 | 217 | /* hotspots */ |
| 125 | | UINT32 hotspot_select; |
| 126 | | hotspot_info hotspot[MIPS3_MAX_HOTSPOTS]; |
| 127 | | |
| 128 | | /* code logging */ |
| 129 | | x86log_context *log; |
| 130 | | |
| 131 | | #if COMPARE_AGAINST_C |
| 132 | | /* memory state */ |
| 133 | | UINT8 access_size; |
| 134 | | UINT8 access_type; |
| 135 | | offs_t access_addr; |
| 136 | | UINT64 access_data; |
| 137 | | UINT64 access_mask; |
| 138 | | memory_handlers orighandler; |
| 139 | | #endif |
| 218 | UINT32 hotspot_select; |
| 219 | hotspot_info hotspot[MIPS3_MAX_HOTSPOTS]; |
| 140 | 220 | }; |
| 141 | 221 | |
| 142 | 222 | |
| r2627 | r2628 | |
| 145 | 225 | FUNCTION PROTOTYPES |
| 146 | 226 | ***************************************************************************/ |
| 147 | 227 | |
| 148 | | static void mips3drc_init(void); |
| 149 | | static void mips3drc_exit(void); |
| 228 | static void code_flush_cache(drcuml_state *drcuml); |
| 229 | static void code_compile_block(drcuml_state *drcuml, UINT8 mode, offs_t pc); |
| 150 | 230 | |
| 151 | | #if COMPARE_AGAINST_C |
| 152 | | static void execute_c_version(void); |
| 153 | | static void compare_c_version(void); |
| 154 | | static void mips3c_init(mips3_flavor flavor, int bigendian, int index, int clock, const struct mips3_config *config, int (*irqcallback)(int)); |
| 155 | | static void mips3c_reset(void); |
| 156 | | #endif |
| 231 | static void cfunc_printf_exception(void *param); |
| 232 | static void cfunc_get_cycles(void *param); |
| 233 | static void cfunc_printf_probe(void *param); |
| 157 | 234 | |
| 235 | static void static_generate_entry_point(drcuml_state *drcuml); |
| 236 | static void static_generate_nocode_handler(drcuml_state *drcuml); |
| 237 | static void static_generate_out_of_cycles(drcuml_state *drcuml); |
| 238 | static void static_generate_exception(drcuml_state *drcuml, UINT8 exception, int recover, const char *name); |
| 239 | static void static_generate_memory_accessor(drcuml_state *drcuml, int size, int iswrite, int ismasked, const char *name, drcuml_codehandle **handleptr); |
| 158 | 240 | |
| 241 | static void generate_update_cycles(drcuml_block *block, compiler_state *compiler, UINT32 nextpc, UINT32 *mempc, int allow_exception); |
| 242 | static void generate_checksum_block(drcuml_block *block, compiler_state *compiler, const opcode_desc *seqhead, const opcode_desc *seqlast); |
| 243 | static void generate_sequence_instruction(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 244 | static void generate_delay_slot_and_branch(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 linkreg); |
| 245 | static int generate_opcode(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 246 | static int generate_special(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 247 | static int generate_regimm(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 248 | static int generate_idt(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 249 | static int generate_set_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg); |
| 250 | static int generate_get_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg); |
| 251 | static int generate_cop0(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 252 | static int generate_cop1(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 253 | static int generate_cop1x(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc); |
| 159 | 254 | |
| 255 | static void log_add_disasm_comment(drcuml_block *block, UINT32 pc, UINT32 op); |
| 256 | static const char *log_desc_flags_to_string(UINT32 flags); |
| 257 | static void log_register_list(drcuml_state *drcuml, const char *string, UINT64 gprmask, UINT64 fprmask); |
| 258 | static void log_opcode_desc(drcuml_state *drcuml, const opcode_desc *desclist, int indent); |
| 259 | |
| 260 | |
| 261 | |
| 160 | 262 | /*************************************************************************** |
| 161 | 263 | PRIVATE GLOBAL VARIABLES |
| 162 | 264 | ***************************************************************************/ |
| 163 | 265 | |
| 164 | 266 | static mips3_regs mips3; |
| 165 | | static size_t allocatedsize; |
| 166 | 267 | |
| 167 | 268 | |
| 269 | /* bit indexes for various FCCs */ |
| 270 | static const UINT32 fcc_mask[8] = { 1 << 23, 1 << 25, 1 << 26, 1 << 27, 1 << 28, 1 << 29, 1 << 30, 1 << 31 }; |
| 168 | 271 | |
| 272 | /* lookup table for FP modes */ |
| 273 | static const UINT8 fpmode_source[4] = |
| 274 | { |
| 275 | DRCUML_FMOD_ROUND, |
| 276 | DRCUML_FMOD_TRUNC, |
| 277 | DRCUML_FMOD_CEIL, |
| 278 | DRCUML_FMOD_FLOOR |
| 279 | }; |
| 280 | |
| 281 | /* flag lookup table for SLT */ |
| 282 | static const UINT64 slt_table_source[32] = |
| 283 | { |
| 284 | /* ..... */ 0, |
| 285 | /* ....C */ 0, |
| 286 | /* ...V. */ 1, |
| 287 | /* ...VC */ 1, |
| 288 | /* ..Z.. */ 0, |
| 289 | /* ..Z.C */ 0, |
| 290 | /* ..ZV. */ 1, |
| 291 | /* ..ZVC */ 1, |
| 292 | /* .S... */ 1, |
| 293 | /* .S..C */ 1, |
| 294 | /* .S.V. */ 0, |
| 295 | /* .S.VC */ 0, |
| 296 | /* .SZ.. */ 1, |
| 297 | /* .SZ.C */ 1, |
| 298 | /* .SZV. */ 0, |
| 299 | /* .SZVC */ 0, |
| 300 | /* U.... */ 0, |
| 301 | /* U...C */ 0, |
| 302 | /* U..V. */ 1, |
| 303 | /* U..VC */ 1, |
| 304 | /* U.Z.. */ 0, |
| 305 | /* U.Z.C */ 0, |
| 306 | /* U.ZV. */ 1, |
| 307 | /* U.ZVC */ 1, |
| 308 | /* US... */ 1, |
| 309 | /* US..C */ 1, |
| 310 | /* US.V. */ 0, |
| 311 | /* US.VC */ 0, |
| 312 | /* USZ.. */ 1, |
| 313 | /* USZ.C */ 1, |
| 314 | /* USZV. */ 0, |
| 315 | /* USZVC */ 0 |
| 316 | }; |
| 317 | |
| 318 | /* flag lookup table for SLTU */ |
| 319 | static const UINT64 sltu_table_source[32] = |
| 320 | { |
| 321 | /* ..... */ 0, |
| 322 | /* ....C */ 1, |
| 323 | /* ...V. */ 0, |
| 324 | /* ...VC */ 1, |
| 325 | /* ..Z.. */ 0, |
| 326 | /* ..Z.C */ 1, |
| 327 | /* ..ZV. */ 0, |
| 328 | /* ..ZVC */ 1, |
| 329 | /* .S... */ 0, |
| 330 | /* .S..C */ 1, |
| 331 | /* .S.V. */ 0, |
| 332 | /* .S.VC */ 1, |
| 333 | /* .SZ.. */ 0, |
| 334 | /* .SZ.C */ 1, |
| 335 | /* .SZV. */ 0, |
| 336 | /* .SZVC */ 1, |
| 337 | /* U.... */ 0, |
| 338 | /* U...C */ 1, |
| 339 | /* U..V. */ 0, |
| 340 | /* U..VC */ 1, |
| 341 | /* U.Z.. */ 0, |
| 342 | /* U.Z.C */ 1, |
| 343 | /* U.ZV. */ 0, |
| 344 | /* U.ZVC */ 1, |
| 345 | /* US... */ 0, |
| 346 | /* US..C */ 1, |
| 347 | /* US.V. */ 0, |
| 348 | /* US.VC */ 1, |
| 349 | /* USZ.. */ 0, |
| 350 | /* USZ.C */ 1, |
| 351 | /* USZV. */ 0, |
| 352 | /* USZVC */ 1 |
| 353 | }; |
| 354 | |
| 355 | /* flag lookup table for C.UN */ |
| 356 | static const UINT32 c_un_table_source[32] = |
| 357 | { |
| 358 | /* ..... */ 0, |
| 359 | /* ....C */ 0, |
| 360 | /* ...V. */ 0, |
| 361 | /* ...VC */ 0, |
| 362 | /* ..Z.. */ 0, |
| 363 | /* ..Z.C */ 0, |
| 364 | /* ..ZV. */ 0, |
| 365 | /* ..ZVC */ 0, |
| 366 | /* .S... */ 0, |
| 367 | /* .S..C */ 0, |
| 368 | /* .S.V. */ 0, |
| 369 | /* .S.VC */ 0, |
| 370 | /* .SZ.. */ 0, |
| 371 | /* .SZ.C */ 0, |
| 372 | /* .SZV. */ 0, |
| 373 | /* .SZVC */ 0, |
| 374 | /* U.... */ ~0, |
| 375 | /* U...C */ ~0, |
| 376 | /* U..V. */ ~0, |
| 377 | /* U..VC */ ~0, |
| 378 | /* U.Z.. */ ~0, |
| 379 | /* U.Z.C */ ~0, |
| 380 | /* U.ZV. */ ~0, |
| 381 | /* U.ZVC */ ~0, |
| 382 | /* US... */ ~0, |
| 383 | /* US..C */ ~0, |
| 384 | /* US.V. */ ~0, |
| 385 | /* US.VC */ ~0, |
| 386 | /* USZ.. */ ~0, |
| 387 | /* USZ.C */ ~0, |
| 388 | /* USZV. */ ~0, |
| 389 | /* USZVC */ ~0 |
| 390 | }; |
| 391 | |
| 392 | /* flag lookup table for C.EQ */ |
| 393 | static const UINT32 c_eq_table_source[32] = |
| 394 | { |
| 395 | /* ..... */ 0, |
| 396 | /* ....C */ 0, |
| 397 | /* ...V. */ 0, |
| 398 | /* ...VC */ 0, |
| 399 | /* ..Z.. */ ~0, |
| 400 | /* ..Z.C */ ~0, |
| 401 | /* ..ZV. */ ~0, |
| 402 | /* ..ZVC */ ~0, |
| 403 | /* .S... */ 0, |
| 404 | /* .S..C */ 0, |
| 405 | /* .S.V. */ 0, |
| 406 | /* .S.VC */ 0, |
| 407 | /* .SZ.. */ ~0, |
| 408 | /* .SZ.C */ ~0, |
| 409 | /* .SZV. */ ~0, |
| 410 | /* .SZVC */ ~0, |
| 411 | /* U.... */ 0, |
| 412 | /* U...C */ 0, |
| 413 | /* U..V. */ 0, |
| 414 | /* U..VC */ 0, |
| 415 | /* U.Z.. */ 0, |
| 416 | /* U.Z.C */ 0, |
| 417 | /* U.ZV. */ 0, |
| 418 | /* U.ZVC */ 0, |
| 419 | /* US... */ 0, |
| 420 | /* US..C */ 0, |
| 421 | /* US.V. */ 0, |
| 422 | /* US.VC */ 0, |
| 423 | /* USZ.. */ 0, |
| 424 | /* USZ.C */ 0, |
| 425 | /* USZV. */ 0, |
| 426 | /* USZVC */ 0 |
| 427 | }; |
| 428 | |
| 429 | /* flag lookup table for C.UEQ */ |
| 430 | static const UINT32 c_ueq_table_source[32] = |
| 431 | { |
| 432 | /* ..... */ 0, |
| 433 | /* ....C */ 0, |
| 434 | /* ...V. */ 0, |
| 435 | /* ...VC */ 0, |
| 436 | /* ..Z.. */ ~0, |
| 437 | /* ..Z.C */ ~0, |
| 438 | /* ..ZV. */ ~0, |
| 439 | /* ..ZVC */ ~0, |
| 440 | /* .S... */ 0, |
| 441 | /* .S..C */ 0, |
| 442 | /* .S.V. */ 0, |
| 443 | /* .S.VC */ 0, |
| 444 | /* .SZ.. */ ~0, |
| 445 | /* .SZ.C */ ~0, |
| 446 | /* .SZV. */ ~0, |
| 447 | /* .SZVC */ ~0, |
| 448 | /* U.... */ ~0, |
| 449 | /* U...C */ ~0, |
| 450 | /* U..V. */ ~0, |
| 451 | /* U..VC */ ~0, |
| 452 | /* U.Z.. */ ~0, |
| 453 | /* U.Z.C */ ~0, |
| 454 | /* U.ZV. */ ~0, |
| 455 | /* U.ZVC */ ~0, |
| 456 | /* US... */ ~0, |
| 457 | /* US..C */ ~0, |
| 458 | /* US.V. */ ~0, |
| 459 | /* US.VC */ ~0, |
| 460 | /* USZ.. */ ~0, |
| 461 | /* USZ.C */ ~0, |
| 462 | /* USZV. */ ~0, |
| 463 | /* USZVC */ ~0 |
| 464 | }; |
| 465 | |
| 466 | /* flag lookup table for C.OLT */ |
| 467 | static const UINT32 c_olt_table_source[32] = |
| 468 | { |
| 469 | /* ..... */ 0, |
| 470 | /* ....C */ ~0, |
| 471 | /* ...V. */ 0, |
| 472 | /* ...VC */ ~0, |
| 473 | /* ..Z.. */ 0, |
| 474 | /* ..Z.C */ ~0, |
| 475 | /* ..ZV. */ 0, |
| 476 | /* ..ZVC */ ~0, |
| 477 | /* .S... */ 0, |
| 478 | /* .S..C */ ~0, |
| 479 | /* .S.V. */ 0, |
| 480 | /* .S.VC */ ~0, |
| 481 | /* .SZ.. */ 0, |
| 482 | /* .SZ.C */ ~0, |
| 483 | /* .SZV. */ 0, |
| 484 | /* .SZVC */ ~0, |
| 485 | /* U.... */ 0, |
| 486 | /* U...C */ 0, |
| 487 | /* U..V. */ 0, |
| 488 | /* U..VC */ 0, |
| 489 | /* U.Z.. */ 0, |
| 490 | /* U.Z.C */ 0, |
| 491 | /* U.ZV. */ 0, |
| 492 | /* U.ZVC */ 0, |
| 493 | /* US... */ 0, |
| 494 | /* US..C */ 0, |
| 495 | /* US.V. */ 0, |
| 496 | /* US.VC */ 0, |
| 497 | /* USZ.. */ 0, |
| 498 | /* USZ.C */ 0, |
| 499 | /* USZV. */ 0, |
| 500 | /* USZVC */ 0 |
| 501 | }; |
| 502 | |
| 503 | /* flag lookup table for C.ULT */ |
| 504 | static const UINT32 c_ult_table_source[32] = |
| 505 | { |
| 506 | /* ..... */ 0, |
| 507 | /* ....C */ ~0, |
| 508 | /* ...V. */ 0, |
| 509 | /* ...VC */ ~0, |
| 510 | /* ..Z.. */ 0, |
| 511 | /* ..Z.C */ ~0, |
| 512 | /* ..ZV. */ 0, |
| 513 | /* ..ZVC */ ~0, |
| 514 | /* .S... */ 0, |
| 515 | /* .S..C */ ~0, |
| 516 | /* .S.V. */ 0, |
| 517 | /* .S.VC */ ~0, |
| 518 | /* .SZ.. */ 0, |
| 519 | /* .SZ.C */ ~0, |
| 520 | /* .SZV. */ 0, |
| 521 | /* .SZVC */ ~0, |
| 522 | /* U.... */ ~0, |
| 523 | /* U...C */ ~0, |
| 524 | /* U..V. */ ~0, |
| 525 | /* U..VC */ ~0, |
| 526 | /* U.Z.. */ ~0, |
| 527 | /* U.Z.C */ ~0, |
| 528 | /* U.ZV. */ ~0, |
| 529 | /* U.ZVC */ ~0, |
| 530 | /* US... */ ~0, |
| 531 | /* US..C */ ~0, |
| 532 | /* US.V. */ ~0, |
| 533 | /* US.VC */ ~0, |
| 534 | /* USZ.. */ ~0, |
| 535 | /* USZ.C */ ~0, |
| 536 | /* USZV. */ ~0, |
| 537 | /* USZVC */ ~0 |
| 538 | }; |
| 539 | |
| 540 | /* flag lookup table for C.OLE */ |
| 541 | static const UINT32 c_ole_table_source[32] = |
| 542 | { |
| 543 | /* ..... */ 0, |
| 544 | /* ....C */ ~0, |
| 545 | /* ...V. */ 0, |
| 546 | /* ...VC */ ~0, |
| 547 | /* ..Z.. */ ~0, |
| 548 | /* ..Z.C */ ~0, |
| 549 | /* ..ZV. */ ~0, |
| 550 | /* ..ZVC */ ~0, |
| 551 | /* .S... */ 0, |
| 552 | /* .S..C */ ~0, |
| 553 | /* .S.V. */ 0, |
| 554 | /* .S.VC */ ~0, |
| 555 | /* .SZ.. */ ~0, |
| 556 | /* .SZ.C */ ~0, |
| 557 | /* .SZV. */ ~0, |
| 558 | /* .SZVC */ ~0, |
| 559 | /* U.... */ 0, |
| 560 | /* U...C */ 0, |
| 561 | /* U..V. */ 0, |
| 562 | /* U..VC */ 0, |
| 563 | /* U.Z.. */ 0, |
| 564 | /* U.Z.C */ 0, |
| 565 | /* U.ZV. */ 0, |
| 566 | /* U.ZVC */ 0, |
| 567 | /* US... */ 0, |
| 568 | /* US..C */ 0, |
| 569 | /* US.V. */ 0, |
| 570 | /* US.VC */ 0, |
| 571 | /* USZ.. */ 0, |
| 572 | /* USZ.C */ 0, |
| 573 | /* USZV. */ 0, |
| 574 | /* USZVC */ 0 |
| 575 | }; |
| 576 | |
| 577 | /* flag lookup table for C.ULE */ |
| 578 | static const UINT32 c_ule_table_source[32] = |
| 579 | { |
| 580 | /* ..... */ 0, |
| 581 | /* ....C */ ~0, |
| 582 | /* ...V. */ 0, |
| 583 | /* ...VC */ ~0, |
| 584 | /* ..Z.. */ ~0, |
| 585 | /* ..Z.C */ ~0, |
| 586 | /* ..ZV. */ ~0, |
| 587 | /* ..ZVC */ ~0, |
| 588 | /* .S... */ 0, |
| 589 | /* .S..C */ ~0, |
| 590 | /* .S.V. */ 0, |
| 591 | /* .S.VC */ ~0, |
| 592 | /* .SZ.. */ ~0, |
| 593 | /* .SZ.C */ ~0, |
| 594 | /* .SZV. */ ~0, |
| 595 | /* .SZVC */ ~0, |
| 596 | /* U.... */ ~0, |
| 597 | /* U...C */ ~0, |
| 598 | /* U..V. */ ~0, |
| 599 | /* U..VC */ ~0, |
| 600 | /* U.Z.. */ ~0, |
| 601 | /* U.Z.C */ ~0, |
| 602 | /* U.ZV. */ ~0, |
| 603 | /* U.ZVC */ ~0, |
| 604 | /* US... */ ~0, |
| 605 | /* US..C */ ~0, |
| 606 | /* US.V. */ ~0, |
| 607 | /* US.VC */ ~0, |
| 608 | /* USZ.. */ ~0, |
| 609 | /* USZ.C */ ~0, |
| 610 | /* USZV. */ ~0, |
| 611 | /* USZVC */ ~0 |
| 612 | }; |
| 613 | |
| 614 | |
| 615 | |
| 169 | 616 | /*************************************************************************** |
| 617 | INLINE FUNCTIONS |
| 618 | ***************************************************************************/ |
| 619 | |
| 620 | /*------------------------------------------------- |
| 621 | epc - compute the exception PC from a |
| 622 | descriptor |
| 623 | -------------------------------------------------*/ |
| 624 | |
| 625 | INLINE UINT32 epc(const opcode_desc *desc) |
| 626 | { |
| 627 | return (desc->flags & OPFLAG_IN_DELAY_SLOT) ? (desc->pc - 3) : desc->pc; |
| 628 | } |
| 629 | |
| 630 | |
| 631 | /*------------------------------------------------- |
| 632 | alloc_handle - allocate a handle if not |
| 633 | already allocated |
| 634 | -------------------------------------------------*/ |
| 635 | |
| 636 | INLINE void alloc_handle(drcuml_state *drcuml, drcuml_codehandle **handleptr, const char *name) |
| 637 | { |
| 638 | if (*handleptr == NULL) |
| 639 | *handleptr = drcuml_handle_alloc(drcuml, name); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | |
| 644 | /*************************************************************************** |
| 170 | 645 | CORE CALLBACKS |
| 171 | 646 | ***************************************************************************/ |
| 172 | 647 | |
| r2627 | r2628 | |
| 183 | 658 | COMPILE_MAX_SEQUENCE, /* maximum instructions to include in a sequence */ |
| 184 | 659 | mips3fe_describe /* callback to describe a single instruction */ |
| 185 | 660 | }; |
| 186 | | size_t memsize = mips3com_init(NULL, flavor, bigendian, index, clock, config, irqcallback, NULL); |
| 661 | UINT32 flags = 0; |
| 662 | size_t extrasize; |
| 663 | void *extramem; |
| 187 | 664 | |
| 188 | | /* allocate a cache and memory for the core data in a single block */ |
| 189 | | allocatedsize = CACHE_SIZE + sizeof(*mips3.core) + memsize; |
| 190 | | mips3.core = osd_alloc_executable(allocatedsize); |
| 191 | | if (mips3.core == NULL) |
| 192 | | fatalerror("Unable to allocate cache of size %d\n", (UINT32)allocatedsize); |
| 193 | | mips3.cache = (UINT8 *)(mips3.core + 1) + memsize; |
| 665 | /* determine how much memory beyond the core size we need */ |
| 666 | extrasize = mips3com_init(NULL, flavor, bigendian, index, clock, config, irqcallback, NULL); |
| 194 | 667 | |
| 668 | /* allocate enough space for the cache and the core */ |
| 669 | mips3.cache = drccache_alloc(CACHE_SIZE + sizeof(*mips3.core) + extrasize); |
| 670 | if (mips3.cache == NULL) |
| 671 | fatalerror("Unable to allocate cache of size %d", (UINT32)(CACHE_SIZE + sizeof(*mips3.core) + extrasize)); |
| 672 | |
| 673 | /* allocate the core and the extra memory */ |
| 674 | mips3.core = drccache_memory_alloc_near(mips3.cache, sizeof(*mips3.core)); |
| 675 | extramem = drccache_memory_alloc(mips3.cache, extrasize); |
| 676 | |
| 195 | 677 | /* initialize the core */ |
| 196 | | mips3com_init(mips3.core, flavor, bigendian, index, clock, config, irqcallback, mips3.core + 1); |
| 197 | | #if COMPARE_AGAINST_C |
| 198 | | mips3c_init(flavor, bigendian, index, clock, config, irqcallback); |
| 199 | | #endif |
| 678 | mips3com_init(mips3.core, flavor, bigendian, index, clock, config, irqcallback, extramem); |
| 200 | 679 | |
| 201 | | /* initialize the DRC to use the cache */ |
| 202 | | mips3drc_init(); |
| 203 | | if (Machine->debug_mode || SINGLE_INSTRUCTION_MODE) |
| 680 | /* allocate memory for cache-local state and initialize it */ |
| 681 | mips3.cstate = drccache_memory_alloc_near(mips3.cache, sizeof(*mips3.cstate)); |
| 682 | memset(mips3.cstate, 0, sizeof(*mips3.cstate)); |
| 683 | memcpy(mips3.cstate->fpmode, fpmode_source, sizeof(fpmode_source)); |
| 684 | memcpy(mips3.cstate->slt_table, slt_table_source, sizeof(slt_table_source)); |
| 685 | memcpy(mips3.cstate->sltu_table, sltu_table_source, sizeof(sltu_table_source)); |
| 686 | memcpy(mips3.cstate->c_un_table, c_un_table_source, sizeof(c_un_table_source)); |
| 687 | memcpy(mips3.cstate->c_eq_table, c_eq_table_source, sizeof(c_eq_table_source)); |
| 688 | memcpy(mips3.cstate->c_ueq_table, c_ueq_table_source, sizeof(c_ueq_table_source)); |
| 689 | memcpy(mips3.cstate->c_olt_table, c_olt_table_source, sizeof(c_olt_table_source)); |
| 690 | memcpy(mips3.cstate->c_ult_table, c_ult_table_source, sizeof(c_ult_table_source)); |
| 691 | memcpy(mips3.cstate->c_ole_table, c_ole_table_source, sizeof(c_ole_table_source)); |
| 692 | memcpy(mips3.cstate->c_ule_table, c_ule_table_source, sizeof(c_ule_table_source)); |
| 693 | |
| 694 | /* initialize the UML generator */ |
| 695 | if (FORCE_C_BACKEND) |
| 696 | flags |= DRCUML_OPTION_USE_C; |
| 697 | if (LOG_UML) |
| 698 | flags |= DRCUML_OPTION_LOG_UML; |
| 699 | if (LOG_NATIVE) |
| 700 | flags |= DRCUML_OPTION_LOG_NATIVE; |
| 701 | mips3.drcuml = drcuml_alloc(mips3.cache, flags, 2, 32, 2); |
| 702 | if (mips3.drcuml == NULL) |
| 703 | fatalerror("Error initializing the UML"); |
| 704 | |
| 705 | /* initialize the front-end helper */ |
| 706 | if (SINGLE_INSTRUCTION_MODE) |
| 204 | 707 | feconfig.max_sequence = 1; |
| 205 | 708 | mips3.drcfe = drcfe_init(&feconfig, mips3.core); |
| 206 | 709 | |
| 207 | | /* start up code logging */ |
| 208 | | if (LOG_CODE) |
| 209 | | mips3.log = x86log_create_context("mips3drc.asm"); |
| 710 | /* mark the cache dirty so it is updated on next execute */ |
| 711 | mips3.cache_dirty = TRUE; |
| 210 | 712 | } |
| 211 | 713 | |
| 212 | 714 | |
| r2627 | r2628 | |
| 216 | 718 | |
| 217 | 719 | static void mips3_reset(void) |
| 218 | 720 | { |
| 219 | | /* reset the common code and flush the cache */ |
| 721 | /* reset the common code and mark the cache dirty */ |
| 220 | 722 | mips3com_reset(mips3.core); |
| 221 | | drc_cache_reset(mips3.drc); |
| 222 | | |
| 223 | | #if COMPARE_AGAINST_C |
| 224 | | mips3c_reset(); |
| 225 | | #endif |
| 723 | mips3.cache_dirty = TRUE; |
| 226 | 724 | } |
| 227 | 725 | |
| 228 | 726 | |
| r2627 | r2628 | |
| 233 | 731 | |
| 234 | 732 | static int mips3_execute(int cycles) |
| 235 | 733 | { |
| 734 | drcuml_state *drcuml = mips3.drcuml; |
| 735 | int execute_result; |
| 736 | |
| 236 | 737 | /* reset the cache if dirty */ |
| 237 | 738 | if (mips3.cache_dirty) |
| 238 | | drc_cache_reset(mips3.drc); |
| 739 | code_flush_cache(drcuml); |
| 239 | 740 | mips3.cache_dirty = FALSE; |
| 240 | 741 | |
| 241 | 742 | /* execute */ |
| 242 | 743 | mips3.core->icount = cycles; |
| 243 | | drc_execute(mips3.drc); |
| 744 | do |
| 745 | { |
| 746 | /* run as much as we can */ |
| 747 | execute_result = drcuml_execute(drcuml, mips3.entry); |
| 748 | |
| 749 | /* if we need to recompile, do it */ |
| 750 | if (execute_result == EXECUTE_MISSING_CODE) |
| 751 | code_compile_block(drcuml, mips3.cstate->mode, mips3.core->pc); |
| 752 | |
| 753 | } while (execute_result != EXECUTE_OUT_OF_CYCLES); |
| 754 | |
| 755 | /* return the number of cycles executed */ |
| 244 | 756 | return cycles - mips3.core->icount; |
| 245 | 757 | } |
| 246 | 758 | |
| r2627 | r2628 | |
| 251 | 763 | |
| 252 | 764 | static void mips3_exit(void) |
| 253 | 765 | { |
| 254 | | /* clean up code logging */ |
| 255 | | if (LOG_CODE) |
| 256 | | x86log_free_context(mips3.log); |
| 257 | | |
| 258 | 766 | /* clean up the DRC */ |
| 259 | 767 | drcfe_exit(mips3.drcfe); |
| 260 | | mips3drc_exit(); |
| 261 | | |
| 262 | | /* free the cache */ |
| 263 | | osd_free_executable(mips3.core, CACHE_SIZE + sizeof(*mips3.core)); |
| 768 | drcuml_free(mips3.drcuml); |
| 769 | drccache_free(mips3.cache); |
| 264 | 770 | } |
| 265 | 771 | |
| 266 | 772 | |
| r2627 | r2628 | |
| 314 | 820 | |
| 315 | 821 | |
| 316 | 822 | /*************************************************************************** |
| 317 | | COMMON UTILITIES |
| 823 | CACHE MANAGEMENT |
| 318 | 824 | ***************************************************************************/ |
| 319 | 825 | |
| 320 | 826 | /*------------------------------------------------- |
| 321 | | fastram_ptr - return a pointer to the base of |
| 322 | | memory containing the given address, if it |
| 323 | | is within a fast RAM range; returns 0 |
| 324 | | otherwise |
| 827 | code_flush_cache - flush the cache and |
| 828 | regenerate static code |
| 325 | 829 | -------------------------------------------------*/ |
| 326 | 830 | |
| 327 | | #ifdef PTR64 |
| 328 | | static void *fastram_ptr(offs_t addr, int size, int iswrite) |
| 831 | static void code_flush_cache(drcuml_state *drcuml) |
| 329 | 832 | { |
| 330 | | int ramnum; |
| 833 | /* empty the transient cache contents */ |
| 834 | drcuml_reset(drcuml); |
| 331 | 835 | |
| 332 | | /* only direct maps are supported */ |
| 333 | | if (addr < 0x80000000 || addr >= 0xc0000000) |
| 334 | | return NULL; |
| 335 | | addr &= 0x1fffffff; |
| 836 | /* generate the entry point and out-of-cycles handlers */ |
| 837 | static_generate_entry_point(drcuml); |
| 838 | static_generate_nocode_handler(drcuml); |
| 839 | static_generate_out_of_cycles(drcuml); |
| 336 | 840 | |
| 337 | | /* adjust address for endianness */ |
| 338 | | if (mips3.core->bigendian) |
| 841 | /* append exception handlers for various types */ |
| 842 | static_generate_exception(drcuml, EXCEPTION_INTERRUPT, TRUE, "exception_interrupt"); |
| 843 | static_generate_exception(drcuml, EXCEPTION_INTERRUPT, FALSE, "exception_interrupt_norecover"); |
| 844 | static_generate_exception(drcuml, EXCEPTION_TLBMOD, TRUE, "exception_tlbmod"); |
| 845 | static_generate_exception(drcuml, EXCEPTION_TLBLOAD, TRUE, "exception_tlbload"); |
| 846 | static_generate_exception(drcuml, EXCEPTION_TLBSTORE, TRUE, "exception_tlbstore"); |
| 847 | static_generate_exception(drcuml, EXCEPTION_SYSCALL, TRUE, "exception_syscall"); |
| 848 | static_generate_exception(drcuml, EXCEPTION_BREAK, TRUE, "exception_break"); |
| 849 | static_generate_exception(drcuml, EXCEPTION_INVALIDOP, TRUE, "exception_invalidop"); |
| 850 | static_generate_exception(drcuml, EXCEPTION_BADCOP, TRUE, "exception_badcop"); |
| 851 | static_generate_exception(drcuml, EXCEPTION_OVERFLOW, TRUE, "exception_overflow"); |
| 852 | static_generate_exception(drcuml, EXCEPTION_TRAP, TRUE, "exception_trap"); |
| 853 | |
| 854 | /* add subroutines for memory accesses */ |
| 855 | static_generate_memory_accessor(drcuml, 1, FALSE, FALSE, "read8", &mips3.read8); |
| 856 | static_generate_memory_accessor(drcuml, 1, TRUE, FALSE, "write8", &mips3.write8); |
| 857 | static_generate_memory_accessor(drcuml, 2, FALSE, FALSE, "read16", &mips3.read16); |
| 858 | static_generate_memory_accessor(drcuml, 2, TRUE, FALSE, "write16", &mips3.write16); |
| 859 | static_generate_memory_accessor(drcuml, 4, FALSE, FALSE, "read32", &mips3.read32); |
| 860 | static_generate_memory_accessor(drcuml, 4, FALSE, TRUE, "read32mask", &mips3.read32mask); |
| 861 | static_generate_memory_accessor(drcuml, 4, TRUE, FALSE, "write32", &mips3.write32); |
| 862 | static_generate_memory_accessor(drcuml, 4, TRUE, TRUE, "write32mask", &mips3.write32mask); |
| 863 | static_generate_memory_accessor(drcuml, 8, FALSE, FALSE, "read64", &mips3.read64); |
| 864 | static_generate_memory_accessor(drcuml, 8, FALSE, TRUE, "read64mask", &mips3.read64mask); |
| 865 | static_generate_memory_accessor(drcuml, 8, TRUE, FALSE, "write64", &mips3.write64); |
| 866 | static_generate_memory_accessor(drcuml, 8, TRUE, TRUE, "write64mask", &mips3.write64mask); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | /*------------------------------------------------- |
| 871 | code_compile_block - compile a block of the |
| 872 | given mode at the specified pc |
| 873 | -------------------------------------------------*/ |
| 874 | |
| 875 | static void code_compile_block(drcuml_state *drcuml, UINT8 mode, offs_t pc) |
| 876 | { |
| 877 | compiler_state compiler = { 0 }; |
| 878 | const opcode_desc *seqhead, *seqlast; |
| 879 | const opcode_desc *desclist; |
| 880 | int override = FALSE; |
| 881 | drcuml_block *block; |
| 882 | jmp_buf errorbuf; |
| 883 | |
| 884 | /* get a description of this sequence */ |
| 885 | desclist = drcfe_describe_code(mips3.drcfe, pc); |
| 886 | if (LOG_UML) |
| 887 | log_opcode_desc(drcuml, desclist, 0); |
| 888 | |
| 889 | /* if we get an error back, flush the cache and try again */ |
| 890 | if (setjmp(errorbuf) != 0) |
| 891 | code_flush_cache(drcuml); |
| 892 | |
| 893 | /* start the block */ |
| 894 | block = drcuml_block_begin(drcuml, 4096, &errorbuf); |
| 895 | |
| 896 | /* loop until we get through all instruction sequences */ |
| 897 | for (seqhead = desclist; seqhead != NULL; seqhead = seqlast->next) |
| 339 | 898 | { |
| 340 | | if (size == 1) |
| 341 | | addr ^= 3; |
| 342 | | else if (size == 2) |
| 343 | | addr ^= 2; |
| 899 | const opcode_desc *curdesc; |
| 900 | UINT32 nextpc; |
| 901 | |
| 902 | /* add a code log entry */ |
| 903 | if (LOG_UML) |
| 904 | UML_COMMENT(block, "-------------------------"); // comment |
| 905 | |
| 906 | /* determine the last instruction in this sequence */ |
| 907 | for (seqlast = seqhead; seqlast != NULL; seqlast = seqlast->next) |
| 908 | if (seqlast->flags & OPFLAG_END_SEQUENCE) |
| 909 | break; |
| 910 | assert(seqlast != NULL); |
| 911 | |
| 912 | /* if we don't have a hash for this mode/pc, or if we are overriding all, add one */ |
| 913 | if (override || !drcuml_hash_exists(drcuml, mode, seqhead->pc)) |
| 914 | UML_HASH(block, mode, seqhead->pc); // hash mode,pc |
| 915 | |
| 916 | /* if we already have a hash, and this is the first sequence, assume that we */ |
| 917 | /* are recompiling due to being out of sync and allow future overrides */ |
| 918 | else if (seqhead == desclist) |
| 919 | { |
| 920 | override = TRUE; |
| 921 | UML_HASH(block, mode, seqhead->pc); // hash mode,pc |
| 922 | } |
| 923 | |
| 924 | /* otherwise, redispatch to that fixed PC and skip the rest of the processing */ |
| 925 | else |
| 926 | { |
| 927 | UML_LABEL(block, seqhead->pc | 0x80000000); // label seqhead->pc | 0x80000000 |
| 928 | UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(seqhead->pc), mips3.nocode); |
| 929 | // hashjmp <mode>,seqhead->pc,nocode |
| 930 | continue; |
| 931 | } |
| 932 | |
| 933 | /* validate this code block if we're not pointing into ROM */ |
| 934 | if (memory_get_write_ptr(cpu_getactivecpu(), ADDRESS_SPACE_PROGRAM, seqhead->physpc) != NULL) |
| 935 | generate_checksum_block(block, &compiler, seqhead, seqlast); |
| 936 | |
| 937 | /* label this instruction, if it may be jumped to locally */ |
| 938 | if (seqhead->flags & OPFLAG_IS_BRANCH_TARGET) |
| 939 | UML_LABEL(block, seqhead->pc | 0x80000000); // label seqhead->pc | 0x80000000 |
| 940 | |
| 941 | /* iterate over instructions in the sequence and compile them */ |
| 942 | for (curdesc = seqhead; curdesc != seqlast->next; curdesc = curdesc->next) |
| 943 | generate_sequence_instruction(block, &compiler, curdesc); |
| 944 | |
| 945 | /* if we need to return to the start, do it */ |
| 946 | if (seqlast->flags & OPFLAG_RETURN_TO_START) |
| 947 | nextpc = pc; |
| 948 | |
| 949 | /* otherwise we just go to the next instruction */ |
| 950 | else |
| 951 | nextpc = seqlast->pc + (seqlast->skipslots + 1) * 4; |
| 952 | |
| 953 | /* count off cycles and go there */ |
| 954 | generate_update_cycles(block, &compiler, nextpc, NULL, TRUE); // <subtract cycles> |
| 955 | |
| 956 | /* if the last instruction can change modes, use a variable mode; otherwise, assume the same mode */ |
| 957 | if (seqlast->flags & OPFLAG_CAN_CHANGE_MODES) |
| 958 | UML_HASHJMP(block, MEM(&mips3.cstate->mode), IMM(nextpc), mips3.nocode);// hashjmp <mode>,nextpc,nocode |
| 959 | else if (seqlast->next == NULL || seqlast->next->pc != nextpc) |
| 960 | UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(nextpc), mips3.nocode); // hashjmp <mode>,nextpc,nocode |
| 344 | 961 | } |
| 345 | 962 | |
| 346 | | /* search for fastram */ |
| 963 | /* end the sequence */ |
| 964 | drcuml_block_end(block); |
| 965 | } |
| 966 | |
| 967 | |
| 968 | |
| 969 | /*************************************************************************** |
| 970 | C FUNCTION CALLBACKS |
| 971 | ***************************************************************************/ |
| 972 | |
| 973 | /*------------------------------------------------- |
| 974 | cfunc_printf_exception - log any exceptions that |
| 975 | aren't interrupts |
| 976 | -------------------------------------------------*/ |
| 977 | |
| 978 | static void cfunc_printf_exception(void *param) |
| 979 | { |
| 980 | printf("Exception: EPC=%08X Cause=%08X BadVAddr=%08X Jmp=%08X\n", (UINT32)mips3.core->cpr[0][COP0_EPC], (UINT32)mips3.core->cpr[0][COP0_Cause], (UINT32)mips3.core->cpr[0][COP0_BadVAddr], mips3.core->pc); |
| 981 | cfunc_printf_probe((void *)(FPTR)mips3.core->cpr[0][COP0_EPC]); |
| 982 | } |
| 983 | |
| 984 | |
| 985 | /*------------------------------------------------- |
| 986 | cfunc_get_cycles - compute the total number |
| 987 | of cycles executed so far |
| 988 | -------------------------------------------------*/ |
| 989 | |
| 990 | static void cfunc_get_cycles(void *param) |
| 991 | { |
| 992 | UINT64 *dest = param; |
| 993 | *dest = activecpu_gettotalcycles(); |
| 994 | } |
| 995 | |
| 996 | |
| 997 | /*------------------------------------------------- |
| 998 | cfunc_printf_probe - print the current CPU |
| 999 | state and return |
| 1000 | -------------------------------------------------*/ |
| 1001 | |
| 1002 | static void cfunc_printf_probe(void *param) |
| 1003 | { |
| 1004 | UINT32 pc = (UINT32)(FPTR)param; |
| 1005 | |
| 1006 | printf(" PC=%08X r1=%08X%08X r2=%08X%08X r3=%08X%08X\n", |
| 1007 | pc, |
| 1008 | (UINT32)(mips3.core->r[1] >> 32), (UINT32)mips3.core->r[1], |
| 1009 | (UINT32)(mips3.core->r[2] >> 32), (UINT32)mips3.core->r[2], |
| 1010 | (UINT32)(mips3.core->r[3] >> 32), (UINT32)mips3.core->r[3]); |
| 1011 | printf(" r4=%08X%08X r5=%08X%08X r6=%08X%08X r7=%08X%08X\n", |
| 1012 | (UINT32)(mips3.core->r[4] >> 32), (UINT32)mips3.core->r[4], |
| 1013 | (UINT32)(mips3.core->r[5] >> 32), (UINT32)mips3.core->r[5], |
| 1014 | (UINT32)(mips3.core->r[6] >> 32), (UINT32)mips3.core->r[6], |
| 1015 | (UINT32)(mips3.core->r[7] >> 32), (UINT32)mips3.core->r[7]); |
| 1016 | printf(" r8=%08X%08X r9=%08X%08X r10=%08X%08X r11=%08X%08X\n", |
| 1017 | (UINT32)(mips3.core->r[8] >> 32), (UINT32)mips3.core->r[8], |
| 1018 | (UINT32)(mips3.core->r[9] >> 32), (UINT32)mips3.core->r[9], |
| 1019 | (UINT32)(mips3.core->r[10] >> 32), (UINT32)mips3.core->r[10], |
| 1020 | (UINT32)(mips3.core->r[11] >> 32), (UINT32)mips3.core->r[11]); |
| 1021 | printf("r12=%08X%08X r13=%08X%08X r14=%08X%08X r15=%08X%08X\n", |
| 1022 | (UINT32)(mips3.core->r[12] >> 32), (UINT32)mips3.core->r[12], |
| 1023 | (UINT32)(mips3.core->r[13] >> 32), (UINT32)mips3.core->r[13], |
| 1024 | (UINT32)(mips3.core->r[14] >> 32), (UINT32)mips3.core->r[14], |
| 1025 | (UINT32)(mips3.core->r[15] >> 32), (UINT32)mips3.core->r[15]); |
| 1026 | printf("r16=%08X%08X r17=%08X%08X r18=%08X%08X r19=%08X%08X\n", |
| 1027 | (UINT32)(mips3.core->r[16] >> 32), (UINT32)mips3.core->r[16], |
| 1028 | (UINT32)(mips3.core->r[17] >> 32), (UINT32)mips3.core->r[17], |
| 1029 | (UINT32)(mips3.core->r[18] >> 32), (UINT32)mips3.core->r[18], |
| 1030 | (UINT32)(mips3.core->r[19] >> 32), (UINT32)mips3.core->r[19]); |
| 1031 | printf("r20=%08X%08X r21=%08X%08X r22=%08X%08X r23=%08X%08X\n", |
| 1032 | (UINT32)(mips3.core->r[20] >> 32), (UINT32)mips3.core->r[20], |
| 1033 | (UINT32)(mips3.core->r[21] >> 32), (UINT32)mips3.core->r[21], |
| 1034 | (UINT32)(mips3.core->r[22] >> 32), (UINT32)mips3.core->r[22], |
| 1035 | (UINT32)(mips3.core->r[23] >> 32), (UINT32)mips3.core->r[23]); |
| 1036 | printf("r24=%08X%08X r25=%08X%08X r26=%08X%08X r27=%08X%08X\n", |
| 1037 | (UINT32)(mips3.core->r[24] >> 32), (UINT32)mips3.core->r[24], |
| 1038 | (UINT32)(mips3.core->r[25] >> 32), (UINT32)mips3.core->r[25], |
| 1039 | (UINT32)(mips3.core->r[26] >> 32), (UINT32)mips3.core->r[26], |
| 1040 | (UINT32)(mips3.core->r[27] >> 32), (UINT32)mips3.core->r[27]); |
| 1041 | printf("r28=%08X%08X r29=%08X%08X r30=%08X%08X r31=%08X%08X\n", |
| 1042 | (UINT32)(mips3.core->r[28] >> 32), (UINT32)mips3.core->r[28], |
| 1043 | (UINT32)(mips3.core->r[29] >> 32), (UINT32)mips3.core->r[29], |
| 1044 | (UINT32)(mips3.core->r[30] >> 32), (UINT32)mips3.core->r[30], |
| 1045 | (UINT32)(mips3.core->r[31] >> 32), (UINT32)mips3.core->r[31]); |
| 1046 | printf(" hi=%08X%08X lo=%08X%08X\n", |
| 1047 | (UINT32)(mips3.core->r[REG_HI] >> 32), (UINT32)mips3.core->r[REG_HI], |
| 1048 | (UINT32)(mips3.core->r[REG_LO] >> 32), (UINT32)mips3.core->r[REG_LO]); |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | /*************************************************************************** |
| 1054 | STATIC CODEGEN |
| 1055 | ***************************************************************************/ |
| 1056 | |
| 1057 | /*------------------------------------------------- |
| 1058 | static_generate_entry_point - generate a |
| 1059 | static entry point |
| 1060 | -------------------------------------------------*/ |
| 1061 | |
| 1062 | static void static_generate_entry_point(drcuml_state *drcuml) |
| 1063 | { |
| 1064 | drcuml_codelabel skip = 1; |
| 1065 | drcuml_block *block; |
| 1066 | jmp_buf errorbuf; |
| 1067 | |
| 1068 | /* if we get an error back, we're screwed */ |
| 1069 | if (setjmp(errorbuf) != 0) |
| 1070 | fatalerror("Unrecoverable error in static_generate_entry_point"); |
| 1071 | |
| 1072 | /* begin generating */ |
| 1073 | block = drcuml_block_begin(drcuml, 20, &errorbuf); |
| 1074 | |
| 1075 | /* forward references */ |
| 1076 | alloc_handle(drcuml, &mips3.exception_norecover[EXCEPTION_INTERRUPT], "interrupt_norecover"); |
| 1077 | alloc_handle(drcuml, &mips3.nocode, "nocode"); |
| 1078 | |
| 1079 | alloc_handle(drcuml, &mips3.entry, "entry"); |
| 1080 | UML_HANDLE(block, mips3.entry); // handle entry |
| 1081 | |
| 1082 | /* reset the FPU mode */ |
| 1083 | UML_AND(block, IREG(0), CCR164(31), IMM(3)); // and i0,ccr1[31],3 |
| 1084 | UML_LOAD1U(block, IREG(0), &mips3.cstate->fpmode[0], IREG(0)); // load1u i0,fpmode,i0 |
| 1085 | UML_SETFMOD(block, IREG(0)); // setfmod i0 |
| 1086 | |
| 1087 | /* check for interrupts */ |
| 1088 | UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status)); // and i0,[Cause],[Status] |
| 1089 | UML_ANDf(block, IREG(0), IREG(0), IMM(0xfc00), FLAGS_Z); // and i0,i0,0xfc00,Z |
| 1090 | UML_JMPc(block, IF_Z, skip); // jmp skip,Z |
| 1091 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_IE)); // test [Status],SR_IE |
| 1092 | UML_JMPc(block, IF_Z, skip); // jmp skip,Z |
| 1093 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_EXL | SR_ERL)); // test [Status],SR_EXL | SR_ERL |
| 1094 | UML_JMPc(block, IF_NZ, skip); // jmp skip,NZ |
| 1095 | UML_MOV(block, IREG(0), MEM(&mips3.core->pc)); // mov i0,pc |
| 1096 | UML_MOV(block, IREG(1), IMM(0)); // mov i1,0 |
| 1097 | UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]); // callh exception_norecover |
| 1098 | UML_LABEL(block, skip); // skip: |
| 1099 | |
| 1100 | /* generate a hash jump via the current mode and PC */ |
| 1101 | UML_HASHJMP(block, MEM(&mips3.cstate->mode), MEM(&mips3.core->pc), mips3.nocode);// hashjmp <mode>,<pc>,nocode |
| 1102 | |
| 1103 | drcuml_block_end(block); |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | /*------------------------------------------------- |
| 1108 | static_generate_nocode_handler - generate an |
| 1109 | exception handler for "out of code" |
| 1110 | -------------------------------------------------*/ |
| 1111 | |
| 1112 | static void static_generate_nocode_handler(drcuml_state *drcuml) |
| 1113 | { |
| 1114 | drcuml_block *block; |
| 1115 | jmp_buf errorbuf; |
| 1116 | |
| 1117 | /* if we get an error back, we're screwed */ |
| 1118 | if (setjmp(errorbuf) != 0) |
| 1119 | fatalerror("Unrecoverable error in static_generate_nocode_handler"); |
| 1120 | |
| 1121 | /* begin generating */ |
| 1122 | block = drcuml_block_begin(drcuml, 10, &errorbuf); |
| 1123 | |
| 1124 | /* generate a hash jump via the current mode and PC */ |
| 1125 | alloc_handle(drcuml, &mips3.nocode, "nocode"); |
| 1126 | UML_HANDLE(block, mips3.nocode); // handle nocode |
| 1127 | UML_GETEXP(block, IREG(0)); // getexp i0 |
| 1128 | UML_MOV(block, MEM(&mips3.core->pc), IREG(0)); // mov [pc],i0 |
| 1129 | UML_EXIT(block, IMM(EXECUTE_MISSING_CODE)); // exit EXECUTE_MISSING_CODE |
| 1130 | |
| 1131 | drcuml_block_end(block); |
| 1132 | } |
| 1133 | |
| 1134 | |
| 1135 | /*------------------------------------------------- |
| 1136 | static_generate_out_of_cycles - generate an |
| 1137 | out of cycles exception handler |
| 1138 | -------------------------------------------------*/ |
| 1139 | |
| 1140 | static void static_generate_out_of_cycles(drcuml_state *drcuml) |
| 1141 | { |
| 1142 | drcuml_block *block; |
| 1143 | jmp_buf errorbuf; |
| 1144 | |
| 1145 | /* if we get an error back, we're screwed */ |
| 1146 | if (setjmp(errorbuf) != 0) |
| 1147 | fatalerror("Unrecoverable error in static_generate_out_of_cycles"); |
| 1148 | |
| 1149 | /* begin generating */ |
| 1150 | block = drcuml_block_begin(drcuml, 10, &errorbuf); |
| 1151 | |
| 1152 | /* generate a hash jump via the current mode and PC */ |
| 1153 | alloc_handle(drcuml, &mips3.out_of_cycles, "out_of_cycles"); |
| 1154 | UML_HANDLE(block, mips3.out_of_cycles); // handle out_of_cycles |
| 1155 | UML_GETEXP(block, IREG(0)); // getexp i0 |
| 1156 | UML_MOV(block, MEM(&mips3.core->pc), IREG(0)); // mov <pc>,i0 |
| 1157 | UML_EXIT(block, IMM(EXECUTE_OUT_OF_CYCLES)); // exit EXECUTE_OUT_OF_CYCLES |
| 1158 | |
| 1159 | drcuml_block_end(block); |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | /*------------------------------------------------- |
| 1164 | static_generate_exception - generate a static |
| 1165 | exception handler |
| 1166 | -------------------------------------------------*/ |
| 1167 | |
| 1168 | static void static_generate_exception(drcuml_state *drcuml, UINT8 exception, int recover, const char *name) |
| 1169 | { |
| 1170 | UINT32 offset = (exception >= EXCEPTION_TLBMOD && exception <= EXCEPTION_TLBSTORE) ? 0x00 : 0x180; |
| 1171 | drcuml_codelabel next = 1; |
| 1172 | drcuml_block *block; |
| 1173 | jmp_buf errorbuf; |
| 1174 | |
| 1175 | /* if we get an error back, we're screwed */ |
| 1176 | if (setjmp(errorbuf) != 0) |
| 1177 | fatalerror("Unrecoverable error in static_generate_exception"); |
| 1178 | |
| 1179 | /* begin generating */ |
| 1180 | block = drcuml_block_begin(drcuml, 1024, &errorbuf); |
| 1181 | |
| 1182 | /* add a global entry for this */ |
| 1183 | if (recover) |
| 1184 | { |
| 1185 | alloc_handle(drcuml, &mips3.exception[exception], name); |
| 1186 | UML_HANDLE(block, mips3.exception[exception]); // handle name |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | alloc_handle(drcuml, &mips3.exception_norecover[exception], name); |
| 1191 | UML_HANDLE(block, mips3.exception_norecover[exception]); // handle name |
| 1192 | } |
| 1193 | |
| 1194 | /* exception parameter is expected to be the fault address in this case */ |
| 1195 | if (exception == EXCEPTION_TLBLOAD || exception == EXCEPTION_TLBSTORE) |
| 1196 | { |
| 1197 | /* set BadVAddr to the fault address */ |
| 1198 | UML_GETEXP(block, CPR032(COP0_BadVAddr)); // mov [BadVAddr],exp |
| 1199 | |
| 1200 | /* set the upper bits of EntryHi to the fault page */ |
| 1201 | UML_AND(block, IREG(0), IREG(0), IMM(0xffffe000)); // and i0,i0,0xffffe000 |
| 1202 | UML_AND(block, IREG(1), CPR032(COP0_EntryHi), IMM(0x000000ff)); // and i1,[EntryHi],0x000000ff |
| 1203 | UML_OR(block, CPR032(COP0_EntryHi), IREG(0), IREG(1)); // or [EntryHi],i0,i1 |
| 1204 | |
| 1205 | /* set the lower bits of Context to the fault page */ |
| 1206 | UML_SHR(block, IREG(0), IREG(0), IMM(9)); // shr i0,i0,9 |
| 1207 | UML_AND(block, IREG(1), CPR032(COP0_Context), IMM(0xff800000)); // and i1,[Context],0xff800000 |
| 1208 | UML_OR(block, CPR032(COP0_Context), IREG(0), IREG(1)); // or [Context],i0,i1 |
| 1209 | } |
| 1210 | |
| 1211 | /* set the EPC and Cause registers */ |
| 1212 | if (recover) |
| 1213 | { |
| 1214 | UML_RECOVER(block, IREG(0), MAPVAR_PC); // recover i0,PC |
| 1215 | UML_RECOVER(block, IREG(1), MAPVAR_CYCLES); // recover i1,CYCLES |
| 1216 | } |
| 1217 | |
| 1218 | UML_AND(block, IREG(2), CPR032(COP0_Cause), IMM(~0x800000ff)); // and i2,[Cause],~0x800000ff |
| 1219 | UML_TEST(block, IREG(0), IMM(1)); // test i0,1 |
| 1220 | UML_JMPc(block, IF_Z, next); // jz <next> |
| 1221 | UML_OR(block, IREG(2), IREG(2), IMM(0x80000000)); // or i2,i2,0x80000000 |
| 1222 | UML_SUB(block, IREG(0), IREG(0), IMM(1)); // sub i0,i0,1 |
| 1223 | UML_LABEL(block, next); // <next>: |
| 1224 | UML_MOV(block, CPR032(COP0_EPC), IREG(0)); // mov [EPC],i0 |
| 1225 | UML_OR(block, CPR032(COP0_Cause), IREG(2), IMM(exception << 2)); // or [Cause],i2,exception << 2 |
| 1226 | |
| 1227 | /* set EXL in the SR */ |
| 1228 | UML_OR(block, CPR032(COP0_Status), CPR032(COP0_Status), IMM(SR_EXL)); // or [Status],[Status],SR_EXL |
| 1229 | |
| 1230 | /* optionally print exceptions */ |
| 1231 | if (PRINTF_EXCEPTIONS && exception != EXCEPTION_INTERRUPT && exception != EXCEPTION_SYSCALL) |
| 1232 | UML_CALLC(block, cfunc_printf_exception, NULL); // callc cfunc_printf_exception,NULL |
| 1233 | |
| 1234 | /* choose our target PC */ |
| 1235 | UML_MOV(block, IREG(0), IMM(0xbfc00200 + offset)); // mov i0,0xbfc00200 + offset |
| 1236 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_BEV)); // test [Status],SR_BEV |
| 1237 | UML_MOVc(block, IF_Z, IREG(0), IMM(0x80000000 + offset)); // mov i0,0x80000000 + offset,z |
| 1238 | |
| 1239 | /* adjust cycles */ |
| 1240 | UML_SUBf(block, MEM(&mips3.core->icount), MEM(&mips3.core->icount), IREG(1), FLAGS_S); // sub icount,icount,cycles,S |
| 1241 | UML_EXHc(block, IF_S, mips3.out_of_cycles, IREG(0)); // exh out_of_cycles,i0 |
| 1242 | |
| 1243 | UML_HASHJMP(block, MEM(&mips3.cstate->mode), IREG(0), mips3.nocode); // hashjmp <mode>,i0,nocode |
| 1244 | |
| 1245 | drcuml_block_end(block); |
| 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | /*------------------------------------------------------------------ |
| 1250 | static_generate_memory_accessor |
| 1251 | ------------------------------------------------------------------*/ |
| 1252 | |
| 1253 | static void static_generate_memory_accessor(drcuml_state *drcuml, int size, int iswrite, int ismasked, const char *name, drcuml_codehandle **handleptr) |
| 1254 | { |
| 1255 | /* on entry, address is in I0; data for writes is in I1; mask for accesses is in I2 */ |
| 1256 | /* on exit, read result is in I0 */ |
| 1257 | /* routine trashes I0-I3 */ |
| 1258 | drcuml_codehandle *exception_target = mips3.exception[iswrite ? EXCEPTION_TLBSTORE : EXCEPTION_TLBLOAD]; |
| 1259 | drcuml_block *block; |
| 1260 | jmp_buf errorbuf; |
| 1261 | int label = 0; |
| 1262 | int ramnum; |
| 1263 | |
| 1264 | /* if we get an error back, we're screwed */ |
| 1265 | if (setjmp(errorbuf) != 0) |
| 1266 | fatalerror("Unrecoverable error in static_generate_exception"); |
| 1267 | |
| 1268 | /* begin generating */ |
| 1269 | block = drcuml_block_begin(drcuml, 1024, &errorbuf); |
| 1270 | |
| 1271 | /* add a global entry for this */ |
| 1272 | alloc_handle(drcuml, handleptr, name); |
| 1273 | UML_HANDLE(block, *handleptr); // handle *handleptr |
| 1274 | |
| 1275 | /* general case: assume paging and perform a translation */ |
| 1276 | UML_SHR(block, IREG(3), IREG(0), IMM(12)); // shr i3,i0,12 |
| 1277 | UML_LOAD4(block, IREG(3), mips3.core->tlb_table, IREG(3)); // load4 i3,[tlb_table],i3 |
| 1278 | UML_TEST(block, IREG(3), IMM(iswrite ? 1 : 2)); // test i3,iswrite ? 1 : 2 |
| 1279 | UML_EXHc(block, IF_NZ, exception_target, IREG(0)); // exnz exception_target,i0 |
| 1280 | UML_AND(block, IREG(0), IREG(0), IMM(0xfff)); // and i0,i0,0xfff |
| 1281 | UML_AND(block, IREG(3), IREG(3), IMM(~0xfff)); // and i3,i3,~0xfff |
| 1282 | UML_OR(block, IREG(0), IREG(0), IREG(3)); // or i0,i0,i3 |
| 1283 | |
| 347 | 1284 | for (ramnum = 0; ramnum < MIPS3_MAX_FASTRAM; ramnum++) |
| 348 | | if (mips3.fastram[ramnum].base != NULL && (!iswrite || !mips3.fastram[ramnum].readonly) && |
| 349 | | addr >= mips3.fastram[ramnum].start && addr <= mips3.fastram[ramnum].end) |
| 1285 | if (!Machine->debug_mode && mips3.fastram[ramnum].base != NULL && (!iswrite || !mips3.fastram[ramnum].readonly)) |
| 350 | 1286 | { |
| 351 | | return (UINT8 *)mips3.fastram[ramnum].base + (addr - mips3.fastram[ramnum].start); |
| 1287 | void *fastbase = (UINT8 *)mips3.fastram[ramnum].base - mips3.fastram[ramnum].start; |
| 1288 | UINT32 skip = label++; |
| 1289 | if (mips3.fastram[ramnum].end != 0xffffffff) |
| 1290 | { |
| 1291 | UML_CMP(block, IREG(0), IMM(mips3.fastram[ramnum].end)); // cmp i0,end |
| 1292 | UML_JMPc(block, IF_A, skip); // ja skip |
| 1293 | } |
| 1294 | if (mips3.fastram[ramnum].start != 0x00000000) |
| 1295 | { |
| 1296 | UML_CMP(block, IREG(0), IMM(mips3.fastram[ramnum].start)); // cmp i0,fastram_start |
| 1297 | UML_JMPc(block, IF_B, skip); // jb skip |
| 1298 | } |
| 1299 | |
| 1300 | if (!iswrite) |
| 1301 | { |
| 1302 | if (size == 1) |
| 1303 | { |
| 1304 | UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE4_XOR_BE(0) : BYTE4_XOR_LE(0))); |
| 1305 | // xor i0,i0,bytexor |
| 1306 | UML_LOAD1U(block, IREG(0), fastbase, IREG(0)); // load1u i0,fastbase,i0 |
| 1307 | } |
| 1308 | else if (size == 2) |
| 1309 | { |
| 1310 | UML_SHR(block, IREG(0), IREG(0), IMM(1)); // shr i0,i0,1 |
| 1311 | UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0))); |
| 1312 | // xor i0,i0,bytexor |
| 1313 | UML_LOAD2U(block, IREG(0), fastbase, IREG(0)); // load2u i0,fastbase,i0 |
| 1314 | } |
| 1315 | else if (size == 4) |
| 1316 | { |
| 1317 | UML_SHR(block, IREG(0), IREG(0), IMM(2)); // shr i0,i0,2 |
| 1318 | UML_LOAD4(block, IREG(0), fastbase, IREG(0)); // load4 i0,fastbase,i0 |
| 1319 | } |
| 1320 | else if (size == 8) |
| 1321 | { |
| 1322 | UML_SHR(block, IREG(0), IREG(0), IMM(3)); // shr i0,i0,3 |
| 1323 | UML_DLOAD8(block, IREG(0), fastbase, IREG(0)); // dload8 i0,fastbase,i0 |
| 1324 | UML_DROR(block, IREG(0), IREG(0), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0)))); |
| 1325 | // dror i0,i0,32*bytexor |
| 1326 | } |
| 1327 | UML_RET(block); // ret |
| 1328 | } |
| 1329 | else |
| 1330 | { |
| 1331 | if (size == 1) |
| 1332 | { |
| 1333 | UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE4_XOR_BE(0) : BYTE4_XOR_LE(0))); |
| 1334 | // xor i0,i0,bytexor |
| 1335 | UML_STORE1(block, fastbase, IREG(0), IREG(1)); // store1 fastbase,i0,i1 |
| 1336 | } |
| 1337 | else if (size == 2) |
| 1338 | { |
| 1339 | UML_SHR(block, IREG(0), IREG(0), IMM(1)); // shr i0,i0,1 |
| 1340 | UML_XOR(block, IREG(0), IREG(0), IMM(mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0))); |
| 1341 | // xor i0,i0,bytexor |
| 1342 | UML_STORE2(block, fastbase, IREG(0), IREG(1)); // store2 fastbase,i0,i1 |
| 1343 | } |
| 1344 | else if (size == 4) |
| 1345 | { |
| 1346 | UML_SHR(block, IREG(0), IREG(0), IMM(2)); // shr i0,i0,2 |
| 1347 | if (ismasked) |
| 1348 | { |
| 1349 | UML_LOAD4(block, IREG(3), fastbase, IREG(0)); // load4 i3,fastbase,i0 |
| 1350 | UML_AND(block, IREG(1), IREG(1), IREG(2)); // and i1,i1,i2 |
| 1351 | UML_XOR(block, IREG(2), IREG(2), IMM(0xffffffff)); // xor i2,i2,0xfffffffff |
| 1352 | UML_AND(block, IREG(3), IREG(3), IREG(2)); // and i3,i3,i2 |
| 1353 | UML_OR(block, IREG(1), IREG(1), IREG(3)); // or i1,i1,i3 |
| 1354 | } |
| 1355 | UML_STORE4(block, fastbase, IREG(0), IREG(1)); // store4 fastbase,i0,i1 |
| 1356 | } |
| 1357 | else if (size == 8) |
| 1358 | { |
| 1359 | UML_SHR(block, IREG(0), IREG(0), IMM(3)); // shr i0,i0,3 |
| 1360 | UML_DROR(block, IREG(1), IREG(1), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0)))); |
| 1361 | // dror i1,i1,32*bytexor |
| 1362 | if (ismasked) |
| 1363 | { |
| 1364 | UML_DROR(block, IREG(2), IREG(2), IMM(32 * (mips3.core->bigendian ? BYTE_XOR_BE(0) : BYTE_XOR_LE(0)))); |
| 1365 | // dror i2,i2,32*bytexor |
| 1366 | UML_DLOAD8(block, IREG(3), fastbase, IREG(0)); // dload8 i3,fastbase,i0 |
| 1367 | UML_DAND(block, IREG(1), IREG(1), IREG(2)); // dand i1,i1,i2 |
| 1368 | UML_DXOR(block, IREG(2), IREG(2), IMM(U64(0xffffffffffffffff))); |
| 1369 | // dxor i2,i2,0xfffffffffffffffff |
| 1370 | UML_DAND(block, IREG(3), IREG(3), IREG(2)); // dand i3,i3,i2 |
| 1371 | UML_DOR(block, IREG(1), IREG(1), IREG(3)); // dor i1,i1,i3 |
| 1372 | } |
| 1373 | UML_DSTORE8(block, fastbase, IREG(0), IREG(1)); // dstore8 fastbase,i0,i1 |
| 1374 | } |
| 1375 | UML_RET(block); // ret |
| 1376 | } |
| 1377 | |
| 1378 | UML_LABEL(block, skip); // skip: |
| 352 | 1379 | } |
| 353 | 1380 | |
| 354 | | return NULL; |
| 1381 | switch (size) |
| 1382 | { |
| 1383 | case 1: |
| 1384 | if (iswrite) |
| 1385 | UML_WRITE1(block, PROGRAM, IREG(0), IREG(1)); // write1 i0,i1 |
| 1386 | else |
| 1387 | UML_READ1U(block, IREG(0), PROGRAM, IREG(0)); // read1u i0,i0 |
| 1388 | break; |
| 1389 | |
| 1390 | case 2: |
| 1391 | if (iswrite) |
| 1392 | UML_WRITE2(block, PROGRAM, IREG(0), IREG(1)); // write2 i0,i1 |
| 1393 | else |
| 1394 | UML_READ2U(block, IREG(0), PROGRAM, IREG(0)); // read2u i0,i0 |
| 1395 | break; |
| 1396 | |
| 1397 | case 4: |
| 1398 | if (iswrite) |
| 1399 | { |
| 1400 | if (!ismasked) |
| 1401 | UML_WRITE4(block, PROGRAM, IREG(0), IREG(1)); // write4 PROGRAM,i0,i1 |
| 1402 | else |
| 1403 | UML_WRIT4M(block, PROGRAM, IREG(0), IREG(2), IREG(1)); // writ4m PROGRAM,i0,i2,i1 |
| 1404 | } |
| 1405 | else |
| 1406 | { |
| 1407 | if (!ismasked) |
| 1408 | UML_READ4(block, IREG(0), PROGRAM, IREG(0)); // read4 i0,PROGRAM,i0 |
| 1409 | else |
| 1410 | UML_READ4M(block, IREG(0), PROGRAM, IREG(0), IREG(2)); // read4m i0,PROGRAM,i0,i2 |
| 1411 | } |
| 1412 | break; |
| 1413 | |
| 1414 | case 8: |
| 1415 | if (iswrite) |
| 1416 | { |
| 1417 | if (!ismasked) |
| 1418 | UML_DWRITE8(block, PROGRAM, IREG(0), IREG(1)); // dwrite8 PROGRAM,i0,i1 |
| 1419 | else |
| 1420 | UML_DWRIT8M(block, PROGRAM, IREG(0), IREG(2), IREG(1)); // dwrit8m PROGRAM,i0,i2,i1 |
| 1421 | } |
| 1422 | else |
| 1423 | { |
| 1424 | if (!ismasked) |
| 1425 | UML_DREAD8(block, IREG(0), PROGRAM, IREG(0)); // dread8 i0,PROGRAM,i0 |
| 1426 | else |
| 1427 | UML_DREAD8M(block, IREG(0), PROGRAM, IREG(0), IREG(2)); // dread8m i0,PROGRAM,i0,i2 |
| 1428 | } |
| 1429 | break; |
| 1430 | } |
| 1431 | UML_RET(block); // ret |
| 1432 | |
| 1433 | drcuml_block_end(block); |
| 355 | 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | |
| 1438 | /*************************************************************************** |
| 1439 | CODE GENERATION |
| 1440 | ***************************************************************************/ |
| 1441 | |
| 1442 | /*------------------------------------------------- |
| 1443 | generate_update_cycles - generate code to |
| 1444 | subtract cycles from the icount and generate |
| 1445 | an exception if out |
| 1446 | -------------------------------------------------*/ |
| 1447 | |
| 1448 | static void generate_update_cycles(drcuml_block *block, compiler_state *compiler, UINT32 nextpc, UINT32 *mempc, int allow_exception) |
| 1449 | { |
| 1450 | /* check software interrupts if pending */ |
| 1451 | if (compiler->checksoftints) |
| 1452 | { |
| 1453 | drcuml_codelabel skip; |
| 1454 | |
| 1455 | compiler->checksoftints = FALSE; |
| 1456 | UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status)); // and i0,[Cause],[Status] |
| 1457 | UML_ANDf(block, IREG(0), IREG(0), IMM(0x0300), FLAGS_Z); // and i0,i0,0x0300,Z |
| 1458 | UML_JMPc(block, IF_Z, skip = compiler->labelnum++); // jmp skip,Z |
| 1459 | if (mempc == NULL) |
| 1460 | UML_MOV(block, IREG(0), IMM(nextpc)); // mov i0,nextpc |
| 1461 | else |
| 1462 | UML_MOV(block, IREG(0), MEM(mempc)); // mov i0,[mempc] |
| 1463 | UML_MOV(block, IREG(1), IMM(compiler->cycles)); // mov i1,cycles |
| 1464 | UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]); // callh interrupt_norecover |
| 1465 | UML_LABEL(block, skip); // skip: |
| 1466 | } |
| 1467 | |
| 1468 | /* check full interrupts if pending */ |
| 1469 | if (compiler->checkints) |
| 1470 | { |
| 1471 | drcuml_codelabel skip; |
| 1472 | |
| 1473 | compiler->checkints = FALSE; |
| 1474 | UML_AND(block, IREG(0), CPR032(COP0_Cause), CPR032(COP0_Status)); // and i0,[Cause],[Status] |
| 1475 | UML_ANDf(block, IREG(0), IREG(0), IMM(0xfc00), FLAGS_Z); // and i0,i0,0xfc00,Z |
| 1476 | UML_JMPc(block, IF_Z, skip = compiler->labelnum++); // jmp skip,Z |
| 1477 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_IE)); // test [Status],SR_IE |
| 1478 | UML_JMPc(block, IF_Z, skip); // jmp skip,Z |
| 1479 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_EXL | SR_ERL)); // test [Status],SR_EXL | SR_ERL |
| 1480 | UML_JMPc(block, IF_NZ, skip); // jmp skip,NZ |
| 1481 | if (mempc == NULL) |
| 1482 | UML_MOV(block, IREG(0), IMM(nextpc)); // mov i0,nextpc |
| 1483 | else |
| 1484 | UML_MOV(block, IREG(0), MEM(mempc)); // mov i0,[mempc] |
| 1485 | UML_MOV(block, IREG(1), IMM(compiler->cycles)); // mov i1,cycles |
| 1486 | UML_CALLH(block, mips3.exception_norecover[EXCEPTION_INTERRUPT]); // callh interrupt_norecover |
| 1487 | UML_LABEL(block, skip); // skip: |
| 1488 | } |
| 1489 | |
| 1490 | /* account for cycles */ |
| 1491 | if (compiler->cycles > 0) |
| 1492 | { |
| 1493 | UML_SUBf(block, MEM(&mips3.core->icount), MEM(&mips3.core->icount), MAPVAR_CYCLES, allow_exception ? FLAGS_S : 0); |
| 1494 | // sub icount,icount,cycles,S |
| 1495 | UML_MAPVAR(block, MAPVAR_CYCLES, 0); // mapvar cycles,0 |
| 1496 | if (allow_exception) |
| 1497 | { |
| 1498 | if (mempc == NULL) |
| 1499 | UML_EXHc(block, IF_S, mips3.out_of_cycles, IMM(nextpc)); // exh out_of_cycles,nextpc |
| 1500 | else |
| 1501 | UML_EXHc(block, IF_S, mips3.out_of_cycles, MEM(mempc)); // exh out_of_cycles,<mempc> |
| 1502 | } |
| 1503 | } |
| 1504 | compiler->cycles = 0; |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | /*------------------------------------------------- |
| 1509 | generate_checksum_block - generate code to |
| 1510 | validate a sequence of opcodes |
| 1511 | -------------------------------------------------*/ |
| 1512 | |
| 1513 | static void generate_checksum_block(drcuml_block *block, compiler_state *compiler, const opcode_desc *seqhead, const opcode_desc *seqlast) |
| 1514 | { |
| 1515 | const opcode_desc *curdesc; |
| 1516 | if (LOG_UML) |
| 1517 | UML_COMMENT(block, "[Validation for %08X]", seqhead->pc); // comment |
| 1518 | |
| 1519 | /* loose verify or single instruction: just compare and fail */ |
| 1520 | if (!(mips3.drcoptions & MIPS3DRC_STRICT_VERIFY) || seqhead->next == NULL) |
| 1521 | { |
| 1522 | UML_LOAD4(block, IREG(0), seqhead->opptr.l, IMM(0)); // load4 i0,*opptr |
| 1523 | UML_CMP(block, IREG(0), IMM(*seqhead->opptr.l)); // cmp i0,*opptr |
| 1524 | UML_EXHc(block, IF_NE, mips3.nocode, IMM(epc(seqhead))); // exne nocode,seqhead->pc |
| 1525 | } |
| 1526 | |
| 1527 | /* full verification; sum up everything */ |
| 1528 | else |
| 1529 | { |
| 1530 | UINT32 sum = 0; |
| 1531 | UML_LOAD4(block, IREG(0), seqhead->opptr.l, IMM(0)); // load4 i0,*opptr |
| 1532 | sum += *seqhead->opptr.l; |
| 1533 | for (curdesc = seqhead->next; curdesc != seqlast->next; curdesc = curdesc->next) |
| 1534 | { |
| 1535 | UML_LOAD4(block, IREG(1), curdesc->opptr.l, IMM(0)); // load4 i1,*opptr |
| 1536 | UML_ADD(block, IREG(0), IREG(0), IREG(1)); // add i0,i0,i1 |
| 1537 | sum += *curdesc->opptr.l; |
| 1538 | } |
| 1539 | UML_CMP(block, IREG(0), IMM(sum)); // cmp i0,sum |
| 1540 | UML_EXHc(block, IF_NE, mips3.nocode, IMM(epc(seqhead))); // exne nocode,seqhead->pc |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | |
| 1545 | /*------------------------------------------------- |
| 1546 | generate_sequence_instruction - generate code |
| 1547 | for a single instruction in a sequence |
| 1548 | -------------------------------------------------*/ |
| 1549 | |
| 1550 | static void generate_sequence_instruction(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 1551 | { |
| 1552 | offs_t expc; |
| 1553 | int hotnum; |
| 1554 | |
| 1555 | /* add an entry for the log */ |
| 1556 | if (LOG_UML) |
| 1557 | log_add_disasm_comment(block, desc->pc, *desc->opptr.l); |
| 1558 | |
| 1559 | /* set the PC map variable */ |
| 1560 | expc = (desc->flags & OPFLAG_IN_DELAY_SLOT) ? desc->pc - 3 : desc->pc; |
| 1561 | UML_MAPVAR(block, MAPVAR_PC, expc); // mapvar PC,expc |
| 1562 | |
| 1563 | /* accumulate total cycles */ |
| 1564 | compiler->cycles += desc->cycles; |
| 1565 | |
| 1566 | /* is this a hotspot? */ |
| 1567 | for (hotnum = 0; hotnum < MIPS3_MAX_HOTSPOTS; hotnum++) |
| 1568 | if (desc->pc == mips3.hotspot[hotnum].pc && *desc->opptr.l == mips3.hotspot[hotnum].opcode) |
| 1569 | { |
| 1570 | compiler->cycles += mips3.hotspot[hotnum].cycles; |
| 1571 | break; |
| 1572 | } |
| 1573 | |
| 1574 | /* update the icount map variable */ |
| 1575 | UML_MAPVAR(block, MAPVAR_CYCLES, compiler->cycles); // mapvar CYCLES,compiler->cycles |
| 1576 | |
| 1577 | /* if we want a probe, add it here */ |
| 1578 | if (desc->pc == PROBE_ADDRESS) |
| 1579 | UML_CALLC(block, cfunc_printf_probe, desc->pc); // callc cfunc_printf_probe,desc->pc |
| 1580 | |
| 1581 | /* if we are debugging, call the debugger */ |
| 1582 | if (Machine->debug_mode) |
| 1583 | { |
| 1584 | UML_MOV(block, MEM(&mips3.core->pc), IMM(desc->pc)); // mov [pc],desc->pc |
| 1585 | UML_DEBUG(block, IMM(desc->pc)); // debug desc->pc |
| 1586 | } |
| 1587 | |
| 1588 | /* validate our TLB entry at this PC; if we fail, we need to recompile */ |
| 1589 | if ((desc->flags & OPFLAG_VALIDATE_TLB) && (desc->pc < 0x80000000 || desc->pc >= 0xc0000000)) |
| 1590 | { |
| 1591 | UML_CMP(block, MEM(&mips3.core->tlb_table[desc->pc >> 12]), |
| 1592 | IMM(mips3.core->tlb_table[desc->pc >> 12])); // cmp [tlbentry],*tlbentry |
| 1593 | UML_EXHc(block, IF_NE, mips3.tlb_mismatch, IMM(0)); // exh tlb_mismatch,0,NE |
| 1594 | } |
| 1595 | |
| 1596 | /* if this is an invalid opcode, generate the exception now */ |
| 1597 | if (desc->flags & OPFLAG_INVALID_OPCODE) |
| 1598 | UML_EXH(block, mips3.exception[EXCEPTION_INVALIDOP], IMM(0)); // exh invalidop,0 |
| 1599 | |
| 1600 | /* otherwise, it's a regular instruction */ |
| 1601 | else |
| 1602 | { |
| 1603 | /* compile the instruction */ |
| 1604 | if (!generate_opcode(block, compiler, desc)) |
| 1605 | fatalerror("Unimplemented op %08X (%02X,%02X)", *desc->opptr.l, *desc->opptr.l >> 26, *desc->opptr.l & 0x3f); |
| 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | |
| 1610 | /*------------------------------------------------------------------ |
| 1611 | generate_delay_slot_and_branch |
| 1612 | ------------------------------------------------------------------*/ |
| 1613 | |
| 1614 | static void generate_delay_slot_and_branch(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 linkreg) |
| 1615 | { |
| 1616 | compiler_state compiler_temp = *compiler; |
| 1617 | UINT32 op = *desc->opptr.l; |
| 1618 | |
| 1619 | /* set the link if needed */ |
| 1620 | if (linkreg != 0) |
| 1621 | UML_DSEXT4(block, R64(linkreg), IMM((INT32)(desc->pc + 8))); // dsext4 <linkreg>,desc->pc + 8 |
| 1622 | |
| 1623 | /* compile the delay slot using temporary compiler state */ |
| 1624 | assert(desc->delay != NULL); |
| 1625 | generate_sequence_instruction(block, &compiler_temp, desc->delay); // <next instruction> |
| 1626 | |
| 1627 | /* update the cycles and jump through the hash table to the target */ |
| 1628 | if (desc->targetpc != BRANCH_TARGET_DYNAMIC) |
| 1629 | { |
| 1630 | generate_update_cycles(block, &compiler_temp, desc->targetpc, NULL, TRUE); // <subtract cycles> |
| 1631 | if (desc->flags & OPFLAG_INTRABLOCK_BRANCH) |
| 1632 | UML_JMP(block, desc->targetpc | 0x80000000); // jmp desc->targetpc | 0x80000000 |
| 1633 | else |
| 1634 | UML_HASHJMP(block, IMM(mips3.cstate->mode), IMM(desc->targetpc), mips3.nocode); |
| 1635 | // hashjmp <mode>,desc->targetpc,nocode |
| 1636 | } |
| 1637 | else |
| 1638 | { |
| 1639 | generate_update_cycles(block, &compiler_temp, 0, LOPTR(&mips3.core->r[RSREG]), TRUE); |
| 1640 | // <subtract cycles> |
| 1641 | UML_HASHJMP(block, IMM(mips3.cstate->mode), R32(RSREG), mips3.nocode); // hashjmp <mode>,<rsreg>,nocode |
| 1642 | } |
| 1643 | |
| 1644 | /* update the label */ |
| 1645 | compiler->labelnum = compiler_temp.labelnum; |
| 1646 | |
| 1647 | /* reset the mapvar to the current cycles */ |
| 1648 | UML_MAPVAR(block, MAPVAR_CYCLES, compiler->cycles); // mapvar CYCLES,compiler->cycles |
| 1649 | } |
| 1650 | |
| 1651 | |
| 1652 | /*------------------------------------------------- |
| 1653 | generate_opcode - generate code for a specific |
| 1654 | opcode |
| 1655 | -------------------------------------------------*/ |
| 1656 | |
| 1657 | static int generate_opcode(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 1658 | { |
| 1659 | int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0); |
| 1660 | UINT32 op = *desc->opptr.l; |
| 1661 | UINT8 opswitch = op >> 26; |
| 1662 | drcuml_codelabel skip; |
| 1663 | |
| 1664 | switch (opswitch) |
| 1665 | { |
| 1666 | /* ----- sub-groups ----- */ |
| 1667 | |
| 1668 | case 0x00: /* SPECIAL - MIPS I */ |
| 1669 | return generate_special(block, compiler, desc); |
| 1670 | |
| 1671 | case 0x01: /* REGIMM - MIPS I */ |
| 1672 | return generate_regimm(block, compiler, desc); |
| 1673 | |
| 1674 | case 0x1c: /* IDT-specific */ |
| 1675 | return generate_idt(block, compiler, desc); |
| 1676 | |
| 1677 | |
| 1678 | /* ----- jumps and branches ----- */ |
| 1679 | |
| 1680 | case 0x02: /* J - MIPS I */ |
| 1681 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1682 | return TRUE; |
| 1683 | |
| 1684 | case 0x03: /* JAL - MIPS I */ |
| 1685 | generate_delay_slot_and_branch(block, compiler, desc, 31); // <next instruction + hashjmp> |
| 1686 | return TRUE; |
| 1687 | |
| 1688 | case 0x04: /* BEQ - MIPS I */ |
| 1689 | case 0x14: /* BEQL - MIPS II */ |
| 1690 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 1691 | UML_JMPc(block, IF_NE, skip = compiler->labelnum++); // jmp skip,NE |
| 1692 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1693 | UML_LABEL(block, skip); // skip: |
| 1694 | return TRUE; |
| 1695 | |
| 1696 | case 0x05: /* BNE - MIPS I */ |
| 1697 | case 0x15: /* BNEL - MIPS II */ |
| 1698 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 1699 | UML_JMPc(block, IF_E, skip = compiler->labelnum++); // jmp skip,E |
| 1700 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1701 | UML_LABEL(block, skip); // skip: |
| 1702 | return TRUE; |
| 1703 | |
| 1704 | case 0x06: /* BLEZ - MIPS I */ |
| 1705 | case 0x16: /* BLEZL - MIPS II */ |
| 1706 | if (RSREG != 0) |
| 1707 | { |
| 1708 | UML_DCMP(block, R64(RSREG), IMM(0)); // dcmp <rsreg>,0 |
| 1709 | UML_JMPc(block, IF_G, skip = compiler->labelnum++); // jmp skip,G |
| 1710 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1711 | UML_LABEL(block, skip); // skip: |
| 1712 | } |
| 1713 | else |
| 1714 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1715 | return TRUE; |
| 1716 | |
| 1717 | case 0x07: /* BGTZ - MIPS I */ |
| 1718 | case 0x17: /* BGTZL - MIPS II */ |
| 1719 | UML_DCMP(block, R64(RSREG), IMM(0)); // dcmp <rsreg>,0 |
| 1720 | UML_JMPc(block, IF_LE, skip = compiler->labelnum++); // jmp skip,LE |
| 1721 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 1722 | UML_LABEL(block, skip); // skip: |
| 1723 | return TRUE; |
| 1724 | |
| 1725 | |
| 1726 | /* ----- immediate arithmetic ----- */ |
| 1727 | |
| 1728 | case 0x0f: /* LUI - MIPS I */ |
| 1729 | if (RTREG != 0) |
| 1730 | UML_DSEXT4(block, R64(RTREG), IMM(SIMMVAL << 16)); // dsext4 <rtreg>,SIMMVAL << 16 |
| 1731 | return TRUE; |
| 1732 | |
| 1733 | case 0x08: /* ADDI - MIPS I */ |
| 1734 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 1735 | { |
| 1736 | UML_ADDf(block, IREG(0), R32(RSREG), IMM(SIMMVAL), FLAGS_V); // add i0,<rsreg>,SIMMVAL,V |
| 1737 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0 |
| 1738 | } |
| 1739 | else |
| 1740 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1741 | if (RTREG != 0) |
| 1742 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 1743 | return TRUE; |
| 1744 | |
| 1745 | case 0x09: /* ADDIU - MIPS I */ |
| 1746 | if (RTREG != 0) |
| 1747 | { |
| 1748 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL,V |
| 1749 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 1750 | } |
| 1751 | return TRUE; |
| 1752 | |
| 1753 | case 0x18: /* DADDI - MIPS III */ |
| 1754 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 1755 | { |
| 1756 | UML_DADDf(block, IREG(0), R64(RSREG), IMM(SIMMVAL), FLAGS_V); // dadd i0,<rsreg>,SIMMVAL,V |
| 1757 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0 |
| 1758 | } |
| 1759 | else |
| 1760 | UML_DADD(block, IREG(0), R64(RSREG), IMM(SIMMVAL)); // dadd i0,<rsreg>,SIMMVAL |
| 1761 | if (RTREG != 0) |
| 1762 | UML_DMOV(block, R64(RTREG), IREG(0)); // dmov <rtreg>,i0 |
| 1763 | return TRUE; |
| 1764 | |
| 1765 | case 0x19: /* DADDIU - MIPS III */ |
| 1766 | if (RTREG != 0) |
| 1767 | UML_DADD(block, R64(RTREG), R64(RSREG), IMM(SIMMVAL)); // dadd <rtreg>,<rsreg>,SIMMVAL |
| 1768 | return TRUE; |
| 1769 | |
| 1770 | case 0x0c: /* ANDI - MIPS I */ |
| 1771 | if (RTREG != 0) |
| 1772 | UML_DAND(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL)); // dand <rtreg>,<rsreg>,UIMMVAL |
| 1773 | return TRUE; |
| 1774 | |
| 1775 | case 0x0d: /* ORI - MIPS I */ |
| 1776 | if (RTREG != 0) |
| 1777 | UML_DOR(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL)); // dor <rtreg>,<rsreg>,UIMMVAL |
| 1778 | return TRUE; |
| 1779 | |
| 1780 | case 0x0e: /* XORI - MIPS I */ |
| 1781 | if (RTREG != 0) |
| 1782 | UML_DXOR(block, R64(RTREG), R64(RSREG), IMM(UIMMVAL)); // dxor <rtreg>,<rsreg>,UIMMVAL |
| 1783 | return TRUE; |
| 1784 | |
| 1785 | case 0x0a: /* SLTI - MIPS I */ |
| 1786 | if (RTREG != 0) |
| 1787 | { |
| 1788 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 1789 | UML_DFLAGS(block, R64(RTREG), (UINT64)~0, mips3.cstate->slt_table); // dflags <rtreg>,~0,slt_table |
| 1790 | } |
| 1791 | return TRUE; |
| 1792 | |
| 1793 | case 0x0b: /* SLTIU - MIPS I */ |
| 1794 | if (RTREG != 0) |
| 1795 | { |
| 1796 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 1797 | UML_DFLAGS(block, R64(RTREG), (UINT64)~0, mips3.cstate->sltu_table);// dflags <rtreg>,~0,sltu_table |
| 1798 | } |
| 1799 | return TRUE; |
| 1800 | |
| 1801 | |
| 1802 | /* ----- memory load operations ----- */ |
| 1803 | |
| 1804 | case 0x20: /* LB - MIPS I */ |
| 1805 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1806 | UML_CALLH(block, mips3.read8); // callh read8 |
| 1807 | if (RTREG != 0) |
| 1808 | UML_DSEXT1(block, R64(RTREG), IREG(0)); // dsext1 <rtreg>,i0 |
| 1809 | if (!in_delay_slot) |
| 1810 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1811 | return TRUE; |
| 1812 | |
| 1813 | case 0x21: /* LH - MIPS I */ |
| 1814 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1815 | UML_CALLH(block, mips3.read16); // callh read16 |
| 1816 | if (RTREG != 0) |
| 1817 | UML_DSEXT2(block, R64(RTREG), IREG(0)); // dsext2 <rtreg>,i0 |
| 1818 | if (!in_delay_slot) |
| 1819 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1820 | return TRUE; |
| 1821 | |
| 1822 | case 0x23: /* LW - MIPS I */ |
| 1823 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1824 | UML_CALLH(block, mips3.read32); // callh read32 |
| 1825 | if (RTREG != 0) |
| 1826 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 1827 | if (!in_delay_slot) |
| 1828 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1829 | return TRUE; |
| 1830 | |
| 1831 | case 0x24: /* LBU - MIPS I */ |
| 1832 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1833 | UML_CALLH(block, mips3.read8); // callh read8 |
| 1834 | if (RTREG != 0) |
| 1835 | UML_DZEXT1(block, R64(RTREG), IREG(0)); // dzext1 <rtreg>,i0 |
| 1836 | if (!in_delay_slot) |
| 1837 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1838 | return TRUE; |
| 1839 | |
| 1840 | case 0x25: /* LHU - MIPS I */ |
| 1841 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1842 | UML_CALLH(block, mips3.read16); // callh read16 |
| 1843 | if (RTREG != 0) |
| 1844 | UML_DZEXT2(block, R64(RTREG), IREG(0)); // dzext2 <rtreg>,i0 |
| 1845 | if (!in_delay_slot) |
| 1846 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1847 | return TRUE; |
| 1848 | |
| 1849 | case 0x27: /* LWU - MIPS III */ |
| 1850 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1851 | UML_CALLH(block, mips3.read32); // callh read32 |
| 1852 | if (RTREG != 0) |
| 1853 | UML_DZEXT4(block, R64(RTREG), IREG(0)); // dzext4 <rtreg>,i0 |
| 1854 | if (!in_delay_slot) |
| 1855 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1856 | return TRUE; |
| 1857 | |
| 1858 | case 0x37: /* LD - MIPS III */ |
| 1859 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1860 | UML_CALLH(block, mips3.read64); // callh read64 |
| 1861 | if (RTREG != 0) |
| 1862 | UML_DMOV(block, R64(RTREG), IREG(0)); // dmov <rtreg>,i0 |
| 1863 | if (!in_delay_slot) |
| 1864 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1865 | return TRUE; |
| 1866 | |
| 1867 | case 0x22: /* LWL - MIPS I */ |
| 1868 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1869 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 1870 | if (!mips3.core->bigendian) |
| 1871 | UML_XOR(block, IREG(4), IREG(4), IMM(0x18)); // xor i4,i4,0x18 |
| 1872 | UML_SHR(block, IREG(2), IMM(~0), IREG(4)); // shr i2,~0,i4 |
| 1873 | UML_CALLH(block, mips3.read32mask); // callh read32mask |
| 1874 | if (RTREG != 0) |
| 1875 | { |
| 1876 | UML_SHL(block, IREG(2), IMM(~0), IREG(4)); // shl i2,~0,i4 |
| 1877 | UML_XOR(block, IREG(2), IREG(2), IMM(~0)); // xor i2,i2,~0 |
| 1878 | UML_SHL(block, IREG(0), IREG(0), IREG(4)); // shl i0,i0,i4 |
| 1879 | UML_AND(block, IREG(2), IREG(2), R32(RTREG)); // and i2,i2,<rtreg> |
| 1880 | UML_OR(block, IREG(0), IREG(0), IREG(2)); // or i0,i0,i2 |
| 1881 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 1882 | } |
| 1883 | if (!in_delay_slot) |
| 1884 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1885 | return TRUE; |
| 1886 | |
| 1887 | case 0x26: /* LWR - MIPS I */ |
| 1888 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1889 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 1890 | if (mips3.core->bigendian) |
| 1891 | UML_XOR(block, IREG(4), IREG(4), IMM(0x18)); // xor i4,i4,0x18 |
| 1892 | UML_SHL(block, IREG(2), IMM(~0), IREG(4)); // shl i2,~0,i4 |
| 1893 | UML_CALLH(block, mips3.read32mask); // callh read32mask |
| 1894 | if (RTREG != 0) |
| 1895 | { |
| 1896 | UML_SHR(block, IREG(2), IMM(~0), IREG(4)); // shr i2,~0,i4 |
| 1897 | UML_XOR(block, IREG(2), IREG(2), IMM(~0)); // xor i2,i2,~0 |
| 1898 | UML_SHR(block, IREG(0), IREG(0), IREG(4)); // shr i0,i0,i4 |
| 1899 | UML_AND(block, IREG(2), IREG(2), R32(RTREG)); // and i2,i2,<rtreg> |
| 1900 | UML_OR(block, IREG(0), IREG(0), IREG(2)); // or i0,i0,i2 |
| 1901 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 1902 | } |
| 1903 | if (!in_delay_slot) |
| 1904 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1905 | return TRUE; |
| 1906 | |
| 1907 | case 0x1a: /* LDL - MIPS III */ |
| 1908 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1909 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 1910 | if (!mips3.core->bigendian) |
| 1911 | UML_XOR(block, IREG(4), IREG(4), IMM(0x38)); // xor i4,i4,0x38 |
| 1912 | UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshr i2,~0,i4 |
| 1913 | UML_CALLH(block, mips3.read64mask); // callh read64mask |
| 1914 | if (RTREG != 0) |
| 1915 | { |
| 1916 | UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshl i2,~0,i4 |
| 1917 | UML_DXOR(block, IREG(2), IREG(2), IMM((UINT64)~0)); // dxor i2,i2,~0 |
| 1918 | UML_DSHL(block, IREG(0), IREG(0), IREG(4)); // dshl i0,i0,i4 |
| 1919 | UML_DAND(block, IREG(2), IREG(2), R64(RTREG)); // dand i2,i2,<rtreg> |
| 1920 | UML_DOR(block, R64(RTREG), IREG(0), IREG(2)); // dor <rtreg>,i0,i2 |
| 1921 | } |
| 1922 | if (!in_delay_slot) |
| 1923 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1924 | return TRUE; |
| 1925 | |
| 1926 | case 0x1b: /* LDR - MIPS III */ |
| 1927 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1928 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 1929 | if (mips3.core->bigendian) |
| 1930 | UML_XOR(block, IREG(4), IREG(4), IMM(0x38)); // xor i4,i4,0x38 |
| 1931 | UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshl i2,~0,i4 |
| 1932 | UML_CALLH(block, mips3.read64mask); // callh read64mask |
| 1933 | if (RTREG != 0) |
| 1934 | { |
| 1935 | UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshr i2,~0,i4 |
| 1936 | UML_DXOR(block, IREG(2), IREG(2), IMM((UINT64)~0)); // dxor i2,i2,~0 |
| 1937 | UML_DSHR(block, IREG(0), IREG(0), IREG(4)); // dshr i0,i0,i4 |
| 1938 | UML_DAND(block, IREG(2), IREG(2), R64(RTREG)); // dand i2,i2,<rtreg> |
| 1939 | UML_DOR(block, R64(RTREG), IREG(0), IREG(2)); // dor <rtreg>,i0,i2 |
| 1940 | } |
| 1941 | if (!in_delay_slot) |
| 1942 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1943 | return TRUE; |
| 1944 | |
| 1945 | case 0x31: /* LWC1 - MIPS I */ |
| 1946 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1947 | UML_CALLH(block, mips3.read32); // callh read32 |
| 1948 | UML_MOV(block, FPR32(RTREG), IREG(0)); // mov <cpr1_rt>,i0 |
| 1949 | if (!in_delay_slot) |
| 1950 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1951 | return TRUE; |
| 1952 | |
| 1953 | case 0x35: /* LDC1 - MIPS III */ |
| 1954 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1955 | UML_CALLH(block, mips3.read64); // callh read64 |
| 1956 | UML_DMOV(block, FPR64(RTREG), IREG(0)); // dmov <cpr1_rt>,i0 |
| 1957 | if (!in_delay_slot) |
| 1958 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1959 | return TRUE; |
| 1960 | |
| 1961 | case 0x32: /* LWC2 - MIPS I */ |
| 1962 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1963 | UML_CALLH(block, mips3.read32); // callh read32 |
| 1964 | UML_DZEXT4(block, CPR264(RTREG), IREG(0)); // dzext4 <cpr2_rt>,i0 |
| 1965 | if (!in_delay_slot) |
| 1966 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1967 | return TRUE; |
| 1968 | |
| 1969 | case 0x36: /* LDC2 - MIPS II */ |
| 1970 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1971 | UML_CALLH(block, mips3.read64); // callh read64 |
| 1972 | UML_DMOV(block, CPR264(RTREG), IREG(0)); // dmov <cpr2_rt>,i0 |
| 1973 | if (!in_delay_slot) |
| 1974 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1975 | return TRUE; |
| 1976 | |
| 1977 | |
| 1978 | /* ----- memory store operations ----- */ |
| 1979 | |
| 1980 | case 0x28: /* SB - MIPS I */ |
| 1981 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1982 | UML_MOV(block, IREG(1), R32(RTREG)); // mov i1,<rtreg> |
| 1983 | UML_CALLH(block, mips3.write8); // callh write8 |
| 1984 | if (!in_delay_slot) |
| 1985 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1986 | return TRUE; |
| 1987 | |
| 1988 | case 0x29: /* SH - MIPS I */ |
| 1989 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1990 | UML_MOV(block, IREG(1), R32(RTREG)); // mov i1,<rtreg> |
| 1991 | UML_CALLH(block, mips3.write16); // callh write16 |
| 1992 | if (!in_delay_slot) |
| 1993 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 1994 | return TRUE; |
| 1995 | |
| 1996 | case 0x2b: /* SW - MIPS I */ |
| 1997 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 1998 | UML_MOV(block, IREG(1), R32(RTREG)); // mov i1,<rtreg> |
| 1999 | UML_CALLH(block, mips3.write32); // callh write32 |
| 2000 | if (!in_delay_slot) |
| 2001 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2002 | return TRUE; |
| 2003 | |
| 2004 | case 0x3f: /* SD - MIPS III */ |
| 2005 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2006 | UML_DMOV(block, IREG(1), R64(RTREG)); // dmov i1,<rtreg> |
| 2007 | UML_CALLH(block, mips3.write64); // callh write64 |
| 2008 | if (!in_delay_slot) |
| 2009 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2010 | return TRUE; |
| 2011 | |
| 2012 | case 0x2a: /* SWL - MIPS I */ |
| 2013 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2014 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 2015 | UML_MOV(block, IREG(1), R32(RTREG)); // mov i1,<rtreg> |
| 2016 | if (!mips3.core->bigendian) |
| 2017 | UML_XOR(block, IREG(4), IREG(4), IMM(0x18)); // xor i4,i4,0x18 |
| 2018 | UML_SHR(block, IREG(2), IMM(~0), IREG(4)); // shr i2,~0,i4 |
| 2019 | UML_SHR(block, IREG(1), IREG(1), IREG(4)); // shr i1,i1,i4 |
| 2020 | UML_CALLH(block, mips3.write32mask); // callh write32mask |
| 2021 | if (!in_delay_slot) |
| 2022 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2023 | return TRUE; |
| 2024 | |
| 2025 | case 0x2e: /* SWR - MIPS I */ |
| 2026 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2027 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 2028 | UML_MOV(block, IREG(1), R32(RTREG)); // mov i1,<rtreg> |
| 2029 | if (mips3.core->bigendian) |
| 2030 | UML_XOR(block, IREG(4), IREG(4), IMM(0x18)); // xor i4,i4,0x18 |
| 2031 | UML_SHL(block, IREG(2), IMM(~0), IREG(4)); // shl i2,~0,i4 |
| 2032 | UML_SHL(block, IREG(1), IREG(1), IREG(4)); // shl i1,i1,i4 |
| 2033 | UML_CALLH(block, mips3.write32mask); // callh write32mask |
| 2034 | if (!in_delay_slot) |
| 2035 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2036 | return TRUE; |
| 2037 | |
| 2038 | case 0x2c: /* SDL - MIPS III */ |
| 2039 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2040 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 2041 | UML_DMOV(block, IREG(1), R64(RTREG)); // dmov i1,<rtreg> |
| 2042 | if (!mips3.core->bigendian) |
| 2043 | UML_XOR(block, IREG(4), IREG(4), IMM(0x38)); // xor i4,i4,0x38 |
| 2044 | UML_DSHR(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshr i2,~0,i4 |
| 2045 | UML_DSHR(block, IREG(1), IREG(1), IREG(4)); // dshr i1,i1,i4 |
| 2046 | UML_CALLH(block, mips3.write64mask); // callh write64mask |
| 2047 | if (!in_delay_slot) |
| 2048 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2049 | return TRUE; |
| 2050 | |
| 2051 | case 0x2d: /* SDR - MIPS III */ |
| 2052 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2053 | UML_SHL(block, IREG(4), IREG(0), IMM(3)); // shl i4,i0,3 |
| 2054 | UML_DMOV(block, IREG(1), R64(RTREG)); // dmov i1,<rtreg> |
| 2055 | if (mips3.core->bigendian) |
| 2056 | UML_XOR(block, IREG(4), IREG(4), IMM(0x38)); // xor i4,i4,0x38 |
| 2057 | UML_DSHL(block, IREG(2), IMM((UINT64)~0), IREG(4)); // dshl i2,~0,i4 |
| 2058 | UML_DSHL(block, IREG(1), IREG(1), IREG(4)); // dshl i1,i1,i4 |
| 2059 | UML_CALLH(block, mips3.write64mask); // callh write64mask |
| 2060 | if (!in_delay_slot) |
| 2061 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2062 | return TRUE; |
| 2063 | |
| 2064 | case 0x39: /* SWC1 - MIPS I */ |
| 2065 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2066 | UML_MOV(block, IREG(1), FPR32(RTREG)); // mov i1,<cpr1_rt> |
| 2067 | UML_CALLH(block, mips3.write32); // callh write32 |
| 2068 | if (!in_delay_slot) |
| 2069 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2070 | return TRUE; |
| 2071 | |
| 2072 | case 0x3d: /* SDC1 - MIPS III */ |
| 2073 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2074 | UML_DMOV(block, IREG(1), FPR64(RTREG)); // dmov i1,<cpr1_rt> |
| 2075 | UML_CALLH(block, mips3.write64); // callh write64 |
| 2076 | if (!in_delay_slot) |
| 2077 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2078 | return TRUE; |
| 2079 | |
| 2080 | case 0x3a: /* SWC2 - MIPS I */ |
| 2081 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2082 | UML_MOV(block, IREG(1), CPR232(RTREG)); // mov i1,<cpr2_rt> |
| 2083 | UML_CALLH(block, mips3.write32); // callh write32 |
| 2084 | if (!in_delay_slot) |
| 2085 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2086 | return TRUE; |
| 2087 | |
| 2088 | case 0x3e: /* SDC2 - MIPS II */ |
| 2089 | UML_ADD(block, IREG(0), R32(RSREG), IMM(SIMMVAL)); // add i0,<rsreg>,SIMMVAL |
| 2090 | UML_DMOV(block, IREG(1), CPR264(RTREG)); // dmov i1,<cpr2_rt> |
| 2091 | UML_CALLH(block, mips3.write64); // callh write64 |
| 2092 | if (!in_delay_slot) |
| 2093 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 2094 | return TRUE; |
| 2095 | |
| 2096 | |
| 2097 | /* ----- effective no-ops ----- */ |
| 2098 | |
| 2099 | case 0x2f: /* CACHE - MIPS II */ |
| 2100 | case 0x33: /* PREF - MIPS IV */ |
| 2101 | return TRUE; |
| 2102 | |
| 2103 | |
| 2104 | /* ----- coprocessor instructions ----- */ |
| 2105 | |
| 2106 | case 0x10: /* COP0 - MIPS I */ |
| 2107 | return generate_cop0(block, compiler, desc); |
| 2108 | |
| 2109 | case 0x11: /* COP1 - MIPS I */ |
| 2110 | return generate_cop1(block, compiler, desc); |
| 2111 | |
| 2112 | case 0x13: /* COP1X - MIPS IV */ |
| 2113 | return generate_cop1x(block, compiler, desc); |
| 2114 | |
| 2115 | case 0x12: /* COP2 - MIPS I */ |
| 2116 | UML_EXH(block, mips3.exception[EXCEPTION_INVALIDOP], IMM(0)); // exh invalidop,0 |
| 2117 | return TRUE; |
| 2118 | |
| 2119 | |
| 2120 | /* ----- unimplemented/illegal instructions ----- */ |
| 2121 | |
| 2122 | // case 0x30: /* LL - MIPS II */ logerror("mips3 Unhandled op: LL\n"); break; |
| 2123 | // case 0x34: /* LLD - MIPS III */ logerror("mips3 Unhandled op: LLD\n"); break; |
| 2124 | // case 0x38: /* SC - MIPS II */ logerror("mips3 Unhandled op: SC\n"); break; |
| 2125 | // case 0x3c: /* SCD - MIPS III */ logerror("mips3 Unhandled op: SCD\n"); break; |
| 2126 | // default: /* ??? */ invalid_instruction(op); break; |
| 2127 | } |
| 2128 | |
| 2129 | return FALSE; |
| 2130 | } |
| 2131 | |
| 2132 | |
| 2133 | /*------------------------------------------------- |
| 2134 | generate_special - compile opcodes in the |
| 2135 | 'SPECIAL' group |
| 2136 | -------------------------------------------------*/ |
| 2137 | |
| 2138 | static int generate_special(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 2139 | { |
| 2140 | UINT32 op = *desc->opptr.l; |
| 2141 | UINT8 opswitch = op & 63; |
| 2142 | |
| 2143 | switch (opswitch) |
| 2144 | { |
| 2145 | /* ----- shift instructions ----- */ |
| 2146 | |
| 2147 | case 0x00: /* SLL - MIPS I */ |
| 2148 | if (RDREG != 0) |
| 2149 | { |
| 2150 | UML_SHL(block, IREG(0), R32(RTREG), IMM(SHIFT)); // shl i0,<rtreg>,<shift> |
| 2151 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2152 | } |
| 2153 | return TRUE; |
| 2154 | |
| 2155 | case 0x02: /* SRL - MIPS I */ |
| 2156 | if (RDREG != 0) |
| 2157 | { |
| 2158 | UML_SHR(block, IREG(0), R32(RTREG), IMM(SHIFT)); // shr i0,<rtreg>,<shift> |
| 2159 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2160 | } |
| 2161 | return TRUE; |
| 2162 | |
| 2163 | case 0x03: /* SRA - MIPS I */ |
| 2164 | if (RDREG != 0) |
| 2165 | { |
| 2166 | UML_SAR(block, IREG(0), R32(RTREG), IMM(SHIFT)); // sar i0,<rtreg>,<shift> |
| 2167 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2168 | } |
| 2169 | return TRUE; |
| 2170 | |
| 2171 | case 0x04: /* SLLV - MIPS I */ |
| 2172 | if (RDREG != 0) |
| 2173 | { |
| 2174 | UML_SHL(block, IREG(0), R32(RTREG), R32(RSREG)); // shl i0,<rtreg>,<rsreg> |
| 2175 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2176 | } |
| 2177 | return TRUE; |
| 2178 | |
| 2179 | case 0x06: /* SRLV - MIPS I */ |
| 2180 | if (RDREG != 0) |
| 2181 | { |
| 2182 | UML_SHR(block, IREG(0), R32(RTREG), R32(RSREG)); // shr i0,<rtreg>,<rsreg> |
| 2183 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2184 | } |
| 2185 | return TRUE; |
| 2186 | |
| 2187 | case 0x07: /* SRAV - MIPS I */ |
| 2188 | if (RDREG != 0) |
| 2189 | { |
| 2190 | UML_SAR(block, IREG(0), R32(RTREG), R32(RSREG)); // sar i0,<rtreg>,<rsreg> |
| 2191 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2192 | } |
| 2193 | return TRUE; |
| 2194 | |
| 2195 | case 0x38: /* DSLL - MIPS III */ |
| 2196 | if (RDREG != 0) |
| 2197 | UML_DSHL(block, R64(RDREG), R64(RTREG), IMM(SHIFT)); // dshl <rdreg>,<rtreg>,<shift> |
| 2198 | return TRUE; |
| 2199 | |
| 2200 | case 0x3a: /* DSRL - MIPS III */ |
| 2201 | if (RDREG != 0) |
| 2202 | UML_DSHR(block, R64(RDREG), R64(RTREG), IMM(SHIFT)); // dshr <rdreg>,<rtreg>,<shift> |
| 2203 | return TRUE; |
| 2204 | |
| 2205 | case 0x3b: /* DSRA - MIPS III */ |
| 2206 | if (RDREG != 0) |
| 2207 | UML_DSAR(block, R64(RDREG), R64(RTREG), IMM(SHIFT)); // dsar <rdreg>,<rtreg>,<shift> |
| 2208 | return TRUE; |
| 2209 | |
| 2210 | case 0x3c: /* DSLL32 - MIPS III */ |
| 2211 | if (RDREG != 0) |
| 2212 | UML_DSHL(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32)); // dshl <rdreg>,<rtreg>,<shift>+32 |
| 2213 | return TRUE; |
| 2214 | |
| 2215 | case 0x3e: /* DSRL32 - MIPS III */ |
| 2216 | if (RDREG != 0) |
| 2217 | UML_DSHR(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32)); // dshr <rdreg>,<rtreg>,<shift>+32 |
| 2218 | return TRUE; |
| 2219 | |
| 2220 | case 0x3f: /* DSRA32 - MIPS III */ |
| 2221 | if (RDREG != 0) |
| 2222 | UML_DSAR(block, R64(RDREG), R64(RTREG), IMM(SHIFT + 32)); // dsar <rdreg>,<rtreg>,<shift>+32 |
| 2223 | return TRUE; |
| 2224 | |
| 2225 | case 0x14: /* DSLLV - MIPS III */ |
| 2226 | if (RDREG != 0) |
| 2227 | UML_DSHL(block, R64(RDREG), R64(RTREG), R64(RSREG)); // dshl <rdreg>,<rtreg>,<rsreg> |
| 2228 | return TRUE; |
| 2229 | |
| 2230 | case 0x16: /* DSRLV - MIPS III */ |
| 2231 | if (RDREG != 0) |
| 2232 | UML_DSHR(block, R64(RDREG), R64(RTREG), R64(RSREG)); // dshr <rdreg>,<rtreg>,<rsreg> |
| 2233 | return TRUE; |
| 2234 | |
| 2235 | case 0x17: /* DSRAV - MIPS III */ |
| 2236 | if (RDREG != 0) |
| 2237 | UML_DSAR(block, R64(RDREG), R64(RTREG), R64(RSREG)); // dsar <rdreg>,<rtreg>,<rsreg> |
| 2238 | return TRUE; |
| 2239 | |
| 2240 | |
| 2241 | /* ----- basic arithmetic ----- */ |
| 2242 | |
| 2243 | case 0x20: /* ADD - MIPS I */ |
| 2244 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 2245 | { |
| 2246 | UML_ADDf(block, IREG(0), R32(RSREG), R32(RTREG), FLAGS_V); // add i0,<rsreg>,<rtreg>,V |
| 2247 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0,V |
| 2248 | if (RDREG != 0) |
| 2249 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2250 | } |
| 2251 | else if (RDREG != 0) |
| 2252 | { |
| 2253 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 2254 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2255 | } |
| 2256 | return TRUE; |
| 2257 | |
| 2258 | case 0x21: /* ADDU - MIPS I */ |
| 2259 | if (RDREG != 0) |
| 2260 | { |
| 2261 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 2262 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2263 | } |
| 2264 | return TRUE; |
| 2265 | |
| 2266 | case 0x2c: /* DADD - MIPS III */ |
| 2267 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 2268 | { |
| 2269 | UML_DADDf(block, IREG(0), R64(RSREG), R64(RTREG), FLAGS_V); // dadd i0,<rsreg>,<rtreg>,V |
| 2270 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0,V |
| 2271 | if (RDREG != 0) |
| 2272 | UML_DMOV(block, R64(RDREG), IREG(0)); // dmov <rdreg>,i0 |
| 2273 | } |
| 2274 | else if (RDREG != 0) |
| 2275 | UML_DADD(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dadd <rdreg>,<rsreg>,<rtreg> |
| 2276 | return TRUE; |
| 2277 | |
| 2278 | case 0x2d: /* DADDU - MIPS III */ |
| 2279 | if (RDREG != 0) |
| 2280 | UML_DADD(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dadd <rdreg>,<rsreg>,<rtreg> |
| 2281 | return TRUE; |
| 2282 | |
| 2283 | case 0x22: /* SUB - MIPS I */ |
| 2284 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 2285 | { |
| 2286 | UML_SUBf(block, IREG(0), R32(RSREG), R32(RTREG), FLAGS_V); // sub i0,<rsreg>,<rtreg>,V |
| 2287 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0,V |
| 2288 | if (RDREG != 0) |
| 2289 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2290 | } |
| 2291 | else if (RDREG != 0) |
| 2292 | { |
| 2293 | UML_SUB(block, IREG(0), R32(RSREG), R32(RTREG)); // sub i0,<rsreg>,<rtreg> |
| 2294 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2295 | } |
| 2296 | return TRUE; |
| 2297 | |
| 2298 | case 0x23: /* SUBU - MIPS I */ |
| 2299 | if (RDREG != 0) |
| 2300 | { |
| 2301 | UML_SUB(block, IREG(0), R32(RSREG), R32(RTREG)); // sub i0,<rsreg>,<rtreg> |
| 2302 | UML_DSEXT4(block, R64(RDREG), IREG(0)); // dsext4 <rdreg>,i0 |
| 2303 | } |
| 2304 | return TRUE; |
| 2305 | |
| 2306 | case 0x2e: /* DSUB - MIPS III */ |
| 2307 | if (mips3.drcoptions & MIPS3DRC_CHECK_OVERFLOWS) |
| 2308 | { |
| 2309 | UML_DSUBf(block, IREG(0), R64(RSREG), R64(RTREG), FLAGS_V); // dsub i0,<rsreg>,<rtreg>,V |
| 2310 | UML_EXHc(block, IF_V, mips3.exception[EXCEPTION_OVERFLOW], IMM(0)); // exh overflow,0,V |
| 2311 | if (RDREG != 0) |
| 2312 | UML_DMOV(block, R64(RDREG), IREG(0)); // dmov <rdreg>,i0 |
| 2313 | } |
| 2314 | else if (RDREG != 0) |
| 2315 | UML_DSUB(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dsub <rdreg>,<rsreg>,<rtreg> |
| 2316 | return TRUE; |
| 2317 | |
| 2318 | case 0x2f: /* DSUBU - MIPS III */ |
| 2319 | if (RDREG != 0) |
| 2320 | UML_DSUB(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dsub <rdreg>,<rsreg>,<rtreg> |
| 2321 | return TRUE; |
| 2322 | |
| 2323 | case 0x18: /* MULT - MIPS I */ |
| 2324 | UML_MULS(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG)); // muls i0,i1,<rsreg>,<rtreg> |
| 2325 | UML_DSEXT4(block, LO64, IREG(0)); // dsext4 lo,i0 |
| 2326 | UML_DSEXT4(block, HI64, IREG(1)); // dsext4 hi,i1 |
| 2327 | return TRUE; |
| 2328 | |
| 2329 | case 0x19: /* MULTU - MIPS I */ |
| 2330 | UML_MULU(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG)); // mulu i0,i1,<rsreg>,<rtreg> |
| 2331 | UML_DSEXT4(block, LO64, IREG(0)); // dsext4 lo,i0 |
| 2332 | UML_DSEXT4(block, HI64, IREG(1)); // dsext4 hi,i1 |
| 2333 | return TRUE; |
| 2334 | |
| 2335 | case 0x1c: /* DMULT - MIPS III */ |
| 2336 | UML_DMULS(block, LO64, HI64, R64(RSREG), R64(RTREG)); // dmuls lo,hi,<rsreg>,<rtreg> |
| 2337 | return TRUE; |
| 2338 | |
| 2339 | case 0x1d: /* DMULTU - MIPS III */ |
| 2340 | UML_DMULU(block, LO64, HI64, R64(RSREG), R64(RTREG)); // dmulu lo,hi,<rsreg>,<rtreg> |
| 2341 | return TRUE; |
| 2342 | |
| 2343 | case 0x1a: /* DIV - MIPS I */ |
| 2344 | UML_DIVS(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG)); // divs i0,i1,<rsreg>,<rtreg> |
| 2345 | UML_DSEXT4(block, LO64, IREG(0)); // dsext4 lo,i0 |
| 2346 | UML_DSEXT4(block, HI64, IREG(1)); // dsext4 hi,i1 |
| 2347 | return TRUE; |
| 2348 | |
| 2349 | case 0x1b: /* DIVU - MIPS I */ |
| 2350 | UML_DIVU(block, IREG(0), IREG(1), R32(RSREG), R32(RTREG)); // divu i0,i1,<rsreg>,<rtreg> |
| 2351 | UML_DSEXT4(block, LO64, IREG(0)); // dsext4 lo,i0 |
| 2352 | UML_DSEXT4(block, HI64, IREG(1)); // dsext4 hi,i1 |
| 2353 | return TRUE; |
| 2354 | |
| 2355 | case 0x1e: /* DDIV - MIPS III */ |
| 2356 | UML_DDIVS(block, LO64, HI64, R64(RSREG), R64(RTREG)); // ddivs lo,hi,<rsreg>,<rtreg> |
| 2357 | return TRUE; |
| 2358 | |
| 2359 | case 0x1f: /* DDIVU - MIPS III */ |
| 2360 | UML_DDIVU(block, LO64, HI64, R64(RSREG), R64(RTREG)); // ddivu lo,hi,<rsreg>,<rtreg> |
| 2361 | return TRUE; |
| 2362 | |
| 2363 | |
| 2364 | /* ----- basic logical ops ----- */ |
| 2365 | |
| 2366 | case 0x24: /* AND - MIPS I */ |
| 2367 | if (RDREG != 0) |
| 2368 | UML_DAND(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dand <rdreg>,<rsreg>,<rtreg> |
| 2369 | return TRUE; |
| 2370 | |
| 2371 | case 0x25: /* OR - MIPS I */ |
| 2372 | if (RDREG != 0) |
| 2373 | UML_DOR(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dor <rdreg>,<rsreg>,<rtreg> |
| 2374 | return TRUE; |
| 2375 | |
| 2376 | case 0x26: /* XOR - MIPS I */ |
| 2377 | if (RDREG != 0) |
| 2378 | UML_DXOR(block, R64(RDREG), R64(RSREG), R64(RTREG)); // dxor <rdreg>,<rsreg>,<rtreg> |
| 2379 | return TRUE; |
| 2380 | |
| 2381 | case 0x27: /* NOR - MIPS I */ |
| 2382 | if (RDREG != 0) |
| 2383 | { |
| 2384 | UML_DOR(block, IREG(0), R64(RSREG), R64(RTREG)); // dor i0,<rsreg>,<rtreg> |
| 2385 | UML_DXOR(block, R64(RDREG), IREG(0), IMM((UINT64)~0)); // dxor <rdreg>,i0,~0 |
| 2386 | } |
| 2387 | return TRUE; |
| 2388 | |
| 2389 | |
| 2390 | /* ----- basic comparisons ----- */ |
| 2391 | |
| 2392 | case 0x2a: /* SLT - MIPS I */ |
| 2393 | if (RDREG != 0) |
| 2394 | { |
| 2395 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2396 | UML_DFLAGS(block, R64(RDREG), (UINT64)~0, mips3.cstate->slt_table); // dflags <rdreg>,~0,slt_table |
| 2397 | } |
| 2398 | return TRUE; |
| 2399 | |
| 2400 | case 0x2b: /* SLTU - MIPS I */ |
| 2401 | if (RDREG != 0) |
| 2402 | { |
| 2403 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2404 | UML_DFLAGS(block, R64(RDREG), (UINT64)~0, mips3.cstate->sltu_table);// dflags <rdreg>,~0,sltu_table |
| 2405 | } |
| 2406 | return TRUE; |
| 2407 | |
| 2408 | |
| 2409 | /* ----- conditional traps ----- */ |
| 2410 | |
| 2411 | case 0x30: /* TGE - MIPS II */ |
| 2412 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2413 | UML_EXHc(block, IF_GE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,GE |
| 2414 | return TRUE; |
| 2415 | |
| 2416 | case 0x31: /* TGEU - MIPS II */ |
| 2417 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2418 | UML_EXHc(block, IF_AE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,AE |
| 2419 | return TRUE; |
| 2420 | |
| 2421 | case 0x32: /* TLT - MIPS II */ |
| 2422 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2423 | UML_EXHc(block, IF_L, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,LT |
| 2424 | return TRUE; |
| 2425 | |
| 2426 | case 0x33: /* TLTU - MIPS II */ |
| 2427 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2428 | UML_EXHc(block, IF_B, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,B |
| 2429 | return TRUE; |
| 2430 | |
| 2431 | case 0x34: /* TEQ - MIPS II */ |
| 2432 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2433 | UML_EXHc(block, IF_E, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,E |
| 2434 | return TRUE; |
| 2435 | |
| 2436 | case 0x36: /* TNE - MIPS II */ |
| 2437 | UML_DCMP(block, R64(RSREG), R64(RTREG)); // dcmp <rsreg>,<rtreg> |
| 2438 | UML_EXHc(block, IF_NE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,NE |
| 2439 | return TRUE; |
| 2440 | |
| 2441 | |
| 2442 | /* ----- conditional moves ----- */ |
| 2443 | |
| 2444 | case 0x0a: /* MOVZ - MIPS IV */ |
| 2445 | if (RDREG != 0) |
| 2446 | { |
| 2447 | UML_DCMP(block, R64(RTREG), IMM(0)); // dcmp <rtreg>,0 |
| 2448 | UML_DMOVc(block, IF_Z, R64(RDREG), R64(RSREG)); // mov <rdreg>,<rsreg>,Z |
| 2449 | } |
| 2450 | return TRUE; |
| 2451 | |
| 2452 | case 0x0b: /* MOVN - MIPS IV */ |
| 2453 | if (RDREG != 0) |
| 2454 | { |
| 2455 | UML_DCMP(block, R64(RTREG), IMM(0)); // dcmp <rtreg>,0 |
| 2456 | UML_DMOVc(block, IF_NZ, R64(RDREG), R64(RSREG)); // mov <rdreg>,<rsreg>,NZ |
| 2457 | } |
| 2458 | return TRUE; |
| 2459 | |
| 2460 | case 0x01: /* MOVF/MOVT - MIPS IV */ |
| 2461 | if (RDREG != 0) |
| 2462 | { |
| 2463 | UML_TEST(block, CCR132(31), IMM(fcc_mask[(op >> 18) & 7])); // test ccr31,fcc_mask[x] |
| 2464 | UML_DMOVc(block, ((op >> 16) & 1) ? IF_NZ : IF_Z, R64(RDREG), R64(RSREG)); |
| 2465 | // mov <rdreg>,<rsreg>,NZ/Z |
| 2466 | } |
| 2467 | return TRUE; |
| 2468 | |
| 2469 | |
| 2470 | /* ----- jumps and branches ----- */ |
| 2471 | |
| 2472 | case 0x08: /* JR - MIPS I */ |
| 2473 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 2474 | return TRUE; |
| 2475 | |
| 2476 | case 0x09: /* JALR - MIPS I */ |
| 2477 | generate_delay_slot_and_branch(block, compiler, desc, 31); // <next instruction + hashjmp> |
| 2478 | return TRUE; |
| 2479 | |
| 2480 | |
| 2481 | /* ----- system calls ----- */ |
| 2482 | |
| 2483 | case 0x0c: /* SYSCALL - MIPS I */ |
| 2484 | UML_EXH(block, mips3.exception[EXCEPTION_SYSCALL], IMM(0)); // exh syscall,0 |
| 2485 | return TRUE; |
| 2486 | |
| 2487 | case 0x0d: /* BREAK - MIPS I */ |
| 2488 | UML_EXH(block, mips3.exception[EXCEPTION_BREAK], IMM(0)); // exh break,0 |
| 2489 | return TRUE; |
| 2490 | |
| 2491 | |
| 2492 | /* ----- effective no-ops ----- */ |
| 2493 | |
| 2494 | case 0x0f: /* SYNC - MIPS II */ |
| 2495 | return TRUE; |
| 2496 | |
| 2497 | |
| 2498 | /* ----- hi/lo register access ----- */ |
| 2499 | |
| 2500 | case 0x10: /* MFHI - MIPS I */ |
| 2501 | if (RDREG != 0) |
| 2502 | UML_DMOV(block, R64(RDREG), HI64); // dmov <rdreg>,hi |
| 2503 | return TRUE; |
| 2504 | |
| 2505 | case 0x11: /* MTHI - MIPS I */ |
| 2506 | UML_DMOV(block, HI64, R64(RSREG)); // dmov hi,<rsreg> |
| 2507 | return TRUE; |
| 2508 | |
| 2509 | case 0x12: /* MFLO - MIPS I */ |
| 2510 | if (RDREG != 0) |
| 2511 | UML_DMOV(block, R64(RDREG), LO64); // dmov <rdreg>,lo |
| 2512 | return TRUE; |
| 2513 | |
| 2514 | case 0x13: /* MTLO - MIPS I */ |
| 2515 | UML_DMOV(block, LO64, R64(RSREG)); // dmov lo,<rsreg> |
| 2516 | return TRUE; |
| 2517 | } |
| 2518 | return FALSE; |
| 2519 | } |
| 2520 | |
| 2521 | |
| 2522 | /*------------------------------------------------- |
| 2523 | generate_regimm - compile opcodes in the |
| 2524 | 'REGIMM' group |
| 2525 | -------------------------------------------------*/ |
| 2526 | |
| 2527 | static int generate_regimm(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 2528 | { |
| 2529 | UINT32 op = *desc->opptr.l; |
| 2530 | UINT8 opswitch = RTREG; |
| 2531 | drcuml_codelabel skip; |
| 2532 | |
| 2533 | switch (opswitch) |
| 2534 | { |
| 2535 | case 0x00: /* BLTZ */ |
| 2536 | case 0x02: /* BLTZL */ |
| 2537 | case 0x10: /* BLTZAL */ |
| 2538 | case 0x12: /* BLTZALL */ |
| 2539 | if (RSREG != 0) |
| 2540 | { |
| 2541 | UML_DCMP(block, R64(RSREG), IMM(0)); // dcmp <rsreg>,0 |
| 2542 | UML_JMPc(block, IF_GE, skip = compiler->labelnum++); // jmp skip,GE |
| 2543 | generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0); |
| 2544 | // <next instruction + hashjmp> |
| 2545 | UML_LABEL(block, skip); // skip: |
| 2546 | } |
| 2547 | return TRUE; |
| 2548 | |
| 2549 | case 0x01: /* BGEZ */ |
| 2550 | case 0x03: /* BGEZL */ |
| 2551 | case 0x11: /* BGEZAL */ |
| 2552 | case 0x13: /* BGEZALL */ |
| 2553 | if (RSREG != 0) |
| 2554 | { |
| 2555 | UML_DCMP(block, R64(RSREG), IMM(0)); // dcmp <rsreg>,0 |
| 2556 | UML_JMPc(block, IF_L, skip = compiler->labelnum++); // jmp skip,L |
| 2557 | generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0); |
| 2558 | // <next instruction + hashjmp> |
| 2559 | UML_LABEL(block, skip); // skip: |
| 2560 | } |
| 2561 | else |
| 2562 | generate_delay_slot_and_branch(block, compiler, desc, (opswitch & 0x10) ? 31 : 0); |
| 2563 | // <next instruction + hashjmp> |
| 2564 | return TRUE; |
| 2565 | |
| 2566 | case 0x08: /* TGEI */ |
| 2567 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2568 | UML_EXHc(block, IF_GE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,GE |
| 2569 | return TRUE; |
| 2570 | |
| 2571 | case 0x09: /* TGEIU */ |
| 2572 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2573 | UML_EXHc(block, IF_AE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,AE |
| 2574 | return TRUE; |
| 2575 | |
| 2576 | case 0x0a: /* TLTI */ |
| 2577 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2578 | UML_EXHc(block, IF_L, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,L |
| 2579 | return TRUE; |
| 2580 | |
| 2581 | case 0x0b: /* TLTIU */ |
| 2582 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2583 | UML_EXHc(block, IF_B, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,B |
| 2584 | return TRUE; |
| 2585 | |
| 2586 | case 0x0c: /* TEQI */ |
| 2587 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2588 | UML_EXHc(block, IF_E, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,E |
| 2589 | return TRUE; |
| 2590 | |
| 2591 | case 0x0e: /* TNEI */ |
| 2592 | UML_DCMP(block, R64(RSREG), IMM(SIMMVAL)); // dcmp <rsreg>,SIMMVAL |
| 2593 | UML_EXHc(block, IF_NE, mips3.exception[EXCEPTION_TRAP], IMM(0)); // exh trap,0,NE |
| 2594 | return TRUE; |
| 2595 | } |
| 2596 | return FALSE; |
| 2597 | } |
| 2598 | |
| 2599 | |
| 2600 | /*------------------------------------------------- |
| 2601 | generate_idt - compile opcodes in the IDT- |
| 2602 | specific group |
| 2603 | -------------------------------------------------*/ |
| 2604 | |
| 2605 | static int generate_idt(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 2606 | { |
| 2607 | fatalerror("Unimplemented IDT instructions"); |
| 2608 | #if 0 |
| 2609 | UINT32 op = *desc->opptr.l; |
| 2610 | UINT8 opswitch = op & 0x1f; |
| 2611 | |
| 2612 | switch (opswitch) |
| 2613 | { |
| 2614 | case 0: /* MAD */ |
| 2615 | if (RSREG != 0 && RTREG != 0) |
| 2616 | { |
| 2617 | emit_mov_r32_m32(REG_EAX, REGADDR(RSREG)); // mov eax,[rsreg] |
| 2618 | emit_mov_r32_m32(REG_EDX, REGADDR(RTREG)); // mov edx,[rtreg] |
| 2619 | emit_imul_r32(DRCTOP, REG_EDX); // imul edx |
| 2620 | emit_add_r32_m32(DRCTOP, LOADDR); // add eax,[lo] |
| 2621 | emit_adc_r32_m32(DRCTOP, HIADDR); // adc edx,[hi] |
| 2622 | emit_mov_r32_r32(DRCTOP, REG_EBX, REG_EDX); // mov ebx,edx |
| 2623 | emit_cdq(DRCTOP); // cdq |
| 2624 | emit_mov_m64_r64(DRCTOP, LOADDR, REG_EAX); // mov [lo],edx:eax |
| 2625 | emit_mov_r32_r32(DRCTOP, REG_EAX, REG_EBX); // mov eax,ebx |
| 2626 | emit_cdq(DRCTOP); // cdq |
| 2627 | emit_mov_m64_r64(DRCTOP, HIADDR, REG_EAX); // mov [hi],edx:eax |
| 2628 | } |
| 2629 | return compile_SUCCESSFUL_CP(3,4); |
| 2630 | |
| 2631 | case 1: /* MADU */ |
| 2632 | if (RSREG != 0 && RTREG != 0) |
| 2633 | { |
| 2634 | emit_mov_r32_m32(REG_EAX, REGADDR(RSREG)); // mov eax,[rsreg] |
| 2635 | emit_mov_r32_m32(REG_EDX, REGADDR(RTREG)); // mov edx,[rtreg] |
| 2636 | emit_mul_r32(DRCTOP, REG_EDX); // mul edx |
| 2637 | emit_add_r32_m32(DRCTOP, LOADDR); // add eax,[lo] |
| 2638 | emit_adc_r32_m32(DRCTOP, HIADDR); // adc edx,[hi] |
| 2639 | emit_mov_r32_r32(DRCTOP, REG_EBX, REG_EDX); // mov ebx,edx |
| 2640 | emit_cdq(DRCTOP); // cdq |
| 2641 | emit_mov_m64_r64(DRCTOP, LOADDR, REG_EDX, REG_EAX); // mov [lo],edx:eax |
| 2642 | emit_mov_r32_r32(DRCTOP, REG_EAX, REG_EBX); // mov eax,ebx |
| 2643 | emit_cdq(DRCTOP); // cdq |
| 2644 | emit_mov_m64_r64(DRCTOP, HIADDR, REG_EDX, REG_EAX); // mov [hi],edx:eax |
| 2645 | } |
| 2646 | return compile_SUCCESSFUL_CP(3,4); |
| 2647 | |
| 2648 | case 2: /* MUL */ |
| 2649 | if (RDREG != 0) |
| 2650 | { |
| 2651 | if (RSREG != 0 && RTREG != 0) |
| 2652 | { |
| 2653 | emit_mov_r32_m32(REG_EAX, REGADDR(RSREG)); // mov eax,[rsreg] |
| 2654 | emit_imul_r32_m32(REG_EAX, REGADDR(RTREG)); // imul eax,[rtreg] |
| 2655 | emit_cdq(DRCTOP); // cdq |
| 2656 | emit_mov_m64_r64(REG_ESI, REGADDR(RDREG), REG_EDX, REG_EAX); // mov [rd],edx:eax |
| 2657 | } |
| 2658 | else |
| 2659 | emit_zero_m64(REG_ESI, REGADDR(RDREG)); |
| 2660 | } |
| 2661 | return compile_SUCCESSFUL_CP(3,4); |
| 2662 | } |
| 356 | 2663 | #endif |
| 2664 | return FALSE; |
| 2665 | } |
| 357 | 2666 | |
| 358 | 2667 | |
| 2668 | /*------------------------------------------------- |
| 2669 | generate_set_cop0_reg - generate code to |
| 2670 | handle special COP0 registers |
| 2671 | -------------------------------------------------*/ |
| 359 | 2672 | |
| 2673 | static int generate_set_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg) |
| 2674 | { |
| 2675 | int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0); |
| 2676 | drcuml_codelabel link; |
| 2677 | |
| 2678 | switch (reg) |
| 2679 | { |
| 2680 | case COP0_Cause: |
| 2681 | UML_AND(block, IREG(0), IREG(0), IMM(~0xfc00)); // and i0,i0,~0xfc00 |
| 2682 | UML_AND(block, IREG(1), CPR032(COP0_Cause), IMM(0xfc00)); // and i1,[Cause],0xfc00 |
| 2683 | UML_OR(block, CPR032(COP0_Cause), IREG(0), IREG(1)); // or [Cause],i0,i1 |
| 2684 | compiler->checksoftints = TRUE; |
| 2685 | if (!in_delay_slot) |
| 2686 | generate_update_cycles(block, compiler, desc->pc + 4, NULL, TRUE); |
| 2687 | return TRUE; |
| 2688 | |
| 2689 | case COP0_Status: |
| 2690 | generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles> |
| 2691 | UML_MOV(block, IREG(1), CPR032(COP0_Status)); // mov i1,[Status] |
| 2692 | UML_MOV(block, CPR032(COP0_Status), IREG(0)); // mov [Status],i0 |
| 2693 | UML_SHR(block, IREG(2), IREG(1), IMM(26)); // shr i2,i1,26 |
| 2694 | UML_AND(block, MEM(&mips3.cstate->mode), IREG(2), IMM(1)); // and [mode],i2,1 |
| 2695 | UML_XOR(block, IREG(0), IREG(0), IREG(1)); // xor i0,i0,i1 |
| 2696 | UML_TEST(block, IREG(0), IMM(0x8000)); // test i0,0x8000 |
| 2697 | UML_CALLCc(block, IF_NZ, mips3com_update_cycle_counting, mips3.core); // callc mips3com_update_cycle_counting,mips.core,NZ |
| 2698 | compiler->checkints = TRUE; |
| 2699 | if (!in_delay_slot) |
| 2700 | generate_update_cycles(block, compiler, desc->pc + 4, NULL, TRUE); |
| 2701 | return TRUE; |
| 2702 | |
| 2703 | case COP0_Count: |
| 2704 | generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles> |
| 2705 | UML_MOV(block, CPR032(COP0_Count), IREG(0)); // mov [Count],i0 |
| 2706 | UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles); // callc cfunc_get_cycles,&mips3.numcycles |
| 2707 | UML_DZEXT4(block, IREG(0), IREG(0)); // dzext4 i0,i0 |
| 2708 | UML_DADD(block, IREG(0), IREG(0), IREG(0)); // dadd i0,i0,i0 |
| 2709 | UML_DSUB(block, MEM(&mips3.core->count_zero_time), MEM(&mips3.cstate->numcycles), IREG(0)); |
| 2710 | // dsub [count_zero_time],[mips3.numcycles],i0 |
| 2711 | UML_CALLC(block, mips3com_update_cycle_counting, mips3.core); // callc mips3com_update_cycle_counting,mips.core |
| 2712 | return TRUE; |
| 2713 | |
| 2714 | case COP0_Compare: |
| 2715 | generate_update_cycles(block, compiler, desc->pc, NULL, !in_delay_slot);// <subtract cycles> |
| 2716 | UML_MOV(block, CPR032(COP0_Compare), IREG(0)); // mov [Compare],i0 |
| 2717 | UML_AND(block, CPR032(COP0_Cause), CPR032(COP0_Cause), IMM(~0x8000)); // and [Cause],[Cause],~0x8000 |
| 2718 | UML_CALLC(block, mips3com_update_cycle_counting, mips3.core); // callc mips3com_update_cycle_counting,mips.core |
| 2719 | return TRUE; |
| 2720 | |
| 2721 | case COP0_PRId: |
| 2722 | return TRUE; |
| 2723 | |
| 2724 | case COP0_Config: |
| 2725 | UML_AND(block, IREG(0), IREG(0), IMM(0x0007)); // and i0,i0,0x0007 |
| 2726 | UML_AND(block, IREG(1), CPR032(COP0_Config), IMM(~0x0007)); // and i1,[Config],~0x0007 |
| 2727 | UML_OR(block, CPR032(COP0_Config), IREG(0), IREG(1)); // or [Config],i0,i1 |
| 2728 | return TRUE; |
| 2729 | |
| 2730 | case COP0_EntryHi: |
| 2731 | UML_XOR(block, IREG(1), IREG(0), CPR032(reg)); // xor i1,i0,cpr0[reg] |
| 2732 | UML_MOV(block, CPR032(reg), IREG(0)); // mov cpr0[reg],i0 |
| 2733 | UML_TEST(block, IREG(1), IMM(0xff)); // test i1,0xff |
| 2734 | UML_JMPc(block, IF_Z, link = compiler->labelnum++); // jmp link,z |
| 2735 | UML_CALLC(block, mips3com_unmap_tlb_entries, mips3.core); // callc mips3com_unmap_tlb_entries |
| 2736 | UML_CALLC(block, mips3com_map_tlb_entries, mips3.core); // callc mips3com_map_tlb_entries |
| 2737 | UML_LABEL(block, link); // link: |
| 2738 | return TRUE; |
| 2739 | |
| 2740 | default: |
| 2741 | UML_MOV(block, CPR032(reg), IREG(0)); // mov cpr0[reg],i0 |
| 2742 | return TRUE; |
| 2743 | } |
| 2744 | } |
| 2745 | |
| 2746 | |
| 2747 | /*------------------------------------------------- |
| 2748 | generate_get_cop0_reg - generate code to |
| 2749 | read special COP0 registers |
| 2750 | -------------------------------------------------*/ |
| 2751 | |
| 2752 | static int generate_get_cop0_reg(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc, UINT8 reg) |
| 2753 | { |
| 2754 | drcuml_codelabel link1, link2; |
| 2755 | |
| 2756 | switch (reg) |
| 2757 | { |
| 2758 | case COP0_Count: |
| 2759 | generate_update_cycles(block, compiler, desc->pc, NULL, FALSE); // <subtract cycles> |
| 2760 | UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles); // callc cfunc_get_cycles,&mips3.numcycles |
| 2761 | UML_DSUB(block, IREG(0), MEM(&mips3.cstate->numcycles), MEM(&mips3.core->count_zero_time)); |
| 2762 | // dsub i0,[numcycles],[count_zero_time] |
| 2763 | UML_DSHR(block, IREG(0), IREG(0), IMM(1)); // dshr i0,i0,1 |
| 2764 | UML_DSEXT4(block, IREG(0), IREG(0)); // dsext4 i0,i0 |
| 2765 | return TRUE; |
| 2766 | |
| 2767 | case COP0_Random: |
| 2768 | generate_update_cycles(block, compiler, desc->pc, NULL, FALSE); // <subtract cycles> |
| 2769 | UML_CALLC(block, cfunc_get_cycles, &mips3.cstate->numcycles); // callc cfunc_get_cycles,&mips3.numcycles |
| 2770 | UML_DSUB(block, IREG(0), MEM(&mips3.cstate->numcycles), MEM(&mips3.core->count_zero_time)); |
| 2771 | // dsub i0,[numcycles],[count_zero_time] |
| 2772 | UML_AND(block, IREG(1), CPR032(COP0_Wired), IMM(0x3f)); // and i1,[Wired],0x3f |
| 2773 | UML_SUBf(block, IREG(2), IMM(48), IREG(1), FLAGS_BE); // sub i2,48,i1,ALL |
| 2774 | UML_JMPc(block, IF_BE, link1 = compiler->labelnum++); // jmp link1,BE |
| 2775 | UML_DZEXT4(block, IREG(2), IREG(2)); // dzext4 i2,i2 |
| 2776 | UML_DDIVU(block, IREG(0), IREG(2), IREG(0), IREG(2)); // ddivu i0,i2,i0,i2 |
| 2777 | UML_ADD(block, IREG(0), IREG(2), IREG(1)); // add i0,i2,i1 |
| 2778 | UML_DAND(block, IREG(0), IREG(0), IMM(0x3f)); // dand i0,i0,0x3f |
| 2779 | UML_JMP(block, link2 = compiler->labelnum++); // jmp link2 |
| 2780 | UML_LABEL(block, link1); // link1: |
| 2781 | UML_DSEXT4(block, IREG(0), IMM(47)); // dsext4 i0,47 |
| 2782 | UML_LABEL(block, link2); // link2: |
| 2783 | return TRUE; |
| 2784 | |
| 2785 | default: |
| 2786 | UML_DSEXT4(block, IREG(0), CPR032(reg)); // dsext4 i0,cpr0[reg] |
| 2787 | return TRUE; |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | |
| 2792 | /*------------------------------------------------- |
| 2793 | generate_cop0 - compile COP0 opcodes |
| 2794 | -------------------------------------------------*/ |
| 2795 | |
| 2796 | static int generate_cop0(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 2797 | { |
| 2798 | UINT32 op = *desc->opptr.l; |
| 2799 | UINT8 opswitch = RSREG; |
| 2800 | |
| 2801 | /* generate an exception if COP0 is disabled or we are not in kernel mode */ |
| 2802 | if (mips3.drcoptions & MIPS3DRC_STRICT_COP0) |
| 2803 | { |
| 2804 | drcuml_codelabel okay; |
| 2805 | |
| 2806 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_KSU_MASK)); // test [Status],SR_KSU_MASK |
| 2807 | UML_JMPc(block, IF_Z, okay = compiler->labelnum++); // jmp okay,Z |
| 2808 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP0)); // test [Status],SR_COP0 |
| 2809 | UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0)); // exh cop,0,Z |
| 2810 | UML_LABEL(block, okay); // okay: |
| 2811 | } |
| 2812 | |
| 2813 | switch (opswitch) |
| 2814 | { |
| 2815 | case 0x00: /* MFCz */ |
| 2816 | if (RTREG != 0) |
| 2817 | { |
| 2818 | generate_get_cop0_reg(block, compiler, desc, RDREG); // <get cop0 reg> |
| 2819 | UML_DSEXT4(block, R64(RTREG), IREG(0)); // dsext4 <rtreg>,i0 |
| 2820 | } |
| 2821 | return TRUE; |
| 2822 | |
| 2823 | case 0x01: /* DMFCz */ |
| 2824 | if (RTREG != 0) |
| 2825 | { |
| 2826 | generate_get_cop0_reg(block, compiler, desc, RDREG); // <get cop0 reg> |
| 2827 | UML_DMOV(block, R64(RTREG), IREG(0)); // dmov <rtreg>,i0 |
| 2828 | } |
| 2829 | return TRUE; |
| 2830 | |
| 2831 | case 0x02: /* CFCz */ |
| 2832 | if (RTREG != 0) |
| 2833 | UML_DSEXT4(block, R32(RTREG), CCR032(RDREG)); // dsext4 <rtreg>,ccr0[rdreg] |
| 2834 | return TRUE; |
| 2835 | |
| 2836 | case 0x04: /* MTCz */ |
| 2837 | UML_DSEXT4(block, IREG(0), R32(RTREG)); // dsext4 i0,<rtreg> |
| 2838 | generate_set_cop0_reg(block, compiler, desc, RDREG); // <set cop0 reg> |
| 2839 | return TRUE; |
| 2840 | |
| 2841 | case 0x05: /* DMTCz */ |
| 2842 | UML_DMOV(block, IREG(0), R64(RTREG)); // dmov i0,<rtreg> |
| 2843 | generate_set_cop0_reg(block, compiler, desc, RDREG); // <set cop0 reg> |
| 2844 | return TRUE; |
| 2845 | |
| 2846 | case 0x06: /* CTCz */ |
| 2847 | UML_DSEXT4(block, CCR064(RDREG), R32(RTREG)); // dsext4 ccr0[rdreg],<rtreg> |
| 2848 | return TRUE; |
| 2849 | |
| 2850 | case 0x10: |
| 2851 | case 0x11: |
| 2852 | case 0x12: |
| 2853 | case 0x13: |
| 2854 | case 0x14: |
| 2855 | case 0x15: |
| 2856 | case 0x16: |
| 2857 | case 0x17: |
| 2858 | case 0x18: |
| 2859 | case 0x19: |
| 2860 | case 0x1a: |
| 2861 | case 0x1b: |
| 2862 | case 0x1c: |
| 2863 | case 0x1d: |
| 2864 | case 0x1e: |
| 2865 | case 0x1f: /* COP */ |
| 2866 | switch (op & 0x01ffffff) |
| 2867 | { |
| 2868 | case 0x01: /* TLBR */ |
| 2869 | UML_CALLC(block, mips3com_tlbr, mips3.core); // callc mips3com_tlbr,mips3.core |
| 2870 | return TRUE; |
| 2871 | |
| 2872 | case 0x02: /* TLBWI */ |
| 2873 | UML_CALLC(block, mips3com_tlbwi, mips3.core); // callc mips3com_tlbwi,mips3.core |
| 2874 | return TRUE; |
| 2875 | |
| 2876 | case 0x06: /* TLBWR */ |
| 2877 | UML_CALLC(block, mips3com_tlbwr, mips3.core); // callc mips3com_tlbwr,mips3.core |
| 2878 | return TRUE; |
| 2879 | |
| 2880 | case 0x08: /* TLBP */ |
| 2881 | UML_CALLC(block, mips3com_tlbp, mips3.core); // callc mips3com_tlbp,mips3.core |
| 2882 | return TRUE; |
| 2883 | |
| 2884 | case 0x18: /* ERET */ |
| 2885 | UML_AND(block, CPR032(COP0_Status), CPR032(COP0_Status), IMM(~SR_EXL)); |
| 2886 | // and [Status],[Status],~SR_EXL |
| 2887 | compiler->checkints = TRUE; |
| 2888 | generate_update_cycles(block, compiler, 0, LOPTR(&mips3.core->cpr[0][COP0_EPC]), TRUE); |
| 2889 | // <subtract cycles> |
| 2890 | UML_HASHJMP(block, IMM(mips3.cstate->mode), CPR032(COP0_EPC), mips3.nocode); |
| 2891 | // hashjmp <mode>,[EPC],nocode |
| 2892 | return TRUE; |
| 2893 | |
| 2894 | case 0x20: /* WAIT */ |
| 2895 | return TRUE; |
| 2896 | } |
| 2897 | break; |
| 2898 | } |
| 2899 | |
| 2900 | return FALSE; |
| 2901 | } |
| 2902 | |
| 2903 | |
| 2904 | |
| 360 | 2905 | /*************************************************************************** |
| 2906 | COP1 RECOMPILATION |
| 2907 | ***************************************************************************/ |
| 2908 | |
| 2909 | /*------------------------------------------------- |
| 2910 | generate_cop1 - compile COP1 opcodes |
| 2911 | -------------------------------------------------*/ |
| 2912 | |
| 2913 | static int generate_cop1(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 2914 | { |
| 2915 | UINT32 op = *desc->opptr.l; |
| 2916 | drcuml_codelabel skip; |
| 2917 | int condition; |
| 2918 | |
| 2919 | /* generate an exception if COP1 is disabled */ |
| 2920 | if (mips3.drcoptions & MIPS3DRC_STRICT_COP1) |
| 2921 | { |
| 2922 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP1)); // test [Status],SR_COP1 |
| 2923 | UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0)); // exh cop,0,Z |
| 2924 | } |
| 2925 | |
| 2926 | switch (RSREG) |
| 2927 | { |
| 2928 | case 0x00: /* MFCz - MIPS I */ |
| 2929 | if (RTREG != 0) |
| 2930 | UML_DSEXT4(block, R64(RTREG), FPR32(RDREG)); // dsext4 <rtreg>,fpr[rdreg] |
| 2931 | return TRUE; |
| 2932 | |
| 2933 | case 0x01: /* DMFCz - MIPS III */ |
| 2934 | if (RTREG != 0) |
| 2935 | UML_DMOV(block, R64(RTREG), FPR64(RDREG)); // dmov <rtreg>,fpr[rdreg] |
| 2936 | return TRUE; |
| 2937 | |
| 2938 | case 0x02: /* CFCz - MIPS I */ |
| 2939 | if (RTREG != 0) |
| 2940 | UML_DSEXT4(block, R64(RTREG), CCR132(RDREG)); // dsext4 <rtreg>,ccr132[rdreg] |
| 2941 | return TRUE; |
| 2942 | |
| 2943 | case 0x04: /* MTCz - MIPS I */ |
| 2944 | UML_MOV(block, FPR32(RDREG), R32(RTREG)); // mov fpr[rdreg],<rtreg> |
| 2945 | return TRUE; |
| 2946 | |
| 2947 | case 0x05: /* DMTCz - MIPS III */ |
| 2948 | UML_DMOV(block, FPR64(RDREG), R64(RTREG)); // dmov fpr[rdreg],<rtreg> |
| 2949 | return TRUE; |
| 2950 | |
| 2951 | case 0x06: /* CTCz - MIPS I */ |
| 2952 | if (RDREG != 31) |
| 2953 | UML_DSEXT4(block, CCR164(RDREG), R32(RTREG)); // dsext4 ccr1[rdreg],<rtreg> |
| 2954 | else |
| 2955 | { |
| 2956 | UML_XOR(block, IREG(0), CCR132(31), R32(RTREG)); // xor i0,ccr1[31],<rtreg> |
| 2957 | UML_DSEXT4(block, CCR164(31), R32(RTREG)); // dsext4 ccr1[31],<rtreg> |
| 2958 | UML_TEST(block, IREG(0), IMM(3)); // test i0,3 |
| 2959 | UML_JMPc(block, IF_Z, skip = compiler->labelnum++); // jmp skip,Z |
| 2960 | UML_AND(block, IREG(0), CCR132(31), IMM(3)); // and i0,ccr1[31],3 |
| 2961 | UML_LOAD1U(block, IREG(0), &mips3.cstate->fpmode[0], IREG(0)); // load1u i0,fpmode,i0 |
| 2962 | UML_SETFMOD(block, IREG(0)); // setfmod i0 |
| 2963 | UML_LABEL(block, skip); // skip: |
| 2964 | } |
| 2965 | return TRUE; |
| 2966 | |
| 2967 | case 0x08: /* BC */ |
| 2968 | switch ((op >> 16) & 3) |
| 2969 | { |
| 2970 | case 0x00: /* BCzF - MIPS I */ |
| 2971 | case 0x02: /* BCzFL - MIPS II */ |
| 2972 | UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18))); // test ccr1[31],fccmask[which] |
| 2973 | UML_JMPc(block, IF_NZ, skip = compiler->labelnum++); // jmp skip,NZ |
| 2974 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 2975 | UML_LABEL(block, skip); // skip: |
| 2976 | return TRUE; |
| 2977 | |
| 2978 | case 0x01: /* BCzT - MIPS I */ |
| 2979 | case 0x03: /* BCzTL - MIPS II */ |
| 2980 | UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18))); // test ccr1[31],fccmask[which] |
| 2981 | UML_JMPc(block, IF_Z, skip = compiler->labelnum++); // jmp skip,Z |
| 2982 | generate_delay_slot_and_branch(block, compiler, desc, 0); // <next instruction + hashjmp> |
| 2983 | UML_LABEL(block, skip); // skip: |
| 2984 | return TRUE; |
| 2985 | } |
| 2986 | break; |
| 2987 | |
| 2988 | default: |
| 2989 | switch (op & 0x3f) |
| 2990 | { |
| 2991 | case 0x00: |
| 2992 | if (IS_SINGLE(op)) /* ADD.S - MIPS I */ |
| 2993 | UML_FSADD(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG)); // fsadd <fdreg>,<fsreg>,<ftreg> |
| 2994 | else /* ADD.D - MIPS I */ |
| 2995 | UML_FDADD(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG)); // fdadd <fdreg>,<fsreg>,<ftreg> |
| 2996 | return TRUE; |
| 2997 | |
| 2998 | case 0x01: |
| 2999 | if (IS_SINGLE(op)) /* SUB.S - MIPS I */ |
| 3000 | UML_FSSUB(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG)); // fssub <fdreg>,<fsreg>,<ftreg> |
| 3001 | else /* SUB.D - MIPS I */ |
| 3002 | UML_FDSUB(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG)); // fdsub <fdreg>,<fsreg>,<ftreg> |
| 3003 | return TRUE; |
| 3004 | |
| 3005 | case 0x02: |
| 3006 | if (IS_SINGLE(op)) /* MUL.S - MIPS I */ |
| 3007 | UML_FSMUL(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG)); // fsmul <fdreg>,<fsreg>,<ftreg> |
| 3008 | else /* MUL.D - MIPS I */ |
| 3009 | UML_FDMUL(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG)); // fdmul <fdreg>,<fsreg>,<ftreg> |
| 3010 | return TRUE; |
| 3011 | |
| 3012 | case 0x03: |
| 3013 | if (IS_SINGLE(op)) /* DIV.S - MIPS I */ |
| 3014 | UML_FSDIV(block, FPR32(FDREG), FPR32(FSREG), FPR32(FTREG)); // fsdiv <fdreg>,<fsreg>,<ftreg> |
| 3015 | else /* DIV.D - MIPS I */ |
| 3016 | UML_FDDIV(block, FPR64(FDREG), FPR64(FSREG), FPR64(FTREG)); // fddiv <fdreg>,<fsreg>,<ftreg> |
| 3017 | return TRUE; |
| 3018 | |
| 3019 | case 0x04: |
| 3020 | if (IS_SINGLE(op)) /* SQRT.S - MIPS II */ |
| 3021 | UML_FSSQRT(block, FPR32(FDREG), FPR32(FSREG)); // fssqrt <fdreg>,<fsreg> |
| 3022 | else /* SQRT.D - MIPS II */ |
| 3023 | UML_FDSQRT(block, FPR64(FDREG), FPR64(FSREG)); // fdsqrt <fdreg>,<fsreg> |
| 3024 | return TRUE; |
| 3025 | |
| 3026 | case 0x05: |
| 3027 | if (IS_SINGLE(op)) /* ABS.S - MIPS I */ |
| 3028 | UML_FSABS(block, FPR32(FDREG), FPR32(FSREG)); // fsabs <fdreg>,<fsreg> |
| 3029 | else /* ABS.D - MIPS I */ |
| 3030 | UML_FDABS(block, FPR64(FDREG), FPR64(FSREG)); // fdabs <fdreg>,<fsreg> |
| 3031 | return TRUE; |
| 3032 | |
| 3033 | case 0x06: |
| 3034 | if (IS_SINGLE(op)) /* MOV.S - MIPS I */ |
| 3035 | UML_FSMOV(block, FPR32(FDREG), FPR32(FSREG)); // fsmov <fdreg>,<fsreg> |
| 3036 | else /* MOV.D - MIPS I */ |
| 3037 | UML_FDMOV(block, FPR64(FDREG), FPR64(FSREG)); // fdmov <fdreg>,<fsreg> |
| 3038 | return TRUE; |
| 3039 | |
| 3040 | case 0x07: |
| 3041 | if (IS_SINGLE(op)) /* NEG.S - MIPS I */ |
| 3042 | UML_FSNEG(block, FPR32(FDREG), FPR32(FSREG)); // fsneg <fdreg>,<fsreg> |
| 3043 | else /* NEG.D - MIPS I */ |
| 3044 | UML_FDNEG(block, FPR64(FDREG), FPR64(FSREG)); // fdneg <fdreg>,<fsreg> |
| 3045 | return TRUE; |
| 3046 | |
| 3047 | case 0x08: |
| 3048 | if (IS_SINGLE(op)) /* ROUND.L.S - MIPS III */ |
| 3049 | UML_FSTOI8R(block, FPR64(FDREG), FPR32(FSREG)); // fstoi8r <fdreg>,<fsreg> |
| 3050 | else /* ROUND.L.D - MIPS III */ |
| 3051 | UML_FDTOI8R(block, FPR64(FDREG), FPR64(FSREG)); // fdtoi8r <fdreg>,<fsreg> |
| 3052 | return TRUE; |
| 3053 | |
| 3054 | case 0x09: |
| 3055 | if (IS_SINGLE(op)) /* TRUNC.L.S - MIPS III */ |
| 3056 | UML_FSTOI8T(block, FPR64(FDREG), FPR32(FSREG)); // fstoi8t <fdreg>,<fsreg> |
| 3057 | else /* TRUNC.L.D - MIPS III */ |
| 3058 | UML_FDTOI8T(block, FPR64(FDREG), FPR64(FSREG)); // fdtoi8t <fdreg>,<fsreg> |
| 3059 | return TRUE; |
| 3060 | |
| 3061 | case 0x0a: |
| 3062 | if (IS_SINGLE(op)) /* CEIL.L.S - MIPS III */ |
| 3063 | UML_FSTOI8C(block, FPR64(FDREG), FPR32(FSREG)); // fstoi8c <fdreg>,<fsreg> |
| 3064 | else /* CEIL.L.D - MIPS III */ |
| 3065 | UML_FDTOI8C(block, FPR64(FDREG), FPR64(FSREG)); // fdtoi8c <fdreg>,<fsreg> |
| 3066 | return TRUE; |
| 3067 | |
| 3068 | case 0x0b: |
| 3069 | if (IS_SINGLE(op)) /* FLOOR.L.S - MIPS III */ |
| 3070 | UML_FSTOI8F(block, FPR64(FDREG), FPR32(FSREG)); // fstoi8f <fdreg>,<fsreg> |
| 3071 | else /* FLOOR.L.D - MIPS III */ |
| 3072 | UML_FDTOI8F(block, FPR64(FDREG), FPR64(FSREG)); // fdtoi8f <fdreg>,<fsreg> |
| 3073 | return TRUE; |
| 3074 | |
| 3075 | case 0x0c: |
| 3076 | if (IS_SINGLE(op)) /* ROUND.W.S - MIPS II */ |
| 3077 | UML_FSTOI4R(block, FPR32(FDREG), FPR32(FSREG)); // fstoi4r <fdreg>,<fsreg> |
| 3078 | else /* ROUND.W.D - MIPS II */ |
| 3079 | UML_FDTOI4R(block, FPR32(FDREG), FPR64(FSREG)); // fdtoi4r <fdreg>,<fsreg> |
| 3080 | return TRUE; |
| 3081 | |
| 3082 | case 0x0d: |
| 3083 | if (IS_SINGLE(op)) /* TRUNC.W.S - MIPS II */ |
| 3084 | UML_FSTOI4T(block, FPR32(FDREG), FPR32(FSREG)); // fstoi4t <fdreg>,<fsreg> |
| 3085 | else /* TRUNC.W.D - MIPS II */ |
| 3086 | UML_FDTOI4T(block, FPR32(FDREG), FPR64(FSREG)); // fdtoi4t <fdreg>,<fsreg> |
| 3087 | return TRUE; |
| 3088 | |
| 3089 | case 0x0e: |
| 3090 | if (IS_SINGLE(op)) /* CEIL.W.S - MIPS II */ |
| 3091 | UML_FSTOI4C(block, FPR32(FDREG), FPR32(FSREG)); // fstoi4c <fdreg>,<fsreg> |
| 3092 | else /* CEIL.W.D - MIPS II */ |
| 3093 | UML_FDTOI4C(block, FPR32(FDREG), FPR64(FSREG)); // fdtoi4c <fdreg>,<fsreg> |
| 3094 | return TRUE; |
| 3095 | |
| 3096 | case 0x0f: |
| 3097 | if (IS_SINGLE(op)) /* FLOOR.W.S - MIPS II */ |
| 3098 | UML_FSTOI4F(block, FPR32(FDREG), FPR32(FSREG)); // fstoi4f <fdreg>,<fsreg> |
| 3099 | else /* FLOOR.W.D - MIPS II */ |
| 3100 | UML_FDTOI4F(block, FPR32(FDREG), FPR64(FSREG)); // fdtoi4f <fdreg>,<fsreg> |
| 3101 | return TRUE; |
| 3102 | |
| 3103 | case 0x11: |
| 3104 | condition = ((op >> 16) & 1) ? IF_NZ : IF_Z; |
| 3105 | UML_TEST(block, CCR132(31), IMM(FCCMASK(op >> 18))); // test ccr31,fccmask[op] |
| 3106 | if (IS_SINGLE(op)) /* MOVT/F.S - MIPS IV */ |
| 3107 | UML_FSMOVc(block, condition, FPR32(FDREG), FPR32(FSREG)); // fsmov <fdreg>,<fsreg>,condition |
| 3108 | else /* MOVT/F.D - MIPS IV */ |
| 3109 | UML_FDMOVc(block, condition, FPR64(FDREG), FPR64(FSREG)); // fdmov <fdreg>,<fsreg>,condition |
| 3110 | return TRUE; |
| 3111 | |
| 3112 | case 0x12: |
| 3113 | UML_DCMP(block, R64(RTREG), IMM(0)); // dcmp <rtreg>,0 |
| 3114 | if (IS_SINGLE(op)) /* MOVZ.S - MIPS IV */ |
| 3115 | UML_FSMOVc(block, IF_Z, FPR32(FDREG), FPR32(FSREG)); // fsmov <fdreg>,<fsreg>,Z |
| 3116 | else /* MOVZ.D - MIPS IV */ |
| 3117 | UML_FDMOVc(block, IF_Z, FPR64(FDREG), FPR64(FSREG)); // fdmov <fdreg>,<fsreg>,Z |
| 3118 | return TRUE; |
| 3119 | |
| 3120 | case 0x13: |
| 3121 | UML_DCMP(block, R64(RTREG), IMM(0)); // dcmp <rtreg>,0 |
| 3122 | if (IS_SINGLE(op)) /* MOVN.S - MIPS IV */ |
| 3123 | UML_FSMOVc(block, IF_NZ, FPR32(FDREG), FPR32(FSREG)); // fsmov <fdreg>,<fsreg>,NZ |
| 3124 | else /* MOVN.D - MIPS IV */ |
| 3125 | UML_FDMOVc(block, IF_NZ, FPR64(FDREG), FPR64(FSREG)); // fdmov <fdreg>,<fsreg>,NZ |
| 3126 | return TRUE; |
| 3127 | |
| 3128 | case 0x15: |
| 3129 | if (IS_SINGLE(op)) /* RECIP.S - MIPS IV */ |
| 3130 | UML_FSRECIP(block, FPR32(FDREG), FPR32(FSREG)); // fsrecip <fdreg>,<fsreg> |
| 3131 | else /* RECIP.D - MIPS IV */ |
| 3132 | UML_FDRECIP(block, FPR64(FDREG), FPR64(FSREG)); // fdrecip <fdreg>,<fsreg> |
| 3133 | return TRUE; |
| 3134 | |
| 3135 | case 0x16: |
| 3136 | if (IS_SINGLE(op)) /* RSQRT.S - MIPS IV */ |
| 3137 | UML_FSRSQRT(block, FPR32(FDREG), FPR32(FSREG)); // fsrsqrt <fdreg>,<fsreg> |
| 3138 | else /* RSQRT.D - MIPS IV */ |
| 3139 | UML_FDRSQRT(block, FPR64(FDREG), FPR64(FSREG)); // fdrsqrt <fdreg>,<fsreg> |
| 3140 | return TRUE; |
| 3141 | |
| 3142 | case 0x20: |
| 3143 | if (IS_INTEGRAL(op)) |
| 3144 | { |
| 3145 | if (IS_SINGLE(op)) /* CVT.S.W - MIPS I */ |
| 3146 | UML_FSFRI4(block, FPR32(FDREG), FPR32(FSREG)); // fsfri4 <fdreg>,<fsreg> |
| 3147 | else /* CVT.S.L - MIPS I */ |
| 3148 | UML_FSFRI8(block, FPR32(FDREG), FPR64(FSREG)); // fsfri8 <fdreg>,<fsreg> |
| 3149 | } |
| 3150 | else /* CVT.S.D - MIPS I */ |
| 3151 | UML_FSFRFD(block, FPR32(FDREG), FPR64(FSREG)); // fsfrfd <fdreg>,<fsreg> |
| 3152 | return TRUE; |
| 3153 | |
| 3154 | case 0x21: |
| 3155 | if (IS_INTEGRAL(op)) |
| 3156 | { |
| 3157 | if (IS_SINGLE(op)) /* CVT.D.W - MIPS I */ |
| 3158 | UML_FDFRI4(block, FPR64(FDREG), FPR32(FSREG)); // fdfri4 <fdreg>,<fsreg> |
| 3159 | else /* CVT.D.L - MIPS I */ |
| 3160 | UML_FDFRI8(block, FPR64(FDREG), FPR64(FSREG)); // fdfri8 <fdreg>,<fsreg> |
| 3161 | } |
| 3162 | else /* CVT.D.S - MIPS I */ |
| 3163 | UML_FDFRFS(block, FPR64(FDREG), FPR32(FSREG)); // fdfrfs <fdreg>,<fsreg> |
| 3164 | return TRUE; |
| 3165 | |
| 3166 | case 0x24: |
| 3167 | if (IS_SINGLE(op)) /* CVT.W.S - MIPS I */ |
| 3168 | UML_FSTOI4(block, FPR32(FDREG), FPR32(FSREG)); // fstoi4 <fdreg>,<fsreg> |
| 3169 | else /* CVT.W.D - MIPS I */ |
| 3170 | UML_FDTOI4(block, FPR32(FDREG), FPR64(FSREG)); // fdtoi4 <fdreg>,<fsreg> |
| 3171 | return TRUE; |
| 3172 | |
| 3173 | case 0x25: |
| 3174 | if (IS_SINGLE(op)) /* CVT.L.S - MIPS I */ |
| 3175 | UML_FSTOI8(block, FPR64(FDREG), FPR32(FSREG)); // fstoi8 <fdreg>,<fsreg> |
| 3176 | else /* CVT.L.D - MIPS I */ |
| 3177 | UML_FDTOI8(block, FPR64(FDREG), FPR64(FSREG)); // fdtoi8 <fdreg>,<fsreg> |
| 3178 | return TRUE; |
| 3179 | |
| 3180 | case 0x30: |
| 3181 | case 0x38: /* C.F.S/D - MIPS I */ |
| 3182 | UML_AND(block, CCR132(31), CCR132(31), IMM(~FCCMASK(op >> 8))); // and ccr31,ccr31,~fccmask[op] |
| 3183 | return TRUE; |
| 3184 | |
| 3185 | case 0x31: |
| 3186 | case 0x39: |
| 3187 | if (IS_SINGLE(op)) /* C.UN.S - MIPS I */ |
| 3188 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3189 | else /* C.UN.D - MIPS I */ |
| 3190 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3191 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_un_table); |
| 3192 | // flags ccr31,fccmask[op],c_eq_table |
| 3193 | return TRUE; |
| 3194 | |
| 3195 | case 0x32: |
| 3196 | case 0x3a: |
| 3197 | if (IS_SINGLE(op)) /* C.EQ.S - MIPS I */ |
| 3198 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3199 | else /* C.EQ.D - MIPS I */ |
| 3200 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3201 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_eq_table); |
| 3202 | // flags ccr31,fccmask[op],c_eq_table |
| 3203 | return TRUE; |
| 3204 | |
| 3205 | case 0x33: |
| 3206 | case 0x3b: |
| 3207 | if (IS_SINGLE(op)) /* C.UEQ.S - MIPS I */ |
| 3208 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3209 | else /* C.UEQ.D - MIPS I */ |
| 3210 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3211 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ueq_table); |
| 3212 | // flags ccr31,fccmask[op],c_eq_table |
| 3213 | return TRUE; |
| 3214 | |
| 3215 | case 0x34: |
| 3216 | case 0x3c: |
| 3217 | if (IS_SINGLE(op)) /* C.OLT.S - MIPS I */ |
| 3218 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3219 | else /* C.OLT.D - MIPS I */ |
| 3220 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3221 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_olt_table); |
| 3222 | // flags ccr31,fccmask[op],c_lt_table |
| 3223 | return TRUE; |
| 3224 | |
| 3225 | case 0x35: |
| 3226 | case 0x3d: |
| 3227 | if (IS_SINGLE(op)) /* C.ULT.S - MIPS I */ |
| 3228 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3229 | else /* C.ULT.D - MIPS I */ |
| 3230 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3231 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ult_table); |
| 3232 | // flags ccr31,fccmask[op],c_lt_table |
| 3233 | return TRUE; |
| 3234 | |
| 3235 | case 0x36: |
| 3236 | case 0x3e: |
| 3237 | if (IS_SINGLE(op)) /* C.OLE.S - MIPS I */ |
| 3238 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3239 | else /* C.OLE.D - MIPS I */ |
| 3240 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3241 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ole_table); |
| 3242 | // flags ccr31,fccmask[op],c_le_table |
| 3243 | return TRUE; |
| 3244 | |
| 3245 | case 0x37: |
| 3246 | case 0x3f: |
| 3247 | if (IS_SINGLE(op)) /* C.ULE.S - MIPS I */ |
| 3248 | UML_FSCMP(block, FPR32(FSREG), FPR32(FTREG)); // fscmp <fsreg>,<ftreg> |
| 3249 | else /* C.ULE.D - MIPS I */ |
| 3250 | UML_FDCMP(block, FPR64(FSREG), FPR64(FTREG)); // fdcmp <fsreg>,<ftreg> |
| 3251 | UML_FLAGS(block, CCR132(31), FCCMASK(op >> 8), mips3.cstate->c_ule_table); |
| 3252 | // flags ccr31,fccmask[op],c_le_table |
| 3253 | return TRUE; |
| 3254 | } |
| 3255 | break; |
| 3256 | } |
| 3257 | return FALSE; |
| 3258 | } |
| 3259 | |
| 3260 | |
| 3261 | |
| 3262 | /*************************************************************************** |
| 3263 | COP1X RECOMPILATION |
| 3264 | ***************************************************************************/ |
| 3265 | |
| 3266 | /*------------------------------------------------- |
| 3267 | generate_cop1x - compile COP1X opcodes |
| 3268 | -------------------------------------------------*/ |
| 3269 | |
| 3270 | static int generate_cop1x(drcuml_block *block, compiler_state *compiler, const opcode_desc *desc) |
| 3271 | { |
| 3272 | int in_delay_slot = ((desc->flags & OPFLAG_IN_DELAY_SLOT) != 0); |
| 3273 | UINT32 op = *desc->opptr.l; |
| 3274 | |
| 3275 | if (mips3.drcoptions & MIPS3DRC_STRICT_COP1) |
| 3276 | { |
| 3277 | UML_TEST(block, CPR032(COP0_Status), IMM(SR_COP1)); // test [Status],SR_COP1 |
| 3278 | UML_EXHc(block, IF_Z, mips3.exception[EXCEPTION_BADCOP], IMM(0)); // exh cop,0,Z |
| 3279 | } |
| 3280 | |
| 3281 | switch (op & 0x3f) |
| 3282 | { |
| 3283 | case 0x00: /* LWXC1 - MIPS IV */ |
| 3284 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 3285 | UML_CALLH(block, mips3.read32); // callh read32 |
| 3286 | UML_MOV(block, FPR32(RDREG), IREG(0)); // mov <cpr1_rd>,i0 |
| 3287 | if (!in_delay_slot) |
| 3288 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 3289 | return TRUE; |
| 3290 | |
| 3291 | case 0x01: /* LDXC1 - MIPS IV */ |
| 3292 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 3293 | UML_CALLH(block, mips3.read64); // callh read64 |
| 3294 | UML_DMOV(block, FPR64(RDREG), IREG(0)); // dmov <cpr1_rd>,i0 |
| 3295 | if (!in_delay_slot) |
| 3296 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 3297 | return TRUE; |
| 3298 | |
| 3299 | case 0x08: /* SWXC1 - MIPS IV */ |
| 3300 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 3301 | UML_MOV(block, IREG(1), FPR32(RTREG)); // mov i1,<cpr1_rt> |
| 3302 | UML_CALLH(block, mips3.write32); // callh write32 |
| 3303 | if (!in_delay_slot) |
| 3304 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 3305 | return TRUE; |
| 3306 | |
| 3307 | case 0x09: /* SDXC1 - MIPS IV */ |
| 3308 | UML_ADD(block, IREG(0), R32(RSREG), R32(RTREG)); // add i0,<rsreg>,<rtreg> |
| 3309 | UML_DMOV(block, IREG(1), FPR64(RTREG)); // dmov i1,<cpr1_rt> |
| 3310 | UML_CALLH(block, mips3.write64); // callh write64 |
| 3311 | if (!in_delay_slot) |
| 3312 | generate_update_cycles(block, compiler, desc->pc + 4, 0, TRUE); |
| 3313 | return TRUE; |
| 3314 | |
| 3315 | case 0x0f: /* PREFX */ |
| 3316 | return TRUE; |
| 3317 | |
| 3318 | case 0x20: /* MADD.S - MIPS IV */ |
| 3319 | UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG)); // fsmul f0,<fsreg>,<ftreg> |
| 3320 | UML_FSADD(block, FPR32(FDREG), FREG(0), FPR32(FRREG)); // fsadd <fdreg>,f0,<frreg> |
| 3321 | return TRUE; |
| 3322 | |
| 3323 | case 0x21: /* MADD.D - MIPS IV */ |
| 3324 | UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG)); // fdmul f0,<fsreg>,<ftreg> |
| 3325 | UML_FDADD(block, FPR64(FDREG), FREG(0), FPR64(FRREG)); // fdadd <fdreg>,f0,<frreg> |
| 3326 | return TRUE; |
| 3327 | |
| 3328 | case 0x28: /* MSUB.S - MIPS IV */ |
| 3329 | UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG)); // fsmul f0,<fsreg>,<ftreg> |
| 3330 | UML_FSSUB(block, FPR32(FDREG), FREG(0), FPR32(FRREG)); // fssub <fdreg>,f0,<frreg> |
| 3331 | return TRUE; |
| 3332 | |
| 3333 | case 0x29: /* MSUB.D - MIPS IV */ |
| 3334 | UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG)); // fdmul f0,<fsreg>,<ftreg> |
| 3335 | UML_FDSUB(block, FPR64(FDREG), FREG(0), FPR64(FRREG)); // fdsub <fdreg>,f0,<frreg> |
| 3336 | return TRUE; |
| 3337 | |
| 3338 | case 0x30: /* NMADD.S - MIPS IV */ |
| 3339 | UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG)); // fsmul f0,<fsreg>,<ftreg> |
| 3340 | UML_FSADD(block, FREG(0), FREG(0), FPR32(FRREG)); // fsadd f0,f0,<frreg> |
| 3341 | UML_FSNEG(block, FPR32(FDREG), FREG(0)); // fsneg <fdreg>,f0 |
| 3342 | return TRUE; |
| 3343 | |
| 3344 | case 0x31: /* NMADD.D - MIPS IV */ |
| 3345 | UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG)); // fdmul f0,<fsreg>,<ftreg> |
| 3346 | UML_FDADD(block, FREG(0), FREG(0), FPR64(FRREG)); // fdadd f0,f0,<frreg> |
| 3347 | UML_FDNEG(block, FPR64(FDREG), FREG(0)); // fdneg <fdreg>,f0 |
| 3348 | return TRUE; |
| 3349 | |
| 3350 | case 0x38: /* NMSUB.S - MIPS IV */ |
| 3351 | UML_FSMUL(block, FREG(0), FPR32(FSREG), FPR32(FTREG)); // fsmul f0,<fsreg>,<ftreg> |
| 3352 | UML_FSSUB(block, FPR32(FDREG), FPR32(FRREG), FREG(0)); // fssub <fdreg>,<frreg>,f0 |
| 3353 | return TRUE; |
| 3354 | |
| 3355 | case 0x39: /* NMSUB.D - MIPS IV */ |
| 3356 | UML_FDMUL(block, FREG(0), FPR64(FSREG), FPR64(FTREG)); // fdmul f0,<fsreg>,<ftreg> |
| 3357 | UML_FDSUB(block, FPR64(FDREG), FPR64(FRREG), FREG(0)); // fdsub <fdreg>,<frreg>,f0 |
| 3358 | return TRUE; |
| 3359 | |
| 3360 | default: |
| 3361 | fprintf(stderr, "cop1x %X\n", op); |
| 3362 | break; |
| 3363 | } |
| 3364 | return FALSE; |
| 3365 | } |
| 3366 | |
| 3367 | |
| 3368 | |
| 3369 | /*************************************************************************** |
| 361 | 3370 | CODE LOGGING HELPERS |
| 362 | 3371 | ***************************************************************************/ |
| 363 | 3372 | |
| r2627 | r2628 | |
| 366 | 3375 | including disassembly of a MIPS instruction |
| 367 | 3376 | -------------------------------------------------*/ |
| 368 | 3377 | |
| 369 | | static void log_add_disasm_comment(x86log_context *log, x86code *base, UINT32 pc, UINT32 op) |
| 3378 | static void log_add_disasm_comment(drcuml_block *block, UINT32 pc, UINT32 op) |
| 370 | 3379 | { |
| 371 | | #if (LOG_CODE) |
| 372 | | { |
| 373 | | char buffer[100]; |
| 374 | | dasmmips3(buffer, pc, op); |
| 375 | | x86log_add_comment(log, base, "%08X: %s", pc, buffer); |
| 376 | | } |
| 3380 | #if (LOG_UML) |
| 3381 | char buffer[100]; |
| 3382 | dasmmips3(buffer, pc, op); |
| 3383 | UML_COMMENT(block, "%08X: %s", pc, buffer); // comment |
| 377 | 3384 | #endif |
| 378 | 3385 | } |
| 379 | 3386 | |
| 380 | 3387 | |
| 381 | | #ifdef PTR64 |
| 382 | | |
| 383 | 3388 | /*------------------------------------------------- |
| 384 | | desc_flags_to_string - generate a string |
| 3389 | log_desc_flags_to_string - generate a string |
| 385 | 3390 | representing the instruction description |
| 386 | 3391 | flags |
| 387 | 3392 | -------------------------------------------------*/ |
| 388 | 3393 | |
| 389 | | static const char *desc_flags_to_string(UINT32 flags) |
| 3394 | static const char *log_desc_flags_to_string(UINT32 flags) |
| 390 | 3395 | { |
| 391 | 3396 | static char tempbuf[30]; |
| 392 | 3397 | char *dest = tempbuf; |
| r2627 | r2628 | |
| 440 | 3445 | log_register_list - log a list of GPR registers |
| 441 | 3446 | -------------------------------------------------*/ |
| 442 | 3447 | |
| 443 | | static void log_register_list(x86log_context *log, const char *string, UINT64 gprmask, UINT64 fprmask) |
| 3448 | static void log_register_list(drcuml_state *drcuml, const char *string, UINT64 gprmask, UINT64 fprmask) |
| 444 | 3449 | { |
| 445 | 3450 | int count = 0; |
| 446 | 3451 | int regnum; |
| r2627 | r2628 | |
| 449 | 3454 | if ((gprmask & ~1) == 0 && fprmask == 0) |
| 450 | 3455 | return; |
| 451 | 3456 | |
| 452 | | x86log_printf(log, "[%s:", string); |
| 3457 | drcuml_log_printf(drcuml, "[%s:", string); |
| 453 | 3458 | |
| 454 | 3459 | for (regnum = 1; regnum < 32; regnum++) |
| 455 | 3460 | if (gprmask & ((UINT64)1 << regnum)) |
| 456 | | x86log_printf(log, "%sr%d", (count++ == 0) ? "" : ",", regnum); |
| 3461 | drcuml_log_printf(drcuml, "%sr%d", (count++ == 0) ? "" : ",", regnum); |
| 457 | 3462 | if (gprmask & ((UINT64)1 << REG_LO)) |
| 458 | | x86log_printf(log, "%slo", (count++ == 0) ? "" : ","); |
| 3463 | drcuml_log_printf(drcuml, "%slo", (count++ == 0) ? "" : ","); |
| 459 | 3464 | if (gprmask & ((UINT64)1 << REG_HI)) |
| 460 | | x86log_printf(log, "%shi", (count++ == 0) ? "" : ","); |
| 3465 | drcuml_log_printf(drcuml, "%shi", (count++ == 0) ? "" : ","); |
| 461 | 3466 | |
| 462 | 3467 | for (regnum = 0; regnum < 32; regnum++) |
| 463 | 3468 | if (fprmask & ((UINT64)1 << regnum)) |
| 464 | | x86log_printf(log, "%sfpr%d", (count++ == 0) ? "" : ",", regnum); |
| 3469 | drcuml_log_printf(drcuml, "%sfpr%d", (count++ == 0) ? "" : ",", regnum); |
| 465 | 3470 | if (fprmask & REGFLAG_FCC) |
| 466 | | x86log_printf(log, "%sfcc", (count++ == 0) ? "" : ","); |
| 467 | | x86log_printf(log, "] "); |
| 3471 | drcuml_log_printf(drcuml, "%sfcc", (count++ == 0) ? "" : ","); |
| 3472 | drcuml_log_printf(drcuml, "] "); |
| 468 | 3473 | } |
| 469 | 3474 | |
| 470 | 3475 | |
| r2627 | r2628 | |
| 472 | 3477 | log_opcode_desc - log a list of descriptions |
| 473 | 3478 | -------------------------------------------------*/ |
| 474 | 3479 | |
| 475 | | static void log_opcode_desc(x86log_context *log, const opcode_desc *desclist, int indent) |
| 3480 | static void log_opcode_desc(drcuml_state *drcuml, const opcode_desc *desclist, int indent) |
| 476 | 3481 | { |
| 477 | 3482 | /* open the file, creating it if necessary */ |
| 478 | 3483 | if (indent == 0) |
| 479 | | x86log_printf(log, "\nDescriptor list @ %08X\n", desclist->pc); |
| 3484 | drcuml_log_printf(drcuml, "\nDescriptor list @ %08X\n", desclist->pc); |
| 480 | 3485 | |
| 481 | 3486 | /* output each descriptor */ |
| 482 | 3487 | for ( ; desclist != NULL; desclist = desclist->next) |
| r2627 | r2628 | |
| 484 | 3489 | char buffer[100]; |
| 485 | 3490 | |
| 486 | 3491 | /* disassemle the current instruction and output it to the log */ |
| 487 | | #if LOG_CODE |
| 3492 | #if LOG_UML |
| 488 | 3493 | dasmmips3(buffer, desclist->pc, *desclist->opptr.l); |
| 489 | 3494 | #else |
| 490 | 3495 | strcpy(buffer, "???"); |
| 491 | 3496 | #endif |
| 492 | | x86log_printf(log, "%08X [%08X] t:%08X f:%s: %-30s", desclist->pc, desclist->physpc, desclist->targetpc, desc_flags_to_string(desclist->flags), buffer); |
| 3497 | drcuml_log_printf(drcuml, "%08X [%08X] t:%08X f:%s: %-30s", desclist->pc, desclist->physpc, desclist->targetpc, log_desc_flags_to_string(desclist->flags), buffer); |
| 493 | 3498 | |
| 494 | 3499 | /* output register states */ |
| 495 | | log_register_list(log, "use", desclist->gpr.used, desclist->fpr.used); |
| 496 | | log_register_list(log, "mod", desclist->gpr.modified, desclist->fpr.modified); |
| 497 | | log_register_list(log, "lrd", desclist->gpr.liveread, desclist->fpr.liveread); |
| 498 | | log_register_list(log, "lwr", desclist->gpr.livewrite, desclist->fpr.livewrite); |
| 499 | | x86log_printf(log, "\n"); |
| 3500 | log_register_list(drcuml, "use", desclist->gpr.used, desclist->fpr.used); |
| 3501 | log_register_list(drcuml, "mod", desclist->gpr.modified, desclist->fpr.modified); |
| 3502 | log_register_list(drcuml, "lrd", desclist->gpr.liveread, desclist->fpr.liveread); |
| 3503 | log_register_list(drcuml, "lwr", desclist->gpr.livewrite, desclist->fpr.livewrite); |
| 3504 | drcuml_log_printf(drcuml, "\n"); |
| 500 | 3505 | |
| 501 | 3506 | /* if we have a delay slot, output it recursively */ |
| 502 | 3507 | if (desclist->delay != NULL) |
| 503 | | log_opcode_desc(log, desclist->delay, indent + 1); |
| 3508 | log_opcode_desc(drcuml, desclist->delay, indent + 1); |
| 504 | 3509 | |
| 505 | 3510 | /* at the end of a sequence add a dividing line */ |
| 506 | 3511 | if (desclist->flags & OPFLAG_END_SEQUENCE) |
| 507 | | x86log_printf(log, "-----\n"); |
| 3512 | drcuml_log_printf(drcuml, "-----\n"); |
| 508 | 3513 | } |
| 509 | 3514 | } |
| 510 | | #endif |
| 511 | 3515 | |
| 512 | 3516 | |
| 513 | 3517 | |
| 514 | 3518 | /*************************************************************************** |
| 515 | | RECOMPILER CORE |
| 516 | | ***************************************************************************/ |
| 517 | | |
| 518 | | #ifdef PTR64 |
| 519 | | #include "mdrc64.c" |
| 520 | | #else |
| 521 | | #include "mdrcold.c" |
| 522 | | #endif |
| 523 | | |
| 524 | | |
| 525 | | |
| 526 | | /*************************************************************************** |
| 527 | 3519 | GENERIC GET/SET INFO |
| 528 | 3520 | ***************************************************************************/ |
| 529 | 3521 | |
| r2627 | r2628 | |
| 725 | 3717 | } |
| 726 | 3718 | } |
| 727 | 3719 | |
| 728 | | |
| 729 | 3720 | void r4700le_get_info(UINT32 state, cpuinfo *info) |
| 730 | 3721 | { |
| 731 | 3722 | switch (state) |
| r2627 | r2628 | |
| 913 | 3904 | DISASSEMBLERS |
| 914 | 3905 | ***************************************************************************/ |
| 915 | 3906 | |
| 916 | | #if !defined(ENABLE_DEBUGGER) && (LOG_CODE) |
| 3907 | #if !defined(MAME_DEBUG) && (LOG_UML) |
| 917 | 3908 | #include "mips3dsm.c" |
| 918 | 3909 | #endif |
| 919 | 3910 | |
| 920 | | |
| 921 | | |
| 922 | | /*************************************************************************** |
| 923 | | C CORE FOR COMPARISON |
| 924 | | ***************************************************************************/ |
| 925 | | |
| 926 | | #if COMPARE_AGAINST_C |
| 927 | | |
| 928 | | static UINT8 dummy_read_byte_32le(offs_t offs) |
| 929 | | { |
| 930 | | if (mips3.access_type == 0) |
| 931 | | { |
| 932 | | mips3.access_type = 1; |
| 933 | | mips3.access_size = 1; |
| 934 | | mips3.access_addr = offs; |
| 935 | | mips3.access_mask = 0; |
| 936 | | mips3.access_data = program_read_byte_32le(offs); |
| 937 | | } |
| 938 | | else |
| 939 | | { |
| 940 | | assert(mips3.access_type == 1); |
| 941 | | assert(mips3.access_size == 1); |
| 942 | | assert(mips3.access_addr == offs); |
| 943 | | assert(mips3.access_mask == 0); |
| 944 | | mips3.access_type = 0; |
| 945 | | } |
| 946 | | return mips3.access_data; |
| 947 | | } |
| 948 | | |
| 949 | | static UINT16 dummy_read_word_32le(offs_t offs) |
| 950 | | { |
| 951 | | if (mips3.access_type == 0) |
| 952 | | { |
| 953 | | mips3.access_type = 1; |
| 954 | | mips3.access_size = 2; |
| 955 | | mips3.access_addr = offs; |
| 956 | | mips3.access_mask = 0; |
| 957 | | mips3.access_data = program_read_word_32le(offs); |
| 958 | | } |
| 959 | | else |
| 960 | | { |
| 961 | | assert(mips3.access_type == 1); |
| 962 | | assert(mips3.access_size == 2); |
| 963 | | assert(mips3.access_addr == offs); |
| 964 | | assert(mips3.access_mask == 0); |
| 965 | | mips3.access_type = 0; |
| 966 | | } |
| 967 | | return mips3.access_data; |
| 968 | | } |
| 969 | | |
| 970 | | static UINT32 dummy_read_dword_32le(offs_t offs) |
| 971 | | { |
| 972 | | if (mips3.access_type == 0) |
| 973 | | { |
| 974 | | mips3.access_type = 1; |
| 975 | | mips3.access_size = 4; |
| 976 | | mips3.access_addr = offs; |
| 977 | | mips3.access_mask = 0; |
| 978 | | mips3.access_data = program_read_dword_32le(offs); |
| 979 | | } |
| 980 | | else |
| 981 | | { |
| 982 | | assert(mips3.access_type == 1); |
| 983 | | assert(mips3.access_size == 4); |
| 984 | | assert(mips3.access_addr == offs); |
| 985 | | assert(mips3.access_mask == 0); |
| 986 | | mips3.access_type = 0; |
| 987 | | } |
| 988 | | return mips3.access_data; |
| 989 | | } |
| 990 | | |
| 991 | | static UINT32 dummy_read_masked_32le(offs_t offs, UINT32 mask) |
| 992 | | { |
| 993 | | if (mips3.access_type == 0) |
| 994 | | { |
| 995 | | mips3.access_type = 1; |
| 996 | | mips3.access_size = 4; |
| 997 | | mips3.access_addr = offs; |
| 998 | | mips3.access_mask = mask; |
| 999 | | mips3.access_data = program_read_masked_32le(offs, mask); |
| 1000 | | } |
| 1001 | | else |
| 1002 | | { |
| 1003 | | assert(mips3.access_type == 1); |
| 1004 | | assert(mips3.access_size == 4); |
| 1005 | | assert(mips3.access_addr == offs); |
| 1006 | | assert(mips3.access_mask == mask); |
| 1007 | | mips3.access_type = 0; |
| 1008 | | } |
| 1009 | | return mips3.access_data; |
| 1010 | | } |
| 1011 | | |
| 1012 | | static UINT64 dummy_read_qword_32le(offs_t offs) |
| 1013 | | { |
| 1014 | | if (mips3.access_type == 0) |
| 1015 | | { |
| 1016 | | mips3.access_type = 1; |
| 1017 | | mips3.access_size = 8; |
| 1018 | | mips3.access_addr = offs; |
| 1019 | | mips3.access_mask = 0; |
| 1020 | | mips3.access_data = (*mips3.orighandler.readdouble)(offs); |
| 1021 | | } |
| 1022 | | else |
| 1023 | | { |
| 1024 | | assert(mips3.access_type == 1); |
| 1025 | | assert(mips3.access_size == 8); |
| 1026 | | assert(mips3.access_addr == offs); |
| 1027 | | assert(mips3.access_mask == 0); |
| 1028 | | mips3.access_type = 0; |
| 1029 | | } |
| 1030 | | return mips3.access_data; |
| 1031 | | } |
| 1032 | | |
| 1033 | | static UINT64 dummy_read_qword_masked_32le(offs_t offs, UINT64 mask) |
| 1034 | | { |
| 1035 | | if (mips3.access_type == 0) |
| 1036 | | { |
| 1037 | | mips3.access_type = 1; |
| 1038 | | mips3.access_size = 8; |
| 1039 | | mips3.access_addr = offs; |
| 1040 | | mips3.access_mask = mask; |
| 1041 | | mips3.access_data = (*mips3.orighandler.readdouble_masked)(offs, mask); |
| 1042 | | } |
| 1043 | | else |
| 1044 | | { |
| 1045 | | assert(mips3.access_type == 1); |
| 1046 | | assert(mips3.access_size == 8); |
| 1047 | | assert(mips3.access_addr == offs); |
| 1048 | | assert(mips3.access_mask == mask); |
| 1049 | | mips3.access_type = 0; |
| 1050 | | } |
| 1051 | | return mips3.access_data; |
| 1052 | | } |
| 1053 | | |
| 1054 | | static void dummy_write_byte_32le(offs_t offs, UINT8 data) |
| 1055 | | { |
| 1056 | | if (mips3.access_type == 0) |
| 1057 | | { |
| 1058 | | mips3.access_type = 2; |
| 1059 | | mips3.access_size = 1; |
| 1060 | | mips3.access_addr = offs; |
| 1061 | | mips3.access_data = data; |
| 1062 | | mips3.access_mask = 0; |
| 1063 | | } |
| 1064 | | else |
| 1065 | | { |
| 1066 | | assert(mips3.access_type == 2); |
| 1067 | | assert(mips3.access_size == 1); |
| 1068 | | assert(mips3.access_addr == offs); |
| 1069 | | assert(mips3.access_data == data); |
| 1070 | | assert(mips3.access_mask == 0); |
| 1071 | | mips3.access_type = 0; |
| 1072 | | program_write_byte_32le(offs, data); |
| 1073 | | } |
| 1074 | | } |
| 1075 | | |
| 1076 | | static void dummy_write_word_32le(offs_t offs, UINT16 data) |
| 1077 | | { |
| 1078 | | if (mips3.access_type == 0) |
| 1079 | | { |
| 1080 | | mips3.access_type = 2; |
| 1081 | | mips3.access_size = 2; |
| 1082 | | mips3.access_addr = offs; |
| 1083 | | mips3.access_data = data; |
| 1084 | | mips3.access_mask = 0; |
| 1085 | | } |
| 1086 | | else |
| 1087 | | { |
| 1088 | | assert(mips3.access_type == 2); |
| 1089 | | assert(mips3.access_size == 2); |
| 1090 | | assert(mips3.access_addr == offs); |
| 1091 | | assert(mips3.access_data == data); |
| 1092 | | assert(mips3.access_mask == 0); |
| 1093 | | mips3.access_type = 0; |
| 1094 | | program_write_word_32le(offs, data); |
| 1095 | | } |
| 1096 | | } |
| 1097 | | |
| 1098 | | static void dummy_write_dword_32le(offs_t offs, UINT32 data) |
| 1099 | | { |
| 1100 | | if (mips3.access_type == 0) |
| 1101 | | { |
| 1102 | | mips3.access_type = 2; |
| 1103 | | mips3.access_size = 4; |
| 1104 | | mips3.access_addr = offs; |
| 1105 | | mips3.access_data = data; |
| 1106 | | mips3.access_mask = 0; |
| 1107 | | } |
| 1108 | | else |
| 1109 | | { |
| 1110 | | assert(mips3.access_type == 2); |
| 1111 | | assert(mips3.access_size == 4); |
| 1112 | | assert(mips3.access_addr == offs); |
| 1113 | | assert(mips3.access_data == data); |
| 1114 | | assert(mips3.access_mask == 0); |
| 1115 | | mips3.access_type = 0; |
| 1116 | | program_write_dword_32le(offs, data); |
| 1117 | | } |
| 1118 | | } |
| 1119 | | |
| 1120 | | static void dummy_write_masked_32le(offs_t offs, UINT32 data, UINT32 mask) |
| 1121 | | { |
| 1122 | | if (mips3.access_type == 0) |
| 1123 | | { |
| 1124 | | mips3.access_type = 2; |
| 1125 | | mips3.access_size = 4; |
| 1126 | | mips3.access_addr = offs; |
| 1127 | | mips3.access_data = data; |
| 1128 | | mips3.access_mask = mask; |
| 1129 | | } |
| 1130 | | else |
| 1131 | | { |
| 1132 | | assert(mips3.access_type == 2); |
| 1133 | | assert(mips3.access_size == 4); |
| 1134 | | assert(mips3.access_addr == offs); |
| 1135 | | assert(mips3.access_data == data); |
| 1136 | | assert(mips3.access_mask == mask); |
| 1137 | | mips3.access_type = 0; |
| 1138 | | program_write_masked_32le(offs, data, mask); |
| 1139 | | } |
| 1140 | | } |
| 1141 | | |
| 1142 | | static void dummy_write_qword_32le(offs_t offs, UINT64 data) |
| 1143 | | { |
| 1144 | | if (mips3.access_type == 0) |
| 1145 | | { |
| 1146 | | mips3.access_type = 2; |
| 1147 | | mips3.access_size = 8; |
| 1148 | | mips3.access_addr = offs; |
| 1149 | | mips3.access_data = data; |
| 1150 | | mips3.access_mask = 0; |
| 1151 | | } |
| 1152 | | else |
| 1153 | | { |
| 1154 | | assert(mips3.access_type == 2); |
| 1155 | | assert(mips3.access_size == 8); |
| 1156 | | assert(mips3.access_addr == offs); |
| 1157 | | assert(mips3.access_data == data); |
| 1158 | | assert(mips3.access_mask == 0); |
| 1159 | | mips3.access_type = 0; |
| 1160 | | (*mips3.orighandler.writedouble)(offs, data); |
| 1161 | | } |
| 1162 | | } |
| 1163 | | |
| 1164 | | static void dummy_write_qword_masked_32le(offs_t offs, UINT64 data, UINT64 mask) |
| 1165 | | { |
| 1166 | | if (mips3.access_type == 0) |
| 1167 | | { |
| 1168 | | mips3.access_type = 2; |
| 1169 | | mips3.access_size = 8; |
| 1170 | | mips3.access_addr = offs; |
| 1171 | | mips3.access_data = data; |
| 1172 | | mips3.access_mask = mask; |
| 1173 | | } |
| 1174 | | else |
| 1175 | | { |
| 1176 | | assert(mips3.access_type == 2); |
| 1177 | | assert(mips3.access_size == 8); |
| 1178 | | assert(mips3.access_addr == offs); |
| 1179 | | assert(mips3.access_data == data); |
| 1180 | | assert(mips3.access_mask == mask); |
| 1181 | | mips3.access_type = 0; |
| 1182 | | (*mips3.orighandler.writedouble_masked)(offs, data, mask); |
| 1183 | | } |
| 1184 | | } |
| 1185 | | |
| 1186 | | static const memory_handlers override_memory = |
| 1187 | | { |
| 1188 | | dummy_read_byte_32le, |
| 1189 | | dummy_read_word_32le, |
| 1190 | | dummy_read_dword_32le, |
| 1191 | | dummy_read_masked_32le, |
| 1192 | | dummy_read_qword_32le, |
| 1193 | | dummy_read_qword_masked_32le, |
| 1194 | | |
| 1195 | | dummy_write_byte_32le, |
| 1196 | | dummy_write_word_32le, |
| 1197 | | dummy_write_dword_32le, |
| 1198 | | dummy_write_masked_32le, |
| 1199 | | dummy_write_qword_32le, |
| 1200 | | dummy_write_qword_masked_32le |
| 1201 | | }; |
| 1202 | | |
| 1203 | | #undef SR |
| 1204 | | |
| 1205 | | #define mips3 c_mips3 |
| 1206 | | #define mips3_regs c_mips3_regs |
| 1207 | | |
| 1208 | | #define mips3_get_context c_mips3_get_context |
| 1209 | | #define mips3_set_context c_mips3_set_context |
| 1210 | | #define mips3_reset c_mips3_reset |
| 1211 | | #define mips3_translate c_mips3_translate |
| 1212 | | #define mips3_dasm c_mips3_dasm |
| 1213 | | #define mips3_execute c_mips3_execute |
| 1214 | | #define mips3_set_info c_mips3_set_info |
| 1215 | | #define mips3_get_info c_mips3_get_info |
| 1216 | | |
| 1217 | | #define r4600be_init c_r4600be_init |
| 1218 | | #define r4600le_init c_r4600le_init |
| 1219 | | #define r4600be_get_info c_r4600be_get_info |
| 1220 | | #define r4600le_get_info c_r4600le_get_info |
| 1221 | | |
| 1222 | | #define r4650be_init c_r4650be_init |
| 1223 | | #define r4650le_init c_r4650le_init |
| 1224 | | #define r4650be_get_info c_r4650be_get_info |
| 1225 | | #define r4650le_get_info c_r4650le_get_info |
| 1226 | | |
| 1227 | | #define r4700be_init c_r4700be_init |
| 1228 | | #define r4700le_init c_r4700le_init |
| 1229 | | #define r4700be_get_info c_r4700be_get_info |
| 1230 | | #define r4700le_get_info c_r4700le_get_info |
| 1231 | | |
| 1232 | | #define r5000be_init c_r5000be_init |
| 1233 | | #define r5000le_init c_r5000le_init |
| 1234 | | #define r5000be_get_info c_r5000be_get_info |
| 1235 | | #define r5000le_get_info c_r5000le_get_info |
| 1236 | | |
| 1237 | | #define qed5271be_init c_qed5271be_init |
| 1238 | | #define qed5271le_init c_qed5271le_init |
| 1239 | | #define qed5271be_get_info c_qed5271be_get_info |
| 1240 | | #define qed5271le_get_info c_qed5271le_get_info |
| 1241 | | |
| 1242 | | #define rm7000be_init c_rm7000be_init |
| 1243 | | #define rm7000le_init c_rm7000le_init |
| 1244 | | #define rm7000be_get_info c_rm7000be_get_info |
| 1245 | | #define rm7000le_get_info c_rm7000le_get_info |
| 1246 | | |
| 1247 | | #include "mips3.c" |
| 1248 | | |
| 1249 | | #undef mips3 |
| 1250 | | |
| 1251 | | static void mips3c_init(mips3_flavor flavor, int bigendian, int index, int clock, const struct mips3_config *config, int (*irqcallback)(int)) |
| 1252 | | { |
| 1253 | | size_t memsize = mips3com_init(&c_mips3.core, flavor, bigendian, index, clock, config, irqcallback, NULL); |
| 1254 | | void *memory = auto_malloc(memsize); |
| 1255 | | mips3com_init(&c_mips3.core, flavor, bigendian, index, clock, config, irqcallback, memory); |
| 1256 | | mips3.orighandler = mips3.core->memory; |
| 1257 | | mips3.core->memory = override_memory; |
| 1258 | | c_mips3.core.memory = override_memory; |
| 1259 | | } |
| 1260 | | |
| 1261 | | static void mips3c_reset(void) |
| 1262 | | { |
| 1263 | | c_mips3_reset(); |
| 1264 | | } |
| 1265 | | |
| 1266 | | static void execute_c_version(void) |
| 1267 | | { |
| 1268 | | int save_debug_mode = Machine->debug_mode; |
| 1269 | | int i; |
| 1270 | | |
| 1271 | | assert(mips3.access_type == 0); |
| 1272 | | |
| 1273 | | c_mips3.core.cpr[0][COP0_Cause] = (c_mips3.core.cpr[0][COP0_Cause] & ~0xfc00) | (mips3.core->cpr[0][COP0_Cause] & 0xfc00); |
| 1274 | | check_irqs(); |
| 1275 | | |
| 1276 | | assert(c_mips3.core.pc == mips3.core->pc); |
| 1277 | | assert(c_mips3.core.r[REG_HI] == mips3.core->r[REG_HI]); |
| 1278 | | assert(c_mips3.core.r[REG_LO] == mips3.core->r[REG_LO]); |
| 1279 | | for (i = 0; i < 32; i++) |
| 1280 | | { |
| 1281 | | assert(c_mips3.core.r[i] == mips3.core->r[i]); |
| 1282 | | assert(c_mips3.core.cpr[0][i] == mips3.core->cpr[0][i]); |
| 1283 | | assert(c_mips3.core.ccr[0][i] == mips3.core->ccr[0][i]); |
| 1284 | | assert(c_mips3.core.cpr[1][i] == mips3.core->cpr[1][i]); |
| 1285 | | assert(c_mips3.core.ccr[1][i] == mips3.core->ccr[1][i]); |
| 1286 | | } |
| 1287 | | for (i = 0; i < 8; i++) |
| 1288 | | assert(c_mips3.core.cf[1][i] == mips3.core->cf[1][i]); |
| 1289 | | assert(c_mips3.core.count_zero_time == mips3.core->count_zero_time); |
| 1290 | | |
| 1291 | | mips3.access_type = 0; |
| 1292 | | |
| 1293 | | Machine->debug_mode = 0; |
| 1294 | | c_mips3_execute(0); |
| 1295 | | Machine->debug_mode = save_debug_mode; |
| 1296 | | } |
| 1297 | | |
| 1298 | | static void compare_c_version(void) |
| 1299 | | { |
| 1300 | | } |
| 1301 | | |
| 1302 | | #endif |
src/emu/cpu/drcbex86.c
| r0 | r2628 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | drcbex86.c |
| 4 | |
| 5 | 32-bit x86 back-end for the universal machine language. |
| 6 | |
| 7 | Copyright Aaron Giles |
| 8 | Released for general non-commercial use under the MAME license |
| 9 | Visit http://mamedev.org for licensing and usage restrictions. |
| 10 | |
| 11 | **************************************************************************** |
| 12 | |
| 13 | Future improvements/changes: |
| 14 | |
| 15 | * Optimize to avoid unnecessary reloads |
| 16 | - especially EDX for 64-bit operations |
| 17 | - also FCMP/FLAGS has unnecessary PUSHF/POP EAX |
| 18 | |
| 19 | * Identify common pairs and optimize output |
| 20 | |
| 21 | **************************************************************************** |
| 22 | |
| 23 | --------------- |
| 24 | ABI/conventions |
| 25 | --------------- |
| 26 | |
| 27 | Registers: |
| 28 | EAX - volatile, function return value |
| 29 | EBX - non-volatile |
| 30 | ECX - volatile |
| 31 | EDX - volatile, function return value (upper 32 bits) |
| 32 | ESI - non-volatile |
| 33 | EDI - non-volatile |
| 34 | EBP - non-volatile |
| 35 | |
| 36 | FP stack - volatile |
| 37 | |
| 38 | |
| 39 | --------------- |
| 40 | Execution model |
| 41 | --------------- |
| 42 | |
| 43 | Registers: |
| 44 | EAX - scratch register |
| 45 | EBX - maps to I0 (low 32 bits) |
| 46 | ECX - scratch register |
| 47 | EDX - scratch register |
| 48 | ESI - maps to I1 (low 32 bits) |
| 49 | EDI - maps to I2 (low 32 bits) |
| 50 | EBP - maps to I3 (low 32 bits) |
| 51 | |
| 52 | FP stack - scratch registers |
| 53 | |
| 54 | Entry point: |
| 55 | Assumes 1 parameter passed, which is the codeptr of the code |
| 56 | to execute once the environment is set up. |
| 57 | |
| 58 | Exit point: |
| 59 | Assumes exit value is in EAX. |
| 60 | |
| 61 | Entry stack: |
| 62 | [esp] - return |
| 63 | [esp+4] - input parameter (entry handle) |
| 64 | |
| 65 | Runtime stack: |
| 66 | [esp] - temporary space for hash jump |
| 67 | [esp+4] - saved ebp |
| 68 | [esp+8] - saved edi |
| 69 | [esp+12] - saved esi |
| 70 | [esp+16] - saved ebx |
| 71 | [esp+20] - ret |
| 72 | [esp+24] - input parameter (entry handle) |
| 73 | |
| 74 | ***************************************************************************/ |
| 75 | |
| 76 | #include "drcuml.h" |
| 77 | #include "drcumld.h" |
| 78 | #include "drcbeut.h" |
| 79 | #include "debugger.h" |
| 80 | #include "x86emit.h" |
| 81 | #undef REG_SP |
| 82 | #include "x86log.h" |
| 83 | #include <math.h> |
| 84 | |
| 85 | |
| 86 | |
| 87 | /*************************************************************************** |
| 88 | DEBUGGING |
| 89 | ***************************************************************************/ |
| 90 | |
| 91 | #define LOG_HASHJMPS (0) |
| 92 | |
| 93 | |
| 94 | |
| 95 | /*************************************************************************** |
| 96 | CONSTANTS |
| 97 | ***************************************************************************/ |
| 98 | |
| 99 | #define PTYPE_M (1 << DRCUML_PTYPE_MEMORY) |
| 100 | #define PTYPE_I (1 << DRCUML_PTYPE_IMMEDIATE) |
| 101 | #define PTYPE_R (1 << DRCUML_PTYPE_INT_REGISTER) |
| 102 | #define PTYPE_F (1 << DRCUML_PTYPE_FLOAT_REGISTER) |
| 103 | #define PTYPE_MI (PTYPE_M | PTYPE_I) |
| 104 | #define PTYPE_RI (PTYPE_R | PTYPE_I) |
| 105 | #define PTYPE_MR (PTYPE_M | PTYPE_R) |
| 106 | #define PTYPE_MRI (PTYPE_M | PTYPE_R | PTYPE_I) |
| 107 | #define PTYPE_MF (PTYPE_M | PTYPE_F) |
| 108 | |
| 109 | |
| 110 | |
| 111 | /*************************************************************************** |
| 112 | MACROS |
| 113 | ***************************************************************************/ |
| 114 | |
| 115 | #define X86_CONDITION(condflags) (condition_map[condflags - DRCUML_COND_Z]) |
| 116 | #define X86_NOT_CONDITION(condflags) (condition_map[condflags - DRCUML_COND_Z] ^ 1) |
| 117 | |
| 118 | |
| 119 | |
| 120 | /*************************************************************************** |
| 121 | TYPE DEFINITIONS |
| 122 | ***************************************************************************/ |
| 123 | |
| 124 | /* entry point */ |
| 125 | typedef UINT32 (*x86_entry_point_func)(x86code *entry); |
| 126 | |
| 127 | /* opcode handler */ |
| 128 | typedef x86code *(*opcode_generate_func)(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 129 | |
| 130 | |
| 131 | /* opcode table entry */ |
| 132 | typedef struct _opcode_table_entry opcode_table_entry; |
| 133 | struct _opcode_table_entry |
| 134 | { |
| 135 | drcuml_opcode opcode; /* opcode in question */ |
| 136 | opcode_generate_func func; /* function pointer to the work */ |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | /* internal backend-specific state */ |
| 141 | struct _drcbe_state |
| 142 | { |
| 143 | drcuml_state * drcuml; /* pointer back to our owner */ |
| 144 | drccache * cache; /* pointer to the cache */ |
| 145 | drcuml_machine_state state; /* state of the machine */ |
| 146 | drchash_state * hash; /* hash table state */ |
| 147 | drcmap_state * map; /* code map */ |
| 148 | drclabel_list * labels; /* label list */ |
| 149 | |
| 150 | x86_entry_point_func entry; /* entry point */ |
| 151 | x86code * exit; /* exit point */ |
| 152 | x86code * nocode; /* nocode handler */ |
| 153 | |
| 154 | UINT32 * reglo[REG_MAX]; /* pointer to low part of data for each register */ |
| 155 | UINT32 * reghi[REG_MAX]; /* pointer to high part of data for each register */ |
| 156 | double fptemp; /* temporary storage for floating point */ |
| 157 | |
| 158 | UINT16 fpumode; /* saved FPU mode */ |
| 159 | UINT16 fmodesave; /* temporary location for saving */ |
| 160 | |
| 161 | void * stacksave; /* saved stack pointer */ |
| 162 | void * hashstacksave; /* saved stack pointer for hashjmp */ |
| 163 | UINT64 reslo; /* extended low result */ |
| 164 | UINT64 reshi; /* extended high result */ |
| 165 | |
| 166 | x86log_context * log; /* logging */ |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | |
| 171 | /*************************************************************************** |
| 172 | FUNCTION PROTOTYPES |
| 173 | ***************************************************************************/ |
| 174 | |
| 175 | /* primary back-end callbacks */ |
| 176 | static drcbe_state *drcbex86_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits); |
| 177 | static void drcbex86_free(drcbe_state *drcbe); |
| 178 | static void drcbex86_reset(drcbe_state *drcbe); |
| 179 | static int drcbex86_execute(drcbe_state *drcbe, drcuml_codehandle *entry); |
| 180 | static void drcbex86_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst); |
| 181 | static int drcbex86_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc); |
| 182 | |
| 183 | /* private helper functions */ |
| 184 | static void fixup_label(void *parameter, drccodeptr labelcodeptr); |
| 185 | |
| 186 | /* miscellaneous functions */ |
| 187 | static void dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2); |
| 188 | static void dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2); |
| 189 | static void ddivu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2); |
| 190 | static void ddivs(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2); |
| 191 | |
| 192 | |
| 193 | |
| 194 | /*************************************************************************** |
| 195 | GLOBAL VARIABLES |
| 196 | ***************************************************************************/ |
| 197 | |
| 198 | /* globally-accessible interface to the backend */ |
| 199 | const drcbe_interface drcbe_x86_be_interface = |
| 200 | { |
| 201 | drcbex86_alloc, |
| 202 | drcbex86_free, |
| 203 | drcbex86_reset, |
| 204 | drcbex86_execute, |
| 205 | drcbex86_generate, |
| 206 | drcbex86_hash_exists |
| 207 | }; |
| 208 | |
| 209 | /* opcode table */ |
| 210 | static opcode_generate_func opcode_table[DRCUML_OP_MAX]; |
| 211 | |
| 212 | /* size-to-mask table */ |
| 213 | static const UINT64 size_to_mask[] = { 0, 0xff, 0xffff, 0, 0xffffffff, 0, 0, 0, U64(0xffffffffffffffff) }; |
| 214 | |
| 215 | /* register mapping tables */ |
| 216 | static UINT8 int_register_map[DRCUML_REG_I_END - DRCUML_REG_I0] = |
| 217 | { |
| 218 | REG_EBX, REG_ESI, REG_EDI, REG_EBP |
| 219 | }; |
| 220 | |
| 221 | /* flags mapping tables */ |
| 222 | static UINT8 flags_map[0x1000]; |
| 223 | |
| 224 | /* condition mapping table */ |
| 225 | static UINT8 condition_map[DRCUML_COND_MAX - DRCUML_COND_Z] = |
| 226 | { |
| 227 | COND_Z, /* DRCUML_COND_Z = 0x80, requires Z */ |
| 228 | COND_NZ, /* DRCUML_COND_NZ, requires Z */ |
| 229 | COND_S, /* DRCUML_COND_S, requires S */ |
| 230 | COND_NS, /* DRCUML_COND_NS, requires S */ |
| 231 | COND_C, /* DRCUML_COND_C, requires C */ |
| 232 | COND_NC, /* DRCUML_COND_NC, requires C */ |
| 233 | COND_O, /* DRCUML_COND_V, requires V */ |
| 234 | COND_NO, /* DRCUML_COND_NV, requires V */ |
| 235 | COND_P, /* DRCUML_COND_U, requires U */ |
| 236 | COND_NP, /* DRCUML_COND_NU, requires U */ |
| 237 | COND_A, /* DRCUML_COND_A, requires CZ */ |
| 238 | COND_BE, /* DRCUML_COND_BE, requires CZ */ |
| 239 | COND_G, /* DRCUML_COND_G, requires SVZ */ |
| 240 | COND_LE, /* DRCUML_COND_LE, requires SVZ */ |
| 241 | COND_L, /* DRCUML_COND_L, requires SV */ |
| 242 | COND_GE, /* DRCUML_COND_GE, requires SV */ |
| 243 | }; |
| 244 | |
| 245 | /* FPU control register mapping */ |
| 246 | static const UINT16 fp_control[4] = |
| 247 | { |
| 248 | 0x0e3f, /* DRCUML_FMOD_TRUNC */ |
| 249 | 0x023f, /* DRCUML_FMOD_ROUND */ |
| 250 | 0x0a3f, /* DRCUML_FMOD_CEIL */ |
| 251 | 0x063f /* DRCUML_FMOD_FLOOR */ |
| 252 | }; |
| 253 | |
| 254 | |
| 255 | |
| 256 | /*************************************************************************** |
| 257 | TABLES |
| 258 | ***************************************************************************/ |
| 259 | |
| 260 | static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 261 | static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 262 | static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 263 | static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 264 | static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 265 | |
| 266 | static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 267 | static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 268 | static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 269 | static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 270 | static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 271 | static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 272 | static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 273 | static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 274 | static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 275 | |
| 276 | static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 277 | static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 278 | static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 279 | |
| 280 | static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 281 | static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 282 | static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 283 | static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 284 | static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 285 | static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 286 | static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 287 | static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 288 | static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 289 | static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 290 | static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 291 | static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 292 | static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 293 | static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 294 | static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 295 | static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 296 | static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 297 | static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 298 | static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 299 | static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 300 | static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 301 | static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 302 | static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 303 | static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 304 | static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 305 | static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 306 | static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 307 | static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 308 | static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 309 | static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 310 | static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 311 | static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 312 | static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 313 | static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 314 | static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 315 | static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 316 | static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 317 | static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 318 | static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 319 | static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 320 | static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 321 | static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 322 | static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 323 | static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 324 | static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 325 | static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 326 | static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 327 | static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 328 | static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 329 | static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 330 | static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 331 | static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 332 | static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 333 | static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 334 | static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 335 | static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 336 | |
| 337 | static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 338 | static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 339 | static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 340 | static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 341 | static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 342 | static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 343 | static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 344 | static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 345 | static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 346 | static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 347 | static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 348 | static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 349 | static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 350 | static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 351 | static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 352 | static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 353 | static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 354 | static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 355 | static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 356 | static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 357 | static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 358 | static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 359 | static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 360 | static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 361 | static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 362 | static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 363 | static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 364 | static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 365 | static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst); |
| 366 | |
| 367 | static const opcode_table_entry opcode_table_source[] = |
| 368 | { |
| 369 | /* Compile-time opcodes */ |
| 370 | { DRCUML_OP_HANDLE, op_handle }, /* HANDLE handle */ |
| 371 | { DRCUML_OP_HASH, op_hash }, /* HASH mode,pc */ |
| 372 | { DRCUML_OP_LABEL, op_label }, /* LABEL imm */ |
| 373 | { DRCUML_OP_COMMENT, op_comment }, /* COMMENT string */ |
| 374 | { DRCUML_OP_MAPVAR, op_mapvar }, /* MAPVAR mapvar,value */ |
| 375 | |
| 376 | /* Control Flow Operations */ |
| 377 | { DRCUML_OP_DEBUG, op_debug }, /* DEBUG pc */ |
| 378 | { DRCUML_OP_EXIT, op_exit }, /* EXIT src1[,c] */ |
| 379 | { DRCUML_OP_HASHJMP, op_hashjmp }, /* HASHJMP mode,pc,handle */ |
| 380 | { DRCUML_OP_JMP, op_jmp }, /* JMP imm[,c] */ |
| 381 | { DRCUML_OP_EXH, op_exh }, /* EXH handle,param[,c] */ |
| 382 | { DRCUML_OP_CALLH, op_callh }, /* CALLH handle[,c] */ |
| 383 | { DRCUML_OP_RET, op_ret }, /* RET [c] */ |
| 384 | { DRCUML_OP_CALLC, op_callc }, /* CALLC func,ptr[,c] */ |
| 385 | { DRCUML_OP_RECOVER, op_recover }, /* RECOVER dst,mapvar */ |
| 386 | |
| 387 | /* Internal Register Operations */ |
| 388 | { DRCUML_OP_SETFMOD, op_setfmod }, /* SETFMOD src */ |
| 389 | { DRCUML_OP_GETFMOD, op_getfmod }, /* GETFMOD dst */ |
| 390 | { DRCUML_OP_GETEXP, op_getexp }, /* GETEXP dst,index */ |
| 391 | |
| 392 | /* Integer Operations */ |
| 393 | { DRCUML_OP_LOAD1U, op_load1u }, /* LOAD1U dst,base,index */ |
| 394 | { DRCUML_OP_LOAD1S, op_load1s }, /* LOAD1S dst,base,index */ |
| 395 | { DRCUML_OP_LOAD2U, op_load2u }, /* LOAD2U dst,base,index */ |
| 396 | { DRCUML_OP_LOAD2S, op_load2s }, /* LOAD2S dst,base,index */ |
| 397 | { DRCUML_OP_LOAD4U, op_load4u }, /* LOAD4U dst,base,index */ |
| 398 | { DRCUML_OP_LOAD4S, op_load4s }, /* LOAD4S dst,base,index */ |
| 399 | { DRCUML_OP_LOAD8U, op_load8u }, /* LOAD8U dst,base,index */ |
| 400 | { DRCUML_OP_STORE1, op_store1 }, /* STORE1 base,index,src */ |
| 401 | { DRCUML_OP_STORE2, op_store2 }, /* STORE2 base,index,src */ |
| 402 | { DRCUML_OP_STORE4, op_store4 }, /* STORE4 base,index,src */ |
| 403 | { DRCUML_OP_STORE8, op_store8 }, /* STORE8 base,index,src */ |
| 404 | { DRCUML_OP_READ1U, op_read1u }, /* READ1U dst,space,src1 */ |
| 405 | { DRCUML_OP_READ1S, op_read1s }, /* READ1S dst,space,src1 */ |
| 406 | { DRCUML_OP_READ2U, op_read2u }, /* READ2U dst,space,src1 */ |
| 407 | { DRCUML_OP_READ2S, op_read2s }, /* READ2S dst,space,src1 */ |
| 408 | { DRCUML_OP_READ2M, op_read2m }, /* READ2M dst,space,src1,mask */ |
| 409 | { DRCUML_OP_READ4U, op_read4u }, /* READ4U dst,space,src1 */ |
| 410 | { DRCUML_OP_READ4S, op_read4s }, /* READ4S dst,space,src1 */ |
| 411 | { DRCUML_OP_READ4M, op_read4m }, /* READ4M dst,space,src1,mask */ |
| 412 | { DRCUML_OP_READ8U, op_read8u }, /* READ8U dst,space,src1 */ |
| 413 | { DRCUML_OP_READ8M, op_read8m }, /* READ8M dst,space,src1,mask */ |
| 414 | { DRCUML_OP_WRITE1, op_write1 }, /* WRITE1 space,dst,src1 */ |
| 415 | { DRCUML_OP_WRITE2, op_write2 }, /* WRITE2 space,dst,src1 */ |
| 416 | { DRCUML_OP_WRIT2M, op_writ2m }, /* WRIT2M space,dst,src1 */ |
| 417 | { DRCUML_OP_WRITE4, op_write4 }, /* WRITE4 space,dst,src1 */ |
| 418 | { DRCUML_OP_WRIT4M, op_writ4m }, /* WRIT4M space,dst,mask,src1 */ |
| 419 | { DRCUML_OP_WRITE8, op_write8 }, /* WRITE8 space,dst,src1 */ |
| 420 | { DRCUML_OP_WRIT8M, op_writ8m }, /* WRIT8M space,dst,mask,src1 */ |
| 421 | { DRCUML_OP_FLAGS, op_flags }, /* FLAGS dst,mask,table */ |
| 422 | { DRCUML_OP_MOV, op_mov }, /* MOV dst,src[,c] */ |
| 423 | { DRCUML_OP_ZEXT1, op_zext1 }, /* ZEXT1 dst,src */ |
| 424 | { DRCUML_OP_ZEXT2, op_zext2 }, /* ZEXT2 dst,src */ |
| 425 | { DRCUML_OP_ZEXT4, op_zext4 }, /* ZEXT4 dst,src */ |
| 426 | { DRCUML_OP_SEXT1, op_sext1 }, /* SEXT1 dst,src */ |
| 427 | { DRCUML_OP_SEXT2, op_sext2 }, /* SEXT2 dst,src */ |
| 428 | { DRCUML_OP_SEXT4, op_sext4 }, /* SEXT4 dst,src */ |
| 429 | { DRCUML_OP_ADD, op_add }, /* ADD dst,src1,src2[,f] */ |
| 430 | { DRCUML_OP_ADDC, op_addc }, /* ADDC dst,src1,src2[,f] */ |
| 431 | { DRCUML_OP_SUB, op_sub }, /* SUB dst,src1,src2[,f] */ |
| 432 | { DRCUML_OP_SUBB, op_subc }, /* SUBB dst,src1,src2[,f] */ |
| 433 | { DRCUML_OP_CMP, op_cmp }, /* CMP src1,src2[,f] */ |
| 434 | { DRCUML_OP_MULU, op_mulu }, /* MULU dst,edst,src1,src2[,f] */ |
| 435 | { DRCUML_OP_MULS, op_muls }, /* MULS dst,edst,src1,src2[,f] */ |
| 436 | { DRCUML_OP_DIVU, op_divu }, /* DIVU dst,edst,src1,src2[,f] */ |
| 437 | { DRCUML_OP_DIVS, op_divs }, /* DIVS dst,edst,src1,src2[,f] */ |
| 438 | { DRCUML_OP_AND, op_and }, /* AND dst,src1,src2[,f] */ |
| 439 | { DRCUML_OP_TEST, op_test }, /* TEST src1,src2[,f] */ |
| 440 | { DRCUML_OP_OR, op_or }, /* OR dst,src1,src2[,f] */ |
| 441 | { DRCUML_OP_XOR, op_xor }, /* XOR dst,src1,src2[,f] */ |
| 442 | { DRCUML_OP_SHL, op_shl }, /* SHL dst,src,count[,f] */ |
| 443 | { DRCUML_OP_SHR, op_shr }, /* SHR dst,src,count[,f] */ |
| 444 | { DRCUML_OP_SAR, op_sar }, /* SAR dst,src,count[,f] */ |
| 445 | { DRCUML_OP_ROL, op_rol }, /* ROL dst,src,count[,f] */ |
| 446 | { DRCUML_OP_ROLC, op_rolc }, /* ROLC dst,src,count[,f] */ |
| 447 | { DRCUML_OP_ROR, op_ror }, /* ROR dst,src,count[,f] */ |
| 448 | { DRCUML_OP_RORC, op_rorc }, /* RORC dst,src,count[,f] */ |
| 449 | |
| 450 | /* Floating Point Operations */ |
| 451 | { DRCUML_OP_FLOAD, op_fload }, /* FLOAD dst,base,index */ |
| 452 | { DRCUML_OP_FSTORE, op_fstore }, /* FSTORE base,index,src */ |
| 453 | { DRCUML_OP_FREAD, op_fread }, /* FREAD dst,space,src1 */ |
| 454 | { DRCUML_OP_FWRITE, op_fwrite }, /* FWRITE space,dst,src1 */ |
| 455 | { DRCUML_OP_FMOV, op_fmov }, /* FMOV dst,src1[,c] */ |
| 456 | { DRCUML_OP_FTOI4, op_ftoi4 }, /* FTOI4 dst,src1 */ |
| 457 | { DRCUML_OP_FTOI4T, op_ftoi4t }, /* FTOI4T dst,src1 */ |
| 458 | { DRCUML_OP_FTOI4R, op_ftoi4r }, /* FTOI4R dst,src1 */ |
| 459 | { DRCUML_OP_FTOI4F, op_ftoi4f }, /* FTOI4F dst,src1 */ |
| 460 | { DRCUML_OP_FTOI4C, op_ftoi4c }, /* FTOI4C dst,src1 */ |
| 461 | { DRCUML_OP_FTOI8, op_ftoi8 }, /* FTOI8 dst,src1 */ |
| 462 | { DRCUML_OP_FTOI8T, op_ftoi8t }, /* FTOI8T dst,src1 */ |
| 463 | { DRCUML_OP_FTOI8R, op_ftoi8r }, /* FTOI8R dst,src1 */ |
| 464 | { DRCUML_OP_FTOI8F, op_ftoi8f }, /* FTOI8F dst,src1 */ |
| 465 | { DRCUML_OP_FTOI8C, op_ftoi8c }, /* FTOI8C dst,src1 */ |
| 466 | { DRCUML_OP_FFRFS, op_ffrfs }, /* FFRFS dst,src1 */ |
| 467 | { DRCUML_OP_FFRFD, op_ffrfd }, /* FFRFD dst,src1 */ |
| 468 | { DRCUML_OP_FFRI4, op_ffri4 }, /* FFRI4 dst,src1 */ |
| 469 | { DRCUML_OP_FFRI8, op_ffri8 }, /* FFRI8 dst,src1 */ |
| 470 | { DRCUML_OP_FADD, op_fadd }, /* FADD dst,src1,src2 */ |
| 471 | { DRCUML_OP_FSUB, op_fsub }, /* FSUB dst,src1,src2 */ |
| 472 | { DRCUML_OP_FCMP, op_fcmp }, /* FCMP src1,src2 */ |
| 473 | { DRCUML_OP_FMUL, op_fmul }, /* FMUL dst,src1,src2 */ |
| 474 | { DRCUML_OP_FDIV, op_fdiv }, /* FDIV dst,src1,src2 */ |
| 475 | { DRCUML_OP_FNEG, op_fneg }, /* FNEG dst,src1 */ |
| 476 | { DRCUML_OP_FABS, op_fabs }, /* FABS dst,src1 */ |
| 477 | { DRCUML_OP_FSQRT, op_fsqrt }, /* FSQRT dst,src1 */ |
| 478 | { DRCUML_OP_FRECIP, op_frecip }, /* FRECIP dst,src1 */ |
| 479 | { DRCUML_OP_FRSQRT, op_frsqrt } /* FRSQRT dst,src1 */ |
| 480 | }; |
| 481 | |
| 482 | |
| 483 | |
| 484 | /*************************************************************************** |
| 485 | INLINE FUNCTIONS |
| 486 | ***************************************************************************/ |
| 487 | |
| 488 | /*------------------------------------------------- |
| 489 | param_select_register - select a register |
| 490 | to use, avoiding conflicts with the optional |
| 491 | checkparam |
| 492 | -------------------------------------------------*/ |
| 493 | |
| 494 | INLINE int param_select_register(int defreg, const drcuml_parameter *param, const drcuml_parameter *checkparam) |
| 495 | { |
| 496 | if (param->type == DRCUML_PTYPE_INT_REGISTER && (checkparam == NULL || param->type != checkparam->type || param->value != checkparam->value)) |
| 497 | return param->value; |
| 498 | return defreg; |
| 499 | } |
| 500 | |
| 501 | |
| 502 | /*------------------------------------------------- |
| 503 | emit_combine_z_flags - combine the Z flag from |
| 504 | two 32-bit operations |
| 505 | -------------------------------------------------*/ |
| 506 | |
| 507 | INLINE void emit_combine_z_flags(x86code **dst) |
| 508 | { |
| 509 | /* this assumes that the flags from the low 32-bit op are on the stack */ |
| 510 | /* and the flags from the high 32-bit op are live */ |
| 511 | emit_pushf(dst); // pushf |
| 512 | emit_mov_r32_m32(dst, REG_ECX, MBD(REG_ESP, 4)); // mov ecx,[esp+4] |
| 513 | emit_or_r32_imm(dst, REG_ECX, ~0x40); // or ecx,~0x40 |
| 514 | emit_and_m32_r32(dst, MBD(REG_ESP, 0), REG_ECX); // and [esp],ecx |
| 515 | emit_popf(dst); // popf |
| 516 | emit_lea_r32_m32(dst, REG_ESP, MBD(REG_ESP, 4)); // lea esp,[esp+4] |
| 517 | } |
| 518 | |
| 519 | |
| 520 | /*------------------------------------------------- |
| 521 | emit_combine_z_shl_flags - combine the Z |
| 522 | flags from two 32-bit shift left operations |
| 523 | -------------------------------------------------*/ |
| 524 | |
| 525 | INLINE void emit_combine_z_shl_flags(x86code **dst) |
| 526 | { |
| 527 | /* this assumes that the flags from the high 32-bit op are on the stack */ |
| 528 | /* and the flags from the low 32-bit op are live */ |
| 529 | emit_pushf(dst); // pushf |
| 530 | emit_pop_r32(dst, REG_ECX); // pop ecx |
| 531 | emit_or_r32_imm(dst, REG_ECX, ~0x40); // or ecx,~0x40 |
| 532 | emit_and_m32_r32(dst, MBD(REG_ESP, 0), REG_ECX); // and [esp],ecx |
| 533 | emit_popf(dst); // popf |
| 534 | } |
| 535 | |
| 536 | |
| 537 | |
| 538 | /*************************************************************************** |
| 539 | BACKEND CALLBACKS |
| 540 | ***************************************************************************/ |
| 541 | |
| 542 | /*------------------------------------------------- |
| 543 | drcbex86_alloc - allocate back-end-specific |
| 544 | state |
| 545 | -------------------------------------------------*/ |
| 546 | |
| 547 | static drcbe_state *drcbex86_alloc(drcuml_state *drcuml, drccache *cache, UINT32 flags, int modes, int addrbits, int ignorebits) |
| 548 | { |
| 549 | int opnum, regnum, entry; |
| 550 | drcbe_state *drcbe; |
| 551 | |
| 552 | /* allocate space in the cache for our state */ |
| 553 | drcbe = drccache_memory_alloc(cache, sizeof(*drcbe)); |
| 554 | if (drcbe == NULL) |
| 555 | return NULL; |
| 556 | memset(drcbe, 0, sizeof(*drcbe)); |
| 557 | |
| 558 | /* remember our pointers */ |
| 559 | drcbe->drcuml = drcuml; |
| 560 | drcbe->cache = cache; |
| 561 | |
| 562 | /* allocate hash tables */ |
| 563 | drcbe->hash = drchash_alloc(cache, modes, addrbits, ignorebits); |
| 564 | if (drcbe->hash == NULL) |
| 565 | return NULL; |
| 566 | |
| 567 | /* allocate code map */ |
| 568 | drcbe->map = drcmap_alloc(cache, 0); |
| 569 | if (drcbe->map == NULL) |
| 570 | return NULL; |
| 571 | |
| 572 | /* allocate a label tracker */ |
| 573 | drcbe->labels = drclabel_list_alloc(cache); |
| 574 | if (drcbe->labels == NULL) |
| 575 | return NULL; |
| 576 | |
| 577 | /* build the opcode table (static but it doesn't hurt to regenerate it) */ |
| 578 | for (opnum = 0; opnum < ARRAY_LENGTH(opcode_table_source); opnum++) |
| 579 | opcode_table[opcode_table_source[opnum].opcode] = opcode_table_source[opnum].func; |
| 580 | |
| 581 | /* build the flags map (static but it doesn't hurt to regenerate it) */ |
| 582 | for (entry = 0; entry < ARRAY_LENGTH(flags_map); entry++) |
| 583 | { |
| 584 | UINT8 flags = 0; |
| 585 | if (entry & 0x001) flags |= DRCUML_FLAG_C; |
| 586 | if (entry & 0x004) flags |= DRCUML_FLAG_U; |
| 587 | if (entry & 0x040) flags |= DRCUML_FLAG_Z; |
| 588 | if (entry & 0x080) flags |= DRCUML_FLAG_S; |
| 589 | if (entry & 0x800) flags |= DRCUML_FLAG_V; |
| 590 | flags_map[entry] = flags; |
| 591 | } |
| 592 | |
| 593 | /* compute hi pointers for each register */ |
| 594 | for (regnum = 0; regnum < ARRAY_LENGTH(int_register_map); regnum++) |
| 595 | if (int_register_map[regnum] != 0) |
| 596 | { |
| 597 | drcbe->reglo[int_register_map[regnum]] = &drcbe->state.r[regnum].l; |
| 598 | drcbe->reghi[int_register_map[regnum]] = &drcbe->state.r[regnum].h; |
| 599 | } |
| 600 | |
| 601 | /* create the log */ |
| 602 | if (flags & DRCUML_OPTION_LOG_NATIVE) |
| 603 | drcbe->log = x86log_create_context("drcbex86.asm"); |
| 604 | |
| 605 | return drcbe; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | /*------------------------------------------------- |
| 610 | drcbex86_free - free back-end specific state |
| 611 | -------------------------------------------------*/ |
| 612 | |
| 613 | static void drcbex86_free(drcbe_state *drcbe) |
| 614 | { |
| 615 | /* free the log context */ |
| 616 | if (drcbe->log != NULL) |
| 617 | x86log_free_context(drcbe->log); |
| 618 | } |
| 619 | |
| 620 | |
| 621 | /*------------------------------------------------- |
| 622 | drcbex86_reset - reset back-end specific state |
| 623 | -------------------------------------------------*/ |
| 624 | |
| 625 | static void drcbex86_reset(drcbe_state *drcbe) |
| 626 | { |
| 627 | x86code **dst; |
| 628 | |
| 629 | /* output a note to the log */ |
| 630 | if (drcbe->log != NULL) |
| 631 | x86log_printf(drcbe->log, "\n\n===========\nCACHE RESET\n===========\n\n"); |
| 632 | |
| 633 | /* generate a little bit of glue code to set up the environment */ |
| 634 | dst = (x86code **)drccache_begin_codegen(drcbe->cache, 500); |
| 635 | if (dst == NULL) |
| 636 | fatalerror("Out of cache space after a reset!"); |
| 637 | |
| 638 | /* generate an entry point */ |
| 639 | drcbe->entry = (x86_entry_point_func)*dst; |
| 640 | emit_mov_r32_m32(dst, REG_EAX, MBD(REG_ESP, 4)); // mov eax,[esp+4] |
| 641 | emit_push_r32(dst, REG_EBX); // push ebx |
| 642 | emit_push_r32(dst, REG_ESI); // push esi |
| 643 | emit_push_r32(dst, REG_EDI); // push edi |
| 644 | emit_push_r32(dst, REG_EBP); // push ebp |
| 645 | emit_mov_m32_r32(dst, MABS(&drcbe->hashstacksave), REG_ESP); // mov [hashstacksave],esp |
| 646 | emit_sub_r32_imm(dst, REG_ESP, 4); // sub esp,4 |
| 647 | emit_mov_m32_r32(dst, MABS(&drcbe->stacksave), REG_ESP); // mov [stacksave],esp |
| 648 | emit_fstcw_m16(dst, MABS(&drcbe->fpumode)); // fstcw [fpumode] |
| 649 | emit_jmp_r32(dst, REG_EAX); // jmp eax |
| 650 | if (drcbe->log != NULL) |
| 651 | x86log_disasm_code_range(drcbe->log, "entry_point", (x86code *)drcbe->entry, *dst); |
| 652 | |
| 653 | /* generate an exit point */ |
| 654 | drcbe->exit = *dst; |
| 655 | emit_fldcw_m16(dst, MABS(&drcbe->fpumode)); // fldcw [fpumode] |
| 656 | emit_mov_r32_m32(dst, REG_ESP, MABS(&drcbe->hashstacksave)); // mov esp,[hashstacksave] |
| 657 | emit_pop_r32(dst, REG_EBP); // pop ebp |
| 658 | emit_pop_r32(dst, REG_EDI); // pop edi |
| 659 | emit_pop_r32(dst, REG_ESI); // pop esi |
| 660 | emit_pop_r32(dst, REG_EBX); // pop ebx |
| 661 | emit_ret(dst); // ret |
| 662 | if (drcbe->log != NULL) |
| 663 | x86log_disasm_code_range(drcbe->log, "exit_point", drcbe->exit, *dst); |
| 664 | |
| 665 | /* generate a no code point */ |
| 666 | drcbe->nocode = *dst; |
| 667 | emit_ret(dst); // ret |
| 668 | if (drcbe->log != NULL) |
| 669 | x86log_disasm_code_range(drcbe->log, "nocode", drcbe->nocode, *dst); |
| 670 | |
| 671 | /* finish up codegen */ |
| 672 | drccache_end_codegen(drcbe->cache); |
| 673 | |
| 674 | /* reset our hash tables */ |
| 675 | drchash_reset(drcbe->hash); |
| 676 | drchash_set_default_codeptr(drcbe->hash, drcbe->nocode); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | /*------------------------------------------------- |
| 681 | drcbex86_generate - generate code |
| 682 | -------------------------------------------------*/ |
| 683 | |
| 684 | static void drcbex86_generate(drcbe_state *drcbe, drcuml_block *block, const drcuml_instruction *instlist, UINT32 numinst) |
| 685 | { |
| 686 | const char *blockname = NULL; |
| 687 | char blockbuffer[100]; |
| 688 | drccodeptr *cachetop; |
| 689 | x86code *base; |
| 690 | x86code *dst; |
| 691 | int inum; |
| 692 | |
| 693 | /* tell all of our utility objects that a block is beginning */ |
| 694 | drchash_block_begin(drcbe->hash, block, instlist, numinst); |
| 695 | drclabel_block_begin(drcbe->labels, block); |
| 696 | drcmap_block_begin(drcbe->map, block); |
| 697 | |
| 698 | /* begin codegen; fail if we can't */ |
| 699 | cachetop = drccache_begin_codegen(drcbe->cache, numinst * 8 * 4); |
| 700 | if (cachetop == NULL) |
| 701 | drcuml_block_abort(block); |
| 702 | |
| 703 | /* compute the base by aligning the cache top to a cache line (assumed to be 64 bytes) */ |
| 704 | base = (x86code *)(((FPTR)*cachetop + 63) & ~63); |
| 705 | dst = base; |
| 706 | |
| 707 | /* generate code */ |
| 708 | for (inum = 0; inum < numinst; inum++) |
| 709 | { |
| 710 | const drcuml_instruction *inst = &instlist[inum]; |
| 711 | assert(inst->opcode < ARRAY_LENGTH(opcode_table)); |
| 712 | |
| 713 | /* add a comment */ |
| 714 | if (drcbe->log != NULL) |
| 715 | { |
| 716 | char dasm[100]; |
| 717 | drcuml_disasm(inst, dasm); |
| 718 | x86log_add_comment(drcbe->log, dst, "%s", dasm); |
| 719 | } |
| 720 | |
| 721 | /* extract a blockname */ |
| 722 | if (blockname == NULL) |
| 723 | { |
| 724 | if (inst->opcode == DRCUML_OP_HANDLE) |
| 725 | blockname = drcuml_handle_name((drcuml_codehandle *)(FPTR)inst->param[0].value); |
| 726 | else if (inst->opcode == DRCUML_OP_HASH) |
| 727 | { |
| 728 | sprintf(blockbuffer, "Code: mode=%d PC=%08X", (UINT32)inst->param[0].value, (offs_t)inst->param[1].value); |
| 729 | blockname = blockbuffer; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* generate code */ |
| 734 | dst = (*opcode_table[inst->opcode])(drcbe, dst, inst); |
| 735 | } |
| 736 | |
| 737 | /* complete codegen */ |
| 738 | *cachetop = (drccodeptr)dst; |
| 739 | drccache_end_codegen(drcbe->cache); |
| 740 | |
| 741 | /* log it */ |
| 742 | if (drcbe->log != NULL) |
| 743 | x86log_disasm_code_range(drcbe->log, (blockname == NULL) ? "Unknown block" : blockname, base, drccache_top(drcbe->cache)); |
| 744 | |
| 745 | /* tell all of our utility objects that the block is finished */ |
| 746 | drchash_block_end(drcbe->hash, block); |
| 747 | drclabel_block_end(drcbe->labels, block); |
| 748 | drcmap_block_end(drcbe->map, block); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | /*------------------------------------------------- |
| 753 | drcbex86_hash_exists - return true if the |
| 754 | given mode/pc exists in the hash table |
| 755 | -------------------------------------------------*/ |
| 756 | |
| 757 | static int drcbex86_hash_exists(drcbe_state *drcbe, UINT32 mode, UINT32 pc) |
| 758 | { |
| 759 | return drchash_code_exists(drcbe->hash, mode, pc); |
| 760 | } |
| 761 | |
| 762 | |
| 763 | /*------------------------------------------------- |
| 764 | drcbec_execute - execute a block of code |
| 765 | registered at the given mode/pc |
| 766 | -------------------------------------------------*/ |
| 767 | |
| 768 | static int drcbex86_execute(drcbe_state *drcbe, drcuml_codehandle *entry) |
| 769 | { |
| 770 | /* call our entry point which will jump to the destination */ |
| 771 | return (*drcbe->entry)((x86code *)drcuml_handle_codeptr(entry)); |
| 772 | } |
| 773 | |
| 774 | |
| 775 | |
| 776 | /*************************************************************************** |
| 777 | COMPILE HELPERS |
| 778 | ***************************************************************************/ |
| 779 | |
| 780 | /*------------------------------------------------- |
| 781 | param_normalize - convert a full parameter |
| 782 | into a reduced set |
| 783 | -------------------------------------------------*/ |
| 784 | |
| 785 | static void param_normalize(drcbe_state *drcbe, const drcuml_parameter *src, drcuml_parameter *dest, UINT32 allowed) |
| 786 | { |
| 787 | int regnum; |
| 788 | |
| 789 | switch (src->type) |
| 790 | { |
| 791 | /* immediates pass through */ |
| 792 | case DRCUML_PTYPE_IMMEDIATE: |
| 793 | assert(allowed & PTYPE_I); |
| 794 | dest->type = DRCUML_PTYPE_IMMEDIATE; |
| 795 | dest->value = src->value; |
| 796 | break; |
| 797 | |
| 798 | /* mapvars are converted to immediates with their current value */ |
| 799 | case DRCUML_PTYPE_MAPVAR: |
| 800 | assert(allowed & PTYPE_I); |
| 801 | dest->type = DRCUML_PTYPE_IMMEDIATE; |
| 802 | dest->value = drcmap_get_last_value(drcbe->map, src->value); |
| 803 | break; |
| 804 | |
| 805 | /* memory passes through */ |
| 806 | case DRCUML_PTYPE_MEMORY: |
| 807 | assert(allowed & PTYPE_M); |
| 808 | dest->type = DRCUML_PTYPE_MEMORY; |
| 809 | dest->value = src->value; |
| 810 | break; |
| 811 | |
| 812 | /* if a register maps to a register, keep it as a register; otherwise map it to memory */ |
| 813 | case DRCUML_PTYPE_INT_REGISTER: |
| 814 | assert(allowed & PTYPE_R); |
| 815 | assert(allowed & PTYPE_M); |
| 816 | regnum = int_register_map[src->value - DRCUML_REG_I0]; |
| 817 | if (regnum != 0) |
| 818 | { |
| 819 | dest->type = DRCUML_PTYPE_INT_REGISTER; |
| 820 | dest->value = regnum; |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | dest->type = DRCUML_PTYPE_MEMORY; |
| 825 | dest->value = (FPTR)&drcbe->state.r[src->value - DRCUML_REG_I0]; |
| 826 | } |
| 827 | break; |
| 828 | |
| 829 | /* all floating point registers map to memory */ |
| 830 | case DRCUML_PTYPE_FLOAT_REGISTER: |
| 831 | assert(allowed & PTYPE_F); |
| 832 | assert(allowed & PTYPE_M); |
| 833 | dest->type = DRCUML_PTYPE_MEMORY; |
| 834 | dest->value = (FPTR)&drcbe->state.f[src->value - DRCUML_REG_F0]; |
| 835 | break; |
| 836 | |
| 837 | /* everything else is unexpected */ |
| 838 | default: |
| 839 | fatalerror("Unexpected parameter type"); |
| 840 | break; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | |
| 845 | /*------------------------------------------------- |
| 846 | param_normalize_1 - normalize a single |
| 847 | parameter instruction |
| 848 | -------------------------------------------------*/ |
| 849 | |
| 850 | static void param_normalize_1(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0) |
| 851 | { |
| 852 | assert(inst->numparams == 1); |
| 853 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 854 | } |
| 855 | |
| 856 | |
| 857 | /*------------------------------------------------- |
| 858 | param_normalize_2 - normalize a 2 |
| 859 | parameter instruction |
| 860 | -------------------------------------------------*/ |
| 861 | |
| 862 | static void param_normalize_2(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1) |
| 863 | { |
| 864 | assert(inst->numparams == 2); |
| 865 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 866 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 867 | } |
| 868 | |
| 869 | |
| 870 | /*------------------------------------------------- |
| 871 | param_normalize_2_commutative - normalize a 2 |
| 872 | parameter instruction, shuffling the |
| 873 | parameters on the assumption that the two |
| 874 | parameters can be swapped |
| 875 | -------------------------------------------------*/ |
| 876 | |
| 877 | static void param_normalize_2_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1) |
| 878 | { |
| 879 | param_normalize_2(drcbe, inst, dest0, allowed0, dest1, allowed1); |
| 880 | |
| 881 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 882 | if (dest0->type == DRCUML_PTYPE_MEMORY) |
| 883 | { |
| 884 | drcuml_parameter temp = *dest0; |
| 885 | *dest0 = *dest1; |
| 886 | *dest1 = temp; |
| 887 | } |
| 888 | |
| 889 | /* if the inner parameter is an immediate, push it to the outer */ |
| 890 | if (dest0->type == DRCUML_PTYPE_IMMEDIATE) |
| 891 | { |
| 892 | drcuml_parameter temp = *dest0; |
| 893 | *dest0 = *dest1; |
| 894 | *dest1 = temp; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | |
| 899 | /*------------------------------------------------- |
| 900 | param_normalize_3 - normalize a 3 |
| 901 | parameter instruction |
| 902 | -------------------------------------------------*/ |
| 903 | |
| 904 | static void param_normalize_3(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2) |
| 905 | { |
| 906 | assert(inst->numparams == 3); |
| 907 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 908 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 909 | param_normalize(drcbe, &inst->param[2], dest2, allowed2); |
| 910 | } |
| 911 | |
| 912 | |
| 913 | /*------------------------------------------------- |
| 914 | param_normalize_3_commutative - normalize a 3 |
| 915 | parameter instruction, shuffling the |
| 916 | parameters on the assumption that the last |
| 917 | 2 can be swapped |
| 918 | -------------------------------------------------*/ |
| 919 | |
| 920 | static void param_normalize_3_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2) |
| 921 | { |
| 922 | param_normalize_3(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2); |
| 923 | |
| 924 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 925 | if (dest1->type == DRCUML_PTYPE_MEMORY) |
| 926 | { |
| 927 | drcuml_parameter temp = *dest1; |
| 928 | *dest1 = *dest2; |
| 929 | *dest2 = temp; |
| 930 | } |
| 931 | |
| 932 | /* if the inner parameter is an immediate, push it to the outer */ |
| 933 | if (dest1->type == DRCUML_PTYPE_IMMEDIATE) |
| 934 | { |
| 935 | drcuml_parameter temp = *dest1; |
| 936 | *dest1 = *dest2; |
| 937 | *dest2 = temp; |
| 938 | } |
| 939 | |
| 940 | /* if the destination and outer parameters are equal, move the outer to the inner */ |
| 941 | if (dest0->type == dest2->type && dest0->value == dest2->value && dest0->type != DRCUML_PTYPE_IMMEDIATE) |
| 942 | { |
| 943 | drcuml_parameter temp = *dest1; |
| 944 | *dest1 = *dest2; |
| 945 | *dest2 = temp; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | |
| 950 | /*------------------------------------------------- |
| 951 | param_normalize_4 - normalize a 4 |
| 952 | parameter instruction |
| 953 | -------------------------------------------------*/ |
| 954 | |
| 955 | static void param_normalize_4(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3) |
| 956 | { |
| 957 | assert(inst->numparams == 4); |
| 958 | param_normalize(drcbe, &inst->param[0], dest0, allowed0); |
| 959 | param_normalize(drcbe, &inst->param[1], dest1, allowed1); |
| 960 | param_normalize(drcbe, &inst->param[2], dest2, allowed2); |
| 961 | param_normalize(drcbe, &inst->param[3], dest3, allowed3); |
| 962 | } |
| 963 | |
| 964 | |
| 965 | /*------------------------------------------------- |
| 966 | param_normalize_4_commutative - normalize a 4 |
| 967 | parameter instruction, shuffling the |
| 968 | parameters on the assumption that the last |
| 969 | 2 can be swapped |
| 970 | -------------------------------------------------*/ |
| 971 | |
| 972 | static void param_normalize_4_commutative(drcbe_state *drcbe, const drcuml_instruction *inst, drcuml_parameter *dest0, UINT32 allowed0, drcuml_parameter *dest1, UINT32 allowed1, drcuml_parameter *dest2, UINT32 allowed2, drcuml_parameter *dest3, UINT32 allowed3) |
| 973 | { |
| 974 | param_normalize_4(drcbe, inst, dest0, allowed0, dest1, allowed1, dest2, allowed2, dest3, allowed3); |
| 975 | |
| 976 | /* if the inner parameter is a memory operand, push it to the outer */ |
| 977 | if (dest2->type == DRCUML_PTYPE_MEMORY) |
| 978 | { |
| 979 | drcuml_parameter temp = *dest2; |
| 980 | *dest2 = *dest3; |
| 981 | *dest3 = temp; |
| 982 | } |
| 983 | |
| 984 | /* if the inner parameter is an immediate, push it to the outer */ |
| 985 | if (dest2->type == DRCUML_PTYPE_IMMEDIATE) |
| 986 | { |
| 987 | drcuml_parameter temp = *dest2; |
| 988 | *dest2 = *dest3; |
| 989 | *dest3 = temp; |
| 990 | } |
| 991 | |
| 992 | /* if the destination and outer parameters are equal, move the outer to the inner */ |
| 993 | if (dest0->type == dest3->type && dest0->value == dest3->value && dest0->type != DRCUML_PTYPE_IMMEDIATE) |
| 994 | { |
| 995 | drcuml_parameter temp = *dest2; |
| 996 | *dest2 = *dest3; |
| 997 | *dest3 = temp; |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | /*************************************************************************** |
| 1004 | EMITTERS FOR 32-BIT OPERATIONS WITH PARAMETERS |
| 1005 | ***************************************************************************/ |
| 1006 | |
| 1007 | /*------------------------------------------------- |
| 1008 | emit_mov_r32_p32 - move a 32-bit parameter |
| 1009 | into a register |
| 1010 | -------------------------------------------------*/ |
| 1011 | |
| 1012 | static void emit_mov_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param) |
| 1013 | { |
| 1014 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1015 | { |
| 1016 | if (param->value == 0) |
| 1017 | emit_xor_r32_r32(dst, reg, reg); // xor reg,reg |
| 1018 | else |
| 1019 | emit_mov_r32_imm(dst, reg, param->value); // mov reg,param |
| 1020 | } |
| 1021 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1022 | emit_mov_r32_m32(dst, reg, MABS(param->value)); // mov reg,[param] |
| 1023 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1024 | { |
| 1025 | if (reg != param->value) |
| 1026 | emit_mov_r32_r32(dst, reg, param->value); // mov reg,param |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | |
| 1031 | /*------------------------------------------------- |
| 1032 | emit_mov_m32_p32 - move a 32-bit parameter |
| 1033 | into a memory location |
| 1034 | -------------------------------------------------*/ |
| 1035 | |
| 1036 | static void emit_mov_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param) |
| 1037 | { |
| 1038 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1039 | emit_mov_m32_imm(dst, MEMPARAMS, param->value); // mov [mem],param |
| 1040 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1041 | { |
| 1042 | emit_mov_r32_m32(dst, REG_EAX, MABS(param->value)); // mov eax,[param] |
| 1043 | emit_mov_m32_r32(dst, MEMPARAMS, REG_EAX); // mov [mem],eax |
| 1044 | } |
| 1045 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1046 | emit_mov_m32_r32(dst, MEMPARAMS, param->value); // mov [mem],param |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | /*------------------------------------------------- |
| 1051 | emit_mov_p32_r32 - move a register into a |
| 1052 | 32-bit parameter |
| 1053 | -------------------------------------------------*/ |
| 1054 | |
| 1055 | static void emit_mov_p32_r32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reg) |
| 1056 | { |
| 1057 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 1058 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1059 | emit_mov_m32_r32(dst, MABS(param->value), reg); // mov [param],reg |
| 1060 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1061 | { |
| 1062 | if (reg != param->value) |
| 1063 | emit_mov_r32_r32(dst, param->value, reg); // mov param,reg |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | |
| 1068 | /*------------------------------------------------- |
| 1069 | emit_push_p32 - push a 32-bit parameter onto |
| 1070 | the stack |
| 1071 | -------------------------------------------------*/ |
| 1072 | |
| 1073 | static void emit_push_p32(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *src) |
| 1074 | { |
| 1075 | if (src->type == DRCUML_PTYPE_IMMEDIATE) |
| 1076 | emit_push_imm(dst, src->value); // push src |
| 1077 | else if (src->type == DRCUML_PTYPE_MEMORY) |
| 1078 | emit_push_m32(dst, MABS(src->value)); // push [src] |
| 1079 | else if (src->type == DRCUML_PTYPE_INT_REGISTER) |
| 1080 | emit_push_r32(dst, src->value); // push src |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | /*------------------------------------------------- |
| 1085 | emit_add_r32_p32 - add operation to a 32-bit |
| 1086 | register from a 32-bit parameter |
| 1087 | -------------------------------------------------*/ |
| 1088 | |
| 1089 | static void emit_add_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1090 | { |
| 1091 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1092 | { |
| 1093 | if (inst->condflags != 0 || param->value != 0) |
| 1094 | emit_add_r32_imm(dst, reg, param->value); // add reg,param |
| 1095 | } |
| 1096 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1097 | emit_add_r32_m32(dst, reg, MABS(param->value)); // add reg,[param] |
| 1098 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1099 | emit_add_r32_r32(dst, reg, param->value); // add reg,param |
| 1100 | } |
| 1101 | |
| 1102 | |
| 1103 | /*------------------------------------------------- |
| 1104 | emit_add_m32_p32 - add operation to a 32-bit |
| 1105 | memory location from a 32-bit parameter |
| 1106 | -------------------------------------------------*/ |
| 1107 | |
| 1108 | static void emit_add_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1109 | { |
| 1110 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1111 | { |
| 1112 | if (inst->condflags != 0 || param->value != 0) |
| 1113 | emit_add_m32_imm(dst, MEMPARAMS, param->value); // add [dest],param |
| 1114 | } |
| 1115 | else |
| 1116 | { |
| 1117 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1118 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1119 | emit_add_m32_r32(dst, MEMPARAMS, reg); // add [dest],reg |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | |
| 1124 | /*------------------------------------------------- |
| 1125 | emit_adc_r32_p32 - adc operation to a 32-bit |
| 1126 | register from a 32-bit parameter |
| 1127 | -------------------------------------------------*/ |
| 1128 | |
| 1129 | static void emit_adc_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1130 | { |
| 1131 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1132 | { |
| 1133 | if (inst->condflags != 0 || param->value != 0) |
| 1134 | emit_adc_r32_imm(dst, reg, param->value); // adc reg,param |
| 1135 | } |
| 1136 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1137 | emit_adc_r32_m32(dst, reg, MABS(param->value)); // adc reg,[param] |
| 1138 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1139 | emit_adc_r32_r32(dst, reg, param->value); // adc reg,param |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | /*------------------------------------------------- |
| 1144 | emit_adc_m32_p32 - adc operation to a 32-bit |
| 1145 | memory location from a 32-bit parameter |
| 1146 | -------------------------------------------------*/ |
| 1147 | |
| 1148 | static void emit_adc_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1149 | { |
| 1150 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1151 | { |
| 1152 | if (inst->condflags != 0 || param->value != 0) |
| 1153 | emit_adc_m32_imm(dst, MEMPARAMS, param->value); // adc [dest],param |
| 1154 | } |
| 1155 | else |
| 1156 | { |
| 1157 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1158 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1159 | emit_adc_m32_r32(dst, MEMPARAMS, reg); // adc [dest],reg |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | /*------------------------------------------------- |
| 1165 | emit_sub_r32_p32 - sub operation to a 32-bit |
| 1166 | register from a 32-bit parameter |
| 1167 | -------------------------------------------------*/ |
| 1168 | |
| 1169 | static void emit_sub_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1170 | { |
| 1171 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1172 | { |
| 1173 | if (inst->condflags != 0 || param->value != 0) |
| 1174 | emit_sub_r32_imm(dst, reg, param->value); // sub reg,param |
| 1175 | } |
| 1176 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1177 | emit_sub_r32_m32(dst, reg, MABS(param->value)); // sub reg,[param] |
| 1178 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1179 | emit_sub_r32_r32(dst, reg, param->value); // sub reg,param |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /*------------------------------------------------- |
| 1184 | emit_sub_m32_p32 - sub operation to a 32-bit |
| 1185 | memory location from a 32-bit parameter |
| 1186 | -------------------------------------------------*/ |
| 1187 | |
| 1188 | static void emit_sub_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1189 | { |
| 1190 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1191 | { |
| 1192 | if (inst->condflags != 0 || param->value != 0) |
| 1193 | emit_sub_m32_imm(dst, MEMPARAMS, param->value); // sub [dest],param |
| 1194 | } |
| 1195 | else |
| 1196 | { |
| 1197 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1198 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1199 | emit_sub_m32_r32(dst, MEMPARAMS, reg); // sub [dest],reg |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | /*------------------------------------------------- |
| 1205 | emit_sbb_r32_p32 - sbb operation to a 32-bit |
| 1206 | register from a 32-bit parameter |
| 1207 | -------------------------------------------------*/ |
| 1208 | |
| 1209 | static void emit_sbb_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1210 | { |
| 1211 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1212 | { |
| 1213 | if (inst->condflags != 0 || param->value != 0) |
| 1214 | emit_sbb_r32_imm(dst, reg, param->value); // sbb reg,param |
| 1215 | } |
| 1216 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1217 | emit_sbb_r32_m32(dst, reg, MABS(param->value)); // sbb reg,[param] |
| 1218 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1219 | emit_sbb_r32_r32(dst, reg, param->value); // sbb reg,param |
| 1220 | } |
| 1221 | |
| 1222 | |
| 1223 | /*------------------------------------------------- |
| 1224 | emit_sbb_m32_p32 - sbb operation to a 32-bit |
| 1225 | memory location from a 32-bit parameter |
| 1226 | -------------------------------------------------*/ |
| 1227 | |
| 1228 | static void emit_sbb_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1229 | { |
| 1230 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1231 | { |
| 1232 | if (inst->condflags != 0 || param->value != 0) |
| 1233 | emit_sbb_m32_imm(dst, MEMPARAMS, param->value); // sbb [dest],param |
| 1234 | } |
| 1235 | else |
| 1236 | { |
| 1237 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1238 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1239 | emit_sbb_m32_r32(dst, MEMPARAMS, reg); // sbb [dest],reg |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | |
| 1244 | /*------------------------------------------------- |
| 1245 | emit_cmp_r32_p32 - cmp operation to a 32-bit |
| 1246 | register from a 32-bit parameter |
| 1247 | -------------------------------------------------*/ |
| 1248 | |
| 1249 | static void emit_cmp_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1250 | { |
| 1251 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1252 | emit_cmp_r32_imm(dst, reg, param->value); // cmp reg,param |
| 1253 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1254 | emit_cmp_r32_m32(dst, reg, MABS(param->value)); // cmp reg,[param] |
| 1255 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1256 | emit_cmp_r32_r32(dst, reg, param->value); // cmp reg,param |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | /*------------------------------------------------- |
| 1261 | emit_cmp_r32_p32hi - cmp operation to a 32-bit |
| 1262 | register from the upper 32 bits of a 64-bit |
| 1263 | parameter |
| 1264 | -------------------------------------------------*/ |
| 1265 | |
| 1266 | static void emit_cmp_r32_p32hi(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1267 | { |
| 1268 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1269 | emit_cmp_r32_imm(dst, reg, param->value >> 32); // cmp reg,param >> 32 |
| 1270 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1271 | emit_cmp_r32_m32(dst, reg, MABS(param->value + 4)); // cmp reg,[param+4] |
| 1272 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1273 | emit_cmp_r32_m32(dst, reg, MABS(drcbe->reghi[param->value])); // cmp reg,reghi[param] |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | /*------------------------------------------------- |
| 1278 | emit_cmp_m32_p32 - cmp operation to a 32-bit |
| 1279 | memory location from a 32-bit parameter |
| 1280 | -------------------------------------------------*/ |
| 1281 | |
| 1282 | static void emit_cmp_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1283 | { |
| 1284 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1285 | emit_cmp_m32_imm(dst, MEMPARAMS, param->value); // cmp [dest],param |
| 1286 | else |
| 1287 | { |
| 1288 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1289 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1290 | emit_cmp_m32_r32(dst, MEMPARAMS, reg); // cmp [dest],reg |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | /*------------------------------------------------- |
| 1296 | emit_cmp_m32_p32hi - cmp operation to a 32-bit |
| 1297 | memory location from the upper half of a |
| 1298 | 64-bit parameter |
| 1299 | -------------------------------------------------*/ |
| 1300 | |
| 1301 | static void emit_cmp_m32_p32hi(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1302 | { |
| 1303 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1304 | emit_cmp_m32_imm(dst, MEMPARAMS, param->value >> 32); // cmp [dest],param >> 32 |
| 1305 | else |
| 1306 | { |
| 1307 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1308 | emit_mov_r32_m32(dst, REG_EAX, MABS(param->value + 4)); // mov eax,[param+4] |
| 1309 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1310 | emit_mov_r32_m32(dst, REG_EAX, MABS(drcbe->reghi[param->value])); // mov eax,reghi[param] |
| 1311 | emit_cmp_m32_r32(dst, MEMPARAMS, REG_EAX); // cmp [dest],eax |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /*------------------------------------------------- |
| 1317 | emit_and_r32_p32 - and operation to a 32-bit |
| 1318 | register from a 32-bit parameter |
| 1319 | -------------------------------------------------*/ |
| 1320 | |
| 1321 | static void emit_and_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1322 | { |
| 1323 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1324 | { |
| 1325 | if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1326 | /* skip */; |
| 1327 | else if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1328 | emit_xor_r32_r32(dst, reg, reg); // xor reg,reg |
| 1329 | else |
| 1330 | emit_and_r32_imm(dst, reg, param->value); // and reg,param |
| 1331 | } |
| 1332 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1333 | emit_and_r32_m32(dst, reg, MABS(param->value)); // and reg,[param] |
| 1334 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1335 | emit_and_r32_r32(dst, reg, param->value); // and reg,param |
| 1336 | } |
| 1337 | |
| 1338 | |
| 1339 | /*------------------------------------------------- |
| 1340 | emit_and_m32_p32 - and operation to a 32-bit |
| 1341 | memory location from a 32-bit parameter |
| 1342 | -------------------------------------------------*/ |
| 1343 | |
| 1344 | static void emit_and_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1345 | { |
| 1346 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1347 | { |
| 1348 | if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1349 | /* skip */; |
| 1350 | else if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1351 | emit_mov_m32_imm(dst, MEMPARAMS, 0); // mov [dest],0 |
| 1352 | else |
| 1353 | emit_and_m32_imm(dst, MEMPARAMS, param->value); // and [dest],param |
| 1354 | } |
| 1355 | else |
| 1356 | { |
| 1357 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1358 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1359 | emit_and_m32_r32(dst, MEMPARAMS, reg); // and [dest],reg |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | |
| 1364 | /*------------------------------------------------- |
| 1365 | emit_test_r32_p32 - test operation to a 32-bit |
| 1366 | register from a 32-bit parameter |
| 1367 | -------------------------------------------------*/ |
| 1368 | |
| 1369 | static void emit_test_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1370 | { |
| 1371 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1372 | emit_test_r32_imm(dst, reg, param->value); // test reg,param |
| 1373 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1374 | emit_test_m32_r32(dst, MABS(param->value), reg); // test [param],reg |
| 1375 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1376 | emit_test_r32_r32(dst, reg, param->value); // test reg,param |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | /*------------------------------------------------- |
| 1381 | emit_test_m32_p32 - test operation to a 32-bit |
| 1382 | memory location from a 32-bit parameter |
| 1383 | -------------------------------------------------*/ |
| 1384 | |
| 1385 | static void emit_test_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1386 | { |
| 1387 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1388 | emit_test_m32_imm(dst, MEMPARAMS, param->value); // test [dest],param |
| 1389 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1390 | { |
| 1391 | emit_mov_r32_p32(drcbe, dst, REG_EAX, param); // mov reg,param |
| 1392 | emit_test_m32_r32(dst, MEMPARAMS, REG_EAX); // test [dest],reg |
| 1393 | } |
| 1394 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1395 | emit_test_m32_r32(dst, MEMPARAMS, param->value); // test [dest],param |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | /*------------------------------------------------- |
| 1400 | emit_or_r32_p32 - or operation to a 32-bit |
| 1401 | register from a 32-bit parameter |
| 1402 | -------------------------------------------------*/ |
| 1403 | |
| 1404 | static void emit_or_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1405 | { |
| 1406 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1407 | { |
| 1408 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1409 | /* skip */; |
| 1410 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1411 | emit_mov_r32_imm(dst, reg, -1); // mov reg,-1 |
| 1412 | else |
| 1413 | emit_or_r32_imm(dst, reg, param->value); // or reg,param |
| 1414 | } |
| 1415 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1416 | emit_or_r32_m32(dst, reg, MABS(param->value)); // or reg,[param] |
| 1417 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1418 | emit_or_r32_r32(dst, reg, param->value); // or reg,param |
| 1419 | } |
| 1420 | |
| 1421 | |
| 1422 | /*------------------------------------------------- |
| 1423 | emit_or_m32_p32 - or operation to a 32-bit |
| 1424 | memory location from a 32-bit parameter |
| 1425 | -------------------------------------------------*/ |
| 1426 | |
| 1427 | static void emit_or_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1428 | { |
| 1429 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1430 | { |
| 1431 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1432 | /* skip */; |
| 1433 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1434 | emit_mov_m32_imm(dst, MEMPARAMS, -1); // mov [dest],-1 |
| 1435 | else |
| 1436 | emit_or_m32_imm(dst, MEMPARAMS, param->value); // or [dest],param |
| 1437 | } |
| 1438 | else |
| 1439 | { |
| 1440 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1441 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1442 | emit_or_m32_r32(dst, MEMPARAMS, reg); // or [dest],reg |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | /*------------------------------------------------- |
| 1448 | emit_xor_r32_p32 - xor operation to a 32-bit |
| 1449 | register from a 32-bit parameter |
| 1450 | -------------------------------------------------*/ |
| 1451 | |
| 1452 | static void emit_xor_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1453 | { |
| 1454 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1455 | { |
| 1456 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1457 | /* skip */; |
| 1458 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1459 | emit_not_r32(dst, reg); // not reg |
| 1460 | else |
| 1461 | emit_xor_r32_imm(dst, reg, param->value); // xor reg,param |
| 1462 | } |
| 1463 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1464 | emit_xor_r32_m32(dst, reg, MABS(param->value)); // xor reg,[param] |
| 1465 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1466 | emit_xor_r32_r32(dst, reg, param->value); // xor reg,param |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | /*------------------------------------------------- |
| 1471 | emit_xor_m32_p32 - xor operation to a 32-bit |
| 1472 | memory location from a 32-bit parameter |
| 1473 | -------------------------------------------------*/ |
| 1474 | |
| 1475 | static void emit_xor_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1476 | { |
| 1477 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1478 | { |
| 1479 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1480 | /* skip */; |
| 1481 | else if (inst->condflags != 0 && (UINT32)param->value == 0xffffffff) |
| 1482 | emit_not_m32(dst, MEMPARAMS); // not [dest] |
| 1483 | else |
| 1484 | emit_xor_m32_imm(dst, MEMPARAMS, param->value); // xor [dest],param |
| 1485 | } |
| 1486 | else |
| 1487 | { |
| 1488 | int reg = param_select_register(REG_EAX, param, NULL); |
| 1489 | emit_mov_r32_p32(drcbe, dst, reg, param); // mov reg,param |
| 1490 | emit_xor_m32_r32(dst, MEMPARAMS, reg); // xor [dest],reg |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | |
| 1495 | /*------------------------------------------------- |
| 1496 | emit_shl_r32_p32 - shl operation to a 32-bit |
| 1497 | register from a 32-bit parameter |
| 1498 | -------------------------------------------------*/ |
| 1499 | |
| 1500 | static void emit_shl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1501 | { |
| 1502 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1503 | { |
| 1504 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1505 | /* skip */; |
| 1506 | else |
| 1507 | emit_shl_r32_imm(dst, reg, param->value); // shl reg,param |
| 1508 | } |
| 1509 | else |
| 1510 | { |
| 1511 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1512 | emit_shl_r32_cl(dst, reg); // shl reg,cl |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | |
| 1517 | /*------------------------------------------------- |
| 1518 | emit_shl_m32_p32 - shl operation to a 32-bit |
| 1519 | memory location from a 32-bit parameter |
| 1520 | -------------------------------------------------*/ |
| 1521 | |
| 1522 | static void emit_shl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1523 | { |
| 1524 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1525 | { |
| 1526 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1527 | /* skip */; |
| 1528 | else |
| 1529 | emit_shl_m32_imm(dst, MEMPARAMS, param->value); // shl [dest],param |
| 1530 | } |
| 1531 | else |
| 1532 | { |
| 1533 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1534 | emit_shl_m32_cl(dst, MEMPARAMS); // shl [dest],cl |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | |
| 1539 | /*------------------------------------------------- |
| 1540 | emit_shr_r32_p32 - shr operation to a 32-bit |
| 1541 | register from a 32-bit parameter |
| 1542 | -------------------------------------------------*/ |
| 1543 | |
| 1544 | static void emit_shr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1545 | { |
| 1546 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1547 | { |
| 1548 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1549 | /* skip */; |
| 1550 | else |
| 1551 | emit_shr_r32_imm(dst, reg, param->value); // shr reg,param |
| 1552 | } |
| 1553 | else |
| 1554 | { |
| 1555 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1556 | emit_shr_r32_cl(dst, reg); // shr reg,cl |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | /*------------------------------------------------- |
| 1562 | emit_shr_m32_p32 - shr operation to a 32-bit |
| 1563 | memory location from a 32-bit parameter |
| 1564 | -------------------------------------------------*/ |
| 1565 | |
| 1566 | static void emit_shr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1567 | { |
| 1568 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1569 | { |
| 1570 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1571 | /* skip */; |
| 1572 | else |
| 1573 | emit_shr_m32_imm(dst, MEMPARAMS, param->value); // shr [dest],param |
| 1574 | } |
| 1575 | else |
| 1576 | { |
| 1577 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1578 | emit_shr_m32_cl(dst, MEMPARAMS); // shr [dest],cl |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | /*------------------------------------------------- |
| 1584 | emit_sar_r32_p32 - sar operation to a 32-bit |
| 1585 | register from a 32-bit parameter |
| 1586 | -------------------------------------------------*/ |
| 1587 | |
| 1588 | static void emit_sar_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1589 | { |
| 1590 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1591 | { |
| 1592 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1593 | /* skip */; |
| 1594 | else |
| 1595 | emit_sar_r32_imm(dst, reg, param->value); // sar reg,param |
| 1596 | } |
| 1597 | else |
| 1598 | { |
| 1599 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1600 | emit_sar_r32_cl(dst, reg); // sar reg,cl |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | /*------------------------------------------------- |
| 1606 | emit_sar_m32_p32 - sar operation to a 32-bit |
| 1607 | memory location from a 32-bit parameter |
| 1608 | -------------------------------------------------*/ |
| 1609 | |
| 1610 | static void emit_sar_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1611 | { |
| 1612 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1613 | { |
| 1614 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1615 | /* skip */; |
| 1616 | else |
| 1617 | emit_sar_m32_imm(dst, MEMPARAMS, param->value); // sar [dest],param |
| 1618 | } |
| 1619 | else |
| 1620 | { |
| 1621 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1622 | emit_sar_m32_cl(dst, MEMPARAMS); // sar [dest],cl |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | |
| 1627 | /*------------------------------------------------- |
| 1628 | emit_rol_r32_p32 - rol operation to a 32-bit |
| 1629 | register from a 32-bit parameter |
| 1630 | -------------------------------------------------*/ |
| 1631 | |
| 1632 | static void emit_rol_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1633 | { |
| 1634 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1635 | { |
| 1636 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1637 | /* skip */; |
| 1638 | else |
| 1639 | emit_rol_r32_imm(dst, reg, param->value); // rol reg,param |
| 1640 | } |
| 1641 | else |
| 1642 | { |
| 1643 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1644 | emit_rol_r32_cl(dst, reg); // rol reg,cl |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | /*------------------------------------------------- |
| 1650 | emit_rol_m32_p32 - rol operation to a 32-bit |
| 1651 | memory location from a 32-bit parameter |
| 1652 | -------------------------------------------------*/ |
| 1653 | |
| 1654 | static void emit_rol_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1655 | { |
| 1656 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1657 | { |
| 1658 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1659 | /* skip */; |
| 1660 | else |
| 1661 | emit_rol_m32_imm(dst, MEMPARAMS, param->value); // rol [dest],param |
| 1662 | } |
| 1663 | else |
| 1664 | { |
| 1665 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1666 | emit_rol_m32_cl(dst, MEMPARAMS); // rol [dest],cl |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | |
| 1671 | /*------------------------------------------------- |
| 1672 | emit_ror_r32_p32 - ror operation to a 32-bit |
| 1673 | register from a 32-bit parameter |
| 1674 | -------------------------------------------------*/ |
| 1675 | |
| 1676 | static void emit_ror_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1677 | { |
| 1678 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1679 | { |
| 1680 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1681 | /* skip */; |
| 1682 | else |
| 1683 | emit_ror_r32_imm(dst, reg, param->value); // ror reg,param |
| 1684 | } |
| 1685 | else |
| 1686 | { |
| 1687 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1688 | emit_ror_r32_cl(dst, reg); // ror reg,cl |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | /*------------------------------------------------- |
| 1694 | emit_ror_m32_p32 - ror operation to a 32-bit |
| 1695 | memory location from a 32-bit parameter |
| 1696 | -------------------------------------------------*/ |
| 1697 | |
| 1698 | static void emit_ror_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1699 | { |
| 1700 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1701 | { |
| 1702 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1703 | /* skip */; |
| 1704 | else |
| 1705 | emit_ror_m32_imm(dst, MEMPARAMS, param->value); // ror [dest],param |
| 1706 | } |
| 1707 | else |
| 1708 | { |
| 1709 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1710 | emit_ror_m32_cl(dst, MEMPARAMS); // ror [dest],cl |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | /*------------------------------------------------- |
| 1716 | emit_rcl_r32_p32 - rcl operation to a 32-bit |
| 1717 | register from a 32-bit parameter |
| 1718 | -------------------------------------------------*/ |
| 1719 | |
| 1720 | static void emit_rcl_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1721 | { |
| 1722 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1723 | { |
| 1724 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1725 | /* skip */; |
| 1726 | else |
| 1727 | emit_rcl_r32_imm(dst, reg, param->value); // rcl reg,param |
| 1728 | } |
| 1729 | else |
| 1730 | { |
| 1731 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1732 | emit_rcl_r32_cl(dst, reg); // rcl reg,cl |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | /*------------------------------------------------- |
| 1738 | emit_rcl_m32_p32 - rcl operation to a 32-bit |
| 1739 | memory location from a 32-bit parameter |
| 1740 | -------------------------------------------------*/ |
| 1741 | |
| 1742 | static void emit_rcl_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1743 | { |
| 1744 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1745 | { |
| 1746 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1747 | /* skip */; |
| 1748 | else |
| 1749 | emit_rcl_m32_imm(dst, MEMPARAMS, param->value); // rcl [dest],param |
| 1750 | } |
| 1751 | else |
| 1752 | { |
| 1753 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1754 | emit_rcl_m32_cl(dst, MEMPARAMS); // rcl [dest],cl |
| 1755 | } |
| 1756 | } |
| 1757 | |
| 1758 | |
| 1759 | /*------------------------------------------------- |
| 1760 | emit_rcr_r32_p32 - rcr operation to a 32-bit |
| 1761 | register from a 32-bit parameter |
| 1762 | -------------------------------------------------*/ |
| 1763 | |
| 1764 | static void emit_rcr_r32_p32(drcbe_state *drcbe, x86code **dst, UINT8 reg, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1765 | { |
| 1766 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1767 | { |
| 1768 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1769 | /* skip */; |
| 1770 | else |
| 1771 | emit_rcr_r32_imm(dst, reg, param->value); // rcr reg,param |
| 1772 | } |
| 1773 | else |
| 1774 | { |
| 1775 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1776 | emit_rcr_r32_cl(dst, reg); // rcr reg,cl |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | /*------------------------------------------------- |
| 1782 | emit_rcr_m32_p32 - rcr operation to a 32-bit |
| 1783 | memory location from a 32-bit parameter |
| 1784 | -------------------------------------------------*/ |
| 1785 | |
| 1786 | static void emit_rcr_m32_p32(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1787 | { |
| 1788 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1789 | { |
| 1790 | if (inst->condflags != 0 && (UINT32)param->value == 0) |
| 1791 | /* skip */; |
| 1792 | else |
| 1793 | emit_rcr_m32_imm(dst, MEMPARAMS, param->value); // rcr [dest],param |
| 1794 | } |
| 1795 | else |
| 1796 | { |
| 1797 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 1798 | emit_rcr_m32_cl(dst, MEMPARAMS); // rcr [dest],cl |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | |
| 1803 | |
| 1804 | /*************************************************************************** |
| 1805 | EMITTERS FOR 64-BIT OPERATIONS WITH PARAMETERS |
| 1806 | ***************************************************************************/ |
| 1807 | |
| 1808 | /*------------------------------------------------- |
| 1809 | emit_mov_r64_p64 - move a 64-bit parameter |
| 1810 | into a pair of registers |
| 1811 | -------------------------------------------------*/ |
| 1812 | |
| 1813 | static void emit_mov_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param) |
| 1814 | { |
| 1815 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1816 | { |
| 1817 | if ((UINT32)param->value == 0) |
| 1818 | emit_xor_r32_r32(dst, reglo, reglo); // xor reglo,reglo |
| 1819 | else |
| 1820 | emit_mov_r32_imm(dst, reglo, param->value); // mov reglo,param |
| 1821 | if ((UINT32)(param->value >> 32) == 0) |
| 1822 | emit_xor_r32_r32(dst, reghi, reghi); // xor reghi,reghi |
| 1823 | else |
| 1824 | emit_mov_r32_imm(dst, reghi, param->value >> 32); // mov reghi,param >> 32 |
| 1825 | } |
| 1826 | else if (param->type == DRCUML_PTYPE_MEMORY) |
| 1827 | { |
| 1828 | emit_mov_r32_m32(dst, reglo, MABS(param->value)); // mov reglo,[param] |
| 1829 | emit_mov_r32_m32(dst, reghi, MABS(param->value + 4)); // mov reghi,[param+4] |
| 1830 | } |
| 1831 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1832 | { |
| 1833 | if (reglo != param->value) |
| 1834 | emit_mov_r32_r32(dst, reglo, param->value); // mov reglo,param |
| 1835 | emit_mov_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // mov reghi,reghi[param] |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | |
| 1840 | /*------------------------------------------------- |
| 1841 | emit_mov_p64_r64 - move a pair of registers |
| 1842 | into a 64-bit parameter |
| 1843 | -------------------------------------------------*/ |
| 1844 | |
| 1845 | static void emit_mov_p64_r64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *param, UINT8 reglo, UINT8 reghi) |
| 1846 | { |
| 1847 | assert(param->type != DRCUML_PTYPE_IMMEDIATE); |
| 1848 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1849 | { |
| 1850 | emit_mov_m32_r32(dst, MABS(param->value), reglo); // mov [param],reglo |
| 1851 | emit_mov_m32_r32(dst, MABS(param->value + 4), reghi); // mov [param+4],reghi |
| 1852 | } |
| 1853 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1854 | { |
| 1855 | if (reglo != param->value) |
| 1856 | emit_mov_r32_r32(dst, param->value, reglo); // mov param,reglo |
| 1857 | emit_mov_m32_r32(dst, MABS(drcbe->reghi[param->value]), reghi); // mov reghi[param],reghi |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | |
| 1862 | /*------------------------------------------------- |
| 1863 | emit_push_p64 - push a 64-bit parameter onto |
| 1864 | the stack |
| 1865 | -------------------------------------------------*/ |
| 1866 | |
| 1867 | static void emit_push_p64(drcbe_state *drcbe, x86code **dst, const drcuml_parameter *src) |
| 1868 | { |
| 1869 | if (src->type == DRCUML_PTYPE_IMMEDIATE) |
| 1870 | { |
| 1871 | emit_push_imm(dst, src->value >> 32); // push src >> 32 |
| 1872 | emit_push_imm(dst, src->value); // push src |
| 1873 | } |
| 1874 | else if (src->type == DRCUML_PTYPE_MEMORY) |
| 1875 | { |
| 1876 | emit_push_m32(dst, MABS(src->value + 4)); // push [src+4] |
| 1877 | emit_push_m32(dst, MABS(src->value)); // push [src] |
| 1878 | } |
| 1879 | else if (src->type == DRCUML_PTYPE_INT_REGISTER) |
| 1880 | { |
| 1881 | emit_push_m32(dst, MABS(drcbe->reghi[src->value])); // push reghi[src] |
| 1882 | emit_push_r32(dst, src->value); // push src |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | |
| 1887 | /*------------------------------------------------- |
| 1888 | emit_add_r64_p64 - add operation to a 64-bit |
| 1889 | pair of registers from a 64-bit parameter |
| 1890 | -------------------------------------------------*/ |
| 1891 | |
| 1892 | static void emit_add_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1893 | { |
| 1894 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 1895 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1896 | { |
| 1897 | emit_add_r32_m32(dst, reglo, MABS(param->value)); // add reglo,[param] |
| 1898 | if (saveflags) emit_pushf(dst); // pushf |
| 1899 | emit_adc_r32_m32(dst, reghi, MABS(param->value + 4)); // adc reghi,[param] |
| 1900 | } |
| 1901 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1902 | { |
| 1903 | emit_add_r32_imm(dst, reglo, param->value); // add reglo,param |
| 1904 | if (saveflags) emit_pushf(dst); // pushf |
| 1905 | emit_adc_r32_imm(dst, reghi, param->value >> 32); // adc reghi,param >> 32 |
| 1906 | } |
| 1907 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1908 | { |
| 1909 | emit_add_r32_r32(dst, reglo, param->value); // add reglo,param |
| 1910 | if (saveflags) emit_pushf(dst); // pushf |
| 1911 | emit_adc_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // adc reghi,reghi[param] |
| 1912 | } |
| 1913 | if (saveflags) |
| 1914 | emit_combine_z_flags(dst); |
| 1915 | } |
| 1916 | |
| 1917 | |
| 1918 | /*------------------------------------------------- |
| 1919 | emit_add_m64_p64 - add operation to a 64-bit |
| 1920 | memory location from a 64-bit parameter |
| 1921 | -------------------------------------------------*/ |
| 1922 | |
| 1923 | static void emit_add_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1924 | { |
| 1925 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 1926 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1927 | { |
| 1928 | emit_add_m32_imm(dst, MEMPARAMS, param->value); // add [dest],param |
| 1929 | if (saveflags) emit_pushf(dst); // pushf |
| 1930 | emit_adc_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // adc [dest+4],param >> 32 |
| 1931 | } |
| 1932 | else |
| 1933 | { |
| 1934 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 1935 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 1936 | emit_add_m32_r32(dst, MEMPARAMS, reglo); // add [dest],reglo |
| 1937 | if (saveflags) emit_pushf(dst); // pushf |
| 1938 | emit_adc_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // adc [dest+4],edx |
| 1939 | } |
| 1940 | if (saveflags) |
| 1941 | emit_combine_z_flags(dst); |
| 1942 | } |
| 1943 | |
| 1944 | |
| 1945 | /*------------------------------------------------- |
| 1946 | emit_adc_r64_p64 - adc operation to a 64-bit |
| 1947 | pair of registers from a 64-bit parameter |
| 1948 | -------------------------------------------------*/ |
| 1949 | |
| 1950 | static void emit_adc_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1951 | { |
| 1952 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 1953 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 1954 | { |
| 1955 | emit_adc_r32_m32(dst, reglo, MABS(param->value)); // adc reglo,[param] |
| 1956 | if (saveflags) emit_pushf(dst); // pushf |
| 1957 | emit_adc_r32_m32(dst, reghi, MABS(param->value + 4)); // adc reghi,[param] |
| 1958 | } |
| 1959 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1960 | { |
| 1961 | emit_adc_r32_imm(dst, reglo, param->value); // adc reglo,param |
| 1962 | if (saveflags) emit_pushf(dst); // pushf |
| 1963 | emit_adc_r32_imm(dst, reghi, param->value >> 32); // adc reghi,param >> 32 |
| 1964 | } |
| 1965 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 1966 | { |
| 1967 | emit_adc_r32_r32(dst, reglo, param->value); // adc reglo,param |
| 1968 | if (saveflags) emit_pushf(dst); // pushf |
| 1969 | emit_adc_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // adc reghi,reghi[param] |
| 1970 | } |
| 1971 | if (saveflags) |
| 1972 | emit_combine_z_flags(dst); |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | /*------------------------------------------------- |
| 1977 | emit_adc_m64_p64 - adc operation to a 64-bit |
| 1978 | memory location from a 64-bit parameter |
| 1979 | -------------------------------------------------*/ |
| 1980 | |
| 1981 | static void emit_adc_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 1982 | { |
| 1983 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 1984 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 1985 | { |
| 1986 | emit_adc_m32_imm(dst, MEMPARAMS, param->value); // adc [dest],param |
| 1987 | if (saveflags) emit_pushf(dst); // pushf |
| 1988 | emit_adc_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // adc [dest+4],param >> 32 |
| 1989 | } |
| 1990 | else |
| 1991 | { |
| 1992 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 1993 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 1994 | emit_adc_m32_r32(dst, MEMPARAMS, reglo); // adc [dest],reglo |
| 1995 | if (saveflags) emit_pushf(dst); // pushf |
| 1996 | emit_adc_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // adc [dest+4],edx |
| 1997 | } |
| 1998 | if (saveflags) |
| 1999 | emit_combine_z_flags(dst); |
| 2000 | } |
| 2001 | |
| 2002 | |
| 2003 | /*------------------------------------------------- |
| 2004 | emit_sub_r64_p64 - sub operation to a 64-bit |
| 2005 | pair of registers from a 64-bit parameter |
| 2006 | -------------------------------------------------*/ |
| 2007 | |
| 2008 | static void emit_sub_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2009 | { |
| 2010 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2011 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2012 | { |
| 2013 | emit_sub_r32_m32(dst, reglo, MABS(param->value)); // sub reglo,[param] |
| 2014 | if (saveflags) emit_pushf(dst); // pushf |
| 2015 | emit_sbb_r32_m32(dst, reghi, MABS(param->value + 4)); // sbb reghi,[param] |
| 2016 | } |
| 2017 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2018 | { |
| 2019 | emit_sub_r32_imm(dst, reglo, param->value); // sub reglo,param |
| 2020 | if (saveflags) emit_pushf(dst); // pushf |
| 2021 | emit_sbb_r32_imm(dst, reghi, param->value >> 32); // sbb reghi,param >> 32 |
| 2022 | } |
| 2023 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2024 | { |
| 2025 | emit_sub_r32_r32(dst, reglo, param->value); // sub reglo,param |
| 2026 | if (saveflags) emit_pushf(dst); // pushf |
| 2027 | emit_sbb_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // sbb reghi,reghi[param] |
| 2028 | } |
| 2029 | if (saveflags) |
| 2030 | emit_combine_z_flags(dst); |
| 2031 | } |
| 2032 | |
| 2033 | |
| 2034 | /*------------------------------------------------- |
| 2035 | emit_sub_m64_p64 - sub operation to a 64-bit |
| 2036 | memory location from a 64-bit parameter |
| 2037 | -------------------------------------------------*/ |
| 2038 | |
| 2039 | static void emit_sub_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2040 | { |
| 2041 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2042 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2043 | { |
| 2044 | emit_sub_m32_imm(dst, MEMPARAMS, param->value); // sub [dest],param |
| 2045 | if (saveflags) emit_pushf(dst); // pushf |
| 2046 | emit_sbb_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // sbb [dest+4],param >> 32 |
| 2047 | } |
| 2048 | else |
| 2049 | { |
| 2050 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2051 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2052 | emit_sub_m32_r32(dst, MEMPARAMS, reglo); // sub [dest],reglo |
| 2053 | if (saveflags) emit_pushf(dst); // pushf |
| 2054 | emit_sbb_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // sbb [dest+4],edx |
| 2055 | } |
| 2056 | if (saveflags) |
| 2057 | emit_combine_z_flags(dst); |
| 2058 | } |
| 2059 | |
| 2060 | |
| 2061 | /*------------------------------------------------- |
| 2062 | emit_sbb_r64_p64 - sbb operation to a 64-bit |
| 2063 | pair of registers from a 64-bit parameter |
| 2064 | -------------------------------------------------*/ |
| 2065 | |
| 2066 | static void emit_sbb_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2067 | { |
| 2068 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2069 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2070 | { |
| 2071 | emit_sbb_r32_m32(dst, reglo, MABS(param->value)); // sbb reglo,[param] |
| 2072 | if (saveflags) emit_pushf(dst); // pushf |
| 2073 | emit_sbb_r32_m32(dst, reghi, MABS(param->value + 4)); // sbb reghi,[param] |
| 2074 | } |
| 2075 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2076 | { |
| 2077 | emit_sbb_r32_imm(dst, reglo, param->value); // sbb reglo,param |
| 2078 | if (saveflags) emit_pushf(dst); // pushf |
| 2079 | emit_sbb_r32_imm(dst, reghi, param->value >> 32); // sbb reghi,param >> 32 |
| 2080 | } |
| 2081 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2082 | { |
| 2083 | emit_sbb_r32_r32(dst, reglo, param->value); // sbb reglo,param |
| 2084 | if (saveflags) emit_pushf(dst); // pushf |
| 2085 | emit_sbb_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // sbb reghi,reghi[param] |
| 2086 | } |
| 2087 | if (saveflags) |
| 2088 | emit_combine_z_flags(dst); |
| 2089 | } |
| 2090 | |
| 2091 | |
| 2092 | /*------------------------------------------------- |
| 2093 | emit_sbb_m64_p64 - sbb operation to a 64-bit |
| 2094 | memory location from a 64-bit parameter |
| 2095 | -------------------------------------------------*/ |
| 2096 | |
| 2097 | static void emit_sbb_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2098 | { |
| 2099 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2100 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2101 | { |
| 2102 | emit_sbb_m32_imm(dst, MEMPARAMS, param->value); // sbb [dest],param |
| 2103 | if (saveflags) emit_pushf(dst); // pushf |
| 2104 | emit_sbb_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // sbb [dest+4],param >> 32 |
| 2105 | } |
| 2106 | else |
| 2107 | { |
| 2108 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2109 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2110 | emit_sbb_m32_r32(dst, MEMPARAMS, reglo); // sbb [dest],reglo |
| 2111 | if (saveflags) emit_pushf(dst); // pushf |
| 2112 | emit_sbb_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // sbb [dest+4],edx |
| 2113 | } |
| 2114 | if (saveflags) |
| 2115 | emit_combine_z_flags(dst); |
| 2116 | } |
| 2117 | |
| 2118 | |
| 2119 | /*------------------------------------------------- |
| 2120 | emit_and_r64_p64 - and operation to a 64-bit |
| 2121 | pair of registers from a 64-bit parameter |
| 2122 | -------------------------------------------------*/ |
| 2123 | |
| 2124 | static void emit_and_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2125 | { |
| 2126 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2127 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2128 | { |
| 2129 | emit_and_r32_m32(dst, reglo, MABS(param->value)); // and reglo,[param] |
| 2130 | if (saveflags) emit_pushf(dst); // pushf |
| 2131 | emit_and_r32_m32(dst, reghi, MABS(param->value + 4)); // and reghi,[param] |
| 2132 | } |
| 2133 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2134 | { |
| 2135 | if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2136 | /* skip */; |
| 2137 | else if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2138 | emit_xor_r32_r32(dst, reglo, reglo); // xor reglo,reglo |
| 2139 | else |
| 2140 | emit_and_r32_imm(dst, reglo, param->value); // and reglo,param |
| 2141 | if (saveflags) emit_pushf(dst); // pushf |
| 2142 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2143 | /* skip */; |
| 2144 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2145 | emit_xor_r32_r32(dst, reghi, reghi); // xor reghi,reghi |
| 2146 | else |
| 2147 | emit_and_r32_imm(dst, reghi, param->value >> 32); // and reghi,param >> 32 |
| 2148 | } |
| 2149 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2150 | { |
| 2151 | emit_and_r32_r32(dst, reglo, param->value); // and reglo,param |
| 2152 | if (saveflags) emit_pushf(dst); // pushf |
| 2153 | emit_and_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // and reghi,reghi[param] |
| 2154 | } |
| 2155 | if (saveflags) |
| 2156 | emit_combine_z_flags(dst); |
| 2157 | } |
| 2158 | |
| 2159 | |
| 2160 | /*------------------------------------------------- |
| 2161 | emit_and_m64_p64 - and operation to a 64-bit |
| 2162 | memory location from a 64-bit parameter |
| 2163 | -------------------------------------------------*/ |
| 2164 | |
| 2165 | static void emit_and_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2166 | { |
| 2167 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2168 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2169 | { |
| 2170 | if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2171 | /* skip */; |
| 2172 | else if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2173 | emit_mov_m32_imm(dst, MEMPARAMS, 0); // mov [dest],0 |
| 2174 | else |
| 2175 | emit_and_m32_imm(dst, MEMPARAMS, param->value); // and [dest],param |
| 2176 | if (saveflags) emit_pushf(dst); // pushf |
| 2177 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2178 | /* skip */; |
| 2179 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2180 | emit_mov_m32_imm(dst, MEMPARAMS + 4, 0); // mov [dest+4],0 |
| 2181 | else |
| 2182 | emit_and_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // and [dest+4],param >> 32 |
| 2183 | } |
| 2184 | else |
| 2185 | { |
| 2186 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2187 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2188 | emit_and_m32_r32(dst, MEMPARAMS, reglo); // and [dest],reglo |
| 2189 | if (saveflags) emit_pushf(dst); // pushf |
| 2190 | emit_and_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // and [dest+4],edx |
| 2191 | } |
| 2192 | if (saveflags) |
| 2193 | emit_combine_z_flags(dst); |
| 2194 | } |
| 2195 | |
| 2196 | |
| 2197 | /*------------------------------------------------- |
| 2198 | emit_test_r64_p64 - test operation to a 64-bit |
| 2199 | pair of registers from a 64-bit parameter |
| 2200 | -------------------------------------------------*/ |
| 2201 | |
| 2202 | static void emit_test_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2203 | { |
| 2204 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2205 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2206 | { |
| 2207 | emit_test_m32_r32(dst, MABS(param->value), reglo); // test [param],reglo |
| 2208 | if (saveflags) emit_pushf(dst); // pushf |
| 2209 | emit_test_m32_r32(dst, MABS(param->value + 4), reghi); // test [param],reghi |
| 2210 | } |
| 2211 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2212 | { |
| 2213 | emit_test_r32_imm(dst, reglo, param->value); // test reglo,param |
| 2214 | if (saveflags) emit_pushf(dst); // pushf |
| 2215 | emit_test_r32_imm(dst, reghi, param->value >> 32); // test reghi,param >> 32 |
| 2216 | } |
| 2217 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2218 | { |
| 2219 | emit_test_r32_r32(dst, reglo, param->value); // test reglo,param |
| 2220 | if (saveflags) emit_pushf(dst); // pushf |
| 2221 | emit_test_m32_r32(dst, MABS(drcbe->reghi[param->value]), reghi); // test reghi[param],reghi |
| 2222 | } |
| 2223 | if (saveflags) |
| 2224 | emit_combine_z_flags(dst); |
| 2225 | } |
| 2226 | |
| 2227 | |
| 2228 | /*------------------------------------------------- |
| 2229 | emit_test_m64_p64 - test operation to a 64-bit |
| 2230 | memory location from a 64-bit parameter |
| 2231 | -------------------------------------------------*/ |
| 2232 | |
| 2233 | static void emit_test_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2234 | { |
| 2235 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2236 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2237 | { |
| 2238 | emit_test_m32_imm(dst, MEMPARAMS, param->value); // test [dest],param |
| 2239 | if (saveflags) emit_pushf(dst); // pushf |
| 2240 | emit_test_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // test [dest+4],param >> 32 |
| 2241 | } |
| 2242 | else |
| 2243 | { |
| 2244 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2245 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2246 | emit_test_m32_r32(dst, MEMPARAMS, reglo); // test [dest],reglo |
| 2247 | if (saveflags) emit_pushf(dst); // pushf |
| 2248 | emit_test_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // test [dest+4],edx |
| 2249 | } |
| 2250 | if (saveflags) |
| 2251 | emit_combine_z_flags(dst); |
| 2252 | } |
| 2253 | |
| 2254 | |
| 2255 | /*------------------------------------------------- |
| 2256 | emit_or_r64_p64 - or operation to a 64-bit |
| 2257 | pair of registers from a 64-bit parameter |
| 2258 | -------------------------------------------------*/ |
| 2259 | |
| 2260 | static void emit_or_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2261 | { |
| 2262 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2263 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2264 | { |
| 2265 | emit_or_r32_m32(dst, reglo, MABS(param->value)); // or reglo,[param] |
| 2266 | if (saveflags) emit_pushf(dst); // pushf |
| 2267 | emit_or_r32_m32(dst, reghi, MABS(param->value + 4)); // or reghi,[param] |
| 2268 | } |
| 2269 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2270 | { |
| 2271 | if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2272 | /* skip */; |
| 2273 | else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2274 | emit_mov_r32_imm(dst, reglo, -1); // mov reglo,-1 |
| 2275 | else |
| 2276 | emit_or_r32_imm(dst, reglo, param->value); // or reglo,param |
| 2277 | if (saveflags) emit_pushf(dst); // pushf |
| 2278 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2279 | /* skip */; |
| 2280 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2281 | emit_mov_r32_imm(dst, reghi, -1); // mov reghi,-1 |
| 2282 | else |
| 2283 | emit_or_r32_imm(dst, reghi, param->value >> 32); // or reghi,param >> 32 |
| 2284 | } |
| 2285 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2286 | { |
| 2287 | emit_or_r32_r32(dst, reglo, param->value); // or reglo,param |
| 2288 | if (saveflags) emit_pushf(dst); // pushf |
| 2289 | emit_or_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // or reghi,reghi[param] |
| 2290 | } |
| 2291 | if (saveflags) |
| 2292 | emit_combine_z_flags(dst); |
| 2293 | } |
| 2294 | |
| 2295 | |
| 2296 | /*------------------------------------------------- |
| 2297 | emit_or_m64_p64 - or operation to a 64-bit |
| 2298 | memory location from a 64-bit parameter |
| 2299 | -------------------------------------------------*/ |
| 2300 | |
| 2301 | static void emit_or_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2302 | { |
| 2303 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2304 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2305 | { |
| 2306 | if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2307 | /* skip */; |
| 2308 | else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2309 | emit_mov_m32_imm(dst, MEMPARAMS, -1); // mov [dest],-1 |
| 2310 | else |
| 2311 | emit_or_m32_imm(dst, MEMPARAMS, param->value); // or [dest],param |
| 2312 | if (saveflags) emit_pushf(dst); // pushf |
| 2313 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2314 | /* skip */; |
| 2315 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2316 | emit_mov_m32_imm(dst, MEMPARAMS + 4, -1); // mov [dest+4],-1 |
| 2317 | else |
| 2318 | emit_or_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // or [dest+4],param >> 32 |
| 2319 | } |
| 2320 | else |
| 2321 | { |
| 2322 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2323 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2324 | emit_or_m32_r32(dst, MEMPARAMS, reglo); // or [dest],reglo |
| 2325 | if (saveflags) emit_pushf(dst); // pushf |
| 2326 | emit_or_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // or [dest+4],edx |
| 2327 | } |
| 2328 | if (saveflags) |
| 2329 | emit_combine_z_flags(dst); |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /*------------------------------------------------- |
| 2334 | emit_xor_r64_p64 - xor operation to a 64-bit |
| 2335 | pair of registers from a 64-bit parameter |
| 2336 | -------------------------------------------------*/ |
| 2337 | |
| 2338 | static void emit_xor_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2339 | { |
| 2340 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2341 | if (param->type == DRCUML_PTYPE_MEMORY) |
| 2342 | { |
| 2343 | emit_xor_r32_m32(dst, reglo, MABS(param->value)); // xor reglo,[param] |
| 2344 | if (saveflags) emit_pushf(dst); // pushf |
| 2345 | emit_xor_r32_m32(dst, reghi, MABS(param->value + 4)); // xor reghi,[param] |
| 2346 | } |
| 2347 | else if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2348 | { |
| 2349 | if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2350 | /* skip */; |
| 2351 | else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2352 | emit_not_r32(dst, reglo); // not reglo |
| 2353 | else |
| 2354 | emit_xor_r32_imm(dst, reglo, param->value); // xor reglo,param |
| 2355 | if (saveflags) emit_pushf(dst); // pushf |
| 2356 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2357 | /* skip */; |
| 2358 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2359 | emit_not_r32(dst, reghi); // not reghi |
| 2360 | else |
| 2361 | emit_xor_r32_imm(dst, reghi, param->value >> 32); // xor reghi,param >> 32 |
| 2362 | } |
| 2363 | else if (param->type == DRCUML_PTYPE_INT_REGISTER) |
| 2364 | { |
| 2365 | emit_xor_r32_r32(dst, reglo, param->value); // xor reglo,param |
| 2366 | if (saveflags) emit_pushf(dst); // pushf |
| 2367 | emit_xor_r32_m32(dst, reghi, MABS(drcbe->reghi[param->value])); // xor reghi,reghi[param] |
| 2368 | } |
| 2369 | if (saveflags) |
| 2370 | emit_combine_z_flags(dst); |
| 2371 | } |
| 2372 | |
| 2373 | |
| 2374 | /*------------------------------------------------- |
| 2375 | emit_xor_m64_p64 - xor operation to a 64-bit |
| 2376 | memory location from a 64-bit parameter |
| 2377 | -------------------------------------------------*/ |
| 2378 | |
| 2379 | static void emit_xor_m64_p64(drcbe_state *drcbe, x86code **dst, DECLARE_MEMPARAMS, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2380 | { |
| 2381 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2382 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2383 | { |
| 2384 | if (inst->condflags == 0 && (UINT32)param->value == 0) |
| 2385 | /* skip */; |
| 2386 | else if (inst->condflags == 0 && (UINT32)param->value == 0xffffffff) |
| 2387 | emit_not_m32(dst, MEMPARAMS); // not [dest] |
| 2388 | else |
| 2389 | emit_xor_m32_imm(dst, MEMPARAMS, param->value); // xor [dest],param |
| 2390 | if (saveflags) emit_pushf(dst); // pushf |
| 2391 | if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0) |
| 2392 | /* skip */; |
| 2393 | else if (inst->condflags == 0 && (UINT32)(param->value >> 32) == 0xffffffff) |
| 2394 | emit_not_m32(dst, MEMPARAMS + 4); // not [dest+4] |
| 2395 | else |
| 2396 | emit_xor_m32_imm(dst, MEMPARAMS + 4, param->value >> 32); // xor [dest+4],param >> 32 |
| 2397 | } |
| 2398 | else |
| 2399 | { |
| 2400 | int reglo = (param->type == DRCUML_PTYPE_INT_REGISTER) ? param->value : REG_EAX; |
| 2401 | emit_mov_r64_p64(drcbe, dst, reglo, REG_EDX, param); // mov edx:reglo,param |
| 2402 | emit_xor_m32_r32(dst, MEMPARAMS, reglo); // xor [dest],reglo |
| 2403 | if (saveflags) emit_pushf(dst); // pushf |
| 2404 | emit_xor_m32_r32(dst, MEMPARAMS + 4, REG_EDX); // xor [dest+4],edx |
| 2405 | } |
| 2406 | if (saveflags) |
| 2407 | emit_combine_z_flags(dst); |
| 2408 | } |
| 2409 | |
| 2410 | |
| 2411 | /*------------------------------------------------- |
| 2412 | emit_shl_r64_p64 - shl operation to a 64-bit |
| 2413 | pair of registers from a 64-bit parameter |
| 2414 | -------------------------------------------------*/ |
| 2415 | |
| 2416 | static void emit_shl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2417 | { |
| 2418 | int saveflags = (inst->condflags != 0); |
| 2419 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2420 | { |
| 2421 | int count = param->value & 63; |
| 2422 | if (inst->condflags != 0 && count == 0) |
| 2423 | /* skip */; |
| 2424 | else |
| 2425 | { |
| 2426 | while (count >= 32) |
| 2427 | { |
| 2428 | if (inst->condflags != 0) |
| 2429 | { |
| 2430 | emit_shld_r32_r32_imm(dst, reghi, reglo, 31); // shld reghi,reglo,31 |
| 2431 | emit_shl_r32_imm(dst, reglo, 31); // shl reglo,31 |
| 2432 | count -= 31; |
| 2433 | } |
| 2434 | else |
| 2435 | { |
| 2436 | emit_mov_r32_r32(dst, reghi, reglo); // mov reghi,reglo |
| 2437 | emit_xor_r32_r32(dst, reglo, reglo); // xor reglo,reglo |
| 2438 | count -= 32; |
| 2439 | } |
| 2440 | } |
| 2441 | if (inst->condflags != 0 || count > 0) |
| 2442 | { |
| 2443 | emit_shld_r32_r32_imm(dst, reghi, reglo, count); // shld reghi,reglo,count |
| 2444 | if (saveflags) emit_pushf(dst); // pushf |
| 2445 | emit_shl_r32_imm(dst, reglo, count); // shl reglo,count |
| 2446 | } |
| 2447 | } |
| 2448 | } |
| 2449 | else |
| 2450 | { |
| 2451 | emit_link skip1, skip2; |
| 2452 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2453 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2454 | emit_jcc_short_link(dst, COND_Z, &skip1); // jz skip1 |
| 2455 | if (inst->condflags != 0) |
| 2456 | { |
| 2457 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2458 | emit_shld_r32_r32_imm(dst, reghi, reglo, 31); // shld reghi,reglo,31 |
| 2459 | emit_shl_r32_imm(dst, reglo, 31); // shl reglo,31 |
| 2460 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2461 | emit_jcc_short_link(dst, COND_Z, &skip2); // jz skip2 |
| 2462 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2463 | emit_shld_r32_r32_imm(dst, reghi, reglo, 31); // shld reghi,reglo,31 |
| 2464 | emit_shl_r32_imm(dst, reglo, 31); // shl reglo,31 |
| 2465 | resolve_link(dst, &skip2); // skip2: |
| 2466 | } |
| 2467 | else |
| 2468 | { |
| 2469 | emit_mov_r32_r32(dst, reghi, reglo); // mov reghi,reglo |
| 2470 | emit_xor_r32_r32(dst, reglo, reglo); // xor reglo,reglo |
| 2471 | } |
| 2472 | resolve_link(dst, &skip1); // skip1: |
| 2473 | emit_shld_r32_r32_cl(dst, reghi, reglo); // shld reghi,reglo,cl |
| 2474 | if (saveflags) emit_pushf(dst); // pushf |
| 2475 | emit_shl_r32_cl(dst, reglo); // shl reglo,cl |
| 2476 | } |
| 2477 | if (saveflags) |
| 2478 | emit_combine_z_shl_flags(dst); |
| 2479 | } |
| 2480 | |
| 2481 | |
| 2482 | /*------------------------------------------------- |
| 2483 | emit_shr_r64_p64 - shr operation to a 64-bit |
| 2484 | pair of registers from a 64-bit parameter |
| 2485 | -------------------------------------------------*/ |
| 2486 | |
| 2487 | static void emit_shr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2488 | { |
| 2489 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2490 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2491 | { |
| 2492 | int count = param->value & 63; |
| 2493 | if (inst->condflags != 0 && count == 0) |
| 2494 | /* skip */; |
| 2495 | else |
| 2496 | { |
| 2497 | while (count >= 32) |
| 2498 | { |
| 2499 | if (inst->condflags != 0) |
| 2500 | { |
| 2501 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2502 | emit_shr_r32_imm(dst, reghi, 31); // shr reghi,31 |
| 2503 | count -= 31; |
| 2504 | } |
| 2505 | else |
| 2506 | { |
| 2507 | emit_mov_r32_r32(dst, reglo, reghi); // mov reglo,reghi |
| 2508 | emit_xor_r32_r32(dst, reghi, reghi); // xor reghi,reghi |
| 2509 | count -= 32; |
| 2510 | } |
| 2511 | } |
| 2512 | if (inst->condflags != 0 || count > 0) |
| 2513 | { |
| 2514 | emit_shrd_r32_r32_imm(dst, reglo, reghi, count); // shrd reglo,reghi,count |
| 2515 | if (saveflags) emit_pushf(dst); // pushf |
| 2516 | emit_shr_r32_imm(dst, reghi, count); // shr reghi,count |
| 2517 | } |
| 2518 | } |
| 2519 | } |
| 2520 | else |
| 2521 | { |
| 2522 | emit_link skip1, skip2; |
| 2523 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2524 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2525 | emit_jcc_short_link(dst, COND_Z, &skip1); // jz skip1 |
| 2526 | if (inst->condflags != 0) |
| 2527 | { |
| 2528 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2529 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2530 | emit_shr_r32_imm(dst, reghi, 31); // shr reghi,31 |
| 2531 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2532 | emit_jcc_short_link(dst, COND_Z, &skip2); // jz skip2 |
| 2533 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2534 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2535 | emit_shr_r32_imm(dst, reghi, 31); // shr reghi,31 |
| 2536 | resolve_link(dst, &skip2); // skip2: |
| 2537 | } |
| 2538 | else |
| 2539 | { |
| 2540 | emit_mov_r32_r32(dst, reglo, reghi); // mov reglo,reghi |
| 2541 | emit_xor_r32_r32(dst, reghi, reghi); // xor reghi,reghi |
| 2542 | } |
| 2543 | resolve_link(dst, &skip1); // skip1: |
| 2544 | emit_shrd_r32_r32_cl(dst, reglo, reghi); // shrd reglo,reghi,cl |
| 2545 | if (saveflags) emit_pushf(dst); // pushf |
| 2546 | emit_shr_r32_cl(dst, reghi); // shr reghi,cl |
| 2547 | } |
| 2548 | if (saveflags) |
| 2549 | emit_combine_z_flags(dst); |
| 2550 | } |
| 2551 | |
| 2552 | |
| 2553 | /*------------------------------------------------- |
| 2554 | emit_sar_r64_p64 - sar operation to a 64-bit |
| 2555 | pair of registers from a 64-bit parameter |
| 2556 | -------------------------------------------------*/ |
| 2557 | |
| 2558 | static void emit_sar_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2559 | { |
| 2560 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2561 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2562 | { |
| 2563 | int count = param->value & 63; |
| 2564 | if (inst->condflags != 0 && count == 0) |
| 2565 | /* skip */; |
| 2566 | else |
| 2567 | { |
| 2568 | while (count >= 32) |
| 2569 | { |
| 2570 | if (inst->condflags != 0) |
| 2571 | { |
| 2572 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2573 | emit_sar_r32_imm(dst, reghi, 31); // sar reghi,31 |
| 2574 | count -= 31; |
| 2575 | } |
| 2576 | else |
| 2577 | { |
| 2578 | emit_mov_r32_r32(dst, reglo, reghi); // mov reglo,reghi |
| 2579 | emit_sar_r32_imm(dst, reghi, 31); // sar reghi,31 |
| 2580 | count -= 32; |
| 2581 | } |
| 2582 | } |
| 2583 | if (inst->condflags != 0 || count > 0) |
| 2584 | { |
| 2585 | emit_shrd_r32_r32_imm(dst, reglo, reghi, count); // shrd reglo,reghi,count |
| 2586 | if (saveflags) emit_pushf(dst); // pushf |
| 2587 | emit_sar_r32_imm(dst, reghi, count); // sar reghi,count |
| 2588 | } |
| 2589 | } |
| 2590 | } |
| 2591 | else |
| 2592 | { |
| 2593 | emit_link skip1, skip2; |
| 2594 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2595 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2596 | emit_jcc_short_link(dst, COND_Z, &skip1); // jz skip1 |
| 2597 | if (inst->condflags != 0) |
| 2598 | { |
| 2599 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2600 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2601 | emit_sar_r32_imm(dst, reghi, 31); // sar reghi,31 |
| 2602 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2603 | emit_jcc_short_link(dst, COND_Z, &skip2); // jz skip |
| 2604 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2605 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2606 | emit_sar_r32_imm(dst, reghi, 31); // sar reghi,31 |
| 2607 | resolve_link(dst, &skip2); // skip2: |
| 2608 | } |
| 2609 | else |
| 2610 | { |
| 2611 | emit_mov_r32_r32(dst, reglo, reghi); // mov reglo,reghi |
| 2612 | emit_sar_r32_imm(dst, reghi, 31); // sar reghi,31 |
| 2613 | } |
| 2614 | resolve_link(dst, &skip1); // skip1: |
| 2615 | emit_shrd_r32_r32_cl(dst, reglo, reghi); // shrd reglo,reghi,cl |
| 2616 | if (saveflags) emit_pushf(dst); // pushf |
| 2617 | emit_sar_r32_cl(dst, reghi); // sar reghi,cl |
| 2618 | } |
| 2619 | if (saveflags) |
| 2620 | emit_combine_z_flags(dst); |
| 2621 | } |
| 2622 | |
| 2623 | |
| 2624 | /*------------------------------------------------- |
| 2625 | emit_rol_r64_p64 - rol operation to a 64-bit |
| 2626 | pair of registers from a 64-bit parameter |
| 2627 | -------------------------------------------------*/ |
| 2628 | |
| 2629 | static void emit_rol_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2630 | { |
| 2631 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2632 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2633 | { |
| 2634 | int count = param->value & 63; |
| 2635 | if (inst->condflags != 0 && count == 0) |
| 2636 | /* skip */; |
| 2637 | else |
| 2638 | { |
| 2639 | while (count >= 32) |
| 2640 | { |
| 2641 | if (inst->condflags != 0) |
| 2642 | { |
| 2643 | emit_mov_r32_r32(dst, REG_ECX, reglo); // mov ecx,reglo |
| 2644 | emit_shld_r32_r32_imm(dst, reglo, reghi, 31); // shld reglo,reghi,31 |
| 2645 | emit_shld_r32_r32_imm(dst, reghi, REG_ECX, 31); // shld reghi,ecx,31 |
| 2646 | count -= 31; |
| 2647 | } |
| 2648 | else |
| 2649 | { |
| 2650 | emit_xchg_r32_r32(dst, reghi, reglo); // xchg reghi,reglo |
| 2651 | count -= 32; |
| 2652 | } |
| 2653 | } |
| 2654 | if (inst->condflags != 0 || count > 0) |
| 2655 | { |
| 2656 | emit_mov_r32_r32(dst, REG_ECX, reglo); // mov ecx,reglo |
| 2657 | emit_shld_r32_r32_imm(dst, reglo, reghi, count); // shld reglo,reghi,count |
| 2658 | if (saveflags) emit_pushf(dst); // pushf |
| 2659 | emit_shld_r32_r32_imm(dst, reghi, REG_ECX, count); // shld reghi,ecx,count |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | else |
| 2664 | { |
| 2665 | emit_link skip1, skip2; |
| 2666 | emit_mov_m32_r32(dst, MBD(REG_ESP, -8), REG_EBX); // mov [esp-8],ebx |
| 2667 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2668 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2669 | emit_jcc_short_link(dst, COND_Z, &skip1); // jz skip1 |
| 2670 | if (inst->condflags != 0) |
| 2671 | { |
| 2672 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2673 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2674 | emit_shld_r32_r32_imm(dst, reglo, reghi, 31); // shld reglo,reghi,31 |
| 2675 | emit_shld_r32_r32_imm(dst, reghi, REG_EBX, 31); // shld reghi,ebx,31 |
| 2676 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2677 | emit_jcc_short_link(dst, COND_Z, &skip2); // jz skip2 |
| 2678 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2679 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2680 | emit_shld_r32_r32_imm(dst, reglo, reghi, 31); // shld reglo,reghi,31 |
| 2681 | emit_shld_r32_r32_imm(dst, reghi, REG_EBX, 31); // shld reghi,ebx,31 |
| 2682 | resolve_link(dst, &skip2); // skip2: |
| 2683 | } |
| 2684 | else |
| 2685 | emit_xchg_r32_r32(dst, reghi, reglo); // xchg reghi,reglo |
| 2686 | resolve_link(dst, &skip1); // skip1: |
| 2687 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2688 | emit_shld_r32_r32_cl(dst, reglo, reghi); // shld reglo,reghi,cl |
| 2689 | if (saveflags) emit_pushf(dst); // pushf |
| 2690 | emit_shld_r32_r32_cl(dst, reghi, REG_EBX); // shld reghi,ebx,cl |
| 2691 | emit_mov_r32_m32(dst, MBD(REG_ESP, saveflags ? -4 : -8), REG_EBX); // mov ebx,[esp-8] |
| 2692 | } |
| 2693 | if (saveflags) |
| 2694 | emit_combine_z_flags(dst); |
| 2695 | } |
| 2696 | |
| 2697 | |
| 2698 | /*------------------------------------------------- |
| 2699 | emit_ror_r64_p64 - ror operation to a 64-bit |
| 2700 | pair of registers from a 64-bit parameter |
| 2701 | -------------------------------------------------*/ |
| 2702 | |
| 2703 | static void emit_ror_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2704 | { |
| 2705 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2706 | if (param->type == DRCUML_PTYPE_IMMEDIATE) |
| 2707 | { |
| 2708 | int count = param->value & 63; |
| 2709 | if (inst->condflags != 0 && count == 0) |
| 2710 | /* skip */; |
| 2711 | else |
| 2712 | { |
| 2713 | while (count >= 32) |
| 2714 | { |
| 2715 | if (inst->condflags != 0) |
| 2716 | { |
| 2717 | emit_mov_r32_r32(dst, REG_ECX, reglo); // mov ecx,reglo |
| 2718 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2719 | emit_shrd_r32_r32_imm(dst, reghi, REG_ECX, 31); // shrd reghi,ecx,31 |
| 2720 | count -= 31; |
| 2721 | } |
| 2722 | else |
| 2723 | { |
| 2724 | emit_xchg_r32_r32(dst, reghi, reglo); // xchg reghi,reglo |
| 2725 | count -= 32; |
| 2726 | } |
| 2727 | } |
| 2728 | if (inst->condflags != 0 || count > 0) |
| 2729 | { |
| 2730 | emit_mov_r32_r32(dst, REG_ECX, reglo); // mov ecx,reglo |
| 2731 | emit_shrd_r32_r32_imm(dst, reglo, reghi, count); // shrd reglo,reghi,count |
| 2732 | if (saveflags) emit_pushf(dst); // pushf |
| 2733 | emit_shrd_r32_r32_imm(dst, reghi, REG_ECX, count); // shrd reghi,ecx,count |
| 2734 | } |
| 2735 | } |
| 2736 | } |
| 2737 | else |
| 2738 | { |
| 2739 | emit_link skip1, skip2; |
| 2740 | emit_mov_m32_r32(dst, MBD(REG_ESP, -8), REG_EBX); // mov [esp-8],ebx |
| 2741 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2742 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2743 | emit_jcc_short_link(dst, COND_Z, &skip1); // jz skip1 |
| 2744 | if (inst->condflags != 0) |
| 2745 | { |
| 2746 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2747 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2748 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2749 | emit_shrd_r32_r32_imm(dst, reghi, REG_EBX, 31); // shrd reghi,ebx,31 |
| 2750 | emit_test_r32_imm(dst, REG_ECX, 0x20); // test ecx,0x20 |
| 2751 | emit_jcc_short_link(dst, COND_Z, &skip2); // jz skip2 |
| 2752 | emit_sub_r32_imm(dst, REG_ECX, 31); // sub ecx,31 |
| 2753 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2754 | emit_shrd_r32_r32_imm(dst, reglo, reghi, 31); // shrd reglo,reghi,31 |
| 2755 | emit_shrd_r32_r32_imm(dst, reghi, REG_EBX, 31); // shrd reghi,ebx,31 |
| 2756 | resolve_link(dst, &skip2); // skip2: |
| 2757 | } |
| 2758 | else |
| 2759 | emit_xchg_r32_r32(dst, reghi, reglo); // xchg reghi,reglo |
| 2760 | resolve_link(dst, &skip1); // skip1: |
| 2761 | emit_mov_r32_r32(dst, REG_EBX, reglo); // mov ebx,reglo |
| 2762 | emit_shrd_r32_r32_cl(dst, reglo, reghi); // shrd reglo,reghi,cl |
| 2763 | if (saveflags) emit_pushf(dst); // pushf |
| 2764 | emit_shrd_r32_r32_cl(dst, reghi, REG_EBX); // shrd reghi,ebx,cl |
| 2765 | emit_mov_r32_m32(dst, MBD(REG_ESP, saveflags ? -4 : -8), REG_EBX); // mov ebx,[esp-8] |
| 2766 | } |
| 2767 | if (saveflags) |
| 2768 | emit_combine_z_flags(dst); |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | /*------------------------------------------------- |
| 2773 | emit_rcl_r64_p64 - rcl operation to a 64-bit |
| 2774 | pair of registers from a 64-bit parameter |
| 2775 | -------------------------------------------------*/ |
| 2776 | |
| 2777 | static void emit_rcl_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2778 | { |
| 2779 | int saveflags = ((inst->condflags & DRCUML_FLAG_Z) != 0); |
| 2780 | emit_link skipall, skiploop; |
| 2781 | x86code *loop; |
| 2782 | |
| 2783 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2784 | if (!saveflags) |
| 2785 | { |
| 2786 | loop = *dst; // loop: |
| 2787 | emit_jecxz_link(dst, &skipall); // jecxz skipall |
| 2788 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2789 | emit_rcl_r32_imm(dst, reglo, 1); // rcl reglo,1 |
| 2790 | emit_rcl_r32_imm(dst, reghi, 1); // rcl reghi,1 |
| 2791 | emit_jmp(dst, loop); // jmp loop |
| 2792 | resolve_link(dst, &skipall); // skipall: |
| 2793 | } |
| 2794 | else |
| 2795 | { |
| 2796 | emit_jecxz_link(dst, &skipall); // jecxz skipall |
| 2797 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2798 | loop = *dst; // loop: |
| 2799 | emit_jecxz_link(dst, &skiploop); // jecxz skiploop |
| 2800 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2801 | emit_rcl_r32_imm(dst, reglo, 1); // rcl reglo,1 |
| 2802 | emit_rcl_r32_imm(dst, reghi, 1); // rcl reghi,1 |
| 2803 | emit_jmp(dst, loop); // jmp loop |
| 2804 | resolve_link(dst, &skiploop); // skiploop: |
| 2805 | emit_rcl_r32_imm(dst, reglo, 1); // rcl reglo,1 |
| 2806 | emit_pushf(dst); // pushf |
| 2807 | emit_rcl_r32_imm(dst, reghi, 1); // rcl reghi,1 |
| 2808 | resolve_link(dst, &skipall); // skipall: |
| 2809 | emit_combine_z_flags(dst); |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | |
| 2814 | /*------------------------------------------------- |
| 2815 | emit_rcr_r64_p64 - rcr operation to a 64-bit |
| 2816 | pair of registers from a 64-bit parameter |
| 2817 | -------------------------------------------------*/ |
| 2818 | |
| 2819 | static void emit_rcr_r64_p64(drcbe_state *drcbe, x86code **dst, UINT8 reglo, UINT8 reghi, const drcuml_parameter *param, const drcuml_instruction *inst) |
| 2820 | { |
| 2821 | int saveflags = (inst->condflags != 0); |
| 2822 | emit_link skipall, skiploop; |
| 2823 | x86code *loop; |
| 2824 | |
| 2825 | emit_mov_r32_p32(drcbe, dst, REG_ECX, param); // mov ecx,param |
| 2826 | if (!saveflags) |
| 2827 | { |
| 2828 | loop = *dst; // loop: |
| 2829 | emit_jecxz_link(dst, &skipall); // jecxz skipall |
| 2830 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2831 | emit_rcr_r32_imm(dst, reghi, 1); // rcr reghi,1 |
| 2832 | emit_rcr_r32_imm(dst, reglo, 1); // rcr reglo,1 |
| 2833 | emit_jmp(dst, loop); // jmp loop |
| 2834 | resolve_link(dst, &skipall); // skipall: |
| 2835 | } |
| 2836 | else |
| 2837 | { |
| 2838 | emit_jecxz_link(dst, &skipall); // jecxz skipall |
| 2839 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2840 | loop = *dst; // loop: |
| 2841 | emit_jecxz_link(dst, &skiploop); // jecxz skiploop |
| 2842 | emit_lea_r32_m32(dst, REG_ECX, MBD(REG_ECX, -1)); // lea ecx,[ecx-1] |
| 2843 | emit_rcr_r32_imm(dst, reghi, 1); // rcr reghi,1 |
| 2844 | emit_rcr_r32_imm(dst, reglo, 1); // rcr reglo,1 |
| 2845 | emit_jmp(dst, loop); // jmp loop |
| 2846 | resolve_link(dst, &skiploop); // skiploop: |
| 2847 | emit_rcr_r32_imm(dst, reghi, 1); // rcr reghi,1 |
| 2848 | emit_pushf(dst); // pushf |
| 2849 | emit_rcr_r32_imm(dst, reglo, 1); // rcr reglo,1 |
| 2850 | resolve_link(dst, &skipall); // skipall: |
| 2851 | emit_combine_z_shl_flags(dst); |
| 2852 | } |
| 2853 | } |
| 2854 | |
| 2855 | |
| 2856 | |
| 2857 | /*************************************************************************** |
| 2858 | EMITTERS FOR FLOATING POINT |
| 2859 | ***************************************************************************/ |
| 2860 | |
| 2861 | /*------------------------------------------------- |
| 2862 | emit_fld_p - load a floating point parameter |
| 2863 | onto the stack |
| 2864 | -------------------------------------------------*/ |
| 2865 | |
| 2866 | static void emit_fld_p(x86code **dst, int size, const drcuml_parameter *param) |
| 2867 | { |
| 2868 | assert(param->type == DRCUML_PTYPE_MEMORY); |
| 2869 | assert(size == 4 || size == 8); |
| 2870 | if (size == 4) |
| 2871 | emit_fld_m32(dst, MABS(param->value)); |
| 2872 | else if (size == 8) |
| 2873 | emit_fld_m64(dst, MABS(param->value)); |
| 2874 | } |
| 2875 | |
| 2876 | |
| 2877 | /*------------------------------------------------- |
| 2878 | emit_fstp_p - store a floating point parameter |
| 2879 | from the stack and pop it |
| 2880 | -------------------------------------------------*/ |
| 2881 | |
| 2882 | static void emit_fstp_p(x86code **dst, int size, const drcuml_parameter *param) |
| 2883 | { |
| 2884 | assert(param->type == DRCUML_PTYPE_MEMORY); |
| 2885 | assert(size == 4 || size == 8); |
| 2886 | if (size == 4) |
| 2887 | emit_fstp_m32(dst, MABS(param->value)); |
| 2888 | else if (size == 8) |
| 2889 | emit_fstp_m64(dst, MABS(param->value)); |
| 2890 | } |
| 2891 | |
| 2892 | |
| 2893 | |
| 2894 | /*************************************************************************** |
| 2895 | HELPERS TO CONVERT OTHER OPERATIONS TO MOVES |
| 2896 | ***************************************************************************/ |
| 2897 | |
| 2898 | /*------------------------------------------------- |
| 2899 | convert_to_mov_imm - convert an instruction |
| 2900 | into a mov immediate |
| 2901 | -------------------------------------------------*/ |
| 2902 | |
| 2903 | static x86code *convert_to_mov_imm(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, UINT64 imm) |
| 2904 | { |
| 2905 | drcuml_instruction temp = *inst; |
| 2906 | temp.numparams = 2; |
| 2907 | temp.param[0] = *dstp; |
| 2908 | temp.param[1].type = DRCUML_PTYPE_IMMEDIATE; |
| 2909 | temp.param[1].value = imm; |
| 2910 | return op_mov(drcbe, dst, &temp); |
| 2911 | } |
| 2912 | |
| 2913 | |
| 2914 | /*------------------------------------------------- |
| 2915 | convert_to_mov_src1 - convert an instruction |
| 2916 | into a mov src1 |
| 2917 | -------------------------------------------------*/ |
| 2918 | |
| 2919 | static x86code *convert_to_mov_src1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst, const drcuml_parameter *dstp, const drcuml_parameter *srcp) |
| 2920 | { |
| 2921 | drcuml_instruction temp = *inst; |
| 2922 | temp.numparams = 2; |
| 2923 | temp.param[0] = *dstp; |
| 2924 | temp.param[1] = *srcp; |
| 2925 | return op_mov(drcbe, dst, &temp); |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | |
| 2930 | /*************************************************************************** |
| 2931 | OUT-OF-BAND CODE FIXUP CALLBACKS |
| 2932 | ***************************************************************************/ |
| 2933 | |
| 2934 | /*------------------------------------------------- |
| 2935 | fixup_label - callback to fixup forward- |
| 2936 | referenced labels |
| 2937 | -------------------------------------------------*/ |
| 2938 | |
| 2939 | static void fixup_label(void *parameter, drccodeptr labelcodeptr) |
| 2940 | { |
| 2941 | drccodeptr src = parameter; |
| 2942 | |
| 2943 | /* find the end of the instruction */ |
| 2944 | if (src[0] == 0xe3) |
| 2945 | { |
| 2946 | src += 1 + 1; |
| 2947 | src[-1] = labelcodeptr - src; |
| 2948 | } |
| 2949 | else if (src[0] == 0xe9) |
| 2950 | { |
| 2951 | src += 1 + 4; |
| 2952 | ((UINT32 *)src)[-1] = labelcodeptr - src; |
| 2953 | } |
| 2954 | else if (src[0] == 0x0f && (src[1] & 0xf0) == 0x80) |
| 2955 | { |
| 2956 | src += 2 + 4; |
| 2957 | ((UINT32 *)src)[-1] = labelcodeptr - src; |
| 2958 | } |
| 2959 | else |
| 2960 | fatalerror("fixup_label called with invalid jmp source!"); |
| 2961 | } |
| 2962 | |
| 2963 | |
| 2964 | /*------------------------------------------------- |
| 2965 | fixup_exception - callback to perform cleanup |
| 2966 | and jump to an exception handler |
| 2967 | -------------------------------------------------*/ |
| 2968 | |
| 2969 | static void fixup_exception(drccodeptr *codeptr, void *param1, void *param2, void *param3) |
| 2970 | { |
| 2971 | drcuml_parameter handp, exp; |
| 2972 | drcbe_state *drcbe = param1; |
| 2973 | drccodeptr src = param2; |
| 2974 | const drcuml_instruction *inst = param3; |
| 2975 | drccodeptr dst = *codeptr; |
| 2976 | drccodeptr *targetptr; |
| 2977 | |
| 2978 | /* normalize parameters */ |
| 2979 | param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI); |
| 2980 | |
| 2981 | /* look up the handle target */ |
| 2982 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value); |
| 2983 | |
| 2984 | /* first fixup the jump to get us here */ |
| 2985 | ((UINT32 *)src)[-1] = dst - src; |
| 2986 | |
| 2987 | /* then store the exception parameter */ |
| 2988 | emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp), &exp); // mov [exp],exp |
| 2989 | |
| 2990 | /* push the original return address on the stack */ |
| 2991 | emit_push_imm(&dst, (FPTR)src); // push <return> |
| 2992 | if (*targetptr != NULL) |
| 2993 | emit_jmp(&dst, *targetptr); // jmp *targetptr |
| 2994 | else |
| 2995 | emit_jmp_m32(&dst, MABS(targetptr)); // jmp [targetptr] |
| 2996 | |
| 2997 | *codeptr = dst; |
| 2998 | } |
| 2999 | |
| 3000 | |
| 3001 | |
| 3002 | /*************************************************************************** |
| 3003 | DEBUG HELPERS |
| 3004 | ***************************************************************************/ |
| 3005 | |
| 3006 | /*------------------------------------------------- |
| 3007 | debug_log_hashjmp - callback to handle |
| 3008 | logging of hashjmps |
| 3009 | -------------------------------------------------*/ |
| 3010 | |
| 3011 | #if LOG_HASHJMPS |
| 3012 | static void debug_log_hashjmp(int mode, offs_t pc) |
| 3013 | { |
| 3014 | printf("mode=%d PC=%08X\n", mode, pc); |
| 3015 | } |
| 3016 | #endif |
| 3017 | |
| 3018 | |
| 3019 | |
| 3020 | /*************************************************************************** |
| 3021 | COMPILE-TIME OPCODES |
| 3022 | ***************************************************************************/ |
| 3023 | |
| 3024 | /*------------------------------------------------- |
| 3025 | op_handle - process a HANDLE opcode |
| 3026 | -------------------------------------------------*/ |
| 3027 | |
| 3028 | static x86code *op_handle(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3029 | { |
| 3030 | assert(inst->numparams == 1); |
| 3031 | assert(inst->param[0].type == DRCUML_PTYPE_MEMORY); |
| 3032 | |
| 3033 | /* register the current pointer for the handle */ |
| 3034 | drcuml_handle_set_codeptr((drcuml_codehandle *)(FPTR)inst->param[0].value, dst); |
| 3035 | return dst; |
| 3036 | } |
| 3037 | |
| 3038 | |
| 3039 | /*------------------------------------------------- |
| 3040 | op_hash - process a HASH opcode |
| 3041 | -------------------------------------------------*/ |
| 3042 | |
| 3043 | static x86code *op_hash(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3044 | { |
| 3045 | assert(inst->numparams == 2); |
| 3046 | assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE); |
| 3047 | assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE); |
| 3048 | |
| 3049 | /* register the current pointer for the mode/PC */ |
| 3050 | drchash_set_codeptr(drcbe->hash, inst->param[0].value, inst->param[1].value, dst); |
| 3051 | return dst; |
| 3052 | } |
| 3053 | |
| 3054 | |
| 3055 | /*------------------------------------------------- |
| 3056 | op_label - process a LABEL opcode |
| 3057 | -------------------------------------------------*/ |
| 3058 | |
| 3059 | static x86code *op_label(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3060 | { |
| 3061 | assert(inst->numparams == 1); |
| 3062 | assert(inst->param[0].type == DRCUML_PTYPE_IMMEDIATE); |
| 3063 | |
| 3064 | /* register the current pointer for the label */ |
| 3065 | drclabel_set_codeptr(drcbe->labels, inst->param[0].value, dst); |
| 3066 | return dst; |
| 3067 | } |
| 3068 | |
| 3069 | |
| 3070 | /*------------------------------------------------- |
| 3071 | op_comment - process a COMMENT opcode |
| 3072 | -------------------------------------------------*/ |
| 3073 | |
| 3074 | static x86code *op_comment(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3075 | { |
| 3076 | assert(inst->numparams == 1); |
| 3077 | assert(inst->param[0].type == DRCUML_PTYPE_MEMORY); |
| 3078 | |
| 3079 | /* do nothing */ |
| 3080 | return dst; |
| 3081 | } |
| 3082 | |
| 3083 | |
| 3084 | /*------------------------------------------------- |
| 3085 | op_mapvar - process a MAPVAR opcode |
| 3086 | -------------------------------------------------*/ |
| 3087 | |
| 3088 | static x86code *op_mapvar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3089 | { |
| 3090 | assert(inst->numparams == 2); |
| 3091 | assert(inst->param[0].type == DRCUML_PTYPE_MAPVAR); |
| 3092 | assert(inst->param[1].type == DRCUML_PTYPE_IMMEDIATE); |
| 3093 | |
| 3094 | /* set the value of the specified mapvar */ |
| 3095 | drcmap_set_value(drcbe->map, dst, inst->param[0].value, inst->param[1].value); |
| 3096 | return dst; |
| 3097 | } |
| 3098 | |
| 3099 | |
| 3100 | |
| 3101 | /*************************************************************************** |
| 3102 | CONTROL FLOW OPCODES |
| 3103 | ***************************************************************************/ |
| 3104 | |
| 3105 | /*------------------------------------------------- |
| 3106 | op_debug - process a DEBUG opcode |
| 3107 | -------------------------------------------------*/ |
| 3108 | |
| 3109 | static x86code *op_debug(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3110 | { |
| 3111 | #ifdef ENABLE_DEBUGGER |
| 3112 | if (Machine->debug_mode) |
| 3113 | { |
| 3114 | drcuml_parameter pcp; |
| 3115 | |
| 3116 | /* validate instruction */ |
| 3117 | assert(inst->size == 4); |
| 3118 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3119 | |
| 3120 | /* normalize parameters */ |
| 3121 | param_normalize_1(drcbe, inst, &pcp, PTYPE_MRI); |
| 3122 | |
| 3123 | /* push the parameter */ |
| 3124 | emit_push_p32(drcbe, &dst, &pcp); // push pcp |
| 3125 | emit_call(&dst, (x86code *)mame_debug_hook); // call mame_debug_hook |
| 3126 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 3127 | } |
| 3128 | #endif |
| 3129 | |
| 3130 | return dst; |
| 3131 | } |
| 3132 | |
| 3133 | |
| 3134 | /*------------------------------------------------- |
| 3135 | op_exit - process an EXIT opcode |
| 3136 | -------------------------------------------------*/ |
| 3137 | |
| 3138 | static x86code *op_exit(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3139 | { |
| 3140 | drcuml_parameter retp; |
| 3141 | |
| 3142 | /* validate instruction */ |
| 3143 | assert(inst->size == 4); |
| 3144 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3145 | |
| 3146 | /* normalize parameters */ |
| 3147 | param_normalize_1(drcbe, inst, &retp, PTYPE_MRI); |
| 3148 | |
| 3149 | /* load the parameter into EAX */ |
| 3150 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &retp); // mov eax,retp |
| 3151 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3152 | emit_jmp(&dst, drcbe->exit); // jmp exit |
| 3153 | else |
| 3154 | emit_jcc(&dst, X86_CONDITION(inst->condflags), drcbe->exit); // jcc exit |
| 3155 | |
| 3156 | return dst; |
| 3157 | } |
| 3158 | |
| 3159 | |
| 3160 | /*------------------------------------------------- |
| 3161 | op_hashjmp - process a HASHJMP opcode |
| 3162 | -------------------------------------------------*/ |
| 3163 | |
| 3164 | static x86code *op_hashjmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3165 | { |
| 3166 | drcuml_parameter modep, pcp, exp; |
| 3167 | |
| 3168 | /* validate instruction */ |
| 3169 | assert(inst->size == 4); |
| 3170 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3171 | |
| 3172 | /* normalize parameters */ |
| 3173 | param_normalize_3(drcbe, inst, &modep, PTYPE_MRI, &pcp, PTYPE_MRI, &exp, PTYPE_M); |
| 3174 | |
| 3175 | #if LOG_HASHJMPS |
| 3176 | emit_push_p32(drcbe, &dst, &pcp); |
| 3177 | emit_push_p32(drcbe, &dst, &modep); |
| 3178 | emit_call(&dst, (x86code *)debug_log_hashjmp); |
| 3179 | emit_add_r32_imm(&dst, REG_ESP, 8); |
| 3180 | #endif |
| 3181 | |
| 3182 | /* load the stack base one word early so we end up at the right spot after our call below */ |
| 3183 | emit_mov_r32_m32(&dst, REG_ESP, MABS(&drcbe->hashstacksave)); // mov esp,[hashstacksave] |
| 3184 | |
| 3185 | /* fixed mode cases */ |
| 3186 | if (modep.type == DRCUML_PTYPE_IMMEDIATE) |
| 3187 | { |
| 3188 | /* a straight immediate jump is direct, though we need the PC in EAX in case of failure */ |
| 3189 | if (pcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3190 | { |
| 3191 | UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask; |
| 3192 | UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask; |
| 3193 | emit_call_m32(&dst, MABS(&drcbe->hash->base[modep.value][l1val][l2val])); // call hash[modep][l1val][l2val] |
| 3194 | } |
| 3195 | |
| 3196 | /* a fixed mode but variable PC */ |
| 3197 | else |
| 3198 | { |
| 3199 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp); // mov eax,pcp |
| 3200 | emit_mov_r32_r32(&dst, REG_EDX, REG_EAX); // mov edx,eax |
| 3201 | emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift); // shr edx,l1shift |
| 3202 | emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and eax,l2mask << l2shift |
| 3203 | emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EDX, 4, &drcbe->hash->base[modep.value][0])); |
| 3204 | // mov edx,hash[modep+edx*4] |
| 3205 | emit_call_m32(&dst, MBISD(REG_EDX, REG_EAX, 4 >> drcbe->hash->l2shift, 0)); // call [edx+eax*shift] |
| 3206 | } |
| 3207 | } |
| 3208 | else |
| 3209 | { |
| 3210 | /* variable mode */ |
| 3211 | int modereg = param_select_register(REG_ECX, &modep, NULL); |
| 3212 | emit_mov_r32_p32(drcbe, &dst, modereg, &modep); // mov modereg,modep |
| 3213 | emit_mov_r32_m32(&dst, REG_ECX, MISD(modereg, 4, &drcbe->hash->base[0])); // mov ecx,hash[modereg*4] |
| 3214 | |
| 3215 | /* fixed PC */ |
| 3216 | if (pcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3217 | { |
| 3218 | UINT32 l1val = (pcp.value >> drcbe->hash->l1shift) & drcbe->hash->l1mask; |
| 3219 | UINT32 l2val = (pcp.value >> drcbe->hash->l2shift) & drcbe->hash->l2mask; |
| 3220 | emit_mov_r32_m32(&dst, REG_EDX, MBD(REG_ECX, l1val*4)); // mov edx,[ecx+l1val*4] |
| 3221 | emit_call_m32(&dst, MBD(REG_EDX, l2val*4)); // call [l2val*4] |
| 3222 | } |
| 3223 | |
| 3224 | /* variable PC */ |
| 3225 | else |
| 3226 | { |
| 3227 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &pcp); // mov eax,pcp |
| 3228 | emit_mov_r32_r32(&dst, REG_EDX, REG_EAX); // mov edx,eax |
| 3229 | emit_shr_r32_imm(&dst, REG_EDX, drcbe->hash->l1shift); // shr edx,l1shift |
| 3230 | emit_mov_r32_m32(&dst, REG_EDX, MBISD(REG_ECX, REG_EDX, 4, 0)); // mov edx,[ecx+edx*4] |
| 3231 | emit_and_r32_imm(&dst, REG_EAX, drcbe->hash->l2mask << drcbe->hash->l2shift);// and eax,l2mask << l2shift |
| 3232 | emit_call_m32(&dst, MBISD(REG_EDX, REG_EAX, 4 >> drcbe->hash->l2shift, 0)); // call [edx+eax*shift] |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | /* in all cases, if there is no code, we return here to generate the exception */ |
| 3237 | emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp.l), &pcp); // mov [exp],param |
| 3238 | emit_sub_r32_imm(&dst, REG_ESP, 4); // sub esp,4 |
| 3239 | emit_call_m32(&dst, MABS(exp.value)); // call [exp] |
| 3240 | |
| 3241 | return dst; |
| 3242 | } |
| 3243 | |
| 3244 | |
| 3245 | /*------------------------------------------------- |
| 3246 | op_jmp - process a JMP opcode |
| 3247 | -------------------------------------------------*/ |
| 3248 | |
| 3249 | static x86code *op_jmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3250 | { |
| 3251 | drcuml_parameter labelp; |
| 3252 | x86code *jmptarget; |
| 3253 | |
| 3254 | /* validate instruction */ |
| 3255 | assert(inst->size == 4); |
| 3256 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3257 | |
| 3258 | /* normalize parameters */ |
| 3259 | param_normalize_1(drcbe, inst, &labelp, PTYPE_I); |
| 3260 | |
| 3261 | /* look up the jump target and jump there */ |
| 3262 | jmptarget = (x86code *)drclabel_get_codeptr(drcbe->labels, labelp.value, fixup_label, dst); |
| 3263 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3264 | emit_jmp(&dst, jmptarget); // jmp target |
| 3265 | else |
| 3266 | emit_jcc(&dst, X86_CONDITION(inst->condflags), jmptarget); // jcc target |
| 3267 | |
| 3268 | return dst; |
| 3269 | } |
| 3270 | |
| 3271 | |
| 3272 | /*------------------------------------------------- |
| 3273 | op_exh - process an EXH opcode |
| 3274 | -------------------------------------------------*/ |
| 3275 | |
| 3276 | static x86code *op_exh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3277 | { |
| 3278 | drcuml_parameter handp, exp; |
| 3279 | drccodeptr *targetptr; |
| 3280 | |
| 3281 | /* validate instruction */ |
| 3282 | assert(inst->size == 4); |
| 3283 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3284 | |
| 3285 | /* normalize parameters */ |
| 3286 | param_normalize_2(drcbe, inst, &handp, PTYPE_M, &exp, PTYPE_MRI); |
| 3287 | |
| 3288 | /* look up the handle target */ |
| 3289 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value); |
| 3290 | |
| 3291 | /* perform the exception processing inline if unconditional */ |
| 3292 | if (inst->condflags == DRCUML_COND_ALWAYS) |
| 3293 | { |
| 3294 | emit_mov_m32_p32(drcbe, &dst, MABS(&drcbe->state.exp.l), &exp); // mov [exp],exp |
| 3295 | if (*targetptr != NULL) |
| 3296 | emit_call(&dst, *targetptr); // call *targetptr |
| 3297 | else |
| 3298 | emit_call_m32(&dst, MABS(targetptr)); // call [targetptr] |
| 3299 | } |
| 3300 | |
| 3301 | /* otherwise, jump to an out-of-band handler */ |
| 3302 | else |
| 3303 | { |
| 3304 | emit_jcc(&dst, X86_CONDITION(inst->condflags), 0); // jcc exception |
| 3305 | drccache_request_oob_codegen(drcbe->cache, fixup_exception, drcbe, dst, (void *)inst); |
| 3306 | } |
| 3307 | return dst; |
| 3308 | } |
| 3309 | |
| 3310 | |
| 3311 | /*------------------------------------------------- |
| 3312 | op_callh - process a CALLH opcode |
| 3313 | -------------------------------------------------*/ |
| 3314 | |
| 3315 | static x86code *op_callh(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3316 | { |
| 3317 | drcuml_parameter handp; |
| 3318 | drccodeptr *targetptr; |
| 3319 | emit_link skip = { 0 }; |
| 3320 | |
| 3321 | /* validate instruction */ |
| 3322 | assert(inst->size == 4); |
| 3323 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3324 | |
| 3325 | /* normalize parameters */ |
| 3326 | param_normalize_1(drcbe, inst, &handp, PTYPE_M); |
| 3327 | |
| 3328 | /* look up the handle target */ |
| 3329 | targetptr = drcuml_handle_codeptr_addr((drcuml_codehandle *)(FPTR)handp.value); |
| 3330 | |
| 3331 | /* skip if conditional */ |
| 3332 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3333 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3334 | |
| 3335 | /* jump through the handle; directly if a normal jump */ |
| 3336 | if (*targetptr != NULL) |
| 3337 | emit_call(&dst, *targetptr); // call *targetptr |
| 3338 | else |
| 3339 | emit_call_m32(&dst, MABS(targetptr)); // call [targetptr] |
| 3340 | |
| 3341 | /* resolve the conditional link */ |
| 3342 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3343 | resolve_link(&dst, &skip); // skip: |
| 3344 | return dst; |
| 3345 | } |
| 3346 | |
| 3347 | |
| 3348 | /*------------------------------------------------- |
| 3349 | op_ret - process a RET opcode |
| 3350 | -------------------------------------------------*/ |
| 3351 | |
| 3352 | static x86code *op_ret(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3353 | { |
| 3354 | emit_link skip = { 0 }; |
| 3355 | |
| 3356 | /* validate instruction */ |
| 3357 | assert(inst->size == 4); |
| 3358 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3359 | assert(inst->numparams == 0); |
| 3360 | |
| 3361 | /* skip if conditional */ |
| 3362 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3363 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3364 | |
| 3365 | /* return */ |
| 3366 | emit_ret(&dst); // ret |
| 3367 | |
| 3368 | /* resolve the conditional link */ |
| 3369 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3370 | resolve_link(&dst, &skip); // skip: |
| 3371 | return dst; |
| 3372 | } |
| 3373 | |
| 3374 | |
| 3375 | /*------------------------------------------------- |
| 3376 | op_callc - process a CALLC opcode |
| 3377 | -------------------------------------------------*/ |
| 3378 | |
| 3379 | static x86code *op_callc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3380 | { |
| 3381 | drcuml_parameter funcp, paramp; |
| 3382 | emit_link skip = { 0 }; |
| 3383 | |
| 3384 | /* validate instruction */ |
| 3385 | assert(inst->size == 4); |
| 3386 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 3387 | |
| 3388 | /* normalize parameters */ |
| 3389 | param_normalize_2(drcbe, inst, &funcp, PTYPE_M, ¶mp, PTYPE_M); |
| 3390 | |
| 3391 | /* skip if conditional */ |
| 3392 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3393 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 3394 | |
| 3395 | /* perform the call */ |
| 3396 | emit_push_imm(&dst, paramp.value); // push paramp |
| 3397 | emit_call(&dst, (x86code *)(FPTR)funcp.value); // call funcp |
| 3398 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 3399 | |
| 3400 | /* resolve the conditional link */ |
| 3401 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 3402 | resolve_link(&dst, &skip); // skip: |
| 3403 | return dst; |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | /*------------------------------------------------- |
| 3408 | op_recover - process a RECOVER opcode |
| 3409 | -------------------------------------------------*/ |
| 3410 | |
| 3411 | static x86code *op_recover(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3412 | { |
| 3413 | drcuml_parameter dstp, mvparam; |
| 3414 | |
| 3415 | /* validate instruction */ |
| 3416 | assert(inst->size == 4); |
| 3417 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3418 | |
| 3419 | /* normalize parameters */ |
| 3420 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &mvparam, PTYPE_I); |
| 3421 | |
| 3422 | /* call the recovery code */ |
| 3423 | emit_mov_r32_m32(&dst, REG_EAX, MABS(&drcbe->stacksave)); // mov eax,stacksave |
| 3424 | emit_mov_r32_m32(&dst, REG_EAX, MBD(REG_EAX, -4)); // mov eax,[eax-4] |
| 3425 | emit_sub_r32_imm(&dst, REG_EAX, 1); // sub eax,1 |
| 3426 | emit_push_imm(&dst, inst->param[1].value); // push param1 |
| 3427 | emit_push_r32(&dst, REG_EAX); // push eax |
| 3428 | emit_push_imm(&dst, (FPTR)drcbe->map); // push drcbe->map |
| 3429 | emit_call(&dst, (x86code *)drcmap_get_value); // call drcmap_get_value |
| 3430 | emit_add_r32_imm(&dst, REG_ESP, 12); // add esp,12 |
| 3431 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 3432 | |
| 3433 | return dst; |
| 3434 | } |
| 3435 | |
| 3436 | |
| 3437 | |
| 3438 | /*************************************************************************** |
| 3439 | INTERNAL REGISTER OPCODES |
| 3440 | ***************************************************************************/ |
| 3441 | |
| 3442 | /*------------------------------------------------- |
| 3443 | op_setfmod - process a SETFMOD opcode |
| 3444 | -------------------------------------------------*/ |
| 3445 | |
| 3446 | static x86code *op_setfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3447 | { |
| 3448 | drcuml_parameter srcp; |
| 3449 | |
| 3450 | /* validate instruction */ |
| 3451 | assert(inst->size == 4); |
| 3452 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3453 | |
| 3454 | /* normalize parameters */ |
| 3455 | param_normalize_1(drcbe, inst, &srcp, PTYPE_MRI); |
| 3456 | |
| 3457 | /* immediate case */ |
| 3458 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3459 | { |
| 3460 | srcp.value &= 3; |
| 3461 | emit_mov_m8_imm(&dst, MABS(&drcbe->state.fmod), srcp.value); // mov [fmod],srcp |
| 3462 | emit_fldcw_m16(&dst, MABS(&fp_control[srcp.value])); // fldcw fp_control[srcp] |
| 3463 | } |
| 3464 | |
| 3465 | /* register/memory case */ |
| 3466 | else |
| 3467 | { |
| 3468 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp); // mov eax,srcp |
| 3469 | emit_and_r32_imm(&dst, REG_EAX, 3); // and eax,3 |
| 3470 | emit_mov_m8_r8(&dst, MABS(&drcbe->state.fmod), REG_AL); // mov [fmod],al |
| 3471 | emit_fldcw_m16(&dst, MISD(REG_EAX, 2, &fp_control[0])); // fldcw fp_control[eax] |
| 3472 | } |
| 3473 | |
| 3474 | return dst; |
| 3475 | } |
| 3476 | |
| 3477 | |
| 3478 | /*------------------------------------------------- |
| 3479 | op_getfmod - process a GETFMOD opcode |
| 3480 | -------------------------------------------------*/ |
| 3481 | |
| 3482 | static x86code *op_getfmod(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3483 | { |
| 3484 | drcuml_parameter dstp; |
| 3485 | |
| 3486 | /* validate instruction */ |
| 3487 | assert(inst->size == 4); |
| 3488 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3489 | |
| 3490 | /* normalize parameters */ |
| 3491 | param_normalize_1(drcbe, inst, &dstp, PTYPE_MR); |
| 3492 | |
| 3493 | /* fetch the current mode and store to the destination */ |
| 3494 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3495 | emit_movzx_r32_m8(&dst, dstp.value, MABS(&drcbe->state.fmod)); // movzx reg,[fmod] |
| 3496 | else |
| 3497 | { |
| 3498 | emit_movzx_r32_m8(&dst, REG_EAX, MABS(&drcbe->state.fmod)); // movzx eax,[fmod] |
| 3499 | emit_mov_m32_r32(&dst, MABS(dstp.value), REG_EAX); // mov [dstp],eax |
| 3500 | } |
| 3501 | |
| 3502 | return dst; |
| 3503 | } |
| 3504 | |
| 3505 | |
| 3506 | /*------------------------------------------------- |
| 3507 | op_getexp - process a GETEXP opcode |
| 3508 | -------------------------------------------------*/ |
| 3509 | |
| 3510 | static x86code *op_getexp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3511 | { |
| 3512 | drcuml_parameter dstp; |
| 3513 | |
| 3514 | /* validate instruction */ |
| 3515 | assert(inst->size == 4); |
| 3516 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3517 | |
| 3518 | /* normalize parameters */ |
| 3519 | param_normalize_1(drcbe, inst, &dstp, PTYPE_MR); |
| 3520 | |
| 3521 | /* fetch the exception parameter and store to the destination */ |
| 3522 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3523 | emit_mov_r32_m32(&dst, dstp.value, MABS(&drcbe->state.exp.l)); // mov reg,[exp] |
| 3524 | else |
| 3525 | { |
| 3526 | emit_mov_r32_m32(&dst, REG_EAX, MABS(&drcbe->state.exp.l)); // mov eax,[exp] |
| 3527 | emit_mov_m32_r32(&dst, MABS(dstp.value), REG_EAX); // mov [dstp],eax |
| 3528 | } |
| 3529 | |
| 3530 | return dst; |
| 3531 | } |
| 3532 | |
| 3533 | |
| 3534 | |
| 3535 | /*************************************************************************** |
| 3536 | INTEGER OPERATIONS |
| 3537 | ***************************************************************************/ |
| 3538 | |
| 3539 | /*------------------------------------------------- |
| 3540 | op_load1u - process a LOAD1U opcode |
| 3541 | -------------------------------------------------*/ |
| 3542 | |
| 3543 | static x86code *op_load1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3544 | { |
| 3545 | drcuml_parameter dstp, basep, indp; |
| 3546 | int dstreg; |
| 3547 | |
| 3548 | /* validate instruction */ |
| 3549 | assert(inst->size == 4 || inst->size == 8); |
| 3550 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3551 | |
| 3552 | /* normalize parameters */ |
| 3553 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3554 | |
| 3555 | /* pick a target register for the general case */ |
| 3556 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3557 | |
| 3558 | /* immediate index */ |
| 3559 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3560 | emit_movzx_r32_m8(&dst, dstreg, MABS(basep.value + 1*indp.value)); // movzx dstreg,[basep + 1*indp] |
| 3561 | |
| 3562 | /* other index */ |
| 3563 | else |
| 3564 | { |
| 3565 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3566 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3567 | emit_movzx_r32_m8(&dst, dstreg, MISD(indreg, 1, basep.value)); // movzx dstreg,[basep + 1*indp] |
| 3568 | } |
| 3569 | |
| 3570 | /* store low 32 bits */ |
| 3571 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3572 | |
| 3573 | /* 64-bit form stores upper 32 bits */ |
| 3574 | if (inst->size == 8) |
| 3575 | { |
| 3576 | /* general case */ |
| 3577 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 3578 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 3579 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3580 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 3581 | } |
| 3582 | return dst; |
| 3583 | } |
| 3584 | |
| 3585 | |
| 3586 | /*------------------------------------------------- |
| 3587 | op_load1s - process a LOAD1S opcode |
| 3588 | -------------------------------------------------*/ |
| 3589 | |
| 3590 | static x86code *op_load1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3591 | { |
| 3592 | drcuml_parameter dstp, basep, indp; |
| 3593 | int dstreg; |
| 3594 | |
| 3595 | /* validate instruction */ |
| 3596 | assert(inst->size == 4 || inst->size == 8); |
| 3597 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3598 | |
| 3599 | /* normalize parameters */ |
| 3600 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3601 | |
| 3602 | /* pick a target register for the general case */ |
| 3603 | dstreg = (inst->size == 8) ? REG_EAX : param_select_register(REG_EAX, &dstp, NULL); |
| 3604 | |
| 3605 | /* immediate index */ |
| 3606 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3607 | emit_movsx_r32_m8(&dst, dstreg, MABS(basep.value + 1*indp.value)); // movsx eax,[basep + 1*indp] |
| 3608 | |
| 3609 | /* other index */ |
| 3610 | else |
| 3611 | { |
| 3612 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3613 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3614 | emit_movsx_r32_m8(&dst, dstreg, MISD(indreg, 1, basep.value)); // movsx eax,[basep + 1*indp] |
| 3615 | } |
| 3616 | |
| 3617 | /* 32-bit form */ |
| 3618 | if (inst->size == 4) |
| 3619 | { |
| 3620 | /* general case */ |
| 3621 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3622 | } |
| 3623 | |
| 3624 | /* 64-bit form */ |
| 3625 | else |
| 3626 | { |
| 3627 | /* general case */ |
| 3628 | emit_cdq(&dst); // cdq |
| 3629 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 3630 | } |
| 3631 | return dst; |
| 3632 | } |
| 3633 | |
| 3634 | |
| 3635 | /*------------------------------------------------- |
| 3636 | op_load2u - process a LOAD2U opcode |
| 3637 | -------------------------------------------------*/ |
| 3638 | |
| 3639 | static x86code *op_load2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3640 | { |
| 3641 | drcuml_parameter dstp, basep, indp; |
| 3642 | int dstreg; |
| 3643 | |
| 3644 | /* validate instruction */ |
| 3645 | assert(inst->size == 4 || inst->size == 8); |
| 3646 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3647 | |
| 3648 | /* normalize parameters */ |
| 3649 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3650 | |
| 3651 | /* pick a target register for the general case */ |
| 3652 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3653 | |
| 3654 | /* immediate index */ |
| 3655 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3656 | emit_movzx_r32_m16(&dst, dstreg, MABS(basep.value + 2*indp.value)); // movzx dstreg,[basep + 2*indp] |
| 3657 | |
| 3658 | /* other index */ |
| 3659 | else |
| 3660 | { |
| 3661 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3662 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3663 | emit_movzx_r32_m16(&dst, dstreg, MISD(indreg, 2, basep.value)); // movzx dstreg,[basep + 2*indp] |
| 3664 | } |
| 3665 | |
| 3666 | /* store low 32 bits */ |
| 3667 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3668 | |
| 3669 | /* 64-bit form stores upper 32 bits */ |
| 3670 | if (inst->size == 8) |
| 3671 | { |
| 3672 | /* general case */ |
| 3673 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 3674 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 3675 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3676 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 3677 | } |
| 3678 | return dst; |
| 3679 | } |
| 3680 | |
| 3681 | |
| 3682 | /*------------------------------------------------- |
| 3683 | op_load2s - process a LOAD2S opcode |
| 3684 | -------------------------------------------------*/ |
| 3685 | |
| 3686 | static x86code *op_load2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3687 | { |
| 3688 | drcuml_parameter dstp, basep, indp; |
| 3689 | int dstreg; |
| 3690 | |
| 3691 | /* validate instruction */ |
| 3692 | assert(inst->size == 4 || inst->size == 8); |
| 3693 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3694 | |
| 3695 | /* normalize parameters */ |
| 3696 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3697 | |
| 3698 | /* pick a target register for the general case */ |
| 3699 | dstreg = (inst->size == 8) ? REG_EAX : param_select_register(REG_EAX, &dstp, NULL); |
| 3700 | |
| 3701 | /* immediate index */ |
| 3702 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3703 | emit_movsx_r32_m16(&dst, dstreg, MABS(basep.value + 2*indp.value)); // movsx eax,[basep + 2*indp] |
| 3704 | |
| 3705 | /* other index */ |
| 3706 | else |
| 3707 | { |
| 3708 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3709 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3710 | emit_movsx_r32_m16(&dst, dstreg, MISD(indreg, 2, basep.value)); // movsx eax,[basep + 2*indp] |
| 3711 | } |
| 3712 | |
| 3713 | /* 32-bit form */ |
| 3714 | if (inst->size == 4) |
| 3715 | { |
| 3716 | /* general case */ |
| 3717 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3718 | } |
| 3719 | |
| 3720 | /* 64-bit form */ |
| 3721 | else |
| 3722 | { |
| 3723 | /* general case */ |
| 3724 | emit_cdq(&dst); // cdq |
| 3725 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 3726 | } |
| 3727 | return dst; |
| 3728 | } |
| 3729 | |
| 3730 | |
| 3731 | /*------------------------------------------------- |
| 3732 | op_load4u - process a LOAD4U opcode |
| 3733 | -------------------------------------------------*/ |
| 3734 | |
| 3735 | static x86code *op_load4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3736 | { |
| 3737 | drcuml_parameter dstp, basep, indp; |
| 3738 | int dstreg; |
| 3739 | |
| 3740 | /* validate instruction */ |
| 3741 | assert(inst->size == 4 || inst->size == 8); |
| 3742 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3743 | |
| 3744 | /* normalize parameters */ |
| 3745 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3746 | |
| 3747 | /* pick a target register for the general case */ |
| 3748 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3749 | |
| 3750 | /* immediate index */ |
| 3751 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3752 | emit_mov_r32_m32(&dst, dstreg, MABS(basep.value + 4*indp.value)); // mov dstreg,[basep + 4*indp] |
| 3753 | |
| 3754 | /* other index */ |
| 3755 | else |
| 3756 | { |
| 3757 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3758 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3759 | emit_mov_r32_m32(&dst, dstreg, MISD(indreg, 4, basep.value)); // mov dstreg,[basep + 4*indp] |
| 3760 | } |
| 3761 | |
| 3762 | /* store low 32 bits */ |
| 3763 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 3764 | |
| 3765 | /* 64-bit form stores upper 32 bits */ |
| 3766 | if (inst->size == 8) |
| 3767 | { |
| 3768 | /* general case */ |
| 3769 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 3770 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 3771 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 3772 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 3773 | } |
| 3774 | return dst; |
| 3775 | } |
| 3776 | |
| 3777 | |
| 3778 | /*------------------------------------------------- |
| 3779 | op_load4s - process a LOAD4S opcode |
| 3780 | -------------------------------------------------*/ |
| 3781 | |
| 3782 | static x86code *op_load4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3783 | { |
| 3784 | drcuml_parameter dstp, basep, indp; |
| 3785 | |
| 3786 | /* validate instruction */ |
| 3787 | assert(inst->size == 8); |
| 3788 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3789 | |
| 3790 | /* normalize parameters */ |
| 3791 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3792 | |
| 3793 | /* immediate index */ |
| 3794 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3795 | emit_mov_r32_m32(&dst, REG_EAX, MABS(basep.value + 4*indp.value)); // mov eax,[basep + 4*indp] |
| 3796 | |
| 3797 | /* other index */ |
| 3798 | else |
| 3799 | { |
| 3800 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3801 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3802 | emit_mov_r32_m32(&dst, REG_EAX, MISD(indreg, 4, basep.value)); // movsx eax,[basep + 4*indp] |
| 3803 | } |
| 3804 | |
| 3805 | /* general case */ |
| 3806 | emit_cdq(&dst); // cdq |
| 3807 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 3808 | |
| 3809 | return dst; |
| 3810 | } |
| 3811 | |
| 3812 | |
| 3813 | /*------------------------------------------------- |
| 3814 | op_load8u - process a LOAD8U opcode |
| 3815 | -------------------------------------------------*/ |
| 3816 | |
| 3817 | static x86code *op_load8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3818 | { |
| 3819 | drcuml_parameter dstp, basep, indp; |
| 3820 | int dstreg; |
| 3821 | |
| 3822 | /* validate instruction */ |
| 3823 | assert(inst->size == 8); |
| 3824 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3825 | |
| 3826 | /* normalize parameters */ |
| 3827 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 3828 | |
| 3829 | /* pick a target register for the general case */ |
| 3830 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 3831 | |
| 3832 | /* immediate index */ |
| 3833 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3834 | { |
| 3835 | emit_mov_r32_m32(&dst, REG_EDX, MABS(basep.value + 8*indp.value + 4)); // mov edx,[basep + 8*indp + 4] |
| 3836 | emit_mov_r32_m32(&dst, dstreg, MABS(basep.value + 8*indp.value)); // mov dstreg,[basep + 8*indp] |
| 3837 | } |
| 3838 | |
| 3839 | /* other index */ |
| 3840 | else |
| 3841 | { |
| 3842 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3843 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 3844 | emit_mov_r32_m32(&dst, REG_EDX, MISD(indreg, 8, basep.value + 4)); // mov edx,[basep + 8*indp + 4] |
| 3845 | emit_mov_r32_m32(&dst, dstreg, MISD(indreg, 8, basep.value)); // mov dstreg,[basep + 8*indp] |
| 3846 | } |
| 3847 | |
| 3848 | /* general case */ |
| 3849 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,edx:dstreg |
| 3850 | |
| 3851 | return dst; |
| 3852 | } |
| 3853 | |
| 3854 | |
| 3855 | /*------------------------------------------------- |
| 3856 | op_store1 - process a STORE1 opcode |
| 3857 | -------------------------------------------------*/ |
| 3858 | |
| 3859 | static x86code *op_store1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3860 | { |
| 3861 | drcuml_parameter srcp, basep, indp; |
| 3862 | int srcreg; |
| 3863 | |
| 3864 | /* validate instruction */ |
| 3865 | assert(inst->size == 4 || inst->size == 8); |
| 3866 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3867 | |
| 3868 | /* normalize parameters */ |
| 3869 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3870 | |
| 3871 | /* pick a source register for the general case */ |
| 3872 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3873 | if (srcreg & 4) |
| 3874 | srcreg = REG_EAX; |
| 3875 | |
| 3876 | /* degenerate case: constant index */ |
| 3877 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3878 | { |
| 3879 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3880 | emit_mov_m8_imm(&dst, MABS(basep.value + 1*indp.value), srcp.value); // mov [basep + 1*indp],srcp |
| 3881 | else |
| 3882 | { |
| 3883 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3884 | emit_mov_m8_r8(&dst, MABS(basep.value + 1*indp.value), srcreg); // mov [basep + 1*indp],srcreg |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | /* normal case: variable index */ |
| 3889 | else |
| 3890 | { |
| 3891 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3892 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3893 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3894 | emit_mov_m8_imm(&dst, MISD(indreg, 1, basep.value), srcp.value); // mov [basep + 1*ecx],srcp |
| 3895 | else |
| 3896 | { |
| 3897 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3898 | emit_mov_m8_r8(&dst, MISD(indreg, 1, basep.value), srcreg); // mov [basep + 1*ecx],srcreg |
| 3899 | } |
| 3900 | } |
| 3901 | return dst; |
| 3902 | } |
| 3903 | |
| 3904 | |
| 3905 | /*------------------------------------------------- |
| 3906 | op_store2 - process a STORE2 opcode |
| 3907 | -------------------------------------------------*/ |
| 3908 | |
| 3909 | static x86code *op_store2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3910 | { |
| 3911 | drcuml_parameter srcp, basep, indp; |
| 3912 | int srcreg; |
| 3913 | |
| 3914 | /* validate instruction */ |
| 3915 | assert(inst->size == 4 || inst->size == 8); |
| 3916 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3917 | |
| 3918 | /* normalize parameters */ |
| 3919 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3920 | |
| 3921 | /* pick a source register for the general case */ |
| 3922 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3923 | |
| 3924 | /* degenerate case: constant index */ |
| 3925 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3926 | { |
| 3927 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3928 | emit_mov_m16_imm(&dst, MABS(basep.value + 2*indp.value), srcp.value); // mov [basep + 2*indp],srcp |
| 3929 | else |
| 3930 | { |
| 3931 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3932 | emit_mov_m16_r16(&dst, MABS(basep.value + 2*indp.value), srcreg); // mov [basep + 2*indp],srcreg |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | /* normal case: variable index */ |
| 3937 | else |
| 3938 | { |
| 3939 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3940 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3941 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3942 | emit_mov_m16_imm(&dst, MISD(indreg, 2, basep.value), srcp.value); // mov [basep + 2*ecx],srcp |
| 3943 | else |
| 3944 | { |
| 3945 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3946 | emit_mov_m16_r16(&dst, MISD(indreg, 2, basep.value), srcreg); // mov [basep + 2*ecx],srcreg |
| 3947 | } |
| 3948 | } |
| 3949 | return dst; |
| 3950 | } |
| 3951 | |
| 3952 | |
| 3953 | /*------------------------------------------------- |
| 3954 | op_store4 - process a STORE4 opcode |
| 3955 | -------------------------------------------------*/ |
| 3956 | |
| 3957 | static x86code *op_store4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 3958 | { |
| 3959 | drcuml_parameter srcp, basep, indp; |
| 3960 | int srcreg; |
| 3961 | |
| 3962 | /* validate instruction */ |
| 3963 | assert(inst->size == 4 || inst->size == 8); |
| 3964 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 3965 | |
| 3966 | /* normalize parameters */ |
| 3967 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 3968 | |
| 3969 | /* pick a source register for the general case */ |
| 3970 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 3971 | |
| 3972 | /* degenerate case: constant index */ |
| 3973 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3974 | { |
| 3975 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3976 | emit_mov_m32_imm(&dst, MABS(basep.value + 4*indp.value), srcp.value); // mov [basep + 4*indp],srcp |
| 3977 | else |
| 3978 | { |
| 3979 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3980 | emit_mov_m32_r32(&dst, MABS(basep.value + indp.value), srcreg); // mov [basep + 4*indp],srcreg |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | /* normal case: variable index */ |
| 3985 | else |
| 3986 | { |
| 3987 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 3988 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 3989 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 3990 | emit_mov_m32_imm(&dst, MISD(indreg, 4, basep.value), srcp.value); // mov [basep + 4*ecx],srcp |
| 3991 | else |
| 3992 | { |
| 3993 | emit_mov_r32_p32(drcbe, &dst, srcreg, &srcp); // mov srcreg,srcp |
| 3994 | emit_mov_m32_r32(&dst, MISD(indreg, 4, basep.value), srcreg); // mov [basep + 4*ecx],srcreg |
| 3995 | } |
| 3996 | } |
| 3997 | return dst; |
| 3998 | } |
| 3999 | |
| 4000 | |
| 4001 | /*------------------------------------------------- |
| 4002 | op_store8 - process a STORE8 opcode |
| 4003 | -------------------------------------------------*/ |
| 4004 | |
| 4005 | static x86code *op_store8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4006 | { |
| 4007 | drcuml_parameter srcp, basep, indp; |
| 4008 | int srcreg; |
| 4009 | |
| 4010 | /* validate instruction */ |
| 4011 | assert(inst->size == 8); |
| 4012 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4013 | |
| 4014 | /* normalize parameters */ |
| 4015 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4016 | |
| 4017 | /* pick a source register for the general case */ |
| 4018 | srcreg = param_select_register(REG_EAX, &srcp, NULL); |
| 4019 | |
| 4020 | /* degenerate case: constant index */ |
| 4021 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4022 | { |
| 4023 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4024 | { |
| 4025 | emit_mov_m32_imm(&dst, MABS(basep.value + 8*indp.value), srcp.value); // mov [basep + 8*indp],srcp |
| 4026 | emit_mov_m32_imm(&dst, MABS(basep.value + 4 + 8*indp.value), srcp.value >> 32); |
| 4027 | // mov [basep + 4 + 8*indp],srcp >> 32 |
| 4028 | } |
| 4029 | else |
| 4030 | { |
| 4031 | emit_mov_r64_p64(drcbe, &dst, srcreg, REG_EDX, &srcp); // mov edx:srcreg,srcp |
| 4032 | emit_mov_m32_r32(&dst, MABS(basep.value + 8*indp.value), srcreg); // mov [basep + 8*indp],srcreg |
| 4033 | emit_mov_m32_r32(&dst, MABS(basep.value + 4 + 8*indp.value), REG_EDX); // mov [basep + 8*indp + 4],edx |
| 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | /* normal case: variable index */ |
| 4038 | else |
| 4039 | { |
| 4040 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 4041 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); // mov indreg,indp |
| 4042 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4043 | { |
| 4044 | emit_mov_m32_imm(&dst, MISD(indreg, 8, basep.value), srcp.value); // mov [basep + 8*ecx],srcp |
| 4045 | emit_mov_m32_imm(&dst, MISD(indreg, 8, basep.value + 4), srcp.value >> 32); // mov [basep + 8*ecx + 4],srcp >> 32 |
| 4046 | } |
| 4047 | else |
| 4048 | { |
| 4049 | emit_mov_r64_p64(drcbe, &dst, srcreg, REG_EDX, &srcp); // mov edx:srcreg,srcp |
| 4050 | emit_mov_m32_r32(&dst, MISD(indreg, 8, basep.value), srcreg); // mov [basep + 8*ecx],srcreg |
| 4051 | emit_mov_m32_r32(&dst, MISD(indreg, 8, basep.value + 4), REG_EDX); // mov [basep + 4 + 8*ecx],edx |
| 4052 | } |
| 4053 | } |
| 4054 | return dst; |
| 4055 | } |
| 4056 | |
| 4057 | |
| 4058 | /*------------------------------------------------- |
| 4059 | op_read1u - process a READ1U opcode |
| 4060 | -------------------------------------------------*/ |
| 4061 | |
| 4062 | static x86code *op_read1u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4063 | { |
| 4064 | drcuml_parameter dstp, spacep, addrp; |
| 4065 | int dstreg; |
| 4066 | |
| 4067 | /* validate instruction */ |
| 4068 | assert(inst->size == 4 || inst->size == 8); |
| 4069 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4070 | |
| 4071 | /* normalize parameters */ |
| 4072 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4073 | |
| 4074 | /* pick a target register for the general case */ |
| 4075 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4076 | |
| 4077 | /* set up a call to the read byte handler */ |
| 4078 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4079 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_byte);// call read_byte |
| 4080 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4081 | emit_movzx_r32_r8(&dst, dstreg, REG_AL); // movzx dstreg,al |
| 4082 | |
| 4083 | /* store low 32 bits */ |
| 4084 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4085 | |
| 4086 | /* 64-bit form stores upper 32 bits */ |
| 4087 | if (inst->size == 8) |
| 4088 | { |
| 4089 | /* general case */ |
| 4090 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4091 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4092 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4093 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 4094 | } |
| 4095 | return dst; |
| 4096 | } |
| 4097 | |
| 4098 | |
| 4099 | /*------------------------------------------------- |
| 4100 | op_read1s - process a READ1S opcode |
| 4101 | -------------------------------------------------*/ |
| 4102 | |
| 4103 | static x86code *op_read1s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4104 | { |
| 4105 | drcuml_parameter dstp, spacep, addrp; |
| 4106 | int dstreg; |
| 4107 | |
| 4108 | /* validate instruction */ |
| 4109 | assert(inst->size == 4 || inst->size == 8); |
| 4110 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4111 | |
| 4112 | /* normalize parameters */ |
| 4113 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4114 | |
| 4115 | /* pick a target register for the general case */ |
| 4116 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4117 | |
| 4118 | /* set up a call to the read byte handler */ |
| 4119 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4120 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_byte);// call read_byte |
| 4121 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4122 | emit_movsx_r32_r8(&dst, dstreg, REG_AL); // movsx dstreg,al |
| 4123 | |
| 4124 | /* 32-bit form */ |
| 4125 | if (inst->size == 4) |
| 4126 | { |
| 4127 | /* general case */ |
| 4128 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4129 | } |
| 4130 | |
| 4131 | /* 64-bit form */ |
| 4132 | else |
| 4133 | { |
| 4134 | /* general case */ |
| 4135 | emit_cdq(&dst); // cdq |
| 4136 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 4137 | } |
| 4138 | return dst; |
| 4139 | } |
| 4140 | |
| 4141 | |
| 4142 | /*------------------------------------------------- |
| 4143 | op_read2u - process a READ2U opcode |
| 4144 | -------------------------------------------------*/ |
| 4145 | |
| 4146 | static x86code *op_read2u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4147 | { |
| 4148 | drcuml_parameter dstp, spacep, addrp; |
| 4149 | int dstreg; |
| 4150 | |
| 4151 | /* validate instruction */ |
| 4152 | assert(inst->size == 4 || inst->size == 8); |
| 4153 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4154 | |
| 4155 | /* normalize parameters */ |
| 4156 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4157 | |
| 4158 | /* pick a target register for the general case */ |
| 4159 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4160 | |
| 4161 | /* set up a call to the read word handler */ |
| 4162 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4163 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word);// call read_word |
| 4164 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4165 | emit_movzx_r32_r16(&dst, dstreg, REG_AX); // movzx dstreg,ax |
| 4166 | |
| 4167 | /* store low 32 bits */ |
| 4168 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4169 | |
| 4170 | /* 64-bit form stores upper 32 bits */ |
| 4171 | if (inst->size == 8) |
| 4172 | { |
| 4173 | /* general case */ |
| 4174 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4175 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4176 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4177 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 4178 | } |
| 4179 | return dst; |
| 4180 | } |
| 4181 | |
| 4182 | |
| 4183 | /*------------------------------------------------- |
| 4184 | op_read2s - process a READ2S opcode |
| 4185 | -------------------------------------------------*/ |
| 4186 | |
| 4187 | static x86code *op_read2s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4188 | { |
| 4189 | drcuml_parameter dstp, spacep, addrp; |
| 4190 | int dstreg; |
| 4191 | |
| 4192 | /* validate instruction */ |
| 4193 | assert(inst->size == 4 || inst->size == 8); |
| 4194 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4195 | |
| 4196 | /* normalize parameters */ |
| 4197 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4198 | |
| 4199 | /* pick a target register for the general case */ |
| 4200 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4201 | |
| 4202 | /* set up a call to the read word handler */ |
| 4203 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4204 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word);// call read_word |
| 4205 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4206 | emit_movsx_r32_r16(&dst, dstreg, REG_AX); // movsx dstreg,ax |
| 4207 | |
| 4208 | /* 32-bit form */ |
| 4209 | if (inst->size == 4) |
| 4210 | { |
| 4211 | /* general case */ |
| 4212 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4213 | } |
| 4214 | |
| 4215 | /* 64-bit form */ |
| 4216 | else |
| 4217 | { |
| 4218 | /* general case */ |
| 4219 | emit_cdq(&dst); // cdq |
| 4220 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 4221 | } |
| 4222 | return dst; |
| 4223 | } |
| 4224 | |
| 4225 | |
| 4226 | /*------------------------------------------------- |
| 4227 | op_read2m - process a READ2M opcode |
| 4228 | -------------------------------------------------*/ |
| 4229 | |
| 4230 | static x86code *op_read2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4231 | { |
| 4232 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4233 | int dstreg; |
| 4234 | |
| 4235 | /* validate instruction */ |
| 4236 | assert(inst->size == 4 || inst->size == 8); |
| 4237 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4238 | |
| 4239 | /* normalize parameters */ |
| 4240 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4241 | |
| 4242 | /* pick a target register for the general case */ |
| 4243 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4244 | |
| 4245 | /* set up a call to the read dword masked handler */ |
| 4246 | emit_push_p32(drcbe, &dst, &maskp); // push maskp |
| 4247 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4248 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_word_masked); |
| 4249 | // call read_word_masked |
| 4250 | emit_add_r32_imm(&dst, REG_ESP, 8); // add esp,8 |
| 4251 | emit_movzx_r32_r16(&dst, dstreg, REG_AX); // movzx dstreg,ax |
| 4252 | |
| 4253 | /* store low 32 bits */ |
| 4254 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4255 | |
| 4256 | /* 64-bit form stores upper 32 bits */ |
| 4257 | if (inst->size == 8) |
| 4258 | { |
| 4259 | /* general case */ |
| 4260 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4261 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4262 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4263 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 4264 | } |
| 4265 | return dst; |
| 4266 | } |
| 4267 | |
| 4268 | |
| 4269 | /*------------------------------------------------- |
| 4270 | op_read4u - process a READ4U opcode |
| 4271 | -------------------------------------------------*/ |
| 4272 | |
| 4273 | static x86code *op_read4u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4274 | { |
| 4275 | drcuml_parameter dstp, spacep, addrp; |
| 4276 | |
| 4277 | /* validate instruction */ |
| 4278 | assert(inst->size == 4 || inst->size == 8); |
| 4279 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4280 | |
| 4281 | /* normalize parameters */ |
| 4282 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4283 | |
| 4284 | /* set up a call to the read dword handler */ |
| 4285 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4286 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call read_dword |
| 4287 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4288 | |
| 4289 | /* store low 32 bits */ |
| 4290 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 4291 | |
| 4292 | /* 64-bit form stores upper 32 bits */ |
| 4293 | if (inst->size == 8) |
| 4294 | { |
| 4295 | /* general case */ |
| 4296 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4297 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4298 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4299 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 4300 | } |
| 4301 | return dst; |
| 4302 | } |
| 4303 | |
| 4304 | |
| 4305 | /*------------------------------------------------- |
| 4306 | op_read4s - process a READ4S opcode |
| 4307 | -------------------------------------------------*/ |
| 4308 | |
| 4309 | static x86code *op_read4s(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4310 | { |
| 4311 | drcuml_parameter dstp, spacep, addrp; |
| 4312 | |
| 4313 | /* validate instruction */ |
| 4314 | assert(inst->size == 8); |
| 4315 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4316 | |
| 4317 | /* normalize parameters */ |
| 4318 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4319 | |
| 4320 | /* set up a call to the read dword handler */ |
| 4321 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4322 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call read_dword |
| 4323 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4324 | |
| 4325 | /* 64-bit form */ |
| 4326 | emit_cdq(&dst); // cdq |
| 4327 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dst,edx:eax |
| 4328 | |
| 4329 | return dst; |
| 4330 | } |
| 4331 | |
| 4332 | |
| 4333 | /*------------------------------------------------- |
| 4334 | op_read4m - process a READ4M opcode |
| 4335 | -------------------------------------------------*/ |
| 4336 | |
| 4337 | static x86code *op_read4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4338 | { |
| 4339 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4340 | |
| 4341 | /* validate instruction */ |
| 4342 | assert(inst->size == 4 || inst->size == 8); |
| 4343 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4344 | |
| 4345 | /* normalize parameters */ |
| 4346 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4347 | |
| 4348 | /* set up a call to the read dword masked handler */ |
| 4349 | emit_push_p32(drcbe, &dst, &maskp); // push maskp |
| 4350 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4351 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword_masked); |
| 4352 | // call read_dword_masked |
| 4353 | emit_add_r32_imm(&dst, REG_ESP, 8); // add esp,8 |
| 4354 | |
| 4355 | /* store low 32 bits */ |
| 4356 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 4357 | |
| 4358 | /* 64-bit form stores upper 32 bits */ |
| 4359 | if (inst->size == 8) |
| 4360 | { |
| 4361 | /* general case */ |
| 4362 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4363 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4364 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4365 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov [reghi],0 |
| 4366 | } |
| 4367 | return dst; |
| 4368 | } |
| 4369 | |
| 4370 | |
| 4371 | /*------------------------------------------------- |
| 4372 | op_read8u - process a READ8U opcode |
| 4373 | -------------------------------------------------*/ |
| 4374 | |
| 4375 | static x86code *op_read8u(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4376 | { |
| 4377 | drcuml_parameter dstp, spacep, addrp; |
| 4378 | |
| 4379 | /* validate instruction */ |
| 4380 | assert(inst->size == 8); |
| 4381 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4382 | |
| 4383 | /* normalize parameters */ |
| 4384 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 4385 | |
| 4386 | /* set up a call to the read qword handler */ |
| 4387 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4388 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword);// call read_qword |
| 4389 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 4390 | |
| 4391 | /* 64-bit form */ |
| 4392 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dst,edx:eax |
| 4393 | |
| 4394 | return dst; |
| 4395 | } |
| 4396 | |
| 4397 | |
| 4398 | /*------------------------------------------------- |
| 4399 | op_read8m - process a READ8M opcode |
| 4400 | -------------------------------------------------*/ |
| 4401 | |
| 4402 | static x86code *op_read8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4403 | { |
| 4404 | drcuml_parameter dstp, spacep, addrp, maskp; |
| 4405 | |
| 4406 | /* validate instruction */ |
| 4407 | assert(inst->size == 8); |
| 4408 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4409 | |
| 4410 | /* normalize parameters */ |
| 4411 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI); |
| 4412 | |
| 4413 | /* set up a call to the read qword masked handler */ |
| 4414 | emit_push_p64(drcbe, &dst, &maskp); // push maskp |
| 4415 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4416 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword_masked); |
| 4417 | // call read_qword_masked |
| 4418 | emit_add_r32_imm(&dst, REG_ESP, 12); // add esp,12 |
| 4419 | |
| 4420 | /* 64-bit form */ |
| 4421 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dst,edx:eax |
| 4422 | |
| 4423 | return dst; |
| 4424 | } |
| 4425 | |
| 4426 | |
| 4427 | /*------------------------------------------------- |
| 4428 | op_write1 - process a WRITE1 opcode |
| 4429 | -------------------------------------------------*/ |
| 4430 | |
| 4431 | static x86code *op_write1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4432 | { |
| 4433 | drcuml_parameter spacep, addrp, srcp; |
| 4434 | |
| 4435 | /* validate instruction */ |
| 4436 | assert(inst->size == 4 || inst->size == 8); |
| 4437 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4438 | |
| 4439 | /* normalize parameters */ |
| 4440 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4441 | |
| 4442 | /* set up a call to the write byte handler */ |
| 4443 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 4444 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4445 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_byte);// call write_byte |
| 4446 | emit_add_r32_imm(&dst, REG_ESP, 8); // add esp,8 |
| 4447 | |
| 4448 | return dst; |
| 4449 | } |
| 4450 | |
| 4451 | |
| 4452 | /*------------------------------------------------- |
| 4453 | op_write2 - process a WRITE2 opcode |
| 4454 | -------------------------------------------------*/ |
| 4455 | |
| 4456 | static x86code *op_write2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4457 | { |
| 4458 | drcuml_parameter spacep, addrp, srcp; |
| 4459 | |
| 4460 | /* validate instruction */ |
| 4461 | assert(inst->size == 4 || inst->size == 8); |
| 4462 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4463 | |
| 4464 | /* normalize parameters */ |
| 4465 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4466 | |
| 4467 | /* set up a call to the write word handler */ |
| 4468 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 4469 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4470 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_word);// call write_word |
| 4471 | emit_add_r32_imm(&dst, REG_ESP, 8); // add esp,8 |
| 4472 | |
| 4473 | return dst; |
| 4474 | } |
| 4475 | |
| 4476 | |
| 4477 | /*------------------------------------------------- |
| 4478 | op_writ2m - process a WRIT2M opcode |
| 4479 | -------------------------------------------------*/ |
| 4480 | |
| 4481 | static x86code *op_writ2m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4482 | { |
| 4483 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4484 | |
| 4485 | /* validate instruction */ |
| 4486 | assert(inst->size == 4 || inst->size == 8); |
| 4487 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4488 | |
| 4489 | /* normalize parameters */ |
| 4490 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4491 | |
| 4492 | /* set up a call to the write word handler */ |
| 4493 | emit_push_p32(drcbe, &dst, &maskp); // push maskp |
| 4494 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 4495 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4496 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_word_masked); |
| 4497 | // call write_word_masked |
| 4498 | emit_add_r32_imm(&dst, REG_ESP, 12); // add esp,12 |
| 4499 | return dst; |
| 4500 | } |
| 4501 | |
| 4502 | |
| 4503 | /*------------------------------------------------- |
| 4504 | op_write4 - process a WRITE4 opcode |
| 4505 | -------------------------------------------------*/ |
| 4506 | |
| 4507 | static x86code *op_write4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4508 | { |
| 4509 | drcuml_parameter spacep, addrp, srcp; |
| 4510 | |
| 4511 | /* validate instruction */ |
| 4512 | assert(inst->size == 4 || inst->size == 8); |
| 4513 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4514 | |
| 4515 | /* normalize parameters */ |
| 4516 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4517 | |
| 4518 | /* set up a call to the write dword handler */ |
| 4519 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 4520 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4521 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword);// call write_dword |
| 4522 | emit_add_r32_imm(&dst, REG_ESP, 8); // add esp,8 |
| 4523 | |
| 4524 | return dst; |
| 4525 | } |
| 4526 | |
| 4527 | |
| 4528 | /*------------------------------------------------- |
| 4529 | op_writ4m - process a WRIT4M opcode |
| 4530 | -------------------------------------------------*/ |
| 4531 | |
| 4532 | static x86code *op_writ4m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4533 | { |
| 4534 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4535 | |
| 4536 | /* validate instruction */ |
| 4537 | assert(inst->size == 4 || inst->size == 8); |
| 4538 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4539 | |
| 4540 | /* normalize parameters */ |
| 4541 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4542 | |
| 4543 | /* set up a call to the write word handler */ |
| 4544 | emit_push_p32(drcbe, &dst, &maskp); // push maskp |
| 4545 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 4546 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4547 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword_masked); |
| 4548 | // call write_dword_masked |
| 4549 | emit_add_r32_imm(&dst, REG_ESP, 12); // add esp,12 |
| 4550 | return dst; |
| 4551 | } |
| 4552 | |
| 4553 | |
| 4554 | /*------------------------------------------------- |
| 4555 | op_write8 - process a WRITE8 opcode |
| 4556 | -------------------------------------------------*/ |
| 4557 | |
| 4558 | static x86code *op_write8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4559 | { |
| 4560 | drcuml_parameter spacep, addrp, srcp; |
| 4561 | |
| 4562 | /* validate instruction */ |
| 4563 | assert(inst->size == 8); |
| 4564 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4565 | |
| 4566 | /* normalize parameters */ |
| 4567 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4568 | |
| 4569 | /* set up a call to the write qword handler */ |
| 4570 | emit_push_p64(drcbe, &dst, &srcp); // push srcp |
| 4571 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4572 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword);// call write_qword |
| 4573 | |
| 4574 | emit_add_r32_imm(&dst, REG_ESP, 12); // add esp,12 |
| 4575 | return dst; |
| 4576 | } |
| 4577 | |
| 4578 | |
| 4579 | /*------------------------------------------------- |
| 4580 | op_writ8m - process a WRIT8M opcode |
| 4581 | -------------------------------------------------*/ |
| 4582 | |
| 4583 | static x86code *op_writ8m(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4584 | { |
| 4585 | drcuml_parameter spacep, addrp, maskp, srcp; |
| 4586 | |
| 4587 | /* validate instruction */ |
| 4588 | assert(inst->size == 8); |
| 4589 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4590 | |
| 4591 | /* normalize parameters */ |
| 4592 | param_normalize_4(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &maskp, PTYPE_MRI, &srcp, PTYPE_MRI); |
| 4593 | |
| 4594 | /* set up a call to the write word handler */ |
| 4595 | emit_push_p64(drcbe, &dst, &maskp); // push maskp |
| 4596 | emit_push_p64(drcbe, &dst, &srcp); // push srcp |
| 4597 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 4598 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword_masked); |
| 4599 | // call write_qword_masked |
| 4600 | emit_add_r32_imm(&dst, REG_ESP, 20); // add esp,20 |
| 4601 | return dst; |
| 4602 | } |
| 4603 | |
| 4604 | |
| 4605 | /*------------------------------------------------- |
| 4606 | op_flags - process a FLAGS opcode |
| 4607 | -------------------------------------------------*/ |
| 4608 | |
| 4609 | static x86code *op_flags(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4610 | { |
| 4611 | drcuml_parameter dstp, maskp, tablep; |
| 4612 | int dstreg; |
| 4613 | |
| 4614 | /* validate instruction */ |
| 4615 | assert(inst->size == 4 || inst->size == 8); |
| 4616 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4617 | |
| 4618 | /* normalize parameters */ |
| 4619 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &maskp, PTYPE_I, &tablep, PTYPE_M); |
| 4620 | |
| 4621 | /* pick a target register for the general case */ |
| 4622 | dstreg = param_select_register(REG_ECX, &dstp, NULL); |
| 4623 | |
| 4624 | /* translate live flags into UML flags */ |
| 4625 | emit_pushf(&dst); // pushf |
| 4626 | emit_pop_r32(&dst, REG_EAX); // pop eax |
| 4627 | emit_and_r32_imm(&dst, REG_EAX, 0x8c5); // and eax,0x8c5 |
| 4628 | emit_movzx_r32_m8(&dst, REG_EAX, MBD(REG_EAX, flags_map)); // movzx eax,[flags_map] |
| 4629 | |
| 4630 | /* 32-bit form */ |
| 4631 | if (inst->size == 4) |
| 4632 | { |
| 4633 | /* no masking */ |
| 4634 | if (maskp.value == 0xffffffff) |
| 4635 | { |
| 4636 | emit_mov_r32_m32(&dst, dstreg, MISD(REG_EAX, 4, tablep.value)); // mov dstreg,[eax*4+table] |
| 4637 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4638 | } |
| 4639 | |
| 4640 | /* general case */ |
| 4641 | else |
| 4642 | { |
| 4643 | emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp); // mov dstreg,dstp |
| 4644 | emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 4, tablep.value)); // mov edx,[eax*4+table] |
| 4645 | emit_and_r32_imm(&dst, dstreg, ~maskp.value); // and dstreg,~mask |
| 4646 | emit_and_r32_imm(&dst, REG_EDX, maskp.value); // and edx,mask |
| 4647 | emit_or_r32_r32(&dst, dstreg, REG_EDX); // or dstreg,edx |
| 4648 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | /* 64-bit form */ |
| 4653 | else |
| 4654 | { |
| 4655 | /* no masking */ |
| 4656 | if (maskp.value == U64(0xffffffffffffffff)) |
| 4657 | { |
| 4658 | emit_mov_r32_m32(&dst, dstreg, MISD(REG_EAX, 8, tablep.value)); // mov dstreg,[eax*4+table] |
| 4659 | emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 8, tablep.value + 4)); // mov edx,[eax*4+table+4] |
| 4660 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dst,edx:dstreg |
| 4661 | } |
| 4662 | |
| 4663 | /* general case */ |
| 4664 | else |
| 4665 | { |
| 4666 | if ((UINT32)maskp.value != 0) |
| 4667 | { |
| 4668 | emit_mov_r32_p32(drcbe, &dst, dstreg, &dstp); // mov dstreg,dstp |
| 4669 | emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EAX, 8, tablep.value)); // mov edx,[eax*8+table] |
| 4670 | emit_and_r32_imm(&dst, dstreg, ~maskp.value); // and dstreg,~mask |
| 4671 | emit_and_r32_imm(&dst, REG_EDX, maskp.value); // and edx,mask |
| 4672 | emit_or_r32_r32(&dst, dstreg, REG_EDX); // or dstreg,edx |
| 4673 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg.lo |
| 4674 | } |
| 4675 | if ((UINT32)(maskp.value >> 32) != 0) |
| 4676 | { |
| 4677 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4678 | emit_mov_r32_m32(&dst, REG_ECX, MABS(drcbe->reghi[dstp.value])); // mov ecx,dstp.hi |
| 4679 | else |
| 4680 | emit_mov_r32_m32(&dst, REG_ECX, MABS(dstp.value + 4)); // mov ecx,dstp.hi |
| 4681 | emit_mov_r32_m32(&dst, REG_EDX, MISD(REG_EDX, 8, tablep.value+4)); // mov edx,[eax*8+table+4] |
| 4682 | emit_and_r32_imm(&dst, REG_ECX, ~maskp.value); // and ecx,~mask |
| 4683 | emit_and_r32_imm(&dst, REG_EDX, maskp.value); // and edx,mask |
| 4684 | emit_or_r32_r32(&dst, REG_ECX, REG_EDX); // or ecx,edx |
| 4685 | if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4686 | emit_mov_m32_r32(&dst, MABS(drcbe->reghi[dstp.value]), REG_ECX); // mov dstp.hi,ecx |
| 4687 | else |
| 4688 | emit_mov_m32_r32(&dst, MABS(dstp.value + 4), REG_ECX); // mov dstp.hi,ecx |
| 4689 | } |
| 4690 | } |
| 4691 | } |
| 4692 | return dst; |
| 4693 | } |
| 4694 | |
| 4695 | |
| 4696 | /*------------------------------------------------- |
| 4697 | op_mov - process a MOV opcode |
| 4698 | -------------------------------------------------*/ |
| 4699 | |
| 4700 | static x86code *op_mov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4701 | { |
| 4702 | drcuml_parameter dstp, srcp; |
| 4703 | x86code *savedst = dst; |
| 4704 | emit_link skip = { 0 }; |
| 4705 | int dstreg; |
| 4706 | |
| 4707 | /* validate instruction */ |
| 4708 | assert(inst->size == 4 || inst->size == 8); |
| 4709 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 4710 | |
| 4711 | /* normalize parameters, but only if we got here directly */ |
| 4712 | /* other opcodes call through here with pre-normalized parameters */ |
| 4713 | if (inst->opcode == DRCUML_OP_MOV) |
| 4714 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4715 | else |
| 4716 | { |
| 4717 | dstp = inst->param[0]; |
| 4718 | srcp = inst->param[1]; |
| 4719 | } |
| 4720 | |
| 4721 | /* degenerate case: dest and source are equal */ |
| 4722 | if (dstp.type == srcp.type && dstp.value == srcp.value) |
| 4723 | return dst; |
| 4724 | |
| 4725 | /* pick a target register for the general case */ |
| 4726 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4727 | |
| 4728 | /* always start with a jmp */ |
| 4729 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 4730 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 4731 | |
| 4732 | /* 32-bit form */ |
| 4733 | if (inst->size == 4) |
| 4734 | { |
| 4735 | /* register to memory */ |
| 4736 | if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4737 | emit_mov_m32_r32(&dst, MABS(dstp.value), srcp.value); // mov [dstp],srcp |
| 4738 | |
| 4739 | /* immediate to memory */ |
| 4740 | else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4741 | emit_mov_m32_imm(&dst, MABS(dstp.value), srcp.value); // mov [dstp],srcp |
| 4742 | |
| 4743 | /* conditional memory to register */ |
| 4744 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_MEMORY) |
| 4745 | { |
| 4746 | dst = savedst; |
| 4747 | skip.target = NULL; |
| 4748 | emit_cmovcc_r32_m32(&dst, X86_CONDITION(inst->condflags), dstp.value, MABS(srcp.value)); |
| 4749 | // cmovcc dstp,[srcp] |
| 4750 | } |
| 4751 | |
| 4752 | /* conditional register to register */ |
| 4753 | else if (inst->condflags != 0 && dstp.type == DRCUML_PTYPE_INT_REGISTER && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4754 | { |
| 4755 | dst = savedst; |
| 4756 | skip.target = NULL; |
| 4757 | emit_cmovcc_r32_r32(&dst, X86_CONDITION(inst->condflags), dstp.value, srcp.value); |
| 4758 | // cmovcc dstp,srcp |
| 4759 | } |
| 4760 | |
| 4761 | /* general case */ |
| 4762 | else |
| 4763 | { |
| 4764 | emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp); // mov dstreg,srcp |
| 4765 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4766 | } |
| 4767 | } |
| 4768 | |
| 4769 | /* 64-bit form */ |
| 4770 | else if (inst->size == 8) |
| 4771 | { |
| 4772 | /* register to memory */ |
| 4773 | if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4774 | { |
| 4775 | emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe->reghi[srcp.value])); // mov eax,reghi[srcp] |
| 4776 | emit_mov_m32_r32(&dst, MABS(dstp.value), srcp.value); // mov [dstp],srcp |
| 4777 | emit_mov_m32_r32(&dst, MABS(dstp.value + 4), REG_EAX); // mov [dstp+4],eax |
| 4778 | } |
| 4779 | |
| 4780 | /* immediate to memory */ |
| 4781 | else if (dstp.type == DRCUML_PTYPE_MEMORY && srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4782 | { |
| 4783 | emit_mov_m32_imm(&dst, MABS(dstp.value), srcp.value); // mov [dstp],srcp |
| 4784 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), srcp.value >> 32); // mov [dstp+4],srcp >> 32 |
| 4785 | } |
| 4786 | |
| 4787 | /* general case */ |
| 4788 | else |
| 4789 | { |
| 4790 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &srcp); // mov edx:dstreg,srcp |
| 4791 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,edx:dstreg |
| 4792 | } |
| 4793 | } |
| 4794 | |
| 4795 | /* resolve the jump */ |
| 4796 | if (skip.target != NULL) |
| 4797 | resolve_link(&dst, &skip); |
| 4798 | return dst; |
| 4799 | } |
| 4800 | |
| 4801 | |
| 4802 | /*------------------------------------------------- |
| 4803 | op_zext1 - process a ZEXT1 opcode |
| 4804 | -------------------------------------------------*/ |
| 4805 | |
| 4806 | static x86code *op_zext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4807 | { |
| 4808 | drcuml_parameter dstp, srcp; |
| 4809 | int dstreg; |
| 4810 | |
| 4811 | /* validate instruction */ |
| 4812 | assert(inst->size == 4 || inst->size == 8); |
| 4813 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4814 | |
| 4815 | /* normalize parameters */ |
| 4816 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4817 | |
| 4818 | /* degenerate cases -- convert to a move */ |
| 4819 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4820 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT8)srcp.value); |
| 4821 | |
| 4822 | /* pick a target register for the general case */ |
| 4823 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4824 | |
| 4825 | /* convert 8-bit source registers */ |
| 4826 | if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4)) |
| 4827 | { |
| 4828 | emit_mov_r32_r32(&dst, REG_EAX, srcp.value); // mov eax,srcp |
| 4829 | srcp.value = REG_EAX; |
| 4830 | } |
| 4831 | |
| 4832 | /* 32-bit form */ |
| 4833 | if (inst->size == 4) |
| 4834 | { |
| 4835 | /* general case */ |
| 4836 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4837 | emit_movzx_r32_m8(&dst, dstreg, MABS(srcp.value)); // movzx dstreg,[srcp] |
| 4838 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4839 | emit_movzx_r32_r8(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4840 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4841 | } |
| 4842 | |
| 4843 | /* 64-bit form */ |
| 4844 | else if (inst->size == 8) |
| 4845 | { |
| 4846 | /* general case */ |
| 4847 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4848 | emit_movzx_r32_m8(&dst, dstreg, MABS(srcp.value)); // movzx dstreg,[srcp] |
| 4849 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4850 | emit_movzx_r32_r8(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4851 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4852 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4853 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4854 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4855 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov reghi[dstp],0 |
| 4856 | } |
| 4857 | return dst; |
| 4858 | } |
| 4859 | |
| 4860 | |
| 4861 | /*------------------------------------------------- |
| 4862 | op_zext2 - process a ZEXT2 opcode |
| 4863 | -------------------------------------------------*/ |
| 4864 | |
| 4865 | static x86code *op_zext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4866 | { |
| 4867 | drcuml_parameter dstp, srcp; |
| 4868 | int dstreg; |
| 4869 | |
| 4870 | /* validate instruction */ |
| 4871 | assert(inst->size == 4 || inst->size == 8); |
| 4872 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4873 | |
| 4874 | /* normalize parameters */ |
| 4875 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4876 | |
| 4877 | /* degenerate cases -- convert to a move */ |
| 4878 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4879 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT16)srcp.value); |
| 4880 | |
| 4881 | /* pick a target register for the general case */ |
| 4882 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4883 | |
| 4884 | /* 32-bit form */ |
| 4885 | if (inst->size == 4) |
| 4886 | { |
| 4887 | /* general case */ |
| 4888 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4889 | emit_movzx_r32_m16(&dst, dstreg, MABS(srcp.value)); // movzx dstreg,[srcp] |
| 4890 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4891 | emit_movzx_r32_r16(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4892 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4893 | } |
| 4894 | |
| 4895 | /* 64-bit form */ |
| 4896 | else if (inst->size == 8) |
| 4897 | { |
| 4898 | /* general case */ |
| 4899 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4900 | emit_movzx_r32_m16(&dst, dstreg, MABS(srcp.value)); // movzx dstreg,[srcp] |
| 4901 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4902 | emit_movzx_r32_r16(&dst, dstreg, srcp.value); // movzx dstreg,srcp |
| 4903 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4904 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4905 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4906 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4907 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov reghi[dstp],0 |
| 4908 | } |
| 4909 | return dst; |
| 4910 | } |
| 4911 | |
| 4912 | |
| 4913 | /*------------------------------------------------- |
| 4914 | op_zext4 - process a ZEXT4 opcode |
| 4915 | -------------------------------------------------*/ |
| 4916 | |
| 4917 | static x86code *op_zext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4918 | { |
| 4919 | drcuml_parameter dstp, srcp; |
| 4920 | int dstreg; |
| 4921 | |
| 4922 | /* validate instruction */ |
| 4923 | assert(inst->size == 8); |
| 4924 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4925 | |
| 4926 | /* normalize parameters */ |
| 4927 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4928 | |
| 4929 | /* degenerate cases -- convert to a move */ |
| 4930 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4931 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)srcp.value); |
| 4932 | |
| 4933 | /* pick a target register for the general case */ |
| 4934 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4935 | |
| 4936 | /* 64-bit form */ |
| 4937 | if (inst->size == 8) |
| 4938 | { |
| 4939 | /* general case */ |
| 4940 | emit_mov_r32_p32(drcbe, &dst, dstreg, &srcp); // mov dstreg,srcp |
| 4941 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4942 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 4943 | emit_mov_m32_imm(&dst, MABS(dstp.value + 4), 0); // mov [dstp+4],0 |
| 4944 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4945 | emit_mov_m32_imm(&dst, MABS(drcbe->reghi[dstp.value]), 0); // mov reghi[dstp],0 |
| 4946 | } |
| 4947 | return dst; |
| 4948 | } |
| 4949 | |
| 4950 | |
| 4951 | /*------------------------------------------------- |
| 4952 | op_sext1 - process a SEXT1 opcode |
| 4953 | -------------------------------------------------*/ |
| 4954 | |
| 4955 | static x86code *op_sext1(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 4956 | { |
| 4957 | drcuml_parameter dstp, srcp; |
| 4958 | int dstreg; |
| 4959 | |
| 4960 | /* validate instruction */ |
| 4961 | assert(inst->size == 4 || inst->size == 8); |
| 4962 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 4963 | |
| 4964 | /* normalize parameters */ |
| 4965 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 4966 | |
| 4967 | /* degenerate cases -- convert to a move */ |
| 4968 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 4969 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT8)srcp.value); |
| 4970 | |
| 4971 | /* pick a target register for the general case */ |
| 4972 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 4973 | |
| 4974 | /* convert 8-bit source registers */ |
| 4975 | if (srcp.type == DRCUML_PTYPE_INT_REGISTER && (srcp.value & 4)) |
| 4976 | { |
| 4977 | emit_mov_r32_r32(&dst, REG_EAX, srcp.value); // mov eax,srcp |
| 4978 | srcp.value = REG_EAX; |
| 4979 | } |
| 4980 | |
| 4981 | /* 32-bit form */ |
| 4982 | if (inst->size == 4) |
| 4983 | { |
| 4984 | /* general case */ |
| 4985 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4986 | emit_movsx_r32_m8(&dst, dstreg, MABS(srcp.value)); // movsx dstreg,[srcp] |
| 4987 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4988 | emit_movsx_r32_r8(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 4989 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 4990 | } |
| 4991 | |
| 4992 | /* 64-bit form */ |
| 4993 | else if (inst->size == 8) |
| 4994 | { |
| 4995 | /* general case */ |
| 4996 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 4997 | emit_movsx_r32_m8(&dst, REG_EAX, MABS(srcp.value)); // movsx eax,[srcp] |
| 4998 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 4999 | emit_movsx_r32_r8(&dst, REG_EAX, srcp.value); // movsx eax,srcp |
| 5000 | emit_cdq(&dst); // cdq |
| 5001 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5002 | } |
| 5003 | return dst; |
| 5004 | } |
| 5005 | |
| 5006 | |
| 5007 | /*------------------------------------------------- |
| 5008 | op_sext2 - process a SEXT2 opcode |
| 5009 | -------------------------------------------------*/ |
| 5010 | |
| 5011 | static x86code *op_sext2(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5012 | { |
| 5013 | drcuml_parameter dstp, srcp; |
| 5014 | int dstreg; |
| 5015 | |
| 5016 | /* validate instruction */ |
| 5017 | assert(inst->size == 4 || inst->size == 8); |
| 5018 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 5019 | |
| 5020 | /* normalize parameters */ |
| 5021 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 5022 | |
| 5023 | /* degenerate cases -- convert to a move */ |
| 5024 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 5025 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT16)srcp.value); |
| 5026 | |
| 5027 | /* pick a target register for the general case */ |
| 5028 | dstreg = param_select_register(REG_EAX, &dstp, NULL); |
| 5029 | |
| 5030 | /* 32-bit form */ |
| 5031 | if (inst->size == 4) |
| 5032 | { |
| 5033 | /* general case */ |
| 5034 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 5035 | emit_movsx_r32_m16(&dst, dstreg, MABS(srcp.value)); // movsx dstreg,[srcp] |
| 5036 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 5037 | emit_movsx_r32_r16(&dst, dstreg, srcp.value); // movsx dstreg,srcp |
| 5038 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5039 | } |
| 5040 | |
| 5041 | /* 64-bit form */ |
| 5042 | else if (inst->size == 8) |
| 5043 | { |
| 5044 | /* general case */ |
| 5045 | if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 5046 | emit_movsx_r32_m16(&dst, REG_EAX, MABS(srcp.value)); // movsx eax,[srcp] |
| 5047 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 5048 | emit_movsx_r32_r16(&dst, REG_EAX, srcp.value); // movsx eax,srcp |
| 5049 | emit_cdq(&dst); // cdq |
| 5050 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5051 | } |
| 5052 | return dst; |
| 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | /*------------------------------------------------- |
| 5057 | op_sext4 - process a SEXT4 opcode |
| 5058 | -------------------------------------------------*/ |
| 5059 | |
| 5060 | static x86code *op_sext4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5061 | { |
| 5062 | drcuml_parameter dstp, srcp; |
| 5063 | |
| 5064 | /* validate instruction */ |
| 5065 | assert(inst->size == 8); |
| 5066 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 5067 | |
| 5068 | /* normalize parameters */ |
| 5069 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MRI); |
| 5070 | |
| 5071 | /* degenerate cases -- convert to a move */ |
| 5072 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 5073 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)srcp.value); |
| 5074 | |
| 5075 | /* 64-bit form */ |
| 5076 | if (inst->size == 8) |
| 5077 | { |
| 5078 | /* general case */ |
| 5079 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &srcp); // mov eax,srcp |
| 5080 | emit_cdq(&dst); // cdq |
| 5081 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5082 | } |
| 5083 | return dst; |
| 5084 | } |
| 5085 | |
| 5086 | |
| 5087 | /*------------------------------------------------- |
| 5088 | op_add - process a ADD opcode |
| 5089 | -------------------------------------------------*/ |
| 5090 | |
| 5091 | static x86code *op_add(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5092 | { |
| 5093 | drcuml_parameter dstp, src1p, src2p; |
| 5094 | int dstreg; |
| 5095 | |
| 5096 | /* validate instruction */ |
| 5097 | assert(inst->size == 4 || inst->size == 8); |
| 5098 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5099 | |
| 5100 | /* normalize parameters */ |
| 5101 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5102 | |
| 5103 | /* degenerate cases -- convert to a move */ |
| 5104 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5105 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value + src2p.value); |
| 5106 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5107 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5108 | |
| 5109 | /* pick a target register for the general case */ |
| 5110 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5111 | |
| 5112 | /* 32-bit form */ |
| 5113 | if (inst->size == 4) |
| 5114 | { |
| 5115 | /* dstp == src1p in memory */ |
| 5116 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5117 | emit_add_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // add [dstp],src2p |
| 5118 | |
| 5119 | /* reg = reg + imm */ |
| 5120 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5121 | emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, src2p.value)); // lea dstp,[src1p+src2p] |
| 5122 | |
| 5123 | /* reg = reg + reg */ |
| 5124 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_INT_REGISTER && inst->condflags == 0) |
| 5125 | emit_lea_r32_m32(&dst, dstp.value, MBISD(src1p.value, src2p.value, 1, 0)); // lea dstp,[src1p+src2p] |
| 5126 | |
| 5127 | /* general case */ |
| 5128 | else |
| 5129 | { |
| 5130 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5131 | emit_add_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // add dstreg,src2p |
| 5132 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | /* 64-bit form */ |
| 5137 | else if (inst->size == 8) |
| 5138 | { |
| 5139 | /* dstp == src1p in memory */ |
| 5140 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5141 | emit_add_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // add [dstp],src2p |
| 5142 | |
| 5143 | /* general case */ |
| 5144 | else |
| 5145 | { |
| 5146 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5147 | emit_add_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // add dstreg:dstp,src2p |
| 5148 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5149 | } |
| 5150 | } |
| 5151 | return dst; |
| 5152 | } |
| 5153 | |
| 5154 | |
| 5155 | /*------------------------------------------------- |
| 5156 | op_addc - process a ADDC opcode |
| 5157 | -------------------------------------------------*/ |
| 5158 | |
| 5159 | static x86code *op_addc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5160 | { |
| 5161 | drcuml_parameter dstp, src1p, src2p; |
| 5162 | int dstreg; |
| 5163 | |
| 5164 | /* validate instruction */ |
| 5165 | assert(inst->size == 4 || inst->size == 8); |
| 5166 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5167 | |
| 5168 | /* normalize parameters */ |
| 5169 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5170 | |
| 5171 | /* pick a target register for the general case */ |
| 5172 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5173 | |
| 5174 | /* 32-bit form */ |
| 5175 | if (inst->size == 4) |
| 5176 | { |
| 5177 | /* dstp == src1p in memory */ |
| 5178 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5179 | emit_adc_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // adc [dstp],src2p |
| 5180 | |
| 5181 | /* general case */ |
| 5182 | else |
| 5183 | { |
| 5184 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5185 | emit_adc_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // adc dstreg,src2p |
| 5186 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5187 | } |
| 5188 | } |
| 5189 | |
| 5190 | /* 64-bit form */ |
| 5191 | else if (inst->size == 8) |
| 5192 | { |
| 5193 | /* dstp == src1p in memory */ |
| 5194 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5195 | emit_adc_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // adc [dstp],src2p |
| 5196 | |
| 5197 | /* general case */ |
| 5198 | else |
| 5199 | { |
| 5200 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5201 | emit_adc_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // adc dstreg:dstp,src2p |
| 5202 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5203 | } |
| 5204 | } |
| 5205 | return dst; |
| 5206 | } |
| 5207 | |
| 5208 | |
| 5209 | /*------------------------------------------------- |
| 5210 | op_sub - process a SUB opcode |
| 5211 | -------------------------------------------------*/ |
| 5212 | |
| 5213 | static x86code *op_sub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5214 | { |
| 5215 | drcuml_parameter dstp, src1p, src2p; |
| 5216 | int dstreg; |
| 5217 | |
| 5218 | /* validate instruction */ |
| 5219 | assert(inst->size == 4 || inst->size == 8); |
| 5220 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5221 | |
| 5222 | /* normalize parameters */ |
| 5223 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5224 | |
| 5225 | /* degenerate cases -- convert to a move */ |
| 5226 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5227 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value - src2p.value); |
| 5228 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5229 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5230 | |
| 5231 | /* pick a target register for the general case */ |
| 5232 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5233 | |
| 5234 | /* 32-bit form */ |
| 5235 | if (inst->size == 4) |
| 5236 | { |
| 5237 | /* dstp == src1p in memory */ |
| 5238 | if (dstp.type == DRCUML_PTYPE_MEMORY && src1p.type == DRCUML_PTYPE_MEMORY && src1p.value == dstp.value) |
| 5239 | emit_sub_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // sub [dstp],src2p |
| 5240 | |
| 5241 | /* reg = reg - imm */ |
| 5242 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER && src1p.type == DRCUML_PTYPE_INT_REGISTER && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5243 | emit_lea_r32_m32(&dst, dstp.value, MBD(src1p.value, -src2p.value)); // lea dstp,[src1p-src2p] |
| 5244 | |
| 5245 | /* general case */ |
| 5246 | else |
| 5247 | { |
| 5248 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5249 | emit_sub_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sub dstreg,src2p |
| 5250 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5251 | } |
| 5252 | } |
| 5253 | |
| 5254 | /* 64-bit form */ |
| 5255 | else if (inst->size == 8) |
| 5256 | { |
| 5257 | /* dstp == src1p in memory */ |
| 5258 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5259 | emit_sub_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // sub [dstp],src2p |
| 5260 | |
| 5261 | /* general case */ |
| 5262 | else |
| 5263 | { |
| 5264 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5265 | emit_sub_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // sub dstreg:dstp,src2p |
| 5266 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5267 | } |
| 5268 | } |
| 5269 | return dst; |
| 5270 | } |
| 5271 | |
| 5272 | |
| 5273 | /*------------------------------------------------- |
| 5274 | op_subc - process a SUBC opcode |
| 5275 | -------------------------------------------------*/ |
| 5276 | |
| 5277 | static x86code *op_subc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5278 | { |
| 5279 | drcuml_parameter dstp, src1p, src2p; |
| 5280 | int dstreg; |
| 5281 | |
| 5282 | /* validate instruction */ |
| 5283 | assert(inst->size == 4 || inst->size == 8); |
| 5284 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5285 | |
| 5286 | /* normalize parameters */ |
| 5287 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5288 | |
| 5289 | /* pick a target register for the general case */ |
| 5290 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5291 | |
| 5292 | /* 32-bit form */ |
| 5293 | if (inst->size == 4) |
| 5294 | { |
| 5295 | /* dstp == src1p in memory */ |
| 5296 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5297 | emit_sbb_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // sbb [dstp],src2p |
| 5298 | |
| 5299 | /* general case */ |
| 5300 | else |
| 5301 | { |
| 5302 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5303 | emit_sbb_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sbb dstreg,src2p |
| 5304 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | /* 64-bit form */ |
| 5309 | else if (inst->size == 8) |
| 5310 | { |
| 5311 | /* dstp == src1p in memory */ |
| 5312 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5313 | emit_sbb_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // sbb [dstp],src2p |
| 5314 | |
| 5315 | /* general case */ |
| 5316 | else |
| 5317 | { |
| 5318 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5319 | emit_sbb_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // sbb dstreg:dstp,src2p |
| 5320 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5321 | } |
| 5322 | } |
| 5323 | return dst; |
| 5324 | } |
| 5325 | |
| 5326 | |
| 5327 | /*------------------------------------------------- |
| 5328 | op_cmp - process a CMP opcode |
| 5329 | -------------------------------------------------*/ |
| 5330 | |
| 5331 | static x86code *op_cmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5332 | { |
| 5333 | drcuml_parameter src1p, src2p; |
| 5334 | emit_link skip; |
| 5335 | int src1reg; |
| 5336 | |
| 5337 | /* validate instruction */ |
| 5338 | assert(inst->size == 4 || inst->size == 8); |
| 5339 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5340 | |
| 5341 | /* normalize parameters */ |
| 5342 | param_normalize_2(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5343 | |
| 5344 | /* degenerate cases */ |
| 5345 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && src1p.value == src2p.value) |
| 5346 | { |
| 5347 | emit_cmp_r32_r32(&dst, REG_EAX, REG_EAX); // cmp eax,eax |
| 5348 | return dst; |
| 5349 | } |
| 5350 | |
| 5351 | /* pick a target register for the general case */ |
| 5352 | src1reg = param_select_register(REG_EAX, &src1p, NULL); |
| 5353 | |
| 5354 | /* 32-bit form */ |
| 5355 | if (inst->size == 4) |
| 5356 | { |
| 5357 | /* memory versus anything */ |
| 5358 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5359 | emit_cmp_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst); // cmp [dstp],src2p |
| 5360 | |
| 5361 | /* general case */ |
| 5362 | else |
| 5363 | { |
| 5364 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5365 | emit_mov_r32_imm(&dst, src1reg, src1p.value); // mov src1reg,imm |
| 5366 | emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst); // cmp src1reg,src2p |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | /* 64-bit form */ |
| 5371 | else |
| 5372 | { |
| 5373 | /* memory versus anything */ |
| 5374 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5375 | { |
| 5376 | emit_cmp_m32_p32hi(drcbe, &dst, MABS(src1p.value + 4), &src2p, inst); // cmp [dstp],src2p.hi |
| 5377 | emit_jcc_short_link(&dst, COND_NE, &skip); // jne skip |
| 5378 | emit_cmp_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst); // cmp [dstp],src2p |
| 5379 | resolve_link(&dst, &skip); // skip: |
| 5380 | } |
| 5381 | |
| 5382 | /* general case */ |
| 5383 | else |
| 5384 | { |
| 5385 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5386 | emit_mov_r32_imm(&dst, REG_EAX, src1p.value >> 32); // mov eax,imm >> 32 |
| 5387 | else if (src1p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5388 | emit_mov_r32_m32(&dst, REG_EAX, MABS(drcbe->reghi[src1p.value])); // mov eax,reghi[src1p] |
| 5389 | emit_cmp_r32_p32hi(drcbe, &dst, REG_EAX, &src2p, inst); // cmp eax,src2p.hi |
| 5390 | emit_jcc_short_link(&dst, COND_NE, &skip); // jne skip |
| 5391 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5392 | emit_mov_r32_imm(&dst, src1reg, src1p.value); // mov src1reg,imm |
| 5393 | emit_cmp_r32_p32(drcbe, &dst, src1reg, &src2p, inst); // cmp src1reg,src2p |
| 5394 | resolve_link(&dst, &skip); // skip: |
| 5395 | } |
| 5396 | } |
| 5397 | return dst; |
| 5398 | } |
| 5399 | |
| 5400 | |
| 5401 | /*------------------------------------------------- |
| 5402 | op_mulu - process a MULU opcode |
| 5403 | -------------------------------------------------*/ |
| 5404 | |
| 5405 | static x86code *op_mulu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5406 | { |
| 5407 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5408 | int compute_hi; |
| 5409 | |
| 5410 | /* validate instruction */ |
| 5411 | assert(inst->size == 4 || inst->size == 8); |
| 5412 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5413 | |
| 5414 | /* normalize parameters */ |
| 5415 | param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5416 | compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5417 | |
| 5418 | /* degenerate cases -- convert to a move */ |
| 5419 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5420 | { |
| 5421 | if (inst->size == 4) |
| 5422 | { |
| 5423 | if (!compute_hi) |
| 5424 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value * (UINT32)src2p.value); |
| 5425 | else |
| 5426 | { |
| 5427 | UINT64 result = (UINT64)(UINT32)src1p.value * (UINT64)(UINT32)src2p.value; |
| 5428 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff); |
| 5429 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32); |
| 5430 | } |
| 5431 | } |
| 5432 | else if (inst->size == 8) |
| 5433 | { |
| 5434 | UINT64 reslo, reshi; |
| 5435 | if (!compute_hi) |
| 5436 | { |
| 5437 | dmulu(&reslo, &reslo, src1p.value, src2p.value); |
| 5438 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5439 | } |
| 5440 | else |
| 5441 | { |
| 5442 | dmulu(&reslo, &reshi, src1p.value, src2p.value); |
| 5443 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5444 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5445 | } |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | /* 32-bit form */ |
| 5450 | if (inst->size == 4) |
| 5451 | { |
| 5452 | /* general case */ |
| 5453 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5454 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5455 | emit_mul_m32(&dst, MABS(src2p.value)); // mul [src2p] |
| 5456 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5457 | emit_mul_r32(&dst, src2p.value); // mul src2p |
| 5458 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5459 | { |
| 5460 | emit_mov_r32_imm(&dst, REG_EDX, src2p.value); // mov edx,src2p |
| 5461 | emit_mul_r32(&dst, REG_EDX); // mul edx |
| 5462 | } |
| 5463 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5464 | if (compute_hi) |
| 5465 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5466 | } |
| 5467 | |
| 5468 | /* 64-bit form */ |
| 5469 | else if (inst->size == 8) |
| 5470 | { |
| 5471 | /* general case */ |
| 5472 | emit_push_p64(drcbe, &dst, &src2p); // push src2p |
| 5473 | emit_push_p64(drcbe, &dst, &src1p); // push src1p |
| 5474 | if (!compute_hi) |
| 5475 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5476 | else |
| 5477 | emit_push_imm(&dst, (FPTR)&drcbe->reshi); // push &reshi |
| 5478 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5479 | emit_call(&dst, (x86code *)dmulu); // call dmulu |
| 5480 | emit_add_r32_imm(&dst, REG_ESP, 24); // add esp,24 |
| 5481 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0)); // mov eax,reslo.lo |
| 5482 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1)); // mov edx,reslo.hi |
| 5483 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5484 | if (inst->condflags != 0) |
| 5485 | emit_or_r32_r32(&dst, REG_EAX, REG_EDX); // or eax,edx |
| 5486 | if (compute_hi) |
| 5487 | { |
| 5488 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0)); // mov eax,reshi.lo |
| 5489 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1)); // mov edx,reshi.hi |
| 5490 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX); // mov edstp,edx:eax |
| 5491 | } |
| 5492 | } |
| 5493 | return dst; |
| 5494 | } |
| 5495 | |
| 5496 | |
| 5497 | /*------------------------------------------------- |
| 5498 | op_muls - process a MULS opcode |
| 5499 | -------------------------------------------------*/ |
| 5500 | |
| 5501 | static x86code *op_muls(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5502 | { |
| 5503 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5504 | int compute_hi; |
| 5505 | |
| 5506 | /* validate instruction */ |
| 5507 | assert(inst->size == 4 || inst->size == 8); |
| 5508 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5509 | |
| 5510 | /* normalize parameters */ |
| 5511 | param_normalize_4_commutative(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5512 | compute_hi = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5513 | |
| 5514 | /* degenerate cases -- convert to a move */ |
| 5515 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5516 | { |
| 5517 | if (inst->size == 4) |
| 5518 | { |
| 5519 | if (!compute_hi) |
| 5520 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value * (INT32)src2p.value); |
| 5521 | else |
| 5522 | { |
| 5523 | UINT64 result = (INT64)(INT32)src1p.value * (INT64)(INT32)src2p.value; |
| 5524 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, result & 0xffffffff); |
| 5525 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, result >> 32); |
| 5526 | } |
| 5527 | } |
| 5528 | else if (inst->size == 8) |
| 5529 | { |
| 5530 | UINT64 reslo, reshi; |
| 5531 | if (!compute_hi) |
| 5532 | { |
| 5533 | dmuls(&reslo, &reslo, src1p.value, src2p.value); |
| 5534 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5535 | } |
| 5536 | else |
| 5537 | { |
| 5538 | dmuls(&reslo, &reshi, src1p.value, src2p.value); |
| 5539 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5540 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5541 | } |
| 5542 | } |
| 5543 | } |
| 5544 | |
| 5545 | /* 32-bit form */ |
| 5546 | if (inst->size == 4) |
| 5547 | { |
| 5548 | /* 32-bit destination with memory/immediate or register/immediate */ |
| 5549 | if (!compute_hi && src1p.type != DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5550 | { |
| 5551 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5552 | emit_imul_r32_m32_imm(&dst, REG_EAX, MABS(src1p.value), src2p.value); // imul eax,[src1p],src2p |
| 5553 | else if (src1p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5554 | emit_imul_r32_r32_imm(&dst, REG_EAX, src1p.value, src2p.value); // imul eax,src1p,src2p |
| 5555 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5556 | } |
| 5557 | |
| 5558 | /* 32-bit destination, general case */ |
| 5559 | else if (!compute_hi) |
| 5560 | { |
| 5561 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5562 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5563 | emit_imul_r32_m32(&dst, REG_EAX, MABS(src2p.value)); // imul eax,[src2p] |
| 5564 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5565 | emit_imul_r32_r32(&dst, REG_EAX, src2p.value); // imul eax,src2p |
| 5566 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5567 | } |
| 5568 | |
| 5569 | /* 64-bit destination, general case */ |
| 5570 | else |
| 5571 | { |
| 5572 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5573 | if (src2p.type == DRCUML_PTYPE_MEMORY) |
| 5574 | emit_imul_m32(&dst, MABS(src2p.value)); // imul [src2p] |
| 5575 | else if (src2p.type == DRCUML_PTYPE_INT_REGISTER) |
| 5576 | emit_imul_r32(&dst, src2p.value); // imul src2p |
| 5577 | else if (src2p.type == DRCUML_PTYPE_IMMEDIATE) |
| 5578 | { |
| 5579 | emit_mov_r32_imm(&dst, REG_EDX, src2p.value); // mov edx,src2p |
| 5580 | emit_imul_r32(&dst, REG_EDX); // imul edx |
| 5581 | } |
| 5582 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5583 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | /* 64-bit form */ |
| 5588 | else if (inst->size == 8) |
| 5589 | { |
| 5590 | /* general case */ |
| 5591 | emit_push_p64(drcbe, &dst, &src2p); // push src2p |
| 5592 | emit_push_p64(drcbe, &dst, &src1p); // push src1p |
| 5593 | if (!compute_hi) |
| 5594 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5595 | else |
| 5596 | emit_push_imm(&dst, (FPTR)&drcbe->reshi); // push &reshi |
| 5597 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5598 | emit_call(&dst, (x86code *)dmuls); // call dmuls |
| 5599 | emit_add_r32_imm(&dst, REG_ESP, 24); // add esp,24 |
| 5600 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0)); // mov eax,reslo.lo |
| 5601 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1)); // mov edx,reslo.hi |
| 5602 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5603 | if (inst->condflags != 0) |
| 5604 | emit_or_r32_r32(&dst, REG_EAX, REG_EDX); // or eax,edx |
| 5605 | if (compute_hi) |
| 5606 | { |
| 5607 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0)); // mov eax,reshi.lo |
| 5608 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1)); // mov edx,reshi.hi |
| 5609 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX); // mov edstp,edx:eax |
| 5610 | } |
| 5611 | } |
| 5612 | return dst; |
| 5613 | } |
| 5614 | |
| 5615 | |
| 5616 | /*------------------------------------------------- |
| 5617 | op_divu - process a DIVU opcode |
| 5618 | -------------------------------------------------*/ |
| 5619 | |
| 5620 | static x86code *op_divu(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5621 | { |
| 5622 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5623 | int compute_rem; |
| 5624 | emit_link skip; |
| 5625 | |
| 5626 | /* validate instruction */ |
| 5627 | assert(inst->size == 4 || inst->size == 8); |
| 5628 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5629 | |
| 5630 | /* normalize parameters */ |
| 5631 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5632 | compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5633 | |
| 5634 | /* degenerate cases -- convert to a move */ |
| 5635 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5636 | { |
| 5637 | if (inst->size == 4) |
| 5638 | { |
| 5639 | if ((UINT32)src2p.value == 0) |
| 5640 | return dst; |
| 5641 | if (!compute_rem) |
| 5642 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value); |
| 5643 | else |
| 5644 | { |
| 5645 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value / (UINT32)src2p.value); |
| 5646 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT32)src1p.value % (UINT32)src2p.value); |
| 5647 | } |
| 5648 | } |
| 5649 | else if (inst->size == 8) |
| 5650 | { |
| 5651 | UINT64 reslo, reshi; |
| 5652 | if (src2p.value == 0) |
| 5653 | return dst; |
| 5654 | if (!compute_rem) |
| 5655 | { |
| 5656 | ddivu(&reslo, &reslo, src1p.value, src2p.value); |
| 5657 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5658 | } |
| 5659 | else |
| 5660 | { |
| 5661 | ddivu(&reslo, &reshi, src1p.value, src2p.value); |
| 5662 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5663 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5664 | } |
| 5665 | } |
| 5666 | } |
| 5667 | |
| 5668 | /* 32-bit form */ |
| 5669 | if (inst->size == 4) |
| 5670 | { |
| 5671 | /* general case */ |
| 5672 | emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p); // mov ecx,src2p |
| 5673 | emit_jecxz_link(&dst, &skip); // jecxz skip |
| 5674 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5675 | emit_xor_r32_r32(&dst, REG_EDX, REG_EDX); // xor edx,edx |
| 5676 | emit_div_r32(&dst, REG_ECX); // div ecx |
| 5677 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5678 | if (compute_rem) |
| 5679 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5680 | resolve_link(&dst, &skip); // skip: |
| 5681 | } |
| 5682 | |
| 5683 | /* 64-bit form */ |
| 5684 | else if (inst->size == 8) |
| 5685 | { |
| 5686 | /* general case */ |
| 5687 | emit_push_p64(drcbe, &dst, &src2p); // push src2p |
| 5688 | emit_push_p64(drcbe, &dst, &src1p); // push src1p |
| 5689 | if (!compute_rem) |
| 5690 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5691 | else |
| 5692 | emit_push_imm(&dst, (FPTR)&drcbe->reshi); // push &reshi |
| 5693 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5694 | emit_call(&dst, (x86code *)ddivu); // call ddivu |
| 5695 | emit_add_r32_imm(&dst, REG_ESP, 24); // add esp,24 |
| 5696 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0)); // mov eax,reslo.lo |
| 5697 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1)); // mov edx,reslo.hi |
| 5698 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5699 | if (inst->condflags != 0) |
| 5700 | emit_or_r32_r32(&dst, REG_EAX, REG_EDX); // or eax,edx |
| 5701 | if (compute_rem) |
| 5702 | { |
| 5703 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0)); // mov eax,reshi.lo |
| 5704 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1)); // mov edx,reshi.hi |
| 5705 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX); // mov edstp,edx:eax |
| 5706 | } |
| 5707 | } |
| 5708 | return dst; |
| 5709 | } |
| 5710 | |
| 5711 | |
| 5712 | /*------------------------------------------------- |
| 5713 | op_divs - process a DIVS opcode |
| 5714 | -------------------------------------------------*/ |
| 5715 | |
| 5716 | static x86code *op_divs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5717 | { |
| 5718 | drcuml_parameter dstp, edstp, src1p, src2p; |
| 5719 | int compute_rem; |
| 5720 | emit_link skip; |
| 5721 | |
| 5722 | /* validate instruction */ |
| 5723 | assert(inst->size == 4 || inst->size == 8); |
| 5724 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5725 | |
| 5726 | /* normalize parameters */ |
| 5727 | param_normalize_4(drcbe, inst, &dstp, PTYPE_MR, &edstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5728 | compute_rem = (dstp.type != edstp.type || dstp.value != edstp.value); |
| 5729 | |
| 5730 | /* degenerate cases -- convert to a move */ |
| 5731 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5732 | { |
| 5733 | if (inst->size == 4) |
| 5734 | { |
| 5735 | if ((INT32)src2p.value == 0) |
| 5736 | return dst; |
| 5737 | if (!compute_rem) |
| 5738 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value); |
| 5739 | else |
| 5740 | { |
| 5741 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value / (INT32)src2p.value); |
| 5742 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value % (INT32)src2p.value); |
| 5743 | } |
| 5744 | } |
| 5745 | else if (inst->size == 8) |
| 5746 | { |
| 5747 | UINT64 reslo, reshi; |
| 5748 | if (src2p.value == 0) |
| 5749 | return dst; |
| 5750 | if (!compute_rem) |
| 5751 | { |
| 5752 | ddivs(&reslo, &reslo, src1p.value, src2p.value); |
| 5753 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5754 | } |
| 5755 | else |
| 5756 | { |
| 5757 | ddivs(&reslo, &reshi, src1p.value, src2p.value); |
| 5758 | dst = convert_to_mov_imm(drcbe, dst, inst, &dstp, reslo); |
| 5759 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, reshi); |
| 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | /* 32-bit form */ |
| 5765 | if (inst->size == 4) |
| 5766 | { |
| 5767 | /* general case */ |
| 5768 | emit_mov_r32_p32(drcbe, &dst, REG_ECX, &src2p); // mov ecx,src2p |
| 5769 | emit_jecxz_link(&dst, &skip); // jecxz skip |
| 5770 | emit_mov_r32_p32(drcbe, &dst, REG_EAX, &src1p); // mov eax,src1p |
| 5771 | emit_cdq(&dst); // cdq |
| 5772 | emit_idiv_r32(&dst, REG_ECX); // idiv ecx |
| 5773 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 5774 | if (compute_rem) |
| 5775 | emit_mov_p32_r32(drcbe, &dst, &edstp, REG_EDX); // mov edstp,edx |
| 5776 | resolve_link(&dst, &skip); // skip: |
| 5777 | } |
| 5778 | |
| 5779 | /* 64-bit form */ |
| 5780 | else if (inst->size == 8) |
| 5781 | { |
| 5782 | /* general case */ |
| 5783 | emit_push_p64(drcbe, &dst, &src2p); // push src2p |
| 5784 | emit_push_p64(drcbe, &dst, &src1p); // push src1p |
| 5785 | if (!compute_rem) |
| 5786 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5787 | else |
| 5788 | emit_push_imm(&dst, (FPTR)&drcbe->reshi); // push &reshi |
| 5789 | emit_push_imm(&dst, (FPTR)&drcbe->reslo); // push &reslo |
| 5790 | emit_call(&dst, (x86code *)ddivs); // call ddivs |
| 5791 | emit_add_r32_imm(&dst, REG_ESP, 24); // add esp,24 |
| 5792 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reslo + 0)); // mov eax,reslo.lo |
| 5793 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reslo + 1)); // mov edx,reslo.hi |
| 5794 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 5795 | if (inst->condflags != 0) |
| 5796 | emit_or_r32_r32(&dst, REG_EAX, REG_EDX); // or eax,edx |
| 5797 | if (compute_rem) |
| 5798 | { |
| 5799 | emit_mov_r32_m32(&dst, REG_EAX, MABS((UINT32 *)&drcbe->reshi + 0)); // mov eax,reshi.lo |
| 5800 | emit_mov_r32_m32(&dst, REG_EDX, MABS((UINT32 *)&drcbe->reshi + 1)); // mov edx,reshi.hi |
| 5801 | emit_mov_p64_r64(drcbe, &dst, &edstp, REG_EAX, REG_EDX); // mov edstp,edx:eax |
| 5802 | } |
| 5803 | } |
| 5804 | return dst; |
| 5805 | } |
| 5806 | |
| 5807 | |
| 5808 | /*------------------------------------------------- |
| 5809 | op_and - process a AND opcode |
| 5810 | -------------------------------------------------*/ |
| 5811 | |
| 5812 | static x86code *op_and(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5813 | { |
| 5814 | drcuml_parameter dstp, src1p, src2p; |
| 5815 | int dstreg; |
| 5816 | |
| 5817 | /* validate instruction */ |
| 5818 | assert(inst->size == 4 || inst->size == 8); |
| 5819 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5820 | |
| 5821 | /* normalize parameters */ |
| 5822 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5823 | |
| 5824 | /* degenerate cases -- convert to a move */ |
| 5825 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5826 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value); |
| 5827 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && (src2p.value & size_to_mask[inst->size]) == size_to_mask[inst->size] && inst->condflags == 0) |
| 5828 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5829 | |
| 5830 | /* pick a target register for the general case */ |
| 5831 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5832 | |
| 5833 | /* 32-bit form */ |
| 5834 | if (inst->size == 4) |
| 5835 | { |
| 5836 | /* dstp == src1p in memory */ |
| 5837 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5838 | emit_and_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // and [dstp],src2p |
| 5839 | |
| 5840 | /* general case */ |
| 5841 | else |
| 5842 | { |
| 5843 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5844 | emit_and_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // and dstreg,src2p |
| 5845 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | /* 64-bit form */ |
| 5850 | else if (inst->size == 8) |
| 5851 | { |
| 5852 | /* dstp == src1p in memory */ |
| 5853 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5854 | emit_and_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // and [dstp],src2p |
| 5855 | |
| 5856 | /* general case */ |
| 5857 | else |
| 5858 | { |
| 5859 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5860 | emit_and_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // and dstreg:dstp,src2p |
| 5861 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5862 | } |
| 5863 | } |
| 5864 | return dst; |
| 5865 | } |
| 5866 | |
| 5867 | |
| 5868 | /*------------------------------------------------- |
| 5869 | op_test - process a TEST opcode |
| 5870 | -------------------------------------------------*/ |
| 5871 | |
| 5872 | static x86code *op_test(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5873 | { |
| 5874 | drcuml_parameter src1p, src2p; |
| 5875 | int src1reg; |
| 5876 | |
| 5877 | /* validate instruction */ |
| 5878 | assert(inst->size == 4 || inst->size == 8); |
| 5879 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5880 | |
| 5881 | /* normalize parameters */ |
| 5882 | param_normalize_2_commutative(drcbe, inst, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5883 | |
| 5884 | /* pick a target register for the general case */ |
| 5885 | src1reg = param_select_register(REG_EAX, &src1p, NULL); |
| 5886 | |
| 5887 | /* 32-bit form */ |
| 5888 | if (inst->size == 4) |
| 5889 | { |
| 5890 | /* src1p in memory */ |
| 5891 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5892 | emit_test_m32_p32(drcbe, &dst, MABS(src1p.value), &src2p, inst); // test [src1p],src2p |
| 5893 | |
| 5894 | /* general case */ |
| 5895 | else |
| 5896 | { |
| 5897 | emit_mov_r32_p32(drcbe, &dst, src1reg, &src1p); // mov src1reg,src1p |
| 5898 | emit_test_r32_p32(drcbe, &dst, src1reg, &src2p, inst); // test src1reg,src2p |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | /* 64-bit form */ |
| 5903 | else if (inst->size == 8) |
| 5904 | { |
| 5905 | /* src1p in memory */ |
| 5906 | if (src1p.type == DRCUML_PTYPE_MEMORY) |
| 5907 | emit_test_m64_p64(drcbe, &dst, MABS(src1p.value), &src2p, inst); // test [dstp],src2p |
| 5908 | |
| 5909 | /* general case */ |
| 5910 | else |
| 5911 | { |
| 5912 | emit_mov_r64_p64(drcbe, &dst, src1reg, REG_EDX, &src1p); // mov src1reg:dstp,[src1p] |
| 5913 | emit_test_r64_p64(drcbe, &dst, src1reg, REG_EDX, &src2p, inst); // test src1reg:dstp,src2p |
| 5914 | } |
| 5915 | } |
| 5916 | return dst; |
| 5917 | } |
| 5918 | |
| 5919 | |
| 5920 | /*------------------------------------------------- |
| 5921 | op_or - process a OR opcode |
| 5922 | -------------------------------------------------*/ |
| 5923 | |
| 5924 | static x86code *op_or(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5925 | { |
| 5926 | drcuml_parameter dstp, src1p, src2p; |
| 5927 | int dstreg; |
| 5928 | |
| 5929 | /* validate instruction */ |
| 5930 | assert(inst->size == 4 || inst->size == 8); |
| 5931 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5932 | |
| 5933 | /* normalize parameters */ |
| 5934 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5935 | |
| 5936 | /* degenerate cases -- convert to a move */ |
| 5937 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5938 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value | src2p.value); |
| 5939 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 5940 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 5941 | |
| 5942 | /* pick a target register for the general case */ |
| 5943 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 5944 | |
| 5945 | /* 32-bit form */ |
| 5946 | if (inst->size == 4) |
| 5947 | { |
| 5948 | /* dstp == src1p in memory */ |
| 5949 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5950 | emit_or_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // or [dstp],src2p |
| 5951 | |
| 5952 | /* general case */ |
| 5953 | else |
| 5954 | { |
| 5955 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 5956 | emit_or_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // or dstreg,src2p |
| 5957 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | /* 64-bit form */ |
| 5962 | else if (inst->size == 8) |
| 5963 | { |
| 5964 | /* dstp == src1p in memory */ |
| 5965 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 5966 | emit_or_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // or [dstp],src2p |
| 5967 | |
| 5968 | /* general case */ |
| 5969 | else |
| 5970 | { |
| 5971 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 5972 | emit_or_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // or dstreg:dstp,src2p |
| 5973 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 5974 | } |
| 5975 | } |
| 5976 | return dst; |
| 5977 | } |
| 5978 | |
| 5979 | |
| 5980 | /*------------------------------------------------- |
| 5981 | op_xor - process a XOR opcode |
| 5982 | -------------------------------------------------*/ |
| 5983 | |
| 5984 | static x86code *op_xor(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 5985 | { |
| 5986 | drcuml_parameter dstp, src1p, src2p; |
| 5987 | int dstreg; |
| 5988 | |
| 5989 | /* validate instruction */ |
| 5990 | assert(inst->size == 4 || inst->size == 8); |
| 5991 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 5992 | |
| 5993 | /* normalize parameters */ |
| 5994 | param_normalize_3_commutative(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 5995 | |
| 5996 | /* degenerate cases -- convert to a move */ |
| 5997 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 5998 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value & src2p.value); |
| 5999 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6000 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6001 | |
| 6002 | /* pick a target register for the general case */ |
| 6003 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6004 | |
| 6005 | /* 32-bit form */ |
| 6006 | if (inst->size == 4) |
| 6007 | { |
| 6008 | /* dstp == src1p in memory */ |
| 6009 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6010 | emit_xor_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // xor [dstp],src2p |
| 6011 | |
| 6012 | /* general case */ |
| 6013 | else |
| 6014 | { |
| 6015 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6016 | emit_xor_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // xor dstreg,src2p |
| 6017 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6018 | } |
| 6019 | } |
| 6020 | |
| 6021 | /* 64-bit form */ |
| 6022 | else if (inst->size == 8) |
| 6023 | { |
| 6024 | /* dstp == src1p in memory */ |
| 6025 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6026 | emit_xor_m64_p64(drcbe, &dst, MABS(dstp.value), &src2p, inst); // xor [dstp],src2p |
| 6027 | |
| 6028 | /* general case */ |
| 6029 | else |
| 6030 | { |
| 6031 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6032 | emit_xor_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // xor dstreg:dstp,src2p |
| 6033 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6034 | } |
| 6035 | } |
| 6036 | return dst; |
| 6037 | } |
| 6038 | |
| 6039 | |
| 6040 | /*------------------------------------------------- |
| 6041 | op_shl - process a SHL opcode |
| 6042 | -------------------------------------------------*/ |
| 6043 | |
| 6044 | static x86code *op_shl(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6045 | { |
| 6046 | drcuml_parameter dstp, src1p, src2p; |
| 6047 | int dstreg; |
| 6048 | |
| 6049 | /* validate instruction */ |
| 6050 | assert(inst->size == 4 || inst->size == 8); |
| 6051 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6052 | |
| 6053 | /* normalize parameters */ |
| 6054 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6055 | |
| 6056 | /* degenerate cases -- convert to a move */ |
| 6057 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6058 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, src1p.value << src2p.value); |
| 6059 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6060 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6061 | |
| 6062 | /* pick a target register for the general case */ |
| 6063 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6064 | |
| 6065 | /* 32-bit form */ |
| 6066 | if (inst->size == 4) |
| 6067 | { |
| 6068 | /* dstp == src1p in memory */ |
| 6069 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6070 | emit_shl_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // shl [dstp],src2p |
| 6071 | |
| 6072 | /* general case */ |
| 6073 | else |
| 6074 | { |
| 6075 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6076 | emit_shl_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // shl dstreg,src2p |
| 6077 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6078 | } |
| 6079 | } |
| 6080 | |
| 6081 | /* 64-bit form */ |
| 6082 | else if (inst->size == 8) |
| 6083 | { |
| 6084 | /* general case */ |
| 6085 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6086 | emit_shl_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // shl dstreg:dstp,src2p |
| 6087 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6088 | } |
| 6089 | return dst; |
| 6090 | } |
| 6091 | |
| 6092 | |
| 6093 | /*------------------------------------------------- |
| 6094 | op_shr - process a SHR opcode |
| 6095 | -------------------------------------------------*/ |
| 6096 | |
| 6097 | static x86code *op_shr(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6098 | { |
| 6099 | drcuml_parameter dstp, src1p, src2p; |
| 6100 | int dstreg; |
| 6101 | |
| 6102 | /* validate instruction */ |
| 6103 | assert(inst->size == 4 || inst->size == 8); |
| 6104 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6105 | |
| 6106 | /* normalize parameters */ |
| 6107 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6108 | |
| 6109 | /* degenerate cases -- convert to a move */ |
| 6110 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6111 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6112 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6113 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6114 | |
| 6115 | /* pick a target register for the general case */ |
| 6116 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6117 | |
| 6118 | /* 32-bit form */ |
| 6119 | if (inst->size == 4) |
| 6120 | { |
| 6121 | /* dstp == src1p in memory */ |
| 6122 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6123 | emit_shr_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // shr [dstp],src2p |
| 6124 | |
| 6125 | /* general case */ |
| 6126 | else |
| 6127 | { |
| 6128 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6129 | emit_shr_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // shr dstreg,src2p |
| 6130 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6131 | } |
| 6132 | } |
| 6133 | |
| 6134 | /* 64-bit form */ |
| 6135 | else if (inst->size == 8) |
| 6136 | { |
| 6137 | /* general case */ |
| 6138 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6139 | emit_shr_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // shr dstreg:dstp,src2p |
| 6140 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6141 | } |
| 6142 | return dst; |
| 6143 | } |
| 6144 | |
| 6145 | |
| 6146 | /*------------------------------------------------- |
| 6147 | op_sar - process a SAR opcode |
| 6148 | -------------------------------------------------*/ |
| 6149 | |
| 6150 | static x86code *op_sar(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6151 | { |
| 6152 | drcuml_parameter dstp, src1p, src2p; |
| 6153 | int dstreg; |
| 6154 | |
| 6155 | /* validate instruction */ |
| 6156 | assert(inst->size == 4 || inst->size == 8); |
| 6157 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6158 | |
| 6159 | /* normalize parameters */ |
| 6160 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6161 | |
| 6162 | /* degenerate cases -- convert to a move */ |
| 6163 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6164 | { |
| 6165 | if (inst->size == 4) |
| 6166 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT32)src1p.value >> src2p.value); |
| 6167 | else if (inst->size == 8) |
| 6168 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (INT64)src1p.value >> src2p.value); |
| 6169 | } |
| 6170 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6171 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6172 | |
| 6173 | /* pick a target register for the general case */ |
| 6174 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6175 | |
| 6176 | /* 32-bit form */ |
| 6177 | if (inst->size == 4) |
| 6178 | { |
| 6179 | /* dstp == src1p in memory */ |
| 6180 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6181 | emit_sar_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // sar [dstp],src2p |
| 6182 | |
| 6183 | /* general case */ |
| 6184 | else |
| 6185 | { |
| 6186 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6187 | emit_sar_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // sar dstreg,src2p |
| 6188 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6189 | } |
| 6190 | } |
| 6191 | |
| 6192 | /* 64-bit form */ |
| 6193 | else if (inst->size == 8) |
| 6194 | { |
| 6195 | /* general case */ |
| 6196 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6197 | emit_sar_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // sar dstreg:dstp,src2p |
| 6198 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6199 | } |
| 6200 | return dst; |
| 6201 | } |
| 6202 | |
| 6203 | |
| 6204 | /*------------------------------------------------- |
| 6205 | op_rol - process a rol opcode |
| 6206 | -------------------------------------------------*/ |
| 6207 | |
| 6208 | static x86code *op_rol(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6209 | { |
| 6210 | drcuml_parameter dstp, src1p, src2p; |
| 6211 | int dstreg; |
| 6212 | |
| 6213 | /* validate instruction */ |
| 6214 | assert(inst->size == 4 || inst->size == 8); |
| 6215 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6216 | |
| 6217 | /* normalize parameters */ |
| 6218 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6219 | |
| 6220 | /* degenerate cases -- convert to a move */ |
| 6221 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6222 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6223 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6224 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6225 | |
| 6226 | /* pick a target register for the general case */ |
| 6227 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6228 | |
| 6229 | /* 32-bit form */ |
| 6230 | if (inst->size == 4) |
| 6231 | { |
| 6232 | /* dstp == src1p in memory */ |
| 6233 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6234 | emit_rol_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // rol [dstp],src2p |
| 6235 | |
| 6236 | /* general case */ |
| 6237 | else |
| 6238 | { |
| 6239 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6240 | emit_rol_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rol dstreg,src2p |
| 6241 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6242 | } |
| 6243 | } |
| 6244 | |
| 6245 | /* 64-bit form */ |
| 6246 | else if (inst->size == 8) |
| 6247 | { |
| 6248 | /* general case */ |
| 6249 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6250 | emit_rol_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // rol dstreg:dstp,src2p |
| 6251 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6252 | } |
| 6253 | return dst; |
| 6254 | } |
| 6255 | |
| 6256 | |
| 6257 | /*------------------------------------------------- |
| 6258 | op_ror - process a ROR opcode |
| 6259 | -------------------------------------------------*/ |
| 6260 | |
| 6261 | static x86code *op_ror(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6262 | { |
| 6263 | drcuml_parameter dstp, src1p, src2p; |
| 6264 | int dstreg; |
| 6265 | |
| 6266 | /* validate instruction */ |
| 6267 | assert(inst->size == 4 || inst->size == 8); |
| 6268 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6269 | |
| 6270 | /* normalize parameters */ |
| 6271 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6272 | |
| 6273 | /* degenerate cases -- convert to a move */ |
| 6274 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6275 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6276 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6277 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6278 | |
| 6279 | /* pick a target register for the general case */ |
| 6280 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6281 | |
| 6282 | /* 32-bit form */ |
| 6283 | if (inst->size == 4) |
| 6284 | { |
| 6285 | /* dstp == src1p in memory */ |
| 6286 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6287 | emit_ror_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // ror [dstp],src2p |
| 6288 | |
| 6289 | /* general case */ |
| 6290 | else |
| 6291 | { |
| 6292 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6293 | emit_ror_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // ror dstreg,src2p |
| 6294 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6295 | } |
| 6296 | } |
| 6297 | |
| 6298 | /* 64-bit form */ |
| 6299 | else if (inst->size == 8) |
| 6300 | { |
| 6301 | /* general case */ |
| 6302 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6303 | emit_ror_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // ror dstreg:dstp,src2p |
| 6304 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6305 | } |
| 6306 | return dst; |
| 6307 | } |
| 6308 | |
| 6309 | |
| 6310 | /*------------------------------------------------- |
| 6311 | op_rolc - process a ROLC opcode |
| 6312 | -------------------------------------------------*/ |
| 6313 | |
| 6314 | static x86code *op_rolc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6315 | { |
| 6316 | drcuml_parameter dstp, src1p, src2p; |
| 6317 | int dstreg; |
| 6318 | |
| 6319 | /* validate instruction */ |
| 6320 | assert(inst->size == 4 || inst->size == 8); |
| 6321 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6322 | |
| 6323 | /* normalize parameters */ |
| 6324 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6325 | |
| 6326 | /* degenerate cases -- convert to a move */ |
| 6327 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6328 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6329 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6330 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6331 | |
| 6332 | /* pick a target register for the general case */ |
| 6333 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6334 | |
| 6335 | /* 32-bit form */ |
| 6336 | if (inst->size == 4) |
| 6337 | { |
| 6338 | /* dstp == src1p in memory */ |
| 6339 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6340 | emit_rcl_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // rcl [dstp],src2p |
| 6341 | |
| 6342 | /* general case */ |
| 6343 | else |
| 6344 | { |
| 6345 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6346 | emit_rcl_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rcl dstreg,src2p |
| 6347 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | /* 64-bit form */ |
| 6352 | else if (inst->size == 8) |
| 6353 | { |
| 6354 | /* general case */ |
| 6355 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6356 | emit_rcl_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // rcl dstreg:dstp,src2p |
| 6357 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6358 | } |
| 6359 | return dst; |
| 6360 | } |
| 6361 | |
| 6362 | |
| 6363 | /*------------------------------------------------- |
| 6364 | op_rorc - process a RORC opcode |
| 6365 | -------------------------------------------------*/ |
| 6366 | |
| 6367 | static x86code *op_rorc(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6368 | { |
| 6369 | drcuml_parameter dstp, src1p, src2p; |
| 6370 | int dstreg; |
| 6371 | |
| 6372 | /* validate instruction */ |
| 6373 | assert(inst->size == 4 || inst->size == 8); |
| 6374 | assert((inst->condflags & ~(DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 6375 | |
| 6376 | /* normalize parameters */ |
| 6377 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MR, &src1p, PTYPE_MRI, &src2p, PTYPE_MRI); |
| 6378 | |
| 6379 | /* degenerate cases -- convert to a move */ |
| 6380 | if (src1p.type == DRCUML_PTYPE_IMMEDIATE && src2p.type == DRCUML_PTYPE_IMMEDIATE && inst->condflags == 0) |
| 6381 | return convert_to_mov_imm(drcbe, dst, inst, &dstp, (UINT64)src1p.value >> src2p.value); |
| 6382 | if (src2p.type == DRCUML_PTYPE_IMMEDIATE && src2p.value == 0 && inst->condflags == 0) |
| 6383 | return convert_to_mov_src1(drcbe, dst, inst, &dstp, &src1p); |
| 6384 | |
| 6385 | /* pick a target register for the general case */ |
| 6386 | dstreg = param_select_register(REG_EAX, &dstp, &src2p); |
| 6387 | |
| 6388 | /* 32-bit form */ |
| 6389 | if (inst->size == 4) |
| 6390 | { |
| 6391 | /* dstp == src1p in memory */ |
| 6392 | if (src1p.type == dstp.type && src1p.value == dstp.value && dstp.type == DRCUML_PTYPE_MEMORY) |
| 6393 | emit_rcr_m32_p32(drcbe, &dst, MABS(dstp.value), &src2p, inst); // rcr [dstp],src2p |
| 6394 | |
| 6395 | /* general case */ |
| 6396 | else |
| 6397 | { |
| 6398 | emit_mov_r32_p32(drcbe, &dst, dstreg, &src1p); // mov dstreg,src1p |
| 6399 | emit_rcr_r32_p32(drcbe, &dst, dstreg, &src2p, inst); // rcr dstreg,src2p |
| 6400 | emit_mov_p32_r32(drcbe, &dst, &dstp, dstreg); // mov dstp,dstreg |
| 6401 | } |
| 6402 | } |
| 6403 | |
| 6404 | /* 64-bit form */ |
| 6405 | else if (inst->size == 8) |
| 6406 | { |
| 6407 | /* general case */ |
| 6408 | emit_mov_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src1p); // mov dstreg:dstp,[src1p] |
| 6409 | emit_rcr_r64_p64(drcbe, &dst, dstreg, REG_EDX, &src2p, inst); // rcr dstreg:dstp,src2p |
| 6410 | emit_mov_p64_r64(drcbe, &dst, &dstp, dstreg, REG_EDX); // mov dstp,dstreg:eax |
| 6411 | } |
| 6412 | return dst; |
| 6413 | } |
| 6414 | |
| 6415 | |
| 6416 | |
| 6417 | /*************************************************************************** |
| 6418 | FLOATING POINT OPERATIONS |
| 6419 | ***************************************************************************/ |
| 6420 | |
| 6421 | /*------------------------------------------------- |
| 6422 | op_fload - process a FLOAD opcode |
| 6423 | -------------------------------------------------*/ |
| 6424 | |
| 6425 | static x86code *op_fload(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6426 | { |
| 6427 | drcuml_parameter dstp, basep, indp; |
| 6428 | |
| 6429 | /* validate instruction */ |
| 6430 | assert(inst->size == 4 || inst->size == 8); |
| 6431 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6432 | |
| 6433 | /* normalize parameters */ |
| 6434 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &basep, PTYPE_M, &indp, PTYPE_MRI); |
| 6435 | |
| 6436 | /* immediate index */ |
| 6437 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6438 | { |
| 6439 | if (inst->size == 4) |
| 6440 | emit_fld_m32(&dst, MABS(basep.value + 4*indp.value)); // fld [basep + 4*indp] |
| 6441 | else if (inst->size == 8) |
| 6442 | emit_fld_m64(&dst, MABS(basep.value + 8*indp.value)); // fld [basep + 8*indp] |
| 6443 | } |
| 6444 | |
| 6445 | /* other index */ |
| 6446 | else |
| 6447 | { |
| 6448 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6449 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 6450 | if (inst->size == 4) |
| 6451 | emit_fld_m32(&dst, MISD(indreg, 4, basep.value)); // fld [basep + 4*indp] |
| 6452 | else if (inst->size == 8) |
| 6453 | emit_fld_m64(&dst, MISD(indreg, 8, basep.value)); // fld [basep + 8*indp] |
| 6454 | } |
| 6455 | |
| 6456 | /* general case */ |
| 6457 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 6458 | |
| 6459 | return dst; |
| 6460 | } |
| 6461 | |
| 6462 | |
| 6463 | /*------------------------------------------------- |
| 6464 | op_fstore - process a FSTORE opcode |
| 6465 | -------------------------------------------------*/ |
| 6466 | |
| 6467 | static x86code *op_fstore(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6468 | { |
| 6469 | drcuml_parameter srcp, basep, indp; |
| 6470 | |
| 6471 | /* validate instruction */ |
| 6472 | assert(inst->size == 4 || inst->size == 8); |
| 6473 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6474 | |
| 6475 | /* normalize parameters */ |
| 6476 | param_normalize_3(drcbe, inst, &basep, PTYPE_M, &indp, PTYPE_MRI, &srcp, PTYPE_MF); |
| 6477 | |
| 6478 | /* general case */ |
| 6479 | emit_fld_p(&dst, inst->size, &srcp); |
| 6480 | |
| 6481 | /* immediate index */ |
| 6482 | if (indp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6483 | { |
| 6484 | if (inst->size == 4) |
| 6485 | emit_fstp_m32(&dst, MABS(basep.value + 4*indp.value)); // fstp [basep + 4*indp] |
| 6486 | else if (inst->size == 8) |
| 6487 | emit_fstp_m64(&dst, MABS(basep.value + 8*indp.value)); // fstp [basep + 8*indp] |
| 6488 | } |
| 6489 | |
| 6490 | /* other index */ |
| 6491 | else |
| 6492 | { |
| 6493 | int indreg = param_select_register(REG_ECX, &indp, NULL); |
| 6494 | emit_mov_r32_p32(drcbe, &dst, indreg, &indp); |
| 6495 | if (inst->size == 4) |
| 6496 | emit_fstp_m32(&dst, MISD(indreg, 4, basep.value)); // fstp [basep + 4*indp] |
| 6497 | else if (inst->size == 8) |
| 6498 | emit_fstp_m64(&dst, MISD(indreg, 8, basep.value)); // fstp [basep + 8*indp] |
| 6499 | } |
| 6500 | return dst; |
| 6501 | } |
| 6502 | |
| 6503 | |
| 6504 | /*------------------------------------------------- |
| 6505 | op_fread - process a FREAD opcode |
| 6506 | -------------------------------------------------*/ |
| 6507 | |
| 6508 | static x86code *op_fread(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6509 | { |
| 6510 | drcuml_parameter dstp, spacep, addrp; |
| 6511 | |
| 6512 | /* validate instruction */ |
| 6513 | assert(inst->size == 4 || inst->size == 8); |
| 6514 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6515 | |
| 6516 | /* normalize parameters */ |
| 6517 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &spacep, PTYPE_I, &addrp, PTYPE_MRI); |
| 6518 | |
| 6519 | /* set up a call to the read dword/qword handler */ |
| 6520 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 6521 | if (inst->size == 4) |
| 6522 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_dword);// call read_dword |
| 6523 | else if (inst->size == 8) |
| 6524 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->read_qword);// call read_qword |
| 6525 | emit_add_r32_imm(&dst, REG_ESP, 4); // add esp,4 |
| 6526 | |
| 6527 | /* store result */ |
| 6528 | if (inst->size == 4) |
| 6529 | emit_mov_p32_r32(drcbe, &dst, &dstp, REG_EAX); // mov dstp,eax |
| 6530 | else if (inst->size == 8) |
| 6531 | emit_mov_p64_r64(drcbe, &dst, &dstp, REG_EAX, REG_EDX); // mov dstp,edx:eax |
| 6532 | |
| 6533 | return dst; |
| 6534 | } |
| 6535 | |
| 6536 | |
| 6537 | /*------------------------------------------------- |
| 6538 | op_fwrite - process a FWRITE opcode |
| 6539 | -------------------------------------------------*/ |
| 6540 | |
| 6541 | static x86code *op_fwrite(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6542 | { |
| 6543 | drcuml_parameter spacep, addrp, srcp; |
| 6544 | |
| 6545 | /* validate instruction */ |
| 6546 | assert(inst->size == 4 || inst->size == 8); |
| 6547 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6548 | |
| 6549 | /* normalize parameters */ |
| 6550 | param_normalize_3(drcbe, inst, &spacep, PTYPE_I, &addrp, PTYPE_MRI, &srcp, PTYPE_MF); |
| 6551 | |
| 6552 | /* set up a call to the write dword/qword handler */ |
| 6553 | if (inst->size == 4) |
| 6554 | emit_push_p32(drcbe, &dst, &srcp); // push srcp |
| 6555 | else if (inst->size == 8) |
| 6556 | emit_push_p64(drcbe, &dst, &srcp); // push srcp |
| 6557 | emit_push_p32(drcbe, &dst, &addrp); // push addrp |
| 6558 | if (inst->size == 4) |
| 6559 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_dword);// call write_dword |
| 6560 | else if (inst->size == 8) |
| 6561 | emit_call(&dst, (x86code *)active_address_space[spacep.value].accessors->write_qword);// call write_qword |
| 6562 | emit_add_r32_imm(&dst, REG_ESP, 4 + inst->size); // add esp,8 |
| 6563 | |
| 6564 | return dst; |
| 6565 | } |
| 6566 | |
| 6567 | |
| 6568 | /*------------------------------------------------- |
| 6569 | op_fmov - process a FMOV opcode |
| 6570 | -------------------------------------------------*/ |
| 6571 | |
| 6572 | static x86code *op_fmov(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6573 | { |
| 6574 | drcuml_parameter dstp, srcp; |
| 6575 | emit_link skip = { 0 }; |
| 6576 | |
| 6577 | /* validate instruction */ |
| 6578 | assert(inst->size == 4 || inst->size == 8); |
| 6579 | assert(inst->condflags == DRCUML_COND_ALWAYS || (inst->condflags >= COND_Z && inst->condflags < DRCUML_COND_MAX)); |
| 6580 | |
| 6581 | /* normalize parameters */ |
| 6582 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 6583 | |
| 6584 | /* always start with a jmp */ |
| 6585 | if (inst->condflags != DRCUML_COND_ALWAYS) |
| 6586 | emit_jcc_short_link(&dst, X86_NOT_CONDITION(inst->condflags), &skip); // jcc skip |
| 6587 | |
| 6588 | /* general case */ |
| 6589 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6590 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 6591 | |
| 6592 | /* resolve the jump */ |
| 6593 | if (skip.target != NULL) |
| 6594 | resolve_link(&dst, &skip); // skip: |
| 6595 | return dst; |
| 6596 | } |
| 6597 | |
| 6598 | |
| 6599 | /*------------------------------------------------- |
| 6600 | op_ftoi4 - process a FTOI4 opcode |
| 6601 | -------------------------------------------------*/ |
| 6602 | |
| 6603 | static x86code *op_ftoi4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6604 | { |
| 6605 | drcuml_parameter dstp, srcp; |
| 6606 | |
| 6607 | /* validate instruction */ |
| 6608 | assert(inst->size == 4 || inst->size == 8); |
| 6609 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6610 | |
| 6611 | /* normalize parameters */ |
| 6612 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6613 | |
| 6614 | /* general case */ |
| 6615 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6616 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6617 | emit_fistp_m32(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6618 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6619 | { |
| 6620 | emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6621 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6622 | } |
| 6623 | return dst; |
| 6624 | } |
| 6625 | |
| 6626 | |
| 6627 | /*------------------------------------------------- |
| 6628 | op_ftoi4t - process a FTOI4T opcode |
| 6629 | -------------------------------------------------*/ |
| 6630 | |
| 6631 | static x86code *op_ftoi4t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6632 | { |
| 6633 | drcuml_parameter dstp, srcp; |
| 6634 | |
| 6635 | /* validate instruction */ |
| 6636 | assert(inst->size == 4 || inst->size == 8); |
| 6637 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6638 | |
| 6639 | /* normalize parameters */ |
| 6640 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6641 | |
| 6642 | /* general case */ |
| 6643 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6644 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6645 | emit_fisttp_m32(&dst, MABS(dstp.value)); // fisttp [dstp] |
| 6646 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6647 | { |
| 6648 | emit_fisttp_m32(&dst, MABS(drcbe->reglo[dstp.value])); // fisttp reglo[dstp] |
| 6649 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6650 | } |
| 6651 | return dst; |
| 6652 | } |
| 6653 | |
| 6654 | |
| 6655 | /*------------------------------------------------- |
| 6656 | op_ftoi4r - process a FTOI4R opcode |
| 6657 | -------------------------------------------------*/ |
| 6658 | |
| 6659 | static x86code *op_ftoi4r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6660 | { |
| 6661 | drcuml_parameter dstp, srcp; |
| 6662 | |
| 6663 | /* validate instruction */ |
| 6664 | assert(inst->size == 4 || inst->size == 8); |
| 6665 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6666 | |
| 6667 | /* normalize parameters */ |
| 6668 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6669 | |
| 6670 | /* save and set the control word */ |
| 6671 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6672 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_ROUND])); // fldcw fpcontrol[DRCUML_FMOD_ROUND] |
| 6673 | |
| 6674 | /* general case */ |
| 6675 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6676 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6677 | emit_fistp_m32(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6678 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6679 | { |
| 6680 | emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6681 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6682 | } |
| 6683 | |
| 6684 | /* restore control word and proceed */ |
| 6685 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6686 | return dst; |
| 6687 | } |
| 6688 | |
| 6689 | |
| 6690 | /*------------------------------------------------- |
| 6691 | op_ftoi4f - process a FTOI4F opcode |
| 6692 | -------------------------------------------------*/ |
| 6693 | |
| 6694 | static x86code *op_ftoi4f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6695 | { |
| 6696 | drcuml_parameter dstp, srcp; |
| 6697 | |
| 6698 | /* validate instruction */ |
| 6699 | assert(inst->size == 4 || inst->size == 8); |
| 6700 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6701 | |
| 6702 | /* normalize parameters */ |
| 6703 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6704 | |
| 6705 | /* save and set the control word */ |
| 6706 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6707 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_FLOOR])); // fldcw fpcontrol[DRCUML_FMOD_FLOOR] |
| 6708 | |
| 6709 | /* general case */ |
| 6710 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6711 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6712 | emit_fistp_m32(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6713 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6714 | { |
| 6715 | emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6716 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6717 | } |
| 6718 | |
| 6719 | /* restore control word and proceed */ |
| 6720 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6721 | return dst; |
| 6722 | } |
| 6723 | |
| 6724 | |
| 6725 | /*------------------------------------------------- |
| 6726 | op_ftoi4c - process a FTOI4C opcode |
| 6727 | -------------------------------------------------*/ |
| 6728 | |
| 6729 | static x86code *op_ftoi4c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6730 | { |
| 6731 | drcuml_parameter dstp, srcp; |
| 6732 | |
| 6733 | /* validate instruction */ |
| 6734 | assert(inst->size == 4 || inst->size == 8); |
| 6735 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6736 | |
| 6737 | /* normalize parameters */ |
| 6738 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6739 | |
| 6740 | /* save and set the control word */ |
| 6741 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6742 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_CEIL])); // fldcw fpcontrol[DRCUML_FMOD_CEIL] |
| 6743 | |
| 6744 | /* general case */ |
| 6745 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6746 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6747 | emit_fistp_m32(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6748 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6749 | { |
| 6750 | emit_fistp_m32(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6751 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6752 | } |
| 6753 | |
| 6754 | /* restore control word and proceed */ |
| 6755 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6756 | return dst; |
| 6757 | } |
| 6758 | |
| 6759 | |
| 6760 | /*------------------------------------------------- |
| 6761 | op_ftoi8 - process a FTOI8 opcode |
| 6762 | -------------------------------------------------*/ |
| 6763 | |
| 6764 | static x86code *op_ftoi8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6765 | { |
| 6766 | drcuml_parameter dstp, srcp; |
| 6767 | |
| 6768 | /* validate instruction */ |
| 6769 | assert(inst->size == 4 || inst->size == 8); |
| 6770 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6771 | |
| 6772 | /* normalize parameters */ |
| 6773 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6774 | |
| 6775 | /* general case */ |
| 6776 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6777 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6778 | emit_fistp_m64(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6779 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6780 | { |
| 6781 | emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6782 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6783 | } |
| 6784 | return dst; |
| 6785 | } |
| 6786 | |
| 6787 | |
| 6788 | /*------------------------------------------------- |
| 6789 | op_ftoi8t - process a FTOI8T opcode |
| 6790 | -------------------------------------------------*/ |
| 6791 | |
| 6792 | static x86code *op_ftoi8t(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6793 | { |
| 6794 | drcuml_parameter dstp, srcp; |
| 6795 | |
| 6796 | /* validate instruction */ |
| 6797 | assert(inst->size == 4 || inst->size == 8); |
| 6798 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6799 | |
| 6800 | /* normalize parameters */ |
| 6801 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6802 | |
| 6803 | /* general case */ |
| 6804 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6805 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6806 | emit_fisttp_m64(&dst, MABS(dstp.value)); // fisttp [dstp] |
| 6807 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6808 | { |
| 6809 | emit_fisttp_m64(&dst, MABS(drcbe->reglo[dstp.value])); // fisttp reglo[dstp] |
| 6810 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6811 | } |
| 6812 | return dst; |
| 6813 | } |
| 6814 | |
| 6815 | |
| 6816 | /*------------------------------------------------- |
| 6817 | op_ftoi8r - process a FTOI8R opcode |
| 6818 | -------------------------------------------------*/ |
| 6819 | |
| 6820 | static x86code *op_ftoi8r(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6821 | { |
| 6822 | drcuml_parameter dstp, srcp; |
| 6823 | |
| 6824 | /* validate instruction */ |
| 6825 | assert(inst->size == 4 || inst->size == 8); |
| 6826 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6827 | |
| 6828 | /* normalize parameters */ |
| 6829 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6830 | |
| 6831 | /* save and set the control word */ |
| 6832 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6833 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_ROUND])); // fldcw fpcontrol[DRCUML_FMOD_ROUND] |
| 6834 | |
| 6835 | /* general case */ |
| 6836 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6837 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6838 | emit_fistp_m64(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6839 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6840 | { |
| 6841 | emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6842 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6843 | } |
| 6844 | |
| 6845 | /* restore control word and proceed */ |
| 6846 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6847 | return dst; |
| 6848 | } |
| 6849 | |
| 6850 | |
| 6851 | /*------------------------------------------------- |
| 6852 | op_ftoi8f - process a FTOI8F opcode |
| 6853 | -------------------------------------------------*/ |
| 6854 | |
| 6855 | static x86code *op_ftoi8f(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6856 | { |
| 6857 | drcuml_parameter dstp, srcp; |
| 6858 | |
| 6859 | /* validate instruction */ |
| 6860 | assert(inst->size == 4 || inst->size == 8); |
| 6861 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6862 | |
| 6863 | /* normalize parameters */ |
| 6864 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6865 | |
| 6866 | /* save and set the control word */ |
| 6867 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6868 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_FLOOR])); // fldcw fpcontrol[DRCUML_FMOD_FLOOR] |
| 6869 | |
| 6870 | /* general case */ |
| 6871 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6872 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6873 | emit_fistp_m64(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6874 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6875 | { |
| 6876 | emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6877 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6878 | } |
| 6879 | |
| 6880 | /* restore control word and proceed */ |
| 6881 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6882 | return dst; |
| 6883 | } |
| 6884 | |
| 6885 | |
| 6886 | /*------------------------------------------------- |
| 6887 | op_ftoi8c - process a FTOI8C opcode |
| 6888 | -------------------------------------------------*/ |
| 6889 | |
| 6890 | static x86code *op_ftoi8c(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6891 | { |
| 6892 | drcuml_parameter dstp, srcp; |
| 6893 | |
| 6894 | /* validate instruction */ |
| 6895 | assert(inst->size == 4 || inst->size == 8); |
| 6896 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6897 | |
| 6898 | /* normalize parameters */ |
| 6899 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MR, &srcp, PTYPE_MF); |
| 6900 | |
| 6901 | /* save and set the control word */ |
| 6902 | emit_fstcw_m16(&dst, MABS(&drcbe->fmodesave)); // fstcw [fmodesave] |
| 6903 | emit_fldcw_m16(&dst, MABS(&fp_control[DRCUML_FMOD_CEIL])); // fldcw fpcontrol[DRCUML_FMOD_CEIL] |
| 6904 | |
| 6905 | /* general case */ |
| 6906 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 6907 | if (dstp.type == DRCUML_PTYPE_MEMORY) |
| 6908 | emit_fistp_m64(&dst, MABS(dstp.value)); // fistp [dstp] |
| 6909 | else if (dstp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6910 | { |
| 6911 | emit_fistp_m64(&dst, MABS(drcbe->reglo[dstp.value])); // fistp reglo[dstp] |
| 6912 | emit_mov_r32_m32(&dst, dstp.value, MABS(drcbe->reglo[dstp.value])); // mov dstp,reglo[dstp] |
| 6913 | } |
| 6914 | |
| 6915 | /* restore control word and proceed */ |
| 6916 | emit_fldcw_m16(&dst, MABS(&drcbe->fmodesave)); // fldcw [fmodesave] |
| 6917 | return dst; |
| 6918 | } |
| 6919 | |
| 6920 | |
| 6921 | /*------------------------------------------------- |
| 6922 | op_ffrfs - process a FFRFS opcode |
| 6923 | -------------------------------------------------*/ |
| 6924 | |
| 6925 | static x86code *op_ffrfs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6926 | { |
| 6927 | drcuml_parameter dstp, srcp; |
| 6928 | |
| 6929 | /* validate instruction */ |
| 6930 | assert(inst->size == 8); |
| 6931 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6932 | |
| 6933 | /* normalize parameters */ |
| 6934 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 6935 | |
| 6936 | /* general case */ |
| 6937 | emit_fld_m32(&dst, MABS(srcp.value)); // fld [srcp] |
| 6938 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 6939 | |
| 6940 | return dst; |
| 6941 | } |
| 6942 | |
| 6943 | |
| 6944 | /*------------------------------------------------- |
| 6945 | op_ffrfd - process a FFRFD opcode |
| 6946 | -------------------------------------------------*/ |
| 6947 | |
| 6948 | static x86code *op_ffrfd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6949 | { |
| 6950 | drcuml_parameter dstp, srcp; |
| 6951 | |
| 6952 | /* validate instruction */ |
| 6953 | assert(inst->size == 4); |
| 6954 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6955 | |
| 6956 | /* normalize parameters */ |
| 6957 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 6958 | |
| 6959 | /* general case */ |
| 6960 | emit_fld_m64(&dst, MABS(srcp.value)); // fld [srcp] |
| 6961 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 6962 | |
| 6963 | return dst; |
| 6964 | } |
| 6965 | |
| 6966 | |
| 6967 | /*------------------------------------------------- |
| 6968 | op_ffri4 - process a FFRI4 opcode |
| 6969 | -------------------------------------------------*/ |
| 6970 | |
| 6971 | static x86code *op_ffri4(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 6972 | { |
| 6973 | drcuml_parameter dstp, srcp; |
| 6974 | |
| 6975 | /* validate instruction */ |
| 6976 | assert(inst->size == 4 || inst->size == 8); |
| 6977 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 6978 | |
| 6979 | /* normalize parameters */ |
| 6980 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MRI); |
| 6981 | |
| 6982 | /* general case */ |
| 6983 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 6984 | { |
| 6985 | emit_mov_m32_imm(&dst, MABS(&drcbe->fptemp), srcp.value); // mov [fptemp],srcp |
| 6986 | emit_fild_m32(&dst, MABS(&drcbe->fptemp)); // fild [fptemp] |
| 6987 | } |
| 6988 | else if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 6989 | emit_fild_m32(&dst, MABS(srcp.value)); // fild [srcp] |
| 6990 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 6991 | { |
| 6992 | emit_mov_m32_r32(&dst, MABS(drcbe->reglo[srcp.value]), srcp.value); // mov reglo[srcp],srcp |
| 6993 | emit_fild_m32(&dst, MABS(drcbe->reglo[srcp.value])); // fild reglo[srcp] |
| 6994 | } |
| 6995 | emit_fstp_p(&dst, inst->size, &dstp); // fstp [dstp] |
| 6996 | return dst; |
| 6997 | } |
| 6998 | |
| 6999 | |
| 7000 | /*------------------------------------------------- |
| 7001 | op_ffri8 - process a FFRI8 opcode |
| 7002 | -------------------------------------------------*/ |
| 7003 | |
| 7004 | static x86code *op_ffri8(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7005 | { |
| 7006 | drcuml_parameter dstp, srcp; |
| 7007 | |
| 7008 | /* validate instruction */ |
| 7009 | assert(inst->size == 4 || inst->size == 8); |
| 7010 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7011 | |
| 7012 | /* normalize parameters */ |
| 7013 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MRI); |
| 7014 | |
| 7015 | /* general case */ |
| 7016 | if (srcp.type == DRCUML_PTYPE_IMMEDIATE) |
| 7017 | { |
| 7018 | emit_mov_m32_imm(&dst, MABS(&drcbe->fptemp), srcp.value); // mov [fptemp],srcp |
| 7019 | emit_mov_m32_imm(&dst, MABS((UINT8 *)&drcbe->fptemp + 4), srcp.value); // mov [fptemp+4],srcp |
| 7020 | emit_fild_m64(&dst, MABS(&drcbe->fptemp)); // fild [fptemp] |
| 7021 | } |
| 7022 | else if (srcp.type == DRCUML_PTYPE_MEMORY) |
| 7023 | emit_fild_m64(&dst, MABS(srcp.value)); // fild [srcp] |
| 7024 | else if (srcp.type == DRCUML_PTYPE_INT_REGISTER) |
| 7025 | { |
| 7026 | emit_mov_m32_r32(&dst, MABS(drcbe->reglo[srcp.value]), srcp.value); // mov reglo[srcp],srcp |
| 7027 | emit_fild_m64(&dst, MABS(drcbe->reglo[srcp.value])); // fild reglo[srcp] |
| 7028 | } |
| 7029 | emit_fstp_p(&dst, inst->size, &dstp); // fstp [dstp] |
| 7030 | return dst; |
| 7031 | } |
| 7032 | |
| 7033 | |
| 7034 | /*------------------------------------------------- |
| 7035 | op_fadd - process a FADD opcode |
| 7036 | -------------------------------------------------*/ |
| 7037 | |
| 7038 | static x86code *op_fadd(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7039 | { |
| 7040 | drcuml_parameter dstp, src1p, src2p; |
| 7041 | |
| 7042 | /* validate instruction */ |
| 7043 | assert(inst->size == 4 || inst->size == 8); |
| 7044 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7045 | |
| 7046 | /* normalize parameters */ |
| 7047 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7048 | |
| 7049 | /* general case */ |
| 7050 | emit_fld_p(&dst, inst->size, &src1p); // fld src1p |
| 7051 | emit_fld_p(&dst, inst->size, &src2p); // fld src2p |
| 7052 | emit_faddp(&dst); // faddp |
| 7053 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7054 | |
| 7055 | return dst; |
| 7056 | } |
| 7057 | |
| 7058 | |
| 7059 | /*------------------------------------------------- |
| 7060 | op_fsub - process a FSUB opcode |
| 7061 | -------------------------------------------------*/ |
| 7062 | |
| 7063 | static x86code *op_fsub(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7064 | { |
| 7065 | drcuml_parameter dstp, src1p, src2p; |
| 7066 | |
| 7067 | /* validate instruction */ |
| 7068 | assert(inst->size == 4 || inst->size == 8); |
| 7069 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7070 | |
| 7071 | /* normalize parameters */ |
| 7072 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7073 | |
| 7074 | /* general case */ |
| 7075 | emit_fld_p(&dst, inst->size, &src1p); // fld src1p |
| 7076 | emit_fld_p(&dst, inst->size, &src2p); // fld src2p |
| 7077 | emit_fsubp(&dst); // fsubp |
| 7078 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7079 | |
| 7080 | return dst; |
| 7081 | } |
| 7082 | |
| 7083 | |
| 7084 | /*------------------------------------------------- |
| 7085 | op_fcmp - process a FCMP opcode |
| 7086 | -------------------------------------------------*/ |
| 7087 | |
| 7088 | static x86code *op_fcmp(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7089 | { |
| 7090 | drcuml_parameter src1p, src2p; |
| 7091 | |
| 7092 | /* validate instruction */ |
| 7093 | assert(inst->size == 4 || inst->size == 8); |
| 7094 | assert((inst->condflags & ~(DRCUML_FLAG_C | DRCUML_FLAG_V | DRCUML_FLAG_Z | DRCUML_FLAG_S)) == 0); |
| 7095 | |
| 7096 | /* normalize parameters */ |
| 7097 | param_normalize_2(drcbe, inst, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7098 | |
| 7099 | /* general case */ |
| 7100 | emit_fld_p(&dst, inst->size, &src2p); // fld src2p |
| 7101 | emit_fld_p(&dst, inst->size, &src1p); // fld src1p |
| 7102 | emit_fcompp(&dst); // fcompp |
| 7103 | emit_fstsw_ax(&dst); // fnstsw ax |
| 7104 | emit_sahf(&dst); // sahf |
| 7105 | |
| 7106 | return dst; |
| 7107 | } |
| 7108 | |
| 7109 | |
| 7110 | /*------------------------------------------------- |
| 7111 | op_fmul - process a FMUL opcode |
| 7112 | -------------------------------------------------*/ |
| 7113 | |
| 7114 | static x86code *op_fmul(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7115 | { |
| 7116 | drcuml_parameter dstp, src1p, src2p; |
| 7117 | |
| 7118 | /* validate instruction */ |
| 7119 | assert(inst->size == 4 || inst->size == 8); |
| 7120 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7121 | |
| 7122 | /* normalize parameters */ |
| 7123 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7124 | |
| 7125 | /* general case */ |
| 7126 | emit_fld_p(&dst, inst->size, &src1p); // fld src1p |
| 7127 | emit_fld_p(&dst, inst->size, &src2p); // fld src2p |
| 7128 | emit_fmulp(&dst); // fmulp |
| 7129 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7130 | |
| 7131 | return dst; |
| 7132 | } |
| 7133 | |
| 7134 | |
| 7135 | /*------------------------------------------------- |
| 7136 | op_fdiv - process a FDIV opcode |
| 7137 | -------------------------------------------------*/ |
| 7138 | |
| 7139 | static x86code *op_fdiv(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7140 | { |
| 7141 | drcuml_parameter dstp, src1p, src2p; |
| 7142 | |
| 7143 | /* validate instruction */ |
| 7144 | assert(inst->size == 4 || inst->size == 8); |
| 7145 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7146 | |
| 7147 | /* normalize parameters */ |
| 7148 | param_normalize_3(drcbe, inst, &dstp, PTYPE_MF, &src1p, PTYPE_MF, &src2p, PTYPE_MF); |
| 7149 | |
| 7150 | /* general case */ |
| 7151 | emit_fld_p(&dst, inst->size, &src1p); // fld src1p |
| 7152 | emit_fld_p(&dst, inst->size, &src2p); // fld src2p |
| 7153 | emit_fdivp(&dst); // fdivp |
| 7154 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7155 | |
| 7156 | return dst; |
| 7157 | } |
| 7158 | |
| 7159 | |
| 7160 | /*------------------------------------------------- |
| 7161 | op_fneg - process a FNEG opcode |
| 7162 | -------------------------------------------------*/ |
| 7163 | |
| 7164 | static x86code *op_fneg(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7165 | { |
| 7166 | drcuml_parameter dstp, srcp; |
| 7167 | |
| 7168 | /* validate instruction */ |
| 7169 | assert(inst->size == 4 || inst->size == 8); |
| 7170 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7171 | |
| 7172 | /* normalize parameters */ |
| 7173 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7174 | |
| 7175 | /* general case */ |
| 7176 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 7177 | emit_fchs(&dst); // fchs |
| 7178 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7179 | |
| 7180 | return dst; |
| 7181 | } |
| 7182 | |
| 7183 | |
| 7184 | /*------------------------------------------------- |
| 7185 | op_fabs - process a FABS opcode |
| 7186 | -------------------------------------------------*/ |
| 7187 | |
| 7188 | static x86code *op_fabs(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7189 | { |
| 7190 | drcuml_parameter dstp, srcp; |
| 7191 | |
| 7192 | /* validate instruction */ |
| 7193 | assert(inst->size == 4 || inst->size == 8); |
| 7194 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7195 | |
| 7196 | /* normalize parameters */ |
| 7197 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7198 | |
| 7199 | /* general case */ |
| 7200 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 7201 | emit_fabs(&dst); // fabs |
| 7202 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7203 | |
| 7204 | return dst; |
| 7205 | } |
| 7206 | |
| 7207 | |
| 7208 | /*------------------------------------------------- |
| 7209 | op_fsqrt - process a FSQRT opcode |
| 7210 | -------------------------------------------------*/ |
| 7211 | |
| 7212 | static x86code *op_fsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7213 | { |
| 7214 | drcuml_parameter dstp, srcp; |
| 7215 | |
| 7216 | /* validate instruction */ |
| 7217 | assert(inst->size == 4 || inst->size == 8); |
| 7218 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7219 | |
| 7220 | /* normalize parameters */ |
| 7221 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7222 | |
| 7223 | /* general case */ |
| 7224 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 7225 | emit_fsqrt(&dst); // fsqrt |
| 7226 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7227 | |
| 7228 | return dst; |
| 7229 | } |
| 7230 | |
| 7231 | |
| 7232 | /*------------------------------------------------- |
| 7233 | op_frecip - process a FRECIP opcode |
| 7234 | -------------------------------------------------*/ |
| 7235 | |
| 7236 | static x86code *op_frecip(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7237 | { |
| 7238 | drcuml_parameter dstp, srcp; |
| 7239 | |
| 7240 | /* validate instruction */ |
| 7241 | assert(inst->size == 4 || inst->size == 8); |
| 7242 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7243 | |
| 7244 | /* normalize parameters */ |
| 7245 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7246 | |
| 7247 | /* general case */ |
| 7248 | emit_fld1(&dst); // fld1 |
| 7249 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 7250 | emit_fdivp(&dst); // fdivp |
| 7251 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7252 | |
| 7253 | return dst; |
| 7254 | } |
| 7255 | |
| 7256 | |
| 7257 | /*------------------------------------------------- |
| 7258 | op_frsqrt - process a FRSQRT opcode |
| 7259 | -------------------------------------------------*/ |
| 7260 | |
| 7261 | static x86code *op_frsqrt(drcbe_state *drcbe, x86code *dst, const drcuml_instruction *inst) |
| 7262 | { |
| 7263 | drcuml_parameter dstp, srcp; |
| 7264 | |
| 7265 | /* validate instruction */ |
| 7266 | assert(inst->size == 4 || inst->size == 8); |
| 7267 | assert(inst->condflags == DRCUML_COND_ALWAYS); |
| 7268 | |
| 7269 | /* normalize parameters */ |
| 7270 | param_normalize_2(drcbe, inst, &dstp, PTYPE_MF, &srcp, PTYPE_MF); |
| 7271 | |
| 7272 | /* general case */ |
| 7273 | emit_fld1(&dst); // fld1 |
| 7274 | emit_fld_p(&dst, inst->size, &srcp); // fld srcp |
| 7275 | emit_fsqrt(&dst); // fsqrt |
| 7276 | emit_fdivp(&dst); // fdivp |
| 7277 | emit_fstp_p(&dst, inst->size, &dstp); // fstp dstp |
| 7278 | |
| 7279 | return dst; |
| 7280 | } |
| 7281 | |
| 7282 | |
| 7283 | |
| 7284 | /*************************************************************************** |
| 7285 | MISCELLAENOUS FUNCTIONS |
| 7286 | ***************************************************************************/ |
| 7287 | |
| 7288 | /*------------------------------------------------- |
| 7289 | dmulu - perform a double-wide unsigned multiply |
| 7290 | -------------------------------------------------*/ |
| 7291 | |
| 7292 | static void dmulu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2) |
| 7293 | { |
| 7294 | UINT64 lo, hi, prevlo; |
| 7295 | UINT64 a, b, temp; |
| 7296 | |
| 7297 | /* shortcut if we don't care about the high bits */ |
| 7298 | if (dstlo == dsthi) |
| 7299 | { |
| 7300 | *dstlo = src1 * src2; |
| 7301 | return; |
| 7302 | } |
| 7303 | |
| 7304 | /* fetch source values */ |
| 7305 | a = src1; |
| 7306 | b = src2; |
| 7307 | if (a == 0 || b == 0) |
| 7308 | { |
| 7309 | *dsthi = *dstlo = 0; |
| 7310 | return; |
| 7311 | } |
| 7312 | |
| 7313 | /* compute high and low parts first */ |
| 7314 | lo = (UINT64)(UINT32)(a >> 0) * (UINT64)(UINT32)(b >> 0); |
| 7315 | hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32); |
| 7316 | |
| 7317 | /* compute middle parts */ |
| 7318 | prevlo = lo; |
| 7319 | temp = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 0); |
| 7320 | lo += temp << 32; |
| 7321 | hi += (temp >> 32) + (lo < prevlo); |
| 7322 | |
| 7323 | prevlo = lo; |
| 7324 | temp = (UINT64)(UINT32)(a >> 0) * (UINT64)(UINT32)(b >> 32); |
| 7325 | lo += temp << 32; |
| 7326 | hi += (temp >> 32) + (lo < prevlo); |
| 7327 | |
| 7328 | /* store the results */ |
| 7329 | *dsthi = hi; |
| 7330 | *dstlo = lo; |
| 7331 | } |
| 7332 | |
| 7333 | |
| 7334 | /*------------------------------------------------- |
| 7335 | dmuls - perform a double-wide signed multiply |
| 7336 | -------------------------------------------------*/ |
| 7337 | |
| 7338 | static void dmuls(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2) |
| 7339 | { |
| 7340 | UINT64 lo, hi, prevlo; |
| 7341 | UINT64 a, b, temp; |
| 7342 | |
| 7343 | /* shortcut if we don't care about the high bits */ |
| 7344 | if (dstlo == dsthi) |
| 7345 | { |
| 7346 | *dstlo = src1 * src2; |
| 7347 | return; |
| 7348 | } |
| 7349 | |
| 7350 | /* fetch absolute source values */ |
| 7351 | a = src1; if ((INT64)a < 0) a = -a; |
| 7352 | b = src2; if ((INT64)b < 0) b = -b; |
| 7353 | if (a == 0 || b == 0) |
| 7354 | { |
| 7355 | *dsthi = *dstlo = 0; |
| 7356 | return; |
| 7357 | } |
| 7358 | |
| 7359 | /* compute high and low parts first */ |
| 7360 | lo = (UINT64)(UINT32)(a >> 0) * (UINT64)(UINT32)(b >> 0); |
| 7361 | hi = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 32); |
| 7362 | |
| 7363 | /* compute middle parts */ |
| 7364 | prevlo = lo; |
| 7365 | temp = (UINT64)(UINT32)(a >> 32) * (UINT64)(UINT32)(b >> 0); |
| 7366 | lo += temp << 32; |
| 7367 | hi += (temp >> 32) + (lo < prevlo); |
| 7368 | |
| 7369 | prevlo = lo; |
| 7370 | temp = (UINT64)(UINT32)(a >> 0) * (UINT64)(UINT32)(b >> 32); |
| 7371 | lo += temp << 32; |
| 7372 | hi += (temp >> 32) + (lo < prevlo); |
| 7373 | |
| 7374 | /* adjust for signage */ |
| 7375 | if ((INT64)(src1 ^ src2) < 0) |
| 7376 | { |
| 7377 | hi = ~hi + (lo == 0); |
| 7378 | lo = ~lo + 1; |
| 7379 | } |
| 7380 | |
| 7381 | /* store the results */ |
| 7382 | *dsthi = hi; |
| 7383 | *dstlo = lo; |
| 7384 | } |
| 7385 | |
| 7386 | |
| 7387 | /*------------------------------------------------- |
| 7388 | ddivu - perform a double-wide unsigned divide |
| 7389 | -------------------------------------------------*/ |
| 7390 | |
| 7391 | static void ddivu(UINT64 *dstlo, UINT64 *dsthi, UINT64 src1, UINT64 src2) |
| 7392 | { |
| 7393 | /* do nothing if src2 == 0 */ |
| 7394 | if (src2 == 0) |
| 7395 | return; |
| 7396 | |
| 7397 | /* shortcut if no remainder */ |
| 7398 | *dstlo = src1 / src2; |
| 7399 | if (dstlo != dsthi) |
| 7400 | *dsthi = src1 % src2; |
| 7401 | } |
| 7402 | |
| 7403 | |
| 7404 | /*------------------------------------------------- |
| 7405 | ddivs - perform a double-wide signed divide |
| 7406 | -------------------------------------------------*/ |
| 7407 | |
| 7408 | static void ddivs(UINT64 *dstlo, UINT64 *dsthi, INT64 src1, INT64 src2) |
| 7409 | { |
| 7410 | /* do nothing if src2 == 0 */ |
| 7411 | if (src2 == 0) |
| 7412 | return; |
| 7413 | |
| 7414 | /* shortcut if no remainder */ |
| 7415 | *dstlo = src1 / src2; |
| 7416 | if (dstlo != dsthi) |
| 7417 | *dsthi = src1 % src2; |
| 7418 | } |