* src/vm/jit/i386/emit.c: Use IS_INMEMORY macro.
[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 5375 2006-09-06 16:01:23Z edwin $
35
36 */
37
38
39 #include "config.h"
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "vm/types.h"
45
46 #include "mm/memory.h"
47 #include "native/native.h"
48
49 #if defined(ENABLE_THREADS)
50 # include "threads/native/lock.h"
51 #endif
52
53 #include "toolbox/logging.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/linker.h"
58 #include "vm/loader.h"
59 #include "vm/resolve.h"
60 #include "vm/options.h"
61 #include "vm/statistics.h"
62 #include "vm/stringlocal.h"
63 #include "vm/suck.h"
64 #include "vm/jit/asmpart.h"
65 #include "vm/jit/jit.h"
66 #include "vm/jit/parse.h"
67 #include "vm/jit/patcher.h"
68 #include "vm/jit/loop/loop.h"
69
70 #define INSTRUCTIONS_INCREMENT  5  /* number of additional instructions to    */
71                                    /* allocate if space runs out              */
72
73
74 /* parserdata_t ***************************************************************/
75
76 typedef struct parsedata_t parsedata_t;
77
78 struct parsedata_t {
79         instruction *instructions;          /* instruction array                  */
80         s4           instructionslength;    /* length of the instruction array    */
81         u1          *instructionstart;
82 };
83
84
85 /* parse_setup *****************************************************************
86
87    Fills the passed parsedata_t structure.
88
89 *******************************************************************************/
90
91 static void parse_setup(jitdata *jd, parsedata_t *pd)
92 {
93         methodinfo *m;
94
95         /* get required compiler data */
96
97         m = jd->m;
98
99         /* Allocate instruction array and block index table (1 additional
100            for end ipc). */
101
102         jd->new_basicblockindex = DMNEW(s4, m->jcodelength + 1);
103         pd->instructionstart = DMNEW(u1, m->jcodelength + 1);
104
105         MZERO(jd->new_basicblockindex, s4, m->jcodelength + 1);
106         MZERO(pd->instructionstart, u1, m->jcodelength + 1);
107
108         /* Set the length of the instruction array.  We simply add 5 more
109            instruction, as this seems to be a reasonable value. */
110
111         pd->instructionslength = m->jcodelength + 1;
112
113         /* allocate the instruction array */
114
115         pd->instructions = DMNEW(instruction, pd->instructionslength);
116
117         /* Zero the intermediate instructions array so we don't have any
118            invalid pointers in it if we cannot finish stack_analyse(). */
119
120         MZERO(pd->instructions, instruction, pd->instructionslength);
121 }
122
123
124 /* parse_check_instructions ****************************************************
125
126    Checks if there's enough room in the instructions array for the
127    required instructions.  If not, reallocate the data structures.
128
129 *******************************************************************************/
130
131 static instruction *parse_check_instructions(parsedata_t *pd, s4 ipc)
132 {
133         puts("REALLOCATE!");
134
135         /* increase the size of the instruction array */
136
137         pd->instructionslength += INSTRUCTIONS_INCREMENT;
138
139         /* reallocate the array */
140
141         pd->instructions = DMREALLOC(pd->instructions, instruction, ipc,
142                                                                  pd->instructionslength);
143
144         /* return the iptr */
145
146         return pd->instructions + ipc;
147 }
148
149
150 /*******************************************************************************
151
152         function 'parse' scans the JavaVM code and generates intermediate code
153
154         During parsing the block index table is used to store at bit pos 0
155         a flag which marks basic block starts and at position 1 to 31 the
156         intermediate instruction index. After parsing the block index table
157         is scanned, for marked positions a block is generated and the block
158         number is stored in the block index table.
159
160 *******************************************************************************/
161
162 static exceptiontable * new_fillextable(
163                                                                         jitdata *jd,
164                                                                         methodinfo *m, 
165                                                                         exceptiontable *extable, 
166                                                                         exceptiontable *raw_extable, 
167                                                                 int exceptiontablelength, 
168                                                                         int *block_count)
169 {
170         int b_count, p, src;
171         
172         if (exceptiontablelength == 0) 
173                 return extable;
174
175         b_count = *block_count;
176
177         for (src = exceptiontablelength-1; src >=0; src--) {
178                 /* the start of the handled region becomes a basic block start */
179                 p = raw_extable[src].startpc;
180                 CHECK_BYTECODE_INDEX(p);
181                 extable->startpc = p;
182                 MARK_BASICBLOCK(p);
183                 
184                 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
185                 CHECK_BYTECODE_INDEX_EXCLUSIVE(p);
186
187 #if defined(ENABLE_VERIFIER)
188                 if (p <= raw_extable[src].startpc) {
189                         exceptions_throw_verifyerror(m,
190                                 "Invalid exception handler range");
191                         return NULL;
192                 }
193 #endif
194                 extable->endpc = p;
195                 
196                 /* end of handled region becomes a basic block boundary  */
197                 /* (If it is the bytecode end, we'll use the special     */
198                 /* end block that is created anyway.)                    */
199                 if (p < m->jcodelength) 
200                         MARK_BASICBLOCK(p);
201
202                 /* the start of the handler becomes a basic block start  */
203                 p = raw_extable[src].handlerpc;
204                 CHECK_BYTECODE_INDEX(p);
205                 extable->handlerpc = p;
206                 MARK_BASICBLOCK(p);
207
208                 extable->catchtype = raw_extable[src].catchtype;
209                 extable->next = NULL;
210                 extable->down = &extable[1];
211                 extable--;
212         }
213
214         *block_count = b_count;
215         
216         /* everything ok */
217         return extable;
218
219 #if defined(ENABLE_VERIFIER)
220 throw_invalid_bytecode_index:
221         exceptions_throw_verifyerror(m,
222                                                                  "Illegal bytecode index in exception table");
223         return NULL;
224 #endif
225 }
226
227 /*** macro for checking the length of the bytecode ***/
228
229 #if defined(ENABLE_VERIFIER)
230 #define CHECK_END_OF_BYTECODE(neededlength) \
231         do { \
232                 if ((neededlength) > m->jcodelength) \
233                         goto throw_unexpected_end_of_bytecode; \
234         } while (0)
235 #else /* !ENABLE_VERIFIER */
236 #define CHECK_END_OF_BYTECODE(neededlength)
237 #endif /* ENABLE_VERIFIER */
238
239 bool new_parse(jitdata *jd)
240 {
241         methodinfo  *m;                     /* method being parsed                */
242         codeinfo    *code;
243         codegendata *cd;
244         parsedata_t  pd;
245         instruction *iptr;                  /* current ptr into instruction array */
246         s4           ipc;                   /* intermediate instruction counter   */
247         s4           p;                     /* java instruction counter           */
248         s4           nextp;                 /* start of next java instruction     */
249         s4           opcode;                /* java opcode                        */
250         s4           i;
251         s4           j;
252         int  b_count = 0;           /* basic block counter                      */
253         int  s_count = 0;           /* stack element counter                    */
254         bool blockend = false;      /* true if basic block end has been reached */
255         bool iswide = false;        /* true if last instruction was a wide      */
256         constant_classref  *cr;
257         constant_classref  *compr;
258         classinfo          *c;
259         builtintable_entry *bte;
260         constant_FMIref    *mr;
261         methoddesc         *md;
262         unresolved_method  *um;
263         resolve_result_t    result;
264         u2                  lineindex = 0;
265         u2                  currentline = 0;
266         u2                  linepcchange = 0;
267         u4                  flags;
268         basicblock         *bptr;
269
270 #if defined(NEW_VAR)
271         int                *local_map; /* local pointer to renaming structore     */
272                                        /* is assigned to rd->local_map at the end */
273 #endif
274         /* get required compiler data */
275
276         m    = jd->m;
277         code = jd->code;
278         cd   = jd->cd;
279
280 #if defined(NEW_VAR)
281         /* allocate buffers for local variable renaming */
282         local_map = DMNEW(int, cd->maxlocals * 5);
283         for (i = 0; i < cd->maxlocals; i++) {
284                 local_map[i * 5 + 0] = 0;
285                 local_map[i * 5 + 1] = 0;
286                 local_map[i * 5 + 2] = 0;
287                 local_map[i * 5 + 3] = 0;
288                 local_map[i * 5 + 4] = 0;
289         }
290 #endif
291
292         /* initialize the parse data structures */
293   
294         parse_setup(jd, &pd);
295   
296         /* initialize local variables */
297   
298         iptr = pd.instructions;
299         ipc  = 0;
300   
301         /* compute branch targets of exception table */
302
303         if (!new_fillextable(jd, m,
304                         &(cd->exceptiontable[cd->exceptiontablelength-1]),
305                         m->exceptiontable,
306                         m->exceptiontablelength,
307                         &b_count))
308         {
309                 return false;
310         }
311
312         s_count = 1 + m->exceptiontablelength; /* initialize stack element counter   */
313
314 #if defined(ENABLE_THREADS)
315         if (checksync && (m->flags & ACC_SYNCHRONIZED))
316                 jd->isleafmethod = false;
317 #endif
318
319         /* setup line number info */
320
321         currentline = 0;
322         linepcchange = 0;
323
324         if (m->linenumbercount == 0) {
325                 lineindex = 0;
326         }
327         else {
328                 linepcchange = m->linenumbers[0].start_pc;
329         }
330
331         /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
332
333         for (p = 0; p < m->jcodelength; p = nextp) {
334
335                 /* mark this position as a valid instruction start */
336
337                 pd.instructionstart[p] = 1;
338
339                 /* change the current line number, if necessary */
340
341                 /* XXX rewrite this using pointer arithmetic */
342
343                 if (linepcchange == p) {
344                         if (m->linenumbercount > lineindex) {
345 next_linenumber:
346                                 currentline = m->linenumbers[lineindex].line_number;
347                                 lineindex++;
348                                 if (lineindex < m->linenumbercount) {
349                                         linepcchange = m->linenumbers[lineindex].start_pc;
350                                         if (linepcchange == p)
351                                                 goto next_linenumber;
352                                 }
353                         }
354                 }
355
356 fetch_opcode:
357                 /* fetch next opcode  */        
358
359                 opcode = SUCK_BE_U1(m->jcode + p);
360
361                 /* some compilers put a JAVA_NOP after a blockend instruction */
362
363                 if (blockend && (opcode != JAVA_NOP)) {
364                         /* start new block */
365
366                         MARK_BASICBLOCK(p);
367                         blockend = false;
368                 }
369
370                 /* store intermediate instruction count (bit 0 mark block starts) */
371
372                 jd->new_basicblockindex[p] |= (ipc << 1);
373
374                 /* compute next instruction start */
375
376                 nextp = p + jcommandsize[opcode];
377
378                 CHECK_END_OF_BYTECODE(nextp);
379
380                 /* add stack elements produced by this instruction */
381
382                 s_count += stackreq[opcode];
383
384                 /* We check here for the space of 1 instruction in the
385                    instruction array.  If an opcode is converted to more than
386                    1 instruction, this is checked in the corresponding
387                    case. */
388
389                 INSTRUCTIONS_CHECK(1);
390
391                 /* translate this bytecode instruction */
392
393                 switch (opcode) {
394
395                 case JAVA_NOP:
396                         break;
397
398                 /* pushing constants onto the stack ***********************************/
399
400                 case JAVA_BIPUSH:
401                         OP_LOADCONST_I(SUCK_BE_S1(m->jcode + p + 1));
402                         break;
403
404                 case JAVA_SIPUSH:
405                         OP_LOADCONST_I(SUCK_BE_S2(m->jcode + p + 1));
406                         break;
407
408                 case JAVA_LDC1:
409                         i = SUCK_BE_U1(m->jcode + p + 1);
410                         goto pushconstantitem;
411
412                 case JAVA_LDC2:
413                 case JAVA_LDC2W:
414                         i = SUCK_BE_U2(m->jcode + p + 1);
415
416                 pushconstantitem:
417
418 #if defined(ENABLE_VERIFIER)
419                         if (i >= m->class->cpcount) {
420                                 exceptions_throw_verifyerror(m,
421                                         "Attempt to access constant outside range");
422                                 return false;
423                         }
424 #endif
425
426                         switch (m->class->cptags[i]) {
427                         case CONSTANT_Integer:
428                                 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
429                                 break;
430                         case CONSTANT_Long:
431                                 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
432                                 break;
433                         case CONSTANT_Float:
434                                 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
435                                 break;
436                         case CONSTANT_Double:
437                                 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
438                                 break;
439                         case CONSTANT_String:
440                                 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
441                                 break;
442                         case CONSTANT_Class:
443                                 cr = (constant_classref *) (m->class->cpinfos[i]);
444
445                                 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
446                                         return false;
447
448                                 /* if not resolved, c == NULL */
449
450                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
451
452                                 break;
453
454 #if defined(ENABLE_VERIFIER)
455                         default:
456                                 exceptions_throw_verifyerror(m,
457                                                 "Invalid constant type to push");
458                                 return false;
459 #endif
460                         }
461                         break;
462
463                 case JAVA_ACONST_NULL:
464                         OP_LOADCONST_NULL();
465                         break;
466
467                 case JAVA_ICONST_M1:
468                 case JAVA_ICONST_0:
469                 case JAVA_ICONST_1:
470                 case JAVA_ICONST_2:
471                 case JAVA_ICONST_3:
472                 case JAVA_ICONST_4:
473                 case JAVA_ICONST_5:
474                         OP_LOADCONST_I(opcode - JAVA_ICONST_0);
475                         break;
476
477                 case JAVA_LCONST_0:
478                 case JAVA_LCONST_1:
479                         OP_LOADCONST_L(opcode - JAVA_LCONST_0);
480                         break;
481
482                 case JAVA_FCONST_0:
483                 case JAVA_FCONST_1:
484                 case JAVA_FCONST_2:
485                         OP_LOADCONST_F(opcode - JAVA_FCONST_0);
486                         break;
487
488                 case JAVA_DCONST_0:
489                 case JAVA_DCONST_1:
490                         OP_LOADCONST_D(opcode - JAVA_DCONST_0);
491                         break;
492
493                 /* local variable access instructions *********************************/
494
495                 case JAVA_ILOAD:
496                 case JAVA_FLOAD:
497                 case JAVA_ALOAD:
498                         if (iswide == false) {
499                                 i = SUCK_BE_U1(m->jcode + p + 1);
500                         }
501                         else {
502                                 i = SUCK_BE_U2(m->jcode + p + 1);
503                                 nextp = p + 3;
504                                 iswide = false;
505                         }
506                         OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
507                         break;
508
509                 case JAVA_LLOAD:
510                 case JAVA_DLOAD:
511                         if (iswide == false) {
512                                 i = SUCK_BE_U1(m->jcode + p + 1);
513                         }
514                         else {
515                                 i = SUCK_BE_U2(m->jcode + p + 1);
516                                 nextp = p + 3;
517                                 iswide = false;
518                         }
519                         OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
520                         break;
521
522                 case JAVA_ILOAD_0:
523                 case JAVA_ILOAD_1:
524                 case JAVA_ILOAD_2:
525                 case JAVA_ILOAD_3:
526                         OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
527                         break;
528
529                 case JAVA_LLOAD_0:
530                 case JAVA_LLOAD_1:
531                 case JAVA_LLOAD_2:
532                 case JAVA_LLOAD_3:
533                         OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
534                         break;
535
536                 case JAVA_FLOAD_0:
537                 case JAVA_FLOAD_1:
538                 case JAVA_FLOAD_2:
539                 case JAVA_FLOAD_3:
540                         OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
541                         break;
542
543                 case JAVA_DLOAD_0:
544                 case JAVA_DLOAD_1:
545                 case JAVA_DLOAD_2:
546                 case JAVA_DLOAD_3:
547                         OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
548                         break;
549
550                 case JAVA_ALOAD_0:
551                 case JAVA_ALOAD_1:
552                 case JAVA_ALOAD_2:
553                 case JAVA_ALOAD_3:
554                         OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
555                         break;
556
557                 case JAVA_ISTORE:
558                 case JAVA_FSTORE:
559                 case JAVA_ASTORE:
560                         if (iswide == false) {
561                                 i = SUCK_BE_U1(m->jcode + p + 1);
562                         }
563                         else {
564                                 i = SUCK_BE_U2(m->jcode + p + 1);
565                                 iswide = false;
566                                 nextp = p + 3;
567                         }
568                         OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
569                         break;
570
571                 case JAVA_LSTORE:
572                 case JAVA_DSTORE:
573                         if (iswide == false) {
574                                 i = SUCK_BE_U1(m->jcode + p + 1);
575                         }
576                         else {
577                                 i = SUCK_BE_U2(m->jcode + p + 1);
578                                 iswide = false;
579                                 nextp = p + 3;
580                         }
581                         OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
582                         break;
583
584                 case JAVA_ISTORE_0:
585                 case JAVA_ISTORE_1:
586                 case JAVA_ISTORE_2:
587                 case JAVA_ISTORE_3:
588                         OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
589                         break;
590
591                 case JAVA_LSTORE_0:
592                 case JAVA_LSTORE_1:
593                 case JAVA_LSTORE_2:
594                 case JAVA_LSTORE_3:
595                         OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
596                         break;
597
598                 case JAVA_FSTORE_0:
599                 case JAVA_FSTORE_1:
600                 case JAVA_FSTORE_2:
601                 case JAVA_FSTORE_3:
602                         OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
603                         break;
604
605                 case JAVA_DSTORE_0:
606                 case JAVA_DSTORE_1:
607                 case JAVA_DSTORE_2:
608                 case JAVA_DSTORE_3:
609                         OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
610                         break;
611
612                 case JAVA_ASTORE_0:
613                 case JAVA_ASTORE_1:
614                 case JAVA_ASTORE_2:
615                 case JAVA_ASTORE_3:
616                         OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
617                         break;
618
619                 case JAVA_IINC:
620                         {
621                                 int v;
622
623                                 if (iswide == false) {
624                                         i = SUCK_BE_U1(m->jcode + p + 1);
625                                         v = SUCK_BE_S1(m->jcode + p + 2);
626
627                                 }
628                                 else {
629                                         i = SUCK_BE_U2(m->jcode + p + 1);
630                                         v = SUCK_BE_S2(m->jcode + p + 3);
631                                         iswide = false;
632                                         nextp = p + 5;
633                                 }
634                                 INDEX_ONEWORD(i);
635                                 LOCALTYPE_USED(i, TYPE_INT);
636                                 OP_LOCALINDEX_I(opcode, i, v);
637                         }
638                         break;
639
640                 /* wider index for loading, storing and incrementing ******************/
641
642                 case JAVA_WIDE:
643                         iswide = true;
644                         p++;
645                         goto fetch_opcode;
646
647                 /* managing arrays ****************************************************/
648
649                 case JAVA_NEWARRAY:
650                         switch (SUCK_BE_S1(m->jcode + p + 1)) {
651                         case 4:
652                                 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
653                                 break;
654                         case 5:
655                                 bte = builtintable_get_internal(BUILTIN_newarray_char);
656                                 break;
657                         case 6:
658                                 bte = builtintable_get_internal(BUILTIN_newarray_float);
659                                 break;
660                         case 7:
661                                 bte = builtintable_get_internal(BUILTIN_newarray_double);
662                                 break;
663                         case 8:
664                                 bte = builtintable_get_internal(BUILTIN_newarray_byte);
665                                 break;
666                         case 9:
667                                 bte = builtintable_get_internal(BUILTIN_newarray_short);
668                                 break;
669                         case 10:
670                                 bte = builtintable_get_internal(BUILTIN_newarray_int);
671                                 break;
672                         case 11:
673                                 bte = builtintable_get_internal(BUILTIN_newarray_long);
674                                 break;
675 #if defined(ENABLE_VERIFIER)
676                         default:
677                                 exceptions_throw_verifyerror(m, "Invalid array-type to create");
678                                 return false;
679 #endif
680                         }
681                         OP_BUILTIN_CHECK_EXCEPTION(bte);
682                         break;
683
684                 case JAVA_ANEWARRAY:
685                         i = SUCK_BE_U2(m->jcode + p + 1);
686                         compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
687                         if (!compr)
688                                 return false;
689
690                         if (!(cr = class_get_classref_multiarray_of(1, compr)))
691                                 return false;
692
693                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
694                                 return false;
695
696                         INSTRUCTIONS_CHECK(2);
697                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
698                         bte = builtintable_get_internal(BUILTIN_newarray);
699                         OP_BUILTIN_CHECK_EXCEPTION(bte);
700                         s_count++;
701                         break;
702
703                 case JAVA_MULTIANEWARRAY:
704                         jd->isleafmethod = false;
705                         i = SUCK_BE_U2(m->jcode + p + 1);
706                         j = SUCK_BE_U1(m->jcode + p + 3);
707   
708                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
709                         if (cr == NULL)
710                                 return false;
711   
712                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
713                                 return false;
714   
715                         /* if unresolved, c == NULL */
716   
717                         iptr->s1.argcount = j;
718                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
719                         break;
720
721                 /* control flow instructions ******************************************/
722
723                 case JAVA_IFEQ:
724                 case JAVA_IFLT:
725                 case JAVA_IFLE:
726                 case JAVA_IFNE:
727                 case JAVA_IFGT:
728                 case JAVA_IFGE:
729                 case JAVA_IFNULL:
730                 case JAVA_IFNONNULL:
731                 case JAVA_IF_ICMPEQ:
732                 case JAVA_IF_ICMPNE:
733                 case JAVA_IF_ICMPLT:
734                 case JAVA_IF_ICMPGT:
735                 case JAVA_IF_ICMPLE:
736                 case JAVA_IF_ICMPGE:
737                 case JAVA_IF_ACMPEQ:
738                 case JAVA_IF_ACMPNE:
739                 case JAVA_GOTO:
740                         i = p + SUCK_BE_S2(m->jcode + p + 1);
741                         CHECK_BYTECODE_INDEX(i);
742                         MARK_BASICBLOCK(i);
743                         blockend = true;
744                         OP_INSINDEX(opcode, i);
745                         break;
746
747                 case JAVA_GOTO_W:
748                         i = p + SUCK_BE_S4(m->jcode + p + 1);
749                         CHECK_BYTECODE_INDEX(i);
750                         MARK_BASICBLOCK(i);
751                         blockend = true;
752                         OP_INSINDEX(opcode, i);
753                         break;
754
755                 case JAVA_JSR:
756                         i = p + SUCK_BE_S2(m->jcode + p + 1);
757 jsr_tail:
758                         CHECK_BYTECODE_INDEX(i);
759                         MARK_BASICBLOCK(i);
760                         blockend = true;
761                         OP_PREPARE_ZEROFLAGS(JAVA_JSR);
762                         iptr->sx.s23.s3.jsrtarget.insindex = i;
763                         PINC;
764                         break;
765
766                 case JAVA_JSR_W:
767                         i = p + SUCK_BE_S4(m->jcode + p + 1);
768                         goto jsr_tail;
769
770                 case JAVA_RET:
771                         if (iswide == false) {
772                                 i = SUCK_BE_U1(m->jcode + p + 1);
773                         }
774                         else {
775                                 i = SUCK_BE_U2(m->jcode + p + 1);
776                                 nextp = p + 3;
777                                 iswide = false;
778                         }
779                         blockend = true;
780
781                         OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
782                         break;
783
784                 case JAVA_IRETURN:
785                 case JAVA_LRETURN:
786                 case JAVA_FRETURN:
787                 case JAVA_DRETURN:
788                 case JAVA_ARETURN:
789                 case JAVA_RETURN:
790                         blockend = true;
791                         /* XXX ARETURN will need a flag in the typechecker */
792                         OP(opcode);
793                         break;
794
795                 case JAVA_ATHROW:
796                         blockend = true;
797                         /* XXX ATHROW will need a flag in the typechecker */
798                         OP(opcode);
799                         break;
800
801
802                 /* table jumps ********************************************************/
803
804                 case JAVA_LOOKUPSWITCH:
805                         {
806                                 s4 num, j;
807                                 lookup_target_t *lookup;
808 #if defined(ENABLE_VERIFIER)
809                                 s4 prevvalue = 0;
810 #endif
811                                 blockend = true;
812                                 nextp = ALIGN((p + 1), 4);
813
814                                 CHECK_END_OF_BYTECODE(nextp + 8);
815
816                                 OP_PREPARE_ZEROFLAGS(opcode);
817
818                                 /* default target */
819
820                                 j = p + SUCK_BE_S4(m->jcode + nextp);
821                                 iptr->sx.s23.s3.lookupdefault.insindex = j;
822                                 nextp += 4;
823                                 CHECK_BYTECODE_INDEX(j);
824                                 MARK_BASICBLOCK(j);
825
826                                 /* number of pairs */
827
828                                 num = SUCK_BE_U4(m->jcode + nextp);
829                                 iptr->sx.s23.s2.lookupcount = num;
830                                 nextp += 4;
831
832                                 /* allocate the intermediate code table */
833
834                                 lookup = DMNEW(lookup_target_t, num);
835                                 iptr->dst.lookup = lookup;
836
837                                 /* iterate over the lookup table */
838
839                                 CHECK_END_OF_BYTECODE(nextp + 8 * num);
840
841                                 for (i = 0; i < num; i++) {
842                                         /* value */
843
844                                         j = SUCK_BE_S4(m->jcode + nextp);
845                                         lookup->value = j;
846
847                                         nextp += 4;
848
849 #if defined(ENABLE_VERIFIER)
850                                         /* check if the lookup table is sorted correctly */
851
852                                         if (i && (j <= prevvalue)) {
853                                                 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
854                                                 return false;
855                                         }
856                                         prevvalue = j;
857 #endif
858                                         /* target */
859
860                                         j = p + SUCK_BE_S4(m->jcode + nextp);
861                                         lookup->target.insindex = j;
862                                         lookup++;
863                                         nextp += 4;
864                                         CHECK_BYTECODE_INDEX(j);
865                                         MARK_BASICBLOCK(j);
866                                 }
867
868                                 PINC;
869                                 break;
870                         }
871
872
873                 case JAVA_TABLESWITCH:
874                         {
875                                 s4 num, j;
876                                 s4 deftarget;
877                                 branch_target_t *table;
878
879                                 blockend = true;
880                                 nextp = ALIGN((p + 1), 4);
881
882                                 CHECK_END_OF_BYTECODE(nextp + 12);
883
884                                 OP_PREPARE_ZEROFLAGS(opcode);
885
886                                 /* default target */
887
888                                 deftarget = p + SUCK_BE_S4(m->jcode + nextp);
889                                 nextp += 4;
890                                 CHECK_BYTECODE_INDEX(deftarget);
891                                 MARK_BASICBLOCK(deftarget);
892
893                                 /* lower bound */
894
895                                 j = SUCK_BE_S4(m->jcode + nextp);
896                                 iptr->sx.s23.s2.tablelow = j;
897                                 nextp += 4;
898
899                                 /* upper bound */
900
901                                 num = SUCK_BE_S4(m->jcode + nextp);
902                                 iptr->sx.s23.s3.tablehigh = num;
903                                 nextp += 4;
904
905                                 /* calculate the number of table entries */
906
907                                 num = num - j + 1;
908
909 #if defined(ENABLE_VERIFIER)
910                                 if (num < 1) {
911                                         exceptions_throw_verifyerror(m,
912                                                         "invalid TABLESWITCH: upper bound < lower bound");
913                                         return false;
914                                 }
915 #endif
916                                 /* create the intermediate code table */
917                                 /* the first entry is the default target */
918
919                                 table = MNEW(branch_target_t, 1 + num);
920                                 iptr->dst.table = table;
921                                 (table++)->insindex = deftarget;
922
923                                 /* iterate over the target table */
924
925                                 CHECK_END_OF_BYTECODE(nextp + 4 * num);
926
927                                 for (i = 0; i < num; i++) {
928                                         j = p + SUCK_BE_S4(m->jcode + nextp);
929                                         (table++)->insindex = j;
930                                         nextp += 4;
931                                         CHECK_BYTECODE_INDEX(j);
932                                         MARK_BASICBLOCK(j);
933                                 }
934
935                                 PINC;
936                                 break;
937                         }
938
939
940                 /* load and store of object fields ************************************/
941
942                 case JAVA_AASTORE:
943                         OP(opcode);
944                         jd->isleafmethod = false;
945                         break;
946
947                 case JAVA_GETSTATIC:
948                 case JAVA_PUTSTATIC:
949                 case JAVA_GETFIELD:
950                 case JAVA_PUTFIELD:
951                         {
952                                 constant_FMIref  *fr;
953                                 unresolved_field *uf;
954
955                                 i = SUCK_BE_U2(m->jcode + p + 1);
956                                 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
957                                 if (!fr)
958                                         return false;
959
960                                 OP_PREPARE_ZEROFLAGS(opcode);
961                                 iptr->sx.s23.s3.fmiref = fr;
962
963                                 /* only with -noverify, otherwise the typechecker does this */
964
965 #if defined(ENABLE_VERIFIER)
966                                 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
967 #endif
968                                         result = new_resolve_field_lazy(iptr, m);
969                                         if (result == resolveFailed)
970                                                 return false;
971
972                                         if (result != resolveSucceeded) {
973                                                 uf = new_create_unresolved_field(m->class, m, iptr);
974
975                                                 if (uf == NULL)
976                                                         return false;
977
978                                                 /* store the unresolved_field pointer */
979
980                                                 iptr->sx.s23.s3.uf = uf;
981                                                 iptr->flags.bits = INS_FLAG_UNRESOLVED;
982                                         }
983 #if defined(ENABLE_VERIFIER)
984                                 }
985 #endif
986                                 PINC;
987                         }
988                         break;
989
990
991                 /* method invocation **************************************************/
992
993                 case JAVA_INVOKESTATIC:
994                         i = SUCK_BE_U2(m->jcode + p + 1);
995                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
996
997                         if (mr == NULL)
998                                 return false;
999
1000                         md = mr->parseddesc.md;
1001
1002                         if (md->params == NULL)
1003                                 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1004                                         return false;
1005
1006                         goto invoke_method;
1007
1008                 case JAVA_INVOKEINTERFACE:
1009                         i = SUCK_BE_U2(m->jcode + p + 1);
1010
1011                         mr = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1012
1013                         goto invoke_nonstatic_method;
1014
1015                 case JAVA_INVOKESPECIAL:
1016                 case JAVA_INVOKEVIRTUAL:
1017                         i = SUCK_BE_U2(m->jcode + p + 1);
1018                         mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1019
1020 invoke_nonstatic_method:
1021                         if (mr == NULL)
1022                                 return false;
1023
1024                         md = mr->parseddesc.md;
1025
1026                         if (md->params == NULL)
1027                                 if (!descriptor_params_from_paramtypes(md, 0))
1028                                         return false;
1029
1030 invoke_method:
1031                         jd->isleafmethod = false;
1032
1033                         OP_PREPARE_ZEROFLAGS(opcode);
1034                         iptr->sx.s23.s3.fmiref = mr;
1035
1036                         /* only with -noverify, otherwise the typechecker does this */
1037
1038 #if defined(ENABLE_VERIFIER)
1039                         if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1040 #endif
1041                                 result = new_resolve_method_lazy(iptr, m);
1042                                 if (result == resolveFailed)
1043                                         return false;
1044
1045                                 if (result != resolveSucceeded) {
1046                                         um = new_create_unresolved_method(m->class, m, iptr);
1047
1048                                         if (!um)
1049                                                 return false;
1050
1051                                         /* store the unresolved_method pointer */
1052
1053                                         iptr->sx.s23.s3.um = um;
1054                                         iptr->flags.bits = INS_FLAG_UNRESOLVED;
1055                                 }
1056 #if defined(ENABLE_VERIFIER)
1057                         }
1058 #endif
1059                         PINC;
1060                         break;
1061
1062                 /* instructions taking class arguments ********************************/
1063
1064                 case JAVA_NEW:
1065                         i = SUCK_BE_U2(m->jcode + p + 1);
1066                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1067                         if (!cr)
1068                                 return false;
1069
1070                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1071                                 return false;
1072
1073                         INSTRUCTIONS_CHECK(2);
1074                         OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1075                         bte = builtintable_get_internal(BUILTIN_new);
1076                         OP_BUILTIN_CHECK_EXCEPTION(bte);
1077                         s_count++;
1078                         break;
1079
1080                 case JAVA_CHECKCAST:
1081                         i = SUCK_BE_U2(m->jcode + p + 1);
1082                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1083                         if (cr == NULL)
1084                                 return false;
1085
1086                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1087                                 return false;
1088
1089                         if (cr->name->text[0] == '[') {
1090                                 /* array type cast-check */
1091                                 flags = INS_FLAG_ARRAY;
1092                                 jd->isleafmethod = false;
1093                         }
1094                         else {
1095                                 /* object type cast-check */
1096                                 flags = 0;
1097                         }
1098                         OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1099                         break;
1100
1101                 case JAVA_INSTANCEOF:
1102                         i = SUCK_BE_U2(m->jcode + p + 1);
1103                         cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1104                         if (cr == NULL)
1105                                 return false;
1106
1107                         if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1108                                 return false;
1109
1110                         if (cr->name->text[0] == '[') {
1111                                 /* array type cast-check */
1112                                 INSTRUCTIONS_CHECK(2);
1113                                 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1114                                 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1115                                 OP_BUILTIN_NO_EXCEPTION(bte);
1116                                 s_count++;
1117                         }
1118                         else {
1119                                 /* object type cast-check */
1120                                 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1121                         }
1122                         break;
1123
1124                 /* synchronization instructions ***************************************/
1125
1126                 case JAVA_MONITORENTER:
1127 #if defined(ENABLE_THREADS)
1128                         if (checksync) {
1129                                 bte = builtintable_get_internal(LOCK_monitor_enter);
1130                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1131                         }
1132                         else
1133 #endif
1134                         {
1135                                 OP(ICMD_CHECKNULL_POP);
1136                         }
1137                         break;
1138
1139                 case JAVA_MONITOREXIT:
1140 #if defined(ENABLE_THREADS)
1141                         if (checksync) {
1142                                 bte = builtintable_get_internal(LOCK_monitor_exit);
1143                                 OP_BUILTIN_CHECK_EXCEPTION(bte);
1144                         }
1145                         else
1146 #endif
1147                         {
1148                                 OP(ICMD_CHECKNULL_POP);
1149                         }
1150                         break;
1151
1152                 /* arithmetic instructions that may become builtin functions **********/
1153
1154                 case JAVA_IDIV:
1155 #if !SUPPORT_DIVISION
1156                         bte = builtintable_get_internal(BUILTIN_idiv);
1157                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1158 #else
1159                         OP(opcode);
1160 #endif
1161                         break;
1162
1163                 case JAVA_IREM:
1164 #if !SUPPORT_DIVISION
1165                         bte = builtintable_get_internal(BUILTIN_irem);
1166                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1167 #else
1168                         OP(opcode);
1169 #endif
1170                         break;
1171
1172                 case JAVA_LDIV:
1173 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1174                         bte = builtintable_get_internal(BUILTIN_ldiv);
1175                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1176 #else
1177                         OP(opcode);
1178 #endif
1179                         break;
1180
1181                 case JAVA_LREM:
1182 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1183                         bte = builtintable_get_internal(BUILTIN_lrem);
1184                         OP_BUILTIN_ARITHMETIC(opcode, bte);
1185 #else
1186                         OP(opcode);
1187 #endif
1188                         break;
1189
1190                 case JAVA_FREM:
1191 #if defined(__I386__)
1192                         OP(opcode);
1193 #else
1194                         bte = builtintable_get_internal(BUILTIN_frem);
1195                         OP_BUILTIN_NO_EXCEPTION(bte);
1196 #endif
1197                         break;
1198
1199                 case JAVA_DREM:
1200 #if defined(__I386__)
1201                         OP(opcode);
1202 #else
1203                         bte = builtintable_get_internal(BUILTIN_drem);
1204                         OP_BUILTIN_NO_EXCEPTION(bte);
1205 #endif
1206                         break;
1207
1208                 case JAVA_F2I:
1209 #if defined(__ALPHA__)
1210                         if (!opt_noieee) {
1211                                 bte = builtintable_get_internal(BUILTIN_f2i);
1212                                 OP_BUILTIN_NO_EXCEPTION(bte);
1213                         }
1214                         else
1215 #endif
1216                         {
1217                                 OP(opcode);
1218                         }
1219                         break;
1220
1221                 case JAVA_F2L:
1222 #if defined(__ALPHA__)
1223                         if (!opt_noieee) {
1224                                 bte = builtintable_get_internal(BUILTIN_f2l);
1225                                 OP_BUILTIN_NO_EXCEPTION(bte);
1226                         }
1227                         else
1228 #endif
1229                         {
1230                                 OP(opcode);
1231                         }
1232                         break;
1233
1234                 case JAVA_D2I:
1235 #if defined(__ALPHA__)
1236                         if (!opt_noieee) {
1237                                 bte = builtintable_get_internal(BUILTIN_d2i);
1238                                 OP_BUILTIN_NO_EXCEPTION(bte);
1239                         }
1240                         else
1241 #endif
1242                         {
1243                                 OP(opcode);
1244                         }
1245                         break;
1246
1247                 case JAVA_D2L:
1248 #if defined(__ALPHA__)
1249                         if (!opt_noieee) {
1250                                 bte = builtintable_get_internal(BUILTIN_d2l);
1251                                 OP_BUILTIN_NO_EXCEPTION(bte);
1252                         }
1253                         else
1254 #endif
1255                         {
1256                                 OP(opcode);
1257                         }
1258                         break;
1259
1260                 /* invalid opcodes ****************************************************/
1261
1262                         /* check for invalid opcodes if the verifier is enabled */
1263 #if defined(ENABLE_VERIFIER)
1264                 case JAVA_BREAKPOINT:
1265                         exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1266                         return false;
1267
1268                 case 186: /* unused opcode */
1269                 case 203:
1270                 case 204:
1271                 case 205:
1272                 case 206:
1273                 case 207:
1274                 case 208:
1275                 case 209:
1276                 case 210:
1277                 case 211:
1278                 case 212:
1279                 case 213:
1280                 case 214:
1281                 case 215:
1282                 case 216:
1283                 case 217:
1284                 case 218:
1285                 case 219:
1286                 case 220:
1287                 case 221:
1288                 case 222:
1289                 case 223:
1290                 case 224:
1291                 case 225:
1292                 case 226:
1293                 case 227:
1294                 case 228:
1295                 case 229:
1296                 case 230:
1297                 case 231:
1298                 case 232:
1299                 case 233:
1300                 case 234:
1301                 case 235:
1302                 case 236:
1303                 case 237:
1304                 case 238:
1305                 case 239:
1306                 case 240:
1307                 case 241:
1308                 case 242:
1309                 case 243:
1310                 case 244:
1311                 case 245:
1312                 case 246:
1313                 case 247:
1314                 case 248:
1315                 case 249:
1316                 case 250:
1317                 case 251:
1318                 case 252:
1319                 case 253:
1320                 case 254:
1321                 case 255:
1322                         exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1323                                                                                  opcode, ipc);
1324                         return false;
1325                         break;
1326 #endif /* defined(ENABLE_VERIFIER) */
1327
1328                 /* opcodes that don't require translation *****************************/
1329
1330                 default:
1331                         /* straight-forward translation to ICMD */
1332                         OP(opcode);
1333                         break;
1334
1335                 } /* end switch */
1336
1337                 /* verifier checks ****************************************************/
1338
1339 #if defined(ENABLE_VERIFIER)
1340                 /* If WIDE was used correctly, iswide should have been reset by now. */
1341                 if (iswide) {
1342                         exceptions_throw_verifyerror(m,
1343                                         "Illegal instruction: WIDE before incompatible opcode");
1344                         return false;
1345                 }
1346 #endif /* defined(ENABLE_VERIFIER) */
1347
1348         } /* end for */
1349
1350         /* add a NOP to the last basic block */
1351
1352         INSTRUCTIONS_CHECK(1);
1353         OP(ICMD_NOP);
1354
1355         /*** END OF LOOP **********************************************************/
1356
1357         /* assert that we did not write more ICMDs than allocated */
1358
1359         assert(ipc <= pd.instructionslength);
1360         assert(ipc == (iptr - pd.instructions));
1361
1362         /*** verifier checks ******************************************************/
1363
1364 #if defined(ENABLE_VERIFIER)
1365         if (p != m->jcodelength) {
1366                 exceptions_throw_verifyerror(m,
1367                                 "Command-sequence crosses code-boundary");
1368                 return false;
1369         }
1370
1371         if (!blockend) {
1372                 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1373                 return false;
1374         }
1375 #endif /* defined(ENABLE_VERIFIER) */
1376
1377         /*** setup the methodinfo, allocate stack and basic blocks ****************/
1378
1379         /* adjust block count if target 0 is not first intermediate instruction */
1380
1381         if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1))
1382                 b_count++;
1383
1384         /* copy local to method variables */
1385
1386         jd->new_instructions = pd.instructions;
1387         jd->new_instructioncount = ipc;
1388         jd->new_basicblockcount = b_count;
1389         jd->new_stackcount = s_count + jd->new_basicblockcount * m->maxstack; /* in-stacks */
1390
1391         /* allocate stack table */
1392
1393         jd->new_stack = DMNEW(stackelement, jd->new_stackcount);
1394
1395         /* build basic block list */
1396
1397         bptr = jd->new_basicblocks = DMNEW(basicblock, b_count + 1);    /* one more for end ipc */
1398
1399         /* zero out all basic block structures */
1400
1401         MZERO(bptr, basicblock, b_count + 1);
1402
1403         b_count = 0;
1404         jd->new_c_debug_nr = 0;
1405
1406         /* additional block if target 0 is not first intermediate instruction */
1407
1408         if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1)) {
1409                 BASICBLOCK_INIT(bptr, m);
1410
1411                 bptr->iinstr = jd->new_instructions;
1412                 /* bptr->icount is set when the next block is allocated */
1413
1414                 bptr++;
1415                 b_count++;
1416                 bptr[-1].next = bptr;
1417         }
1418
1419         /* allocate blocks */
1420
1421         for (p = 0; p < m->jcodelength; p++) {
1422                 if (jd->new_basicblockindex[p] & 1) {
1423 #if defined(ENABLE_VERIFIER)
1424                         /* Check if this block starts at the beginning of an
1425                            instruction. */
1426
1427                         if (!pd.instructionstart[p]) {
1428                                 exceptions_throw_verifyerror(m,
1429                                                 "Branch into middle of instruction");
1430                                 return false;
1431                         }
1432 #endif
1433
1434                         /* allocate the block */
1435
1436                         BASICBLOCK_INIT(bptr, m);
1437
1438                         bptr->iinstr = jd->new_instructions + (jd->new_basicblockindex[p] >> 1);
1439                         if (b_count) {
1440                                 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1441                         }
1442                         /* bptr->icount is set when the next block is allocated */
1443
1444                         jd->new_basicblockindex[p] = b_count;
1445
1446                         bptr++;
1447                         b_count++;
1448                         bptr[-1].next = bptr;
1449                 }
1450         }
1451
1452         /* set instruction count of last real block */
1453
1454         if (b_count) {
1455                 bptr[-1].icount = (jd->new_instructions + jd->new_instructioncount) - bptr[-1].iinstr;
1456         }
1457
1458         /* allocate additional block at end */
1459
1460         BASICBLOCK_INIT(bptr,m);
1461
1462         /* set basicblock pointers in exception table */
1463
1464         if (cd->exceptiontablelength > 0) {
1465                 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1466         }
1467
1468         for (i = 0; i < cd->exceptiontablelength; ++i) {
1469                 p = cd->exceptiontable[i].startpc;
1470                 cd->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1471
1472                 p = cd->exceptiontable[i].endpc;
1473                 cd->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1474
1475                 p = cd->exceptiontable[i].handlerpc;
1476                 cd->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1477         }
1478
1479         /* XXX activate this if you want to try inlining */
1480 #if 0
1481         for (i = 0; i < m->exceptiontablelength; ++i) {
1482                 p = m->exceptiontable[i].startpc;
1483                 m->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1484
1485                 p = m->exceptiontable[i].endpc;
1486                 m->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1487
1488                 p = m->exceptiontable[i].handlerpc;
1489                 m->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1490         }
1491 #endif
1492
1493 #if defined(NEW_VAR)
1494         jd->local_map = local_map;
1495
1496         /* calculate local variable renaming */
1497
1498         {
1499                 s4 nlocals = 0;
1500                 s4 i;
1501
1502                 s4 *mapptr;
1503
1504                 mapptr = local_map;
1505
1506                 /* iterate over local_map[0..m->maxlocals*5] and set all existing  */
1507                 /* index,type pairs (localmap[index*5+type]==1) to an unique value */
1508                 /* -> == new local var index */
1509                 for(i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1510                         if (*mapptr)
1511                                 *mapptr = nlocals++;
1512                         else
1513                                 *mapptr = LOCAL_UNUSED;
1514                 }
1515
1516                 jd->localcount = nlocals;
1517                 /* if dropped varindices for temp stackslots get reused(?max 2*       */
1518                 /* m->maxstack elements for stack), nlocals + s_count would be        */
1519                 /* sufficient */
1520                 jd->varcount   = nlocals + s_count + 
1521                         jd->new_basicblockcount * m->maxstack;        /* out-stacks */
1522                 
1523                 jd->var_top = nlocals;
1524                 jd->var = DMNEW(varinfo, jd->varcount);
1525         }
1526 #endif
1527
1528         /* everything's ok */
1529
1530         return true;
1531
1532         /*** goto labels for throwing verifier exceptions *************************/
1533
1534 #if defined(ENABLE_VERIFIER)
1535
1536 throw_unexpected_end_of_bytecode:
1537         exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1538         return false;
1539
1540 throw_invalid_bytecode_index:
1541         exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1542         return false;
1543
1544 throw_illegal_local_variable_number:
1545         exceptions_throw_verifyerror(m, "Illegal local variable number");
1546         return false;
1547
1548 #endif /* ENABLE_VERIFIER */
1549 }
1550
1551
1552 /*
1553  * These are local overrides for various environment variables in Emacs.
1554  * Please do not remove this and leave it at the end of the file, where
1555  * Emacs will automagically detect them.
1556  * ---------------------------------------------------------------------
1557  * Local variables:
1558  * mode: c
1559  * indent-tabs-mode: t
1560  * c-basic-offset: 4
1561  * tab-width: 4
1562  * End:
1563  * vim:noexpandtab:sw=4:ts=4:
1564  */