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