Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Common/src/TypeSystem/Common/TypeSystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ static public MetadataType GetClosestDefType(this TypeDesc type)
return type.BaseType;
}

static public int GetElementSize(this TypeDesc type)
{
if (type.IsValueType)
{
return ((MetadataType)type).InstanceFieldSize;
}
else
{
return type.Context.Target.PointerSize;
}
}

static private MethodDesc FindMethodOnExactTypeWithMatchingTypicalMethod(this TypeDesc type, MethodDesc method)
{
Expand Down
3 changes: 2 additions & 1 deletion src/ILToNative/reproNative/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ int __initialize_runtime();
void __shutdown_runtime();

extern "C" Object * __allocate_object(MethodTable * pMT);
extern "C" Object * __allocate_array(MethodTable * pMT, size_t elements);
extern "C" Object * __allocate_array(size_t elements, MethodTable * pMT);
Object * __allocate_string(int32_t len);
__declspec(noreturn) void __throw_exception(void * pEx);
Object * __load_string_literal(const char * string);

extern "C" Object * __castclass_class(void * p, MethodTable * pMT);
extern "C" Object * __isinst_class(void * p, MethodTable * pMT);

extern "C" void __range_check_fail();
void __range_check(void * a, size_t elem);

Object * __get_commandline_args(int argc, char * argv[]);
Expand Down
9 changes: 7 additions & 2 deletions src/ILToNative/reproNative/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Object * __allocate_string(int32_t len)
#endif
}

extern "C" Object * __allocate_array(MethodTable * pMT, size_t elements)
extern "C" Object * __allocate_array(size_t elements, MethodTable * pMT)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please also update the order of arguments in ILToNative\src\CppCodeGen\ILToCppImporter.cs

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

{
#if !USE_MRT
alloc_context * acontext = GetThread()->GetAllocContext();
Expand Down Expand Up @@ -301,6 +301,11 @@ void ThrowRangeOverflowException()
throw 0;
}

void __range_check_fail()
{
ThrowRangeOverflowException();
}

void __range_check(void * a, size_t elem)
{
if (elem >= *((size_t*)a + 1))
Expand All @@ -310,7 +315,7 @@ void __range_check(void * a, size_t elem)
#ifdef CPPCODEGEN
Object * __get_commandline_args(int argc, char * argv[])
{
System::Array * p = (System::Array *)__allocate_array(System::String__Array::__getMethodTable(), argc);
System::Array * p = (System::Array *)__allocate_array(argc, System::String__Array::__getMethodTable());

for (int i = 0; i < argc; i++)
{
Expand Down
27 changes: 25 additions & 2 deletions src/ILToNative/src/Compiler/AsmWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ void OutputMethodCode(RegisteredMethod m)
targetName = ((ReadyToRunHelper)target).MangledName;
}
else
if (target is JitHelper)
{
targetName = ((JitHelper)target).MangledName;
}
else
{
// TODO:
throw new NotImplementedException();
Expand Down Expand Up @@ -228,6 +233,14 @@ void OutputReadyToHelpers()
Out.WriteLine("jmp __castclass_class");
break;

case ReadyToRunHelperId.NewArr1:
Out.Write("leaq __EEType_");
Out.Write(NameMangler.GetMangledTypeName((TypeDesc)helper.Target));
Out.WriteLine("(%rip), %rdx");

Out.WriteLine("jmp __allocate_array");
break;

case ReadyToRunHelperId.GetNonGCStaticBase:
Out.Write("leaq __NonGCStaticBase_");
Out.Write(NameMangler.GetMangledTypeName((TypeDesc)helper.Target));
Expand All @@ -254,7 +267,17 @@ void OutputEETypes()
Out.Write(NameMangler.GetMangledTypeName(t.Type));
Out.WriteLine(":");

Out.WriteLine(".int 0, 24");
if (t.Type.IsArray && ((ArrayType)t.Type).Rank == 1)
{
Out.Write(".word ");
Out.WriteLine(t.Type.GetElementSize()); // m_ComponentSize
Out.WriteLine(".word 4"); // m_flags: IsArray(0x4)
Out.WriteLine(".int 24");
}
else
{
Out.WriteLine(".int 0, 24");
}

if (t.Type.BaseType != null)
{
Expand Down Expand Up @@ -321,4 +344,4 @@ void OutputVirtualSlots(TypeDesc implType, TypeDesc declType)
}
}
}
}
}
11 changes: 11 additions & 0 deletions src/ILToNative/src/Compiler/Compilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,16 @@ public Object GetReadyToRunHelper(ReadyToRunHelperId id, Object target)

return helper;
}

Dictionary<JitHelperId, JitHelper> _jitHelpers = new Dictionary<JitHelperId, JitHelper>();
public Object GetJitHelper(JitHelperId id)
{
JitHelper helper;

if (!_jitHelpers.TryGetValue(id, out helper))
_jitHelpers.Add(id, helper = new JitHelper(this, id));

return helper;
}
}
}
46 changes: 46 additions & 0 deletions src/ILToNative/src/Compiler/JitHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Internal.TypeSystem;

namespace ILToNative
{
public enum JitHelperId
{
RngChkFail,
}

class JitHelper
{
Compilation _compilation;

public JitHelper(Compilation compilation, JitHelperId id)
{
_compilation = compilation;

this.Id = id;
}

public JitHelperId Id { get; private set; }

public string MangledName
{
get
{
switch (this.Id)
{
case JitHelperId.RngChkFail:
return "__range_check_fail";

default:
throw new NotImplementedException();
}
}
}
}

}

3 changes: 3 additions & 0 deletions src/ILToNative/src/Compiler/ReadyToRunHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace ILToNative
public enum ReadyToRunHelperId
{
NewHelper,
NewArr1,
VirtualCall,
IsInstanceOf,
CastClass,
Expand Down Expand Up @@ -43,6 +44,8 @@ public string MangledName
{
case ReadyToRunHelperId.NewHelper:
return "__NewHelper_" + _compilation.NameMangler.GetMangledTypeName((TypeDesc)this.Target);
case ReadyToRunHelperId.NewArr1:
return "__NewArr1_" + _compilation.NameMangler.GetMangledTypeName((TypeDesc)this.Target);
case ReadyToRunHelperId.VirtualCall:
return "__VirtualCall_" + _compilation.NameMangler.GetMangledMethodName((MethodDesc)this.Target);
case ReadyToRunHelperId.IsInstanceOf:
Expand Down
5 changes: 3 additions & 2 deletions src/ILToNative/src/CppCodeGen/ILToCppImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,9 +1779,10 @@ void ImportNewArray(int token)
_compilation.MarkAsConstructed(arrayType);

Append("__allocate_array(");
Append(_writer.GetCppTypeName(arrayType));
Append("::__getMethodTable(), ");
Append(numElements.Value.Name);
Append(", ");
Append(_writer.GetCppTypeName(arrayType));
Append("::__getMethodTable()");
Append(")");
Finish();
}
Expand Down
1 change: 1 addition & 0 deletions src/ILToNative/src/ILToNative.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="Compiler\AsmWriter.cs" />
<Compile Include="Compiler\Compilation.cs" />
<Compile Include="Compiler\CompilerTypeSystemContext.cs" />
<Compile Include="Compiler\JitHelper.cs" />
<Compile Include="Compiler\MemoryHelper.cs" />
<Compile Include="Compiler\MethodCode.cs" />
<Compile Include="Compiler\MethodExtensions.cs" />
Expand Down
62 changes: 45 additions & 17 deletions src/JitInterface/src/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ void Get_CORINFO_SIG_INFO(MethodDesc method, out CORINFO_SIG_INFO sig)

sig.numArgs = (ushort)signature.Length;

sig.args = (CORINFO_ARG_LIST_STRUCT_ * )0; // CORINFO_ARG_LIST_STRUCT_ is argument index
sig.args = (CORINFO_ARG_LIST_STRUCT_*)0; // CORINFO_ARG_LIST_STRUCT_ is argument index

// TODO: Shared generic
sig.sigInst.classInst = null;
sig.sigInst.classInstCount = 0;
sig.sigInst.methInst = null;
sig.sigInst.methInstCount = 0;

sig.pSig = (byte * )ObjectToHandle(signature);
sig.pSig = (byte*)ObjectToHandle(signature);
sig.cbSig = 0; // Not used by the JIT
sig.scope = null; // Not used by the JIT
sig.token = 0; // Not used by the JIT
Expand Down Expand Up @@ -246,7 +246,7 @@ void Get_CORINFO_SIG_INFO(TypeDesc[] locals, out CORINFO_SIG_INFO sig)
sig.token = 0; // Not used by the JIT
}

CorInfoType asCorInfoType(TypeDesc type, out CORINFO_CLASS_STRUCT_* structType)
CorInfoType asCorInfoType(TypeDesc type)
{
if (type.IsEnum)
{
Expand All @@ -258,20 +258,29 @@ CorInfoType asCorInfoType(TypeDesc type, out CORINFO_CLASS_STRUCT_* structType)
Debug.Assert((CorInfoType)TypeFlags.Void == CorInfoType.CORINFO_TYPE_VOID);
Debug.Assert((CorInfoType)TypeFlags.Double == CorInfoType.CORINFO_TYPE_DOUBLE);

structType = null;
return (CorInfoType)type.Category;
}

if (type.IsByRef)
{
return CorInfoType.CORINFO_TYPE_BYREF;
}

if (type.IsValueType)
{
structType = ObjectToHandle(type);
return CorInfoType.CORINFO_TYPE_VALUECLASS;
}

structType = null;
return CorInfoType.CORINFO_TYPE_CLASS;
}

CorInfoType asCorInfoType(TypeDesc type, out CORINFO_CLASS_STRUCT_* structType)
{
var corInfoType = asCorInfoType(type);
structType = (corInfoType == CorInfoType.CORINFO_TYPE_VALUECLASS) ? ObjectToHandle(type) : null;
return corInfoType;
}

uint getMethodAttribsInternal(MethodDesc method)
{
CorInfoFlag result = 0;
Expand Down Expand Up @@ -501,8 +510,12 @@ bool isValidStringRef(IntPtr _this, CORINFO_MODULE_STRUCT_* module, uint metaTOK
[return: MarshalAs(UnmanagedType.Bool)]
bool shouldEnforceCallvirtRestriction(IntPtr _this, CORINFO_MODULE_STRUCT_* scope)
{ throw new NotImplementedException(); }

CorInfoType asCorInfoType(IntPtr _this, CORINFO_CLASS_STRUCT_* cls)
{ throw new NotImplementedException(); }
{
var type = HandleToObject(cls);
return asCorInfoType(type);
}

byte* getClassName(IntPtr _this, CORINFO_CLASS_STRUCT_* cls)
{
Expand Down Expand Up @@ -598,14 +611,7 @@ void LongLifetimeFree(IntPtr _this, void* obj)
uint getClassSize(IntPtr _this, CORINFO_CLASS_STRUCT_* cls)
{
TypeDesc type = HandleToObject(cls);
if (type.IsValueType)
{
return (uint)((MetadataType)type).InstanceFieldSize;
}
else
{
return (uint)type.Context.Target.PointerSize;
}
return (uint)type.GetElementSize();
}

uint getClassAlignmentRequirement(IntPtr _this, CORINFO_CLASS_STRUCT_* cls, [MarshalAs(UnmanagedType.Bool)]bool fDoubleAlignHint)
Expand Down Expand Up @@ -704,6 +710,16 @@ void getReadyToRunHelper(IntPtr _this, ref CORINFO_RESOLVED_TOKEN pResolvedToken
pLookup.addr = (void*)ObjectToHandle(_compilation.GetReadyToRunHelper(ReadyToRunHelperId.NewHelper, type));
}
break;
case CorInfoHelpFunc.CORINFO_HELP_READYTORUN_NEWARR_1:
{
var type = HandleToObject(pResolvedToken.hClass);
var arrayType = type.Context.GetArrayType(type);
_compilation.AddType(arrayType);
_compilation.MarkAsConstructed(arrayType);

pLookup.addr = (void*)ObjectToHandle(_compilation.GetReadyToRunHelper(ReadyToRunHelperId.NewArr1, arrayType));
}
break;
case CorInfoHelpFunc.CORINFO_HELP_READYTORUN_ISINSTANCEOF:
{
var type = HandleToObject(pResolvedToken.hClass);
Expand Down Expand Up @@ -740,8 +756,12 @@ void classMustBeLoadedBeforeCodeIsRun(IntPtr _this, CORINFO_CLASS_STRUCT_* cls)

CORINFO_CLASS_STRUCT_* getBuiltinClass(IntPtr _this, CorInfoClassId classId)
{ throw new NotImplementedException(); }

CorInfoType getTypeForPrimitiveValueClass(IntPtr _this, CORINFO_CLASS_STRUCT_* cls)
{ throw new NotImplementedException(); }
{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just do:
return asCorInfoType(HandleToObject(cls));

Implement a new asCorInfoType method - copy the existing asCorInfoType method with two arguments, delete the second out argument and all places where it is assigned to.

(The complex implementation from desktop/CoreCLR is not needed here because of we are not going to have the weird handling of struct as primitives that x86 JIT has.)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this first, but I felt this wasn't accurately emulating the Desktop. I see why now.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I noticed post your comment, that I left uninitialized null for Byref case originally.

return asCorInfoType(HandleToObject(cls));
}

[return: MarshalAs(UnmanagedType.Bool)]
bool canCast(IntPtr _this, CORINFO_CLASS_STRUCT_* child, CORINFO_CLASS_STRUCT_* parent)
{ throw new NotImplementedException(); }
Expand Down Expand Up @@ -1036,7 +1056,15 @@ uint getThreadTLSIndex(IntPtr _this, ref void* ppIndirection)
SIZE_T* getAddrModuleDomainID(IntPtr _this, CORINFO_MODULE_STRUCT_* module)
{ throw new NotImplementedException(); }
void* getHelperFtn(IntPtr _this, CorInfoHelpFunc ftnNum, ref void* ppIndirection)
{ throw new NotImplementedException(); }
{
switch (ftnNum)
{
case CorInfoHelpFunc.CORINFO_HELP_RNGCHKFAIL:
return (void*)ObjectToHandle(_compilation.GetJitHelper(JitHelperId.RngChkFail));
default:
throw new NotImplementedException();
}
}
void getFunctionEntryPoint(IntPtr _this, CORINFO_METHOD_STRUCT_* ftn, ref CORINFO_CONST_LOOKUP pResult, CORINFO_ACCESS_FLAGS accessFlags)
{ throw new NotImplementedException(); }
void getFunctionFixedEntryPoint(IntPtr _this, CORINFO_METHOD_STRUCT_* ftn, ref CORINFO_CONST_LOOKUP pResult)
Expand Down