2
Copyright (C) 2007-2011 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
26
using System.Collections.Generic;
28
using IKVM.Reflection;
29
using IKVM.Reflection.Emit;
30
using Type = IKVM.Reflection.Type;
32
using System.Reflection;
33
using System.Reflection.Emit;
36
using InstructionFlags = IKVM.Internal.ClassFile.Method.InstructionFlags;
38
static class AtomicReferenceFieldUpdaterEmitter
40
internal static bool Emit(DynamicTypeWrapper.FinishContext context, TypeWrapper wrapper, CodeEmitter ilgen, ClassFile classFile, int i, ClassFile.Method.Instruction[] code, InstructionFlags[] flags)
43
&& (flags[i - 0] & InstructionFlags.BranchTarget) == 0
44
&& (flags[i - 1] & InstructionFlags.BranchTarget) == 0
45
&& (flags[i - 2] & InstructionFlags.BranchTarget) == 0
46
&& (flags[i - 3] & InstructionFlags.BranchTarget) == 0
47
&& code[i - 1].NormalizedOpCode == NormalizedByteCode.__ldc_nothrow
48
&& code[i - 2].NormalizedOpCode == NormalizedByteCode.__ldc
49
&& code[i - 3].NormalizedOpCode == NormalizedByteCode.__ldc)
51
// we now have a structural match, now we need to make sure that the argument values are what we expect
52
TypeWrapper tclass = classFile.GetConstantPoolClassType(code[i - 3].Arg1);
53
TypeWrapper vclass = classFile.GetConstantPoolClassType(code[i - 2].Arg1);
54
string fieldName = classFile.GetConstantPoolConstantString(code[i - 1].Arg1);
55
if (tclass == wrapper && !vclass.IsUnloadable && !vclass.IsPrimitive && !vclass.IsNonPrimitiveValueType)
57
FieldWrapper field = wrapper.GetFieldWrapper(fieldName, vclass.SigName);
58
if (field != null && !field.IsStatic && field.IsVolatile && field.DeclaringType == wrapper && field.FieldTypeWrapper == vclass)
60
// everything matches up, now call the actual emitter
61
ilgen.Emit(OpCodes.Pop);
62
ilgen.Emit(OpCodes.Pop);
63
ilgen.Emit(OpCodes.Pop);
64
ilgen.Emit(OpCodes.Newobj, context.GetAtomicReferenceFieldUpdater(field));
72
internal static void EmitImpl(TypeBuilder tb, FieldInfo field)
74
EmitCompareAndSet("compareAndSet", tb, field);
76
EmitSet("set", tb, field);
79
private static void EmitCompareAndSet(string name, TypeBuilder tb, FieldInfo field)
81
MethodBuilder compareAndSet = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, Types.Boolean, new Type[] { Types.Object, Types.Object, Types.Object });
82
ILGenerator ilgen = compareAndSet.GetILGenerator();
83
ilgen.Emit(OpCodes.Ldarg_1);
84
ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
85
ilgen.Emit(OpCodes.Ldflda, field);
86
ilgen.Emit(OpCodes.Ldarg_3);
87
ilgen.Emit(OpCodes.Castclass, field.FieldType);
88
ilgen.Emit(OpCodes.Ldarg_2);
89
ilgen.Emit(OpCodes.Castclass, field.FieldType);
90
ilgen.Emit(OpCodes.Call, MakeCompareExchange(field.FieldType));
91
ilgen.Emit(OpCodes.Ldarg_2);
92
ilgen.Emit(OpCodes.Ceq);
93
ilgen.Emit(OpCodes.Ret);
96
internal static MethodInfo MakeCompareExchange(Type type)
98
MethodInfo interlockedCompareExchange = null;
99
foreach (MethodInfo m in JVM.Import(typeof(System.Threading.Interlocked)).GetMethods())
101
if (m.Name == "CompareExchange" && m.IsGenericMethodDefinition)
103
interlockedCompareExchange = m;
107
return interlockedCompareExchange.MakeGenericMethod(type);
110
private static void EmitGet(TypeBuilder tb, FieldInfo field)
112
MethodBuilder get = tb.DefineMethod("get", MethodAttributes.Public | MethodAttributes.Virtual, Types.Object, new Type[] { Types.Object });
113
ILGenerator ilgen = get.GetILGenerator();
114
ilgen.Emit(OpCodes.Ldarg_1);
115
ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
116
ilgen.Emit(OpCodes.Volatile);
117
ilgen.Emit(OpCodes.Ldfld, field);
118
ilgen.Emit(OpCodes.Ret);
121
private static void EmitSet(string name, TypeBuilder tb, FieldInfo field)
123
MethodBuilder set = tb.DefineMethod(name, MethodAttributes.Public | MethodAttributes.Virtual, Types.Void, new Type[] { Types.Object, Types.Object });
124
CodeEmitter ilgen = CodeEmitter.Create(set);
125
ilgen.Emit(OpCodes.Ldarg_1);
126
ilgen.Emit(OpCodes.Castclass, field.DeclaringType);
127
ilgen.Emit(OpCodes.Ldarg_2);
128
ilgen.Emit(OpCodes.Castclass, field.FieldType);
129
ilgen.Emit(OpCodes.Volatile);
130
ilgen.Emit(OpCodes.Stfld, field);
131
ilgen.EmitMemoryBarrier();
132
ilgen.Emit(OpCodes.Ret);