Arm64:Implemeting ThisPtrRetBufPrecode:
authorRama Krishnan Raghupathy <ramarag@microsoft.com>
Fri, 22 Apr 2016 02:51:49 +0000 (19:51 -0700)
committerRama Krishnan Raghupathy <ramarag@microsoft.com>
Tue, 3 May 2016 00:46:44 +0000 (17:46 -0700)
Precode to shuffle this and retbuf for closed delegates over static methods with return buffer

Commit migrated from https://github.com/dotnet/coreclr/commit/ea83434a4f3a92f3d476291031300b9de9c3528b

src/coreclr/src/vm/arm/cgencpu.h
src/coreclr/src/vm/arm64/cgencpu.h
src/coreclr/src/vm/arm64/stubs.cpp
src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.cs [new file with mode: 0644]
src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.csproj [new file with mode: 0644]

index 301118b..30573eb 100644 (file)
@@ -1196,7 +1196,7 @@ struct FixupPrecode {
 typedef DPTR(FixupPrecode) PTR_FixupPrecode;
 
 
-// Precode to stuffle this and retbuf for closed delegates over static methods with return buffer
+// Precode to shuffle this and retbuf for closed delegates over static methods with return buffer
 struct ThisPtrRetBufPrecode {
 
     static const int Type = 0x84;
index 9ac7d49..cd54ea0 100644 (file)
@@ -614,18 +614,12 @@ struct FixupPrecode {
 typedef DPTR(FixupPrecode) PTR_FixupPrecode;
 
 
-// Precode to stuffle this and retbuf for closed delegates over static methods with return buffer
+// Precode to shuffle this and retbuf for closed delegates over static methods with return buffer
 struct ThisPtrRetBufPrecode {
 
-    static const int Type = 0x84;
+    static const int Type = 0x10;
 
-    // mov r12, r0
-    // mov r0, r1
-    // mov r1, r12
-    // ldr pc, [pc, #0]     ; =m_pTarget
-    // dcd pTarget
-    // dcd pMethodDesc
-    WORD    m_rgCode[6];
+    UINT32  m_rgCode[6];
     TADDR   m_pTarget;
     TADDR   m_pMethodDesc;
 
@@ -633,20 +627,29 @@ struct ThisPtrRetBufPrecode {
 
     TADDR GetMethodDesc()
     {
-        _ASSERTE(!"ARM64:NYI");
-        return NULL;
+        LIMITED_METHOD_DAC_CONTRACT;
+
+        return m_pMethodDesc;
     }
 
     PCODE GetTarget()
     { 
-        _ASSERTE(!"ARM64:NYI");
-        return NULL;
+        LIMITED_METHOD_DAC_CONTRACT;
+        return m_pTarget;
     }
 
     BOOL SetTargetInterlocked(TADDR target, TADDR expected)
     {
-        _ASSERTE(!"ARM64:NYI");
-        return NULL;
+        CONTRACTL
+        {
+            THROWS;
+            GC_TRIGGERS;
+        }
+        CONTRACTL_END;
+
+        EnsureWritableExecutablePages(&m_pTarget);
+        return (TADDR)InterlockedCompareExchange64(
+            (LONGLONG*)&m_pTarget, (TADDR)target, (TADDR)expected) == expected;
     }
 };
 typedef DPTR(ThisPtrRetBufPrecode) PTR_ThisPtrRetBufPrecode;
index 38ce1d9..2a3cddb 100644 (file)
@@ -572,9 +572,27 @@ void FixupPrecode::Fixup(DataImage *image, MethodDesc * pMD)
 }
 #endif // FEATURE_NATIVE_IMAGE_GENERATION
 
+
 void ThisPtrRetBufPrecode::Init(MethodDesc* pMD, LoaderAllocator *pLoaderAllocator)
 {
-    _ASSERTE(!"ARM64:NYI");
+    WRAPPER_NO_CONTRACT;
+
+    int n = 0;
+    //Initially
+    //x0 -This ptr
+    //x1 -ReturnBuffer
+    m_rgCode[n++] = 0x91000010; // mov x16, x0
+    m_rgCode[n++] = 0x91000020; // mov x0, x1
+    m_rgCode[n++] = 0x91000201; // mov x1, x16
+    m_rgCode[n++] = 0x58000070; // ldr x16, [pc, #12]
+    _ASSERTE((UINT32*)&m_pTarget == &m_rgCode[n + 2]);
+    m_rgCode[n++] = 0xd61f0200; // br  x16
+    n++;                        // empty 4 bytes for data alignment below
+    _ASSERTE(n == _countof(m_rgCode));
+    
+    
+    m_pTarget = GetPreStubEntryPoint();
+    m_pMethodDesc = (TADDR)pMD;
 }
 
 
diff --git a/src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.cs b/src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.cs
new file mode 100644 (file)
index 0000000..6b35433
--- /dev/null
@@ -0,0 +1,41 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+using System;
+using System.Reflection;
+
+class Program
+{
+    public int scale;
+
+    public Program(int scale)
+    {
+        this.scale = scale;
+    }
+    public static decimal getfunc(Program prog,int constituent)
+    {
+        return new decimal(constituent/prog.scale);
+    }
+    static int Main(string[] args)
+    {
+        int result = -1;
+        
+        int constituent = 3;
+        Program prog = new Program(1);
+        2.Equals(3);
+        
+        MethodInfo info = typeof(Program).GetMethod("getfunc", BindingFlags.Static | BindingFlags.Public);
+        
+        //Tests closed delegates over static methods with return buffer
+        Func<int, decimal> deepThought = (Func<int, decimal>)info.CreateDelegate(typeof(Func<int, decimal>), prog);
+
+        var res1 = deepThought(constituent);
+        var res2 = deepThought(constituent);
+
+        if (decimal.Compare(res1, res2) == 0)
+            return 100;
+
+        return result;
+        
+    }
+}
diff --git a/src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.csproj b/src/coreclr/tests/src/CoreMangLib/system/delegate/miscellaneous/ClosedStatic.csproj
new file mode 100644 (file)
index 0000000..59b1c83
--- /dev/null
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{95DFC527-4DC1-495E-97D7-E94EE1F7140D}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <FileAlignment>512</FileAlignment>
+    <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
+    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\11.0\UITestExtensionPackages</ReferencePath>
+    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
+    <NuGetPackageImportStamp>7a9bfb7d</NuGetPackageImportStamp>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <ReferenceLocalMscorlib>false</ReferenceLocalMscorlib>
+    <CLRTestKind>BuildAndRun</CLRTestKind>
+  </PropertyGroup>
+  <!-- Default configurations to help VS understand the configurations -->
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+  </PropertyGroup>
+  <ItemGroup>
+    <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
+      <Visible>False</Visible>
+    </CodeAnalysisDependentAssemblyPaths>
+  </ItemGroup>
+  <ItemGroup>
+    <!-- Add Compile Object Here -->
+    <Compile Include="ClosedStatic.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="app.config" />
+    <None Include="project.json" />
+  </ItemGroup>
+  <ItemGroup>
+    <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
+  </ItemGroup>
+  <ItemGroup>
+    <WarningLevel Include="1" />
+  </ItemGroup>
+  <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
+  <PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' ">
+  </PropertyGroup>
+</Project>