* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[cacao.git] / src / vm / jit / m68k / md.c
1 /*      src/vm/jit/m68k/md.c
2
3    Copyright (C) 1996-2005, 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
7
8    This file is part of CACAO.
9
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.
14
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.
19
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
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <stdint.h>
32
33 #include "vm/types.h"
34
35 #include "vm/vm.h"
36
37
38 /*
39  *      As a sanity measuremnt we assert the offset.h values in here as m68k gets
40  *      crosscompiled for sure and noone thinks of offset.h wen changing compile flags
41  *      and subtile bugs will result...
42  *
43  *      m68k uses the trap instruction for hardware exceptions, need to register
44  *      according signal handler
45  */
46 void md_init(void) 
47 {
48 }
49
50
51 /* md_jit_method_patch_address *************************************************
52  
53    Gets the patch address of the currently compiled method. Has to be 
54    extracted from the load instructions which lead to the jump.
55
56 from asmpart.S (asm_vm_call_method):
57 84:   2879 0000 0000  moveal 0 <asm_vm_call_method-0x34>,%a4
58 8a:   4e94            jsr %a4@
59
60
61 from invokestatic / invokespecial
62 0x40290882:   247c 4029 03b4    moveal #1076429748,%a2
63 0x40290888:   4e92              jsr %a2@
64
65 from invokevirtual
66 0x40297eca:   266a 0000         moveal %a2@(0),%a3
67 0x40297ece:   246b 002c         moveal %a3@(44),%a2
68 0x40297ed2:   4e92              jsr %a2@
69
70
71
72 *******************************************************************************/
73
74 void *md_jit_method_patch_address(void *pv, void *ra, void *mptr)
75 {
76         uint8_t *pc;
77         int16_t  disp;
78         void    *pa;
79
80         pc = (uint8_t *) ra;
81
82         if (*((u2*)(pc - 2)) == 0x4e94) {               /* jsr %a4@ */
83                 if (*((u2*)(pc - 6)) == 0x286b) {
84                         /* found an invokevirtual */
85                         /* get offset of load instruction 246b XXXX */
86                         disp = *((s2*)(pc - 4));
87
88                         /* return NULL if no mptr was specified (used for replacement) */
89
90                         if (mptr == NULL)
91                                 return NULL;
92
93                         pa = ((uint8_t *) mptr) + disp;/* mptr contains the magic we want */
94                 } else  {
95                         /* we had a moveal XXX, %a3 which is a 3 word opcode */
96                         /* 2679 0000 0000 */
97                         assert(*(u2*)(pc - 8) == 0x2879);               /* moveal */
98                         pa = (void*)*((u4*)(pc - 6));                           /* another indirection ! */
99                 }
100         } else if (*((u2*)(pc - 2)) == 0x4e92)  {               /* jsr %a2@ */
101                 if (*(u2*)(pc - 8) == 0x247c)   {
102                         /* found a invokestatic/invokespecial */
103                         pa = ((u4*)(pc - 6));                   /* no indirection ! */
104                 } else {
105                         assert(0);
106                 }
107         } else {
108                 assert(0);
109         }
110
111         return pa;
112 }
113
114
115 /*
116  * These are local overrides for various environment variables in Emacs.
117  * Please do not remove this and leave it at the end of the file, where
118  * Emacs will automagically detect them.
119  * ---------------------------------------------------------------------
120  * Local variables:
121  * mode: c
122  * indent-tabs-mode: t
123  * c-basic-offset: 4
124  * tab-width: 4
125  * End:
126  * vim:noexpandtab:sw=4:ts=4:
127  */