dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[dotgnu-pnet-commits] pnet ./ChangeLog engine/call.c engine/engine.h ...


From: Klaus Treichel
Subject: [dotgnu-pnet-commits] pnet ./ChangeLog engine/call.c engine/engine.h ...
Date: Tue, 27 Dec 2005 20:07:24 +0000

CVSROOT:        /cvsroot/dotgnu-pnet
Module name:    pnet
Branch:         
Changes by:     Klaus Treichel <address@hidden> 05/12/27 20:07:24

Modified files:
        .              : ChangeLog 
        engine         : call.c engine.h jitc.c jitc.h jitc_call.c 

Log message:
        Add more jit coder stuff.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/ChangeLog.diff?tr1=1.3257&tr2=1.3258&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/engine/call.c.diff?tr1=1.32&tr2=1.33&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/engine/engine.h.diff?tr1=1.108&tr2=1.109&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/engine/jitc.c.diff?tr1=1.3&tr2=1.4&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/engine/jitc.h.diff?tr1=1.2&tr2=1.3&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/dotgnu-pnet/pnet/engine/jitc_call.c.diff?tr1=1.1&tr2=1.2&r1=text&r2=text

Patches:
Index: pnet/ChangeLog
diff -u pnet/ChangeLog:1.3257 pnet/ChangeLog:1.3258
--- pnet/ChangeLog:1.3257       Sat Dec 24 11:34:56 2005
+++ pnet/ChangeLog      Tue Dec 27 20:07:24 2005
@@ -1,3 +1,14 @@
+2005-12-27  Klaus Treichel  <address@hidden>
+
+       * engine/call.c: implement _ILCallPackVaParams, _ILCallPackVParams and
+       _ILCallMethod for the jit coder.
+
+       * engine/engine.h: add the prototypes for _ILCallPackVaParams and
+       _ILCallPackVParams and the definition of ILCallPackFunc for the jit 
coder.
+
+       * engine/jitc.c, engine/jitc.h, engine/jit_call.h: add some more work 
on the
+       jit coder.
+
 2005-12-24  Klaus Treichel  <address@hidden>
 
        * engine/jitc.c, engine/jitc_setup.c: Add some more work on the jit 
coder.
Index: pnet/engine/call.c
diff -u pnet/engine/call.c:1.32 pnet/engine/call.c:1.33
--- pnet/engine/call.c:1.32     Tue Aug 23 10:45:52 2005
+++ pnet/engine/call.c  Tue Dec 27 20:07:24 2005
@@ -99,6 +99,160 @@
                                } \
                        } while (0)
 
+#ifdef IL_USE_JIT
+int _ILCallPackVaParams(ILExecThread *thread, ILType *signature,
+                                           void *argBuffer, void **jitArgs, 
void *userData)
+{
+       VA_LIST va;
+       ILExecValue *args = (ILExecValue *)userData;
+       ILUInt32 param, numParams;
+       ILType *paramType;
+
+       /* TODO */
+       /* What i'm doing here right now might be not portable at all. */
+       /* We might need to create a local buffer for the args and get the */
+       /* parameter pointers from this buffer. */
+
+       /* Copy the incoming "va_list" value */
+       ILMemCpy(&va, userData, sizeof(VA_LIST));
+
+       numParams = ILTypeNumParams(signature);
+       for(param = 1; param <= numParams; ++param)
+       {
+               paramType = ILTypeGetEnumType(ILTypeGetParam(signature, param));
+               if(ILType_IsPrimitive(paramType))
+               {
+                       /* Process a primitive value */
+                       switch(ILType_ToElement(paramType))
+                       {
+                               case IL_META_ELEMTYPE_VOID:             break;
+
+                               case IL_META_ELEMTYPE_BOOLEAN:
+                               case IL_META_ELEMTYPE_I1:
+                               case IL_META_ELEMTYPE_I2:
+                               case IL_META_ELEMTYPE_I4:
+                       #ifdef IL_NATIVE_INT32
+                               case IL_META_ELEMTYPE_I:
+                       #endif
+                               {
+                                       *jitArgs = argBuffer;
+                                       *((ILVaInt *)argBuffer) = VA_ARG(va, 
ILVaInt);
+                                       argBuffer += sizeof(ILVaInt);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_U1:
+                               case IL_META_ELEMTYPE_U2:
+                               case IL_META_ELEMTYPE_CHAR:
+                               case IL_META_ELEMTYPE_U4:
+                       #ifdef IL_NATIVE_INT32
+                               case IL_META_ELEMTYPE_U:
+                       #endif
+                               {
+                                       *jitArgs = argBuffer;
+                                       *((ILVaUInt *)argBuffer) = VA_ARG(va, 
ILVaUInt);
+                                       argBuffer += sizeof(ILVaUInt);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_I8:
+                       #ifdef IL_NATIVE_INT64
+                               case IL_META_ELEMTYPE_I:
+                               case IL_META_ELEMTYPE_U:
+                       #endif
+                               {
+                                       *jitArgs = argBuffer;
+                                       *((ILInt64 *)argBuffer) = VA_ARG(va, 
ILInt64);
+                                       argBuffer += sizeof(ILInt64);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_U8:
+                       #ifdef IL_NATIVE_INT64
+                               case IL_META_ELEMTYPE_U:
+                       #endif
+                               {
+                                       *jitArgs = argBuffer;
+                                       *((ILUInt64 *)argBuffer) = VA_ARG(va, 
ILUInt64);
+                                       argBuffer += sizeof(ILUInt64);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_R4:
+                               case IL_META_ELEMTYPE_R8:
+                               case IL_META_ELEMTYPE_R:
+                               {
+                                       *jitArgs = argBuffer;
+                                       *((ILVaDouble *)argBuffer) = VA_ARG(va, 
ILVaDouble);
+                                       argBuffer += sizeof(ILUInt64);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_TYPEDBYREF:
+                               {
+                                       /* We assume that typed references are 
passed to us
+                                          as a pointer to a temporary typedref 
structure */
+                                       *jitArgs = argBuffer;
+                                       *((void **)argBuffer) = VA_ARG(va, void 
*);
+                                       argBuffer += sizeof(void *);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+                       }
+               }
+               else if(ILType_IsClass(paramType))
+               {
+                       /* Process an object reference */
+                       *jitArgs = argBuffer;
+                       *((ILObject **)argBuffer) = VA_ARG(va, ILObject *);
+                       argBuffer += sizeof(ILObject *);
+                       ++args;
+                       ++jitArgs;
+               }
+               else if(ILType_IsValueType(paramType))
+               {
+                       /* Process a value type: we assume that the caller has
+                          put the value into a temporary location and then
+                          passed a pointer to the temporary to us */
+                       *jitArgs = (void *)(VA_ARG(va, void *));
+                       ++args;
+                       ++jitArgs;
+               }
+               else if(paramType != 0 && ILType_IsComplex(paramType) &&
+                               ILType_Kind(paramType) == IL_TYPE_COMPLEX_BYREF)
+               {
+                       /* Process a value that is being passed by reference */
+                       *jitArgs = argBuffer;
+                       *((void **)argBuffer) = VA_ARG(va, void *);
+                       argBuffer += sizeof(void *);
+                       ++args;
+                       ++jitArgs;
+               }
+               else
+               {
+                       /* Assume that everything else is an object reference */
+                       *jitArgs = argBuffer;
+                       *((ILObject **)argBuffer) = VA_ARG(va, ILObject *);
+                       argBuffer += sizeof(ILObject *);
+                       ++args;
+                       ++jitArgs;
+               }
+       }
+
+       return 0;
+}
+#else
 int _ILCallPackVaParams(ILExecThread *thread, ILMethod *method,
                                            int isCtor, void *_this, void 
*userData)
 {
@@ -252,7 +406,134 @@
        thread->stackTop = stacktop;
        return 0;
 }
+#endif
 
+#ifdef IL_USE_JIT
+int _ILCallPackVParams(ILExecThread *thread, ILType *signature,
+                                          void *argBuffer, void **jitArgs, 
void *userData)
+{
+       ILExecValue *args = (ILExecValue *)userData;
+       ILUInt32 param, numParams;
+       ILType *paramType;
+
+       /* Store pointers to the args in the jitArgs Array. */
+       numParams = ILTypeNumParams(signature);
+       for(param = 1; param <= numParams; ++param)
+       {
+               paramType = ILTypeGetEnumType(ILTypeGetParam(signature, param));
+               if(ILType_IsPrimitive(paramType))
+               {
+                       /* Process a primitive value */
+                       switch(ILType_ToElement(paramType))
+                       {
+                               case IL_META_ELEMTYPE_VOID:             break;
+
+                               case IL_META_ELEMTYPE_BOOLEAN:
+                               case IL_META_ELEMTYPE_I1:
+                               case IL_META_ELEMTYPE_I2:
+                               case IL_META_ELEMTYPE_CHAR:
+                               case IL_META_ELEMTYPE_I4:
+                       #ifdef IL_NATIVE_INT32
+                               case IL_META_ELEMTYPE_I:
+                       #endif
+                               {
+                                       *jitArgs = &(args->int32Value);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_U1:
+                               case IL_META_ELEMTYPE_U2:
+                               case IL_META_ELEMTYPE_U4:
+                       #ifdef IL_NATIVE_INT32
+                               case IL_META_ELEMTYPE_U:
+                       #endif
+                               {
+                                       *jitArgs = &(args->uint32Value);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_I8:
+                       #ifdef IL_NATIVE_INT64
+                               case IL_META_ELEMTYPE_I:
+                       #endif
+                               {
+                                       *jitArgs = &(args->int64Value);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_U8:
+                       #ifdef IL_NATIVE_INT64
+                               case IL_META_ELEMTYPE_U:
+                       #endif
+                               {
+                                       *jitArgs = &(args->uint64Value);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_R4:
+                               case IL_META_ELEMTYPE_R8:
+                               case IL_META_ELEMTYPE_R:
+                               {
+                                       *jitArgs = &(args->floatValue);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+
+                               case IL_META_ELEMTYPE_TYPEDBYREF:
+                               {
+                                       *jitArgs = &(args->typedRefValue);
+                                       ++args;
+                                       ++jitArgs;
+                               }
+                               break;
+                       }
+               }
+               else if(ILType_IsClass(paramType))
+               {
+                       /* Process an object reference */
+                       *jitArgs = &(args->objValue);
+                       ++args;
+                       ++jitArgs;
+               }
+               else if(ILType_IsValueType(paramType))
+               {
+                       /* Process a value type: we assume that the caller has
+                          put the value into a temporary location and then
+                          passed a pointer to the temporary to us */
+                       *jitArgs = args->ptrValue;
+                       ++args;
+                       ++jitArgs;
+               }
+               else if(paramType != 0 && ILType_IsComplex(paramType) &&
+                               ILType_Kind(paramType) == IL_TYPE_COMPLEX_BYREF)
+               {
+                       /* Process a value that is being passed by reference */
+                       *jitArgs = &(args->ptrValue);
+                       ++args;
+                       ++jitArgs;
+               }
+               else
+               {
+                       /* Assume that everything else is an object reference */
+                       *jitArgs = &(args->objValue);
+                       ++args;
+                       ++jitArgs;
+               }
+       }
+
+       return 0;
+
+}
+#else
 int _ILCallPackVParams(ILExecThread *thread, ILMethod *method,
                                           int isCtor, void *_this, void 
*userData)
 {
@@ -408,6 +689,7 @@
        thread->stackTop = stacktop;
        return 0;
 }
+#endif
 
 void _ILCallUnpackDirectResult(ILExecThread *thread, ILMethod *method,
                                                   int isCtor, void *result, 
void *userData)
@@ -642,6 +924,61 @@
        }
 }
 
+#ifdef IL_USE_JIT
+int _ILCallMethod(ILExecThread *thread, ILMethod *method,
+                                 ILCallUnpackFunc unpack, void *result,
+                                 int isCtor, void *_this,
+                                 ILCallPackFunc pack, void *userData)
+{
+       ILType *signature = ILMethod_Signature(method);
+       ILUInt32 numParams = ILTypeNumParams(signature);
+       /* We need an additional parameter for the ILExecThread. */
+       ILUInt32 totalParams = numParams + 1;
+       /* current arg in the parameter Array. */
+       ILUInt32 current = 1;
+
+       /* We need to calculate the number of needed arguments first for the 
args array. */
+       if(ILType_HasThis(signature))
+       {
+               /* We need an additional parameter for the this pointer. */
+               totalParams++;
+       }
+
+       /* Now create the array for the args. */
+       void *jitArgs[totalParams];
+       /* and a buffer for the va args. */
+       /* We take the biggest possible arg type here so we don't need to 
compute it. */
+       ILNativeFloat jitArgsBuffer[numParams];
+
+       /* Store the ILExecThread instance in arg[0]. */
+       jitArgs[0] = &thread;
+
+       if(ILType_HasThis(signature))
+       {
+               if(isCtor)
+               {
+                       /* We need to allocate the Object. */
+                       if(!(jitArgs[1] = _ILEngineAlloc(thread, 
ILMethod_Owner(method), 0)))
+                       {
+                               return 1;
+                       }
+               }
+               else
+               {
+                       jitArgs[1] = &_this;
+               }
+               current++;
+       }
+
+       if((*pack)(thread, signature, &(jitArgsBuffer[0]), &(jitArgs[current]), 
userData))
+       {
+               return 1;
+       }
+
+       /* Now we can call the jitted function. */
+       return ILJitCallMethod(method, jitArgs, result);
+}
+#else
 int _ILCallMethod(ILExecThread *thread, ILMethod *method,
                                  ILCallUnpackFunc unpack, void *result,
                                  int isCtor, void *_this,
@@ -721,6 +1058,7 @@
        /* Done */
        return threwException;
 }
+#endif
 
 ILMethod *_ILLookupInterfaceMethod(ILClassPrivate *objectClassPrivate,
                                                                   ILClass 
*interfaceClass,
Index: pnet/engine/engine.h
diff -u pnet/engine/engine.h:1.108 pnet/engine/engine.h:1.109
--- pnet/engine/engine.h:1.108  Mon Dec 19 18:00:35 2005
+++ pnet/engine/engine.h        Tue Dec 27 20:07:24 2005
@@ -595,15 +595,25 @@
  * Pack parameters onto the CVM stack for a call, using a "va_list"
  * as the source of the values.
  */
+#ifdef IL_USE_JIT
+int _ILCallPackVaParams(ILExecThread *thread, ILType *signature,
+                                           void *argBuffer, void **jitArgs, 
void *userData);
+#else
 int _ILCallPackVaParams(ILExecThread *thread, ILMethod *method,
                                            int isCtor, void *_this, void 
*userData);
+#endif
 
 /*
  * Pack parameters onto the CVM stack for a call, using an array
  * of ILExecValue values to supply the parameters.
  */
+#ifdef IL_USE_JIT
+int _ILCallPackVParams(ILExecThread *thread, ILType *signature,
+                                          void *argBuffer, void **jitArgs, 
void *userData);
+#else
 int _ILCallPackVParams(ILExecThread *thread, ILMethod *method,
                                           int isCtor, void *_this, void 
*userData);
+#endif
 
 /*
  * Unpack a method result from the CVM stack and store it into
@@ -622,8 +632,13 @@
 /*
  * Prototype for a parameter packing function.
  */
+#ifdef IL_USE_JIT
+typedef int (*ILCallPackFunc)(ILExecThread *thread, ILType *signature,
+                                                 void *argBuffer, void 
**jitArgs, void *userData);
+#else
 typedef int (*ILCallPackFunc)(ILExecThread *thread, ILMethod *method,
                                                  int isCtor, void *_this, void 
*userData);
+#endif
 
 /*
  * Prototype for a return value unpacking function.
Index: pnet/engine/jitc.c
diff -u pnet/engine/jitc.c:1.3 pnet/engine/jitc.c:1.4
--- pnet/engine/jitc.c:1.3      Sat Dec 24 11:34:57 2005
+++ pnet/engine/jitc.c  Tue Dec 27 20:07:24 2005
@@ -36,6 +36,9 @@
 extern "C" {
 #endif
 
+/*
+ * Mapping of the native clr types to the corresponing jit types.
+ */
 static struct _tagILJitTypes _ILJitType_VOID;
 static struct _tagILJitTypes _ILJitType_BOOLEAN;
 static struct _tagILJitTypes _ILJitType_BYTE;
@@ -55,6 +58,25 @@
 static struct _tagILJitTypes _ILJitType_VPTR;
 
 /*
+ * Definition of signatures of internal functions used by jitted code.
+ * They have to be kept in sync wirh the corresponding engine funcions.
+ */
+
+/*
+ * ILObject *_ILEngineAlloc(ILExecThread *thread, ILClass *classInfo,
+ *                                                     ILUInt32 size)
+ */
+static ILJitType _ILJitSignature_ILEngineAlloc = 0;
+
+/*
+ * Define offsetof macro if not present.
+ */
+#ifndef offsetof
+#define offsetof(struct_type, member) \
+          (size_t) &(((struct_type *)0)->member)
+#endif
+
+/*
  * Define the structure of a JIT coder's instance block.
  */
 typedef struct _tagILJITCoder ILJITCoder;
@@ -340,14 +362,30 @@
 }
 
 /*
+ * Generate the code to throw the current exception in the thread in libjit.
+ */
+static void _ILJitThrowCurrentException(ILJITCoder *coder)
+{
+       ILJitValue thread = jit_value_get_param(coder->jitFunction,0);
+       ILJitValue currentException = 
jit_insn_load_relative(coder->jitFunction, thread,
+                                                                       
offsetof(ILExecThread, currentException), 
+                                                                       
jit_type_void_ptr);
+       jit_insn_throw(coder->jitFunction, currentException);
+}
+
+/*
  * Initialize the libjit coder.
  * Returns 1 on success and 0 on failure.
  */
 int ILJitInit()
 {
+       ILJitType       returnType;
+       ILJitType       args[3];
+
        /* Initialize libjit */
        jit_init();
 
+       /* Initialize the nattive types. */
        _ILJitTypesInitBase(&_ILJitType_VOID, jit_type_void);
        _ILJitTypesInitBase(&_ILJitType_BOOLEAN, jit_type_ubyte);
        _ILJitTypesInitBase(&_ILJitType_BYTE, jit_type_ubyte);
@@ -378,6 +416,17 @@
        _ILJitTypesInitBase(&_ILJitType_U8, jit_type_ulong);
        _ILJitTypesInitBase(&_ILJitType_VPTR, jit_type_void_ptr);
 
+       /* Initialize the native method signatures. */
+       args[0] = jit_type_void_ptr;
+       args[1] = jit_type_void_ptr;
+       args[2] = jit_type_int;
+       returnType = jit_type_void_ptr;
+       if(!(_ILJitSignature_ILEngineAlloc = 
+               jit_type_create_signature(IL_JIT_CALLCONV_CDECL, returnType, 
args, 3, 1)))
+       {
+               return 0;
+       }
+
        return 1;
 }
 /*
@@ -523,6 +572,97 @@
 
 #endif /* !IL_CONFIG_REDUCE_CODE */
 
+#ifdef IL_CONFIG_PINVOKE
+
+/*
+ * Locate or load an external module that is being referenced via "PInvoke".
+ * Returns the system module pointer, or NULL if it could not be loaded.
+ */
+static void *LocateExternalModule(ILExecProcess *process, const char *name,
+                                                                 ILPInvoke 
*pinvoke)
+{
+       ILLoadedModule *loaded;
+       char *pathname;
+
+       /* Search for an already-loaded module with the same name */
+       loaded = process->loadedModules;
+       while(loaded != 0)
+       {
+               if(!ILStrICmp(loaded->name, name))
+               {
+                       return loaded->handle;
+               }
+               loaded = loaded->next;
+       }
+
+       /* Create a new module structure.  We keep this structure even
+          if we cannot load the actual module.  This ensures that
+          future requests for the same module will be rejected without
+          re-trying the open */
+       loaded = (ILLoadedModule *)ILMalloc(sizeof(ILLoadedModule) + 
strlen(name));
+       if(!loaded)
+       {
+               return 0;
+       }
+       loaded->next = process->loadedModules;
+       loaded->handle = 0;
+       strcpy(loaded->name, name);
+       process->loadedModules = loaded;
+
+       /* Resolve the module name to a library name */
+       pathname = ILPInvokeResolveModule(pinvoke);
+       if(!pathname)
+       {
+               return 0;
+       }
+
+       /* Attempt to open the module */
+       loaded->handle = ILDynLibraryOpen(pathname);
+       ILFree(pathname);
+       return loaded->handle;
+}
+
+#endif /* IL_CONFIG_PINVOKE */
+
+/*
+ * Generate the stub for calling an internal function.
+ */
+static int _ILJitCompileInternal(jit_function_t func, void *nativeFunction)
+{
+       ILJitType signature = jit_function_get_signature(func);
+       ILJitType returnType = jit_type_get_return(signature);
+       unsigned int numParams = jit_type_num_params(signature);
+       ILJitValue returnValue;
+
+       if(numParams > 0)
+       {
+               ILJitValue jitParams[numParams];
+               ILUInt32 current;
+               
+               for(current = 0; current < numParams; current++)
+               {
+                       if(!(jitParams[current] = jit_value_get_param(func, 
current)))
+                       {
+                               return JIT_RESULT_OUT_OF_MEMORY;
+                       }
+               }
+               returnValue = jit_insn_call_native(func, 0, nativeFunction, 
signature, jitParams, numParams, 0);
+       }
+       else
+       {
+               returnValue = jit_insn_call_native(func, 0, nativeFunction, 
signature, 0, 0, 0);
+       }
+       if(returnType != jit_type_void_ptr)
+       {
+               jit_insn_return(func, returnValue);     
+       }
+       else
+       {
+               jit_insn_return(func, 0);       
+       }
+       return JIT_RESULT_OK;
+}
+
 /*
  * On demand code generator.
  */
@@ -556,11 +696,125 @@
        else
        {
                /* This is a "PInvoke", "internalcall", or "runtime" method */
-               ILPInvoke *pinv;
+               ILPInvoke *pinv = ILPInvokeFind(method);
+               ILInternalInfo fnInfo;
+               int isConstructor = ILMethod_IsConstructor(method);;
+       #ifdef IL_CONFIG_PINVOKE
+               ILModule *module;
+               const char *name;
+               void *moduleHandle;
+       #endif
+
+               switch(method->implementAttrs &
+                                       (IL_META_METHODIMPL_CODE_TYPE_MASK |
+                                        IL_META_METHODIMPL_INTERNAL_CALL |
+                                        IL_META_METHODIMPL_JAVA))
+               {
+                       case IL_META_METHODIMPL_IL:
+                       case IL_META_METHODIMPL_OPTIL:
+                       {
+                               /* If we don't have a PInvoke record, then we 
don't
+                                  know what to map this method call to */
+                               if(!pinv)
+                               {
+                                       return JIT_RESULT_COMPILE_ERROR;
+                               }
+
+                       #ifdef IL_CONFIG_PINVOKE
+                               /* Find the module for the PInvoke record */
+                               module = ILPInvoke_Module(pinv);
+                               if(!module)
+                               {
+                                       return JIT_RESULT_COMPILE_ERROR;
+                               }
+                               name = ILModule_Name(module);
+                               if(!name || *name == '\0')
+                               {
+                                       return JIT_RESULT_COMPILE_ERROR;
+                               }
+                               moduleHandle = LocateExternalModule
+                                                                       
(ILExecThreadGetProcess(thread), name, pinv);
+                               if(!moduleHandle)
+                               {
+                                       return JIT_RESULT_COMPILE_ERROR;
+                               }
+
+                               /* Get the name of the function within the 
module */
+                               name = ILPInvoke_Alias(pinv);
+                               if(!name || *name == '\0')
+                               {
+                                       name = ILMethod_Name(method);
+                               }
 
-               pinv = ILPInvokeFind(method);
+                               /* Look up the method within the module */
+                               fnInfo.func = 
ILDynLibraryGetSymbol(moduleHandle, name);
+                       #else /* !IL_CONFIG_PINVOKE */
+                               return JIT_RESULT_COMPILE_ERROR;
+                       #endif /* IL_CONFIG_PINVOKE */
+                       }
+                       break;
 
-               return JIT_RESULT_OK;
+                       case IL_META_METHODIMPL_RUNTIME:
+                       case IL_META_METHODIMPL_IL | 
IL_META_METHODIMPL_INTERNAL_CALL:
+                       {
+                               /* "internalcall" and "runtime" methods must not
+                                  have PInvoke records associated with them */
+                               if(pinv)
+                               {
+                                       return JIT_RESULT_COMPILE_ERROR;
+                               }
+
+                               /* Look up the internalcall function details */
+                               
if(!_ILFindInternalCall(_ILExecThreadProcess(thread),
+                                                                               
method, 0, &fnInfo))
+                               {
+                                       if(isConstructor)
+                                       {
+                                               
if(!_ILFindInternalCall(_ILExecThreadProcess(thread),
+                                                                               
                method, 1, &fnInfo))
+                                               {
+                                                       return 
JIT_RESULT_COMPILE_ERROR;
+                                               }
+                                       }
+                                       else
+                                       {
+                                               return JIT_RESULT_COMPILE_ERROR;
+                                       }
+                               }
+                               else if(isConstructor)
+                               {
+                                       
_ILFindInternalCall(ILExecThreadGetProcess(thread),
+                                                                               
method, 1, &fnInfo);
+                               }
+                       }
+                       break;
+
+                       default:
+                       {
+                               /* No idea how to invoke this method */
+                               return JIT_RESULT_COMPILE_ERROR;
+                       }
+                       /* Not reached */
+               }
+
+               /* Bail out if we did not find the underlying native method */
+               if(!(fnInfo.func))
+               {
+                       return JIT_RESULT_COMPILE_ERROR;
+               }
+
+               switch(method->implementAttrs &
+                                       (IL_META_METHODIMPL_CODE_TYPE_MASK |
+                                        IL_META_METHODIMPL_INTERNAL_CALL |
+                                        IL_META_METHODIMPL_JAVA))
+               {
+                       case IL_META_METHODIMPL_RUNTIME:
+                       case IL_META_METHODIMPL_IL | 
IL_META_METHODIMPL_INTERNAL_CALL:
+                       {
+                               return _ILJitCompileInternal(func, fnInfo.func);
+                       }
+               }
+               return JIT_RESULT_COMPILE_ERROR;
        }
 }
 
@@ -715,6 +969,40 @@
 }
 
 /*
+ * Call the jit function for a ILMethod.
+ * Returns 1 if an exception occured.
+ */
+int ILJitCallMethod(ILMethod *method, void**jitArgs, void *result)
+{
+       ILJitFunction jitFunction = method->userData;
+
+       if(!jitFunction)
+       {
+               /* We may have to layout the class. */
+               return 1;
+       }
+
+       if(!jit_function_apply(jitFunction, jitArgs, result))
+       {
+               return 1;
+       }
+       return 0;
+}
+
+/*
+ * Get the ILJitFunction for an ILMethod.
+ * Returns 0 if the jit function stub isn't created yet.
+ */
+ILJitFunction ILJitFunctionFromILMethod(ILMethod *method)
+{
+       if(method)
+       {
+               return (ILJitFunction)(method->userData);
+       }
+       return 0;
+}
+
+/*
  * Create the class/struct representation of a clr type for libjit.
  * and store the type in classPrivate.
  * Returns the 1 on success else 0
Index: pnet/engine/jitc.h
diff -u pnet/engine/jitc.h:1.2 pnet/engine/jitc.h:1.3
--- pnet/engine/jitc.h:1.2      Thu Dec 22 18:23:44 2005
+++ pnet/engine/jitc.h  Tue Dec 27 20:07:24 2005
@@ -48,13 +48,28 @@
  */
 #define IL_JIT_META_METHOD 1
 
+
+/*
+ * Defaut calling convention for libjit.
+ */
+#define IL_JIT_CALLCONV_DEFAULT                jit_abi_cdecl
+
+
+/*
+ * Calling conventions for libjit.
+ */
+#define IL_JIT_CALLCONV_CDECL          jit_abi_cdecl
+#define IL_JIT_CALLCONV_VARARG         jit_abi_vararg
+#define IL_JIT_CALLCONV_STDCALL                jit_abi_stdcall
+#define IL_JIT_CALLCONV_FASTCALL       jit_abi_fastcall
+
 /*
- * Representation of a type representation for lingit.
+ * Representation of a type representation for libjit.
  */
 typedef jit_type_t             ILJitType;
 
 /*
- * Representation of a method representation for lingit.
+ * Representation of a method representation for libjit.
  */
 typedef jit_function_t ILJitFunction;
 
@@ -106,6 +121,18 @@
 int ILJitCreateFunctionsForClass(ILCoder *_coder, ILClass *info);
 
 /*
+ * Get the ILJitFunction for an ILMethod.
+ * Returns 0 if the jit function stub isn't created yet.
+ */
+ILJitFunction ILJitFunctionFromILMethod(ILMethod *method);
+
+/*
+ * Call the jit function for an ILMethod.
+ * Returns 1 if an exception occured.
+ */
+int ILJitCallMethod(ILMethod *method, void**jitArgs, void *result);
+
+/*
  * Create the class/struct representation of a clr type for libjit.
  * and store the type in classPrivate.
  * Returns the jit type on success else 0
Index: pnet/engine/jitc_call.c
diff -u pnet/engine/jitc_call.c:1.1 pnet/engine/jitc_call.c:1.2
--- pnet/engine/jitc_call.c:1.1 Mon Dec 19 18:00:35 2005
+++ pnet/engine/jitc_call.c     Tue Dec 27 20:07:24 2005
@@ -20,6 +20,88 @@
 
 #ifdef IL_JITC_CODE
 
+/*
+ * Call the static constructor for a class if necessary.
+ */
+static void _ILJitCallStaticConstructor(ILCoder *_coder, ILClass *classInfo,
+                                                                 int isCtor)
+{
+       if((classInfo->attributes & IL_META_TYPEDEF_CCTOR_ONCE) != 0)
+       {
+               /* We already know that the static constructor has been called,
+                  so there is no point outputting a call to it again */
+               return;
+       }
+       if(isCtor ||
+          (classInfo->attributes & IL_META_TYPEDEF_BEFORE_FIELD_INIT) == 0)
+       {
+               ILJITCoder *coder = _ILCoderToILJITCoder(_coder);
+
+               /* We must call the static constructor before instance
+                  constructors, or before static methods when the
+                  "beforefieldinit" attribute is not present */
+               ILMethod *cctor = 0;
+               while((cctor = (ILMethod *)ILClassNextMemberByKind
+                                       (classInfo, (ILMember *)cctor,
+                                        IL_META_MEMBERKIND_METHOD)) != 0)
+               {
+                       if(ILMethod_IsStaticConstructor(cctor))
+                       {
+                               break;
+                       }
+               }
+               if(cctor != 0)
+               {
+                       /* Don't call it if we are within the constructor 
already */
+                       if(cctor != coder->currentMethod)
+                       {
+                               /* Output a call to the static constructor */
+                               jit_value_t thread = 
jit_value_get_param(coder->jitFunction, 0);
+
+                               jit_insn_call(coder->jitFunction, "cctor",
+                                                               
ILJitFunctionFromILMethod(cctor), 0,
+                                                               &thread, 1, 0);
+                       }
+               }
+               else
+               {
+                       /* This class does not have a static constructor,
+                          so mark it so that we never do this test again */
+                       classInfo->attributes |= IL_META_TYPEDEF_CCTOR_ONCE;
+               }
+       }
+}
+
+/*
+ * Create a new object and push it on the stack.
+ */
+static void _ILJitNewObj(ILJITCoder *coder, ILClass *info, ILJitValue *newArg)
+{
+       ILJitValue newObj;
+       ILJitValue args[3];
+       jit_label_t label1 = jit_label_undefined;
+       
+       args[0] = jit_value_get_param(coder->jitFunction, 0);
+       args[1] = jit_value_create_nint_constant(coder->jitFunction,
+                                                                               
                jit_type_void_ptr, (jit_nint)info);
+       args[2] = jit_value_create_nint_constant(coder->jitFunction,
+                                                                               
                jit_type_int, 0);
+       newObj = jit_insn_call_native(coder->jitFunction, 0, _ILEngineAlloc,
+                                                                       
_ILJitSignature_ILEngineAlloc, args, 3, 0);
+       jit_insn_branch_if(coder->jitFunction, newObj, &label1);
+       _ILJitThrowCurrentException(coder);
+       jit_insn_label(coder->jitFunction, &label1);
+       *newArg = newObj;
+}
+
+/*
+ * Call a Method.
+ */
+static void _ILJitCallMethod(ILJITCoder *coder, ILJitValue *stackTop, int 
isCtor)
+{
+
+}
+
 static void JITCoder_UpConvertArg(ILCoder *coder, ILEngineStackItem *args,
                                                          ILUInt32 numArgs, 
ILUInt32 param,
                                                          ILType *paramType)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]