1 /* src/vm/jit/i386/md.c - machine dependent i386 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
34 #include "vm/jit/i386/codegen.h"
36 #include "vm/global.h"
39 #include "vm/jit/asmpart.h"
40 #include "vm/jit/jit.hpp"
41 #include "vm/jit/trap.hpp"
44 /* md_init *********************************************************************
46 Do some machine dependent initialization.
48 *******************************************************************************/
56 /* md_jit_method_patch_address *************************************************
58 Gets the patch address of the currently compiled method. The offset
59 is extracted from the load instruction(s) before the jump and added
60 to the right base address (PV or REG_METHODPTR).
64 b9 30 00 49 b7 mov $0xb7490030,%ecx
70 8b 91 00 00 00 00 mov 0x0(%ecx),%edx
76 8b 89 00 00 00 00 mov 0x0(%ecx),%ecx
77 8b 91 00 00 00 00 mov 0x0(%ecx),%edx
80 *******************************************************************************/
82 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
87 void *pa; /* patch address */
89 /* go back to the actual call instruction (2-bytes) */
91 pc = ((uint8_t *) ra) - 2;
93 /* Get the opcode of the call. */
95 opcode = *((uint16_t *) pc);
97 /* check for the different calls */
101 /* INVOKESTATIC/SPECIAL */
103 /* Patch address is 4-bytes before the call instruction. */
109 /* INVOKEVIRTUAL/INTERFACE */
111 /* Return NULL if no mptr was specified (used for
117 /* Get the displacement from the instruction (the displacement
118 address is 4-bytes before the call instruction). */
120 disp = *((int32_t *) (pc - 4));
122 /* Add the displacement to the method pointer. */
124 pa = ((uint8_t *) mptr) + disp;
128 vm_abort_disassemble(pc, 1, "md_jit_method_patch_address: unknown instruction %x", opcode);
137 * Decode the trap instruction at the given PC.
139 * @param trp information about trap to be filled
140 * @param sig signal number
141 * @param xpc exception PC
142 * @param es execution state of the machine
143 * @return true if trap was decoded successfully, false otherwise.
145 bool md_trap_decode(trapinfo_t* trp, int sig, void* _xpc, executionstate_t* es)
147 uint8_t* xpc = (uint8_t*) _xpc;
151 // This is an ArithmeticException.
152 trp->type = TRAP_ArithmeticException;
157 // Check for valid trap instruction.
158 if (patcher_is_valid_trap_instruction_at(xpc)) {
159 trp->type = TRAP_PATCHER;
167 // Get exception-throwing instruction.
168 uint8_t opc = M_ALD_MEM_GET_OPC(xpc);
169 uint8_t mod = M_ALD_MEM_GET_MOD(xpc);
170 uint8_t rm = M_ALD_MEM_GET_RM(xpc);
172 // Check for hardware exception, for values
173 // see emit_mov_mem_reg and emit_mem.
174 if ((opc == 0x8b) && (mod == 0) && (rm == 5)) {
175 int32_t d = M_ALD_MEM_GET_REG(xpc);
176 int32_t disp = M_ALD_MEM_GET_DISP(xpc);
178 // We use the exception type as load displacement.
180 trp->value = es->intregs[d];
184 // Default case is a normal NullPointerException.
186 trp->type = TRAP_NullPointerException;
198 /* md_patch_replacement_point **************************************************
200 Patch the given replacement point.
202 *******************************************************************************/
204 #if defined(ENABLE_REPLACEMENT)
205 void md_patch_replacement_point(u1 *pc, u1 *savedmcode, bool revert)
210 /* write saved machine code */
211 *(u2*)(pc) = *(u2*)(savedmcode);
214 /* save the current machine code */
215 *(u2*)(savedmcode) = *(u2*)(pc);
217 /* build the machine code for the patch */
220 /* write new machine code */
224 /* XXX if required asm_cacheflush(pc,8); */
226 #endif /* defined(ENABLE_REPLACEMENT) */
229 * These are local overrides for various environment variables in Emacs.
230 * Please do not remove this and leave it at the end of the file, where
231 * Emacs will automagically detect them.
232 * ---------------------------------------------------------------------
235 * indent-tabs-mode: t
239 * vim:noexpandtab:sw=4:ts=4: