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