blob: 25523f68b100f4f0ad583a3b438f62a17211d5bd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using Internal.TypeSystem;
using Debug = System.Diagnostics.Debug;
namespace Internal.IL.Stubs
{
/// <summary>
/// Provides method bodies for System.Threading.Interlocked intrinsics.
/// </summary>
public static class InterlockedIntrinsics
{
public static MethodIL EmitIL(
#if READYTORUN
ILCompiler.CompilationModuleGroup compilationModuleGroup,
#endif // READYTORUN
MethodDesc method)
{
Debug.Assert(((MetadataType)method.OwningType).Name == "Interlocked");
if (method.HasInstantiation && method.Name == "CompareExchange")
{
#if READYTORUN
// Check to see if the tokens needed to describe the CompareExchange are naturally present within
// the compilation. The current implementation of stable tokens used by cross module inlining is not
// compatible with rewriting the IL of a compiler generated intrinsic. Fortunately, it turns out
// that the managed implementation of this intrinsic is correct, just a few more IL instructions.
if (compilationModuleGroup.ContainsType(method.OwningType))
#endif // READYTORUN
{
TypeDesc objectType = method.Context.GetWellKnownType(WellKnownType.Object);
MethodDesc compareExchangeObject = method.OwningType.GetKnownMethod("CompareExchange",
new MethodSignature(
MethodSignatureFlags.Static,
genericParameterCount: 0,
returnType: objectType,
parameters: new TypeDesc[] { objectType.MakeByRefType(), objectType, objectType }));
ILEmitter emit = new ILEmitter();
ILCodeStream codeStream = emit.NewCodeStream();
codeStream.EmitLdArg(0);
codeStream.EmitLdArg(1);
codeStream.EmitLdArg(2);
codeStream.Emit(ILOpcode.call, emit.NewToken(compareExchangeObject));
codeStream.Emit(ILOpcode.ret);
return emit.Link(method);
}
}
return null;
}
}
}
|