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