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