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 8268 2007-08-07 13:24:43Z twisti $
41 #include "mm/memory.h"
42 #include "native/native.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/initialize.h"
46 #include "vm/jit/patcher-common.h"
47 #include "vm/jit/s390/codegen.h"
48 #include "vm/jit/s390/md-abi.h"
49 #include "vm/jit/stacktrace.h"
50 #include "vm/resolve.h"
52 #include "vmcore/class.h"
53 #include "vmcore/field.h"
54 #include "vmcore/options.h"
55 #include "vmcore/references.h"
57 #define PATCH_BACK_ORIGINAL_MCODE \
58 *((u2 *) pr->mpc) = (u2) pr->mcode;
62 /* patcher_get_putstatic *******************************************************
66 *******************************************************************************/
68 bool patcher_get_putstatic(patchref_t *pr)
76 /* get stuff from the stack */
78 uf = (unresolved_field *) pr->ref;
79 datap = (u1 *) pr->datap;
81 /* get the fieldinfo */
83 if (!(fi = resolve_field_eager(uf)))
86 /* check if the field's class is initialized */
88 if (!(fi->class->state & CLASS_INITIALIZED))
89 if (!initialize_class(fi->class))
92 PATCH_BACK_ORIGINAL_MCODE;
94 /* patch the field value's address */
96 *((intptr_t *) datap) = (intptr_t) fi->value;
102 /* patcher_get_putfield ********************************************************
106 *******************************************************************************/
108 bool patcher_get_putfield(patchref_t *pr)
111 unresolved_field *uf;
117 /* get stuff from the stack */
120 uf = (unresolved_field *) pr->ref;
123 /* get the fieldinfo */
125 if (!(fi = resolve_field_eager(uf)))
128 PATCH_BACK_ORIGINAL_MCODE;
130 /* If NOPs are generated, skip them */
133 ra += PATCHER_NOPS_SKIP;
135 /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
139 /* patch correct offset */
141 if (fi->type == TYPE_LNG) {
142 assert(N_VALID_DISP(fi->offset + 4));
143 /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
144 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
146 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
148 assert(N_VALID_DISP(fi->offset));
150 *((u4 *) ra) |= fi->offset;
156 /* patcher_invokestatic_special ************************************************
160 *******************************************************************************/
162 bool patcher_invokestatic_special(patchref_t *pr)
164 unresolved_method *um;
170 /* get stuff from the stack */
172 um = (unresolved_method *) pr->ref;
173 datap = (u1 *) pr->datap;
175 /* get the fieldinfo */
177 if (!(m = resolve_method_eager(um)))
180 PATCH_BACK_ORIGINAL_MCODE;
182 /* patch stubroutine */
184 *((ptrint *) datap) = (ptrint) m->stubroutine;
189 /* patcher_invokevirtual *******************************************************
193 *******************************************************************************/
195 bool patcher_invokevirtual(patchref_t *pr)
198 unresolved_method *um;
204 /* get stuff from the stack */
207 um = (unresolved_method *) pr->ref;
209 /* get the fieldinfo */
211 if (!(m = resolve_method_eager(um)))
214 /* patch back original code */
216 PATCH_BACK_ORIGINAL_MCODE;
218 /* If NOPs are generated, skip them */
221 ra += PATCHER_NOPS_SKIP;
223 /* patch vftbl index */
226 off = (s4) (OFFSET(vftbl_t, table[0]) +
227 sizeof(methodptr) * m->vftblindex);
229 assert(N_VALID_DISP(off));
231 *((s4 *)(ra + 4)) |= off;
237 /* patcher_invokeinterface *****************************************************
241 *******************************************************************************/
243 bool patcher_invokeinterface(patchref_t *pr)
246 unresolved_method *um;
252 /* get stuff from the stack */
255 um = (unresolved_method *) pr->ref;
257 /* get the fieldinfo */
259 if (!(m = resolve_method_eager(um)))
262 /* patch back original code */
264 PATCH_BACK_ORIGINAL_MCODE;
266 /* If NOPs are generated, skip them */
269 ra += PATCHER_NOPS_SKIP;
271 /* get interfacetable index */
273 idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
274 sizeof(methodptr) * m->class->index);
276 ASSERT_VALID_IMM(idx);
278 /* get method offset */
281 (s4) (sizeof(methodptr) * (m - m->class->methods));
282 ASSERT_VALID_DISP(off);
286 *((s4 *)(ra + 4)) |= (u2)idx;
287 *((s4 *)(ra + 4 + 4 + 4)) |= off;
293 /* patcher_resolve_classref_to_flags *******************************************
295 CHECKCAST/INSTANCEOF:
297 <patched call position>
299 *******************************************************************************/
301 bool patcher_resolve_classref_to_flags(patchref_t *pr)
303 constant_classref *cr;
309 /* get stuff from the stack */
311 cr = (constant_classref *) pr->ref;
312 datap = (u1 *) pr->datap;
314 /* get the fieldinfo */
316 if (!(c = resolve_classref_eager(cr)))
319 PATCH_BACK_ORIGINAL_MCODE;
321 /* patch class flags */
323 *((s4 *) datap) = (s4) c->flags;
328 /* patcher_resolve_classref_to_classinfo ***************************************
334 *******************************************************************************/
336 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
338 constant_classref *cr;
344 /* get stuff from the stack */
346 cr = (constant_classref *) pr->ref;
347 datap = (u1 *) pr->datap;
349 /* get the classinfo */
351 if (!(c = resolve_classref_eager(cr)))
354 PATCH_BACK_ORIGINAL_MCODE;
356 /* patch the classinfo pointer */
358 *((ptrint *) datap) = (ptrint) c;
363 /* patcher_resolve_classref_to_vftbl *******************************************
368 *******************************************************************************/
370 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
372 constant_classref *cr;
378 /* get stuff from the stack */
380 cr = (constant_classref *) pr->ref;
381 datap = (u1 *) pr->datap;
383 /* get the fieldinfo */
385 if (!(c = resolve_classref_eager(cr)))
388 PATCH_BACK_ORIGINAL_MCODE;
390 /* patch super class' vftbl */
392 *((ptrint *) datap) = (ptrint) c->vftbl;
397 /* patcher_checkcast_instanceof_interface **************************************
401 *******************************************************************************/
403 bool patcher_checkcast_instanceof_interface(patchref_t *pr)
407 constant_classref *cr;
412 /* get stuff from the stack */
415 cr = (constant_classref *) pr->ref;
417 /* get the fieldinfo */
419 if (!(c = resolve_classref_eager(cr)))
422 /* patch back original code */
424 PATCH_BACK_ORIGINAL_MCODE;
426 /* If NOPs are generated, skip them */
429 ra += PATCHER_NOPS_SKIP;
431 /* patch super class index */
433 /* From here, split your editor and open codegen.c */
435 switch (*(ra + 1) >> 4) {
437 /* First M_ALD is into ITMP1 */
438 /* INSTANCEOF code */
440 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
441 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC) |=
442 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
443 c->index * sizeof(methodptr*));
448 /* First M_ALD is into ITMP2 */
451 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
452 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL) |=
453 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
454 c->index * sizeof(methodptr*));
466 /* patcher_clinit **************************************************************
468 May be used for GET/PUTSTATIC and in native stub.
472 *******************************************************************************/
474 bool patcher_clinit(patchref_t *pr)
480 /* get stuff from the stack */
482 c = (classinfo *)pr->ref;
484 /* check if the class is initialized */
486 if (!(c->state & CLASS_INITIALIZED))
487 if (!initialize_class(c))
490 /* patch back original code */
492 PATCH_BACK_ORIGINAL_MCODE;
498 /* patcher_athrow_areturn ******************************************************
502 <patched call position>
504 *******************************************************************************/
506 #ifdef ENABLE_VERIFIER
507 bool patcher_athrow_areturn(patchref_t *pr)
509 unresolved_class *uc;
513 /* get stuff from the stack */
515 uc = (unresolved_class *) pr->ref;
517 /* resolve the class and check subtype constraints */
519 if (!resolve_class_eager_no_access_check(uc))
522 /* patch back original code */
524 PATCH_BACK_ORIGINAL_MCODE;
528 #endif /* ENABLE_VERIFIER */
531 /* patcher_resolve_native ******************************************************
533 *******************************************************************************/
535 #if !defined(WITH_STATIC_CLASSPATH)
536 bool patcher_resolve_native_function(patchref_t *pr)
544 /* get stuff from the stack */
546 m = (methodinfo *) pr->ref;
547 datap = (u1 *) pr->datap;
549 /* resolve native function */
551 if (!(f = native_resolve_function(m)))
554 PATCH_BACK_ORIGINAL_MCODE;
556 /* patch native function pointer */
558 *((ptrint *) datap) = (ptrint) f;
562 #endif /* !defined(WITH_STATIC_CLASSPATH) */
565 * These are local overrides for various environment variables in Emacs.
566 * Please do not remove this and leave it at the end of the file, where
567 * Emacs will automagically detect them.
568 * ---------------------------------------------------------------------
571 * indent-tabs-mode: t
575 * vim:noexpandtab:sw=4:ts=4: