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 5375 2006-09-06 16:01:23Z 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->new_basicblockindex = DMNEW(s4, m->jcodelength + 1);
103 pd->instructionstart = DMNEW(u1, m->jcodelength + 1);
105 MZERO(jd->new_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_check_instructions ****************************************************
126 Checks if there's enough room in the instructions array for the
127 required instructions. If not, reallocate the data structures.
129 *******************************************************************************/
131 static instruction *parse_check_instructions(parsedata_t *pd, s4 ipc)
135 /* increase the size of the instruction array */
137 pd->instructionslength += INSTRUCTIONS_INCREMENT;
139 /* reallocate the array */
141 pd->instructions = DMREALLOC(pd->instructions, instruction, ipc,
142 pd->instructionslength);
144 /* return the iptr */
146 return pd->instructions + ipc;
150 /*******************************************************************************
152 function 'parse' scans the JavaVM code and generates intermediate code
154 During parsing the block index table is used to store at bit pos 0
155 a flag which marks basic block starts and at position 1 to 31 the
156 intermediate instruction index. After parsing the block index table
157 is scanned, for marked positions a block is generated and the block
158 number is stored in the block index table.
160 *******************************************************************************/
162 static exceptiontable * new_fillextable(
165 exceptiontable *extable,
166 exceptiontable *raw_extable,
167 int exceptiontablelength,
172 if (exceptiontablelength == 0)
175 b_count = *block_count;
177 for (src = exceptiontablelength-1; src >=0; src--) {
178 /* the start of the handled region becomes a basic block start */
179 p = raw_extable[src].startpc;
180 CHECK_BYTECODE_INDEX(p);
181 extable->startpc = p;
184 p = raw_extable[src].endpc; /* see JVM Spec 4.7.3 */
185 CHECK_BYTECODE_INDEX_EXCLUSIVE(p);
187 #if defined(ENABLE_VERIFIER)
188 if (p <= raw_extable[src].startpc) {
189 exceptions_throw_verifyerror(m,
190 "Invalid exception handler range");
196 /* end of handled region becomes a basic block boundary */
197 /* (If it is the bytecode end, we'll use the special */
198 /* end block that is created anyway.) */
199 if (p < m->jcodelength)
202 /* the start of the handler becomes a basic block start */
203 p = raw_extable[src].handlerpc;
204 CHECK_BYTECODE_INDEX(p);
205 extable->handlerpc = p;
208 extable->catchtype = raw_extable[src].catchtype;
209 extable->next = NULL;
210 extable->down = &extable[1];
214 *block_count = b_count;
219 #if defined(ENABLE_VERIFIER)
220 throw_invalid_bytecode_index:
221 exceptions_throw_verifyerror(m,
222 "Illegal bytecode index in exception table");
227 /*** macro for checking the length of the bytecode ***/
229 #if defined(ENABLE_VERIFIER)
230 #define CHECK_END_OF_BYTECODE(neededlength) \
232 if ((neededlength) > m->jcodelength) \
233 goto throw_unexpected_end_of_bytecode; \
235 #else /* !ENABLE_VERIFIER */
236 #define CHECK_END_OF_BYTECODE(neededlength)
237 #endif /* ENABLE_VERIFIER */
239 bool new_parse(jitdata *jd)
241 methodinfo *m; /* method being parsed */
245 instruction *iptr; /* current ptr into instruction array */
246 s4 ipc; /* intermediate instruction counter */
247 s4 p; /* java instruction counter */
248 s4 nextp; /* start of next java instruction */
249 s4 opcode; /* java opcode */
252 int b_count = 0; /* basic block counter */
253 int s_count = 0; /* stack element counter */
254 bool blockend = false; /* true if basic block end has been reached */
255 bool iswide = false; /* true if last instruction was a wide */
256 constant_classref *cr;
257 constant_classref *compr;
259 builtintable_entry *bte;
262 unresolved_method *um;
263 resolve_result_t result;
271 int *local_map; /* local pointer to renaming structore */
272 /* is assigned to rd->local_map at the end */
274 /* get required compiler data */
281 /* allocate buffers for local variable renaming */
282 local_map = DMNEW(int, cd->maxlocals * 5);
283 for (i = 0; i < cd->maxlocals; i++) {
284 local_map[i * 5 + 0] = 0;
285 local_map[i * 5 + 1] = 0;
286 local_map[i * 5 + 2] = 0;
287 local_map[i * 5 + 3] = 0;
288 local_map[i * 5 + 4] = 0;
292 /* initialize the parse data structures */
294 parse_setup(jd, &pd);
296 /* initialize local variables */
298 iptr = pd.instructions;
301 /* compute branch targets of exception table */
303 if (!new_fillextable(jd, m,
304 &(cd->exceptiontable[cd->exceptiontablelength-1]),
306 m->exceptiontablelength,
312 s_count = 1 + m->exceptiontablelength; /* initialize stack element counter */
314 #if defined(ENABLE_THREADS)
315 if (checksync && (m->flags & ACC_SYNCHRONIZED))
316 jd->isleafmethod = false;
319 /* setup line number info */
324 if (m->linenumbercount == 0) {
328 linepcchange = m->linenumbers[0].start_pc;
331 /*** LOOP OVER ALL BYTECODE INSTRUCTIONS **********************************/
333 for (p = 0; p < m->jcodelength; p = nextp) {
335 /* mark this position as a valid instruction start */
337 pd.instructionstart[p] = 1;
339 /* change the current line number, if necessary */
341 /* XXX rewrite this using pointer arithmetic */
343 if (linepcchange == p) {
344 if (m->linenumbercount > lineindex) {
346 currentline = m->linenumbers[lineindex].line_number;
348 if (lineindex < m->linenumbercount) {
349 linepcchange = m->linenumbers[lineindex].start_pc;
350 if (linepcchange == p)
351 goto next_linenumber;
357 /* fetch next opcode */
359 opcode = SUCK_BE_U1(m->jcode + p);
361 /* some compilers put a JAVA_NOP after a blockend instruction */
363 if (blockend && (opcode != JAVA_NOP)) {
364 /* start new block */
370 /* store intermediate instruction count (bit 0 mark block starts) */
372 jd->new_basicblockindex[p] |= (ipc << 1);
374 /* compute next instruction start */
376 nextp = p + jcommandsize[opcode];
378 CHECK_END_OF_BYTECODE(nextp);
380 /* add stack elements produced by this instruction */
382 s_count += stackreq[opcode];
384 /* We check here for the space of 1 instruction in the
385 instruction array. If an opcode is converted to more than
386 1 instruction, this is checked in the corresponding
389 INSTRUCTIONS_CHECK(1);
391 /* translate this bytecode instruction */
398 /* pushing constants onto the stack ***********************************/
401 OP_LOADCONST_I(SUCK_BE_S1(m->jcode + p + 1));
405 OP_LOADCONST_I(SUCK_BE_S2(m->jcode + p + 1));
409 i = SUCK_BE_U1(m->jcode + p + 1);
410 goto pushconstantitem;
414 i = SUCK_BE_U2(m->jcode + p + 1);
418 #if defined(ENABLE_VERIFIER)
419 if (i >= m->class->cpcount) {
420 exceptions_throw_verifyerror(m,
421 "Attempt to access constant outside range");
426 switch (m->class->cptags[i]) {
427 case CONSTANT_Integer:
428 OP_LOADCONST_I(((constant_integer *) (m->class->cpinfos[i]))->value);
431 OP_LOADCONST_L(((constant_long *) (m->class->cpinfos[i]))->value);
434 OP_LOADCONST_F(((constant_float *) (m->class->cpinfos[i]))->value);
436 case CONSTANT_Double:
437 OP_LOADCONST_D(((constant_double *) (m->class->cpinfos[i]))->value);
439 case CONSTANT_String:
440 OP_LOADCONST_STRING(literalstring_new((utf *) (m->class->cpinfos[i])));
443 cr = (constant_classref *) (m->class->cpinfos[i]);
445 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
448 /* if not resolved, c == NULL */
450 OP_LOADCONST_CLASSINFO_OR_CLASSREF_CHECK(c, cr);
454 #if defined(ENABLE_VERIFIER)
456 exceptions_throw_verifyerror(m,
457 "Invalid constant type to push");
463 case JAVA_ACONST_NULL:
474 OP_LOADCONST_I(opcode - JAVA_ICONST_0);
479 OP_LOADCONST_L(opcode - JAVA_LCONST_0);
485 OP_LOADCONST_F(opcode - JAVA_FCONST_0);
490 OP_LOADCONST_D(opcode - JAVA_DCONST_0);
493 /* local variable access instructions *********************************/
498 if (iswide == false) {
499 i = SUCK_BE_U1(m->jcode + p + 1);
502 i = SUCK_BE_U2(m->jcode + p + 1);
506 OP_LOAD_ONEWORD(opcode, i, opcode - JAVA_ILOAD);
511 if (iswide == false) {
512 i = SUCK_BE_U1(m->jcode + p + 1);
515 i = SUCK_BE_U2(m->jcode + p + 1);
519 OP_LOAD_TWOWORD(opcode, i, opcode - JAVA_ILOAD);
526 OP_LOAD_ONEWORD(ICMD_ILOAD, opcode - JAVA_ILOAD_0, TYPE_INT);
533 OP_LOAD_TWOWORD(ICMD_LLOAD, opcode - JAVA_LLOAD_0, TYPE_LNG);
540 OP_LOAD_ONEWORD(ICMD_FLOAD, opcode - JAVA_FLOAD_0, TYPE_FLT);
547 OP_LOAD_TWOWORD(ICMD_DLOAD, opcode - JAVA_DLOAD_0, TYPE_DBL);
554 OP_LOAD_ONEWORD(ICMD_ALOAD, opcode - JAVA_ALOAD_0, TYPE_ADR);
560 if (iswide == false) {
561 i = SUCK_BE_U1(m->jcode + p + 1);
564 i = SUCK_BE_U2(m->jcode + p + 1);
568 OP_STORE_ONEWORD(opcode, i, opcode - JAVA_ISTORE);
573 if (iswide == false) {
574 i = SUCK_BE_U1(m->jcode + p + 1);
577 i = SUCK_BE_U2(m->jcode + p + 1);
581 OP_STORE_TWOWORD(opcode, i, opcode - JAVA_ISTORE);
588 OP_STORE_ONEWORD(ICMD_ISTORE, opcode - JAVA_ISTORE_0, TYPE_INT);
595 OP_STORE_TWOWORD(ICMD_LSTORE, opcode - JAVA_LSTORE_0, TYPE_LNG);
602 OP_STORE_ONEWORD(ICMD_FSTORE, opcode - JAVA_FSTORE_0, TYPE_FLT);
609 OP_STORE_TWOWORD(ICMD_DSTORE, opcode - JAVA_DSTORE_0, TYPE_DBL);
616 OP_STORE_ONEWORD(ICMD_ASTORE, opcode - JAVA_ASTORE_0, TYPE_ADR);
623 if (iswide == false) {
624 i = SUCK_BE_U1(m->jcode + p + 1);
625 v = SUCK_BE_S1(m->jcode + p + 2);
629 i = SUCK_BE_U2(m->jcode + p + 1);
630 v = SUCK_BE_S2(m->jcode + p + 3);
635 LOCALTYPE_USED(i, TYPE_INT);
636 OP_LOCALINDEX_I(opcode, i, v);
640 /* wider index for loading, storing and incrementing ******************/
647 /* managing arrays ****************************************************/
650 switch (SUCK_BE_S1(m->jcode + p + 1)) {
652 bte = builtintable_get_internal(BUILTIN_newarray_boolean);
655 bte = builtintable_get_internal(BUILTIN_newarray_char);
658 bte = builtintable_get_internal(BUILTIN_newarray_float);
661 bte = builtintable_get_internal(BUILTIN_newarray_double);
664 bte = builtintable_get_internal(BUILTIN_newarray_byte);
667 bte = builtintable_get_internal(BUILTIN_newarray_short);
670 bte = builtintable_get_internal(BUILTIN_newarray_int);
673 bte = builtintable_get_internal(BUILTIN_newarray_long);
675 #if defined(ENABLE_VERIFIER)
677 exceptions_throw_verifyerror(m, "Invalid array-type to create");
681 OP_BUILTIN_CHECK_EXCEPTION(bte);
685 i = SUCK_BE_U2(m->jcode + p + 1);
686 compr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
690 if (!(cr = class_get_classref_multiarray_of(1, compr)))
693 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
696 INSTRUCTIONS_CHECK(2);
697 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
698 bte = builtintable_get_internal(BUILTIN_newarray);
699 OP_BUILTIN_CHECK_EXCEPTION(bte);
703 case JAVA_MULTIANEWARRAY:
704 jd->isleafmethod = false;
705 i = SUCK_BE_U2(m->jcode + p + 1);
706 j = SUCK_BE_U1(m->jcode + p + 3);
708 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
712 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
715 /* if unresolved, c == NULL */
717 iptr->s1.argcount = j;
718 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags */);
721 /* control flow instructions ******************************************/
740 i = p + SUCK_BE_S2(m->jcode + p + 1);
741 CHECK_BYTECODE_INDEX(i);
744 OP_INSINDEX(opcode, i);
748 i = p + SUCK_BE_S4(m->jcode + p + 1);
749 CHECK_BYTECODE_INDEX(i);
752 OP_INSINDEX(opcode, i);
756 i = p + SUCK_BE_S2(m->jcode + p + 1);
758 CHECK_BYTECODE_INDEX(i);
761 OP_PREPARE_ZEROFLAGS(JAVA_JSR);
762 iptr->sx.s23.s3.jsrtarget.insindex = i;
767 i = p + SUCK_BE_S4(m->jcode + p + 1);
771 if (iswide == false) {
772 i = SUCK_BE_U1(m->jcode + p + 1);
775 i = SUCK_BE_U2(m->jcode + p + 1);
781 OP_LOAD_ONEWORD(opcode, i, TYPE_ADR);
791 /* XXX ARETURN will need a flag in the typechecker */
797 /* XXX ATHROW will need a flag in the typechecker */
802 /* table jumps ********************************************************/
804 case JAVA_LOOKUPSWITCH:
807 lookup_target_t *lookup;
808 #if defined(ENABLE_VERIFIER)
812 nextp = ALIGN((p + 1), 4);
814 CHECK_END_OF_BYTECODE(nextp + 8);
816 OP_PREPARE_ZEROFLAGS(opcode);
820 j = p + SUCK_BE_S4(m->jcode + nextp);
821 iptr->sx.s23.s3.lookupdefault.insindex = j;
823 CHECK_BYTECODE_INDEX(j);
826 /* number of pairs */
828 num = SUCK_BE_U4(m->jcode + nextp);
829 iptr->sx.s23.s2.lookupcount = num;
832 /* allocate the intermediate code table */
834 lookup = DMNEW(lookup_target_t, num);
835 iptr->dst.lookup = lookup;
837 /* iterate over the lookup table */
839 CHECK_END_OF_BYTECODE(nextp + 8 * num);
841 for (i = 0; i < num; i++) {
844 j = SUCK_BE_S4(m->jcode + nextp);
849 #if defined(ENABLE_VERIFIER)
850 /* check if the lookup table is sorted correctly */
852 if (i && (j <= prevvalue)) {
853 exceptions_throw_verifyerror(m, "Unsorted lookup switch");
860 j = p + SUCK_BE_S4(m->jcode + nextp);
861 lookup->target.insindex = j;
864 CHECK_BYTECODE_INDEX(j);
873 case JAVA_TABLESWITCH:
877 branch_target_t *table;
880 nextp = ALIGN((p + 1), 4);
882 CHECK_END_OF_BYTECODE(nextp + 12);
884 OP_PREPARE_ZEROFLAGS(opcode);
888 deftarget = p + SUCK_BE_S4(m->jcode + nextp);
890 CHECK_BYTECODE_INDEX(deftarget);
891 MARK_BASICBLOCK(deftarget);
895 j = SUCK_BE_S4(m->jcode + nextp);
896 iptr->sx.s23.s2.tablelow = j;
901 num = SUCK_BE_S4(m->jcode + nextp);
902 iptr->sx.s23.s3.tablehigh = num;
905 /* calculate the number of table entries */
909 #if defined(ENABLE_VERIFIER)
911 exceptions_throw_verifyerror(m,
912 "invalid TABLESWITCH: upper bound < lower bound");
916 /* create the intermediate code table */
917 /* the first entry is the default target */
919 table = MNEW(branch_target_t, 1 + num);
920 iptr->dst.table = table;
921 (table++)->insindex = deftarget;
923 /* iterate over the target table */
925 CHECK_END_OF_BYTECODE(nextp + 4 * num);
927 for (i = 0; i < num; i++) {
928 j = p + SUCK_BE_S4(m->jcode + nextp);
929 (table++)->insindex = j;
931 CHECK_BYTECODE_INDEX(j);
940 /* load and store of object fields ************************************/
944 jd->isleafmethod = false;
953 unresolved_field *uf;
955 i = SUCK_BE_U2(m->jcode + p + 1);
956 fr = class_getconstant(m->class, i, CONSTANT_Fieldref);
960 OP_PREPARE_ZEROFLAGS(opcode);
961 iptr->sx.s23.s3.fmiref = fr;
963 /* only with -noverify, otherwise the typechecker does this */
965 #if defined(ENABLE_VERIFIER)
966 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
968 result = new_resolve_field_lazy(iptr, m);
969 if (result == resolveFailed)
972 if (result != resolveSucceeded) {
973 uf = new_create_unresolved_field(m->class, m, iptr);
978 /* store the unresolved_field pointer */
980 iptr->sx.s23.s3.uf = uf;
981 iptr->flags.bits = INS_FLAG_UNRESOLVED;
983 #if defined(ENABLE_VERIFIER)
991 /* method invocation **************************************************/
993 case JAVA_INVOKESTATIC:
994 i = SUCK_BE_U2(m->jcode + p + 1);
995 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1000 md = mr->parseddesc.md;
1002 if (md->params == NULL)
1003 if (!descriptor_params_from_paramtypes(md, ACC_STATIC))
1008 case JAVA_INVOKEINTERFACE:
1009 i = SUCK_BE_U2(m->jcode + p + 1);
1011 mr = class_getconstant(m->class, i, CONSTANT_InterfaceMethodref);
1013 goto invoke_nonstatic_method;
1015 case JAVA_INVOKESPECIAL:
1016 case JAVA_INVOKEVIRTUAL:
1017 i = SUCK_BE_U2(m->jcode + p + 1);
1018 mr = class_getconstant(m->class, i, CONSTANT_Methodref);
1020 invoke_nonstatic_method:
1024 md = mr->parseddesc.md;
1026 if (md->params == NULL)
1027 if (!descriptor_params_from_paramtypes(md, 0))
1031 jd->isleafmethod = false;
1033 OP_PREPARE_ZEROFLAGS(opcode);
1034 iptr->sx.s23.s3.fmiref = mr;
1036 /* only with -noverify, otherwise the typechecker does this */
1038 #if defined(ENABLE_VERIFIER)
1039 if (!JITDATA_HAS_FLAG_VERIFY(jd)) {
1041 result = new_resolve_method_lazy(iptr, m);
1042 if (result == resolveFailed)
1045 if (result != resolveSucceeded) {
1046 um = new_create_unresolved_method(m->class, m, iptr);
1051 /* store the unresolved_method pointer */
1053 iptr->sx.s23.s3.um = um;
1054 iptr->flags.bits = INS_FLAG_UNRESOLVED;
1056 #if defined(ENABLE_VERIFIER)
1062 /* instructions taking class arguments ********************************/
1065 i = SUCK_BE_U2(m->jcode + p + 1);
1066 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1070 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1073 INSTRUCTIONS_CHECK(2);
1074 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1075 bte = builtintable_get_internal(BUILTIN_new);
1076 OP_BUILTIN_CHECK_EXCEPTION(bte);
1080 case JAVA_CHECKCAST:
1081 i = SUCK_BE_U2(m->jcode + p + 1);
1082 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1086 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1089 if (cr->name->text[0] == '[') {
1090 /* array type cast-check */
1091 flags = INS_FLAG_ARRAY;
1092 jd->isleafmethod = false;
1095 /* object type cast-check */
1098 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, flags);
1101 case JAVA_INSTANCEOF:
1102 i = SUCK_BE_U2(m->jcode + p + 1);
1103 cr = (constant_classref *) class_getconstant(m->class, i, CONSTANT_Class);
1107 if (!resolve_classref(m, cr, resolveLazy, true, true, &c))
1110 if (cr->name->text[0] == '[') {
1111 /* array type cast-check */
1112 INSTRUCTIONS_CHECK(2);
1113 OP_LOADCONST_CLASSINFO_OR_CLASSREF_NOCHECK(c, cr);
1114 bte = builtintable_get_internal(BUILTIN_arrayinstanceof);
1115 OP_BUILTIN_NO_EXCEPTION(bte);
1119 /* object type cast-check */
1120 OP_S3_CLASSINFO_OR_CLASSREF(opcode, c, cr, 0 /* flags*/);
1124 /* synchronization instructions ***************************************/
1126 case JAVA_MONITORENTER:
1127 #if defined(ENABLE_THREADS)
1129 bte = builtintable_get_internal(LOCK_monitor_enter);
1130 OP_BUILTIN_CHECK_EXCEPTION(bte);
1135 OP(ICMD_CHECKNULL_POP);
1139 case JAVA_MONITOREXIT:
1140 #if defined(ENABLE_THREADS)
1142 bte = builtintable_get_internal(LOCK_monitor_exit);
1143 OP_BUILTIN_CHECK_EXCEPTION(bte);
1148 OP(ICMD_CHECKNULL_POP);
1152 /* arithmetic instructions that may become builtin functions **********/
1155 #if !SUPPORT_DIVISION
1156 bte = builtintable_get_internal(BUILTIN_idiv);
1157 OP_BUILTIN_ARITHMETIC(opcode, bte);
1164 #if !SUPPORT_DIVISION
1165 bte = builtintable_get_internal(BUILTIN_irem);
1166 OP_BUILTIN_ARITHMETIC(opcode, bte);
1173 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1174 bte = builtintable_get_internal(BUILTIN_ldiv);
1175 OP_BUILTIN_ARITHMETIC(opcode, bte);
1182 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
1183 bte = builtintable_get_internal(BUILTIN_lrem);
1184 OP_BUILTIN_ARITHMETIC(opcode, bte);
1191 #if defined(__I386__)
1194 bte = builtintable_get_internal(BUILTIN_frem);
1195 OP_BUILTIN_NO_EXCEPTION(bte);
1200 #if defined(__I386__)
1203 bte = builtintable_get_internal(BUILTIN_drem);
1204 OP_BUILTIN_NO_EXCEPTION(bte);
1209 #if defined(__ALPHA__)
1211 bte = builtintable_get_internal(BUILTIN_f2i);
1212 OP_BUILTIN_NO_EXCEPTION(bte);
1222 #if defined(__ALPHA__)
1224 bte = builtintable_get_internal(BUILTIN_f2l);
1225 OP_BUILTIN_NO_EXCEPTION(bte);
1235 #if defined(__ALPHA__)
1237 bte = builtintable_get_internal(BUILTIN_d2i);
1238 OP_BUILTIN_NO_EXCEPTION(bte);
1248 #if defined(__ALPHA__)
1250 bte = builtintable_get_internal(BUILTIN_d2l);
1251 OP_BUILTIN_NO_EXCEPTION(bte);
1260 /* invalid opcodes ****************************************************/
1262 /* check for invalid opcodes if the verifier is enabled */
1263 #if defined(ENABLE_VERIFIER)
1264 case JAVA_BREAKPOINT:
1265 exceptions_throw_verifyerror(m, "Quick instructions shouldn't appear, yet.");
1268 case 186: /* unused opcode */
1322 exceptions_throw_verifyerror(m, "Illegal opcode %d at instr %d\n",
1326 #endif /* defined(ENABLE_VERIFIER) */
1328 /* opcodes that don't require translation *****************************/
1331 /* straight-forward translation to ICMD */
1337 /* verifier checks ****************************************************/
1339 #if defined(ENABLE_VERIFIER)
1340 /* If WIDE was used correctly, iswide should have been reset by now. */
1342 exceptions_throw_verifyerror(m,
1343 "Illegal instruction: WIDE before incompatible opcode");
1346 #endif /* defined(ENABLE_VERIFIER) */
1350 /* add a NOP to the last basic block */
1352 INSTRUCTIONS_CHECK(1);
1355 /*** END OF LOOP **********************************************************/
1357 /* assert that we did not write more ICMDs than allocated */
1359 assert(ipc <= pd.instructionslength);
1360 assert(ipc == (iptr - pd.instructions));
1362 /*** verifier checks ******************************************************/
1364 #if defined(ENABLE_VERIFIER)
1365 if (p != m->jcodelength) {
1366 exceptions_throw_verifyerror(m,
1367 "Command-sequence crosses code-boundary");
1372 exceptions_throw_verifyerror(m, "Falling off the end of the code");
1375 #endif /* defined(ENABLE_VERIFIER) */
1377 /*** setup the methodinfo, allocate stack and basic blocks ****************/
1379 /* adjust block count if target 0 is not first intermediate instruction */
1381 if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1))
1384 /* copy local to method variables */
1386 jd->new_instructions = pd.instructions;
1387 jd->new_instructioncount = ipc;
1388 jd->new_basicblockcount = b_count;
1389 jd->new_stackcount = s_count + jd->new_basicblockcount * m->maxstack; /* in-stacks */
1391 /* allocate stack table */
1393 jd->new_stack = DMNEW(stackelement, jd->new_stackcount);
1395 /* build basic block list */
1397 bptr = jd->new_basicblocks = DMNEW(basicblock, b_count + 1); /* one more for end ipc */
1399 /* zero out all basic block structures */
1401 MZERO(bptr, basicblock, b_count + 1);
1404 jd->new_c_debug_nr = 0;
1406 /* additional block if target 0 is not first intermediate instruction */
1408 if (!jd->new_basicblockindex[0] || (jd->new_basicblockindex[0] > 1)) {
1409 BASICBLOCK_INIT(bptr, m);
1411 bptr->iinstr = jd->new_instructions;
1412 /* bptr->icount is set when the next block is allocated */
1416 bptr[-1].next = bptr;
1419 /* allocate blocks */
1421 for (p = 0; p < m->jcodelength; p++) {
1422 if (jd->new_basicblockindex[p] & 1) {
1423 #if defined(ENABLE_VERIFIER)
1424 /* Check if this block starts at the beginning of an
1427 if (!pd.instructionstart[p]) {
1428 exceptions_throw_verifyerror(m,
1429 "Branch into middle of instruction");
1434 /* allocate the block */
1436 BASICBLOCK_INIT(bptr, m);
1438 bptr->iinstr = jd->new_instructions + (jd->new_basicblockindex[p] >> 1);
1440 bptr[-1].icount = bptr->iinstr - bptr[-1].iinstr;
1442 /* bptr->icount is set when the next block is allocated */
1444 jd->new_basicblockindex[p] = b_count;
1448 bptr[-1].next = bptr;
1452 /* set instruction count of last real block */
1455 bptr[-1].icount = (jd->new_instructions + jd->new_instructioncount) - bptr[-1].iinstr;
1458 /* allocate additional block at end */
1460 BASICBLOCK_INIT(bptr,m);
1462 /* set basicblock pointers in exception table */
1464 if (cd->exceptiontablelength > 0) {
1465 cd->exceptiontable[cd->exceptiontablelength - 1].down = NULL;
1468 for (i = 0; i < cd->exceptiontablelength; ++i) {
1469 p = cd->exceptiontable[i].startpc;
1470 cd->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1472 p = cd->exceptiontable[i].endpc;
1473 cd->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1475 p = cd->exceptiontable[i].handlerpc;
1476 cd->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1479 /* XXX activate this if you want to try inlining */
1481 for (i = 0; i < m->exceptiontablelength; ++i) {
1482 p = m->exceptiontable[i].startpc;
1483 m->exceptiontable[i].start = jd->new_basicblocks + jd->new_basicblockindex[p];
1485 p = m->exceptiontable[i].endpc;
1486 m->exceptiontable[i].end = (p == m->jcodelength) ? (jd->new_basicblocks + jd->new_basicblockcount /*+ 1*/) : (jd->new_basicblocks + jd->new_basicblockindex[p]);
1488 p = m->exceptiontable[i].handlerpc;
1489 m->exceptiontable[i].handler = jd->new_basicblocks + jd->new_basicblockindex[p];
1493 #if defined(NEW_VAR)
1494 jd->local_map = local_map;
1496 /* calculate local variable renaming */
1506 /* iterate over local_map[0..m->maxlocals*5] and set all existing */
1507 /* index,type pairs (localmap[index*5+type]==1) to an unique value */
1508 /* -> == new local var index */
1509 for(i = 0; i < (m->maxlocals * 5); i++, mapptr++) {
1511 *mapptr = nlocals++;
1513 *mapptr = LOCAL_UNUSED;
1516 jd->localcount = nlocals;
1517 /* if dropped varindices for temp stackslots get reused(?max 2* */
1518 /* m->maxstack elements for stack), nlocals + s_count would be */
1520 jd->varcount = nlocals + s_count +
1521 jd->new_basicblockcount * m->maxstack; /* out-stacks */
1523 jd->var_top = nlocals;
1524 jd->var = DMNEW(varinfo, jd->varcount);
1528 /* everything's ok */
1532 /*** goto labels for throwing verifier exceptions *************************/
1534 #if defined(ENABLE_VERIFIER)
1536 throw_unexpected_end_of_bytecode:
1537 exceptions_throw_verifyerror(m, "Unexpected end of bytecode");
1540 throw_invalid_bytecode_index:
1541 exceptions_throw_verifyerror(m, "Illegal target of branch instruction");
1544 throw_illegal_local_variable_number:
1545 exceptions_throw_verifyerror(m, "Illegal local variable number");
1548 #endif /* ENABLE_VERIFIER */
1553 * These are local overrides for various environment variables in Emacs.
1554 * Please do not remove this and leave it at the end of the file, where
1555 * Emacs will automagically detect them.
1556 * ---------------------------------------------------------------------
1559 * indent-tabs-mode: t
1563 * vim:noexpandtab:sw=4:ts=4: