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 5958 2006-11-12 13:21:07Z 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);
144 MZERO(pd->instructions + ipc, instruction, (pd->instructionslength - ipc));
146 /* return the iptr */
148 return pd->instructions + ipc;
152 /* parse_mark_exception_boundaries *********************************************
154 Mark exception handlers and the boundaries of the handled regions as
155 basic block boundaries.
158 jd...............current jitdata
161 >= 0.............the number of new basic blocks marked
162 -1...............an exception has been thrown
164 *******************************************************************************/
166 static int parse_mark_exception_boundaries(jitdata *jd)
172 raw_exception_entry *rex;
177 len = m->rawexceptiontablelength;
183 rex = m->rawexceptiontable;
185 for (i = 0; i < len; ++i, ++rex) {
187 /* the start of the handled region becomes a basic block start */
190 CHECK_BYTECODE_INDEX(pc);
193 pc = rex->endpc; /* see JVM Spec 4.7.3 */
194 CHECK_BYTECODE_INDEX_EXCLUSIVE(pc);
196 /* check that the range is valid */
198 #if defined(ENABLE_VERIFIER)
199 if (pc <= rex->startpc) {
200 exceptions_throw_verifyerror(m,
201 "Invalid exception handler range");
206 /* end of handled region becomes a basic block boundary */
207 /* (If it is the bytecode end, we'll use the special */
208 /* end block that is created anyway.) */
210 if (pc < m->jcodelength)
213 jd->branchtoend = true;
215 /* the start of the handler becomes a basic block start */
218 CHECK_BYTECODE_INDEX(pc);
226 #if defined(ENABLE_VERIFIER)
227 throw_invalid_bytecode_index:
228 exceptions_throw_verifyerror(m,
229 "Illegal bytecode index in exception table");
235 /* parse_resolve_exception_table ***********************************************
237 Enter the exception handlers and their ranges, resolved to basicblock *s,
241 jd...............current jitdata
244 true.............everything ok
245 false............an exception has been thrown
247 *******************************************************************************/
249 static bool parse_resolve_exception_table(jitdata *jd)
252 raw_exception_entry *rex;
260 len = m->rawexceptiontablelength;
262 /* common case: no handler entries */
267 /* allocate the exception table */
269 jd->exceptiontablelength = len;
270 jd->exceptiontable = DMNEW(exception_entry, len + 1); /* XXX why +1? */
272 /* copy and resolve the entries */
274 ex = jd->exceptiontable;
275 rex = m->rawexceptiontable;
277 for (i = 0; i < len; ++i, ++rex, ++ex) {
278 /* resolve instruction indices to basic blocks */
280 ex->start = BLOCK_OF(rex->startpc);
281 ex->end = BLOCK_OF(rex->endpc);
282 ex->handler = BLOCK_OF(rex->handlerpc);
284 /* lazily resolve the catchtype */
286 if (rex->catchtype.any != NULL) {
287 if (!resolve_classref_or_classinfo(m,
289 resolveLazy, true, false,
293 /* if resolved, enter the result of resolution in the table */
296 rex->catchtype.cls = exclass;
299 ex->catchtype = rex->catchtype;
300 ex->next = NULL; /* set by loop analysis */
301 ex->down = ex + 1; /* link to next exception entry */
304 /* terminate the ->down linked list */
306 assert(ex != jd->exceptiontable);
313 /*******************************************************************************
315 function 'parse' scans the JavaVM code and generates intermediate code
317 During parsing the block index table is used to store at bit pos 0
318 a flag which marks basic block starts and at position 1 to 31 the
319 intermediate instruction index. After parsing the block index table
320 is scanned, for marked positions a block is generated and the block
321 number is stored in the block index table.
323 *******************************************************************************/
325 /*** macro for checking the length of the bytecode ***/
327 #if defined(ENABLE_VERIFIER)
328 #define CHECK_END_OF_BYTECODE(neededlength) \
330 if ((neededlength) > m->jcodelength) \
331 goto throw_unexpected_end_of_bytecode; \
333 #else /* !ENABLE_VERIFIER */
334 #define CHECK_END_OF_BYTECODE(neededlength)
335 #endif /* ENABLE_VERIFIER */
337 bool parse(jitdata *jd)
339 methodinfo *m; /* method being parsed */
341 instruction *iptr; /* current ptr into instruction array */
342 s4 ipc; /* intermediate instruction counter */
343 s4 p; /* java instruction counter */
344 s4 nextp; /* start of next java instruction */
345 s4 opcode; /* java opcode */
348 int b_count; /* basic block counter */
349 int s_count = 0; /* stack element counter */
350 bool blockend = false; /* true if basic block end has been reached */
351 bool iswide = false; /* true if last instruction was a wide */
352 constant_classref *cr;
353 constant_classref *compr;
355 builtintable_entry *bte;
358 unresolved_method *um;
359 resolve_result_t result;
366 int *local_map; /* local pointer to renaming structore */
367 /* is assigned to rd->local_map at the end */
368 /* get required compiler data */
372 /* allocate buffers for local variable renaming */
373 local_map = DMNEW(int, m->maxlocals * 5);
374 for (i = 0; i < m->maxlocals; i++) {
375 local_map[i * 5 + 0] = 0;
376 local_map[i * 5 + 1] = 0;
377 local_map[i * 5 + 2] = 0;
378 local_map[i * 5 + 3] = 0;
379 local_map[i * 5 + 4] = 0;
382 /* initialize the parse data structures */
384 parse_setup(jd, &pd);
386 /* initialize local variables */
388 iptr = pd.instructions;
391 /* mark basic block boundaries for exception table */
393 b_count = parse_mark_exception_boundaries(jd);
397 /* initialize stack element counter */
399 s_count = 1 + m->rawexceptiontablelength;
401 /* setup line number info */
406 if (m->linenumbercount == 0) {
410 linepcchange = m->linenumbers[0].start_pc;
413 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
415 for (p = 0; p < m->jcodelength; p = nextp) {
417 /* mark this position as a valid instruction start */
419 pd.instructionstart[p] = 1;
421 /* change the current line number, if necessary */
423 /* XXX rewrite this using pointer arithmetic */
425 if (linepcchange == p) {
426 if (m->linenumbercount > lineindex) {
428 currentline = m->linenumbers[lineindex].line_number;
430 if (lineindex < m->linenumbercount) {
431 linepcchange = m->linenumbers[lineindex].start_pc;
432 if (linepcchange == p)
433 goto next_linenumber;
439 /* fetch next opcode */
441 opcode = SUCK_BE_U1(m->jcode + p);
443 /* some compilers put a JAVA_NOP after a blockend instruction */
445 if (blockend && (opcode != JAVA_NOP)) {
446 /* start new block */
452 /* We need a NOP as last instruction in each basic block
453 for basic block reordering (may be replaced with a GOTO
456 if (jd->basicblockindex[p] & 1) {
457 INSTRUCTIONS_CHECK(1);
461 /* store intermediate instruction count (bit 0 mark block starts) */
463 jd->basicblockindex[p] |= (ipc << 1);
465 /* compute next instruction start */
467 nextp = p + jcommandsize[opcode];
469 CHECK_END_OF_BYTECODE(nextp);
471 /* add stack elements produced by this instruction */
473 s_count += stackreq[opcode];
475 /* We check here for the space of 1 instruction in the
476 instruction array. If an opcode is converted to more than
477 1 instruction, this is checked in the corresponding
480 INSTRUCTIONS_CHECK(1);
482 /* translate this bytecode instruction */
488 /* pushing constants onto the stack ***********************************/
491 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + p + 1));
495 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + p + 1));
499 i = SUCK_BE_U1(m->jcode + p + 1);
500 goto pushconstantitem;
504 i = SUCK_BE_U2(m->jcode + p + 1);
508 #if defined(ENABLE_VERIFIER)
509 if (i >= m->class->cpcount) {
510 exceptions_throw_verifyerror(m,
511 "Attempt to access constant outside range");
516 switch (m->class->cptags[i]) {
517 case CONSTANT_Integer:
518 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
521 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
524 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
526 case CONSTANT_Double:
527 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
529 case CONSTANT_String:
530 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
533 cr = (constant_classref *) (m->class->cpinfos[i]);
535 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
538 /* if not resolved, c == NULL */
540 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
544 #if defined(ENABLE_VERIFIER)
546 exceptions_throw_verifyerror(m,
547 "Invalid constant type to push");
553 case JAVA_ACONST_NULL:
564 OP_LOADCONST_I(opcode - JAVA_ICONST_0);
569 OP_LOADCONST_L(opcode - JAVA_LCONST_0);
575 OP_LOADCONST_F(opcode - JAVA_FCONST_0);
580 OP_LOADCONST_D(opcode - JAVA_DCONST_0);
583 /* stack operations ***************************************************/
585 /* We need space for additional ICMDs so we can translate these */
586 /* instructions to sequences of ICMD_COPY and ICMD_MOVE instructions. */
589 INSTRUCTIONS_CHECK(4);
597 INSTRUCTIONS_CHECK(6);
607 INSTRUCTIONS_CHECK(2);
613 INSTRUCTIONS_CHECK(7);
624 INSTRUCTIONS_CHECK(9);
637 INSTRUCTIONS_CHECK(3);
643 /* local variable access instructions *********************************/
648 if (iswide == false) {
649 i = SUCK_BE_U1(m->jcode + p + 1);
652 i = SUCK_BE_U2(m->jcode + p + 1);
656 OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
661 if (iswide == false) {
662 i = SUCK_BE_U1(m->jcode + p + 1);
665 i = SUCK_BE_U2(m->jcode + p + 1);
669 OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
676 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
683 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
690 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
697 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
704 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
710 if (iswide == false) {
711 i = SUCK_BE_U1(m->jcode + p + 1);
714 i = SUCK_BE_U2(m->jcode + p + 1);
718 OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
723 if (iswide == false) {
724 i = SUCK_BE_U1(m->jcode + p + 1);
727 i = SUCK_BE_U2(m->jcode + p + 1);
731 OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
738 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
745 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
752 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
759 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
766 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
773 if (iswide == false) {
774 i = SUCK_BE_U1(m->jcode + p + 1);
775 v = SUCK_BE_S1(m->jcode + p + 2);
779 i = SUCK_BE_U2(m->jcode + p + 1);
780 v = SUCK_BE_S2(m->jcode + p + 3);
785 LOCALTYPE_USED(i, TYPE_INT);
786 OP_LOCALINDEX_I(opcode, i, v);
790 /* wider index for loading, storing and incrementing ******************/
797 /* managing arrays ****************************************************/
800 switch (SUCK_BE_S1(m->jcode + p + 1)) {
802 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
805 bte = builtintable_get_internal(BUILTIN_newarray_char);
808 bte = builtintable_get_internal(BUILTIN_newarray_float);
811 bte = builtintable_get_internal(BUILTIN_newarray_double);
814 bte = builtintable_get_internal(BUILTIN_newarray_byte);
817 bte = builtintable_get_internal(BUILTIN_newarray_short);
820 bte = builtintable_get_internal(BUILTIN_newarray_int);
823 bte = builtintable_get_internal(BUILTIN_newarray_long);
825 #if defined(ENABLE_VERIFIER)
827 exceptions_throw_verifyerror(m, "Invalid array-type to create");
831 OP_BUILTIN_CHECK_EXCEPTION(bte);
835 i = SUCK_BE_U2(m->jcode + p + 1);
836 compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
840 if (!(cr = class_get_classref_multiarray_of(1, compr)))
843 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
846 INSTRUCTIONS_CHECK(2);
847 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
848 bte = builtintable_get_internal(BUILTIN_newarray);
849 OP_BUILTIN_CHECK_EXCEPTION(bte);
853 case JAVA_MULTIANEWARRAY:
854 jd->isleafmethod = false;
855 i = SUCK_BE_U2(m->jcode + p + 1);
856 j = SUCK_BE_U1(m->jcode + p + 3);
858 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
862 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
865 /* if unresolved, c == NULL */
867 iptr->s1.argcount = j;
868 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
871 /* control flow instructions ******************************************/
890 i = p + SUCK_BE_S2(m->jcode + p + 1);
891 CHECK_BYTECODE_INDEX(i);
894 OP_INSINDEX(opcode, i);
898 i = p + SUCK_BE_S4(m->jcode + p + 1);
899 CHECK_BYTECODE_INDEX(i);
902 OP_INSINDEX(opcode, i);
906 i = p + SUCK_BE_S2(m->jcode + p + 1);
908 CHECK_BYTECODE_INDEX(i);
911 OP_PREPARE_ZEROFLAGS(JAVA_JSR);
912 iptr->sx.s23.s3.jsrtarget.insindex = i;
917 i = p + SUCK_BE_S4(m->jcode + p + 1);
921 if (iswide == false) {
922 i = SUCK_BE_U1(m->jcode + p + 1);
925 i = SUCK_BE_U2(m->jcode + p + 1);
931 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
941 /* XXX ARETURN will need a flag in the typechecker */
947 /* XXX ATHROW will need a flag in the typechecker */
952 /* table jumps ********************************************************/
954 case JAVA_LOOKUPSWITCH:
957 lookup_target_t *lookup;
958 #if defined(ENABLE_VERIFIER)
962 nextp = MEMORY_ALIGN((p + 1), 4);
964 CHECK_END_OF_BYTECODE(nextp + 8);
966 OP_PREPARE_ZEROFLAGS(opcode);
970 j = p + SUCK_BE_S4(m->jcode + nextp);
971 iptr->sx.s23.s3.lookupdefault.insindex = j;
973 CHECK_BYTECODE_INDEX(j);
976 /* number of pairs */
978 num = SUCK_BE_U4(m->jcode + nextp);
979 iptr->sx.s23.s2.lookupcount = num;
982 /* allocate the intermediate code table */
984 lookup = DMNEW(lookup_target_t, num);
985 iptr->dst.lookup = lookup;
987 /* iterate over the lookup table */
989 CHECK_END_OF_BYTECODE(nextp + 8 * num);
991 for (i = 0; i < num; i++) {
994 j = SUCK_BE_S4(m->jcode + nextp);
999 #if defined(ENABLE_VERIFIER)
1000 /* check if the lookup table is sorted correctly */
1002 if (i && (j <= prevvalue)) {
1003 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
1010 j = p + SUCK_BE_S4(m->jcode + nextp);
1011 lookup->target.insindex = j;
1014 CHECK_BYTECODE_INDEX(j);
1023 case JAVA_TABLESWITCH:
1027 branch_target_t *table;
1030 nextp = MEMORY_ALIGN((p + 1), 4);
1032 CHECK_END_OF_BYTECODE(nextp + 12);
1034 OP_PREPARE_ZEROFLAGS(opcode);
1036 /* default target */
1038 deftarget = p + SUCK_BE_S4(m->jcode + nextp);
1040 CHECK_BYTECODE_INDEX(deftarget);
1041 MARK_BASICBLOCK(deftarget);
1045 j = SUCK_BE_S4(m->jcode + nextp);
1046 iptr->sx.s23.s2.tablelow = j;
1051 num = SUCK_BE_S4(m->jcode + nextp);
1052 iptr->sx.s23.s3.tablehigh = num;
1055 /* calculate the number of table entries */
1059 #if defined(ENABLE_VERIFIER)
1061 exceptions_throw_verifyerror(m,
1062 "invalid TABLESWITCH: upper bound < lower bound");
1066 /* create the intermediate code table */
1067 /* the first entry is the default target */
1069 table = DMNEW(branch_target_t, 1 + num);
1070 iptr->dst.table = table;
1071 (table++)->insindex = deftarget;
1073 /* iterate over the target table */
1075 CHECK_END_OF_BYTECODE(nextp + 4 * num);
1077 for (i = 0; i < num; i++) {
1078 j = p + SUCK_BE_S4(m->jcode + nextp);
1079 (table++)->insindex = j;
1081 CHECK_BYTECODE_INDEX(j);
1090 /* load and store of object fields ************************************/
1094 jd->isleafmethod = false;
1097 case JAVA_GETSTATIC:
1098 case JAVA_PUTSTATIC:
1102 constant_FMIref *fr;
1103 unresolved_field *uf;
1105 i = SUCK_BE_U2(m->jcode + p + 1);
1106 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
1110 OP_PREPARE_ZEROFLAGS(opcode);
1111 iptr->sx.s23.s3.fmiref = fr;
1113 /* only with -noverify, otherwise the typechecker does this */
1115 #if defined(ENABLE_VERIFIER)
1116 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1118 result = resolve_field_lazy(m, fr);
1119 if (result == resolveFailed)
1122 if (result != resolveSucceeded) {
1123 uf = resolve_create_unresolved_field(m->class, m, iptr);
1128 /* store the unresolved_field pointer */
1130 iptr->sx.s23.s3.uf = uf;
1131 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1133 #if defined(ENABLE_VERIFIER)
1141 /* method invocation **************************************************/
1143 case JAVA_INVOKESTATIC:
1144 i = SUCK_BE_U2(m->jcode + p + 1);
1145 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1150 md = mr->parseddesc.md;
1152 if (md->params == NULL)
1153 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1158 case JAVA_INVOKEINTERFACE:
1159 i = SUCK_BE_U2(m->jcode + p + 1);
1161 mr = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1163 goto invoke_nonstatic_method;
1165 case JAVA_INVOKESPECIAL:
1166 case JAVA_INVOKEVIRTUAL:
1167 i = SUCK_BE_U2(m->jcode + p + 1);
1168 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1170 invoke_nonstatic_method:
1174 md = mr->parseddesc.md;
1176 if (md->params == NULL)
1177 if (!descriptor_params_from_paramtypes(md, 0))
1181 jd->isleafmethod = false;
1183 OP_PREPARE_ZEROFLAGS(opcode);
1184 iptr->sx.s23.s3.fmiref = mr;
1186 /* only with -noverify, otherwise the typechecker does this */
1188 #if defined(ENABLE_VERIFIER)
1189 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1191 result = resolve_method_lazy(m, mr,
1192 (opcode == JAVA_INVOKESPECIAL));
1193 if (result == resolveFailed)
1196 if (result == resolveSucceeded) {
1197 methodinfo *mi = iptr->sx.s23.s3.fmiref->p.method;
1199 /* if this call is monomorphic, turn it into an INVOKESPECIAL */
1201 assert(IS_FMIREF_RESOLVED(iptr->sx.s23.s3.fmiref));
1203 if ((iptr->opc == ICMD_INVOKEVIRTUAL)
1204 && (mi->flags & (ACC_FINAL | ACC_PRIVATE)))
1206 iptr->opc = ICMD_INVOKESPECIAL;
1210 um = resolve_create_unresolved_method(m->class, m, mr,
1211 (opcode == JAVA_INVOKESTATIC),
1212 (opcode == JAVA_INVOKESPECIAL));
1217 /* store the unresolved_method pointer */
1219 iptr->sx.s23.s3.um = um;
1220 iptr->flags.bits |= INS_FLAG_UNRESOLVED;
1222 #if defined(ENABLE_VERIFIER)
1228 /* instructions taking class arguments ********************************/
1231 i = SUCK_BE_U2(m->jcode + p + 1);
1232 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1236 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1239 INSTRUCTIONS_CHECK(2);
1240 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1241 bte = builtintable_get_internal(BUILTIN_new);
1242 OP_BUILTIN_CHECK_EXCEPTION(bte);
1246 case JAVA_CHECKCAST:
1247 i = SUCK_BE_U2(m->jcode + p + 1);
1248 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1252 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1255 if (cr->name->text[0] == '[') {
1256 /* array type cast-check */
1257 flags = INS_FLAG_ARRAY;
1258 jd->isleafmethod = false;
1261 /* object type cast-check */
1264 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1267 case JAVA_INSTANCEOF:
1268 i = SUCK_BE_U2(m->jcode + p + 1);
1269 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1273 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1276 if (cr->name->text[0] == '[') {
1277 /* array type cast-check */
1278 INSTRUCTIONS_CHECK(2);
1279 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1280 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1281 OP_BUILTIN_NO_EXCEPTION(bte);
1285 /* object type cast-check */
1286 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1290 /* synchronization instructions ***************************************/
1292 case JAVA_MONITORENTER:
1293 #if defined(ENABLE_THREADS)
1295 bte = builtintable_get_internal(LOCK_monitor_enter);
1296 OP_BUILTIN_CHECK_EXCEPTION(bte);
1301 OP(ICMD_CHECKNULL_POP);
1305 case JAVA_MONITOREXIT:
1306 #if defined(ENABLE_THREADS)
1308 bte = builtintable_get_internal(LOCK_monitor_exit);
1309 OP_BUILTIN_CHECK_EXCEPTION(bte);
1314 OP(ICMD_CHECKNULL_POP);
1318 /* arithmetic instructions that may become builtin functions **********/
1321 #if !SUPPORT_DIVISION
1322 bte = builtintable_get_internal(BUILTIN_idiv);
1323 OP_BUILTIN_ARITHMETIC(opcode, bte);
1330 #if !SUPPORT_DIVISION
1331 bte = builtintable_get_internal(BUILTIN_irem);
1332 OP_BUILTIN_ARITHMETIC(opcode, bte);
1339 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1340 bte = builtintable_get_internal(BUILTIN_ldiv);
1341 OP_BUILTIN_ARITHMETIC(opcode, bte);
1348 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1349 bte = builtintable_get_internal(BUILTIN_lrem);
1350 OP_BUILTIN_ARITHMETIC(opcode, bte);
1357 #if defined(__I386__)
1360 bte = builtintable_get_internal(BUILTIN_frem);
1361 OP_BUILTIN_NO_EXCEPTION(bte);
1366 #if defined(__I386__)
1369 bte = builtintable_get_internal(BUILTIN_drem);
1370 OP_BUILTIN_NO_EXCEPTION(bte);
1375 #if defined(__ALPHA__)
1377 bte = builtintable_get_internal(BUILTIN_f2i);
1378 OP_BUILTIN_NO_EXCEPTION(bte);
1388 #if defined(__ALPHA__)
1390 bte = builtintable_get_internal(BUILTIN_f2l);
1391 OP_BUILTIN_NO_EXCEPTION(bte);
1401 #if defined(__ALPHA__)
1403 bte = builtintable_get_internal(BUILTIN_d2i);
1404 OP_BUILTIN_NO_EXCEPTION(bte);
1414 #if defined(__ALPHA__)
1416 bte = builtintable_get_internal(BUILTIN_d2l);
1417 OP_BUILTIN_NO_EXCEPTION(bte);
1426 /* invalid opcodes ****************************************************/
1428 /* check for invalid opcodes if the verifier is enabled */
1429 #if defined(ENABLE_VERIFIER)
1430 case JAVA_BREAKPOINT:
1431 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1434 case 186: /* unused opcode */
1488 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1492 #endif /* defined(ENABLE_VERIFIER) */
1494 /* opcodes that don't require translation *****************************/
1497 /* straight-forward translation to ICMD */
1503 /* verifier checks ****************************************************/
1505 #if defined(ENABLE_VERIFIER)
1506 /* If WIDE was used correctly, iswide should have been reset by now. */
1508 exceptions_throw_verifyerror(m,
1509 "Illegal instruction: WIDE before incompatible opcode");
1512 #endif /* defined(ENABLE_VERIFIER) */
1516 if (JITDATA_HAS_FLAG_REORDER(jd)) {
1517 /* add a NOP to the last basic block */
1519 INSTRUCTIONS_CHECK(1);
1523 /*** END OF LOOP **********************************************************/
1525 /* assert that we did not write more ICMDs than allocated */
1527 assert(ipc <= pd.instructionslength);
1528 assert(ipc == (iptr - pd.instructions));
1530 /*** verifier checks ******************************************************/
1532 #if defined(ENABLE_VERIFIER)
1533 if (p != m->jcodelength) {
1534 exceptions_throw_verifyerror(m,
1535 "Command-sequence crosses code-boundary");
1540 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1543 #endif /* defined(ENABLE_VERIFIER) */
1545 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1547 /* adjust block count if target 0 is not first intermediate instruction */
1549 if (!jd->basicblockindex[0] || (jd->basicblockindex[0] > 1))
1552 jd->branchtoentry = true;
1554 /* copy local to method variables */
1556 jd->instructions = pd.instructions;
1557 jd->instructioncount = ipc;
1558 jd->basicblockcount = b_count;
1559 jd->stackcount = s_count + jd->basicblockcount * m->maxstack; /* in-stacks */
1561 /* allocate stack table */
1563 jd->stack = DMNEW(stackelement, jd->stackcount);
1565 /* build basic block list */
1567 bptr = jd->basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1569 /* zero out all basic block structures */
1571 MZERO(bptr, basicblock, b_count + 1);
1575 /* additional block if target 0 is not first intermediate instruction */
1577 if (!jd->basicblockindex[0] || (jd->basicblockindex[0] > 1)) {
1578 BASICBLOCK_INIT(bptr, m);
1580 bptr->iinstr = jd->instructions;
1581 /* bptr->icount is set when the next block is allocated */
1583 bptr->nr = b_count++;
1585 bptr[-1].next = bptr;
1588 /* allocate blocks */
1590 for (p = 0; p < m->jcodelength; p++) {
1591 if (jd->basicblockindex[p] & 1) {
1592 #if defined(ENABLE_VERIFIER)
1593 /* Check if this block starts at the beginning of an
1596 if (!pd.instructionstart[p]) {
1597 exceptions_throw_verifyerror(m,
1598 "Branch into middle of instruction");
1603 /* allocate the block */
1605 BASICBLOCK_INIT(bptr, m);
1607 bptr->iinstr = jd->instructions + (jd->basicblockindex[p] >> 1);
1609 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1611 /* bptr->icount is set when the next block is allocated */
1613 jd->basicblockindex[p] = b_count;
1615 bptr->nr = b_count++;
1617 bptr[-1].next = bptr;
1621 /* set instruction count of last real block */
1624 bptr[-1].icount = (jd->instructions + jd->instructioncount) - bptr[-1].iinstr;
1627 /* allocate additional block at end */
1629 BASICBLOCK_INIT(bptr,m);
1631 jd->basicblockindex[m->jcodelength] = b_count;
1633 /* set basicblock pointers in exception table */
1635 if (!parse_resolve_exception_table(jd))
1638 /* store the local map */
1640 jd->local_map = local_map;
1642 /* calculate local variable renaming */
1652 /* iterate over local_map[0..m->maxlocals*5] and set all existing */
1653 /* index,type pairs (local_map[index*5+type]==1) to an unique value */
1654 /* -> == new local var index */
1655 for(i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1657 *mapptr = nlocals++;
1662 jd->localcount = nlocals;
1664 /* calculate the (maximum) number of variables needed */
1667 nlocals /* local variables */
1668 + jd->basicblockcount * m->maxstack /* invars */
1669 + s_count; /* variables created within blocks (non-invar) */
1671 /* reserve the first indices for local variables */
1673 jd->vartop = nlocals;
1675 /* reserve extra variables needed by stack analyse */
1677 jd->varcount += STACK_EXTRA_VARS;
1678 jd->vartop += STACK_EXTRA_VARS;
1680 /* The verifier needs space for saving invars in some cases and */
1681 /* extra variables. */
1683 #if defined(ENABLE_VERIFIER)
1684 jd->varcount += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1685 jd->vartop += VERIFIER_EXTRA_LOCALS + VERIFIER_EXTRA_VARS + m->maxstack;
1687 /* allocate and initialize the variable array */
1689 jd->var = DMNEW(varinfo, jd->varcount);
1690 MZERO(jd->var, varinfo, jd->varcount);
1692 /* set types of all locals in jd->var */
1694 for(mapptr = local_map, i = 0; i < (m->maxlocals * 5); i++, mapptr++)
1695 if (*mapptr != UNUSED)
1696 VAR(*mapptr)->type = i%5;
1699 /* everything's ok */
1703 /*** goto labels for throwing verifier exceptions *************************/
1705 #if defined(ENABLE_VERIFIER)
1707 throw_unexpected_end_of_bytecode:
1708 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1711 throw_invalid_bytecode_index:
1712 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1715 throw_illegal_local_variable_number:
1716 exceptions_throw_verifyerror(m, "Illegal local variable number");
1719 #endif /* ENABLE_VERIFIER */
1724 * These are local overrides for various environment variables in Emacs.
1725 * Please do not remove this and leave it at the end of the file, where
1726 * Emacs will automagically detect them.
1727 * ---------------------------------------------------------------------
1730 * indent-tabs-mode: t
1734 * vim:noexpandtab:sw=4:ts=4: