1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include "mm/memory.h"
37 #include "native/native.h"
39 #include "threads/lock-common.h"
41 #include "toolbox/logging.h"
43 #include "vm/builtin.h"
44 #include "vm/exceptions.h"
45 #include "vm/global.h"
46 #include "vm/stringlocal.h"
48 #include "vm/jit/asmpart.h"
49 #include "vm/jit/jit.h"
50 #include "vm/jit/parse.h"
51 #include "vm/jit/loop/loop.h"
53 #include "vm/jit/ir/bytecode.h"
55 #include "vmcore/linker.h"
56 #include "vmcore/loader.h"
57 #include "vmcore/options.h"
58 #include "vm/resolve.h"
60 #if defined(ENABLE_STATISTICS)
61 # include "vmcore/statistics.h"
64 #include "vmcore/suck.h"
66 #define INSTRUCTIONS_INCREMENT 5 /* number of additional instructions to */
67 /* allocate if space runs out */
70 /* local macros ***************************************************************/
72 #define BYTECODEINDEX_TO_BASICBLOCK(dst) \
75 parse_bytecodeindex_to_basicblock(jd, &pd, (dst).insindex); \
79 /* parserdata_t ***************************************************************/
81 typedef struct parsedata_t parsedata_t;
84 u1 *bytecodestart; /* start of bytecode instructions */
85 u1 *basicblockstart; /* start of bytecode basic-blocks */
87 s4 *bytecodemap; /* bytecode to IR mapping */
89 instruction *instructions; /* instruction array */
90 s4 instructionslength; /* length of the instruction array */
92 s4 *instructionmap; /* IR to basic-block mapping */
96 /* parse_setup *****************************************************************
98 Fills the passed parsedata_t structure.
100 *******************************************************************************/
102 static void parse_setup(jitdata *jd, parsedata_t *pd)
106 /* get required compiler data */
110 /* bytecode start array */
112 pd->bytecodestart = DMNEW(u1, m->jcodelength + 1);
113 MZERO(pd->bytecodestart, u1, m->jcodelength + 1);
115 /* bytecode basic-block start array */
117 pd->basicblockstart = DMNEW(u1, m->jcodelength + 1);
118 MZERO(pd->basicblockstart, u1, m->jcodelength + 1);
120 /* bytecode instruction index to IR instruction mapping */
122 pd->bytecodemap = DMNEW(s4, m->jcodelength + 1);
123 MSET(pd->bytecodemap, -1, s4, m->jcodelength + 1);
125 /* allocate the instruction array */
127 pd->instructionslength = m->jcodelength + 1;
128 pd->instructions = DMNEW(instruction, pd->instructionslength);
130 /* Zero the intermediate instructions array so we don't have any
131 invalid pointers in it if we cannot finish stack_analyse(). */
133 MZERO(pd->instructions, instruction, pd->instructionslength);
135 /* The instructionmap is allocated later when we know the count of
138 pd->instructionmap = NULL;
142 /* parse_realloc_instructions **************************************************
144 Reallocate the instructions array so there is room for at least N
145 additional instructions.
148 the new value for iptr
150 *******************************************************************************/
152 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 icount, s4 n)
154 /* increase the size of the instruction array */
156 pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
158 /* reallocate the array */
160 pd->instructions = DMREALLOC(pd->instructions, instruction, icount,
161 pd->instructionslength);
162 MZERO(pd->instructions + icount, instruction,
163 (pd->instructionslength - icount));
165 /* return the iptr */
167 return pd->instructions + icount;
171 /* parse_bytecodeindex_to_basicblock *******************************************
173 Resolves a bytecode index to the corresponding basic block.
175 *******************************************************************************/
177 static basicblock *parse_bytecodeindex_to_basicblock(jitdata *jd,
184 irindex = pd->bytecodemap[bcindex];
185 bb = jd->basicblocks + pd->instructionmap[irindex];
191 /* parse_mark_exception_boundaries *********************************************
193 Mark exception handlers and the boundaries of the handled regions as
194 basic block boundaries.
197 jd...............current jitdata
200 true.............everything ok
201 false............an exception has been thrown
203 *******************************************************************************/
205 static bool parse_mark_exception_boundaries(jitdata *jd, parsedata_t *pd)
210 raw_exception_entry *rex;
215 len = m->rawexceptiontablelength;
220 rex = m->rawexceptiontable;
222 for (i = 0; i < len; ++i, ++rex) {
224 /* the start of the handled region becomes a basic block start */
226 bcindex = rex->startpc;
227 CHECK_BYTECODE_INDEX(bcindex);
228 MARK_BASICBLOCK(pd, bcindex);
230 bcindex = rex->endpc; /* see JVM Spec 4.7.3 */
231 CHECK_BYTECODE_INDEX_EXCLUSIVE(bcindex);
233 /* check that the range is valid */
235 #if defined(ENABLE_VERIFIER)
236 if (bcindex <= rex->startpc) {
237 exceptions_throw_verifyerror(m, "Invalid exception handler range");
242 /* End of handled region becomes a basic block boundary (if it
243 is the bytecode end, we'll use the special end block that
244 is created anyway). */
246 if (bcindex < m->jcodelength)
247 MARK_BASICBLOCK(pd, bcindex);
249 jd->branchtoend = true;
251 /* the start of the handler becomes a basic block start */
253 bcindex = rex->handlerpc;
254 CHECK_BYTECODE_INDEX(bcindex);
255 MARK_BASICBLOCK(pd, bcindex);
262 #if defined(ENABLE_VERIFIER)
263 throw_invalid_bytecode_index:
264 exceptions_throw_verifyerror(m,
265 "Illegal bytecode index in exception table");
271 /* parse_resolve_exception_table ***********************************************
273 Enter the exception handlers and their ranges, resolved to basicblock *s,
277 jd...............current jitdata
280 true.............everything ok
281 false............an exception has been thrown
283 *******************************************************************************/
285 static bool parse_resolve_exception_table(jitdata *jd, parsedata_t *pd)
288 raw_exception_entry *rex;
296 len = m->rawexceptiontablelength;
298 /* common case: no handler entries */
303 /* allocate the exception table */
305 jd->exceptiontablelength = len;
306 jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
308 /* copy and resolve the entries */
310 ex = jd->exceptiontable;
311 rex = m->rawexceptiontable;
313 for (i = 0; i < len; ++i, ++rex, ++ex) {
314 /* resolve instruction indices to basic blocks */
316 ex->start = parse_bytecodeindex_to_basicblock(jd, pd, rex->startpc);
317 ex->end = parse_bytecodeindex_to_basicblock(jd, pd, rex->endpc);
318 ex->handler = parse_bytecodeindex_to_basicblock(jd, pd, rex->handlerpc);
320 /* lazily resolve the catchtype */
322 if (rex->catchtype.any != NULL) {
323 if (!resolve_classref_or_classinfo(m,
325 resolveLazy, true, false,
329 /* if resolved, enter the result of resolution in the table */
332 rex->catchtype.cls = exclass;
335 ex->catchtype = rex->catchtype;
336 ex->next = NULL; /* set by loop analysis */
337 ex->down = ex + 1; /* link to next exception entry */
340 /* terminate the ->down linked list */
342 assert(ex != jd->exceptiontable);
349 /*******************************************************************************
351 function 'parse' scans the JavaVM code and generates intermediate code
353 During parsing the block index table is used to store at bit pos 0
354 a flag which marks basic block starts and at position 1 to 31 the
355 intermediate instruction index. After parsing the block index table
356 is scanned, for marked positions a block is generated and the block
357 number is stored in the block index table.
359 *******************************************************************************/
361 /*** macro for checking the length of the bytecode ***/
363 #if defined(ENABLE_VERIFIER)
364 #define CHECK_END_OF_BYTECODE(neededlength) \
366 if ((neededlength) > m->jcodelength) \
367 goto throw_unexpected_end_of_bytecode; \
369 #else /* !ENABLE_VERIFIER */
370 #define CHECK_END_OF_BYTECODE(neededlength)
371 #endif /* ENABLE_VERIFIER */
373 bool parse(jitdata *jd)
375 methodinfo *m; /* method being parsed */
378 instruction *iptr; /* current ptr into instruction array */
380 s4 bcindex; /* bytecode instruction index */
381 s4 nextbc; /* start of next bytecode instruction */
382 s4 opcode; /* bytecode instruction opcode */
384 s4 irindex; /* IR instruction index */
385 s4 ircount; /* IR instruction count */
387 s4 bbcount; /* basic block count */
389 int s_count = 0; /* stack element counter */
390 bool blockend; /* true if basic block end has been reached */
391 bool iswide; /* true if last instruction was a wide */
393 constant_classref *cr;
394 constant_classref *compr;
396 builtintable_entry *bte;
397 constant_FMIref *fmi;
399 unresolved_method *um;
400 unresolved_field *uf;
402 resolve_result_t result;
409 int *local_map; /* local pointer to renaming map */
410 /* is assigned to rd->local_map at the end */
411 branch_target_t *table;
412 lookup_target_t *lookup;
416 /* get required compiler data */
421 /* allocate buffers for local variable renaming */
423 local_map = DMNEW(int, m->maxlocals * 5);
425 for (i = 0; i < m->maxlocals; i++) {
426 local_map[i * 5 + 0] = 0;
427 local_map[i * 5 + 1] = 0;
428 local_map[i * 5 + 2] = 0;
429 local_map[i * 5 + 3] = 0;
430 local_map[i * 5 + 4] = 0;
433 /* initialize the parse data structures */
435 parse_setup(jd, &pd);
437 /* initialize local variables */
439 iptr = pd.instructions;
445 /* mark basic block boundaries for exception table */
447 if (!parse_mark_exception_boundaries(jd, &pd))
450 /* initialize stack element counter */
452 s_count = 1 + m->rawexceptiontablelength;
454 /* setup line number info */
459 if (m->linenumbercount == 0) {
463 linepcchange = m->linenumbers[0].start_pc;
466 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
468 for (bcindex = 0; bcindex < m->jcodelength; bcindex = nextbc) {
470 /* mark this position as a valid bytecode instruction start */
472 pd.bytecodestart[bcindex] = 1;
474 /* change the current line number, if necessary */
476 /* XXX rewrite this using pointer arithmetic */
478 if (linepcchange == bcindex) {
479 if (m->linenumbercount > lineindex) {
481 currentline = m->linenumbers[lineindex].line_number;
483 if (lineindex < m->linenumbercount) {
484 linepcchange = m->linenumbers[lineindex].start_pc;
485 if (linepcchange == bcindex)
486 goto next_linenumber;
492 /* fetch next opcode */
494 opcode = SUCK_BE_U1(m->jcode + bcindex);
496 /* If the previous instruction was a block-end instruction,
497 mark the current bytecode instruction as basic-block
498 starting instruction. */
500 /* NOTE: Some compilers put a BC_nop after a blockend
503 if (blockend && (opcode != BC_nop)) {
504 MARK_BASICBLOCK(&pd, bcindex);
508 /* If the current bytecode instruction was marked as
509 basic-block starting instruction before (e.g. blockend,
510 forward-branch target), mark the current IR instruction
513 if (pd.basicblockstart[bcindex] != 0) {
514 /* We need a NOP as last instruction in each basic block
515 for basic block reordering (may be replaced with a GOTO
518 INSTRUCTIONS_CHECK(1);
522 /* store intermediate instruction count (bit 0 mark block starts) */
524 pd.bytecodemap[bcindex] = ircount;
526 /* compute next instruction start */
528 nextbc = bcindex + bytecode[opcode].length;
530 CHECK_END_OF_BYTECODE(nextbc);
532 /* add stack elements produced by this instruction */
534 s_count += bytecode[opcode].slots;
536 /* We check here for the space of 1 instruction in the
537 instruction array. If an opcode is converted to more than
538 1 instruction, this is checked in the corresponding
541 INSTRUCTIONS_CHECK(1);
543 /* translate this bytecode instruction */
549 /* pushing constants onto the stack ***********************************/
552 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + bcindex + 1));
556 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + bcindex + 1));
560 i = SUCK_BE_U1(m->jcode + bcindex + 1);
561 goto pushconstantitem;
565 i = SUCK_BE_U2(m->jcode + bcindex + 1);
569 #if defined(ENABLE_VERIFIER)
570 if (i >= m->class->cpcount) {
571 exceptions_throw_verifyerror(m,
572 "Attempt to access constant outside range");
577 switch (m->class->cptags[i]) {
578 case CONSTANT_Integer:
579 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
582 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
585 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
587 case CONSTANT_Double:
588 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
590 case CONSTANT_String:
591 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
594 cr = (constant_classref *) (m->class->cpinfos[i]);
596 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
599 /* if not resolved, c == NULL */
601 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
605 #if defined(ENABLE_VERIFIER)
607 exceptions_throw_verifyerror(m,
608 "Invalid constant type to push");
625 OP_LOADCONST_I(opcode - BC_iconst_0);
630 OP_LOADCONST_L(opcode - BC_lconst_0);
636 OP_LOADCONST_F(opcode - BC_fconst_0);
641 OP_LOADCONST_D(opcode - BC_dconst_0);
644 /* stack operations ***************************************************/
646 /* We need space for additional instruction so we can
647 translate these instructions to sequences of ICMD_COPY and
648 ICMD_MOVE instructions. */
651 INSTRUCTIONS_CHECK(4);
659 INSTRUCTIONS_CHECK(6);
669 INSTRUCTIONS_CHECK(2);
675 INSTRUCTIONS_CHECK(7);
686 INSTRUCTIONS_CHECK(9);
699 INSTRUCTIONS_CHECK(3);
705 /* local variable access instructions *********************************/
710 if (iswide == false) {
711 i = SUCK_BE_U1(m->jcode + bcindex + 1);
714 i = SUCK_BE_U2(m->jcode + bcindex + 1);
715 nextbc = bcindex + 3;
718 OP_LOAD_ONEWORD(opcode, i, opcode - BC_iload);
723 if (iswide == false) {
724 i = SUCK_BE_U1(m->jcode + bcindex + 1);
727 i = SUCK_BE_U2(m->jcode + bcindex + 1);
728 nextbc = bcindex + 3;
731 OP_LOAD_TWOWORD(opcode, i, opcode - BC_iload);
738 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - BC_iload_0, TYPE_INT);
745 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - BC_lload_0, TYPE_LNG);
752 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - BC_fload_0, TYPE_FLT);
759 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - BC_dload_0, TYPE_DBL);
766 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - BC_aload_0, TYPE_ADR);
772 if (iswide == false) {
773 i = SUCK_BE_U1(m->jcode + bcindex + 1);
776 i = SUCK_BE_U2(m->jcode + bcindex + 1);
777 nextbc = bcindex + 3;
780 OP_STORE_ONEWORD(opcode, i, opcode - BC_istore);
785 if (iswide == false) {
786 i = SUCK_BE_U1(m->jcode + bcindex + 1);
789 i = SUCK_BE_U2(m->jcode + bcindex + 1);
790 nextbc = bcindex + 3;
793 OP_STORE_TWOWORD(opcode, i, opcode - BC_istore);
800 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - BC_istore_0, TYPE_INT);
807 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - BC_lstore_0, TYPE_LNG);
814 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - BC_fstore_0, TYPE_FLT);
821 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - BC_dstore_0, TYPE_DBL);
828 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - BC_astore_0, TYPE_ADR);
835 if (iswide == false) {
836 i = SUCK_BE_U1(m->jcode + bcindex + 1);
837 v = SUCK_BE_S1(m->jcode + bcindex + 2);
840 i = SUCK_BE_U2(m->jcode + bcindex + 1);
841 v = SUCK_BE_S2(m->jcode + bcindex + 3);
842 nextbc = bcindex + 5;
846 LOCALTYPE_USED(i, TYPE_INT);
847 OP_LOCALINDEX_I(opcode, i, v);
851 /* wider index for loading, storing and incrementing ******************/
858 /* managing arrays ****************************************************/
861 switch (SUCK_BE_S1(m->jcode + bcindex + 1)) {
863 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
866 bte = builtintable_get_internal(BUILTIN_newarray_char);
869 bte = builtintable_get_internal(BUILTIN_newarray_float);
872 bte = builtintable_get_internal(BUILTIN_newarray_double);
875 bte = builtintable_get_internal(BUILTIN_newarray_byte);
878 bte = builtintable_get_internal(BUILTIN_newarray_short);
881 bte = builtintable_get_internal(BUILTIN_newarray_int);
884 bte = builtintable_get_internal(BUILTIN_newarray_long);
886 #if defined(ENABLE_VERIFIER)
888 exceptions_throw_verifyerror(m, "Invalid array-type to create");
892 OP_BUILTIN_CHECK_EXCEPTION(bte);
896 i = SUCK_BE_U2(m->jcode + bcindex + 1);
897 compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
901 if (!(cr = class_get_classref_multiarray_of(1, compr)))
904 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
907 INSTRUCTIONS_CHECK(2);
908 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
909 bte = builtintable_get_internal(BUILTIN_newarray);
910 OP_BUILTIN_CHECK_EXCEPTION(bte);
914 case BC_multianewarray:
915 i = SUCK_BE_U2(m->jcode + bcindex + 1);
916 j = SUCK_BE_U1(m->jcode + bcindex + 3);
918 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
922 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
925 /* if unresolved, c == NULL */
927 iptr->s1.argcount = j;
928 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
929 code_unflag_leafmethod(code);
932 /* control flow instructions ******************************************/
951 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
952 CHECK_BYTECODE_INDEX(i);
953 MARK_BASICBLOCK(&pd, i);
955 OP_INSINDEX(opcode, i);
959 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
960 CHECK_BYTECODE_INDEX(i);
961 MARK_BASICBLOCK(&pd, i);
963 OP_INSINDEX(ICMD_GOTO, i);
967 i = bcindex + SUCK_BE_S2(m->jcode + bcindex + 1);
969 CHECK_BYTECODE_INDEX(i);
970 MARK_BASICBLOCK(&pd, i);
972 OP_PREPARE_ZEROFLAGS(BC_jsr);
973 iptr->sx.s23.s3.jsrtarget.insindex = i;
978 i = bcindex + SUCK_BE_S4(m->jcode + bcindex + 1);
982 if (iswide == false) {
983 i = SUCK_BE_U1(m->jcode + bcindex + 1);
986 i = SUCK_BE_U2(m->jcode + bcindex + 1);
987 nextbc = bcindex + 3;
992 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
1002 /* XXX ARETURN will need a flag in the typechecker */
1008 /* XXX ATHROW will need a flag in the typechecker */
1013 /* table jumps ********************************************************/
1015 case BC_lookupswitch:
1018 lookup_target_t *lookup;
1019 #if defined(ENABLE_VERIFIER)
1023 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1025 CHECK_END_OF_BYTECODE(nextbc + 8);
1027 OP_PREPARE_ZEROFLAGS(opcode);
1029 /* default target */
1031 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1032 iptr->sx.s23.s3.lookupdefault.insindex = j;
1034 CHECK_BYTECODE_INDEX(j);
1035 MARK_BASICBLOCK(&pd, j);
1037 /* number of pairs */
1039 num = SUCK_BE_U4(m->jcode + nextbc);
1040 iptr->sx.s23.s2.lookupcount = num;
1043 /* allocate the intermediate code table */
1045 lookup = DMNEW(lookup_target_t, num);
1046 iptr->dst.lookup = lookup;
1048 /* iterate over the lookup table */
1050 CHECK_END_OF_BYTECODE(nextbc + 8 * num);
1052 for (i = 0; i < num; i++) {
1055 j = SUCK_BE_S4(m->jcode + nextbc);
1060 #if defined(ENABLE_VERIFIER)
1061 /* check if the lookup table is sorted correctly */
1063 if (i && (j <= prevvalue)) {
1064 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1071 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1072 lookup->target.insindex = j;
1075 CHECK_BYTECODE_INDEX(j);
1076 MARK_BASICBLOCK(&pd, j);
1083 case BC_tableswitch:
1087 branch_target_t *table;
1090 nextbc = MEMORY_ALIGN((bcindex + 1), 4);
1092 CHECK_END_OF_BYTECODE(nextbc + 12);
1094 OP_PREPARE_ZEROFLAGS(opcode);
1096 /* default target */
1098 deftarget = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1100 CHECK_BYTECODE_INDEX(deftarget);
1101 MARK_BASICBLOCK(&pd, deftarget);
1105 j = SUCK_BE_S4(m->jcode + nextbc);
1106 iptr->sx.s23.s2.tablelow = j;
1111 num = SUCK_BE_S4(m->jcode + nextbc);
1112 iptr->sx.s23.s3.tablehigh = num;
1115 /* calculate the number of table entries */
1119 #if defined(ENABLE_VERIFIER)
1121 exceptions_throw_verifyerror(m,
1122 "invalid TABLESWITCH: upper bound < lower bound");
1126 /* create the intermediate code table */
1127 /* the first entry is the default target */
1129 table = DMNEW(branch_target_t, 1 + num);
1130 iptr->dst.table = table;
1131 (table++)->insindex = deftarget;
1133 /* iterate over the target table */
1135 CHECK_END_OF_BYTECODE(nextbc + 4 * num);
1137 for (i = 0; i < num; i++) {
1138 j = bcindex + SUCK_BE_S4(m->jcode + nextbc);
1139 (table++)->insindex = j;
1141 CHECK_BYTECODE_INDEX(j);
1142 MARK_BASICBLOCK(&pd, j);
1150 /* load and store of object fields ************************************/
1154 code_unflag_leafmethod(code);
1161 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1162 fmi = class_getconstant(m->class, i, CONSTANT_Fieldref);
1167 OP_PREPARE_ZEROFLAGS(opcode);
1168 iptr->sx.s23.s3.fmiref = fmi;
1170 /* only with -noverify, otherwise the typechecker does this */
1172 #if defined(ENABLE_VERIFIER)
1173 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1175 result = resolve_field_lazy(m, fmi);
1177 if (result == resolveFailed)
1180 if (result != resolveSucceeded) {
1181 uf = resolve_create_unresolved_field(m->class, m, iptr);
1186 /* store the unresolved_field pointer */
1188 iptr->sx.s23.s3.uf = uf;
1189 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1191 #if defined(ENABLE_VERIFIER)
1198 /* method invocation **************************************************/
1200 case BC_invokestatic:
1201 OP_PREPARE_ZEROFLAGS(opcode);
1203 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1204 fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1209 md = fmi->parseddesc.md;
1211 if (md->params == NULL)
1212 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1217 case BC_invokespecial:
1218 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1220 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1221 fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1223 goto invoke_nonstatic_method;
1225 case BC_invokeinterface:
1226 OP_PREPARE_ZEROFLAGS(opcode);
1228 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1229 fmi = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1231 goto invoke_nonstatic_method;
1233 case BC_invokevirtual:
1234 OP_PREPARE_ZEROFLAGS(opcode);
1236 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1237 fmi = class_getconstant(m->class, i, CONSTANT_Methodref);
1239 invoke_nonstatic_method:
1243 md = fmi->parseddesc.md;
1245 if (md->params == NULL)
1246 if (!descriptor_params_from_paramtypes(md, 0))
1250 code_unflag_leafmethod(code);
1252 iptr->sx.s23.s3.fmiref = fmi;
1254 /* only with -noverify, otherwise the typechecker does this */
1256 #if defined(ENABLE_VERIFIER)
1257 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1259 result = resolve_method_lazy(m, fmi,
1260 (opcode == BC_invokespecial));
1262 if (result == resolveFailed)
1265 if (result == resolveSucceeded) {
1266 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1268 /* if this call is monomorphic, turn it into an
1271 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1273 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1274 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1276 iptr->opc = ICMD_INVOKESPECIAL;
1277 iptr->flags.bits |= INS_FLAG_CHECK;
1281 um = resolve_create_unresolved_method(m->class, m, fmi,
1282 (opcode == BC_invokestatic),
1283 (opcode == BC_invokespecial));
1288 /* store the unresolved_method pointer */
1290 iptr->sx.s23.s3.um = um;
1291 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1293 #if defined(ENABLE_VERIFIER)
1299 /* instructions taking class arguments ********************************/
1302 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1303 cr = class_getconstant(m->class, i, CONSTANT_Class);
1308 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1311 INSTRUCTIONS_CHECK(2);
1312 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1313 bte = builtintable_get_internal(BUILTIN_new);
1314 OP_BUILTIN_CHECK_EXCEPTION(bte);
1319 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1320 cr = class_getconstant(m->class, i, CONSTANT_Class);
1325 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1328 if (cr->name->text[0] == '[') {
1329 /* array type cast-check */
1330 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1331 code_unflag_leafmethod(code);
1334 /* object type cast-check */
1335 flags = INS_FLAG_CHECK;
1337 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1341 i = SUCK_BE_U2(m->jcode + bcindex + 1);
1342 cr = class_getconstant(m->class, i, CONSTANT_Class);
1347 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1350 if (cr->name->text[0] == '[') {
1351 /* array type cast-check */
1352 INSTRUCTIONS_CHECK(2);
1353 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1354 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1355 OP_BUILTIN_NO_EXCEPTION(bte);
1359 /* object type cast-check */
1360 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1364 /* synchronization instructions ***************************************/
1366 case BC_monitorenter:
1367 #if defined(ENABLE_THREADS)
1369 bte = builtintable_get_internal(LOCK_monitor_enter);
1370 OP_BUILTIN_CHECK_EXCEPTION(bte);
1375 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1380 case BC_monitorexit:
1381 #if defined(ENABLE_THREADS)
1383 bte = builtintable_get_internal(LOCK_monitor_exit);
1384 OP_BUILTIN_CHECK_EXCEPTION(bte);
1389 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1394 /* arithmetic instructions that may become builtin functions **********/
1397 #if !SUPPORT_DIVISION
1398 bte = builtintable_get_internal(BUILTIN_idiv);
1399 OP_BUILTIN_ARITHMETIC(opcode, bte);
1401 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1404 OP_CHECK_EXCEPTION(opcode);
1410 #if !SUPPORT_DIVISION
1411 bte = builtintable_get_internal(BUILTIN_irem);
1412 OP_BUILTIN_ARITHMETIC(opcode, bte);
1414 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1417 OP_CHECK_EXCEPTION(opcode);
1423 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1424 bte = builtintable_get_internal(BUILTIN_ldiv);
1425 OP_BUILTIN_ARITHMETIC(opcode, bte);
1427 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1430 OP_CHECK_EXCEPTION(opcode);
1436 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1437 bte = builtintable_get_internal(BUILTIN_lrem);
1438 OP_BUILTIN_ARITHMETIC(opcode, bte);
1440 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1443 OP_CHECK_EXCEPTION(opcode);
1449 #if defined(__I386__)
1452 bte = builtintable_get_internal(BUILTIN_frem);
1453 OP_BUILTIN_NO_EXCEPTION(bte);
1458 #if defined(__I386__)
1461 bte = builtintable_get_internal(BUILTIN_drem);
1462 OP_BUILTIN_NO_EXCEPTION(bte);
1467 #if defined(__ALPHA__)
1469 bte = builtintable_get_internal(BUILTIN_f2i);
1470 OP_BUILTIN_NO_EXCEPTION(bte);
1480 #if defined(__ALPHA__)
1482 bte = builtintable_get_internal(BUILTIN_f2l);
1483 OP_BUILTIN_NO_EXCEPTION(bte);
1493 #if defined(__ALPHA__)
1495 bte = builtintable_get_internal(BUILTIN_d2i);
1496 OP_BUILTIN_NO_EXCEPTION(bte);
1506 #if defined(__ALPHA__)
1508 bte = builtintable_get_internal(BUILTIN_d2l);
1509 OP_BUILTIN_NO_EXCEPTION(bte);
1519 /* invalid opcodes ****************************************************/
1521 /* check for invalid opcodes if the verifier is enabled */
1522 #if defined(ENABLE_VERIFIER)
1524 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1528 /* Unused opcodes ************************************************** */
1584 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1588 #endif /* defined(ENABLE_VERIFIER) */
1590 /* opcodes that don't require translation *****************************/
1593 /* Straight-forward translation to HIR. */
1599 /* verifier checks ****************************************************/
1601 #if defined(ENABLE_VERIFIER)
1602 /* If WIDE was used correctly, iswide should have been reset by now. */
1604 exceptions_throw_verifyerror(m,
1605 "Illegal instruction: WIDE before incompatible opcode");
1608 #endif /* defined(ENABLE_VERIFIER) */
1612 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1613 /* add a NOP to the last basic block */
1615 INSTRUCTIONS_CHECK(1);
1619 /*** END OF LOOP **********************************************************/
1621 /* assert that we did not write more ICMDs than allocated */
1623 assert(ircount <= pd.instructionslength);
1624 assert(ircount == (iptr - pd.instructions));
1626 /*** verifier checks ******************************************************/
1628 #if defined(ENABLE_VERIFIER)
1629 if (bcindex != m->jcodelength) {
1630 exceptions_throw_verifyerror(m,
1631 "Command-sequence crosses code-boundary");
1636 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1639 #endif /* defined(ENABLE_VERIFIER) */
1641 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1643 /* identify basic blocks */
1645 /* check if first instruction is a branch target */
1647 if (pd.basicblockstart[0] == 1) {
1648 jd->branchtoentry = true;
1651 /* first instruction always starts a basic block */
1653 iptr = pd.instructions;
1655 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1658 /* Iterate over all bytecode instructions and set missing
1659 basic-block starts in IR instructions. */
1661 for (bcindex = 0; bcindex < m->jcodelength; bcindex++) {
1662 /* Does the current bytecode instruction start a basic
1665 if (pd.basicblockstart[bcindex] == 1) {
1666 #if defined(ENABLE_VERIFIER)
1667 /* Check if this bytecode basic-block start at the
1668 beginning of a bytecode instruction. */
1670 if (pd.bytecodestart[bcindex] == 0) {
1671 exceptions_throw_verifyerror(m,
1672 "Branch into middle of instruction");
1677 /* Get the IR instruction mapped to the bytecode
1678 instruction and set the basic block flag. */
1680 irindex = pd.bytecodemap[bcindex];
1681 iptr = pd.instructions + irindex;
1683 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1687 /* IR instruction index to basic-block index mapping */
1689 pd.instructionmap = DMNEW(s4, ircount);
1690 MZERO(pd.instructionmap, s4, ircount);
1692 /* Iterate over all IR instructions and count the basic blocks. */
1694 iptr = pd.instructions;
1698 for (i = 0; i < ircount; i++, iptr++) {
1699 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1700 /* store the basic-block number in the IR instruction
1703 pd.instructionmap[i] = bbcount;
1705 /* post-increment the basic-block count */
1711 /* Allocate basic block array (one more for end ipc). */
1713 jd->basicblocks = DMNEW(basicblock, bbcount + 1);
1714 MZERO(jd->basicblocks, basicblock, bbcount + 1);
1716 /* Now iterate again over all IR instructions and initialize the
1717 basic block structures and, in the same loop, resolve the
1718 branch-target instruction indices to basic blocks. */
1720 iptr = pd.instructions;
1721 bptr = jd->basicblocks;
1725 for (i = 0; i < ircount; i++, iptr++) {
1726 /* check for basic block */
1728 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1729 /* intialize the basic block */
1731 BASICBLOCK_INIT(bptr, m);
1733 bptr->iinstr = iptr;
1736 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1739 /* bptr->icount is set when the next block is allocated */
1741 bptr->nr = bbcount++;
1743 bptr[-1].next = bptr;
1746 /* resolve instruction indices to basic blocks */
1748 switch (iptr->opc) {
1756 case ICMD_IFNONNULL:
1757 case ICMD_IF_ICMPEQ:
1758 case ICMD_IF_ICMPNE:
1759 case ICMD_IF_ICMPLT:
1760 case ICMD_IF_ICMPGT:
1761 case ICMD_IF_ICMPLE:
1762 case ICMD_IF_ICMPGE:
1763 case ICMD_IF_ACMPEQ:
1764 case ICMD_IF_ACMPNE:
1766 BYTECODEINDEX_TO_BASICBLOCK(iptr->dst);
1770 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.jsrtarget);
1773 case ICMD_TABLESWITCH:
1774 table = iptr->dst.table;
1776 BYTECODEINDEX_TO_BASICBLOCK(*table);
1779 j = iptr->sx.s23.s3.tablehigh - iptr->sx.s23.s2.tablelow + 1;
1782 BYTECODEINDEX_TO_BASICBLOCK(*table);
1787 case ICMD_LOOKUPSWITCH:
1788 BYTECODEINDEX_TO_BASICBLOCK(iptr->sx.s23.s3.lookupdefault);
1790 lookup = iptr->dst.lookup;
1792 j = iptr->sx.s23.s2.lookupcount;
1795 BYTECODEINDEX_TO_BASICBLOCK(lookup->target);
1802 /* set instruction count of last real block */
1805 bptr[-1].icount = (pd.instructions + ircount) - bptr[-1].iinstr;
1808 /* allocate additional block at end */
1810 BASICBLOCK_INIT(bptr, m);
1813 /* set basicblock pointers in exception table */
1815 if (!parse_resolve_exception_table(jd, &pd))
1818 /* store the local map */
1820 jd->local_map = local_map;
1822 /* calculate local variable renaming */
1831 /* iterate over local_map[0..m->maxlocals*5-1] and allocate a unique */
1832 /* variable index for each _used_ (javaindex,type) pair. */
1833 /* (local_map[javaindex*5+type] = cacaoindex) */
1834 /* Unused (javaindex,type) pairs are marked with UNUSED. */
1836 for (i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1838 *mapptr = nlocals++;
1843 jd->localcount = nlocals;
1845 /* calculate the (maximum) number of variables needed */
1848 nlocals /* local variables */
1849 + bbcount * m->maxstack /* invars */
1850 + s_count; /* variables created within blocks (non-invar) */
1852 /* reserve the first indices for local variables */
1854 jd->vartop = nlocals;
1856 /* reserve extra variables needed by stack analyse */
1858 jd->varcount += STACK_EXTRA_VARS;
1859 jd->vartop += STACK_EXTRA_VARS;
1861 /* The verifier needs space for saving invars in some cases and */
1862 /* extra variables. */
1864 #if defined(ENABLE_VERIFIER)
1865 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1866 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1868 /* allocate and initialize the variable array */
1870 jd->var = DMNEW(varinfo, jd->varcount);
1871 MZERO(jd->var, varinfo, jd->varcount);
1873 /* set types of all locals in jd->var */
1875 for (mapptr = local_map, i = 0; i < (m->maxlocals * 5); i++, mapptr++)
1876 if (*mapptr != UNUSED)
1877 VAR(*mapptr)->type = i%5;
1880 /* assign local variables to method variables */
1882 jd->instructions = pd.instructions;
1883 jd->instructioncount = ircount;
1884 jd->basicblockcount = bbcount;
1885 jd->stackcount = s_count + bbcount * m->maxstack; /* in-stacks */
1887 /* allocate stack table */
1889 jd->stack = DMNEW(stackelement, jd->stackcount);
1891 /* everything's ok */
1895 /*** goto labels for throwing verifier exceptions *************************/
1897 #if defined(ENABLE_VERIFIER)
1899 throw_unexpected_end_of_bytecode:
1900 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1903 throw_invalid_bytecode_index:
1904 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1907 throw_illegal_local_variable_number:
1908 exceptions_throw_verifyerror(m, "Illegal local variable number");
1911 #endif /* ENABLE_VERIFIER */
1916 * These are local overrides for various environment variables in Emacs.
1917 * Please do not remove this and leave it at the end of the file, where
1918 * Emacs will automagically detect them.
1919 * ---------------------------------------------------------------------
1922 * indent-tabs-mode: t
1926 * vim:noexpandtab:sw=4:ts=4: