* src/vm/resolve.c (new_resolve_field_lazy): New method. This will
[cacao.git] / src / vm / jit / parse.c
1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
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    Author: Andreas Krall
28
29    Changes: Carolyn Oates
30             Edwin Steiner
31             Joseph Wenninger
32             Christian Thalinger
33
34    $Id: parse.c 5008 2006-06-01 16:00:18Z edwin $
35
36 */
37
38
39 #include "config.h"
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "vm/types.h"
45
46 #include "mm/memory.h"
47 #include "native/native.h"
48 #include "toolbox/logging.h"
49 #include "vm/builtin.h"
50 #include "vm/exceptions.h"
51 #include "vm/global.h"
52 #include "vm/linker.h"
53 #include "vm/loader.h"
54 #include "vm/resolve.h"
55 #include "vm/options.h"
56 #include "vm/statistics.h"
57 #include "vm/stringlocal.h"
58 #include "vm/jit/asmpart.h"
59 #include "vm/jit/jit.h"
60 #include "vm/jit/parse.h"
61 #include "vm/jit/patcher.h"
62 #include "vm/jit/loop/loop.h"
63
64 /*******************************************************************************
65
66         function 'parse' scans the JavaVM code and generates intermediate code
67
68         During parsing the block index table is used to store at bit pos 0
69         a flag which marks basic block starts and at position 1 to 31 the
70         intermediate instruction index. After parsing the block index table
71         is scanned, for marked positions a block is generated and the block
72         number is stored in the block index table.
73
74 *******************************************************************************/
75
76 static exceptiontable * fillextable(methodinfo *m, 
77                                                                         exceptiontable *extable, 
78                                                                         exceptiontable *raw_extable, 
79                                                                 int exceptiontablelength, 
80                                                                         int *block_count)
81 {
82         int b_count, p, src;
83         
84         if (exceptiontablelength == 0) 
85                 return extable;
86
87         b_count = *block_count;
88
89         for (src = exceptiontablelength-1; src >=0; src--) {
90                 /* the start of the handled region becomes a basic block start */
91                 p = raw_extable[src].startpc;
92                 CHECK_BYTECODE_INDEX(p);
93                 extable->startpc = p;
94                 block_insert(p);
95                 
96                 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
97                 CHECK_BYTECODE_INDEX_EXCLUSIVE(p);
98
99 #if defined(ENABLE_VERIFIER)
100                 if (p <= raw_extable[src].startpc) {
101                         *exceptionptr = new_verifyerror(m,
102                                 "Invalid exception handler range");
103                         return NULL;
104                 }
105 #endif
106                 extable->endpc = p;
107                 
108                 /* end of handled region becomes a basic block boundary  */
109                 /* (If it is the bytecode end, we'll use the special     */
110                 /* end block that is created anyway.)                    */
111                 if (p < m->jcodelength) 
112                         block_insert(p);
113
114                 /* the start of the handler becomes a basic block start  */
115                 p = raw_extable[src].handlerpc;
116                 CHECK_BYTECODE_INDEX(p);
117                 extable->handlerpc = p;
118                 block_insert(p);
119
120                 extable->catchtype = raw_extable[src].catchtype;
121                 extable->next = NULL;
122                 extable->down = &extable[1];
123                 extable--;
124         }
125
126         *block_count = b_count;
127         
128         /* everything ok */
129         return extable;
130
131 #if defined(ENABLE_VERIFIER)
132 throw_invalid_bytecode_index:
133         *exceptionptr =
134                 new_verifyerror(m, "Illegal bytecode index in exception table");
135         return NULL;
136 #endif
137 }
138
139 /*** macro for checking the length of the bytecode ***/
140
141 #if defined(ENABLE_VERIFIER)
142 #define CHECK_END_OF_BYTECODE(neededlength) \
143         do { \
144                 if ((neededlength) > m->jcodelength) \
145                         goto throw_unexpected_end_of_bytecode; \
146         } while (0)
147 #else /* !ENABLE_VERIFIER */
148 #define CHECK_END_OF_BYTECODE(neededlength)
149 #endif /* ENABLE_VERIFIER */
150
151 bool new_parse(jitdata *jd)
152 {
153         methodinfo  *m;             /* method being parsed                      */
154         codegendata *cd;
155         int  p;                     /* java instruction counter                 */
156         int  nextp;                 /* start of next java instruction           */
157         int  opcode;                /* java opcode                              */
158         int  i;                     /* temporary for different uses (ctrs)      */
159         int  ipc = 0;               /* intermediate instruction counter         */
160         int  b_count = 0;           /* basic block counter                      */
161         int  s_count = 0;           /* stack element counter                    */
162         bool blockend = false;      /* true if basic block end has been reached */
163         bool iswide = false;        /* true if last instruction was a wide      */
164         new_instruction *iptr;      /* current ptr into instruction array       */
165         u1 *instructionstart;       /* 1 for pcs which are valid instr. starts  */
166         constant_classref  *cr;
167         constant_classref  *compr;
168         classinfo          *c;
169         builtintable_entry *bte;
170         constant_FMIref    *mr;
171         methoddesc         *md;
172         unresolved_method  *um;
173         resolve_result_t    result;
174         u2                  lineindex = 0;
175         u2                  currentline = 0;
176         u2                  linepcchange = 0;
177         u4                  flags;
178
179         /* get required compiler data */
180
181         m  = jd->m;
182         cd = jd->cd;
183
184         /* allocate instruction array and block index table */
185
186         /* 1 additional for end ipc  */
187         m->basicblockindex = DMNEW(s4, m->jcodelength + 1);
188         memset(m->basicblockindex, 0, sizeof(s4) * (m->jcodelength + 1));
189
190         instructionstart = DMNEW(u1, m->jcodelength + 1);
191         memset(instructionstart, 0, sizeof(u1) * (m->jcodelength + 1));
192
193         /* IMPORTANT: We assume that parsing creates at most one instruction per */
194         /*            byte of original bytecode!                                 */
195         /* XXX add an assertion checking this at the end                         */
196
197         iptr = jd->new_instructions = DMNEW(new_instruction, m->jcodelength);
198
199         /* compute branch targets of exception table */
200
201         if (!fillextable(m,
202                         &(cd->exceptiontable[cd->exceptiontablelength-1]),
203                         m->exceptiontable,
204                         m->exceptiontablelength,
205                         &b_count))
206         {
207                 return false;
208         }
209
210         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
211
212 #if defined(ENABLE_THREADS)
213         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
214                 m->isleafmethod = false;
215         }
216 #endif
217
218         /* scan all java instructions */
219         currentline = 0;
220         linepcchange = 0;
221
222         if (m->linenumbercount == 0) {
223                 lineindex = 0;
224         }
225         else {
226                 linepcchange = m->linenumbers[0].start_pc;
227         }
228
229         /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
230
231         for (p = 0; p < m->jcodelength; p = nextp) {
232
233                 /* mark this position as a valid instruction start */
234
235                 instructionstart[p] = 1;
236
237                 /* change the current line number, if necessary */
238
239                 /* XXX rewrite this using pointer arithmetic */
240
241                 if (linepcchange == p) {
242                         if (m->linenumbercount > lineindex) {
243 next_linenumber:
244                                 currentline = m->linenumbers[lineindex].line_number;
245                                 lineindex++;
246                                 if (lineindex < m->linenumbercount) {
247                                         linepcchange = m->linenumbers[lineindex].start_pc;
248                                         if (linepcchange == p)
249                                                 goto next_linenumber;
250                                 }
251                         }
252                 }
253
254                 /* fetch next opcode  */
255 fetch_opcode:
256                 opcode = code_get_u1(p, m);
257
258                 /* store intermediate instruction count (bit 0 mark block starts) */
259
260                 m->basicblockindex[p] |= (ipc << 1);
261
262                 /* some compilers put a JAVA_NOP after a blockend instruction */
263
264                 if (blockend && (opcode != JAVA_NOP)) {
265                         /* start new block */
266
267                         block_insert(p);
268                         blockend = false;
269                 }
270
271                 /* compute next instruction start */
272
273                 nextp = p + jcommandsize[opcode];
274
275                 CHECK_END_OF_BYTECODE(nextp);
276
277                 /* add stack elements produced by this instruction */
278
279                 s_count += stackreq[opcode];
280
281                 /* translate this bytecode instruction */
282
283                 switch (opcode) {
284
285                 case JAVA_NOP:
286                         break;
287
288                 /* pushing constants onto the stack ***********************************/
289
290                 case JAVA_BIPUSH:
291                         NEW_OP_LOADCONST_I(code_get_s1(p+1,m));
292                         break;
293
294                 case JAVA_SIPUSH:
295                         NEW_OP_LOADCONST_I(code_get_s2(p+1,m));
296                         break;
297
298                 case JAVA_LDC1:
299                         i = code_get_u1(p + 1, m);
300                         goto pushconstantitem;
301
302                 case JAVA_LDC2:
303                 case JAVA_LDC2W:
304                         i = code_get_u2(p + 1, m);
305
306                 pushconstantitem:
307
308 #if defined(ENABLE_VERIFIER)
309                         if (i >= m->class->cpcount) {
310                                 *exceptionptr = new_verifyerror(m,
311                                         "Attempt to access constant outside range");
312                                 return false;
313                         }
314 #endif
315
316                         switch (m->class->cptags[i]) {
317                         case CONSTANT_Integer:
318                                 NEW_OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
319                                 break;
320                         case CONSTANT_Long:
321                                 NEW_OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
322                                 break;
323                         case CONSTANT_Float:
324                                 NEW_OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
325                                 break;
326                         case CONSTANT_Double:
327                                 NEW_OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
328                                 break;
329                         case CONSTANT_String:
330                                 NEW_OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
331                                 break;
332                         case CONSTANT_Class:
333                                 cr = (constant_classref *) (m->class->cpinfos[i]);
334
335                                 if (!resolve_classref(m, cr, resolveLazy, true,
336                                                                           true, &c))
337                                         return false;
338
339                                 /* if not resolved, c == NULL */
340
341                                 NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, 0 /* no extra flags */);
342
343                                 break;
344
345 #if defined(ENABLE_VERIFIER)
346                         default:
347                                 *exceptionptr = new_verifyerror(m,
348                                                 "Invalid constant type to push");
349                                 return false;
350 #endif
351                         }
352                         break;
353
354                 case JAVA_ACONST_NULL:
355                         NEW_OP_LOADCONST_NULL();
356                         break;
357
358                 case JAVA_ICONST_M1:
359                 case JAVA_ICONST_0:
360                 case JAVA_ICONST_1:
361                 case JAVA_ICONST_2:
362                 case JAVA_ICONST_3:
363                 case JAVA_ICONST_4:
364                 case JAVA_ICONST_5:
365                         NEW_OP_LOADCONST_I(opcode - JAVA_ICONST_0);
366                         break;
367
368                 case JAVA_LCONST_0:
369                 case JAVA_LCONST_1:
370                         NEW_OP_LOADCONST_L(opcode - JAVA_LCONST_0);
371                         break;
372
373                 case JAVA_FCONST_0:
374                 case JAVA_FCONST_1:
375                 case JAVA_FCONST_2:
376                         NEW_OP_LOADCONST_F(opcode - JAVA_FCONST_0);
377                         break;
378
379                 case JAVA_DCONST_0:
380                 case JAVA_DCONST_1:
381                         NEW_OP_LOADCONST_D(opcode - JAVA_DCONST_0);
382                         break;
383
384                 /* local variable access instructions *********************************/
385
386                 case JAVA_ILOAD:
387                 case JAVA_FLOAD:
388                 case JAVA_ALOAD:
389                         if (!iswide) {
390                                 i = code_get_u1(p + 1,m);
391                         }
392                         else {
393                                 i = code_get_u2(p + 1,m);
394                                 nextp = p + 3;
395                                 iswide = false;
396                         }
397                         NEW_OP_LOAD_ONEWORD(opcode, i);
398                         break;
399
400                 case JAVA_LLOAD:
401                 case JAVA_DLOAD:
402                         if (!iswide) {
403                                 i = code_get_u1(p + 1,m);
404                         }
405                         else {
406                                 i = code_get_u2(p + 1,m);
407                                 nextp = p + 3;
408                                 iswide = false;
409                         }
410                         NEW_OP_LOAD_TWOWORD(opcode, i);
411                         break;
412
413                 case JAVA_ILOAD_0:
414                 case JAVA_ILOAD_1:
415                 case JAVA_ILOAD_2:
416                 case JAVA_ILOAD_3:
417                         NEW_OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
418                         break;
419
420                 case JAVA_LLOAD_0:
421                 case JAVA_LLOAD_1:
422                 case JAVA_LLOAD_2:
423                 case JAVA_LLOAD_3:
424                         NEW_OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
425                         break;
426
427                 case JAVA_FLOAD_0:
428                 case JAVA_FLOAD_1:
429                 case JAVA_FLOAD_2:
430                 case JAVA_FLOAD_3:
431                         NEW_OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
432                         break;
433
434                 case JAVA_DLOAD_0:
435                 case JAVA_DLOAD_1:
436                 case JAVA_DLOAD_2:
437                 case JAVA_DLOAD_3:
438                         NEW_OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
439                         break;
440
441                 case JAVA_ALOAD_0:
442                 case JAVA_ALOAD_1:
443                 case JAVA_ALOAD_2:
444                 case JAVA_ALOAD_3:
445                         NEW_OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
446                         break;
447
448                 case JAVA_ISTORE:
449                 case JAVA_FSTORE:
450                 case JAVA_ASTORE:
451                         if (!iswide) {
452                                 i = code_get_u1(p + 1,m);
453                         }
454                         else {
455                                 i = code_get_u2(p + 1,m);
456                                 iswide = false;
457                                 nextp = p + 3;
458                         }
459                         NEW_OP_STORE_ONEWORD(opcode, i);
460                         break;
461
462                 case JAVA_LSTORE:
463                 case JAVA_DSTORE:
464                         if (!iswide) {
465                                 i = code_get_u1(p + 1,m);
466                         }
467                         else {
468                                 i = code_get_u2(p + 1,m);
469                                 iswide = false;
470                                 nextp = p + 3;
471                         }
472                         NEW_OP_STORE_TWOWORD(opcode, i);
473                         break;
474
475                 case JAVA_ISTORE_0:
476                 case JAVA_ISTORE_1:
477                 case JAVA_ISTORE_2:
478                 case JAVA_ISTORE_3:
479                         NEW_OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
480                         break;
481
482                 case JAVA_LSTORE_0:
483                 case JAVA_LSTORE_1:
484                 case JAVA_LSTORE_2:
485                 case JAVA_LSTORE_3:
486                         NEW_OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
487                         break;
488
489                 case JAVA_FSTORE_0:
490                 case JAVA_FSTORE_1:
491                 case JAVA_FSTORE_2:
492                 case JAVA_FSTORE_3:
493                         NEW_OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
494                         break;
495
496                 case JAVA_DSTORE_0:
497                 case JAVA_DSTORE_1:
498                 case JAVA_DSTORE_2:
499                 case JAVA_DSTORE_3:
500                         NEW_OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
501                         break;
502
503                 case JAVA_ASTORE_0:
504                 case JAVA_ASTORE_1:
505                 case JAVA_ASTORE_2:
506                 case JAVA_ASTORE_3:
507                         NEW_OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
508                         break;
509
510                 case JAVA_IINC:
511                         {
512                                 int v;
513
514                                 if (!iswide) {
515                                         i = code_get_u1(p + 1,m);
516                                         v = code_get_s1(p + 2,m);
517
518                                 }
519                                 else {
520                                         i = code_get_u2(p + 1,m);
521                                         v = code_get_s2(p + 3,m);
522                                         iswide = false;
523                                         nextp = p + 5;
524                                 }
525                                 INDEX_ONEWORD(i);
526                                 NEW_OP_LOCALINDEX_I(opcode, i, v);
527                         }
528                         break;
529
530                 /* wider index for loading, storing and incrementing ******************/
531
532                 case JAVA_WIDE:
533                         iswide = true;
534                         p++;
535                         goto fetch_opcode;
536
537                 /* managing arrays ****************************************************/
538
539                 case JAVA_NEWARRAY:
540                         switch (code_get_s1(p + 1, m)) {
541                         case 4:
542                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
543                                 break;
544                         case 5:
545                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
546                                 break;
547                         case 6:
548                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
549                                 break;
550                         case 7:
551                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
552                                 break;
553                         case 8:
554                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
555                                 break;
556                         case 9:
557                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
558                                 break;
559                         case 10:
560                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
561                                 break;
562                         case 11:
563                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
564                                 break;
565 #if defined(ENABLE_VERIFIER)
566                         default:
567                                 *exceptionptr = new_verifyerror(m,
568                                                 "Invalid array-type to create");
569                                 return false;
570 #endif
571                         }
572                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
573                         break;
574
575                 case JAVA_ANEWARRAY:
576                         i = code_get_u2(p + 1, m);
577                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
578                         if (!compr)
579                                 return false;
580
581                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
582                                 return false;
583
584                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
585                                 return false;
586
587                         NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
588                         bte = builtintable_get_internal(BUILTIN_newarray);
589                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
590                         s_count++;
591                         break;
592
593                 case JAVA_MULTIANEWARRAY:
594                         m->isleafmethod = false;
595                         i = code_get_u2(p + 1, m);
596                         {
597                                 s4 v = code_get_u1(p + 3, m);
598
599                                 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
600                                 if (!cr)
601                                         return false;
602
603                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
604                                         return false;
605
606                                 /* if unresolved, c == NULL */
607
608                                 iptr->s1.argcount = v;
609                                 NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
610                         }
611                         break;
612
613                 /* control flow instructions ******************************************/
614
615                 case JAVA_IFEQ:
616                 case JAVA_IFLT:
617                 case JAVA_IFLE:
618                 case JAVA_IFNE:
619                 case JAVA_IFGT:
620                 case JAVA_IFGE:
621                 case JAVA_IFNULL:
622                 case JAVA_IFNONNULL:
623                 case JAVA_IF_ICMPEQ:
624                 case JAVA_IF_ICMPNE:
625                 case JAVA_IF_ICMPLT:
626                 case JAVA_IF_ICMPGT:
627                 case JAVA_IF_ICMPLE:
628                 case JAVA_IF_ICMPGE:
629                 case JAVA_IF_ACMPEQ:
630                 case JAVA_IF_ACMPNE:
631                 case JAVA_GOTO:
632                 case JAVA_JSR:
633                         i = p + code_get_s2(p + 1,m);
634                         CHECK_BYTECODE_INDEX(i);
635                         block_insert(i);
636                         blockend = true;
637                         NEW_OP_INSINDEX(opcode, i);
638                         break;
639
640                 case JAVA_GOTO_W:
641                 case JAVA_JSR_W:
642                         i = p + code_get_s4(p + 1,m);
643                         CHECK_BYTECODE_INDEX(i);
644                         block_insert(i);
645                         blockend = true;
646                         NEW_OP_INSINDEX(opcode, i);
647                         break;
648
649                 case JAVA_RET:
650                         if (!iswide) {
651                                 i = code_get_u1(p + 1,m);
652                         }
653                         else {
654                                 i = code_get_u2(p + 1,m);
655                                 nextp = p + 3;
656                                 iswide = false;
657                         }
658                         blockend = true;
659
660                         NEW_OP_LOAD_ONEWORD(opcode, i);
661                         break;
662
663                 case JAVA_IRETURN:
664                 case JAVA_LRETURN:
665                 case JAVA_FRETURN:
666                 case JAVA_DRETURN:
667                 case JAVA_ARETURN:
668                 case JAVA_RETURN:
669                         blockend = true;
670                         /* XXX ARETURN will need a flag in the typechecker */
671                         NEW_OP(opcode);
672                         break;
673
674                 case JAVA_ATHROW:
675                         blockend = true;
676                         /* XXX ATHROW will need a flag in the typechecker */
677                         NEW_OP(opcode);
678                         break;
679
680
681                 /* table jumps ********************************************************/
682
683                 case JAVA_LOOKUPSWITCH:
684                         {
685                                 s4 num, j;
686                                 lookup_target_t *lookup;
687 #if defined(ENABLE_VERIFIER)
688                                 s4 prevvalue = 0;
689 #endif
690                                 blockend = true;
691                                 nextp = ALIGN((p + 1), 4);
692
693                                 CHECK_END_OF_BYTECODE(nextp + 8);
694
695                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
696
697                                 /* default target */
698
699                                 j =  p + code_get_s4(nextp, m);
700                                 iptr->sx.s23.s3.lookupdefault.insindex = j;
701                                 nextp += 4;
702                                 CHECK_BYTECODE_INDEX(j);
703                                 block_insert(j);
704
705                                 /* number of pairs */
706
707                                 num = code_get_u4(nextp, m);
708                                 iptr->sx.s23.s2.lookupcount = num;
709                                 nextp += 4;
710
711                                 /* allocate the intermediate code table */
712
713                                 lookup = DMNEW(lookup_target_t, num);
714                                 iptr->dst.lookup = lookup;
715
716                                 /* iterate over the lookup table */
717
718                                 CHECK_END_OF_BYTECODE(nextp + 8 * num);
719
720                                 for (i = 0; i < num; i++) {
721                                         /* value */
722
723                                         j = code_get_s4(nextp, m);
724                                         lookup->value = j;
725
726                                         nextp += 4;
727
728 #if defined(ENABLE_VERIFIER)
729                                         /* check if the lookup table is sorted correctly */
730
731                                         if (i && (j <= prevvalue)) {
732                                                 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
733                                                 return false;
734                                         }
735                                         prevvalue = j;
736 #endif
737                                         /* target */
738
739                                         j = p + code_get_s4(nextp,m);
740                                         lookup->target.insindex = j;
741                                         lookup++;
742                                         nextp += 4;
743                                         CHECK_BYTECODE_INDEX(j);
744                                         block_insert(j);
745                                 }
746
747                                 PINC;
748                                 break;
749                         }
750
751
752                 case JAVA_TABLESWITCH:
753                         {
754                                 s4 num, j;
755                                 s4 deftarget;
756                                 branch_target_t *table;
757
758                                 blockend = true;
759                                 nextp = ALIGN((p + 1), 4);
760
761                                 CHECK_END_OF_BYTECODE(nextp + 12);
762
763                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
764
765                                 /* default target */
766
767                                 deftarget = p + code_get_s4(nextp, m);
768                                 nextp += 4;
769                                 CHECK_BYTECODE_INDEX(deftarget);
770                                 block_insert(deftarget);
771
772                                 /* lower bound */
773
774                                 j = code_get_s4(nextp, m);
775                                 iptr->sx.s23.s2.tablelow = j;
776                                 nextp += 4;
777
778                                 /* upper bound */
779
780                                 num = code_get_s4(nextp, m);
781                                 iptr->sx.s23.s3.tablehigh = num;
782                                 nextp += 4;
783
784                                 /* calculate the number of table entries */
785
786                                 num = num - j + 1;
787
788 #if defined(ENABLE_VERIFIER)
789                                 if (num < 1) {
790                                         *exceptionptr = new_verifyerror(m,
791                                                         "invalid TABLESWITCH: upper bound < lower bound");
792                                         return false;
793                                 }
794 #endif
795                                 /* create the intermediate code table */
796                                 /* the first entry is the default target */
797
798                                 table = MNEW(branch_target_t, 1 + num);
799                                 iptr->dst.table = table;
800                                 (table++)->insindex = deftarget;
801
802                                 /* iterate over the target table */
803
804                                 CHECK_END_OF_BYTECODE(nextp + 4 * num);
805
806                                 for (i = 0; i < num; i++) {
807                                         j = p + code_get_s4(nextp,m);
808                                         (table++)->insindex = j;
809                                         nextp += 4;
810                                         CHECK_BYTECODE_INDEX(j);
811                                         block_insert(j);
812                                 }
813
814                                 PINC;
815                                 break;
816                         }
817
818
819                 /* load and store of object fields ************************************/
820
821                 case JAVA_AASTORE:
822                         NEW_OP(opcode);
823                         m->isleafmethod = false;
824                         break;
825
826                 case JAVA_GETSTATIC:
827                 case JAVA_PUTSTATIC:
828                 case JAVA_GETFIELD:
829                 case JAVA_PUTFIELD:
830                         {
831                                 constant_FMIref  *fr;
832                                 unresolved_field *uf;
833
834                                 i = code_get_u2(p + 1, m);
835                                 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
836                                 if (!fr)
837                                         return false;
838
839                                 NEW_OP_PREPARE_ZEROFLAGS(opcode);
840                                 iptr->sx.s23.s3.fmiref = fr;
841
842                                 /* only with -noverify, otherwise the typechecker does this */
843
844 #if defined(ENABLE_VERIFIER)
845                                 if (!opt_verify) {
846 #endif
847                                         result = new_resolve_field_lazy(iptr, NULL, m);
848                                         if (result == resolveFailed)
849                                                 return false;
850
851                                         if (result != resolveSucceeded) {
852                                                 uf = new_create_unresolved_field(m->class, m, iptr);
853
854                                                 if (!uf)
855                                                         return false;
856
857                                                 /* store the unresolved_field pointer */
858
859                                                 iptr->sx.s23.s3.uf = uf;
860                                                 iptr->flags.bits = INS_FLAG_UNRESOLVED;
861                                         }
862 #if defined(ENABLE_VERIFIER)
863                                 }
864 #endif
865                                 PINC;
866                         }
867                         break;
868
869
870                 /* method invocation **************************************************/
871
872                 case JAVA_INVOKESTATIC:
873                         i = code_get_u2(p + 1, m);
874                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
875                         if (!mr)
876                                 return false;
877
878                         md = mr->parseddesc.md;
879
880                         if (!md->params)
881                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
882                                         return false;
883
884                         goto invoke_method;
885
886                 case JAVA_INVOKEINTERFACE:
887                         i = code_get_u2(p + 1, m);
888
889                         mr = class_getconstant(m->class, i,
890                                         CONSTANT_InterfaceMethodref);
891
892                         goto invoke_nonstatic_method;
893
894                 case JAVA_INVOKESPECIAL:
895                 case JAVA_INVOKEVIRTUAL:
896                         i = code_get_u2(p + 1, m);
897                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
898
899 invoke_nonstatic_method:
900                         if (!mr)
901                                 return false;
902
903                         md = mr->parseddesc.md;
904
905                         if (!md->params)
906                                 if (!descriptor_params_from_paramtypes(md, 0))
907                                         return false;
908
909 invoke_method:
910                         m->isleafmethod = false;
911
912                         NEW_OP_PREPARE_ZEROFLAGS(opcode);
913                         iptr->sx.s23.s3.fmiref = mr;
914
915                         /* only with -noverify, otherwise the typechecker does this */
916
917 #if defined(ENABLE_VERIFIER)
918                         if (!opt_verify) {
919 #endif
920                                 result = new_resolve_method_lazy(iptr, NULL, m);
921                                 if (result == resolveFailed)
922                                         return false;
923
924                                 if (result != resolveSucceeded) {
925                                         um = new_create_unresolved_method(m->class, m, iptr);
926
927                                         if (!um)
928                                                 return false;
929
930                                         /* store the unresolved_method pointer */
931
932                                         iptr->sx.s23.s3.um = um;
933                                         iptr->flags.bits = INS_FLAG_UNRESOLVED;
934                                 }
935 #if defined(ENABLE_VERIFIER)
936                         }
937 #endif
938                         PINC;
939                         break;
940
941                 /* instructions taking class arguments ********************************/
942
943                 case JAVA_NEW:
944                         i = code_get_u2(p + 1, m);
945                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
946                         if (!cr)
947                                 return false;
948
949                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
950                                 return false;
951
952                         NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
953                         bte = builtintable_get_internal(BUILTIN_new);
954                         NEW_OP_BUILTIN_CHECK_EXCEPTION(bte);
955                         s_count++;
956                         break;
957
958                 case JAVA_CHECKCAST:
959                         i = code_get_u2(p + 1, m);
960                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
961                         if (!cr)
962                                 return false;
963
964                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
965                                 return false;
966
967                         if (cr->name->text[0] == '[') {
968                                 /* array type cast-check */
969                                 flags = INS_FLAG_ARRAY;
970                                 m->isleafmethod = false;
971                         }
972                         else {
973                                 /* object type cast-check */
974                                 flags = 0;
975                         }
976                         NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
977                         break;
978
979                 case JAVA_INSTANCEOF:
980                         i = code_get_u2(p + 1,m);
981                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
982                         if (!cr)
983                                 return false;
984
985                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
986                                 return false;
987
988                         if (cr->name->text[0] == '[') {
989                                 /* array type cast-check */
990                                 NEW_OP_LOADCONST_CLASSINFO_OR_CLASSREF(c, cr, INS_FLAG_NOCHECK);
991                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
992                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
993                                 s_count++;
994                         }
995                         else {
996                                 /* object type cast-check */
997                                 NEW_OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
998                         }
999                         break;
1000
1001                 /* synchronization instructions ***************************************/
1002
1003                 case JAVA_MONITORENTER:
1004 #if defined(ENABLE_THREADS)
1005                         if (checksync) {
1006                                 /* XXX null check */
1007                                 bte = builtintable_get_internal(BUILTIN_monitorenter);
1008                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1009                         }
1010                         else
1011 #endif
1012                         {
1013                                 NEW_OP(ICMD_CHECKNULL_POP);
1014                         }
1015                         break;
1016
1017                 case JAVA_MONITOREXIT:
1018 #if defined(ENABLE_THREADS)
1019                         if (checksync) {
1020                                 /* XXX null check */
1021                                 bte = builtintable_get_internal(BUILTIN_monitorexit);
1022                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1023                         }
1024                         else
1025 #endif
1026                         {
1027                                 NEW_OP(ICMD_CHECKNULL_POP);
1028                         }
1029                         break;
1030
1031                 /* arithmetic instructions they may become builtin functions **********/
1032
1033                 case JAVA_IDIV:
1034 #if !SUPPORT_DIVISION
1035                         bte = builtintable_get_internal(BUILTIN_idiv);
1036                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1037 #else
1038                         OP(opcode);
1039 #endif
1040                         break;
1041
1042                 case JAVA_IREM:
1043 #if !SUPPORT_DIVISION
1044                         bte = builtintable_get_internal(BUILTIN_irem);
1045                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1046 #else
1047                         OP(opcode);
1048 #endif
1049                         break;
1050
1051                 case JAVA_LDIV:
1052 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1053                         bte = builtintable_get_internal(BUILTIN_ldiv);
1054                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1055 #else
1056                         OP(opcode);
1057 #endif
1058                         break;
1059
1060                 case JAVA_LREM:
1061 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1062                         bte = builtintable_get_internal(BUILTIN_lrem);
1063                         NEW_OP_BUILTIN_ARITHMETIC(opcode, bte);
1064 #else
1065                         OP(opcode);
1066 #endif
1067                         break;
1068
1069                 case JAVA_FREM:
1070 #if defined(__I386__)
1071                         NEW_OP(opcode);
1072 #else
1073                         bte = builtintable_get_internal(BUILTIN_frem);
1074                         NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1075 #endif
1076                         break;
1077
1078                 case JAVA_DREM:
1079 #if defined(__I386__)
1080                         NEW_OP(opcode);
1081 #else
1082                         bte = builtintable_get_internal(BUILTIN_drem);
1083                         NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1084 #endif
1085                         break;
1086
1087                 case JAVA_F2I:
1088 #if defined(__ALPHA__)
1089                         if (!opt_noieee) {
1090                                 bte = builtintable_get_internal(BUILTIN_f2i);
1091                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1092                         }
1093                         else
1094 #endif
1095                         {
1096                                 NEW_OP(opcode);
1097                         }
1098                         break;
1099
1100                 case JAVA_F2L:
1101 #if defined(__ALPHA__)
1102                         if (!opt_noieee) {
1103                                 bte = builtintable_get_internal(BUILTIN_f2l);
1104                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1105                         }
1106                         else
1107 #endif
1108                         {
1109                                 NEW_OP(opcode);
1110                         }
1111                         break;
1112
1113                 case JAVA_D2I:
1114 #if defined(__ALPHA__)
1115                         if (!opt_noieee) {
1116                                 bte = builtintable_get_internal(BUILTIN_d2i);
1117                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1118                         }
1119                         else
1120 #endif
1121                         {
1122                                 NEW_OP(opcode);
1123                         }
1124                         break;
1125
1126                 case JAVA_D2L:
1127 #if defined(__ALPHA__)
1128                         if (!opt_noieee) {
1129                                 bte = builtintable_get_internal(BUILTIN_d2l);
1130                                 NEW_OP_BUILTIN_NO_EXCEPTION(bte);
1131                         }
1132                         else
1133 #endif
1134                         {
1135                                 NEW_OP(opcode);
1136                         }
1137                         break;
1138
1139                 /* invalid opcodes ****************************************************/
1140
1141                         /* check for invalid opcodes if the verifier is enabled */
1142 #if defined(ENABLE_VERIFIER)
1143                 case JAVA_BREAKPOINT:
1144                         *exceptionptr =
1145                                 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
1146                         return false;
1147
1148                 case 186: /* unused opcode */
1149                 case 203:
1150                 case 204:
1151                 case 205:
1152                 case 206:
1153                 case 207:
1154                 case 208:
1155                 case 209:
1156                 case 210:
1157                 case 211:
1158                 case 212:
1159                 case 213:
1160                 case 214:
1161                 case 215:
1162                 case 216:
1163                 case 217:
1164                 case 218:
1165                 case 219:
1166                 case 220:
1167                 case 221:
1168                 case 222:
1169                 case 223:
1170                 case 224:
1171                 case 225:
1172                 case 226:
1173                 case 227:
1174                 case 228:
1175                 case 229:
1176                 case 230:
1177                 case 231:
1178                 case 232:
1179                 case 233:
1180                 case 234:
1181                 case 235:
1182                 case 236:
1183                 case 237:
1184                 case 238:
1185                 case 239:
1186                 case 240:
1187                 case 241:
1188                 case 242:
1189                 case 243:
1190                 case 244:
1191                 case 245:
1192                 case 246:
1193                 case 247:
1194                 case 248:
1195                 case 249:
1196                 case 250:
1197                 case 251:
1198                 case 252:
1199                 case 253:
1200                 case 254:
1201                 case 255:
1202                         *exceptionptr =
1203                                 new_verifyerror(m,"Illegal opcode %d at instr %d\n",
1204                                                                   opcode, ipc);
1205                         return false;
1206                         break;
1207 #endif /* defined(ENABLE_VERIFIER) */
1208
1209                 /* opcodes that don't require translation *****************************/
1210
1211                 default:
1212                         /* straight-forward translation to ICMD */
1213                         NEW_OP(opcode);
1214                         break;
1215
1216                 } /* end switch */
1217
1218 #if defined(ENABLE_VERIFIER)
1219                 /* If WIDE was used correctly, iswide should have been reset by now. */
1220                 if (iswide) {
1221                         *exceptionptr = new_verifyerror(m,
1222                                         "Illegal instruction: WIDE before incompatible opcode");
1223                         return false;
1224                 }
1225 #endif /* defined(ENABLE_VERIFIER) */
1226
1227         } /* end for */
1228
1229         /*** END OF LOOP **********************************************************/
1230
1231 #if defined(ENABLE_VERIFIER)
1232         if (p != m->jcodelength) {
1233                 *exceptionptr = new_verifyerror(m,
1234                                 "Command-sequence crosses code-boundary");
1235                 return false;
1236         }
1237
1238         if (!blockend) {
1239                 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
1240                 return false;
1241         }
1242 #endif /* defined(ENABLE_VERIFIER) */
1243
1244         /* adjust block count if target 0 is not first intermediate instruction */
1245
1246         if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
1247                 b_count++;
1248
1249         /* copy local to method variables */
1250
1251         m->instructioncount = ipc;
1252         m->basicblockcount = b_count;
1253         m->stackcount = s_count + m->basicblockcount * m->maxstack;
1254
1255         /* allocate stack table */
1256
1257         m->stack = DMNEW(stackelement, m->stackcount);
1258
1259         {
1260                 basicblock *bptr;
1261
1262                 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
1263
1264                 b_count = 0;
1265                 m->c_debug_nr = 0;
1266
1267                 /* additional block if target 0 is not first intermediate instruction */
1268
1269                 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
1270                         BASICBLOCK_INIT(bptr,m);
1271
1272                         bptr->iinstr = m->instructions;
1273                         /* bptr->icount is set when the next block is allocated */
1274
1275                         bptr++;
1276                         b_count++;
1277                         bptr[-1].next = bptr;
1278                 }
1279
1280                 /* allocate blocks */
1281
1282                 for (p = 0; p < m->jcodelength; p++) {
1283                         if (m->basicblockindex[p] & 1) {
1284                                 /* Check if this block starts at the beginning of an          */
1285                                 /* instruction.                                               */
1286 #if defined(ENABLE_VERIFIER)
1287                                 if (!instructionstart[p]) {
1288                                         *exceptionptr = new_verifyerror(m,
1289                                                 "Branch into middle of instruction");
1290                                         return false;
1291                                 }
1292 #endif
1293
1294                                 /* allocate the block */
1295
1296                                 BASICBLOCK_INIT(bptr,m);
1297
1298                                 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
1299                                 if (b_count) {
1300                                         bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1301                                 }
1302                                 /* bptr->icount is set when the next block is allocated */
1303
1304                                 m->basicblockindex[p] = b_count;
1305
1306                                 bptr++;
1307                                 b_count++;
1308                                 bptr[-1].next = bptr;
1309                         }
1310                 }
1311
1312                 /* set instruction count of last real block */
1313
1314                 if (b_count) {
1315                         bptr[-1].icount = (m->instructions + m->instructioncount) - bptr[-1].iinstr;
1316                 }
1317
1318                 /* allocate additional block at end */
1319
1320                 BASICBLOCK_INIT(bptr,m);
1321
1322                 bptr->instack = bptr->outstack = NULL;
1323                 bptr->indepth = bptr->outdepth = 0;
1324                 bptr->iinstr = NULL;
1325                 bptr->icount = 0;
1326                 bptr->next = NULL;
1327
1328                 /* set basicblock pointers in exception table */
1329
1330                 if (cd->exceptiontablelength > 0) {
1331                         cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1332                 }
1333
1334                 for (i = 0; i < cd->exceptiontablelength; ++i) {
1335                         p = cd->exceptiontable[i].startpc;
1336                         cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1337
1338                         p = cd->exceptiontable[i].endpc;
1339                         cd->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
1340
1341                         p = cd->exceptiontable[i].handlerpc;
1342                         cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1343             }
1344
1345                 /* XXX activate this if you want to try inlining */
1346 #if 0
1347                 for (i = 0; i < m->exceptiontablelength; ++i) {
1348                         p = m->exceptiontable[i].startpc;
1349                         m->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
1350
1351                         p = m->exceptiontable[i].endpc;
1352                         m->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
1353
1354                         p = m->exceptiontable[i].handlerpc;
1355                         m->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
1356             }
1357 #endif
1358
1359         }
1360
1361         /* everything's ok */
1362
1363         return true;
1364
1365 #if defined(ENABLE_VERIFIER)
1366
1367 throw_unexpected_end_of_bytecode:
1368         *exceptionptr = new_verifyerror(m, "Unexpected end of bytecode");
1369         return false;
1370
1371 throw_invalid_bytecode_index:
1372         *exceptionptr =
1373                 new_verifyerror(m, "Illegal target of branch instruction");
1374         return false;
1375
1376 throw_illegal_local_variable_number:
1377         *exceptionptr =
1378                 new_verifyerror(m, "Illegal local variable number");
1379         return false;
1380
1381 #endif /* ENABLE_VERIFIER */
1382 }
1383
1384
1385 bool parse(jitdata *jd)
1386 {
1387         methodinfo  *m;
1388         codegendata *cd;
1389         int  p;                     /* java instruction counter           */
1390         int  nextp;                 /* start of next java instruction     */
1391         int  opcode;                /* java opcode                        */
1392         int  i;                     /* temporary for different uses (ctrs)*/
1393         int  ipc = 0;               /* intermediate instruction counter   */
1394         int  b_count = 0;           /* basic block counter                */
1395         int  s_count = 0;           /* stack element counter              */
1396         bool blockend = false;      /* true if basic block end has been reached   */
1397         bool iswide = false;        /* true if last instruction was a wide*/
1398         instruction *iptr;          /* current ptr into instruction array */
1399
1400         u1 *instructionstart;       /* 1 for pcs which are valid instr. starts    */
1401
1402         constant_classref  *cr;
1403         constant_classref  *compr;
1404         classinfo          *c;
1405         builtintable_entry *bte;
1406
1407         constant_FMIref   *mr;
1408         methoddesc        *md;
1409         unresolved_method *um;
1410         resolve_result_t   result;
1411
1412         u2 lineindex = 0;
1413         u2 currentline = 0;
1414         u2 linepcchange = 0;
1415
1416         /* get required compiler data */
1417
1418         m  = jd->m;
1419         cd = jd->cd;
1420
1421         /* allocate instruction array and block index table */
1422         
1423         /* 1 additional for end ipc  */
1424         m->basicblockindex = DMNEW(s4, m->jcodelength + 1);
1425         memset(m->basicblockindex, 0, sizeof(s4) * (m->jcodelength + 1));
1426
1427         instructionstart = DMNEW(u1, m->jcodelength + 1);
1428         memset(instructionstart, 0, sizeof(u1) * (m->jcodelength + 1));
1429
1430         /* 1 additional for TRACEBUILTIN and 4 for MONITORENTER/EXIT */
1431         /* additional MONITOREXITS are reached by branches which are 3 bytes */
1432         
1433         iptr = m->instructions = DMNEW(instruction, m->jcodelength + 5);
1434
1435         /* Zero the intermediate instructions array so we don't have any
1436          * invalid pointers in it if we cannot finish analyse_stack(). */
1437
1438         memset(iptr, 0, sizeof(instruction) * (m->jcodelength + 5));
1439         
1440         /* compute branch targets of exception table */
1441
1442         if (!fillextable(m, 
1443                         &(cd->exceptiontable[cd->exceptiontablelength-1]), 
1444                         m->exceptiontable, 
1445                         m->exceptiontablelength, 
1446                         &b_count))
1447         {
1448                 return false;
1449         }
1450
1451         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
1452
1453 #if defined(ENABLE_THREADS)
1454         if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
1455                 m->isleafmethod = false;
1456         }                       
1457 #endif
1458
1459         /* scan all java instructions */
1460         currentline = 0;
1461         linepcchange = 0;
1462
1463         if (m->linenumbercount == 0) {
1464                 lineindex = 0;
1465         } 
1466         else {
1467                 linepcchange = m->linenumbers[0].start_pc;
1468         }
1469
1470         for (p = 0; p < m->jcodelength; p = nextp) {
1471           
1472                 /* mark this position as a valid instruction start */
1473                 instructionstart[p] = 1;
1474                 if (linepcchange == p) {
1475                         if (m->linenumbercount > lineindex) {
1476 next_linenumber:
1477                                 currentline = m->linenumbers[lineindex].line_number;
1478                                 lineindex++;
1479                                 if (lineindex < m->linenumbercount) {
1480                                         linepcchange = m->linenumbers[lineindex].start_pc;
1481                                         if (linepcchange == p)
1482                                                 goto next_linenumber;
1483                                 }
1484                         }
1485                 }
1486
1487                 /* fetch next opcode  */
1488 fetch_opcode:
1489                 opcode = code_get_u1(p, m);
1490
1491                 m->basicblockindex[p] |= (ipc << 1); /*store intermed cnt*/
1492
1493                 /* some compilers put a JAVA_NOP after a blockend instruction */
1494
1495                 if (blockend && (opcode != JAVA_NOP)) {
1496                         /* start new block */
1497
1498                         block_insert(p);
1499                         blockend = false;
1500                 }
1501
1502                 nextp = p + jcommandsize[opcode];   /* compute next instruction start */
1503
1504                 CHECK_END_OF_BYTECODE(nextp);
1505
1506                 s_count += stackreq[opcode];            /* compute stack element count    */
1507                 switch (opcode) {
1508                 case JAVA_NOP:
1509                         break;
1510
1511                         /* pushing constants onto the stack p */
1512
1513                 case JAVA_BIPUSH:
1514                         LOADCONST_I(code_get_s1(p+1,m));
1515                         break;
1516
1517                 case JAVA_SIPUSH:
1518                         LOADCONST_I(code_get_s2(p+1,m));
1519                         break;
1520
1521                 case JAVA_LDC1:
1522                         i = code_get_u1(p + 1, m);
1523                         goto pushconstantitem;
1524
1525                 case JAVA_LDC2:
1526                 case JAVA_LDC2W:
1527                         i = code_get_u2(p + 1, m);
1528
1529                 pushconstantitem:
1530
1531 #if defined(ENABLE_VERIFIER)
1532                         if (i >= m->class->cpcount) {
1533                                 *exceptionptr = new_verifyerror(m,
1534                                         "Attempt to access constant outside range");
1535                                 return false;
1536                         }
1537 #endif
1538
1539                         switch (m->class->cptags[i]) {
1540                         case CONSTANT_Integer:
1541                                 LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
1542                                 break;
1543                         case CONSTANT_Long:
1544                                 LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
1545                                 break;
1546                         case CONSTANT_Float:
1547                                 LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
1548                                 break;
1549                         case CONSTANT_Double:
1550                                 LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
1551                                 break;
1552                         case CONSTANT_String:
1553                                 LOADCONST_A(literalstring_new((utf *) (m->class->cpinfos[i])));
1554                                 break;
1555                         case CONSTANT_Class:
1556                                 cr = (constant_classref *) (m->class->cpinfos[i]);
1557
1558                                 if (!resolve_classref(m, cr, resolveLazy, true,
1559                                                                           true, &c))
1560                                         return false;
1561
1562                                 /* if not resolved, c == NULL */
1563
1564                                 if (c) {
1565                                         iptr->target = (void*) 0x02; /* XXX target used temporarily as flag */
1566                                         LOADCONST_A(c);
1567                                 }
1568                                 else {
1569                                         iptr->target = (void*) 0x03; /* XXX target used temporarily as flag */
1570                                         LOADCONST_A(cr);
1571                                 }
1572                                 break;
1573
1574 #if defined(ENABLE_VERIFIER)
1575                         default:
1576                                 *exceptionptr = new_verifyerror(m,
1577                                                 "Invalid constant type to push");
1578                                 return false;
1579 #endif
1580                         }
1581                         break;
1582
1583                 case JAVA_ACONST_NULL:
1584                         LOADCONST_A(NULL);
1585                         break;
1586
1587                 case JAVA_ICONST_M1:
1588                 case JAVA_ICONST_0:
1589                 case JAVA_ICONST_1:
1590                 case JAVA_ICONST_2:
1591                 case JAVA_ICONST_3:
1592                 case JAVA_ICONST_4:
1593                 case JAVA_ICONST_5:
1594                         LOADCONST_I(opcode - JAVA_ICONST_0);
1595                         break;
1596
1597                 case JAVA_LCONST_0:
1598                 case JAVA_LCONST_1:
1599                         LOADCONST_L(opcode - JAVA_LCONST_0);
1600                         break;
1601
1602                 case JAVA_FCONST_0:
1603                 case JAVA_FCONST_1:
1604                 case JAVA_FCONST_2:
1605                         LOADCONST_F(opcode - JAVA_FCONST_0);
1606                         break;
1607
1608                 case JAVA_DCONST_0:
1609                 case JAVA_DCONST_1:
1610                         LOADCONST_D(opcode - JAVA_DCONST_0);
1611                         break;
1612
1613                         /* loading variables onto the stack */
1614
1615                 case JAVA_ILOAD:
1616                 case JAVA_FLOAD:
1617                 case JAVA_ALOAD:
1618                         if (!iswide) {
1619                                 i = code_get_u1(p + 1,m);
1620                         } 
1621                         else {
1622                                 i = code_get_u2(p + 1,m);
1623                                 nextp = p + 3;
1624                                 iswide = false;
1625                         }
1626                         OP1LOAD_ONEWORD(opcode, i);
1627                         break;
1628
1629                 case JAVA_LLOAD:
1630                 case JAVA_DLOAD:
1631                         if (!iswide) {
1632                                 i = code_get_u1(p + 1,m);
1633                         } 
1634                         else {
1635                                 i = code_get_u2(p + 1,m);
1636                                 nextp = p + 3;
1637                                 iswide = false;
1638                         }
1639                         OP1LOAD_TWOWORD(opcode, i);
1640                         break;
1641
1642                 case JAVA_ILOAD_0:
1643                 case JAVA_ILOAD_1:
1644                 case JAVA_ILOAD_2:
1645                 case JAVA_ILOAD_3:
1646                         OP1LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0);
1647                         break;
1648
1649                 case JAVA_LLOAD_0:
1650                 case JAVA_LLOAD_1:
1651                 case JAVA_LLOAD_2:
1652                 case JAVA_LLOAD_3:
1653                         OP1LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0);
1654                         break;
1655
1656                 case JAVA_FLOAD_0:
1657                 case JAVA_FLOAD_1:
1658                 case JAVA_FLOAD_2:
1659                 case JAVA_FLOAD_3:
1660                         OP1LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0);
1661                         break;
1662
1663                 case JAVA_DLOAD_0:
1664                 case JAVA_DLOAD_1:
1665                 case JAVA_DLOAD_2:
1666                 case JAVA_DLOAD_3:
1667                         OP1LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0);
1668                         break;
1669
1670                 case JAVA_ALOAD_0:
1671                 case JAVA_ALOAD_1:
1672                 case JAVA_ALOAD_2:
1673                 case JAVA_ALOAD_3:
1674                         OP1LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0);
1675                         break;
1676
1677                         /* storing stack values into local variables */
1678
1679                 case JAVA_ISTORE:
1680                 case JAVA_FSTORE:
1681                 case JAVA_ASTORE:
1682                         if (!iswide) {
1683                                 i = code_get_u1(p + 1,m);
1684                         } 
1685                         else {
1686                                 i = code_get_u2(p + 1,m);
1687                                 iswide = false;
1688                                 nextp = p + 3;
1689                         }
1690                         OP1STORE_ONEWORD(opcode, i);
1691                         break;
1692
1693                 case JAVA_LSTORE:
1694                 case JAVA_DSTORE:
1695                         if (!iswide) {
1696                                 i = code_get_u1(p + 1,m);
1697                         } 
1698                         else {
1699                                 i = code_get_u2(p + 1,m);
1700                                 iswide = false;
1701                                 nextp = p + 3;
1702                         }
1703                         OP1STORE_TWOWORD(opcode, i);
1704                         break;
1705
1706                 case JAVA_ISTORE_0:
1707                 case JAVA_ISTORE_1:
1708                 case JAVA_ISTORE_2:
1709                 case JAVA_ISTORE_3:
1710                         OP1STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0);
1711                         break;
1712
1713                 case JAVA_LSTORE_0:
1714                 case JAVA_LSTORE_1:
1715                 case JAVA_LSTORE_2:
1716                 case JAVA_LSTORE_3:
1717                         OP1STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0);
1718                         break;
1719
1720                 case JAVA_FSTORE_0:
1721                 case JAVA_FSTORE_1:
1722                 case JAVA_FSTORE_2:
1723                 case JAVA_FSTORE_3:
1724                         OP1STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0);
1725                         break;
1726
1727                 case JAVA_DSTORE_0:
1728                 case JAVA_DSTORE_1:
1729                 case JAVA_DSTORE_2:
1730                 case JAVA_DSTORE_3:
1731                         OP1STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0);
1732                         break;
1733
1734                 case JAVA_ASTORE_0:
1735                 case JAVA_ASTORE_1:
1736                 case JAVA_ASTORE_2:
1737                 case JAVA_ASTORE_3:
1738                         OP1STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0);
1739                         break;
1740
1741                 case JAVA_IINC:
1742                         {
1743                                 int v;
1744                                 
1745                                 if (!iswide) {
1746                                         i = code_get_u1(p + 1,m);
1747                                         v = code_get_s1(p + 2,m);
1748
1749                                 } 
1750                                 else {
1751                                         i = code_get_u2(p + 1,m);
1752                                         v = code_get_s2(p + 3,m);
1753                                         iswide = false;
1754                                         nextp = p + 5;
1755                                 }
1756                                 INDEX_ONEWORD(i);
1757                                 OP2I(opcode, i, v);
1758                         }
1759                         break;
1760
1761                         /* wider index for loading, storing and incrementing */
1762
1763                 case JAVA_WIDE:
1764                         iswide = true;
1765                         p++;
1766                         goto fetch_opcode;
1767
1768                 /* managing arrays ****************************************************/
1769
1770                 case JAVA_NEWARRAY:
1771                         switch (code_get_s1(p + 1, m)) {
1772                         case 4:
1773                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
1774                                 break;
1775                         case 5:
1776                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
1777                                 break;
1778                         case 6:
1779                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
1780                                 break;
1781                         case 7:
1782                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
1783                                 break;
1784                         case 8:
1785                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
1786                                 break;
1787                         case 9:
1788                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
1789                                 break;
1790                         case 10:
1791                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
1792                                 break;
1793                         case 11:
1794                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
1795                                 break;
1796 #if defined(ENABLE_VERIFIER)
1797                         default:
1798                                 *exceptionptr = new_verifyerror(m,
1799                                                 "Invalid array-type to create");
1800                                 return false;
1801 #endif
1802                         }
1803                         BUILTIN(bte, true, NULL, currentline);
1804                         break;
1805
1806                 case JAVA_ANEWARRAY:
1807                         i = code_get_u2(p + 1, m);
1808                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1809                         if (!compr)
1810                                 return false;
1811
1812                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
1813                                 return false;
1814
1815                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1816                                 return false;
1817
1818                         LOADCONST_A_BUILTIN(c, cr);
1819                         bte = builtintable_get_internal(BUILTIN_newarray);
1820                         BUILTIN(bte, true, NULL, currentline);
1821                         s_count++;
1822                         break;
1823
1824                 case JAVA_MULTIANEWARRAY:
1825                         m->isleafmethod = false;
1826                         i = code_get_u2(p + 1, m);
1827                         {
1828                                 s4 v = code_get_u1(p + 3, m);
1829
1830                                 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1831                                 if (!cr)
1832                                         return false;
1833
1834                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1835                                         return false;
1836
1837                                 /* if unresolved, c == NULL */
1838                                 OP2AT(opcode, v, c, cr, currentline);
1839                         }
1840                         break;
1841
1842                 case JAVA_IFEQ:
1843                 case JAVA_IFLT:
1844                 case JAVA_IFLE:
1845                 case JAVA_IFNE:
1846                 case JAVA_IFGT:
1847                 case JAVA_IFGE:
1848                 case JAVA_IFNULL:
1849                 case JAVA_IFNONNULL:
1850                 case JAVA_IF_ICMPEQ:
1851                 case JAVA_IF_ICMPNE:
1852                 case JAVA_IF_ICMPLT:
1853                 case JAVA_IF_ICMPGT:
1854                 case JAVA_IF_ICMPLE:
1855                 case JAVA_IF_ICMPGE:
1856                 case JAVA_IF_ACMPEQ:
1857                 case JAVA_IF_ACMPNE:
1858                 case JAVA_GOTO:
1859                 case JAVA_JSR:
1860                         i = p + code_get_s2(p + 1,m);
1861                         CHECK_BYTECODE_INDEX(i);
1862                         block_insert(i);
1863                         blockend = true;
1864                         OP1(opcode, i);
1865                         break;
1866
1867                 case JAVA_GOTO_W:
1868                 case JAVA_JSR_W:
1869                         i = p + code_get_s4(p + 1,m);
1870                         CHECK_BYTECODE_INDEX(i);
1871                         block_insert(i);
1872                         blockend = true;
1873                         OP1(opcode, i);
1874                         break;
1875
1876                 case JAVA_RET:
1877                         if (!iswide) {
1878                                 i = code_get_u1(p + 1,m);
1879                         } 
1880                         else {
1881                                 i = code_get_u2(p + 1,m);
1882                                 nextp = p + 3;
1883                                 iswide = false;
1884                         }
1885                         blockend = true;
1886                                 
1887                         OP1LOAD_ONEWORD(opcode, i);
1888                         break;
1889
1890                 case JAVA_IRETURN:
1891                 case JAVA_LRETURN:
1892                 case JAVA_FRETURN:
1893                 case JAVA_DRETURN:
1894                 case JAVA_ARETURN:
1895                 case JAVA_RETURN:
1896                         blockend = true;
1897                         /* zero val.a so no patcher is inserted */
1898                         /* the type checker may set this later  */
1899                         iptr->val.a = NULL;
1900                         OP(opcode);
1901                         break;
1902
1903                 case JAVA_ATHROW:
1904                         blockend = true;
1905                         /* zero val.a so no patcher is inserted */
1906                         /* the type checker may set this later  */
1907                         iptr->val.a = NULL;
1908                         OP(opcode);
1909                         break;
1910                                 
1911
1912                 /* table jumps ********************************************************/
1913
1914                 case JAVA_LOOKUPSWITCH:
1915                         {
1916                                 s4 num, j;
1917                                 s4 *tablep;
1918 #if defined(ENABLE_VERIFIER)
1919                                 s4 prevvalue = 0;
1920 #endif
1921
1922                                 blockend = true;
1923                                 nextp = ALIGN((p + 1), 4);
1924
1925                                 CHECK_END_OF_BYTECODE(nextp + 8);
1926
1927                                 tablep = (s4 *) (m->jcode + nextp);
1928
1929                                 OP2A(opcode, 0, tablep, currentline);
1930
1931                                 /* default target */
1932
1933                                 j =  p + code_get_s4(nextp, m);
1934                                 *tablep = j;     /* restore for little endian */
1935                                 tablep++;
1936                                 nextp += 4;
1937                                 CHECK_BYTECODE_INDEX(j);
1938                                 block_insert(j);
1939
1940                                 /* number of pairs */
1941
1942                                 num = code_get_u4(nextp, m);
1943                                 *tablep = num;
1944                                 tablep++;
1945                                 nextp += 4;
1946
1947                                 CHECK_END_OF_BYTECODE(nextp + 8 * num);
1948
1949                                 for (i = 0; i < num; i++) {
1950                                         /* value */
1951
1952                                         j = code_get_s4(nextp, m);
1953                                         *tablep = j; /* restore for little endian */
1954                                         tablep++;
1955                                         nextp += 4;
1956
1957 #if defined(ENABLE_VERIFIER)
1958                                         /* check if the lookup table is sorted correctly */
1959                                         
1960                                         if (i && (j <= prevvalue)) {
1961                                                 *exceptionptr = new_verifyerror(m, "Unsorted lookup switch");
1962                                                 return false;
1963                                         }
1964                                         prevvalue = j;
1965 #endif
1966
1967                                         /* target */
1968
1969                                         j = p + code_get_s4(nextp,m);
1970                                         *tablep = j; /* restore for little endian */
1971                                         tablep++;
1972                                         nextp += 4;
1973                                         CHECK_BYTECODE_INDEX(j);
1974                                         block_insert(j);
1975                                 }
1976
1977                                 break;
1978                         }
1979
1980
1981                 case JAVA_TABLESWITCH:
1982                         {
1983                                 s4 num, j;
1984                                 s4 *tablep;
1985
1986                                 blockend = true;
1987                                 nextp = ALIGN((p + 1), 4);
1988
1989                                 CHECK_END_OF_BYTECODE(nextp + 12);
1990
1991                                 tablep = (s4 *) (m->jcode + nextp);
1992
1993                                 OP2A(opcode, 0, tablep, currentline);
1994
1995                                 /* default target */
1996
1997                                 j = p + code_get_s4(nextp, m);
1998                                 *tablep = j;     /* restore for little endian */
1999                                 tablep++;
2000                                 nextp += 4;
2001                                 CHECK_BYTECODE_INDEX(j);
2002                                 block_insert(j);
2003
2004                                 /* lower bound */
2005
2006                                 j = code_get_s4(nextp, m);
2007                                 *tablep = j;     /* restore for little endian */
2008                                 tablep++;
2009                                 nextp += 4;
2010
2011                                 /* upper bound */
2012
2013                                 num = code_get_s4(nextp, m);
2014                                 *tablep = num;   /* restore for little endian */
2015                                 tablep++;
2016                                 nextp += 4;
2017
2018                                 num -= j;  /* difference of upper - lower */
2019
2020 #if defined(ENABLE_VERIFIER)
2021                                 if (num < 0) {
2022                                         *exceptionptr = new_verifyerror(m,
2023                                                         "invalid TABLESWITCH: upper bound < lower bound");
2024                                         return false;
2025                                 }
2026 #endif
2027
2028                                 CHECK_END_OF_BYTECODE(nextp + 4 * (num + 1));
2029
2030                                 for (i = 0; i <= num; i++) {
2031                                         j = p + code_get_s4(nextp,m);
2032                                         *tablep = j; /* restore for little endian */
2033                                         tablep++;
2034                                         nextp += 4;
2035                                         CHECK_BYTECODE_INDEX(j);
2036                                         block_insert(j);
2037                                 }
2038
2039                                 break;
2040                         }
2041
2042
2043                 /* load and store of object fields ************************************/
2044
2045                 case JAVA_AASTORE:
2046                         OP(opcode);
2047                         m->isleafmethod = false;
2048                         break;
2049
2050                 case JAVA_GETSTATIC:
2051                 case JAVA_PUTSTATIC:
2052                 case JAVA_GETFIELD:
2053                 case JAVA_PUTFIELD:
2054                         {
2055                                 constant_FMIref  *fr;
2056                                 unresolved_field *uf;
2057
2058                                 i = code_get_u2(p + 1, m);
2059                                 fr = class_getconstant(m->class, i,
2060                                                                            CONSTANT_Fieldref);
2061                                 if (!fr)
2062                                         return false;
2063
2064                                 OP2A_NOINC(opcode, fr->parseddesc.fd->type, fr, currentline);
2065
2066                                 /* only with -noverify, otherwise the typechecker does this */
2067
2068 #if defined(ENABLE_VERIFIER)
2069                                 if (!opt_verify) {
2070 #endif
2071                                         result = resolve_field_lazy(iptr,NULL,m);
2072                                         if (result == resolveFailed)
2073                                                 return false;
2074
2075                                         if (result != resolveSucceeded) {
2076                                                 uf = create_unresolved_field(m->class,
2077                                                                                                          m, iptr);
2078
2079                                                 if (!uf)
2080                                                         return false;
2081
2082                                                 /* store the unresolved_field pointer */
2083
2084                                                 /* XXX this will be changed */
2085                                                 iptr->val.a = uf;
2086                                                 iptr->target = (void*) 0x01; /* XXX target temporarily used as flag */
2087                                         }
2088                                         else {
2089                                                 iptr->target = NULL;
2090                                         }
2091 #if defined(ENABLE_VERIFIER)
2092                                 }
2093                                 else {
2094                                         iptr->target = NULL;
2095                                 }
2096 #endif
2097                                 PINC;
2098                         }
2099                         break;
2100                                 
2101
2102                 /* method invocation **************************************************/
2103
2104                 case JAVA_INVOKESTATIC:
2105                         i = code_get_u2(p + 1, m);
2106                         mr = class_getconstant(m->class, i,
2107                                         CONSTANT_Methodref);
2108                         if (!mr)
2109                                 return false;
2110
2111                         md = mr->parseddesc.md;
2112
2113                         if (!md->params)
2114                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
2115                                         return false;
2116
2117                         goto invoke_method;
2118
2119                 case JAVA_INVOKEINTERFACE:
2120                         i = code_get_u2(p + 1, m);
2121                                 
2122                         mr = class_getconstant(m->class, i,
2123                                         CONSTANT_InterfaceMethodref);
2124
2125                         goto invoke_nonstatic_method;
2126
2127                 case JAVA_INVOKESPECIAL:
2128                 case JAVA_INVOKEVIRTUAL:
2129                         i = code_get_u2(p + 1, m);
2130                         mr = class_getconstant(m->class, i,
2131                                         CONSTANT_Methodref);
2132
2133 invoke_nonstatic_method:
2134                         if (!mr)
2135                                 return false;
2136
2137                         md = mr->parseddesc.md;
2138
2139                         if (!md->params)
2140                                 if (!descriptor_params_from_paramtypes(md, 0))
2141                                         return false;
2142
2143 invoke_method:
2144                         m->isleafmethod = false;
2145
2146                         OP2A_NOINC(opcode, 0, mr, currentline);
2147
2148                         /* only with -noverify, otherwise the typechecker does this */
2149
2150 #if defined(ENABLE_VERIFIER)
2151                         if (!opt_verify) {
2152 #endif
2153                                 result = resolve_method_lazy(iptr,NULL,m);
2154                                 if (result == resolveFailed)
2155                                         return false;
2156
2157                                 if (result != resolveSucceeded) {
2158                                         um = create_unresolved_method(m->class,
2159                                                         m, iptr);
2160
2161                                         if (!um)
2162                                                 return false;
2163
2164                                         /* store the unresolved_method pointer */
2165
2166                                         /* XXX this will be changed */
2167                                         iptr->val.a = um;
2168                                         iptr->target = (void*) 0x01; /* XXX target temporarily used as flag */
2169                                 }
2170                                 else {
2171                                         /* the method could be resolved */
2172                                         iptr->target = NULL;
2173                                 }
2174 #if defined(ENABLE_VERIFIER)
2175                         }
2176                         else {
2177                                 iptr->target = NULL;
2178                         }
2179 #endif
2180                         PINC;
2181                         break;
2182
2183                 /* miscellaneous object operations ************************************/
2184
2185                 case JAVA_NEW:
2186                         i = code_get_u2(p + 1, m);
2187                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2188                         if (!cr)
2189                                 return false;
2190
2191                         if (!resolve_classref(m, cr, resolveLazy, true, true,
2192                                                                   &c))
2193                                 return false;
2194
2195                         LOADCONST_A_BUILTIN(c, cr);
2196                         bte = builtintable_get_internal(BUILTIN_new);
2197                         BUILTIN(bte, true, NULL, currentline);
2198                         s_count++;
2199                         break;
2200
2201                 case JAVA_CHECKCAST:
2202                         i = code_get_u2(p + 1, m);
2203                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2204                         if (!cr)
2205                                 return false;
2206
2207                         if (!resolve_classref(m, cr, resolveLazy, true,
2208                                                                   true, &c))
2209                                 return false;
2210
2211                         if (cr->name->text[0] == '[') {
2212                                 /* array type cast-check */
2213                                 OP2AT(opcode, 0, c, cr, currentline);
2214                                 m->isleafmethod = false;
2215
2216                         } 
2217                         else {
2218                                 /* object type cast-check */
2219                                 OP2AT(opcode, 1, c, cr, currentline);
2220                         }
2221                         break;
2222
2223                 case JAVA_INSTANCEOF:
2224                         i = code_get_u2(p + 1,m);
2225                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
2226                         if (!cr)
2227                                 return false;
2228
2229                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
2230                                 return false;
2231
2232                         if (cr->name->text[0] == '[') {
2233                                 /* array type cast-check */
2234                                 LOADCONST_A_BUILTIN(c, cr);
2235                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
2236                                 BUILTIN(bte, false, NULL, currentline);
2237                                 s_count++;
2238
2239                         } 
2240                         else {
2241                                 /* object type cast-check */
2242                                 OP2AT(opcode, 1, c, cr, currentline);
2243                         }
2244                         break;
2245
2246                 case JAVA_MONITORENTER:
2247 #if defined(ENABLE_THREADS)
2248                         if (checksync) {
2249                                 OP(ICMD_CHECKNULL);
2250                                 bte = builtintable_get_internal(BUILTIN_monitorenter);
2251                                 BUILTIN(bte, false, NULL, currentline);
2252                         } 
2253                         else
2254 #endif
2255                                 {
2256                                         OP(ICMD_CHECKNULL);
2257                                         OP(ICMD_POP);
2258                                 }
2259                         break;
2260
2261                 case JAVA_MONITOREXIT:
2262 #if defined(ENABLE_THREADS)
2263                         if (checksync) {
2264                                 bte = builtintable_get_internal(BUILTIN_monitorexit);
2265                                 BUILTIN(bte, false, NULL, currentline);
2266                         } 
2267                         else
2268 #endif
2269                                 {
2270                                         OP(ICMD_POP);
2271                                 }
2272                         break;
2273
2274                 /* any other basic operation ******************************************/
2275
2276                 case JAVA_IDIV:
2277 #if !SUPPORT_DIVISION
2278                         bte = builtintable_get_internal(BUILTIN_idiv);
2279                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2280                         m->isleafmethod = false;
2281 #else
2282                         OP(opcode);
2283 #endif
2284                         break;
2285
2286                 case JAVA_IREM:
2287 #if !SUPPORT_DIVISION
2288                         bte = builtintable_get_internal(BUILTIN_irem);
2289                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2290                         m->isleafmethod = false;
2291 #else
2292                         OP(opcode);
2293 #endif
2294                         break;
2295
2296                 case JAVA_LDIV:
2297 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
2298                         bte = builtintable_get_internal(BUILTIN_ldiv);
2299                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2300                         m->isleafmethod = false;
2301 #else
2302                         OP(opcode);
2303 #endif
2304                         break;
2305
2306                 case JAVA_LREM:
2307 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
2308                         bte = builtintable_get_internal(BUILTIN_lrem);
2309                         OP2A(opcode, bte->md->paramcount, bte, currentline);
2310                         m->isleafmethod = false;
2311 #else
2312                         OP(opcode);
2313 #endif
2314                         break;
2315
2316                 case JAVA_FREM:
2317 #if defined(__I386__)
2318                         OP(opcode);
2319 #else
2320                         bte = builtintable_get_internal(BUILTIN_frem);
2321                         BUILTIN(bte, false, NULL, currentline);
2322 #endif
2323                         break;
2324
2325                 case JAVA_DREM:
2326 #if defined(__I386__)
2327                         OP(opcode);
2328 #else
2329                         bte = builtintable_get_internal(BUILTIN_drem);
2330                         BUILTIN(bte, false, NULL, currentline);
2331 #endif
2332                         break;
2333
2334                 case JAVA_F2I:
2335 #if defined(__ALPHA__)
2336                         if (!opt_noieee) {
2337                                 bte = builtintable_get_internal(BUILTIN_f2i);
2338                                 BUILTIN(bte, false, NULL, currentline);
2339                         } 
2340                         else
2341 #endif
2342                                 {
2343                                         OP(opcode);
2344                                 }
2345                         break;
2346
2347                 case JAVA_F2L:
2348 #if defined(__ALPHA__)
2349                         if (!opt_noieee) {
2350                                 bte = builtintable_get_internal(BUILTIN_f2l);
2351                                 BUILTIN(bte, false, NULL, currentline);
2352                         } 
2353                         else 
2354 #endif
2355                                 {
2356                                         OP(opcode);
2357                                 }
2358                         break;
2359
2360                 case JAVA_D2I:
2361 #if defined(__ALPHA__)
2362                         if (!opt_noieee) {
2363                                 bte = builtintable_get_internal(BUILTIN_d2i);
2364                                 BUILTIN(bte, false, NULL, currentline);
2365                         } 
2366                         else
2367 #endif
2368                                 {
2369                                         OP(opcode);
2370                                 }
2371                         break;
2372
2373                 case JAVA_D2L:
2374 #if defined(__ALPHA__)
2375                         if (!opt_noieee) {
2376                                 bte = builtintable_get_internal(BUILTIN_d2l);
2377                                 BUILTIN(bte, false, NULL, currentline);
2378                         } 
2379                         else
2380 #endif
2381                                 {
2382                                         OP(opcode);
2383                                 }
2384                         break;
2385
2386                         /* check for invalid opcodes if the verifier is enabled */
2387 #if defined(ENABLE_VERIFIER)
2388                 case JAVA_BREAKPOINT:
2389                         *exceptionptr =
2390                                 new_verifyerror(m, "Quick instructions shouldn't appear yet.");
2391                         return false;
2392
2393                 case 186: /* unused opcode */
2394                 case 203:
2395                 case 204:
2396                 case 205:
2397                 case 206:
2398                 case 207:
2399                 case 208:
2400                 case 209:
2401                 case 210:
2402                 case 211:
2403                 case 212:
2404                 case 213:
2405                 case 214:
2406                 case 215:
2407                 case 216:
2408                 case 217:
2409                 case 218:
2410                 case 219:
2411                 case 220:
2412                 case 221:
2413                 case 222:
2414                 case 223:
2415                 case 224:
2416                 case 225:
2417                 case 226:
2418                 case 227:
2419                 case 228:
2420                 case 229:
2421                 case 230:
2422                 case 231:
2423                 case 232:
2424                 case 233:
2425                 case 234:
2426                 case 235:
2427                 case 236:
2428                 case 237:
2429                 case 238:
2430                 case 239:
2431                 case 240:
2432                 case 241:
2433                 case 242:
2434                 case 243:
2435                 case 244:
2436                 case 245:
2437                 case 246:
2438                 case 247:
2439                 case 248:
2440                 case 249:
2441                 case 250:
2442                 case 251:
2443                 case 252:
2444                 case 253:
2445                 case 254:
2446                 case 255:
2447                         *exceptionptr =
2448                                 new_verifyerror(m,"Illegal opcode %d at instr %d\n",
2449                                                                   opcode, ipc);
2450                         return false;
2451                         break;
2452 #endif /* defined(ENABLE_VERIFIER) */
2453
2454                 default:
2455                         /* straight-forward translation to ICMD */
2456                         OP(opcode);
2457                         break;
2458                                 
2459                 } /* end switch */
2460
2461 #if defined(ENABLE_VERIFIER)
2462                 /* If WIDE was used correctly, iswide should have been reset by now. */
2463                 if (iswide) {
2464                         *exceptionptr = new_verifyerror(m,
2465                                         "Illegal instruction: WIDE before incompatible opcode");
2466                         return false;
2467                 }
2468 #endif /* defined(ENABLE_VERIFIER) */
2469
2470         } /* end for */
2471
2472 #if defined(ENABLE_VERIFIER)
2473         if (p != m->jcodelength) {
2474                 *exceptionptr = new_verifyerror(m,
2475                                 "Command-sequence crosses code-boundary");
2476                 return false;
2477         }
2478
2479         if (!blockend) {
2480                 *exceptionptr = new_verifyerror(m, "Falling off the end of the code");
2481                 return false;
2482         }
2483 #endif /* defined(ENABLE_VERIFIER) */
2484
2485         /* adjust block count if target 0 is not first intermediate instruction */
2486
2487         if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1))
2488                 b_count++;
2489
2490         /* copy local to method variables */
2491
2492         m->instructioncount = ipc;
2493         m->basicblockcount = b_count;
2494         m->stackcount = s_count + m->basicblockcount * m->maxstack;
2495
2496         /* allocate stack table */
2497
2498         m->stack = DMNEW(stackelement, m->stackcount);
2499
2500         {
2501                 basicblock *bptr;
2502
2503                 bptr = m->basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
2504
2505                 b_count = 0;
2506                 m->c_debug_nr = 0;
2507         
2508                 /* additional block if target 0 is not first intermediate instruction */
2509
2510                 if (!m->basicblockindex[0] || (m->basicblockindex[0] > 1)) {
2511                         BASICBLOCK_INIT(bptr,m);
2512
2513                         bptr->iinstr = m->instructions;
2514                         /* bptr->icount is set when the next block is allocated */
2515
2516                         bptr++;
2517                         b_count++;
2518                         bptr[-1].next = bptr;
2519                 }
2520
2521                 /* allocate blocks */
2522
2523                 for (p = 0; p < m->jcodelength; p++) { 
2524                         if (m->basicblockindex[p] & 1) {
2525                                 /* Check if this block starts at the beginning of an          */
2526                                 /* instruction.                                               */
2527 #if defined(ENABLE_VERIFIER)
2528                                 if (!instructionstart[p]) {
2529                                         *exceptionptr = new_verifyerror(m,
2530                                                 "Branch into middle of instruction");
2531                                         return false;
2532                                 }
2533 #endif
2534
2535                                 /* allocate the block */
2536
2537                                 BASICBLOCK_INIT(bptr,m);
2538
2539                                 bptr->iinstr = m->instructions + (m->basicblockindex[p] >> 1);
2540                                 if (b_count) {
2541                                         bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
2542                                 }
2543                                 /* bptr->icount is set when the next block is allocated */
2544
2545                                 m->basicblockindex[p] = b_count;
2546
2547                                 bptr++;
2548                                 b_count++;
2549                                 bptr[-1].next = bptr;
2550                         }
2551                 }
2552
2553                 /* set instruction count of last real block */
2554
2555                 if (b_count) {
2556                         bptr[-1].icount = (m->instructions + m->instructioncount) - bptr[-1].iinstr;
2557                 }
2558
2559                 /* allocate additional block at end */
2560
2561                 BASICBLOCK_INIT(bptr,m);
2562                 
2563                 bptr->instack = bptr->outstack = NULL;
2564                 bptr->indepth = bptr->outdepth = 0;
2565                 bptr->iinstr = NULL;
2566                 bptr->icount = 0;
2567                 bptr->next = NULL;
2568
2569                 /* set basicblock pointers in exception table */
2570
2571                 if (cd->exceptiontablelength > 0) {
2572                         cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
2573                 }
2574                 
2575                 for (i = 0; i < cd->exceptiontablelength; ++i) {
2576                         p = cd->exceptiontable[i].startpc;
2577                         cd->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
2578
2579                         p = cd->exceptiontable[i].endpc;
2580                         cd->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
2581
2582                         p = cd->exceptiontable[i].handlerpc;
2583                         cd->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
2584             }
2585
2586                 /* XXX activate this if you want to try inlining */
2587 #if 0
2588                 for (i = 0; i < m->exceptiontablelength; ++i) {
2589                         p = m->exceptiontable[i].startpc;
2590                         m->exceptiontable[i].start = m->basicblocks + m->basicblockindex[p];
2591
2592                         p = m->exceptiontable[i].endpc;
2593                         m->exceptiontable[i].end = (p == m->jcodelength) ? (m->basicblocks + m->basicblockcount /*+ 1*/) : (m->basicblocks + m->basicblockindex[p]);
2594
2595                         p = m->exceptiontable[i].handlerpc;
2596                         m->exceptiontable[i].handler = m->basicblocks + m->basicblockindex[p];
2597             }
2598 #endif
2599
2600         }
2601
2602         /* everything's ok */
2603
2604         return true;
2605
2606 #if defined(ENABLE_VERIFIER)
2607
2608 throw_unexpected_end_of_bytecode:
2609         *exceptionptr = new_verifyerror(m, "Unexpected end of bytecode");
2610         return false;
2611
2612 throw_invalid_bytecode_index:
2613         *exceptionptr =
2614                 new_verifyerror(m, "Illegal target of branch instruction");
2615         return false;
2616
2617 throw_illegal_local_variable_number:
2618         *exceptionptr =
2619                 new_verifyerror(m, "Illegal local variable number");
2620         return false;
2621                 
2622 #endif /* ENABLE_VERIFIER */
2623 }
2624
2625
2626 /*
2627  * These are local overrides for various environment variables in Emacs.
2628  * Please do not remove this and leave it at the end of the file, where
2629  * Emacs will automagically detect them.
2630  * ---------------------------------------------------------------------
2631  * Local variables:
2632  * mode: c
2633  * indent-tabs-mode: t
2634  * c-basic-offset: 4
2635  * tab-width: 4
2636  * End:
2637  * vim:noexpandtab:sw=4:ts=4:
2638  */