bidirectional layout of vftbl/interfaces added
[cacao.git] / newcomp.c
1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4 -*- */
2 /***************************** ncomp/ncomp.c ***********************************
3
4         Copyright (c) 1997 A. Krall, R. Grafl, M. Gschwind, M. Probst
5
6         See file COPYRIGHT for information on usage and disclaimer of warranties.
7
8         Contains the functions which translates a JavaVM method into native code.
9         This is the new version of the compiler which is a lot faster and has new
10         exception handling schemes. The main function is new_comp.
11
12         Authors: Andreas  Krall      EMAIL: cacao@complang.tuwien.ac.at
13                  Reinhard Grafl      EMAIL: cacao@complang.tuwien.ac.at
14
15         Last Change: 1997/11/05
16
17 *******************************************************************************/
18
19 #include "signal.h"
20 #include "global.h"
21 #include "ncomp/ncomp.h"
22
23 #include "loader.h"
24 #include "tables.h"
25 #include "builtin.h"
26 #include "native.h"
27 #include "asmpart.h"
28
29 #include "threads/thread.h"
30
31
32 /*************************** global switches **********************************/
33
34 bool compileverbose = false;
35 bool showstack = false;
36 bool showdisassemble = false; 
37 bool showddatasegment = false; 
38 bool showintermediate = false;
39 int  optimizelevel = 0;
40
41 bool checkbounds = true;
42 bool checknull = true;
43 bool checkfloats = true;
44 bool checksync = true;
45
46 bool getcompilingtime = false;
47 long compilingtime = 0;
48
49 int  has_ext_instr_set = 0;
50
51 bool statistics = false;         
52
53 int count_jit_calls = 0;
54 int count_methods = 0;
55 int count_spills = 0;
56 int count_pcmd_activ = 0;
57 int count_pcmd_drop = 0;
58 int count_pcmd_zero = 0;
59 int count_pcmd_const_store = 0;
60 int count_pcmd_const_alu = 0;
61 int count_pcmd_const_bra = 0;
62 int count_pcmd_load = 0;
63 int count_pcmd_move = 0;
64 int count_load_instruction = 0;
65 int count_pcmd_store = 0;
66 int count_pcmd_store_comb = 0;
67 int count_dup_instruction = 0;
68 int count_pcmd_op = 0;
69 int count_pcmd_mem = 0;
70 int count_pcmd_met = 0;
71 int count_pcmd_bra = 0;
72 int count_pcmd_table = 0;
73 int count_pcmd_return = 0;
74 int count_pcmd_returnx = 0;
75 int count_check_null = 0;
76 int count_check_bound = 0;
77 int count_max_basic_blocks = 0;
78 int count_basic_blocks = 0;
79 int count_javainstr = 0;
80 int count_max_javainstr = 0;
81 int count_javacodesize = 0;
82 int count_javaexcsize = 0;
83 int count_calls = 0;
84 int count_tryblocks = 0;
85 int count_code_len = 0;
86 int count_data_len = 0;
87 int count_cstub_len = 0;
88 int count_nstub_len = 0;
89 int count_max_new_stack = 0;
90 int count_upper_bound_new_stack = 0;
91 static int count_block_stack_init[11] = {0, 0, 0, 0, 0,  0, 0, 0, 0, 0,  0};
92 int *count_block_stack = count_block_stack_init;
93 static int count_analyse_iterations_init[5] = {0, 0, 0, 0, 0};
94 int *count_analyse_iterations = count_analyse_iterations_init;
95 static int count_method_bb_distribution_init[9] = {0, 0, 0, 0, 0,  0, 0, 0, 0};
96 int *count_method_bb_distribution = count_method_bb_distribution_init;
97 static int count_block_size_distribution_init[18] = {0, 0, 0, 0, 0,
98         0, 0, 0, 0, 0,   0, 0, 0, 0, 0,   0, 0, 0};
99 int *count_block_size_distribution = count_block_size_distribution_init;
100 static int count_store_length_init[21] = {0, 0, 0, 0, 0,  0, 0, 0, 0, 0,
101                                           0, 0, 0, 0, 0,  0, 0, 0, 0, 0,  0};
102 int *count_store_length = count_store_length_init;
103 static int count_store_depth_init[11] = {0, 0, 0, 0, 0,  0, 0, 0, 0, 0,   0};
104 int *count_store_depth = count_store_depth_init;
105
106
107 /*********************** include compiler data types **************************/ 
108
109 #include "ncomp/ncompdef.h"
110
111
112 /*********************** global compiler variables ****************************/
113
114                                 /* data about the currently compiled method   */
115
116 static classinfo  *class;       /* class the compiled method belongs to       */
117 static methodinfo *method;      /* pointer to method info of compiled method  */
118 static unicode    *descriptor;  /* type descriptor of compiled method         */
119 static u2         mparamcount;  /* number of parameters (incl. this)          */
120 static u1         *mparamtypes; /* types of all parameters (TYPE_INT, ...)    */
121 static u2         mreturntype;  /* return type of method                      */
122         
123 static int maxstack;            /* maximal JavaVM stack size                  */
124 static int maxlocals;           /* maximal number of local JavaVM variables   */
125 static int jcodelength;         /* length of JavaVM-codes                     */
126 static u1 *jcode;               /* pointer to start of JavaVM-code            */
127 static int exceptiontablelength;/* length of exception table                  */
128 static exceptiontable *extable; /* pointer to start of exception table        */
129
130 static int block_count;         /* number of basic blocks                     */
131 static basicblock *block;       /* points to basic block array                */
132 static int *block_index;        /* a table which contains for every byte of   */
133                                 /* JavaVM code a basic block index if at this */
134                                 /* byte there is the start of a basic block   */
135
136 static int instr_count;         /* number of JavaVM instructions              */
137 static instruction *instr;      /* points to intermediate code instructions   */
138
139 static int stack_count;         /* number of stack elements                   */
140 static stackelement *stack;     /* points to intermediate code instructions   */
141
142 static bool isleafmethod;       /* true if a method doesn't call subroutines  */
143
144 /* list of all classes used by the compiled method which have to be           */
145 /* initialised (if not already done) before execution of this method          */
146
147 static chain *uninitializedclasses;  
148                                 
149
150 /******************** include compiler subsystems *****************************/
151
152 #include "sysdep/ngen.h"        /* code generator header file                 */ 
153 #include "ncomp/ntools.c"       /* compiler tool functions                    */ 
154 #include "ncomp/mcode.c"        /* code generation tool functions             */ 
155 #include "sysdep/disass.c"      /* disassembler (for debug purposes only)     */ 
156 #include "ncomp/nparse.c"       /* parsing of JavaVM code                     */ 
157 #include "ncomp/nreg.c"         /* register allocation and support routines   */ 
158 #include "ncomp/nstack.c"       /* analysing the stack operations             */ 
159 #include "sysdep/ngen.c"        /* code generator                             */ 
160
161
162
163
164 /* dummy function, used when there is no JavaVM code available                */
165
166 static void* do_nothing_function() 
167 {
168         return NULL;
169 }
170
171
172 /*******************************************************************************
173
174         new_compile, new version of compiler, translates one method to machine code
175
176 *******************************************************************************/
177
178 methodptr new_compile(methodinfo *m)
179 {
180         int  dumpsize;
181         long starttime = 0;
182         long stoptime  = 0;
183
184         /* if method has been already compiled return immediately */
185
186         count_jit_calls++;
187
188         if (m->entrypoint)
189                 return m->entrypoint;
190
191         count_methods++;
192
193         intsDisable();      /* disable interrupts */
194         
195
196         /* mark start of dump memory area */
197
198         dumpsize = dump_size ();
199
200         /* measure time */
201
202         if (getcompilingtime)
203                 starttime = getcputime();
204
205         /* if there is no javacode print error message and return empty method    */
206
207         if (! m->jcode) {
208                 sprintf(logtext, "No code given for: ");
209                 unicode_sprint(logtext+strlen(logtext), m->class->name);
210                 strcpy(logtext+strlen(logtext), ".");
211                 unicode_sprint(logtext+strlen(logtext), m->name);
212                 unicode_sprint(logtext+strlen(logtext), m->descriptor);
213                 dolog();
214                 intsRestore();                             /* enable interrupts again */
215                 return (methodptr) do_nothing_function;    /* return empty method     */
216                 }
217
218         /* print log message for compiled method */
219
220         if (compileverbose) {
221                 sprintf(logtext, "Compiling: ");
222                 unicode_sprint(logtext+strlen(logtext), m->class->name);
223                 strcpy(logtext+strlen(logtext), ".");
224                 unicode_sprint(logtext+strlen(logtext), m->name);
225                 unicode_sprint(logtext+strlen(logtext), m->descriptor);
226                 dolog ();
227                 }
228
229
230         /* initialisation of variables and subsystems */
231
232         isleafmethod = true;
233
234         method = m;
235         class = m->class;
236         descriptor = m->descriptor;
237         maxstack = m->maxstack;
238         maxlocals = m->maxlocals;
239         jcodelength = m->jcodelength;
240         jcode = m->jcode;
241         exceptiontablelength = m->exceptiontablelength;
242         extable = m->exceptiontable;
243
244 #ifdef STATISTICS
245         count_tryblocks += exceptiontablelength;
246         count_javacodesize += jcodelength + 18;
247         count_javaexcsize += exceptiontablelength * 8;
248 #endif
249
250         /* initialise parameter type descriptor */
251
252         descriptor2types (m);
253         mreturntype = m->returntype;
254         mparamcount = m->paramcount;
255         mparamtypes = m->paramtypes;
256
257         /* initialize class list with class the compiled method belongs to */
258
259         uninitializedclasses = chain_new(); 
260         compiler_addinitclass (m->class);
261
262
263         /********************** call the compiler passes **************************/
264         
265         reg_init();
266         local_init();
267         mcode_init();
268
269         if (runverbose)
270                 allocate_literals();
271
272         parse();
273
274         analyse_stack();
275
276         interface_regalloc();
277
278         allocate_scratch_registers();
279         
280         local_regalloc();
281         
282         gen_mcode();
283
284         
285         /*********** Zwischendarstellungen auf Wunsch ausgeben **********/
286                 
287         if (showintermediate)
288                 show_icmd_method();
289         else if (showdisassemble)
290                 disassemble((void*) (m->mcode + dseglen), m->mcodelength - dseglen);
291
292         if (showddatasegment)
293                 dseg_display((void*) (m->mcode));
294
295
296
297         /* release dump area */
298
299         dump_release (dumpsize);
300
301         /* measure time */
302
303         if (getcompilingtime) {
304                 stoptime = getcputime();
305                 compilingtime += (stoptime-starttime); 
306                 }
307
308         /* initialize all used classes */
309         /* because of reentrant code global variables are not allowed here        */
310
311         {
312         chain *ul = uninitializedclasses;   /* list of uninitialized classes      */ 
313         classinfo *c;                       /* single class                       */
314
315         while ((c = chain_first(ul)) != NULL) {
316                 chain_remove (ul);
317                 class_init (c);                         /* may again call the compiler        */
318                 }
319         chain_free (ul);
320         }
321
322         intsRestore();    /* enable interrupts again */
323
324         /* return pointer to the methods entry point */
325         
326         return m -> entrypoint;
327 }
328
329
330 /************ functions for compiler initialisation and finalisation **********/
331
332 void ncomp_init ()
333 {
334         int i;
335
336         has_ext_instr_set = ! has_no_x_instr_set();
337
338         for (i = 0; i < 256; i++)
339                 stackreq[i] = 1;
340
341         stackreq[JAVA_NOP]          = 0;
342         stackreq[JAVA_ISTORE]       = 0;
343         stackreq[JAVA_LSTORE]       = 0;
344         stackreq[JAVA_FSTORE]       = 0;
345         stackreq[JAVA_DSTORE]       = 0;
346         stackreq[JAVA_ASTORE]       = 0;
347         stackreq[JAVA_ISTORE_0]     = 0;
348         stackreq[JAVA_ISTORE_1]     = 0;
349         stackreq[JAVA_ISTORE_2]     = 0;
350         stackreq[JAVA_ISTORE_3]     = 0;
351         stackreq[JAVA_LSTORE_0]     = 0;
352         stackreq[JAVA_LSTORE_1]     = 0;
353         stackreq[JAVA_LSTORE_2]     = 0;
354         stackreq[JAVA_LSTORE_3]     = 0;
355         stackreq[JAVA_FSTORE_0]     = 0;
356         stackreq[JAVA_FSTORE_1]     = 0;
357         stackreq[JAVA_FSTORE_2]     = 0;
358         stackreq[JAVA_FSTORE_3]     = 0;
359         stackreq[JAVA_DSTORE_0]     = 0;
360         stackreq[JAVA_DSTORE_1]     = 0;
361         stackreq[JAVA_DSTORE_2]     = 0;
362         stackreq[JAVA_DSTORE_3]     = 0;
363         stackreq[JAVA_ASTORE_0]     = 0;
364         stackreq[JAVA_ASTORE_1]     = 0;
365         stackreq[JAVA_ASTORE_2]     = 0;
366         stackreq[JAVA_ASTORE_3]     = 0;
367         stackreq[JAVA_IASTORE]      = 0;
368         stackreq[JAVA_LASTORE]      = 0;
369         stackreq[JAVA_FASTORE]      = 0;
370         stackreq[JAVA_DASTORE]      = 0;
371         stackreq[JAVA_AASTORE]      = 0;
372         stackreq[JAVA_BASTORE]      = 0;
373         stackreq[JAVA_CASTORE]      = 0;
374         stackreq[JAVA_SASTORE]      = 0;
375         stackreq[JAVA_POP]          = 0;
376         stackreq[JAVA_POP2]         = 0;
377         stackreq[JAVA_IINC]         = 0;
378         stackreq[JAVA_IFEQ]         = 0;
379         stackreq[JAVA_IFNE]         = 0;
380         stackreq[JAVA_IFLT]         = 0;
381         stackreq[JAVA_IFGE]         = 0;
382         stackreq[JAVA_IFGT]         = 0;
383         stackreq[JAVA_IFLE]         = 0;
384         stackreq[JAVA_IF_ICMPEQ]    = 0;
385         stackreq[JAVA_IF_ICMPNE]    = 0;
386         stackreq[JAVA_IF_ICMPLT]    = 0;
387         stackreq[JAVA_IF_ICMPGE]    = 0;
388         stackreq[JAVA_IF_ICMPGT]    = 0;
389         stackreq[JAVA_IF_ICMPLE]    = 0;
390         stackreq[JAVA_IF_ACMPEQ]    = 0;
391         stackreq[JAVA_IF_ACMPNE]    = 0;
392         stackreq[JAVA_GOTO]         = 0;
393         stackreq[JAVA_RET]          = 0;
394         stackreq[JAVA_TABLESWITCH]  = 0;
395         stackreq[ICMD_LOOKUPSWITCH] = 0;
396         stackreq[JAVA_IRETURN]      = 0;
397         stackreq[JAVA_LRETURN]      = 0;
398         stackreq[JAVA_FRETURN]      = 0;
399         stackreq[JAVA_DRETURN]      = 0;
400         stackreq[JAVA_ARETURN]      = 0;
401         stackreq[JAVA_RETURN]       = 0;
402         stackreq[JAVA_PUTSTATIC]    = 0;
403         stackreq[JAVA_PUTFIELD]     = 0;
404         stackreq[JAVA_MONITORENTER] = 0;
405         stackreq[ICMD_MONITOREXIT]  = 0;
406         stackreq[JAVA_WIDE]         = 0;
407         stackreq[JAVA_IFNULL]       = 0;
408         stackreq[JAVA_IFNONNULL]    = 0;
409         stackreq[JAVA_GOTO_W]       = 0;
410         stackreq[JAVA_BREAKPOINT]   = 0;
411
412         stackreq[JAVA_SWAP] = 2;
413         stackreq[JAVA_DUP2] = 2;
414         stackreq[JAVA_DUP_X1] = 3;
415         stackreq[JAVA_DUP_X2] = 4;
416         stackreq[JAVA_DUP2_X1] = 3;
417         stackreq[JAVA_DUP2_X2] = 4;
418         
419         for (i = 0; i < 256; i++) stdopdescriptors[i] = NULL;
420
421         for (i = 0; i < sizeof(stdopdescriptortable)/sizeof(stdopdescriptor); i++) {
422                 
423                 if (stdopdescriptortable[i].isfloat && checkfloats) {
424                         stdopdescriptortable[i].supported = false;
425                         }
426
427                 stdopdescriptors[stdopdescriptortable[i].opcode] = 
428                    &(stdopdescriptortable[i]);
429                 }
430
431         init_exceptions();
432 }
433
434
435 void ncomp_close()
436 {
437 /*      mcode_close(); */
438 }
439