* src/vm/jit/codegen-common.cpp (codegen_emit): New generic version of the
[cacao.git] / src / vm / jit / powerpc / md.h
1 /* src/vm/jit/powerpc/md.h - machine dependent PowerPC functions
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _VM_JIT_POWERPC_MD_H
27 #define _VM_JIT_POWERPC_MD_H
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <stdint.h>
33
34 #include "vm/jit/powerpc/codegen.h"
35
36 #include "vm/global.h"
37 #include "vm/vm.hpp"
38
39 #include "vm/jit/asmpart.h"
40 #include "vm/jit/codegen-common.hpp"
41
42
43 /**
44  * Returns the size (in bytes) of the current stackframe, specified by
45  * the passed codeinfo structure.
46  */
47 inline static int32_t md_stacktrace_get_framesize(codeinfo* code)
48 {
49         // Check for the asm_vm_call_method special case.
50         if (code == NULL)
51                 return 0;
52
53         // On PowerPC we use 8-byte stackslots.
54         return code->stackframesize * 8;
55 }
56
57
58 /* md_stacktrace_get_returnaddress *********************************************
59
60    Returns the return address of the current stackframe, specified by
61    the passed stack pointer and the stack frame size.
62
63 *******************************************************************************/
64
65 inline static void *md_stacktrace_get_returnaddress(void *sp, int32_t stackframesize)
66 {
67         void *ra;
68
69         /* Pn PowerPC the return address is located in the linkage
70            area. */
71
72         ra = *((void **) (((uintptr_t) sp) + stackframesize + LA_LR_OFFSET));
73
74         return ra;
75 }
76
77
78 /* md_codegen_get_pv_from_pc ***************************************************
79
80    Machine code:
81
82    7d6802a6    mflr    r11
83    39abffe0    addi    r13,r11,-32
84
85    or
86
87    7d6802a6    mflr    r11
88    3dabffff    addis   r13,r11,-1
89    39ad68b0    addi    r13,r13,26800
90
91 *******************************************************************************/
92
93 inline static void* md_codegen_get_pv_from_pc(void* ra)
94 {
95         int32_t offset;
96
97         uint32_t* pc = (uint32_t*) ra;
98
99         /* get first instruction word after jump */
100
101         uint32_t mcode = pc[1];
102
103         /* check if we have 2 instructions (addis, addi) */
104
105         if ((mcode >> 16) == 0x3dab) {
106                 /* get displacement of first instruction (addis) */
107
108                 offset = (int32_t) (mcode << 16);
109
110                 /* get displacement of second instruction (addi) */
111
112                 mcode = pc[2];
113
114                 /* check for addi instruction */
115
116                 assert((mcode >> 16) == 0x39ad);
117
118                 offset += (int16_t) (mcode & 0x0000ffff);
119         }
120         else {
121                 /* check for addi instruction */
122
123                 assert((mcode >> 16) == 0x39ab);
124
125                 /* get offset of first instruction (addi) */
126
127                 offset = (int16_t) (mcode & 0x0000ffff);
128         }
129
130         /* calculate PV via RA + offset */
131
132         void* pv = (void*) (((uintptr_t) ra) + offset);
133
134         return pv;
135 }
136
137
138 /* md_cacheflush ***************************************************************
139
140    Calls the system's function to flush the instruction and data
141    cache.
142
143 *******************************************************************************/
144
145 inline static void md_cacheflush(void *addr, int nbytes)
146 {
147         asm_cacheflush(addr, nbytes);
148 }
149
150
151 /* md_icacheflush **************************************************************
152
153    Calls the system's function to flush the instruction cache.
154
155 *******************************************************************************/
156
157 inline static void md_icacheflush(void *addr, int nbytes)
158 {
159         asm_cacheflush(addr, nbytes);
160 }
161
162
163 /* md_dcacheflush **************************************************************
164
165    Calls the system's function to flush the data cache.
166
167 *******************************************************************************/
168
169 inline static void md_dcacheflush(void *addr, int nbytes)
170 {
171         asm_cacheflush(addr, nbytes);
172 }
173
174 #endif /* _VM_JIT_POWERPC_MD_H */
175
176
177 /*
178  * These are local overrides for various environment variables in Emacs.
179  * Please do not remove this and leave it at the end of the file, where
180  * Emacs will automagically detect them.
181  * ---------------------------------------------------------------------
182  * Local variables:
183  * mode: c
184  * indent-tabs-mode: t
185  * c-basic-offset: 4
186  * tab-width: 4
187  * End:
188  * vim:noexpandtab:sw=4:ts=4:
189  */