Debian Codegraph @

src/cpu/core_dynrec/risc_armv4le-thumb-iw.h

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