updated inline.c so works with lazy loading
[cacao.git] / src / vm / jit / inline / inline.c
1 /* jit/inline.c - code inliner
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Dieter Thuernbeck
28
29    $Id: inline.c 1205 2004-06-25 06:18:44Z carolyn $
30
31 */
32
33
34 #include <stdio.h>
35 #include <string.h>
36 #include "main.h"
37 #include "jit/inline.h"
38 #include "jit/jit.h"
39 #include "jit/parse.h"
40 #include "loader.h"
41 #include "tables.h"
42 #include "toolbox/logging.h"
43 #include "toolbox/memory.h"
44
45
46 // checked functions and macros: LOADCONST code_get OP1 BUILTIN block_insert bound_check ALIGN
47
48 // replace jcodelength loops with correct number after main for loop in parse()!
49
50 static list *inlining_stack;
51 //static list *inlining_patchlist;
52 bool isinlinedmethod;
53 int cumjcodelength;         /* cumulative immediate intruction length      */
54 static int cumlocals;
55 int cummaxstack;
56 int cumextablelength;
57 static int cummethods;
58 inlining_methodinfo *inlining_rootinfo;
59
60
61 void inlining_init(methodinfo *m)
62 {
63         inlining_stack = NULL;
64         //      inlining_patchlist = NULL;
65         isinlinedmethod = 0;
66         cumjcodelength = 0;
67         cumlocals = 0;
68         cumextablelength = 0;
69         cummaxstack = 0;
70         cummethods = 0;
71
72         inlining_stack = NEW(list);
73         list_init(inlining_stack, OFFSET(t_inlining_stacknode, linkage));
74         
75         inlining_rootinfo = inlining_analyse_method(m, 0, 0, 0, 0);
76         m->maxlocals = cumlocals;
77         m->maxstack = cummaxstack;
78 }
79
80
81 void inlining_cleanup()
82 {
83         FREE(inlining_stack, t_inlining_stacknode);
84 }
85
86
87 void inlining_push_compiler_variables(methodinfo *m, int i, int p, int nextp, int opcode, inlining_methodinfo *inlinfo) 
88 {
89         t_inlining_stacknode *new = NEW(t_inlining_stacknode);
90
91         new->i = i;
92         new->p = p;
93         new->nextp = nextp;
94         new->opcode = opcode;
95         new->method = m;
96         //      new->patchlist = inlining_patchlist;
97         new->inlinfo = inlinfo;
98         
99         list_addfirst(inlining_stack, new);
100         isinlinedmethod++;
101 }
102
103
104 void inlining_pop_compiler_variables(methodinfo *m, int *i, int *p, int *nextp, int *opcode, inlining_methodinfo **inlinfo) 
105 {
106         t_inlining_stacknode *tmp = (t_inlining_stacknode *) list_first(inlining_stack);
107
108         if (!isinlinedmethod) panic("Attempting to pop from inlining stack in toplevel method!\n");
109
110         *i = tmp->i;
111         *p = tmp->p;
112         *nextp = tmp->nextp;
113         *opcode = tmp->opcode;
114         *inlinfo = tmp->inlinfo;
115
116         /* XXX TWISTI */
117 /*      method = tmp->method; */
118 /*      class = method->class; */
119 /*      jcodelength = method->jcodelength; */
120 /*      jcode = method->jcode; */
121         //      inlining_patchlist = tmp->patchlist;
122
123         list_remove(inlining_stack, tmp);
124         FREE(tmp, t_inlining_stacknode);
125         isinlinedmethod--;
126 }
127
128
129 void inlining_set_compiler_variables_fun(methodinfo *m)
130 {
131         /* XXX TWISTI */
132 /*      method = m; */
133 /*      class = m->class; */
134 /*      jcodelength = m->jcodelength; */
135 /*      jcode = m->jcode; */
136         
137         //      inlining_patchlist = DNEW(list);
138         //      list_init(inlining_patchlist, OFFSET(t_patchlistnode, linkage));
139 }
140
141
142 /*void inlining_addpatch(instruction *iptr)  
143   {
144   t_patchlistnode *patch = DNEW(t_patchlistnode);
145   patch->iptr = iptr;
146   list_addlast(inlining_patchlist, patch);
147   }*/
148
149
150 classinfo *first_occurence(classinfo* class, utf* name, utf* desc)
151 {
152         classinfo *first = class;
153         
154         for (; class->super != NULL ; class = class->super) {
155                 if (class_findmethod(class->super, name, desc) != NULL) {
156                         first = class->super;
157                 }                       
158         }
159
160         return first;
161 }
162
163
164 bool is_unique_rec(classinfo *class, methodinfo *m, utf* name, utf* desc)
165 {
166         methodinfo *tmp = class_findmethod(class, name, desc);
167         if ((tmp != NULL) && (tmp != m))
168                 return false;
169
170         for (; class != NULL; class = class->nextsub) {
171                 if ((class->sub != NULL) && !is_unique_rec(class->sub, m, name, desc)) {
172                         return false; 
173                 }
174         }
175         return true;
176 }
177
178
179 bool is_unique_method(classinfo *class, methodinfo *m, utf* name, utf* desc)
180 {
181         classinfo *firstclass;
182         
183         /*      sprintf (logtext, "First occurence of: ");
184         utf_sprint (logtext+strlen(logtext), m->class->name);
185         strcpy (logtext+strlen(logtext), ".");
186         utf_sprint (logtext+strlen(logtext), m->name);
187         utf_sprint (logtext+strlen(logtext), m->descriptor);
188         dolog (); */
189         
190         firstclass = first_occurence(class, name, desc);
191         
192         /*      sprintf (logtext, "\nis in class:");
193         utf_sprint (logtext+strlen(logtext), firstclass->name);
194         dolog (); */
195
196         if (firstclass != class) return false;
197
198         return is_unique_rec(class, m, name, desc);
199 }
200
201
202 inlining_methodinfo *inlining_analyse_method(methodinfo *m, int level, int gp, int firstlocal, int maxstackdepth)
203 {
204         inlining_methodinfo *newnode = DNEW(inlining_methodinfo);
205         u1 *jcode = m->jcode;
206         int jcodelength = m->jcodelength;
207         int p;
208         int nextp;
209         int opcode;
210         int i;
211 /*      int lastlabel = 0; */
212         bool iswide = false, oldiswide;
213         bool *readonly = NULL;
214         int  *label_index = NULL;
215         bool isnotrootlevel = (level > 0);
216         bool isnotleaflevel = (level < INLINING_MAXDEPTH);
217
218         //      if (level == 0) gp = 0;
219         /*
220         sprintf (logtext, "Performing inlining analysis of: ");
221         utf_sprint (logtext+strlen(logtext), m->class->name);
222         strcpy (logtext+strlen(logtext), ".");
223         utf_sprint (logtext+strlen(logtext), m->name);
224         utf_sprint (logtext+strlen(logtext), m->descriptor);
225         dolog (); */
226
227         if (isnotrootlevel) {
228                 newnode->readonly = readonly = DMNEW(bool, m->maxlocals); //FIXME only paramcount entrys necessary
229                 for (i = 0; i < m->maxlocals; readonly[i++] = true);
230                 isnotrootlevel = true;
231
232         } else {
233                 readonly = NULL;
234         }
235         
236         label_index = DMNEW(int, jcodelength);
237
238         newnode->inlinedmethods = DNEW(list);
239         list_init(newnode->inlinedmethods, OFFSET(inlining_methodinfo, linkage));
240
241         newnode->method = m;
242         newnode->startgp = gp;
243         newnode->readonly = readonly;
244         newnode->label_index = label_index;
245         newnode->firstlocal = firstlocal;
246         cumjcodelength += jcodelength + m->paramcount + 1 + 5;
247
248         if ((firstlocal + m->maxlocals) > cumlocals) {
249                 cumlocals = firstlocal + m->maxlocals;
250         }
251
252         if ((maxstackdepth + m->maxstack) > cummaxstack) {
253                 cummaxstack = maxstackdepth + m->maxstack;
254         }
255
256         cumextablelength += m->exceptiontablelength;
257    
258
259         for (p = 0; p < jcodelength; gp += (nextp - p), p = nextp) {
260                 opcode = code_get_u1 (p);
261                 nextp = p + jcommandsize[opcode];
262                 oldiswide = iswide;
263
264                 /* figure out nextp */
265
266                 switch (opcode) {
267                 case JAVA_ILOAD:
268                 case JAVA_LLOAD:
269                 case JAVA_FLOAD:
270                 case JAVA_DLOAD:
271                 case JAVA_ALOAD: 
272
273                 case JAVA_ISTORE:
274                 case JAVA_LSTORE:
275                 case JAVA_FSTORE:
276                 case JAVA_DSTORE:
277                 case JAVA_ASTORE: 
278
279                 case JAVA_RET:
280                         if (iswide) {
281                                 nextp = p + 3;
282                                 iswide = false;
283                         }
284                         break;
285
286                 case JAVA_IINC:
287                         if (iswide) {
288                                 nextp = p + 5;
289                                 iswide = false;
290                         }
291                         break;
292
293                 case JAVA_WIDE:
294                         iswide = true;
295                         nextp = p + 1;
296                         break;
297
298                 case JAVA_LOOKUPSWITCH:
299                         nextp = ALIGN((p + 1), 4) + 4;
300                         nextp += code_get_u4(nextp) * 8 + 4;
301                         break;
302
303                 case JAVA_TABLESWITCH:
304                         nextp = ALIGN((p + 1), 4) + 4;
305                         nextp += (code_get_u4(nextp+4) - code_get_u4(nextp) + 1) * 4 + 4;
306                         break;
307                 }
308
309                 /* detect readonly variables in inlined methods */
310                 
311                 if (isnotrootlevel) { 
312                         bool iswide = oldiswide;
313                         
314                         switch (opcode) {
315                         case JAVA_ISTORE:
316                         case JAVA_LSTORE:
317                         case JAVA_FSTORE:
318                         case JAVA_DSTORE:
319                         case JAVA_ASTORE: 
320                                 if (!iswide) {
321                                         i = code_get_u1(p + 1);
322
323                                 } else {
324                                         i = code_get_u2(p + 1);
325                                 }
326                                 readonly[i] = false;
327                                 break;
328
329                         case JAVA_ISTORE_0:
330                         case JAVA_LSTORE_0:
331                         case JAVA_FSTORE_0:
332                         case JAVA_ASTORE_0:
333                                 readonly[0] = false;
334                                 break;
335
336                         case JAVA_ISTORE_1:
337                         case JAVA_LSTORE_1:
338                         case JAVA_FSTORE_1:
339                         case JAVA_ASTORE_1:
340                                 readonly[1] = false;
341                                 break;
342
343                         case JAVA_ISTORE_2:
344                         case JAVA_LSTORE_2:
345                         case JAVA_FSTORE_2:
346                         case JAVA_ASTORE_2:
347                                 readonly[2] = false;
348                                 break;
349
350                         case JAVA_ISTORE_3:
351                         case JAVA_LSTORE_3:
352                         case JAVA_FSTORE_3:
353                         case JAVA_ASTORE_3:
354                                 readonly[3] = false;
355                                 break;
356
357                         case JAVA_IINC:
358                                 if (!iswide) {
359                                         i = code_get_u1(p + 1);
360
361                                 } else {
362                                         i = code_get_u2(p + 1);
363                                 }
364                                 readonly[i] = false;
365                                 break;
366                         }
367                 }
368
369                 /*              for (i=lastlabel; i<=p; i++) label_index[i] = gp; 
370                 //              printf("lastlabel=%d p=%d gp=%d\n",lastlabel, p, gp);
371                 lastlabel = p+1; */
372                 for (i = p; i < nextp; i++) label_index[i] = gp;
373
374                 if (isnotleaflevel) { 
375
376                         switch (opcode) {
377                         case JAVA_INVOKEVIRTUAL:
378                                 if (!inlinevirtuals)
379                                         break;
380                         case JAVA_INVOKESTATIC:
381                                 i = code_get_u2(p + 1);
382                                 {
383                                         constant_FMIref *imr;
384                                         methodinfo *imi;
385
386                                         imr = class_getconstant(m->class, i, CONSTANT_Methodref);
387
388                                         if (!class_load(imr->class))
389                                                 return NULL;
390
391                                         if (!class_link(imr->class))
392                                                 return NULL;
393
394                                         imi = class_resolveclassmethod(imr->class,
395                                                                                                    imr->name,
396                                                                                                    imr->descriptor,
397                                                                                                    m->class,
398                                                                                                    true);
399
400                                         if (!imi)
401                                                 panic("Exception thrown while parsing bytecode"); /* XXX should be passed on */
402
403                                         if (opcode == JAVA_INVOKEVIRTUAL) {
404                                                 if (!is_unique_method(imi->class, imi, imr->name, imr->descriptor))
405                                                         break;
406                                         }
407
408                                         if ((cummethods < INLINING_MAXMETHODS) &&
409                                                 (!(imi->flags & ACC_NATIVE)) &&  
410                                                 (!inlineoutsiders || (m->class == imr->class)) && 
411                                                 (imi->jcodelength < INLINING_MAXCODESIZE) && 
412                                                 (imi->jcodelength > 0) && 
413                                                 (!inlineexceptions || (imi->exceptiontablelength == 0))) { //FIXME: eliminate empty methods?
414                                                 inlining_methodinfo *tmp;
415                                                 descriptor2types(imi);
416
417                                                 cummethods++;
418
419                                                 if (verbose) {
420                                                         char logtext[MAXLOGTEXT];
421                                                         sprintf(logtext, "Going to inline: ");
422                                                         utf_sprint(logtext  +strlen(logtext), imi->class->name);
423                                                         strcpy(logtext + strlen(logtext), ".");
424                                                         utf_sprint(logtext + strlen(logtext), imi->name);
425                                                         utf_sprint(logtext + strlen(logtext), imi->descriptor);
426                                                         log_text(logtext);
427                                                 }
428                                                 
429                                                 tmp = inlining_analyse_method(imi, level + 1, gp, firstlocal + m->maxlocals, maxstackdepth + m->maxstack);
430                                                 list_addlast(newnode->inlinedmethods, tmp);
431                                                 gp = tmp->stopgp;
432                                                 p = nextp;
433                                         }
434                                 }
435                                 break;
436                         }
437                 }  
438         } /* for */
439         
440         newnode->stopgp = gp;
441
442         /*
443         sprintf (logtext, "Result of inlining analysis of: ");
444         utf_sprint (logtext+strlen(logtext), m->class->name);
445         strcpy (logtext+strlen(logtext), ".");
446         utf_sprint (logtext+strlen(logtext), m->name);
447         utf_sprint (logtext+strlen(logtext), m->descriptor);
448         dolog ();
449         sprintf (logtext, "label_index[0..%d]->", jcodelength);
450         for (i=0; i<jcodelength; i++) sprintf (logtext, "%d:%d ", i, label_index[i]);
451         sprintf(logtext,"stopgp : %d\n",newnode->stopgp); */
452
453     return newnode;
454 }
455
456
457 /*
458  * These are local overrides for various environment variables in Emacs.
459  * Please do not remove this and leave it at the end of the file, where
460  * Emacs will automagically detect them.
461  * ---------------------------------------------------------------------
462  * Local variables:
463  * mode: c
464  * indent-tabs-mode: t
465  * c-basic-offset: 4
466  * tab-width: 4
467  * End:
468  */