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 5290 2006-09-04 17:12:48Z christian $
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;
145 /* patcher_initialize_class ****************************************************
147 Initalizes a given classinfo pointer. This function does not patch
150 *******************************************************************************/
152 bool patcher_initialize_class(u1 *sp)
156 /* get stuff from the stack */
158 c = (classinfo *) *((ptrint *) (sp + 2 * 8));
160 /* check if the class is initialized */
162 if (!(c->state & CLASS_INITIALIZED))
163 if (!initialize_class(c))
169 /* patcher_resolve_class *****************************************************
171 Initalizes a given classinfo pointer. This function does not patch
174 *******************************************************************************/
176 #ifdef ENABLE_VERIFIER
177 bool patcher_resolve_class(u1 *sp)
179 unresolved_class *uc;
182 /* get stuff from the stack */
184 uc = (unresolved_class *) *((ptrint *) (sp + 2 * 8));
186 /* resolve the class */
188 if (!resolve_class(uc, resolveEager, false, &c))
193 #endif /* ENABLE_VERIFIER */
196 /* patcher_resolve_classref_to_classinfo ***************************************
200 <patched call postition>
201 a61bff80 ldq a0,-128(pv)
205 <patched call position>
206 a63bff80 ldq a1,-128(pv)
208 a77bff78 ldq pv,-136(pv)
213 <patched call position>
214 a63bfe60 ldq a1,-416(pv)
215 a77bfe58 ldq pv,-424(pv)
218 *******************************************************************************/
220 bool patcher_resolve_classref_to_classinfo(u1 *sp)
222 constant_classref *cr;
227 /* get stuff from the stack */
229 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
230 disp = *((s4 *) (sp + 1 * 8));
231 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
233 /* get the classinfo */
235 if (!(c = resolve_classref_eager(cr)))
238 /* patch the classinfo pointer */
240 *((ptrint *) (pv + disp)) = (ptrint) c;
246 /* patcher_resolve_classref_to_vftbl *******************************************
251 <patched call position>
252 a7940000 ldq at,0(a4)
253 a7bbff28 ldq gp,-216(pv)
255 *******************************************************************************/
257 bool patcher_resolve_classref_to_vftbl(u1 *sp)
259 constant_classref *cr;
264 /* get stuff from the stack */
266 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
267 disp = *((s4 *) (sp + 1 * 8));
268 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
270 /* get the fieldinfo */
272 if (!(c = resolve_classref_eager(cr)))
275 /* patch super class' vftbl */
277 *((ptrint *) (pv + disp)) = (ptrint) c->vftbl;
283 /* patcher_resolve_classref_to_flags *******************************************
285 CHECKCAST/INSTANCEOF:
287 <patched call position>
289 *******************************************************************************/
291 bool patcher_resolve_classref_to_flags(u1 *sp)
293 constant_classref *cr;
298 /* get stuff from the stack */
300 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
301 disp = *((s4 *) (sp + 1 * 8));
302 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
304 /* get the fieldinfo */
306 if (!(c = resolve_classref_eager(cr)))
309 /* patch class flags */
311 *((s4 *) (pv + disp)) = (s4) c->flags;
317 /* patcher_resolve_native_function *********************************************
321 *******************************************************************************/
323 #if !defined(WITH_STATIC_CLASSPATH)
324 bool patcher_resolve_native_function(u1 *sp)
331 /* get stuff from the stack */
333 m = (methodinfo *) *((ptrint *) (sp + 2 * 8));
334 disp = *((s4 *) (sp + 1 * 8));
335 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
337 /* resolve native function */
339 if (!(f = native_resolve_function(m)))
342 /* patch native function pointer */
344 *((ptrint *) (pv + disp)) = (ptrint) f;
348 #endif /* !defined(WITH_STATIC_CLASSPATH) */
351 /* patcher_get_putstatic *******************************************************
355 <patched call position>
356 a73bff98 ldq t11,-104(pv)
357 a2590000 ldl a2,0(t11)
359 *******************************************************************************/
361 bool patcher_get_putstatic(u1 *sp)
363 unresolved_field *uf;
368 /* get stuff from the stack */
370 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
371 disp = *((s4 *) (sp + 1 * 8));
372 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
374 /* get the fieldinfo */
376 if (!(fi = resolve_field_eager(uf)))
379 /* check if the field's class is initialized */
381 if (!(fi->class->state & CLASS_INITIALIZED))
382 if (!initialize_class(fi->class))
385 /* patch the field value's address */
387 *((ptrint *) (pv + disp)) = (ptrint) &(fi->value);
393 /* patcher_get_putfield ********************************************************
397 <patched call position>
398 a2af0020 ldl a5,32(s6)
400 *******************************************************************************/
402 bool patcher_get_putfield(u1 *sp)
405 unresolved_field *uf;
408 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
409 uf = (unresolved_field *) *((ptrint *) (sp + 2 * 8));
411 /* get the fieldinfo */
413 if (!(fi = resolve_field_eager(uf)))
416 /* if we show disassembly, we have to skip the nop */
418 if (opt_showdisassemble) {
419 /* patch the field's offset into the instruction */
421 *((u4 *) (ra + 4)) |= (s2) (fi->offset & 0x0000ffff);
424 /* otherwise store the patched instruction on the stack */
426 *((u4 *) (sp + 3 * 8)) |= (s2) (fi->offset & 0x0000ffff);
433 /* patcher_invokestatic_special ************************************************
437 <patched call position>
438 a77bffa8 ldq pv,-88(pv)
441 ******************************************************************************/
443 bool patcher_invokestatic_special(u1 *sp)
445 unresolved_method *um;
450 /* get stuff from the stack */
452 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
453 disp = *((s4 *) (sp + 1 * 8));
454 pv = (u1 *) *((ptrint *) (sp + 0 * 8));
456 /* get the fieldinfo */
458 if (!(m = resolve_method_eager(um)))
461 /* patch stubroutine */
463 *((ptrint *) (pv + disp)) = (ptrint) m->stubroutine;
469 /* patcher_invokevirtual *******************************************************
473 <patched call position>
474 a7900000 ldq at,0(a0)
475 a77c0100 ldq pv,256(at)
478 *******************************************************************************/
480 bool patcher_invokevirtual(u1 *sp)
483 unresolved_method *um;
486 /* get stuff from the stack */
488 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
489 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
491 /* get the fieldinfo */
493 if (!(m = resolve_method_eager(um)))
496 /* if we show disassembly, we have to skip the nop */
498 if (opt_showdisassemble)
501 /* patch vftbl index */
503 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, table[0]) +
504 sizeof(methodptr) * m->vftblindex) & 0x0000ffff);
510 /* patcher_invokeinterface *****************************************************
514 <patched call position>
515 a7900000 ldq at,0(a0)
516 a79cffa0 ldq at,-96(at)
517 a77c0018 ldq pv,24(at)
520 *******************************************************************************/
522 bool patcher_invokeinterface(u1 *sp)
525 unresolved_method *um;
528 /* get stuff from the stack */
530 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
531 um = (unresolved_method *) *((ptrint *) (sp + 2 * 8));
533 /* get the fieldinfo */
535 if (!(m = resolve_method_eager(um)))
538 /* if we show disassembly, we have to skip the nop */
540 if (opt_showdisassemble)
543 /* patch interfacetable index */
545 *((s4 *) (ra + 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
546 sizeof(methodptr*) * m->class->index) & 0x0000ffff);
548 /* patch method offset */
550 *((s4 *) (ra + 4 + 4)) |=
551 (s4) ((sizeof(methodptr) * (m - m->class->methods)) & 0x0000ffff);
557 /* patcher_checkcast_instanceof_interface **************************************
561 <patched call position>
562 a78e0000 ldq at,0(s5)
563 a3bc001c ldl gp,28(at)
564 23bdfffd lda gp,-3(gp)
565 efa0002e ble gp,0x00000200002bf6b0
566 a7bcffe8 ldq gp,-24(at)
568 *******************************************************************************/
570 bool patcher_checkcast_instanceof_interface(u1 *sp)
573 constant_classref *cr;
576 /* get stuff from the stack */
578 ra = (u1 *) *((ptrint *) (sp + 5 * 8));
579 cr = (constant_classref *) *((ptrint *) (sp + 2 * 8));
581 /* get the fieldinfo */
583 if (!(c = resolve_classref_eager(cr)))
586 /* if we show disassembly, we have to skip the nop */
588 if (opt_showdisassemble)
591 /* patch super class index */
593 *((s4 *) (ra + 2 * 4)) |= (s4) (-(c->index) & 0x0000ffff);
595 *((s4 *) (ra + 4 * 4)) |= (s4) ((OFFSET(vftbl_t, interfacetable[0]) -
596 c->index * sizeof(methodptr*)) & 0x0000ffff);
603 * These are local overrides for various environment variables in Emacs.
604 * Please do not remove this and leave it at the end of the file, where
605 * Emacs will automagically detect them.
606 * ---------------------------------------------------------------------
609 * indent-tabs-mode: t
613 * vim:noexpandtab:sw=4:ts=4: