1 /* src/vm/jit/codegen-common.c - architecture independent code generator stuff
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
27 Authors: Reinhard Grafl
33 All functions assume the following code area / data area layout:
37 | code area | code area grows to higher addresses
39 +-----------+ <-- start of procedure
41 | data area | data area grows to lower addresses
45 The functions first write into a temporary code/data area allocated by
46 "codegen_init". "codegen_finish" copies the code and data area into permanent
47 memory. All functions writing values into the data area return the offset
48 relative the begin of the code area (start of procedure).
50 $Id: codegen-common.c 6251 2006-12-27 23:15:56Z twisti $
62 #if defined(ENABLE_JIT)
63 /* this is required PATCHER_CALL_SIZE */
68 /* this is required for REG_SPLIT */
72 #include "mm/memory.h"
73 #include "toolbox/avl.h"
74 #include "toolbox/list.h"
75 #include "toolbox/logging.h"
76 #include "native/jni.h"
77 #include "native/native.h"
79 #if defined(ENABLE_THREADS)
80 # include "threads/native/threads.h"
83 #include "vm/exceptions.h"
84 #include "vm/method.h"
85 #include "vm/options.h"
86 #include "vm/statistics.h"
87 #include "vm/stringlocal.h"
88 #include "vm/jit/asmpart.h"
89 #include "vm/jit/codegen-common.h"
91 #if defined(ENABLE_DISASSEMBLER)
92 # include "vm/jit/disass.h"
95 #include "vm/jit/dseg.h"
96 #include "vm/jit/jit.h"
97 #include "vm/jit/stacktrace.h"
98 #include "vm/jit/replace.h"
100 #if defined(ENABLE_INTRP)
101 #include "vm/jit/intrp/intrp.h"
105 /* in this tree we store all method addresses *********************************/
107 static avl_tree *methodtree = NULL;
108 static s4 methodtree_comparator(const void *pc, const void *element);
111 /* codegen_init ****************************************************************
115 *******************************************************************************/
117 void codegen_init(void)
119 /* this tree is global, not method specific */
122 #if defined(ENABLE_JIT)
123 methodtree_element *mte;
126 methodtree = avl_create(&methodtree_comparator);
128 #if defined(ENABLE_JIT)
129 /* insert asm_vm_call_method */
131 mte = NEW(methodtree_element);
133 mte->startpc = (u1 *) (ptrint) asm_vm_call_method;
134 mte->endpc = (u1 *) ((ptrint) asm_call_jit_compiler - 1);
136 avl_insert(methodtree, mte);
137 #endif /* defined(ENABLE_JIT) */
142 /* codegen_setup ***************************************************************
144 Allocates and initialises code area, data area and references.
146 *******************************************************************************/
148 void codegen_setup(jitdata *jd)
153 /* get required compiler data */
158 cd->mcodebase = DMNEW(u1, MCODEINITSIZE);
159 cd->mcodeend = cd->mcodebase + MCODEINITSIZE;
160 cd->mcodesize = MCODEINITSIZE;
162 /* initialize mcode variables */
164 cd->mcodeptr = cd->mcodebase;
165 cd->lastmcodeptr = cd->mcodebase;
167 #if defined(ENABLE_INTRP)
168 /* native dynamic superinstructions variables */
171 cd->ncodebase = DMNEW(u1, NCODEINITSIZE);
172 cd->ncodesize = NCODEINITSIZE;
174 /* initialize ncode variables */
176 cd->ncodeptr = cd->ncodebase;
178 cd->lastinstwithoutdispatch = ~0; /* no inst without dispatch */
179 cd->superstarts = NULL;
186 cd->jumpreferences = NULL;
188 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(ENABLE_INTRP)
189 cd->datareferences = NULL;
192 cd->exceptionrefs = NULL;
193 /* cd->patchrefs = list_create_dump(OFFSET(patchref, linkage)); */
194 cd->patchrefs = NULL;
196 cd->linenumberreferences = NULL;
197 cd->linenumbertablesizepos = 0;
198 cd->linenumbertablestartpos = 0;
199 cd->linenumbertab = 0;
203 cd->maxstack = m->maxstack;
205 #if defined(ENABLE_THREADS)
206 cd->threadcritcurrent.next = NULL;
207 cd->threadcritcount = 0;
212 /* codegen_close ***************************************************************
216 *******************************************************************************/
218 void codegen_close(void)
220 /* TODO: release avl tree on i386 and x86_64 */
224 /* codegen_increase ************************************************************
228 *******************************************************************************/
230 void codegen_increase(codegendata *cd)
234 /* save old mcodebase pointer */
236 oldmcodebase = cd->mcodebase;
238 /* reallocate to new, doubled memory */
240 cd->mcodebase = DMREALLOC(cd->mcodebase,
245 cd->mcodeend = cd->mcodebase + cd->mcodesize;
247 /* set new mcodeptr */
249 cd->mcodeptr = cd->mcodebase + (cd->mcodeptr - oldmcodebase);
251 #if defined(__I386__) || defined(__MIPS__) || defined(__X86_64__) || defined(ENABLE_INTRP)
252 /* adjust the pointer to the last patcher position */
254 if (cd->lastmcodeptr != NULL)
255 cd->lastmcodeptr = cd->mcodebase + (cd->lastmcodeptr - oldmcodebase);
260 /* codegen_ncode_increase ******************************************************
264 *******************************************************************************/
266 #if defined(ENABLE_INTRP)
267 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr)
271 /* save old ncodebase pointer */
273 oldncodebase = cd->ncodebase;
275 /* reallocate to new, doubled memory */
277 cd->ncodebase = DMREALLOC(cd->ncodebase,
283 /* return the new ncodeptr */
285 return (cd->ncodebase + (ncodeptr - oldncodebase));
290 /* codegen_add_branch_ref ******************************************************
292 Prepends an branch to the list.
294 *******************************************************************************/
296 void codegen_add_branch_ref(codegendata *cd, basicblock *target)
300 /* calculate the mpc of the branch instruction */
302 branchmpc = cd->mcodeptr - cd->mcodebase;
304 #if defined(ENABLE_JIT)
305 /* Check if the target basicblock has already a start pc, so the
306 jump is backward and we can resolve it immediately. */
308 if ((target->mpc >= 0)
309 # if defined(ENABLE_INTRP)
310 /* The interpreter uses absolute branches, so we do branch
311 resolving after the code and data segment move. */
317 md_codegen_patch_branch(cd, branchmpc, target->mpc);
322 branchref *br = DNEW(branchref);
324 br->branchpos = branchmpc;
325 br->next = target->branchrefs;
327 target->branchrefs = br;
332 /* codegen_resolve_branchrefs **************************************************
334 Resolves and patches the branch references of a given basic block.
336 *******************************************************************************/
338 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr)
346 targetmpc = bptr->mpc;
348 for (br = bptr->branchrefs; br != NULL; br = br->next) {
349 branchmpc = br->branchpos;
351 md_codegen_patch_branch(cd, branchmpc, targetmpc);
356 /* codegen_add_exception_ref ***************************************************
358 Prepends an exception branch to the list.
360 *******************************************************************************/
362 static void codegen_add_exception_ref(codegendata *cd, s4 reg,
363 functionptr function)
368 branchmpc = cd->mcodeptr - cd->mcodebase;
370 er = DNEW(exceptionref);
372 er->branchpos = branchmpc;
374 er->function = function;
376 er->next = cd->exceptionrefs;
378 cd->exceptionrefs = er;
382 /* codegen_add_arithmeticexception_ref *****************************************
384 Adds an ArithmeticException branch to the list.
386 *******************************************************************************/
388 void codegen_add_arithmeticexception_ref(codegendata *cd)
390 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_arithmeticexception);
394 /* codegen_add_arrayindexoutofboundsexception_ref ******************************
396 Adds an ArrayIndexOutOfBoundsException branch to the list.
398 *******************************************************************************/
400 void codegen_add_arrayindexoutofboundsexception_ref(codegendata *cd, s4 reg)
402 codegen_add_exception_ref(cd, reg,
403 STACKTRACE_inline_arrayindexoutofboundsexception);
407 /* codegen_add_arraystoreexception_ref *****************************************
409 Adds an ArrayStoreException branch to the list.
411 *******************************************************************************/
413 void codegen_add_arraystoreexception_ref(codegendata *cd)
415 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_arraystoreexception);
419 /* codegen_add_classcastexception_ref ******************************************
421 Adds an ClassCastException branch to the list.
423 *******************************************************************************/
425 void codegen_add_classcastexception_ref(codegendata *cd, s4 reg)
427 codegen_add_exception_ref(cd, reg, STACKTRACE_inline_classcastexception);
431 /* codegen_add_nullpointerexception_ref ****************************************
433 Adds an NullPointerException branch to the list.
435 *******************************************************************************/
437 void codegen_add_nullpointerexception_ref(codegendata *cd)
439 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_nullpointerexception);
443 /* codegen_add_fillinstacktrace_ref ********************************************
445 Adds a fillInStackTrace branch to the list.
447 *******************************************************************************/
449 void codegen_add_fillinstacktrace_ref(codegendata *cd)
451 codegen_add_exception_ref(cd, -1, STACKTRACE_inline_fillInStackTrace);
455 /* codegen_add_patch_ref *******************************************************
457 Appends a new patcher reference to the list of patching positions.
459 *******************************************************************************/
461 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
467 branchmpc = cd->mcodeptr - cd->mcodebase;
471 pr->branchpos = branchmpc;
473 pr->patcher = patcher;
476 /* list_add_first(cd->patchrefs, pr); */
477 pr->next = cd->patchrefs;
480 #if defined(ENABLE_JIT) && (defined(__ALPHA__) || defined(__MIPS__) || defined(__POWERPC__) || defined(__X86_64__))
481 /* Generate NOPs for opt_shownops. */
487 #if defined(ENABLE_JIT) && (defined(__I386__) || defined(__MIPS__) || defined(__X86_64__))
488 /* On some architectures the patcher stub call instruction might
489 be longer than the actual instruction generated. On this
490 architectures we store the last patcher call position and after
491 the basic block code generation is completed, we check the
492 range and maybe generate some nop's. */
494 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
499 /* methodtree_comparator *******************************************************
501 Comparator function used for the AVL tree of methods.
503 *******************************************************************************/
505 static s4 methodtree_comparator(const void *pc, const void *element)
507 methodtree_element *mte;
508 methodtree_element *mtepc;
510 mte = (methodtree_element *) element;
511 mtepc = (methodtree_element *) pc;
513 /* compare both startpc and endpc of pc, even if they have the same value,
514 otherwise the avl_probe sometimes thinks the element is already in the
517 if ((long) mte->startpc <= (long) mtepc->startpc &&
518 (long) mtepc->startpc <= (long) mte->endpc &&
519 (long) mte->startpc <= (long) mtepc->endpc &&
520 (long) mtepc->endpc <= (long) mte->endpc) {
523 } else if ((long) mtepc->startpc < (long) mte->startpc) {
532 /* codegen_insertmethod ********************************************************
534 Insert the machine code range of a method into the AVL tree of methods.
536 *******************************************************************************/
538 void codegen_insertmethod(u1 *startpc, u1 *endpc)
540 methodtree_element *mte;
542 /* allocate new method entry */
544 mte = NEW(methodtree_element);
546 mte->startpc = startpc;
549 /* this function does not return an error, but asserts for
552 avl_insert(methodtree, mte);
556 /* codegen_get_pv_from_pc ******************************************************
558 Find the PV for the given PC by searching in the AVL tree of
561 *******************************************************************************/
563 u1 *codegen_get_pv_from_pc(u1 *pc)
565 methodtree_element mtepc;
566 methodtree_element *mte;
568 /* allocation of the search structure on the stack is much faster */
573 mte = avl_find(methodtree, &mtepc);
576 /* No method was found. Let's dump a stacktrace. */
578 log_println("We received a SIGSEGV and tried to handle it, but we were");
579 log_println("unable to find a Java method at:");
581 #if SIZEOF_VOID_P == 8
582 log_println("PC=0x%016lx", pc);
584 log_println("PC=0x%08x", pc);
587 log_println("Dumping the current stacktrace:");
589 stacktrace_dump_trace(THREADOBJECT);
591 vm_abort("Exiting...");
598 /* codegen_get_pv_from_pc_nocheck **********************************************
600 Find the PV for the given PC by searching in the AVL tree of
601 methods. This method does not check the return value and is used
604 *******************************************************************************/
606 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc)
608 methodtree_element mtepc;
609 methodtree_element *mte;
611 /* allocation of the search structure on the stack is much faster */
616 mte = avl_find(methodtree, &mtepc);
625 /* codegen_set_replacement_point_notrap ****************************************
627 Record the position of a non-trappable replacement point.
629 *******************************************************************************/
632 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type)
634 void codegen_set_replacement_point_notrap(codegendata *cd)
637 assert(cd->replacementpoint);
638 assert(cd->replacementpoint->type == type);
639 assert(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP);
641 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
643 cd->replacementpoint++;
647 /* codegen_set_replacement_point ***********************************************
649 Record the position of a trappable replacement point.
651 *******************************************************************************/
654 void codegen_set_replacement_point(codegendata *cd, s4 type)
656 void codegen_set_replacement_point(codegendata *cd)
659 assert(cd->replacementpoint);
660 assert(cd->replacementpoint->type == type);
661 assert(!(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP));
663 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
665 cd->replacementpoint++;
667 /* XXX assert(cd->lastmcodeptr <= cd->mcodeptr); */
669 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
673 /* codegen_finish **************************************************************
675 Finishes the code generation. A new memory, large enough for both
676 data and code, is allocated and data and code are copied together
677 to their final layout, unresolved jumps are resolved, ...
679 *******************************************************************************/
681 void codegen_finish(jitdata *jd)
686 #if defined(ENABLE_INTRP)
695 /* get required compiler data */
700 /* prevent compiler warning */
702 #if defined(ENABLE_INTRP)
706 /* calculate the code length */
708 mcodelen = (s4) (cd->mcodeptr - cd->mcodebase);
710 #if defined(ENABLE_THREADS)
711 extralen = sizeof(critical_section_node_t) * cd->threadcritcount;
716 #if defined(ENABLE_STATISTICS)
718 count_code_len += mcodelen;
719 count_data_len += cd->dseglen;
723 alignedmcodelen = MEMORY_ALIGN(mcodelen, MAX_ALIGN);
725 #if defined(ENABLE_INTRP)
727 ncodelen = cd->ncodeptr - cd->ncodebase;
729 ncodelen = 0; /* avoid compiler warning */
733 cd->dseglen = MEMORY_ALIGN(cd->dseglen, MAX_ALIGN);
734 alignedlen = alignedmcodelen + cd->dseglen;
736 #if defined(ENABLE_INTRP)
738 alignedlen += ncodelen;
742 /* allocate new memory */
744 code->mcodelength = mcodelen + cd->dseglen;
745 code->mcode = CNEW(u1, alignedlen + extralen);
747 /* set the entrypoint of the method */
749 assert(code->entrypoint == NULL);
750 code->entrypoint = epoint = (code->mcode + cd->dseglen);
752 /* fill the data segment (code->entrypoint must already be set!) */
756 /* copy code to the new location */
758 MCOPY((void *) code->entrypoint, cd->mcodebase, u1, mcodelen);
760 #if defined(ENABLE_INTRP)
761 /* relocate native dynamic superinstruction code (if any) */
764 cd->mcodebase = code->entrypoint;
767 u1 *ncodebase = code->mcode + cd->dseglen + alignedmcodelen;
769 MCOPY((void *) ncodebase, cd->ncodebase, u1, ncodelen);
771 /* flush the instruction and data caches */
773 md_cacheflush(ncodebase, ncodelen);
775 /* set some cd variables for dynamic_super_rerwite */
777 cd->ncodebase = ncodebase;
780 cd->ncodebase = NULL;
783 dynamic_super_rewrite(cd);
787 /* jump table resolving */
789 for (jr = cd->jumpreferences; jr != NULL; jr = jr->next)
790 *((functionptr *) ((ptrint) epoint + jr->tablepos)) =
791 (functionptr) ((ptrint) epoint + (ptrint) jr->target->mpc);
793 /* line number table resolving */
799 for (lr = cd->linenumberreferences; lr != NULL; lr = lr->next) {
801 target = lr->targetmpc;
802 /* if the entry contains an mcode pointer (normal case), resolve it */
803 /* (see doc/inlining_stacktrace.txt for details) */
804 if (lr->linenumber >= -2) {
805 target += (ptrint) epoint;
807 *((functionptr *) ((ptrint) epoint + (ptrint) lr->tablepos)) =
808 (functionptr) target;
811 *((functionptr *) ((ptrint) epoint + cd->linenumbertablestartpos)) =
812 (functionptr) ((ptrint) epoint + cd->linenumbertab);
814 *((ptrint *) ((ptrint) epoint + cd->linenumbertablesizepos)) = lrtlen;
817 /* replacement point resolving */
822 code->replacementstubs += (ptrint) epoint;
824 rp = code->rplpoints;
825 for (i=0; i<code->rplpointcount; ++i, ++rp) {
826 rp->pc = (u1*) ((ptrint) epoint + (ptrint) rp->pc);
830 /* add method into methodtree to find the entrypoint */
832 codegen_insertmethod(code->entrypoint, code->entrypoint + mcodelen);
834 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(ENABLE_INTRP)
835 /* resolve data segment references */
837 dseg_resolve_datareferences(jd);
840 #if defined(ENABLE_THREADS)
842 critical_section_node_t *n = (critical_section_node_t *) ((ptrint) code->mcode + alignedlen);
844 codegen_critical_section_t *nt = cd->threadcrit;
846 for (i = 0; i < cd->threadcritcount; i++) {
847 n->mcodebegin = (u1 *) (ptrint) code->mcode + nt->mcodebegin;
848 n->mcodeend = (u1 *) (ptrint) code->mcode + nt->mcodeend;
849 n->mcoderestart = (u1 *) (ptrint) code->mcode + nt->mcoderestart;
850 critical_register_critical_section(n);
857 /* flush the instruction and data caches */
859 md_cacheflush(code->mcode, code->mcodelength);
863 /* codegen_createnativestub ****************************************************
865 Wrapper for createnativestub.
868 the codeinfo representing the stub code.
870 *******************************************************************************/
872 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m)
881 /* mark dump memory */
883 dumpsize = dump_size();
888 jd->cd = DNEW(codegendata);
889 jd->rd = DNEW(registerdata);
892 /* Allocate codeinfo memory from the heap as we need to keep them. */
894 jd->code = code_codeinfo_new(m); /* XXX check allocation */
896 /* get required compiler data */
900 /* set the flags for the current JIT run */
902 #if defined(ENABLE_PROFILING)
904 jd->flags |= JITDATA_FLAG_INSTRUMENT;
908 jd->flags |= JITDATA_FLAG_VERBOSECALL;
910 /* setup code generation stuff */
912 #if defined(ENABLE_JIT)
913 # if defined(ENABLE_INTRP)
921 /* create new method descriptor with additional native parameters */
924 nativeparams = (m->flags & ACC_STATIC) ? 2 : 1;
926 nmd = (methoddesc *) DMNEW(u1, sizeof(methoddesc) - sizeof(typedesc) +
927 md->paramcount * sizeof(typedesc) +
928 nativeparams * sizeof(typedesc));
930 nmd->paramcount = md->paramcount + nativeparams;
932 nmd->params = DMNEW(paramdesc, nmd->paramcount);
934 nmd->paramtypes[0].type = TYPE_ADR; /* add environment pointer */
936 if (m->flags & ACC_STATIC)
937 nmd->paramtypes[1].type = TYPE_ADR; /* add class pointer */
939 MCOPY(nmd->paramtypes + nativeparams, md->paramtypes, typedesc,
942 #if defined(ENABLE_JIT)
943 # if defined(ENABLE_INTRP)
949 /* generate the code */
951 #if defined(ENABLE_JIT)
952 # if defined(ENABLE_INTRP)
954 code->entrypoint = intrp_createnativestub(f, jd, nmd);
957 code->entrypoint = createnativestub(f, jd, nmd);
959 code->entrypoint = intrp_createnativestub(f, jd, nmd);
962 #if defined(ENABLE_STATISTICS)
964 count_nstub_len += code->mcodelength;
968 /* disassemble native stub */
970 if (opt_shownativestub) {
971 #if defined(ENABLE_DISASSEMBLER)
972 codegen_disassemble_nativestub(m,
973 (u1 *) (ptrint) code->entrypoint,
974 (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
977 /* show data segment */
979 if (opt_showddatasegment)
982 #endif /* !defined(NDEBUG) */
986 dump_release(dumpsize);
988 /* return native stub code */
994 /* codegen_disassemble_nativestub **********************************************
996 Disassembles the generated native stub.
998 *******************************************************************************/
1000 #if defined(ENABLE_DISASSEMBLER)
1001 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end)
1003 printf("Native stub: ");
1004 utf_fprint_printable_ascii_classname(stdout, m->class->name);
1006 utf_fprint_printable_ascii(stdout, m->name);
1007 utf_fprint_printable_ascii(stdout, m->descriptor);
1008 printf("\n\nLength: %d\n\n", (s4) (end - start));
1010 DISASSEMBLE(start, end);
1015 /* codegen_start_native_call ***************************************************
1017 Prepares the stuff required for a native (JNI) function call:
1019 - adds a stackframe info structure to the chain, for stacktraces
1020 - prepares the local references table on the stack
1022 The layout of the native stub stackframe should look like this:
1024 +---------------------------+ <- SP (of parent Java function)
1026 +---------------------------+
1028 | stackframe info structure |
1030 +---------------------------+
1032 | local references table |
1034 +---------------------------+
1036 | arguments (if any) |
1038 +---------------------------+ <- SP (native stub)
1040 *******************************************************************************/
1042 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
1044 stackframeinfo *sfi;
1045 localref_table *lrt;
1047 /* get data structures from stack */
1049 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1050 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1051 sizeof(localref_table));
1053 /* add a stackframeinfo to the chain */
1055 stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
1057 #if defined(ENABLE_JAVASE)
1058 /* add current JNI local references table to this thread */
1060 lrt->capacity = LOCALREFTABLE_CAPACITY;
1062 lrt->localframes = 1;
1063 lrt->prev = LOCALREFTABLE;
1065 /* clear the references array (memset is faster the a for-loop) */
1067 MSET(lrt->refs, 0, java_objectheader*, LOCALREFTABLE_CAPACITY);
1069 LOCALREFTABLE = lrt;
1074 /* codegen_finish_native_call **************************************************
1076 Removes the stuff required for a native (JNI) function call.
1077 Additionally it checks for an exceptions and in case, get the
1078 exception object and clear the pointer.
1080 *******************************************************************************/
1082 java_objectheader *codegen_finish_native_call(u1 *datasp)
1084 stackframeinfo *sfi;
1085 stackframeinfo **psfi;
1086 localref_table *lrt;
1087 localref_table *plrt;
1089 java_objectheader *e;
1091 /* get data structures from stack */
1093 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1094 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1095 sizeof(localref_table));
1097 /* remove current stackframeinfo from chain */
1099 psfi = STACKFRAMEINFO;
1103 #if defined(ENABLE_JAVASE)
1104 /* release JNI local references tables for this thread */
1106 lrt = LOCALREFTABLE;
1108 /* release all current local frames */
1110 for (localframes = lrt->localframes; localframes >= 1; localframes--) {
1111 /* get previous frame */
1115 /* Clear all reference entries (only for tables allocated on
1118 if (localframes > 1)
1119 MSET(&lrt->refs[0], 0, java_objectheader*, lrt->capacity);
1123 /* set new local references table */
1128 /* now store the previous local frames in the thread structure */
1130 LOCALREFTABLE = lrt;
1133 /* get the exception and return it */
1135 e = exceptions_get_and_clear_exception();
1141 /* removecompilerstub **********************************************************
1143 Deletes a compilerstub from memory (simply by freeing it).
1145 *******************************************************************************/
1147 void removecompilerstub(u1 *stub)
1149 /* pass size 1 to keep the intern function happy */
1151 CFREE((void *) stub, 1);
1155 /* removenativestub ************************************************************
1157 Removes a previously created native-stub from memory.
1159 *******************************************************************************/
1161 void removenativestub(u1 *stub)
1163 /* pass size 1 to keep the intern function happy */
1165 CFREE((void *) stub, 1);
1169 /* codegen_reg_of_var **********************************************************
1171 This function determines a register, to which the result of an
1172 operation should go, when it is ultimatively intended to store the
1173 result in pseudoregister v. If v is assigned to an actual
1174 register, this register will be returned. Otherwise (when v is
1175 spilled) this function returns tempregnum. If not already done,
1176 regoff and flags are set in the stack location.
1178 On ARM we have to check if a long/double variable is splitted
1179 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1180 register of v for LOW_REG and the tempregnum for HIGH_REG in such
1181 cases. (michi 2005/07/24)
1183 *******************************************************************************/
1185 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum)
1189 /* Do we have to generate a conditional move? Yes, then always
1190 return the temporary register. The real register is identified
1191 during the store. */
1193 if (opcode & ICMD_CONDITION_MASK)
1197 if (!(v->flags & INMEMORY)) {
1198 #if defined(__ARM__) && defined(__ARMEL__)
1199 if (IS_2_WORD_TYPE(v->type) && (GET_HIGH_REG(v->vv.regoff) == REG_SPLIT))
1200 return PACK_REGS(GET_LOW_REG(v->vv.regoff),
1201 GET_HIGH_REG(tempregnum));
1203 #if defined(__ARM__) && defined(__ARMEB__)
1204 if (IS_2_WORD_TYPE(v->type) && (GET_LOW_REG(v->vv.regoff) == REG_SPLIT))
1205 return PACK_REGS(GET_LOW_REG(tempregnum),
1206 GET_HIGH_REG(v->vv.regoff));
1208 return v->vv.regoff;
1211 #if defined(ENABLE_STATISTICS)
1213 count_spills_read++;
1219 /* codegen_reg_of_dst **********************************************************
1221 This function determines a register, to which the result of an
1222 operation should go, when it is ultimatively intended to store the
1223 result in iptr->dst.var. If dst.var is assigned to an actual
1224 register, this register will be returned. Otherwise (when it is
1225 spilled) this function returns tempregnum. If not already done,
1226 regoff and flags are set in the stack location.
1228 On ARM we have to check if a long/double variable is splitted
1229 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1230 register of dst.var for LOW_REG and the tempregnum for HIGH_REG in such
1231 cases. (michi 2005/07/24)
1233 *******************************************************************************/
1235 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum)
1237 return codegen_reg_of_var(iptr->opc, VAROP(iptr->dst), tempregnum);
1241 #if defined(ENABLE_THREADS)
1242 void codegen_threadcritrestart(codegendata *cd, int offset)
1244 cd->threadcritcurrent.mcoderestart = offset;
1248 void codegen_threadcritstart(codegendata *cd, int offset)
1250 cd->threadcritcurrent.mcodebegin = offset;
1254 void codegen_threadcritstop(codegendata *cd, int offset)
1256 cd->threadcritcurrent.next = cd->threadcrit;
1257 cd->threadcritcurrent.mcodeend = offset;
1258 cd->threadcrit = DNEW(codegen_critical_section_t);
1259 *(cd->threadcrit) = cd->threadcritcurrent;
1260 cd->threadcritcount++;
1266 * These are local overrides for various environment variables in Emacs.
1267 * Please do not remove this and leave it at the end of the file, where
1268 * Emacs will automagically detect them.
1269 * ---------------------------------------------------------------------
1272 * indent-tabs-mode: t
1276 * vim:noexpandtab:sw=4:ts=4: