1 /* src/vm/jit/powerpc/md.c - machine dependent PowerPC functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2009 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "vm/jit/powerpc/codegen.h"
36 #include "vm/jit/powerpc/md.h"
38 #include "vm/global.h"
41 #include "vm/jit/jit.hpp"
42 #include "vm/jit/trap.hpp"
45 /* md_init *********************************************************************
47 Do some machine dependent initialization.
49 *******************************************************************************/
57 /* md_jit_method_patch_address *************************************************
59 Gets the patch address of the currently compiled method. The offset
60 is extracted from the load instruction(s) before the jump and added
61 to the right base address (PV or REG_METHODPTR).
65 81adffd4 lwz r13,-44(r13)
71 81830000 lwz r12,0(r3)
72 81ac0000 lwz r13,0(r12)
78 81830000 lwz r12,0(r3)
79 818c0000 lwz r12,0(r12)
80 81ac0000 lwz r13,0(r12)
84 *******************************************************************************/
86 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
93 /* go back to the actual load instruction (3 instructions) */
95 pc = ((uint32_t *) ra) - 3;
97 /* get first instruction word (lwz) */
101 /* check if we have 2 instructions (addis, addi) */
103 if ((mcode >> 16) == 0x3c19) {
104 /* XXX write a regression for this */
108 /* get displacement of first instruction (addis) */
110 offset = (int32_t) (mcode << 16);
112 /* get displacement of second instruction (addi) */
116 assert((mcode >> 16) != 0x6739);
118 offset += (int16_t) (mcode & 0x0000ffff);
121 /* get the offset from the instruction */
123 offset = (int16_t) (mcode & 0x0000ffff);
125 /* check for load from PV */
127 if ((mcode >> 16) == 0x81ad) {
128 /* get the final data segment address */
130 pa = ((uint8_t *) pv) + offset;
132 else if ((mcode >> 16) == 0x81ac) {
133 /* in this case we use the passed method pointer */
135 /* return NULL if no mptr was specified (used for replacement) */
140 pa = ((uint8_t *) mptr) + offset;
143 /* catch any problems */
145 vm_abort("md_jit_method_patch_address: unknown instruction %x",
148 /* keep compiler happy */
159 * Decode the trap instruction at the given PC.
161 * @param trp information about trap to be filled
162 * @param sig signal number
163 * @param xpc exception PC
164 * @param es execution state of the machine
165 * @return true if trap was decoded successfully, false otherwise.
167 bool md_trap_decode(trapinfo_t* trp, int sig, void* xpc, executionstate_t* es)
169 // Get the exception-throwing instruction.
170 uint32_t mcode = *((uint32_t*) xpc);
174 // Check for valid trap instruction.
175 if (patcher_is_valid_trap_instruction_at(xpc)) {
176 trp->type = TRAP_PATCHER;
184 int s1 = M_OP3_GET_A(mcode);
186 // For now we only handle ArrayIndexOutOfBoundsException.
187 trp->type = TRAP_ArrayIndexOutOfBoundsException;
188 trp->value = es->intregs[s1];
194 int s1 = M_INSTR_OP2_IMM_A(mcode);
195 uintptr_t addr = es->intregs[s1];
197 // Check for special-load.
198 if (s1 == REG_ZERO) {
199 int16_t disp = M_INSTR_OP2_IMM_I(mcode);
200 int d = M_INSTR_OP2_IMM_D(mcode);
202 // We use the exception type as load displacement.
204 trp->value = es->intregs[d];
208 // Check for implicit NullPointerException.
210 trp->type = TRAP_NullPointerException;
224 /* md_patch_replacement_point **************************************************
226 Patch the given replacement point.
228 *******************************************************************************/
230 #if defined(ENABLE_REPLACEMENT)
231 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
236 /* restore the patched-over instruction */
237 *(u4*)(pc) = *(u4*)(savedmcode);
240 /* save the current machine code */
241 *(u4*)(savedmcode) = *(u4*)(pc);
243 /* build the machine code for the patch */
244 assert(0); /* XXX build trap instruction below */
247 /* write the new machine code */
248 *(u4*)(pc) = (u4) mcode;
251 /* flush instruction cache */
252 md_icacheflush(pc,4);
254 #endif /* defined(ENABLE_REPLACEMENT) */
258 * These are local overrides for various environment variables in Emacs.
259 * Please do not remove this and leave it at the end of the file, where
260 * Emacs will automagically detect them.
261 * ---------------------------------------------------------------------
264 * indent-tabs-mode: t
268 * vim:noexpandtab:sw=4:ts=4: