1 /* src/vm/jit/alpha/patcher.c - Alpha 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 5160 2006-07-19 09:13:34Z twisti $
42 #include "mm/memory.h"
43 #include "native/native.h"
44 #include "vm/builtin.h"
46 #include "vm/exceptions.h"
48 #include "vm/initialize.h"
49 #include "vm/options.h"
50 #include "vm/references.h"
51 #include "vm/resolve.h"
52 #include "vm/jit/asmpart.h"
53 #include "vm/jit/patcher.h"
54 #include "vm/jit/methodheader.h"
57 /* patcher_wrapper *************************************************************
59 Wrapper for all patchers. It also creates the stackframe info
62 If the return value of the patcher function is false, it gets the
63 exception object, clears the exception pointer and returns the
66 *******************************************************************************/
68 java_objectheader *patcher_wrapper(u1 *sp, u1 *pv, u1 *ra)
78 /* define the patcher function */
80 bool (*patcher_function)(u1 *);
84 /* get stuff from the stack */
86 xpc = (u1 *) *((ptrint *) (sp + 5 * 8));
87 o = (java_objectheader *) *((ptrint *) (sp + 4 * 8));
88 f = (functionptr) *((ptrint *) (sp + 0 * 8));
90 /* calculate and set the new return address */
94 *((ptrint *) (sp + 5 * 8)) = (ptrint) xpc;
96 /* store PV into the patcher function position */
98 *((ptrint *) (sp + 0 * 8)) = (ptrint) pv;
100 /* cast the passed function to a patcher function */
102 patcher_function = (bool (*)(u1 *)) (ptrint) f;
104 /* enter a monitor on the patching position */
106 PATCHER_MONITORENTER;
108 /* create the stackframeinfo */
110 stacktrace_create_extern_stackframeinfo(&sfi, pv, sp + 6 * 8, ra, xpc);
112 /* call the proper patcher function */
114 result = (patcher_function)(sp);
116 /* remove the stackframeinfo */
118 stacktrace_remove_stackframeinfo(&sfi);
120 /* check for an error, get the exception and return it */
122 if (result == false) {
123 e = exceptions_get_and_clear_exception();
130 /* patch back original code */
132 mcode = *((u4 *) (sp + 3 * 8));
134 *((u4 *) xpc) = mcode;
136 /* synchronize instruction cache */
138 md_icacheflush(NULL, 0);
140 PATCHER_MARK_PATCHED_MONITOREXIT;
146 /* patcher_get_putstatic *******************************************************
150 <patched call position>
151 a73bff98 ldq t11,-104(pv)
152 a2590000 ldl a2,0(t11)
154 *******************************************************************************/
156 bool patcher_get_putstatic(u1 *sp)
158 unresolved_field *uf;
163 /* get stuff from the stack */
165 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
166 disp = *((s4 *) (sp + 1 * 8));
167 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
169 /* get the fieldinfo */
171 if (!(fi = resolve_field_eager(uf)))
174 /* check if the field's class is initialized */
176 if (!(fi->class->state & CLASS_INITIALIZED))
177 if (!initialize_class(fi->class))
180 /* patch the field value's address */
182 *((ptrint *) (pv + disp)) = (ptrint) &(fi->value);
188 /* patcher_get_putfield ********************************************************
192 <patched call position>
193 a2af0020 ldl a5,32(s6)
195 *******************************************************************************/
197 bool patcher_get_putfield(u1 *sp)
200 unresolved_field *uf;
203 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
204 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
206 /* get the fieldinfo */
208 if (!(fi = resolve_field_eager(uf)))
211 /* if we show disassembly, we have to skip the nop */
213 if (opt_showdisassemble) {
214 /* patch the field's offset into the instruction */
216 *((u4 *) (ra + 4)) |= (s2) (fi->offset & 0x0000ffff);
219 /* otherwise store the patched instruction on the stack */
221 *((u4 *) (sp + 3 * 8)) |= (s2) (fi->offset & 0x0000ffff);
228 /* patcher_aconst **************************************************************
232 <patched call postition>
233 a61bff80 ldq a0,-128(pv)
235 *******************************************************************************/
237 bool patcher_aconst(u1 *sp)
239 constant_classref *cr;
244 /* get stuff from the stack */
246 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
247 disp = *((s4 *) (sp + 1 * 8));
248 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
250 /* get the classinfo */
252 if (!(c = resolve_classref_eager(cr)))
255 /* patch the classinfo pointer */
257 *((ptrint *) (pv + disp)) = (ptrint) c;
263 /* patcher_builtin_multianewarray **********************************************
267 <patched call position>
268 a63bff80 ldq a1,-128(pv)
270 a77bff78 ldq pv,-136(pv)
273 *******************************************************************************/
275 bool patcher_builtin_multianewarray(u1 *sp)
277 constant_classref *cr;
282 /* get stuff from the stack */
284 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
285 disp = *((s4 *) (sp + 1 * 8));
286 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
288 /* get the classinfo */
290 if (!(c = resolve_classref_eager(cr)))
293 /* patch the classinfo pointer */
295 *((ptrint *) (pv + disp)) = (ptrint) c;
301 /* patcher_builtin_arraycheckcast **********************************************
305 <patched call position>
306 a63bfe60 ldq a1,-416(pv)
307 a77bfe58 ldq pv,-424(pv)
310 *******************************************************************************/
312 bool patcher_builtin_arraycheckcast(u1 *sp)
314 constant_classref *cr;
319 /* get stuff from the stack */
321 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
322 disp = *((s4 *) (sp + 1 * 8));
323 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
325 /* get the classinfo */
327 if (!(c = resolve_classref_eager(cr)))
330 /* patch the classinfo pointer */
332 *((ptrint *) (pv + disp)) = (ptrint) c;
338 /* patcher_invokestatic_special ************************************************
342 <patched call position>
343 a77bffa8 ldq pv,-88(pv)
346 ******************************************************************************/
348 bool patcher_invokestatic_special(u1 *sp)
350 unresolved_method *um;
355 /* get stuff from the stack */
357 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
358 disp = *((s4 *) (sp + 1 * 8));
359 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
361 /* get the fieldinfo */
363 if (!(m = resolve_method_eager(um)))
366 /* patch stubroutine */
368 *((ptrint *) (pv + disp)) = (ptrint) m->stubroutine;
374 /* patcher_invokevirtual *******************************************************
378 <patched call position>
379 a7900000 ldq at,0(a0)
380 a77c0100 ldq pv,256(at)
383 *******************************************************************************/
385 bool patcher_invokevirtual(u1 *sp)
388 unresolved_method *um;
391 /* get stuff from the stack */
393 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
394 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
396 /* get the fieldinfo */
398 if (!(m = resolve_method_eager(um)))
401 /* if we show disassembly, we have to skip the nop */
403 if (opt_showdisassemble)
406 /* patch vftbl index */
408 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, table[0]) +
409 sizeof(methodptr) * m->vftblindex) & 0x0000ffff);
415 /* patcher_invokeinterface *****************************************************
419 <patched call position>
420 a7900000 ldq at,0(a0)
421 a79cffa0 ldq at,-96(at)
422 a77c0018 ldq pv,24(at)
425 *******************************************************************************/
427 bool patcher_invokeinterface(u1 *sp)
430 unresolved_method *um;
433 /* get stuff from the stack */
435 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
436 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
438 /* get the fieldinfo */
440 if (!(m = resolve_method_eager(um)))
443 /* if we show disassembly, we have to skip the nop */
445 if (opt_showdisassemble)
448 /* patch interfacetable index */
450 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
451 sizeof(methodptr*) * m->class->index) & 0x0000ffff);
453 /* patch method offset */
455 *((s4 *) (ra + 4 + 4)) |=
456 (s4) ((sizeof(methodptr) * (m - m->class->methods)) & 0x0000ffff);
462 /* patcher_checkcast_instanceof_flags ******************************************
466 <patched call position>
468 *******************************************************************************/
470 bool patcher_checkcast_instanceof_flags(u1 *sp)
472 constant_classref *cr;
477 /* get stuff from the stack */
479 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
480 disp = *((s4 *) (sp + 1 * 8));
481 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
483 /* get the fieldinfo */
485 if (!(c = resolve_classref_eager(cr)))
488 /* patch class flags */
490 *((s4 *) (pv + disp)) = (s4) c->flags;
496 /* patcher_checkcast_instanceof_interface **************************************
500 <patched call position>
501 a78e0000 ldq at,0(s5)
502 a3bc001c ldl gp,28(at)
503 23bdfffd lda gp,-3(gp)
504 efa0002e ble gp,0x00000200002bf6b0
505 a7bcffe8 ldq gp,-24(at)
507 *******************************************************************************/
509 bool patcher_checkcast_instanceof_interface(u1 *sp)
512 constant_classref *cr;
515 /* get stuff from the stack */
517 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
518 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
520 /* get the fieldinfo */
522 if (!(c = resolve_classref_eager(cr)))
525 /* if we show disassembly, we have to skip the nop */
527 if (opt_showdisassemble)
530 /* patch super class index */
532 *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
534 *((s4 *) (ra + 4 * 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
535 c->index * sizeof(methodptr*)) & 0x0000ffff);
541 /* patcher_checkcast_instanceof_class ******************************************
545 <patched call position>
546 a7940000 ldq at,0(a4)
547 a7bbff28 ldq gp,-216(pv)
549 *******************************************************************************/
551 bool patcher_checkcast_instanceof_class(u1 *sp)
553 constant_classref *cr;
558 /* get stuff from the stack */
560 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
561 disp = *((s4 *) (sp + 1 * 8));
562 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
564 /* get the fieldinfo */
566 if (!(c = resolve_classref_eager(cr)))
569 /* patch super class' vftbl */
571 *((ptrint *) (pv + disp)) = (ptrint) c->vftbl;
577 /* patcher_clinit **************************************************************
581 *******************************************************************************/
583 bool patcher_clinit(u1 *sp)
587 /* get stuff from the stack */
589 c = (classinfo *) *((ptrint *) (sp + 2 * 8));
591 /* check if the class is initialized */
593 if (!(c->state & CLASS_INITIALIZED))
594 if (!initialize_class(c))
601 /* patcher_athrow_areturn ******************************************************
605 <patched call position>
607 *******************************************************************************/
609 #ifdef ENABLE_VERIFIER
610 bool patcher_athrow_areturn(u1 *sp)
612 unresolved_class *uc;
615 /* get stuff from the stack */
617 uc = (unresolved_class *) *((ptrint *) (sp + 2 * 8));
619 /* resolve the class */
621 if (!resolve_class(uc, resolveEager, false, &c))
626 #endif /* ENABLE_VERIFIER */
629 /* patcher_resolve_native ******************************************************
633 *******************************************************************************/
635 #if !defined(WITH_STATIC_CLASSPATH)
636 bool patcher_resolve_native(u1 *sp)
643 /* get stuff from the stack */
645 m = (methodinfo *) *((ptrint *) (sp + 2 * 8));
646 disp = *((s4 *) (sp + 1 * 8));
647 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
649 /* resolve native function */
651 if (!(f = native_resolve_function(m)))
654 /* patch native function pointer */
656 *((ptrint *) (pv + disp)) = (ptrint) f;
660 #endif /* !defined(WITH_STATIC_CLASSPATH) */
664 * These are local overrides for various environment variables in Emacs.
665 * Please do not remove this and leave it at the end of the file, where
666 * Emacs will automagically detect them.
667 * ---------------------------------------------------------------------
670 * indent-tabs-mode: t
674 * vim:noexpandtab:sw=4:ts=4: