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
31 $Id: patcher.c 8251 2007-08-01 15:26:59Z pm $
40 #include "mm/memory.h"
41 #include "native/native.h"
42 #include "vm/builtin.h"
43 #include "vm/exceptions.h"
44 #include "vm/initialize.h"
45 #include "vm/jit/patcher-common.h"
46 #include "vm/jit/s390/codegen.h"
47 #include "vm/jit/s390/md-abi.h"
48 #include "vm/jit/stacktrace.h"
49 #include "vm/resolve.h"
51 #include "vmcore/class.h"
52 #include "vmcore/field.h"
53 #include "vmcore/options.h"
54 #include "vmcore/references.h"
56 #define PATCH_BACK_ORIGINAL_MCODE \
57 *((u2 *) pr->mpc) = (u2) pr->mcode;
61 /* patcher_get_putstatic *******************************************************
65 *******************************************************************************/
67 bool patcher_get_putstatic(patchref_t *pr)
75 /* get stuff from the stack */
77 uf = (unresolved_field *) pr->ref;
78 datap = (u1 *) pr->datap;
80 /* get the fieldinfo */
82 if (!(fi = resolve_field_eager(uf)))
85 /* check if the field's class is initialized */
87 if (!(fi->class->state & CLASS_INITIALIZED))
88 if (!initialize_class(fi->class))
91 PATCH_BACK_ORIGINAL_MCODE;
93 /* patch the field value's address */
95 *((ptrint *) datap) = (ptrint) &(fi->value);
101 /* patcher_get_putfield ********************************************************
105 *******************************************************************************/
107 bool patcher_get_putfield(patchref_t *pr)
110 unresolved_field *uf;
116 /* get stuff from the stack */
119 uf = (unresolved_field *) pr->ref;
122 /* get the fieldinfo */
124 if (!(fi = resolve_field_eager(uf)))
127 PATCH_BACK_ORIGINAL_MCODE;
129 /* If NOPs are generated, skip them */
132 ra += PATCHER_NOPS_SKIP;
134 /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
138 /* patch correct offset */
140 if (fi->type == TYPE_LNG) {
141 assert(N_VALID_DISP(fi->offset + 4));
142 /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
143 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
145 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
147 assert(N_VALID_DISP(fi->offset));
149 *((u4 *) ra) |= fi->offset;
155 /* patcher_invokestatic_special ************************************************
159 *******************************************************************************/
161 bool patcher_invokestatic_special(patchref_t *pr)
163 unresolved_method *um;
169 /* get stuff from the stack */
171 um = (unresolved_method *) pr->ref;
172 datap = (u1 *) pr->datap;
174 /* get the fieldinfo */
176 if (!(m = resolve_method_eager(um)))
179 PATCH_BACK_ORIGINAL_MCODE;
181 /* patch stubroutine */
183 *((ptrint *) datap) = (ptrint) m->stubroutine;
188 /* patcher_invokevirtual *******************************************************
192 *******************************************************************************/
194 bool patcher_invokevirtual(patchref_t *pr)
197 unresolved_method *um;
203 /* get stuff from the stack */
206 um = (unresolved_method *) pr->ref;
208 /* get the fieldinfo */
210 if (!(m = resolve_method_eager(um)))
213 /* patch back original code */
215 PATCH_BACK_ORIGINAL_MCODE;
217 /* If NOPs are generated, skip them */
220 ra += PATCHER_NOPS_SKIP;
222 /* patch vftbl index */
225 off = (s4) (OFFSET(vftbl_t, table[0]) +
226 sizeof(methodptr) * m->vftblindex);
228 assert(N_VALID_DISP(off));
230 *((s4 *)(ra + 4)) |= off;
236 /* patcher_invokeinterface *****************************************************
240 *******************************************************************************/
242 bool patcher_invokeinterface(patchref_t *pr)
245 unresolved_method *um;
251 /* get stuff from the stack */
254 um = (unresolved_method *) pr->ref;
256 /* get the fieldinfo */
258 if (!(m = resolve_method_eager(um)))
261 /* patch back original code */
263 PATCH_BACK_ORIGINAL_MCODE;
265 /* If NOPs are generated, skip them */
268 ra += PATCHER_NOPS_SKIP;
270 /* get interfacetable index */
272 idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
273 sizeof(methodptr) * m->class->index);
275 ASSERT_VALID_IMM(idx);
277 /* get method offset */
280 (s4) (sizeof(methodptr) * (m - m->class->methods));
281 ASSERT_VALID_DISP(off);
285 *((s4 *)(ra + 4)) |= (u2)idx;
286 *((s4 *)(ra + 4 + 4 + 4)) |= off;
292 /* patcher_resolve_classref_to_flags *******************************************
294 CHECKCAST/INSTANCEOF:
296 <patched call position>
298 *******************************************************************************/
300 bool patcher_resolve_classref_to_flags(patchref_t *pr)
302 constant_classref *cr;
308 /* get stuff from the stack */
310 cr = (constant_classref *) pr->ref;
311 datap = (u1 *) pr->datap;
313 /* get the fieldinfo */
315 if (!(c = resolve_classref_eager(cr)))
318 PATCH_BACK_ORIGINAL_MCODE;
320 /* patch class flags */
322 *((s4 *) datap) = (s4) c->flags;
327 /* patcher_resolve_classref_to_classinfo ***************************************
333 *******************************************************************************/
335 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
337 constant_classref *cr;
343 /* get stuff from the stack */
345 cr = (constant_classref *) pr->ref;
346 datap = (u1 *) pr->datap;
348 /* get the classinfo */
350 if (!(c = resolve_classref_eager(cr)))
353 PATCH_BACK_ORIGINAL_MCODE;
355 /* patch the classinfo pointer */
357 *((ptrint *) datap) = (ptrint) c;
362 /* patcher_resolve_classref_to_vftbl *******************************************
367 *******************************************************************************/
369 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
371 constant_classref *cr;
377 /* get stuff from the stack */
379 cr = (constant_classref *) pr->ref;
380 datap = (u1 *) pr->datap;
382 /* get the fieldinfo */
384 if (!(c = resolve_classref_eager(cr)))
387 PATCH_BACK_ORIGINAL_MCODE;
389 /* patch super class' vftbl */
391 *((ptrint *) datap) = (ptrint) c->vftbl;
396 /* patcher_checkcast_instanceof_interface **************************************
400 *******************************************************************************/
402 bool patcher_checkcast_instanceof_interface(patchref_t *pr)
406 constant_classref *cr;
411 /* get stuff from the stack */
414 cr = (constant_classref *) pr->ref;
416 /* get the fieldinfo */
418 if (!(c = resolve_classref_eager(cr)))
421 /* patch back original code */
423 PATCH_BACK_ORIGINAL_MCODE;
425 /* If NOPs are generated, skip them */
428 ra += PATCHER_NOPS_SKIP;
430 /* patch super class index */
432 /* From here, split your editor and open codegen.c */
434 switch (*(ra + 1) >> 4) {
436 /* First M_ALD is into ITMP1 */
437 /* INSTANCEOF code */
439 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
440 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC) |=
441 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
442 c->index * sizeof(methodptr*));
447 /* First M_ALD is into ITMP2 */
450 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
451 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL) |=
452 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
453 c->index * sizeof(methodptr*));
465 /* patcher_clinit **************************************************************
467 May be used for GET/PUTSTATIC and in native stub.
471 *******************************************************************************/
473 bool patcher_clinit(patchref_t *pr)
479 /* get stuff from the stack */
481 c = (classinfo *)pr->ref;
483 /* check if the class is initialized */
485 if (!(c->state & CLASS_INITIALIZED))
486 if (!initialize_class(c))
489 /* patch back original code */
491 PATCH_BACK_ORIGINAL_MCODE;
497 /* patcher_athrow_areturn ******************************************************
501 <patched call position>
503 *******************************************************************************/
505 #ifdef ENABLE_VERIFIER
506 bool patcher_athrow_areturn(patchref_t *pr)
508 unresolved_class *uc;
512 /* get stuff from the stack */
514 uc = (unresolved_class *) pr->ref;
516 /* resolve the class and check subtype constraints */
518 if (!resolve_class_eager_no_access_check(uc))
521 /* patch back original code */
523 PATCH_BACK_ORIGINAL_MCODE;
527 #endif /* ENABLE_VERIFIER */
530 /* patcher_resolve_native ******************************************************
532 *******************************************************************************/
534 #if !defined(WITH_STATIC_CLASSPATH)
535 bool patcher_resolve_native_function(patchref_t *pr)
543 /* get stuff from the stack */
545 m = (methodinfo *) pr->ref;
546 datap = (u1 *) pr->datap;
548 /* resolve native function */
550 if (!(f = native_resolve_function(m)))
553 PATCH_BACK_ORIGINAL_MCODE;
555 /* patch native function pointer */
557 *((ptrint *) datap) = (ptrint) f;
561 #endif /* !defined(WITH_STATIC_CLASSPATH) */
564 * These are local overrides for various environment variables in Emacs.
565 * Please do not remove this and leave it at the end of the file, where
566 * Emacs will automagically detect them.
567 * ---------------------------------------------------------------------
570 * indent-tabs-mode: t
574 * vim:noexpandtab:sw=4:ts=4: