update
[qemu] / dyngen.h
1 /*
2  * dyngen helpers
3  * 
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #ifdef __alpha__
22
23 register int gp asm("$29");
24
25 static inline void immediate_ldah(void *p, int val) {
26     uint32_t *dest = p;
27     long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff;
28
29     *dest &= ~0xffff;
30     *dest |= high;
31     *dest |= 31 << 16;
32 }
33 static inline void immediate_lda(void *dest, int val) {
34     *(uint16_t *) dest = val;
35 }
36 void fix_bsr(void *p, int offset) {
37     uint32_t *dest = p;
38     *dest &= ~((1 << 21) - 1);
39     *dest |= (offset >> 2) & ((1 << 21) - 1);
40 }
41
42 #endif /* __alpha__ */
43
44 #ifdef __arm__
45
46 #define MAX_OP_SIZE    (128 * 4) /* in bytes */
47 /* max size of the code that can be generated without calling arm_flush_ldr */
48 #define MAX_FRAG_SIZE  (1024 * 4) 
49 //#define MAX_FRAG_SIZE  (135 * 4) /* for testing */ 
50
51 typedef struct LDREntry {
52     uint8_t *ptr;
53     uint32_t *data_ptr;
54 } LDREntry;
55
56 static LDREntry arm_ldr_table[1024];
57 static uint32_t arm_data_table[1024];
58
59 extern char exec_loop;
60
61 static inline void arm_reloc_pc24(uint32_t *ptr, uint32_t insn, int val)
62 {
63     *ptr = (insn & ~0xffffff) | ((insn + ((val - (int)ptr) >> 2)) & 0xffffff);
64 }
65
66 static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr,
67                               LDREntry *ldr_start, LDREntry *ldr_end, 
68                               uint32_t *data_start, uint32_t *data_end, 
69                               int gen_jmp)
70 {
71     LDREntry *le;
72     uint32_t *ptr;
73     int offset, data_size, target;
74     uint8_t *data_ptr;
75     uint32_t insn;
76  
77     data_size = (uint8_t *)data_end - (uint8_t *)data_start;
78
79     if (!gen_jmp) {
80         /* b exec_loop */
81         arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, (long)(&exec_loop)); 
82         gen_code_ptr += 4;
83     } else {
84         /* generate branch to skip the data */
85         if (data_size == 0)
86             return gen_code_ptr;
87         target = (long)gen_code_ptr + data_size + 4;
88         arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, target);
89         gen_code_ptr += 4;
90     }
91    
92     /* copy the data */
93     data_ptr = gen_code_ptr;
94     memcpy(gen_code_ptr, data_start, data_size);
95     gen_code_ptr += data_size;
96     
97     /* patch the ldr to point to the data */
98     for(le = ldr_start; le < ldr_end; le++) {
99         ptr = (uint32_t *)le->ptr;
100         offset = ((unsigned long)(le->data_ptr) - (unsigned long)data_start) + 
101             (unsigned long)data_ptr - 
102             (unsigned long)ptr - 8;
103         insn = *ptr & ~(0xfff | 0x00800000);
104         if (offset < 0) {
105             offset = - offset;
106         } else {
107             insn |= 0x00800000;
108         }
109         if (offset > 0xfff) {
110             fprintf(stderr, "Error ldr offset\n");
111             abort();
112         }
113         insn |= offset;
114         *ptr = insn;
115     }
116     return gen_code_ptr;
117 }
118
119 #endif /* __arm__ */
120
121                        
122