1 /* src/vm/jit/intrp/codegen.c - code generator for Interpreter
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: codegen.c 7357 2007-02-14 11:35:59Z twisti $
37 #elif defined(WITH_FFCALL)
40 # error neither WITH_FFI nor WITH_FFCALL defined
47 #include "vm/jit/intrp/codegen.h"
48 #include "vm/jit/intrp/intrp.h"
50 #include "mm/memory.h"
52 #include "native/native.h"
54 #include "vm/builtin.h"
55 #include "vm/exceptions.h"
56 #include "vm/global.h"
57 #include "vm/stringlocal.h"
60 #include "vm/jit/asmpart.h"
61 #include "vm/jit/codegen-common.h"
62 #include "vm/jit/dseg.h"
63 #include "vm/jit/jit.h"
64 #include "vm/jit/parse.h"
65 #include "vm/jit/patcher.h"
66 #include "vm/jit/stack.h"
67 #include "vm/jit/stacktrace.h"
69 #include "vmcore/class.h"
70 #include "vmcore/options.h"
73 #define gen_branch(_inst) { \
75 codegen_addreference(cd, BLOCK_OF(iptr->dst.insindex)); \
78 #define index2offset(_i) (-(_i) * SIZEOF_VOID_P)
80 /* functions used by cacao-gen.i */
82 void genarg_v(codegendata *cd1, Cell v)
84 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
85 *((Cell *) *mcodepp) = v;
89 void genarg_i(codegendata *cd1, s4 i)
91 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
92 *((Cell *) *mcodepp) = i;
96 void genarg_b(codegendata *cd1, s4 i)
101 void genarg_f(codegendata *cd1, float f)
109 void genarg_l(codegendata *cd1, s8 l)
111 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
112 vm_l2twoCell(l, ((Cell *) *mcodepp)[1], ((Cell *) *mcodepp)[0]);
116 void genarg_aRef(codegendata *cd1, java_objectheader *a)
118 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
119 *((java_objectheader **) *mcodepp) = a;
123 void genarg_aArray(codegendata *cd1, java_arrayheader *a)
125 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
126 *((java_arrayheader **) *mcodepp) = a;
130 void genarg_aaTarget(codegendata *cd1, Inst **a)
132 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
133 *((Inst ***) *mcodepp) = a;
137 void genarg_aClass(codegendata *cd1, classinfo *a)
139 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
140 *((classinfo **) *mcodepp) = a;
144 void genarg_acr(codegendata *cd1, constant_classref *a)
146 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
147 *((constant_classref **) *mcodepp) = a;
151 void genarg_addr(codegendata *cd1, u1 *a)
153 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
154 *((u1 **) *mcodepp) = a;
158 void genarg_af(codegendata *cd1, functionptr a)
160 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
161 *((functionptr *) *mcodepp) = a;
165 void genarg_afi(codegendata *cd1, fieldinfo *a)
167 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
168 *((fieldinfo **) *mcodepp) = a;
172 void genarg_am(codegendata *cd1, methodinfo *a)
174 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
175 *((methodinfo **) *mcodepp) = a;
179 void genarg_acell(codegendata *cd1, Cell *a)
181 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
182 *((Cell **) *mcodepp) = a;
186 void genarg_ainst(codegendata *cd1, Inst *a)
188 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
189 *((Inst **) *mcodepp) = a;
193 void genarg_auf(codegendata *cd1, unresolved_field *a)
195 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
196 *((unresolved_field **) *mcodepp) = a;
200 void genarg_aum(codegendata *cd1, unresolved_method *a)
202 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
203 *((unresolved_method **) *mcodepp) = a;
207 void genarg_avftbl(codegendata *cd1, vftbl_t *a)
209 Inst **mcodepp = (Inst **) &(cd1->mcodeptr);
210 *((vftbl_t **) *mcodepp) = a;
215 /* include the interpreter generation functions *******************************/
217 /* Do not use "java-gen.i", it does not work with builddir. */
218 #include <java-gen.i>
221 typedef void (*genfunctionptr) (codegendata *);
223 typedef struct builtin_gen builtin_gen;
230 struct builtin_gen builtin_gen_table[] = {
231 {BUILTIN_new, gen_NEW, },
232 {BUILTIN_newarray, gen_NEWARRAY, },
233 {BUILTIN_newarray_boolean, gen_NEWARRAY_BOOLEAN,},
234 {BUILTIN_newarray_byte, gen_NEWARRAY_BYTE, },
235 {BUILTIN_newarray_char, gen_NEWARRAY_CHAR, },
236 {BUILTIN_newarray_short, gen_NEWARRAY_SHORT, },
237 {BUILTIN_newarray_int, gen_NEWARRAY_INT, },
238 {BUILTIN_newarray_long, gen_NEWARRAY_LONG, },
239 {BUILTIN_newarray_float, gen_NEWARRAY_FLOAT, },
240 {BUILTIN_newarray_double, gen_NEWARRAY_DOUBLE, },
241 {BUILTIN_arrayinstanceof, gen_ARRAYINSTANCEOF, },
243 #if defined(ENABLE_THREADS)
244 {LOCK_monitor_enter, gen_MONITORENTER, },
245 {LOCK_monitor_exit, gen_MONITOREXIT, },
248 #if !(SUPPORT_FLOAT && SUPPORT_LONG && SUPPORT_F2L)
249 {BUILTIN_f2l, gen_F2L, },
252 #if !(SUPPORT_DOUBLE && SUPPORT_LONG && SUPPORT_D2L)
253 {BUILTIN_d2l, gen_D2L, },
256 #if !(SUPPORT_FLOAT && SUPPORT_F2I)
257 {BUILTIN_f2i, gen_F2I, },
260 #if !(SUPPORT_DOUBLE && SUPPORT_D2I)
261 {BUILTIN_d2i, gen_D2I, },
264 #if !SUPPORT_DIVISION
265 {BUILTIN_idiv, gen_IDIV, },
266 {BUILTIN_irem, gen_IREM, },
269 #if !(SUPPORT_DIVISION && SUPPORT_LONG && SUPPORT_LONG_DIV)
270 {BUILTIN_ldiv, gen_LDIV, },
271 {BUILTIN_lrem, gen_LREM, },
274 {BUILTIN_frem, gen_FREM, },
275 {BUILTIN_drem, gen_DREM, },
279 /* codegen *********************************************************************
281 Generates machine code.
283 *******************************************************************************/
285 #define I(value) iptr[0].sx.val.i = (value); break;
287 bool intrp_codegen(jitdata *jd)
292 s4 i, len, s1, s2, d;
297 methodinfo *lm; /* local methodinfo for ICMD_INVOKE* */
298 unresolved_method *um;
299 builtintable_entry *bte;
302 unresolved_field *uf;
305 /* get required compiler data */
311 /* prevent compiler warnings */
318 /* determine stackframe size (in units of ptrint slots) */
320 cd->stackframesize = m->maxlocals;
322 #if defined(ENABLE_THREADS)
323 if (checksync && (m->flags & ACC_SYNCHRONIZED))
324 cd->stackframesize += 1;
327 /* create method header */
329 (void) dseg_add_unique_address(cd, jd->code);
330 (void) dseg_add_unique_s4(cd, cd->stackframesize * SIZEOF_VOID_P);
332 #if defined(ENABLE_THREADS)
333 if (checksync && (m->flags & ACC_SYNCHRONIZED))
334 (void) dseg_add_unique_s4(cd, 1);
337 (void) dseg_add_unique_s4(cd, 0);
339 (void) dseg_add_unique_s4(cd, 0);
340 (void) dseg_add_unique_s4(cd, 0);
341 (void) dseg_add_unique_s4(cd, 0);
343 dseg_addlinenumbertablesize(cd);
345 (void) dseg_add_unique_s4(cd, jd->exceptiontablelength);
347 /* create exception table */
349 for (ex = jd->exceptiontable; ex != NULL; ex = ex->down) {
350 dseg_add_target(cd, ex->start);
351 dseg_add_target(cd, ex->end);
352 dseg_add_target(cd, ex->handler);
353 (void) dseg_add_unique_address(cd, ex->catchtype.any);
357 /* initialize mcode variables */
359 cd->mcodeptr = cd->mcodebase;
360 cd->mcodeend = (s4 *) (cd->mcodebase + cd->mcodesize);
365 #if defined(ENABLE_THREADS)
366 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
367 if (m->flags & ACC_STATIC) {
368 gen_ACONST(cd, (java_objectheader *) m->class);
373 gen_ASTORE(cd, index2offset(m->maxlocals));
376 gen_MONITORENTER(cd);
381 gen_TRACECALL(cd, m);
385 /* walk through all basic blocks */
387 for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
389 bptr->mpc = (s4) (cd->mcodeptr - cd->mcodebase);
391 if (bptr->flags != BBDELETED) {
393 /* walk through all instructions */
399 for (iptr = bptr->iinstr; len > 0; len--, iptr++) {
400 if (iptr->line != currentline) {
401 dseg_addlinenumber(cd, iptr->line);
402 currentline = iptr->line;
405 MCODECHECK(64); /* an instruction usually needs < 64 words */
410 case ICMD_INLINE_START:
411 case ICMD_INLINE_END:
414 case ICMD_NOP: /* ... ==> ... */
417 case ICMD_CHECKNULL: /* ..., objectref ==> ..., objectref */
422 /* constant operations ************************************************/
424 case ICMD_ICONST: /* ... ==> ..., constant */
425 /* op1 = 0, val.i = constant */
427 /* optimize ICONST (2^x) .. IREM --> IREMPOW2 (const) */
429 if (len >= 2 && iptr[1].opc == ICMD_IREM) {
430 switch (iptr[0].sx.val.i) {
431 case 0x00000001: case 0x00000002: case 0x00000004: case 0x00000008:
432 case 0x00000010: case 0x00000020: case 0x00000040: case 0x00000080:
433 case 0x00000100: case 0x00000200: case 0x00000400: case 0x00000800:
434 case 0x00001000: case 0x00002000: case 0x00004000: case 0x00008000:
435 case 0x00010000: case 0x00020000: case 0x00040000: case 0x00080000:
436 case 0x00100000: case 0x00200000: case 0x00400000: case 0x00800000:
437 case 0x01000000: case 0x02000000: case 0x04000000: case 0x08000000:
438 case 0x10000000: case 0x20000000: case 0x40000000: case 0x80000000:
439 iptr[0].opc = ICMD_IREMPOW2;
441 iptr[1].opc = ICMD_NOP;
446 /* optimize ICONST (2^x) .. IDIV --> IDIVPOW2 (const) */
448 if (len >= 2 && iptr[1].opc == ICMD_IDIV) {
449 switch (iptr[0].sx.val.i) {
450 case 0x00000002: I( 1) case 0x00000004: I( 2) case 0x00000008: I( 3)
451 case 0x00000010: I( 4) case 0x00000020: I( 5) case 0x00000040: I( 6) case 0x00000080: I( 7)
452 case 0x00000100: I( 8) case 0x00000200: I( 9) case 0x00000400: I(10) case 0x00000800: I(11)
453 case 0x00001000: I(12) case 0x00002000: I(13) case 0x00004000: I(14) case 0x00008000: I(15)
454 case 0x00010000: I(16) case 0x00020000: I(17) case 0x00040000: I(18) case 0x00080000: I(19)
455 case 0x00100000: I(20) case 0x00200000: I(21) case 0x00400000: I(22) case 0x00800000: I(23)
456 case 0x01000000: I(24) case 0x02000000: I(25) case 0x04000000: I(26) case 0x08000000: I(27)
457 case 0x10000000: I(28) case 0x20000000: I(29) case 0x40000000: I(30) case 0x80000000: I(31)
458 default: goto dont_opt_IDIVPOW2;
460 iptr[0].opc = ICMD_IDIVPOW2;
461 iptr[1].opc = ICMD_NOP;
466 /* optimize ICONST .. IF_ICMPxx --> IFxx (const) */
469 switch (iptr[1].opc) {
470 case ICMD_IF_ICMPEQ: iptr[0].opc = ICMD_IFEQ; break;
471 case ICMD_IF_ICMPNE: iptr[0].opc = ICMD_IFNE; break;
472 case ICMD_IF_ICMPLT: iptr[0].opc = ICMD_IFLT; break;
473 case ICMD_IF_ICMPLE: iptr[0].opc = ICMD_IFLE; break;
474 case ICMD_IF_ICMPGT: iptr[0].opc = ICMD_IFGT; break;
475 case ICMD_IF_ICMPGE: iptr[0].opc = ICMD_IFGE; break;
476 default: goto dont_opt_IFxx;
478 iptr[0].dst.insindex = iptr[1].dst.insindex;
479 iptr[1].opc = ICMD_NOP;
484 gen_ICONST(cd, iptr->sx.val.i);
487 case ICMD_LCONST: /* ... ==> ..., constant */
488 /* op1 = 0, val.l = constant */
490 /* optimize LCONST (2^x) .. LREM --> LREMPOW2 (const) */
492 if (len >= 2 && iptr[1].opc == ICMD_LREM) {
493 switch (iptr[0].sx.val.l) {
494 case 0x00000001: case 0x00000002: case 0x00000004: case 0x00000008:
495 case 0x00000010: case 0x00000020: case 0x00000040: case 0x00000080:
496 case 0x00000100: case 0x00000200: case 0x00000400: case 0x00000800:
497 case 0x00001000: case 0x00002000: case 0x00004000: case 0x00008000:
498 case 0x00010000: case 0x00020000: case 0x00040000: case 0x00080000:
499 case 0x00100000: case 0x00200000: case 0x00400000: case 0x00800000:
500 case 0x01000000: case 0x02000000: case 0x04000000: case 0x08000000:
501 case 0x10000000: case 0x20000000: case 0x40000000: case 0x80000000:
502 iptr[0].opc = ICMD_LREMPOW2;
504 iptr[1].opc = ICMD_NOP;
509 /* optimize LCONST (2^x) .. LDIV --> LDIVPOW2 (const) */
511 if (len >= 2 && iptr[1].opc == ICMD_LDIV) {
512 switch (iptr[0].sx.val.l) {
513 case 0x00000002: I( 1) case 0x00000004: I( 2) case 0x00000008: I( 3)
514 case 0x00000010: I( 4) case 0x00000020: I( 5) case 0x00000040: I( 6) case 0x00000080: I( 7)
515 case 0x00000100: I( 8) case 0x00000200: I( 9) case 0x00000400: I(10) case 0x00000800: I(11)
516 case 0x00001000: I(12) case 0x00002000: I(13) case 0x00004000: I(14) case 0x00008000: I(15)
517 case 0x00010000: I(16) case 0x00020000: I(17) case 0x00040000: I(18) case 0x00080000: I(19)
518 case 0x00100000: I(20) case 0x00200000: I(21) case 0x00400000: I(22) case 0x00800000: I(23)
519 case 0x01000000: I(24) case 0x02000000: I(25) case 0x04000000: I(26) case 0x08000000: I(27)
520 case 0x10000000: I(28) case 0x20000000: I(29) case 0x40000000: I(30) case 0x80000000: I(31)
521 default: goto dont_opt_LDIVPOW2;
523 iptr[0].opc = ICMD_LDIVPOW2;
524 iptr[1].opc = ICMD_NOP;
529 /* optimize LCONST .. LCMP .. IFxx (0) --> IF_Lxx */
531 if (len >= 3 && iptr[1].opc == ICMD_LCMP && iptr[2].sx.val.i == 0) {
532 switch (iptr[2].opc) {
533 case ICMD_IFEQ: iptr[0].opc = ICMD_IF_LEQ; break;
534 case ICMD_IFNE: iptr[0].opc = ICMD_IF_LNE; break;
535 case ICMD_IFLT: iptr[0].opc = ICMD_IF_LLT; break;
536 case ICMD_IFLE: iptr[0].opc = ICMD_IF_LLE; break;
537 case ICMD_IFGT: iptr[0].opc = ICMD_IF_LGT; break;
538 case ICMD_IFGE: iptr[0].opc = ICMD_IF_LGE; break;
539 default: goto dont_opt_IF_Lxx;
541 iptr[0].dst.insindex = iptr[2].dst.insindex;
542 iptr[1].opc = ICMD_NOP;
543 iptr[2].opc = ICMD_NOP;
548 gen_LCONST(cd, iptr->sx.val.l);
551 case ICMD_FCONST: /* ... ==> ..., constant */
552 /* op1 = 0, val.f = constant */
556 vm_f2Cell(iptr->sx.val.f, fi);
561 case ICMD_DCONST: /* ... ==> ..., constant */
562 /* op1 = 0, val.d = constant */
564 gen_LCONST(cd, *(s8 *)&(iptr->sx.val.d));
567 case ICMD_ACONST: /* ... ==> ..., constant */
568 /* op1 = 0, val.a = constant */
570 if (INSTRUCTION_IS_UNRESOLVED(iptr))
571 gen_PATCHER_ACONST(cd, NULL, iptr->sx.val.c.ref);
573 gen_ACONST(cd, iptr->sx.val.anyptr);
577 /* load/store operations **********************************************/
579 case ICMD_ILOAD: /* ... ==> ..., content of local variable */
580 /* op1 = local variable */
582 gen_ILOAD(cd, index2offset(iptr->s1.varindex));
585 case ICMD_LLOAD: /* ... ==> ..., content of local variable */
586 /* op1 = local variable */
588 gen_LLOAD(cd, index2offset(iptr->s1.varindex));
591 case ICMD_ALOAD: /* ... ==> ..., content of local variable */
592 /* op1 = local variable */
594 gen_ALOAD(cd, index2offset(iptr->s1.varindex));
597 case ICMD_FLOAD: /* ... ==> ..., content of local variable */
598 /* op1 = local variable */
600 gen_ILOAD(cd, index2offset(iptr->s1.varindex));
603 case ICMD_DLOAD: /* ... ==> ..., content of local variable */
604 /* op1 = local variable */
606 gen_LLOAD(cd, index2offset(iptr->s1.varindex));
610 case ICMD_ISTORE: /* ..., value ==> ... */
611 /* op1 = local variable */
613 gen_ISTORE(cd, index2offset(iptr->dst.varindex));
616 case ICMD_LSTORE: /* ..., value ==> ... */
617 /* op1 = local variable */
619 gen_LSTORE(cd, index2offset(iptr->dst.varindex));
622 case ICMD_ASTORE: /* ..., value ==> ... */
623 /* op1 = local variable */
625 gen_ASTORE(cd, index2offset(iptr->dst.varindex));
629 case ICMD_FSTORE: /* ..., value ==> ... */
630 /* op1 = local variable */
632 gen_ISTORE(cd, index2offset(iptr->dst.varindex));
635 case ICMD_DSTORE: /* ..., value ==> ... */
636 /* op1 = local variable */
638 gen_LSTORE(cd, index2offset(iptr->dst.varindex));
642 /* pop/dup/swap operations ********************************************/
644 case ICMD_POP: /* ..., value ==> ... */
649 case ICMD_POP2: /* ..., value, value ==> ... */
654 case ICMD_DUP: /* ..., a ==> ..., a, a */
659 case ICMD_DUP_X1: /* ..., a, b ==> ..., b, a, b */
664 case ICMD_DUP_X2: /* ..., a, b, c ==> ..., c, a, b, c */
669 case ICMD_DUP2: /* ..., a, b ==> ..., a, b, a, b */
674 case ICMD_DUP2_X1: /* ..., a, b, c ==> ..., b, c, a, b, c */
679 case ICMD_DUP2_X2: /* ..., a, b, c, d ==> ..., c, d, a, b, c, d */
684 case ICMD_SWAP: /* ..., a, b ==> ..., b, a */
690 /* integer operations *************************************************/
692 case ICMD_INEG: /* ..., value ==> ..., - value */
697 case ICMD_LNEG: /* ..., value ==> ..., - value */
702 case ICMD_I2L: /* ..., value ==> ..., value */
707 case ICMD_L2I: /* ..., value ==> ..., value */
712 case ICMD_INT2BYTE: /* ..., value ==> ..., value */
717 case ICMD_INT2CHAR: /* ..., value ==> ..., value */
722 case ICMD_INT2SHORT: /* ..., value ==> ..., value */
728 case ICMD_IADD: /* ..., val1, val2 ==> ..., val1 + val2 */
733 case ICMD_LADD: /* ..., val1, val2 ==> ..., val1 + val2 */
738 case ICMD_ISUB: /* ..., val1, val2 ==> ..., val1 - val2 */
743 case ICMD_LSUB: /* ..., val1, val2 ==> ..., val1 - val2 */
748 case ICMD_IMUL: /* ..., val1, val2 ==> ..., val1 * val2 */
753 case ICMD_LMUL: /* ..., val1, val2 ==> ..., val1 * val2 */
758 case ICMD_IDIV: /* ..., val1, val2 ==> ..., val1 / val2 */
763 case ICMD_IREM: /* ..., val1, val2 ==> ..., val1 % val2 */
768 case ICMD_LDIV: /* ..., val1, val2 ==> ..., val1 / val2 */
773 case ICMD_LREM: /* ..., val1, val2 ==> ..., val1 % val2 */
778 case ICMD_IDIVPOW2: /* ..., value ==> ..., value << constant */
779 /* val.i = constant */
781 gen_IDIVPOW2(cd, iptr->sx.val.i);
784 case ICMD_IREMPOW2: /* ..., value ==> ..., value % constant */
785 /* val.i = constant */
787 gen_IREMPOW2(cd, iptr->sx.val.i);
790 case ICMD_LDIVPOW2: /* ..., value ==> ..., value << constant */
791 /* val.i = constant */
793 gen_LDIVPOW2(cd, iptr->sx.val.i);
796 case ICMD_LREMPOW2: /* ..., value ==> ..., value % constant */
797 /* val.l = constant */
799 gen_LREMPOW2(cd, iptr->sx.val.i);
802 case ICMD_ISHL: /* ..., val1, val2 ==> ..., val1 << val2 */
807 case ICMD_ISHR: /* ..., val1, val2 ==> ..., val1 >> val2 */
812 case ICMD_IUSHR: /* ..., val1, val2 ==> ..., val1 >>> val2 */
817 case ICMD_LSHL: /* ..., val1, val2 ==> ..., val1 << val2 */
822 case ICMD_LSHR: /* ..., val1, val2 ==> ..., val1 >> val2 */
827 case ICMD_LUSHR: /* ..., val1, val2 ==> ..., val1 >>> val2 */
832 case ICMD_IAND: /* ..., val1, val2 ==> ..., val1 & val2 */
837 case ICMD_LAND: /* ..., val1, val2 ==> ..., val1 & val2 */
842 case ICMD_IOR: /* ..., val1, val2 ==> ..., val1 | val2 */
847 case ICMD_LOR: /* ..., val1, val2 ==> ..., val1 | val2 */
852 case ICMD_IXOR: /* ..., val1, val2 ==> ..., val1 ^ val2 */
857 case ICMD_LXOR: /* ..., val1, val2 ==> ..., val1 ^ val2 */
863 case ICMD_LCMP: /* ..., val1, val2 ==> ..., val1 cmp val2 */
865 /* optimize LCMP .. IFxx (0) --> IF_LCMPxx */
867 if (len >= 2 && iptr[1].sx.val.i == 0) {
868 switch (iptr[1].opc) {
869 case ICMD_IFEQ: iptr[0].opc = ICMD_IF_LCMPEQ; break;
870 case ICMD_IFNE: iptr[0].opc = ICMD_IF_LCMPNE; break;
871 case ICMD_IFLT: iptr[0].opc = ICMD_IF_LCMPLT; break;
872 case ICMD_IFLE: iptr[0].opc = ICMD_IF_LCMPLE; break;
873 case ICMD_IFGT: iptr[0].opc = ICMD_IF_LCMPGT; break;
874 case ICMD_IFGE: iptr[0].opc = ICMD_IF_LCMPGE; break;
875 default: goto dont_opt_IF_LCMPxx;
877 iptr[0].dst.insindex = iptr[1].dst.insindex;
878 iptr[1].opc = ICMD_NOP;
887 case ICMD_IINC: /* ..., value ==> ..., value + constant */
888 /* op1 = variable, val.i = constant */
890 gen_IINC(cd, index2offset(iptr->s1.varindex), iptr->sx.val.i);
894 /* floating operations ************************************************/
896 case ICMD_FNEG: /* ..., value ==> ..., - value */
901 case ICMD_DNEG: /* ..., value ==> ..., - value */
906 case ICMD_FADD: /* ..., val1, val2 ==> ..., val1 + val2 */
911 case ICMD_DADD: /* ..., val1, val2 ==> ..., val1 + val2 */
916 case ICMD_FSUB: /* ..., val1, val2 ==> ..., val1 - val2 */
921 case ICMD_DSUB: /* ..., val1, val2 ==> ..., val1 - val2 */
926 case ICMD_FMUL: /* ..., val1, val2 ==> ..., val1 * val2 */
931 case ICMD_DMUL: /* ..., val1, val2 ==> ..., val1 *** val2 */
936 case ICMD_FDIV: /* ..., val1, val2 ==> ..., val1 / val2 */
941 case ICMD_DDIV: /* ..., val1, val2 ==> ..., val1 / val2 */
946 case ICMD_FREM: /* ..., val1, val2 ==> ..., val1 % val2 */
951 case ICMD_DREM: /* ..., val1, val2 ==> ..., val1 % val2 */
956 case ICMD_I2F: /* ..., value ==> ..., (float) value */
961 case ICMD_L2F: /* ..., value ==> ..., (float) value */
966 case ICMD_I2D: /* ..., value ==> ..., (double) value */
971 case ICMD_L2D: /* ..., value ==> ..., (double) value */
976 case ICMD_F2I: /* ..., value ==> ..., (int) value */
981 case ICMD_D2I: /* ..., value ==> ..., (int) value */
986 case ICMD_F2L: /* ..., value ==> ..., (long) value */
991 case ICMD_D2L: /* ..., value ==> ..., (long) value */
996 case ICMD_F2D: /* ..., value ==> ..., (double) value */
1001 case ICMD_D2F: /* ..., value ==> ..., (float) value */
1006 case ICMD_FCMPL: /* ..., val1, val2 ==> ..., val1 fcmpl val2 */
1011 case ICMD_DCMPL: /* ..., val1, val2 ==> ..., val1 fcmpl val2 */
1016 case ICMD_FCMPG: /* ..., val1, val2 ==> ..., val1 fcmpg val2 */
1021 case ICMD_DCMPG: /* ..., val1, val2 ==> ..., val1 fcmpg val2 */
1027 /* memory operations **************************************************/
1029 case ICMD_ARRAYLENGTH: /* ..., arrayref ==> ..., length */
1031 gen_ARRAYLENGTH(cd);
1034 case ICMD_BALOAD: /* ..., arrayref, index ==> ..., value */
1039 case ICMD_CALOAD: /* ..., arrayref, index ==> ..., value */
1044 case ICMD_SALOAD: /* ..., arrayref, index ==> ..., value */
1049 case ICMD_IALOAD: /* ..., arrayref, index ==> ..., value */
1054 case ICMD_FALOAD: /* ..., arrayref, index ==> ..., value */
1059 case ICMD_LALOAD: /* ..., arrayref, index ==> ..., value */
1060 case ICMD_DALOAD: /* ..., arrayref, index ==> ..., value */
1065 case ICMD_AALOAD: /* ..., arrayref, index ==> ..., value */
1071 case ICMD_BASTORE: /* ..., arrayref, index, value ==> ... */
1076 case ICMD_CASTORE: /* ..., arrayref, index, value ==> ... */
1077 case ICMD_SASTORE: /* ..., arrayref, index, value ==> ... */
1082 case ICMD_IASTORE: /* ..., arrayref, index, value ==> ... */
1087 case ICMD_FASTORE: /* ..., arrayref, index, value ==> ... */
1092 case ICMD_LASTORE: /* ..., arrayref, index, value ==> ... */
1093 case ICMD_DASTORE: /* ..., arrayref, index, value ==> ... */
1098 case ICMD_AASTORE: /* ..., arrayref, index, value ==> ... */
1104 case ICMD_GETSTATIC: /* ... ==> ..., value */
1105 /* op1 = type, val.a = field address */
1107 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1109 uf = iptr->sx.s23.s3.uf;
1110 fieldtype = uf->fieldref->parseddesc.fd->type;
1113 fi = iptr->sx.s23.s3.fmiref->p.field;
1115 fieldtype = fi->type;
1118 switch (fieldtype) {
1121 gen_PATCHER_GETSTATIC_INT(cd, 0, uf);
1122 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1123 gen_PATCHER_GETSTATIC_CLINIT_INT(cd, 0, fi);
1125 gen_GETSTATIC_INT(cd, (u1 *) &(fi->value.i), fi);
1129 gen_PATCHER_GETSTATIC_FLOAT(cd, 0, uf);
1130 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1131 gen_PATCHER_GETSTATIC_CLINIT_FLOAT(cd, 0, fi);
1133 gen_GETSTATIC_FLOAT(cd, (u1 *) &(fi->value.i), fi);
1138 gen_PATCHER_GETSTATIC_LONG(cd, 0, uf);
1139 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1140 gen_PATCHER_GETSTATIC_CLINIT_LONG(cd, 0, fi);
1142 gen_GETSTATIC_LONG(cd, (u1 *) &(fi->value.l), fi);
1146 gen_PATCHER_GETSTATIC_CELL(cd, 0, uf);
1147 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1148 gen_PATCHER_GETSTATIC_CLINIT_CELL(cd, 0, fi);
1150 gen_GETSTATIC_CELL(cd, (u1 *) &(fi->value.a), fi);
1155 case ICMD_PUTSTATIC: /* ..., value ==> ... */
1156 /* op1 = type, val.a = field address */
1158 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1160 uf = iptr->sx.s23.s3.uf;
1161 fieldtype = uf->fieldref->parseddesc.fd->type;
1164 fi = iptr->sx.s23.s3.fmiref->p.field;
1166 fieldtype = fi->type;
1169 switch (fieldtype) {
1172 gen_PATCHER_PUTSTATIC_INT(cd, 0, uf);
1173 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1174 gen_PATCHER_PUTSTATIC_CLINIT_INT(cd, 0, fi);
1176 gen_PUTSTATIC_INT(cd, (u1 *) &(fi->value.i), fi);
1180 gen_PATCHER_PUTSTATIC_FLOAT(cd, 0, uf);
1181 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1182 gen_PATCHER_PUTSTATIC_CLINIT_FLOAT(cd, 0, fi);
1184 gen_PUTSTATIC_FLOAT(cd, (u1 *) &(fi->value.i), fi);
1189 gen_PATCHER_PUTSTATIC_LONG(cd, 0, uf);
1190 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1191 gen_PATCHER_PUTSTATIC_CLINIT_LONG(cd, 0, fi);
1193 gen_PUTSTATIC_LONG(cd, (u1 *) &(fi->value.l), fi);
1197 gen_PATCHER_PUTSTATIC_CELL(cd, 0, uf);
1198 else if (!CLASS_IS_OR_ALMOST_INITIALIZED(fi->class))
1199 gen_PATCHER_PUTSTATIC_CLINIT_CELL(cd, 0, fi);
1201 gen_PUTSTATIC_CELL(cd, (u1 *) &(fi->value.a), fi);
1207 case ICMD_GETFIELD: /* ... ==> ..., value */
1208 /* op1 = type, val.a = field address */
1210 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1212 uf = iptr->sx.s23.s3.uf;
1213 fieldtype = uf->fieldref->parseddesc.fd->type;
1216 fi = iptr->sx.s23.s3.fmiref->p.field;
1218 fieldtype = fi->type;
1221 switch (fieldtype) {
1224 gen_PATCHER_GETFIELD_INT(cd, 0, uf);
1226 gen_GETFIELD_INT(cd, fi->offset, fi);
1230 gen_PATCHER_GETFIELD_FLOAT(cd, 0, uf);
1232 gen_GETFIELD_FLOAT(cd, fi->offset, fi);
1237 gen_PATCHER_GETFIELD_LONG(cd, 0, uf);
1239 gen_GETFIELD_LONG(cd, fi->offset, fi);
1243 gen_PATCHER_GETFIELD_CELL(cd, 0, uf);
1245 gen_GETFIELD_CELL(cd, fi->offset, fi);
1250 case ICMD_PUTFIELD: /* ..., objectref, value ==> ... */
1251 /* op1 = type, val.a = field address */
1253 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1255 uf = iptr->sx.s23.s3.uf;
1256 fieldtype = uf->fieldref->parseddesc.fd->type;
1259 fi = iptr->sx.s23.s3.fmiref->p.field;
1261 fieldtype = fi->type;
1264 switch (fieldtype) {
1267 gen_PATCHER_PUTFIELD_INT(cd, 0, uf);
1269 gen_PUTFIELD_INT(cd, fi->offset, fi);
1273 gen_PATCHER_PUTFIELD_FLOAT(cd, 0, uf);
1275 gen_PUTFIELD_FLOAT(cd, fi->offset, fi);
1280 gen_PATCHER_PUTFIELD_LONG(cd, 0, uf);
1282 gen_PUTFIELD_LONG(cd, fi->offset, fi);
1286 gen_PATCHER_PUTFIELD_CELL(cd, 0, uf);
1288 gen_PUTFIELD_CELL(cd, fi->offset, fi);
1294 /* branch operations **************************************************/
1296 case ICMD_ATHROW: /* ..., objectref ==> ... (, objectref) */
1301 case ICMD_GOTO: /* ... ==> ... */
1302 /* op1 = target JavaVM pc */
1306 case ICMD_JSR: /* ... ==> ... */
1307 /* op1 = target JavaVM pc */
1310 codegen_addreference(cd, BLOCK_OF(iptr->sx.s23.s3.jsrtarget.insindex));
1313 case ICMD_RET: /* ... ==> ... */
1314 /* op1 = local variable */
1316 gen_RET(cd, index2offset(iptr->s1.varindex));
1319 case ICMD_IFNULL: /* ..., value ==> ... */
1320 /* op1 = target JavaVM pc */
1325 case ICMD_IFNONNULL: /* ..., value ==> ... */
1326 /* op1 = target JavaVM pc */
1328 gen_branch(IFNONNULL);
1331 case ICMD_IFEQ: /* ..., value ==> ... */
1332 /* op1 = target JavaVM pc, val.i = constant */
1334 if (iptr->sx.val.i == 0) {
1337 gen_ICONST(cd, iptr->sx.val.i);
1338 gen_branch(IF_ICMPEQ);
1342 case ICMD_IFLT: /* ..., value ==> ... */
1343 /* op1 = target JavaVM pc, val.i = constant */
1345 if (iptr->sx.val.i == 0) {
1348 gen_ICONST(cd, iptr->sx.val.i);
1349 gen_branch(IF_ICMPLT);
1353 case ICMD_IFLE: /* ..., value ==> ... */
1354 /* op1 = target JavaVM pc, val.i = constant */
1356 if (iptr->sx.val.i == 0) {
1359 gen_ICONST(cd, iptr->sx.val.i);
1360 gen_branch(IF_ICMPLE);
1364 case ICMD_IFNE: /* ..., value ==> ... */
1365 /* op1 = target JavaVM pc, val.i = constant */
1367 if (iptr->sx.val.i == 0) {
1370 gen_ICONST(cd, iptr->sx.val.i);
1371 gen_branch(IF_ICMPNE);
1375 case ICMD_IFGT: /* ..., value ==> ... */
1376 /* op1 = target JavaVM pc, val.i = constant */
1378 if (iptr->sx.val.i == 0) {
1381 gen_ICONST(cd, iptr->sx.val.i);
1382 gen_branch(IF_ICMPGT);
1386 case ICMD_IFGE: /* ..., value ==> ... */
1387 /* op1 = target JavaVM pc, val.i = constant */
1389 if (iptr->sx.val.i == 0) {
1392 gen_ICONST(cd, iptr->sx.val.i);
1393 gen_branch(IF_ICMPGE);
1398 case ICMD_IF_LEQ: /* ..., value ==> ... */
1399 /* op1 = target JavaVM pc, val.l = constant */
1401 gen_LCONST(cd, iptr->sx.val.l);
1402 gen_branch(IF_LCMPEQ);
1405 case ICMD_IF_LLT: /* ..., value ==> ... */
1406 /* op1 = target JavaVM pc, val.l = constant */
1408 gen_LCONST(cd, iptr->sx.val.l);
1409 gen_branch(IF_LCMPLT);
1412 case ICMD_IF_LLE: /* ..., value ==> ... */
1413 /* op1 = target JavaVM pc, val.l = constant */
1415 gen_LCONST(cd, iptr->sx.val.l);
1416 gen_branch(IF_LCMPLE);
1419 case ICMD_IF_LNE: /* ..., value ==> ... */
1420 /* op1 = target JavaVM pc, val.l = constant */
1422 gen_LCONST(cd, iptr->sx.val.l);
1423 gen_branch(IF_LCMPNE);
1426 case ICMD_IF_LGT: /* ..., value ==> ... */
1427 /* op1 = target JavaVM pc, val.l = constant */
1429 gen_LCONST(cd, iptr->sx.val.l);
1430 gen_branch(IF_LCMPGT);
1433 case ICMD_IF_LGE: /* ..., value ==> ... */
1434 /* op1 = target JavaVM pc, val.l = constant */
1436 gen_LCONST(cd, iptr->sx.val.l);
1437 gen_branch(IF_LCMPGE);
1440 case ICMD_IF_LCMPEQ: /* ..., value, value ==> ... */
1441 /* op1 = target JavaVM pc */
1443 gen_branch(IF_LCMPEQ);
1446 case ICMD_IF_LCMPNE: /* ..., value, value ==> ... */
1447 /* op1 = target JavaVM pc */
1449 gen_branch(IF_LCMPNE);
1452 case ICMD_IF_LCMPLT: /* ..., value, value ==> ... */
1453 /* op1 = target JavaVM pc */
1455 gen_branch(IF_LCMPLT);
1458 case ICMD_IF_LCMPGT: /* ..., value, value ==> ... */
1459 /* op1 = target JavaVM pc */
1461 gen_branch(IF_LCMPGT);
1464 case ICMD_IF_LCMPLE: /* ..., value, value ==> ... */
1465 /* op1 = target JavaVM pc */
1467 gen_branch(IF_LCMPLE);
1470 case ICMD_IF_LCMPGE: /* ..., value, value ==> ... */
1471 /* op1 = target JavaVM pc */
1473 gen_branch(IF_LCMPGE);
1477 case ICMD_IF_ICMPEQ: /* ..., value, value ==> ... */
1478 /* op1 = target JavaVM pc */
1480 gen_branch(IF_ICMPEQ);
1483 case ICMD_IF_ACMPEQ: /* ..., value, value ==> ... */
1484 /* op1 = target JavaVM pc */
1486 gen_branch(IF_ACMPEQ);
1489 case ICMD_IF_ICMPNE: /* ..., value, value ==> ... */
1490 /* op1 = target JavaVM pc */
1492 gen_branch(IF_ICMPNE);
1495 case ICMD_IF_ACMPNE: /* ..., value, value ==> ... */
1496 /* op1 = target JavaVM pc */
1498 gen_branch(IF_ACMPNE);
1501 case ICMD_IF_ICMPLT: /* ..., value, value ==> ... */
1502 /* op1 = target JavaVM pc */
1504 gen_branch(IF_ICMPLT);
1507 case ICMD_IF_ICMPGT: /* ..., value, value ==> ... */
1508 /* op1 = target JavaVM pc */
1510 gen_branch(IF_ICMPGT);
1513 case ICMD_IF_ICMPLE: /* ..., value, value ==> ... */
1514 /* op1 = target JavaVM pc */
1516 gen_branch(IF_ICMPLE);
1519 case ICMD_IF_ICMPGE: /* ..., value, value ==> ... */
1520 /* op1 = target JavaVM pc */
1522 gen_branch(IF_ICMPGE);
1526 case ICMD_ARETURN: /* ..., retvalue ==> ... */
1527 case ICMD_IRETURN: /* ..., retvalue ==> ... */
1528 case ICMD_FRETURN: /* ..., retvalue ==> ... */
1530 #if defined(ENABLE_THREADS)
1531 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
1532 if (m->flags & ACC_STATIC) {
1533 gen_ACONST(cd, (java_objectheader *) m->class);
1535 gen_ALOAD(cd, index2offset(m->maxlocals));
1537 gen_MONITOREXIT(cd);
1540 if (opt_verbosecall)
1541 gen_TRACERETURN(cd, m);
1543 gen_IRETURN(cd, index2offset(cd->stackframesize));
1546 case ICMD_LRETURN: /* ..., retvalue ==> ... */
1547 case ICMD_DRETURN: /* ..., retvalue ==> ... */
1549 #if defined(ENABLE_THREADS)
1550 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
1551 if (m->flags & ACC_STATIC) {
1552 gen_ACONST(cd, (java_objectheader *) m->class);
1554 gen_ALOAD(cd, index2offset(m->maxlocals));
1556 gen_MONITOREXIT(cd);
1559 if (opt_verbosecall)
1560 gen_TRACELRETURN(cd, m);
1562 gen_LRETURN(cd, index2offset(cd->stackframesize));
1565 case ICMD_RETURN: /* ... ==> ... */
1567 #if defined(ENABLE_THREADS)
1568 if (checksync && (m->flags & ACC_SYNCHRONIZED)) {
1569 if (m->flags & ACC_STATIC) {
1570 gen_ACONST(cd, (java_objectheader *) m->class);
1572 gen_ALOAD(cd, index2offset(m->maxlocals));
1574 gen_MONITOREXIT(cd);
1577 if (opt_verbosecall)
1578 gen_TRACERETURN(cd, m);
1580 gen_RETURN(cd, index2offset(cd->stackframesize));
1584 case ICMD_TABLESWITCH: /* ..., index ==> ... */
1587 branch_target_t *table;
1589 table = iptr->dst.table;
1591 l = iptr->sx.s23.s2.tablelow;
1592 i = iptr->sx.s23.s3.tablehigh;
1596 /* arguments: low, range, datasegment address, table
1597 offset in datasegment, default target */
1599 gen_TABLESWITCH(cd, l, i, NULL, 0, NULL);
1601 /* actually -3 cells offset */
1603 cd->mcodeptr = (u1 *) cd->mcodeptr - 2 * sizeof(Inst);
1605 cd->mcodeptr = (u1 *) cd->mcodeptr + 2 * sizeof(Inst);
1607 codegen_addreference(cd, BLOCK_OF(table[0].insindex));
1609 /* build jump table top down and use address of lowest entry */
1614 dseg_add_target(cd, BLOCK_OF(table->insindex));
1619 /* length of dataseg after last dseg_add_target is used by load */
1620 ((ptrint *)(cd->mcodeptr))[-2] = (ptrint) -(cd->dseglen);
1624 case ICMD_LOOKUPSWITCH: /* ..., key ==> ... */
1627 lookup_target_t *lookup;
1629 lookup = iptr->dst.lookup;
1631 i = iptr->sx.s23.s2.lookupcount;
1633 /* arguments: count, datasegment address, table offset in */
1634 /* datasegment, default target */
1635 gen_LOOKUPSWITCH(cd, i, NULL, 0, NULL);
1637 /* actually -3 cells offset */
1639 cd->mcodeptr = (u1 *) cd->mcodeptr - 2 * sizeof(Inst);
1641 cd->mcodeptr = (u1 *) cd->mcodeptr + 2 * sizeof(Inst);
1643 /* build jump table top down and use address of lowest entry */
1646 dseg_add_target(cd, BLOCK_OF(lookup->target.insindex));
1647 dseg_add_unique_address(cd, lookup->value);
1651 codegen_addreference(cd, BLOCK_OF(iptr->sx.s23.s3.lookupdefault.insindex));
1654 /* length of dataseg after last dseg_add_target is used by load */
1655 ((ptrint *)(cd->mcodeptr))[-2] = (ptrint) -(cd->dseglen);
1659 case ICMD_BUILTIN: /* ..., arg1, arg2, arg3 ==> ... */
1660 /* op1 = arg count val.a = builtintable entry */
1661 bte = iptr->sx.s23.s3.bte;
1663 for (i = 0; i < sizeof(builtin_gen_table)/sizeof(builtin_gen); i++) {
1664 builtin_gen *bg = &builtin_gen_table[i];
1665 if (bg->builtin == bte->fp) {
1667 goto gen_builtin_end;
1676 case ICMD_INVOKESTATIC: /* ..., [arg1, [arg2 ...]] ==> ... */
1677 /* op1 = arg count, val.a = method pointer */
1679 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1680 um = iptr->sx.s23.s3.um;
1681 md = um->methodref->parseddesc.md;
1682 gen_PATCHER_INVOKESTATIC(cd, 0, md->paramslots, um);
1685 lm = iptr->sx.s23.s3.fmiref->p.method;
1686 md = lm->parseddesc;
1687 gen_INVOKESTATIC(cd, (Inst **) lm->stubroutine, md->paramslots, lm);
1691 case ICMD_INVOKESPECIAL:/* ..., objectref, [arg1, [arg2 ...]] ==> ... */
1693 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1694 um = iptr->sx.s23.s3.um;
1695 md = um->methodref->parseddesc.md;
1696 gen_PATCHER_INVOKESPECIAL(cd, 0, md->paramslots, um);
1699 lm = iptr->sx.s23.s3.fmiref->p.method;
1700 md = lm->parseddesc;
1701 gen_INVOKESPECIAL(cd, (Inst **) lm->stubroutine, md->paramslots, lm);
1705 case ICMD_INVOKEVIRTUAL:/* op1 = arg count, val.a = method pointer */
1707 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1708 um = iptr->sx.s23.s3.um;
1709 md = um->methodref->parseddesc.md;
1710 gen_PATCHER_INVOKEVIRTUAL(cd, 0, md->paramslots, um);
1713 lm = iptr->sx.s23.s3.fmiref->p.method;
1714 md = lm->parseddesc;
1716 s1 = OFFSET(vftbl_t, table[0]) +
1717 sizeof(methodptr) * lm->vftblindex;
1719 gen_INVOKEVIRTUAL(cd, s1, md->paramslots, lm);
1723 case ICMD_INVOKEINTERFACE:/* op1 = arg count, val.a = method pointer */
1725 if (INSTRUCTION_IS_UNRESOLVED(iptr)) {
1726 um = iptr->sx.s23.s3.um;
1727 md = um->methodref->parseddesc.md;
1728 gen_PATCHER_INVOKEINTERFACE(cd, 0, 0, md->paramslots, um);
1731 lm = iptr->sx.s23.s3.fmiref->p.method;
1732 md = lm->parseddesc;
1734 s1 = OFFSET(vftbl_t, interfacetable[0]) -
1735 sizeof(methodptr*) * lm->class->index;
1737 s2 = sizeof(methodptr) * (lm - lm->class->methods);
1739 gen_INVOKEINTERFACE(cd, s1, s2, md->paramslots, lm);
1744 case ICMD_CHECKCAST: /* ..., objectref ==> ..., objectref */
1745 /* op1: 0 == array, 1 == class */
1746 /* val.a: (classinfo *) superclass */
1748 if (!(iptr->flags.bits & INS_FLAG_ARRAY)) {
1749 if (INSTRUCTION_IS_UNRESOLVED(iptr))
1750 gen_PATCHER_CHECKCAST(cd, NULL, iptr->sx.s23.s3.c.ref);
1752 gen_CHECKCAST(cd, iptr->sx.s23.s3.c.cls, NULL);
1755 if (INSTRUCTION_IS_UNRESOLVED(iptr))
1756 gen_PATCHER_ARRAYCHECKCAST(cd, NULL, iptr->sx.s23.s3.c.ref);
1758 gen_ARRAYCHECKCAST(cd, iptr->sx.s23.s3.c.cls, NULL);
1762 case ICMD_INSTANCEOF: /* ..., objectref ==> ..., intresult */
1763 /* op1: 0 == array, 1 == class */
1764 /* val.a: (classinfo *) superclass */
1766 if (INSTRUCTION_IS_UNRESOLVED(iptr))
1767 gen_PATCHER_INSTANCEOF(cd, NULL, iptr->sx.s23.s3.c.ref);
1769 gen_INSTANCEOF(cd, iptr->sx.s23.s3.c.cls, iptr->sx.s23.s3.c.ref);
1772 case ICMD_MULTIANEWARRAY:/* ..., cnt1, [cnt2, ...] ==> ..., arrayref */
1773 /* op1 = dimension, val.a = class */
1775 if (INSTRUCTION_IS_UNRESOLVED(iptr))
1776 gen_PATCHER_MULTIANEWARRAY(cd, NULL, iptr->s1.argcount, iptr->sx.s23.s3.c.ref);
1778 gen_MULTIANEWARRAY(cd, iptr->sx.s23.s3.c.cls, iptr->s1.argcount, NULL);
1782 exceptions_throw_internalerror("Unknown ICMD %d during code generation",
1787 } /* for instruction */
1791 } /* if (bptr->flags != BBDELETED) */
1792 } /* for basic block */
1794 dseg_createlinenumbertable(cd);
1799 vm_block_insert(jd->code->mcode + jd->code->mcodelength);
1802 /* branch resolving (walk through all basic blocks) */
1804 for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
1807 for (brefs = bptr->branchrefs; brefs != NULL; brefs = brefs->next) {
1808 gen_resolveanybranch(((u1*) jd->code->entrypoint) + brefs->branchpos,
1809 ((u1 *)jd->code->entrypoint) + bptr->mpc);
1813 /* everything's ok */
1819 /* createcompilerstub **********************************************************
1821 Creates a stub routine which calls the compiler.
1827 +-------------+ <-- stub
1830 | framesize | (in ptrint units, does not include return address)
1837 codeptr points either to TRANSLATE or to the translated threaded code
1839 all methods are called indirectly through methodptr
1841 *******************************************************************************/
1843 #define COMPILERSTUB_DATASIZE 2
1844 #define COMPILERSTUB_CODESIZE 4
1846 #define COMPILERSTUB_SIZE COMPILERSTUB_DATASIZE + COMPILERSTUB_CODESIZE
1849 u1 *intrp_createcompilerstub(methodinfo *m)
1857 s = CNEW(Inst, COMPILERSTUB_SIZE);
1859 /* set data pointer and code pointer */
1862 s = s + COMPILERSTUB_DATASIZE;
1864 /* The codeinfo pointer is actually a pointer to the
1865 methodinfo. This fakes a codeinfo structure. */
1868 d[1] = (Inst *) &d[0]; /* fake code->m */
1870 /* mark start of dump memory area */
1872 dumpsize = dump_size();
1874 cd = DNEW(codegendata);
1875 cd->mcodeptr = (u1 *) s;
1876 cd->lastinstwithoutdispatch = ~0;
1877 cd->superstarts = NULL;
1879 genarg_ainst(cd, s + 2);
1881 if (m->flags & ACC_NATIVE) {
1882 stackframesize = m->parseddesc->paramslots;
1885 stackframesize = m->maxlocals;
1887 #if defined(ENABLE_THREADS)
1888 if (checksync && (m->flags & ACC_SYNCHRONIZED))
1889 stackframesize += 1;
1893 genarg_i(cd, stackframesize);
1896 gen_TRANSLATE(cd, m);
1900 vm_block_insert(cd->mcodeptr);
1903 #if defined(ENABLE_STATISTICS)
1905 count_cstub_len += COMPILERSTUB_SIZE;
1908 /* release dump area */
1910 dump_release(dumpsize);
1916 #if defined(WITH_FFI)
1917 static ffi_type *cacaotype2ffitype(s4 cacaotype)
1919 switch (cacaotype) {
1921 #if SIZEOF_VOID_P == 8
1922 return &ffi_type_sint64;
1924 return &ffi_type_sint32;
1927 return &ffi_type_sint64;
1929 return &ffi_type_float;
1931 return &ffi_type_double;
1933 return &ffi_type_pointer;
1935 return &ffi_type_void;
1955 #if defined(WITH_FFI)
1956 static ffi_cif *createnativecif(methodinfo *m, methoddesc *nmd)
1958 methoddesc *md = m->parseddesc;
1959 ffi_cif *pcif = NEW(ffi_cif);
1960 ffi_type **types = MNEW(ffi_type *, nmd->paramcount);
1961 ffi_type **ptypes = types;
1964 /* pass env pointer */
1966 *ptypes++ = &ffi_type_pointer;
1968 /* for static methods, pass class pointer */
1970 if (m->flags & ACC_STATIC)
1971 *ptypes++ = &ffi_type_pointer;
1973 /* pass parameter to native function */
1975 for (i = 0; i < md->paramcount; i++)
1976 *ptypes++ = cacaotype2ffitype(md->paramtypes[i].type);
1978 assert(ptypes - types == nmd->paramcount);
1980 if (ffi_prep_cif(pcif, FFI_DEFAULT_ABI, nmd->paramcount, cacaotype2ffitype(md->returntype.type), types) != FFI_OK)
1988 u1 *intrp_createnativestub(functionptr f, jitdata *jd, methoddesc *nmd)
1994 #if defined(WITH_FFI)
2001 /* get required compiler data */
2008 /* determine stackframe size (in units of ptrint) */
2010 stackframesize = nmd->paramslots;
2012 /* create method header */
2014 (void) dseg_add_unique_address(cd, code); /* CodeinfoPointer */
2015 (void) dseg_add_unique_s4(cd, stackframesize * SIZEOF_VOID_P); /*FrameSize*/
2016 (void) dseg_add_unique_s4(cd, 0); /* IsSync */
2017 (void) dseg_add_unique_s4(cd, 0); /* IsLeaf */
2018 (void) dseg_add_unique_s4(cd, 0); /* IntSave */
2019 (void) dseg_add_unique_s4(cd, 0); /* FltSave */
2020 dseg_addlinenumbertablesize(cd);
2021 (void) dseg_add_unique_s4(cd, 0); /* ExTableSize */
2023 #if defined(WITH_FFI)
2024 /* prepare ffi cif structure */
2026 cif = createnativecif(m, nmd);
2033 if (opt_verbosecall)
2034 gen_TRACECALL(cd, m);
2037 gen_PATCHER_NATIVECALL(cd, m, f, (u1 *)cif);
2039 if (opt_verbosecall)
2040 gen_TRACENATIVECALL(cd, m, f, (u1 *)cif);
2042 gen_NATIVECALL(cd, m, f, (u1 *)cif);
2050 vm_block_insert(jd->code->mcode + jd->code->mcodelength);
2053 return jd->code->entrypoint;
2057 /* call jni function */
2058 Cell *nativecall(functionptr f, methodinfo *m, Cell *sp, Inst *ra, Cell *fp, u1 *addrcif)
2060 #if defined(WITH_FFCALL)
2074 switch (md->returntype.type) {
2076 endsp = sp - 1 + md->paramslots;
2077 av_start_long(alist, f, endsp);
2080 endsp = sp - 2 + md->paramslots;
2081 av_start_longlong(alist, f, endsp);
2084 endsp = sp - 1 + md->paramslots;
2085 av_start_float(alist, f, endsp);
2088 endsp = sp - 2 + md->paramslots;
2089 av_start_double(alist, f, endsp);
2092 endsp = sp - 1 + md->paramslots;
2093 av_start_ptr(alist, f, void *, endsp);
2096 endsp = sp + md->paramslots;
2097 av_start_void(alist, f);
2103 av_ptr(alist, _Jv_JNIEnv *, _Jv_env);
2105 if (m->flags & ACC_STATIC)
2106 av_ptr(alist, classinfo *, m->class);
2108 for (i = 0, p = sp + md->paramslots; i < md->paramcount; i++) {
2109 switch (md->paramtypes[i].type) {
2116 av_longlong(alist, *(s8 *)p);
2120 av_float(alist, *((float *) p));
2124 av_double(alist, *(double *) p);
2128 av_ptr(alist, void *, *(void **) p);
2137 /* create stackframe info structure */
2139 codegen_start_native_call(((u1 *) &s) + sizeof(s), m->code->entrypoint,
2140 (u1 *) fp, (u1 *) ra);
2144 *exceptionptr = codegen_finish_native_call(((u1 *) &s) + sizeof(s));
2149 #elif defined(WITH_FFI)
2150 methoddesc *md = m->parseddesc;
2152 void *values[md->paramcount + 2];
2153 void **pvalues = values;
2164 pcif = (ffi_cif *) addrcif;
2166 /* pass env pointer */
2168 penv = (_Jv_JNIEnv *) _Jv_env;
2171 /* for static methods, pass class pointer */
2173 if (m->flags & ACC_STATIC)
2174 *pvalues++ = &m->class;
2176 /* pass parameter to native function */
2178 for (i = 0, p = sp + md->paramslots; i < md->paramcount; i++) {
2179 if (IS_2_WORD_TYPE(md->paramtypes[i].type))
2187 /* calculate position of return value */
2189 if (md->returntype.type == TYPE_VOID)
2190 endsp = sp + md->paramslots;
2192 endsp = sp - (IS_2_WORD_TYPE(md->returntype.type) ? 2 : 1) + md->paramslots;
2196 /* create stackframe info structure */
2198 codegen_start_native_call(((u1 *) &s) + sizeof(s), m->code->entrypoint,
2199 (u1 *) fp, (u1 *) ra);
2201 ffi_call(pcif, FFI_FN(f), endsp, values);
2203 *exceptionptr = codegen_finish_native_call(((u1 *) &s) + sizeof(s));
2212 u1 *createcalljavafunction(methodinfo *m)
2222 /* mark dump memory */
2224 dumpsize = dump_size();
2226 /* allocate memory */
2230 tmpm = DNEW(methodinfo);
2231 cd = DNEW(codegendata);
2232 rd = DNEW(registerdata);
2239 /* Allocate codeinfo memory from the heap as we need to keep them. */
2241 jd->code = code_codeinfo_new(tmpm); /* XXX check allocation */
2243 /* setup code generation stuff */
2245 MSET(tmpm, 0, u1, sizeof(methodinfo));
2251 /* create method header */
2253 (void) dseg_add_unique_address(cd, NULL); /* CodeinfoPointer */
2254 (void) dseg_add_unique_s4(cd, md->paramslots * SIZEOF_VOID_P);/* FrameSize*/
2255 (void) dseg_add_unique_s4(cd, 0); /* IsSync */
2256 (void) dseg_add_unique_s4(cd, 0); /* IsLeaf */
2257 (void) dseg_add_unique_s4(cd, 0); /* IntSave */
2258 (void) dseg_add_unique_s4(cd, 0); /* FltSave */
2259 dseg_addlinenumbertablesize(cd);
2260 (void) dseg_add_unique_s4(cd, 0); /* ExTableSize */
2266 gen_INVOKESTATIC(cd, (Inst **)m->stubroutine, md->paramslots, 0);
2274 vm_block_insert(jd->code->mcode + jd->code->mcodelength);
2276 entrypoint = jd->code->entrypoint;
2278 /* release memory */
2280 dump_release(dumpsize);
2287 * These are local overrides for various environment variables in Emacs.
2288 * Please do not remove this and leave it at the end of the file, where
2289 * Emacs will automagically detect them.
2290 * ---------------------------------------------------------------------
2293 * indent-tabs-mode: t
2297 * vim:noexpandtab:sw=4:ts=4: