Debian Codegraph @

src/cpu/core_dynrec/risc_x86.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: risc_x86.h,v 1.10 2009-06-25 19:31:43 c2woody Exp $ */
20
21
22 // some configuring defines that specify the capabilities of this architecture
23 // or aspects of the recompiling
24
25 // protect FC_ADDR over function calls if necessaray
26 // #define DRC_PROTECT_ADDR_REG
27
28 // try to use non-flags generating functions if possible
29 #define DRC_FLAGS_INVALIDATION
30 // try to replace _simple functions by code
31 #define DRC_FLAGS_INVALIDATION_DCODE
32
33 // type with the same size as a pointer
34 #define DRC_PTR_SIZE_IM Bit32u
35
36 // calling convention modifier
37 #if defined (WIN32)
38 #define DRC_CALL_CONV _fastcall
39 #define DRC_FC /* nothing */
40 #else
41 #define DRC_CALL_CONV /* nothing */
42 #define DRC_FC GCC_ATTRIBUTE(fastcall)
43 #endif
44
45
46 // register mapping
47 enum HostReg {
48 HOST_EAX=0,
49 HOST_ECX,
50 HOST_EDX,
51 HOST_EBX,
52 HOST_ESP,
53 HOST_EBP,
54 HOST_ESI,
55 HOST_EDI
56 };
57
58
59 // register that holds function return values
60 #define FC_RETOP HOST_EAX
61
62 // register used for address calculations, if the ABI does not
63 // state that this register is preserved across function calls
64 // then define DRC_PROTECT_ADDR_REG above
65 #define FC_ADDR HOST_EBX
66
67 // register that holds the first parameter
68 #define FC_OP1 HOST_ECX
69
70 // register that holds the second parameter
71 #define FC_OP2 HOST_EDX
72
73 // special register that holds the third parameter for _R3 calls (byte accessible)
74 #define FC_OP3 HOST_EAX
75
76 // register that holds byte-accessible temporary values
77 #define FC_TMP_BA1 HOST_ECX
78
79 // register that holds byte-accessible temporary values
80 #define FC_TMP_BA2 HOST_EDX
81
82
83 // temporary register for LEA
84 #define TEMP_REG_DRC HOST_ESI
85
86
87 // move a full register from reg_src to reg_dst
88 static void gen_mov_regs(HostReg reg_dst,HostReg reg_src) {
89 cache_addb(0x8b); // mov reg_dst,reg_src
90 cache_addb(0xc0+(reg_dst<<3)+reg_src);
91 }
92
93 // move a 32bit (dword==true) or 16bit (dword==false) value from memory into dest_reg
94 // 16bit moves may destroy the upper 16bit of the destination register
95 static void gen_mov_word_to_reg(HostReg dest_reg,void* data,bool dword) {
96 if (!dword) cache_addb(0x66);
97 cache_addw(0x058b+(dest_reg<<11)); // mov reg,[data]
98 cache_addd((Bit32u)data);
99 }
100
101 // move a 16bit constant value into dest_reg
102 // the upper 16bit of the destination register may be destroyed
103 static void gen_mov_word_to_reg_imm(HostReg dest_reg,Bit16u imm) {
104 cache_addb(0x66);
105 cache_addb(0xb8+dest_reg); // mov reg,imm
106 cache_addw(imm);
107 }
108
109 // move a 32bit constant value into dest_reg
110 static void gen_mov_dword_to_reg_imm(HostReg dest_reg,Bit32u imm) {
111 cache_addb(0xb8+dest_reg); // mov reg,imm
112 cache_addd(imm);
113 }
114
115 // move 32bit (dword==true) or 16bit (dword==false) of a register into memory
116 static void gen_mov_word_from_reg(HostReg src_reg,void* dest,bool dword) {
117 if (!dword) cache_addb(0x66);
118 cache_addw(0x0589+(src_reg<<11)); // mov [data],reg
119 cache_addd((Bit32u)dest);
120 }
121
122 // move an 8bit value from memory into dest_reg
123 // the upper 24bit of the destination register can be destroyed
124 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
125 // registers might not be directly byte-accessible on some architectures
126 static void gen_mov_byte_to_reg_low(HostReg dest_reg,void* data) {
127 cache_addw(0x058a+(dest_reg<<11)); // mov reg,[data]
128 cache_addd((Bit32u)data);
129 }
130
131 // move an 8bit value from memory into dest_reg
132 // the upper 24bit of the destination register can be destroyed
133 // this function can use FC_OP1/FC_OP2 as dest_reg which are
134 // not directly byte-accessible on some architectures
135 static void gen_mov_byte_to_reg_low_canuseword(HostReg dest_reg,void* data) {
136 cache_addb(0x66);
137 cache_addw(0x058b+(dest_reg<<11)); // mov reg,[data]
138 cache_addd((Bit32u)data);
139 }
140
141 // move an 8bit constant value into dest_reg
142 // the upper 24bit of the destination register can be destroyed
143 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
144 // registers might not be directly byte-accessible on some architectures
145 static void gen_mov_byte_to_reg_low_imm(HostReg dest_reg,Bit8u imm) {
146 cache_addb(0xb0+dest_reg); // mov reg,imm
147 cache_addb(imm);
148 }
149
150 // move an 8bit constant value into dest_reg
151 // the upper 24bit of the destination register can be destroyed
152 // this function can use FC_OP1/FC_OP2 as dest_reg which are
153 // not directly byte-accessible on some architectures
154 static void gen_mov_byte_to_reg_low_imm_canuseword(HostReg dest_reg,Bit8u imm) {
155 cache_addb(0x66);
156 cache_addb(0xb8+dest_reg); // mov reg,imm
157 cache_addw(imm);
158 }
159
160 // move the lowest 8bit of a register into memory
161 static void gen_mov_byte_from_reg_low(HostReg src_reg,void* dest) {
162 cache_addw(0x0588+(src_reg<<11)); // mov [data],reg
163 cache_addd((Bit32u)dest);
164 }
165
166
167
168 // convert an 8bit word to a 32bit dword
169 // the register is zero-extended (sign==false) or sign-extended (sign==true)
170 static void gen_extend_byte(bool sign,HostReg reg) {
171 cache_addw(0xb60f+(sign?0x800:0)); // movsx/movzx
172 cache_addb(0xc0+(reg<<3)+reg);
173 }
174
175 // convert a 16bit word to a 32bit dword
176 // the register is zero-extended (sign==false) or sign-extended (sign==true)
177 static void gen_extend_word(bool sign,HostReg reg) {
178 cache_addw(0xb70f+(sign?0x800:0)); // movsx/movzx
179 cache_addb(0xc0+(reg<<3)+reg);
180 }
181
182
183
184 // add a 32bit value from memory to a full register
185 static void gen_add(HostReg reg,void* op) {
186 cache_addw(0x0503+(reg<<11)); // add reg,[data]
187 cache_addd((Bit32u)op);
188 }
189
190 // add a 32bit constant value to a full register
191 static void gen_add_imm(HostReg reg,Bit32u imm) {
192 cache_addw(0xc081+(reg<<8)); // add reg,imm
193 cache_addd(imm);
194 }
195
196 // and a 32bit constant value with a full register
197 static void gen_and_imm(HostReg reg,Bit32u imm) {
198 cache_addw(0xe081+(reg<<8)); // and reg,imm
199 cache_addd(imm);
200 }
201
202
203
204 // move a 32bit constant value into memory
205 static void gen_mov_direct_dword(void* dest,Bit32u imm) {
206 cache_addw(0x05c7); // mov [data],imm
207 cache_addd((Bit32u)dest);
208 cache_addd(imm);
209 }
210
211 // move an address into memory
212 static void INLINE gen_mov_direct_ptr(void* dest,DRC_PTR_SIZE_IM imm) {
213 gen_mov_direct_dword(dest,(Bit32u)imm);
214 }
215
216
217 // add an 8bit constant value to a memory value
218 static void gen_add_direct_byte(void* dest,Bit8s imm) {
219 cache_addw(0x0583); // add [data],imm
220 cache_addd((Bit32u)dest);
221 cache_addb(imm);
222 }
223
224 // add a 32bit (dword==true) or 16bit (dword==false) constant value to a memory value
225 static void gen_add_direct_word(void* dest,Bit32u imm,bool dword) {
226 if ((imm<128) && dword) {
227 gen_add_direct_byte(dest,(Bit8s)imm);
228 return;
229 }
230 if (!dword) cache_addb(0x66);
231 cache_addw(0x0581); // add [data],imm
232 cache_addd((Bit32u)dest);
233 if (dword) cache_addd((Bit32u)imm);
234 else cache_addw((Bit16u)imm);
235 }
236
237 // subtract an 8bit constant value from a memory value
238 static void gen_sub_direct_byte(void* dest,Bit8s imm) {
239 cache_addw(0x2d83); // sub [data],imm
240 cache_addd((Bit32u)dest);
241 cache_addb(imm);
242 }
243
244 // subtract a 32bit (dword==true) or 16bit (dword==false) constant value from a memory value
245 static void gen_sub_direct_word(void* dest,Bit32u imm,bool dword) {
246 if ((imm<128) && dword) {
247 gen_sub_direct_byte(dest,(Bit8s)imm);
248 return;
249 }
250 if (!dword) cache_addb(0x66);
251 cache_addw(0x2d81); // sub [data],imm
252 cache_addd((Bit32u)dest);
253 if (dword) cache_addd((Bit32u)imm);
254 else cache_addw((Bit16u)imm);
255 }
256
257
258
259 // effective address calculation, destination is dest_reg
260 // scale_reg is scaled by scale (scale_reg*(2^scale)) and
261 // added to dest_reg, then the immediate value is added
262 static INLINE void gen_lea(HostReg dest_reg,HostReg scale_reg,Bitu scale,Bits imm) {
263 Bit8u rm_base;
264 Bitu imm_size;
265 if (!imm) {
266 imm_size=0; rm_base=0x0; //no imm
267 } else if ((imm>=-128 && imm<=127)) {
268 imm_size=1; rm_base=0x40; //Signed byte imm
269 } else {
270 imm_size=4; rm_base=0x80; //Signed dword imm
271 }
272
273 // ea_reg := ea_reg+scale_reg*(2^scale)+imm
274 cache_addb(0x8d); //LEA
275 cache_addb(0x04+(dest_reg << 3)+rm_base); //The sib indicator
276 cache_addb(dest_reg+(scale_reg<<3)+(scale<<6));
277
278 switch (imm_size) {
279 case 0: break;
280 case 1:cache_addb(imm);break;
281 case 4:cache_addd(imm);break;
282 }
283 }
284
285 // effective address calculation, destination is dest_reg
286 // dest_reg is scaled by scale (dest_reg*(2^scale)),
287 // then the immediate value is added
288 static INLINE void gen_lea(HostReg dest_reg,Bitu scale,Bits imm) {
289 // ea_reg := ea_reg*(2^scale)+imm
290 // ea_reg := op2 *(2^scale)+imm
291 cache_addb(0x8d); //LEA
292 cache_addb(0x04+(dest_reg<<3));
293 cache_addb(0x05+(dest_reg<<3)+(scale<<6));
294
295 cache_addd(imm); // always add dword immediate
296 }
297
298
299
300 // generate a call to a parameterless function
301 static void INLINE gen_call_function_raw(void * func) {
302 cache_addb(0xe8);
303 cache_addd((Bit32u)func - (Bit32u)cache.pos-4);
304 }
305
306 // generate a call to a function with paramcount parameters
307 // note: the parameters are loaded in the architecture specific way
308 // using the gen_load_param_ functions below
309 static Bit32u INLINE gen_call_function_setup(void * func,Bitu paramcount,bool fastcall=false) {
310 Bit32u proc_addr=(Bit32u)cache.pos;
311 // Do the actual call to the procedure
312 cache_addb(0xe8);
313 cache_addd((Bit32u)func - (Bit32u)cache.pos-4);
314
315 // Restore the params of the stack
316 if (paramcount) {
317 cache_addw(0xc483); //add ESP,imm byte
318 cache_addb((!fastcall)?paramcount*4:0);
319 }
320 return proc_addr;
321 }
322
323
324 // load an immediate value as param'th function parameter
325 static void INLINE gen_load_param_imm(Bitu imm,Bitu param) {
326 cache_addb(0x68); // push immediate
327 cache_addd(imm);
328 }
329
330 // load an address as param'th function parameter
331 static void INLINE gen_load_param_addr(Bitu addr,Bitu param) {
332 cache_addb(0x68); // push immediate (address)
333 cache_addd(addr);
334 }
335
336 // load a host-register as param'th function parameter
337 static void INLINE gen_load_param_reg(Bitu reg,Bitu param) {
338 cache_addb(0x50+(reg&7)); // push reg
339 }
340
341 // load a value from memory as param'th function parameter
342 static void INLINE gen_load_param_mem(Bitu mem,Bitu param) {
343 cache_addw(0x35ff); // push []
344 cache_addd(mem);
345 }
346
347
348
349 // jump to an address pointed at by ptr, offset is in imm
350 static void gen_jmp_ptr(void * ptr,Bits imm=0) {
351 gen_mov_word_to_reg(HOST_EAX,ptr,true);
352 cache_addb(0xff); // jmp [eax+imm]
353 if (!imm) {
354 cache_addb(0x20);
355 } else if ((imm>=-128 && imm<=127)) {
356 cache_addb(0x60);
357 cache_addb(imm);
358 } else {
359 cache_addb(0xa0);
360 cache_addd(imm);
361 }
362 }
363
364
365 // short conditional jump (+-127 bytes) if register is zero
366 // the destination is set by gen_fill_branch() later
367 static Bit32u gen_create_branch_on_zero(HostReg reg,bool dword) {
368 if (!dword) cache_addb(0x66);
369 cache_addb(0x0b); // or reg,reg
370 cache_addb(0xc0+reg+(reg<<3));
371
372 cache_addw(0x0074); // jz addr
373 return ((Bit32u)cache.pos-1);
374 }
375
376 // short conditional jump (+-127 bytes) if register is nonzero
377 // the destination is set by gen_fill_branch() later
378 static Bit32u gen_create_branch_on_nonzero(HostReg reg,bool dword) {
379 if (!dword) cache_addb(0x66);
380 cache_addb(0x0b); // or reg,reg
381 cache_addb(0xc0+reg+(reg<<3));
382
383 cache_addw(0x0075); // jnz addr
384 return ((Bit32u)cache.pos-1);
385 }
386
387 // calculate relative offset and fill it into the location pointed to by data
388 static void gen_fill_branch(DRC_PTR_SIZE_IM data) {
389 #if C_DEBUG
390 Bits len=(Bit32u)cache.pos-data;
391 if (len<0) len=-len;
392 if (len>126) LOG_MSG("Big jump %d",len);
393 #endif
394 *(Bit8u*)data=(Bit8u)((Bit32u)cache.pos-data-1);
395 }
396
397 // conditional jump if register is nonzero
398 // for isdword==true the 32bit of the register are tested
399 // for isdword==false the lowest 8bit of the register are tested
400 static Bit32u gen_create_branch_long_nonzero(HostReg reg,bool isdword) {
401 // isdword: cmp reg32,0
402 // not isdword: cmp reg8,0
403 cache_addb(0x0a+(isdword?1:0)); // or reg,reg
404 cache_addb(0xc0+reg+(reg<<3));
405
406 cache_addw(0x850f); // jnz
407 cache_addd(0);
408 return ((Bit32u)cache.pos-4);
409 }
410
411 // compare 32bit-register against zero and jump if value less/equal than zero
412 static Bit32u gen_create_branch_long_leqzero(HostReg reg) {
413 cache_addw(0xf883+(reg<<8));
414 cache_addb(0x00); // cmp reg,0
415
416 cache_addw(0x8e0f); // jle
417 cache_addd(0);
418 return ((Bit32u)cache.pos-4);
419 }
420
421 // calculate long relative offset and fill it into the location pointed to by data
422 static void gen_fill_branch_long(Bit32u data) {
423 *(Bit32u*)data=((Bit32u)cache.pos-data-4);
424 }
425
426
427 static void gen_run_code(void) {
428 cache_addd(0x0424448b); // mov eax,[esp+4]
429 cache_addb(0x53); // push ebx
430 cache_addb(0x56); // push esi
431 cache_addw(0xd0ff); // call eax
432 cache_addb(0x5e); // pop esi
433 cache_addb(0x5b); // pop ebx
434 }
435
436 // return from a function
437 static void gen_return_function(void) {
438 cache_addb(0xc3); // ret
439 }
440
441 #ifdef DRC_FLAGS_INVALIDATION
442 // called when a call to a function can be replaced by a
443 // call to a simpler function
444 static void gen_fill_function_ptr(Bit8u * pos,void* fct_ptr,Bitu flags_type) {
445 #ifdef DRC_FLAGS_INVALIDATION_DCODE
446 // try to avoid function calls but rather directly fill in code
447 switch (flags_type) {
448 case t_ADDb:
449 case t_ADDw:
450 case t_ADDd:
451 *(Bit32u*)pos=0xc203c18b; // mov eax,ecx; add eax,edx
452 *(pos+4)=0x90;
453 break;
454 case t_ORb:
455 case t_ORw:
456 case t_ORd:
457 *(Bit32u*)pos=0xc20bc18b; // mov eax,ecx; or eax,edx
458 *(pos+4)=0x90;
459 break;
460 case t_ANDb:
461 case t_ANDw:
462 case t_ANDd:
463 *(Bit32u*)pos=0xc223c18b; // mov eax,ecx; and eax,edx
464 *(pos+4)=0x90;
465 break;
466 case t_SUBb:
467 case t_SUBw:
468 case t_SUBd:
469 *(Bit32u*)pos=0xc22bc18b; // mov eax,ecx; sub eax,edx
470 *(pos+4)=0x90;
471 break;
472 case t_XORb:
473 case t_XORw:
474 case t_XORd:
475 *(Bit32u*)pos=0xc233c18b; // mov eax,ecx; xor eax,edx
476 *(pos+4)=0x90;
477 break;
478 case t_CMPb:
479 case t_CMPw:
480 case t_CMPd:
481 case t_TESTb:
482 case t_TESTw:
483 case t_TESTd:
484 *(Bit32u*)pos=0x909003eb; // skip
485 *(pos+4)=0x90;
486 break;
487 case t_INCb:
488 case t_INCw:
489 case t_INCd:
490 *(Bit32u*)pos=0x9040c18b; // mov eax,ecx; inc eax
491 *(pos+4)=0x90;
492 break;
493 case t_DECb:
494 case t_DECw:
495 case t_DECd:
496 *(Bit32u*)pos=0x9048c18b; // mov eax,ecx; dec eax
497 *(pos+4)=0x90;
498 break;
499 case t_NEGb:
500 case t_NEGw:
501 case t_NEGd:
502 *(Bit32u*)pos=0xd8f7c18b; // mov eax,ecx; neg eax
503 *(pos+4)=0x90;
504 break;
505 default:
506 *(Bit32u*)(pos+1)=(Bit32u)((Bit8u*)fct_ptr - (pos+1+4)); // fill function pointer
507 break;
508 }
509 #else
510 *(Bit32u*)(pos+1)=(Bit32u)((Bit8u*)fct_ptr - (pos+1+4)); // fill function pointer
511 #endif
512 }
513 #endif
514
515 static void cache_block_closing(Bit8u* block_start,Bitu block_size) { }
516
517 static void cache_block_before_close(void) { }