1 /* src/vm/jit/x86_64/patcher.c - x86_64 code patching functions
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
27 Authors: Christian Thalinger
39 #include "mm/memory.h"
40 #include "native/native.h"
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/initialize.h"
44 #include "vm/jit/patcher-common.h"
45 #include "vm/jit/s390/codegen.h"
46 #include "vm/jit/s390/md-abi.h"
47 #include "vm/jit/stacktrace.h"
48 #include "vm/resolve.h"
50 #include "vmcore/class.h"
51 #include "vmcore/field.h"
52 #include "vmcore/options.h"
53 #include "vmcore/references.h"
55 #define PATCH_BACK_ORIGINAL_MCODE \
56 *((u2 *) pr->mpc) = (u2) pr->mcode;
61 /* patcher_patch_code **********************************************************
63 Just patches back the original machine code.
65 *******************************************************************************/
67 void patcher_patch_code(patchref_t *pr)
69 PATCH_BACK_ORIGINAL_MCODE;
73 /* patcher_get_putstatic *******************************************************
77 *******************************************************************************/
79 bool patcher_get_putstatic(patchref_t *pr)
87 /* get stuff from the stack */
89 uf = (unresolved_field *) pr->ref;
90 datap = (u1 *) pr->datap;
92 /* get the fieldinfo */
94 if (!(fi = resolve_field_eager(uf)))
97 /* check if the field's class is initialized */
99 if (!(fi->class->state & CLASS_INITIALIZED))
100 if (!initialize_class(fi->class))
103 PATCH_BACK_ORIGINAL_MCODE;
105 /* patch the field value's address */
107 *((intptr_t *) datap) = (intptr_t) fi->value;
113 /* patcher_get_putfield ********************************************************
117 *******************************************************************************/
119 bool patcher_get_putfield(patchref_t *pr)
122 unresolved_field *uf;
128 /* get stuff from the stack */
131 uf = (unresolved_field *) pr->ref;
134 /* get the fieldinfo */
136 if (!(fi = resolve_field_eager(uf)))
139 PATCH_BACK_ORIGINAL_MCODE;
141 /* If NOPs are generated, skip them */
144 ra += PATCHER_NOPS_SKIP;
146 /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
150 /* patch correct offset */
152 if (fi->type == TYPE_LNG) {
153 ASSERT_VALID_DISP(fi->offset + 4);
154 /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
155 N_RX_SET_DISP(ra, fi->offset + N_RX_GET_DISP(ra));
157 N_RX_SET_DISP(ra, fi->offset + N_RX_GET_DISP(ra));
159 ASSERT_VALID_DISP(fi->offset);
161 N_RX_SET_DISP(ra, fi->offset);
167 /* patcher_invokestatic_special ************************************************
171 *******************************************************************************/
173 bool patcher_invokestatic_special(patchref_t *pr)
175 unresolved_method *um;
181 /* get stuff from the stack */
183 um = (unresolved_method *) pr->ref;
184 datap = (u1 *) pr->datap;
186 /* get the fieldinfo */
188 if (!(m = resolve_method_eager(um)))
191 PATCH_BACK_ORIGINAL_MCODE;
193 /* patch stubroutine */
195 *((ptrint *) datap) = (ptrint) m->stubroutine;
200 /* patcher_invokevirtual *******************************************************
204 *******************************************************************************/
206 bool patcher_invokevirtual(patchref_t *pr)
209 unresolved_method *um;
215 /* get stuff from the stack */
218 um = (unresolved_method *) pr->ref;
220 /* get the fieldinfo */
222 if (!(m = resolve_method_eager(um)))
225 /* patch back original code */
227 PATCH_BACK_ORIGINAL_MCODE;
229 /* If NOPs are generated, skip them */
232 ra += PATCHER_NOPS_SKIP;
234 /* patch vftbl index */
237 off = (s4) (OFFSET(vftbl_t, table[0]) +
238 sizeof(methodptr) * m->vftblindex);
240 ASSERT_VALID_DISP(off);
242 N_RX_SET_DISP(ra + SZ_RX, off);
248 /* patcher_invokeinterface *****************************************************
252 *******************************************************************************/
254 bool patcher_invokeinterface(patchref_t *pr)
257 unresolved_method *um;
263 /* get stuff from the stack */
266 um = (unresolved_method *) pr->ref;
268 /* get the fieldinfo */
270 if (!(m = resolve_method_eager(um)))
273 /* patch back original code */
275 PATCH_BACK_ORIGINAL_MCODE;
277 /* If NOPs are generated, skip them */
280 ra += PATCHER_NOPS_SKIP;
282 /* get interfacetable index */
284 idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
285 sizeof(methodptr) * m->class->index);
287 ASSERT_VALID_IMM(idx);
289 /* get method offset */
292 (s4) (sizeof(methodptr) * (m - m->class->methods));
294 ASSERT_VALID_DISP(off);
298 N_RI_SET_IMM(ra + SZ_L, idx);
299 N_RX_SET_DISP(ra + SZ_L + SZ_LHI + SZ_L, off);
305 /* patcher_resolve_classref_to_flags *******************************************
307 CHECKCAST/INSTANCEOF:
309 <patched call position>
311 *******************************************************************************/
313 bool patcher_resolve_classref_to_flags(patchref_t *pr)
315 constant_classref *cr;
321 /* get stuff from the stack */
323 cr = (constant_classref *) pr->ref;
324 datap = (u1 *) pr->datap;
326 /* get the fieldinfo */
328 if (!(c = resolve_classref_eager(cr)))
331 PATCH_BACK_ORIGINAL_MCODE;
333 /* patch class flags */
335 *((s4 *) datap) = (s4) c->flags;
340 /* patcher_resolve_classref_to_classinfo ***************************************
346 *******************************************************************************/
348 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
350 constant_classref *cr;
356 /* get stuff from the stack */
358 cr = (constant_classref *) pr->ref;
359 datap = (u1 *) pr->datap;
361 /* get the classinfo */
363 if (!(c = resolve_classref_eager(cr)))
366 PATCH_BACK_ORIGINAL_MCODE;
368 /* patch the classinfo pointer */
370 *((ptrint *) datap) = (ptrint) c;
375 /* patcher_resolve_classref_to_vftbl *******************************************
380 *******************************************************************************/
382 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
384 constant_classref *cr;
390 /* get stuff from the stack */
392 cr = (constant_classref *) pr->ref;
393 datap = (u1 *) pr->datap;
395 /* get the fieldinfo */
397 if (!(c = resolve_classref_eager(cr)))
400 PATCH_BACK_ORIGINAL_MCODE;
402 /* patch super class' vftbl */
404 *((ptrint *) datap) = (ptrint) c->vftbl;
409 /* patcher_checkcast_instanceof_interface **************************************
413 *******************************************************************************/
415 bool patcher_checkcast_instanceof_interface(patchref_t *pr)
419 constant_classref *cr;
424 /* get stuff from the stack */
427 cr = (constant_classref *) pr->ref;
429 /* get the fieldinfo */
431 if (!(c = resolve_classref_eager(cr)))
434 /* patch back original code */
436 PATCH_BACK_ORIGINAL_MCODE;
438 /* If NOPs are generated, skip them */
441 ra += PATCHER_NOPS_SKIP;
443 /* patch super class index */
445 /* From here, split your editor and open codegen.c */
447 switch (N_RX_GET_REG(ra)) {
449 /* First M_ALD is into ITMP1 */
450 /* INSTANCEOF code */
452 N_RI_SET_IMM(ra + SZ_L + SZ_L, - c->index);
454 ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC,
455 (int16_t)(OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))
460 /* First M_ALD is into ITMP2 */
463 N_RI_SET_IMM(ra + SZ_L + SZ_L, c->index);
465 ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL,
466 (int16_t)(OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))
480 * These are local overrides for various environment variables in Emacs.
481 * Please do not remove this and leave it at the end of the file, where
482 * Emacs will automagically detect them.
483 * ---------------------------------------------------------------------
486 * indent-tabs-mode: t
490 * vim:noexpandtab:sw=4:ts=4: