MIPS repair work
[cacao.git] / src / vm / jit / mips / md.h
1 /* src/vm/jit/mips/md.h - machine dependent MIPS 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_MIPS_MD_H
27 #define _VM_JIT_MIPS_MD_H
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <stdint.h>
33 #include <sys/cachectl.h>
34
35 #include "vm/global.h"
36 #include "vm/vm.hpp"
37
38 #include "vm/jit/asmpart.h"
39 #include "vm/jit/codegen-common.hpp"
40
41 /**
42  * Returns the size (in bytes) of the current stackframe, specified by
43  * the passed codeinfo structure.
44  */
45 inline static int32_t md_stacktrace_get_framesize(codeinfo* code)
46 {
47         // Check for the asm_vm_call_method special case.
48         if (code == NULL)
49                 return 0;
50
51         // On MIPS we use 8-byte stackslots.
52         return code->stackframesize * 8;
53 }
54
55
56 /* md_stacktrace_get_returnaddress *********************************************
57
58    Returns the return address of the current stackframe, specified by
59    the passed stack pointer and the stack frame size.
60
61 *******************************************************************************/
62
63 inline static void *md_stacktrace_get_returnaddress(void *sp, int32_t stackframesize)
64 {
65         void *ra;
66
67         /* On MIPS the return address is located on the top of the
68            stackframe. */
69
70         ra = *((void **) (((uintptr_t) sp) + stackframesize - 8));
71
72         return ra;
73 }
74
75
76 /* md_codegen_get_pv_from_pc ***************************************************
77
78    Machine code:
79
80    03c0f809    jalr     s8
81    00000000    nop
82    27feff9c    addiu    s8,ra,-100
83
84 *******************************************************************************/
85
86 inline static void* md_codegen_get_pv_from_pc(void* ra)
87 {
88         int32_t offset;
89
90         uint32_t* pc = (uint32_t*) ra;
91
92         /* get the offset of the instructions */
93
94         /* get first instruction word after jump */
95
96         uint32_t mcode = pc[0];
97
98         /* check if we have 2 instructions (lui, daddiu) */
99
100         if ((mcode >> 16) == 0x3c19) {
101                 /* get displacement of first instruction (lui) */
102
103                 offset = (int32_t) (mcode << 16);
104
105                 /* get displacement of second instruction (daddiu) */
106
107                 mcode = pc[1];
108
109 #if SIZEOF_VOID_P == 8
110                 assert((mcode >> 16) == 0x6739);
111 #else
112                 assert((mcode >> 16) == 0x2739);
113 #endif
114
115                 offset += (int16_t) (mcode & 0x0000ffff);
116         }
117         else {
118                 /* get offset of first instruction (daddiu) */
119
120 #if SIZEOF_VOID_P == 8
121                 assert((mcode >> 16) == 0x67fe);
122 #else
123                 assert((mcode >> 16) == 0x27fe);
124 #endif
125
126                 offset = (int16_t) (mcode & 0x0000ffff);
127         }
128
129         /* calculate PV via RA + offset */
130
131         void* pv = (void*) (((uintptr_t) ra) + offset);
132
133         return pv;
134 }
135
136
137 /* md_cacheflush ***************************************************************
138
139    Calls the system's function to flush the instruction and data
140    cache.
141
142 *******************************************************************************/
143
144 inline static void md_cacheflush(void *addr, int nbytes)
145 {
146         cacheflush(addr, nbytes, BCACHE);
147 }
148
149
150 /* md_icacheflush **************************************************************
151
152    Calls the system's function to flush the instruction cache.
153
154 *******************************************************************************/
155
156 inline static void md_icacheflush(void *addr, int nbytes)
157 {
158         cacheflush(addr, nbytes, ICACHE);
159 }
160
161
162 /* md_dcacheflush **************************************************************
163
164    Calls the system's function to flush the data cache.
165
166 *******************************************************************************/
167
168 inline static void md_dcacheflush(void *addr, int nbytes)
169 {
170         cacheflush(addr, nbytes, DCACHE);
171 }
172
173 #endif /* _VM_JIT_MIPS_MD_H */
174
175
176 /*
177  * These are local overrides for various environment variables in Emacs.
178  * Please do not remove this and leave it at the end of the file, where
179  * Emacs will automagically detect them.
180  * ---------------------------------------------------------------------
181  * Local variables:
182  * mode: c
183  * indent-tabs-mode: t
184  * c-basic-offset: 4
185  * tab-width: 4
186  * End:
187  * vim:noexpandtab:sw=4:ts=4:
188  */