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 7667 2007-04-05 00:16:05Z michi $
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 #include "threads/threads-common.h"
75 #include "vm/exceptions.h"
76 #include "vm/stringlocal.h"
78 #include "vm/jit/abi.h"
79 #include "vm/jit/asmpart.h"
80 #include "vm/jit/codegen-common.h"
82 #if defined(ENABLE_DISASSEMBLER)
83 # include "vm/jit/disass.h"
86 #include "vm/jit/dseg.h"
87 #include "vm/jit/emit-common.h"
88 #include "vm/jit/jit.h"
89 #include "vm/jit/md.h"
90 #include "vm/jit/replace.h"
91 #include "vm/jit/stacktrace.h"
93 #if defined(ENABLE_INTRP)
94 #include "vm/jit/intrp/intrp.h"
97 #include "vmcore/method.h"
98 #include "vmcore/options.h"
100 # include "vmcore/statistics.h"
103 /* in this tree we store all method addresses *********************************/
105 static avl_tree *methodtree = NULL;
106 static s4 methodtree_comparator(const void *pc, const void *element);
109 /* codegen_init ****************************************************************
113 *******************************************************************************/
115 void codegen_init(void)
117 /* this tree is global, not method specific */
120 #if defined(ENABLE_JIT)
121 methodtree_element *mte;
124 methodtree = avl_create(&methodtree_comparator);
126 #if defined(ENABLE_JIT)
127 /* insert asm_vm_call_method */
129 mte = NEW(methodtree_element);
131 mte->startpc = (u1 *) (ptrint) asm_vm_call_method;
132 mte->endpc = (u1 *) (ptrint) asm_vm_call_method_end;
134 avl_insert(methodtree, mte);
135 #endif /* defined(ENABLE_JIT) */
140 /* codegen_setup ***************************************************************
142 Allocates and initialises code area, data area and references.
144 *******************************************************************************/
146 void codegen_setup(jitdata *jd)
151 /* get required compiler data */
156 /* initialize members */
160 cd->mcodebase = DMNEW(u1, MCODEINITSIZE);
161 cd->mcodeend = cd->mcodebase + MCODEINITSIZE;
162 cd->mcodesize = MCODEINITSIZE;
164 /* initialize mcode variables */
166 cd->mcodeptr = cd->mcodebase;
167 cd->lastmcodeptr = cd->mcodebase;
169 #if defined(ENABLE_INTRP)
170 /* native dynamic superinstructions variables */
173 cd->ncodebase = DMNEW(u1, NCODEINITSIZE);
174 cd->ncodesize = NCODEINITSIZE;
176 /* initialize ncode variables */
178 cd->ncodeptr = cd->ncodebase;
180 cd->lastinstwithoutdispatch = ~0; /* no inst without dispatch */
181 cd->superstarts = NULL;
188 cd->jumpreferences = NULL;
190 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP)
191 cd->datareferences = NULL;
194 /* cd->patchrefs = list_create_dump(OFFSET(patchref, linkage)); */
195 cd->patchrefs = NULL;
196 cd->brancheslabel = list_create_dump(OFFSET(branch_label_ref_t, linkage));
198 cd->linenumberreferences = NULL;
199 cd->linenumbertablesizepos = 0;
200 cd->linenumbertablestartpos = 0;
201 cd->linenumbertab = 0;
203 cd->maxstack = m->maxstack;
205 #if defined(ENABLE_THREADS)
206 cd->threadcritcurrent.next = NULL;
207 cd->threadcritcount = 0;
212 /* codegen_reset ***************************************************************
214 Resets the codegen data structure so we can recompile the method.
216 *******************************************************************************/
218 static void codegen_reset(jitdata *jd)
224 /* get required compiler data */
229 /* reset error flag */
231 cd->flags &= ~CODEGENDATA_FLAG_ERROR;
233 /* reset some members, we reuse the code memory already allocated
234 as this should have almost the correct size */
236 cd->mcodeptr = cd->mcodebase;
237 cd->lastmcodeptr = cd->mcodebase;
242 cd->jumpreferences = NULL;
244 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP)
245 cd->datareferences = NULL;
248 /* cd->patchrefs = list_create_dump(OFFSET(patchref, linkage)); */
249 cd->patchrefs = NULL;
250 cd->brancheslabel = list_create_dump(OFFSET(branch_label_ref_t, linkage));
252 cd->linenumberreferences = NULL;
253 cd->linenumbertablesizepos = 0;
254 cd->linenumbertablestartpos = 0;
255 cd->linenumbertab = 0;
257 #if defined(ENABLE_THREADS)
258 cd->threadcritcurrent.next = NULL;
259 cd->threadcritcount = 0;
262 /* We need to clear the mpc and the branch references from all
263 basic blocks as they will definitely change. */
265 for (bptr = jd->basicblocks; bptr != NULL; bptr = bptr->next) {
267 bptr->branchrefs = NULL;
270 #if defined(ENABLE_REPLACEMENT)
271 code->rplpoints = NULL;
272 code->rplpointcount = 0;
273 code->regalloc = NULL;
274 code->regalloccount = 0;
275 code->globalcount = 0;
280 /* codegen_generate ************************************************************
282 Generates the code for the currently compiled method.
284 *******************************************************************************/
286 bool codegen_generate(jitdata *jd)
290 /* get required compiler data */
294 /* call the machine-dependent code generation function */
296 if (!codegen_emit(jd))
299 /* check for an error */
301 if (CODEGENDATA_HAS_FLAG_ERROR(cd)) {
302 /* check for long-branches flag, if it is set we recompile the
304 /* XXX maybe we should tag long-branches-methods for recompilation */
306 if (CODEGENDATA_HAS_FLAG_LONGBRANCHES(cd)) {
307 /* we have to reset the codegendata structure first */
311 /* and restart the compiler run */
313 if (!codegen_emit(jd))
317 vm_abort("codegen_generate: unknown error occurred during codegen_emit: flags=%x\n", cd->flags);
321 /* reallocate the memory and finish the code generation */
325 /* everything's ok */
331 /* codegen_close ***************************************************************
335 *******************************************************************************/
337 void codegen_close(void)
339 /* TODO: release avl tree on i386 and x86_64 */
343 /* codegen_increase ************************************************************
347 *******************************************************************************/
349 void codegen_increase(codegendata *cd)
353 /* save old mcodebase pointer */
355 oldmcodebase = cd->mcodebase;
357 /* reallocate to new, doubled memory */
359 cd->mcodebase = DMREALLOC(cd->mcodebase,
364 cd->mcodeend = cd->mcodebase + cd->mcodesize;
366 /* set new mcodeptr */
368 cd->mcodeptr = cd->mcodebase + (cd->mcodeptr - oldmcodebase);
370 #if defined(__I386__) || defined(__MIPS__) || defined(__X86_64__) || defined(ENABLE_INTRP)
371 /* adjust the pointer to the last patcher position */
373 if (cd->lastmcodeptr != NULL)
374 cd->lastmcodeptr = cd->mcodebase + (cd->lastmcodeptr - oldmcodebase);
379 /* codegen_ncode_increase ******************************************************
383 *******************************************************************************/
385 #if defined(ENABLE_INTRP)
386 u1 *codegen_ncode_increase(codegendata *cd, u1 *ncodeptr)
390 /* save old ncodebase pointer */
392 oldncodebase = cd->ncodebase;
394 /* reallocate to new, doubled memory */
396 cd->ncodebase = DMREALLOC(cd->ncodebase,
402 /* return the new ncodeptr */
404 return (cd->ncodebase + (ncodeptr - oldncodebase));
409 /* codegen_add_branch_ref ******************************************************
411 Prepends an branch to the list.
413 *******************************************************************************/
415 void codegen_add_branch_ref(codegendata *cd, basicblock *target, s4 condition, s4 reg, u4 options)
420 STATISTICS(count_branches_unresolved++);
422 /* calculate the mpc of the branch instruction */
424 branchmpc = cd->mcodeptr - cd->mcodebase;
426 br = DNEW(branchref);
428 br->branchmpc = branchmpc;
429 br->condition = condition;
431 br->options = options;
432 br->next = target->branchrefs;
434 target->branchrefs = br;
438 /* codegen_resolve_branchrefs **************************************************
440 Resolves and patches the branch references of a given basic block.
442 *******************************************************************************/
444 void codegen_resolve_branchrefs(codegendata *cd, basicblock *bptr)
449 /* Save the mcodeptr because in the branch emitting functions
450 we generate code somewhere inside already generated code,
451 but we're still in the actual code generation phase. */
453 mcodeptr = cd->mcodeptr;
455 /* just to make sure */
457 assert(bptr->mpc >= 0);
459 for (br = bptr->branchrefs; br != NULL; br = br->next) {
460 /* temporary set the mcodeptr */
462 cd->mcodeptr = cd->mcodebase + br->branchmpc;
464 /* emit_bccz and emit_branch emit the correct code, even if we
465 pass condition == BRANCH_UNCONDITIONAL or reg == -1. */
467 emit_bccz(cd, bptr, br->condition, br->reg, br->options);
470 /* restore mcodeptr */
472 cd->mcodeptr = mcodeptr;
476 /* codegen_branch_label_add ****************************************************
478 Append an branch to the label-branch list.
480 *******************************************************************************/
482 void codegen_branch_label_add(codegendata *cd, s4 label, s4 condition, s4 reg, u4 options)
485 branch_label_ref_t *br;
488 /* get the label list */
490 list = cd->brancheslabel;
492 /* calculate the current mpc */
494 mpc = cd->mcodeptr - cd->mcodebase;
496 br = DNEW(branch_label_ref_t);
500 br->condition = condition;
502 br->options = options;
504 /* add the branch to the list */
506 list_add_last_unsynced(list, br);
510 /* codegen_add_patch_ref *******************************************************
512 Appends a new patcher reference to the list of patching positions.
514 *******************************************************************************/
516 void codegen_add_patch_ref(codegendata *cd, functionptr patcher, voidptr ref,
522 branchmpc = cd->mcodeptr - cd->mcodebase;
526 pr->branchpos = branchmpc;
528 pr->patcher = patcher;
531 /* list_add_first(cd->patchrefs, pr); */
532 pr->next = cd->patchrefs;
535 /* Generate NOPs for opt_shownops. */
540 #if defined(ENABLE_JIT) && (defined(__I386__) || defined(__MIPS__) || defined(__X86_64__))
541 /* On some architectures the patcher stub call instruction might
542 be longer than the actual instruction generated. On this
543 architectures we store the last patcher call position and after
544 the basic block code generation is completed, we check the
545 range and maybe generate some nop's. */
547 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
552 /* methodtree_comparator *******************************************************
554 Comparator function used for the AVL tree of methods.
556 *******************************************************************************/
558 static s4 methodtree_comparator(const void *pc, const void *element)
560 methodtree_element *mte;
561 methodtree_element *mtepc;
563 mte = (methodtree_element *) element;
564 mtepc = (methodtree_element *) pc;
566 /* compare both startpc and endpc of pc, even if they have the same value,
567 otherwise the avl_probe sometimes thinks the element is already in the
571 /* On S390 addresses are 31 bit. Compare only 31 bits of value.
573 # define ADDR_MASK(a) ((a) & 0x7FFFFFFF)
575 # define ADDR_MASK(a) (a)
578 if (ADDR_MASK((long) mte->startpc) <= ADDR_MASK((long) mtepc->startpc) &&
579 ADDR_MASK((long) mtepc->startpc) <= ADDR_MASK((long) mte->endpc) &&
580 ADDR_MASK((long) mte->startpc) <= ADDR_MASK((long) mtepc->endpc) &&
581 ADDR_MASK((long) mtepc->endpc) <= ADDR_MASK((long) mte->endpc)) {
584 } else if (ADDR_MASK((long) mtepc->startpc) < ADDR_MASK((long) mte->startpc)) {
595 /* codegen_insertmethod ********************************************************
597 Insert the machine code range of a method into the AVL tree of methods.
599 *******************************************************************************/
601 void codegen_insertmethod(u1 *startpc, u1 *endpc)
603 methodtree_element *mte;
605 /* allocate new method entry */
607 mte = NEW(methodtree_element);
609 mte->startpc = startpc;
612 /* this function does not return an error, but asserts for
615 avl_insert(methodtree, mte);
619 /* codegen_get_pv_from_pc ******************************************************
621 Find the PV for the given PC by searching in the AVL tree of
624 *******************************************************************************/
626 u1 *codegen_get_pv_from_pc(u1 *pc)
628 methodtree_element mtepc;
629 methodtree_element *mte;
631 /* allocation of the search structure on the stack is much faster */
636 mte = avl_find(methodtree, &mtepc);
639 /* No method was found. Let's dump a stacktrace. */
641 log_println("We received a SIGSEGV and tried to handle it, but we were");
642 log_println("unable to find a Java method at:");
644 #if SIZEOF_VOID_P == 8
645 log_println("PC=0x%016lx", pc);
647 log_println("PC=0x%08x", pc);
650 log_println("Dumping the current stacktrace:");
652 threads_print_stacktrace(THREADOBJECT);
654 vm_abort("Exiting...");
661 /* codegen_get_pv_from_pc_nocheck **********************************************
663 Find the PV for the given PC by searching in the AVL tree of
664 methods. This method does not check the return value and is used
667 *******************************************************************************/
669 u1 *codegen_get_pv_from_pc_nocheck(u1 *pc)
671 methodtree_element mtepc;
672 methodtree_element *mte;
674 /* allocation of the search structure on the stack is much faster */
679 mte = avl_find(methodtree, &mtepc);
688 /* codegen_set_replacement_point_notrap ****************************************
690 Record the position of a non-trappable replacement point.
692 *******************************************************************************/
694 #if defined(ENABLE_REPLACEMENT)
696 void codegen_set_replacement_point_notrap(codegendata *cd, s4 type)
698 void codegen_set_replacement_point_notrap(codegendata *cd)
701 assert(cd->replacementpoint);
702 assert(cd->replacementpoint->type == type);
703 assert(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP);
705 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
707 cd->replacementpoint++;
709 #endif /* defined(ENABLE_REPLACEMENT) */
712 /* codegen_set_replacement_point ***********************************************
714 Record the position of a trappable replacement point.
716 *******************************************************************************/
718 #if defined(ENABLE_REPLACEMENT)
720 void codegen_set_replacement_point(codegendata *cd, s4 type)
722 void codegen_set_replacement_point(codegendata *cd)
725 assert(cd->replacementpoint);
726 assert(cd->replacementpoint->type == type);
727 assert(!(cd->replacementpoint->flags & RPLPOINT_FLAG_NOTRAP));
729 cd->replacementpoint->pc = (u1*) (ptrint) (cd->mcodeptr - cd->mcodebase);
731 cd->replacementpoint++;
733 /* XXX assert(cd->lastmcodeptr <= cd->mcodeptr); */
735 cd->lastmcodeptr = cd->mcodeptr + PATCHER_CALL_SIZE;
737 #endif /* defined(ENABLE_REPLACEMENT) */
740 /* codegen_finish **************************************************************
742 Finishes the code generation. A new memory, large enough for both
743 data and code, is allocated and data and code are copied together
744 to their final layout, unresolved jumps are resolved, ...
746 *******************************************************************************/
748 void codegen_finish(jitdata *jd)
753 #if defined(ENABLE_INTRP)
762 /* get required compiler data */
767 /* prevent compiler warning */
769 #if defined(ENABLE_INTRP)
773 /* calculate the code length */
775 mcodelen = (s4) (cd->mcodeptr - cd->mcodebase);
777 #if defined(ENABLE_THREADS)
778 extralen = sizeof(critical_section_node_t) * cd->threadcritcount;
783 #if defined(ENABLE_STATISTICS)
785 count_code_len += mcodelen;
786 count_data_len += cd->dseglen;
790 alignedmcodelen = MEMORY_ALIGN(mcodelen, MAX_ALIGN);
792 #if defined(ENABLE_INTRP)
794 ncodelen = cd->ncodeptr - cd->ncodebase;
796 ncodelen = 0; /* avoid compiler warning */
800 cd->dseglen = MEMORY_ALIGN(cd->dseglen, MAX_ALIGN);
801 alignedlen = alignedmcodelen + cd->dseglen;
803 #if defined(ENABLE_INTRP)
805 alignedlen += ncodelen;
809 /* allocate new memory */
811 code->mcodelength = mcodelen + cd->dseglen;
812 code->mcode = CNEW(u1, alignedlen + extralen);
814 /* set the entrypoint of the method */
816 assert(code->entrypoint == NULL);
817 code->entrypoint = epoint = (code->mcode + cd->dseglen);
819 /* fill the data segment (code->entrypoint must already be set!) */
823 /* copy code to the new location */
825 MCOPY((void *) code->entrypoint, cd->mcodebase, u1, mcodelen);
827 #if defined(ENABLE_INTRP)
828 /* relocate native dynamic superinstruction code (if any) */
831 cd->mcodebase = code->entrypoint;
834 u1 *ncodebase = code->mcode + cd->dseglen + alignedmcodelen;
836 MCOPY((void *) ncodebase, cd->ncodebase, u1, ncodelen);
838 /* flush the instruction and data caches */
840 md_cacheflush(ncodebase, ncodelen);
842 /* set some cd variables for dynamic_super_rerwite */
844 cd->ncodebase = ncodebase;
847 cd->ncodebase = NULL;
850 dynamic_super_rewrite(cd);
854 /* jump table resolving */
856 for (jr = cd->jumpreferences; jr != NULL; jr = jr->next)
857 *((functionptr *) ((ptrint) epoint + jr->tablepos)) =
858 (functionptr) ((ptrint) epoint + (ptrint) jr->target->mpc);
860 /* line number table resolving */
866 for (lr = cd->linenumberreferences; lr != NULL; lr = lr->next) {
868 target = lr->targetmpc;
869 /* if the entry contains an mcode pointer (normal case), resolve it */
870 /* (see doc/inlining_stacktrace.txt for details) */
871 if (lr->linenumber >= -2) {
872 target += (ptrint) epoint;
874 *((functionptr *) ((ptrint) epoint + (ptrint) lr->tablepos)) =
875 (functionptr) target;
878 *((functionptr *) ((ptrint) epoint + cd->linenumbertablestartpos)) =
879 (functionptr) ((ptrint) epoint + cd->linenumbertab);
881 *((ptrint *) ((ptrint) epoint + cd->linenumbertablesizepos)) = lrtlen;
884 #if defined(ENABLE_REPLACEMENT)
885 /* replacement point resolving */
890 code->replacementstubs += (ptrint) epoint;
892 rp = code->rplpoints;
893 for (i=0; i<code->rplpointcount; ++i, ++rp) {
894 rp->pc = (u1*) ((ptrint) epoint + (ptrint) rp->pc);
897 #endif /* defined(ENABLE_REPLACEMENT) */
899 /* add method into methodtree to find the entrypoint */
901 codegen_insertmethod(code->entrypoint, code->entrypoint + mcodelen);
903 #if defined(__I386__) || defined(__X86_64__) || defined(__XDSPCORE__) || defined(__M68K__) || defined(ENABLE_INTRP)
904 /* resolve data segment references */
906 dseg_resolve_datareferences(jd);
909 #if defined(ENABLE_THREADS)
911 critical_section_node_t *n = (critical_section_node_t *) ((ptrint) code->mcode + alignedlen);
913 codegen_critical_section_t *nt = cd->threadcrit;
915 for (i = 0; i < cd->threadcritcount; i++) {
916 n->mcodebegin = (u1 *) (ptrint) code->mcode + nt->mcodebegin;
917 n->mcodeend = (u1 *) (ptrint) code->mcode + nt->mcodeend;
918 n->mcoderestart = (u1 *) (ptrint) code->mcode + nt->mcoderestart;
919 critical_register_critical_section(n);
926 /* flush the instruction and data caches */
928 md_cacheflush(code->mcode, code->mcodelength);
932 /* codegen_createnativestub ****************************************************
934 Wrapper for createnativestub.
937 the codeinfo representing the stub code.
939 *******************************************************************************/
941 codeinfo *codegen_createnativestub(functionptr f, methodinfo *m)
950 /* mark dump memory */
952 dumpsize = dump_size();
957 jd->cd = DNEW(codegendata);
958 jd->rd = DNEW(registerdata);
961 /* Allocate codeinfo memory from the heap as we need to keep them. */
963 jd->code = code_codeinfo_new(m); /* XXX check allocation */
965 /* get required compiler data */
969 /* set the flags for the current JIT run */
971 #if defined(ENABLE_PROFILING)
973 jd->flags |= JITDATA_FLAG_INSTRUMENT;
977 jd->flags |= JITDATA_FLAG_VERBOSECALL;
979 /* setup code generation stuff */
981 #if defined(ENABLE_JIT)
982 # if defined(ENABLE_INTRP)
990 /* create new method descriptor with additional native parameters */
993 nativeparams = (m->flags & ACC_STATIC) ? 2 : 1;
995 nmd = (methoddesc *) DMNEW(u1, sizeof(methoddesc) - sizeof(typedesc) +
996 md->paramcount * sizeof(typedesc) +
997 nativeparams * sizeof(typedesc));
999 nmd->paramcount = md->paramcount + nativeparams;
1001 nmd->params = DMNEW(paramdesc, nmd->paramcount);
1003 nmd->paramtypes[0].type = TYPE_ADR; /* add environment pointer */
1005 if (m->flags & ACC_STATIC)
1006 nmd->paramtypes[1].type = TYPE_ADR; /* add class pointer */
1008 MCOPY(nmd->paramtypes + nativeparams, md->paramtypes, typedesc,
1011 #if defined(ENABLE_JIT)
1012 # if defined(ENABLE_INTRP)
1015 /* pre-allocate the arguments for the native ABI */
1017 md_param_alloc_native(nmd);
1020 /* generate the code */
1022 #if defined(ENABLE_JIT)
1023 # if defined(ENABLE_INTRP)
1025 code->entrypoint = intrp_createnativestub(f, jd, nmd);
1028 code->entrypoint = createnativestub(f, jd, nmd);
1030 code->entrypoint = intrp_createnativestub(f, jd, nmd);
1033 #if defined(ENABLE_STATISTICS)
1035 count_nstub_len += code->mcodelength;
1038 #if !defined(NDEBUG)
1039 /* disassemble native stub */
1041 if (opt_shownativestub) {
1042 #if defined(ENABLE_DISASSEMBLER)
1043 codegen_disassemble_nativestub(m,
1044 (u1 *) (ptrint) code->entrypoint,
1045 (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
1048 /* show data segment */
1050 if (opt_showddatasegment)
1053 #endif /* !defined(NDEBUG) */
1055 /* release memory */
1057 dump_release(dumpsize);
1059 /* return native stub code */
1065 /* codegen_disassemble_nativestub **********************************************
1067 Disassembles the generated native stub.
1069 *******************************************************************************/
1071 #if defined(ENABLE_DISASSEMBLER)
1072 void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end)
1074 printf("Native stub: ");
1075 utf_fprint_printable_ascii_classname(stdout, m->class->name);
1077 utf_fprint_printable_ascii(stdout, m->name);
1078 utf_fprint_printable_ascii(stdout, m->descriptor);
1079 printf("\n\nLength: %d\n\n", (s4) (end - start));
1081 DISASSEMBLE(start, end);
1086 /* codegen_start_native_call ***************************************************
1088 Prepares the stuff required for a native (JNI) function call:
1090 - adds a stackframe info structure to the chain, for stacktraces
1091 - prepares the local references table on the stack
1093 The layout of the native stub stackframe should look like this:
1095 +---------------------------+ <- SP (of parent Java function)
1097 +---------------------------+
1099 | stackframe info structure |
1101 +---------------------------+
1103 | local references table |
1105 +---------------------------+
1107 | arguments (if any) |
1109 +---------------------------+ <- SP (native stub)
1111 *******************************************************************************/
1113 void codegen_start_native_call(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
1115 stackframeinfo *sfi;
1116 localref_table *lrt;
1118 /* get data structures from stack */
1120 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1121 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1122 sizeof(localref_table));
1124 /* add a stackframeinfo to the chain */
1126 stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
1128 #if defined(ENABLE_JAVASE)
1129 /* add current JNI local references table to this thread */
1131 lrt->capacity = LOCALREFTABLE_CAPACITY;
1133 lrt->localframes = 1;
1134 lrt->prev = LOCALREFTABLE;
1136 /* clear the references array (memset is faster the a for-loop) */
1138 MSET(lrt->refs, 0, java_objectheader*, LOCALREFTABLE_CAPACITY);
1140 LOCALREFTABLE = lrt;
1143 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
1144 /* set the native world flag */
1146 THREADOBJECT->flags |= THREAD_FLAG_IN_NATIVE;
1151 /* codegen_finish_native_call **************************************************
1153 Removes the stuff required for a native (JNI) function call.
1154 Additionally it checks for an exceptions and in case, get the
1155 exception object and clear the pointer.
1157 *******************************************************************************/
1159 java_objectheader *codegen_finish_native_call(u1 *datasp)
1161 stackframeinfo *sfi;
1162 stackframeinfo **psfi;
1163 localref_table *lrt;
1164 localref_table *plrt;
1166 java_objectheader *e;
1168 /* get data structures from stack */
1170 sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
1171 lrt = (localref_table *) (datasp - sizeof(stackframeinfo) -
1172 sizeof(localref_table));
1174 #if defined(ENABLE_THREADS) && defined(ENABLE_GC_CACAO)
1175 /* clear the native world flag */
1177 THREADOBJECT->flags &= ~THREAD_FLAG_IN_NATIVE;
1180 /* remove current stackframeinfo from chain */
1182 psfi = &STACKFRAMEINFO;
1186 #if defined(ENABLE_JAVASE)
1187 /* release JNI local references tables for this thread */
1189 lrt = LOCALREFTABLE;
1191 /* release all current local frames */
1193 for (localframes = lrt->localframes; localframes >= 1; localframes--) {
1194 /* get previous frame */
1198 /* Clear all reference entries (only for tables allocated on
1201 if (localframes > 1)
1202 MSET(&lrt->refs[0], 0, java_objectheader*, lrt->capacity);
1206 /* set new local references table */
1211 /* now store the previous local frames in the thread structure */
1213 LOCALREFTABLE = lrt;
1216 /* get the exception and return it */
1218 e = exceptions_get_and_clear_exception();
1224 /* removecompilerstub **********************************************************
1226 Deletes a compilerstub from memory (simply by freeing it).
1228 *******************************************************************************/
1230 void removecompilerstub(u1 *stub)
1232 /* pass size 1 to keep the intern function happy */
1234 CFREE((void *) stub, 1);
1238 /* removenativestub ************************************************************
1240 Removes a previously created native-stub from memory.
1242 *******************************************************************************/
1244 void removenativestub(u1 *stub)
1246 /* pass size 1 to keep the intern function happy */
1248 CFREE((void *) stub, 1);
1252 /* codegen_reg_of_var **********************************************************
1254 This function determines a register, to which the result of an
1255 operation should go, when it is ultimatively intended to store the
1256 result in pseudoregister v. If v is assigned to an actual
1257 register, this register will be returned. Otherwise (when v is
1258 spilled) this function returns tempregnum. If not already done,
1259 regoff and flags are set in the stack location.
1261 On ARM we have to check if a long/double variable is splitted
1262 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1263 register of v for LOW_REG and the tempregnum for HIGH_REG in such
1264 cases. (michi 2005/07/24)
1266 *******************************************************************************/
1268 s4 codegen_reg_of_var(u2 opcode, varinfo *v, s4 tempregnum)
1272 /* Do we have to generate a conditional move? Yes, then always
1273 return the temporary register. The real register is identified
1274 during the store. */
1276 if (opcode & ICMD_CONDITION_MASK)
1280 if (!(v->flags & INMEMORY)) {
1281 #if defined(__ARM__) && defined(__ARMEL__)
1282 if (IS_2_WORD_TYPE(v->type) && (GET_HIGH_REG(v->vv.regoff) == REG_SPLIT))
1283 return PACK_REGS(GET_LOW_REG(v->vv.regoff),
1284 GET_HIGH_REG(tempregnum));
1286 #if defined(__ARM__) && defined(__ARMEB__)
1287 if (IS_2_WORD_TYPE(v->type) && (GET_LOW_REG(v->vv.regoff) == REG_SPLIT))
1288 return PACK_REGS(GET_LOW_REG(tempregnum),
1289 GET_HIGH_REG(v->vv.regoff));
1291 return v->vv.regoff;
1294 #if defined(ENABLE_STATISTICS)
1296 count_spills_read++;
1302 /* codegen_reg_of_dst **********************************************************
1304 This function determines a register, to which the result of an
1305 operation should go, when it is ultimatively intended to store the
1306 result in iptr->dst.var. If dst.var is assigned to an actual
1307 register, this register will be returned. Otherwise (when it is
1308 spilled) this function returns tempregnum. If not already done,
1309 regoff and flags are set in the stack location.
1311 On ARM we have to check if a long/double variable is splitted
1312 across reg/stack (HIGH_REG == REG_SPLIT). We return the actual
1313 register of dst.var for LOW_REG and the tempregnum for HIGH_REG in such
1314 cases. (michi 2005/07/24)
1316 *******************************************************************************/
1318 s4 codegen_reg_of_dst(jitdata *jd, instruction *iptr, s4 tempregnum)
1320 return codegen_reg_of_var(iptr->opc, VAROP(iptr->dst), tempregnum);
1324 #if defined(ENABLE_THREADS)
1325 void codegen_threadcritrestart(codegendata *cd, int offset)
1327 cd->threadcritcurrent.mcoderestart = offset;
1331 void codegen_threadcritstart(codegendata *cd, int offset)
1333 cd->threadcritcurrent.mcodebegin = offset;
1337 void codegen_threadcritstop(codegendata *cd, int offset)
1339 cd->threadcritcurrent.next = cd->threadcrit;
1340 cd->threadcritcurrent.mcodeend = offset;
1341 cd->threadcrit = DNEW(codegen_critical_section_t);
1342 *(cd->threadcrit) = cd->threadcritcurrent;
1343 cd->threadcritcount++;
1349 * These are local overrides for various environment variables in Emacs.
1350 * Please do not remove this and leave it at the end of the file, where
1351 * Emacs will automagically detect them.
1352 * ---------------------------------------------------------------------
1355 * indent-tabs-mode: t
1359 * vim:noexpandtab:sw=4:ts=4: