1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 #include "mm/memory.hpp"
35 #include "native/native.hpp"
37 #include "threads/lock.hpp"
39 #include "toolbox/logging.hpp"
41 #include "vm/jit/builtin.hpp"
42 #include "vm/exceptions.hpp"
43 #include "vm/global.h"
44 #include "vm/linker.hpp"
45 #include "vm/loader.hpp"
46 #include "vm/options.h"
47 #include "vm/resolve.hpp"
49 #if defined(ENABLE_STATISTICS)
50 # include "vm/statistics.h"
53 #include "vm/string.hpp"
54 #include "vm/suck.hpp"
56 #include "vm/jit/asmpart.h"
57 #include "vm/jit/jit.hpp"
58 #include "vm/jit/parse.hpp"
59 #include "vm/jit/loop/loop.h"
61 #include "vm/jit/ir/bytecode.h"
64 #define INSTRUCTIONS_INCREMENT 5 /* number of additional instructions to */
65 /* allocate if space runs out */
68 /* local macros ***************************************************************/
70 #define BYTECODEINDEX_TO_BASICBLOCK(dst) \
73 parse_bytecodeindex_to_basicblock(jd, &pd, (dst).insindex); \
77 /* parserdata_t ***************************************************************/
79 typedef struct parsedata_t parsedata_t;
82 u1 *bytecodestart; /* start of bytecode instructions */
83 u1 *basicblockstart; /* start of bytecode basic-blocks */
85 s4 *bytecodemap; /* bytecode to IR mapping */
87 instruction *instructions; /* instruction array */
88 s4 instructionslength; /* length of the instruction array */
90 s4 *instructionmap; /* IR to basic-block mapping */
93 #if defined(__cplusplus)
97 /* parse_setup *****************************************************************
99 Fills the passed parsedata_t structure.
101 *******************************************************************************/
103 static void parse_setup(jitdata *jd, parsedata_t *pd)
107 /* get required compiler data */
111 /* bytecode start array */
113 pd->bytecodestart = (u1*) DumpMemory::allocate(sizeof(u1) * (m->jcodelength + 1));
114 MZERO(pd->bytecodestart, u1, m->jcodelength + 1);
116 /* bytecode basic-block start array */
118 pd->basicblockstart = (u1*) DumpMemory::allocate(sizeof(u1) *(m->jcodelength + 1));
119 MZERO(pd->basicblockstart, u1, m->jcodelength + 1);
121 /* bytecode instruction index to IR instruction mapping */
123 pd->bytecodemap = (s4*) DumpMemory::allocate(sizeof(s4) * (m->jcodelength + 1));
124 MSET(pd->bytecodemap, -1, s4, m->jcodelength + 1);
126 /* allocate the instruction array */
128 pd->instructionslength = m->jcodelength + 1;
129 pd->instructions = (instruction*) DumpMemory::allocate(sizeof(instruction) * pd->instructionslength);
131 /* Zero the intermediate instructions array so we don't have any
132 invalid pointers in it if we cannot finish stack_analyse(). */
134 MZERO(pd->instructions, instruction, pd->instructionslength);
136 /* The instructionmap is allocated later when we know the count of
139 pd->instructionmap = NULL;
143 /* parse_realloc_instructions **************************************************
145 Reallocate the instructions array so there is room for at least N
146 additional instructions.
149 the new value for iptr
151 *******************************************************************************/
153 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 icount, s4 n)
155 /* increase the size of the instruction array */
157 pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
159 /* reallocate the array */
161 pd->instructions = (instruction*) DumpMemory::reallocate(pd->instructions, sizeof(instruction) * icount,
162 sizeof(instruction) * pd->instructionslength);
163 MZERO(pd->instructions + icount, instruction,
164 (pd->instructionslength - icount));
166 /* return the iptr */
168 return pd->instructions + icount;
172 /* parse_bytecodeindex_to_basicblock *******************************************
174 Resolves a bytecode index to the corresponding basic block.
176 *******************************************************************************/
178 static basicblock *parse_bytecodeindex_to_basicblock(jitdata *jd,
185 irindex = pd->bytecodemap[bcindex];
186 bb = jd->basicblocks + pd->instructionmap[irindex];
192 /* parse_mark_exception_boundaries *********************************************
194 Mark exception handlers and the boundaries of the handled regions as
195 basic block boundaries.
198 jd...............current jitdata
201 true.............everything ok
202 false............an exception has been thrown
204 *******************************************************************************/
206 static bool parse_mark_exception_boundaries(jitdata *jd, parsedata_t *pd)
211 raw_exception_entry *rex;
216 len = m->rawexceptiontablelength;
221 rex = m->rawexceptiontable;
223 for (i = 0; i < len; ++i, ++rex) {
225 /* the start of the handled region becomes a basic block start */
227 bcindex = rex->startpc;
228 CHECK_BYTECODE_INDEX(bcindex);
229 MARK_BASICBLOCK(pd, bcindex);
231 bcindex = rex->endpc; /* see JVM Spec 4.7.3 */
232 CHECK_BYTECODE_INDEX_EXCLUSIVE(bcindex);
234 /* check that the range is valid */
236 #if defined(ENABLE_VERIFIER)
237 if (bcindex <= rex->startpc) {
238 exceptions_throw_verifyerror(m, "Invalid exception handler range");
243 /* End of handled region becomes a basic block boundary (if it
244 is the bytecode end, we'll use the special end block that
245 is created anyway). */
247 if (bcindex < m->jcodelength)
248 MARK_BASICBLOCK(pd, bcindex);
250 jd->branchtoend = true;
252 /* the start of the handler becomes a basic block start */
254 bcindex = rex->handlerpc;
255 CHECK_BYTECODE_INDEX(bcindex);
256 MARK_BASICBLOCK(pd, bcindex);
263 #if defined(ENABLE_VERIFIER)
264 throw_invalid_bytecode_index:
265 exceptions_throw_verifyerror(m,
266 "Illegal bytecode index in exception table");
272 /* parse_resolve_exception_table ***********************************************
274 Enter the exception handlers and their ranges, resolved to basicblock *s,
278 jd...............current jitdata
281 true.............everything ok
282 false............an exception has been thrown
284 *******************************************************************************/
286 static bool parse_resolve_exception_table(jitdata *jd, parsedata_t *pd)
289 raw_exception_entry *rex;
297 len = m->rawexceptiontablelength;
299 /* common case: no handler entries */
304 /* allocate the exception table */
306 jd->exceptiontablelength = len;
307 jd->exceptiontable = (exception_entry*) DumpMemory::allocate(sizeof(exception_entry) * (len + 1)); /* XXX why +1? */
309 /* copy and resolve the entries */
311 ex = jd->exceptiontable;
312 rex = m->rawexceptiontable;
314 for (i = 0; i < len; ++i, ++rex, ++ex) {
315 /* resolve instruction indices to basic blocks */
317 ex->start = parse_bytecodeindex_to_basicblock(jd, pd, rex->startpc);
318 ex->end = parse_bytecodeindex_to_basicblock(jd, pd, rex->endpc);
319 ex->handler = parse_bytecodeindex_to_basicblock(jd, pd, rex->handlerpc);
321 /* lazily resolve the catchtype */
323 if (rex->catchtype.any != NULL) {
324 if (!resolve_classref_or_classinfo(m,
326 resolveLazy, true, false,
330 /* if resolved, enter the result of resolution in the table */
333 rex->catchtype.cls = exclass;
336 ex->catchtype = rex->catchtype;
337 ex->next = NULL; /* set by loop analysis */
338 ex->down = ex + 1; /* link to next exception entry */
341 /* terminate the ->down linked list */
343 assert(ex != jd->exceptiontable);
350 /*******************************************************************************
352 function 'parse' scans the JavaVM code and generates intermediate code
354 During parsing the block index table is used to store at bit pos 0
355 a flag which marks basic block starts and at position 1 to 31 the
356 intermediate instruction index. After parsing the block index table
357 is scanned, for marked positions a block is generated and the block
358 number is stored in the block index table.
360 *******************************************************************************/
362 /*** macro for checking the length of the bytecode ***/
364 #if defined(ENABLE_VERIFIER)
365 #define CHECK_END_OF_BYTECODE(neededlength) \
367 if ((neededlength) > m->jcodelength) \
368 goto throw_unexpected_end_of_bytecode; \
370 #else /* !ENABLE_VERIFIER */
371 #define CHECK_END_OF_BYTECODE(neededlength)
372 #endif /* ENABLE_VERIFIER */
374 bool parse(jitdata *jd)
376 methodinfo *m; /* method being parsed */
379 instruction *iptr; /* current ptr into instruction array */
381 s4 bcindex; /* bytecode instruction index */
382 s4 nextbc; /* start of next bytecode instruction */
383 s4 opcode; /* bytecode instruction opcode */
385 s4 irindex; /* IR instruction index */
386 s4 ircount; /* IR instruction count */
388 s4 bbcount; /* basic block count */
390 int s_count = 0; /* stack element counter */
391 bool blockend; /* true if basic block end has been reached */
392 bool iswide; /* true if last instruction was a wide */
394 constant_classref *cr;
395 constant_classref *compr;
397 builtintable_entry *bte;
398 constant_FMIref *fmi;
400 unresolved_method *um;
401 unresolved_field *uf;
403 resolve_result_t result;
410 int *local_map; /* local pointer to renaming map */
411 /* is assigned to rd->local_map at the end */
412 branch_target_t *table;
413 lookup_target_t *lookup;
417 /* get required compiler data */
422 /* allocate buffers for local variable renaming */
424 local_map = (int*) DumpMemory::allocate(sizeof(int) * m->maxlocals * 5);
426 for (i = 0; i < m->maxlocals; i++) {
427 local_map[i * 5 + 0] = 0;
428 local_map[i * 5 + 1] = 0;
429 local_map[i * 5 + 2] = 0;
430 local_map[i * 5 + 3] = 0;
431 local_map[i * 5 + 4] = 0;
434 /* initialize the parse data structures */
436 parse_setup(jd, &pd);
438 /* initialize local variables */
440 iptr = pd.instructions;
446 /* mark basic block boundaries for exception table */
448 if (!parse_mark_exception_boundaries(jd, &pd))
451 /* initialize stack element counter */
453 s_count = 1 + m->rawexceptiontablelength;
455 /* setup line number info */
460 if (m->linenumbercount == 0) {
464 linepcchange = m->linenumbers[0].start_pc;
467 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
469 for (bcindex = 0; bcindex < m->jcodelength; bcindex = nextbc) {
471 /* mark this position as a valid bytecode instruction start */
473 pd.bytecodestart[bcindex] = 1;
475 /* change the current line number, if necessary */
477 /* XXX rewrite this using pointer arithmetic */
479 if (linepcchange == bcindex) {
480 if (m->linenumbercount > lineindex) {
482 currentline = m->linenumbers[lineindex].line_number;
484 if (lineindex < m->linenumbercount) {
485 linepcchange = m->linenumbers[lineindex].start_pc;
486 if (linepcchange == bcindex)
487 goto next_linenumber;
493 /* fetch next opcode */
495 opcode = SUCK_BE_U1(m->jcode + bcindex);
497 /* If the previous instruction was a block-end instruction,
498 mark the current bytecode instruction as basic-block
499 starting instruction. */
501 /* NOTE: Some compilers put a BC_nop after a blockend
504 if (blockend && (opcode != BC_nop)) {
505 MARK_BASICBLOCK(&pd, bcindex);
509 /* If the current bytecode instruction was marked as
510 basic-block starting instruction before (e.g. blockend,
511 forward-branch target), mark the current IR instruction
514 if (pd.basicblockstart[bcindex] != 0) {
515 /* We need a NOP as last instruction in each basic block
516 for basic block reordering (may be replaced with a GOTO
519 INSTRUCTIONS_CHECK(1);
523 /* store intermediate instruction count (bit 0 mark block starts) */
525 pd.bytecodemap[bcindex] = ircount;
527 /* compute next instruction start */
529 nextbc = bcindex + bytecode[opcode].length;
531 CHECK_END_OF_BYTECODE(nextbc);
533 /* add stack elements produced by this instruction */
535 s_count += bytecode[opcode].slots;
537 /* Generate a breakpoint instruction right before the actual
538 instruction, if the method contains a breakpoint at the
539 current bytecode index. */
541 if (m->breakpoints != NULL && m->breakpoints->contains(bcindex)) {
542 INSTRUCTIONS_CHECK(1);
543 OP_PREPARE_ZEROFLAGS(ICMD_BREAKPOINT);
544 iptr->sx.val.anyptr = m->breakpoints->get_breakpoint(bcindex);
548 /* We check here for the space of 1 instruction in the
549 instruction array. If an opcode is converted to more than
550 1 instruction, this is checked in the corresponding
553 INSTRUCTIONS_CHECK(1);
555 /* translate this bytecode instruction */
561 /* pushing constants onto the stack ***********************************/
564 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
568 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
572 i = SUCK_BE_U1(m->jcode + bcindex + 1);
573 goto pushconstantitem;
577 i = SUCK_BE_U2(m->jcode + bcindex + 1);
581 #if defined(ENABLE_VERIFIER)
582 if (i >= m->clazz->cpcount) {
583 exceptions_throw_verifyerror(m,
584 "Attempt to access constant outside range");
589 switch (m->clazz->cptags[i]) {
590 case CONSTANT_Integer:
591 OP_LOADCONST_I(((constant_integer *) (m->clazz->cpinfos[i]))->value);
594 OP_LOADCONST_L(((constant_long *) (m->clazz->cpinfos[i]))->value);
597 OP_LOADCONST_F(((constant_float *) (m->clazz->cpinfos[i]))->value);
599 case CONSTANT_Double:
600 OP_LOADCONST_D(((constant_double *) (m->clazz->cpinfos[i]))->value);
602 case CONSTANT_String:
603 OP_LOADCONST_STRING(literalstring_new((utf *) (m->clazz->cpinfos[i])));
606 cr = (constant_classref *) (m->clazz->cpinfos[i]);
608 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
611 /* if not resolved, c == NULL */
613 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
617 #if defined(ENABLE_VERIFIER)
619 exceptions_throw_verifyerror(m,
620 "Invalid constant type to push");
637 OP_LOADCONST_I(opcode - BC_iconst_0);
642 OP_LOADCONST_L(opcode - BC_lconst_0);
648 OP_LOADCONST_F(opcode - BC_fconst_0);
653 OP_LOADCONST_D(opcode - BC_dconst_0);
656 /* stack operations ***************************************************/
658 /* We need space for additional instruction so we can
659 translate these instructions to sequences of ICMD_COPY and
660 ICMD_MOVE instructions. */
663 INSTRUCTIONS_CHECK(4);
671 INSTRUCTIONS_CHECK(6);
681 INSTRUCTIONS_CHECK(2);
687 INSTRUCTIONS_CHECK(7);
698 INSTRUCTIONS_CHECK(9);
711 INSTRUCTIONS_CHECK(3);
717 /* local variable access instructions *********************************/
722 if (iswide == false) {
723 i = SUCK_BE_U1(m->jcode + bcindex + 1);
726 i = SUCK_BE_U2(m->jcode + bcindex + 1);
727 nextbc = bcindex + 3;
730 OP_LOAD_ONEWORD(opcode, i, opcode - BC_iload);
735 if (iswide == false) {
736 i = SUCK_BE_U1(m->jcode + bcindex + 1);
739 i = SUCK_BE_U2(m->jcode + bcindex + 1);
740 nextbc = bcindex + 3;
743 OP_LOAD_TWOWORD(opcode, i, opcode - BC_iload);
750 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - BC_iload_0, TYPE_INT);
757 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - BC_lload_0, TYPE_LNG);
764 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - BC_fload_0, TYPE_FLT);
771 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - BC_dload_0, TYPE_DBL);
778 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - BC_aload_0, TYPE_ADR);
784 if (iswide == false) {
785 i = SUCK_BE_U1(m->jcode + bcindex + 1);
788 i = SUCK_BE_U2(m->jcode + bcindex + 1);
789 nextbc = bcindex + 3;
792 OP_STORE_ONEWORD(opcode, i, opcode - BC_istore);
797 if (iswide == false) {
798 i = SUCK_BE_U1(m->jcode + bcindex + 1);
801 i = SUCK_BE_U2(m->jcode + bcindex + 1);
802 nextbc = bcindex + 3;
805 OP_STORE_TWOWORD(opcode, i, opcode - BC_istore);
812 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - BC_istore_0, TYPE_INT);
819 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - BC_lstore_0, TYPE_LNG);
826 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - BC_fstore_0, TYPE_FLT);
833 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - BC_dstore_0, TYPE_DBL);
840 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - BC_astore_0, TYPE_ADR);
847 if (iswide == false) {
848 i = SUCK_BE_U1(m->jcode + bcindex + 1);
849 v = SUCK_BE_S1(m->jcode + bcindex + 2);
852 i = SUCK_BE_U2(m->jcode + bcindex + 1);
853 v = SUCK_BE_S2(m->jcode + bcindex + 3);
854 nextbc = bcindex + 5;
858 LOCALTYPE_USED(i, TYPE_INT);
859 OP_LOCALINDEX_I(opcode, i, v);
863 /* wider index for loading, storing and incrementing ******************/
870 /* managing arrays ****************************************************/
873 switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
875 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
878 bte = builtintable_get_internal(BUILTIN_newarray_char);
881 bte = builtintable_get_internal(BUILTIN_newarray_float);
884 bte = builtintable_get_internal(BUILTIN_newarray_double);
887 bte = builtintable_get_internal(BUILTIN_newarray_byte);
890 bte = builtintable_get_internal(BUILTIN_newarray_short);
893 bte = builtintable_get_internal(BUILTIN_newarray_int);
896 bte = builtintable_get_internal(BUILTIN_newarray_long);
898 #if defined(ENABLE_VERIFIER)
900 exceptions_throw_verifyerror(m, "Invalid array-type to create");
904 OP_BUILTIN_CHECK_EXCEPTION(bte);
908 i = SUCK_BE_U2(m->jcode + bcindex + 1);
909 compr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
913 if (!(cr = class_get_classref_multiarray_of(1, compr)))
916 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
919 INSTRUCTIONS_CHECK(2);
920 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
921 bte = builtintable_get_internal(BUILTIN_newarray);
922 OP_BUILTIN_CHECK_EXCEPTION(bte);
926 case BC_multianewarray:
927 i = SUCK_BE_U2(m->jcode + bcindex + 1);
928 j = SUCK_BE_U1(m->jcode + bcindex + 3);
930 cr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
934 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
937 /* if unresolved, c == NULL */
939 iptr->s1.argcount = j;
940 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
941 code_unflag_leafmethod(code);
944 /* control flow instructions ******************************************/
963 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
964 CHECK_BYTECODE_INDEX(i);
965 MARK_BASICBLOCK(&pd, i);
967 OP_INSINDEX(opcode, i);
971 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
972 CHECK_BYTECODE_INDEX(i);
973 MARK_BASICBLOCK(&pd, i);
975 OP_INSINDEX(ICMD_GOTO, i);
979 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
981 CHECK_BYTECODE_INDEX(i);
982 MARK_BASICBLOCK(&pd, i);
984 OP_PREPARE_ZEROFLAGS(BC_jsr);
985 iptr->sx.s23.s3.jsrtarget.insindex = i;
990 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
994 if (iswide == false) {
995 i = SUCK_BE_U1(m->jcode + bcindex + 1);
998 i = SUCK_BE_U2(m->jcode + bcindex + 1);
999 nextbc = bcindex + 3;
1004 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
1014 /* XXX ARETURN will need a flag in the typechecker */
1020 /* XXX ATHROW will need a flag in the typechecker */
1025 /* table jumps ********************************************************/
1027 case BC_lookupswitch:
1030 lookup_target_t *lookup;
1031 #if defined(ENABLE_VERIFIER)
1035 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1037 CHECK_END_OF_BYTECODE(nextbc + 8);
1039 OP_PREPARE_ZEROFLAGS(opcode);
1041 /* default target */
1043 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1044 iptr->sx.s23.s3.lookupdefault.insindex = j;
1046 CHECK_BYTECODE_INDEX(j);
1047 MARK_BASICBLOCK(&pd, j);
1049 /* number of pairs */
1051 num = SUCK_BE_U4(m->jcode + nextbc);
1052 iptr->sx.s23.s2.lookupcount = num;
1055 /* allocate the intermediate code table */
1057 lookup = (lookup_target_t*) DumpMemory::allocate(sizeof(lookup_target_t) * num);
1058 iptr->dst.lookup = lookup;
1060 /* iterate over the lookup table */
1062 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1064 for (i = 0; i < num; i++) {
1067 j = SUCK_BE_S4(m->jcode + nextbc);
1072 #if defined(ENABLE_VERIFIER)
1073 /* check if the lookup table is sorted correctly */
1075 if (i && (j <= prevvalue)) {
1076 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1083 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1084 lookup->target.insindex = j;
1087 CHECK_BYTECODE_INDEX(j);
1088 MARK_BASICBLOCK(&pd, j);
1095 case BC_tableswitch:
1099 branch_target_t *table;
1102 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1104 CHECK_END_OF_BYTECODE(nextbc + 12);
1106 OP_PREPARE_ZEROFLAGS(opcode);
1108 /* default target */
1110 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1112 CHECK_BYTECODE_INDEX(deftarget);
1113 MARK_BASICBLOCK(&pd, deftarget);
1117 j = SUCK_BE_S4(m->jcode + nextbc);
1118 iptr->sx.s23.s2.tablelow = j;
1123 num = SUCK_BE_S4(m->jcode + nextbc);
1124 iptr->sx.s23.s3.tablehigh = num;
1127 /* calculate the number of table entries */
1131 #if defined(ENABLE_VERIFIER)
1133 exceptions_throw_verifyerror(m,
1134 "invalid TABLESWITCH: upper bound < lower bound");
1138 /* create the intermediate code table */
1139 /* the first entry is the default target */
1141 table = (branch_target_t*) DumpMemory::allocate(sizeof(branch_target_t) * (1 + num));
1142 iptr->dst.table = table;
1143 (table++)->insindex = deftarget;
1145 /* iterate over the target table */
1147 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1149 for (i = 0; i < num; i++) {
1150 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1151 (table++)->insindex = j;
1153 CHECK_BYTECODE_INDEX(j);
1154 MARK_BASICBLOCK(&pd, j);
1162 /* load and store of object fields ************************************/
1166 code_unflag_leafmethod(code);
1173 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1174 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Fieldref);
1179 OP_PREPARE_ZEROFLAGS(opcode);
1180 iptr->sx.s23.s3.fmiref = fmi;
1182 /* only with -noverify, otherwise the typechecker does this */
1184 #if defined(ENABLE_VERIFIER)
1185 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1187 result = resolve_field_lazy(m, fmi);
1189 if (result == resolveFailed)
1192 if (result != resolveSucceeded) {
1193 uf = resolve_create_unresolved_field(m->clazz, m, iptr);
1198 /* store the unresolved_field pointer */
1200 iptr->sx.s23.s3.uf = uf;
1201 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1203 #if defined(ENABLE_VERIFIER)
1210 /* method invocation **************************************************/
1212 case BC_invokestatic:
1213 OP_PREPARE_ZEROFLAGS(opcode);
1215 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1216 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1221 md = fmi->parseddesc.md;
1223 if (md->params == NULL)
1224 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1229 case BC_invokespecial:
1230 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1232 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1233 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1235 goto invoke_nonstatic_method;
1237 case BC_invokeinterface:
1238 OP_PREPARE_ZEROFLAGS(opcode);
1240 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1241 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_InterfaceMethodref);
1243 goto invoke_nonstatic_method;
1245 case BC_invokevirtual:
1246 OP_PREPARE_ZEROFLAGS(opcode);
1248 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1249 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1251 invoke_nonstatic_method:
1255 md = fmi->parseddesc.md;
1257 if (md->params == NULL)
1258 if (!descriptor_params_from_paramtypes(md, 0))
1262 code_unflag_leafmethod(code);
1264 iptr->sx.s23.s3.fmiref = fmi;
1266 /* only with -noverify, otherwise the typechecker does this */
1268 #if defined(ENABLE_VERIFIER)
1269 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1271 result = resolve_method_lazy(m, fmi,
1272 (opcode == BC_invokespecial));
1274 if (result == resolveFailed)
1277 if (result == resolveSucceeded) {
1278 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1280 /* if this call is monomorphic, turn it into an
1283 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1285 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1286 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1288 iptr->opc = ICMD_INVOKESPECIAL;
1289 iptr->flags.bits |= INS_FLAG_CHECK;
1293 um = resolve_create_unresolved_method(m->clazz, m, fmi,
1294 (opcode == BC_invokestatic),
1295 (opcode == BC_invokespecial));
1300 /* store the unresolved_method pointer */
1302 iptr->sx.s23.s3.um = um;
1303 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1305 #if defined(ENABLE_VERIFIER)
1311 /* instructions taking class arguments ********************************/
1314 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1315 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1320 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1323 INSTRUCTIONS_CHECK(2);
1324 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1325 bte = builtintable_get_internal(BUILTIN_new);
1326 OP_BUILTIN_CHECK_EXCEPTION(bte);
1331 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1332 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1337 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1340 if (cr->name->text[0] == '[') {
1341 /* array type cast-check */
1342 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1343 code_unflag_leafmethod(code);
1346 /* object type cast-check */
1347 flags = INS_FLAG_CHECK;
1349 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1353 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1354 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1359 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1362 if (cr->name->text[0] == '[') {
1363 /* array type cast-check */
1364 INSTRUCTIONS_CHECK(2);
1365 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1366 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1367 OP_BUILTIN_NO_EXCEPTION(bte);
1371 /* object type cast-check */
1372 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1376 /* synchronization instructions ***************************************/
1378 case BC_monitorenter:
1379 #if defined(ENABLE_THREADS)
1381 bte = builtintable_get_internal(LOCK_monitor_enter);
1382 OP_BUILTIN_CHECK_EXCEPTION(bte);
1387 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1392 case BC_monitorexit:
1393 #if defined(ENABLE_THREADS)
1395 bte = builtintable_get_internal(LOCK_monitor_exit);
1396 OP_BUILTIN_CHECK_EXCEPTION(bte);
1401 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1406 /* arithmetic instructions that may become builtin functions **********/
1409 #if !SUPPORT_DIVISION
1410 bte = builtintable_get_internal(BUILTIN_idiv);
1411 OP_BUILTIN_ARITHMETIC(opcode, bte);
1413 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1416 OP_CHECK_EXCEPTION(opcode);
1422 #if !SUPPORT_DIVISION
1423 bte = builtintable_get_internal(BUILTIN_irem);
1424 OP_BUILTIN_ARITHMETIC(opcode, bte);
1426 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1429 OP_CHECK_EXCEPTION(opcode);
1435 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1436 bte = builtintable_get_internal(BUILTIN_ldiv);
1437 OP_BUILTIN_ARITHMETIC(opcode, bte);
1439 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1442 OP_CHECK_EXCEPTION(opcode);
1448 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1449 bte = builtintable_get_internal(BUILTIN_lrem);
1450 OP_BUILTIN_ARITHMETIC(opcode, bte);
1452 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1455 OP_CHECK_EXCEPTION(opcode);
1461 #if defined(__I386__)
1464 bte = builtintable_get_internal(BUILTIN_frem);
1465 OP_BUILTIN_NO_EXCEPTION(bte);
1470 #if defined(__I386__)
1473 bte = builtintable_get_internal(BUILTIN_drem);
1474 OP_BUILTIN_NO_EXCEPTION(bte);
1479 #if defined(__ALPHA__)
1480 bte = builtintable_get_internal(BUILTIN_f2i);
1481 OP_BUILTIN_NO_EXCEPTION(bte);
1488 #if defined(__ALPHA__)
1489 bte = builtintable_get_internal(BUILTIN_f2l);
1490 OP_BUILTIN_NO_EXCEPTION(bte);
1497 #if defined(__ALPHA__)
1498 bte = builtintable_get_internal(BUILTIN_d2i);
1499 OP_BUILTIN_NO_EXCEPTION(bte);
1506 #if defined(__ALPHA__)
1507 bte = builtintable_get_internal(BUILTIN_d2l);
1508 OP_BUILTIN_NO_EXCEPTION(bte);
1515 /* invalid opcodes ****************************************************/
1517 /* check for invalid opcodes if the verifier is enabled */
1518 #if defined(ENABLE_VERIFIER)
1520 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1524 /* Unused opcodes ************************************************** */
1580 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1584 #endif /* defined(ENABLE_VERIFIER) */
1586 /* opcodes that don't require translation *****************************/
1589 /* Straight-forward translation to HIR. */
1595 /* verifier checks ****************************************************/
1597 #if defined(ENABLE_VERIFIER)
1598 /* If WIDE was used correctly, iswide should have been reset by now. */
1600 exceptions_throw_verifyerror(m,
1601 "Illegal instruction: WIDE before incompatible opcode");
1604 #endif /* defined(ENABLE_VERIFIER) */
1608 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1609 /* add a NOP to the last basic block */
1611 INSTRUCTIONS_CHECK(1);
1615 /*** END OF LOOP **********************************************************/
1617 /* assert that we did not write more ICMDs than allocated */
1619 assert(ircount <= pd.instructionslength);
1620 assert(ircount == (iptr - pd.instructions));
1622 /*** verifier checks ******************************************************/
1624 #if defined(ENABLE_VERIFIER)
1625 if (bcindex != m->jcodelength) {
1626 exceptions_throw_verifyerror(m,
1627 "Command-sequence crosses code-boundary");
1632 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1635 #endif /* defined(ENABLE_VERIFIER) */
1637 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1639 /* identify basic blocks */
1641 /* check if first instruction is a branch target */
1643 if (pd.basicblockstart[0] == 1) {
1644 jd->branchtoentry = true;
1647 /* first instruction always starts a basic block */
1649 iptr = pd.instructions;
1651 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1654 /* Iterate over all bytecode instructions and set missing
1655 basic-block starts in IR instructions. */
1657 for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1658 /* Does the current bytecode instruction start a basic
1661 if (pd.basicblockstart[bcindex] == 1) {
1662 #if defined(ENABLE_VERIFIER)
1663 /* Check if this bytecode basic-block start at the
1664 beginning of a bytecode instruction. */
1666 if (pd.bytecodestart[bcindex] == 0) {
1667 exceptions_throw_verifyerror(m,
1668 "Branch into middle of instruction");
1673 /* Get the IR instruction mapped to the bytecode
1674 instruction and set the basic block flag. */
1676 irindex = pd.bytecodemap[bcindex];
1677 iptr = pd.instructions + irindex;
1679 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1683 /* IR instruction index to basic-block index mapping */
1685 pd.instructionmap = (s4*) DumpMemory::allocate(sizeof(s4) * ircount);
1686 MZERO(pd.instructionmap, s4, ircount);
1688 /* Iterate over all IR instructions and count the basic blocks. */
1690 iptr = pd.instructions;
1694 for (i = 0; i < ircount; i++, iptr++) {
1695 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1696 /* store the basic-block number in the IR instruction
1699 pd.instructionmap[i] = bbcount;
1701 /* post-increment the basic-block count */
1707 /* Allocate basic block array (one more for end ipc). */
1709 jd->basicblocks = (basicblock*) DumpMemory::allocate(sizeof(basicblock) * (bbcount + 1));
1710 MZERO(jd->basicblocks, basicblock, bbcount + 1);
1712 /* Now iterate again over all IR instructions and initialize the
1713 basic block structures and, in the same loop, resolve the
1714 branch-target instruction indices to basic blocks. */
1716 iptr = pd.instructions;
1717 bptr = jd->basicblocks;
1721 for (i = 0; i < ircount; i++, iptr++) {
1722 /* check for basic block */
1724 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1725 /* intialize the basic block */
1727 BASICBLOCK_INIT(bptr, m);
1729 bptr->iinstr = iptr;
1732 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1735 /* bptr->icount is set when the next block is allocated */
1737 bptr->nr = bbcount++;
1739 bptr[-1].next = bptr;
1742 /* resolve instruction indices to basic blocks */
1744 switch (iptr->opc) {
1752 case ICMD_IFNONNULL:
1753 case ICMD_IF_ICMPEQ:
1754 case ICMD_IF_ICMPNE:
1755 case ICMD_IF_ICMPLT:
1756 case ICMD_IF_ICMPGT:
1757 case ICMD_IF_ICMPLE:
1758 case ICMD_IF_ICMPGE:
1759 case ICMD_IF_ACMPEQ:
1760 case ICMD_IF_ACMPNE:
1762 BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1766 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1769 case ICMD_TABLESWITCH:
1770 table = iptr->dst.table;
1772 BYTECODEINDEX_TO_BASICBLOCK(*table);
1775 j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1778 BYTECODEINDEX_TO_BASICBLOCK(*table);
1783 case ICMD_LOOKUPSWITCH:
1784 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1786 lookup = iptr->dst.lookup;
1788 j = iptr->sx.s23.s2.lookupcount;
1791 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1798 /* set instruction count of last real block */
1801 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1804 /* allocate additional block at end */
1806 BASICBLOCK_INIT(bptr, m);
1809 /* set basicblock pointers in exception table */
1811 if (!parse_resolve_exception_table(jd, &pd))
1814 /* store the local map */
1816 jd->local_map = local_map;
1818 /* calculate local variable renaming */
1830 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1831 /* variable index for each _used_ (javaindex,type) pair. */
1832 /* (local_map[javaindex*5+type] = cacaoindex) */
1833 /* Unused (javaindex,type) pairs are marked with UNUSED. */
1835 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1837 *mapptr = nlocals++;
1842 jd->localcount = nlocals;
1844 /* calculate the (maximum) number of variables needed */
1847 nlocals /* local variables */
1848 + bbcount * m->maxstack /* invars */
1849 + s_count; /* variables created within blocks (non-invar) */
1851 /* reserve the first indices for local variables */
1853 jd->vartop = nlocals;
1855 /* reserve extra variables needed by stack analyse */
1857 jd->varcount += STACK_EXTRA_VARS;
1858 jd->vartop += STACK_EXTRA_VARS;
1860 /* The verifier needs space for saving invars in some cases and */
1861 /* extra variables. */
1863 #if defined(ENABLE_VERIFIER)
1864 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1865 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1867 /* allocate and initialize the variable array */
1869 jd->var = (varinfo*) DumpMemory::allocate(sizeof(varinfo) * jd->varcount);
1870 MZERO(jd->var, varinfo, jd->varcount);
1872 /* set types of all locals in jd->var */
1873 /* and fill the reverselocalmap */
1875 reversemap = (s4*) DumpMemory::allocate(sizeof(s4) * nlocals);
1877 for (i = 0; i < m->maxlocals; i++)
1878 for (t=0; t<5; t++) {
1879 varindex = local_map[5*i + t];
1880 if (varindex != UNUSED) {
1881 VAR(varindex)->type = t;
1882 reversemap[varindex] = i;
1886 jd->reverselocalmap = reversemap;
1889 /* assign local variables to method variables */
1891 jd->instructions = pd.instructions;
1892 jd->instructioncount = ircount;
1893 jd->basicblockcount = bbcount;
1894 jd->stackcount = s_count + bbcount * m->maxstack; /* in-stacks */
1896 /* allocate stack table */
1898 jd->stack = (stackelement_t*) DumpMemory::allocate(sizeof(stackelement_t) * jd->stackcount);
1900 /* everything's ok */
1904 /*** goto labels for throwing verifier exceptions *************************/
1906 #if defined(ENABLE_VERIFIER)
1908 throw_unexpected_end_of_bytecode:
1909 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1912 throw_invalid_bytecode_index:
1913 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1916 throw_illegal_local_variable_number:
1917 exceptions_throw_verifyerror(m, "Illegal local variable number");
1920 #endif /* ENABLE_VERIFIER */
1923 #if defined(__cplusplus)
1928 * These are local overrides for various environment variables in Emacs.
1929 * Please do not remove this and leave it at the end of the file, where
1930 * Emacs will automagically detect them.
1931 * ---------------------------------------------------------------------
1934 * indent-tabs-mode: t
1938 * vim:noexpandtab:sw=4:ts=4: