Debian Codegraph @

src/cpu/core_dynrec/risc_armv4le-thumb-iw.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_armv4le-thumb-iw.h,v 1.5 2009-06-27 12:51:10 c2woody Exp $ */
20
21
22 /* ARMv4 (little endian) backend by M-HT (thumb version with data pool, requires -mthumb-interwork switch when compiling dosbox) */
23
24
25 // temporary "lo" registers
26 #define templo1 HOST_v3
27 #define templo2 HOST_v4
28 #define templo3 HOST_v2
29
30 // register that holds function return values
31 #define FC_RETOP HOST_a1
32
33 // register used for address calculations,
34 #define FC_ADDR HOST_v1 // has to be saved across calls, see DRC_PROTECT_ADDR_REG
35
36 // register that holds the first parameter
37 #define FC_OP1 HOST_a1
38
39 // register that holds the second parameter
40 #define FC_OP2 HOST_a2
41
42 // special register that holds the third parameter for _R3 calls (byte accessible)
43 #define FC_OP3 HOST_a4
44
45 // register that holds byte-accessible temporary values
46 #define FC_TMP_BA1 HOST_a1
47
48 // register that holds byte-accessible temporary values
49 #define FC_TMP_BA2 HOST_a2
50
51 // temporary register for LEA
52 #define TEMP_REG_DRC HOST_a4
53
54 #ifdef DRC_USE_REGS_ADDR
55 // used to hold the address of "cpu_regs" - preferably filled in function gen_run_code
56 #define FC_REGS_ADDR HOST_v7
57 #endif
58
59 #ifdef DRC_USE_SEGS_ADDR
60 // used to hold the address of "Segs" - preferably filled in function gen_run_code
61 #define FC_SEGS_ADDR HOST_v8
62 #endif
63
64
65 // instruction encodings
66
67 // move
68 // mov dst, #imm @ 0 <= imm <= 255
69 #define MOV_IMM(dst, imm) (0x2000 + ((dst) << 8) + (imm) )
70 // mov dst, src
71 #define MOV_REG(dst, src) ADD_IMM3(dst, src, 0)
72 // mov dst, src
73 #define MOV_LO_HI(dst, src) (0x4640 + (dst) + (((src) - HOST_r8) << 3) )
74 // mov dst, src
75 #define MOV_HI_LO(dst, src) (0x4680 + ((dst) - HOST_r8) + ((src) << 3) )
76
77 // arithmetic
78 // add dst, src, #imm @ 0 <= imm <= 7
79 #define ADD_IMM3(dst, src, imm) (0x1c00 + (dst) + ((src) << 3) + ((imm) << 6) )
80 // add dst, #imm @ 0 <= imm <= 255
81 #define ADD_IMM8(dst, imm) (0x3000 + ((dst) << 8) + (imm) )
82 // add dst, src1, src2
83 #define ADD_REG(dst, src1, src2) (0x1800 + (dst) + ((src1) << 3) + ((src2) << 6) )
84 // add dst, pc, #imm @ 0 <= imm < 1024 & imm mod 4 = 0
85 #define ADD_LO_PC_IMM(dst, imm) (0xa000 + ((dst) << 8) + ((imm) >> 2) )
86 // sub dst, src1, src2
87 #define SUB_REG(dst, src1, src2) (0x1a00 + (dst) + ((src1) << 3) + ((src2) << 6) )
88 // sub dst, src, #imm @ 0 <= imm <= 7
89 #define SUB_IMM3(dst, src, imm) (0x1e00 + (dst) + ((src) << 3) + ((imm) << 6) )
90 // sub dst, #imm @ 0 <= imm <= 255
91 #define SUB_IMM8(dst, imm) (0x3800 + ((dst) << 8) + (imm) )
92 // neg dst, src
93 #define NEG(dst, src) (0x4240 + (dst) + ((src) << 3) )
94 // cmp dst, #imm @ 0 <= imm <= 255
95 #define CMP_IMM(dst, imm) (0x2800 + ((dst) << 8) + (imm) )
96 // nop
97 #define NOP (0x46c0)
98
99 // logical
100 // and dst, src
101 #define AND(dst, src) (0x4000 + (dst) + ((src) << 3) )
102 // eor dst, src
103 #define EOR(dst, src) (0x4040 + (dst) + ((src) << 3) )
104 // orr dst, src
105 #define ORR(dst, src) (0x4300 + (dst) + ((src) << 3) )
106
107 // shift/rotate
108 // lsl dst, src, #imm
109 #define LSL_IMM(dst, src, imm) (0x0000 + (dst) + ((src) << 3) + ((imm) << 6) )
110 // lsl dst, reg
111 #define LSL_REG(dst, reg) (0x4080 + (dst) + ((reg) << 3) )
112 // lsr dst, src, #imm
113 #define LSR_IMM(dst, src, imm) (0x0800 + (dst) + ((src) << 3) + ((imm) << 6) )
114 // lsr dst, reg
115 #define LSR_REG(dst, reg) (0x40c0 + (dst) + ((reg) << 3) )
116 // asr dst, src, #imm
117 #define ASR_IMM(dst, src, imm) (0x1000 + (dst) + ((src) << 3) + ((imm) << 6) )
118 // asr dst, reg
119 #define ASR_REG(dst, reg) (0x4100 + (dst) + ((reg) << 3) )
120 // ror dst, reg
121 #define ROR_REG(dst, reg) (0x41c0 + (dst) + ((reg) << 3) )
122
123 // load
124 // ldr reg, [addr, #imm] @ 0 <= imm < 128 & imm mod 4 = 0
125 #define LDR_IMM(reg, addr, imm) (0x6800 + (reg) + ((addr) << 3) + ((imm) << 4) )
126 // ldrh reg, [addr, #imm] @ 0 <= imm < 64 & imm mod 2 = 0
127 #define LDRH_IMM(reg, addr, imm) (0x8800 + (reg) + ((addr) << 3) + ((imm) << 5) )
128 // ldrb reg, [addr, #imm] @ 0 <= imm < 32
129 #define LDRB_IMM(reg, addr, imm) (0x7800 + (reg) + ((addr) << 3) + ((imm) << 6) )
130 // ldr reg, [pc, #imm] @ 0 <= imm < 1024 & imm mod 4 = 0
131 #define LDR_PC_IMM(reg, imm) (0x4800 + ((reg) << 8) + ((imm) >> 2) )
132
133 // store
134 // str reg, [addr, #imm] @ 0 <= imm < 128 & imm mod 4 = 0
135 #define STR_IMM(reg, addr, imm) (0x6000 + (reg) + ((addr) << 3) + ((imm) << 4) )
136 // strh reg, [addr, #imm] @ 0 <= imm < 64 & imm mod 2 = 0
137 #define STRH_IMM(reg, addr, imm) (0x8000 + (reg) + ((addr) << 3) + ((imm) << 5) )
138 // strb reg, [addr, #imm] @ 0 <= imm < 32
139 #define STRB_IMM(reg, addr, imm) (0x7000 + (reg) + ((addr) << 3) + ((imm) << 6) )
140
141 // branch
142 // beq pc+imm @ 0 <= imm < 256 & imm mod 2 = 0
143 #define BEQ_FWD(imm) (0xd000 + ((imm) >> 1) )
144 // bne pc+imm @ 0 <= imm < 256 & imm mod 2 = 0
145 #define BNE_FWD(imm) (0xd100 + ((imm) >> 1) )
146 // bgt pc+imm @ 0 <= imm < 256 & imm mod 2 = 0
147 #define BGT_FWD(imm) (0xdc00 + ((imm) >> 1) )
148 // b pc+imm @ 0 <= imm < 2048 & imm mod 2 = 0
149 #define B_FWD(imm) (0xe000 + ((imm) >> 1) )
150 // bx reg
151 #define BX(reg) (0x4700 + ((reg) << 3) )
152
153
154 // data pool defines
155 #define CACHE_DATA_JUMP (2)
156 #define CACHE_DATA_ALIGN (32)
157 #define CACHE_DATA_MIN (32)
158 #define CACHE_DATA_MAX (288)
159
160 // data pool variables
161 static Bit8u * cache_datapos = NULL; // position of data pool in the cache block
162 static Bit32u cache_datasize = 0; // total size of data pool
163 static Bit32u cache_dataindex = 0; // used size of data pool = index of free data item (in bytes) in data pool
164
165
166 // forwarded function
167 static void INLINE gen_create_branch_short(void * func);
168
169 // function to check distance to data pool
170 // if too close, then generate jump after data pool
171 static void cache_checkinstr(Bit32u size) {
172 if (cache_datasize == 0) {
173 if (cache_datapos != NULL) {
174 if (cache.pos + size + CACHE_DATA_JUMP >= cache_datapos) {
175 cache_datapos = NULL;
176 }
177 }
178 return;
179 }
180
181 if (cache.pos + size + CACHE_DATA_JUMP <= cache_datapos) return;
182
183 {
184 register Bit8u * newcachepos;
185
186 newcachepos = cache_datapos + cache_datasize;
187 gen_create_branch_short(newcachepos);
188 cache.pos = newcachepos;
189 }
190
191 if (cache.pos + CACHE_DATA_MAX + CACHE_DATA_ALIGN >= cache.block.active->cache.start + cache.block.active->cache.size &&
192 cache.pos + CACHE_DATA_MIN + CACHE_DATA_ALIGN + (CACHE_DATA_ALIGN - CACHE_ALIGN) < cache.block.active->cache.start + cache.block.active->cache.size)
193 {
194 cache_datapos = (Bit8u *) (((Bitu)cache.block.active->cache.start + cache.block.active->cache.size - CACHE_DATA_ALIGN) & ~(CACHE_DATA_ALIGN - 1));
195 } else {
196 register Bit32u cachemodsize;
197
198 cachemodsize = (cache.pos - cache.block.active->cache.start) & (CACHE_MAXSIZE - 1);
199
200 if (cachemodsize + CACHE_DATA_MAX + CACHE_DATA_ALIGN <= CACHE_MAXSIZE ||
201 cachemodsize + CACHE_DATA_MIN + CACHE_DATA_ALIGN + (CACHE_DATA_ALIGN - CACHE_ALIGN) > CACHE_MAXSIZE)
202 {
203 cache_datapos = (Bit8u *) (((Bitu)cache.pos + CACHE_DATA_MAX) & ~(CACHE_DATA_ALIGN - 1));
204 } else {
205 cache_datapos = (Bit8u *) (((Bitu)cache.pos + (CACHE_MAXSIZE - CACHE_DATA_ALIGN) - cachemodsize) & ~(CACHE_DATA_ALIGN - 1));
206 }
207 }
208
209 cache_datasize = 0;
210 cache_dataindex = 0;
211 }
212
213 // function to reserve item in data pool
214 // returns address of item
215 static Bit8u * cache_reservedata(void) {
216 // if data pool not yet initialized, then initialize data pool
217 if (GCC_UNLIKELY(cache_datapos == NULL)) {
218 if (cache.pos + CACHE_DATA_MIN + CACHE_DATA_ALIGN < cache.block.active->cache.start + CACHE_DATA_MAX) {
219 cache_datapos = (Bit8u *) (((Bitu)cache.block.active->cache.start + CACHE_DATA_MAX) & ~(CACHE_DATA_ALIGN - 1));
220 }
221 }
222
223 // if data pool not yet used, then set data pool
224 if (cache_datasize == 0) {
225 // set data pool address is too close (or behind) cache.pos then set new data pool size
226 if (cache.pos + CACHE_DATA_MIN + CACHE_DATA_JUMP /*+ CACHE_DATA_ALIGN*/ > cache_datapos) {
227 if (cache.pos + CACHE_DATA_MAX + CACHE_DATA_ALIGN >= cache.block.active->cache.start + cache.block.active->cache.size &&
228 cache.pos + CACHE_DATA_MIN + CACHE_DATA_ALIGN + (CACHE_DATA_ALIGN - CACHE_ALIGN) < cache.block.active->cache.start + cache.block.active->cache.size)
229 {
230 cache_datapos = (Bit8u *) (((Bitu)cache.block.active->cache.start + cache.block.active->cache.size - CACHE_DATA_ALIGN) & ~(CACHE_DATA_ALIGN - 1));
231 } else {
232 register Bit32u cachemodsize;
233
234 cachemodsize = (cache.pos - cache.block.active->cache.start) & (CACHE_MAXSIZE - 1);
235
236 if (cachemodsize + CACHE_DATA_MAX + CACHE_DATA_ALIGN <= CACHE_MAXSIZE ||
237 cachemodsize + CACHE_DATA_MIN + CACHE_DATA_ALIGN + (CACHE_DATA_ALIGN - CACHE_ALIGN) > CACHE_MAXSIZE)
238 {
239 cache_datapos = (Bit8u *) (((Bitu)cache.pos + CACHE_DATA_MAX) & ~(CACHE_DATA_ALIGN - 1));
240 } else {
241 cache_datapos = (Bit8u *) (((Bitu)cache.pos + (CACHE_MAXSIZE - CACHE_DATA_ALIGN) - cachemodsize) & ~(CACHE_DATA_ALIGN - 1));
242 }
243 }
244 }
245 // set initial data pool size
246 cache_datasize = CACHE_DATA_ALIGN;
247 }
248
249 // if data pool is full, then enlarge data pool
250 if (cache_dataindex == cache_datasize) {
251 cache_datasize += CACHE_DATA_ALIGN;
252 }
253
254 cache_dataindex += 4;
255 return (cache_datapos + (cache_dataindex - 4));
256 }
257
258 static void cache_block_before_close(void) {
259 // if data pool in use, then resize cache block to include the data pool
260 if (cache_datasize != 0)
261 {
262 cache.pos = cache_datapos + cache_dataindex;
263 }
264
265 // clear the values before next use
266 cache_datapos = NULL;
267 cache_datasize = 0;
268 cache_dataindex = 0;
269 }
270
271
272 // move a full register from reg_src to reg_dst
273 static void gen_mov_regs(HostReg reg_dst,HostReg reg_src) {
274 if(reg_src == reg_dst) return;
275 cache_checkinstr(2);
276 cache_addw( MOV_REG(reg_dst, reg_src) ); // mov reg_dst, reg_src
277 }
278
279 // move a 32bit constant value into dest_reg
280 static void gen_mov_dword_to_reg_imm(HostReg dest_reg,Bit32u imm) {
281 if ((imm & 0xffffff00) == 0) {
282 cache_checkinstr(2);
283 cache_addw( MOV_IMM(dest_reg, imm) ); // mov dest_reg, #(imm)
284 } else if ((imm & 0xffff00ff) == 0) {
285 cache_checkinstr(4);
286 cache_addw( MOV_IMM(dest_reg, imm >> 8) ); // mov dest_reg, #(imm >> 8)
287 cache_addw( LSL_IMM(dest_reg, dest_reg, 8) ); // lsl dest_reg, dest_reg, #8
288 } else if ((imm & 0xff00ffff) == 0) {
289 cache_checkinstr(4);
290 cache_addw( MOV_IMM(dest_reg, imm >> 16) ); // mov dest_reg, #(imm >> 16)
291 cache_addw( LSL_IMM(dest_reg, dest_reg, 16) ); // lsl dest_reg, dest_reg, #16
292 } else if ((imm & 0x00ffffff) == 0) {
293 cache_checkinstr(4);
294 cache_addw( MOV_IMM(dest_reg, imm >> 24) ); // mov dest_reg, #(imm >> 24)
295 cache_addw( LSL_IMM(dest_reg, dest_reg, 24) ); // lsl dest_reg, dest_reg, #24
296 } else {
297 Bit32u diff;
298
299 cache_checkinstr(4);
300
301 diff = imm - ((Bit32u)cache.pos+4);
302
303 if ((diff < 1024) && ((imm & 0x03) == 0)) {
304 if (((Bit32u)cache.pos & 0x03) == 0) {
305 cache_addw( ADD_LO_PC_IMM(dest_reg, diff >> 2) ); // add dest_reg, pc, #(diff >> 2)
306 } else {
307 cache_addw( NOP ); // nop
308 cache_addw( ADD_LO_PC_IMM(dest_reg, (diff - 2) >> 2) ); // add dest_reg, pc, #((diff - 2) >> 2)
309 }
310 } else {
311 Bit8u *datapos;
312
313 datapos = cache_reservedata();
314 *(Bit32u*)datapos=imm;
315
316 if (((Bit32u)cache.pos & 0x03) == 0) {
317 cache_addw( LDR_PC_IMM(dest_reg, datapos - (cache.pos + 4)) ); // ldr dest_reg, [pc, datapos]
318 } else {
319 cache_addw( LDR_PC_IMM(dest_reg, datapos - (cache.pos + 2)) ); // ldr dest_reg, [pc, datapos]
320 }
321 }
322 }
323 }
324
325 // helper function for gen_mov_word_to_reg
326 static void gen_mov_word_to_reg_helper(HostReg dest_reg,void* data,bool dword,HostReg data_reg) {
327 // alignment....
328 if (dword) {
329 if ((Bit32u)data & 3) {
330 if ( ((Bit32u)data & 3) == 2 ) {
331 cache_checkinstr(8);
332 cache_addw( LDRH_IMM(dest_reg, data_reg, 0) ); // ldrh dest_reg, [data_reg]
333 cache_addw( LDRH_IMM(templo1, data_reg, 2) ); // ldrh templo1, [data_reg, #2]
334 cache_addw( LSL_IMM(templo1, templo1, 16) ); // lsl templo1, templo1, #16
335 cache_addw( ORR(dest_reg, templo1) ); // orr dest_reg, templo1
336 } else {
337 cache_checkinstr(16);
338 cache_addw( LDRB_IMM(dest_reg, data_reg, 0) ); // ldrb dest_reg, [data_reg]
339 cache_addw( ADD_IMM3(templo1, data_reg, 1) ); // add templo1, data_reg, #1
340 cache_addw( LDRH_IMM(templo1, templo1, 0) ); // ldrh templo1, [templo1]
341 cache_addw( LSL_IMM(templo1, templo1, 8) ); // lsl templo1, templo1, #8
342 cache_addw( ORR(dest_reg, templo1) ); // orr dest_reg, templo1
343 cache_addw( LDRB_IMM(templo1, data_reg, 3) ); // ldrb templo1, [data_reg, #3]
344 cache_addw( LSL_IMM(templo1, templo1, 24) ); // lsl templo1, templo1, #24
345 cache_addw( ORR(dest_reg, templo1) ); // orr dest_reg, templo1
346 }
347 } else {
348 cache_checkinstr(2);
349 cache_addw( LDR_IMM(dest_reg, data_reg, 0) ); // ldr dest_reg, [data_reg]
350 }
351 } else {
352 if ((Bit32u)data & 1) {
353 cache_checkinstr(8);
354 cache_addw( LDRB_IMM(dest_reg, data_reg, 0) ); // ldrb dest_reg, [data_reg]
355 cache_addw( LDRB_IMM(templo1, data_reg, 1) ); // ldrb templo1, [data_reg, #1]
356 cache_addw( LSL_IMM(templo1, templo1, 8) ); // lsl templo1, templo1, #8
357 cache_addw( ORR(dest_reg, templo1) ); // orr dest_reg, templo1
358 } else {
359 cache_checkinstr(2);
360 cache_addw( LDRH_IMM(dest_reg, data_reg, 0) ); // ldrh dest_reg, [data_reg]
361 }
362 }
363 }
364
365 // move a 32bit (dword==true) or 16bit (dword==false) value from memory into dest_reg
366 // 16bit moves may destroy the upper 16bit of the destination register
367 static void gen_mov_word_to_reg(HostReg dest_reg,void* data,bool dword) {
368 gen_mov_dword_to_reg_imm(templo2, (Bit32u)data);
369 gen_mov_word_to_reg_helper(dest_reg, data, dword, templo2);
370 }
371
372 // move a 16bit constant value into dest_reg
373 // the upper 16bit of the destination register may be destroyed
374 static void INLINE gen_mov_word_to_reg_imm(HostReg dest_reg,Bit16u imm) {
375 gen_mov_dword_to_reg_imm(dest_reg, (Bit32u)imm);
376 }
377
378 // helper function for gen_mov_word_from_reg
379 static void gen_mov_word_from_reg_helper(HostReg src_reg,void* dest,bool dword, HostReg data_reg) {
380 // alignment....
381 if (dword) {
382 if ((Bit32u)dest & 3) {
383 if ( ((Bit32u)dest & 3) == 2 ) {
384 cache_checkinstr(8);
385 cache_addw( STRH_IMM(src_reg, data_reg, 0) ); // strh src_reg, [data_reg]
386 cache_addw( MOV_REG(templo1, src_reg) ); // mov templo1, src_reg
387 cache_addw( LSR_IMM(templo1, templo1, 16) ); // lsr templo1, templo1, #16
388 cache_addw( STRH_IMM(templo1, data_reg, 2) ); // strh templo1, [data_reg, #2]
389 } else {
390 cache_checkinstr(20);
391 cache_addw( STRB_IMM(src_reg, data_reg, 0) ); // strb src_reg, [data_reg]
392 cache_addw( MOV_REG(templo1, src_reg) ); // mov templo1, src_reg
393 cache_addw( LSR_IMM(templo1, templo1, 8) ); // lsr templo1, templo1, #8
394 cache_addw( STRB_IMM(templo1, data_reg, 1) ); // strb templo1, [data_reg, #1]
395 cache_addw( MOV_REG(templo1, src_reg) ); // mov templo1, src_reg
396 cache_addw( LSR_IMM(templo1, templo1, 16) ); // lsr templo1, templo1, #16
397 cache_addw( STRB_IMM(templo1, data_reg, 2) ); // strb templo1, [data_reg, #2]
398 cache_addw( MOV_REG(templo1, src_reg) ); // mov templo1, src_reg
399 cache_addw( LSR_IMM(templo1, templo1, 24) ); // lsr templo1, templo1, #24
400 cache_addw( STRB_IMM(templo1, data_reg, 3) ); // strb templo1, [data_reg, #3]
401 }
402 } else {
403 cache_checkinstr(2);
404 cache_addw( STR_IMM(src_reg, data_reg, 0) ); // str src_reg, [data_reg]
405 }
406 } else {
407 if ((Bit32u)dest & 1) {
408 cache_checkinstr(8);
409 cache_addw( STRB_IMM(src_reg, data_reg, 0) ); // strb src_reg, [data_reg]
410 cache_addw( MOV_REG(templo1, src_reg) ); // mov templo1, src_reg
411 cache_addw( LSR_IMM(templo1, templo1, 8) ); // lsr templo1, templo1, #8
412 cache_addw( STRB_IMM(templo1, data_reg, 1) ); // strb templo1, [data_reg, #1]
413 } else {
414 cache_checkinstr(2);
415 cache_addw( STRH_IMM(src_reg, data_reg, 0) ); // strh src_reg, [data_reg]
416 }
417 }
418 }
419
420 // move 32bit (dword==true) or 16bit (dword==false) of a register into memory
421 static void gen_mov_word_from_reg(HostReg src_reg,void* dest,bool dword) {
422 gen_mov_dword_to_reg_imm(templo2, (Bit32u)dest);
423 gen_mov_word_from_reg_helper(src_reg, dest, dword, templo2);
424 }
425
426 // move an 8bit value from memory into dest_reg
427 // the upper 24bit of the destination register can be destroyed
428 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
429 // registers might not be directly byte-accessible on some architectures
430 static void gen_mov_byte_to_reg_low(HostReg dest_reg,void* data) {
431 gen_mov_dword_to_reg_imm(templo1, (Bit32u)data);
432 cache_checkinstr(2);
433 cache_addw( LDRB_IMM(dest_reg, templo1, 0) ); // ldrb dest_reg, [templo1]
434 }
435
436 // move an 8bit value from memory into dest_reg
437 // the upper 24bit of the destination register can be destroyed
438 // this function can use FC_OP1/FC_OP2 as dest_reg which are
439 // not directly byte-accessible on some architectures
440 static void INLINE gen_mov_byte_to_reg_low_canuseword(HostReg dest_reg,void* data) {
441 gen_mov_byte_to_reg_low(dest_reg, data);
442 }
443
444 // move an 8bit constant value into dest_reg
445 // the upper 24bit of the destination register can be destroyed
446 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
447 // registers might not be directly byte-accessible on some architectures
448 static void gen_mov_byte_to_reg_low_imm(HostReg dest_reg,Bit8u imm) {
449 cache_checkinstr(2);
450 cache_addw( MOV_IMM(dest_reg, imm) ); // mov dest_reg, #(imm)
451 }
452
453 // move an 8bit constant value into dest_reg
454 // the upper 24bit of the destination register can be destroyed
455 // this function can use FC_OP1/FC_OP2 as dest_reg which are
456 // not directly byte-accessible on some architectures
457 static void INLINE gen_mov_byte_to_reg_low_imm_canuseword(HostReg dest_reg,Bit8u imm) {
458 gen_mov_byte_to_reg_low_imm(dest_reg, imm);
459 }
460
461 // move the lowest 8bit of a register into memory
462 static void gen_mov_byte_from_reg_low(HostReg src_reg,void* dest) {
463 gen_mov_dword_to_reg_imm(templo1, (Bit32u)dest);
464 cache_checkinstr(2);
465 cache_addw( STRB_IMM(src_reg, templo1, 0) ); // strb src_reg, [templo1]
466 }
467
468
469
470 // convert an 8bit word to a 32bit dword
471 // the register is zero-extended (sign==false) or sign-extended (sign==true)
472 static void gen_extend_byte(bool sign,HostReg reg) {
473 cache_checkinstr(4);
474 cache_addw( LSL_IMM(reg, reg, 24) ); // lsl reg, reg, #24
475
476 if (sign) {
477 cache_addw( ASR_IMM(reg, reg, 24) ); // asr reg, reg, #24
478 } else {
479 cache_addw( LSR_IMM(reg, reg, 24) ); // lsr reg, reg, #24
480 }
481 }
482
483 // convert a 16bit word to a 32bit dword
484 // the register is zero-extended (sign==false) or sign-extended (sign==true)
485 static void gen_extend_word(bool sign,HostReg reg) {
486 cache_checkinstr(4);
487 cache_addw( LSL_IMM(reg, reg, 16) ); // lsl reg, reg, #16
488
489 if (sign) {
490 cache_addw( ASR_IMM(reg, reg, 16) ); // asr reg, reg, #16
491 } else {
492 cache_addw( LSR_IMM(reg, reg, 16) ); // lsr reg, reg, #16
493 }
494 }
495
496 // add a 32bit value from memory to a full register
497 static void gen_add(HostReg reg,void* op) {
498 gen_mov_word_to_reg(templo3, op, 1);
499 cache_checkinstr(2);
500 cache_addw( ADD_REG(reg, reg, templo3) ); // add reg, reg, templo3
501 }
502
503 // add a 32bit constant value to a full register
504 static void gen_add_imm(HostReg reg,Bit32u imm) {
505 if(!imm) return;
506 gen_mov_dword_to_reg_imm(templo1, imm);
507 cache_checkinstr(2);
508 cache_addw( ADD_REG(reg, reg, templo1) ); // add reg, reg, templo1
509 }
510
511 // and a 32bit constant value with a full register
512 static void gen_and_imm(HostReg reg,Bit32u imm) {
513 if(imm == 0xffffffff) return;
514 gen_mov_dword_to_reg_imm(templo1, imm);
515 cache_checkinstr(2);
516 cache_addw( AND(reg, templo1) ); // and reg, templo1
517 }
518
519
520 // move a 32bit constant value into memory
521 static void gen_mov_direct_dword(void* dest,Bit32u imm) {
522 gen_mov_dword_to_reg_imm(templo3, imm);
523 gen_mov_word_from_reg(templo3, dest, 1);
524 }
525
526 // move an address into memory
527 static void INLINE gen_mov_direct_ptr(void* dest,DRC_PTR_SIZE_IM imm) {
528 gen_mov_direct_dword(dest,(Bit32u)imm);
529 }
530
531 // add an 8bit constant value to a dword memory value
532 static void gen_add_direct_byte(void* dest,Bit8s imm) {
533 if(!imm) return;
534 gen_mov_dword_to_reg_imm(templo2, (Bit32u)dest);
535 gen_mov_word_to_reg_helper(templo3, dest, 1, templo2);
536 cache_checkinstr(2);
537 if (imm >= 0) {
538 cache_addw( ADD_IMM8(templo3, (Bit32s)imm) ); // add templo3, #(imm)
539 } else {
540 cache_addw( SUB_IMM8(templo3, -((Bit32s)imm)) ); // sub templo3, #(-imm)
541 }
542 gen_mov_word_from_reg_helper(templo3, dest, 1, templo2);
543 }
544
545 // add a 32bit (dword==true) or 16bit (dword==false) constant value to a memory value
546 static void gen_add_direct_word(void* dest,Bit32u imm,bool dword) {
547 if(!imm) return;
548 if (dword && ( (imm<128) || (imm>=0xffffff80) ) ) {
549 gen_add_direct_byte(dest,(Bit8s)imm);
550 return;
551 }
552 gen_mov_dword_to_reg_imm(templo2, (Bit32u)dest);
553 gen_mov_word_to_reg_helper(templo3, dest, dword, templo2);
554 if (dword) {
555 gen_mov_dword_to_reg_imm(templo1, imm);
556 } else {
557 gen_mov_word_to_reg_imm(templo1, (Bit16u)imm);
558 }
559 cache_checkinstr(2);
560 cache_addw( ADD_REG(templo3, templo3, templo1) ); // add templo3, templo3, templo1
561 gen_mov_word_from_reg_helper(templo3, dest, dword, templo2);
562 }
563
564 // subtract an 8bit constant value from a dword memory value
565 static void gen_sub_direct_byte(void* dest,Bit8s imm) {
566 if(!imm) return;
567 gen_mov_dword_to_reg_imm(templo2, (Bit32u)dest);
568 gen_mov_word_to_reg_helper(templo3, dest, 1, templo2);
569 cache_checkinstr(2);
570 if (imm >= 0) {
571 cache_addw( SUB_IMM8(templo3, (Bit32s)imm) ); // sub templo3, #(imm)
572 } else {
573 cache_addw( ADD_IMM8(templo3, -((Bit32s)imm)) ); // add templo3, #(-imm)
574 }
575 gen_mov_word_from_reg_helper(templo3, dest, 1, templo2);
576 }
577
578 // subtract a 32bit (dword==true) or 16bit (dword==false) constant value from a memory value
579 static void gen_sub_direct_word(void* dest,Bit32u imm,bool dword) {
580 if(!imm) return;
581 if (dword && ( (imm<128) || (imm>=0xffffff80) ) ) {
582 gen_sub_direct_byte(dest,(Bit8s)imm);
583 return;
584 }
585 gen_mov_dword_to_reg_imm(templo2, (Bit32u)dest);
586 gen_mov_word_to_reg_helper(templo3, dest, dword, templo2);
587 if (dword) {
588 gen_mov_dword_to_reg_imm(templo1, imm);
589 } else {
590 gen_mov_word_to_reg_imm(templo1, (Bit16u)imm);
591 }
592 cache_checkinstr(2);
593 cache_addw( SUB_REG(templo3, templo3, templo1) ); // sub templo3, templo3, templo1
594 gen_mov_word_from_reg_helper(templo3, dest, dword, templo2);
595 }
596
597 // effective address calculation, destination is dest_reg
598 // scale_reg is scaled by scale (scale_reg*(2^scale)) and
599 // added to dest_reg, then the immediate value is added
600 static INLINE void gen_lea(HostReg dest_reg,HostReg scale_reg,Bitu scale,Bits imm) {
601 if (scale) {
602 cache_checkinstr(4);
603 cache_addw( LSL_IMM(templo1, scale_reg, scale) ); // lsl templo1, scale_reg, #(scale)
604 cache_addw( ADD_REG(dest_reg, dest_reg, templo1) ); // add dest_reg, dest_reg, templo1
605 } else {
606 cache_checkinstr(2);
607 cache_addw( ADD_REG(dest_reg, dest_reg, scale_reg) ); // add dest_reg, dest_reg, scale_reg
608 }
609 gen_add_imm(dest_reg, imm);
610 }
611
612 // effective address calculation, destination is dest_reg
613 // dest_reg is scaled by scale (dest_reg*(2^scale)),
614 // then the immediate value is added
615 static INLINE void gen_lea(HostReg dest_reg,Bitu scale,Bits imm) {
616 if (scale) {
617 cache_checkinstr(2);
618 cache_addw( LSL_IMM(dest_reg, dest_reg, scale) ); // lsl dest_reg, dest_reg, #(scale)
619 }
620 gen_add_imm(dest_reg, imm);
621 }
622
623 // helper function for gen_call_function_raw and gen_call_function_setup
624 static void gen_call_function_helper(void * func) {
625 Bit8u *datapos;
626
627 datapos = cache_reservedata();
628 *(Bit32u*)datapos=(Bit32u)func;
629
630 if (((Bit32u)cache.pos & 0x03) == 0) {
631 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 4)) ); // ldr templo1, [pc, datapos]
632 cache_addw( ADD_LO_PC_IMM(templo2, 8) ); // adr templo2, after_call (add templo2, pc, #8)
633 cache_addw( ADD_IMM8(templo2, 1) ); // add templo2, #1
634 cache_addw( MOV_HI_LO(HOST_lr, templo2) ); // mov lr, templo2
635 cache_addw( BX(templo1) ); // bx templo1 --- switch to arm state
636 cache_addw( NOP ); // nop
637 } else {
638 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 2)) ); // ldr templo1, [pc, datapos]
639 cache_addw( ADD_LO_PC_IMM(templo2, 4) ); // adr templo2, after_call (add templo2, pc, #4)
640 cache_addw( ADD_IMM8(templo2, 1) ); // add templo2, #1
641 cache_addw( MOV_HI_LO(HOST_lr, templo2) ); // mov lr, templo2
642 cache_addw( BX(templo1) ); // bx templo1 --- switch to arm state
643 }
644 // after_call:
645
646 // thumb state from now on
647 }
648
649 // generate a call to a parameterless function
650 static void INLINE gen_call_function_raw(void * func) {
651 cache_checkinstr(12);
652 gen_call_function_helper(func);
653 }
654
655 // generate a call to a function with paramcount parameters
656 // note: the parameters are loaded in the architecture specific way
657 // using the gen_load_param_ functions below
658 static Bit32u INLINE gen_call_function_setup(void * func,Bitu paramcount,bool fastcall=false) {
659 cache_checkinstr(12);
660 Bit32u proc_addr = (Bit32u)cache.pos;
661 gen_call_function_helper(func);
662 return proc_addr;
663 // if proc_addr is on word boundary ((proc_addr & 0x03) == 0)
664 // then length of generated code is 12 bytes
665 // otherwise length of generated code is 10 bytes
666 }
667
668 #if (1)
669 // max of 4 parameters in a1-a4
670
671 // load an immediate value as param'th function parameter
672 static void INLINE gen_load_param_imm(Bitu imm,Bitu param) {
673 gen_mov_dword_to_reg_imm(param, imm);
674 }
675
676 // load an address as param'th function parameter
677 static void INLINE gen_load_param_addr(Bitu addr,Bitu param) {
678 gen_mov_dword_to_reg_imm(param, addr);
679 }
680
681 // load a host-register as param'th function parameter
682 static void INLINE gen_load_param_reg(Bitu reg,Bitu param) {
683 gen_mov_regs(param, reg);
684 }
685
686 // load a value from memory as param'th function parameter
687 static void INLINE gen_load_param_mem(Bitu mem,Bitu param) {
688 gen_mov_word_to_reg(param, (void *)mem, 1);
689 }
690 #else
691 other arm abis
692 #endif
693
694 // jump to an address pointed at by ptr, offset is in imm
695 static void gen_jmp_ptr(void * ptr,Bits imm=0) {
696 gen_mov_word_to_reg(templo3, ptr, 1);
697
698 if (imm) {
699 gen_mov_dword_to_reg_imm(templo2, imm);
700 cache_checkinstr(2);
701 cache_addw( ADD_REG(templo3, templo3, templo2) ); // add templo3, templo3, templo2
702 }
703
704 #if (1)
705 // (*ptr) should be word aligned
706 if ((imm & 0x03) == 0) {
707 cache_checkinstr(6);
708 cache_addw( LDR_IMM(templo2, templo3, 0) ); // ldr templo2, [templo3]
709 } else
710 #endif
711 {
712 cache_checkinstr(24);
713 cache_addw( LDRB_IMM(templo2, templo3, 0) ); // ldrb templo2, [templo3]
714 cache_addw( LDRB_IMM(templo1, templo3, 1) ); // ldrb templo1, [templo3, #1]
715 cache_addw( LSL_IMM(templo1, templo1, 8) ); // lsl templo1, templo1, #8
716 cache_addw( ORR(templo2, templo1) ); // orr templo2, templo1
717 cache_addw( LDRB_IMM(templo1, templo3, 2) ); // ldrb templo1, [templo3, #2]
718 cache_addw( LSL_IMM(templo1, templo1, 16) ); // lsl templo1, templo1, #16
719 cache_addw( ORR(templo2, templo1) ); // orr templo2, templo1
720 cache_addw( LDRB_IMM(templo1, templo3, 3) ); // ldrb templo1, [templo3, #3]
721 cache_addw( LSL_IMM(templo1, templo1, 24) ); // lsl templo1, templo1, #24
722 cache_addw( ORR(templo2, templo1) ); // orr templo2, templo1
723 }
724
725 // increase jmp address to keep thumb state
726 cache_addw( ADD_IMM3(templo2, templo2, 1) ); // add templo2, templo2, #1
727
728 cache_addw( BX(templo2) ); // bx templo2
729 }
730
731 // short conditional jump (+-127 bytes) if register is zero
732 // the destination is set by gen_fill_branch() later
733 static Bit32u gen_create_branch_on_zero(HostReg reg,bool dword) {
734 cache_checkinstr(4);
735 if (dword) {
736 cache_addw( CMP_IMM(reg, 0) ); // cmp reg, #0
737 } else {
738 cache_addw( LSL_IMM(templo1, reg, 16) ); // lsl templo1, reg, #16
739 }
740 cache_addw( BEQ_FWD(0) ); // beq j
741 return ((Bit32u)cache.pos-2);
742 }
743
744 // short conditional jump (+-127 bytes) if register is nonzero
745 // the destination is set by gen_fill_branch() later
746 static Bit32u gen_create_branch_on_nonzero(HostReg reg,bool dword) {
747 cache_checkinstr(4);
748 if (dword) {
749 cache_addw( CMP_IMM(reg, 0) ); // cmp reg, #0
750 } else {
751 cache_addw( LSL_IMM(templo1, reg, 16) ); // lsl templo1, reg, #16
752 }
753 cache_addw( BNE_FWD(0) ); // bne j
754 return ((Bit32u)cache.pos-2);
755 }
756
757 // calculate relative offset and fill it into the location pointed to by data
758 static void INLINE gen_fill_branch(DRC_PTR_SIZE_IM data) {
759 #if C_DEBUG
760 Bits len=(Bit32u)cache.pos-(data+4);
761 if (len<0) len=-len;
762 if (len>252) LOG_MSG("Big jump %d",len);
763 #endif
764 *(Bit8u*)data=(Bit8u)( ((Bit32u)cache.pos-(data+4)) >> 1 );
765 }
766
767
768 // conditional jump if register is nonzero
769 // for isdword==true the 32bit of the register are tested
770 // for isdword==false the lowest 8bit of the register are tested
771 static Bit32u gen_create_branch_long_nonzero(HostReg reg,bool isdword) {
772 Bit8u *datapos;
773
774 cache_checkinstr(8);
775 datapos = cache_reservedata();
776
777 if (isdword) {
778 cache_addw( CMP_IMM(reg, 0) ); // cmp reg, #0
779 } else {
780 cache_addw( LSL_IMM(templo2, reg, 24) ); // lsl templo2, reg, #24
781 }
782 cache_addw( BEQ_FWD(2) ); // beq nobranch (pc+2)
783 if (((Bit32u)cache.pos & 0x03) == 0) {
784 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 4)) ); // ldr templo1, [pc, datapos]
785 } else {
786 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 2)) ); // ldr templo1, [pc, datapos]
787 }
788 cache_addw( BX(templo1) ); // bx templo1
789 // nobranch:
790 return ((Bit32u)datapos);
791 }
792
793 // compare 32bit-register against zero and jump if value less/equal than zero
794 static Bit32u gen_create_branch_long_leqzero(HostReg reg) {
795 Bit8u *datapos;
796
797 cache_checkinstr(8);
798 datapos = cache_reservedata();
799
800 cache_addw( CMP_IMM(reg, 0) ); // cmp reg, #0
801 cache_addw( BGT_FWD(2) ); // bgt nobranch (pc+2)
802 if (((Bit32u)cache.pos & 0x03) == 0) {
803 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 4)) ); // ldr templo1, [pc, datapos]
804 } else {
805 cache_addw( LDR_PC_IMM(templo1, datapos - (cache.pos + 2)) ); // ldr templo1, [pc, datapos]
806 }
807 cache_addw( BX(templo1) ); // bx templo1
808 // nobranch:
809 return ((Bit32u)datapos);
810 }
811
812 // calculate long relative offset and fill it into the location pointed to by data
813 static void INLINE gen_fill_branch_long(Bit32u data) {
814 // this is an absolute branch
815 *(Bit32u*)data=((Bit32u)cache.pos) + 1; // add 1 to keep processor in thumb state
816 }
817
818 static void gen_run_code(void) {
819 // switch from arm to thumb state
820 cache_addd(0xe2800000 + (HOST_r3 << 12) + (HOST_pc << 16) + (1)); // add r3, pc, #1
821 cache_addd(0xe12fff10 + (HOST_r3)); // bx r3
822
823 // thumb state from now on
824 cache_addw(0xb500); // push {lr}
825 cache_addw( MOV_LO_HI(HOST_r3, FC_SEGS_ADDR) ); // mov r3, FC_SEGS_ADDR
826 cache_addw( MOV_LO_HI(HOST_r2, FC_REGS_ADDR) ); // mov r2, FC_REGS_ADDR
827 cache_addw(0xb4fc); // push {r2,r3,v1-v4}
828
829 // adr: 16
830 cache_addw( LDR_PC_IMM(HOST_r3, 64 - (16 + 4)) ); // ldr r3, [pc, #(&Segs)]
831 // adr: 18
832 cache_addw( LDR_PC_IMM(HOST_r2, 68 - (18 + 2)) ); // ldr r2, [pc, #(&cpu_regs)]
833 cache_addw( MOV_HI_LO(FC_SEGS_ADDR, HOST_r3) ); // mov FC_SEGS_ADDR, r3
834 cache_addw( MOV_HI_LO(FC_REGS_ADDR, HOST_r2) ); // mov FC_REGS_ADDR, r2
835
836 // align 4
837 cache_addw( ADD_LO_PC_IMM(HOST_r3, 8) ); // add r3, pc, #8
838 cache_addw( ADD_IMM8(HOST_r0, 1) ); // add r0, #1
839 cache_addw( ADD_IMM8(HOST_r3, 1) ); // add r3, #1
840 cache_addw(0xb408); // push {r3}
841 cache_addw( BX(HOST_r0) ); // bx r0
842 cache_addw( NOP ); // nop
843
844 // align 4
845 cache_addw(0xbcfc); // pop {r2,r3,v1-v4}
846 cache_addw( MOV_HI_LO(FC_SEGS_ADDR, HOST_r3) ); // mov FC_SEGS_ADDR, r3
847 cache_addw( MOV_HI_LO(FC_REGS_ADDR, HOST_r2) ); // mov FC_REGS_ADDR, r2
848
849 cache_addw(0xbc08); // pop {r3}
850 cache_addw( BX(HOST_r3) ); // bx r3
851
852 // fill up to 64 bytes
853 cache_addw( NOP ); // nop
854 cache_addd( NOP | (NOP << 16) ); // nop, nop
855 cache_addd( NOP | (NOP << 16) ); // nop, nop
856 cache_addd( NOP | (NOP << 16) ); // nop, nop
857 cache_addd( NOP | (NOP << 16) ); // nop, nop
858
859 // adr: 64
860 cache_addd((Bit32u)&Segs); // address of "Segs"
861 // adr: 68
862 cache_addd((Bit32u)&cpu_regs); // address of "cpu_regs"
863 }
864
865 // return from a function
866 static void gen_return_function(void) {
867 cache_checkinstr(4);
868 cache_addw(0xbc08); // pop {r3}
869 cache_addw( BX(HOST_r3) ); // bx r3
870 }
871
872
873 // short unconditional jump (over data pool)
874 // must emit at most CACHE_DATA_JUMP bytes
875 static void INLINE gen_create_branch_short(void * func) {
876 cache_addw( B_FWD((Bit32u)func - ((Bit32u)cache.pos + 4)) ); // b func
877 }
878
879
880 #ifdef DRC_FLAGS_INVALIDATION
881
882 // called when a call to a function can be replaced by a
883 // call to a simpler function
884 static void gen_fill_function_ptr(Bit8u * pos,void* fct_ptr,Bitu flags_type) {
885 if ((*(Bit16u*)pos & 0xf000) == 0xe000) {
886 if ((*(Bit16u*)pos & 0x0fff) >= ((CACHE_DATA_ALIGN / 2) - 1) &&
887 (*(Bit16u*)pos & 0x0fff) < 0x0800)
888 {
889 pos = (Bit8u *) ( ( ( (Bit32u)(*(Bit16u*)pos & 0x0fff) ) << 1 ) + ((Bit32u)pos + 4) );
890 }
891 }
892
893 #ifdef DRC_FLAGS_INVALIDATION_DCODE
894 if (((Bit32u)pos & 0x03) == 0)
895 {
896 // try to avoid function calls but rather directly fill in code
897 switch (flags_type) {
898 case t_ADDb:
899 case t_ADDw:
900 case t_ADDd:
901 *(Bit16u*)pos=ADD_REG(HOST_a1, HOST_a1, HOST_a2); // add a1, a1, a2
902 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
903 break;
904 case t_ORb:
905 case t_ORw:
906 case t_ORd:
907 *(Bit16u*)pos=ORR(HOST_a1, HOST_a2); // orr a1, a2
908 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
909 break;
910 case t_ANDb:
911 case t_ANDw:
912 case t_ANDd:
913 *(Bit16u*)pos=AND(HOST_a1, HOST_a2); // and a1, a2
914 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
915 break;
916 case t_SUBb:
917 case t_SUBw:
918 case t_SUBd:
919 *(Bit16u*)pos=SUB_REG(HOST_a1, HOST_a1, HOST_a2); // sub a1, a1, a2
920 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
921 break;
922 case t_XORb:
923 case t_XORw:
924 case t_XORd:
925 *(Bit16u*)pos=EOR(HOST_a1, HOST_a2); // eor a1, a2
926 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
927 break;
928 case t_CMPb:
929 case t_CMPw:
930 case t_CMPd:
931 case t_TESTb:
932 case t_TESTw:
933 case t_TESTd:
934 *(Bit16u*)pos=B_FWD(8); // b after_call (pc+8)
935 break;
936 case t_INCb:
937 case t_INCw:
938 case t_INCd:
939 *(Bit16u*)pos=ADD_IMM3(HOST_a1, HOST_a1, 1); // add a1, a1, #1
940 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
941 break;
942 case t_DECb:
943 case t_DECw:
944 case t_DECd:
945 *(Bit16u*)pos=SUB_IMM3(HOST_a1, HOST_a1, 1); // sub a1, a1, #1
946 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
947 break;
948 case t_SHLb:
949 case t_SHLw:
950 case t_SHLd:
951 *(Bit16u*)pos=LSL_REG(HOST_a1, HOST_a2); // lsl a1, a2
952 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
953 break;
954 case t_SHRb:
955 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 24); // lsl a1, a1, #24
956 *(Bit16u*)(pos+2)=NOP; // nop
957 *(Bit16u*)(pos+4)=LSR_IMM(HOST_a1, HOST_a1, 24); // lsr a1, a1, #24
958 *(Bit16u*)(pos+6)=NOP; // nop
959 *(Bit16u*)(pos+8)=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
960 *(Bit16u*)(pos+10)=NOP; // nop
961 break;
962 case t_SHRw:
963 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
964 *(Bit16u*)(pos+2)=NOP; // nop
965 *(Bit16u*)(pos+4)=LSR_IMM(HOST_a1, HOST_a1, 16); // lsr a1, a1, #16
966 *(Bit16u*)(pos+6)=NOP; // nop
967 *(Bit16u*)(pos+8)=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
968 *(Bit16u*)(pos+10)=NOP; // nop
969 break;
970 case t_SHRd:
971 *(Bit16u*)pos=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
972 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
973 break;
974 case t_SARb:
975 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 24); // lsl a1, a1, #24
976 *(Bit16u*)(pos+2)=NOP; // nop
977 *(Bit16u*)(pos+4)=ASR_IMM(HOST_a1, HOST_a1, 24); // asr a1, a1, #24
978 *(Bit16u*)(pos+6)=NOP; // nop
979 *(Bit16u*)(pos+8)=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
980 *(Bit16u*)(pos+10)=NOP; // nop
981 break;
982 case t_SARw:
983 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
984 *(Bit16u*)(pos+2)=NOP; // nop
985 *(Bit16u*)(pos+4)=ASR_IMM(HOST_a1, HOST_a1, 16); // asr a1, a1, #16
986 *(Bit16u*)(pos+6)=NOP; // nop
987 *(Bit16u*)(pos+8)=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
988 *(Bit16u*)(pos+10)=NOP; // nop
989 break;
990 case t_SARd:
991 *(Bit16u*)pos=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
992 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
993 break;
994 case t_RORb:
995 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 24); // lsl a1, a1, #24
996 *(Bit16u*)(pos+2)=LSR_IMM(templo1, HOST_a1, 8); // lsr templo1, a1, #8
997 *(Bit16u*)(pos+4)=ORR(HOST_a1, templo1); // orr a1, templo1
998 *(Bit16u*)(pos+6)=LSR_IMM(templo1, HOST_a1, 16); // lsr templo1, a1, #16
999 *(Bit16u*)(pos+8)=ORR(HOST_a1, templo1); // orr a1, templo1
1000 *(Bit16u*)(pos+10)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1001 break;
1002 case t_RORw:
1003 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
1004 *(Bit16u*)(pos+2)=LSR_IMM(templo1, HOST_a1, 16); // lsr templo1, a1, #16
1005 *(Bit16u*)(pos+4)=NOP; // nop
1006 *(Bit16u*)(pos+6)=ORR(HOST_a1, templo1); // orr a1, templo1
1007 *(Bit16u*)(pos+8)=NOP; // nop
1008 *(Bit16u*)(pos+10)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1009 break;
1010 case t_RORd:
1011 *(Bit16u*)pos=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1012 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
1013 break;
1014 case t_ROLw:
1015 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
1016 *(Bit16u*)(pos+2)=NEG(HOST_a2, HOST_a2); // neg a2, a2
1017 *(Bit16u*)(pos+4)=LSR_IMM(templo1, HOST_a1, 16); // lsr templo1, a1, #16
1018 *(Bit16u*)(pos+6)=ADD_IMM8(HOST_a2, 32); // add a2, #32
1019 *(Bit16u*)(pos+8)=ORR(HOST_a1, templo1); // orr a1, templo1
1020 *(Bit16u*)(pos+10)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1021 break;
1022 case t_ROLd:
1023 *(Bit16u*)pos=NEG(HOST_a2, HOST_a2); // neg a2, a2
1024 *(Bit16u*)(pos+2)=NOP; // nop
1025 *(Bit16u*)(pos+4)=ADD_IMM8(HOST_a2, 32); // add a2, #32
1026 *(Bit16u*)(pos+6)=NOP; // nop
1027 *(Bit16u*)(pos+8)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1028 *(Bit16u*)(pos+10)=NOP; // nop
1029 break;
1030 case t_NEGb:
1031 case t_NEGw:
1032 case t_NEGd:
1033 *(Bit16u*)pos=NEG(HOST_a1, HOST_a1); // neg a1, a1
1034 *(Bit16u*)(pos+2)=B_FWD(6); // b after_call (pc+6)
1035 break;
1036 default:
1037 *(Bit32u*)( ( ((Bit32u) (*pos)) << 2 ) + ((Bit32u)pos + 4) ) = (Bit32u)fct_ptr; // simple_func
1038 break;
1039 }
1040 }
1041 else
1042 {
1043 // try to avoid function calls but rather directly fill in code
1044 switch (flags_type) {
1045 case t_ADDb:
1046 case t_ADDw:
1047 case t_ADDd:
1048 *(Bit16u*)pos=ADD_REG(HOST_a1, HOST_a1, HOST_a2); // add a1, a1, a2
1049 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1050 break;
1051 case t_ORb:
1052 case t_ORw:
1053 case t_ORd:
1054 *(Bit16u*)pos=ORR(HOST_a1, HOST_a2); // orr a1, a2
1055 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1056 break;
1057 case t_ANDb:
1058 case t_ANDw:
1059 case t_ANDd:
1060 *(Bit16u*)pos=AND(HOST_a1, HOST_a2); // and a1, a2
1061 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1062 break;
1063 case t_SUBb:
1064 case t_SUBw:
1065 case t_SUBd:
1066 *(Bit16u*)pos=SUB_REG(HOST_a1, HOST_a1, HOST_a2); // sub a1, a1, a2
1067 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1068 break;
1069 case t_XORb:
1070 case t_XORw:
1071 case t_XORd:
1072 *(Bit16u*)pos=EOR(HOST_a1, HOST_a2); // eor a1, a2
1073 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1074 break;
1075 case t_CMPb:
1076 case t_CMPw:
1077 case t_CMPd:
1078 case t_TESTb:
1079 case t_TESTw:
1080 case t_TESTd:
1081 *(Bit16u*)pos=B_FWD(6); // b after_call (pc+6)
1082 break;
1083 case t_INCb:
1084 case t_INCw:
1085 case t_INCd:
1086 *(Bit16u*)pos=ADD_IMM3(HOST_a1, HOST_a1, 1); // add a1, a1, #1
1087 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1088 break;
1089 case t_DECb:
1090 case t_DECw:
1091 case t_DECd:
1092 *(Bit16u*)pos=SUB_IMM3(HOST_a1, HOST_a1, 1); // sub a1, a1, #1
1093 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1094 break;
1095 case t_SHLb:
1096 case t_SHLw:
1097 case t_SHLd:
1098 *(Bit16u*)pos=LSL_REG(HOST_a1, HOST_a2); // lsl a1, a2
1099 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1100 break;
1101 case t_SHRb:
1102 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 24); // lsl a1, a1, #24
1103 *(Bit16u*)(pos+2)=NOP; // nop
1104 *(Bit16u*)(pos+4)=LSR_IMM(HOST_a1, HOST_a1, 24); // lsr a1, a1, #24
1105 *(Bit16u*)(pos+6)=NOP; // nop
1106 *(Bit16u*)(pos+8)=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
1107 break;
1108 case t_SHRw:
1109 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
1110 *(Bit16u*)(pos+2)=NOP; // nop
1111 *(Bit16u*)(pos+4)=LSR_IMM(HOST_a1, HOST_a1, 16); // lsr a1, a1, #16
1112 *(Bit16u*)(pos+6)=NOP; // nop
1113 *(Bit16u*)(pos+8)=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
1114 break;
1115 case t_SHRd:
1116 *(Bit16u*)pos=LSR_REG(HOST_a1, HOST_a2); // lsr a1, a2
1117 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1118 break;
1119 case t_SARb:
1120 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 24); // lsl a1, a1, #24
1121 *(Bit16u*)(pos+2)=NOP; // nop
1122 *(Bit16u*)(pos+4)=ASR_IMM(HOST_a1, HOST_a1, 24); // asr a1, a1, #24
1123 *(Bit16u*)(pos+6)=NOP; // nop
1124 *(Bit16u*)(pos+8)=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
1125 break;
1126 case t_SARw:
1127 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
1128 *(Bit16u*)(pos+2)=NOP; // nop
1129 *(Bit16u*)(pos+4)=ASR_IMM(HOST_a1, HOST_a1, 16); // asr a1, a1, #16
1130 *(Bit16u*)(pos+6)=NOP; // nop
1131 *(Bit16u*)(pos+8)=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
1132 break;
1133 case t_SARd:
1134 *(Bit16u*)pos=ASR_REG(HOST_a1, HOST_a2); // asr a1, a2
1135 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1136 break;
1137 case t_RORw:
1138 *(Bit16u*)pos=LSL_IMM(HOST_a1, HOST_a1, 16); // lsl a1, a1, #16
1139 *(Bit16u*)(pos+2)=LSR_IMM(templo1, HOST_a1, 16); // lsr templo1, a1, #16
1140 *(Bit16u*)(pos+4)=NOP; // nop
1141 *(Bit16u*)(pos+6)=ORR(HOST_a1, templo1); // orr a1, templo1
1142 *(Bit16u*)(pos+8)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1143 break;
1144 case t_RORd:
1145 *(Bit16u*)pos=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1146 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1147 break;
1148 case t_ROLd:
1149 *(Bit16u*)pos=NEG(HOST_a2, HOST_a2); // neg a2, a2
1150 *(Bit16u*)(pos+2)=NOP; // nop
1151 *(Bit16u*)(pos+4)=ADD_IMM8(HOST_a2, 32); // add a2, #32
1152 *(Bit16u*)(pos+6)=NOP; // nop
1153 *(Bit16u*)(pos+8)=ROR_REG(HOST_a1, HOST_a2); // ror a1, a2
1154 break;
1155 case t_NEGb:
1156 case t_NEGw:
1157 case t_NEGd:
1158 *(Bit16u*)pos=NEG(HOST_a1, HOST_a1); // neg a1, a1
1159 *(Bit16u*)(pos+2)=B_FWD(4); // b after_call (pc+4)
1160 break;
1161 default:
1162 *(Bit32u*)( ( ((Bit32u) (*pos)) << 2 ) + ((Bit32u)pos + 2) ) = (Bit32u)fct_ptr; // simple_func
1163 break;
1164 }
1165
1166 }
1167 #else
1168 if (((Bit32u)pos & 0x03) == 0)
1169 {
1170 *(Bit32u*)( ( ((Bit32u) (*pos)) << 2 ) + ((Bit32u)pos + 4) ) = (Bit32u)fct_ptr; // simple_func
1171 }
1172 else
1173 {
1174 *(Bit32u*)( ( ((Bit32u) (*pos)) << 2 ) + ((Bit32u)pos + 2) ) = (Bit32u)fct_ptr; // simple_func
1175 }
1176 #endif
1177 }
1178 #endif
1179
1180 #ifdef DRC_USE_SEGS_ADDR
1181
1182 // mov 16bit value from Segs[index] into dest_reg using FC_SEGS_ADDR (index modulo 2 must be zero)
1183 // 16bit moves may destroy the upper 16bit of the destination register
1184 static void gen_mov_seg16_to_reg(HostReg dest_reg,Bitu index) {
1185 cache_checkinstr(4);
1186 cache_addw( MOV_LO_HI(templo1, FC_SEGS_ADDR) ); // mov templo1, FC_SEGS_ADDR
1187 cache_addw( LDRH_IMM(dest_reg, templo1, index) ); // ldrh dest_reg, [templo1, #index]
1188 }
1189
1190 // mov 32bit value from Segs[index] into dest_reg using FC_SEGS_ADDR (index modulo 4 must be zero)
1191 static void gen_mov_seg32_to_reg(HostReg dest_reg,Bitu index) {
1192 cache_checkinstr(4);
1193 cache_addw( MOV_LO_HI(templo1, FC_SEGS_ADDR) ); // mov templo1, FC_SEGS_ADDR
1194 cache_addw( LDR_IMM(dest_reg, templo1, index) ); // ldr dest_reg, [templo1, #index]
1195 }
1196
1197 // add a 32bit value from Segs[index] to a full register using FC_SEGS_ADDR (index modulo 4 must be zero)
1198 static void gen_add_seg32_to_reg(HostReg reg,Bitu index) {
1199 cache_checkinstr(6);
1200 cache_addw( MOV_LO_HI(templo1, FC_SEGS_ADDR) ); // mov templo1, FC_SEGS_ADDR
1201 cache_addw( LDR_IMM(templo2, templo1, index) ); // ldr templo2, [templo1, #index]
1202 cache_addw( ADD_REG(reg, reg, templo2) ); // add reg, reg, templo2
1203 }
1204
1205 #endif
1206
1207 #ifdef DRC_USE_REGS_ADDR
1208
1209 // mov 16bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR (index modulo 2 must be zero)
1210 // 16bit moves may destroy the upper 16bit of the destination register
1211 static void gen_mov_regval16_to_reg(HostReg dest_reg,Bitu index) {
1212 cache_checkinstr(4);
1213 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1214 cache_addw( LDRH_IMM(dest_reg, templo2, index) ); // ldrh dest_reg, [templo2, #index]
1215 }
1216
1217 // mov 32bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR (index modulo 4 must be zero)
1218 static void gen_mov_regval32_to_reg(HostReg dest_reg,Bitu index) {
1219 cache_checkinstr(4);
1220 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1221 cache_addw( LDR_IMM(dest_reg, templo2, index) ); // ldr dest_reg, [templo2, #index]
1222 }
1223
1224 // move a 32bit (dword==true) or 16bit (dword==false) value from cpu_regs[index] into dest_reg using FC_REGS_ADDR (if dword==true index modulo 4 must be zero) (if dword==false index modulo 2 must be zero)
1225 // 16bit moves may destroy the upper 16bit of the destination register
1226 static void gen_mov_regword_to_reg(HostReg dest_reg,Bitu index,bool dword) {
1227 cache_checkinstr(4);
1228 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1229 if (dword) {
1230 cache_addw( LDR_IMM(dest_reg, templo2, index) ); // ldr dest_reg, [templo2, #index]
1231 } else {
1232 cache_addw( LDRH_IMM(dest_reg, templo2, index) ); // ldrh dest_reg, [templo2, #index]
1233 }
1234 }
1235
1236 // move an 8bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR
1237 // the upper 24bit of the destination register can be destroyed
1238 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
1239 // registers might not be directly byte-accessible on some architectures
1240 static void gen_mov_regbyte_to_reg_low(HostReg dest_reg,Bitu index) {
1241 cache_checkinstr(4);
1242 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1243 cache_addw( LDRB_IMM(dest_reg, templo2, index) ); // ldrb dest_reg, [templo2, #index]
1244 }
1245
1246 // move an 8bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR
1247 // the upper 24bit of the destination register can be destroyed
1248 // this function can use FC_OP1/FC_OP2 as dest_reg which are
1249 // not directly byte-accessible on some architectures
1250 static void INLINE gen_mov_regbyte_to_reg_low_canuseword(HostReg dest_reg,Bitu index) {
1251 cache_checkinstr(4);
1252 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1253 cache_addw( LDRB_IMM(dest_reg, templo2, index) ); // ldrb dest_reg, [templo2, #index]
1254 }
1255
1256
1257 // add a 32bit value from cpu_regs[index] to a full register using FC_REGS_ADDR (index modulo 4 must be zero)
1258 static void gen_add_regval32_to_reg(HostReg reg,Bitu index) {
1259 cache_checkinstr(6);
1260 cache_addw( MOV_LO_HI(templo2, FC_REGS_ADDR) ); // mov templo2, FC_REGS_ADDR
1261 cache_addw( LDR_IMM(templo1, templo2, index) ); // ldr templo1, [templo2, #index]
1262 cache_addw( ADD_REG(reg, reg, templo1) ); // add reg, reg, templo1
1263 }
1264
1265
1266 // move 16bit of register into cpu_regs[index] using FC_REGS_ADDR (index modulo 2 must be zero)
1267 static void gen_mov_regval16_from_reg(HostReg src_reg,Bitu index) {
1268 cache_checkinstr(4);
1269 cache_addw( MOV_LO_HI(templo1, FC_REGS_ADDR) ); // mov templo1, FC_REGS_ADDR
1270 cache_addw( STRH_IMM(src_reg, templo1, index) ); // strh src_reg, [templo1, #index]
1271 }
1272
1273 // move 32bit of register into cpu_regs[index] using FC_REGS_ADDR (index modulo 4 must be zero)
1274 static void gen_mov_regval32_from_reg(HostReg src_reg,Bitu index) {
1275 cache_checkinstr(4);
1276 cache_addw( MOV_LO_HI(templo1, FC_REGS_ADDR) ); // mov templo1, FC_REGS_ADDR
1277 cache_addw( STR_IMM(src_reg, templo1, index) ); // str src_reg, [templo1, #index]
1278 }
1279
1280 // move 32bit (dword==true) or 16bit (dword==false) of a register into cpu_regs[index] using FC_REGS_ADDR (if dword==true index modulo 4 must be zero) (if dword==false index modulo 2 must be zero)
1281 static void gen_mov_regword_from_reg(HostReg src_reg,Bitu index,bool dword) {
1282 cache_checkinstr(4);
1283 cache_addw( MOV_LO_HI(templo1, FC_REGS_ADDR) ); // mov templo1, FC_REGS_ADDR
1284 if (dword) {
1285 cache_addw( STR_IMM(src_reg, templo1, index) ); // str src_reg, [templo1, #index]
1286 } else {
1287 cache_addw( STRH_IMM(src_reg, templo1, index) ); // strh src_reg, [templo1, #index]
1288 }
1289 }
1290
1291 // move the lowest 8bit of a register into cpu_regs[index] using FC_REGS_ADDR
1292 static void gen_mov_regbyte_from_reg_low(HostReg src_reg,Bitu index) {
1293 cache_checkinstr(4);
1294 cache_addw( MOV_LO_HI(templo1, FC_REGS_ADDR) ); // mov templo1, FC_REGS_ADDR
1295 cache_addw( STRB_IMM(src_reg, templo1, index) ); // strb src_reg, [templo1, #index]
1296 }
1297
1298 #endif