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.h"
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.h"
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 */
94 /* parse_setup *****************************************************************
96 Fills the passed parsedata_t structure.
98 *******************************************************************************/
100 static void parse_setup(jitdata *jd, parsedata_t *pd)
104 /* get required compiler data */
108 /* bytecode start array */
110 pd->bytecodestart = DMNEW(u1, m->jcodelength + 1);
111 MZERO(pd->bytecodestart, u1, m->jcodelength + 1);
113 /* bytecode basic-block start array */
115 pd->basicblockstart = DMNEW(u1, m->jcodelength + 1);
116 MZERO(pd->basicblockstart, u1, m->jcodelength + 1);
118 /* bytecode instruction index to IR instruction mapping */
120 pd->bytecodemap = DMNEW(s4, m->jcodelength + 1);
121 MSET(pd->bytecodemap, -1, s4, m->jcodelength + 1);
123 /* allocate the instruction array */
125 pd->instructionslength = m->jcodelength + 1;
126 pd->instructions = DMNEW(instruction, pd->instructionslength);
128 /* Zero the intermediate instructions array so we don't have any
129 invalid pointers in it if we cannot finish stack_analyse(). */
131 MZERO(pd->instructions, instruction, pd->instructionslength);
133 /* The instructionmap is allocated later when we know the count of
136 pd->instructionmap = NULL;
140 /* parse_realloc_instructions **************************************************
142 Reallocate the instructions array so there is room for at least N
143 additional instructions.
146 the new value for iptr
148 *******************************************************************************/
150 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 icount, s4 n)
152 /* increase the size of the instruction array */
154 pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
156 /* reallocate the array */
158 pd->instructions = DMREALLOC(pd->instructions, instruction, icount,
159 pd->instructionslength);
160 MZERO(pd->instructions + icount, instruction,
161 (pd->instructionslength - icount));
163 /* return the iptr */
165 return pd->instructions + icount;
169 /* parse_bytecodeindex_to_basicblock *******************************************
171 Resolves a bytecode index to the corresponding basic block.
173 *******************************************************************************/
175 static basicblock *parse_bytecodeindex_to_basicblock(jitdata *jd,
182 irindex = pd->bytecodemap[bcindex];
183 bb = jd->basicblocks + pd->instructionmap[irindex];
189 /* parse_mark_exception_boundaries *********************************************
191 Mark exception handlers and the boundaries of the handled regions as
192 basic block boundaries.
195 jd...............current jitdata
198 true.............everything ok
199 false............an exception has been thrown
201 *******************************************************************************/
203 static bool parse_mark_exception_boundaries(jitdata *jd, parsedata_t *pd)
208 raw_exception_entry *rex;
213 len = m->rawexceptiontablelength;
218 rex = m->rawexceptiontable;
220 for (i = 0; i < len; ++i, ++rex) {
222 /* the start of the handled region becomes a basic block start */
224 bcindex = rex->startpc;
225 CHECK_BYTECODE_INDEX(bcindex);
226 MARK_BASICBLOCK(pd, bcindex);
228 bcindex = rex->endpc; /* see JVM Spec 4.7.3 */
229 CHECK_BYTECODE_INDEX_EXCLUSIVE(bcindex);
231 /* check that the range is valid */
233 #if defined(ENABLE_VERIFIER)
234 if (bcindex <= rex->startpc) {
235 exceptions_throw_verifyerror(m, "Invalid exception handler range");
240 /* End of handled region becomes a basic block boundary (if it
241 is the bytecode end, we'll use the special end block that
242 is created anyway). */
244 if (bcindex < m->jcodelength)
245 MARK_BASICBLOCK(pd, bcindex);
247 jd->branchtoend = true;
249 /* the start of the handler becomes a basic block start */
251 bcindex = rex->handlerpc;
252 CHECK_BYTECODE_INDEX(bcindex);
253 MARK_BASICBLOCK(pd, bcindex);
260 #if defined(ENABLE_VERIFIER)
261 throw_invalid_bytecode_index:
262 exceptions_throw_verifyerror(m,
263 "Illegal bytecode index in exception table");
269 /* parse_resolve_exception_table ***********************************************
271 Enter the exception handlers and their ranges, resolved to basicblock *s,
275 jd...............current jitdata
278 true.............everything ok
279 false............an exception has been thrown
281 *******************************************************************************/
283 static bool parse_resolve_exception_table(jitdata *jd, parsedata_t *pd)
286 raw_exception_entry *rex;
294 len = m->rawexceptiontablelength;
296 /* common case: no handler entries */
301 /* allocate the exception table */
303 jd->exceptiontablelength = len;
304 jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
306 /* copy and resolve the entries */
308 ex = jd->exceptiontable;
309 rex = m->rawexceptiontable;
311 for (i = 0; i < len; ++i, ++rex, ++ex) {
312 /* resolve instruction indices to basic blocks */
314 ex->start = parse_bytecodeindex_to_basicblock(jd, pd, rex->startpc);
315 ex->end = parse_bytecodeindex_to_basicblock(jd, pd, rex->endpc);
316 ex->handler = parse_bytecodeindex_to_basicblock(jd, pd, rex->handlerpc);
318 /* lazily resolve the catchtype */
320 if (rex->catchtype.any != NULL) {
321 if (!resolve_classref_or_classinfo(m,
323 resolveLazy, true, false,
327 /* if resolved, enter the result of resolution in the table */
330 rex->catchtype.cls = exclass;
333 ex->catchtype = rex->catchtype;
334 ex->next = NULL; /* set by loop analysis */
335 ex->down = ex + 1; /* link to next exception entry */
338 /* terminate the ->down linked list */
340 assert(ex != jd->exceptiontable);
347 /*******************************************************************************
349 function 'parse' scans the JavaVM code and generates intermediate code
351 During parsing the block index table is used to store at bit pos 0
352 a flag which marks basic block starts and at position 1 to 31 the
353 intermediate instruction index. After parsing the block index table
354 is scanned, for marked positions a block is generated and the block
355 number is stored in the block index table.
357 *******************************************************************************/
359 /*** macro for checking the length of the bytecode ***/
361 #if defined(ENABLE_VERIFIER)
362 #define CHECK_END_OF_BYTECODE(neededlength) \
364 if ((neededlength) > m->jcodelength) \
365 goto throw_unexpected_end_of_bytecode; \
367 #else /* !ENABLE_VERIFIER */
368 #define CHECK_END_OF_BYTECODE(neededlength)
369 #endif /* ENABLE_VERIFIER */
371 bool parse(jitdata *jd)
373 methodinfo *m; /* method being parsed */
376 instruction *iptr; /* current ptr into instruction array */
378 s4 bcindex; /* bytecode instruction index */
379 s4 nextbc; /* start of next bytecode instruction */
380 s4 opcode; /* bytecode instruction opcode */
382 s4 irindex; /* IR instruction index */
383 s4 ircount; /* IR instruction count */
385 s4 bbcount; /* basic block count */
387 int s_count = 0; /* stack element counter */
388 bool blockend; /* true if basic block end has been reached */
389 bool iswide; /* true if last instruction was a wide */
391 constant_classref *cr;
392 constant_classref *compr;
394 builtintable_entry *bte;
395 constant_FMIref *fmi;
397 unresolved_method *um;
398 unresolved_field *uf;
400 resolve_result_t result;
407 int *local_map; /* local pointer to renaming map */
408 /* is assigned to rd->local_map at the end */
409 branch_target_t *table;
410 lookup_target_t *lookup;
414 /* get required compiler data */
419 /* allocate buffers for local variable renaming */
421 local_map = DMNEW(int, m->maxlocals * 5);
423 for (i = 0; i < m->maxlocals; i++) {
424 local_map[i * 5 + 0] = 0;
425 local_map[i * 5 + 1] = 0;
426 local_map[i * 5 + 2] = 0;
427 local_map[i * 5 + 3] = 0;
428 local_map[i * 5 + 4] = 0;
431 /* initialize the parse data structures */
433 parse_setup(jd, &pd);
435 /* initialize local variables */
437 iptr = pd.instructions;
443 /* mark basic block boundaries for exception table */
445 if (!parse_mark_exception_boundaries(jd, &pd))
448 /* initialize stack element counter */
450 s_count = 1 + m->rawexceptiontablelength;
452 /* setup line number info */
457 if (m->linenumbercount == 0) {
461 linepcchange = m->linenumbers[0].start_pc;
464 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
466 for (bcindex = 0; bcindex < m->jcodelength; bcindex = nextbc) {
468 /* mark this position as a valid bytecode instruction start */
470 pd.bytecodestart[bcindex] = 1;
472 /* change the current line number, if necessary */
474 /* XXX rewrite this using pointer arithmetic */
476 if (linepcchange == bcindex) {
477 if (m->linenumbercount > lineindex) {
479 currentline = m->linenumbers[lineindex].line_number;
481 if (lineindex < m->linenumbercount) {
482 linepcchange = m->linenumbers[lineindex].start_pc;
483 if (linepcchange == bcindex)
484 goto next_linenumber;
490 /* fetch next opcode */
492 opcode = SUCK_BE_U1(m->jcode + bcindex);
494 /* If the previous instruction was a block-end instruction,
495 mark the current bytecode instruction as basic-block
496 starting instruction. */
498 /* NOTE: Some compilers put a BC_nop after a blockend
501 if (blockend && (opcode != BC_nop)) {
502 MARK_BASICBLOCK(&pd, bcindex);
506 /* If the current bytecode instruction was marked as
507 basic-block starting instruction before (e.g. blockend,
508 forward-branch target), mark the current IR instruction
511 if (pd.basicblockstart[bcindex] != 0) {
512 /* We need a NOP as last instruction in each basic block
513 for basic block reordering (may be replaced with a GOTO
516 INSTRUCTIONS_CHECK(1);
520 /* store intermediate instruction count (bit 0 mark block starts) */
522 pd.bytecodemap[bcindex] = ircount;
524 /* compute next instruction start */
526 nextbc = bcindex + bytecode[opcode].length;
528 CHECK_END_OF_BYTECODE(nextbc);
530 /* add stack elements produced by this instruction */
532 s_count += bytecode[opcode].slots;
534 /* We check here for the space of 1 instruction in the
535 instruction array. If an opcode is converted to more than
536 1 instruction, this is checked in the corresponding
539 INSTRUCTIONS_CHECK(1);
541 /* translate this bytecode instruction */
547 /* pushing constants onto the stack ***********************************/
550 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
554 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
558 i = SUCK_BE_U1(m->jcode + bcindex + 1);
559 goto pushconstantitem;
563 i = SUCK_BE_U2(m->jcode + bcindex + 1);
567 #if defined(ENABLE_VERIFIER)
568 if (i >= m->clazz->cpcount) {
569 exceptions_throw_verifyerror(m,
570 "Attempt to access constant outside range");
575 switch (m->clazz->cptags[i]) {
576 case CONSTANT_Integer:
577 OP_LOADCONST_I(((constant_integer *) (m->clazz->cpinfos[i]))->value);
580 OP_LOADCONST_L(((constant_long *) (m->clazz->cpinfos[i]))->value);
583 OP_LOADCONST_F(((constant_float *) (m->clazz->cpinfos[i]))->value);
585 case CONSTANT_Double:
586 OP_LOADCONST_D(((constant_double *) (m->clazz->cpinfos[i]))->value);
588 case CONSTANT_String:
589 OP_LOADCONST_STRING(literalstring_new((utf *) (m->clazz->cpinfos[i])));
592 cr = (constant_classref *) (m->clazz->cpinfos[i]);
594 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
597 /* if not resolved, c == NULL */
599 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
603 #if defined(ENABLE_VERIFIER)
605 exceptions_throw_verifyerror(m,
606 "Invalid constant type to push");
623 OP_LOADCONST_I(opcode - BC_iconst_0);
628 OP_LOADCONST_L(opcode - BC_lconst_0);
634 OP_LOADCONST_F(opcode - BC_fconst_0);
639 OP_LOADCONST_D(opcode - BC_dconst_0);
642 /* stack operations ***************************************************/
644 /* We need space for additional instruction so we can
645 translate these instructions to sequences of ICMD_COPY and
646 ICMD_MOVE instructions. */
649 INSTRUCTIONS_CHECK(4);
657 INSTRUCTIONS_CHECK(6);
667 INSTRUCTIONS_CHECK(2);
673 INSTRUCTIONS_CHECK(7);
684 INSTRUCTIONS_CHECK(9);
697 INSTRUCTIONS_CHECK(3);
703 /* local variable access instructions *********************************/
708 if (iswide == false) {
709 i = SUCK_BE_U1(m->jcode + bcindex + 1);
712 i = SUCK_BE_U2(m->jcode + bcindex + 1);
713 nextbc = bcindex + 3;
716 OP_LOAD_ONEWORD(opcode, i, opcode - BC_iload);
721 if (iswide == false) {
722 i = SUCK_BE_U1(m->jcode + bcindex + 1);
725 i = SUCK_BE_U2(m->jcode + bcindex + 1);
726 nextbc = bcindex + 3;
729 OP_LOAD_TWOWORD(opcode, i, opcode - BC_iload);
736 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - BC_iload_0, TYPE_INT);
743 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - BC_lload_0, TYPE_LNG);
750 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - BC_fload_0, TYPE_FLT);
757 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - BC_dload_0, TYPE_DBL);
764 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - BC_aload_0, TYPE_ADR);
770 if (iswide == false) {
771 i = SUCK_BE_U1(m->jcode + bcindex + 1);
774 i = SUCK_BE_U2(m->jcode + bcindex + 1);
775 nextbc = bcindex + 3;
778 OP_STORE_ONEWORD(opcode, i, opcode - BC_istore);
783 if (iswide == false) {
784 i = SUCK_BE_U1(m->jcode + bcindex + 1);
787 i = SUCK_BE_U2(m->jcode + bcindex + 1);
788 nextbc = bcindex + 3;
791 OP_STORE_TWOWORD(opcode, i, opcode - BC_istore);
798 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - BC_istore_0, TYPE_INT);
805 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - BC_lstore_0, TYPE_LNG);
812 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - BC_fstore_0, TYPE_FLT);
819 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - BC_dstore_0, TYPE_DBL);
826 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - BC_astore_0, TYPE_ADR);
833 if (iswide == false) {
834 i = SUCK_BE_U1(m->jcode + bcindex + 1);
835 v = SUCK_BE_S1(m->jcode + bcindex + 2);
838 i = SUCK_BE_U2(m->jcode + bcindex + 1);
839 v = SUCK_BE_S2(m->jcode + bcindex + 3);
840 nextbc = bcindex + 5;
844 LOCALTYPE_USED(i, TYPE_INT);
845 OP_LOCALINDEX_I(opcode, i, v);
849 /* wider index for loading, storing and incrementing ******************/
856 /* managing arrays ****************************************************/
859 switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
861 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
864 bte = builtintable_get_internal(BUILTIN_newarray_char);
867 bte = builtintable_get_internal(BUILTIN_newarray_float);
870 bte = builtintable_get_internal(BUILTIN_newarray_double);
873 bte = builtintable_get_internal(BUILTIN_newarray_byte);
876 bte = builtintable_get_internal(BUILTIN_newarray_short);
879 bte = builtintable_get_internal(BUILTIN_newarray_int);
882 bte = builtintable_get_internal(BUILTIN_newarray_long);
884 #if defined(ENABLE_VERIFIER)
886 exceptions_throw_verifyerror(m, "Invalid array-type to create");
890 OP_BUILTIN_CHECK_EXCEPTION(bte);
894 i = SUCK_BE_U2(m->jcode + bcindex + 1);
895 compr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
899 if (!(cr = class_get_classref_multiarray_of(1, compr)))
902 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
905 INSTRUCTIONS_CHECK(2);
906 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
907 bte = builtintable_get_internal(BUILTIN_newarray);
908 OP_BUILTIN_CHECK_EXCEPTION(bte);
912 case BC_multianewarray:
913 i = SUCK_BE_U2(m->jcode + bcindex + 1);
914 j = SUCK_BE_U1(m->jcode + bcindex + 3);
916 cr = (constant_classref *) class_getconstant(m->clazz, i, CONSTANT_Class);
920 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
923 /* if unresolved, c == NULL */
925 iptr->s1.argcount = j;
926 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
927 code_unflag_leafmethod(code);
930 /* control flow instructions ******************************************/
949 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
950 CHECK_BYTECODE_INDEX(i);
951 MARK_BASICBLOCK(&pd, i);
953 OP_INSINDEX(opcode, i);
957 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
958 CHECK_BYTECODE_INDEX(i);
959 MARK_BASICBLOCK(&pd, i);
961 OP_INSINDEX(ICMD_GOTO, i);
965 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
967 CHECK_BYTECODE_INDEX(i);
968 MARK_BASICBLOCK(&pd, i);
970 OP_PREPARE_ZEROFLAGS(BC_jsr);
971 iptr->sx.s23.s3.jsrtarget.insindex = i;
976 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
980 if (iswide == false) {
981 i = SUCK_BE_U1(m->jcode + bcindex + 1);
984 i = SUCK_BE_U2(m->jcode + bcindex + 1);
985 nextbc = bcindex + 3;
990 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
1000 /* XXX ARETURN will need a flag in the typechecker */
1006 /* XXX ATHROW will need a flag in the typechecker */
1011 /* table jumps ********************************************************/
1013 case BC_lookupswitch:
1016 lookup_target_t *lookup;
1017 #if defined(ENABLE_VERIFIER)
1021 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1023 CHECK_END_OF_BYTECODE(nextbc + 8);
1025 OP_PREPARE_ZEROFLAGS(opcode);
1027 /* default target */
1029 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1030 iptr->sx.s23.s3.lookupdefault.insindex = j;
1032 CHECK_BYTECODE_INDEX(j);
1033 MARK_BASICBLOCK(&pd, j);
1035 /* number of pairs */
1037 num = SUCK_BE_U4(m->jcode + nextbc);
1038 iptr->sx.s23.s2.lookupcount = num;
1041 /* allocate the intermediate code table */
1043 lookup = DMNEW(lookup_target_t, num);
1044 iptr->dst.lookup = lookup;
1046 /* iterate over the lookup table */
1048 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1050 for (i = 0; i < num; i++) {
1053 j = SUCK_BE_S4(m->jcode + nextbc);
1058 #if defined(ENABLE_VERIFIER)
1059 /* check if the lookup table is sorted correctly */
1061 if (i && (j <= prevvalue)) {
1062 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1069 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1070 lookup->target.insindex = j;
1073 CHECK_BYTECODE_INDEX(j);
1074 MARK_BASICBLOCK(&pd, j);
1081 case BC_tableswitch:
1085 branch_target_t *table;
1088 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1090 CHECK_END_OF_BYTECODE(nextbc + 12);
1092 OP_PREPARE_ZEROFLAGS(opcode);
1094 /* default target */
1096 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1098 CHECK_BYTECODE_INDEX(deftarget);
1099 MARK_BASICBLOCK(&pd, deftarget);
1103 j = SUCK_BE_S4(m->jcode + nextbc);
1104 iptr->sx.s23.s2.tablelow = j;
1109 num = SUCK_BE_S4(m->jcode + nextbc);
1110 iptr->sx.s23.s3.tablehigh = num;
1113 /* calculate the number of table entries */
1117 #if defined(ENABLE_VERIFIER)
1119 exceptions_throw_verifyerror(m,
1120 "invalid TABLESWITCH: upper bound < lower bound");
1124 /* create the intermediate code table */
1125 /* the first entry is the default target */
1127 table = DMNEW(branch_target_t, 1 + num);
1128 iptr->dst.table = table;
1129 (table++)->insindex = deftarget;
1131 /* iterate over the target table */
1133 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1135 for (i = 0; i < num; i++) {
1136 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1137 (table++)->insindex = j;
1139 CHECK_BYTECODE_INDEX(j);
1140 MARK_BASICBLOCK(&pd, j);
1148 /* load and store of object fields ************************************/
1152 code_unflag_leafmethod(code);
1159 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1160 fmi = class_getconstant(m->clazz, i, CONSTANT_Fieldref);
1165 OP_PREPARE_ZEROFLAGS(opcode);
1166 iptr->sx.s23.s3.fmiref = fmi;
1168 /* only with -noverify, otherwise the typechecker does this */
1170 #if defined(ENABLE_VERIFIER)
1171 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1173 result = resolve_field_lazy(m, fmi);
1175 if (result == resolveFailed)
1178 if (result != resolveSucceeded) {
1179 uf = resolve_create_unresolved_field(m->clazz, m, iptr);
1184 /* store the unresolved_field pointer */
1186 iptr->sx.s23.s3.uf = uf;
1187 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1189 #if defined(ENABLE_VERIFIER)
1196 /* method invocation **************************************************/
1198 case BC_invokestatic:
1199 OP_PREPARE_ZEROFLAGS(opcode);
1201 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1202 fmi = class_getconstant(m->clazz, i, CONSTANT_Methodref);
1207 md = fmi->parseddesc.md;
1209 if (md->params == NULL)
1210 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1215 case BC_invokespecial:
1216 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1218 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1219 fmi = class_getconstant(m->clazz, i, CONSTANT_Methodref);
1221 goto invoke_nonstatic_method;
1223 case BC_invokeinterface:
1224 OP_PREPARE_ZEROFLAGS(opcode);
1226 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1227 fmi = class_getconstant(m->clazz, i, CONSTANT_InterfaceMethodref);
1229 goto invoke_nonstatic_method;
1231 case BC_invokevirtual:
1232 OP_PREPARE_ZEROFLAGS(opcode);
1234 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1235 fmi = class_getconstant(m->clazz, i, CONSTANT_Methodref);
1237 invoke_nonstatic_method:
1241 md = fmi->parseddesc.md;
1243 if (md->params == NULL)
1244 if (!descriptor_params_from_paramtypes(md, 0))
1248 code_unflag_leafmethod(code);
1250 iptr->sx.s23.s3.fmiref = fmi;
1252 /* only with -noverify, otherwise the typechecker does this */
1254 #if defined(ENABLE_VERIFIER)
1255 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1257 result = resolve_method_lazy(m, fmi,
1258 (opcode == BC_invokespecial));
1260 if (result == resolveFailed)
1263 if (result == resolveSucceeded) {
1264 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1266 /* if this call is monomorphic, turn it into an
1269 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1271 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1272 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1274 iptr->opc = ICMD_INVOKESPECIAL;
1275 iptr->flags.bits |= INS_FLAG_CHECK;
1279 um = resolve_create_unresolved_method(m->clazz, m, fmi,
1280 (opcode == BC_invokestatic),
1281 (opcode == BC_invokespecial));
1286 /* store the unresolved_method pointer */
1288 iptr->sx.s23.s3.um = um;
1289 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1291 #if defined(ENABLE_VERIFIER)
1297 /* instructions taking class arguments ********************************/
1300 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1301 cr = class_getconstant(m->clazz, i, CONSTANT_Class);
1306 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1309 INSTRUCTIONS_CHECK(2);
1310 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1311 bte = builtintable_get_internal(BUILTIN_new);
1312 OP_BUILTIN_CHECK_EXCEPTION(bte);
1317 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1318 cr = class_getconstant(m->clazz, i, CONSTANT_Class);
1323 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1326 if (cr->name->text[0] == '[') {
1327 /* array type cast-check */
1328 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1329 code_unflag_leafmethod(code);
1332 /* object type cast-check */
1333 flags = INS_FLAG_CHECK;
1335 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1339 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1340 cr = class_getconstant(m->clazz, i, CONSTANT_Class);
1345 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1348 if (cr->name->text[0] == '[') {
1349 /* array type cast-check */
1350 INSTRUCTIONS_CHECK(2);
1351 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1352 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1353 OP_BUILTIN_NO_EXCEPTION(bte);
1357 /* object type cast-check */
1358 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1362 /* synchronization instructions ***************************************/
1364 case BC_monitorenter:
1365 #if defined(ENABLE_THREADS)
1367 bte = builtintable_get_internal(LOCK_monitor_enter);
1368 OP_BUILTIN_CHECK_EXCEPTION(bte);
1373 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1378 case BC_monitorexit:
1379 #if defined(ENABLE_THREADS)
1381 bte = builtintable_get_internal(LOCK_monitor_exit);
1382 OP_BUILTIN_CHECK_EXCEPTION(bte);
1387 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1392 /* arithmetic instructions that may become builtin functions **********/
1395 #if !SUPPORT_DIVISION
1396 bte = builtintable_get_internal(BUILTIN_idiv);
1397 OP_BUILTIN_ARITHMETIC(opcode, bte);
1399 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1402 OP_CHECK_EXCEPTION(opcode);
1408 #if !SUPPORT_DIVISION
1409 bte = builtintable_get_internal(BUILTIN_irem);
1410 OP_BUILTIN_ARITHMETIC(opcode, bte);
1412 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1415 OP_CHECK_EXCEPTION(opcode);
1421 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1422 bte = builtintable_get_internal(BUILTIN_ldiv);
1423 OP_BUILTIN_ARITHMETIC(opcode, bte);
1425 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1428 OP_CHECK_EXCEPTION(opcode);
1434 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1435 bte = builtintable_get_internal(BUILTIN_lrem);
1436 OP_BUILTIN_ARITHMETIC(opcode, bte);
1438 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1441 OP_CHECK_EXCEPTION(opcode);
1447 #if defined(__I386__)
1450 bte = builtintable_get_internal(BUILTIN_frem);
1451 OP_BUILTIN_NO_EXCEPTION(bte);
1456 #if defined(__I386__)
1459 bte = builtintable_get_internal(BUILTIN_drem);
1460 OP_BUILTIN_NO_EXCEPTION(bte);
1465 #if defined(__ALPHA__)
1466 bte = builtintable_get_internal(BUILTIN_f2i);
1467 OP_BUILTIN_NO_EXCEPTION(bte);
1474 #if defined(__ALPHA__)
1475 bte = builtintable_get_internal(BUILTIN_f2l);
1476 OP_BUILTIN_NO_EXCEPTION(bte);
1483 #if defined(__ALPHA__)
1484 bte = builtintable_get_internal(BUILTIN_d2i);
1485 OP_BUILTIN_NO_EXCEPTION(bte);
1492 #if defined(__ALPHA__)
1493 bte = builtintable_get_internal(BUILTIN_d2l);
1494 OP_BUILTIN_NO_EXCEPTION(bte);
1501 /* invalid opcodes ****************************************************/
1503 /* check for invalid opcodes if the verifier is enabled */
1504 #if defined(ENABLE_VERIFIER)
1506 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1510 /* Unused opcodes ************************************************** */
1566 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1570 #endif /* defined(ENABLE_VERIFIER) */
1572 /* opcodes that don't require translation *****************************/
1575 /* Straight-forward translation to HIR. */
1581 /* verifier checks ****************************************************/
1583 #if defined(ENABLE_VERIFIER)
1584 /* If WIDE was used correctly, iswide should have been reset by now. */
1586 exceptions_throw_verifyerror(m,
1587 "Illegal instruction: WIDE before incompatible opcode");
1590 #endif /* defined(ENABLE_VERIFIER) */
1594 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1595 /* add a NOP to the last basic block */
1597 INSTRUCTIONS_CHECK(1);
1601 /*** END OF LOOP **********************************************************/
1603 /* assert that we did not write more ICMDs than allocated */
1605 assert(ircount <= pd.instructionslength);
1606 assert(ircount == (iptr - pd.instructions));
1608 /*** verifier checks ******************************************************/
1610 #if defined(ENABLE_VERIFIER)
1611 if (bcindex != m->jcodelength) {
1612 exceptions_throw_verifyerror(m,
1613 "Command-sequence crosses code-boundary");
1618 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1621 #endif /* defined(ENABLE_VERIFIER) */
1623 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1625 /* identify basic blocks */
1627 /* check if first instruction is a branch target */
1629 if (pd.basicblockstart[0] == 1) {
1630 jd->branchtoentry = true;
1633 /* first instruction always starts a basic block */
1635 iptr = pd.instructions;
1637 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1640 /* Iterate over all bytecode instructions and set missing
1641 basic-block starts in IR instructions. */
1643 for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1644 /* Does the current bytecode instruction start a basic
1647 if (pd.basicblockstart[bcindex] == 1) {
1648 #if defined(ENABLE_VERIFIER)
1649 /* Check if this bytecode basic-block start at the
1650 beginning of a bytecode instruction. */
1652 if (pd.bytecodestart[bcindex] == 0) {
1653 exceptions_throw_verifyerror(m,
1654 "Branch into middle of instruction");
1659 /* Get the IR instruction mapped to the bytecode
1660 instruction and set the basic block flag. */
1662 irindex = pd.bytecodemap[bcindex];
1663 iptr = pd.instructions + irindex;
1665 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1669 /* IR instruction index to basic-block index mapping */
1671 pd.instructionmap = DMNEW(s4, ircount);
1672 MZERO(pd.instructionmap, s4, ircount);
1674 /* Iterate over all IR instructions and count the basic blocks. */
1676 iptr = pd.instructions;
1680 for (i = 0; i < ircount; i++, iptr++) {
1681 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1682 /* store the basic-block number in the IR instruction
1685 pd.instructionmap[i] = bbcount;
1687 /* post-increment the basic-block count */
1693 /* Allocate basic block array (one more for end ipc). */
1695 jd->basicblocks = DMNEW(basicblock, bbcount + 1);
1696 MZERO(jd->basicblocks, basicblock, bbcount + 1);
1698 /* Now iterate again over all IR instructions and initialize the
1699 basic block structures and, in the same loop, resolve the
1700 branch-target instruction indices to basic blocks. */
1702 iptr = pd.instructions;
1703 bptr = jd->basicblocks;
1707 for (i = 0; i < ircount; i++, iptr++) {
1708 /* check for basic block */
1710 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1711 /* intialize the basic block */
1713 BASICBLOCK_INIT(bptr, m);
1715 bptr->iinstr = iptr;
1718 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1721 /* bptr->icount is set when the next block is allocated */
1723 bptr->nr = bbcount++;
1725 bptr[-1].next = bptr;
1728 /* resolve instruction indices to basic blocks */
1730 switch (iptr->opc) {
1738 case ICMD_IFNONNULL:
1739 case ICMD_IF_ICMPEQ:
1740 case ICMD_IF_ICMPNE:
1741 case ICMD_IF_ICMPLT:
1742 case ICMD_IF_ICMPGT:
1743 case ICMD_IF_ICMPLE:
1744 case ICMD_IF_ICMPGE:
1745 case ICMD_IF_ACMPEQ:
1746 case ICMD_IF_ACMPNE:
1748 BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1752 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1755 case ICMD_TABLESWITCH:
1756 table = iptr->dst.table;
1758 BYTECODEINDEX_TO_BASICBLOCK(*table);
1761 j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1764 BYTECODEINDEX_TO_BASICBLOCK(*table);
1769 case ICMD_LOOKUPSWITCH:
1770 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1772 lookup = iptr->dst.lookup;
1774 j = iptr->sx.s23.s2.lookupcount;
1777 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1784 /* set instruction count of last real block */
1787 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1790 /* allocate additional block at end */
1792 BASICBLOCK_INIT(bptr, m);
1795 /* set basicblock pointers in exception table */
1797 if (!parse_resolve_exception_table(jd, &pd))
1800 /* store the local map */
1802 jd->local_map = local_map;
1804 /* calculate local variable renaming */
1816 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1817 /* variable index for each _used_ (javaindex,type) pair. */
1818 /* (local_map[javaindex*5+type] = cacaoindex) */
1819 /* Unused (javaindex,type) pairs are marked with UNUSED. */
1821 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1823 *mapptr = nlocals++;
1828 jd->localcount = nlocals;
1830 /* calculate the (maximum) number of variables needed */
1833 nlocals /* local variables */
1834 + bbcount * m->maxstack /* invars */
1835 + s_count; /* variables created within blocks (non-invar) */
1837 /* reserve the first indices for local variables */
1839 jd->vartop = nlocals;
1841 /* reserve extra variables needed by stack analyse */
1843 jd->varcount += STACK_EXTRA_VARS;
1844 jd->vartop += STACK_EXTRA_VARS;
1846 /* The verifier needs space for saving invars in some cases and */
1847 /* extra variables. */
1849 #if defined(ENABLE_VERIFIER)
1850 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1851 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1853 /* allocate and initialize the variable array */
1855 jd->var = DMNEW(varinfo, jd->varcount);
1856 MZERO(jd->var, varinfo, jd->varcount);
1858 /* set types of all locals in jd->var */
1859 /* and fill the reverselocalmap */
1861 reversemap = DMNEW(s4, nlocals);
1863 for (i = 0; i < m->maxlocals; i++)
1864 for (t=0; t<5; t++) {
1865 varindex = local_map[5*i + t];
1866 if (varindex != UNUSED) {
1867 VAR(varindex)->type = t;
1868 reversemap[varindex] = i;
1872 jd->reverselocalmap = reversemap;
1875 /* assign local variables to method variables */
1877 jd->instructions = pd.instructions;
1878 jd->instructioncount = ircount;
1879 jd->basicblockcount = bbcount;
1880 jd->stackcount = s_count + bbcount * m->maxstack; /* in-stacks */
1882 /* allocate stack table */
1884 jd->stack = DMNEW(stackelement_t, jd->stackcount);
1886 /* everything's ok */
1890 /*** goto labels for throwing verifier exceptions *************************/
1892 #if defined(ENABLE_VERIFIER)
1894 throw_unexpected_end_of_bytecode:
1895 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1898 throw_invalid_bytecode_index:
1899 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1902 throw_illegal_local_variable_number:
1903 exceptions_throw_verifyerror(m, "Illegal local variable number");
1906 #endif /* ENABLE_VERIFIER */
1911 * These are local overrides for various environment variables in Emacs.
1912 * Please do not remove this and leave it at the end of the file, where
1913 * Emacs will automagically detect them.
1914 * ---------------------------------------------------------------------
1917 * indent-tabs-mode: t
1921 * vim:noexpandtab:sw=4:ts=4: