1 /* src/vm/jit/s390/patcher.c - s390 code patching functions
3 Copyright (C) 2006, 2007 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
33 #include "mm/memory.h"
34 #include "native/native.h"
35 #include "vm/builtin.h"
36 #include "vm/exceptions.h"
37 #include "vm/initialize.h"
38 #include "vm/jit/patcher-common.h"
39 #include "vm/jit/s390/codegen.h"
40 #include "vm/jit/s390/md-abi.h"
41 #include "vm/jit/stacktrace.h"
42 #include "vm/resolve.h"
44 #include "vmcore/class.h"
45 #include "vmcore/field.h"
46 #include "vmcore/options.h"
47 #include "vmcore/references.h"
49 #define PATCH_BACK_ORIGINAL_MCODE \
50 *((u2 *) pr->mpc) = (u2) pr->mcode;
55 /* patcher_patch_code **********************************************************
57 Just patches back the original machine code.
59 *******************************************************************************/
61 void patcher_patch_code(patchref_t *pr)
63 PATCH_BACK_ORIGINAL_MCODE;
67 /* patcher_get_putstatic *******************************************************
71 *******************************************************************************/
73 bool patcher_get_putstatic(patchref_t *pr)
81 /* get stuff from the stack */
83 uf = (unresolved_field *) pr->ref;
84 datap = (u1 *) pr->datap;
86 /* get the fieldinfo */
88 if (!(fi = resolve_field_eager(uf)))
91 /* check if the field's class is initialized */
93 if (!(fi->class->state & CLASS_INITIALIZED))
94 if (!initialize_class(fi->class))
97 PATCH_BACK_ORIGINAL_MCODE;
99 /* patch the field value's address */
101 *((intptr_t *) datap) = (intptr_t) fi->value;
107 /* patcher_get_putfield ********************************************************
111 *******************************************************************************/
113 bool patcher_get_putfield(patchref_t *pr)
116 unresolved_field *uf;
122 /* get stuff from the stack */
125 uf = (unresolved_field *) pr->ref;
128 /* get the fieldinfo */
130 if (!(fi = resolve_field_eager(uf)))
133 PATCH_BACK_ORIGINAL_MCODE;
135 /* If NOPs are generated, skip them */
138 ra += PATCHER_NOPS_SKIP;
140 /* If there is an operand load before, skip the load size passed in disp (see ICMD_PUTFIELD) */
144 /* patch correct offset */
146 if (fi->type == TYPE_LNG) {
147 ASSERT_VALID_DISP(fi->offset + 4);
148 /* 2 RX operations, for 2 words; each already contains a 0 or 4 offset. */
149 N_RX_SET_DISP(ra, fi->offset + N_RX_GET_DISP(ra));
151 N_RX_SET_DISP(ra, fi->offset + N_RX_GET_DISP(ra));
153 ASSERT_VALID_DISP(fi->offset);
155 N_RX_SET_DISP(ra, fi->offset);
161 /* patcher_invokestatic_special ************************************************
165 *******************************************************************************/
167 bool patcher_invokestatic_special(patchref_t *pr)
169 unresolved_method *um;
175 /* get stuff from the stack */
177 um = (unresolved_method *) pr->ref;
178 datap = (u1 *) pr->datap;
180 /* get the fieldinfo */
182 if (!(m = resolve_method_eager(um)))
185 PATCH_BACK_ORIGINAL_MCODE;
187 /* patch stubroutine */
189 *((ptrint *) datap) = (ptrint) m->stubroutine;
194 /* patcher_invokevirtual *******************************************************
198 *******************************************************************************/
200 bool patcher_invokevirtual(patchref_t *pr)
203 unresolved_method *um;
209 /* get stuff from the stack */
212 um = (unresolved_method *) pr->ref;
214 /* get the fieldinfo */
216 if (!(m = resolve_method_eager(um)))
219 /* patch back original code */
221 PATCH_BACK_ORIGINAL_MCODE;
223 /* If NOPs are generated, skip them */
226 ra += PATCHER_NOPS_SKIP;
228 /* patch vftbl index */
231 off = (s4) (OFFSET(vftbl_t, table[0]) +
232 sizeof(methodptr) * m->vftblindex);
234 ASSERT_VALID_DISP(off);
236 N_RX_SET_DISP(ra + SZ_RX, off);
242 /* patcher_invokeinterface *****************************************************
246 *******************************************************************************/
248 bool patcher_invokeinterface(patchref_t *pr)
251 unresolved_method *um;
257 /* get stuff from the stack */
260 um = (unresolved_method *) pr->ref;
262 /* get the fieldinfo */
264 if (!(m = resolve_method_eager(um)))
267 /* patch back original code */
269 PATCH_BACK_ORIGINAL_MCODE;
271 /* If NOPs are generated, skip them */
274 ra += PATCHER_NOPS_SKIP;
276 /* get interfacetable index */
278 idx = (s4) (OFFSET(vftbl_t, interfacetable[0]) -
279 sizeof(methodptr) * m->class->index);
281 ASSERT_VALID_IMM(idx);
283 /* get method offset */
286 (s4) (sizeof(methodptr) * (m - m->class->methods));
288 ASSERT_VALID_DISP(off);
292 N_RI_SET_IMM(ra + SZ_L, idx);
293 N_RX_SET_DISP(ra + SZ_L + SZ_LHI + SZ_L, off);
299 /* patcher_resolve_classref_to_flags *******************************************
301 CHECKCAST/INSTANCEOF:
303 <patched call position>
305 *******************************************************************************/
307 bool patcher_resolve_classref_to_flags(patchref_t *pr)
309 constant_classref *cr;
315 /* get stuff from the stack */
317 cr = (constant_classref *) pr->ref;
318 datap = (u1 *) pr->datap;
320 /* get the fieldinfo */
322 if (!(c = resolve_classref_eager(cr)))
325 PATCH_BACK_ORIGINAL_MCODE;
327 /* patch class flags */
329 *((s4 *) datap) = (s4) c->flags;
334 /* patcher_resolve_classref_to_classinfo ***************************************
340 *******************************************************************************/
342 bool patcher_resolve_classref_to_classinfo(patchref_t *pr)
344 constant_classref *cr;
350 /* get stuff from the stack */
352 cr = (constant_classref *) pr->ref;
353 datap = (u1 *) pr->datap;
355 /* get the classinfo */
357 if (!(c = resolve_classref_eager(cr)))
360 PATCH_BACK_ORIGINAL_MCODE;
362 /* patch the classinfo pointer */
364 *((ptrint *) datap) = (ptrint) c;
369 /* patcher_resolve_classref_to_vftbl *******************************************
374 *******************************************************************************/
376 bool patcher_resolve_classref_to_vftbl(patchref_t *pr)
378 constant_classref *cr;
384 /* get stuff from the stack */
386 cr = (constant_classref *) pr->ref;
387 datap = (u1 *) pr->datap;
389 /* get the fieldinfo */
391 if (!(c = resolve_classref_eager(cr)))
394 PATCH_BACK_ORIGINAL_MCODE;
396 /* patch super class' vftbl */
398 *((ptrint *) datap) = (ptrint) c->vftbl;
403 /* patcher_checkcast_instanceof_interface **************************************
407 *******************************************************************************/
409 bool patcher_checkcast_instanceof_interface(patchref_t *pr)
413 constant_classref *cr;
418 /* get stuff from the stack */
421 cr = (constant_classref *) pr->ref;
423 /* get the fieldinfo */
425 if (!(c = resolve_classref_eager(cr)))
428 /* patch back original code */
430 PATCH_BACK_ORIGINAL_MCODE;
432 /* If NOPs are generated, skip them */
435 ra += PATCHER_NOPS_SKIP;
437 /* patch super class index */
439 /* From here, split your editor and open codegen.c */
441 switch (N_RX_GET_REG(ra)) {
443 /* First M_ALD is into ITMP1 */
444 /* INSTANCEOF code */
446 N_RI_SET_IMM(ra + SZ_L + SZ_L, - c->index);
448 ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC,
449 (int16_t)(OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))
454 /* First M_ALD is into ITMP2 */
457 N_RI_SET_IMM(ra + SZ_L + SZ_L, - c->index);
459 ra + SZ_L + SZ_L + SZ_AHI + SZ_BRC + SZ_ILL,
460 (int16_t)(OFFSET(vftbl_t, interfacetable[0]) - c->index * sizeof(methodptr*))
474 * These are local overrides for various environment variables in Emacs.
475 * Please do not remove this and leave it at the end of the file, where
476 * Emacs will automagically detect them.
477 * ---------------------------------------------------------------------
480 * indent-tabs-mode: t
484 * vim:noexpandtab:sw=4:ts=4: