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