* src/vm/jit/code.c (code_init): New function.
[cacao.git] / src / vm / jit / jit.c
1 /* src/vm/jit/jit.c - calls the code generation functions
2
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
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    Contact: cacao@cacaojvm.org
26
27    Authors: Andreas Krall
28             Reinhard Grafl
29
30    Changes: Edwin Steiner
31             Christian Thalinger
32             Christian Ullrich
33
34    $Id: jit.c 6114 2006-12-04 22:21:49Z twisti $
35
36 */
37
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include <assert.h>
43
44 #include "mm/memory.h"
45 #include "native/native.h"
46 #include "toolbox/logging.h"
47
48 #if defined(ENABLE_THREADS)
49 # include "threads/native/lock.h"
50 #else
51 # include "threads/none/lock.h"
52 #endif
53
54 #include "vm/class.h"
55 #include "vm/global.h"
56 #include "vm/initialize.h"
57 #include "vm/loader.h"
58 #include "vm/method.h"
59 #include "vm/options.h"
60 #include "vm/statistics.h"
61 #include "vm/jit/asmpart.h"
62
63 # include "vm/jit/cfg.h"
64
65 #include "vm/jit/codegen-common.h"
66 #include "vm/jit/disass.h"
67 #include "vm/jit/dseg.h"
68 #include "vm/jit/jit.h"
69 #include "vm/jit/parse.h"
70 #include "vm/jit/reg.h"
71
72 #include "vm/jit/show.h"
73 #include "vm/jit/stack.h"
74
75 #include "vm/jit/allocator/simplereg.h"
76 #if defined(ENABLE_LSRA) && !defined(ENABLE_SSA)
77 # include "vm/jit/allocator/lsra.h"
78 #endif
79 #if defined(ENABLE_SSA)
80 # include "vm/jit/optimizing/lsra.h"
81 # include "vm/jit/optimizing/ssa.h"
82 #endif
83
84 #include "vm/jit/loop/analyze.h"
85 #include "vm/jit/loop/graph.h"
86 #include "vm/jit/loop/loop.h"
87
88 #if defined(ENABLE_IFCONV)
89 # include "vm/jit/optimizing/ifconv.h"
90 #endif
91
92 #include "vm/jit/optimizing/reorder.h"
93
94 #include "vm/jit/verify/typecheck.h"
95 #include "vm/rt-timing.h"
96
97 #if defined(ENABLE_THREADS)
98 # include "threads/native/threads.h"
99 #endif
100
101
102 /* debug macros ***************************************************************/
103
104 #if !defined(NDEBUG)
105 #define DEBUG_JIT_COMPILEVERBOSE(x) \
106     do { \
107         if (compileverbose) { \
108             log_message_method(x, m); \
109         } \
110     } while (0)
111 #else
112 #define DEBUG_JIT_COMPILEVERBOSE(x)    /* nothing */
113 #endif
114
115  
116 /* the ICMD table ************************************************************/
117
118 #if !defined(NDEBUG)
119 #define N(name)  name,
120 #else
121 #define N(name)
122 #endif
123
124 /* abbreviations for flags */
125
126 #define PEI     ICMDTABLE_PEI
127 #define CALLS   ICMDTABLE_CALLS
128
129 /* some machine dependent values */
130
131 #if SUPPORT_DIVISION
132 #define IDIV_CALLS  0
133 #else
134 #define IDIV_CALLS  ICMDTABLE_CALLS
135 #endif
136
137 #if (SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
138 #define LDIV_CALLS  0
139 #else
140 #define LDIV_CALLS  ICMDTABLE_CALLS
141 #endif
142
143 /* include the actual table */
144
145 icmdtable_entry_t icmd_table[256] = {
146 #include <vm/jit/icmdtable.inc>
147 };
148
149 #undef N
150 #undef PEI
151 #undef CALLS
152
153 /* XXX hack until the old "PEI" definition is removed */
154 #define PEI 1
155
156
157 /* stackelement requirements of Java opcodes **********************************/
158
159 int stackreq[256] = {
160         0,    /* JAVA_NOP                         0 */
161         1,    /* JAVA_ACONST                      1 */
162         1,    /* JAVA_ICONST_M1                   2 */
163         1,    /* JAVA_ICONST_0                    3 */
164         1,    /* JAVA_ICONST_1                    4 */
165         1,    /* JAVA_ICONST_2                    5 */
166         1,    /* JAVA_ICONST_3                    6 */
167         1,    /* JAVA_ICONST_4                    7 */
168         1,    /* JAVA_ICONST_5                    8 */
169         1,    /* JAVA_LCONST_0                    9 */
170         1,    /* JAVA_LCONST_1                   10 */
171         1,    /* JAVA_FCONST_0                   11 */
172         1,    /* JAVA_FCONST_1                   12 */
173         1,    /* JAVA_FCONST_2                   13 */
174         1,    /* JAVA_DCONST_0                   14 */
175         1,    /* JAVA_DCONST_1                   15 */
176         1,    /* JAVA_BIPUSH                     16 */
177         1,    /* JAVA_SIPUSH                     17 */
178         1,    /* JAVA_LDC                        18 */
179         1,    /* JAVA_LDC_W                      19 */
180         1,    /* JAVA_LDC2_W                     20 */
181         1,    /* JAVA_ILOAD                      21 */
182         1,    /* JAVA_LLOAD                      22 */
183         1,    /* JAVA_FLOAD                      23 */
184         1,    /* JAVA_DLOAD                      24 */
185         1,    /* JAVA_ALOAD                      25 */
186         1,    /* JAVA_ILOAD_0                    26 */
187         1,    /* JAVA_ILOAD_1                    27 */
188         1,    /* JAVA_ILOAD_2                    28 */
189         1,    /* JAVA_ILOAD_3                    29 */
190         1,    /* JAVA_LLOAD_0                    30 */
191         1,    /* JAVA_LLOAD_1                    31 */
192         1,    /* JAVA_LLOAD_2                    32 */
193         1,    /* JAVA_LLOAD_3                    33 */
194         1,    /* JAVA_FLOAD_0                    34 */
195         1,    /* JAVA_FLOAD_1                    35 */
196         1,    /* JAVA_FLOAD_2                    36 */
197         1,    /* JAVA_FLOAD_3                    37 */
198         1,    /* JAVA_DLOAD_0                    38 */
199         1,    /* JAVA_DLOAD_1                    39 */
200         1,    /* JAVA_DLOAD_2                    40 */
201         1,    /* JAVA_DLOAD_3                    41 */
202         1,    /* JAVA_ALOAD_0                    42 */
203         1,    /* JAVA_ALOAD_1                    43 */
204         1,    /* JAVA_ALOAD_2                    44 */
205         1,    /* JAVA_ALOAD_3                    45 */
206         1,    /* JAVA_IALOAD                     46 */
207         1,    /* JAVA_LALOAD                     47 */
208         1,    /* JAVA_FALOAD                     48 */
209         1,    /* JAVA_DALOAD                     49 */
210         1,    /* JAVA_AALOAD                     50 */
211         1,    /* JAVA_BALOAD                     51 */
212         1,    /* JAVA_CALOAD                     52 */
213         1,    /* JAVA_SALOAD                     53 */
214         0,    /* JAVA_ISTORE                     54 */
215         0,    /* JAVA_LSTORE                     55 */
216         0,    /* JAVA_FSTORE                     56 */
217         0,    /* JAVA_DSTORE                     57 */
218         0,    /* JAVA_ASTORE                     58 */
219         0,    /* JAVA_ISTORE_0                   59 */
220         0,    /* JAVA_ISTORE_1                   60 */
221         0,    /* JAVA_ISTORE_2                   61 */
222         0,    /* JAVA_ISTORE_3                   62 */
223         0,    /* JAVA_LSTORE_0                   63 */
224         0,    /* JAVA_LSTORE_1                   64 */
225         0,    /* JAVA_LSTORE_2                   65 */
226         0,    /* JAVA_LSTORE_3                   66 */
227         0,    /* JAVA_FSTORE_0                   67 */
228         0,    /* JAVA_FSTORE_1                   68 */
229         0,    /* JAVA_FSTORE_2                   69 */
230         0,    /* JAVA_FSTORE_3                   70 */
231         0,    /* JAVA_DSTORE_0                   71 */
232         0,    /* JAVA_DSTORE_1                   72 */
233         0,    /* JAVA_DSTORE_2                   73 */
234         0,    /* JAVA_DSTORE_3                   74 */
235         0,    /* JAVA_ASTORE_0                   75 */
236         0,    /* JAVA_ASTORE_1                   76 */
237         0,    /* JAVA_ASTORE_2                   77 */
238         0,    /* JAVA_ASTORE_3                   78 */
239         0,    /* JAVA_IASTORE                    79 */
240         0,    /* JAVA_LASTORE                    80 */
241         0,    /* JAVA_FASTORE                    81 */
242         0,    /* JAVA_DASTORE                    82 */
243         0,    /* JAVA_AASTORE                    83 */
244         0,    /* JAVA_BASTORE                    84 */
245         0,    /* JAVA_CASTORE                    85 */
246         0,    /* JAVA_SASTORE                    86 */
247         0,    /* JAVA_POP                        87 */
248         0,    /* JAVA_POP2                       88 */
249         1,    /* JAVA_DUP                        89 */
250         1+3,  /* JAVA_DUP_X1                     90 */
251         2+4,  /* JAVA_DUP_X2                     91 */
252         2,    /* JAVA_DUP2                       92 */
253         2+5,  /* JAVA_DUP2_X1                    93 */
254         3+6,  /* JAVA_DUP2_X2                    94 */
255         1+2,  /* JAVA_SWAP                       95 */
256         1,    /* JAVA_IADD                       96 */
257         1,    /* JAVA_LADD                       97 */
258         1,    /* JAVA_FADD                       98 */
259         1,    /* JAVA_DADD                       99 */
260         1,    /* JAVA_ISUB                      100 */
261         1,    /* JAVA_LSUB                      101 */
262         1,    /* JAVA_FSUB                      102 */
263         1,    /* JAVA_DSUB                      103 */
264         1,    /* JAVA_IMUL                      104 */
265         1,    /* JAVA_LMUL                      105 */
266         1,    /* JAVA_FMUL                      106 */
267         1,    /* JAVA_DMUL                      107 */
268         1,    /* JAVA_IDIV                      108 */
269         1,    /* JAVA_LDIV                      109 */
270         1,    /* JAVA_FDIV                      110 */
271         1,    /* JAVA_DDIV                      111 */
272         1,    /* JAVA_IREM                      112 */
273         1,    /* JAVA_LREM                      113 */
274         1,    /* JAVA_FREM                      114 */
275         1,    /* JAVA_DREM                      115 */
276         1,    /* JAVA_INEG                      116 */
277         1,    /* JAVA_LNEG                      117 */
278         1,    /* JAVA_FNEG                      118 */
279         1,    /* JAVA_DNEG                      119 */
280         1,    /* JAVA_ISHL                      120 */
281         1,    /* JAVA_LSHL                      121 */
282         1,    /* JAVA_ISHR                      122 */
283         1,    /* JAVA_LSHR                      123 */
284         1,    /* JAVA_IUSHR                     124 */
285         1,    /* JAVA_LUSHR                     125 */
286         1,    /* JAVA_IAND                      126 */
287         1,    /* JAVA_LAND                      127 */
288         1,    /* JAVA_IOR                       128 */
289         1,    /* JAVA_LOR                       129 */
290         1,    /* JAVA_IXOR                      130 */
291         1,    /* JAVA_LXOR                      131 */
292         0,    /* JAVA_IINC                      132 */
293         1,    /* JAVA_I2L                       133 */
294         1,    /* JAVA_I2F                       134 */
295         1,    /* JAVA_I2D                       135 */
296         1,    /* JAVA_L2I                       136 */
297         1,    /* JAVA_L2F                       137 */
298         1,    /* JAVA_L2D                       138 */
299         1,    /* JAVA_F2I                       139 */
300         1,    /* JAVA_F2L                       140 */
301         1,    /* JAVA_F2D                       141 */
302         1,    /* JAVA_D2I                       142 */
303         1,    /* JAVA_D2L                       143 */
304         1,    /* JAVA_D2F                       144 */
305         1,    /* JAVA_INT2BYTE                  145 */
306         1,    /* JAVA_INT2CHAR                  146 */
307         1,    /* JAVA_INT2SHORT                 147 */
308         1,    /* JAVA_LCMP                      148 */
309         1,    /* JAVA_FCMPL                     149 */
310         1,    /* JAVA_FCMPG                     150 */
311         1,    /* JAVA_DCMPL                     151 */
312         1,    /* JAVA_DCMPG                     152 */
313         0,    /* JAVA_IFEQ                      153 */
314         0,    /* JAVA_IFNE                      154 */
315         0,    /* JAVA_IFLT                      155 */
316         0,    /* JAVA_IFGE                      156 */
317         0,    /* JAVA_IFGT                      157 */
318         0,    /* JAVA_IFLE                      158 */
319         0,    /* JAVA_IF_ICMPEQ                 159 */
320         0,    /* JAVA_IF_ICMPNE                 160 */
321         0,    /* JAVA_IF_ICMPLT                 161 */
322         0,    /* JAVA_IF_ICMPGE                 162 */
323         0,    /* JAVA_IF_ICMPGT                 163 */
324         0,    /* JAVA_IF_ICMPLE                 164 */
325         0,    /* JAVA_IF_ACMPEQ                 165 */
326         0,    /* JAVA_IF_ACMPNE                 166 */
327         0,    /* JAVA_GOTO                      167 */
328         1,    /* JAVA_JSR                       168 */
329         0,    /* JAVA_RET                       169 */
330         0,    /* JAVA_TABLESWITCH               170 */
331         0,    /* JAVA_LOOKUPSWITCH              171 */
332         0,    /* JAVA_IRETURN                   172 */
333         0,    /* JAVA_LRETURN                   173 */
334         0,    /* JAVA_FRETURN                   174 */
335         0,    /* JAVA_DRETURN                   175 */
336         0,    /* JAVA_ARETURN                   176 */
337         0,    /* JAVA_RETURN                    177 */
338         1,    /* JAVA_GETSTATIC                 178 */
339         0,    /* JAVA_PUTSTATIC                 179 */
340         1,    /* JAVA_GETFIELD                  180 */
341         0,    /* JAVA_PUTFIELD                  181 */
342         1,    /* JAVA_INVOKEVIRTUAL             182 */
343         1,    /* JAVA_INVOKESPECIAL             183 */
344         1,    /* JAVA_INVOKESTATIC              184 */
345         1,    /* JAVA_INVOKEINTERFACE           185 */
346         1,    /* JAVA_UNDEF186                  186 */
347         1,    /* JAVA_NEW                       187 */
348         1,    /* JAVA_NEWARRAY                  188 */
349         1,    /* JAVA_ANEWARRAY                 189 */
350         1,    /* JAVA_ARRAYLENGTH               190 */
351         1,    /* JAVA_ATHROW                    191 */
352         1,    /* JAVA_CHECKCAST                 192 */
353         1,    /* JAVA_INSTANCEOF                193 */
354         0,    /* JAVA_MONITORENTER              194 */
355         0,    /* JAVA_MONITOREXIT               195 */
356         0,    /* JAVA_WIDE                      196 */
357         1,    /* JAVA_MULTIANEWARRAY            197 */
358         0,    /* JAVA_IFNULL                    198 */
359         0,    /* JAVA_IFNONNULL                 199 */
360         0,    /* JAVA_GOTO_W                    200 */
361         1,    /* JAVA_JSR_W                     201 */
362         0,    /* JAVA_BREAKPOINT                202 */
363         1,    /* JAVA_UNDEF203                  203 */
364         1,    /* JAVA_UNDEF204                  204 */
365         1,    /* JAVA_UNDEF205                  205 */
366         1,    /* JAVA_UNDEF206                  206 */
367         1,    /* JAVA_UNDEF207                  207 */
368         1,    /* JAVA_UNDEF208                  208 */
369         1,    /* JAVA_UNDEF209                  209 */
370         1,    /* JAVA_UNDEF210                  210 */
371         1,    /* JAVA_UNDEF211                  211 */
372         1,    /* JAVA_UNDEF212                  212 */
373         1,    /* JAVA_UNDEF213                  213 */
374         1,    /* JAVA_UNDEF214                  214 */
375         1,    /* JAVA_UNDEF215                  215 */
376         1,    /* JAVA_UNDEF216                  216 */
377         1,    /* JAVA_UNDEF217                  217 */
378         1,    /* JAVA_UNDEF218                  218 */
379         1,    /* JAVA_UNDEF219                  219 */
380         1,    /* JAVA_UNDEF220                  220 */
381         1,    /* JAVA_UNDEF221                  221 */
382         1,    /* JAVA_UNDEF222                  222 */
383         1,    /* JAVA_UNDEF223                  223 */
384         1,    /* JAVA_UNDEF224                  224 */
385         1,    /* JAVA_UNDEF225                  225 */
386         1,    /* JAVA_UNDEF226                  226 */
387         1,    /* JAVA_UNDEF227                  227 */
388         1,    /* JAVA_UNDEF228                  228 */
389         1,    /* JAVA_UNDEF229                  229 */
390         1,    /* JAVA_UNDEF230                  230 */
391         1,    /* JAVA_UNDEF231                  231 */
392         1,    /* JAVA_UNDEF232                  232 */
393         1,    /* JAVA_UNDEF233                  233 */
394         1,    /* JAVA_UNDEF234                  234 */
395         1,    /* JAVA_UNDEF235                  235 */
396         1,    /* JAVA_UNDEF236                  236 */
397         1,    /* JAVA_UNDEF237                  237 */
398         1,    /* JAVA_UNDEF238                  238 */
399         1,    /* JAVA_UNDEF239                  239 */
400         1,    /* JAVA_UNDEF240                  240 */
401         1,    /* JAVA_UNDEF241                  241 */
402         1,    /* JAVA_UNDEF242                  242 */
403         1,    /* JAVA_UNDEF243                  243 */
404         1,    /* JAVA_UNDEF244                  244 */
405         1,    /* JAVA_UNDEF245                  245 */
406         1,    /* JAVA_UNDEF246                  246 */
407         1,    /* JAVA_UNDEF247                  247 */
408         1,    /* JAVA_UNDEF248                  248 */
409         1,    /* JAVA_UNDEF249                  249 */
410         1,    /* JAVA_UNDEF250                  250 */
411         1,    /* JAVA_UNDEF251                  251 */
412         1,    /* JAVA_UNDEF252                  252 */
413         1,    /* JAVA_UNDEF253                  253 */
414         1,    /* JAVA_UNDEF254                  254 */
415         1,    /* JAVA_UNDEF255                  255 */
416 };
417
418
419 /* size in bytes of Java opcodes **********************************************/
420                                 
421 int jcommandsize[256] = {
422
423         1,    /* JAVA_NOP                         0 */
424         1,    /* JAVA_ACONST_NULL                 1 */
425         1,    /* JAVA_ICONST_M1                   2 */
426         1,    /* JAVA_ICONST_0                    3 */
427         1,    /* JAVA_ICONST_1                    4 */
428         1,    /* JAVA_ICONST_2                    5 */
429         1,    /* JAVA_ICONST_3                    6 */
430         1,    /* JAVA_ICONST_4                    7 */
431         1,    /* JAVA_ICONST_5                    8 */
432         1,    /* JAVA_LCONST_0                    9 */
433         1,    /* JAVA_LCONST_1                   10 */
434         1,    /* JAVA_FCONST_0                   11 */
435         1,    /* JAVA_FCONST_1                   12 */
436         1,    /* JAVA_FCONST_2                   13 */
437         1,    /* JAVA_DCONST_0                   14 */
438         1,    /* JAVA_DCONST_1                   15 */
439         2,    /* JAVA_BIPUSH                     16 */
440         3,    /* JAVA_SIPUSH                     17 */
441         2,    /* JAVA_LDC1                       18 */
442         3,    /* JAVA_LDC2                       19 */
443         3,    /* JAVA_LDC2W                      20 */
444         2,    /* JAVA_ILOAD                      21 */
445         2,    /* JAVA_LLOAD                      22 */
446         2,    /* JAVA_FLOAD                      23 */
447         2,    /* JAVA_DLOAD                      24 */
448         2,    /* JAVA_ALOAD                      25 */
449         1,    /* JAVA_ILOAD_0                    26 */
450         1,    /* JAVA_ILOAD_1                    27 */
451         1,    /* JAVA_ILOAD_2                    28 */
452         1,    /* JAVA_ILOAD_3                    29 */
453         1,    /* JAVA_LLOAD_0                    30 */
454         1,    /* JAVA_LLOAD_1                    31 */
455         1,    /* JAVA_LLOAD_2                    32 */
456         1,    /* JAVA_LLOAD_3                    33 */
457         1,    /* JAVA_FLOAD_0                    34 */
458         1,    /* JAVA_FLOAD_1                    35 */
459         1,    /* JAVA_FLOAD_2                    36 */
460         1,    /* JAVA_FLOAD_3                    37 */
461         1,    /* JAVA_DLOAD_0                    38 */
462         1,    /* JAVA_DLOAD_1                    39 */
463         1,    /* JAVA_DLOAD_2                    40 */
464         1,    /* JAVA_DLOAD_3                    41 */
465         1,    /* JAVA_ALOAD_0                    42 */
466         1,    /* JAVA_ALOAD_1                    43 */
467         1,    /* JAVA_ALOAD_2                    44 */
468         1,    /* JAVA_ALOAD_3                    45 */
469         1,    /* JAVA_IALOAD                     46 */
470         1,    /* JAVA_LALOAD                     47 */
471         1,    /* JAVA_FALOAD                     48 */
472         1,    /* JAVA_DALOAD                     49 */
473         1,    /* JAVA_AALOAD                     50 */
474         1,    /* JAVA_BALOAD                     51 */
475         1,    /* JAVA_CALOAD                     52 */
476         1,    /* JAVA_SALOAD                     53 */
477         2,    /* JAVA_ISTORE                     54 */
478         2,    /* JAVA_LSTORE                     55 */
479         2,    /* JAVA_FSTORE                     56 */
480         2,    /* JAVA_DSTORE                     57 */
481         2,    /* JAVA_ASTORE                     58 */
482         1,    /* JAVA_ISTORE_0                   59 */
483         1,    /* JAVA_ISTORE_1                   60 */
484         1,    /* JAVA_ISTORE_2                   61 */
485         1,    /* JAVA_ISTORE_3                   62 */
486         1,    /* JAVA_LSTORE_0                   63 */
487         1,    /* JAVA_LSTORE_1                   64 */
488         1,    /* JAVA_LSTORE_2                   65 */
489         1,    /* JAVA_LSTORE_3                   66 */
490         1,    /* JAVA_FSTORE_0                   67 */
491         1,    /* JAVA_FSTORE_1                   68 */
492         1,    /* JAVA_FSTORE_2                   69 */
493         1,    /* JAVA_FSTORE_3                   70 */
494         1,    /* JAVA_DSTORE_0                   71 */
495         1,    /* JAVA_DSTORE_1                   72 */
496         1,    /* JAVA_DSTORE_2                   73 */
497         1,    /* JAVA_DSTORE_3                   74 */
498         1,    /* JAVA_ASTORE_0                   75 */
499         1,    /* JAVA_ASTORE_1                   76 */
500         1,    /* JAVA_ASTORE_2                   77 */
501         1,    /* JAVA_ASTORE_3                   78 */
502         1,    /* JAVA_IASTORE                    79 */
503         1,    /* JAVA_LASTORE                    80 */
504         1,    /* JAVA_FASTORE                    81 */
505         1,    /* JAVA_DASTORE                    82 */
506         1,    /* JAVA_AASTORE                    83 */
507         1,    /* JAVA_BASTORE                    84 */
508         1,    /* JAVA_CASTORE                    85 */
509         1,    /* JAVA_SASTORE                    86 */
510         1,    /* JAVA_POP                        87 */
511         1,    /* JAVA_POP2                       88 */
512         1,    /* JAVA_DUP                        89 */
513         1,    /* JAVA_DUP_X1                     90 */
514         1,    /* JAVA_DUP_X2                     91 */
515         1,    /* JAVA_DUP2                       92 */
516         1,    /* JAVA_DUP2_X1                    93 */
517         1,    /* JAVA_DUP2_X2                    94 */
518         1,    /* JAVA_SWAP                       95 */
519         1,    /* JAVA_IADD                       96 */
520         1,    /* JAVA_LADD                       97 */
521         1,    /* JAVA_FADD                       98 */
522         1,    /* JAVA_DADD                       99 */
523         1,    /* JAVA_ISUB                      100 */
524         1,    /* JAVA_LSUB                      101 */
525         1,    /* JAVA_FSUB                      102 */
526         1,    /* JAVA_DSUB                      103 */
527         1,    /* JAVA_IMUL                      104 */
528         1,    /* JAVA_LMUL                      105 */
529         1,    /* JAVA_FMUL                      106 */
530         1,    /* JAVA_DMUL                      107 */
531         1,    /* JAVA_IDIV                      108 */
532         1,    /* JAVA_LDIV                      109 */
533         1,    /* JAVA_FDIV                      110 */
534         1,    /* JAVA_DDIV                      111 */
535         1,    /* JAVA_IREM                      112 */
536         1,    /* JAVA_LREM                      113 */
537         1,    /* JAVA_FREM                      114 */
538         1,    /* JAVA_DREM                      115 */
539         1,    /* JAVA_INEG                      116 */
540         1,    /* JAVA_LNEG                      117 */
541         1,    /* JAVA_FNEG                      118 */
542         1,    /* JAVA_DNEG                      119 */
543         1,    /* JAVA_ISHL                      120 */
544         1,    /* JAVA_LSHL                      121 */
545         1,    /* JAVA_ISHR                      122 */
546         1,    /* JAVA_LSHR                      123 */
547         1,    /* JAVA_IUSHR                     124 */
548         1,    /* JAVA_LUSHR                     125 */
549         1,    /* JAVA_IAND                      126 */
550         1,    /* JAVA_LAND                      127 */
551         1,    /* JAVA_IOR                       128 */
552         1,    /* JAVA_LOR                       129 */
553         1,    /* JAVA_IXOR                      130 */
554         1,    /* JAVA_LXOR                      131 */
555         3,    /* JAVA_IINC                      132 */
556         1,    /* JAVA_I2L                       133 */
557         1,    /* JAVA_I2F                       134 */
558         1,    /* JAVA_I2D                       135 */
559         1,    /* JAVA_L2I                       136 */
560         1,    /* JAVA_L2F                       137 */
561         1,    /* JAVA_L2D                       138 */
562         1,    /* JAVA_F2I                       139 */
563         1,    /* JAVA_F2L                       140 */
564         1,    /* JAVA_F2D                       141 */
565         1,    /* JAVA_D2I                       142 */
566         1,    /* JAVA_D2L                       143 */
567         1,    /* JAVA_D2F                       144 */
568         1,    /* JAVA_INT2BYTE                  145 */
569         1,    /* JAVA_INT2CHAR                  146 */
570         1,    /* JAVA_INT2SHORT                 147 */
571         1,    /* JAVA_LCMP                      148 */
572         1,    /* JAVA_FCMPL                     149 */
573         1,    /* JAVA_FCMPG                     150 */
574         1,    /* JAVA_DCMPL                     151 */
575         1,    /* JAVA_DCMPG                     152 */
576         3,    /* JAVA_IFEQ                      153 */
577         3,    /* JAVA_IFNE                      154 */
578         3,    /* JAVA_IFLT                      155 */
579         3,    /* JAVA_IFGE                      156 */
580         3,    /* JAVA_IFGT                      157 */
581         3,    /* JAVA_IFLE                      158 */
582         3,    /* JAVA_IF_ICMPEQ                 159 */
583         3,    /* JAVA_IF_ICMPNE                 160 */
584         3,    /* JAVA_IF_ICMPLT                 161 */
585         3,    /* JAVA_IF_ICMPGE                 162 */
586         3,    /* JAVA_IF_ICMPGT                 163 */
587         3,    /* JAVA_IF_ICMPLE                 164 */
588         3,    /* JAVA_IF_ACMPEQ                 165 */
589         3,    /* JAVA_IF_ACMPNE                 166 */
590         3,    /* JAVA_GOTO                      167 */
591         3,    /* JAVA_JSR                       168 */
592         2,    /* JAVA_RET                       169 */
593         0,    /* JAVA_TABLESWITCH               170 */ /* variable length */
594         0,    /* JAVA_LOOKUPSWITCH              171 */ /* variable length */
595         1,    /* JAVA_IRETURN                   172 */
596         1,    /* JAVA_LRETURN                   173 */
597         1,    /* JAVA_FRETURN                   174 */
598         1,    /* JAVA_DRETURN                   175 */
599         1,    /* JAVA_ARETURN                   176 */
600         1,    /* JAVA_RETURN                    177 */
601         3,    /* JAVA_GETSTATIC                 178 */
602         3,    /* JAVA_PUTSTATIC                 179 */
603         3,    /* JAVA_GETFIELD                  180 */
604         3,    /* JAVA_PUTFIELD                  181 */
605         3,    /* JAVA_INVOKEVIRTUAL             182 */
606         3,    /* JAVA_INVOKESPECIAL             183 */
607         3,    /* JAVA_INVOKESTATIC              184 */
608         5,    /* JAVA_INVOKEINTERFACE           185 */
609         1,    /* UNDEF186 */
610         3,    /* JAVA_NEW                       187 */
611         2,    /* JAVA_NEWARRAY                  188 */
612         3,    /* JAVA_ANEWARRAY                 189 */
613         1,    /* JAVA_ARRAYLENGTH               190 */
614         1,    /* JAVA_ATHROW                    191 */
615         3,    /* JAVA_CHECKCAST                 192 */
616         3,    /* JAVA_INSTANCEOF                193 */
617         1,    /* JAVA_MONITORENTER              194 */
618         1,    /* JAVA_MONITOREXIT               195 */
619         0,    /* JAVA_WIDE                      196 */ /* variable length */
620         4,    /* JAVA_MULTIANEWARRAY            197 */
621         3,    /* JAVA_IFNULL                    198 */
622         3,    /* JAVA_IFNONNULL                 199 */
623         5,    /* JAVA_GOTO_W                    200 */
624         5,    /* JAVA_JSR_W                     201 */
625         1,    /* JAVA_BREAKPOINT                202 */
626
627         1,    /* UNDEF203 */
628         1,
629         1,
630         1,
631         1,
632         1,
633         1,
634         1,    /* UNDEF210 */
635         1,
636         1,
637         1,
638         1,
639         1,
640         1,
641         1,
642         1,
643         1,
644         1,    /* UNDEF220 */
645         1,
646         1,
647         1,
648         1,
649         1,
650         1,
651         1,
652         1,
653         1,
654         1,    /* UNDEF230 */
655         1,
656         1,
657         1,
658         1,
659
660         /* unused */
661                 1,1,1,1,1,1,
662         1,1,1,1,1,1,1,1,1,1,
663         1,1,1,1,1
664 };
665
666
667 /* Java opcode names *********************************************************/
668
669 char *opcode_names[256] = {
670         "NOP            ", /*               0 */
671         "ACONST         ", /*               1 */
672         "ICONST_M1      ", /* ICONST_M1     2 */
673         "ICONST_0       ", /* ICONST_0      3 */
674         "ICONST_1       ", /* ICONST_1      4 */
675         "ICONST_2       ", /* ICONST_2      5 */
676         "ICONST_3       ", /* ICONST_3      6 */
677         "ICONST_4       ", /* ICONST_4      7 */
678         "ICONST_5       ", /* ICONST_5      8 */
679         "LCONST_0       ", /* LCONST_0      9 */
680         "LCONST_1       ", /* LCONST_1     10 */
681         "FCONST_0       ", /* FCONST_0     11 */
682         "FCONST_1       ", /* FCONST_1     12 */
683         "FCONST_2       ", /* FCONST_2     13 */
684         "DCONST_0       ", /* DCONST_0     14 */
685         "DCONST_1       ", /* DCONST_1     15 */
686         "BIPUSH         ", /* BIPUSH       16 */
687         "SIPUSH         ", /* SIPUSH       17 */
688         "LDC            ", /* LDC          18 */
689         "LDC_W          ", /* LDC_W        19 */
690         "LDC2_W         ", /* LDC2_W       20 */
691         "ILOAD          ", /*              21 */
692         "LLOAD          ", /*              22 */
693         "FLOAD          ", /*              23 */
694         "DLOAD          ", /*              24 */
695         "ALOAD          ", /*              25 */
696         "ILOAD_0        ", /* ILOAD_0      26 */
697         "ILOAD_1        ", /* ILOAD_1      27 */
698         "ILOAD_2        ", /* ILOAD_2      28 */
699         "ILOAD_3        ", /* ILOAD_3      29 */
700         "LLOAD_0        ", /* LLOAD_0      30 */
701         "LLOAD_1        ", /* LLOAD_1      31 */
702         "LLOAD_2        ", /* LLOAD_2      32 */
703         "LLOAD_3        ", /* LLOAD_3      33 */
704         "FLOAD_0        ", /* FLOAD_0      34 */
705         "FLOAD_1        ", /* FLOAD_1      35 */
706         "FLOAD_2        ", /* FLOAD_2      36 */
707         "FLOAD_3        ", /* FLOAD_3      37 */
708         "DLOAD_0        ", /* DLOAD_0      38 */
709         "DLOAD_1        ", /* DLOAD_1      39 */
710         "DLOAD_2        ", /* DLOAD_2      40 */ 
711         "DLOAD_3        ", /* DLOAD_3      41 */
712         "ALOAD_0        ", /* ALOAD_0      42 */
713         "ALOAD_1        ", /* ALOAD_1      43 */
714         "ALOAD_2        ", /* ALOAD_2      44 */
715         "ALOAD_3        ", /* ALOAD_3      45 */
716         "IALOAD         ", /*              46 */
717         "LALOAD         ", /*              47 */
718         "FALOAD         ", /*              48 */
719         "DALOAD         ", /*              49 */
720         "AALOAD         ", /*              50 */
721         "BALOAD         ", /*              51 */
722         "CALOAD         ", /*              52 */
723         "SALOAD         ", /*              53 */
724         "ISTORE         ", /*              54 */
725         "LSTORE         ", /*              55 */
726         "FSTORE         ", /*              56 */
727         "DSTORE         ", /*              57 */
728         "ASTORE         ", /*              58 */
729         "ISTORE_0       ", /* ISTORE_0     59 */
730         "ISTORE_1       ", /* ISTORE_1     60 */
731         "ISTORE_2       ", /* ISTORE_2     61 */
732         "ISTORE_3       ", /* ISTORE_3     62 */
733         "LSTORE_0       ", /* LSTORE_0     63 */
734         "LSTORE_1       ", /* LSTORE_1     64 */
735         "LSTORE_2       ", /* LSTORE_2     65 */
736         "LSTORE_3       ", /* LSTORE_3     66 */
737         "FSTORE_0       ", /* FSTORE_0     67 */
738         "FSTORE_1       ", /* FSTORE_1     68 */
739         "FSTORE_2       ", /* FSTORE_2     69 */
740         "FSTORE_3       ", /* FSTORE_3     70 */
741         "DSTORE_0       ", /* DSTORE_0     71 */
742         "DSTORE_1       ", /* DSTORE_1     72 */
743         "DSTORE_2       ", /* DSTORE_2     73 */
744         "DSTORE_3       ", /* DSTORE_3     74 */
745         "ASTORE_0       ", /* ASTORE_0     75 */
746         "ASTORE_1       ", /* ASTORE_1     76 */
747         "ASTORE_2       ", /* ASTORE_2     77 */
748         "ASTORE_3       ", /* ASTORE_3     78 */
749         "IASTORE        ", /*              79 */
750         "LASTORE        ", /*              80 */
751         "FASTORE        ", /*              81 */
752         "DASTORE        ", /*              82 */
753         "AASTORE        ", /*              83 */
754         "BASTORE        ", /*              84 */
755         "CASTORE        ", /*              85 */
756         "SASTORE        ", /*              86 */
757         "POP            ", /*              87 */
758         "POP2           ", /*              88 */
759         "DUP            ", /*              89 */
760         "DUP_X1         ", /*              90 */
761         "DUP_X2         ", /*              91 */
762         "DUP2           ", /*              92 */
763         "DUP2_X1        ", /*              93 */
764         "DUP2_X2        ", /*              94 */
765         "SWAP           ", /*              95 */
766         "IADD           ", /*              96 */
767         "LADD           ", /*              97 */
768         "FADD           ", /*              98 */
769         "DADD           ", /*              99 */
770         "ISUB           ", /*             100 */
771         "LSUB           ", /*             101 */
772         "FSUB           ", /*             102 */
773         "DSUB           ", /*             103 */
774         "IMUL           ", /*             104 */
775         "LMUL           ", /*             105 */
776         "FMUL           ", /*             106 */
777         "DMUL           ", /*             107 */
778         "IDIV           ", /*             108 */
779         "LDIV           ", /*             109 */
780         "FDIV           ", /*             110 */
781         "DDIV           ", /*             111 */
782         "IREM           ", /*             112 */
783         "LREM           ", /*             113 */
784         "FREM           ", /*             114 */
785         "DREM           ", /*             115 */
786         "INEG           ", /*             116 */
787         "LNEG           ", /*             117 */
788         "FNEG           ", /*             118 */
789         "DNEG           ", /*             119 */
790         "ISHL           ", /*             120 */
791         "LSHL           ", /*             121 */
792         "ISHR           ", /*             122 */
793         "LSHR           ", /*             123 */
794         "IUSHR          ", /*             124 */
795         "LUSHR          ", /*             125 */
796         "IAND           ", /*             126 */
797         "LAND           ", /*             127 */
798         "IOR            ", /*             128 */
799         "LOR            ", /*             129 */
800         "IXOR           ", /*             130 */
801         "LXOR           ", /*             131 */
802         "IINC           ", /*             132 */
803         "I2L            ", /*             133 */
804         "I2F            ", /*             134 */
805         "I2D            ", /*             135 */
806         "L2I            ", /*             136 */
807         "L2F            ", /*             137 */
808         "L2D            ", /*             138 */
809         "F2I            ", /*             139 */
810         "F2L            ", /*             140 */
811         "F2D            ", /*             141 */
812         "D2I            ", /*             142 */
813         "D2L            ", /*             143 */
814         "D2F            ", /*             144 */
815         "INT2BYTE       ", /*             145 */
816         "INT2CHAR       ", /*             146 */
817         "INT2SHORT      ", /*             147 */
818         "LCMP           ", /*             148 */
819         "FCMPL          ", /*             149 */
820         "FCMPG          ", /*             150 */
821         "DCMPL          ", /*             151 */
822         "DCMPG          ", /*             152 */
823         "IFEQ           ", /*             153 */
824         "IFNE           ", /*             154 */
825         "IFLT           ", /*             155 */
826         "IFGE           ", /*             156 */
827         "IFGT           ", /*             157 */
828         "IFLE           ", /*             158 */
829         "IF_ICMPEQ      ", /*             159 */
830         "IF_ICMPNE      ", /*             160 */
831         "IF_ICMPLT      ", /*             161 */
832         "IF_ICMPGE      ", /*             162 */
833         "IF_ICMPGT      ", /*             163 */
834         "IF_ICMPLE      ", /*             164 */
835         "IF_ACMPEQ      ", /*             165 */
836         "IF_ACMPNE      ", /*             166 */
837         "GOTO           ", /*             167 */
838         "JSR            ", /*             168 */
839         "RET            ", /*             169 */
840         "TABLESWITCH    ", /*             170 */
841         "LOOKUPSWITCH   ", /*             171 */
842         "IRETURN        ", /*             172 */
843         "LRETURN        ", /*             173 */
844         "FRETURN        ", /*             174 */
845         "DRETURN        ", /*             175 */
846         "ARETURN        ", /*             176 */
847         "RETURN         ", /*             177 */
848         "GETSTATIC      ", /*             178 */
849         "PUTSTATIC      ", /*             179 */
850         "GETFIELD       ", /*             180 */
851         "PUTFIELD       ", /*             181 */
852         "INVOKEVIRTUAL  ", /*             182 */
853         "INVOKESPECIAL  ", /*             183 */
854         "INVOKESTATIC   ", /*             184 */
855         "INVOKEINTERFACE", /*             185 */
856         "UNDEF186       ", /*             186 */
857         "NEW            ", /*             187 */
858         "NEWARRAY       ", /*             188 */
859         "ANEWARRAY      ", /*             189 */
860         "ARRAYLENGTH    ", /*             190 */
861         "ATHROW         ", /*             191 */
862         "CHECKCAST      ", /*             192 */
863         "INSTANCEOF     ", /*             193 */
864         "MONITORENTER   ", /*             194 */
865         "MONITOREXIT    ", /*             195 */
866         "WIDE           ", /* WIDE        196 */
867         "MULTIANEWARRAY ", /*             197 */
868         "IFNULL         ", /*             198 */
869         "IFNONNULL      ", /*             199 */
870         "GOTO_W         ", /* GOTO_W      200 */
871         "JSR_W          ", /* JSR_W       201 */
872         "BREAKPOINT     ", /* BREAKPOINT  202 */
873
874                                 "UNDEF203", "UNDEF204", "UNDEF205",
875         "UNDEF206", "UNDEF207", "UNDEF208", "UNDEF209", "UNDEF210",
876         "UNDEF211", "UNDEF212", "UNDEF213", "UNDEF214", "UNDEF215",
877         "UNDEF216", "UNDEF217", "UNDEF218", "UNDEF219", "UNDEF220",
878         "UNDEF221", "UNDEF222", "UNDEF223", "UNDEF224", "UNDEF225",
879         "UNDEF226", "UNDEF227", "UNDEF228", "UNDEF229", "UNDEF230",
880         "UNDEF231", "UNDEF232", "UNDEF233", "UNDEF234", "UNDEF235",
881         "UNDEF236", "UNDEF237", "UNDEF238", "UNDEF239", "UNDEF240",
882         "UNDEF241", "UNDEF242", "UNDEF243", "UNDEF244", "UNDEF245",
883         "UNDEF246", "UNDEF247", "UNDEF248", "UNDEF249", "UNDEF250",
884         "UNDEF251", "UNDEF252", "UNDEF253", "UNDEF254", "UNDEF255"
885 };
886
887
888 /* jit_init ********************************************************************
889
890    Initializes the JIT subsystem.
891
892 *******************************************************************************/
893
894 void jit_init(void)
895 {
896 #if defined(ENABLE_JIT)
897         /* initialize stack analysis subsystem */
898
899         (void) stack_init();
900 #endif
901
902         /* initialize show subsystem */
903
904 #if !defined(NDEBUG)
905         (void) show_init();
906 #endif
907
908         /* initialize codegen subsystem */
909
910         codegen_init();
911
912         /* initialize code subsystem */
913
914         (void) code_init();
915 }
916
917
918 /* jit_close *******************************************************************
919
920    Close the JIT subsystem.
921
922 *******************************************************************************/
923
924 void jit_close(void)
925 {
926         /* do nothing */
927 }
928
929
930 /* dummy function, used when there is no JavaVM code available                */
931
932 static u1 *do_nothing_function(void)
933 {
934         return NULL;
935 }
936
937
938 /* jit_jitdata_new *************************************************************
939
940    Allocates and initalizes a new jitdata structure.
941
942 *******************************************************************************/
943
944 jitdata *jit_jitdata_new(methodinfo *m)
945 {
946         jitdata *jd;
947
948         /* allocate jitdata structure and fill it */
949
950         jd = DNEW(jitdata);
951
952         jd->m     = m;
953         jd->cd    = DNEW(codegendata);
954         jd->rd    = DNEW(registerdata);
955 #if defined(ENABLE_LOOP)
956         jd->ld    = DNEW(loopdata);
957 #endif
958
959         /* Allocate codeinfo memory from the heap as we need to keep them. */
960
961         jd->code  = code_codeinfo_new(m);
962
963         /* initialize variables */
964
965         jd->flags = 0;
966         jd->exceptiontable = NULL;
967         jd->exceptiontablelength = 0;
968         jd->returncount = 0;
969         jd->branchtoentry = false;
970         jd->branchtoend = false;
971         jd->returncount = 0;
972         jd->returnblock = NULL;
973         jd->maxlocals = m->maxlocals;
974
975 #if defined(ENABLE_THREADS)
976         if (checksync && (m->flags & ACC_SYNCHRONIZED))
977                 jd->isleafmethod = false;
978         else
979 #endif
980                 jd->isleafmethod = true;
981
982         return jd;
983 }
984
985
986 /* jit_compile *****************************************************************
987
988    Translates one method to machine code.
989
990 *******************************************************************************/
991
992 static u1 *jit_compile_intern(jitdata *jd);
993
994 u1 *jit_compile(methodinfo *m)
995 {
996         u1      *r;
997         jitdata *jd;
998         s4       dumpsize;
999
1000         STATISTICS(count_jit_calls++);
1001
1002         /* Initialize the static function's class. */
1003
1004         /* ATTENTION: This MUST be done before the method lock is aquired,
1005            otherwise we could run into a deadlock with <clinit>'s that
1006            call static methods of it's own class. */
1007
1008         if ((m->flags & ACC_STATIC) && !(m->class->state & CLASS_INITIALIZED)) {
1009 #if !defined(NDEBUG)
1010                 if (initverbose)
1011                         log_message_class("Initialize class ", m->class);
1012 #endif
1013
1014                 if (!initialize_class(m->class))
1015                         return NULL;
1016
1017                 /* check if the method has been compiled during initialization */
1018
1019                 if ((m->code != NULL) && (m->code->entrypoint != NULL))
1020                         return m->code->entrypoint;
1021         }
1022
1023         /* enter a monitor on the method */
1024
1025         LOCK_MONITOR_ENTER(m);
1026
1027         /* if method has been already compiled return immediately */
1028
1029         if (m->code != NULL) {
1030                 LOCK_MONITOR_EXIT(m);
1031
1032                 assert(m->code->entrypoint);
1033                 return m->code->entrypoint;
1034         }
1035
1036         STATISTICS(count_methods++);
1037
1038 #if defined(ENABLE_STATISTICS)
1039         /* measure time */
1040
1041         if (opt_getcompilingtime)
1042                 compilingtime_start();
1043 #endif
1044
1045         /* mark start of dump memory area */
1046
1047         dumpsize = dump_size();
1048
1049         /* create jitdata structure */
1050
1051         jd = jit_jitdata_new(m);
1052
1053         /* set the flags for the current JIT run */
1054
1055         jd->flags = JITDATA_FLAG_PARSE;
1056
1057 #if defined(ENABLE_VERIFIER)
1058         if (opt_verify)
1059                 jd->flags |= JITDATA_FLAG_VERIFY;
1060 #endif
1061
1062 #if defined(ENABLE_PROFILING)
1063         if (opt_prof)
1064                 jd->flags |= JITDATA_FLAG_INSTRUMENT;
1065 #endif
1066
1067 #if defined(ENABLE_IFCONV)
1068         if (opt_ifconv)
1069                 jd->flags |= JITDATA_FLAG_IFCONV;
1070 #endif
1071
1072         if (opt_showintermediate)
1073                 jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
1074
1075         if (opt_showdisassemble)
1076                 jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
1077
1078         if (opt_verbosecall)
1079                 jd->flags |= JITDATA_FLAG_VERBOSECALL;
1080
1081 #if defined(ENABLE_JIT)
1082 # if defined(ENABLE_INTRP)
1083         if (!opt_intrp)
1084 # endif
1085                 /* initialize the register allocator */
1086         {
1087                 reg_setup(jd);
1088         }
1089 #endif
1090
1091         /* setup the codegendata memory */
1092
1093         codegen_setup(jd);
1094
1095         /* now call internal compile function */
1096
1097         r = jit_compile_intern(jd);
1098
1099         if (r == NULL) {
1100                 /* We had an exception! Finish stuff here if necessary. */
1101
1102                 /* release codeinfo */
1103
1104                 code_codeinfo_free(jd->code);
1105
1106                 /* Release memory for basic block profiling information. */
1107
1108                 if (JITDATA_HAS_FLAG_INSTRUMENT(jd))
1109                         if (jd->code->bbfrequency != NULL)
1110                                 MFREE(jd->code->bbfrequency, u4, jd->code->basicblockcount);
1111         }
1112         else {
1113                 DEBUG_JIT_COMPILEVERBOSE("Running: ");
1114         }
1115
1116         /* release dump area */
1117
1118         dump_release(dumpsize);
1119
1120 #if defined(ENABLE_STATISTICS)
1121         /* measure time */
1122
1123         if (opt_getcompilingtime)
1124                 compilingtime_stop();
1125 #endif
1126
1127         /* leave the monitor */
1128
1129         LOCK_MONITOR_EXIT(m);
1130
1131         /* return pointer to the methods entry point */
1132
1133         return r;
1134 }
1135
1136
1137 /* jit_recompile ***************************************************************
1138
1139    Recompiles a Java method.
1140
1141 *******************************************************************************/
1142
1143 u1 *jit_recompile(methodinfo *m)
1144 {
1145         u1      *r;
1146         jitdata *jd;
1147         u1       optlevel;
1148         s4       dumpsize;
1149
1150         /* check for max. optimization level */
1151
1152         optlevel = (m->code) ? m->code->optlevel : 0;
1153
1154 #if 0
1155         if (optlevel == 1) {
1156 /*              log_message_method("not recompiling: ", m); */
1157                 return NULL;
1158         }
1159 #endif
1160
1161         DEBUG_JIT_COMPILEVERBOSE("Recompiling start: ");
1162
1163         STATISTICS(count_jit_calls++);
1164
1165 #if defined(ENABLE_STATISTICS)
1166         /* measure time */
1167
1168         if (opt_getcompilingtime)
1169                 compilingtime_start();
1170 #endif
1171
1172         /* mark start of dump memory area */
1173
1174         dumpsize = dump_size();
1175
1176         /* create jitdata structure */
1177
1178         jd = jit_jitdata_new(m);
1179
1180         /* set the current optimization level to the previous one plus 1 */
1181
1182         jd->code->optlevel = optlevel + 1;
1183
1184         /* get the optimization flags for the current JIT run */
1185
1186 #if defined(ENABLE_VERIFIER)
1187         jd->flags |= JITDATA_FLAG_VERIFY;
1188 #endif
1189
1190         /* jd->flags |= JITDATA_FLAG_REORDER; */
1191         if (opt_showintermediate)
1192                 jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
1193         if (opt_showdisassemble)
1194                 jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
1195         if (opt_verbosecall)
1196                 jd->flags |= JITDATA_FLAG_VERBOSECALL;
1197
1198 #if defined(ENABLE_JIT)
1199 # if defined(ENABLE_INTRP)
1200         if (!opt_intrp)
1201 # endif
1202                 /* initialize the register allocator */
1203
1204                 reg_setup(jd);
1205 #endif
1206
1207         /* setup the codegendata memory */
1208
1209         codegen_setup(jd);
1210
1211         /* now call internal compile function */
1212
1213         r = jit_compile_intern(jd);
1214
1215         if (r == NULL) {
1216                 /* We had an exception! Finish stuff here if necessary. */
1217
1218                 /* release codeinfo */
1219
1220                 code_codeinfo_free(jd->code);
1221         }
1222
1223         /* release dump area */
1224
1225         dump_release(dumpsize);
1226
1227 #if defined(ENABLE_STATISTICS)
1228         /* measure time */
1229
1230         if (opt_getcompilingtime)
1231                 compilingtime_stop();
1232 #endif
1233
1234         DEBUG_JIT_COMPILEVERBOSE("Recompiling done: ");
1235
1236         /* return pointer to the methods entry point */
1237
1238         return r;
1239 }
1240
1241
1242 /* jit_compile_intern **********************************************************
1243
1244    Static internal function which does the actual compilation.
1245
1246 *******************************************************************************/
1247
1248 static u1 *jit_compile_intern(jitdata *jd)
1249 {
1250         methodinfo  *m;
1251         codegendata *cd;
1252         codeinfo    *code;
1253
1254 #if defined(ENABLE_RT_TIMING)
1255         struct timespec time_start,time_checks,time_parse,time_stack,
1256                                         time_typecheck,time_loop,time_ifconv,time_alloc,
1257                                         time_rplpoints,time_codegen;
1258 #endif
1259         
1260         RT_TIMING_GET_TIME(time_start);
1261
1262         /* get required compiler data */
1263
1264 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1265         jd->ls = NULL;
1266 #endif
1267         m    = jd->m;
1268         code = jd->code;
1269         cd   = jd->cd;
1270         
1271         /* print log message for compiled method */
1272
1273         DEBUG_JIT_COMPILEVERBOSE("Compiling: ");
1274
1275         /* handle native methods and create a native stub */
1276
1277         if (m->flags & ACC_NATIVE) {
1278                 functionptr f;
1279
1280 #if defined(WITH_STATIC_CLASSPATH)
1281                 f = native_findfunction(m->class->name, m->name, m->descriptor,
1282                                                                 (m->flags & ACC_STATIC));
1283                 if (f == NULL)
1284                         return NULL;
1285 #else
1286                 f = NULL;
1287 #endif
1288
1289                 code = codegen_createnativestub(f, m);
1290
1291                 assert(!m->code); /* native methods are never recompiled */
1292                 m->code = code;
1293                 
1294                 return code->entrypoint;
1295         }
1296
1297         /* if there is no javacode, print error message and return empty method   */
1298
1299         if (m->jcode == NULL) {
1300                 DEBUG_JIT_COMPILEVERBOSE("No code given for: ");
1301
1302                 code->entrypoint = (u1 *) (ptrint) do_nothing_function;
1303                 m->code = code;
1304
1305                 return code->entrypoint;        /* return empty method                */
1306         }
1307
1308 #if defined(ENABLE_STATISTICS)
1309         if (opt_stat) {
1310                 count_javacodesize += m->jcodelength + 18;
1311                 count_tryblocks    += jd->exceptiontablelength;
1312                 count_javaexcsize  += jd->exceptiontablelength * SIZEOF_VOID_P;
1313         }
1314 #endif
1315
1316         RT_TIMING_GET_TIME(time_checks);
1317
1318         /* call the compiler passes ***********************************************/
1319
1320         DEBUG_JIT_COMPILEVERBOSE("Parsing: ");
1321
1322         /* call parse pass */
1323
1324         if (!parse(jd)) {
1325                 DEBUG_JIT_COMPILEVERBOSE("Exception while parsing: ");
1326
1327                 return NULL;
1328         }
1329         RT_TIMING_GET_TIME(time_parse);
1330
1331         DEBUG_JIT_COMPILEVERBOSE("Parsing done: ");
1332         
1333 #if defined(ENABLE_JIT)
1334 # if defined(ENABLE_INTRP)
1335         if (!opt_intrp) {
1336 # endif
1337                 DEBUG_JIT_COMPILEVERBOSE("Analysing: ");
1338
1339                 /* call stack analysis pass */
1340
1341                 if (!stack_analyse(jd)) {
1342                         DEBUG_JIT_COMPILEVERBOSE("Exception while analysing: ");
1343
1344                         return NULL;
1345                 }
1346                 RT_TIMING_GET_TIME(time_stack);
1347
1348                 DEBUG_JIT_COMPILEVERBOSE("Analysing done: ");
1349
1350                 /* Build the CFG.  This has to be done after stack_analyse, as
1351                    there happens the JSR elimination. */
1352
1353                 if (!cfg_build(jd))
1354                         return NULL;
1355
1356 #ifdef ENABLE_VERIFIER
1357                 if (JITDATA_HAS_FLAG_VERIFY(jd)) {
1358                         DEBUG_JIT_COMPILEVERBOSE("Typechecking: ");
1359
1360                         /* call typecheck pass */
1361                         if (!typecheck(jd)) {
1362                                 DEBUG_JIT_COMPILEVERBOSE("Exception while typechecking: ");
1363
1364                                 return NULL;
1365                         }
1366
1367                         DEBUG_JIT_COMPILEVERBOSE("Typechecking done: ");
1368                 }
1369 #endif
1370                 RT_TIMING_GET_TIME(time_typecheck);
1371
1372 #if defined(ENABLE_LOOP)
1373                 if (opt_loops) {
1374                         depthFirst(jd);
1375                         analyseGraph(jd);
1376                         optimize_loops(jd);
1377                         jit_renumber_basicblocks(jd);
1378                 }
1379 #endif
1380                 RT_TIMING_GET_TIME(time_loop);
1381
1382 #if defined(ENABLE_IFCONV)
1383                 if (JITDATA_HAS_FLAG_IFCONV(jd)) {
1384                         if (!ifconv_static(jd))
1385                                 return NULL;
1386                         jit_renumber_basicblocks(jd);
1387                 }
1388 #endif
1389                 RT_TIMING_GET_TIME(time_ifconv);
1390
1391                 /* Basic block reordering.  I think this should be done after
1392                    if-conversion, as we could lose the ability to do the
1393                    if-conversion. */
1394
1395                 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1396                         if (!reorder(jd))
1397                                 return NULL;
1398                         jit_renumber_basicblocks(jd);
1399                 }
1400
1401                 DEBUG_JIT_COMPILEVERBOSE("Allocating registers: ");
1402
1403 #if defined(ENABLE_LSRA) && !defined(ENABLE_SSA)
1404                 /* allocate registers */
1405                 if (opt_lsra) {
1406                         if (!lsra(jd))
1407                                 return NULL;
1408
1409                         STATISTICS(count_methods_allocated_by_lsra++);
1410
1411                 } else
1412 # endif /* defined(ENABLE_LSRA) && !defined(ENABLE_SSA) */
1413 #if defined(ENABLE_SSA)
1414                 /* allocate registers */
1415                 if ((opt_lsra) && (jd->exceptiontablelength == 0)) {
1416                         jd->ls = DNEW(lsradata);
1417                         lsra(jd);
1418
1419                         STATISTICS(count_methods_allocated_by_lsra++);
1420
1421                 } else
1422 # endif /* defined(ENABLE_SSA) */
1423                 {
1424                         STATISTICS(count_locals_conflicts += (jd->maxlocals - 1) * (jd->maxlocals));
1425
1426                         regalloc(jd);
1427                 }
1428
1429                 STATISTICS(simplereg_make_statistics(jd));
1430
1431                 DEBUG_JIT_COMPILEVERBOSE("Allocating registers done: ");
1432 # if defined(ENABLE_INTRP)
1433         }
1434 # endif
1435 #endif /* defined(ENABLE_JIT) */
1436         RT_TIMING_GET_TIME(time_alloc);
1437
1438         /* Allocate memory for basic block profiling information. This
1439            _must_ be done after loop optimization and register allocation,
1440            since they can change the basic block count. */
1441
1442         if (JITDATA_HAS_FLAG_INSTRUMENT(jd))
1443                 code->bbfrequency = MNEW(u4, jd->basicblockcount);
1444
1445         DEBUG_JIT_COMPILEVERBOSE("Generating code: ");
1446
1447         /* create the replacement points */
1448 #if 0
1449         if (!replace_create_replacement_points(jd))
1450                 return NULL;
1451         RT_TIMING_GET_TIME(time_rplpoints);
1452 #endif
1453
1454         /* now generate the machine code */
1455
1456 #if defined(ENABLE_JIT)
1457 # if defined(ENABLE_INTRP)
1458         if (opt_intrp) {
1459 #if defined(ENABLE_VERIFIER)
1460                 if (opt_verify) {
1461                         DEBUG_JIT_COMPILEVERBOSE("Typechecking (stackbased): ");
1462
1463                         if (!typecheck_stackbased(jd)) {
1464                                 DEBUG_JIT_COMPILEVERBOSE("Exception while typechecking (stackbased): ");
1465                                 return NULL;
1466                         }
1467                 }
1468 #endif
1469                 if (!intrp_codegen(jd)) {
1470                         DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1471
1472                         return NULL;
1473                 }
1474         } else
1475 # endif
1476                 {
1477                         if (!codegen(jd)) {
1478                                 DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1479
1480                                 return NULL;
1481                         }
1482                 }
1483 #else
1484         if (!intrp_codegen(jd)) {
1485                 DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1486
1487                 return NULL;
1488         }
1489 #endif
1490         RT_TIMING_GET_TIME(time_codegen);
1491
1492         DEBUG_JIT_COMPILEVERBOSE("Generating code done: ");
1493
1494 #if !defined(NDEBUG)
1495         /* intermediate and assembly code listings */
1496                 
1497         if (JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd)) {
1498                 show_method(jd, SHOW_CODE);
1499         }
1500         else if (JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
1501 # if defined(ENABLE_DISASSEMBLER)
1502                 DISASSEMBLE(code->entrypoint,
1503                                         code->entrypoint + (code->mcodelength - cd->dseglen));
1504 # endif
1505         }
1506
1507         if (opt_showddatasegment)
1508                 dseg_display(jd);
1509 #endif
1510
1511         DEBUG_JIT_COMPILEVERBOSE("Compiling done: ");
1512
1513         /* switch to the newly generated code */
1514
1515         assert(code);
1516         assert(code->entrypoint);
1517
1518         /* add the current compile version to the methodinfo */
1519
1520         code->prev = m->code;
1521         m->code = code;
1522
1523         RT_TIMING_TIME_DIFF(time_start,time_checks,RT_TIMING_JIT_CHECKS);
1524         RT_TIMING_TIME_DIFF(time_checks,time_parse,RT_TIMING_JIT_PARSE);
1525         RT_TIMING_TIME_DIFF(time_parse,time_stack,RT_TIMING_JIT_STACK);
1526         RT_TIMING_TIME_DIFF(time_stack,time_typecheck,RT_TIMING_JIT_TYPECHECK);
1527         RT_TIMING_TIME_DIFF(time_typecheck,time_loop,RT_TIMING_JIT_LOOP);
1528         RT_TIMING_TIME_DIFF(time_loop,time_alloc,RT_TIMING_JIT_ALLOC);
1529         RT_TIMING_TIME_DIFF(time_alloc,time_rplpoints,RT_TIMING_JIT_RPLPOINTS);
1530         RT_TIMING_TIME_DIFF(time_rplpoints,time_codegen,RT_TIMING_JIT_CODEGEN);
1531         RT_TIMING_TIME_DIFF(time_start,time_codegen,RT_TIMING_JIT_TOTAL);
1532
1533         /* return pointer to the methods entry point */
1534
1535         return code->entrypoint;
1536
1537
1538
1539 /* jit_invalidate_code *********************************************************
1540
1541    Mark the compiled code of the given method as invalid and take care that
1542    it is replaced if necessary.
1543
1544    XXX Not fully implemented, yet.
1545
1546 *******************************************************************************/
1547
1548 void jit_invalidate_code(methodinfo *m)
1549 {
1550         codeinfo *code;
1551         rplpoint *rp;
1552         s4        i;
1553
1554         code = m->code;
1555         if (code == NULL || code->invalid)
1556                 return;
1557
1558         code->invalid = true;
1559
1560         rp = code->rplpoints;
1561         i = code->rplpointcount;
1562         rp += i;
1563
1564         while (i--) {
1565                 rp--;
1566                 if ((rp->type != RPLPOINT_TYPE_RETURN)
1567                         && !(rp->flags & RPLPOINT_FLAG_NOTRAP))
1568                 {
1569                         replace_activate_replacement_point(rp, rp);
1570                 }
1571         }
1572 }
1573
1574
1575 /* jit_request_optimization ****************************************************
1576
1577    Request optimization of the given method. If the code of the method is
1578    unoptimized, it will be invalidated, so the next jit_get_current_code(m)
1579    triggers an optimized recompilation.
1580    If the method is already optimized, this function does nothing.
1581
1582    IN:
1583        m................the method
1584
1585 *******************************************************************************/
1586
1587 void jit_request_optimization(methodinfo *m)
1588 {
1589         codeinfo *code;
1590
1591         code = m->code;
1592
1593         if (code && code->optlevel == 0)
1594                 jit_invalidate_code(m);
1595 }
1596
1597
1598 /* jit_get_current_code ********************************************************
1599
1600    Get the currently valid code for the given method. If there is no valid
1601    code, (re)compile the method.
1602
1603    IN:
1604        m................the method
1605
1606    RETURN VALUE:
1607        the codeinfo* for the current code, or
1608            NULL if an exception has been thrown during recompilation.
1609
1610 *******************************************************************************/
1611
1612 codeinfo *jit_get_current_code(methodinfo *m)
1613 {
1614         assert(m);
1615
1616         /* if we have valid code, return it */
1617
1618         if (m->code && !m->code->invalid)
1619                 return m->code;
1620
1621         /* otherwise: recompile */
1622
1623         if (!jit_recompile(m))
1624                 return NULL;
1625
1626         assert(m->code);
1627
1628         return m->code;
1629 }
1630
1631
1632 /* jit_asm_compile *************************************************************
1633
1634    This method is called from asm_vm_call_method and does:
1635
1636      - create stackframe info for exceptions
1637      - compile the method
1638      - patch the entrypoint of the method into the calculated address in
1639        the JIT code
1640      - flushes the instruction cache.
1641
1642 *******************************************************************************/
1643
1644 #if defined(ENABLE_JIT)
1645 u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra)
1646 {
1647         stackframeinfo  sfi;
1648         u1             *entrypoint;
1649         u1             *pa;
1650         ptrint         *p;
1651
1652         /* create the stackframeinfo (subtract 1 from RA as it points to the */
1653         /* instruction after the call)                                       */
1654
1655         stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, ra-1);
1656
1657         /* actually compile the method */
1658
1659         entrypoint = jit_compile(m);
1660
1661         /* remove the stackframeinfo */
1662
1663         stacktrace_remove_stackframeinfo(&sfi);
1664
1665         /* there was a problem during compilation */
1666
1667         if (entrypoint == NULL)
1668                 return NULL;
1669
1670         /* get the method patch address */
1671
1672         pa = md_get_method_patch_address(ra, &sfi, mptr);
1673
1674         /* patch the method entry point */
1675
1676         p = (ptrint *) pa;
1677
1678         *p = (ptrint) entrypoint;
1679
1680         /* flush the instruction cache */
1681
1682         md_icacheflush(pa, SIZEOF_VOID_P);
1683
1684         return entrypoint;
1685 }
1686 #endif /* defined(ENABLE_JIT) */
1687
1688
1689 /* jit_complement_condition ****************************************************
1690
1691    Returns the complement of the passed conditional instruction.
1692
1693    We use the order of the different conditions, e.g.:
1694
1695    ICMD_IFEQ         153
1696    ICMD_IFNE         154
1697
1698    If the passed opcode is odd, we simply add 1 to get the complement.
1699    If the opcode is even, we subtract 1.
1700
1701    Exception:
1702
1703    ICMD_IFNULL       198
1704    ICMD_IFNONNULL    199
1705
1706 *******************************************************************************/
1707
1708 s4 jit_complement_condition(s4 opcode)
1709 {
1710         switch (opcode) {
1711         case ICMD_IFNULL:
1712                 return ICMD_IFNONNULL;
1713
1714         case ICMD_IFNONNULL:
1715                 return ICMD_IFNULL;
1716
1717         default:
1718                 /* check if opcode is odd */
1719
1720                 if (opcode & 0x1)
1721                         return opcode + 1;
1722                 else
1723                         return opcode - 1;
1724         }
1725 }
1726
1727
1728 /* jit_renumber_basicblocks ****************************************************
1729
1730    Set the ->nr of all blocks so it increases when traversing ->next.
1731
1732    IN:
1733        jitdata..........the current jitdata
1734
1735 *******************************************************************************/
1736
1737 void jit_renumber_basicblocks(jitdata *jd)
1738 {
1739         s4          nr;
1740         basicblock *bptr;
1741
1742         nr = 0;
1743         for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
1744                 bptr->nr = nr++;
1745         }
1746
1747         /* we have one block more than jd->basicblockcount (the end marker) */
1748
1749         assert(nr == jd->basicblockcount + 1);
1750 }
1751
1752
1753 /* jit_check_basicblock_numbers ************************************************
1754
1755    Assert that the ->nr of the first block is zero and increases by 1 each
1756    time ->next is traversed.
1757    This function should be called before any analysis that relies on
1758    the basicblock numbers.
1759
1760    IN:
1761        jitdata..........the current jitdata
1762
1763    NOTE: Aborts with an assertion if the condition is not met!
1764
1765 *******************************************************************************/
1766
1767 #if !defined(NDEBUG)
1768 void jit_check_basicblock_numbers(jitdata *jd)
1769 {
1770         s4          nr;
1771         basicblock *bptr;
1772
1773         nr = 0;
1774         for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
1775                 assert(bptr->nr == nr);
1776                 nr++;
1777         }
1778
1779         /* we have one block more than jd->basicblockcount (the end marker) */
1780
1781         assert(nr == jd->basicblockcount + 1);
1782 }
1783 #endif /* !defined(NDEBUG) */
1784
1785
1786 /*
1787  * These are local overrides for various environment variables in Emacs.
1788  * Please do not remove this and leave it at the end of the file, where
1789  * Emacs will automagically detect them.
1790  * ---------------------------------------------------------------------
1791  * Local variables:
1792  * mode: c
1793  * indent-tabs-mode: t
1794  * c-basic-offset: 4
1795  * tab-width: 4
1796  * End:
1797  * vim:noexpandtab:sw=4:ts=4:
1798  */