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