Debian Codegraph @

src/cpu/core_dynrec/risc_armv4le-o3.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-o3.h,v 1.6 2009-06-27 12:51:10 c2woody Exp $ */
20
21
22 /* ARMv4 (little endian) backend by M-HT (size-tweaked arm version) */
23
24
25 // temporary registers
26 #define temp1 HOST_ip
27 #define temp2 HOST_v3
28 #define temp3 HOST_v4
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_v2
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_v2
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 // helper macro
66 #define ROTATE_SCALE(x) ( (x)?(32 - x):(0) )
67
68
69 // instruction encodings
70
71 // move
72 // mov dst, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
73 #define MOV_IMM(dst, imm, rimm) (0xe3a00000 + ((dst) << 12) + (imm) + ((rimm) << 7) )
74 // mov dst, src, lsl #imm
75 #define MOV_REG_LSL_IMM(dst, src, imm) (0xe1a00000 + ((dst) << 12) + (src) + ((imm) << 7) )
76 // movs dst, src, lsl #imm
77 #define MOVS_REG_LSL_IMM(dst, src, imm) (0xe1b00000 + ((dst) << 12) + (src) + ((imm) << 7) )
78 // mov dst, src, lsr #imm
79 #define MOV_REG_LSR_IMM(dst, src, imm) (0xe1a00020 + ((dst) << 12) + (src) + ((imm) << 7) )
80 // mov dst, src, asr #imm
81 #define MOV_REG_ASR_IMM(dst, src, imm) (0xe1a00040 + ((dst) << 12) + (src) + ((imm) << 7) )
82 // mov dst, src, lsl rreg
83 #define MOV_REG_LSL_REG(dst, src, rreg) (0xe1a00010 + ((dst) << 12) + (src) + ((rreg) << 8) )
84 // mov dst, src, lsr rreg
85 #define MOV_REG_LSR_REG(dst, src, rreg) (0xe1a00030 + ((dst) << 12) + (src) + ((rreg) << 8) )
86 // mov dst, src, asr rreg
87 #define MOV_REG_ASR_REG(dst, src, rreg) (0xe1a00050 + ((dst) << 12) + (src) + ((rreg) << 8) )
88 // mov dst, src, ror rreg
89 #define MOV_REG_ROR_REG(dst, src, rreg) (0xe1a00070 + ((dst) << 12) + (src) + ((rreg) << 8) )
90 // mvn dst, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
91 #define MVN_IMM(dst, imm, rimm) (0xe3e00000 + ((dst) << 12) + (imm) + ((rimm) << 7) )
92
93 // arithmetic
94 // add dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
95 #define ADD_IMM(dst, src, imm, rimm) (0xe2800000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
96 // add dst, src1, src2, lsl #imm
97 #define ADD_REG_LSL_IMM(dst, src1, src2, imm) (0xe0800000 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
98 // sub dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
99 #define SUB_IMM(dst, src, imm, rimm) (0xe2400000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
100 // sub dst, src1, src2, lsl #imm
101 #define SUB_REG_LSL_IMM(dst, src1, src2, imm) (0xe0400000 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
102 // rsb dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
103 #define RSB_IMM(dst, src, imm, rimm) (0xe2600000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
104 // cmp src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
105 #define CMP_IMM(src, imm, rimm) (0xe3500000 + ((src) << 16) + (imm) + ((rimm) << 7) )
106 // nop
107 #define NOP MOV_REG_LSL_IMM(HOST_r0, HOST_r0, 0)
108
109 // logical
110 // tst src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
111 #define TST_IMM(src, imm, rimm) (0xe3100000 + ((src) << 16) + (imm) + ((rimm) << 7) )
112 // and dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
113 #define AND_IMM(dst, src, imm, rimm) (0xe2000000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
114 // and dst, src1, src2, lsl #imm
115 #define AND_REG_LSL_IMM(dst, src1, src2, imm) (0xe0000000 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
116 // orr dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
117 #define ORR_IMM(dst, src, imm, rimm) (0xe3800000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
118 // orr dst, src1, src2, lsl #imm
119 #define ORR_REG_LSL_IMM(dst, src1, src2, imm) (0xe1800000 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
120 // orr dst, src1, src2, lsr #imm
121 #define ORR_REG_LSR_IMM(dst, src1, src2, imm) (0xe1800020 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
122 // eor dst, src1, src2, lsl #imm
123 #define EOR_REG_LSL_IMM(dst, src1, src2, imm) (0xe0200000 + ((dst) << 12) + ((src1) << 16) + (src2) + ((imm) << 7) )
124 // bic dst, src, #(imm ror rimm) @ 0 <= imm <= 255 & rimm mod 2 = 0
125 #define BIC_IMM(dst, src, imm, rimm) (0xe3c00000 + ((dst) << 12) + ((src) << 16) + (imm) + ((rimm) << 7) )
126
127 // load
128 // ldr reg, [addr, #imm] @ 0 <= imm < 4096
129 #define LDR_IMM(reg, addr, imm) (0xe5900000 + ((reg) << 12) + ((addr) << 16) + (imm) )
130 // ldrh reg, [addr, #imm] @ 0 <= imm < 256
131 #define LDRH_IMM(reg, addr, imm) (0xe1d000b0 + ((reg) << 12) + ((addr) << 16) + (((imm) & 0xf0) << 4) + ((imm) & 0x0f) )
132 // ldrb reg, [addr, #imm] @ 0 <= imm < 4096
133 #define LDRB_IMM(reg, addr, imm) (0xe5d00000 + ((reg) << 12) + ((addr) << 16) + (imm) )
134
135 // store
136 // str reg, [addr, #imm] @ 0 <= imm < 4096
137 #define STR_IMM(reg, addr, imm) (0xe5800000 + ((reg) << 12) + ((addr) << 16) + (imm) )
138 // strh reg, [addr, #imm] @ 0 <= imm < 256
139 #define STRH_IMM(reg, addr, imm) (0xe1c000b0 + ((reg) << 12) + ((addr) << 16) + (((imm) & 0xf0) << 4) + ((imm) & 0x0f) )
140 // strb reg, [addr, #imm] @ 0 <= imm < 4096
141 #define STRB_IMM(reg, addr, imm) (0xe5c00000 + ((reg) << 12) + ((addr) << 16) + (imm) )
142
143 // branch
144 // beq pc+imm @ 0 <= imm < 32M & imm mod 4 = 0
145 #define BEQ_FWD(imm) (0x0a000000 + ((imm) >> 2) )
146 // bne pc+imm @ 0 <= imm < 32M & imm mod 4 = 0
147 #define BNE_FWD(imm) (0x1a000000 + ((imm) >> 2) )
148 // bgt pc+imm @ 0 <= imm < 32M & imm mod 4 = 0
149 #define BGT_FWD(imm) (0xca000000 + ((imm) >> 2) )
150 // b pc+imm @ 0 <= imm < 32M & imm mod 4 = 0
151 #define B_FWD(imm) (0xea000000 + ((imm) >> 2) )
152 // bx reg
153 #define BX(reg) (0xe12fff10 + (reg) )
154
155
156 // move a full register from reg_src to reg_dst
157 static void gen_mov_regs(HostReg reg_dst,HostReg reg_src) {
158 if(reg_src == reg_dst) return;
159 cache_addd( MOV_REG_LSL_IMM(reg_dst, reg_src, 0) ); // mov reg_dst, reg_src
160 }
161
162 // helper function
163 static Bits get_imm_gen_len(Bit32u imm) {
164 Bits ret;
165 if (imm == 0) {
166 return 1;
167 } else {
168 ret = 0;
169 while (imm) {
170 while ((imm & 3) == 0) {
171 imm>>=2;
172 }
173 ret++;
174 imm>>=8;
175 }
176 return ret;
177 }
178 }
179
180 // helper function
181 static Bits get_method_imm_gen_len(Bit32u imm, Bits preffer00, Bits *num) {
182 Bits num00, num15, numadd, numsub, numret, ret;
183 num00 = get_imm_gen_len(imm);
184 num15 = get_imm_gen_len(~imm);
185 numadd = get_imm_gen_len(imm - ((Bit32u)cache.pos+8));
186 numsub = get_imm_gen_len(((Bit32u)cache.pos+8) - imm);
187 if (numsub < numadd && numsub < num00 && numsub < num15) {
188 ret = 0;
189 numret = numsub;
190 } else if (numadd < num00 && numadd < num15) {
191 ret = 1;
192 numret = numadd;
193 } else if (num00 < num15 || (num00 == num15 && preffer00)) {
194 ret = 2;
195 numret = num00;
196 } else {
197 ret = 3;
198 numret = num15;
199 }
200 if (num != NULL) *num = numret;
201 return ret;
202 }
203
204 // move a 32bit constant value into dest_reg
205 static void gen_mov_dword_to_reg_imm(HostReg dest_reg,Bit32u imm) {
206 Bits first, method, scale;
207 Bit32u imm2, dist;
208 if (imm == 0) {
209 cache_addd( MOV_IMM(dest_reg, 0, 0) ); // mov dest_reg, #0
210 } else if (imm == 0xffffffff) {
211 cache_addd( MVN_IMM(dest_reg, 0, 0) ); // mvn dest_reg, #0
212 } else {
213 method = get_method_imm_gen_len(imm, 1, NULL);
214
215 scale = 0;
216 first = 1;
217 if (method == 0) {
218 dist = ((Bit32u)cache.pos+8) - imm;
219 while (dist) {
220 while ((dist & 3) == 0) {
221 dist>>=2;
222 scale+=2;
223 }
224 if (first) {
225 cache_addd( SUB_IMM(dest_reg, HOST_pc, dist & 0xff, ROTATE_SCALE(scale)) ); // sub dest_reg, pc, #((dist & 0xff) << scale)
226 first = 0;
227 } else {
228 cache_addd( SUB_IMM(dest_reg, dest_reg, dist & 0xff, ROTATE_SCALE(scale)) ); // sub dest_reg, dest_reg, #((dist & 0xff) << scale)
229 }
230 dist>>=8;
231 scale+=8;
232 }
233 } else if (method == 1) {
234 dist = imm - ((Bit32u)cache.pos+8);
235 if (dist == 0) {
236 cache_addd( MOV_REG_LSL_IMM(dest_reg, HOST_pc, 0) ); // mov dest_reg, pc
237 } else {
238 while (dist) {
239 while ((dist & 3) == 0) {
240 dist>>=2;
241 scale+=2;
242 }
243 if (first) {
244 cache_addd( ADD_IMM(dest_reg, HOST_pc, dist & 0xff, ROTATE_SCALE(scale)) ); // add dest_reg, pc, #((dist & 0xff) << scale)
245 first = 0;
246 } else {
247 cache_addd( ADD_IMM(dest_reg, dest_reg, dist & 0xff, ROTATE_SCALE(scale)) ); // add dest_reg, dest_reg, #((dist & 0xff) << scale)
248 }
249 dist>>=8;
250 scale+=8;
251 }
252 }
253 } else if (method == 2) {
254 while (imm) {
255 while ((imm & 3) == 0) {
256 imm>>=2;
257 scale+=2;
258 }
259 if (first) {
260 cache_addd( MOV_IMM(dest_reg, imm & 0xff, ROTATE_SCALE(scale)) ); // mov dest_reg, #((imm & 0xff) << scale)
261 first = 0;
262 } else {
263 cache_addd( ORR_IMM(dest_reg, dest_reg, imm & 0xff, ROTATE_SCALE(scale)) ); // orr dest_reg, dest_reg, #((imm & 0xff) << scale)
264 }
265 imm>>=8;
266 scale+=8;
267 }
268 } else {
269 imm2 = ~imm;
270 while (imm2) {
271 while ((imm2 & 3) == 0) {
272 imm2>>=2;
273 scale+=2;
274 }
275 if (first) {
276 cache_addd( MVN_IMM(dest_reg, imm2 & 0xff, ROTATE_SCALE(scale)) ); // mvn dest_reg, #((imm2 & 0xff) << scale)
277 first = 0;
278 } else {
279 cache_addd( BIC_IMM(dest_reg, dest_reg, imm2 & 0xff, ROTATE_SCALE(scale)) ); // bic dest_reg, dest_reg, #((imm2 & 0xff) << scale)
280 }
281 imm2>>=8;
282 scale+=8;
283 }
284 }
285 }
286 }
287
288 // helper function for gen_mov_word_to_reg
289 static void gen_mov_word_to_reg_helper(HostReg dest_reg,void* data,bool dword,HostReg data_reg) {
290 // alignment....
291 if (dword) {
292 if ((Bit32u)data & 3) {
293 if ( ((Bit32u)data & 3) == 2 ) {
294 cache_addd( LDRH_IMM(dest_reg, data_reg, 0) ); // ldrh dest_reg, [data_reg]
295 cache_addd( LDRH_IMM(temp2, data_reg, 2) ); // ldrh temp2, [data_reg, #2]
296 cache_addd( ORR_REG_LSL_IMM(dest_reg, dest_reg, temp2, 16) ); // orr dest_reg, dest_reg, temp2, lsl #16
297 } else {
298 cache_addd( LDRB_IMM(dest_reg, data_reg, 0) ); // ldrb dest_reg, [data_reg]
299 cache_addd( LDRH_IMM(temp2, data_reg, 1) ); // ldrh temp2, [data_reg, #1]
300 cache_addd( ORR_REG_LSL_IMM(dest_reg, dest_reg, temp2, 8) ); // orr dest_reg, dest_reg, temp2, lsl #8
301 cache_addd( LDRB_IMM(temp2, data_reg, 3) ); // ldrb temp2, [data_reg, #3]
302 cache_addd( ORR_REG_LSL_IMM(dest_reg, dest_reg, temp2, 24) ); // orr dest_reg, dest_reg, temp2, lsl #24
303 }
304 } else {
305 cache_addd( LDR_IMM(dest_reg, data_reg, 0) ); // ldr dest_reg, [data_reg]
306 }
307 } else {
308 if ((Bit32u)data & 1) {
309 cache_addd( LDRB_IMM(dest_reg, data_reg, 0) ); // ldrb dest_reg, [data_reg]
310 cache_addd( LDRB_IMM(temp2, data_reg, 1) ); // ldrb temp2, [data_reg, #1]
311 cache_addd( ORR_REG_LSL_IMM(dest_reg, dest_reg, temp2, 8) ); // orr dest_reg, dest_reg, temp2, lsl #8
312 } else {
313 cache_addd( LDRH_IMM(dest_reg, data_reg, 0) ); // ldrh dest_reg, [data_reg]
314 }
315 }
316 }
317
318 // move a 32bit (dword==true) or 16bit (dword==false) value from memory into dest_reg
319 // 16bit moves may destroy the upper 16bit of the destination register
320 static void gen_mov_word_to_reg(HostReg dest_reg,void* data,bool dword) {
321 gen_mov_dword_to_reg_imm(temp1, (Bit32u)data);
322 gen_mov_word_to_reg_helper(dest_reg, data, dword, temp1);
323 }
324
325 // move a 16bit constant value into dest_reg
326 // the upper 16bit of the destination register may be destroyed
327 static void gen_mov_word_to_reg_imm(HostReg dest_reg,Bit16u imm) {
328 Bits first, scale;
329 Bit32u imm2;
330 if (imm == 0) {
331 cache_addd( MOV_IMM(dest_reg, 0, 0) ); // mov dest_reg, #0
332 } else {
333 scale = 0;
334 first = 1;
335 imm2 = (Bit32u)imm;
336 while (imm2) {
337 while ((imm2 & 3) == 0) {
338 imm2>>=2;
339 scale+=2;
340 }
341 if (first) {
342 cache_addd( MOV_IMM(dest_reg, imm2 & 0xff, ROTATE_SCALE(scale)) ); // mov dest_reg, #((imm2 & 0xff) << scale)
343 first = 0;
344 } else {
345 cache_addd( ORR_IMM(dest_reg, dest_reg, imm2 & 0xff, ROTATE_SCALE(scale)) ); // orr dest_reg, dest_reg, #((imm2 & 0xff) << scale)
346 }
347 imm2>>=8;
348 scale+=8;
349 }
350 }
351 }
352
353 // helper function for gen_mov_word_from_reg
354 static void gen_mov_word_from_reg_helper(HostReg src_reg,void* dest,bool dword, HostReg data_reg) {
355 // alignment....
356 if (dword) {
357 if ((Bit32u)dest & 3) {
358 if ( ((Bit32u)dest & 3) == 2 ) {
359 cache_addd( STRH_IMM(src_reg, data_reg, 0) ); // strh src_reg, [data_reg]
360 cache_addd( MOV_REG_LSR_IMM(temp2, src_reg, 16) ); // mov temp2, src_reg, lsr #16
361 cache_addd( STRH_IMM(temp2, data_reg, 2) ); // strh temp2, [data_reg, #2]
362 } else {
363 cache_addd( STRB_IMM(src_reg, data_reg, 0) ); // strb src_reg, [data_reg]
364 cache_addd( MOV_REG_LSR_IMM(temp2, src_reg, 8) ); // mov temp2, src_reg, lsr #8
365 cache_addd( STRH_IMM(temp2, data_reg, 1) ); // strh temp2, [data_reg, #1]
366 cache_addd( MOV_REG_LSR_IMM(temp2, temp2, 16) ); // mov temp2, temp2, lsr #16
367 cache_addd( STRB_IMM(temp2, data_reg, 3) ); // strb temp2, [data_reg, #3]
368 }
369 } else {
370 cache_addd( STR_IMM(src_reg, data_reg, 0) ); // str src_reg, [data_reg]
371 }
372 } else {
373 if ((Bit32u)dest & 1) {
374 cache_addd( STRB_IMM(src_reg, data_reg, 0) ); // strb src_reg, [data_reg]
375 cache_addd( MOV_REG_LSR_IMM(temp2, src_reg, 8) ); // mov temp2, src_reg, lsr #8
376 cache_addd( STRB_IMM(temp2, data_reg, 1) ); // strb temp2, [data_reg, #1]
377 } else {
378 cache_addd( STRH_IMM(src_reg, data_reg, 0) ); // strh src_reg, [data_reg]
379 }
380 }
381 }
382
383 // move 32bit (dword==true) or 16bit (dword==false) of a register into memory
384 static void gen_mov_word_from_reg(HostReg src_reg,void* dest,bool dword) {
385 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
386 gen_mov_word_from_reg_helper(src_reg, dest, dword, temp1);
387 }
388
389 // move an 8bit value from memory into dest_reg
390 // the upper 24bit of the destination register can be destroyed
391 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
392 // registers might not be directly byte-accessible on some architectures
393 static void gen_mov_byte_to_reg_low(HostReg dest_reg,void* data) {
394 gen_mov_dword_to_reg_imm(temp1, (Bit32u)data);
395 cache_addd( LDRB_IMM(dest_reg, temp1, 0) ); // ldrb dest_reg, [temp1]
396 }
397
398 // move an 8bit value from memory into dest_reg
399 // the upper 24bit of the destination register can be destroyed
400 // this function can use FC_OP1/FC_OP2 as dest_reg which are
401 // not directly byte-accessible on some architectures
402 static void INLINE gen_mov_byte_to_reg_low_canuseword(HostReg dest_reg,void* data) {
403 gen_mov_byte_to_reg_low(dest_reg, data);
404 }
405
406 // move an 8bit constant value into dest_reg
407 // the upper 24bit of the destination register can be destroyed
408 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
409 // registers might not be directly byte-accessible on some architectures
410 static void gen_mov_byte_to_reg_low_imm(HostReg dest_reg,Bit8u imm) {
411 cache_addd( MOV_IMM(dest_reg, imm, 0) ); // mov dest_reg, #(imm)
412 }
413
414 // move an 8bit constant value into dest_reg
415 // the upper 24bit of the destination register can be destroyed
416 // this function can use FC_OP1/FC_OP2 as dest_reg which are
417 // not directly byte-accessible on some architectures
418 static void INLINE gen_mov_byte_to_reg_low_imm_canuseword(HostReg dest_reg,Bit8u imm) {
419 gen_mov_byte_to_reg_low_imm(dest_reg, imm);
420 }
421
422 // move the lowest 8bit of a register into memory
423 static void gen_mov_byte_from_reg_low(HostReg src_reg,void* dest) {
424 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
425 cache_addd( STRB_IMM(src_reg, temp1, 0) ); // strb src_reg, [temp1]
426 }
427
428
429
430 // convert an 8bit word to a 32bit dword
431 // the register is zero-extended (sign==false) or sign-extended (sign==true)
432 static void gen_extend_byte(bool sign,HostReg reg) {
433 if (sign) {
434 cache_addd( MOV_REG_LSL_IMM(reg, reg, 24) ); // mov reg, reg, lsl #24
435 cache_addd( MOV_REG_ASR_IMM(reg, reg, 24) ); // mov reg, reg, asr #24
436 } else {
437 cache_addd( AND_IMM(reg, reg, 0xff, 0) ); // and reg, reg, #0xff
438 }
439 }
440
441 // convert a 16bit word to a 32bit dword
442 // the register is zero-extended (sign==false) or sign-extended (sign==true)
443 static void gen_extend_word(bool sign,HostReg reg) {
444 if (sign) {
445 cache_addd( MOV_REG_LSL_IMM(reg, reg, 16) ); // mov reg, reg, lsl #16
446 cache_addd( MOV_REG_ASR_IMM(reg, reg, 16) ); // mov reg, reg, asr #16
447 } else {
448 cache_addd( MOV_REG_LSL_IMM(reg, reg, 16) ); // mov reg, reg, lsl #16
449 cache_addd( MOV_REG_LSR_IMM(reg, reg, 16) ); // mov reg, reg, lsr #16
450 }
451 }
452
453 // add a 32bit value from memory to a full register
454 static void gen_add(HostReg reg,void* op) {
455 gen_mov_word_to_reg(temp3, op, 1);
456 cache_addd( ADD_REG_LSL_IMM(reg, reg, temp3, 0) ); // add reg, reg, temp3
457 }
458
459 // add a 32bit constant value to a full register
460 static void gen_add_imm(HostReg reg,Bit32u imm) {
461 Bits method1, method2, num1, num2, scale, sub;
462 if(!imm) return;
463 if (imm == 1) {
464 cache_addd( ADD_IMM(reg, reg, 1, 0) ); // add reg, reg, #1
465 } else if (imm == 0xffffffff) {
466 cache_addd( SUB_IMM(reg, reg, 1, 0) ); // sub reg, reg, #1
467 } else {
468 method1 = get_method_imm_gen_len(imm, 1, &num1);
469 method2 = get_method_imm_gen_len(-((Bit32s)imm), 1, &num2);
470 if (num2 < num1) {
471 method1 = method2;
472 imm = (Bit32u)(-((Bit32s)imm));
473 sub = 1;
474 } else sub = 0;
475
476 if (method1 != 2) {
477 gen_mov_dword_to_reg_imm(temp3, imm);
478 if (sub) {
479 cache_addd( SUB_REG_LSL_IMM(reg, reg, temp3, 0) ); // sub reg, reg, temp3
480 } else {
481 cache_addd( ADD_REG_LSL_IMM(reg, reg, temp3, 0) ); // add reg, reg, temp3
482 }
483 } else {
484 scale = 0;
485 while (imm) {
486 while ((imm & 3) == 0) {
487 imm>>=2;
488 scale+=2;
489 }
490 if (sub) {
491 cache_addd( SUB_IMM(reg, reg, imm & 0xff, ROTATE_SCALE(scale)) ); // sub reg, reg, #((imm & 0xff) << scale)
492 } else {
493 cache_addd( ADD_IMM(reg, reg, imm & 0xff, ROTATE_SCALE(scale)) ); // add reg, reg, #((imm & 0xff) << scale)
494 }
495 imm>>=8;
496 scale+=8;
497 }
498 }
499 }
500 }
501
502 // and a 32bit constant value with a full register
503 static void gen_and_imm(HostReg reg,Bit32u imm) {
504 Bits method, scale;
505 Bit32u imm2;
506 imm2 = ~imm;
507 if(!imm2) return;
508 if (!imm) {
509 cache_addd( MOV_IMM(reg, 0, 0) ); // mov reg, #0
510 } else {
511 method = get_method_imm_gen_len(imm, 0, NULL);
512 if (method != 3) {
513 gen_mov_dword_to_reg_imm(temp3, imm);
514 cache_addd( AND_REG_LSL_IMM(reg, reg, temp3, 0) ); // and reg, reg, temp3
515 } else {
516 scale = 0;
517 while (imm2) {
518 while ((imm2 & 3) == 0) {
519 imm2>>=2;
520 scale+=2;
521 }
522 cache_addd( BIC_IMM(reg, reg, imm2 & 0xff, ROTATE_SCALE(scale)) ); // bic reg, reg, #((imm2 & 0xff) << scale)
523 imm2>>=8;
524 scale+=8;
525 }
526 }
527 }
528 }
529
530
531 // move a 32bit constant value into memory
532 static void gen_mov_direct_dword(void* dest,Bit32u imm) {
533 gen_mov_dword_to_reg_imm(temp3, imm);
534 gen_mov_word_from_reg(temp3, dest, 1);
535 }
536
537 // move an address into memory
538 static void INLINE gen_mov_direct_ptr(void* dest,DRC_PTR_SIZE_IM imm) {
539 gen_mov_direct_dword(dest,(Bit32u)imm);
540 }
541
542 // add an 8bit constant value to a dword memory value
543 static void gen_add_direct_byte(void* dest,Bit8s imm) {
544 if(!imm) return;
545 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
546 gen_mov_word_to_reg_helper(temp3, dest, 1, temp1);
547 if (imm >= 0) {
548 cache_addd( ADD_IMM(temp3, temp3, (Bit32s)imm, 0) ); // add temp3, temp3, #(imm)
549 } else {
550 cache_addd( SUB_IMM(temp3, temp3, -((Bit32s)imm), 0) ); // sub temp3, temp3, #(-imm)
551 }
552 gen_mov_word_from_reg_helper(temp3, dest, 1, temp1);
553 }
554
555 // add a 32bit (dword==true) or 16bit (dword==false) constant value to a memory value
556 static void gen_add_direct_word(void* dest,Bit32u imm,bool dword) {
557 if(!imm) return;
558 if (dword && ( (imm<128) || (imm>=0xffffff80) ) ) {
559 gen_add_direct_byte(dest,(Bit8s)imm);
560 return;
561 }
562 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
563 gen_mov_word_to_reg_helper(temp3, dest, dword, temp1);
564 // maybe use function gen_add_imm
565 if (dword) {
566 gen_mov_dword_to_reg_imm(temp2, imm);
567 } else {
568 gen_mov_word_to_reg_imm(temp2, (Bit16u)imm);
569 }
570 cache_addd( ADD_REG_LSL_IMM(temp3, temp3, temp2, 0) ); // add temp3, temp3, temp2
571 gen_mov_word_from_reg_helper(temp3, dest, dword, temp1);
572 }
573
574 // subtract an 8bit constant value from a dword memory value
575 static void gen_sub_direct_byte(void* dest,Bit8s imm) {
576 if(!imm) return;
577 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
578 gen_mov_word_to_reg_helper(temp3, dest, 1, temp1);
579 if (imm >= 0) {
580 cache_addd( SUB_IMM(temp3, temp3, (Bit32s)imm, 0) ); // sub temp3, temp3, #(imm)
581 } else {
582 cache_addd( ADD_IMM(temp3, temp3, -((Bit32s)imm), 0) ); // add temp3, temp3, #(-imm)
583 }
584 gen_mov_word_from_reg_helper(temp3, dest, 1, temp1);
585 }
586
587 // subtract a 32bit (dword==true) or 16bit (dword==false) constant value from a memory value
588 static void gen_sub_direct_word(void* dest,Bit32u imm,bool dword) {
589 if(!imm) return;
590 if (dword && ( (imm<128) || (imm>=0xffffff80) ) ) {
591 gen_sub_direct_byte(dest,(Bit8s)imm);
592 return;
593 }
594 gen_mov_dword_to_reg_imm(temp1, (Bit32u)dest);
595 gen_mov_word_to_reg_helper(temp3, dest, dword, temp1);
596 // maybe use function gen_add_imm/gen_sub_imm
597 if (dword) {
598 gen_mov_dword_to_reg_imm(temp2, imm);
599 } else {
600 gen_mov_word_to_reg_imm(temp2, (Bit16u)imm);
601 }
602 cache_addd( SUB_REG_LSL_IMM(temp3, temp3, temp2, 0) ); // sub temp3, temp3, temp2
603 gen_mov_word_from_reg_helper(temp3, dest, dword, temp1);
604 }
605
606 // effective address calculation, destination is dest_reg
607 // scale_reg is scaled by scale (scale_reg*(2^scale)) and
608 // added to dest_reg, then the immediate value is added
609 static INLINE void gen_lea(HostReg dest_reg,HostReg scale_reg,Bitu scale,Bits imm) {
610 cache_addd( ADD_REG_LSL_IMM(dest_reg, dest_reg, scale_reg, scale) ); // add dest_reg, dest_reg, scale_reg, lsl #(scale)
611 gen_add_imm(dest_reg, imm);
612 }
613
614 // effective address calculation, destination is dest_reg
615 // dest_reg is scaled by scale (dest_reg*(2^scale)),
616 // then the immediate value is added
617 static INLINE void gen_lea(HostReg dest_reg,Bitu scale,Bits imm) {
618 if (scale) {
619 cache_addd( MOV_REG_LSL_IMM(dest_reg, dest_reg, scale) ); // mov dest_reg, dest_reg, lsl #(scale)
620 }
621 gen_add_imm(dest_reg, imm);
622 }
623
624 // generate a call to a parameterless function
625 static void INLINE gen_call_function_raw(void * func) {
626 cache_addd( LDR_IMM(temp1, HOST_pc, 4) ); // ldr temp1, [pc, #4]
627 cache_addd( ADD_IMM(HOST_lr, HOST_pc, 4, 0) ); // add lr, pc, #4
628 cache_addd( BX(temp1) ); // bx temp1
629 cache_addd((Bit32u)func); // .int func
630 }
631
632 // generate a call to a function with paramcount parameters
633 // note: the parameters are loaded in the architecture specific way
634 // using the gen_load_param_ functions below
635 static Bit32u INLINE gen_call_function_setup(void * func,Bitu paramcount,bool fastcall=false) {
636 Bit32u proc_addr = (Bit32u)cache.pos;
637 gen_call_function_raw(func);
638 return proc_addr;
639 }
640
641 #if (1)
642 // max of 4 parameters in a1-a4
643
644 // load an immediate value as param'th function parameter
645 static void INLINE gen_load_param_imm(Bitu imm,Bitu param) {
646 gen_mov_dword_to_reg_imm(param, imm);
647 }
648
649 // load an address as param'th function parameter
650 static void INLINE gen_load_param_addr(Bitu addr,Bitu param) {
651 gen_mov_dword_to_reg_imm(param, addr);
652 }
653
654 // load a host-register as param'th function parameter
655 static void INLINE gen_load_param_reg(Bitu reg,Bitu param) {
656 gen_mov_regs(param, reg);
657 }
658
659 // load a value from memory as param'th function parameter
660 static void INLINE gen_load_param_mem(Bitu mem,Bitu param) {
661 gen_mov_word_to_reg(param, (void *)mem, 1);
662 }
663 #else
664 other arm abis
665 #endif
666
667 // jump to an address pointed at by ptr, offset is in imm
668 static void gen_jmp_ptr(void * ptr,Bits imm=0) {
669 Bits num1, num2, scale, sub;
670 Bitu imm2;
671 gen_mov_word_to_reg(temp3, ptr, 1);
672
673 if (imm) {
674 num1 = get_imm_gen_len(imm);
675 num2 = get_imm_gen_len(-imm);
676
677 if (num2 < num1) {
678 imm = -imm;
679 sub = 1;
680 } else sub = 0;
681
682 scale = 0;
683 imm2 = (Bitu)imm;
684 while (imm2) {
685 while ((imm2 & 3) == 0) {
686 imm2>>=2;
687 scale+=2;
688 }
689 if (sub) {
690 cache_addd( SUB_IMM(temp3, temp3, imm2 & 0xff, ROTATE_SCALE(scale)) ); // sub temp3, temp3, #((imm2 & 0xff) << scale)
691 } else {
692 cache_addd( ADD_IMM(temp3, temp3, imm2 & 0xff, ROTATE_SCALE(scale)) ); // add temp3, temp3, #((imm2 & 0xff) << scale)
693 }
694 imm2>>=8;
695 scale+=8;
696 }
697 }
698
699 #if (1)
700 // (*ptr) should be word aligned
701 if ((imm & 0x03) == 0) {
702 cache_addd( LDR_IMM(temp1, temp3, 0) ); // ldr temp1, [temp3]
703 } else
704 #endif
705 {
706 cache_addd( LDRB_IMM(temp1, temp3, 0) ); // ldrb temp1, [temp3]
707 cache_addd( LDRB_IMM(temp2, temp3, 1) ); // ldrb temp2, [temp3, #1]
708 cache_addd( ORR_REG_LSL_IMM(temp1, temp1, temp2, 8) ); // orr temp1, temp1, temp2, lsl #8
709 cache_addd( LDRB_IMM(temp2, temp3, 2) ); // ldrb temp2, [temp3, #2]
710 cache_addd( ORR_REG_LSL_IMM(temp1, temp1, temp2, 16) ); // orr temp1, temp1, temp2, lsl #16
711 cache_addd( LDRB_IMM(temp2, temp3, 3) ); // ldrb temp2, [temp3, #3]
712 cache_addd( ORR_REG_LSL_IMM(temp1, temp1, temp2, 24) ); // orr temp1, temp1, temp2, lsl #24
713 }
714
715 cache_addd( BX(temp1) ); // bx temp1
716 }
717
718 // short conditional jump (+-127 bytes) if register is zero
719 // the destination is set by gen_fill_branch() later
720 static Bit32u gen_create_branch_on_zero(HostReg reg,bool dword) {
721 if (dword) {
722 cache_addd( CMP_IMM(reg, 0, 0) ); // cmp reg, #0
723 } else {
724 cache_addd( MOVS_REG_LSL_IMM(temp1, reg, 16) ); // movs temp1, reg, lsl #16
725 }
726 cache_addd( BEQ_FWD(0) ); // beq j
727 return ((Bit32u)cache.pos-4);
728 }
729
730 // short conditional jump (+-127 bytes) if register is nonzero
731 // the destination is set by gen_fill_branch() later
732 static Bit32u gen_create_branch_on_nonzero(HostReg reg,bool dword) {
733 if (dword) {
734 cache_addd( CMP_IMM(reg, 0, 0) ); // cmp reg, #0
735 } else {
736 cache_addd( MOVS_REG_LSL_IMM(temp1, reg, 16) ); // movs temp1, reg, lsl #16
737 }
738 cache_addd( BNE_FWD(0) ); // bne j
739 return ((Bit32u)cache.pos-4);
740 }
741
742 // calculate relative offset and fill it into the location pointed to by data
743 static void INLINE gen_fill_branch(DRC_PTR_SIZE_IM data) {
744 #if C_DEBUG
745 Bits len=(Bit32u)cache.pos-(data+8);
746 if (len<0) len=-len;
747 if (len>0x02000000) LOG_MSG("Big jump %d",len);
748 #endif
749 *(Bit32u*)data=( (*(Bit32u*)data) & 0xff000000 ) | ( ( ((Bit32u)cache.pos - (data+8)) >> 2 ) & 0x00ffffff );
750 }
751
752 // conditional jump if register is nonzero
753 // for isdword==true the 32bit of the register are tested
754 // for isdword==false the lowest 8bit of the register are tested
755 static Bit32u gen_create_branch_long_nonzero(HostReg reg,bool isdword) {
756 if (isdword) {
757 cache_addd( CMP_IMM(reg, 0, 0) ); // cmp reg, #0
758 } else {
759 cache_addd( TST_IMM(reg, 0xff, 0) ); // tst reg, #0xff
760 }
761 cache_addd( BEQ_FWD(8) ); // beq nobranch (pc +8)
762 cache_addd( LDR_IMM(temp1, HOST_pc, 0) ); // ldr temp1, [pc, #0]
763 cache_addd( BX(temp1) ); // bx temp1
764 cache_addd(0); // fill j
765 // nobranch:
766 return ((Bit32u)cache.pos-4);
767 }
768
769 // compare 32bit-register against zero and jump if value less/equal than zero
770 static Bit32u gen_create_branch_long_leqzero(HostReg reg) {
771 cache_addd( CMP_IMM(reg, 0, 0) ); // cmp reg, #0
772 cache_addd( BGT_FWD(8) ); // bgt nobranch (pc+8)
773 cache_addd( LDR_IMM(temp1, HOST_pc, 0) ); // ldr temp1, [pc, #0]
774 cache_addd( BX(temp1) ); // bx temp1
775 cache_addd(0); // fill j
776 // nobranch:
777 return ((Bit32u)cache.pos-4);
778 }
779
780 // calculate long relative offset and fill it into the location pointed to by data
781 static void INLINE gen_fill_branch_long(Bit32u data) {
782 // this is an absolute branch
783 *(Bit32u*)data=(Bit32u)cache.pos;
784 }
785
786 static void gen_run_code(void) {
787 cache_addd(0xe92d4000); // stmfd sp!, {lr}
788 cache_addd(0xe92d0cf0); // stmfd sp!, {v1-v4,v7,v8}
789
790 // adr: 8
791 cache_addd( LDR_IMM(FC_SEGS_ADDR, HOST_pc, 64 - (8 + 8)) ); // ldr FC_SEGS_ADDR, [pc, #(&Segs)]
792 // adr: 12
793 cache_addd( LDR_IMM(FC_REGS_ADDR, HOST_pc, 68 - (12 + 8)) ); // ldr FC_REGS_ADDR, [pc, #(&cpu_regs)]
794
795 cache_addd( ADD_IMM(HOST_lr, HOST_pc, 4, 0) ); // add lr, pc, #4
796 cache_addd(0xe92d4000); // stmfd sp!, {lr}
797 cache_addd( BX(HOST_r0) ); // bx r0
798
799 cache_addd(0xe8bd0cf0); // ldmfd sp!, {v1-v4,v7,v8}
800
801 cache_addd(0xe8bd4000); // ldmfd sp!, {lr}
802 cache_addd( BX(HOST_lr) ); // bx lr
803
804 // fill up to 64 bytes
805 cache_addd( NOP ); // nop
806 cache_addd( NOP ); // nop
807 cache_addd( NOP ); // nop
808 cache_addd( NOP ); // nop
809 cache_addd( NOP ); // nop
810 cache_addd( NOP ); // nop
811
812 // adr: 64
813 cache_addd((Bit32u)&Segs); // address of "Segs"
814 // adr: 68
815 cache_addd((Bit32u)&cpu_regs); // address of "cpu_regs"
816 }
817
818 // return from a function
819 static void gen_return_function(void) {
820 cache_addd(0xe8bd4000); // ldmfd sp!, {lr}
821 cache_addd( BX(HOST_lr) ); // bx lr
822 }
823
824 #ifdef DRC_FLAGS_INVALIDATION
825
826 // called when a call to a function can be replaced by a
827 // call to a simpler function
828 static void gen_fill_function_ptr(Bit8u * pos,void* fct_ptr,Bitu flags_type) {
829 #ifdef DRC_FLAGS_INVALIDATION_DCODE
830 // try to avoid function calls but rather directly fill in code
831 switch (flags_type) {
832 case t_ADDb:
833 case t_ADDw:
834 case t_ADDd:
835 *(Bit32u*)pos=ADD_REG_LSL_IMM(FC_RETOP, HOST_a1, HOST_a2, 0); // add FC_RETOP, a1, a2
836 *(Bit32u*)(pos+4)=NOP; // nop
837 *(Bit32u*)(pos+8)=NOP; // nop
838 *(Bit32u*)(pos+12)=NOP; // nop
839 break;
840 case t_ORb:
841 case t_ORw:
842 case t_ORd:
843 *(Bit32u*)pos=ORR_REG_LSL_IMM(FC_RETOP, HOST_a1, HOST_a2, 0); // orr FC_RETOP, a1, a2
844 *(Bit32u*)(pos+4)=NOP; // nop
845 *(Bit32u*)(pos+8)=NOP; // nop
846 *(Bit32u*)(pos+12)=NOP; // nop
847 break;
848 case t_ANDb:
849 case t_ANDw:
850 case t_ANDd:
851 *(Bit32u*)pos=AND_REG_LSL_IMM(FC_RETOP, HOST_a1, HOST_a2, 0); // and FC_RETOP, a1, a2
852 *(Bit32u*)(pos+4)=NOP; // nop
853 *(Bit32u*)(pos+8)=NOP; // nop
854 *(Bit32u*)(pos+12)=NOP; // nop
855 break;
856 case t_SUBb:
857 case t_SUBw:
858 case t_SUBd:
859 *(Bit32u*)pos=SUB_REG_LSL_IMM(FC_RETOP, HOST_a1, HOST_a2, 0); // sub FC_RETOP, a1, a2
860 *(Bit32u*)(pos+4)=NOP; // nop
861 *(Bit32u*)(pos+8)=NOP; // nop
862 *(Bit32u*)(pos+12)=NOP; // nop
863 break;
864 case t_XORb:
865 case t_XORw:
866 case t_XORd:
867 *(Bit32u*)pos=EOR_REG_LSL_IMM(FC_RETOP, HOST_a1, HOST_a2, 0); // eor FC_RETOP, a1, a2
868 *(Bit32u*)(pos+4)=NOP; // nop
869 *(Bit32u*)(pos+8)=NOP; // nop
870 *(Bit32u*)(pos+12)=NOP; // nop
871 break;
872 case t_CMPb:
873 case t_CMPw:
874 case t_CMPd:
875 case t_TESTb:
876 case t_TESTw:
877 case t_TESTd:
878 *(Bit32u*)pos=B_FWD(8); // b (pc+2*4)
879 break;
880 case t_INCb:
881 case t_INCw:
882 case t_INCd:
883 *(Bit32u*)pos=ADD_IMM(FC_RETOP, HOST_a1, 1, 0); // add FC_RETOP, a1, #1
884 *(Bit32u*)(pos+4)=NOP; // nop
885 *(Bit32u*)(pos+8)=NOP; // nop
886 *(Bit32u*)(pos+12)=NOP; // nop
887 break;
888 case t_DECb:
889 case t_DECw:
890 case t_DECd:
891 *(Bit32u*)pos=SUB_IMM(FC_RETOP, HOST_a1, 1, 0); // sub FC_RETOP, a1, #1
892 *(Bit32u*)(pos+4)=NOP; // nop
893 *(Bit32u*)(pos+8)=NOP; // nop
894 *(Bit32u*)(pos+12)=NOP; // nop
895 break;
896 case t_SHLb:
897 case t_SHLw:
898 case t_SHLd:
899 *(Bit32u*)pos=MOV_REG_LSL_REG(FC_RETOP, HOST_a1, HOST_a2); // mov FC_RETOP, a1, lsl a2
900 *(Bit32u*)(pos+4)=NOP; // nop
901 *(Bit32u*)(pos+8)=NOP; // nop
902 *(Bit32u*)(pos+12)=NOP; // nop
903 break;
904 case t_SHRb:
905 *(Bit32u*)pos=AND_IMM(FC_RETOP, HOST_a1, 0xff, 0); // and FC_RETOP, a1, #0xff
906 *(Bit32u*)(pos+4)=MOV_REG_LSR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, lsr a2
907 *(Bit32u*)(pos+8)=NOP; // nop
908 *(Bit32u*)(pos+12)=NOP; // nop
909 break;
910 case t_SHRw:
911 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 16); // mov FC_RETOP, a1, lsl #16
912 *(Bit32u*)(pos+4)=MOV_REG_LSR_IMM(FC_RETOP, FC_RETOP, 16); // mov FC_RETOP, FC_RETOP, lsr #16
913 *(Bit32u*)(pos+8)=MOV_REG_LSR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, lsr a2
914 *(Bit32u*)(pos+12)=NOP; // nop
915 break;
916 case t_SHRd:
917 *(Bit32u*)pos=MOV_REG_LSR_REG(FC_RETOP, HOST_a1, HOST_a2); // mov FC_RETOP, a1, lsr a2
918 *(Bit32u*)(pos+4)=NOP; // nop
919 *(Bit32u*)(pos+8)=NOP; // nop
920 *(Bit32u*)(pos+12)=NOP; // nop
921 break;
922 case t_SARb:
923 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 24); // mov FC_RETOP, a1, lsl #24
924 *(Bit32u*)(pos+4)=MOV_REG_ASR_IMM(FC_RETOP, FC_RETOP, 24); // mov FC_RETOP, FC_RETOP, asr #24
925 *(Bit32u*)(pos+8)=MOV_REG_ASR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, asr a2
926 *(Bit32u*)(pos+12)=NOP; // nop
927 break;
928 case t_SARw:
929 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 16); // mov FC_RETOP, a1, lsl #16
930 *(Bit32u*)(pos+4)=MOV_REG_ASR_IMM(FC_RETOP, FC_RETOP, 16); // mov FC_RETOP, FC_RETOP, asr #16
931 *(Bit32u*)(pos+8)=MOV_REG_ASR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, asr a2
932 *(Bit32u*)(pos+12)=NOP; // nop
933 break;
934 case t_SARd:
935 *(Bit32u*)pos=MOV_REG_ASR_REG(FC_RETOP, HOST_a1, HOST_a2); // mov FC_RETOP, a1, asr a2
936 *(Bit32u*)(pos+4)=NOP; // nop
937 *(Bit32u*)(pos+8)=NOP; // nop
938 *(Bit32u*)(pos+12)=NOP; // nop
939 break;
940 case t_RORb:
941 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 24); // mov FC_RETOP, a1, lsl #24
942 *(Bit32u*)(pos+4)=ORR_REG_LSR_IMM(FC_RETOP, FC_RETOP, FC_RETOP, 8); // orr FC_RETOP, FC_RETOP, FC_RETOP, lsr #8
943 *(Bit32u*)(pos+8)=ORR_REG_LSR_IMM(FC_RETOP, FC_RETOP, FC_RETOP, 16); // orr FC_RETOP, FC_RETOP, FC_RETOP, lsr #16
944 *(Bit32u*)(pos+12)=MOV_REG_ROR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, ror a2
945 break;
946 case t_RORw:
947 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 16); // mov FC_RETOP, a1, lsl #16
948 *(Bit32u*)(pos+4)=ORR_REG_LSR_IMM(FC_RETOP, FC_RETOP, FC_RETOP, 16); // orr FC_RETOP, FC_RETOP, FC_RETOP, lsr #16
949 *(Bit32u*)(pos+8)=MOV_REG_ROR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, ror a2
950 *(Bit32u*)(pos+12)=NOP; // nop
951 break;
952 case t_RORd:
953 *(Bit32u*)pos=MOV_REG_ROR_REG(FC_RETOP, HOST_a1, HOST_a2); // mov FC_RETOP, a1, ror a2
954 *(Bit32u*)(pos+4)=NOP; // nop
955 *(Bit32u*)(pos+8)=NOP; // nop
956 *(Bit32u*)(pos+12)=NOP; // nop
957 break;
958 case t_ROLw:
959 *(Bit32u*)pos=MOV_REG_LSL_IMM(FC_RETOP, HOST_a1, 16); // mov FC_RETOP, a1, lsl #16
960 *(Bit32u*)(pos+4)=RSB_IMM(HOST_a2, HOST_a2, 32, 0); // rsb a2, a2, #32
961 *(Bit32u*)(pos+8)=ORR_REG_LSR_IMM(FC_RETOP, FC_RETOP, FC_RETOP, 16); // orr FC_RETOP, FC_RETOP, FC_RETOP, lsr #16
962 *(Bit32u*)(pos+12)=MOV_REG_ROR_REG(FC_RETOP, FC_RETOP, HOST_a2); // mov FC_RETOP, FC_RETOP, ror a2
963 break;
964 case t_ROLd:
965 *(Bit32u*)pos=RSB_IMM(HOST_a2, HOST_a2, 32, 0); // rsb a2, a2, #32
966 *(Bit32u*)(pos+4)=MOV_REG_ROR_REG(FC_RETOP, HOST_a1, HOST_a2); // mov FC_RETOP, a1, ror a2
967 *(Bit32u*)(pos+8)=NOP; // nop
968 *(Bit32u*)(pos+12)=NOP; // nop
969 break;
970 case t_NEGb:
971 case t_NEGw:
972 case t_NEGd:
973 *(Bit32u*)pos=RSB_IMM(FC_RETOP, HOST_a1, 0, 0); // rsb FC_RETOP, a1, #0
974 *(Bit32u*)(pos+4)=NOP; // nop
975 *(Bit32u*)(pos+8)=NOP; // nop
976 *(Bit32u*)(pos+12)=NOP; // nop
977 break;
978 default:
979 *(Bit32u*)(pos+12)=(Bit32u)fct_ptr; // simple_func
980 break;
981
982 }
983 #else
984 *(Bit32u*)(pos+12)=(Bit32u)fct_ptr; // simple_func
985 #endif
986 }
987 #endif
988
989 static void cache_block_before_close(void) { }
990
991 #ifdef DRC_USE_SEGS_ADDR
992
993 // mov 16bit value from Segs[index] into dest_reg using FC_SEGS_ADDR (index modulo 2 must be zero)
994 // 16bit moves may destroy the upper 16bit of the destination register
995 static void gen_mov_seg16_to_reg(HostReg dest_reg,Bitu index) {
996 cache_addd( LDRH_IMM(dest_reg, FC_SEGS_ADDR, index) ); // ldrh dest_reg, [FC_SEGS_ADDR, #index]
997 }
998
999 // mov 32bit value from Segs[index] into dest_reg using FC_SEGS_ADDR (index modulo 4 must be zero)
1000 static void gen_mov_seg32_to_reg(HostReg dest_reg,Bitu index) {
1001 cache_addd( LDR_IMM(dest_reg, FC_SEGS_ADDR, index) ); // ldr dest_reg, [FC_SEGS_ADDR, #index]
1002 }
1003
1004 // add a 32bit value from Segs[index] to a full register using FC_SEGS_ADDR (index modulo 4 must be zero)
1005 static void gen_add_seg32_to_reg(HostReg reg,Bitu index) {
1006 cache_addd( LDR_IMM(temp1, FC_SEGS_ADDR, index) ); // ldr temp1, [FC_SEGS_ADDR, #index]
1007 cache_addd( ADD_REG_LSL_IMM(reg, reg, temp1, 0) ); // add reg, reg, temp1
1008 }
1009
1010 #endif
1011
1012 #ifdef DRC_USE_REGS_ADDR
1013
1014 // mov 16bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR (index modulo 2 must be zero)
1015 // 16bit moves may destroy the upper 16bit of the destination register
1016 static void gen_mov_regval16_to_reg(HostReg dest_reg,Bitu index) {
1017 cache_addd( LDRH_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldrh dest_reg, [FC_REGS_ADDR, #index]
1018 }
1019
1020 // mov 32bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR (index modulo 4 must be zero)
1021 static void gen_mov_regval32_to_reg(HostReg dest_reg,Bitu index) {
1022 cache_addd( LDR_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldr dest_reg, [FC_REGS_ADDR, #index]
1023 }
1024
1025 // 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)
1026 // 16bit moves may destroy the upper 16bit of the destination register
1027 static void gen_mov_regword_to_reg(HostReg dest_reg,Bitu index,bool dword) {
1028 if (dword) {
1029 cache_addd( LDR_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldr dest_reg, [FC_REGS_ADDR, #index]
1030 } else {
1031 cache_addd( LDRH_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldrh dest_reg, [FC_REGS_ADDR, #index]
1032 }
1033 }
1034
1035 // move an 8bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR
1036 // the upper 24bit of the destination register can be destroyed
1037 // this function does not use FC_OP1/FC_OP2 as dest_reg as these
1038 // registers might not be directly byte-accessible on some architectures
1039 static void gen_mov_regbyte_to_reg_low(HostReg dest_reg,Bitu index) {
1040 cache_addd( LDRB_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldrb dest_reg, [FC_REGS_ADDR, #index]
1041 }
1042
1043 // move an 8bit value from cpu_regs[index] into dest_reg using FC_REGS_ADDR
1044 // the upper 24bit of the destination register can be destroyed
1045 // this function can use FC_OP1/FC_OP2 as dest_reg which are
1046 // not directly byte-accessible on some architectures
1047 static void INLINE gen_mov_regbyte_to_reg_low_canuseword(HostReg dest_reg,Bitu index) {
1048 cache_addd( LDRB_IMM(dest_reg, FC_REGS_ADDR, index) ); // ldrb dest_reg, [FC_REGS_ADDR, #index]
1049 }
1050
1051
1052 // add a 32bit value from cpu_regs[index] to a full register using FC_REGS_ADDR (index modulo 4 must be zero)
1053 static void gen_add_regval32_to_reg(HostReg reg,Bitu index) {
1054 cache_addd( LDR_IMM(temp2, FC_REGS_ADDR, index) ); // ldr temp2, [FC_REGS_ADDR, #index]
1055 cache_addd( ADD_REG_LSL_IMM(reg, reg, temp2, 0) ); // add reg, reg, temp2
1056 }
1057
1058
1059 // move 16bit of register into cpu_regs[index] using FC_REGS_ADDR (index modulo 2 must be zero)
1060 static void gen_mov_regval16_from_reg(HostReg src_reg,Bitu index) {
1061 cache_addd( STRH_IMM(src_reg, FC_REGS_ADDR, index) ); // strh src_reg, [FC_REGS_ADDR, #index]
1062 }
1063
1064 // move 32bit of register into cpu_regs[index] using FC_REGS_ADDR (index modulo 4 must be zero)
1065 static void gen_mov_regval32_from_reg(HostReg src_reg,Bitu index) {
1066 cache_addd( STR_IMM(src_reg, FC_REGS_ADDR, index) ); // str src_reg, [FC_REGS_ADDR, #index]
1067 }
1068
1069 // 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)
1070 static void gen_mov_regword_from_reg(HostReg src_reg,Bitu index,bool dword) {
1071 if (dword) {
1072 cache_addd( STR_IMM(src_reg, FC_REGS_ADDR, index) ); // str src_reg, [FC_REGS_ADDR, #index]
1073 } else {
1074 cache_addd( STRH_IMM(src_reg, FC_REGS_ADDR, index) ); // strh src_reg, [FC_REGS_ADDR, #index]
1075 }
1076 }
1077
1078 // move the lowest 8bit of a register into cpu_regs[index] using FC_REGS_ADDR
1079 static void gen_mov_regbyte_from_reg_low(HostReg src_reg,Bitu index) {
1080 cache_addd( STRB_IMM(src_reg, FC_REGS_ADDR, index) ); // strb src_reg, [FC_REGS_ADDR, #index]
1081 }
1082
1083 #endif