1 /* src/vm/jit/arm/md.c - machine dependent ARM functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
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
32 #include "vm/jit/arm/codegen.h"
33 #include "vm/jit/arm/md.h"
34 #include "vm/jit/arm/md-abi.h"
36 #include "vm/jit/executionstate.h"
37 #include "vm/jit/trap.hpp"
40 /* md_init *********************************************************************
42 Do some machine dependent initialization.
44 *******************************************************************************/
52 /* md_jit_method_patch_address *************************************************
54 Gets the patch address of the currently compiled method. The offset
55 is extracted from the load instruction(s) before the jump and added
56 to the right base address (PV or REG_METHODPTR).
60 e51cc040 ldr ip, [ip, #-64]
67 e59bc004 ldr ip, [fp, #4]
74 e28bca01 add ip, fp, #4096 ; 0x1000
75 e59cc004 ldr ip, [ip, #4]
79 How we find out the patching address to store new method pointer:
80 - loaded IP with LDR IP,[METHODPTR]?
81 yes=INVOKEVIRTUAL or INVOKEINTERFACE (things are easy!)
82 - loaded IP from data segment
83 yes=INVOKESTATIC or INVOKESPECIAL (things are complicated)
84 recompute pointer to data segment, maybe larger offset
86 *******************************************************************************/
88 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
93 void *pa; /* patch address */
95 /* Go back to the actual load instruction. */
97 pc = ((uint32_t *) ra) - 3;
99 /* Get first instruction word on current PC. */
103 /* Sanity check: Are we inside jit code? */
105 assert(pc[1] == 0xe1a0e00f /*MOV LR,PC*/);
106 assert(pc[2] == 0xe1a0f00c /*MOV PC,IP*/);
108 /* Sanity check: We unconditionally loaded a word into REG_PV? */
110 assert ((mcode & 0xff70f000) == 0xe510c000);
112 /* Get load displacement. */
114 disp = (int32_t) (mcode & 0x0fff);
116 /* Case: We loaded from base REG_PV with negative displacement. */
118 if (M_MEM_GET_Rbase(mcode) == REG_PV && (mcode & 0x00800000) == 0) {
119 /* We loaded from data segment, displacement can be larger. */
123 /* check for "SUB IP, IP, #??, ROTL 12" */
125 if ((mcode & 0xffffff00) == 0xe24cca00)
126 disp += (int32_t) ((mcode & 0x00ff) << 12);
128 /* and get the final data segment address */
130 pa = ((uint8_t *) pv) - disp;
133 /* Case: We loaded from base REG_METHODPTR with positive displacement. */
135 else if (M_MEM_GET_Rbase(mcode) == REG_METHODPTR && (mcode & 0x00800000) == 0x00800000) {
136 /* return NULL if no mptr was specified (used for replacement) */
141 /* we loaded from REG_METHODPTR */
143 pa = ((uint8_t *) mptr) + disp;
146 /* Case: We loaded from base REG_PV with positive offset. */
148 else if (M_MEM_GET_Rbase(mcode) == REG_PV && (mcode & 0x00800000) == 0x00800000) {
149 /* We loaded with a larger displacement. Normally this means we loaded
150 from REG_METHODPTR. However there is a corner case if we loaded
151 from the data segment at an address aligned to 12 bit, which leads to a
152 zero (positive) displacement for the last instruction. */
156 /* check for "ADD IP, FP, #??, ROTL 12" */
158 if ((mcode & 0xffffff00) == 0xe28bca00) {
159 /* We loaded from REG_METHODPTR with a larger displacement. */
161 assert(mptr != NULL);
162 disp += (int32_t) ((mcode & 0x00ff) << 12);
163 pa = ((uint8_t *) mptr) + disp;
166 /* check for "SUB IP, IP, #??, ROTL 12" (corner case) */
168 else if ((mcode & 0xffffff00) == 0xe24cca00 && disp == 0) {
169 /* We loaded from data segment with a larger displacement aligned to 12 bit. */
171 disp += (int32_t) ((mcode & 0x00ff) << 12);
172 pa = ((uint8_t *) pv) - disp;
175 /* Case is not covered, something is severely wrong. */
178 vm_abort_disassemble(pc - 1, 4, "md_jit_method_patch_address: unknown instruction %x", mcode);
180 /* Keep compiler happy. */
186 /* Case is not covered, something is severely wrong. */
189 vm_abort_disassemble(pc, 3, "md_jit_method_patch_address: unknown instruction %x", mcode);
191 /* Keep compiler happy. */
201 * Decode the trap instruction at the given PC.
203 * @param trp information about trap to be filled
204 * @param sig signal number
205 * @param xpc exception PC
206 * @param es execution state of the machine
207 * @return true if trap was decoded successfully, false otherwise.
209 bool md_trap_decode(trapinfo_t* trp, int sig, void* xpc, executionstate_t* es)
211 // Get the exception-throwing instruction.
212 uint32_t mcode = *((uint32_t*) xpc);
216 // Check for valid trap instruction.
217 if (patcher_is_valid_trap_instruction_at(xpc)) {
218 trp->type = (mcode >> 8) & 0x0fff;
219 trp->value = es->intregs[mcode & 0x0f];
226 // Sanity check for load/store instruction.
227 // FIXME Implement this!
229 // Retrieve base address of load/store instruction.
230 uintptr_t addr = es->intregs[(mcode >> 16) & 0x0f];
232 // Check for implicit NullPointerException.
234 trp->type = TRAP_NullPointerException;
247 * Patch the given replacement point.
249 #if defined(ENABLE_REPLACEMENT)
250 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
252 vm_abort("md_patch_replacement_point: IMPLEMENT ME!");
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: