#include "toba.h"

#include <stdio.h>
#include "java_lang_ClassLoader.h"
#ifdef OPTION_JIT
#include "toba_runtime_ClassRT.h"
#include "toba_runtime_SystemClassLoader.h"
#include "toba_runtime_CodeGen.h"
#endif /* OPTION_JIT */
#include "runtime.h"
#include "sthreads.h"

extern struct in_toba_runtime_SystemClassLoader *system_loader;

/* java/lang/ClassLoader init ()V */
Void init__GmRGw(Object Harg1)
/*ARGSUSED*/
{
}

/* java/lang/ClassLoader defineClass0 ([BII)Ljava/lang/Class; */
Object defineClass0_abii_v0LzF(Object Harg1, Object Harg2, Int arg3, Int arg4)
/*ARGSUSED*/
{
#ifdef OPTION_JIT
    struct in_java_lang_Class *cl;
    struct in_toba_runtime_CodeGen *generator;

    initclass(&cl_toba_runtime_ClassRT.C);
    initclass(&cl_toba_runtime_CodeGen.C);

    generator = cl_toba_runtime_CodeGen.V.generator;
    if (generator == 0) {
       throwInternalError ("Dynamic loading is not supported in this system");
    }

    cl = generator->class->M.defineClass_Cabii_B62K6.f(generator, Harg1, Harg2, arg3, arg4);
    return cl;
#else /* OPTION_JIT */
    throwInternalError ("Dynamic loading is not supported in this system");
#endif /* OPTION_JIT */
}

/* java/lang/ClassLoader resolveClass0 (Ljava/lang/Class;)V */
Void resolveClass0_C_lzKt7(Object Harg1, Object Harg2)
/*ARGSUSED*/
{
#ifdef OPTION_JIT
#if 0 /* UNUSED */
    struct in_java_lang_ClassLoader *l = Harg1;
#endif /* 0 */
    struct in_java_lang_Class *cl = Harg2;
    struct in_toba_runtime_CodeGen *generator;
    Class rcl;

    initclass(&cl_toba_runtime_ClassRT.C);
    initclass(&cl_toba_runtime_CodeGen.C);

    /* Find what class this is, so we can tell whether we need to
     * resolve it.  For example, native compiled methods were resolved
     * when they were translated. */
    rcl = find_class (cl);
#define TRY_NO_RESOLVE 0
#if TRY_NO_RESOLVE
    if (0 <= rcl->needinit) {
        /* This has already been resolved. */
#if 0
        fprintf (stderr, "resolveClass0: skipping already-resolved %s at %x: %d\n",
                 cstring (rcl->name), rcl, rcl->needinit);
#endif
        return;
    }
#if 0
    fprintf (stderr, "resolveClass0: resolving on %s at %x\n",
             cstring (rcl->name), rcl);
#endif
    rcl->needinit = 1;
#endif /* TRY_NO_RESOLVE */
    generator = cl_toba_runtime_CodeGen.V.generator;
    if (generator == 0) {
        throwInternalError ("Dynamic loading is not supported in this system");
    }

    generator->class->M.resolveClass_CC_3feVn.f(generator, Harg2, Harg1);
    return;
#else /* OPTION_JIT */
    throwInternalError ("Dynamic loading is not supported in this system");
#endif /* OPTION_JIT */
}

/* java/lang/ClassLoader findSystemClass0 (Ljava/lang/String;)Ljava/lang/Class; */
Object findSystemClass0_S_x1IFk(Object Harg1, Object Harg2)
/*ARGSUSED*/
{
#ifdef OPTION_JIT
    /* This is the main routine of anyclassloader.findSystemClass.  We
     * just have to load the class, not resolve it, so g'head and call
     * toba.runtime.SystemClassLoader.loadClass.  That will figure out
     * where to find the necessaries. */
    return system_loader->class->
	M.loadClass_Sz_YwO8n.f(system_loader,Harg2,0); 
#else /* OPTION_JIT */
    throwInternalError ("Dynamic loading is not supported in this system");
#endif /* OPTION_JIT */
}

