Debian Codegraph @

src/cpu/core_dynrec/risc_armv4le-thumb.h

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