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
25 $Id: parse.c 7632 2007-04-02 20:05:05Z michi $
37 #include "mm/memory.h"
38 #include "native/native.h"
40 #if defined(ENABLE_THREADS)
41 # include "threads/native/lock.h"
44 #include "toolbox/logging.h"
46 #include "vm/builtin.h"
47 #include "vm/exceptions.h"
48 #include "vm/global.h"
49 #include "vm/stringlocal.h"
51 #include "vm/jit/asmpart.h"
52 #include "vm/jit/jit.h"
53 #include "vm/jit/parse.h"
54 #include "vm/jit/patcher.h"
55 #include "vm/jit/loop/loop.h"
57 #include "vmcore/linker.h"
58 #include "vmcore/loader.h"
59 #include "vmcore/options.h"
60 #include "vm/resolve.h"
62 #if defined(ENABLE_STATISTICS)
63 # include "vmcore/statistics.h"
66 #include "vmcore/suck.h"
68 #define INSTRUCTIONS_INCREMENT 5 /* number of additional instructions to */
69 /* allocate if space runs out */
72 /* parserdata_t ***************************************************************/
74 typedef struct parsedata_t parsedata_t;
77 instruction *instructions; /* instruction array */
78 s4 instructionslength; /* length of the instruction array */
83 /* parse_setup *****************************************************************
85 Fills the passed parsedata_t structure.
87 *******************************************************************************/
89 static void parse_setup(jitdata *jd, parsedata_t *pd)
93 /* get required compiler data */
97 /* Allocate instruction array and block index table (1 additional
100 jd->basicblockindex = DMNEW(s4, m->jcodelength + 1);
101 pd->instructionstart = DMNEW(u1, m->jcodelength + 1);
103 MZERO(jd->basicblockindex, s4, m->jcodelength + 1);
104 MZERO(pd->instructionstart, u1, m->jcodelength + 1);
106 /* Set the length of the instruction array. We simply add 5 more
107 instruction, as this seems to be a reasonable value. */
109 pd->instructionslength = m->jcodelength + 1;
111 /* allocate the instruction array */
113 pd->instructions = DMNEW(instruction, pd->instructionslength);
115 /* Zero the intermediate instructions array so we don't have any
116 invalid pointers in it if we cannot finish stack_analyse(). */
118 MZERO(pd->instructions, instruction, pd->instructionslength);
122 /* parse_realloc_instructions **************************************************
124 Reallocate the instructions array so there is room for at least N
125 additional instructions.
128 the new value for iptr
130 *******************************************************************************/
132 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 icount, s4 n)
134 /* increase the size of the instruction array */
136 pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
138 /* reallocate the array */
140 pd->instructions = DMREALLOC(pd->instructions, instruction, icount,
141 pd->instructionslength);
142 MZERO(pd->instructions + icount, instruction,
143 (pd->instructionslength - icount));
145 /* return the iptr */
147 return pd->instructions + icount;
151 /* parse_mark_exception_boundaries *********************************************
153 Mark exception handlers and the boundaries of the handled regions as
154 basic block boundaries.
157 jd...............current jitdata
160 >= 0.............the number of new basic blocks marked
161 -1...............an exception has been thrown
163 *******************************************************************************/
165 static int parse_mark_exception_boundaries(jitdata *jd)
171 raw_exception_entry *rex;
176 len = m->rawexceptiontablelength;
182 rex = m->rawexceptiontable;
184 for (i = 0; i < len; ++i, ++rex) {
186 /* the start of the handled region becomes a basic block start */
189 CHECK_BYTECODE_INDEX(pc);
192 pc = rex->endpc; /* see JVM Spec 4.7.3 */
193 CHECK_BYTECODE_INDEX_EXCLUSIVE(pc);
195 /* check that the range is valid */
197 #if defined(ENABLE_VERIFIER)
198 if (pc <= rex->startpc) {
199 exceptions_throw_verifyerror(m,
200 "Invalid exception handler range");
205 /* end of handled region becomes a basic block boundary */
206 /* (If it is the bytecode end, we'll use the special */
207 /* end block that is created anyway.) */
209 if (pc < m->jcodelength)
212 jd->branchtoend = true;
214 /* the start of the handler becomes a basic block start */
217 CHECK_BYTECODE_INDEX(pc);
225 #if defined(ENABLE_VERIFIER)
226 throw_invalid_bytecode_index:
227 exceptions_throw_verifyerror(m,
228 "Illegal bytecode index in exception table");
234 /* parse_resolve_exception_table ***********************************************
236 Enter the exception handlers and their ranges, resolved to basicblock *s,
240 jd...............current jitdata
243 true.............everything ok
244 false............an exception has been thrown
246 *******************************************************************************/
248 static bool parse_resolve_exception_table(jitdata *jd)
251 raw_exception_entry *rex;
259 len = m->rawexceptiontablelength;
261 /* common case: no handler entries */
266 /* allocate the exception table */
268 jd->exceptiontablelength = len;
269 jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
271 /* copy and resolve the entries */
273 ex = jd->exceptiontable;
274 rex = m->rawexceptiontable;
276 for (i = 0; i < len; ++i, ++rex, ++ex) {
277 /* resolve instruction indices to basic blocks */
279 ex->start = BLOCK_OF(rex->startpc);
280 ex->end = BLOCK_OF(rex->endpc);
281 ex->handler = BLOCK_OF(rex->handlerpc);
283 /* lazily resolve the catchtype */
285 if (rex->catchtype.any != NULL) {
286 if (!resolve_classref_or_classinfo(m,
288 resolveLazy, true, false,
292 /* if resolved, enter the result of resolution in the table */
295 rex->catchtype.cls = exclass;
298 ex->catchtype = rex->catchtype;
299 ex->next = NULL; /* set by loop analysis */
300 ex->down = ex + 1; /* link to next exception entry */
303 /* terminate the ->down linked list */
305 assert(ex != jd->exceptiontable);
312 /*******************************************************************************
314 function 'parse' scans the JavaVM code and generates intermediate code
316 During parsing the block index table is used to store at bit pos 0
317 a flag which marks basic block starts and at position 1 to 31 the
318 intermediate instruction index. After parsing the block index table
319 is scanned, for marked positions a block is generated and the block
320 number is stored in the block index table.
322 *******************************************************************************/
324 /*** macro for checking the length of the bytecode ***/
326 #if defined(ENABLE_VERIFIER)
327 #define CHECK_END_OF_BYTECODE(neededlength) \
329 if ((neededlength) > m->jcodelength) \
330 goto throw_unexpected_end_of_bytecode; \
332 #else /* !ENABLE_VERIFIER */
333 #define CHECK_END_OF_BYTECODE(neededlength)
334 #endif /* ENABLE_VERIFIER */
336 bool parse(jitdata *jd)
338 methodinfo *m; /* method being parsed */
340 instruction *iptr; /* current ptr into instruction array */
341 s4 icount; /* intermediate instruction counter */
342 s4 p; /* java instruction counter */
343 s4 nextp; /* start of next java instruction */
344 s4 opcode; /* java opcode */
347 int b_count; /* basic block counter */
348 int s_count = 0; /* stack element counter */
349 bool blockend = false; /* true if basic block end has been reached */
350 bool iswide = false; /* true if last instruction was a wide */
351 constant_classref *cr;
352 constant_classref *compr;
354 builtintable_entry *bte;
357 unresolved_method *um;
358 resolve_result_t result;
365 int *local_map; /* local pointer to renaming structore */
366 /* is assigned to rd->local_map at the end */
367 /* get required compiler data */
371 /* allocate buffers for local variable renaming */
372 local_map = DMNEW(int, m->maxlocals * 5);
373 for (i = 0; i < m->maxlocals; i++) {
374 local_map[i * 5 + 0] = 0;
375 local_map[i * 5 + 1] = 0;
376 local_map[i * 5 + 2] = 0;
377 local_map[i * 5 + 3] = 0;
378 local_map[i * 5 + 4] = 0;
381 /* initialize the parse data structures */
383 parse_setup(jd, &pd);
385 /* initialize local variables */
387 iptr = pd.instructions;
390 /* mark basic block boundaries for exception table */
392 b_count = parse_mark_exception_boundaries(jd);
396 /* initialize stack element counter */
398 s_count = 1 + m->rawexceptiontablelength;
400 /* setup line number info */
405 if (m->linenumbercount == 0) {
409 linepcchange = m->linenumbers[0].start_pc;
412 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
414 for (p = 0; p < m->jcodelength; p = nextp) {
416 /* mark this position as a valid instruction start */
418 pd.instructionstart[p] = 1;
420 /* change the current line number, if necessary */
422 /* XXX rewrite this using pointer arithmetic */
424 if (linepcchange == p) {
425 if (m->linenumbercount > lineindex) {
427 currentline = m->linenumbers[lineindex].line_number;
429 if (lineindex < m->linenumbercount) {
430 linepcchange = m->linenumbers[lineindex].start_pc;
431 if (linepcchange == p)
432 goto next_linenumber;
438 /* fetch next opcode */
440 opcode = SUCK_BE_U1(m->jcode + p);
442 /* some compilers put a JAVA_NOP after a blockend instruction */
444 if (blockend && (opcode != JAVA_NOP)) {
445 /* start new block */
451 /* We need a NOP as last instruction in each basic block
452 for basic block reordering (may be replaced with a GOTO
455 if (jd->basicblockindex[p] & 1) {
456 INSTRUCTIONS_CHECK(1);
460 /* store intermediate instruction count (bit 0 mark block starts) */
462 jd->basicblockindex[p] |= (icount << 1);
464 /* compute next instruction start */
466 nextp = p + jcommandsize[opcode];
468 CHECK_END_OF_BYTECODE(nextp);
470 /* add stack elements produced by this instruction */
472 s_count += stackreq[opcode];
474 /* We check here for the space of 1 instruction in the
475 instruction array. If an opcode is converted to more than
476 1 instruction, this is checked in the corresponding
479 INSTRUCTIONS_CHECK(1);
481 /* translate this bytecode instruction */
487 /* pushing constants onto the stack ***********************************/
490 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + p + 1));
494 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + p + 1));
498 i = SUCK_BE_U1(m->jcode + p + 1);
499 goto pushconstantitem;
503 i = SUCK_BE_U2(m->jcode + p + 1);
507 #if defined(ENABLE_VERIFIER)
508 if (i >= m->class->cpcount) {
509 exceptions_throw_verifyerror(m,
510 "Attempt to access constant outside range");
515 switch (m->class->cptags[i]) {
516 case CONSTANT_Integer:
517 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
520 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
523 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
525 case CONSTANT_Double:
526 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
528 case CONSTANT_String:
529 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
532 cr = (constant_classref *) (m->class->cpinfos[i]);
534 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
537 /* if not resolved, c == NULL */
539 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
543 #if defined(ENABLE_VERIFIER)
545 exceptions_throw_verifyerror(m,
546 "Invalid constant type to push");
552 case JAVA_ACONST_NULL:
563 OP_LOADCONST_I(opcode - JAVA_ICONST_0);
568 OP_LOADCONST_L(opcode - JAVA_LCONST_0);
574 OP_LOADCONST_F(opcode - JAVA_FCONST_0);
579 OP_LOADCONST_D(opcode - JAVA_DCONST_0);
582 /* stack operations ***************************************************/
584 /* We need space for additional ICMDs so we can translate these */
585 /* instructions to sequences of ICMD_COPY and ICMD_MOVE instructions. */
588 INSTRUCTIONS_CHECK(4);
596 INSTRUCTIONS_CHECK(6);
606 INSTRUCTIONS_CHECK(2);
612 INSTRUCTIONS_CHECK(7);
623 INSTRUCTIONS_CHECK(9);
636 INSTRUCTIONS_CHECK(3);
642 /* local variable access instructions *********************************/
647 if (iswide == false) {
648 i = SUCK_BE_U1(m->jcode + p + 1);
651 i = SUCK_BE_U2(m->jcode + p + 1);
655 OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
660 if (iswide == false) {
661 i = SUCK_BE_U1(m->jcode + p + 1);
664 i = SUCK_BE_U2(m->jcode + p + 1);
668 OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
675 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
682 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
689 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
696 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
703 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
709 if (iswide == false) {
710 i = SUCK_BE_U1(m->jcode + p + 1);
713 i = SUCK_BE_U2(m->jcode + p + 1);
717 OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
722 if (iswide == false) {
723 i = SUCK_BE_U1(m->jcode + p + 1);
726 i = SUCK_BE_U2(m->jcode + p + 1);
730 OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
737 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
744 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
751 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
758 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
765 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
772 if (iswide == false) {
773 i = SUCK_BE_U1(m->jcode + p + 1);
774 v = SUCK_BE_S1(m->jcode + p + 2);
778 i = SUCK_BE_U2(m->jcode + p + 1);
779 v = SUCK_BE_S2(m->jcode + p + 3);
784 LOCALTYPE_USED(i, TYPE_INT);
785 OP_LOCALINDEX_I(opcode, i, v);
789 /* wider index for loading, storing and incrementing ******************/
796 /* managing arrays ****************************************************/
799 switch (SUCK_BE_S1(m->jcode + p + 1)) {
801 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
804 bte = builtintable_get_internal(BUILTIN_newarray_char);
807 bte = builtintable_get_internal(BUILTIN_newarray_float);
810 bte = builtintable_get_internal(BUILTIN_newarray_double);
813 bte = builtintable_get_internal(BUILTIN_newarray_byte);
816 bte = builtintable_get_internal(BUILTIN_newarray_short);
819 bte = builtintable_get_internal(BUILTIN_newarray_int);
822 bte = builtintable_get_internal(BUILTIN_newarray_long);
824 #if defined(ENABLE_VERIFIER)
826 exceptions_throw_verifyerror(m, "Invalid array-type to create");
830 OP_BUILTIN_CHECK_EXCEPTION(bte);
834 i = SUCK_BE_U2(m->jcode + p + 1);
835 compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
839 if (!(cr = class_get_classref_multiarray_of(1, compr)))
842 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
845 INSTRUCTIONS_CHECK(2);
846 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
847 bte = builtintable_get_internal(BUILTIN_newarray);
848 OP_BUILTIN_CHECK_EXCEPTION(bte);
852 case JAVA_MULTIANEWARRAY:
853 jd->isleafmethod = false;
854 i = SUCK_BE_U2(m->jcode + p + 1);
855 j = SUCK_BE_U1(m->jcode + p + 3);
857 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
861 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
864 /* if unresolved, c == NULL */
866 iptr->s1.argcount = j;
867 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, INS_FLAG_CHECK);
870 /* control flow instructions ******************************************/
889 i = p + SUCK_BE_S2(m->jcode + p + 1);
890 CHECK_BYTECODE_INDEX(i);
893 OP_INSINDEX(opcode, i);
897 i = p + SUCK_BE_S4(m->jcode + p + 1);
898 CHECK_BYTECODE_INDEX(i);
901 OP_INSINDEX(ICMD_GOTO, i);
905 i = p + SUCK_BE_S2(m->jcode + p + 1);
907 CHECK_BYTECODE_INDEX(i);
910 OP_PREPARE_ZEROFLAGS(JAVA_JSR);
911 iptr->sx.s23.s3.jsrtarget.insindex = i;
916 i = p + SUCK_BE_S4(m->jcode + p + 1);
920 if (iswide == false) {
921 i = SUCK_BE_U1(m->jcode + p + 1);
924 i = SUCK_BE_U2(m->jcode + p + 1);
930 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
940 /* XXX ARETURN will need a flag in the typechecker */
946 /* XXX ATHROW will need a flag in the typechecker */
951 /* table jumps ********************************************************/
953 case JAVA_LOOKUPSWITCH:
956 lookup_target_t *lookup;
957 #if defined(ENABLE_VERIFIER)
961 nextp = MEMORY_ALIGN((p + 1), 4);
963 CHECK_END_OF_BYTECODE(nextp + 8);
965 OP_PREPARE_ZEROFLAGS(opcode);
969 j = p + SUCK_BE_S4(m->jcode + nextp);
970 iptr->sx.s23.s3.lookupdefault.insindex = j;
972 CHECK_BYTECODE_INDEX(j);
975 /* number of pairs */
977 num = SUCK_BE_U4(m->jcode + nextp);
978 iptr->sx.s23.s2.lookupcount = num;
981 /* allocate the intermediate code table */
983 lookup = DMNEW(lookup_target_t, num);
984 iptr->dst.lookup = lookup;
986 /* iterate over the lookup table */
988 CHECK_END_OF_BYTECODE(nextp + 8 * num);
990 for (i = 0; i < num; i++) {
993 j = SUCK_BE_S4(m->jcode + nextp);
998 #if defined(ENABLE_VERIFIER)
999 /* check if the lookup table is sorted correctly */
1001 if (i && (j <= prevvalue)) {
1002 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1009 j = p + SUCK_BE_S4(m->jcode + nextp);
1010 lookup->target.insindex = j;
1013 CHECK_BYTECODE_INDEX(j);
1022 case JAVA_TABLESWITCH:
1026 branch_target_t *table;
1029 nextp = MEMORY_ALIGN((p + 1), 4);
1031 CHECK_END_OF_BYTECODE(nextp + 12);
1033 OP_PREPARE_ZEROFLAGS(opcode);
1035 /* default target */
1037 deftarget = p + SUCK_BE_S4(m->jcode + nextp);
1039 CHECK_BYTECODE_INDEX(deftarget);
1040 MARK_BASICBLOCK(deftarget);
1044 j = SUCK_BE_S4(m->jcode + nextp);
1045 iptr->sx.s23.s2.tablelow = j;
1050 num = SUCK_BE_S4(m->jcode + nextp);
1051 iptr->sx.s23.s3.tablehigh = num;
1054 /* calculate the number of table entries */
1058 #if defined(ENABLE_VERIFIER)
1060 exceptions_throw_verifyerror(m,
1061 "invalid TABLESWITCH: upper bound < lower bound");
1065 /* create the intermediate code table */
1066 /* the first entry is the default target */
1068 table = DMNEW(branch_target_t, 1 + num);
1069 iptr->dst.table = table;
1070 (table++)->insindex = deftarget;
1072 /* iterate over the target table */
1074 CHECK_END_OF_BYTECODE(nextp + 4 * num);
1076 for (i = 0; i < num; i++) {
1077 j = p + SUCK_BE_S4(m->jcode + nextp);
1078 (table++)->insindex = j;
1080 CHECK_BYTECODE_INDEX(j);
1089 /* load and store of object fields ************************************/
1093 jd->isleafmethod = false;
1096 case JAVA_GETSTATIC:
1097 case JAVA_PUTSTATIC:
1101 constant_FMIref *fr;
1102 unresolved_field *uf;
1104 i = SUCK_BE_U2(m->jcode + p + 1);
1105 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
1109 OP_PREPARE_ZEROFLAGS(opcode);
1110 iptr->sx.s23.s3.fmiref = fr;
1112 /* only with -noverify, otherwise the typechecker does this */
1114 #if defined(ENABLE_VERIFIER)
1115 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1117 result = resolve_field_lazy(m, fr);
1118 if (result == resolveFailed)
1121 if (result != resolveSucceeded) {
1122 uf = resolve_create_unresolved_field(m->class, m, iptr);
1127 /* store the unresolved_field pointer */
1129 iptr->sx.s23.s3.uf = uf;
1130 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1132 #if defined(ENABLE_VERIFIER)
1140 /* method invocation **************************************************/
1142 case JAVA_INVOKESTATIC:
1143 OP_PREPARE_ZEROFLAGS(opcode);
1145 i = SUCK_BE_U2(m->jcode + p + 1);
1146 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1151 md = mr->parseddesc.md;
1153 if (md->params == NULL)
1154 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1159 case JAVA_INVOKESPECIAL:
1160 OP_PREPARE_FLAGS(opcode, INS_FLAG_CHECK);
1162 i = SUCK_BE_U2(m->jcode + p + 1);
1163 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1165 goto invoke_nonstatic_method;
1167 case JAVA_INVOKEINTERFACE:
1168 OP_PREPARE_ZEROFLAGS(opcode);
1170 i = SUCK_BE_U2(m->jcode + p + 1);
1171 mr = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1173 goto invoke_nonstatic_method;
1175 case JAVA_INVOKEVIRTUAL:
1176 OP_PREPARE_ZEROFLAGS(opcode);
1178 i = SUCK_BE_U2(m->jcode + p + 1);
1179 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1181 invoke_nonstatic_method:
1185 md = mr->parseddesc.md;
1187 if (md->params == NULL)
1188 if (!descriptor_params_from_paramtypes(md, 0))
1192 jd->isleafmethod = false;
1194 iptr->sx.s23.s3.fmiref = mr;
1196 /* only with -noverify, otherwise the typechecker does this */
1198 #if defined(ENABLE_VERIFIER)
1199 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1201 result = resolve_method_lazy(m, mr,
1202 (opcode == JAVA_INVOKESPECIAL));
1203 if (result == resolveFailed)
1206 if (result == resolveSucceeded) {
1207 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1209 /* if this call is monomorphic, turn it into an
1212 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1214 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1215 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1217 iptr->opc = ICMD_INVOKESPECIAL;
1218 iptr->flags.bits |= INS_FLAG_CHECK;
1222 um = resolve_create_unresolved_method(m->class, m, mr,
1223 (opcode == JAVA_INVOKESTATIC),
1224 (opcode == JAVA_INVOKESPECIAL));
1229 /* store the unresolved_method pointer */
1231 iptr->sx.s23.s3.um = um;
1232 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1234 #if defined(ENABLE_VERIFIER)
1240 /* instructions taking class arguments ********************************/
1243 i = SUCK_BE_U2(m->jcode + p + 1);
1244 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1248 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1251 INSTRUCTIONS_CHECK(2);
1252 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1253 bte = builtintable_get_internal(BUILTIN_new);
1254 OP_BUILTIN_CHECK_EXCEPTION(bte);
1258 case JAVA_CHECKCAST:
1259 i = SUCK_BE_U2(m->jcode + p + 1);
1260 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1264 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1267 if (cr->name->text[0] == '[') {
1268 /* array type cast-check */
1269 flags = INS_FLAG_CHECK | INS_FLAG_ARRAY;
1270 jd->isleafmethod = false;
1273 /* object type cast-check */
1274 flags = INS_FLAG_CHECK;
1276 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1279 case JAVA_INSTANCEOF:
1280 i = SUCK_BE_U2(m->jcode + p + 1);
1281 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1285 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1288 if (cr->name->text[0] == '[') {
1289 /* array type cast-check */
1290 INSTRUCTIONS_CHECK(2);
1291 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1292 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1293 OP_BUILTIN_NO_EXCEPTION(bte);
1297 /* object type cast-check */
1298 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1302 /* synchronization instructions ***************************************/
1304 case JAVA_MONITORENTER:
1305 #if defined(ENABLE_THREADS)
1307 bte = builtintable_get_internal(LOCK_monitor_enter);
1308 OP_BUILTIN_CHECK_EXCEPTION(bte);
1313 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1318 case JAVA_MONITOREXIT:
1319 #if defined(ENABLE_THREADS)
1321 bte = builtintable_get_internal(LOCK_monitor_exit);
1322 OP_BUILTIN_CHECK_EXCEPTION(bte);
1327 OP_CHECK_EXCEPTION(ICMD_CHECKNULL);
1332 /* arithmetic instructions that may become builtin functions **********/
1335 #if !SUPPORT_DIVISION
1336 bte = builtintable_get_internal(BUILTIN_idiv);
1337 OP_BUILTIN_ARITHMETIC(opcode, bte);
1339 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1342 OP_CHECK_EXCEPTION(opcode);
1348 #if !SUPPORT_DIVISION
1349 bte = builtintable_get_internal(BUILTIN_irem);
1350 OP_BUILTIN_ARITHMETIC(opcode, bte);
1352 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1355 OP_CHECK_EXCEPTION(opcode);
1361 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1362 bte = builtintable_get_internal(BUILTIN_ldiv);
1363 OP_BUILTIN_ARITHMETIC(opcode, bte);
1365 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1368 OP_CHECK_EXCEPTION(opcode);
1374 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1375 bte = builtintable_get_internal(BUILTIN_lrem);
1376 OP_BUILTIN_ARITHMETIC(opcode, bte);
1378 # if SUPPORT_HARDWARE_DIVIDE_BY_ZERO
1381 OP_CHECK_EXCEPTION(opcode);
1387 #if defined(__I386__)
1390 bte = builtintable_get_internal(BUILTIN_frem);
1391 OP_BUILTIN_NO_EXCEPTION(bte);
1396 #if defined(__I386__)
1399 bte = builtintable_get_internal(BUILTIN_drem);
1400 OP_BUILTIN_NO_EXCEPTION(bte);
1405 #if defined(__ALPHA__)
1407 bte = builtintable_get_internal(BUILTIN_f2i);
1408 OP_BUILTIN_NO_EXCEPTION(bte);
1418 #if defined(__ALPHA__)
1420 bte = builtintable_get_internal(BUILTIN_f2l);
1421 OP_BUILTIN_NO_EXCEPTION(bte);
1431 #if defined(__ALPHA__)
1433 bte = builtintable_get_internal(BUILTIN_d2i);
1434 OP_BUILTIN_NO_EXCEPTION(bte);
1444 #if defined(__ALPHA__)
1446 bte = builtintable_get_internal(BUILTIN_d2l);
1447 OP_BUILTIN_NO_EXCEPTION(bte);
1456 /* invalid opcodes ****************************************************/
1458 /* check for invalid opcodes if the verifier is enabled */
1459 #if defined(ENABLE_VERIFIER)
1460 case JAVA_BREAKPOINT:
1461 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1464 case 186: /* unused opcode */
1518 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1522 #endif /* defined(ENABLE_VERIFIER) */
1524 /* opcodes that don't require translation *****************************/
1527 /* straight-forward translation to ICMD */
1533 /* verifier checks ****************************************************/
1535 #if defined(ENABLE_VERIFIER)
1536 /* If WIDE was used correctly, iswide should have been reset by now. */
1538 exceptions_throw_verifyerror(m,
1539 "Illegal instruction: WIDE before incompatible opcode");
1542 #endif /* defined(ENABLE_VERIFIER) */
1546 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1547 /* add a NOP to the last basic block */
1549 INSTRUCTIONS_CHECK(1);
1553 /*** END OF LOOP **********************************************************/
1555 /* assert that we did not write more ICMDs than allocated */
1557 assert(icount <= pd.instructionslength);
1558 assert(icount == (iptr - pd.instructions));
1560 /*** verifier checks ******************************************************/
1562 #if defined(ENABLE_VERIFIER)
1563 if (p != m->jcodelength) {
1564 exceptions_throw_verifyerror(m,
1565 "Command-sequence crosses code-boundary");
1570 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1573 #endif /* defined(ENABLE_VERIFIER) */
1575 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1577 /* identify basic blocks */
1579 /* First instruction always starts a basic block, but check if it
1580 is already a branch target. */
1582 if ((jd->basicblockindex[0] == 0) || (jd->basicblockindex[0] > 1)) {
1583 iptr = pd.instructions;
1585 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1587 /* This is the first basic block. */
1592 jd->branchtoentry = true;
1594 /* In this case the loop below counts the first basic
1600 /* Iterate over all bytecode instructions and mark the
1601 corresponding IR instruction as basic block starting
1604 for (i = 0; i < m->jcodelength; i++) {
1605 if (jd->basicblockindex[i] & 0x1) {
1606 #if defined(ENABLE_VERIFIER)
1607 /* Check if this block starts at the beginning of an
1610 if (pd.instructionstart[i] == 0) {
1611 exceptions_throw_verifyerror(m,
1612 "Branch into middle of instruction");
1617 /* Mark the IR instruction as basic block starting
1620 iptr = pd.instructions + (jd->basicblockindex[i] >> 1);
1622 iptr->flags.bits |= INS_FLAG_BASICBLOCK;
1624 /* Store te basic block number in the array. We need this
1625 information during stack analysis. */
1627 jd->basicblockindex[i] = b_count;
1629 /* basic block indices of course start with 0 */
1635 /* Iterate over all IR instructions and count the basic blocks. */
1637 iptr = pd.instructions;
1641 for (i = 0; i < icount; i++, iptr++) {
1642 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1647 /* Allocate basic block array (one more for end ipc). */
1649 jd->basicblocks = DMNEW(basicblock, b_count + 1);
1651 /* zero out all basic block structures */
1653 MZERO(jd->basicblocks, basicblock, b_count + 1);
1655 /* Now iterate again over all IR instructions and initialize the
1656 basic block structures. */
1658 iptr = pd.instructions;
1659 bptr = jd->basicblocks;
1663 for (i = 0; i < icount; i++, iptr++) {
1664 if (INSTRUCTION_STARTS_BASICBLOCK(iptr)) {
1665 /* intialize the basic block */
1667 BASICBLOCK_INIT(bptr, m);
1669 bptr->iinstr = iptr;
1672 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1675 /* bptr->icount is set when the next block is allocated */
1677 bptr->nr = b_count++;
1679 bptr[-1].next = bptr;
1683 /* set instruction count of last real block */
1686 bptr[-1].icount = (pd.instructions + icount) - bptr[-1].iinstr;
1689 /* allocate additional block at end */
1691 BASICBLOCK_INIT(bptr, m);
1694 jd->basicblockindex[m->jcodelength] = b_count;
1696 /* set basicblock pointers in exception table */
1698 if (!parse_resolve_exception_table(jd))
1701 /* store the local map */
1703 jd->local_map = local_map;
1705 /* calculate local variable renaming */
1715 /* iterate over local_map[0..m->maxlocals*5] and set all existing */
1716 /* index,type pairs (local_map[index*5+type]==1) to an unique value */
1717 /* -> == new local var index */
1718 for(i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1720 *mapptr = nlocals++;
1725 jd->localcount = nlocals;
1727 /* calculate the (maximum) number of variables needed */
1730 nlocals /* local variables */
1731 + b_count * m->maxstack /* invars */
1732 + s_count; /* variables created within blocks (non-invar) */
1734 /* reserve the first indices for local variables */
1736 jd->vartop = nlocals;
1738 /* reserve extra variables needed by stack analyse */
1740 jd->varcount += STACK_EXTRA_VARS;
1741 jd->vartop += STACK_EXTRA_VARS;
1743 /* The verifier needs space for saving invars in some cases and */
1744 /* extra variables. */
1746 #if defined(ENABLE_VERIFIER)
1747 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1748 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1750 /* allocate and initialize the variable array */
1752 jd->var = DMNEW(varinfo, jd->varcount);
1753 MZERO(jd->var, varinfo, jd->varcount);
1755 /* set types of all locals in jd->var */
1757 for(mapptr = local_map, i = 0; i < (m->maxlocals * 5); i++, mapptr++)
1758 if (*mapptr != UNUSED)
1759 VAR(*mapptr)->type = i%5;
1762 /* assign local variables to method variables */
1764 jd->instructions = pd.instructions;
1765 jd->instructioncount = icount;
1766 jd->basicblockcount = b_count;
1767 jd->stackcount = s_count + b_count * m->maxstack; /* in-stacks */
1769 /* allocate stack table */
1771 jd->stack = DMNEW(stackelement, jd->stackcount);
1773 /* everything's ok */
1777 /*** goto labels for throwing verifier exceptions *************************/
1779 #if defined(ENABLE_VERIFIER)
1781 throw_unexpected_end_of_bytecode:
1782 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1785 throw_invalid_bytecode_index:
1786 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1789 throw_illegal_local_variable_number:
1790 exceptions_throw_verifyerror(m, "Illegal local variable number");
1793 #endif /* ENABLE_VERIFIER */
1798 * These are local overrides for various environment variables in Emacs.
1799 * Please do not remove this and leave it at the end of the file, where
1800 * Emacs will automagically detect them.
1801 * ---------------------------------------------------------------------
1804 * indent-tabs-mode: t
1808 * vim:noexpandtab:sw=4:ts=4: