1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2011
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 descriptor_params_from_paramtypes(md, ACC_STATIC);
1227 case BC_invokespecial:
1228 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1230 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1231 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1233 goto invoke_nonstatic_method;
1235 case BC_invokeinterface:
1236 OP_PREPARE_ZEROFLAGS(opcode);
1238 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1239 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_InterfaceMethodref);
1241 goto invoke_nonstatic_method;
1243 case BC_invokevirtual:
1244 OP_PREPARE_ZEROFLAGS(opcode);
1246 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1247 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1249 invoke_nonstatic_method:
1253 md = fmi->parseddesc.md;
1255 descriptor_params_from_paramtypes(md, 0);
1258 code_unflag_leafmethod(code);
1260 iptr->sx.s23.s3.fmiref = fmi;
1262 /* only with -noverify, otherwise the typechecker does this */
1264 #if defined(ENABLE_VERIFIER)
1265 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1267 result = resolve_method_lazy(m, fmi,
1268 (opcode == BC_invokespecial));
1270 if (result == resolveFailed)
1273 if (result == resolveSucceeded) {
1274 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1276 /* if this call is monomorphic, turn it into an
1279 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1281 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1282 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1284 iptr->opc = ICMD_INVOKESPECIAL;
1285 iptr->flags.bits |= INS_FLAG_CHECK;
1289 um = resolve_create_unresolved_method(m->clazz, m, fmi,
1290 (opcode == BC_invokestatic),
1291 (opcode == BC_invokespecial));
1293 /* store the unresolved_method pointer */
1295 iptr->sx.s23.s3.um = um;
1296 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1298 #if defined(ENABLE_VERIFIER)
1304 /* instructions taking class arguments ********************************/
1307 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1308 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1313 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1316 INSTRUCTIONS_CHECK(2);
1317 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1318 bte = builtintable_get_internal(BUILTIN_new);
1319 OP_BUILTIN_CHECK_EXCEPTION(bte);
1324 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1325 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1330 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1333 if (cr->name->text[0] == '[') {
1334 /* array type cast-check */
1335 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1336 code_unflag_leafmethod(code);
1339 /* object type cast-check */
1340 flags = INS_FLAG_CHECK;
1342 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1346 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1347 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1352 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1355 if (cr->name->text[0] == '[') {
1356 /* array type cast-check */
1357 INSTRUCTIONS_CHECK(2);
1358 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1359 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1360 OP_BUILTIN_NO_EXCEPTION(bte);
1364 /* object type cast-check */
1365 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1369 /* synchronization instructions ***************************************/
1371 case BC_monitorenter:
1372 #if defined(ENABLE_THREADS)
1374 bte = builtintable_get_internal(LOCK_monitor_enter);
1375 OP_BUILTIN_CHECK_EXCEPTION(bte);
1380 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1385 case BC_monitorexit:
1386 #if defined(ENABLE_THREADS)
1388 bte = builtintable_get_internal(LOCK_monitor_exit);
1389 OP_BUILTIN_CHECK_EXCEPTION(bte);
1394 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1399 /* arithmetic instructions that may become builtin functions **********/
1402 #if !SUPPORT_DIVISION
1403 bte = builtintable_get_internal(BUILTIN_idiv);
1404 OP_BUILTIN_ARITHMETIC(opcode, bte);
1406 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1409 OP_CHECK_EXCEPTION(opcode);
1415 #if !SUPPORT_DIVISION
1416 bte = builtintable_get_internal(BUILTIN_irem);
1417 OP_BUILTIN_ARITHMETIC(opcode, bte);
1419 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1422 OP_CHECK_EXCEPTION(opcode);
1428 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1429 bte = builtintable_get_internal(BUILTIN_ldiv);
1430 OP_BUILTIN_ARITHMETIC(opcode, bte);
1432 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1435 OP_CHECK_EXCEPTION(opcode);
1441 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1442 bte = builtintable_get_internal(BUILTIN_lrem);
1443 OP_BUILTIN_ARITHMETIC(opcode, bte);
1445 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1448 OP_CHECK_EXCEPTION(opcode);
1454 #if defined(__I386__)
1457 bte = builtintable_get_internal(BUILTIN_frem);
1458 OP_BUILTIN_NO_EXCEPTION(bte);
1463 #if defined(__I386__)
1466 bte = builtintable_get_internal(BUILTIN_drem);
1467 OP_BUILTIN_NO_EXCEPTION(bte);
1472 #if defined(__ALPHA__)
1473 bte = builtintable_get_internal(BUILTIN_f2i);
1474 OP_BUILTIN_NO_EXCEPTION(bte);
1481 #if defined(__ALPHA__)
1482 bte = builtintable_get_internal(BUILTIN_f2l);
1483 OP_BUILTIN_NO_EXCEPTION(bte);
1490 #if defined(__ALPHA__)
1491 bte = builtintable_get_internal(BUILTIN_d2i);
1492 OP_BUILTIN_NO_EXCEPTION(bte);
1499 #if defined(__ALPHA__)
1500 bte = builtintable_get_internal(BUILTIN_d2l);
1501 OP_BUILTIN_NO_EXCEPTION(bte);
1508 /* invalid opcodes ****************************************************/
1510 /* check for invalid opcodes if the verifier is enabled */
1511 #if defined(ENABLE_VERIFIER)
1513 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1517 /* Unused opcodes ************************************************** */
1573 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1577 #endif /* defined(ENABLE_VERIFIER) */
1579 /* opcodes that don't require translation *****************************/
1582 /* Straight-forward translation to HIR. */
1588 /* verifier checks ****************************************************/
1590 #if defined(ENABLE_VERIFIER)
1591 /* If WIDE was used correctly, iswide should have been reset by now. */
1593 exceptions_throw_verifyerror(m,
1594 "Illegal instruction: WIDE before incompatible opcode");
1597 #endif /* defined(ENABLE_VERIFIER) */
1601 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1602 /* add a NOP to the last basic block */
1604 INSTRUCTIONS_CHECK(1);
1608 /*** END OF LOOP **********************************************************/
1610 /* assert that we did not write more ICMDs than allocated */
1612 assert(ircount <= pd.instructionslength);
1613 assert(ircount == (iptr - pd.instructions));
1615 /*** verifier checks ******************************************************/
1617 #if defined(ENABLE_VERIFIER)
1618 if (bcindex != m->jcodelength) {
1619 exceptions_throw_verifyerror(m,
1620 "Command-sequence crosses code-boundary");
1625 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1628 #endif /* defined(ENABLE_VERIFIER) */
1630 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1632 /* identify basic blocks */
1634 /* check if first instruction is a branch target */
1636 if (pd.basicblockstart[0] == 1) {
1637 jd->branchtoentry = true;
1640 /* first instruction always starts a basic block */
1642 iptr = pd.instructions;
1644 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1647 /* Iterate over all bytecode instructions and set missing
1648 basic-block starts in IR instructions. */
1650 for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1651 /* Does the current bytecode instruction start a basic
1654 if (pd.basicblockstart[bcindex] == 1) {
1655 #if defined(ENABLE_VERIFIER)
1656 /* Check if this bytecode basic-block start at the
1657 beginning of a bytecode instruction. */
1659 if (pd.bytecodestart[bcindex] == 0) {
1660 exceptions_throw_verifyerror(m,
1661 "Branch into middle of instruction");
1666 /* Get the IR instruction mapped to the bytecode
1667 instruction and set the basic block flag. */
1669 irindex = pd.bytecodemap[bcindex];
1670 iptr = pd.instructions + irindex;
1672 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1676 /* IR instruction index to basic-block index mapping */
1678 pd.instructionmap = (s4*) DumpMemory::allocate(sizeof(s4) * ircount);
1679 MZERO(pd.instructionmap, s4, ircount);
1681 /* Iterate over all IR instructions and count the basic blocks. */
1683 iptr = pd.instructions;
1687 for (i = 0; i < ircount; i++, iptr++) {
1688 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1689 /* store the basic-block number in the IR instruction
1692 pd.instructionmap[i] = bbcount;
1694 /* post-increment the basic-block count */
1700 /* Allocate basic block array (one more for end ipc). */
1702 jd->basicblocks = (basicblock*) DumpMemory::allocate(sizeof(basicblock) * (bbcount + 1));
1703 MZERO(jd->basicblocks, basicblock, bbcount + 1);
1705 /* Now iterate again over all IR instructions and initialize the
1706 basic block structures and, in the same loop, resolve the
1707 branch-target instruction indices to basic blocks. */
1709 iptr = pd.instructions;
1710 bptr = jd->basicblocks;
1714 for (i = 0; i < ircount; i++, iptr++) {
1715 /* check for basic block */
1717 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1718 /* intialize the basic block */
1720 BASICBLOCK_INIT(bptr, m);
1722 bptr->iinstr = iptr;
1725 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1728 /* bptr->icount is set when the next block is allocated */
1730 bptr->nr = bbcount++;
1732 bptr[-1].next = bptr;
1735 /* resolve instruction indices to basic blocks */
1737 switch (iptr->opc) {
1745 case ICMD_IFNONNULL:
1746 case ICMD_IF_ICMPEQ:
1747 case ICMD_IF_ICMPNE:
1748 case ICMD_IF_ICMPLT:
1749 case ICMD_IF_ICMPGT:
1750 case ICMD_IF_ICMPLE:
1751 case ICMD_IF_ICMPGE:
1752 case ICMD_IF_ACMPEQ:
1753 case ICMD_IF_ACMPNE:
1755 BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1759 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1762 case ICMD_TABLESWITCH:
1763 table = iptr->dst.table;
1765 BYTECODEINDEX_TO_BASICBLOCK(*table);
1768 j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1771 BYTECODEINDEX_TO_BASICBLOCK(*table);
1776 case ICMD_LOOKUPSWITCH:
1777 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1779 lookup = iptr->dst.lookup;
1781 j = iptr->sx.s23.s2.lookupcount;
1784 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1791 /* set instruction count of last real block */
1794 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1797 /* allocate additional block at end */
1799 BASICBLOCK_INIT(bptr, m);
1802 /* set basicblock pointers in exception table */
1804 if (!parse_resolve_exception_table(jd, &pd))
1807 /* store the local map */
1809 jd->local_map = local_map;
1811 /* calculate local variable renaming */
1823 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1824 /* variable index for each _used_ (javaindex,type) pair. */
1825 /* (local_map[javaindex*5+type] = cacaoindex) */
1826 /* Unused (javaindex,type) pairs are marked with UNUSED. */
1828 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1830 *mapptr = nlocals++;
1835 jd->localcount = nlocals;
1837 /* calculate the (maximum) number of variables needed */
1840 nlocals /* local variables */
1841 + bbcount * m->maxstack /* invars */
1842 + s_count; /* variables created within blocks (non-invar) */
1844 /* reserve the first indices for local variables */
1846 jd->vartop = nlocals;
1848 /* reserve extra variables needed by stack analyse */
1850 jd->varcount += STACK_EXTRA_VARS;
1851 jd->vartop += STACK_EXTRA_VARS;
1853 /* The verifier needs space for saving invars in some cases and */
1854 /* extra variables. */
1856 #if defined(ENABLE_VERIFIER)
1857 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1858 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1860 /* allocate and initialize the variable array */
1862 jd->var = (varinfo*) DumpMemory::allocate(sizeof(varinfo) * jd->varcount);
1863 MZERO(jd->var, varinfo, jd->varcount);
1865 /* set types of all locals in jd->var */
1866 /* and fill the reverselocalmap */
1868 reversemap = (s4*) DumpMemory::allocate(sizeof(s4) * nlocals);
1870 for (i = 0; i < m->maxlocals; i++)
1871 for (t=0; t<5; t++) {
1872 varindex = local_map[5*i + t];
1873 if (varindex != UNUSED) {
1874 VAR(varindex)->type = t;
1875 reversemap[varindex] = i;
1879 jd->reverselocalmap = reversemap;
1882 /* assign local variables to method variables */
1884 jd->instructions = pd.instructions;
1885 jd->instructioncount = ircount;
1886 jd->basicblockcount = bbcount;
1887 jd->stackcount = s_count + bbcount * m->maxstack; /* in-stacks */
1889 /* allocate stack table */
1891 jd->stack = (stackelement_t*) DumpMemory::allocate(sizeof(stackelement_t) * jd->stackcount);
1893 /* everything's ok */
1897 /*** goto labels for throwing verifier exceptions *************************/
1899 #if defined(ENABLE_VERIFIER)
1901 throw_unexpected_end_of_bytecode:
1902 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1905 throw_invalid_bytecode_index:
1906 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1909 throw_illegal_local_variable_number:
1910 exceptions_throw_verifyerror(m, "Illegal local variable number");
1913 #endif /* ENABLE_VERIFIER */
1916 #if defined(__cplusplus)
1921 * These are local overrides for various environment variables in Emacs.
1922 * Please do not remove this and leave it at the end of the file, where
1923 * Emacs will automagically detect them.
1924 * ---------------------------------------------------------------------
1927 * indent-tabs-mode: t
1931 * vim:noexpandtab:sw=4:ts=4: