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