Debian Codegraph @

src/cpu/core_dynrec/decoder_basic.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: decoder_basic.h,v 1.16 2009-10-08 20:01:31 c2woody Exp $ */
20
21
22 /*
23 This file provides some definitions and basic level functions
24 that use code generating functions from risc_?.h
25 Important is the function call generation including parameter
26 loading, the effective address calculation and the memory
27 access functions.
28 */
29
30
31 // instructions that use one operand
32 enum SingleOps {
33 SOP_INC,SOP_DEC,
34 SOP_NOT,SOP_NEG
35 };
36
37 // instructions that use two operand
38 enum DualOps {
39 DOP_ADD,DOP_ADC,
40 DOP_SUB,DOP_SBB,
41 DOP_CMP,DOP_XOR,
42 DOP_AND,DOP_OR,
43 DOP_TEST,
44 DOP_MOV,
45 DOP_XCHG
46 };
47
48 // shift and rotate functions
49 enum ShiftOps {
50 SHIFT_ROL,SHIFT_ROR,
51 SHIFT_RCL,SHIFT_RCR,
52 SHIFT_SHL,SHIFT_SHR,
53 SHIFT_SAL,SHIFT_SAR
54 };
55
56 // branch conditions
57 enum BranchTypes {
58 BR_O,BR_NO,BR_B,BR_NB,
59 BR_Z,BR_NZ,BR_BE,BR_NBE,
60 BR_S,BR_NS,BR_P,BR_NP,
61 BR_L,BR_NL,BR_LE,BR_NLE
62 };
63
64 // string instructions
65 enum StringOps {
66 STR_OUTSB=0,STR_OUTSW,STR_OUTSD,
67 STR_INSB=4,STR_INSW,STR_INSD,
68 STR_MOVSB=8,STR_MOVSW,STR_MOVSD,
69 STR_LODSB=12,STR_LODSW,STR_LODSD,
70 STR_STOSB=16,STR_STOSW,STR_STOSD,
71 STR_SCASB=20,STR_SCASW,STR_SCASD,
72 STR_CMPSB=24,STR_CMPSW,STR_CMPSD
73 };
74
75 // repeat prefix type (for string operations)
76 enum REP_Type {
77 REP_NONE=0,REP_NZ,REP_Z
78 };
79
80 // loop type
81 enum LoopTypes {
82 LOOP_NONE,LOOP_NE,LOOP_E,LOOP_JCXZ
83 };
84
85 // rotate operand type
86 enum grp2_types {
87 grp2_1,grp2_imm,grp2_cl
88 };
89
90 // opcode mapping for group1 instructions
91 static DualOps grp1_table[8]={
92 DOP_ADD,DOP_OR,DOP_ADC,DOP_SBB,DOP_AND,DOP_SUB,DOP_XOR,DOP_CMP
93 };
94
95
96 // decoding information used during translation of a code block
97 static struct DynDecode {
98 PhysPt code; // pointer to next byte in the instruction stream
99 PhysPt code_start; // pointer to the start of the current code block
100 PhysPt op_start; // pointer to the start of the current instruction
101 bool big_op; // operand modifier
102 bool big_addr; // address modifier
103 REP_Type rep; // current repeat prefix
104 Bitu cycles; // number cycles used by currently translated code
105 bool seg_prefix_used; // segment overridden
106 Bit8u seg_prefix; // segment prefix (if seg_prefix_used==true)
107
108 // block that contains the first instruction translated
109 CacheBlockDynRec * block;
110 // block that contains the current byte of the instruction stream
111 CacheBlockDynRec * active_block;
112
113 // the active page (containing the current byte of the instruction stream)
114 struct {
115 CodePageHandlerDynRec * code;
116 Bitu index; // index to the current byte of the instruction stream
117 Bit8u * wmap; // write map that indicates code presence for every byte of this page
118 Bit8u * invmap; // invalidation map
119 Bitu first; // page number
120 } page;
121
122 // modrm state of the current instruction (if used)
123 struct {
124 Bitu val;
125 Bitu mod;
126 Bitu rm;
127 Bitu reg;
128 } modrm;
129 } decode;
130
131
132 static bool MakeCodePage(Bitu lin_addr,CodePageHandlerDynRec * &cph) {
133 Bit8u rdval;
134 //Ensure page contains memory:
135 if (GCC_UNLIKELY(mem_readb_checked(lin_addr,&rdval))) return true;
136
137 PageHandler * handler=get_tlb_readhandler(lin_addr);
138 if (handler->flags & PFLAG_HASCODE) {
139 // this is a codepage handler, and the one that we're looking for
140 cph=(CodePageHandlerDynRec *)handler;
141 return false;
142 }
143 if (handler->flags & PFLAG_NOCODE) {
144 if (PAGING_ForcePageInit(lin_addr)) {
145 handler=get_tlb_readhandler(lin_addr);
146 if (handler->flags & PFLAG_HASCODE) {
147 cph=(CodePageHandlerDynRec *)handler;
148 return false;
149 }
150 }
151 if (handler->flags & PFLAG_NOCODE) {
152 LOG_MSG("DYNREC:Can't run code in this page");
153 cph=0;
154 return false;
155 }
156 }
157 Bitu lin_page=lin_addr>>12;
158 Bitu phys_page=lin_page;
159 // find the physical page that the linear page is mapped to
160 if (!PAGING_MakePhysPage(phys_page)) {
161 LOG_MSG("DYNREC:Can't find physpage");
162 cph=0;
163 return false;
164 }
165 // find a free CodePage
166 if (!cache.free_pages) {
167 if (cache.used_pages!=decode.page.code) cache.used_pages->ClearRelease();
168 else {
169 // try another page to avoid clearing our source-crosspage
170 if ((cache.used_pages->next) && (cache.used_pages->next!=decode.page.code))
171 cache.used_pages->next->ClearRelease();
172 else {
173 LOG_MSG("DYNREC:Invalid cache links");
174 cache.used_pages->ClearRelease();
175 }
176 }
177 }
178 CodePageHandlerDynRec * cpagehandler=cache.free_pages;
179 cache.free_pages=cache.free_pages->next;
180
181 // adjust previous and next page pointer
182 cpagehandler->prev=cache.last_page;
183 cpagehandler->next=0;
184 if (cache.last_page) cache.last_page->next=cpagehandler;
185 cache.last_page=cpagehandler;
186 if (!cache.used_pages) cache.used_pages=cpagehandler;
187
188 // initialize the code page handler and add the handler to the memory page
189 cpagehandler->SetupAt(phys_page,handler);
190 MEM_SetPageHandler(phys_page,1,cpagehandler);
191 PAGING_UnlinkPages(lin_page,1);
192 cph=cpagehandler;
193 return false;
194 }
195
196 static void decode_advancepage(void) {
197 // Advance to the next page
198 decode.active_block->page.end=4095;
199 // trigger possible page fault here
200 decode.page.first++;
201 Bitu faddr=decode.page.first << 12;
202 mem_readb(faddr);
203 MakeCodePage(faddr,decode.page.code);
204 CacheBlockDynRec * newblock=cache_getblock();
205 decode.active_block->crossblock=newblock;
206 newblock->crossblock=decode.active_block;
207 decode.active_block=newblock;
208 decode.active_block->page.start=0;
209 decode.page.code->AddCrossBlock(decode.active_block);
210 decode.page.wmap=decode.page.code->write_map;
211 decode.page.invmap=decode.page.code->invalidation_map;
212 decode.page.index=0;
213 }
214
215 // fetch the next byte of the instruction stream
216 static Bit8u decode_fetchb(void) {
217 if (GCC_UNLIKELY(decode.page.index>=4096)) {
218 decode_advancepage();
219 }
220 decode.page.wmap[decode.page.index]+=0x01;
221 decode.page.index++;
222 decode.code+=1;
223 return mem_readb(decode.code-1);
224 }
225 // fetch the next word of the instruction stream
226 static Bit16u decode_fetchw(void) {
227 if (GCC_UNLIKELY(decode.page.index>=4095)) {
228 Bit16u val=decode_fetchb();
229 val|=decode_fetchb() << 8;
230 return val;
231 }
232 *(Bit16u *)&decode.page.wmap[decode.page.index]+=0x0101;
233 decode.code+=2;decode.page.index+=2;
234 return mem_readw(decode.code-2);
235 }
236 // fetch the next dword of the instruction stream
237 static Bit32u decode_fetchd(void) {
238 if (GCC_UNLIKELY(decode.page.index>=4093)) {
239 Bit32u val=decode_fetchb();
240 val|=decode_fetchb() << 8;
241 val|=decode_fetchb() << 16;
242 val|=decode_fetchb() << 24;
243 return val;
244 /* Advance to the next page */
245 }
246 *(Bit32u *)&decode.page.wmap[decode.page.index]+=0x01010101;
247 decode.code+=4;decode.page.index+=4;
248 return mem_readd(decode.code-4);
249 }
250
251 #define START_WMMEM 64
252
253 // adjust writemap mask to care for map holes due to special
254 // codefetch functions
255 static void INLINE decode_increase_wmapmask(Bitu size) {
256 Bitu mapidx;
257 CacheBlockDynRec* activecb=decode.active_block;
258 if (GCC_UNLIKELY(!activecb->cache.wmapmask)) {
259 // no mask memory yet allocated, start with a small buffer
260 activecb->cache.wmapmask=(Bit8u*)malloc(START_WMMEM);
261 memset(activecb->cache.wmapmask,0,START_WMMEM);
262 activecb->cache.maskstart=decode.page.index; // start of buffer is current code position
263 activecb->cache.masklen=START_WMMEM;
264 mapidx=0;
265 } else {
266 mapidx=decode.page.index-activecb->cache.maskstart;
267 if (GCC_UNLIKELY(mapidx+size>=activecb->cache.masklen)) {
268 // mask buffer too small, increase
269 Bitu newmasklen=activecb->cache.masklen*4;
270 if (newmasklen<mapidx+size) newmasklen=((mapidx+size)&~3)*2;
271 Bit8u* tempmem=(Bit8u*)malloc(newmasklen);
272 memset(tempmem,0,newmasklen);
273 memcpy(tempmem,activecb->cache.wmapmask,activecb->cache.masklen);
274 free(activecb->cache.wmapmask);
275 activecb->cache.wmapmask=tempmem;
276 activecb->cache.masklen=newmasklen;
277 }
278 }
279 // update mask entries
280 switch (size) {
281 case 1 : activecb->cache.wmapmask[mapidx]+=0x01; break;
282 case 2 : (*(Bit16u*)&activecb->cache.wmapmask[mapidx])+=0x0101; break;
283 case 4 : (*(Bit32u*)&activecb->cache.wmapmask[mapidx])+=0x01010101; break;
284 }
285 }
286
287 // fetch a byte, val points to the code location if possible,
288 // otherwise val contains the current value read from the position
289 static bool decode_fetchb_imm(Bitu & val) {
290 if (GCC_UNLIKELY(decode.page.index>=4096)) {
291 decode_advancepage();
292 }
293 // see if position is directly accessible
294 if (decode.page.invmap != NULL) {
295 if (decode.page.invmap[decode.page.index] == 0) {
296 // position not yet modified
297 val=(Bit32u)decode_fetchb();
298 return false;
299 }
300
301 HostPt tlb_addr=get_tlb_read(decode.code);
302 if (tlb_addr) {
303 val=(Bitu)(tlb_addr+decode.code);
304 decode_increase_wmapmask(1);
305 decode.code++;
306 decode.page.index++;
307 return true;
308 }
309 }
310 // first time decoding or not directly accessible, just fetch the value
311 val=(Bit32u)decode_fetchb();
312 return false;
313 }
314
315 // fetch a word, val points to the code location if possible,
316 // otherwise val contains the current value read from the position
317 static bool decode_fetchw_imm(Bitu & val) {
318 if (decode.page.index<4095) {
319 if (decode.page.invmap != NULL) {
320 if ((decode.page.invmap[decode.page.index] == 0) &&
321 (decode.page.invmap[decode.page.index + 1] == 0)) {
322 // position not yet modified
323 val=decode_fetchw();
324 return false;
325 }
326
327 HostPt tlb_addr=get_tlb_read(decode.code);
328 // see if position is directly accessible
329 if (tlb_addr) {
330 val=(Bitu)(tlb_addr+decode.code);
331 decode_increase_wmapmask(2);
332 decode.code+=2;
333 decode.page.index+=2;
334 return true;
335 }
336 }
337 }
338 // first time decoding or not directly accessible, just fetch the value
339 val=decode_fetchw();
340 return false;
341 }
342
343 // fetch a dword, val points to the code location if possible,
344 // otherwise val contains the current value read from the position
345 static bool decode_fetchd_imm(Bitu & val) {
346 if (decode.page.index<4093) {
347 if (decode.page.invmap != NULL) {
348 if ((decode.page.invmap[decode.page.index] == 0) &&
349 (decode.page.invmap[decode.page.index + 1] == 0) &&
350 (decode.page.invmap[decode.page.index + 2] == 0) &&
351 (decode.page.invmap[decode.page.index + 3] == 0)) {
352 // position not yet modified
353 val=decode_fetchd();
354 return false;
355 }
356
357 HostPt tlb_addr=get_tlb_read(decode.code);
358 // see if position is directly accessible
359 if (tlb_addr) {
360 val=(Bitu)(tlb_addr+decode.code);
361 decode_increase_wmapmask(4);
362 decode.code+=4;
363 decode.page.index+=4;
364 return true;
365 }
366 }
367 }
368 // first time decoding or not directly accessible, just fetch the value
369 val=decode_fetchd();
370 return false;
371 }
372
373
374 // modrm decoding helper
375 static void INLINE dyn_get_modrm(void) {
376 decode.modrm.val=decode_fetchb();
377 decode.modrm.mod=(decode.modrm.val >> 6) & 3;
378 decode.modrm.reg=(decode.modrm.val >> 3) & 7;
379 decode.modrm.rm=(decode.modrm.val & 7);
380 }
381
382
383 #ifdef DRC_USE_SEGS_ADDR
384
385 #define MOV_SEG_VAL_TO_HOST_REG(host_reg, seg_index) gen_mov_seg16_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_SEG_VAL(seg_index)) - (DRC_PTR_SIZE_IM)(&Segs))
386
387 #define MOV_SEG_PHYS_TO_HOST_REG(host_reg, seg_index) gen_mov_seg32_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_SEG_PHYS(seg_index)) - (DRC_PTR_SIZE_IM)(&Segs))
388 #define ADD_SEG_PHYS_TO_HOST_REG(host_reg, seg_index) gen_add_seg32_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_SEG_PHYS(seg_index)) - (DRC_PTR_SIZE_IM)(&Segs))
389
390 #else
391
392 #define MOV_SEG_VAL_TO_HOST_REG(host_reg, seg_index) gen_mov_word_to_reg(host_reg,DRCD_SEG_VAL(seg_index),false)
393
394 #define MOV_SEG_PHYS_TO_HOST_REG(host_reg, seg_index) gen_mov_word_to_reg(host_reg,DRCD_SEG_PHYS(seg_index),true)
395 #define ADD_SEG_PHYS_TO_HOST_REG(host_reg, seg_index) gen_add(host_reg,DRCD_SEG_PHYS(seg_index))
396
397 #endif
398
399
400 #ifdef DRC_USE_REGS_ADDR
401
402 #define MOV_REG_VAL_TO_HOST_REG(host_reg, reg_index) gen_mov_regval32_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_VAL(reg_index)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
403 #define ADD_REG_VAL_TO_HOST_REG(host_reg, reg_index) gen_add_regval32_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_VAL(reg_index)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
404
405 #define MOV_REG_WORD16_TO_HOST_REG(host_reg, reg_index) gen_mov_regval16_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,false)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
406 #define MOV_REG_WORD32_TO_HOST_REG(host_reg, reg_index) gen_mov_regval32_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,true)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
407 #define MOV_REG_WORD_TO_HOST_REG(host_reg, reg_index, dword) gen_mov_regword_to_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,dword)) - (DRC_PTR_SIZE_IM)(&cpu_regs), dword)
408
409 #define MOV_REG_WORD16_FROM_HOST_REG(host_reg, reg_index) gen_mov_regval16_from_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,false)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
410 #define MOV_REG_WORD32_FROM_HOST_REG(host_reg, reg_index) gen_mov_regval32_from_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,true)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
411 #define MOV_REG_WORD_FROM_HOST_REG(host_reg, reg_index, dword) gen_mov_regword_from_reg(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_WORD(reg_index,dword)) - (DRC_PTR_SIZE_IM)(&cpu_regs), dword)
412
413 #define MOV_REG_BYTE_TO_HOST_REG_LOW(host_reg, reg_index, high_byte) gen_mov_regbyte_to_reg_low(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_BYTE(reg_index,high_byte)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
414 #define MOV_REG_BYTE_TO_HOST_REG_LOW_CANUSEWORD(host_reg, reg_index, high_byte) gen_mov_regbyte_to_reg_low_canuseword(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_BYTE(reg_index,high_byte)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
415 #define MOV_REG_BYTE_FROM_HOST_REG_LOW(host_reg, reg_index, high_byte) gen_mov_regbyte_from_reg_low(host_reg,(DRC_PTR_SIZE_IM)(DRCD_REG_BYTE(reg_index,high_byte)) - (DRC_PTR_SIZE_IM)(&cpu_regs))
416
417 #else
418
419 #define MOV_REG_VAL_TO_HOST_REG(host_reg, reg_index) gen_mov_word_to_reg(host_reg,DRCD_REG_VAL(reg_index),true)
420 #define ADD_REG_VAL_TO_HOST_REG(host_reg, reg_index) gen_add(host_reg,DRCD_REG_VAL(reg_index))
421
422 #define MOV_REG_WORD16_TO_HOST_REG(host_reg, reg_index) gen_mov_word_to_reg(host_reg,DRCD_REG_WORD(reg_index,false),false)
423 #define MOV_REG_WORD32_TO_HOST_REG(host_reg, reg_index) gen_mov_word_to_reg(host_reg,DRCD_REG_WORD(reg_index,true),true)
424 #define MOV_REG_WORD_TO_HOST_REG(host_reg, reg_index, dword) gen_mov_word_to_reg(host_reg,DRCD_REG_WORD(reg_index,dword),dword)
425
426 #define MOV_REG_WORD16_FROM_HOST_REG(host_reg, reg_index) gen_mov_word_from_reg(host_reg,DRCD_REG_WORD(reg_index,false),false)
427 #define MOV_REG_WORD32_FROM_HOST_REG(host_reg, reg_index) gen_mov_word_from_reg(host_reg,DRCD_REG_WORD(reg_index,true),true)
428 #define MOV_REG_WORD_FROM_HOST_REG(host_reg, reg_index, dword) gen_mov_word_from_reg(host_reg,DRCD_REG_WORD(reg_index,dword),dword)
429
430 #define MOV_REG_BYTE_TO_HOST_REG_LOW(host_reg, reg_index, high_byte) gen_mov_byte_to_reg_low(host_reg,DRCD_REG_BYTE(reg_index,high_byte))
431 #define MOV_REG_BYTE_TO_HOST_REG_LOW_CANUSEWORD(host_reg, reg_index, high_byte) gen_mov_byte_to_reg_low_canuseword(host_reg,DRCD_REG_BYTE(reg_index,high_byte))
432 #define MOV_REG_BYTE_FROM_HOST_REG_LOW(host_reg, reg_index, high_byte) gen_mov_byte_from_reg_low(host_reg,DRCD_REG_BYTE(reg_index,high_byte))
433
434 #endif
435
436
437 #define DYN_LEA_MEM_MEM(ea_reg, op1, op2, scale, imm) dyn_lea_mem_mem(ea_reg,op1,op2,scale,imm)
438
439 #if defined(DRC_USE_REGS_ADDR) && defined(DRC_USE_SEGS_ADDR)
440
441 #define DYN_LEA_SEG_PHYS_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_segphys_regval(ea_reg,op1_index,op2_index,scale,imm)
442 #define DYN_LEA_REG_VAL_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_regval_regval(ea_reg,op1_index,op2_index,scale,imm)
443 #define DYN_LEA_MEM_REG_VAL(ea_reg, op1, op2_index, scale, imm) dyn_lea_mem_regval(ea_reg,op1,op2_index,scale,imm)
444
445 #elif defined(DRC_USE_REGS_ADDR)
446
447 #define DYN_LEA_SEG_PHYS_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_mem_regval(ea_reg,DRCD_SEG_PHYS(op1_index),op2_index,scale,imm)
448 #define DYN_LEA_REG_VAL_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_regval_regval(ea_reg,op1_index,op2_index,scale,imm)
449 #define DYN_LEA_MEM_REG_VAL(ea_reg, op1, op2_index, scale, imm) dyn_lea_mem_regval(ea_reg,op1,op2_index,scale,imm)
450
451 #elif defined(DRC_USE_SEGS_ADDR)
452
453 #define DYN_LEA_SEG_PHYS_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_segphys_mem(ea_reg,op1_index,DRCD_REG_VAL(op2_index),scale,imm)
454 #define DYN_LEA_REG_VAL_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_mem_mem(ea_reg,DRCD_REG_VAL(op1_index),DRCD_REG_VAL(op2_index),scale,imm)
455 #define DYN_LEA_MEM_REG_VAL(ea_reg, op1, op2_index, scale, imm) dyn_lea_mem_mem(ea_reg,op1,DRCD_REG_VAL(op2_index),scale,imm)
456
457 #else
458
459 #define DYN_LEA_SEG_PHYS_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_mem_mem(ea_reg,DRCD_SEG_PHYS(op1_index),DRCD_REG_VAL(op2_index),scale,imm)
460 #define DYN_LEA_REG_VAL_REG_VAL(ea_reg, op1_index, op2_index, scale, imm) dyn_lea_mem_mem(ea_reg,DRCD_REG_VAL(op1_index),DRCD_REG_VAL(op2_index),scale,imm)
461 #define DYN_LEA_MEM_REG_VAL(ea_reg, op1, op2_index, scale, imm) dyn_lea_mem_mem(ea_reg,op1,DRCD_REG_VAL(op2_index),scale,imm)
462
463 #endif
464
465
466
467 // adjust CPU_Cycles value
468 static void dyn_reduce_cycles(void) {
469 if (!decode.cycles) decode.cycles++;
470 gen_sub_direct_word(&CPU_Cycles,decode.cycles,true);
471 }
472
473
474 // set reg to the start of the next instruction
475 // set reg_eip to the start of the current instruction
476 static INLINE void dyn_set_eip_last_end(HostReg reg) {
477 gen_mov_word_to_reg(reg,&reg_eip,true);
478 gen_add_imm(reg,(Bit32u)(decode.code-decode.code_start));
479 gen_add_direct_word(&reg_eip,decode.op_start-decode.code_start,decode.big_op);
480 }
481
482 // set reg_eip to the start of the current instruction
483 static INLINE void dyn_set_eip_last(void) {
484 gen_add_direct_word(&reg_eip,decode.op_start-decode.code_start,cpu.code.big);
485 }
486
487 // set reg_eip to the start of the next instruction
488 static INLINE void dyn_set_eip_end(void) {
489 gen_add_direct_word(&reg_eip,decode.code-decode.code_start,cpu.code.big);
490 }
491
492 // set reg_eip to the start of the next instruction plus an offset (imm)
493 static INLINE void dyn_set_eip_end(HostReg reg,Bit32u imm=0) {
494 gen_mov_word_to_reg(reg,&reg_eip,true); //get_extend_word will mask off the upper bits
495 //gen_mov_word_to_reg(reg,&reg_eip,decode.big_op);
496 gen_add_imm(reg,(Bit32u)(decode.code-decode.code_start+imm));
497 if (!decode.big_op) gen_extend_word(false,reg);
498 }
499
500
501
502 // the following functions generate function calls
503 // parameters are loaded by generating code using gen_load_param_ which
504 // is architecture dependent
505 // R=host register; I=32bit immediate value; A=address value; m=memory
506
507 static DRC_PTR_SIZE_IM INLINE gen_call_function_R(void * func,Bitu op) {
508 gen_load_param_reg(op,0);
509 return gen_call_function_setup(func, 1);
510 }
511
512 static DRC_PTR_SIZE_IM INLINE gen_call_function_R3(void * func,Bitu op) {
513 gen_load_param_reg(op,2);
514 return gen_call_function_setup(func, 3, true);
515 }
516
517 static DRC_PTR_SIZE_IM INLINE gen_call_function_RI(void * func,Bitu op1,Bitu op2) {
518 gen_load_param_imm(op2,1);
519 gen_load_param_reg(op1,0);
520 return gen_call_function_setup(func, 2);
521 }
522
523 static DRC_PTR_SIZE_IM INLINE gen_call_function_RA(void * func,Bitu op1,DRC_PTR_SIZE_IM op2) {
524 gen_load_param_addr(op2,1);
525 gen_load_param_reg(op1,0);
526 return gen_call_function_setup(func, 2);
527 }
528
529 static DRC_PTR_SIZE_IM INLINE gen_call_function_RR(void * func,Bitu op1,Bitu op2) {
530 gen_load_param_reg(op2,1);
531 gen_load_param_reg(op1,0);
532 return gen_call_function_setup(func, 2);
533 }
534
535 static DRC_PTR_SIZE_IM INLINE gen_call_function_IR(void * func,Bitu op1,Bitu op2) {
536 gen_load_param_reg(op2,1);
537 gen_load_param_imm(op1,0);
538 return gen_call_function_setup(func, 2);
539 }
540
541 static DRC_PTR_SIZE_IM INLINE gen_call_function_I(void * func,Bitu op) {
542 gen_load_param_imm(op,0);
543 return gen_call_function_setup(func, 1);
544 }
545
546 static DRC_PTR_SIZE_IM INLINE gen_call_function_II(void * func,Bitu op1,Bitu op2) {
547 gen_load_param_imm(op2,1);
548 gen_load_param_imm(op1,0);
549 return gen_call_function_setup(func, 2);
550 }
551
552 static DRC_PTR_SIZE_IM INLINE gen_call_function_III(void * func,Bitu op1,Bitu op2,Bitu op3) {
553 gen_load_param_imm(op3,2);
554 gen_load_param_imm(op2,1);
555 gen_load_param_imm(op1,0);
556 return gen_call_function_setup(func, 3);
557 }
558
559 static DRC_PTR_SIZE_IM INLINE gen_call_function_IA(void * func,Bitu op1,DRC_PTR_SIZE_IM op2) {
560 gen_load_param_addr(op2,1);
561 gen_load_param_imm(op1,0);
562 return gen_call_function_setup(func, 2);
563 }
564
565 static DRC_PTR_SIZE_IM INLINE gen_call_function_IIR(void * func,Bitu op1,Bitu op2,Bitu op3) {
566 gen_load_param_reg(op3,2);
567 gen_load_param_imm(op2,1);
568 gen_load_param_imm(op1,0);
569 return gen_call_function_setup(func, 3);
570 }
571
572 static DRC_PTR_SIZE_IM INLINE gen_call_function_IIIR(void * func,Bitu op1,Bitu op2,Bitu op3,Bitu op4) {
573 gen_load_param_reg(op4,3);
574 gen_load_param_imm(op3,2);
575 gen_load_param_imm(op2,1);
576 gen_load_param_imm(op1,0);
577 return gen_call_function_setup(func, 4);
578 }
579
580 static DRC_PTR_SIZE_IM INLINE gen_call_function_IRRR(void * func,Bitu op1,Bitu op2,Bitu op3,Bitu op4) {
581 gen_load_param_reg(op4,3);
582 gen_load_param_reg(op3,2);
583 gen_load_param_reg(op2,1);
584 gen_load_param_imm(op1,0);
585 return gen_call_function_setup(func, 4);
586 }
587
588 static DRC_PTR_SIZE_IM INLINE gen_call_function_m(void * func,Bitu op) {
589 gen_load_param_mem(op,2);
590 return gen_call_function_setup(func, 3, true);
591 }
592
593 static DRC_PTR_SIZE_IM INLINE gen_call_function_mm(void * func,Bitu op1,Bitu op2) {
594 gen_load_param_mem(op2,3);
595 gen_load_param_mem(op1,2);
596 return gen_call_function_setup(func, 4, true);
597 }
598
599
600
601 enum save_info_type {db_exception, cycle_check, string_break};
602
603
604 // function that is called on exceptions
605 static BlockReturn DynRunException(Bit32u eip_add,Bit32u cycle_sub) {
606 reg_eip+=eip_add;
607 CPU_Cycles-=cycle_sub;
608 if (cpu.exception.which==SMC_CURRENT_BLOCK) return BR_SMCBlock;
609 CPU_Exception(cpu.exception.which,cpu.exception.error);
610 return BR_Normal;
611 }
612
613
614 // array with information about code that is generated at the
615 // end of a cache block because it is rarely reached (like exceptions)
616 static struct {
617 save_info_type type;
618 DRC_PTR_SIZE_IM branch_pos;
619 Bit32u eip_change;
620 Bitu cycles;
621 } save_info_dynrec[512];
622
623 Bitu used_save_info_dynrec=0;
624
625
626 // return from current block, with returncode
627 static void dyn_return(BlockReturn retcode,bool ret_exception=false) {
628 if (!ret_exception) {
629 gen_mov_dword_to_reg_imm(FC_RETOP,retcode);
630 }
631 gen_return_function();
632 }
633
634 static void dyn_run_code(void) {
635 gen_run_code();
636 gen_return_function();
637 }
638
639 // fill in code at the end of the block that contains rarely-executed code
640 // which is executed conditionally (like exceptions)
641 static void dyn_fill_blocks(void) {
642 for (Bitu sct=0; sct<used_save_info_dynrec; sct++) {
643 gen_fill_branch_long(save_info_dynrec[sct].branch_pos);
644 switch (save_info_dynrec[sct].type) {
645 case db_exception:
646 // code for exception handling, load cycles and call DynRunException
647 decode.cycles=save_info_dynrec[sct].cycles;
648 if (cpu.code.big) gen_call_function_II((void *)&DynRunException,save_info_dynrec[sct].eip_change,save_info_dynrec[sct].cycles);
649 else gen_call_function_II((void *)&DynRunException,save_info_dynrec[sct].eip_change&0xffff,save_info_dynrec[sct].cycles);
650 dyn_return(BR_Normal,true);
651 break;
652 case cycle_check:
653 // cycles are <=0 so exit the core
654 dyn_return(BR_Cycles);
655 break;
656 case string_break:
657 // interrupt looped string instruction, can be continued later
658 gen_add_direct_word(&reg_eip,save_info_dynrec[sct].eip_change,decode.big_op);
659 dyn_return(BR_Cycles);
660 break;
661 }
662 }
663 used_save_info_dynrec=0;
664 }
665
666
667 static void dyn_closeblock(void) {
668 //Shouldn't create empty block normally but let's do it like this
669 dyn_fill_blocks();
670 cache_block_before_close();
671 cache_closeblock();
672 cache_block_closing(decode.block->cache.start,decode.block->cache.size);
673 }
674
675
676 // add a check that can branch to the exception handling
677 static void dyn_check_exception(HostReg reg) {
678 save_info_dynrec[used_save_info_dynrec].branch_pos=gen_create_branch_long_nonzero(reg,false);
679 if (!decode.cycles) decode.cycles++;
680 save_info_dynrec[used_save_info_dynrec].cycles=decode.cycles;
681 // in case of an exception eip will point to the start of the current instruction
682 save_info_dynrec[used_save_info_dynrec].eip_change=decode.op_start-decode.code_start;
683 if (!cpu.code.big) save_info_dynrec[used_save_info_dynrec].eip_change&=0xffff;
684 save_info_dynrec[used_save_info_dynrec].type=db_exception;
685 used_save_info_dynrec++;
686 }
687
688
689
690 bool DRC_CALL_CONV mem_readb_checked_drc(PhysPt address) DRC_FC;
691 bool DRC_CALL_CONV mem_readb_checked_drc(PhysPt address) {
692 HostPt tlb_addr=get_tlb_read(address);
693 if (tlb_addr) {
694 *((Bit8u*)(&core_dynrec.readdata))=host_readb(tlb_addr+address);
695 return false;
696 } else {
697 return get_tlb_readhandler(address)->readb_checked(address, (Bit8u*)(&core_dynrec.readdata));
698 }
699 }
700
701 bool DRC_CALL_CONV mem_writeb_checked_drc(PhysPt address,Bit8u val) DRC_FC;
702 bool DRC_CALL_CONV mem_writeb_checked_drc(PhysPt address,Bit8u val) {
703 HostPt tlb_addr=get_tlb_write(address);
704 if (tlb_addr) {
705 host_writeb(tlb_addr+address,val);
706 return false;
707 } else return get_tlb_writehandler(address)->writeb_checked(address,val);
708 }
709
710 bool DRC_CALL_CONV mem_readw_checked_drc(PhysPt address) DRC_FC;
711 bool DRC_CALL_CONV mem_readw_checked_drc(PhysPt address) {
712 if ((address & 0xfff)<0xfff) {
713 HostPt tlb_addr=get_tlb_read(address);
714 if (tlb_addr) {
715 *((Bit16u*)(&core_dynrec.readdata))=host_readw(tlb_addr+address);
716 return false;
717 } else return get_tlb_readhandler(address)->readw_checked(address, (Bit16u*)(&core_dynrec.readdata));
718 } else return mem_unalignedreadw_checked(address, ((Bit16u*)(&core_dynrec.readdata)));
719 }
720
721 bool DRC_CALL_CONV mem_readd_checked_drc(PhysPt address) DRC_FC;
722 bool DRC_CALL_CONV mem_readd_checked_drc(PhysPt address) {
723 if ((address & 0xfff)<0xffd) {
724 HostPt tlb_addr=get_tlb_read(address);
725 if (tlb_addr) {
726 *((Bit32u*)(&core_dynrec.readdata))=host_readd(tlb_addr+address);
727 return false;
728 } else return get_tlb_readhandler(address)->readd_checked(address, (Bit32u*)(&core_dynrec.readdata));
729 } else return mem_unalignedreadd_checked(address, ((Bit32u*)(&core_dynrec.readdata)));
730 }
731
732 bool DRC_CALL_CONV mem_writew_checked_drc(PhysPt address,Bit16u val) DRC_FC;
733 bool DRC_CALL_CONV mem_writew_checked_drc(PhysPt address,Bit16u val) {
734 if ((address & 0xfff)<0xfff) {
735 HostPt tlb_addr=get_tlb_write(address);
736 if (tlb_addr) {
737 host_writew(tlb_addr+address,val);
738 return false;
739 } else return get_tlb_writehandler(address)->writew_checked(address,val);
740 } else return mem_unalignedwritew_checked(address,val);
741 }
742
743 bool DRC_CALL_CONV mem_writed_checked_drc(PhysPt address,Bit32u val) DRC_FC;
744 bool DRC_CALL_CONV mem_writed_checked_drc(PhysPt address,Bit32u val) {
745 if ((address & 0xfff)<0xffd) {
746 HostPt tlb_addr=get_tlb_write(address);
747 if (tlb_addr) {
748 host_writed(tlb_addr+address,val);
749 return false;
750 } else return get_tlb_writehandler(address)->writed_checked(address,val);
751 } else return mem_unalignedwrited_checked(address,val);
752 }
753
754
755 // functions that enable access to the memory
756
757 // read a byte from a given address and store it in reg_dst
758 static void dyn_read_byte(HostReg reg_addr,HostReg reg_dst) {
759 gen_mov_regs(FC_OP1,reg_addr);
760 gen_call_function_raw((void *)&mem_readb_checked_drc);
761 dyn_check_exception(FC_RETOP);
762 gen_mov_byte_to_reg_low(reg_dst,&core_dynrec.readdata);
763 }
764 static void dyn_read_byte_canuseword(HostReg reg_addr,HostReg reg_dst) {
765 gen_mov_regs(FC_OP1,reg_addr);
766 gen_call_function_raw((void *)&mem_readb_checked_drc);
767 dyn_check_exception(FC_RETOP);
768 gen_mov_byte_to_reg_low_canuseword(reg_dst,&core_dynrec.readdata);
769 }
770
771 // write a byte from reg_val into the memory given by the address
772 static void dyn_write_byte(HostReg reg_addr,HostReg reg_val) {
773 gen_mov_regs(FC_OP2,reg_val);
774 gen_mov_regs(FC_OP1,reg_addr);
775 gen_call_function_raw((void *)&mem_writeb_checked_drc);
776 dyn_check_exception(FC_RETOP);
777 }
778
779 // read a 32bit (dword=true) or 16bit (dword=false) value
780 // from a given address and store it in reg_dst
781 static void dyn_read_word(HostReg reg_addr,HostReg reg_dst,bool dword) {
782 gen_mov_regs(FC_OP1,reg_addr);
783 if (dword) gen_call_function_raw((void *)&mem_readd_checked_drc);
784 else gen_call_function_raw((void *)&mem_readw_checked_drc);
785 dyn_check_exception(FC_RETOP);
786 gen_mov_word_to_reg(reg_dst,&core_dynrec.readdata,dword);
787 }
788
789 // write a 32bit (dword=true) or 16bit (dword=false) value
790 // from reg_val into the memory given by the address
791 static void dyn_write_word(HostReg reg_addr,HostReg reg_val,bool dword) {
792 // if (!dword) gen_extend_word(false,reg_val);
793 gen_mov_regs(FC_OP2,reg_val);
794 gen_mov_regs(FC_OP1,reg_addr);
795 if (dword) gen_call_function_raw((void *)&mem_writed_checked_drc);
796 else gen_call_function_raw((void *)&mem_writew_checked_drc);
797 dyn_check_exception(FC_RETOP);
798 }
799
800
801
802 // effective address calculation helper, op2 has to be present!
803 // loads op1 into ea_reg and adds the scaled op2 and the immediate to it
804 static void dyn_lea_mem_mem(HostReg ea_reg,void* op1,void* op2,Bitu scale,Bits imm) {
805 if (scale || imm) {
806 if (op1!=NULL) {
807 gen_mov_word_to_reg(ea_reg,op1,true);
808 gen_mov_word_to_reg(TEMP_REG_DRC,op2,true);
809
810 gen_lea(ea_reg,TEMP_REG_DRC,scale,imm);
811 } else {
812 gen_mov_word_to_reg(ea_reg,op2,true);
813 gen_lea(ea_reg,scale,imm);
814 }
815 } else {
816 gen_mov_word_to_reg(ea_reg,op2,true);
817 if (op1!=NULL) gen_add(ea_reg,op1);
818 }
819 }
820
821 #ifdef DRC_USE_REGS_ADDR
822 // effective address calculation helper
823 // loads op1 into ea_reg and adds the scaled op2 and the immediate to it
824 // op1 is cpu_regs[op1_index], op2 is cpu_regs[op2_index]
825 static void dyn_lea_regval_regval(HostReg ea_reg,Bitu op1_index,Bitu op2_index,Bitu scale,Bits imm) {
826 if (scale || imm) {
827 MOV_REG_VAL_TO_HOST_REG(ea_reg,op1_index);
828 MOV_REG_VAL_TO_HOST_REG(TEMP_REG_DRC,op2_index);
829
830 gen_lea(ea_reg,TEMP_REG_DRC,scale,imm);
831 } else {
832 MOV_REG_VAL_TO_HOST_REG(ea_reg,op2_index);
833 ADD_REG_VAL_TO_HOST_REG(ea_reg,op1_index);
834 }
835 }
836
837 // effective address calculation helper
838 // loads op1 into ea_reg and adds the scaled op2 and the immediate to it
839 // op2 is cpu_regs[op2_index]
840 static void dyn_lea_mem_regval(HostReg ea_reg,void* op1,Bitu op2_index,Bitu scale,Bits imm) {
841 if (scale || imm) {
842 if (op1!=NULL) {
843 gen_mov_word_to_reg(ea_reg,op1,true);
844 MOV_REG_VAL_TO_HOST_REG(TEMP_REG_DRC,op2_index);
845
846 gen_lea(ea_reg,TEMP_REG_DRC,scale,imm);
847 } else {
848 MOV_REG_VAL_TO_HOST_REG(ea_reg,op2_index);
849 gen_lea(ea_reg,scale,imm);
850 }
851 } else {
852 MOV_REG_VAL_TO_HOST_REG(ea_reg,op2_index);
853 if (op1!=NULL) gen_add(ea_reg,op1);
854 }
855 }
856 #endif
857
858 #ifdef DRC_USE_SEGS_ADDR
859 #ifdef DRC_USE_REGS_ADDR
860 // effective address calculation helper
861 // loads op1 into ea_reg and adds the scaled op2 and the immediate to it
862 // op1 is Segs[op1_index], op2 is cpu_regs[op2_index]
863 static void dyn_lea_segphys_regval(HostReg ea_reg,Bitu op1_index,Bitu op2_index,Bitu scale,Bits imm) {
864 if (scale || imm) {
865 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,op1_index);
866 MOV_REG_VAL_TO_HOST_REG(TEMP_REG_DRC,op2_index);
867
868 gen_lea(ea_reg,TEMP_REG_DRC,scale,imm);
869 } else {
870 MOV_REG_VAL_TO_HOST_REG(ea_reg,op2_index);
871 ADD_SEG_PHYS_TO_HOST_REG(ea_reg,op1_index);
872 }
873 }
874
875 #else
876
877 // effective address calculation helper, op2 has to be present!
878 // loads op1 into ea_reg and adds the scaled op2 and the immediate to it
879 // op1 is Segs[op1_index]
880 static void dyn_lea_segphys_mem(HostReg ea_reg,Bitu op1_index,void* op2,Bitu scale,Bits imm) {
881 if (scale || imm) {
882 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,op1_index);
883 gen_mov_word_to_reg(TEMP_REG_DRC,op2,true);
884
885 gen_lea(ea_reg,TEMP_REG_DRC,scale,imm);
886 } else {
887 gen_mov_word_to_reg(ea_reg,op2,true);
888 ADD_SEG_PHYS_TO_HOST_REG(ea_reg,op1_index);
889 }
890 }
891 #endif
892 #endif
893
894 // calculate the effective address and store it in ea_reg
895 static void dyn_fill_ea(HostReg ea_reg,bool addseg=true) {
896 Bit8u seg_base=DRC_SEG_DS;
897 if (!decode.big_addr) {
898 Bits imm;
899 switch (decode.modrm.mod) {
900 case 0:imm=0;break;
901 case 1:imm=(Bit8s)decode_fetchb();break;
902 case 2:imm=(Bit16s)decode_fetchw();break;
903 }
904 switch (decode.modrm.rm) {
905 case 0:// BX+SI
906 DYN_LEA_REG_VAL_REG_VAL(ea_reg,DRC_REG_EBX,DRC_REG_ESI,0,imm);
907 break;
908 case 1:// BX+DI
909 DYN_LEA_REG_VAL_REG_VAL(ea_reg,DRC_REG_EBX,DRC_REG_EDI,0,imm);
910 break;
911 case 2:// BP+SI
912 DYN_LEA_REG_VAL_REG_VAL(ea_reg,DRC_REG_EBP,DRC_REG_ESI,0,imm);
913 seg_base=DRC_SEG_SS;
914 break;
915 case 3:// BP+DI
916 DYN_LEA_REG_VAL_REG_VAL(ea_reg,DRC_REG_EBP,DRC_REG_EDI,0,imm);
917 seg_base=DRC_SEG_SS;
918 break;
919 case 4:// SI
920 MOV_REG_VAL_TO_HOST_REG(ea_reg,DRC_REG_ESI);
921 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
922 break;
923 case 5:// DI
924 MOV_REG_VAL_TO_HOST_REG(ea_reg,DRC_REG_EDI);
925 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
926 break;
927 case 6:// imm/BP
928 if (!decode.modrm.mod) {
929 imm=decode_fetchw();
930 gen_mov_dword_to_reg_imm(ea_reg,(Bit32u)imm);
931 goto skip_extend_word;
932 } else {
933 MOV_REG_VAL_TO_HOST_REG(ea_reg,DRC_REG_EBP);
934 gen_add_imm(ea_reg,(Bit32u)imm);
935 seg_base=DRC_SEG_SS;
936 }
937 break;
938 case 7: // BX
939 MOV_REG_VAL_TO_HOST_REG(ea_reg,DRC_REG_EBX);
940 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
941 break;
942 }
943 // zero out the high 16bit so ea_reg can be used as full register
944 gen_extend_word(false,ea_reg);
945 skip_extend_word:
946 if (addseg) {
947 // add the physical segment value if requested
948 ADD_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
949 }
950 } else {
951 Bits imm=0;
952 Bit8u base_reg;
953 Bit8u scaled_reg;
954 Bitu scale=0;
955 switch (decode.modrm.rm) {
956 case 0:base_reg=DRC_REG_EAX;break;
957 case 1:base_reg=DRC_REG_ECX;break;
958 case 2:base_reg=DRC_REG_EDX;break;
959 case 3:base_reg=DRC_REG_EBX;break;
960 case 4: // SIB
961 {
962 Bitu sib=decode_fetchb();
963 bool scaled_reg_used=false;
964 static Bit8u scaledtable[8]={
965 DRC_REG_EAX,DRC_REG_ECX,DRC_REG_EDX,DRC_REG_EBX,
966 0,DRC_REG_EBP,DRC_REG_ESI,DRC_REG_EDI
967 };
968 // see if scaling should be used and which register is to be scaled in this case
969 if (((sib >> 3) &7)!=4) scaled_reg_used=true;
970 scaled_reg=scaledtable[(sib >> 3) &7];
971 scale=(sib >> 6);
972
973 switch (sib & 7) {
974 case 0:base_reg=DRC_REG_EAX;break;
975 case 1:base_reg=DRC_REG_ECX;break;
976 case 2:base_reg=DRC_REG_EDX;break;
977 case 3:base_reg=DRC_REG_EBX;break;
978 case 4:base_reg=DRC_REG_ESP;seg_base=DRC_SEG_SS;break;
979 case 5:
980 if (decode.modrm.mod) {
981 base_reg=DRC_REG_EBP;seg_base=DRC_SEG_SS;
982 } else {
983 // no basereg, maybe scalereg
984 Bitu val;
985 // try to get a pointer to the next dword code position
986 if (decode_fetchd_imm(val)) {
987 // succeeded, use the pointer to avoid code invalidation
988 if (!addseg) {
989 if (!scaled_reg_used) {
990 gen_mov_word_to_reg(ea_reg,(void*)val,true);
991 } else {
992 DYN_LEA_MEM_REG_VAL(ea_reg,NULL,scaled_reg,scale,0);
993 gen_add(ea_reg,(void*)val);
994 }
995 } else {
996 if (!scaled_reg_used) {
997 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
998 } else {
999 DYN_LEA_SEG_PHYS_REG_VAL(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base),scaled_reg,scale,0);
1000 }
1001 gen_add(ea_reg,(void*)val);
1002 }
1003 return;
1004 }
1005 // couldn't get a pointer, use the current value
1006 imm=(Bit32s)val;
1007
1008 if (!addseg) {
1009 if (!scaled_reg_used) {
1010 gen_mov_dword_to_reg_imm(ea_reg,(Bit32u)imm);
1011 } else {
1012 DYN_LEA_MEM_REG_VAL(ea_reg,NULL,scaled_reg,scale,imm);
1013 }
1014 } else {
1015 if (!scaled_reg_used) {
1016 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1017 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
1018 } else {
1019 DYN_LEA_SEG_PHYS_REG_VAL(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base),scaled_reg,scale,imm);
1020 }
1021 }
1022
1023 return;
1024 }
1025 break;
1026 case 6:base_reg=DRC_REG_ESI;break;
1027 case 7:base_reg=DRC_REG_EDI;break;
1028 }
1029 // basereg, maybe scalereg
1030 switch (decode.modrm.mod) {
1031 case 1:
1032 imm=(Bit8s)decode_fetchb();
1033 break;
1034 case 2: {
1035 Bitu val;
1036 // try to get a pointer to the next dword code position
1037 if (decode_fetchd_imm(val)) {
1038 // succeeded, use the pointer to avoid code invalidation
1039 if (!addseg) {
1040 if (!scaled_reg_used) {
1041 MOV_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1042 gen_add(ea_reg,(void*)val);
1043 } else {
1044 DYN_LEA_REG_VAL_REG_VAL(ea_reg,base_reg,scaled_reg,scale,0);
1045 gen_add(ea_reg,(void*)val);
1046 }
1047 } else {
1048 if (!scaled_reg_used) {
1049 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1050 } else {
1051 DYN_LEA_SEG_PHYS_REG_VAL(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base),scaled_reg,scale,0);
1052 }
1053 ADD_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1054 gen_add(ea_reg,(void*)val);
1055 }
1056 return;
1057 }
1058 // couldn't get a pointer, use the current value
1059 imm=(Bit32s)val;
1060 break;
1061 }
1062 }
1063
1064 if (!addseg) {
1065 if (!scaled_reg_used) {
1066 MOV_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1067 gen_add_imm(ea_reg,(Bit32u)imm);
1068 } else {
1069 DYN_LEA_REG_VAL_REG_VAL(ea_reg,base_reg,scaled_reg,scale,imm);
1070 }
1071 } else {
1072 if (!scaled_reg_used) {
1073 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1074 ADD_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1075 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
1076 } else {
1077 DYN_LEA_SEG_PHYS_REG_VAL(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base),scaled_reg,scale,imm);
1078 ADD_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1079 }
1080 }
1081
1082 return;
1083 }
1084 break; // SIB Break
1085 case 5:
1086 if (decode.modrm.mod) {
1087 base_reg=DRC_REG_EBP;seg_base=DRC_SEG_SS;
1088 } else {
1089 // no base, no scalereg
1090
1091 imm=(Bit32s)decode_fetchd();
1092 if (!addseg) {
1093 gen_mov_dword_to_reg_imm(ea_reg,(Bit32u)imm);
1094 } else {
1095 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1096 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
1097 }
1098
1099 return;
1100 }
1101 break;
1102 case 6:base_reg=DRC_REG_ESI;break;
1103 case 7:base_reg=DRC_REG_EDI;break;
1104 }
1105
1106 // no scalereg, but basereg
1107
1108 switch (decode.modrm.mod) {
1109 case 1:
1110 imm=(Bit8s)decode_fetchb();
1111 break;
1112 case 2: {
1113 Bitu val;
1114 // try to get a pointer to the next dword code position
1115 if (decode_fetchd_imm(val)) {
1116 // succeeded, use the pointer to avoid code invalidation
1117 if (!addseg) {
1118 MOV_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1119 gen_add(ea_reg,(void*)val);
1120 } else {
1121 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1122 ADD_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1123 gen_add(ea_reg,(void*)val);
1124 }
1125 return;
1126 }
1127 // couldn't get a pointer, use the current value
1128 imm=(Bit32s)val;
1129 break;
1130 }
1131 }
1132
1133 if (!addseg) {
1134 MOV_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1135 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
1136 } else {
1137 MOV_SEG_PHYS_TO_HOST_REG(ea_reg,(decode.seg_prefix_used ? decode.seg_prefix : seg_base));
1138 ADD_REG_VAL_TO_HOST_REG(ea_reg,base_reg);
1139 if (imm) gen_add_imm(ea_reg,(Bit32u)imm);
1140 }
1141 }
1142 }
1143
1144
1145
1146 // add code that checks if port access is allowed
1147 // the port is given in a register
1148 static void dyn_add_iocheck(HostReg reg_port,Bitu access_size) {
1149 if (cpu.pmode) {
1150 gen_call_function_RI((void *)&CPU_IO_Exception,reg_port,access_size);
1151 dyn_check_exception(FC_RETOP);
1152 }
1153 }
1154
1155 // add code that checks if port access is allowed
1156 // the port is a constant
1157 static void dyn_add_iocheck_var(Bit8u accessed_port,Bitu access_size) {
1158 if (cpu.pmode) {
1159 gen_call_function_II((void *)&CPU_IO_Exception,accessed_port,access_size);
1160 dyn_check_exception(FC_RETOP);
1161 }
1162 }
1163
1164
1165
1166 // save back the address register
1167 static void gen_protect_addr_reg(void) {
1168 #ifdef DRC_PROTECT_ADDR_REG
1169 gen_mov_word_from_reg(FC_ADDR,&core_dynrec.protected_regs[FC_ADDR],true);
1170 #endif
1171 }
1172
1173 // restore the address register
1174 static void gen_restore_addr_reg(void) {
1175 #ifdef DRC_PROTECT_ADDR_REG
1176 gen_mov_word_to_reg(FC_ADDR,&core_dynrec.protected_regs[FC_ADDR],true);
1177 #endif
1178 }
1179
1180 // save back an arbitrary register
1181 static void gen_protect_reg(HostReg reg) {
1182 gen_mov_word_from_reg(reg,&core_dynrec.protected_regs[reg],true);
1183 }
1184
1185 // restore an arbitrary register
1186 static void gen_restore_reg(HostReg reg) {
1187 gen_mov_word_to_reg(reg,&core_dynrec.protected_regs[reg],true);
1188 }
1189
1190 // restore an arbitrary register into a different register
1191 static void gen_restore_reg(HostReg reg,HostReg dest_reg) {
1192 gen_mov_word_to_reg(dest_reg,&core_dynrec.protected_regs[reg],true);
1193 }
1194
1195
1196
1197 // flags optimization functions
1198 // they try to find out if a function can be replaced by another
1199 // one that does not generate any flags at all
1200
1201 static Bitu mf_functions_num=0;
1202 static struct {
1203 Bit8u* pos;
1204 void* fct_ptr;
1205 Bitu ftype;
1206 } mf_functions[64];
1207
1208 static void InitFlagsOptimization(void) {
1209 mf_functions_num=0;
1210 }
1211
1212 // replace all queued functions with their simpler variants
1213 // because the current instruction destroys all condition flags and
1214 // the flags are not required before
1215 static void InvalidateFlags(void) {
1216 #ifdef DRC_FLAGS_INVALIDATION
1217 for (Bitu ct=0; ct<mf_functions_num; ct++) {
1218 gen_fill_function_ptr(mf_functions[ct].pos,mf_functions[ct].fct_ptr,mf_functions[ct].ftype);
1219 }
1220 mf_functions_num=0;
1221 #endif
1222 }
1223
1224 // replace all queued functions with their simpler variants
1225 // because the current instruction destroys all condition flags and
1226 // the flags are not required before
1227 static void InvalidateFlags(void* current_simple_function,Bitu flags_type) {
1228 #ifdef DRC_FLAGS_INVALIDATION
1229 for (Bitu ct=0; ct<mf_functions_num; ct++) {
1230 gen_fill_function_ptr(mf_functions[ct].pos,mf_functions[ct].fct_ptr,mf_functions[ct].ftype);
1231 }
1232 mf_functions_num=1;
1233 mf_functions[0].pos=cache.pos;
1234 mf_functions[0].fct_ptr=current_simple_function;
1235 mf_functions[0].ftype=flags_type;
1236 #endif
1237 }
1238
1239 // enqueue this instruction, if later an instruction is encountered that
1240 // destroys all condition flags and the flags weren't needed in-between
1241 // this function can be replaced by a simpler one as well
1242 static void InvalidateFlagsPartially(void* current_simple_function,Bitu flags_type) {
1243 #ifdef DRC_FLAGS_INVALIDATION
1244 mf_functions[mf_functions_num].pos=cache.pos;
1245 mf_functions[mf_functions_num].fct_ptr=current_simple_function;
1246 mf_functions[mf_functions_num].ftype=flags_type;
1247 mf_functions_num++;
1248 #endif
1249 }
1250
1251 // enqueue this instruction, if later an instruction is encountered that
1252 // destroys all condition flags and the flags weren't needed in-between
1253 // this function can be replaced by a simpler one as well
1254 static void InvalidateFlagsPartially(void* current_simple_function,DRC_PTR_SIZE_IM cpos,Bitu flags_type) {
1255 #ifdef DRC_FLAGS_INVALIDATION
1256 mf_functions[mf_functions_num].pos=(Bit8u*)cpos;
1257 mf_functions[mf_functions_num].fct_ptr=current_simple_function;
1258 mf_functions[mf_functions_num].ftype=flags_type;
1259 mf_functions_num++;
1260 #endif
1261 }
1262
1263 // the current function needs the condition flags thus reset the queue
1264 static void AcquireFlags(Bitu flags_mask) {
1265 #ifdef DRC_FLAGS_INVALIDATION
1266 mf_functions_num=0;
1267 #endif
1268 }