* src/vm/string.c (javastring_toutf): Check for NULL and return
[cacao.git] / src / vm / jit / jit.c
1 /* src/vm/jit/jit.c - calls the code generation functions
2
3    Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25    Contact: cacao@cacaojvm.org
26
27    Authors: Andreas Krall
28             Reinhard Grafl
29
30    Changes: Edwin Steiner
31             Christian Thalinger
32             Christian Ullrich
33
34    $Id: jit.c 5049 2006-06-23 12:07:26Z twisti $
35
36 */
37
38
39 #include "config.h"
40 #include "vm/types.h"
41
42 #include <assert.h>
43
44 #include "mm/memory.h"
45 #include "native/native.h"
46 #include "toolbox/logging.h"
47 #include "vm/builtin.h"
48 #include "vm/class.h"
49 #include "vm/global.h"
50 #include "vm/initialize.h"
51 #include "vm/loader.h"
52 #include "vm/method.h"
53 #include "vm/options.h"
54 #include "vm/statistics.h"
55 #include "vm/jit/asmpart.h"
56 #include "vm/jit/codegen-common.h"
57 #include "vm/jit/disass.h"
58 #include "vm/jit/dseg.h"
59 #include "vm/jit/jit.h"
60 #include "vm/jit/show.h"
61
62
63 #include "vm/jit/parse.h"
64 #include "vm/jit/reg.h"
65 #include "vm/jit/stack.h"
66
67 #include "vm/jit/allocator/simplereg.h"
68 #if defined(ENABLE_LSRA)
69 # include "vm/jit/allocator/lsra.h"
70 #endif
71
72 #if defined(ENABLE_IFCONV)
73 # include "vm/jit/ifconv/ifconv.h"
74 #endif
75
76 #include "vm/jit/loop/analyze.h"
77 #include "vm/jit/loop/graph.h"
78 #include "vm/jit/loop/loop.h"
79 #include "vm/jit/verify/typecheck.h"
80 #include "vm/rt-timing.h"
81
82 #if defined(ENABLE_THREADS)
83 # include "threads/native/threads.h"
84 #endif
85
86
87 /* debug macros ***************************************************************/
88
89 #if !defined(NDEBUG)
90 #define DEBUG_JIT_COMPILEVERBOSE(x) \
91     do { \
92         if (compileverbose) { \
93             log_message_method(x, m); \
94         } \
95     } while (0)
96 #else
97 #define DEBUG_JIT_COMPILEVERBOSE(x)    /* nothing */
98 #endif
99
100  
101 /* global switches ************************************************************/
102
103 int stackreq[256];
104
105                                 
106 int jcommandsize[256] = {
107
108         1,    /* JAVA_NOP                         0 */
109         1,    /* JAVA_ACONST_NULL                 1 */
110         1,    /* JAVA_ICONST_M1                   2 */
111         1,    /* JAVA_ICONST_0                    3 */
112         1,    /* JAVA_ICONST_1                    4 */
113         1,    /* JAVA_ICONST_2                    5 */
114         1,    /* JAVA_ICONST_3                    6 */
115         1,    /* JAVA_ICONST_4                    7 */
116         1,    /* JAVA_ICONST_5                    8 */
117         1,    /* JAVA_LCONST_0                    9 */
118         1,    /* JAVA_LCONST_1                   10 */
119         1,    /* JAVA_FCONST_0                   11 */
120         1,    /* JAVA_FCONST_1                   12 */
121         1,    /* JAVA_FCONST_2                   13 */
122         1,    /* JAVA_DCONST_0                   14 */
123         1,    /* JAVA_DCONST_1                   15 */
124         2,    /* JAVA_BIPUSH                     16 */
125         3,    /* JAVA_SIPUSH                     17 */
126         2,    /* JAVA_LDC1                       18 */
127         3,    /* JAVA_LDC2                       19 */
128         3,    /* JAVA_LDC2W                      20 */
129         2,    /* JAVA_ILOAD                      21 */
130         2,    /* JAVA_LLOAD                      22 */
131         2,    /* JAVA_FLOAD                      23 */
132         2,    /* JAVA_DLOAD                      24 */
133         2,    /* JAVA_ALOAD                      25 */
134         1,    /* JAVA_ILOAD_0                    26 */
135         1,    /* JAVA_ILOAD_1                    27 */
136         1,    /* JAVA_ILOAD_2                    28 */
137         1,    /* JAVA_ILOAD_3                    29 */
138         1,    /* JAVA_LLOAD_0                    30 */
139         1,    /* JAVA_LLOAD_1                    31 */
140         1,    /* JAVA_LLOAD_2                    32 */
141         1,    /* JAVA_LLOAD_3                    33 */
142         1,    /* JAVA_FLOAD_0                    34 */
143         1,    /* JAVA_FLOAD_1                    35 */
144         1,    /* JAVA_FLOAD_2                    36 */
145         1,    /* JAVA_FLOAD_3                    37 */
146         1,    /* JAVA_DLOAD_0                    38 */
147         1,    /* JAVA_DLOAD_1                    39 */
148         1,    /* JAVA_DLOAD_2                    40 */
149         1,    /* JAVA_DLOAD_3                    41 */
150         1,    /* JAVA_ALOAD_0                    42 */
151         1,    /* JAVA_ALOAD_1                    43 */
152         1,    /* JAVA_ALOAD_2                    44 */
153         1,    /* JAVA_ALOAD_3                    45 */
154         1,    /* JAVA_IALOAD                     46 */
155         1,    /* JAVA_LALOAD                     47 */
156         1,    /* JAVA_FALOAD                     48 */
157         1,    /* JAVA_DALOAD                     49 */
158         1,    /* JAVA_AALOAD                     50 */
159         1,    /* JAVA_BALOAD                     51 */
160         1,    /* JAVA_CALOAD                     52 */
161         1,    /* JAVA_SALOAD                     53 */
162         2,    /* JAVA_ISTORE                     54 */
163         2,    /* JAVA_LSTORE                     55 */
164         2,    /* JAVA_FSTORE                     56 */
165         2,    /* JAVA_DSTORE                     57 */
166         2,    /* JAVA_ASTORE                     58 */
167         1,    /* JAVA_ISTORE_0                   59 */
168         1,    /* JAVA_ISTORE_1                   60 */
169         1,    /* JAVA_ISTORE_2                   61 */
170         1,    /* JAVA_ISTORE_3                   62 */
171         1,    /* JAVA_LSTORE_0                   63 */
172         1,    /* JAVA_LSTORE_1                   64 */
173         1,    /* JAVA_LSTORE_2                   65 */
174         1,    /* JAVA_LSTORE_3                   66 */
175         1,    /* JAVA_FSTORE_0                   67 */
176         1,    /* JAVA_FSTORE_1                   68 */
177         1,    /* JAVA_FSTORE_2                   69 */
178         1,    /* JAVA_FSTORE_3                   70 */
179         1,    /* JAVA_DSTORE_0                   71 */
180         1,    /* JAVA_DSTORE_1                   72 */
181         1,    /* JAVA_DSTORE_2                   73 */
182         1,    /* JAVA_DSTORE_3                   74 */
183         1,    /* JAVA_ASTORE_0                   75 */
184         1,    /* JAVA_ASTORE_1                   76 */
185         1,    /* JAVA_ASTORE_2                   77 */
186         1,    /* JAVA_ASTORE_3                   78 */
187         1,    /* JAVA_IASTORE                    79 */
188         1,    /* JAVA_LASTORE                    80 */
189         1,    /* JAVA_FASTORE                    81 */
190         1,    /* JAVA_DASTORE                    82 */
191         1,    /* JAVA_AASTORE                    83 */
192         1,    /* JAVA_BASTORE                    84 */
193         1,    /* JAVA_CASTORE                    85 */
194         1,    /* JAVA_SASTORE                    86 */
195         1,    /* JAVA_POP                        87 */
196         1,    /* JAVA_POP2                       88 */
197         1,    /* JAVA_DUP                        89 */
198         1,    /* JAVA_DUP_X1                     90 */
199         1,    /* JAVA_DUP_X2                     91 */
200         1,    /* JAVA_DUP2                       92 */
201         1,    /* JAVA_DUP2_X1                    93 */
202         1,    /* JAVA_DUP2_X2                    94 */
203         1,    /* JAVA_SWAP                       95 */
204         1,    /* JAVA_IADD                       96 */
205         1,    /* JAVA_LADD                       97 */
206         1,    /* JAVA_FADD                       98 */
207         1,    /* JAVA_DADD                       99 */
208         1,    /* JAVA_ISUB                      100 */
209         1,    /* JAVA_LSUB                      101 */
210         1,    /* JAVA_FSUB                      102 */
211         1,    /* JAVA_DSUB                      103 */
212         1,    /* JAVA_IMUL                      104 */
213         1,    /* JAVA_LMUL                      105 */
214         1,    /* JAVA_FMUL                      106 */
215         1,    /* JAVA_DMUL                      107 */
216         1,    /* JAVA_IDIV                      108 */
217         1,    /* JAVA_LDIV                      109 */
218         1,    /* JAVA_FDIV                      110 */
219         1,    /* JAVA_DDIV                      111 */
220         1,    /* JAVA_IREM                      112 */
221         1,    /* JAVA_LREM                      113 */
222         1,    /* JAVA_FREM                      114 */
223         1,    /* JAVA_DREM                      115 */
224         1,    /* JAVA_INEG                      116 */
225         1,    /* JAVA_LNEG                      117 */
226         1,    /* JAVA_FNEG                      118 */
227         1,    /* JAVA_DNEG                      119 */
228         1,    /* JAVA_ISHL                      120 */
229         1,    /* JAVA_LSHL                      121 */
230         1,    /* JAVA_ISHR                      122 */
231         1,    /* JAVA_LSHR                      123 */
232         1,    /* JAVA_IUSHR                     124 */
233         1,    /* JAVA_LUSHR                     125 */
234         1,    /* JAVA_IAND                      126 */
235         1,    /* JAVA_LAND                      127 */
236         1,    /* JAVA_IOR                       128 */
237         1,    /* JAVA_LOR                       129 */
238         1,    /* JAVA_IXOR                      130 */
239         1,    /* JAVA_LXOR                      131 */
240         3,    /* JAVA_IINC                      132 */
241         1,    /* JAVA_I2L                       133 */
242         1,    /* JAVA_I2F                       134 */
243         1,    /* JAVA_I2D                       135 */
244         1,    /* JAVA_L2I                       136 */
245         1,    /* JAVA_L2F                       137 */
246         1,    /* JAVA_L2D                       138 */
247         1,    /* JAVA_F2I                       139 */
248         1,    /* JAVA_F2L                       140 */
249         1,    /* JAVA_F2D                       141 */
250         1,    /* JAVA_D2I                       142 */
251         1,    /* JAVA_D2L                       143 */
252         1,    /* JAVA_D2F                       144 */
253         1,    /* JAVA_INT2BYTE                  145 */
254         1,    /* JAVA_INT2CHAR                  146 */
255         1,    /* JAVA_INT2SHORT                 147 */
256         1,    /* JAVA_LCMP                      148 */
257         1,    /* JAVA_FCMPL                     149 */
258         1,    /* JAVA_FCMPG                     150 */
259         1,    /* JAVA_DCMPL                     151 */
260         1,    /* JAVA_DCMPG                     152 */
261         3,    /* JAVA_IFEQ                      153 */
262         3,    /* JAVA_IFNE                      154 */
263         3,    /* JAVA_IFLT                      155 */
264         3,    /* JAVA_IFGE                      156 */
265         3,    /* JAVA_IFGT                      157 */
266         3,    /* JAVA_IFLE                      158 */
267         3,    /* JAVA_IF_ICMPEQ                 159 */
268         3,    /* JAVA_IF_ICMPNE                 160 */
269         3,    /* JAVA_IF_ICMPLT                 161 */
270         3,    /* JAVA_IF_ICMPGE                 162 */
271         3,    /* JAVA_IF_ICMPGT                 163 */
272         3,    /* JAVA_IF_ICMPLE                 164 */
273         3,    /* JAVA_IF_ACMPEQ                 165 */
274         3,    /* JAVA_IF_ACMPNE                 166 */
275         3,    /* JAVA_GOTO                      167 */
276         3,    /* JAVA_JSR                       168 */
277         2,    /* JAVA_RET                       169 */
278         0,    /* JAVA_TABLESWITCH               170 */ /* variable length */
279         0,    /* JAVA_LOOKUPSWITCH              171 */ /* variable length */
280         1,    /* JAVA_IRETURN                   172 */
281         1,    /* JAVA_LRETURN                   173 */
282         1,    /* JAVA_FRETURN                   174 */
283         1,    /* JAVA_DRETURN                   175 */
284         1,    /* JAVA_ARETURN                   176 */
285         1,    /* JAVA_RETURN                    177 */
286         3,    /* JAVA_GETSTATIC                 178 */
287         3,    /* JAVA_PUTSTATIC                 179 */
288         3,    /* JAVA_GETFIELD                  180 */
289         3,    /* JAVA_PUTFIELD                  181 */
290         3,    /* JAVA_INVOKEVIRTUAL             182 */
291         3,    /* JAVA_INVOKESPECIAL             183 */
292         3,    /* JAVA_INVOKESTATIC              184 */
293         5,    /* JAVA_INVOKEINTERFACE           185 */
294         1,    /* UNDEF186 */
295         3,    /* JAVA_NEW                       187 */
296         2,    /* JAVA_NEWARRAY                  188 */
297         3,    /* JAVA_ANEWARRAY                 189 */
298         1,    /* JAVA_ARRAYLENGTH               190 */
299         1,    /* JAVA_ATHROW                    191 */
300         3,    /* JAVA_CHECKCAST                 192 */
301         3,    /* JAVA_INSTANCEOF                193 */
302         1,    /* JAVA_MONITORENTER              194 */
303         1,    /* JAVA_MONITOREXIT               195 */
304         0,    /* JAVA_WIDE                      196 */ /* variable length */
305         4,    /* JAVA_MULTIANEWARRAY            197 */
306         3,    /* JAVA_IFNULL                    198 */
307         3,    /* JAVA_IFNONNULL                 199 */
308         5,    /* JAVA_GOTO_W                    200 */
309         5,    /* JAVA_JSR_W                     201 */
310         1,    /* JAVA_BREAKPOINT                202 */
311
312         1,    /* UNDEF203 */
313         1,
314         1,
315         1,
316         1,
317         1,
318         1,
319         1,    /* UNDEF210 */
320         1,
321         1,
322         1,
323         1,
324         1,
325         1,
326         1,
327         1,
328         1,
329         1,    /* UNDEF220 */
330         1,
331         1,
332         1,
333         1,
334         1,
335         1,
336         1,
337         1,
338         1,
339         1,    /* UNDEF230 */
340         1,
341         1,
342         1,
343         1,
344
345         /* unused */
346                 1,1,1,1,1,1,
347         1,1,1,1,1,1,1,1,1,1,
348         1,1,1,1,1
349 };
350
351
352 char *icmd_names[256] = {
353         "NOP            ", /*               0 */
354         "ACONST         ", /*               1 */
355         "CHECKNULL      ", /* ICONST_M1     2 */
356         "ICONST         ", /*               3 */
357         "CHECKNULL_POP  ", /* ICONST_1      4 */
358         "IDIVPOW2       ", /* ICONST_2      5 */
359         "LDIVPOW2       ", /* ICONST_3      6 */
360         "UNDEF7         ", /* ICONST_4      7 */
361         "UNDEF8         ", /* ICONST_5      8 */
362         "LCONST         ", /*               9 */
363         "LCMPCONST      ", /* LCONST_1     10 */
364         "FCONST         ", /*              11 */
365         "UNDEF12        ", /* FCONST_1     12 */
366         "ELSE_ICONST    ", /* FCONST_2     13 */
367         "DCONST         ", /*              14 */
368         "IFEQ_ICONST    ", /* DCONST_1     15 */
369         "IFNE_ICONST    ", /* BIPUSH       16 */
370         "IFLT_ICONST    ", /* SIPUSH       17 */
371         "IFGE_ICONST    ", /* LDC1         18 */
372         "IFGT_ICONST    ", /* LDC2         19 */
373         "IFLE_ICONST    ", /* LDC2W        20 */
374         "ILOAD          ", /*              21 */
375         "LLOAD          ", /*              22 */
376         "FLOAD          ", /*              23 */
377         "DLOAD          ", /*              24 */
378         "ALOAD          ", /*              25 */
379         "IADDCONST      ", /* ILOAD_0      26 */
380         "ISUBCONST      ", /* ILOAD_1      27 */
381         "IMULCONST      ", /* ILOAD_2      28 */
382         "IANDCONST      ", /* ILOAD_3      29 */
383         "IORCONST       ", /* LLOAD_0      30 */
384         "IXORCONST      ", /* LLOAD_1      31 */
385         "ISHLCONST      ", /* LLOAD_2      32 */
386         "ISHRCONST      ", /* LLOAD_3      33 */
387         "IUSHRCONST     ", /* FLOAD_0      34 */
388         "IREMPOW2       ", /* FLOAD_1      35 */
389         "LADDCONST      ", /* FLOAD_2      36 */
390         "LSUBCONST      ", /* FLOAD_3      37 */
391         "LMULCONST      ", /* DLOAD_0      38 */
392         "LANDCONST      ", /* DLOAD_1      39 */
393         "LORCONST       ", /* DLOAD_2      40 */
394         "LXORCONST      ", /* DLOAD_3      41 */
395         "LSHLCONST      ", /* ALOAD_0      42 */
396         "LSHRCONST      ", /* ALOAD_1      43 */
397         "LUSHRCONST     ", /* ALOAD_2      44 */
398         "LREMPOW2       ", /* ALOAD_3      45 */
399         "IALOAD         ", /*              46 */
400         "LALOAD         ", /*              47 */
401         "FALOAD         ", /*              48 */
402         "DALOAD         ", /*              49 */
403         "AALOAD         ", /*              50 */
404         "BALOAD         ", /*              51 */
405         "CALOAD         ", /*              52 */
406         "SALOAD         ", /*              53 */
407         "ISTORE         ", /*              54 */
408         "LSTORE         ", /*              55 */
409         "FSTORE         ", /*              56 */
410         "DSTORE         ", /*              57 */
411         "ASTORE         ", /*              58 */
412         "IF_LEQ         ", /* ISTORE_0     59 */
413         "IF_LNE         ", /* ISTORE_1     60 */
414         "IF_LLT         ", /* ISTORE_2     61 */
415         "IF_LGE         ", /* ISTORE_3     62 */
416         "IF_LGT         ", /* LSTORE_0     63 */
417         "IF_LLE         ", /* LSTORE_1     64 */
418         "IF_LCMPEQ      ", /* LSTORE_2     65 */
419         "IF_LCMPNE      ", /* LSTORE_3     66 */
420         "IF_LCMPLT      ", /* FSTORE_0     67 */
421         "IF_LCMPGE      ", /* FSTORE_1     68 */
422         "IF_LCMPGT      ", /* FSTORE_2     69 */
423         "IF_LCMPLE      ", /* FSTORE_3     70 */
424         "UNDEF71        ", /* DSTORE_0     71 */
425         "UNDEF72        ", /* DSTORE_1     72 */
426         "UNDEF73        ", /* DSTORE_2     73 */
427         "UNDEF74        ", /* DSTORE_3     74 */
428         "UNDEF75        ", /* ASTORE_0     75 */
429         "UNDEF76        ", /* ASTORE_1     76 */
430         "UNDEF77        ", /* ASTORE_2     77 */
431         "UNDEF78        ", /* ASTORE_3     78 */
432         "IASTORE        ", /*              79 */
433         "LASTORE        ", /*              80 */
434         "FASTORE        ", /*              81 */
435         "DASTORE        ", /*              82 */
436         "AASTORE        ", /*              83 */
437         "BASTORE        ", /*              84 */
438         "CASTORE        ", /*              85 */
439         "SASTORE        ", /*              86 */
440         "POP            ", /*              87 */
441         "POP2           ", /*              88 */
442         "DUP            ", /*              89 */
443         "DUP_X1         ", /*              90 */
444         "DUP_X2         ", /*              91 */
445         "DUP2           ", /*              92 */
446         "DUP2_X1        ", /*              93 */
447         "DUP2_X2        ", /*              94 */
448         "SWAP           ", /*              95 */
449         "IADD           ", /*              96 */
450         "LADD           ", /*              97 */
451         "FADD           ", /*              98 */
452         "DADD           ", /*              99 */
453         "ISUB           ", /*             100 */
454         "LSUB           ", /*             101 */
455         "FSUB           ", /*             102 */
456         "DSUB           ", /*             103 */
457         "IMUL           ", /*             104 */
458         "LMUL           ", /*             105 */
459         "FMUL           ", /*             106 */
460         "DMUL           ", /*             107 */
461         "IDIV           ", /*             108 */
462         "LDIV           ", /*             109 */
463         "FDIV           ", /*             110 */
464         "DDIV           ", /*             111 */
465         "IREM           ", /*             112 */
466         "LREM           ", /*             113 */
467         "FREM           ", /*             114 */
468         "DREM           ", /*             115 */
469         "INEG           ", /*             116 */
470         "LNEG           ", /*             117 */
471         "FNEG           ", /*             118 */
472         "DNEG           ", /*             119 */
473         "ISHL           ", /*             120 */
474         "LSHL           ", /*             121 */
475         "ISHR           ", /*             122 */
476         "LSHR           ", /*             123 */
477         "IUSHR          ", /*             124 */
478         "LUSHR          ", /*             125 */
479         "IAND           ", /*             126 */
480         "LAND           ", /*             127 */
481         "IOR            ", /*             128 */
482         "LOR            ", /*             129 */
483         "IXOR           ", /*             130 */
484         "LXOR           ", /*             131 */
485         "IINC           ", /*             132 */
486         "I2L            ", /*             133 */
487         "I2F            ", /*             134 */
488         "I2D            ", /*             135 */
489         "L2I            ", /*             136 */
490         "L2F            ", /*             137 */
491         "L2D            ", /*             138 */
492         "F2I            ", /*             139 */
493         "F2L            ", /*             140 */
494         "F2D            ", /*             141 */
495         "D2I            ", /*             142 */
496         "D2L            ", /*             143 */
497         "D2F            ", /*             144 */
498         "INT2BYTE       ", /*             145 */
499         "INT2CHAR       ", /*             146 */
500         "INT2SHORT      ", /*             147 */
501         "LCMP           ", /*             148 */
502         "FCMPL          ", /*             149 */
503         "FCMPG          ", /*             150 */
504         "DCMPL          ", /*             151 */
505         "DCMPG          ", /*             152 */
506         "IFEQ           ", /*             153 */
507         "IFNE           ", /*             154 */
508         "IFLT           ", /*             155 */
509         "IFGE           ", /*             156 */
510         "IFGT           ", /*             157 */
511         "IFLE           ", /*             158 */
512         "IF_ICMPEQ      ", /*             159 */
513         "IF_ICMPNE      ", /*             160 */
514         "IF_ICMPLT      ", /*             161 */
515         "IF_ICMPGE      ", /*             162 */
516         "IF_ICMPGT      ", /*             163 */
517         "IF_ICMPLE      ", /*             164 */
518         "IF_ACMPEQ      ", /*             165 */
519         "IF_ACMPNE      ", /*             166 */
520         "GOTO           ", /*             167 */
521         "JSR            ", /*             168 */
522         "RET            ", /*             169 */
523         "TABLESWITCH    ", /*             170 */
524         "LOOKUPSWITCH   ", /*             171 */
525         "IRETURN        ", /*             172 */
526         "LRETURN        ", /*             173 */
527         "FRETURN        ", /*             174 */
528         "DRETURN        ", /*             175 */
529         "ARETURN        ", /*             176 */
530         "RETURN         ", /*             177 */
531         "GETSTATIC      ", /*             178 */
532         "PUTSTATIC      ", /*             179 */
533         "GETFIELD       ", /*             180 */
534         "PUTFIELD       ", /*             181 */
535         "INVOKEVIRTUAL  ", /*             182 */
536         "INVOKESPECIAL  ", /*             183 */
537         "INVOKESTATIC   ", /*             184 */
538         "INVOKEINTERFACE", /*             185 */
539         "UNDEF186       ", /* UNDEF186    186 */
540         "NEW            ", /*             187 */
541         "NEWARRAY       ", /*             188 */
542         "ANEWARRAY      ", /*             189 */
543         "ARRAYLENGTH    ", /*             190 */
544         "ATHROW         ", /*             191 */
545         "CHECKCAST      ", /*             192 */
546         "INSTANCEOF     ", /*             193 */
547         "MONITORENTER   ", /*             194 */
548         "MONITOREXIT    ", /*             195 */
549         "UNDEF196       ", /* WIDE        196 */
550         "MULTIANEWARRAY ", /*             197 */
551         "IFNULL         ", /*             198 */
552         "IFNONNULL      ", /*             199 */
553         "UNDEF200       ", /* GOTO_W      200 */
554         "UNDEF201       ", /* JSR_W       201 */
555         "UNDEF202       ", /* BREAKPOINT  202 */
556         "UNDEF203       ", /* UNDEF203    203 */
557         "IASTORECONST   ", /*             204 */
558         "LASTORECONST   ", /*             205 */
559         "FASTORECONST   ", /*             206 */
560         "DASTORECONST   ", /*             207 */
561         "AASTORECONST   ", /*             208 */
562         "BASTORECONST   ", /*             209 */
563         "CASTORECONST   ", /*             210 */
564         "SASTORECONST   ", /*             211 */
565         "PUTSTATICCONST ", /*             212 */
566         "PUTFIELDCONST  ", /*             213 */
567         "IMULPOW2       ", /*             214 */
568         "LMULPOW2       ", /*             215 */
569
570         "IF_FCMPEQ      ", /*             216 */
571         "IF_FCMPNE      ", /*             217 */
572
573         "IF_FCMPL_LT    ", /*             218 */
574         "IF_FCMPL_GE    ", /*             219 */
575         "IF_FCMPL_GT    ", /*             220 */
576         "IF_FCMPL_LE    ", /*             221 */
577
578         "IF_FCMPG_LT    ", /*             222 */
579         "IF_FCMPG_GE    ", /*             223 */
580         "IF_FCMPG_GT    ", /*             224 */
581         "IF_FCMPG_LE    ", /*             225 */
582
583         "IF_DCMPEQ      ", /*             226 */
584         "IF_DCMPNE      ", /*             227 */
585
586         "IF_DCMPL_LT    ", /*             228 */
587         "IF_DCMPL_GE    ", /*             229 */
588         "IF_DCMPL_GT    ", /*             230 */
589         "IF_DCMPL_LE    ", /*             231 */
590         
591         "IF_DCMPG_LT    ", /*             232 */
592         "IF_DCMPG_GE    ", /*             233 */
593         "IF_DCMPG_GT    ", /*             234 */
594         "IF_DCMPG_LE    ", /*             235 */
595         
596         "UNDEF236", "UNDEF237", "UNDEF238", "UNDEF239", "UNDEF240",
597         "UNDEF241", "UNDEF242", "UNDEF243", "UNDEF244", "UNDEF245",
598         "UNDEF246", "UNDEF247", "UNDEF248", "UNDEF249", "UNDEF250",
599
600         "INLINE_START   ", /*             251 */
601         "INLINE_END     ", /*             252 */
602         "INLINE_GOTO    ", /*             253 */
603
604         "UNDEF254",
605
606         "BUILTIN        "  /*             255 */
607 };
608
609
610 char *opcode_names[256] = {
611         "NOP            ", /*               0 */
612         "ACONST         ", /*               1 */
613         "ICONST_M1      ", /* ICONST_M1     2 */
614         "ICONST_0       ", /* ICONST_0      3 */
615         "ICONST_1       ", /* ICONST_1      4 */
616         "ICONST_2       ", /* ICONST_2      5 */
617         "ICONST_3       ", /* ICONST_3      6 */
618         "ICONST_4       ", /* ICONST_4      7 */
619         "ICONST_5       ", /* ICONST_5      8 */
620         "LCONST_0       ", /* LCONST_0      9 */
621         "LCONST_1       ", /* LCONST_1     10 */
622         "FCONST_0       ", /* FCONST_0     11 */
623         "FCONST_1       ", /* FCONST_1     12 */
624         "FCONST_2       ", /* FCONST_2     13 */
625         "DCONST_0       ", /* DCONST_0     14 */
626         "DCONST_1       ", /* DCONST_1     15 */
627         "BIPUSH         ", /* BIPUSH       16 */
628         "SIPUSH         ", /* SIPUSH       17 */
629         "LDC            ", /* LDC          18 */
630         "LDC_W          ", /* LDC_W        19 */
631         "LDC2_W         ", /* LDC2_W       20 */
632         "ILOAD          ", /*              21 */
633         "LLOAD          ", /*              22 */
634         "FLOAD          ", /*              23 */
635         "DLOAD          ", /*              24 */
636         "ALOAD          ", /*              25 */
637         "ILOAD_0        ", /* ILOAD_0      26 */
638         "ILOAD_1        ", /* ILOAD_1      27 */
639         "ILOAD_2        ", /* ILOAD_2      28 */
640         "ILOAD_3        ", /* ILOAD_3      29 */
641         "LLOAD_0        ", /* LLOAD_0      30 */
642         "LLOAD_1        ", /* LLOAD_1      31 */
643         "LLOAD_2        ", /* LLOAD_2      32 */
644         "LLOAD_3        ", /* LLOAD_3      33 */
645         "FLOAD_0        ", /* FLOAD_0      34 */
646         "FLOAD_1        ", /* FLOAD_1      35 */
647         "FLOAD_2        ", /* FLOAD_2      36 */
648         "FLOAD_3        ", /* FLOAD_3      37 */
649         "DLOAD_0        ", /* DLOAD_0      38 */
650         "DLOAD_1        ", /* DLOAD_1      39 */
651         "DLOAD_2        ", /* DLOAD_2      40 */ 
652         "DLOAD_3        ", /* DLOAD_3      41 */
653         "ALOAD_0        ", /* ALOAD_0      42 */
654         "ALOAD_1        ", /* ALOAD_1      43 */
655         "ALOAD_2        ", /* ALOAD_2      44 */
656         "ALOAD_3        ", /* ALOAD_3      45 */
657         "IALOAD         ", /*              46 */
658         "LALOAD         ", /*              47 */
659         "FALOAD         ", /*              48 */
660         "DALOAD         ", /*              49 */
661         "AALOAD         ", /*              50 */
662         "BALOAD         ", /*              51 */
663         "CALOAD         ", /*              52 */
664         "SALOAD         ", /*              53 */
665         "ISTORE         ", /*              54 */
666         "LSTORE         ", /*              55 */
667         "FSTORE         ", /*              56 */
668         "DSTORE         ", /*              57 */
669         "ASTORE         ", /*              58 */
670         "ISTORE_0       ", /* ISTORE_0     59 */
671         "ISTORE_1       ", /* ISTORE_1     60 */
672         "ISTORE_2       ", /* ISTORE_2     61 */
673         "ISTORE_3       ", /* ISTORE_3     62 */
674         "LSTORE_0       ", /* LSTORE_0     63 */
675         "LSTORE_1       ", /* LSTORE_1     64 */
676         "LSTORE_2       ", /* LSTORE_2     65 */
677         "LSTORE_3       ", /* LSTORE_3     66 */
678         "FSTORE_0       ", /* FSTORE_0     67 */
679         "FSTORE_1       ", /* FSTORE_1     68 */
680         "FSTORE_2       ", /* FSTORE_2     69 */
681         "FSTORE_3       ", /* FSTORE_3     70 */
682         "DSTORE_0       ", /* DSTORE_0     71 */
683         "DSTORE_1       ", /* DSTORE_1     72 */
684         "DSTORE_2       ", /* DSTORE_2     73 */
685         "DSTORE_3       ", /* DSTORE_3     74 */
686         "ASTORE_0       ", /* ASTORE_0     75 */
687         "ASTORE_1       ", /* ASTORE_1     76 */
688         "ASTORE_2       ", /* ASTORE_2     77 */
689         "ASTORE_3       ", /* ASTORE_3     78 */
690         "IASTORE        ", /*              79 */
691         "LASTORE        ", /*              80 */
692         "FASTORE        ", /*              81 */
693         "DASTORE        ", /*              82 */
694         "AASTORE        ", /*              83 */
695         "BASTORE        ", /*              84 */
696         "CASTORE        ", /*              85 */
697         "SASTORE        ", /*              86 */
698         "POP            ", /*              87 */
699         "POP2           ", /*              88 */
700         "DUP            ", /*              89 */
701         "DUP_X1         ", /*              90 */
702         "DUP_X2         ", /*              91 */
703         "DUP2           ", /*              92 */
704         "DUP2_X1        ", /*              93 */
705         "DUP2_X2        ", /*              94 */
706         "SWAP           ", /*              95 */
707         "IADD           ", /*              96 */
708         "LADD           ", /*              97 */
709         "FADD           ", /*              98 */
710         "DADD           ", /*              99 */
711         "ISUB           ", /*             100 */
712         "LSUB           ", /*             101 */
713         "FSUB           ", /*             102 */
714         "DSUB           ", /*             103 */
715         "IMUL           ", /*             104 */
716         "LMUL           ", /*             105 */
717         "FMUL           ", /*             106 */
718         "DMUL           ", /*             107 */
719         "IDIV           ", /*             108 */
720         "LDIV           ", /*             109 */
721         "FDIV           ", /*             110 */
722         "DDIV           ", /*             111 */
723         "IREM           ", /*             112 */
724         "LREM           ", /*             113 */
725         "FREM           ", /*             114 */
726         "DREM           ", /*             115 */
727         "INEG           ", /*             116 */
728         "LNEG           ", /*             117 */
729         "FNEG           ", /*             118 */
730         "DNEG           ", /*             119 */
731         "ISHL           ", /*             120 */
732         "LSHL           ", /*             121 */
733         "ISHR           ", /*             122 */
734         "LSHR           ", /*             123 */
735         "IUSHR          ", /*             124 */
736         "LUSHR          ", /*             125 */
737         "IAND           ", /*             126 */
738         "LAND           ", /*             127 */
739         "IOR            ", /*             128 */
740         "LOR            ", /*             129 */
741         "IXOR           ", /*             130 */
742         "LXOR           ", /*             131 */
743         "IINC           ", /*             132 */
744         "I2L            ", /*             133 */
745         "I2F            ", /*             134 */
746         "I2D            ", /*             135 */
747         "L2I            ", /*             136 */
748         "L2F            ", /*             137 */
749         "L2D            ", /*             138 */
750         "F2I            ", /*             139 */
751         "F2L            ", /*             140 */
752         "F2D            ", /*             141 */
753         "D2I            ", /*             142 */
754         "D2L            ", /*             143 */
755         "D2F            ", /*             144 */
756         "INT2BYTE       ", /*             145 */
757         "INT2CHAR       ", /*             146 */
758         "INT2SHORT      ", /*             147 */
759         "LCMP           ", /*             148 */
760         "FCMPL          ", /*             149 */
761         "FCMPG          ", /*             150 */
762         "DCMPL          ", /*             151 */
763         "DCMPG          ", /*             152 */
764         "IFEQ           ", /*             153 */
765         "IFNE           ", /*             154 */
766         "IFLT           ", /*             155 */
767         "IFGE           ", /*             156 */
768         "IFGT           ", /*             157 */
769         "IFLE           ", /*             158 */
770         "IF_ICMPEQ      ", /*             159 */
771         "IF_ICMPNE      ", /*             160 */
772         "IF_ICMPLT      ", /*             161 */
773         "IF_ICMPGE      ", /*             162 */
774         "IF_ICMPGT      ", /*             163 */
775         "IF_ICMPLE      ", /*             164 */
776         "IF_ACMPEQ      ", /*             165 */
777         "IF_ACMPNE      ", /*             166 */
778         "GOTO           ", /*             167 */
779         "JSR            ", /*             168 */
780         "RET            ", /*             169 */
781         "TABLESWITCH    ", /*             170 */
782         "LOOKUPSWITCH   ", /*             171 */
783         "IRETURN        ", /*             172 */
784         "LRETURN        ", /*             173 */
785         "FRETURN        ", /*             174 */
786         "DRETURN        ", /*             175 */
787         "ARETURN        ", /*             176 */
788         "RETURN         ", /*             177 */
789         "GETSTATIC      ", /*             178 */
790         "PUTSTATIC      ", /*             179 */
791         "GETFIELD       ", /*             180 */
792         "PUTFIELD       ", /*             181 */
793         "INVOKEVIRTUAL  ", /*             182 */
794         "INVOKESPECIAL  ", /*             183 */
795         "INVOKESTATIC   ", /*             184 */
796         "INVOKEINTERFACE", /*             185 */
797         "UNDEF186       ", /*             186 */
798         "NEW            ", /*             187 */
799         "NEWARRAY       ", /*             188 */
800         "ANEWARRAY      ", /*             189 */
801         "ARRAYLENGTH    ", /*             190 */
802         "ATHROW         ", /*             191 */
803         "CHECKCAST      ", /*             192 */
804         "INSTANCEOF     ", /*             193 */
805         "MONITORENTER   ", /*             194 */
806         "MONITOREXIT    ", /*             195 */
807         "WIDE           ", /* WIDE        196 */
808         "MULTIANEWARRAY ", /*             197 */
809         "IFNULL         ", /*             198 */
810         "IFNONNULL      ", /*             199 */
811         "GOTO_W         ", /* GOTO_W      200 */
812         "JSR_W          ", /* JSR_W       201 */
813         "BREAKPOINT     ", /* BREAKPOINT  202 */
814
815                                 "UNDEF203", "UNDEF204", "UNDEF205",
816         "UNDEF206", "UNDEF207", "UNDEF208", "UNDEF209", "UNDEF210",
817         "UNDEF211", "UNDEF212", "UNDEF213", "UNDEF214", "UNDEF215",
818         "UNDEF216", "UNDEF217", "UNDEF218", "UNDEF219", "UNDEF220",
819         "UNDEF221", "UNDEF222", "UNDEF223", "UNDEF224", "UNDEF225",
820         "UNDEF226", "UNDEF227", "UNDEF228", "UNDEF229", "UNDEF230",
821         "UNDEF231", "UNDEF232", "UNDEF233", "UNDEF234", "UNDEF235",
822         "UNDEF236", "UNDEF237", "UNDEF238", "UNDEF239", "UNDEF240",
823         "UNDEF241", "UNDEF242", "UNDEF243", "UNDEF244", "UNDEF245",
824         "UNDEF246", "UNDEF247", "UNDEF248", "UNDEF249", "UNDEF250",
825         "UNDEF251", "UNDEF252", "UNDEF253", "UNDEF254", "UNDEF255"
826 };
827
828
829 /* jit_init ********************************************************************
830
831    Initializes the JIT subsystem.
832
833 *******************************************************************************/
834
835 void jit_init(void)
836 {
837         s4 i;
838
839         for (i = 0; i < 256; i++)
840                 stackreq[i] = 1;
841
842         stackreq[JAVA_NOP]          = 0;
843         stackreq[JAVA_ISTORE]       = 0;
844         stackreq[JAVA_LSTORE]       = 0;
845         stackreq[JAVA_FSTORE]       = 0;
846         stackreq[JAVA_DSTORE]       = 0;
847         stackreq[JAVA_ASTORE]       = 0;
848         stackreq[JAVA_ISTORE_0]     = 0;
849         stackreq[JAVA_ISTORE_1]     = 0;
850         stackreq[JAVA_ISTORE_2]     = 0;
851         stackreq[JAVA_ISTORE_3]     = 0;
852         stackreq[JAVA_LSTORE_0]     = 0;
853         stackreq[JAVA_LSTORE_1]     = 0;
854         stackreq[JAVA_LSTORE_2]     = 0;
855         stackreq[JAVA_LSTORE_3]     = 0;
856         stackreq[JAVA_FSTORE_0]     = 0;
857         stackreq[JAVA_FSTORE_1]     = 0;
858         stackreq[JAVA_FSTORE_2]     = 0;
859         stackreq[JAVA_FSTORE_3]     = 0;
860         stackreq[JAVA_DSTORE_0]     = 0;
861         stackreq[JAVA_DSTORE_1]     = 0;
862         stackreq[JAVA_DSTORE_2]     = 0;
863         stackreq[JAVA_DSTORE_3]     = 0;
864         stackreq[JAVA_ASTORE_0]     = 0;
865         stackreq[JAVA_ASTORE_1]     = 0;
866         stackreq[JAVA_ASTORE_2]     = 0;
867         stackreq[JAVA_ASTORE_3]     = 0;
868         stackreq[JAVA_IASTORE]      = 0;
869         stackreq[JAVA_LASTORE]      = 0;
870         stackreq[JAVA_FASTORE]      = 0;
871         stackreq[JAVA_DASTORE]      = 0;
872         stackreq[JAVA_AASTORE]      = 0;
873         stackreq[JAVA_BASTORE]      = 0;
874         stackreq[JAVA_CASTORE]      = 0;
875         stackreq[JAVA_SASTORE]      = 0;
876         stackreq[JAVA_POP]          = 0;
877         stackreq[JAVA_POP2]         = 0;
878         stackreq[JAVA_IFEQ]         = 0;
879         stackreq[JAVA_IFNE]         = 0;
880         stackreq[JAVA_IFLT]         = 0;
881         stackreq[JAVA_IFGE]         = 0;
882         stackreq[JAVA_IFGT]         = 0;
883         stackreq[JAVA_IFLE]         = 0;
884         stackreq[JAVA_IF_ICMPEQ]    = 0;
885         stackreq[JAVA_IF_ICMPNE]    = 0;
886         stackreq[JAVA_IF_ICMPLT]    = 0;
887         stackreq[JAVA_IF_ICMPGE]    = 0;
888         stackreq[JAVA_IF_ICMPGT]    = 0;
889         stackreq[JAVA_IF_ICMPLE]    = 0;
890         stackreq[JAVA_IF_ACMPEQ]    = 0;
891         stackreq[JAVA_IF_ACMPNE]    = 0;
892         stackreq[JAVA_GOTO]         = 0;
893         stackreq[JAVA_RET]          = 0;
894         stackreq[JAVA_TABLESWITCH]  = 0;
895         stackreq[JAVA_LOOKUPSWITCH] = 0;
896         stackreq[JAVA_IRETURN]      = 0;
897         stackreq[JAVA_LRETURN]      = 0;
898         stackreq[JAVA_FRETURN]      = 0;
899         stackreq[JAVA_DRETURN]      = 0;
900         stackreq[JAVA_ARETURN]      = 0;
901         stackreq[JAVA_RETURN]       = 0;
902         stackreq[JAVA_PUTSTATIC]    = 0;
903         stackreq[JAVA_PUTFIELD]     = 0;
904         stackreq[JAVA_MONITORENTER] = 0;
905         stackreq[JAVA_MONITOREXIT]  = 0;
906         stackreq[JAVA_WIDE]         = 0;
907         stackreq[JAVA_IFNULL]       = 0;
908         stackreq[JAVA_IFNONNULL]    = 0;
909         stackreq[JAVA_GOTO_W]       = 0;
910         stackreq[JAVA_BREAKPOINT]   = 0;
911         stackreq[JAVA_IINC]         = 0;
912         
913         stackreq[JAVA_SWAP] = 2;
914         stackreq[JAVA_DUP2] = 2;
915         stackreq[JAVA_DUP_X1] = 3;
916         stackreq[JAVA_DUP_X2] = 4;
917         stackreq[JAVA_DUP2_X1] = 3;
918         stackreq[JAVA_DUP2_X2] = 4;
919
920         /* initialize stack analysis subsystem */
921
922         (void) stack_init();
923
924         /* initialize show subsystem */
925
926 #if !defined(NDEBUG)
927         (void) show_init();
928 #endif
929
930         /* initialize codegen subsystem */
931
932         codegen_init();
933 }
934
935
936 /* jit_close *******************************************************************
937
938    Close the JIT subsystem.
939
940 *******************************************************************************/
941
942 void jit_close(void)
943 {
944         /* do nothing */
945 }
946
947
948 /* dummy function, used when there is no JavaVM code available                */
949
950 static u1 *do_nothing_function(void)
951 {
952         return NULL;
953 }
954
955
956 /* jit_compile *****************************************************************
957
958    Translates one method to machine code.
959
960 *******************************************************************************/
961
962 static u1 *jit_compile_intern(jitdata *jd);
963
964 u1 *jit_compile(methodinfo *m)
965 {
966         u1      *r;
967         jitdata *jd;
968         s4       dumpsize;
969
970         STATISTICS(count_jit_calls++);
971
972         /* Initialize the static function's class. */
973
974         /* ATTENTION: This MUST be done before the method lock is aquired,
975            otherwise we could run into a deadlock with <clinit>'s that
976            call static methods of it's own class. */
977
978         if ((m->flags & ACC_STATIC) && !(m->class->state & CLASS_INITIALIZED)) {
979 #if !defined(NDEBUG)
980                 if (initverbose)
981                         log_message_class("Initialize class ", m->class);
982 #endif
983
984                 if (!initialize_class(m->class))
985                         return NULL;
986
987                 /* check if the method has been compiled during initialization */
988
989                 if ((m->code != NULL) && (m->code->entrypoint != NULL))
990                         return m->code->entrypoint;
991         }
992
993         /* enter a monitor on the method */
994
995         BUILTIN_MONITOR_ENTER(m);
996
997         /* if method has been already compiled return immediately */
998
999         if (m->code != NULL) {
1000                 BUILTIN_MONITOR_EXIT(m);
1001
1002                 assert(m->code->entrypoint);
1003                 return m->code->entrypoint;
1004         }
1005
1006         STATISTICS(count_methods++);
1007
1008 #if defined(ENABLE_STATISTICS)
1009         /* measure time */
1010
1011         if (opt_getcompilingtime)
1012                 compilingtime_start();
1013 #endif
1014
1015         /* mark start of dump memory area */
1016
1017         dumpsize = dump_size();
1018
1019         /* allocate jitdata structure and fill it */
1020
1021         jd = DNEW(jitdata);
1022
1023         jd->m     = m;
1024         jd->cd    = DNEW(codegendata);
1025         jd->rd    = DNEW(registerdata);
1026 #if defined(ENABLE_LOOP)
1027         jd->ld    = DNEW(loopdata);
1028 #endif
1029
1030         /* Allocate codeinfo memory from the heap as we need to keep them. */
1031
1032         jd->code  = code_codeinfo_new(m); /* XXX check allocation */
1033
1034         /* set the flags for the current JIT run */
1035
1036         jd->flags = JITDATA_FLAG_PARSE;
1037
1038         if (opt_verify)
1039                 jd->flags |= JITDATA_FLAG_VERIFY;
1040
1041         if (opt_ifconv)
1042                 jd->flags |= JITDATA_FLAG_IFCONV;
1043
1044         if (opt_showintermediate)
1045                 jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
1046
1047         if (opt_showdisassemble)
1048                 jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
1049
1050         if (opt_verbosecall)
1051                 jd->flags |= JITDATA_FLAG_VERBOSECALL;
1052
1053 #if defined(ENABLE_JIT)
1054 # if defined(ENABLE_INTRP)
1055         if (!opt_intrp)
1056 # endif
1057                 /* initialize the register allocator */
1058
1059                 reg_setup(jd);
1060 #endif
1061
1062         /* setup the codegendata memory */
1063
1064         codegen_setup(jd);
1065
1066         /* now call internal compile function */
1067
1068         r = jit_compile_intern(jd);
1069
1070         /* clear pointers to dump memory area */
1071
1072         m->basicblocks     = NULL;
1073         m->basicblockindex = NULL;
1074         m->instructions    = NULL;
1075         m->stack           = NULL;
1076
1077         if (r != NULL) {
1078                 DEBUG_JIT_COMPILEVERBOSE("Running: ");
1079         }
1080         else {
1081                 /* We had an exception! Finish stuff here if necessary. */
1082
1083                 /* release codeinfo */
1084
1085                 code_codeinfo_free(jd->code);
1086
1087                 /* Release memory for basic block profiling information. */
1088
1089                 if (opt_prof)
1090                         if (m->bbfrequency)
1091                                 MFREE(m->bbfrequency, u4, m->basicblockcount);
1092         }
1093
1094         /* release dump area */
1095
1096         dump_release(dumpsize);
1097
1098 #if defined(ENABLE_STATISTICS)
1099         /* measure time */
1100
1101         if (opt_getcompilingtime)
1102                 compilingtime_stop();
1103 #endif
1104
1105         /* leave the monitor */
1106
1107         BUILTIN_MONITOR_EXIT(m);
1108
1109         /* return pointer to the methods entry point */
1110
1111         return r;
1112 }
1113
1114
1115 /* jit_recompile ***************************************************************
1116
1117    Recompiles a Java method.
1118
1119 *******************************************************************************/
1120
1121 u1 *jit_recompile(methodinfo *m)
1122 {
1123         u1      *r;
1124         jitdata *jd;
1125         u1       optlevel;
1126         s4       dumpsize;
1127
1128         /* check for max. optimization level */
1129
1130         optlevel = m->code->optlevel;
1131
1132         if (optlevel == 1) {
1133                 log_message_method("not recompiling: ", m);
1134                 return NULL;
1135         }
1136
1137         log_message_method("Recompiling start: ", m);
1138
1139         STATISTICS(count_jit_calls++);
1140
1141 #if defined(ENABLE_STATISTICS)
1142         /* measure time */
1143
1144         if (opt_getcompilingtime)
1145                 compilingtime_start();
1146 #endif
1147
1148         /* mark start of dump memory area */
1149
1150         dumpsize = dump_size();
1151
1152         /* allocate jitdata structure and fill it */
1153
1154         jd = DNEW(jitdata);
1155
1156         jd->m     = m;
1157         jd->cd    = DNEW(codegendata);
1158         jd->rd    = DNEW(registerdata);
1159 #if defined(ENABLE_LOOP)
1160         jd->ld    = DNEW(loopdata);
1161 #endif
1162         jd->flags = 0;
1163
1164         /* Allocate codeinfo memory from the heap as we need to keep them. */
1165
1166         jd->code  = code_codeinfo_new(m); /* XXX check allocation */
1167
1168         /* set the current optimization level to the previous one plus 1 */
1169
1170         jd->code->optlevel = optlevel + 1;
1171
1172         /* get the optimization flags for the current JIT run */
1173
1174         jd->flags |= JITDATA_FLAG_SHOWINTERMEDIATE;
1175         jd->flags |= JITDATA_FLAG_SHOWDISASSEMBLE;
1176 /*      jd->flags |= JITDATA_FLAG_VERBOSECALL; */
1177
1178 #if defined(ENABLE_JIT)
1179 # if defined(ENABLE_INTRP)
1180         if (!opt_intrp)
1181 # endif
1182                 /* initialize the register allocator */
1183
1184                 reg_setup(jd);
1185 #endif
1186
1187         /* setup the codegendata memory */
1188
1189         codegen_setup(jd);
1190
1191         /* now call internal compile function */
1192
1193         r = jit_compile_intern(jd);
1194
1195         /* clear pointers to dump memory area */
1196
1197         m->basicblocks     = NULL;
1198         m->basicblockindex = NULL;
1199         m->instructions    = NULL;
1200         m->stack           = NULL;
1201
1202         if (r == NULL) {
1203                 /* We had an exception! Finish stuff here if necessary. */
1204
1205                 /* release codeinfo */
1206
1207                 code_codeinfo_free(jd->code);
1208         }
1209
1210         /* release dump area */
1211
1212         dump_release(dumpsize);
1213
1214 #if defined(ENABLE_STATISTICS)
1215         /* measure time */
1216
1217         if (opt_getcompilingtime)
1218                 compilingtime_stop();
1219 #endif
1220
1221         log_message_method("Recompiling done: ", m);
1222
1223         /* return pointer to the methods entry point */
1224
1225         return r;
1226 }
1227
1228
1229 /* jit_compile_intern **********************************************************
1230
1231    Static internal function which does the actual compilation.
1232
1233 *******************************************************************************/
1234
1235 static u1 *jit_compile_intern(jitdata *jd)
1236 {
1237         methodinfo  *m;
1238         codegendata *cd;
1239         codeinfo    *code;
1240
1241 #if defined(ENABLE_RT_TIMING)
1242         struct timespec time_start,time_checks,time_parse,time_stack,
1243                                         time_typecheck,time_loop,time_ifconv,time_alloc,
1244                                         time_rplpoints,time_codegen;
1245 #endif
1246         
1247         RT_TIMING_GET_TIME(time_start);
1248
1249         /* get required compiler data */
1250
1251         m    = jd->m;
1252         code = jd->code;
1253         cd   = jd->cd;
1254         
1255         /* print log message for compiled method */
1256
1257         DEBUG_JIT_COMPILEVERBOSE("Compiling: ");
1258
1259         /* handle native methods and create a native stub */
1260
1261         if (m->flags & ACC_NATIVE) {
1262                 functionptr f;
1263
1264 #if defined(WITH_STATIC_CLASSPATH)
1265                 f = native_findfunction(m->class->name, m->name, m->descriptor,
1266                                                                 (m->flags & ACC_STATIC));
1267                 if (f == NULL)
1268                         return NULL;
1269 #else
1270                 f = NULL;
1271 #endif
1272
1273                 code = codegen_createnativestub(f, m);
1274
1275                 assert(!m->code); /* native methods are never recompiled */
1276                 m->code = code;
1277                 
1278                 return code->entrypoint;
1279         }
1280
1281         /* if there is no javacode, print error message and return empty method   */
1282
1283         if (m->jcode == NULL) {
1284                 DEBUG_JIT_COMPILEVERBOSE("No code given for: ");
1285
1286                 code->entrypoint = (u1 *) (ptrint) do_nothing_function;
1287                 m->code = code;
1288
1289                 return code->entrypoint;        /* return empty method                */
1290         }
1291
1292         /* initialisation of variables and subsystems */
1293
1294         m->isleafmethod = true;
1295
1296 #if defined(ENABLE_STATISTICS)
1297         if (opt_stat) {
1298                 count_tryblocks    += m->exceptiontablelength;
1299                 count_javacodesize += m->jcodelength + 18;
1300                 count_javaexcsize  += m->exceptiontablelength * SIZEOF_VOID_P;
1301         }
1302 #endif
1303
1304         RT_TIMING_GET_TIME(time_checks);
1305
1306         /* call the compiler passes ***********************************************/
1307
1308         DEBUG_JIT_COMPILEVERBOSE("Parsing: ");
1309
1310         /* call parse pass */
1311
1312         if (!parse(jd)) {
1313                 DEBUG_JIT_COMPILEVERBOSE("Exception while parsing: ");
1314
1315                 return NULL;
1316         }
1317         RT_TIMING_GET_TIME(time_parse);
1318
1319         DEBUG_JIT_COMPILEVERBOSE("Parsing done: ");
1320         DEBUG_JIT_COMPILEVERBOSE("Analysing: ");
1321
1322         /* call stack analysis pass */
1323
1324         if (!stack_analyse(jd)) {
1325                 DEBUG_JIT_COMPILEVERBOSE("Exception while analysing: ");
1326
1327                 return NULL;
1328         }
1329         RT_TIMING_GET_TIME(time_stack);
1330
1331         DEBUG_JIT_COMPILEVERBOSE("Analysing done: ");
1332
1333 #ifdef ENABLE_VERIFIER
1334         if (jd->flags & JITDATA_FLAG_VERIFY) {
1335                 DEBUG_JIT_COMPILEVERBOSE("Typechecking: ");
1336
1337                 /* call typecheck pass */
1338                 if (!typecheck(jd)) {
1339                         DEBUG_JIT_COMPILEVERBOSE("Exception while typechecking: ");
1340
1341                         return NULL;
1342                 }
1343
1344                 DEBUG_JIT_COMPILEVERBOSE("Typechecking done: ");
1345         }
1346 #endif
1347         RT_TIMING_GET_TIME(time_typecheck);
1348
1349 #if defined(ENABLE_LOOP)
1350         if (opt_loops) {
1351                 depthFirst(jd);
1352                 analyseGraph(jd);
1353                 optimize_loops(jd);
1354         }
1355 #endif
1356         RT_TIMING_GET_TIME(time_loop);
1357
1358 #if defined(ENABLE_IFCONV)
1359         if (JITDATA_HAS_FLAG_IFCONV(jd))
1360                 if (!ifconv_static(jd))
1361                         return NULL;
1362 #endif
1363         RT_TIMING_GET_TIME(time_ifconv);
1364
1365 #if defined(ENABLE_JIT)
1366 # if defined(ENABLE_INTRP)
1367         if (!opt_intrp) {
1368 # endif
1369                 DEBUG_JIT_COMPILEVERBOSE("Allocating registers: ");
1370
1371                 /* allocate registers */
1372 # if defined(ENABLE_LSRA)
1373                 if (opt_lsra) {
1374                         if (!lsra(jd))
1375                                 return NULL;
1376
1377                         STATISTICS(count_methods_allocated_by_lsra++);
1378
1379                 } else
1380 # endif /* defined(ENABLE_LSRA) */
1381                 {
1382                         STATISTICS(count_locals_conflicts += (cd->maxlocals - 1) * (cd->maxlocals));
1383
1384                         regalloc(jd);
1385                 }
1386
1387                 STATISTICS(reg_make_statistics(jd));
1388
1389                 DEBUG_JIT_COMPILEVERBOSE("Allocating registers done: ");
1390 # if defined(ENABLE_INTRP)
1391         }
1392 # endif
1393 #endif /* defined(ENABLE_JIT) */
1394         RT_TIMING_GET_TIME(time_alloc);
1395
1396         /* Allocate memory for basic block profiling information. This
1397            _must_ be done after loop optimization and register allocation,
1398            since they can change the basic block count. */
1399
1400         if (opt_prof)
1401                 m->bbfrequency = MNEW(u4, m->basicblockcount);
1402
1403         DEBUG_JIT_COMPILEVERBOSE("Generating code: ");
1404
1405         /* create the replacement points */
1406
1407         if (!replace_create_replacement_points(jd))
1408                 return NULL;
1409         RT_TIMING_GET_TIME(time_rplpoints);
1410
1411         /* now generate the machine code */
1412
1413 #if defined(ENABLE_JIT)
1414 # if defined(ENABLE_INTRP)
1415         if (opt_intrp) {
1416                 if (!intrp_codegen(jd)) {
1417                         DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1418
1419                         return NULL;
1420                 }
1421         } else
1422 # endif
1423                 {
1424                         if (!codegen(jd)) {
1425                                 DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1426
1427                                 return NULL;
1428                         }
1429                 }
1430 #else
1431         if (!intrp_codegen(jd)) {
1432                 DEBUG_JIT_COMPILEVERBOSE("Exception while generating code: ");
1433
1434                 return NULL;
1435         }
1436 #endif
1437         RT_TIMING_GET_TIME(time_codegen);
1438
1439         DEBUG_JIT_COMPILEVERBOSE("Generating code done: ");
1440
1441 #if !defined(NDEBUG)
1442         /* intermediate and assembly code listings */
1443                 
1444         if (JITDATA_HAS_FLAG_SHOWINTERMEDIATE(jd)) {
1445                 show_method(jd);
1446         }
1447         else if (JITDATA_HAS_FLAG_SHOWDISASSEMBLE(jd)) {
1448 # if defined(ENABLE_DISASSEMBLER)
1449                 DISASSEMBLE(code->entrypoint,
1450                                         code->entrypoint + (code->mcodelength - cd->dseglen));
1451 # endif
1452         }
1453
1454         if (opt_showddatasegment)
1455                 dseg_display(jd);
1456 #endif
1457
1458         DEBUG_JIT_COMPILEVERBOSE("Compiling done: ");
1459
1460         /* switch to the newly generated code */
1461
1462         assert(code);
1463         assert(code->entrypoint);
1464
1465         /* add the current compile version to the methodinfo */
1466
1467         code->prev = m->code;
1468         m->code = code;
1469
1470         RT_TIMING_TIME_DIFF(time_start,time_checks,RT_TIMING_JIT_CHECKS);
1471         RT_TIMING_TIME_DIFF(time_checks,time_parse,RT_TIMING_JIT_PARSE);
1472         RT_TIMING_TIME_DIFF(time_parse,time_stack,RT_TIMING_JIT_STACK);
1473         RT_TIMING_TIME_DIFF(time_stack,time_typecheck,RT_TIMING_JIT_TYPECHECK);
1474         RT_TIMING_TIME_DIFF(time_typecheck,time_loop,RT_TIMING_JIT_LOOP);
1475         RT_TIMING_TIME_DIFF(time_loop,time_alloc,RT_TIMING_JIT_ALLOC);
1476         RT_TIMING_TIME_DIFF(time_alloc,time_rplpoints,RT_TIMING_JIT_RPLPOINTS);
1477         RT_TIMING_TIME_DIFF(time_rplpoints,time_codegen,RT_TIMING_JIT_CODEGEN);
1478         RT_TIMING_TIME_DIFF(time_start,time_codegen,RT_TIMING_JIT_TOTAL);
1479
1480         /* return pointer to the methods entry point */
1481
1482         return code->entrypoint;
1483
1484
1485
1486 /* jit_asm_compile *************************************************************
1487
1488    This method is called from asm_vm_call_method and does:
1489
1490      - create stackframe info for exceptions
1491      - compile the method
1492      - patch the entrypoint of the method into the calculated address in
1493        the JIT code
1494      - flushes the instruction cache.
1495
1496 *******************************************************************************/
1497
1498 u1 *jit_asm_compile(methodinfo *m, u1 *mptr, u1 *sp, u1 *ra)
1499 {
1500         stackframeinfo  sfi;
1501         u1             *entrypoint;
1502         u1             *pa;
1503         ptrint         *p;
1504
1505         /* create the stackframeinfo (XPC is equal to RA) */
1506
1507         stacktrace_create_extern_stackframeinfo(&sfi, NULL, sp, ra, ra);
1508
1509         /* actually compile the method */
1510
1511         entrypoint = jit_compile(m);
1512
1513         /* remove the stackframeinfo */
1514
1515         stacktrace_remove_stackframeinfo(&sfi);
1516
1517         /* there was a problem during compilation */
1518
1519         if (entrypoint == NULL)
1520                 return NULL;
1521
1522         /* get the method patch address */
1523
1524         pa = md_get_method_patch_address(ra, &sfi, mptr);
1525
1526         /* patch the method entry point */
1527
1528         p = (ptrint *) pa;
1529
1530         *p = (ptrint) entrypoint;
1531
1532         /* flush the instruction cache */
1533
1534         md_icacheflush(pa, SIZEOF_VOID_P);
1535
1536         return entrypoint;
1537 }
1538
1539
1540 /*
1541  * These are local overrides for various environment variables in Emacs.
1542  * Please do not remove this and leave it at the end of the file, where
1543  * Emacs will automagically detect them.
1544  * ---------------------------------------------------------------------
1545  * Local variables:
1546  * mode: c
1547  * indent-tabs-mode: t
1548  * c-basic-offset: 4
1549  * tab-width: 4
1550  * End:
1551  * vim:noexpandtab:sw=4:ts=4:
1552  */