1 /* src/vm/jit/codegen-common.c - architecture independent code generator stuff
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 All functions assume the following code area / data area layout:
29 | code area | code area grows to higher addresses
31 +-----------+ <-- start of procedure
33 | data area | data area grows to lower addresses
37 The functions first write into a temporary code/data area allocated by
38 "codegen_init". "codegen_finish" copies the code and data area into permanent
39 memory. All functions writing values into the data area return the offset
40 relative the begin of the code area (start of procedure).
42 $Id: codegen-common.c 7343 2007-02-13 02:36:29Z ajordan $
54 #if defined(ENABLE_JIT)
55 /* this is required PATCHER_CALL_SIZE */
60 /* this is required for REG_SPLIT */
64 #include "mm/memory.h"
66 #include "toolbox/avl.h"
67 #include "toolbox/list.h"
68 #include "toolbox/logging.h"
70 #include "native/jni.h"
71 #include "native/native.h"
73 #if defined(ENABLE_THREADS)
74 # include "threads/native/threads.h"
77 #include "vm/exceptions.h"
78 #include "vm/stringlocal.h"
80 #include "vm/jit/abi.h"
81 #include "vm/jit/asmpart.h"
82 #include "vm/jit/codegen-common.h"
84 #if defined(ENABLE_DISASSEMBLER)
85 # include "vm/jit/disass.h"
88 #include "vm/jit/dseg.h"
89 #include "vm/jit/jit.h"
90 #include "vm/jit/md.h"
91 #include "vm/jit/replace.h"
92 #include "vm/jit/stacktrace.h"
94 #if defined(ENABLE_INTRP)
95 #include "vm/jit/intrp/intrp.h"
98 #include "vmcore/method.h"
99 #include "vmcore/options.h"
101 #if defined(ENABLE_STATISTICS)
102 # include "vmcore/statistics.h"
106 /* in this tree we store all method addresses *********************************/
108 static avl_tree *methodtree = NULL;
109 static s4 methodtree_comparator(const void *pc, const void *element);
112 /* codegen_init ****************************************************************
116 *******************************************************************************/
118 void codegen_init(void)
120 /* this tree is global, not method specific */
123 #if defined(ENABLE_JIT)
124 methodtree_element *mte;
127 methodtree = avl_create(&methodtree_comparator);
129 #if defined(ENABLE_JIT)
130 /* insert asm_vm_call_method */
132 mte = NEW(methodtree_element);
134 mte->startpc = (u1 *) (ptrint) asm_vm_call_method;
135 mte->endpc = (u1 *) ((ptrint) asm_call_jit_compiler - 1);
137 avl_insert(methodtree, mte);
138 #endif /* defined(ENABLE_JIT) */
143 /* codegen_setup ***************************************************************
145 Allocates and initialises code area, data area and references.
147 *******************************************************************************/
149 void codegen_setup(jitdata *jd)
154 /* get required compiler data */
159 cd->mcodebase = DMNEW(u1, MCODEINITSIZE);
160 cd->mcodeend = cd->mcodebase + MCODEINITSIZE;
161 cd->mcodesize = MCODEINITSIZE;
163 /* initialize mcode variables */
165 cd->mcodeptr = cd->mcodebase;
166 cd->lastmcodeptr = cd->mcodebase;
168 #if defined(ENABLE_INTRP)
169 /* native dynamic superinstructions variables */
172 cd->ncodebase = DMNEW(u1, NCODEINITSIZE);
173 cd->ncodesize = NCODEINITSIZE;
175 /* initialize ncode variables */
177 cd->ncodeptr = cd->ncodebase;
179 cd->lastinstwithoutdispatch = ~0; /* no inst without dispatch */
180 cd->superstarts = NULL;
187 cd->jumpreferences = NULL;
189 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(ENABLE_INTRP)
190 cd->datareferences = NULL;
193 cd->exceptionrefs = NULL;
194 /* cd->patchrefs = list_create_dump(OFFSET(patchref, linkage)); */
195 cd->patchrefs = NULL;
197 cd->linenumberreferences = NULL;
198 cd->linenumbertablesizepos = 0;
199 cd->linenumbertablestartpos = 0;
200 cd->linenumbertab = 0;
204 cd->maxstack = m->maxstack;
206 #if defined(ENABLE_THREADS)
207 cd->threadcritcurrent.next = NULL;
208 cd->threadcritcount = 0;
213 /* codegen_close ***************************************************************
217 *******************************************************************************/
219 void codegen_close(void)
221 /* TODO: release avl tree on i386 and x86_64 */
225 /* codegen_increase ************************************************************
229 *******************************************************************************/
231 void codegen_increase(codegendata *cd)
235 /* save old mcodebase pointer */
237 oldmcodebase = cd->mcodebase;
239 /* reallocate to new, doubled memory */
241 cd->mcodebase = DMREALLOC(cd->mcodebase,
246 cd->mcodeend = cd->mcodebase + cd->mcodesize;
248 /* set new mcodeptr */
250 cd->mcodeptr = cd->mcodebase + (cd->mcodeptr - oldmcodebase);
252 #if defined(__I386__) || defined(__MIPS__) || defined(__X86_64__) || defined(ENABLE_INTRP)
253 /* adjust the pointer to the last patcher position */
255 if (cd->lastmcodeptr != NULL)
256 cd->lastmcodeptr = cd->mcodebase + (cd->lastmcodeptr - oldmcodebase);
261 /* codegen_ncode_increase ******************************************************
265 *******************************************************************************/
267 #if defined(ENABLE_INTRP)
268 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr)
272 /* save old ncodebase pointer */
274 oldncodebase = cd->ncodebase;
276 /* reallocate to new, doubled memory */
278 cd->ncodebase = DMREALLOC(cd->ncodebase,
284 /* return the new ncodeptr */
286 return (cd->ncodebase + (ncodeptr - oldncodebase));
291 /* codegen_add_branch_ref ******************************************************
293 Prepends an branch to the list.
295 *******************************************************************************/
297 void codegen_add_branch_ref(codegendata *cd, basicblock *target)
301 /* calculate the mpc of the branch instruction */
303 branchmpc = cd->mcodeptr - cd->mcodebase;
305 #if defined(ENABLE_JIT)
306 /* Check if the target basicblock has already a start pc, so the
307 jump is backward and we can resolve it immediately. */
309 if ((target->mpc >= 0)
310 # if defined(ENABLE_INTRP)
311 /* The interpreter uses absolute branches, so we do branch
312 resolving after the code and data segment move. */
318 md_codegen_patch_branch(cd, branchmpc, target->mpc);
323 branchref *br = DNEW(branchref);
325 br->branchpos = branchmpc;
326 br->next = target->branchrefs;
328 target->branchrefs = br;
333 /* codegen_resolve_branchrefs **************************************************
335 Resolves and patches the branch references of a given basic block.
337 *******************************************************************************/
339 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr)
347 targetmpc = bptr->mpc;
349 for (br = bptr->branchrefs; br != NULL; br = br->next) {
350 branchmpc = br->branchpos;
352 md_codegen_patch_branch(cd, branchmpc, targetmpc);
357 /* codegen_add_exception_ref ***************************************************
359 Prepends an exception branch to the list.
361 *******************************************************************************/
363 static void codegen_add_exception_ref(codegendata *cd, s4 reg,
364 functionptr function)
369 branchmpc = cd->mcodeptr - cd->mcodebase;
371 er = DNEW(exceptionref);
373 er->branchpos = branchmpc;
375 er->function = function;
377 er->next = cd->exceptionrefs;
379 cd->exceptionrefs = er;
383 /* codegen_add_arithmeticexception_ref *****************************************
385 Adds an ArithmeticException branch to the list.
387 *******************************************************************************/
389 void codegen_add_arithmeticexception_ref(codegendata *cd)
391 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_arithmeticexception);
395 /* codegen_add_arrayindexoutofboundsexception_ref ******************************
397 Adds an ArrayIndexOutOfBoundsException branch to the list.
399 *******************************************************************************/
401 void codegen_add_arrayindexoutofboundsexception_ref(codegendata *cd, s4 reg)
403 codegen_add_exception_ref(cd, reg,
404 STACKTRACE_inline_arrayindexoutofboundsexception);
408 /* codegen_add_arraystoreexception_ref *****************************************
410 Adds an ArrayStoreException branch to the list.
412 *******************************************************************************/
414 void codegen_add_arraystoreexception_ref(codegendata *cd)
416 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_arraystoreexception);
420 /* codegen_add_classcastexception_ref ******************************************
422 Adds an ClassCastException branch to the list.
424 *******************************************************************************/
426 void codegen_add_classcastexception_ref(codegendata *cd, s4 reg)
428 codegen_add_exception_ref(cd, reg, STACKTRACE_inline_classcastexception);
432 /* codegen_add_nullpointerexception_ref ****************************************
434 Adds an NullPointerException branch to the list.
436 *******************************************************************************/
438 void codegen_add_nullpointerexception_ref(codegendata *cd)
440 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_nullpointerexception);
444 /* codegen_add_fillinstacktrace_ref ********************************************
446 Adds a fillInStackTrace branch to the list.
448 *******************************************************************************/
450 void codegen_add_fillinstacktrace_ref(codegendata *cd)
452 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_fillInStackTrace);
456 /* codegen_add_patch_ref *******************************************************
458 Appends a new patcher reference to the list of patching positions.
460 *******************************************************************************/
462 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
468 branchmpc = cd->mcodeptr - cd->mcodebase;
472 pr->branchpos = branchmpc;
474 pr->patcher = patcher;
477 /* list_add_first(cd->patchrefs, pr); */
478 pr->next = cd->patchrefs;
481 #if defined(ENABLE_JIT) && (defined(__ALPHA__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__SPARC_64__) || defined(__X86_64__) || defined(__S390__))
482 /* Generate NOPs for opt_shownops. */
488 #if defined(ENABLE_JIT) && (defined(__I386__) || defined(__MIPS__) || defined(__X86_64__))
489 /* On some architectures the patcher stub call instruction might
490 be longer than the actual instruction generated. On this
491 architectures we store the last patcher call position and after
492 the basic block code generation is completed, we check the
493 range and maybe generate some nop's. */
495 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
500 /* methodtree_comparator *******************************************************
502 Comparator function used for the AVL tree of methods.
504 *******************************************************************************/
506 static s4 methodtree_comparator(const void *pc, const void *element)
508 methodtree_element *mte;
509 methodtree_element *mtepc;
511 mte = (methodtree_element *) element;
512 mtepc = (methodtree_element *) pc;
514 /* compare both startpc and endpc of pc, even if they have the same value,
515 otherwise the avl_probe sometimes thinks the element is already in the
519 /* On S390 addresses are 31 bit, and therefore are ambigue. */
520 # define ADDR_MASK(a) ((a) & 0x7FFFFFFF)
522 # define ADDR_MASK(a) (a)
525 if (ADDR_MASK((long) mte->startpc) <= ADDR_MASK((long) mtepc->startpc) &&
526 ADDR_MASK((long) mtepc->startpc) <= ADDR_MASK((long) mte->endpc) &&
527 ADDR_MASK((long) mte->startpc) <= ADDR_MASK((long) mtepc->endpc) &&
528 ADDR_MASK((long) mtepc->endpc) <= ADDR_MASK((long) mte->endpc)) {
531 } else if (ADDR_MASK((long) mtepc->startpc) < ADDR_MASK((long) mte->startpc)) {
542 /* codegen_insertmethod ********************************************************
544 Insert the machine code range of a method into the AVL tree of methods.
546 *******************************************************************************/
548 void codegen_insertmethod(u1 *startpc, u1 *endpc)
550 methodtree_element *mte;
552 /* allocate new method entry */
554 mte = NEW(methodtree_element);
556 mte->startpc = startpc;
559 /* this function does not return an error, but asserts for
562 avl_insert(methodtree, mte);
566 /* codegen_get_pv_from_pc ******************************************************
568 Find the PV for the given PC by searching in the AVL tree of
571 *******************************************************************************/
573 u1 *codegen_get_pv_from_pc(u1 *pc)
575 methodtree_element mtepc;
576 methodtree_element *mte;
578 /* allocation of the search structure on the stack is much faster */
583 mte = avl_find(methodtree, &mtepc);
586 /* No method was found. Let's dump a stacktrace. */
588 log_println("We received a SIGSEGV and tried to handle it, but we were");
589 log_println("unable to find a Java method at:");
591 #if SIZEOF_VOID_P == 8
592 log_println("PC=0x%016lx", pc);
594 log_println("PC=0x%08x", pc);
597 log_println("Dumping the current stacktrace:");
599 stacktrace_dump_trace(THREADOBJECT);
601 vm_abort("Exiting...");
608 /* codegen_get_pv_from_pc_nocheck **********************************************
610 Find the PV for the given PC by searching in the AVL tree of
611 methods. This method does not check the return value and is used
614 *******************************************************************************/
616 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc)
618 methodtree_element mtepc;
619 methodtree_element *mte;
621 /* allocation of the search structure on the stack is much faster */
626 mte = avl_find(methodtree, &mtepc);
635 /* codegen_set_replacement_point_notrap ****************************************
637 Record the position of a non-trappable replacement point.
639 *******************************************************************************/
641 #if defined(ENABLE_REPLACEMENT)
643 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type)
645 void codegen_set_replacement_point_notrap(codegendata *cd)
648 assert(cd->replacementpoint);
649 assert(cd->replacementpoint->type == type);
650 assert(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP);
652 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
654 cd->replacementpoint++;
656 #endif /* defined(ENABLE_REPLACEMENT) */
659 /* codegen_set_replacement_point ***********************************************
661 Record the position of a trappable replacement point.
663 *******************************************************************************/
665 #if defined(ENABLE_REPLACEMENT)
667 void codegen_set_replacement_point(codegendata *cd, s4 type)
669 void codegen_set_replacement_point(codegendata *cd)
672 assert(cd->replacementpoint);
673 assert(cd->replacementpoint->type == type);
674 assert(!(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP));
676 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
678 cd->replacementpoint++;
680 /* XXX assert(cd->lastmcodeptr <= cd->mcodeptr); */
682 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
684 #endif /* defined(ENABLE_REPLACEMENT) */
687 /* codegen_finish **************************************************************
689 Finishes the code generation. A new memory, large enough for both
690 data and code, is allocated and data and code are copied together
691 to their final layout, unresolved jumps are resolved, ...
693 *******************************************************************************/
695 void codegen_finish(jitdata *jd)
700 #if defined(ENABLE_INTRP)
709 /* get required compiler data */
714 /* prevent compiler warning */
716 #if defined(ENABLE_INTRP)
720 /* calculate the code length */
722 mcodelen = (s4) (cd->mcodeptr - cd->mcodebase);
724 #if defined(ENABLE_THREADS)
725 extralen = sizeof(critical_section_node_t) * cd->threadcritcount;
730 #if defined(ENABLE_STATISTICS)
732 count_code_len += mcodelen;
733 count_data_len += cd->dseglen;
737 alignedmcodelen = MEMORY_ALIGN(mcodelen, MAX_ALIGN);
739 #if defined(ENABLE_INTRP)
741 ncodelen = cd->ncodeptr - cd->ncodebase;
743 ncodelen = 0; /* avoid compiler warning */
747 cd->dseglen = MEMORY_ALIGN(cd->dseglen, MAX_ALIGN);
748 alignedlen = alignedmcodelen + cd->dseglen;
750 #if defined(ENABLE_INTRP)
752 alignedlen += ncodelen;
756 /* allocate new memory */
758 code->mcodelength = mcodelen + cd->dseglen;
759 code->mcode = CNEW(u1, alignedlen + extralen);
761 /* set the entrypoint of the method */
763 assert(code->entrypoint == NULL);
764 code->entrypoint = epoint = (code->mcode + cd->dseglen);
766 /* fill the data segment (code->entrypoint must already be set!) */
770 /* copy code to the new location */
772 MCOPY((void *) code->entrypoint, cd->mcodebase, u1, mcodelen);
774 #if defined(ENABLE_INTRP)
775 /* relocate native dynamic superinstruction code (if any) */
778 cd->mcodebase = code->entrypoint;
781 u1 *ncodebase = code->mcode + cd->dseglen + alignedmcodelen;
783 MCOPY((void *) ncodebase, cd->ncodebase, u1, ncodelen);
785 /* flush the instruction and data caches */
787 md_cacheflush(ncodebase, ncodelen);
789 /* set some cd variables for dynamic_super_rerwite */
791 cd->ncodebase = ncodebase;
794 cd->ncodebase = NULL;
797 dynamic_super_rewrite(cd);
801 /* jump table resolving */
803 for (jr = cd->jumpreferences; jr != NULL; jr = jr->next)
804 *((functionptr *) ((ptrint) epoint + jr->tablepos)) =
805 (functionptr) ((ptrint) epoint + (ptrint) jr->target->mpc);
807 /* line number table resolving */
813 for (lr = cd->linenumberreferences; lr != NULL; lr = lr->next) {
815 target = lr->targetmpc;
816 /* if the entry contains an mcode pointer (normal case), resolve it */
817 /* (see doc/inlining_stacktrace.txt for details) */
818 if (lr->linenumber >= -2) {
819 target += (ptrint) epoint;
821 *((functionptr *) ((ptrint) epoint + (ptrint) lr->tablepos)) =
822 (functionptr) target;
825 *((functionptr *) ((ptrint) epoint + cd->linenumbertablestartpos)) =
826 (functionptr) ((ptrint) epoint + cd->linenumbertab);
828 *((ptrint *) ((ptrint) epoint + cd->linenumbertablesizepos)) = lrtlen;
831 #if defined(ENABLE_REPLACEMENT)
832 /* replacement point resolving */
837 code->replacementstubs += (ptrint) epoint;
839 rp = code->rplpoints;
840 for (i=0; i<code->rplpointcount; ++i, ++rp) {
841 rp->pc = (u1*) ((ptrint) epoint + (ptrint) rp->pc);
844 #endif /* defined(ENABLE_REPLACEMENT) */
846 /* add method into methodtree to find the entrypoint */
848 codegen_insertmethod(code->entrypoint, code->entrypoint + mcodelen);
850 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(ENABLE_INTRP)
851 /* resolve data segment references */
853 dseg_resolve_datareferences(jd);
856 #if defined(ENABLE_THREADS)
858 critical_section_node_t *n = (critical_section_node_t *) ((ptrint) code->mcode + alignedlen);
860 codegen_critical_section_t *nt = cd->threadcrit;
862 for (i = 0; i < cd->threadcritcount; i++) {
863 n->mcodebegin = (u1 *) (ptrint) code->mcode + nt->mcodebegin;
864 n->mcodeend = (u1 *) (ptrint) code->mcode + nt->mcodeend;
865 n->mcoderestart = (u1 *) (ptrint) code->mcode + nt->mcoderestart;
866 critical_register_critical_section(n);
873 /* flush the instruction and data caches */
875 md_cacheflush(code->mcode, code->mcodelength);
879 /* codegen_createnativestub ****************************************************
881 Wrapper for createnativestub.
884 the codeinfo representing the stub code.
886 *******************************************************************************/
888 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m)
897 /* mark dump memory */
899 dumpsize = dump_size();
904 jd->cd = DNEW(codegendata);
905 jd->rd = DNEW(registerdata);
908 /* Allocate codeinfo memory from the heap as we need to keep them. */
910 jd->code = code_codeinfo_new(m); /* XXX check allocation */
912 /* get required compiler data */
916 /* set the flags for the current JIT run */
918 #if defined(ENABLE_PROFILING)
920 jd->flags |= JITDATA_FLAG_INSTRUMENT;
924 jd->flags |= JITDATA_FLAG_VERBOSECALL;
926 /* setup code generation stuff */
928 #if defined(ENABLE_JIT)
929 # if defined(ENABLE_INTRP)
937 /* create new method descriptor with additional native parameters */
940 nativeparams = (m->flags & ACC_STATIC) ? 2 : 1;
942 nmd = (methoddesc *) DMNEW(u1, sizeof(methoddesc) - sizeof(typedesc) +
943 md->paramcount * sizeof(typedesc) +
944 nativeparams * sizeof(typedesc));
946 nmd->paramcount = md->paramcount + nativeparams;
948 nmd->params = DMNEW(paramdesc, nmd->paramcount);
950 nmd->paramtypes[0].type = TYPE_ADR; /* add environment pointer */
952 if (m->flags & ACC_STATIC)
953 nmd->paramtypes[1].type = TYPE_ADR; /* add class pointer */
955 MCOPY(nmd->paramtypes + nativeparams, md->paramtypes, typedesc,
958 #if defined(ENABLE_JIT)
959 # if defined(ENABLE_INTRP)
962 /* pre-allocate the arguments for the native ABI */
964 md_param_alloc_native(nmd);
967 /* generate the code */
969 #if defined(ENABLE_JIT)
970 # if defined(ENABLE_INTRP)
972 code->entrypoint = intrp_createnativestub(f, jd, nmd);
975 code->entrypoint = createnativestub(f, jd, nmd);
977 code->entrypoint = intrp_createnativestub(f, jd, nmd);
980 #if defined(ENABLE_STATISTICS)
982 count_nstub_len += code->mcodelength;
986 /* disassemble native stub */
988 if (opt_shownativestub) {
989 #if defined(ENABLE_DISASSEMBLER)
990 codegen_disassemble_nativestub(m,
991 (u1 *) (ptrint) code->entrypoint,
992 (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
995 /* show data segment */
997 if (opt_showddatasegment)
1000 #endif /* !defined(NDEBUG) */
1002 /* release memory */
1004 dump_release(dumpsize);
1006 /* return native stub code */
1012 /* codegen_disassemble_nativestub **********************************************
1014 Disassembles the generated native stub.
1016 *******************************************************************************/
1018 #if defined(ENABLE_DISASSEMBLER)
1019 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end)
1021 printf("Native stub: ");
1022 utf_fprint_printable_ascii_classname(stdout, m->class->name);
1024 utf_fprint_printable_ascii(stdout, m->name);
1025 utf_fprint_printable_ascii(stdout, m->descriptor);
1026 printf("\n\nLength: %d\n\n", (s4) (end - start));
1028 DISASSEMBLE(start, end);
1033 /* codegen_start_native_call ***************************************************
1035 Prepares the stuff required for a native (JNI) function call:
1037 - adds a stackframe info structure to the chain, for stacktraces
1038 - prepares the local references table on the stack
1040 The layout of the native stub stackframe should look like this:
1042 +---------------------------+ <- SP (of parent Java function)
1044 +---------------------------+
1046 | stackframe info structure |
1048 +---------------------------+
1050 | local references table |
1052 +---------------------------+
1054 | arguments (if any) |
1056 +---------------------------+ <- SP (native stub)
1058 *******************************************************************************/
1060 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
1062 stackframeinfo *sfi;
1063 localref_table *lrt;
1065 /* get data structures from stack */
1067 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1068 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1069 sizeof(localref_table));
1071 /* add a stackframeinfo to the chain */
1073 stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
1075 #if defined(ENABLE_JAVASE)
1076 /* add current JNI local references table to this thread */
1078 lrt->capacity = LOCALREFTABLE_CAPACITY;
1080 lrt->localframes = 1;
1081 lrt->prev = LOCALREFTABLE;
1083 /* clear the references array (memset is faster the a for-loop) */
1085 MSET(lrt->refs, 0, java_objectheader*, LOCALREFTABLE_CAPACITY);
1087 LOCALREFTABLE = lrt;
1092 /* codegen_finish_native_call **************************************************
1094 Removes the stuff required for a native (JNI) function call.
1095 Additionally it checks for an exceptions and in case, get the
1096 exception object and clear the pointer.
1098 *******************************************************************************/
1100 java_objectheader *codegen_finish_native_call(u1 *datasp)
1102 stackframeinfo *sfi;
1103 stackframeinfo **psfi;
1104 localref_table *lrt;
1105 localref_table *plrt;
1107 java_objectheader *e;
1109 /* get data structures from stack */
1111 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1112 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1113 sizeof(localref_table));
1115 /* remove current stackframeinfo from chain */
1117 psfi = STACKFRAMEINFO;
1121 #if defined(ENABLE_JAVASE)
1122 /* release JNI local references tables for this thread */
1124 lrt = LOCALREFTABLE;
1126 /* release all current local frames */
1128 for (localframes = lrt->localframes; localframes >= 1; localframes--) {
1129 /* get previous frame */
1133 /* Clear all reference entries (only for tables allocated on
1136 if (localframes > 1)
1137 MSET(&lrt->refs[0], 0, java_objectheader*, lrt->capacity);
1141 /* set new local references table */
1146 /* now store the previous local frames in the thread structure */
1148 LOCALREFTABLE = lrt;
1151 /* get the exception and return it */
1153 e = exceptions_get_and_clear_exception();
1159 /* removecompilerstub **********************************************************
1161 Deletes a compilerstub from memory (simply by freeing it).
1163 *******************************************************************************/
1165 void removecompilerstub(u1 *stub)
1167 /* pass size 1 to keep the intern function happy */
1169 CFREE((void *) stub, 1);
1173 /* removenativestub ************************************************************
1175 Removes a previously created native-stub from memory.
1177 *******************************************************************************/
1179 void removenativestub(u1 *stub)
1181 /* pass size 1 to keep the intern function happy */
1183 CFREE((void *) stub, 1);
1187 /* codegen_reg_of_var **********************************************************
1189 This function determines a register, to which the result of an
1190 operation should go, when it is ultimatively intended to store the
1191 result in pseudoregister v. If v is assigned to an actual
1192 register, this register will be returned. Otherwise (when v is
1193 spilled) this function returns tempregnum. If not already done,
1194 regoff and flags are set in the stack location.
1196 On ARM we have to check if a long/double variable is splitted
1197 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1198 register of v for LOW_REG and the tempregnum for HIGH_REG in such
1199 cases. (michi 2005/07/24)
1201 *******************************************************************************/
1203 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum)
1207 /* Do we have to generate a conditional move? Yes, then always
1208 return the temporary register. The real register is identified
1209 during the store. */
1211 if (opcode & ICMD_CONDITION_MASK)
1215 if (!(v->flags & INMEMORY)) {
1216 #if defined(__ARM__) && defined(__ARMEL__)
1217 if (IS_2_WORD_TYPE(v->type) && (GET_HIGH_REG(v->vv.regoff) == REG_SPLIT))
1218 return PACK_REGS(GET_LOW_REG(v->vv.regoff),
1219 GET_HIGH_REG(tempregnum));
1221 #if defined(__ARM__) && defined(__ARMEB__)
1222 if (IS_2_WORD_TYPE(v->type) && (GET_LOW_REG(v->vv.regoff) == REG_SPLIT))
1223 return PACK_REGS(GET_LOW_REG(tempregnum),
1224 GET_HIGH_REG(v->vv.regoff));
1226 return v->vv.regoff;
1229 #if defined(ENABLE_STATISTICS)
1231 count_spills_read++;
1237 /* codegen_reg_of_dst **********************************************************
1239 This function determines a register, to which the result of an
1240 operation should go, when it is ultimatively intended to store the
1241 result in iptr->dst.var. If dst.var is assigned to an actual
1242 register, this register will be returned. Otherwise (when it is
1243 spilled) this function returns tempregnum. If not already done,
1244 regoff and flags are set in the stack location.
1246 On ARM we have to check if a long/double variable is splitted
1247 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1248 register of dst.var for LOW_REG and the tempregnum for HIGH_REG in such
1249 cases. (michi 2005/07/24)
1251 *******************************************************************************/
1253 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum)
1255 return codegen_reg_of_var(iptr->opc, VAROP(iptr->dst), tempregnum);
1259 #if defined(ENABLE_THREADS)
1260 void codegen_threadcritrestart(codegendata *cd, int offset)
1262 cd->threadcritcurrent.mcoderestart = offset;
1266 void codegen_threadcritstart(codegendata *cd, int offset)
1268 cd->threadcritcurrent.mcodebegin = offset;
1272 void codegen_threadcritstop(codegendata *cd, int offset)
1274 cd->threadcritcurrent.next = cd->threadcrit;
1275 cd->threadcritcurrent.mcodeend = offset;
1276 cd->threadcrit = DNEW(codegen_critical_section_t);
1277 *(cd->threadcrit) = cd->threadcritcurrent;
1278 cd->threadcritcount++;
1284 * These are local overrides for various environment variables in Emacs.
1285 * Please do not remove this and leave it at the end of the file, where
1286 * Emacs will automagically detect them.
1287 * ---------------------------------------------------------------------
1290 * indent-tabs-mode: t
1294 * vim:noexpandtab:sw=4:ts=4: