1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005, 2006, 2007, 2008
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.h"
35 #include "native/native.hpp"
37 #include "threads/lock.hpp"
39 #include "toolbox/logging.h"
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 /* We check here for the space of 1 instruction in the
538 instruction array. If an opcode is converted to more than
539 1 instruction, this is checked in the corresponding
542 INSTRUCTIONS_CHECK(1);
544 /* translate this bytecode instruction */
550 /* pushing constants onto the stack ***********************************/
553 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
557 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
561 i = SUCK_BE_U1(m->jcode + bcindex + 1);
562 goto pushconstantitem;
566 i = SUCK_BE_U2(m->jcode + bcindex + 1);
570 #if defined(ENABLE_VERIFIER)
571 if (i >= m->clazz->cpcount) {
572 exceptions_throw_verifyerror(m,
573 "Attempt to access constant outside range");
578 switch (m->clazz->cptags[i]) {
579 case CONSTANT_Integer:
580 OP_LOADCONST_I(((constant_integer *) (m->clazz->cpinfos[i]))->value);
583 OP_LOADCONST_L(((constant_long *) (m->clazz->cpinfos[i]))->value);
586 OP_LOADCONST_F(((constant_float *) (m->clazz->cpinfos[i]))->value);
588 case CONSTANT_Double:
589 OP_LOADCONST_D(((constant_double *) (m->clazz->cpinfos[i]))->value);
591 case CONSTANT_String:
592 OP_LOADCONST_STRING(literalstring_new((utf *) (m->clazz->cpinfos[i])));
595 cr = (constant_classref *) (m->clazz->cpinfos[i]);
597 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
600 /* if not resolved, c == NULL */
602 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
606 #if defined(ENABLE_VERIFIER)
608 exceptions_throw_verifyerror(m,
609 "Invalid constant type to push");
626 OP_LOADCONST_I(opcode - BC_iconst_0);
631 OP_LOADCONST_L(opcode - BC_lconst_0);
637 OP_LOADCONST_F(opcode - BC_fconst_0);
642 OP_LOADCONST_D(opcode - BC_dconst_0);
645 /* stack operations ***************************************************/
647 /* We need space for additional instruction so we can
648 translate these instructions to sequences of ICMD_COPY and
649 ICMD_MOVE instructions. */
652 INSTRUCTIONS_CHECK(4);
660 INSTRUCTIONS_CHECK(6);
670 INSTRUCTIONS_CHECK(2);
676 INSTRUCTIONS_CHECK(7);
687 INSTRUCTIONS_CHECK(9);
700 INSTRUCTIONS_CHECK(3);
706 /* local variable access instructions *********************************/
711 if (iswide == false) {
712 i = SUCK_BE_U1(m->jcode + bcindex + 1);
715 i = SUCK_BE_U2(m->jcode + bcindex + 1);
716 nextbc = bcindex + 3;
719 OP_LOAD_ONEWORD(opcode, i, opcode - BC_iload);
724 if (iswide == false) {
725 i = SUCK_BE_U1(m->jcode + bcindex + 1);
728 i = SUCK_BE_U2(m->jcode + bcindex + 1);
729 nextbc = bcindex + 3;
732 OP_LOAD_TWOWORD(opcode, i, opcode - BC_iload);
739 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - BC_iload_0, TYPE_INT);
746 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - BC_lload_0, TYPE_LNG);
753 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - BC_fload_0, TYPE_FLT);
760 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - BC_dload_0, TYPE_DBL);
767 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - BC_aload_0, TYPE_ADR);
773 if (iswide == false) {
774 i = SUCK_BE_U1(m->jcode + bcindex + 1);
777 i = SUCK_BE_U2(m->jcode + bcindex + 1);
778 nextbc = bcindex + 3;
781 OP_STORE_ONEWORD(opcode, i, opcode - BC_istore);
786 if (iswide == false) {
787 i = SUCK_BE_U1(m->jcode + bcindex + 1);
790 i = SUCK_BE_U2(m->jcode + bcindex + 1);
791 nextbc = bcindex + 3;
794 OP_STORE_TWOWORD(opcode, i, opcode - BC_istore);
801 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - BC_istore_0, TYPE_INT);
808 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - BC_lstore_0, TYPE_LNG);
815 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - BC_fstore_0, TYPE_FLT);
822 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - BC_dstore_0, TYPE_DBL);
829 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - BC_astore_0, TYPE_ADR);
836 if (iswide == false) {
837 i = SUCK_BE_U1(m->jcode + bcindex + 1);
838 v = SUCK_BE_S1(m->jcode + bcindex + 2);
841 i = SUCK_BE_U2(m->jcode + bcindex + 1);
842 v = SUCK_BE_S2(m->jcode + bcindex + 3);
843 nextbc = bcindex + 5;
847 LOCALTYPE_USED(i, TYPE_INT);
848 OP_LOCALINDEX_I(opcode, i, v);
852 /* wider index for loading, storing and incrementing ******************/
859 /* managing arrays ****************************************************/
862 switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
864 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
867 bte = builtintable_get_internal(BUILTIN_newarray_char);
870 bte = builtintable_get_internal(BUILTIN_newarray_float);
873 bte = builtintable_get_internal(BUILTIN_newarray_double);
876 bte = builtintable_get_internal(BUILTIN_newarray_byte);
879 bte = builtintable_get_internal(BUILTIN_newarray_short);
882 bte = builtintable_get_internal(BUILTIN_newarray_int);
885 bte = builtintable_get_internal(BUILTIN_newarray_long);
887 #if defined(ENABLE_VERIFIER)
889 exceptions_throw_verifyerror(m, "Invalid array-type to create");
893 OP_BUILTIN_CHECK_EXCEPTION(bte);
897 i = SUCK_BE_U2(m->jcode + bcindex + 1);
898 compr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
902 if (!(cr = class_get_classref_multiarray_of(1, compr)))
905 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
908 INSTRUCTIONS_CHECK(2);
909 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
910 bte = builtintable_get_internal(BUILTIN_newarray);
911 OP_BUILTIN_CHECK_EXCEPTION(bte);
915 case BC_multianewarray:
916 i = SUCK_BE_U2(m->jcode + bcindex + 1);
917 j = SUCK_BE_U1(m->jcode + bcindex + 3);
919 cr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
923 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
926 /* if unresolved, c == NULL */
928 iptr->s1.argcount = j;
929 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
930 code_unflag_leafmethod(code);
933 /* control flow instructions ******************************************/
952 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
953 CHECK_BYTECODE_INDEX(i);
954 MARK_BASICBLOCK(&pd, i);
956 OP_INSINDEX(opcode, i);
960 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
961 CHECK_BYTECODE_INDEX(i);
962 MARK_BASICBLOCK(&pd, i);
964 OP_INSINDEX(ICMD_GOTO, i);
968 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
970 CHECK_BYTECODE_INDEX(i);
971 MARK_BASICBLOCK(&pd, i);
973 OP_PREPARE_ZEROFLAGS(BC_jsr);
974 iptr->sx.s23.s3.jsrtarget.insindex = i;
979 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
983 if (iswide == false) {
984 i = SUCK_BE_U1(m->jcode + bcindex + 1);
987 i = SUCK_BE_U2(m->jcode + bcindex + 1);
988 nextbc = bcindex + 3;
993 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
1003 /* XXX ARETURN will need a flag in the typechecker */
1009 /* XXX ATHROW will need a flag in the typechecker */
1014 /* table jumps ********************************************************/
1016 case BC_lookupswitch:
1019 lookup_target_t *lookup;
1020 #if defined(ENABLE_VERIFIER)
1024 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1026 CHECK_END_OF_BYTECODE(nextbc + 8);
1028 OP_PREPARE_ZEROFLAGS(opcode);
1030 /* default target */
1032 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1033 iptr->sx.s23.s3.lookupdefault.insindex = j;
1035 CHECK_BYTECODE_INDEX(j);
1036 MARK_BASICBLOCK(&pd, j);
1038 /* number of pairs */
1040 num = SUCK_BE_U4(m->jcode + nextbc);
1041 iptr->sx.s23.s2.lookupcount = num;
1044 /* allocate the intermediate code table */
1046 lookup = (lookup_target_t*) DumpMemory::allocate(sizeof(lookup_target_t) * num);
1047 iptr->dst.lookup = lookup;
1049 /* iterate over the lookup table */
1051 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1053 for (i = 0; i < num; i++) {
1056 j = SUCK_BE_S4(m->jcode + nextbc);
1061 #if defined(ENABLE_VERIFIER)
1062 /* check if the lookup table is sorted correctly */
1064 if (i && (j <= prevvalue)) {
1065 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1072 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1073 lookup->target.insindex = j;
1076 CHECK_BYTECODE_INDEX(j);
1077 MARK_BASICBLOCK(&pd, j);
1084 case BC_tableswitch:
1088 branch_target_t *table;
1091 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1093 CHECK_END_OF_BYTECODE(nextbc + 12);
1095 OP_PREPARE_ZEROFLAGS(opcode);
1097 /* default target */
1099 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1101 CHECK_BYTECODE_INDEX(deftarget);
1102 MARK_BASICBLOCK(&pd, deftarget);
1106 j = SUCK_BE_S4(m->jcode + nextbc);
1107 iptr->sx.s23.s2.tablelow = j;
1112 num = SUCK_BE_S4(m->jcode + nextbc);
1113 iptr->sx.s23.s3.tablehigh = num;
1116 /* calculate the number of table entries */
1120 #if defined(ENABLE_VERIFIER)
1122 exceptions_throw_verifyerror(m,
1123 "invalid TABLESWITCH: upper bound < lower bound");
1127 /* create the intermediate code table */
1128 /* the first entry is the default target */
1130 table = (branch_target_t*) DumpMemory::allocate(sizeof(branch_target_t) * (1 + num));
1131 iptr->dst.table = table;
1132 (table++)->insindex = deftarget;
1134 /* iterate over the target table */
1136 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1138 for (i = 0; i < num; i++) {
1139 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1140 (table++)->insindex = j;
1142 CHECK_BYTECODE_INDEX(j);
1143 MARK_BASICBLOCK(&pd, j);
1151 /* load and store of object fields ************************************/
1155 code_unflag_leafmethod(code);
1162 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1163 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Fieldref);
1168 OP_PREPARE_ZEROFLAGS(opcode);
1169 iptr->sx.s23.s3.fmiref = fmi;
1171 /* only with -noverify, otherwise the typechecker does this */
1173 #if defined(ENABLE_VERIFIER)
1174 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1176 result = resolve_field_lazy(m, fmi);
1178 if (result == resolveFailed)
1181 if (result != resolveSucceeded) {
1182 uf = resolve_create_unresolved_field(m->clazz, m, iptr);
1187 /* store the unresolved_field pointer */
1189 iptr->sx.s23.s3.uf = uf;
1190 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1192 #if defined(ENABLE_VERIFIER)
1199 /* method invocation **************************************************/
1201 case BC_invokestatic:
1202 OP_PREPARE_ZEROFLAGS(opcode);
1204 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1205 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1210 md = fmi->parseddesc.md;
1212 if (md->params == NULL)
1213 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1218 case BC_invokespecial:
1219 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1221 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1222 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1224 goto invoke_nonstatic_method;
1226 case BC_invokeinterface:
1227 OP_PREPARE_ZEROFLAGS(opcode);
1229 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1230 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_InterfaceMethodref);
1232 goto invoke_nonstatic_method;
1234 case BC_invokevirtual:
1235 OP_PREPARE_ZEROFLAGS(opcode);
1237 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1238 fmi = (constant_FMIref*) class_getconstant(m->clazz, i, CONSTANT_Methodref);
1240 invoke_nonstatic_method:
1244 md = fmi->parseddesc.md;
1246 if (md->params == NULL)
1247 if (!descriptor_params_from_paramtypes(md, 0))
1251 code_unflag_leafmethod(code);
1253 iptr->sx.s23.s3.fmiref = fmi;
1255 /* only with -noverify, otherwise the typechecker does this */
1257 #if defined(ENABLE_VERIFIER)
1258 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1260 result = resolve_method_lazy(m, fmi,
1261 (opcode == BC_invokespecial));
1263 if (result == resolveFailed)
1266 if (result == resolveSucceeded) {
1267 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1269 /* if this call is monomorphic, turn it into an
1272 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1274 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1275 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1277 iptr->opc = ICMD_INVOKESPECIAL;
1278 iptr->flags.bits |= INS_FLAG_CHECK;
1282 um = resolve_create_unresolved_method(m->clazz, m, fmi,
1283 (opcode == BC_invokestatic),
1284 (opcode == BC_invokespecial));
1289 /* store the unresolved_method pointer */
1291 iptr->sx.s23.s3.um = um;
1292 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1294 #if defined(ENABLE_VERIFIER)
1300 /* instructions taking class arguments ********************************/
1303 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1304 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1309 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1312 INSTRUCTIONS_CHECK(2);
1313 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1314 bte = builtintable_get_internal(BUILTIN_new);
1315 OP_BUILTIN_CHECK_EXCEPTION(bte);
1320 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1321 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1326 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1329 if (cr->name->text[0] == '[') {
1330 /* array type cast-check */
1331 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1332 code_unflag_leafmethod(code);
1335 /* object type cast-check */
1336 flags = INS_FLAG_CHECK;
1338 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1342 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1343 cr = (constant_classref*) class_getconstant(m->clazz, i, CONSTANT_Class);
1348 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1351 if (cr->name->text[0] == '[') {
1352 /* array type cast-check */
1353 INSTRUCTIONS_CHECK(2);
1354 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1355 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1356 OP_BUILTIN_NO_EXCEPTION(bte);
1360 /* object type cast-check */
1361 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1365 /* synchronization instructions ***************************************/
1367 case BC_monitorenter:
1368 #if defined(ENABLE_THREADS)
1370 bte = builtintable_get_internal(LOCK_monitor_enter);
1371 OP_BUILTIN_CHECK_EXCEPTION(bte);
1376 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1381 case BC_monitorexit:
1382 #if defined(ENABLE_THREADS)
1384 bte = builtintable_get_internal(LOCK_monitor_exit);
1385 OP_BUILTIN_CHECK_EXCEPTION(bte);
1390 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1395 /* arithmetic instructions that may become builtin functions **********/
1398 #if !SUPPORT_DIVISION
1399 bte = builtintable_get_internal(BUILTIN_idiv);
1400 OP_BUILTIN_ARITHMETIC(opcode, bte);
1402 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1405 OP_CHECK_EXCEPTION(opcode);
1411 #if !SUPPORT_DIVISION
1412 bte = builtintable_get_internal(BUILTIN_irem);
1413 OP_BUILTIN_ARITHMETIC(opcode, bte);
1415 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1418 OP_CHECK_EXCEPTION(opcode);
1424 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1425 bte = builtintable_get_internal(BUILTIN_ldiv);
1426 OP_BUILTIN_ARITHMETIC(opcode, bte);
1428 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1431 OP_CHECK_EXCEPTION(opcode);
1437 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1438 bte = builtintable_get_internal(BUILTIN_lrem);
1439 OP_BUILTIN_ARITHMETIC(opcode, bte);
1441 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1444 OP_CHECK_EXCEPTION(opcode);
1450 #if defined(__I386__)
1453 bte = builtintable_get_internal(BUILTIN_frem);
1454 OP_BUILTIN_NO_EXCEPTION(bte);
1459 #if defined(__I386__)
1462 bte = builtintable_get_internal(BUILTIN_drem);
1463 OP_BUILTIN_NO_EXCEPTION(bte);
1468 #if defined(__ALPHA__)
1469 bte = builtintable_get_internal(BUILTIN_f2i);
1470 OP_BUILTIN_NO_EXCEPTION(bte);
1477 #if defined(__ALPHA__)
1478 bte = builtintable_get_internal(BUILTIN_f2l);
1479 OP_BUILTIN_NO_EXCEPTION(bte);
1486 #if defined(__ALPHA__)
1487 bte = builtintable_get_internal(BUILTIN_d2i);
1488 OP_BUILTIN_NO_EXCEPTION(bte);
1495 #if defined(__ALPHA__)
1496 bte = builtintable_get_internal(BUILTIN_d2l);
1497 OP_BUILTIN_NO_EXCEPTION(bte);
1504 /* invalid opcodes ****************************************************/
1506 /* check for invalid opcodes if the verifier is enabled */
1507 #if defined(ENABLE_VERIFIER)
1509 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1513 /* Unused opcodes ************************************************** */
1569 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1573 #endif /* defined(ENABLE_VERIFIER) */
1575 /* opcodes that don't require translation *****************************/
1578 /* Straight-forward translation to HIR. */
1584 /* verifier checks ****************************************************/
1586 #if defined(ENABLE_VERIFIER)
1587 /* If WIDE was used correctly, iswide should have been reset by now. */
1589 exceptions_throw_verifyerror(m,
1590 "Illegal instruction: WIDE before incompatible opcode");
1593 #endif /* defined(ENABLE_VERIFIER) */
1597 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1598 /* add a NOP to the last basic block */
1600 INSTRUCTIONS_CHECK(1);
1604 /*** END OF LOOP **********************************************************/
1606 /* assert that we did not write more ICMDs than allocated */
1608 assert(ircount <= pd.instructionslength);
1609 assert(ircount == (iptr - pd.instructions));
1611 /*** verifier checks ******************************************************/
1613 #if defined(ENABLE_VERIFIER)
1614 if (bcindex != m->jcodelength) {
1615 exceptions_throw_verifyerror(m,
1616 "Command-sequence crosses code-boundary");
1621 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1624 #endif /* defined(ENABLE_VERIFIER) */
1626 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1628 /* identify basic blocks */
1630 /* check if first instruction is a branch target */
1632 if (pd.basicblockstart[0] == 1) {
1633 jd->branchtoentry = true;
1636 /* first instruction always starts a basic block */
1638 iptr = pd.instructions;
1640 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1643 /* Iterate over all bytecode instructions and set missing
1644 basic-block starts in IR instructions. */
1646 for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1647 /* Does the current bytecode instruction start a basic
1650 if (pd.basicblockstart[bcindex] == 1) {
1651 #if defined(ENABLE_VERIFIER)
1652 /* Check if this bytecode basic-block start at the
1653 beginning of a bytecode instruction. */
1655 if (pd.bytecodestart[bcindex] == 0) {
1656 exceptions_throw_verifyerror(m,
1657 "Branch into middle of instruction");
1662 /* Get the IR instruction mapped to the bytecode
1663 instruction and set the basic block flag. */
1665 irindex = pd.bytecodemap[bcindex];
1666 iptr = pd.instructions + irindex;
1668 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1672 /* IR instruction index to basic-block index mapping */
1674 pd.instructionmap = (s4*) DumpMemory::allocate(sizeof(s4) * ircount);
1675 MZERO(pd.instructionmap, s4, ircount);
1677 /* Iterate over all IR instructions and count the basic blocks. */
1679 iptr = pd.instructions;
1683 for (i = 0; i < ircount; i++, iptr++) {
1684 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1685 /* store the basic-block number in the IR instruction
1688 pd.instructionmap[i] = bbcount;
1690 /* post-increment the basic-block count */
1696 /* Allocate basic block array (one more for end ipc). */
1698 jd->basicblocks = (basicblock*) DumpMemory::allocate(sizeof(basicblock) * (bbcount + 1));
1699 MZERO(jd->basicblocks, basicblock, bbcount + 1);
1701 /* Now iterate again over all IR instructions and initialize the
1702 basic block structures and, in the same loop, resolve the
1703 branch-target instruction indices to basic blocks. */
1705 iptr = pd.instructions;
1706 bptr = jd->basicblocks;
1710 for (i = 0; i < ircount; i++, iptr++) {
1711 /* check for basic block */
1713 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1714 /* intialize the basic block */
1716 BASICBLOCK_INIT(bptr, m);
1718 bptr->iinstr = iptr;
1721 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1724 /* bptr->icount is set when the next block is allocated */
1726 bptr->nr = bbcount++;
1728 bptr[-1].next = bptr;
1731 /* resolve instruction indices to basic blocks */
1733 switch (iptr->opc) {
1741 case ICMD_IFNONNULL:
1742 case ICMD_IF_ICMPEQ:
1743 case ICMD_IF_ICMPNE:
1744 case ICMD_IF_ICMPLT:
1745 case ICMD_IF_ICMPGT:
1746 case ICMD_IF_ICMPLE:
1747 case ICMD_IF_ICMPGE:
1748 case ICMD_IF_ACMPEQ:
1749 case ICMD_IF_ACMPNE:
1751 BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1755 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1758 case ICMD_TABLESWITCH:
1759 table = iptr->dst.table;
1761 BYTECODEINDEX_TO_BASICBLOCK(*table);
1764 j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1767 BYTECODEINDEX_TO_BASICBLOCK(*table);
1772 case ICMD_LOOKUPSWITCH:
1773 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1775 lookup = iptr->dst.lookup;
1777 j = iptr->sx.s23.s2.lookupcount;
1780 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1787 /* set instruction count of last real block */
1790 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1793 /* allocate additional block at end */
1795 BASICBLOCK_INIT(bptr, m);
1798 /* set basicblock pointers in exception table */
1800 if (!parse_resolve_exception_table(jd, &pd))
1803 /* store the local map */
1805 jd->local_map = local_map;
1807 /* calculate local variable renaming */
1819 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1820 /* variable index for each _used_ (javaindex,type) pair. */
1821 /* (local_map[javaindex*5+type] = cacaoindex) */
1822 /* Unused (javaindex,type) pairs are marked with UNUSED. */
1824 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1826 *mapptr = nlocals++;
1831 jd->localcount = nlocals;
1833 /* calculate the (maximum) number of variables needed */
1836 nlocals /* local variables */
1837 + bbcount * m->maxstack /* invars */
1838 + s_count; /* variables created within blocks (non-invar) */
1840 /* reserve the first indices for local variables */
1842 jd->vartop = nlocals;
1844 /* reserve extra variables needed by stack analyse */
1846 jd->varcount += STACK_EXTRA_VARS;
1847 jd->vartop += STACK_EXTRA_VARS;
1849 /* The verifier needs space for saving invars in some cases and */
1850 /* extra variables. */
1852 #if defined(ENABLE_VERIFIER)
1853 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1854 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1856 /* allocate and initialize the variable array */
1858 jd->var = (varinfo*) DumpMemory::allocate(sizeof(varinfo) * jd->varcount);
1859 MZERO(jd->var, varinfo, jd->varcount);
1861 /* set types of all locals in jd->var */
1862 /* and fill the reverselocalmap */
1864 reversemap = (s4*) DumpMemory::allocate(sizeof(s4) * nlocals);
1866 for (i = 0; i < m->maxlocals; i++)
1867 for (t=0; t<5; t++) {
1868 varindex = local_map[5*i + t];
1869 if (varindex != UNUSED) {
1870 VAR(varindex)->type = t;
1871 reversemap[varindex] = i;
1875 jd->reverselocalmap = reversemap;
1878 /* assign local variables to method variables */
1880 jd->instructions = pd.instructions;
1881 jd->instructioncount = ircount;
1882 jd->basicblockcount = bbcount;
1883 jd->stackcount = s_count + bbcount * m->maxstack; /* in-stacks */
1885 /* allocate stack table */
1887 jd->stack = (stackelement_t*) DumpMemory::allocate(sizeof(stackelement_t) * jd->stackcount);
1889 /* everything's ok */
1893 /*** goto labels for throwing verifier exceptions *************************/
1895 #if defined(ENABLE_VERIFIER)
1897 throw_unexpected_end_of_bytecode:
1898 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1901 throw_invalid_bytecode_index:
1902 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1905 throw_illegal_local_variable_number:
1906 exceptions_throw_verifyerror(m, "Illegal local variable number");
1909 #endif /* ENABLE_VERIFIER */
1912 #if defined(__cplusplus)
1917 * These are local overrides for various environment variables in Emacs.
1918 * Please do not remove this and leave it at the end of the file, where
1919 * Emacs will automagically detect them.
1920 * ---------------------------------------------------------------------
1923 * indent-tabs-mode: t
1927 * vim:noexpandtab:sw=4:ts=4: