* src/vm/jit/inline/inline.c: Committed the inliner. This code works but
[cacao.git] / src / vm / jit / inline / inline.c
1 /* src/vm/jit/inline/inline.c - code inliner
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: Edwin Steiner
28
29    Changes:
30
31    $Id: inline.c 4662 2006-03-21 00:13:45Z edwin $
32
33 */
34
35 #include "config.h"
36
37 #include "vm/types.h"
38
39 #include <assert.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <limits.h>
43
44 #include "mm/memory.h"
45 #include "toolbox/logging.h"
46 #include "vm/global.h"
47 #include "vm/options.h"
48 #include "vm/statistics.h"
49 #include "vm/jit/jit.h"
50 #include "vm/jit/parse.h"
51 #include "vm/jit/inline/inline.h"
52 #include "vm/jit/loop/loop.h"
53
54 #include "vm/class.h"
55 #include "vm/initialize.h"
56 #include "vm/method.h"
57 #include "vm/jit/jit.h"
58
59 #include "vm/jit/reg.h"
60 #include "vm/jit/stack.h"
61
62 #include "vm/jit/verify/typecheck.h"
63
64 #if defined(USE_THREADS)
65 # if defined(NATIVE_THREADS)
66 #  include "threads/native/threads.h"
67 # else
68 #  include "threads/green/threads.h"
69 # endif
70 #endif
71
72 #if 1
73 bool inline_debug_log = 0;
74 bool inline_debug_log_names = 0;
75 int inline_debug_start_counter = 0;
76 int inline_debug_max_size = INT_MAX;
77 int inline_debug_min_size = 0;
78 int inline_debug_end_counter = INT_MAX;
79 int inline_count_methods = 0;
80 #define DOLOG(code) do{ if (inline_debug_log) { code; } }while(0)
81 #else
82 #define DOLOG(code)
83 #endif
84
85 typedef struct inline_node inline_node;
86 typedef struct inline_target_ref inline_target_ref;
87 typedef struct inline_context inline_context;
88 typedef struct inline_stack_translation inline_stack_translation;
89 typedef struct inline_block_map inline_block_map;
90
91 struct inline_node {
92         inline_context *ctx;
93         
94         methodinfo *m;
95         inline_node *children;
96         inline_node *next;
97         inline_node *prev;
98         int depth;
99         
100         /* info about the call site (if depth > 0)*/
101         inline_node *parent;
102         basicblock *callerblock;
103         instruction *callerins;
104         s4 callerpc;
105         stackptr n_callerstack;
106         int n_callerstackdepth;
107         stackptr o_callerstack;
108
109         /* info about the callee */
110         int localsoffset;
111         int prolog_instructioncount;
112         int instructioncount;
113         int stackcount;
114         
115         /* cumulative values */
116         int cumul_instructioncount;
117         int cumul_stackcount;
118         int cumul_basicblockcount;
119         int cumul_maxstack;
120         int cumul_maxlocals;
121         int cumul_exceptiontablelength;
122
123         /* output */
124         instruction *inlined_iinstr;
125         instruction *inlined_iinstr_cursor;
126         stackptr n_inlined_stack;
127         stackptr n_inlined_stack_cursor;
128         basicblock *inlined_basicblocks;
129         basicblock *inlined_basicblocks_cursor;
130
131         /* register data */
132         registerdata *regdata;
133
134         /* temporary */
135         inline_target_ref *refs;
136         instruction *inline_start_instruction;
137 };
138
139 struct inline_target_ref {
140         inline_target_ref *next;
141         basicblock **ref;
142         basicblock *target;
143 };
144
145 struct inline_stack_translation {
146         stackptr o_sp;
147         stackptr n_sp;
148 };
149
150 struct inline_block_map {
151         inline_node *iln;
152         basicblock *o_block;
153         basicblock *n_block;
154 };
155
156 struct inline_context {
157         int next_block_number;
158
159         inline_block_map *blockmap;
160         int blockmap_index;
161         
162         stackptr o_translationlimit; /* if a stackptr is smaller than this, look it up in the table */
163         stackptr n_debug_stackbase;
164         inline_stack_translation *stacktranslationstart;
165
166         inline_stack_translation stacktranslation[1];
167 };
168
169 #include "inline_debug.c"
170
171 void inline_print_stats()
172 {
173         printf("inlined callers: %d\n",inline_count_methods);
174 }
175
176 static bool inline_jit_compile_intern(methodinfo *m, codegendata *cd, registerdata *rd,
177                                                           loopdata *ld)
178 {
179         assert(m);
180         assert(cd);
181         assert(rd);
182         assert(ld);
183         
184         /* XXX initialize the static function's class */
185
186         m->isleafmethod = true;
187
188         /* call the compiler passes ***********************************************/
189
190         /* call parse pass */
191
192         DOLOG( log_message_class("Parsing ", m->class) );
193         if (!parse(m, cd)) {
194                 return false;
195         }
196
197         /* call stack analysis pass */
198
199         if (!analyse_stack(m, cd, rd)) {
200                 return false;
201         }
202
203         return true;
204 }
205
206 static bool inline_jit_compile(inline_node *iln)
207 {
208         bool                r;
209         methodinfo         *m;
210         codegendata        *cd;
211         registerdata       *rd;
212         loopdata           *ld;
213         s4 i;
214
215         assert(iln);
216         m = iln->m;
217         assert(m);
218
219 #if defined(USE_THREADS)
220         /* enter a monitor on the method */
221         builtin_monitorenter((java_objectheader *) m);
222 #endif
223         
224         /* XXX dont parse these a second time because parse is not idempotent */
225         for (i=0; i<m->jcodelength; ++i) {
226                 if (m->jcode[i] == JAVA_TABLESWITCH || m->jcode[i] == JAVA_LOOKUPSWITCH) {
227                         r = false;
228                         goto return_r;
229                 }
230         }
231
232         /* allocate memory */
233         cd = DNEW(codegendata);
234         rd = DNEW(registerdata);
235         ld = DNEW(loopdata);
236
237 #if defined(ENABLE_JIT)
238 # if defined(ENABLE_INTRP)
239         if (!opt_intrp)
240 # endif
241                 /* initialize the register allocator */
242                 reg_setup(m, rd);
243 #endif
244
245         /* setup the codegendata memory */
246
247         codegen_setup(m, cd);
248
249         /* now call internal compile function */
250
251         r = inline_jit_compile_intern(m, cd, rd, ld);
252
253         if (r) {
254                 iln->regdata = rd;
255         }
256
257         /* free some memory */
258 #if 0
259
260         
261 #if defined(ENABLE_JIT)
262 # if defined(ENABLE_INTRP)
263         if (!opt_intrp)
264 # endif
265                 codegen_free(m, cd);
266 #endif
267
268 #endif
269
270 return_r:
271 #if defined(USE_THREADS)
272         /* leave the monitor */
273         builtin_monitorexit((java_objectheader *) m );
274 #endif
275
276         return r;
277 }
278
279 static void insert_inline_node(inline_node *parent,inline_node *child)
280 {
281         inline_node *first;
282         inline_node *succ;
283
284         assert(parent && child);
285
286         child->parent = parent;
287
288         first = parent->children;
289         if (!first) {
290                 /* insert as only node */
291                 parent->children = child;
292                 child->next = child;
293                 child->prev = child;
294                 return;
295         }
296
297         /* {there is at least one child already there} */
298
299         succ = first;
300         while (succ->callerpc < child->callerpc) {
301                 succ = succ->next;
302                 if (succ == first) {
303                         /* insert as last node */
304                         child->prev = first->prev;
305                         child->next = first;
306                         child->prev->next = child;
307                         child->next->prev = child;
308                         return;
309                 }
310         }
311
312         assert(succ->callerpc > child->callerpc);
313         
314         /* insert before succ */
315
316         child->prev = succ->prev;
317         child->next = succ;
318         child->prev->next = child;
319         child->next->prev = child;
320 }
321
322 static stackptr relocate_stack_ptr_intern(inline_node *iln,stackptr o_link,ptrint curreloc)
323 {
324         inline_stack_translation *tr;
325         
326         if (o_link) {
327                 /* XXX should limit range in both directions */
328                 if (o_link < iln->ctx->o_translationlimit) {
329                         /* this stack slot is from an earlier chunk, we must look it up */
330                         tr = iln->ctx->stacktranslationstart;
331                         while (tr >= iln->ctx->stacktranslation) {
332                                 if (o_link == tr->o_sp) {
333                                         DOLOG(printf("\t\t\ttable lookup %p -> %d\n",(void*)o_link,DEBUG_SLOT(tr->n_sp)));
334                                         return tr->n_sp;
335                                 }
336                                 tr--;
337                         }
338                         DOLOG(debug_dump_inline_context(iln));
339                         DOLOG(printf("\t\tFAILED TO TRANSLATE: %p\n",(void*)o_link));
340                         assert(false);
341                 }
342                 else {
343                         /* this stack slot it in the most recent chunk */
344                         assert(curreloc);
345                         DOLOG( printf("\t\t\toffset %d\n",curreloc) );
346                         return (stackptr) ((u1*)o_link + curreloc);
347                 }
348         }
349         return iln->n_callerstack;
350 }
351
352 /* XXX for debugging */
353 static stackptr relocate_stack_ptr(inline_node *iln,stackptr o_link,ptrint curreloc)
354 {
355         stackptr new;
356
357         new = relocate_stack_ptr_intern(iln,o_link,curreloc);
358         DOLOG(
359                 printf("\t\treloc %p -> %d (%p)\t(translimit=%p)\n",
360                                 (void*)o_link,DEBUG_SLOT(new),(void*)new,(void*)iln->ctx->o_translationlimit)
361         );
362         return new;
363 }
364
365 static void emit_instruction(inline_node *iln,instruction *ins,ptrint curreloc,stackptr o_curstack)
366 {
367         char indent[100];
368         int i;
369         instruction *n_ins;
370         inline_target_ref *ref;
371
372         assert(iln && ins);
373
374         for (i=0; i<iln->depth; ++i)
375                 indent[i] = '\t';
376         indent[i] = 0;
377
378         n_ins = (iln->inlined_iinstr_cursor++);
379         assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
380         
381         *n_ins = *ins;
382
383         switch (n_ins[0].opc) {
384                                 /****************************************/
385                                 /* VARIABLE ACCESS                      */
386
387                         case ICMD_ILOAD:
388                         case ICMD_IINC:
389                         case ICMD_FLOAD:
390                         case ICMD_LLOAD:
391                         case ICMD_DLOAD:
392                         case ICMD_ISTORE:
393                         case ICMD_FSTORE:
394                         case ICMD_LSTORE:
395                         case ICMD_DSTORE:
396                         case ICMD_ALOAD:
397                         case ICMD_ASTORE:
398                     case ICMD_RET:
399                         n_ins[0].op1 += iln->localsoffset;
400                         break;
401
402                         case ICMD_GOTO:
403                         case ICMD_IFNULL:
404                         case ICMD_IFNONNULL:
405                         case ICMD_IFEQ:
406                         case ICMD_IFNE:
407                         case ICMD_IFLT:
408                         case ICMD_IFGE:
409                         case ICMD_IFGT:
410                         case ICMD_IFLE:
411                         case ICMD_IF_ICMPEQ:
412                         case ICMD_IF_ICMPNE:
413                         case ICMD_IF_ICMPLT:
414                         case ICMD_IF_ICMPGE:
415                         case ICMD_IF_ICMPGT:
416                         case ICMD_IF_ICMPLE:
417                         case ICMD_IF_ACMPEQ:
418                         case ICMD_IF_ACMPNE:
419                         case ICMD_IF_LEQ:
420                         case ICMD_IF_LNE:
421                         case ICMD_IF_LLT:
422                         case ICMD_IF_LGE:
423                         case ICMD_IF_LGT:
424                         case ICMD_IF_LLE:
425                         case ICMD_IF_LCMPEQ:
426                         case ICMD_IF_LCMPNE:
427                         case ICMD_IF_LCMPLT:
428                         case ICMD_IF_LCMPGE:
429                         case ICMD_IF_LCMPGT:
430                         case ICMD_IF_LCMPLE:
431                         case ICMD_JSR:
432                                 ref = DNEW(inline_target_ref);
433                                 ref->ref = (basicblock **) &(n_ins[0].target);
434                                 ref->next = iln->refs;
435                                 iln->refs = ref;
436                                 break;
437
438                                 /****************************************/
439                                 /* RETURNS                              */
440
441                         case ICMD_RETURN:
442                                 if (iln->parent) {
443                                         n_ins[0].opc = ICMD_GOTO;
444                                         n_ins[0].dst = NULL;
445                                         goto return_tail;
446                                 }
447                                 break;
448
449                         case ICMD_ARETURN:
450                         case ICMD_IRETURN:
451                         case ICMD_LRETURN:
452                         case ICMD_FRETURN:
453                         case ICMD_DRETURN:
454                         if (iln->parent) {
455                                 n_ins[0].opc = ICMD_INLINE_GOTO;
456                                 n_ins[0].dst = o_curstack;
457 return_tail:
458                                 n_ins[0].target = (void *) (ptrint) (iln->depth + 0x333); /* XXX */
459                                 ref = DNEW(inline_target_ref);
460                                 ref->ref = (basicblock **) &(n_ins[0].target);
461                                 ref->next = iln->refs;
462                                 iln->refs = ref;
463                         }
464                         break;
465         }
466
467         n_ins[0].dst = relocate_stack_ptr(iln,n_ins[0].dst,curreloc);
468 }
469
470 static stackptr emit_inlining_prolog(inline_node *iln,inline_node *callee,stackptr n_curstack,instruction *o_iptr)
471 {
472         methodinfo *calleem;
473         methoddesc *md;
474         int i;
475         int localindex;
476         int depth;
477         int type;
478         instruction *n_ins;
479
480         assert(iln && callee && o_iptr && o_iptr->method == iln->m);
481
482         calleem = callee->m;
483         md = calleem->parseddesc;
484
485         localindex = callee->localsoffset + md->paramslots;
486         depth = stack_depth(n_curstack) - 1; /* XXX inefficient */
487         for (i=md->paramcount-1; i>=0; --i) {
488                 assert(iln);
489
490                 n_ins = (iln->inlined_iinstr_cursor++);
491                 assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
492
493                 type = md->paramtypes[i].type;
494
495                 localindex -= IS_2_WORD_TYPE(type) ? 2 : 1;
496                 assert(callee->regdata);
497
498                 DOLOG( printf("prologlocal %d type=%d lofs=%d in ",
499                            localindex - callee->localsoffset,
500                            callee->regdata->locals[localindex - callee->localsoffset][type].type,callee->localsoffset);
501                                 method_println(callee->m); );
502
503                 if (callee->regdata->locals[localindex - callee->localsoffset][type].type >= 0) {
504                         n_ins->opc = ICMD_ISTORE + type;
505                         n_ins->op1 = localindex;
506                 }
507                 else {
508                         n_ins->opc = IS_2_WORD_TYPE(type) ? ICMD_POP2 : ICMD_POP;
509                 }
510                 n_ins->method = iln->m;
511                 n_ins->line = o_iptr->line;
512                 assert(n_curstack);
513                 if (n_curstack->varkind == ARGVAR) {
514                         n_curstack->varkind = TEMPVAR;
515                         n_curstack->varnum = depth;
516                         n_curstack->flags &= ~INMEMORY;
517                 }
518                 n_curstack = n_curstack->prev;
519                 n_ins->dst = n_curstack;
520                 depth--;
521         }
522
523         n_ins = (iln->inlined_iinstr_cursor++);
524         assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
525
526         n_ins->opc = ICMD_INLINE_START;
527         n_ins->method = callee->m;
528         n_ins->dst = n_curstack;
529         n_ins->line = o_iptr->line;
530         n_ins->target = NULL; /* ease debugging */
531         iln->inline_start_instruction = n_ins;
532
533         return n_curstack;
534 }
535
536 static void emit_inlining_epilog(inline_node *iln,inline_node *callee,stackptr n_curstack,instruction *o_iptr)
537 {
538         instruction *n_ins;
539         
540         assert(iln && callee && o_iptr);
541         assert(iln->inline_start_instruction);
542
543         n_ins = (iln->inlined_iinstr_cursor++);
544         assert((n_ins - iln->inlined_iinstr) < iln->cumul_instructioncount);
545
546         n_ins->opc = ICMD_INLINE_END;
547         n_ins->method = callee->m;
548         n_ins->dst = n_curstack;
549         n_ins->line = o_iptr->line;
550         n_ins->target = iln->inline_start_instruction; /* needed for line number table creation */
551 }
552
553 static void rewrite_stack(inline_node *iln,stackptr o_first,stackptr o_last,ptrint curreloc)
554 {
555         int n;
556         stackptr o_sp;
557         stackptr n_sp;
558         
559         assert(iln);
560
561         if (!o_first) {
562                 assert(!o_last);
563                 DOLOG(printf("rewrite_stack: no stack slots\n"));
564                 return;
565         }
566
567         assert(o_first);
568         assert(o_last);
569         assert(o_first <= o_last);
570
571         n = o_last - o_first + 1;
572         assert(n >= 0);
573
574         o_sp = o_first;
575         n_sp = iln->n_inlined_stack_cursor;
576         
577         DOLOG(
578         printf("rewrite_stack: rewriting %d stack slots (%p,%p) -> (%d,%d)\n",
579                         n,(void*)o_first,(void*)o_last,DEBUG_SLOT(n_sp),
580                         DEBUG_SLOT(n_sp+n-1))
581         );
582
583         DOLOG( printf("o_first = "); debug_dump_stack(o_first); printf("\n") );
584         DOLOG( printf("o_last = "); debug_dump_stack(o_last); printf("\n") );
585         
586         while (o_sp <= o_last) {
587                 *n_sp = *o_sp;
588
589                 n_sp->prev = relocate_stack_ptr(iln,n_sp->prev,curreloc);
590                 switch (n_sp->varkind) {
591                         case STACKVAR: n_sp->varnum += iln->n_callerstackdepth; break;
592                         case LOCALVAR: n_sp->varnum += iln->localsoffset; break;
593                 }
594                 
595                 o_sp++;
596                 n_sp++;
597         }
598         DOLOG( printf("n_sp = "); debug_dump_stack(n_sp-1); printf("\n") );
599         
600         iln->n_inlined_stack_cursor = n_sp;
601 }
602
603 static void inline_resolve_block_refs(inline_target_ref **refs,basicblock *o_bptr,basicblock *n_bptr)
604 {
605         inline_target_ref *ref;
606         inline_target_ref *prev;
607
608         ref = *refs;
609         prev = NULL;
610         while (ref) {
611                 if (*(ref->ref) == o_bptr) {
612                         DOLOG(
613                                 if ((ptrint) o_bptr < (0x333+100)) { /* XXX */
614                                         printf("resolving RETURN block reference %p -> new L%03d (%p)\n",
615                                                         (void*)o_bptr,n_bptr->debug_nr,(void*)n_bptr);
616                                 }
617                                 else {
618                                         printf("resolving block reference old L%03d (%p) -> new L%03d (%p)\n",
619                                                         o_bptr->debug_nr,(void*)o_bptr,n_bptr->debug_nr,(void*)n_bptr);
620                                 }
621                         );
622                         
623                         *(ref->ref) = n_bptr;
624                         if (prev) {
625                                 prev->next = ref->next;
626                         }
627                         else {
628                                 *refs = ref->next;
629                         }
630                 }
631                 else {
632                         prev = ref;
633                 }
634                 ref = ref->next;
635         }
636 }
637
638 static basicblock * create_block(inline_node *iln,basicblock *o_bptr,inline_target_ref **refs,int indepth)
639 {
640         basicblock *n_bptr;
641         stackptr n_sp;
642         int i;
643
644         assert(iln && o_bptr && refs);
645         
646         n_bptr = iln->inlined_basicblocks_cursor++;
647         assert(n_bptr);
648         
649         memset(n_bptr,0,sizeof(basicblock));
650         n_bptr->mpc = -1;
651         
652         n_bptr->type = BBTYPE_STD; /* XXX not necessary */
653         n_bptr->iinstr = iln->inlined_iinstr_cursor;
654         n_bptr->next = n_bptr+1;
655         n_bptr->debug_nr = iln->ctx->next_block_number++;
656         n_bptr->indepth = indepth;
657
658         if (indepth) {
659                 /* allocate stackslots */
660                 iln->n_inlined_stack_cursor += indepth;
661                 n_sp = iln->n_inlined_stack_cursor - 1;
662                 n_bptr->instack = n_sp;
663
664                 /* link the stack elements */
665                 for (i=indepth-1; i>=0; --i) {
666                         n_sp->varkind = STACKVAR;
667                         n_sp->varnum = i;
668                         n_sp->prev = (i) ? n_sp-1 : NULL;
669                         n_sp->flags = 0; /* XXX */
670                         n_sp--;
671                 }
672         }
673
674         inline_resolve_block_refs(refs,o_bptr,n_bptr);
675         
676         return n_bptr;
677 }
678
679 static void fill_translation_table(inline_node *iln,stackptr o_sp,stackptr n_sp,int n_depth)
680 {
681         int i;
682
683         DOLOG(
684         printf("fill_translation_table (newdepth=%d):\n",n_depth);
685         printf("\tos_sp = "); debug_dump_stack(o_sp); printf("\n");
686         printf("\tns_sp = "); debug_dump_stack(n_sp); printf("\n");
687         );
688
689         /* we must translate all stack slots that were present before the call XXX  */
690         /* and the instack of the block */
691         iln->ctx->stacktranslationstart = iln->ctx->stacktranslation + (n_depth - 1);
692
693         /* fill the translation table */
694         if (n_depth) {
695                 i = n_depth-1;
696
697                 while (o_sp) {
698                         assert(i >= 0);
699                         assert(n_sp);
700                         iln->ctx->stacktranslation[i].o_sp = o_sp;
701                         iln->ctx->stacktranslation[i].n_sp = n_sp;
702                         n_sp->flags |= (o_sp->flags & SAVEDVAR); /* XXX correct? */
703                         n_sp->type = o_sp->type; /* XXX we overwrite this anyway with STACKVAR, right? */
704                         o_sp = o_sp->prev;
705                         n_sp = n_sp->prev;
706                         i--;
707                 }
708
709                 while (n_sp) {
710                         assert(i >= 0);
711                         assert(iln->ctx->stacktranslation[i].o_sp);
712                         iln->ctx->stacktranslation[i].n_sp = n_sp;
713                         n_sp->flags |= SAVEDVAR; /* XXX this is too conservative */
714                         n_sp = n_sp->prev;
715                         i--;
716                 }
717
718                 assert(i == -1);
719         }
720 }
721
722 static void rewrite_method(inline_node *iln)
723 {
724         basicblock *o_bptr;
725         s4 len;
726         instruction *o_iptr;
727         instruction *n_iptr;
728         stackptr o_dst;
729         stackptr n_sp;
730         stackptr o_sp;
731         stackptr o_curstack;
732         stackptr o_nexttorewrite;
733         stackptr o_lasttorewrite;
734         inline_node *nextcall;
735         ptrint curreloc;
736         basicblock *n_bptr;
737         inline_block_map *bm;
738         int i;
739         int icount;
740
741         assert(iln);
742
743         nextcall = iln->children;
744
745         /* set memory cursors */
746         iln->inlined_iinstr_cursor = iln->inlined_iinstr;
747         iln->n_inlined_stack_cursor = iln->n_inlined_stack;
748         iln->inlined_basicblocks_cursor = iln->inlined_basicblocks;
749
750         /* loop over basic blocks */
751         o_bptr = iln->m->basicblocks;
752         for (; o_bptr; o_bptr = o_bptr->next) {
753
754                 if (o_bptr->flags < BBREACHED) {
755                         DOLOG(
756                         printf("skipping old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
757                                         o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
758                                         (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
759                                         DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
760                                         DEBUG_SLOT(iln->n_callerstack));
761                         method_println(iln->m);
762                         );
763
764                         n_bptr = create_block(iln,o_bptr,&(iln->refs),o_bptr->indepth + iln->n_callerstackdepth);
765                         n_bptr->type = o_bptr->type;
766                         /* enter it in the blockmap */
767                         iln->ctx->blockmap[iln->ctx->blockmap_index].iln = iln;
768                         iln->ctx->blockmap[iln->ctx->blockmap_index].o_block = o_bptr;
769                         iln->ctx->blockmap[iln->ctx->blockmap_index++].n_block = n_bptr;
770                         n_bptr->flags = o_bptr->flags;
771                         continue;
772                 }
773
774                 assert(o_bptr->stack);
775                 
776                 len = o_bptr->icount;
777                 o_iptr = o_bptr->iinstr;
778
779                 DOLOG(
780                 printf("rewriting old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
781                                 o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
782                                 (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
783                                 DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
784                                 DEBUG_SLOT(iln->n_callerstack));
785                 method_println(iln->m);
786                 printf("o_instack: ");debug_dump_stack(o_bptr->instack);printf("\n");
787                 printf("o_callerstack: ");debug_dump_stack(iln->o_callerstack);printf("\n");
788                 );
789
790                 o_curstack = o_bptr->instack;
791
792                 /* create an inlined clone of this block */
793                 n_bptr = create_block(iln,o_bptr,&(iln->refs),o_bptr->indepth + iln->n_callerstackdepth);
794                 n_bptr->type = o_bptr->type;
795                 n_bptr->flags = o_bptr->flags;
796
797                 /* enter it in the blockmap */
798                 iln->ctx->blockmap[iln->ctx->blockmap_index].iln = iln;
799                 iln->ctx->blockmap[iln->ctx->blockmap_index].o_block = o_bptr;
800                 iln->ctx->blockmap[iln->ctx->blockmap_index++].n_block = n_bptr;
801
802                 DOLOG( debug_dump_inline_context(iln) );
803
804                 if (iln->n_callerstackdepth)
805                         iln->n_callerstack = n_bptr->instack-o_bptr->indepth;
806                 else
807                         iln->n_callerstack = NULL;
808                 fill_translation_table(iln,iln->o_callerstack,iln->n_callerstack,iln->n_callerstackdepth);
809                 fill_translation_table(iln,o_bptr->instack,n_bptr->instack,n_bptr->indepth);
810                 iln->ctx->o_translationlimit = o_bptr->stack;
811
812                 DOLOG( debug_dump_inline_context(iln) );
813
814                 /* calculate the stack element relocation */
815                 curreloc = (u1*)iln->n_inlined_stack_cursor - (u1*)o_bptr->stack;
816                 DOLOG( printf("curreloc <- %d = %p - %p\n",curreloc,(void*)iln->n_inlined_stack_cursor,(void*)(u1*)o_bptr->stack) );
817
818                 o_nexttorewrite = o_bptr->stack;
819                 o_lasttorewrite = o_bptr->stack-1;
820                 assert(o_nexttorewrite);
821                         
822                 icount = 0;
823
824                 while (--len >= 0) {
825                         o_dst = o_iptr->dst;
826
827                         DOLOG( printf("o_curstack = "); debug_dump_stack(o_curstack); show_icmd(o_iptr,false); printf(", dst = "); debug_dump_stack(o_dst); printf("\n") );
828
829                         if (nextcall && o_iptr == nextcall->callerins) {
830
831                                 /* rewrite stack elements produced so far in this block */
832                                 if (o_nexttorewrite <= o_lasttorewrite) {
833                                         rewrite_stack(iln, o_nexttorewrite, o_lasttorewrite, curreloc);
834                                 }
835                                 
836                                 /* write the inlining prolog */
837                                 n_sp = emit_inlining_prolog(iln,nextcall,relocate_stack_ptr(iln,o_curstack,curreloc),o_iptr);
838                                 icount += nextcall->m->parseddesc->paramcount + 1; /* XXX prolog instructions */
839
840                                 /* find the first stack slot under the arguments of the invocation */
841                                 o_sp = o_curstack;
842                                 for (i=0; i < nextcall->m->parseddesc->paramcount; ++i) {
843                                         assert(o_sp);
844                                         o_sp = o_sp->prev;
845                                 }
846                                 nextcall->o_callerstack = o_sp;
847
848                                 /* see how deep the new stack is after the arguments have been removed */
849                                 i = stack_depth(n_sp);
850                                 assert(i == stack_depth(nextcall->o_callerstack) + iln->n_callerstackdepth);
851
852                                 /* end current block */
853                                 n_bptr->icount = icount;
854                                 n_bptr->outstack = n_sp;
855                                 n_bptr->outdepth = i;
856                                 
857                                 /* caller stack depth for the callee */
858                                 assert(nextcall->n_callerstackdepth == i);
859                                 
860                                 /* set memory pointers in the callee */
861                                 nextcall->inlined_iinstr = iln->inlined_iinstr_cursor;
862                                 nextcall->n_inlined_stack = iln->n_inlined_stack_cursor;
863                                 nextcall->inlined_basicblocks = iln->inlined_basicblocks_cursor;
864                                 
865                                 /* recurse */
866                                 DOLOG( printf("entering inline "); show_icmd(o_iptr,false); printf("\n") );
867                                 rewrite_method(nextcall);
868                                 DOLOG( printf("leaving inline "); show_icmd(o_iptr,false); printf("\n") );
869
870                                 /* skip stack slots used by the inlined callee */
871                                 curreloc += (u1*)nextcall->n_inlined_stack_cursor - (u1*)iln->n_inlined_stack_cursor;
872                                 
873                                 /* update memory cursors */
874                                 assert(nextcall->inlined_iinstr_cursor == iln->inlined_iinstr_cursor + nextcall->cumul_instructioncount);
875                                 /*assert(nextcall->n_inlined_stack_cursor == iln->n_inlined_stack_cursor + nextcall->cumul_stackcount);*/
876                                 assert(nextcall->inlined_basicblocks_cursor == iln->inlined_basicblocks_cursor + nextcall->cumul_basicblockcount);
877                                 iln->inlined_iinstr_cursor = nextcall->inlined_iinstr_cursor;
878                                 iln->n_inlined_stack_cursor = nextcall->n_inlined_stack_cursor;
879                                 iln->inlined_basicblocks_cursor = nextcall->inlined_basicblocks_cursor;
880
881                                 /* start new block */
882                                 i = (nextcall->m->parseddesc->returntype.type == TYPE_VOID) ? 0 : 1; /* number of return slots */
883                                 assert(i == 0 || i == 1);
884                                 n_bptr = create_block(iln,(void*) (ptrint) (nextcall->depth + 0x333) /*XXX*/,
885                                                 &(nextcall->refs),nextcall->n_callerstackdepth + i);
886                                 n_bptr->flags = o_bptr->flags;
887                                 icount = 0;
888
889                                 /* skip allocated stack slots */
890                                 curreloc += sizeof(stackelement) * (n_bptr->indepth - i);
891                                 
892                                 /* fill the translation table for the slots present before the call */
893                                 n_sp = n_bptr->instack;
894                                 fill_translation_table(iln,nextcall->o_callerstack,(i) ? n_sp->prev : n_sp,nextcall->n_callerstackdepth);
895
896                                 /* the return slot */
897                                 if (i) {
898                                         assert(o_dst);
899                                         assert(n_sp);
900                                         fill_translation_table(iln,o_dst,n_sp,nextcall->n_callerstackdepth + 1);
901
902                                         o_nexttorewrite = o_dst + 1;
903                                         o_lasttorewrite = o_dst;
904                                 }
905                                 else {
906                                         /* the next chunk of stack slots start with (including) the slots produced */
907                                         /* by the invocation */
908                                         o_nexttorewrite = o_lasttorewrite + 1;
909                                         o_lasttorewrite = o_nexttorewrite - 1;
910                                 }
911                                 
912                                 DOLOG( debug_dump_inline_context(iln) );
913                                 iln->ctx->o_translationlimit = o_nexttorewrite;
914                                         
915                                 /* emit inlining epilog */
916                                 emit_inlining_epilog(iln,nextcall,n_sp,o_iptr);
917                                 icount++; /* XXX epilog instructions */
918
919                                 /* proceed to next call */
920                                 nextcall = nextcall->next;
921
922                                 DOLOG(
923                                 printf("resuming old L%03d (flags=%d,type=%d,os=%p,oid=%d,ois=%p,cursor=%d,curreloc=%d,callerstack=%d) of ",
924                                                 o_bptr->debug_nr,o_bptr->flags,o_bptr->type,
925                                                 (void*)o_bptr->stack,o_bptr->indepth,(void*)o_bptr->instack,
926                                                 DEBUG_SLOT(iln->n_inlined_stack_cursor),curreloc,
927                                                 DEBUG_SLOT(iln->n_callerstack));
928                                 method_println(iln->m);
929                                 );
930                         }
931                         else {
932                                 emit_instruction(iln,o_iptr,curreloc,o_curstack);
933                                 icount++;
934
935                                 if (o_dst > o_lasttorewrite)
936                                         o_lasttorewrite = o_dst;
937                         }
938
939                         DOLOG( printf("o_dst = %p\n",(void*)o_dst) );
940                         o_curstack = o_dst;
941                         o_iptr++;
942                 }
943
944                 /* end of basic block */
945                 /* rewrite stack after last call */
946                 if (o_nexttorewrite <= o_lasttorewrite) {
947                         rewrite_stack(iln,o_nexttorewrite,o_lasttorewrite,curreloc);
948                 }
949                 n_bptr->outstack = relocate_stack_ptr(iln,o_bptr->outstack,curreloc);
950                 n_bptr->outdepth = iln->n_callerstackdepth + o_bptr->outdepth;
951                 assert(n_bptr->outdepth == stack_depth(n_bptr->outstack));
952 #if 0
953                 if (n_bptr->outstack) {
954                         assert(curreloc);
955                         n_bptr->outstack += curreloc;
956                 }
957 #endif
958                 n_bptr->icount = icount;
959
960                 n_iptr = iln->inlined_iinstr_cursor - 1;
961                 if (n_iptr->opc == ICMD_INLINE_GOTO) {
962                         DOLOG( printf("creating stack slot for ICMD_INLINE_GOTO\n") );
963                         n_sp = iln->n_inlined_stack_cursor++;
964                         assert(n_iptr->dst);
965                         *n_sp = *n_iptr->dst;
966                         n_sp->prev = iln->n_callerstack;
967                         n_iptr->dst = n_sp;
968
969                         n_bptr->outdepth = iln->n_callerstackdepth + 1;
970                         n_bptr->outstack = n_sp;
971                 }
972         }
973
974         /* end of basic blocks */
975         if (!iln->depth) {
976                 n_bptr->next = NULL;
977         }
978
979         bm = iln->ctx->blockmap;
980         for (i=0; i<iln->ctx->blockmap_index; ++i, ++bm) {
981                 assert(bm->iln && bm->o_block && bm->n_block);
982                 if (bm->iln != iln)
983                         continue;
984                 inline_resolve_block_refs(&(iln->refs),iln->ctx->blockmap[i].o_block,iln->ctx->blockmap[i].n_block);
985         }
986
987 #ifndef NDEBUG
988         if (iln->refs) {
989                 inline_target_ref *ref;
990                 ref = iln->refs;
991                 while (ref) {
992                         if (!iln->depth || *(ref->ref) != (void*) (ptrint) (0x333 + iln->depth) /* XXX */) {
993                                 DOLOG( printf("XXX REMAINING REF at depth %d: %p\n",iln->depth,(void*)*(ref->ref)) );
994                                 assert(false);
995                         }
996                         ref = ref->next;
997                 }
998         }
999 #endif
1000 }
1001
1002 static basicblock * inline_map_block(inline_node *iln,basicblock *o_block,inline_node *targetiln)
1003 {
1004         inline_block_map *bm;
1005         inline_block_map *bmend;
1006         
1007         assert(iln);
1008         assert(targetiln);
1009         
1010         if (!o_block)
1011                 return NULL;
1012
1013         bm = iln->ctx->blockmap;
1014         bmend = bm + iln->ctx->blockmap_index;
1015
1016         while (bm < bmend) {
1017                 assert(bm->iln && bm->o_block && bm->n_block);
1018                 if (bm->o_block == o_block && bm->iln == targetiln)
1019                         return bm->n_block;
1020                 bm++;
1021         }
1022
1023         assert(false);
1024         return NULL; /* not reached */
1025 }
1026
1027 static exceptiontable * inline_exception_tables(inline_node *iln,exceptiontable *n_extable,exceptiontable **prevextable)
1028 {
1029         inline_node *child;
1030         exceptiontable *et;
1031         int i;
1032         
1033         assert(iln);
1034         assert(n_extable);
1035         assert(prevextable);
1036
1037         child = iln->children;
1038         if (child) {
1039                 do {
1040                         n_extable = inline_exception_tables(child,n_extable,prevextable);
1041                         child = child->next;
1042                 } while (child != iln->children);
1043         }
1044
1045         et = iln->m->exceptiontable;
1046         for (i=0; i<iln->m->exceptiontablelength; ++i) {
1047                 assert(et);
1048                 memset(n_extable,0,sizeof(exceptiontable));
1049                 n_extable->startpc = et->startpc;
1050                 n_extable->endpc = et->endpc;
1051                 n_extable->handlerpc = et->handlerpc;
1052                 n_extable->start = inline_map_block(iln,et->start,iln);
1053                 n_extable->end = inline_map_block(iln,et->end,iln);
1054                 n_extable->handler = inline_map_block(iln,et->handler,iln);
1055                 n_extable->catchtype = et->catchtype;
1056
1057                 if (*prevextable) {
1058                         (*prevextable)->down = n_extable;
1059                 }
1060                 *prevextable = n_extable;
1061                 
1062                 n_extable++;
1063                 et++;
1064         }
1065
1066         return n_extable;
1067 }
1068
1069 static void inline_locals(inline_node *iln,registerdata *rd)
1070 {
1071         int i;
1072         int t;
1073         inline_node *child;
1074
1075         assert(iln);
1076         assert(rd);
1077
1078         child = iln->children;
1079         if (child) {
1080                 do {
1081                         inline_locals(child,rd);
1082                         child = child->next;
1083                 } while (child != iln->children);
1084         }
1085
1086         assert(iln->regdata);
1087
1088         for (i=0; i<iln->m->maxlocals; ++i) {
1089                 for (t=TYPE_INT; t<=TYPE_ADR; ++t) {
1090                         DOLOG( printf("local %d type=%d in ",i,iln->regdata->locals[i][t].type); method_println(iln->m); );
1091                         if (iln->regdata->locals[i][t].type >= 0) {
1092                                 rd->locals[iln->localsoffset + i][t].type = iln->regdata->locals[i][t].type;
1093                                 rd->locals[iln->localsoffset + i][t].flags |= iln->regdata->locals[i][t].flags;
1094                         }
1095                 }
1096         }
1097
1098         if (iln->regdata->memuse > rd->memuse)
1099                 rd->memuse = iln->regdata->memuse;
1100         if (iln->regdata->argintreguse > rd->argintreguse)
1101                 rd->argintreguse = iln->regdata->argintreguse;
1102         if (iln->regdata->argfltreguse > rd->argfltreguse)
1103                 rd->argfltreguse = iln->regdata->argfltreguse;
1104 }
1105
1106 static void inline_stack_interfaces(inline_node *iln,registerdata *rd)
1107 {
1108         int i;
1109         int d;
1110         basicblock *bptr;
1111         stackptr sp;
1112
1113         assert(iln);
1114         assert(rd);
1115         assert(rd->interfaces);
1116
1117         bptr = iln->inlined_basicblocks;
1118         for (i=0; i<iln->cumul_basicblockcount; ++i, ++bptr) {
1119                 DOLOG( printf("INLINE STACK INTERFACE block L%03d\n",bptr->debug_nr) );
1120                 DOLOG( printf("\toutstack = ");debug_dump_stack(bptr->outstack);printf("\n") );
1121                 DOLOG( printf("\tinstack = ");debug_dump_stack(bptr->outstack);printf("\n") );
1122
1123                 assert(bptr->outdepth == stack_depth(bptr->outstack));
1124                 assert(bptr->indepth == stack_depth(bptr->instack));
1125                 
1126                 sp = bptr->outstack;
1127                 d = bptr->outdepth - 1;
1128                 while (sp) {
1129                         if ((sp->varkind == STACKVAR) && (sp->varnum > d)) {
1130                                 sp->varkind = TEMPVAR;
1131                         }
1132                         else {
1133                                 sp->varkind = STACKVAR;
1134                                 sp->varnum = d;
1135                         }
1136                         DOLOG( printf("INLINE STACK INTERFACE L%03d outstack d=%d varkind=%d varnum=%d type=%d flags=%01x\n",
1137                                         bptr->debug_nr,d,sp->varkind,sp->varnum,sp->type,sp->flags) );
1138                         rd->interfaces[d][sp->type].type = sp->type;
1139                         rd->interfaces[d][sp->type].flags |= sp->flags;
1140                         d--;
1141                         sp = sp->prev;
1142                 }
1143
1144                 sp = bptr->instack;
1145                 d = bptr->indepth - 1;
1146                 while (sp) {
1147                         rd->interfaces[d][sp->type].type = sp->type;
1148                         if (sp->varkind == STACKVAR && (sp->flags & SAVEDVAR)) {
1149                                 rd->interfaces[d][sp->type].flags |= SAVEDVAR;
1150                         }
1151                         DOLOG( printf("INLINE STACK INTERFACE L%03d instack d=%d varkind=%d varnum=%d type=%d flags=%01x\n",
1152                                         bptr->debug_nr,d,sp->varkind,sp->varnum,sp->type,sp->flags) );
1153                         d--;
1154                         sp = sp->prev;
1155                 }
1156         }
1157 }
1158
1159 static bool inline_inline_intern(methodinfo *m, codegendata *cd, registerdata *rd, inline_node *iln)
1160 {
1161         basicblock *bptr;
1162         s4 len;
1163         instruction *iptr;
1164         stackptr o_dst;
1165         stackptr o_curstack;
1166         int opcode;                                   /* invocation opcode */
1167         methodinfo *callee;
1168         inline_node *calleenode;
1169         inline_node *active;
1170         stackptr sp;
1171         int i;
1172
1173         assert(m);
1174         assert(iln);
1175
1176         iln->cumul_maxstack = iln->n_callerstackdepth + m->maxstack + 1 /* XXX builtins */;
1177         iln->cumul_maxlocals = iln->localsoffset + m->maxlocals;
1178         iln->cumul_exceptiontablelength += m->exceptiontablelength;
1179
1180         bptr = m->basicblocks;
1181         for (; bptr; bptr = bptr->next) {
1182
1183                 iln->cumul_basicblockcount++;
1184
1185                 if (bptr->flags < BBREACHED)
1186                         continue;
1187
1188                 assert(bptr->stack);
1189                 
1190                 len = bptr->icount;
1191                 iptr = bptr->iinstr;
1192                 o_curstack = bptr->instack;
1193
1194                 iln->instructioncount += len;
1195                 iln->cumul_instructioncount += len;
1196
1197 #if 0
1198                 printf("ADD INSTRUCTIONS [%d]: %d, count=%d, cumulcount=%d\n",
1199                                 iln->depth,len,iln->instructioncount,iln->cumul_instructioncount);
1200 #endif
1201
1202                 while (--len >= 0) {
1203
1204                         opcode = iptr->opc;
1205                         o_dst = iptr->dst;
1206
1207                         switch (opcode) {
1208                                 case ICMD_IINC:
1209                                         /* XXX we cannot deal with IINC's stack hacking */
1210                                         return false;
1211
1212                                 case ICMD_LOOKUPSWITCH:
1213                                 case ICMD_TABLESWITCH:
1214                                         /* XXX these are not implemented, yet. */
1215                                         return false;
1216                                 
1217                                 /****************************************/
1218                                 /* INVOKATIONS                          */
1219
1220                                 case ICMD_INVOKEVIRTUAL:
1221                                 case ICMD_INVOKESPECIAL:
1222                                 case ICMD_INVOKESTATIC:
1223                                 case ICMD_INVOKEINTERFACE:
1224                                         callee = (methodinfo *) iptr[0].val.a;
1225
1226                                         if (callee) {
1227                                                 if ((callee->flags & (ACC_STATIC | ACC_FINAL | ACC_PRIVATE) || opcode == ICMD_INVOKESPECIAL)
1228                                                     && !(callee->flags & (ACC_NATIVE | ACC_SYNCHRONIZED))) 
1229                                                 {
1230                                                         if (iln->depth < 3) {
1231                                                                 for (active = iln; active; active = active->parent) {
1232                                                                         if (callee == active->m) {
1233                                                                                 DOLOG( printf("RECURSIVE!\n") );
1234                                                                                 goto dont_inline;
1235                                                                         }
1236                                                                 }
1237                                                 
1238                                                                 calleenode = DNEW(inline_node);
1239                                                                 memset(calleenode,0,sizeof(inline_node));
1240                                                                 
1241                                                                 calleenode->ctx = iln->ctx;
1242                                                                 calleenode->m = callee;
1243
1244                                                                 if (!inline_jit_compile(calleenode))
1245                                                                         return false;
1246                                                                 
1247                                                                 calleenode->depth = iln->depth+1;
1248                                                                 calleenode->callerblock = bptr;
1249                                                                 calleenode->callerins = iptr;
1250                                                                 calleenode->callerpc = iptr - m->basicblocks->iinstr;
1251                                                                 
1252                                                                 calleenode->localsoffset = iln->localsoffset + m->maxlocals;
1253                                                                 calleenode->prolog_instructioncount = callee->parseddesc->paramcount;
1254
1255                                                                 calleenode->stackcount = callee->stackcount;
1256                                                                 calleenode->cumul_stackcount = callee->stackcount;
1257
1258                                                                 /* see how deep the stack is below the arguments */
1259                                                                 sp = o_curstack;
1260                                                                 for (i=0; sp; sp = sp->prev)
1261                                                                         i++;
1262                                                                 calleenode->n_callerstackdepth = iln->n_callerstackdepth + i - callee->parseddesc->paramcount;
1263
1264                                                                 insert_inline_node(iln,calleenode);
1265
1266                                                                 if (!inline_inline_intern(callee,cd,rd,calleenode))
1267                                                                         return false;
1268
1269                                                                 iln->cumul_instructioncount += calleenode->prolog_instructioncount;
1270                                                                 iln->cumul_instructioncount += calleenode->cumul_instructioncount - 1/*invoke*/ + 2 /*INLINE_START|END*/;
1271                                                                 iln->cumul_stackcount += calleenode->cumul_stackcount;
1272                                                                 iln->cumul_basicblockcount += calleenode->cumul_basicblockcount + 1/*XXX*/;
1273                                                                 iln->cumul_exceptiontablelength += calleenode->cumul_exceptiontablelength;
1274                                                                 if (calleenode->cumul_maxstack > iln->cumul_maxstack)
1275                                                                         iln->cumul_maxstack = calleenode->cumul_maxstack;
1276                                                                 if (calleenode->cumul_maxlocals > iln->cumul_maxlocals)
1277                                                                         iln->cumul_maxlocals = calleenode->cumul_maxlocals;
1278                                                         }
1279                                                 }
1280                                         }
1281 dont_inline:
1282
1283                                         break;
1284                         }
1285
1286                         o_curstack = o_dst;
1287                         ++iptr;
1288                 }
1289
1290                 /* end of basic block */
1291         }       
1292
1293         return true;
1294 }
1295
1296 static bool test_inlining(inline_node *iln,codegendata *cd,registerdata *rd,
1297                 methodinfo **resultmethod, codegendata **resultcd, registerdata **resultrd)
1298 {
1299         instruction *n_ins;
1300         stackptr n_stack;
1301         basicblock *n_bb;
1302         methodinfo *n_method;
1303         exceptiontable *n_ext;
1304         exceptiontable *prevext;
1305         codegendata *n_cd;
1306         registerdata *n_rd;
1307
1308         static int debug_verify_inlined_code = 1;
1309         static int debug_compile_inlined_code_counter = 0;
1310
1311         assert(iln && cd && rd && resultmethod && resultcd && resultrd);
1312
1313         *resultmethod = iln->m;
1314         *resultcd = cd;
1315         *resultrd = rd;
1316
1317 #if 0
1318         if (debug_compile_inlined_code_counter >5)
1319                 return false;
1320 #endif
1321
1322         n_ins = DMNEW(instruction,iln->cumul_instructioncount);
1323         iln->inlined_iinstr = n_ins;
1324
1325         n_stack = DMNEW(stackelement,iln->cumul_stackcount + 1000 /* XXX */);
1326         iln->n_inlined_stack = n_stack;
1327         iln->ctx->n_debug_stackbase = n_stack;
1328
1329         n_bb = DMNEW(basicblock,iln->cumul_basicblockcount);
1330         iln->inlined_basicblocks = n_bb;
1331
1332         iln->ctx->blockmap = DMNEW(inline_block_map,iln->cumul_basicblockcount);
1333
1334         rewrite_method(iln);
1335
1336         if (iln->cumul_exceptiontablelength) {
1337                 n_ext = DMNEW(exceptiontable,iln->cumul_exceptiontablelength);
1338                 prevext = NULL;
1339                 inline_exception_tables(iln,n_ext,&prevext);
1340                 if (prevext)
1341                         prevext->down = NULL;
1342         }
1343         else {
1344                 n_ext = NULL;
1345         }
1346
1347         /*******************************************************************************/
1348
1349         n_method = NEW(methodinfo);
1350         memcpy(n_method,iln->m,sizeof(methodinfo));
1351         n_method->maxstack = iln->cumul_maxstack; /* XXX put into cd,rd */
1352         n_method->maxlocals = iln->cumul_maxlocals;
1353         n_method->basicblockcount = iln->cumul_basicblockcount;
1354         n_method->basicblocks = iln->inlined_basicblocks;
1355         n_method->basicblockindex = NULL;
1356         n_method->instructioncount = iln->cumul_instructioncount;
1357         n_method->instructions = iln->inlined_iinstr;
1358         n_method->stackcount = iln->cumul_stackcount + 1000 /* XXX */;
1359         n_method->stack = iln->n_inlined_stack;
1360
1361         n_method->exceptiontablelength = iln->cumul_exceptiontablelength;
1362         n_method->exceptiontable = n_ext;
1363         n_method->linenumbercount = 0;
1364
1365         n_cd = DNEW(codegendata);
1366         memcpy(n_cd,cd,sizeof(codegendata));
1367         n_cd->method = n_method;
1368         n_cd->maxstack = n_method->maxstack;
1369         n_cd->maxlocals = n_method->maxlocals;
1370         n_cd->exceptiontablelength = n_method->exceptiontablelength;
1371         n_cd->exceptiontable = n_method->exceptiontable;
1372
1373         n_rd = DNEW(registerdata);
1374         reg_setup(n_method, n_rd);
1375
1376         iln->regdata = rd;
1377         inline_locals(iln,n_rd);
1378         DOLOG( printf("INLINING STACK INTERFACES FOR "); method_println(iln->m) );
1379         inline_stack_interfaces(iln,n_rd);
1380         
1381         if (debug_verify_inlined_code) {
1382                 debug_verify_inlined_code = 0;
1383                 DOLOG( printf("VERIFYING INLINED RESULT...\n") );
1384                 if (!typecheck(n_method,n_cd,n_rd)) {
1385                         *exceptionptr = NULL;
1386                         DOLOG( printf("XXX INLINED RESULT DID NOT PASS VERIFIER XXX\n") );
1387                         return false;
1388                 }
1389                 else {
1390                         DOLOG( printf("VERIFICATION PASSED.\n") );
1391                 }
1392                 debug_verify_inlined_code = 1;
1393         }
1394
1395 #if 1
1396         if (n_method->instructioncount >= inline_debug_min_size && n_method->instructioncount <= inline_debug_max_size) {
1397            if (debug_compile_inlined_code_counter >= inline_debug_start_counter 
1398                            && debug_compile_inlined_code_counter <= inline_debug_end_counter) 
1399 #else
1400         if (
1401                 (strcmp(n_method->class->name->text,"java/lang/reflect/Array") == 0 &&
1402                 strcmp(n_method->name->text,"<clinit>") == 0 &&
1403                 strcmp(n_method->descriptor->text,"()V") == 0)
1404                 ||
1405                 (strcmp(n_method->class->name->text,"java/lang/VMClassLoader") == 0 &&
1406                 strcmp(n_method->name->text,"getSystemClassLoader") == 0 &&
1407                 strcmp(n_method->descriptor->text,"()Ljava/lang/ClassLoader;") == 0)
1408                 ) 
1409         
1410         {
1411 #endif
1412            {
1413                         *resultmethod = n_method;
1414                         *resultcd = n_cd;
1415                         *resultrd = n_rd;
1416                         inline_count_methods++;
1417                         if (inline_debug_log_names)
1418                                 method_println(n_method);
1419
1420                         DOLOG(
1421                         printf("==== %d.INLINE ==================================================================\n",debug_compile_inlined_code_counter);
1422                         method_println(n_method);
1423                         show_icmd_method(iln->m,cd,rd);
1424                         dump_inline_tree(iln);
1425                         show_icmd_method(n_method,n_cd,n_rd);
1426                         debug_dump_inlined_code(iln,n_method,n_cd,n_rd);
1427                         printf("-------- DONE -----------------------------------------------------------\n");
1428                         fflush(stdout);
1429                         );
1430            }
1431
1432                 debug_compile_inlined_code_counter++;
1433         }
1434         return true;
1435 }
1436
1437 bool inline_inline(methodinfo *m, codegendata *cd, registerdata *rd,
1438                                 methodinfo **resultmethod, codegendata **resultcd, registerdata **resultrd)
1439 {
1440         inline_node *iln;
1441
1442 #if 0
1443         printf("==== INLINE ==================================================================\n");
1444         method_println(m);
1445 #endif
1446
1447         iln = DNEW(inline_node);
1448         memset(iln,0,sizeof(inline_node));
1449
1450         iln->ctx = (inline_context *) DMNEW(u1,sizeof(inline_context) + sizeof(inline_stack_translation) * 1000 /* XXX */);
1451         memset(iln->ctx,0,sizeof(inline_context));
1452         iln->ctx->stacktranslationstart = iln->ctx->stacktranslation - 1;
1453         iln->m = m;             
1454
1455         /* we cannot use m->instructioncount because it may be greater than 
1456          * the actual number of instructions in the basic blocks. */
1457         iln->instructioncount = 0;
1458         iln->cumul_instructioncount = 0;
1459
1460         iln->stackcount = m->stackcount;
1461         iln->cumul_stackcount = m->stackcount;
1462
1463         if (inline_inline_intern(m,cd,rd,iln)) {
1464         
1465 #if 0
1466                 printf("==== TEST INLINE =============================================================\n");
1467                 method_println(m);
1468 #endif
1469
1470                 if (iln->children)
1471                         test_inlining(iln,cd,rd,resultmethod,resultcd,resultrd);
1472         }
1473
1474 #if 0
1475         printf("-------- DONE -----------------------------------------------------------\n");
1476         fflush(stdout);
1477 #endif
1478
1479         return true;
1480 }
1481
1482 /*
1483  * These are local overrides for various environment variables in Emacs.
1484  * Please do not remove this and leave it at the end of the file, where
1485  * Emacs will automagically detect them.
1486  * ---------------------------------------------------------------------
1487  * Local variables:
1488  * mode: c
1489  * indent-tabs-mode: t
1490  * c-basic-offset: 4
1491  * tab-width: 4
1492  * End:
1493  * vim:noexpandtab:sw=4:ts=4:
1494  */