* src/vm/jit/codegen-common.c (codegen_disassemble_stub): Renamed and works with
[cacao.git] / src / vm / jit / codegen-common.c
index f36e8438c01f5510b0fceabceca9c12d9093133d..8a04bea20ebb5691c4520b1dea3534b9a95a5feb 100644 (file)
@@ -1194,12 +1194,9 @@ u1 *codegen_generate_stub_compiler(methodinfo *m)
 
    Wrapper for codegen_emit_stub_builtin.
 
-   Returns:
-       Pointer to the entrypoint of the stub.
-
 *******************************************************************************/
 
-void codegen_generate_stub_builtin(builtintable_entry *bte)
+void codegen_generate_stub_builtin(methodinfo *m, builtintable_entry *bte)
 {
 #if defined(__ARM__) || defined(__ALPHA__) || defined(__I386__) || defined(__M68K__) || defined(__POWERPC__) || defined(__SPARC64__) || defined(__X86_64__)
        jitdata  *jd;
@@ -1212,14 +1209,14 @@ void codegen_generate_stub_builtin(builtintable_entry *bte)
 
        jd = DNEW(jitdata);
 
-       jd->m     = NULL;
+       jd->m     = m;
        jd->cd    = DNEW(codegendata);
        jd->rd    = NULL;
        jd->flags = 0;
 
        /* Allocate codeinfo memory from the heap as we need to keep them. */
 
-       jd->code  = code_codeinfo_new(NULL);
+       jd->code  = code_codeinfo_new(m); /* XXX check allocation */
 
        /* get required compiler data */
 
@@ -1235,18 +1232,8 @@ void codegen_generate_stub_builtin(builtintable_entry *bte)
 # if defined(ENABLE_INTRP)
        if (!opt_intrp) {
 # endif
-               /* XXX This is only a hack for builtin_arraycopy and should be done better! */
-               if (bte->flags & BUILTINTABLE_FLAG_EXCEPTION) {
-                       assert(bte->md->returntype.type == TYPE_VOID);
-                       bte->md->returntype.type = TYPE_INT;
-               }
-
-               codegen_emit_stub_builtin(jd, bte);
-
-               /* XXX see above */
-               if (bte->flags & BUILTINTABLE_FLAG_EXCEPTION) {
-                       bte->md->returntype.type = TYPE_VOID;
-               }
+               assert(bte->fp != NULL);
+               codegen_emit_stub_native(jd, bte->md, bte->fp);
 # if defined(ENABLE_INTRP)
        }
 # endif
@@ -1265,6 +1252,21 @@ void codegen_generate_stub_builtin(builtintable_entry *bte)
                size_stub_native += code->mcodelength;
 #endif
 
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
+       /* disassemble native stub */
+
+       if (opt_DisassembleStubs) {
+               codegen_disassemble_stub(m,
+                                                                (u1 *) (ptrint) code->entrypoint,
+                                                                (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
+
+               /* show data segment */
+
+               if (opt_showddatasegment)
+                       dseg_display(jd);
+       }
+#endif /* !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) */
+
        /* release memory */
 
        dump_release(dumpsize);
@@ -1384,19 +1386,17 @@ codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
                size_stub_native += code->mcodelength;
 #endif
 
-#if !defined(NDEBUG)
+#if !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER)
        /* disassemble native stub */
 
-       if (opt_shownativestub) {
-#if defined(ENABLE_DEBUG_FILTER)
+       if (opt_DisassembleStubs) {
+# if defined(ENABLE_DEBUG_FILTER)
                if (m->filtermatches & SHOW_FILTER_FLAG_SHOW_METHOD)
-#endif
+# endif
                {
-#if defined(ENABLE_DISASSEMBLER)
-                       codegen_disassemble_nativestub(m,
-                                                                                  (u1 *) (ptrint) code->entrypoint,
-                                                                                  (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
-#endif
+                       codegen_disassemble_stub(m,
+                                                                        (u1 *) (ptrint) code->entrypoint,
+                                                                        (u1 *) (ptrint) code->entrypoint + (code->mcodelength - jd->cd->dseglen));
 
                        /* show data segment */
 
@@ -1404,7 +1404,7 @@ codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
                                dseg_display(jd);
                }
        }
-#endif /* !defined(NDEBUG) */
+#endif /* !defined(NDEBUG) && defined(ENABLE_DISASSEMBLER) */
 
        /* release memory */
 
@@ -1418,81 +1418,28 @@ codeinfo *codegen_generate_stub_native(methodinfo *m, functionptr f)
 
 /* codegen_disassemble_nativestub **********************************************
 
-   Disassembles the generated native stub.
+   Disassembles the generated builtin or native stub.
 
 *******************************************************************************/
 
 #if defined(ENABLE_DISASSEMBLER)
-void codegen_disassemble_nativestub(methodinfo *m, u1 *start, u1 *end)
+void codegen_disassemble_stub(methodinfo *m, u1 *start, u1 *end)
 {
-       printf("Native stub: ");
-       utf_fprint_printable_ascii_classname(stdout, m->class->name);
+       printf("Stub code: ");
+       if (m->class != NULL)
+               utf_fprint_printable_ascii_classname(stdout, m->class->name);
+       else
+               printf("NULL");
        printf(".");
        utf_fprint_printable_ascii(stdout, m->name);
        utf_fprint_printable_ascii(stdout, m->descriptor);
-       printf("\n\nLength: %d\n\n", (s4) (end - start));
+       printf("\nLength: %d\n\n", (s4) (end - start));
 
        DISASSEMBLE(start, end);
 }
 #endif
 
 
-/* codegen_stub_builtin_enter **************************************************
-
-   Prepares the stuff required for a builtin function call:
-
-   - adds a stackframe info structure to the chain, for stacktraces
-
-   The layout of the builtin stub stackframe should look like this:
-
-   +---------------------------+ <- SP (of parent Java function)
-   | return address            |
-   +---------------------------+
-   |                           |
-   | stackframe info structure |
-   |                           |
-   +---------------------------+
-   |                           |
-   | arguments (if any)        |
-   |                           |
-   +---------------------------+ <- SP (native stub)
-
-*******************************************************************************/
-
-void codegen_stub_builtin_enter(u1 *datasp, u1 *pv, u1 *sp, u1 *ra)
-{
-       stackframeinfo *sfi;
-
-       /* get data structures from stack */
-
-       sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
-
-       /* add a stackframeinfo to the chain */
-
-       stacktrace_create_native_stackframeinfo(sfi, pv, sp, ra);
-}
-
-
-/* codegen_stub_builtin_exit ***************************************************
-
-   Removes the stuff required for a builtin function call.
-
-*******************************************************************************/
-
-void codegen_stub_builtin_exit(u1 *datasp)
-{
-       stackframeinfo *sfi;
-
-       /* get data structures from stack */
-
-       sfi = (stackframeinfo *) (datasp - sizeof(stackframeinfo));
-
-       /* remove current stackframeinfo from chain */
-
-       stacktrace_remove_stackframeinfo(sfi);
-}
-
-
 /* codegen_start_native_call ***************************************************
 
    Prepares the stuff required for a native (JNI) function call:
@@ -1544,14 +1491,12 @@ java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv)
 
        code      = *((codeinfo **) (pv + CodeinfoPointer));
        framesize = *((int32_t *)   (pv + FrameSize));
-
        assert(code);
        assert(framesize > sizeof(stackframeinfo) + sizeof(localref_table));
 
        /* get the methodinfo */
 
        m = code->m;
-
        assert(m);
 
        /* calculate needed values */
@@ -1586,7 +1531,7 @@ java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv)
 #endif
 
 #if !defined(NDEBUG)
-# if defined(__POWERPC__)
+# if defined(__POWERPC__) || defined (__X86_64__)
        /* print the call-trace if necesarry */
 
        if (opt_TraceJavaCalls)
@@ -1616,7 +1561,7 @@ java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv)
 
        stacktrace_create_native_stackframeinfo(sfi, pv, javasp, javara);
 
-       /* return a wrapped classinfo for static methods */
+       /* return a wrapped classinfo for static native methods */
 
        if (m->flags & ACC_STATIC)
                return LLNI_classinfo_wrap(m->class);
@@ -1633,11 +1578,59 @@ java_handle_t *codegen_start_native_call(u1 *currentsp, u1 *pv)
 
 *******************************************************************************/
 
-java_object_t *codegen_finish_native_call(u1 *datasp)
+java_object_t *codegen_finish_native_call(u1 *currentsp, u1 *pv)
 {
        stackframeinfo *sfi;
        java_handle_t  *e;
        java_object_t  *o;
+       codeinfo       *code;
+       methodinfo     *m;
+       int32_t         framesize;
+
+       uint8_t  *datasp;
+       uint64_t *ret_regs;
+
+       /* get information from method header */
+
+       code      = *((codeinfo **) (pv + CodeinfoPointer));
+       framesize = *((int32_t *)   (pv + FrameSize));
+       assert(code);
+
+       /* get the methodinfo */
+
+       m = code->m;
+       assert(m);
+
+       /* calculate needed values */
+
+#if defined(__ALPHA__) || defined(__ARM__)
+       datasp   = currentsp + framesize - SIZEOF_VOID_P;
+       ret_regs = (uint64_t *) currentsp;
+#elif defined(__MIPS__) || defined(__S390__)
+       /* MIPS and S390 always uses 8 bytes to store the RA */
+       datasp   = currentsp + framesize - 8;
+#elif defined(__I386__)
+       datasp   = currentsp + framesize;
+       ret_regs = (uint64_t *) (currentsp + 2 * SIZEOF_VOID_P);
+#elif defined (__M68K__) || defined (__X86_64__)
+       datasp   = currentsp + framesize;
+       ret_regs = (uint64_t *) currentsp;
+#elif defined(__POWERPC__) || defined(__POWERPC64__)
+       datasp   = currentsp + framesize;
+       ret_regs = (uint64_t *) (currentsp + LA_SIZE + 2 * SIZEOF_VOID_P);
+#else
+       vm_abort("codegen_finish_native_call: unsupported architecture");
+#endif
+
+
+#if !defined(NDEBUG)
+# if defined(__POWERPC__) || defined (__X86_64__)
+       /* print the call-trace if necesarry */
+
+       if (opt_TraceJavaCalls)
+               trace_java_call_exit(m, ret_regs);
+# endif
+#endif
 
        /* get data structures from stack */
 
@@ -1647,6 +1640,8 @@ java_object_t *codegen_finish_native_call(u1 *datasp)
 
        stacktrace_remove_stackframeinfo(sfi);
 
+       /* XXX unfill lrt here!!! */
+
        /* get and unwrap the exception */
        /* ATTENTION: do the this _after_ the stackframeinfo was
        removed but _before_ the localref_table gets removed! */