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;
60 /* patcher_get_putstatic *******************************************************
64 *******************************************************************************/
66 bool patcher_get_putstatic(patchref_t *pr)
74 /* get stuff from the stack */
76 uf = (unresolved_field *) pr->ref;
77 datap = (u1 *) pr->datap;
79 /* get the fieldinfo */
81 if (!(fi = resolve_field_eager(uf)))
84 /* check if the field's class is initialized */
86 if (!(fi->class->state & CLASS_INITIALIZED))
87 if (!initialize_class(fi->class))
90 PATCH_BACK_ORIGINAL_MCODE;
92 /* patch the field value's address */
94 *((intptr_t *) datap) = (intptr_t) fi->value;
100 /* patcher_get_putfield ********************************************************
104 *******************************************************************************/
106 bool patcher_get_putfield(patchref_t *pr)
109 unresolved_field *uf;
115 /* get stuff from the stack */
118 uf = (unresolved_field *) pr->ref;
121 /* get the fieldinfo */
123 if (!(fi = resolve_field_eager(uf)))
126 PATCH_BACK_ORIGINAL_MCODE;
128 /* If NOPs are generated, skip them */
131 ra += PATCHER_NOPS_SKIP;
133 /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
137 /* patch correct offset */
139 if (fi->type == TYPE_LNG) {
140 assert(N_VALID_DISP(fi->offset + 4));
141 /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
142 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
144 *((u4 *) ra ) |= (fi->offset + (*((u4 *) ra) & 0xF));
146 assert(N_VALID_DISP(fi->offset));
148 *((u4 *) ra) |= fi->offset;
154 /* patcher_invokestatic_special ************************************************
158 *******************************************************************************/
160 bool patcher_invokestatic_special(patchref_t *pr)
162 unresolved_method *um;
168 /* get stuff from the stack */
170 um = (unresolved_method *) pr->ref;
171 datap = (u1 *) pr->datap;
173 /* get the fieldinfo */
175 if (!(m = resolve_method_eager(um)))
178 PATCH_BACK_ORIGINAL_MCODE;
180 /* patch stubroutine */
182 *((ptrint *) datap) = (ptrint) m->stubroutine;
187 /* patcher_invokevirtual *******************************************************
191 *******************************************************************************/
193 bool patcher_invokevirtual(patchref_t *pr)
196 unresolved_method *um;
202 /* get stuff from the stack */
205 um = (unresolved_method *) pr->ref;
207 /* get the fieldinfo */
209 if (!(m = resolve_method_eager(um)))
212 /* patch back original code */
214 PATCH_BACK_ORIGINAL_MCODE;
216 /* If NOPs are generated, skip them */
219 ra += PATCHER_NOPS_SKIP;
221 /* patch vftbl index */
224 off = (s4) (OFFSET(vftbl_t, table[0]) +
225 sizeof(methodptr) * m->vftblindex);
227 assert(N_VALID_DISP(off));
229 *((s4 *)(ra + 4)) |= off;
235 /* patcher_invokeinterface *****************************************************
239 *******************************************************************************/
241 bool patcher_invokeinterface(patchref_t *pr)
244 unresolved_method *um;
250 /* get stuff from the stack */
253 um = (unresolved_method *) pr->ref;
255 /* get the fieldinfo */
257 if (!(m = resolve_method_eager(um)))
260 /* patch back original code */
262 PATCH_BACK_ORIGINAL_MCODE;
264 /* If NOPs are generated, skip them */
267 ra += PATCHER_NOPS_SKIP;
269 /* get interfacetable index */
271 idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
272 sizeof(methodptr) * m->class->index);
274 ASSERT_VALID_IMM(idx);
276 /* get method offset */
279 (s4) (sizeof(methodptr) * (m - m->class->methods));
280 ASSERT_VALID_DISP(off);
284 *((s4 *)(ra + 4)) |= (u2)idx;
285 *((s4 *)(ra + 4 + 4 + 4)) |= off;
291 /* patcher_resolve_classref_to_flags *******************************************
293 CHECKCAST/INSTANCEOF:
295 <patched call position>
297 *******************************************************************************/
299 bool patcher_resolve_classref_to_flags(patchref_t *pr)
301 constant_classref *cr;
307 /* get stuff from the stack */
309 cr = (constant_classref *) pr->ref;
310 datap = (u1 *) pr->datap;
312 /* get the fieldinfo */
314 if (!(c = resolve_classref_eager(cr)))
317 PATCH_BACK_ORIGINAL_MCODE;
319 /* patch class flags */
321 *((s4 *) datap) = (s4) c->flags;
326 /* patcher_resolve_classref_to_classinfo ***************************************
332 *******************************************************************************/
334 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
336 constant_classref *cr;
342 /* get stuff from the stack */
344 cr = (constant_classref *) pr->ref;
345 datap = (u1 *) pr->datap;
347 /* get the classinfo */
349 if (!(c = resolve_classref_eager(cr)))
352 PATCH_BACK_ORIGINAL_MCODE;
354 /* patch the classinfo pointer */
356 *((ptrint *) datap) = (ptrint) c;
361 /* patcher_resolve_classref_to_vftbl *******************************************
366 *******************************************************************************/
368 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
370 constant_classref *cr;
376 /* get stuff from the stack */
378 cr = (constant_classref *) pr->ref;
379 datap = (u1 *) pr->datap;
381 /* get the fieldinfo */
383 if (!(c = resolve_classref_eager(cr)))
386 PATCH_BACK_ORIGINAL_MCODE;
388 /* patch super class' vftbl */
390 *((ptrint *) datap) = (ptrint) c->vftbl;
395 /* patcher_checkcast_instanceof_interface **************************************
399 *******************************************************************************/
401 bool patcher_checkcast_instanceof_interface(patchref_t *pr)
405 constant_classref *cr;
410 /* get stuff from the stack */
413 cr = (constant_classref *) pr->ref;
415 /* get the fieldinfo */
417 if (!(c = resolve_classref_eager(cr)))
420 /* patch back original code */
422 PATCH_BACK_ORIGINAL_MCODE;
424 /* If NOPs are generated, skip them */
427 ra += PATCHER_NOPS_SKIP;
429 /* patch super class index */
431 /* From here, split your editor and open codegen.c */
433 switch (*(ra + 1) >> 4) {
435 /* First M_ALD is into ITMP1 */
436 /* INSTANCEOF code */
438 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
439 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC) |=
440 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
441 c->index * sizeof(methodptr*));
446 /* First M_ALD is into ITMP2 */
449 *(u4 *)(ra + SZ_L + SZ_L) |= (u2)(s2)(- c->index);
450 *(u4 *)(ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL) |=
451 (u2)(s2)(OFFSET(vftbl_t, interfacetable[0]) -
452 c->index * sizeof(methodptr*));
464 /* patcher_clinit **************************************************************
466 May be used for GET/PUTSTATIC and in native stub.
470 *******************************************************************************/
472 bool patcher_clinit(patchref_t *pr)
478 /* get stuff from the stack */
480 c = (classinfo *)pr->ref;
482 /* check if the class is initialized */
484 if (!(c->state & CLASS_INITIALIZED))
485 if (!initialize_class(c))
488 /* patch back original code */
490 PATCH_BACK_ORIGINAL_MCODE;
496 /* patcher_athrow_areturn ******************************************************
500 <patched call position>
502 *******************************************************************************/
504 #ifdef ENABLE_VERIFIER
505 bool patcher_athrow_areturn(patchref_t *pr)
507 unresolved_class *uc;
511 /* get stuff from the stack */
513 uc = (unresolved_class *) pr->ref;
515 /* resolve the class and check subtype constraints */
517 if (!resolve_class_eager_no_access_check(uc))
520 /* patch back original code */
522 PATCH_BACK_ORIGINAL_MCODE;
526 #endif /* ENABLE_VERIFIER */
529 /* patcher_resolve_native ******************************************************
531 *******************************************************************************/
533 #if !defined(WITH_STATIC_CLASSPATH)
534 bool patcher_resolve_native_function(patchref_t *pr)
542 /* get stuff from the stack */
544 m = (methodinfo *) pr->ref;
545 datap = (u1 *) pr->datap;
547 /* resolve native function */
549 if (!(f = native_resolve_function(m)))
552 PATCH_BACK_ORIGINAL_MCODE;
554 /* patch native function pointer */
556 *((ptrint *) datap) = (ptrint) f;
560 #endif /* !defined(WITH_STATIC_CLASSPATH) */
563 * These are local overrides for various environment variables in Emacs.
564 * Please do not remove this and leave it at the end of the file, where
565 * Emacs will automagically detect them.
566 * ---------------------------------------------------------------------
569 * indent-tabs-mode: t
573 * vim:noexpandtab:sw=4:ts=4: