1 /* src/vm/jit/parse.c - parser for JavaVM to intermediate code translation
3 Copyright (C) 1996-2005, 2006 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 Contact: cacao@cacaojvm.org
29 Changes: Carolyn Oates
34 $Id: parse.c 5859 2006-10-29 23:33:30Z edwin $
46 #include "mm/memory.h"
47 #include "native/native.h"
49 #if defined(ENABLE_THREADS)
50 # include "threads/native/lock.h"
53 #include "toolbox/logging.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/linker.h"
58 #include "vm/loader.h"
59 #include "vm/resolve.h"
60 #include "vm/options.h"
61 #include "vm/statistics.h"
62 #include "vm/stringlocal.h"
64 #include "vm/jit/asmpart.h"
65 #include "vm/jit/jit.h"
66 #include "vm/jit/parse.h"
67 #include "vm/jit/patcher.h"
68 #include "vm/jit/loop/loop.h"
70 #define INSTRUCTIONS_INCREMENT 5 /* number of additional instructions to */
71 /* allocate if space runs out */
74 /* parserdata_t ***************************************************************/
76 typedef struct parsedata_t parsedata_t;
79 instruction *instructions; /* instruction array */
80 s4 instructionslength; /* length of the instruction array */
85 /* parse_setup *****************************************************************
87 Fills the passed parsedata_t structure.
89 *******************************************************************************/
91 static void parse_setup(jitdata *jd, parsedata_t *pd)
95 /* get required compiler data */
99 /* Allocate instruction array and block index table (1 additional
102 jd->basicblockindex = DMNEW(s4, m->jcodelength + 1);
103 pd->instructionstart = DMNEW(u1, m->jcodelength + 1);
105 MZERO(jd->basicblockindex, s4, m->jcodelength + 1);
106 MZERO(pd->instructionstart, u1, m->jcodelength + 1);
108 /* Set the length of the instruction array. We simply add 5 more
109 instruction, as this seems to be a reasonable value. */
111 pd->instructionslength = m->jcodelength + 1;
113 /* allocate the instruction array */
115 pd->instructions = DMNEW(instruction, pd->instructionslength);
117 /* Zero the intermediate instructions array so we don't have any
118 invalid pointers in it if we cannot finish stack_analyse(). */
120 MZERO(pd->instructions, instruction, pd->instructionslength);
124 /* parse_realloc_instructions **************************************************
126 Reallocate the instructions array so there is room for at least N
127 additional instructions.
130 the new value for iptr
132 *******************************************************************************/
134 static instruction *parse_realloc_instructions(parsedata_t *pd, s4 ipc, s4 n)
136 /* increase the size of the instruction array */
138 pd->instructionslength += (n + INSTRUCTIONS_INCREMENT);
140 /* reallocate the array */
142 pd->instructions = DMREALLOC(pd->instructions, instruction, ipc,
143 pd->instructionslength);
145 /* return the iptr */
147 return pd->instructions + ipc;
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 /* the start of the handler becomes a basic block start */
215 CHECK_BYTECODE_INDEX(pc);
223 #if defined(ENABLE_VERIFIER)
224 throw_invalid_bytecode_index:
225 exceptions_throw_verifyerror(m,
226 "Illegal bytecode index in exception table");
232 /* parse_resolve_exception_table ***********************************************
234 Enter the exception handlers and their ranges, resolved to basicblock *s,
238 jd...............current jitdata
241 true.............everything ok
242 false............an exception has been thrown
244 *******************************************************************************/
246 static bool parse_resolve_exception_table(jitdata *jd)
249 raw_exception_entry *rex;
257 len = m->rawexceptiontablelength;
259 /* common case: no handler entries */
264 /* allocate the exception table */
266 jd->exceptiontablelength = len;
267 jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
269 /* copy and resolve the entries */
271 ex = jd->exceptiontable;
272 rex = m->rawexceptiontable;
274 for (i = 0; i < len; ++i, ++rex, ++ex) {
275 /* resolve instruction indices to basic blocks */
277 ex->start = BLOCK_OF(rex->startpc);
278 ex->end = BLOCK_OF(rex->endpc);
279 ex->handler = BLOCK_OF(rex->handlerpc);
281 /* lazily resolve the catchtype */
283 if (rex->catchtype.any != NULL) {
284 if (!resolve_classref_or_classinfo(m,
286 resolveLazy, true, false,
290 /* if resolved, enter the result of resolution in the table */
293 rex->catchtype.cls = exclass;
296 ex->catchtype = rex->catchtype;
297 ex->next = NULL; /* set by loop analysis */
298 ex->down = ex + 1; /* link to next exception entry */
301 /* terminate the ->down linked list */
303 assert(ex != jd->exceptiontable);
310 /*******************************************************************************
312 function 'parse' scans the JavaVM code and generates intermediate code
314 During parsing the block index table is used to store at bit pos 0
315 a flag which marks basic block starts and at position 1 to 31 the
316 intermediate instruction index. After parsing the block index table
317 is scanned, for marked positions a block is generated and the block
318 number is stored in the block index table.
320 *******************************************************************************/
322 /*** macro for checking the length of the bytecode ***/
324 #if defined(ENABLE_VERIFIER)
325 #define CHECK_END_OF_BYTECODE(neededlength) \
327 if ((neededlength) > m->jcodelength) \
328 goto throw_unexpected_end_of_bytecode; \
330 #else /* !ENABLE_VERIFIER */
331 #define CHECK_END_OF_BYTECODE(neededlength)
332 #endif /* ENABLE_VERIFIER */
334 bool parse(jitdata *jd)
336 methodinfo *m; /* method being parsed */
339 instruction *iptr; /* current ptr into instruction array */
340 s4 ipc; /* intermediate instruction counter */
341 s4 p; /* java instruction counter */
342 s4 nextp; /* start of next java instruction */
343 s4 opcode; /* java opcode */
346 int b_count; /* basic block counter */
347 int s_count = 0; /* stack element counter */
348 bool blockend = false; /* true if basic block end has been reached */
349 bool iswide = false; /* true if last instruction was a wide */
350 constant_classref *cr;
351 constant_classref *compr;
353 builtintable_entry *bte;
356 unresolved_method *um;
357 resolve_result_t result;
364 int *local_map; /* local pointer to renaming structore */
365 /* is assigned to rd->local_map at the end */
366 /* 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 #if defined(ENABLE_THREADS)
401 if (checksync && (m->flags & ACC_SYNCHRONIZED))
402 jd->isleafmethod = false;
405 /* setup line number info */
410 if (m->linenumbercount == 0) {
414 linepcchange = m->linenumbers[0].start_pc;
417 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
419 for (p = 0; p < m->jcodelength; p = nextp) {
421 /* mark this position as a valid instruction start */
423 pd.instructionstart[p] = 1;
425 /* change the current line number, if necessary */
427 /* XXX rewrite this using pointer arithmetic */
429 if (linepcchange == p) {
430 if (m->linenumbercount > lineindex) {
432 currentline = m->linenumbers[lineindex].line_number;
434 if (lineindex < m->linenumbercount) {
435 linepcchange = m->linenumbers[lineindex].start_pc;
436 if (linepcchange == p)
437 goto next_linenumber;
443 /* fetch next opcode */
445 opcode = SUCK_BE_U1(m->jcode + p);
447 /* some compilers put a JAVA_NOP after a blockend instruction */
449 if (blockend && (opcode != JAVA_NOP)) {
450 /* start new block */
456 /* We need a NOP as last instruction in each basic block
457 for basic block reordering (may be replaced with a GOTO
460 if (jd->basicblockindex[p] & 1) {
461 INSTRUCTIONS_CHECK(1);
465 /* store intermediate instruction count (bit 0 mark block starts) */
467 jd->basicblockindex[p] |= (ipc << 1);
469 /* compute next instruction start */
471 nextp = p + jcommandsize[opcode];
473 CHECK_END_OF_BYTECODE(nextp);
475 /* add stack elements produced by this instruction */
477 s_count += stackreq[opcode];
479 /* We check here for the space of 1 instruction in the
480 instruction array. If an opcode is converted to more than
481 1 instruction, this is checked in the corresponding
484 INSTRUCTIONS_CHECK(1);
486 /* translate this bytecode instruction */
492 /* pushing constants onto the stack ***********************************/
495 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + p + 1));
499 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + p + 1));
503 i = SUCK_BE_U1(m->jcode + p + 1);
504 goto pushconstantitem;
508 i = SUCK_BE_U2(m->jcode + p + 1);
512 #if defined(ENABLE_VERIFIER)
513 if (i >= m->class->cpcount) {
514 exceptions_throw_verifyerror(m,
515 "Attempt to access constant outside range");
520 switch (m->class->cptags[i]) {
521 case CONSTANT_Integer:
522 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
525 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
528 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
530 case CONSTANT_Double:
531 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
533 case CONSTANT_String:
534 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
537 cr = (constant_classref *) (m->class->cpinfos[i]);
539 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
542 /* if not resolved, c == NULL */
544 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
548 #if defined(ENABLE_VERIFIER)
550 exceptions_throw_verifyerror(m,
551 "Invalid constant type to push");
557 case JAVA_ACONST_NULL:
568 OP_LOADCONST_I(opcode - JAVA_ICONST_0);
573 OP_LOADCONST_L(opcode - JAVA_LCONST_0);
579 OP_LOADCONST_F(opcode - JAVA_FCONST_0);
584 OP_LOADCONST_D(opcode - JAVA_DCONST_0);
587 /* stack operations ***************************************************/
589 /* We need space for additional ICMDs so we can translate these */
590 /* instructions to sequences of ICMD_COPY and ICMD_MOVE instructions. */
593 INSTRUCTIONS_CHECK(4);
601 INSTRUCTIONS_CHECK(6);
611 INSTRUCTIONS_CHECK(2);
617 INSTRUCTIONS_CHECK(7);
628 INSTRUCTIONS_CHECK(9);
641 INSTRUCTIONS_CHECK(3);
647 /* local variable access instructions *********************************/
652 if (iswide == false) {
653 i = SUCK_BE_U1(m->jcode + p + 1);
656 i = SUCK_BE_U2(m->jcode + p + 1);
660 OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
665 if (iswide == false) {
666 i = SUCK_BE_U1(m->jcode + p + 1);
669 i = SUCK_BE_U2(m->jcode + p + 1);
673 OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
680 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
687 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
694 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
701 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
708 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
714 if (iswide == false) {
715 i = SUCK_BE_U1(m->jcode + p + 1);
718 i = SUCK_BE_U2(m->jcode + p + 1);
722 OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
727 if (iswide == false) {
728 i = SUCK_BE_U1(m->jcode + p + 1);
731 i = SUCK_BE_U2(m->jcode + p + 1);
735 OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
742 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
749 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
756 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
763 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
770 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
777 if (iswide == false) {
778 i = SUCK_BE_U1(m->jcode + p + 1);
779 v = SUCK_BE_S1(m->jcode + p + 2);
783 i = SUCK_BE_U2(m->jcode + p + 1);
784 v = SUCK_BE_S2(m->jcode + p + 3);
789 LOCALTYPE_USED(i, TYPE_INT);
790 OP_LOCALINDEX_I(opcode, i, v);
794 /* wider index for loading, storing and incrementing ******************/
801 /* managing arrays ****************************************************/
804 switch (SUCK_BE_S1(m->jcode + p + 1)) {
806 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
809 bte = builtintable_get_internal(BUILTIN_newarray_char);
812 bte = builtintable_get_internal(BUILTIN_newarray_float);
815 bte = builtintable_get_internal(BUILTIN_newarray_double);
818 bte = builtintable_get_internal(BUILTIN_newarray_byte);
821 bte = builtintable_get_internal(BUILTIN_newarray_short);
824 bte = builtintable_get_internal(BUILTIN_newarray_int);
827 bte = builtintable_get_internal(BUILTIN_newarray_long);
829 #if defined(ENABLE_VERIFIER)
831 exceptions_throw_verifyerror(m, "Invalid array-type to create");
835 OP_BUILTIN_CHECK_EXCEPTION(bte);
839 i = SUCK_BE_U2(m->jcode + p + 1);
840 compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
844 if (!(cr = class_get_classref_multiarray_of(1, compr)))
847 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
850 INSTRUCTIONS_CHECK(2);
851 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
852 bte = builtintable_get_internal(BUILTIN_newarray);
853 OP_BUILTIN_CHECK_EXCEPTION(bte);
857 case JAVA_MULTIANEWARRAY:
858 jd->isleafmethod = false;
859 i = SUCK_BE_U2(m->jcode + p + 1);
860 j = SUCK_BE_U1(m->jcode + p + 3);
862 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
866 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
869 /* if unresolved, c == NULL */
871 iptr->s1.argcount = j;
872 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
875 /* control flow instructions ******************************************/
894 i = p + SUCK_BE_S2(m->jcode + p + 1);
895 CHECK_BYTECODE_INDEX(i);
898 OP_INSINDEX(opcode, i);
902 i = p + SUCK_BE_S4(m->jcode + p + 1);
903 CHECK_BYTECODE_INDEX(i);
906 OP_INSINDEX(opcode, i);
910 i = p + SUCK_BE_S2(m->jcode + p + 1);
912 CHECK_BYTECODE_INDEX(i);
915 OP_PREPARE_ZEROFLAGS(JAVA_JSR);
916 iptr->sx.s23.s3.jsrtarget.insindex = i;
921 i = p + SUCK_BE_S4(m->jcode + p + 1);
925 if (iswide == false) {
926 i = SUCK_BE_U1(m->jcode + p + 1);
929 i = SUCK_BE_U2(m->jcode + p + 1);
935 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
945 /* XXX ARETURN will need a flag in the typechecker */
951 /* XXX ATHROW will need a flag in the typechecker */
956 /* table jumps ********************************************************/
958 case JAVA_LOOKUPSWITCH:
961 lookup_target_t *lookup;
962 #if defined(ENABLE_VERIFIER)
966 nextp = ALIGN((p + 1), 4);
968 CHECK_END_OF_BYTECODE(nextp + 8);
970 OP_PREPARE_ZEROFLAGS(opcode);
974 j = p + SUCK_BE_S4(m->jcode + nextp);
975 iptr->sx.s23.s3.lookupdefault.insindex = j;
977 CHECK_BYTECODE_INDEX(j);
980 /* number of pairs */
982 num = SUCK_BE_U4(m->jcode + nextp);
983 iptr->sx.s23.s2.lookupcount = num;
986 /* allocate the intermediate code table */
988 lookup = DMNEW(lookup_target_t, num);
989 iptr->dst.lookup = lookup;
991 /* iterate over the lookup table */
993 CHECK_END_OF_BYTECODE(nextp + 8 * num);
995 for (i = 0; i < num; i++) {
998 j = SUCK_BE_S4(m->jcode + nextp);
1003 #if defined(ENABLE_VERIFIER)
1004 /* check if the lookup table is sorted correctly */
1006 if (i && (j <= prevvalue)) {
1007 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1014 j = p + SUCK_BE_S4(m->jcode + nextp);
1015 lookup->target.insindex = j;
1018 CHECK_BYTECODE_INDEX(j);
1027 case JAVA_TABLESWITCH:
1031 branch_target_t *table;
1034 nextp = ALIGN((p + 1), 4);
1036 CHECK_END_OF_BYTECODE(nextp + 12);
1038 OP_PREPARE_ZEROFLAGS(opcode);
1040 /* default target */
1042 deftarget = p + SUCK_BE_S4(m->jcode + nextp);
1044 CHECK_BYTECODE_INDEX(deftarget);
1045 MARK_BASICBLOCK(deftarget);
1049 j = SUCK_BE_S4(m->jcode + nextp);
1050 iptr->sx.s23.s2.tablelow = j;
1055 num = SUCK_BE_S4(m->jcode + nextp);
1056 iptr->sx.s23.s3.tablehigh = num;
1059 /* calculate the number of table entries */
1063 #if defined(ENABLE_VERIFIER)
1065 exceptions_throw_verifyerror(m,
1066 "invalid TABLESWITCH: upper bound < lower bound");
1070 /* create the intermediate code table */
1071 /* the first entry is the default target */
1073 table = DMNEW(branch_target_t, 1 + num);
1074 iptr->dst.table = table;
1075 (table++)->insindex = deftarget;
1077 /* iterate over the target table */
1079 CHECK_END_OF_BYTECODE(nextp + 4 * num);
1081 for (i = 0; i < num; i++) {
1082 j = p + SUCK_BE_S4(m->jcode + nextp);
1083 (table++)->insindex = j;
1085 CHECK_BYTECODE_INDEX(j);
1094 /* load and store of object fields ************************************/
1098 jd->isleafmethod = false;
1101 case JAVA_GETSTATIC:
1102 case JAVA_PUTSTATIC:
1106 constant_FMIref *fr;
1107 unresolved_field *uf;
1109 i = SUCK_BE_U2(m->jcode + p + 1);
1110 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
1114 OP_PREPARE_ZEROFLAGS(opcode);
1115 iptr->sx.s23.s3.fmiref = fr;
1117 /* only with -noverify, otherwise the typechecker does this */
1119 #if defined(ENABLE_VERIFIER)
1120 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1122 result = resolve_field_lazy(m, fr);
1123 if (result == resolveFailed)
1126 if (result != resolveSucceeded) {
1127 uf = resolve_create_unresolved_field(m->class, m, iptr);
1132 /* store the unresolved_field pointer */
1134 iptr->sx.s23.s3.uf = uf;
1135 iptr->flags.bits = INS_FLAG_UNRESOLVED;
1137 #if defined(ENABLE_VERIFIER)
1145 /* method invocation **************************************************/
1147 case JAVA_INVOKESTATIC:
1148 i = SUCK_BE_U2(m->jcode + p + 1);
1149 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1154 md = mr->parseddesc.md;
1156 if (md->params == NULL)
1157 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1162 case JAVA_INVOKEINTERFACE:
1163 i = SUCK_BE_U2(m->jcode + p + 1);
1165 mr = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1167 goto invoke_nonstatic_method;
1169 case JAVA_INVOKESPECIAL:
1170 case JAVA_INVOKEVIRTUAL:
1171 i = SUCK_BE_U2(m->jcode + p + 1);
1172 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1174 invoke_nonstatic_method:
1178 md = mr->parseddesc.md;
1180 if (md->params == NULL)
1181 if (!descriptor_params_from_paramtypes(md, 0))
1185 jd->isleafmethod = false;
1187 OP_PREPARE_ZEROFLAGS(opcode);
1188 iptr->sx.s23.s3.fmiref = mr;
1190 /* only with -noverify, otherwise the typechecker does this */
1192 #if defined(ENABLE_VERIFIER)
1193 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1195 result = resolve_method_lazy(m, mr,
1196 (opcode == JAVA_INVOKESPECIAL));
1197 if (result == resolveFailed)
1200 if (result == resolveSucceeded) {
1201 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1203 /* if this call is monomorphic, turn it into an INVOKESPECIAL */
1205 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1207 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1208 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1210 iptr->opc = ICMD_INVOKESPECIAL;
1214 um = resolve_create_unresolved_method(m->class, m, mr,
1215 (opcode == JAVA_INVOKESTATIC),
1216 (opcode == JAVA_INVOKESPECIAL));
1221 /* store the unresolved_method pointer */
1223 iptr->sx.s23.s3.um = um;
1224 iptr->flags.bits = INS_FLAG_UNRESOLVED;
1226 #if defined(ENABLE_VERIFIER)
1232 /* instructions taking class arguments ********************************/
1235 i = SUCK_BE_U2(m->jcode + p + 1);
1236 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1240 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1243 INSTRUCTIONS_CHECK(2);
1244 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1245 bte = builtintable_get_internal(BUILTIN_new);
1246 OP_BUILTIN_CHECK_EXCEPTION(bte);
1250 case JAVA_CHECKCAST:
1251 i = SUCK_BE_U2(m->jcode + p + 1);
1252 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1256 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1259 if (cr->name->text[0] == '[') {
1260 /* array type cast-check */
1261 flags = INS_FLAG_ARRAY;
1262 jd->isleafmethod = false;
1265 /* object type cast-check */
1268 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1271 case JAVA_INSTANCEOF:
1272 i = SUCK_BE_U2(m->jcode + p + 1);
1273 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1277 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1280 if (cr->name->text[0] == '[') {
1281 /* array type cast-check */
1282 INSTRUCTIONS_CHECK(2);
1283 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1284 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1285 OP_BUILTIN_NO_EXCEPTION(bte);
1289 /* object type cast-check */
1290 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1294 /* synchronization instructions ***************************************/
1296 case JAVA_MONITORENTER:
1297 #if defined(ENABLE_THREADS)
1299 bte = builtintable_get_internal(LOCK_monitor_enter);
1300 OP_BUILTIN_CHECK_EXCEPTION(bte);
1305 OP(ICMD_CHECKNULL_POP);
1309 case JAVA_MONITOREXIT:
1310 #if defined(ENABLE_THREADS)
1312 bte = builtintable_get_internal(LOCK_monitor_exit);
1313 OP_BUILTIN_CHECK_EXCEPTION(bte);
1318 OP(ICMD_CHECKNULL_POP);
1322 /* arithmetic instructions that may become builtin functions **********/
1325 #if !SUPPORT_DIVISION
1326 bte = builtintable_get_internal(BUILTIN_idiv);
1327 OP_BUILTIN_ARITHMETIC(opcode, bte);
1334 #if !SUPPORT_DIVISION
1335 bte = builtintable_get_internal(BUILTIN_irem);
1336 OP_BUILTIN_ARITHMETIC(opcode, bte);
1343 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1344 bte = builtintable_get_internal(BUILTIN_ldiv);
1345 OP_BUILTIN_ARITHMETIC(opcode, bte);
1352 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1353 bte = builtintable_get_internal(BUILTIN_lrem);
1354 OP_BUILTIN_ARITHMETIC(opcode, bte);
1361 #if defined(__I386__)
1364 bte = builtintable_get_internal(BUILTIN_frem);
1365 OP_BUILTIN_NO_EXCEPTION(bte);
1370 #if defined(__I386__)
1373 bte = builtintable_get_internal(BUILTIN_drem);
1374 OP_BUILTIN_NO_EXCEPTION(bte);
1379 #if defined(__ALPHA__)
1381 bte = builtintable_get_internal(BUILTIN_f2i);
1382 OP_BUILTIN_NO_EXCEPTION(bte);
1392 #if defined(__ALPHA__)
1394 bte = builtintable_get_internal(BUILTIN_f2l);
1395 OP_BUILTIN_NO_EXCEPTION(bte);
1405 #if defined(__ALPHA__)
1407 bte = builtintable_get_internal(BUILTIN_d2i);
1408 OP_BUILTIN_NO_EXCEPTION(bte);
1418 #if defined(__ALPHA__)
1420 bte = builtintable_get_internal(BUILTIN_d2l);
1421 OP_BUILTIN_NO_EXCEPTION(bte);
1430 /* invalid opcodes ****************************************************/
1432 /* check for invalid opcodes if the verifier is enabled */
1433 #if defined(ENABLE_VERIFIER)
1434 case JAVA_BREAKPOINT:
1435 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1438 case 186: /* unused opcode */
1492 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1496 #endif /* defined(ENABLE_VERIFIER) */
1498 /* opcodes that don't require translation *****************************/
1501 /* straight-forward translation to ICMD */
1507 /* verifier checks ****************************************************/
1509 #if defined(ENABLE_VERIFIER)
1510 /* If WIDE was used correctly, iswide should have been reset by now. */
1512 exceptions_throw_verifyerror(m,
1513 "Illegal instruction: WIDE before incompatible opcode");
1516 #endif /* defined(ENABLE_VERIFIER) */
1520 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1521 /* add a NOP to the last basic block */
1523 INSTRUCTIONS_CHECK(1);
1527 /*** END OF LOOP **********************************************************/
1529 /* assert that we did not write more ICMDs than allocated */
1531 assert(ipc <= pd.instructionslength);
1532 assert(ipc == (iptr - pd.instructions));
1534 /*** verifier checks ******************************************************/
1536 #if defined(ENABLE_VERIFIER)
1537 if (p != m->jcodelength) {
1538 exceptions_throw_verifyerror(m,
1539 "Command-sequence crosses code-boundary");
1544 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1547 #endif /* defined(ENABLE_VERIFIER) */
1549 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1551 /* adjust block count if target 0 is not first intermediate instruction */
1553 if (!jd->basicblockindex[0] || (jd->basicblockindex[0] > 1))
1556 /* copy local to method variables */
1558 jd->instructions = pd.instructions;
1559 jd->instructioncount = ipc;
1560 jd->basicblockcount = b_count;
1561 jd->stackcount = s_count + jd->basicblockcount * m->maxstack; /* in-stacks */
1563 /* allocate stack table */
1565 jd->stack = DMNEW(stackelement, jd->stackcount);
1567 /* build basic block list */
1569 bptr = jd->basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1571 /* zero out all basic block structures */
1573 MZERO(bptr, basicblock, b_count + 1);
1577 /* additional block if target 0 is not first intermediate instruction */
1579 if (!jd->basicblockindex[0] || (jd->basicblockindex[0] > 1)) {
1580 BASICBLOCK_INIT(bptr, m);
1582 bptr->iinstr = jd->instructions;
1583 /* bptr->icount is set when the next block is allocated */
1585 bptr->nr = b_count++;
1587 bptr[-1].next = bptr;
1590 /* allocate blocks */
1592 for (p = 0; p < m->jcodelength; p++) {
1593 if (jd->basicblockindex[p] & 1) {
1594 #if defined(ENABLE_VERIFIER)
1595 /* Check if this block starts at the beginning of an
1598 if (!pd.instructionstart[p]) {
1599 exceptions_throw_verifyerror(m,
1600 "Branch into middle of instruction");
1605 /* allocate the block */
1607 BASICBLOCK_INIT(bptr, m);
1609 bptr->iinstr = jd->instructions + (jd->basicblockindex[p] >> 1);
1611 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1613 /* bptr->icount is set when the next block is allocated */
1615 jd->basicblockindex[p] = b_count;
1617 bptr->nr = b_count++;
1619 bptr[-1].next = bptr;
1623 /* set instruction count of last real block */
1626 bptr[-1].icount = (jd->instructions + jd->instructioncount) - bptr[-1].iinstr;
1629 /* allocate additional block at end */
1631 BASICBLOCK_INIT(bptr,m);
1633 jd->basicblockindex[m->jcodelength] = b_count;
1635 /* set basicblock pointers in exception table */
1637 if (!parse_resolve_exception_table(jd))
1640 /* store the local map */
1642 jd->local_map = local_map;
1644 /* calculate local variable renaming */
1654 /* iterate over local_map[0..m->maxlocals*5] and set all existing */
1655 /* index,type pairs (local_map[index*5+type]==1) to an unique value */
1656 /* -> == new local var index */
1657 for(i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1659 *mapptr = nlocals++;
1664 jd->localcount = nlocals;
1666 /* calculate the (maximum) number of variables needed */
1669 nlocals /* local variables */
1670 + jd->basicblockcount * m->maxstack /* invars */
1671 + s_count; /* variables created within blocks (non-invar) */
1673 /* reserve the first indices for local variables */
1675 jd->vartop = nlocals;
1677 /* reserve extra variables needed by stack analyse */
1679 jd->varcount += STACK_EXTRA_VARS;
1680 jd->vartop += STACK_EXTRA_VARS;
1682 /* The verifier needs space for saving invars in some cases and */
1683 /* extra variables. */
1685 #if defined(ENABLE_VERIFIER)
1686 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1687 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1689 /* allocate and initialize the variable array */
1691 jd->var = DMNEW(varinfo, jd->varcount);
1692 MZERO(jd->var, varinfo, jd->varcount);
1694 /* set types of all locals in jd->var */
1696 for(mapptr = local_map, i = 0; i < (m->maxlocals * 5); i++, mapptr++)
1697 if (*mapptr != UNUSED)
1698 VAR(*mapptr)->type = i%5;
1701 /* everything's ok */
1705 /*** goto labels for throwing verifier exceptions *************************/
1707 #if defined(ENABLE_VERIFIER)
1709 throw_unexpected_end_of_bytecode:
1710 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1713 throw_invalid_bytecode_index:
1714 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1717 throw_illegal_local_variable_number:
1718 exceptions_throw_verifyerror(m, "Illegal local variable number");
1721 #endif /* ENABLE_VERIFIER */
1726 * These are local overrides for various environment variables in Emacs.
1727 * Please do not remove this and leave it at the end of the file, where
1728 * Emacs will automagically detect them.
1729 * ---------------------------------------------------------------------
1732 * indent-tabs-mode: t
1736 * vim:noexpandtab:sw=4:ts=4: