本文整理汇总了C#中Compiler.StoreValue方法的典型用法代码示例。如果您正苦于以下问题:C# Compiler.StoreValue方法的具体用法?C# Compiler.StoreValue怎么用?C# Compiler.StoreValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Compiler
的用法示例。
在下文中一共展示了Compiler.StoreValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EmitRead
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
using (Compiler.Local oldValue = ctx.GetLocalWithValue(expectedType, valueFrom))
using (Compiler.Local token = new Compiler.Local(ctx, ctx.MapType(typeof(SubItemToken))))
using (Compiler.Local field = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
{
ctx.LoadReaderWriter();
ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("StartSubItem"));
ctx.StoreValue(token);
Compiler.CodeLabel next = ctx.DefineLabel(), processField = ctx.DefineLabel(), end = ctx.DefineLabel();
ctx.MarkLabel(next);
ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
ctx.CopyValue();
ctx.StoreValue(field);
ctx.LoadValue(Tag); // = 1 - process
ctx.BranchIfEqual(processField, true);
ctx.LoadValue(field);
ctx.LoadValue(1); // < 1 - exit
ctx.BranchIfLess(end, false);
// default: skip
ctx.LoadReaderWriter();
ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));
ctx.Branch(next, true);
// process
ctx.MarkLabel(processField);
if (Tail.RequiresOldValue)
{
if (Helpers.IsValueType(expectedType))
{
ctx.LoadAddress(oldValue, expectedType);
ctx.EmitCall(expectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes));
}
else
{
ctx.LoadValue(oldValue);
}
}
Tail.EmitRead(ctx, null);
// note we demanded always returns a value
if (Helpers.IsValueType(expectedType))
{
ctx.EmitCtor(expectedType, Tail.ExpectedType); // re-nullable<T> it
}
ctx.StoreValue(oldValue);
ctx.Branch(next, false);
// outro
ctx.MarkLabel(end);
ctx.LoadValue(token);
ctx.LoadReaderWriter();
ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("EndSubItem"));
ctx.LoadValue(oldValue); // load the old value
}
}
示例2: EmitDedicatedMethod
bool EmitDedicatedMethod(Compiler.CompilerContext ctx, Compiler.Local valueFrom, bool read)
{
System.Reflection.Emit.MethodBuilder method = ctx.GetDedicatedMethod(key, read);
if (method == null) return false;
using (Compiler.Local token = new ProtoBuf.Compiler.Local(ctx, typeof(SubItemToken)))
{
Type rwType = read ? typeof(ProtoReader) : typeof(ProtoWriter);
ctx.LoadValue(valueFrom);
if (!read) // write requires the object for StartSubItem; read doesn't
{
if (type.IsValueType) { ctx.LoadNullRef(); }
else { ctx.CopyValue(); }
}
ctx.LoadReaderWriter();
ctx.EmitCall(rwType.GetMethod("StartSubItem"));
ctx.StoreValue(token);
// note: value already on the stack
ctx.LoadReaderWriter();
ctx.EmitCall(method);
// handle inheritance (we will be calling the *base* version of things,
// but we expect Read to return the "type" type)
if (read && type != method.ReturnType) ctx.Cast(this.type);
ctx.LoadValue(token);
ctx.LoadReaderWriter();
ctx.EmitCall(rwType.GetMethod("EndSubItem"));
}
return true;
}
示例3: using
void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
Helpers.DebugAssert(valueFrom != null); // don't support stack-head for this
using (Compiler.Local converted = new Compiler.Local(ctx, tail.ExpectedType)) // declare/re-use local
{
ctx.LoadValue(valueFrom); // load primary onto stack
ctx.EmitCall(toTail); // static convert op, primary-to-surrogate
ctx.StoreValue(converted); // store into surrogate local
tail.EmitRead(ctx, converted); // downstream processing against surrogate local
ctx.LoadValue(converted); // load from surrogate local
ctx.EmitCall(fromTail); // static convert op, surrogate-to-primary
ctx.StoreValue(valueFrom); // store back into primary
}
}
示例4: EmitWriteArrayLoop
private void EmitWriteArrayLoop(Compiler.CompilerContext ctx, Compiler.Local i, Compiler.Local arr)
{
// i = 0
ctx.LoadValue(0);
ctx.StoreValue(i);
// range test is last (to minimise branches)
Compiler.CodeLabel loopTest = ctx.DefineLabel(), processItem = ctx.DefineLabel();
ctx.Branch(loopTest, false);
ctx.MarkLabel(processItem);
// {...}
ctx.LoadArrayValue(arr, i);
ctx.WriteNullCheckedTail(itemType, Tail, null);
// i++
ctx.LoadValue(i);
ctx.LoadValue(1);
ctx.Add();
ctx.StoreValue(i);
// i < arr.Length
ctx.MarkLabel(loopTest);
ctx.LoadValue(i);
ctx.LoadLength(arr, false);
ctx.BranchIfLess(processItem, false);
}
示例5: EmitRead
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
{
ctx.LoadAddress(loc, ExpectedType);
if (Tail.RequiresOldValue)
{
ctx.CopyValue();
ctx.LoadValue(field);
}
// value is either now on the stack or not needed
ctx.ReadNullCheckedTail(field.FieldType, Tail, null);
if (Tail.ReturnsValue)
{
if (field.FieldType.IsValueType)
{
// stack is now the return value
ctx.StoreValue(field);
}
else
{
Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel();
ctx.CopyValue();
ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign"
// no value, discard
ctx.DiscardValue();
ctx.DiscardValue();
ctx.Branch(allDone, true);
ctx.MarkLabel(hasValue);
ctx.StoreValue(field);
ctx.MarkLabel(allDone);
}
}
else
{
ctx.DiscardValue();
}
}
}
示例6: EmitRead
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
{
if (Tail.RequiresOldValue)
{
ctx.LoadAddress(loc, ExpectedType);
ctx.LoadValue(field);
}
// value is either now on the stack or not needed
ctx.ReadNullCheckedTail(field.FieldType, Tail, null);
if (Tail.ReturnsValue)
{
using (Compiler.Local newVal = new Compiler.Local(ctx, field.FieldType))
{
ctx.StoreValue(newVal);
if (Helpers.IsValueType(field.FieldType))
{
ctx.LoadAddress(loc, ExpectedType);
ctx.LoadValue(newVal);
ctx.StoreValue(field);
}
else
{
Compiler.CodeLabel allDone = ctx.DefineLabel();
ctx.LoadValue(newVal);
ctx.BranchIfFalse(allDone, true); // interpret null as "don't assign"
ctx.LoadAddress(loc, ExpectedType);
ctx.LoadValue(newVal);
ctx.StoreValue(field);
ctx.MarkLabel(allDone);
}
}
}
}
}
示例7: EmitRead
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
{
ctx.LoadAddress(loc, ExpectedType);
if (Tail.RequiresOldValue)
{
ctx.CopyValue();
ctx.LoadValue(field);
}
// value is either now on the stack or not needed
ctx.ReadNullCheckedTail(field.FieldType, Tail, null);
// stack is now the return value
ctx.StoreValue(field);
}
}
示例8: EmitReadEndPacked
private void EmitReadEndPacked(Compiler.CompilerContext ctx, Compiler.Local list, Compiler.Local oldArr,
Compiler.Local newArr, Type listType)
{
// leave this "using" here, as it can share the "FieldNumber" local with EmitReadList
using(Compiler.Local oldLen = AppendToCollection ? new ProtoBuf.Compiler.Local(ctx, typeof(int)) : null) {
Type[] copyToArrayInt32Args = new Type[] { typeof(Array), typeof(int) };
if (AppendToCollection)
{
ctx.LoadLength(oldArr, true);
ctx.CopyValue();
ctx.StoreValue(oldLen);
ctx.LoadAddress(list, listType);
ctx.LoadValue(listType.GetProperty("Count"));
ctx.Add();
ctx.CreateArray(itemType, null); // length is on the stack
ctx.StoreValue(newArr);
ctx.LoadValue(oldLen);
Compiler.CodeLabel nothingToCopy = ctx.DefineLabel();
ctx.BranchIfFalse(nothingToCopy, true);
ctx.LoadValue(oldArr);
ctx.LoadValue(newArr);
ctx.LoadValue(0); // index in target
ctx.EmitCall(ExpectedType.GetMethod("CopyTo", copyToArrayInt32Args));
ctx.MarkLabel(nothingToCopy);
ctx.LoadValue(list);
ctx.LoadValue(newArr);
ctx.LoadValue(oldLen);
}
else
{
ctx.LoadAddress(list, listType);
ctx.LoadValue(listType.GetProperty("Count"));
ctx.CreateArray(itemType, null);
ctx.StoreValue(newArr);
ctx.LoadAddress(list, listType);
ctx.LoadValue(newArr);
ctx.LoadValue(0);
}
copyToArrayInt32Args[0] = ExpectedType; // // prefer: CopyTo(T[], int)
MethodInfo copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
if (copyTo == null)
{ // fallback: CopyTo(Array, int)
copyToArrayInt32Args[1] = typeof(Array);
copyTo = listType.GetMethod("CopyTo", copyToArrayInt32Args);
}
ctx.EmitCall(copyTo);
}
ctx.LoadValue(newArr);
}
示例9: EmitWrite
public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
Compiler.CodeLabel @end = ctx.DefineLabel();
using (Compiler.Local value = ctx.GetLocalWithValue(ctor.DeclaringType, valueFrom))
{
if (!baseTupleAsReference && asReference)
{
using (Compiler.Local existing = new Compiler.Local(ctx, typeof(bool)))
using (Compiler.Local tupleKey = new Compiler.Local(ctx, typeof(int)))
{
//int tupleKey = dest.NetCache.AddObjectKey(value, out existing);
ctx.LoadReaderWriter();
ctx.LoadValue(typeof(ProtoWriter).GetProperty("NetCache"));
ctx.LoadValue(value);
ctx.CastToObject(ctor.DeclaringType); // HACK : doesn't seem to get boxed from ctx.GetLocalWithValue
ctx.LoadAddress(existing, typeof(bool));
ctx.EmitCall(typeof(NetObjectCache).GetMethod("AddObjectKey", new Type[] { typeof(object), typeof(bool).MakeByRefType() }));
ctx.StoreValue(tupleKey);
//ProtoWriter.WriteUInt32(existing ? (uint) tupleKey : 0, dest);
Compiler.CodeLabel @continueBranch = ctx.DefineLabel();
ctx.LoadValue(0);
ctx.LoadValue(existing);
ctx.BranchIfFalse(@continueBranch, true);
ctx.DiscardValue();
ctx.LoadValue(tupleKey);
//ctx.CastToObject(typeof(uint));
ctx.MarkLabel(@continueBranch);
ctx.LoadReaderWriter();
ctx.EmitCall(typeof(ProtoWriter).GetMethod("WriteUInt32"));
//if (existing) { return; }
ctx.LoadValue(existing);
ctx.BranchIfTrue(@end, false);
}
}
for (int i = 0; i < tails.Length; i++)
{
Type type = GetMemberType(i);
ctx.LoadAddress(value, ExpectedType);
switch(members[i].MemberType)
{
case MemberTypes.Field:
ctx.LoadValue((FieldInfo)members[i]);
break;
case MemberTypes.Property:
ctx.LoadValue((PropertyInfo)members[i]);
break;
}
ctx.WriteNullCheckedTail(type, tails[i], null);
}
}
if (forceIssueFakeHeader)
{
ctx.LoadReaderWriter();
ctx.EmitCall(typeof(ProtoWriter).GetMethod("WriteEndGroupFieldHeaderHack"));
}
ctx.MarkLabel(@end);
}
示例10: EmitWrite
protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
using(Compiler.Local valOrNull = ctx.GetLocalWithValue(expectedType, valueFrom))
using(Compiler.Local token = new Compiler.Local(ctx, ctx.MapType(typeof(SubItemToken))))
{
ctx.LoadNullRef();
ctx.LoadReaderWriter();
ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("StartSubItem"));
ctx.StoreValue(token);
if (Helpers.IsValueType(expectedType))
{
ctx.LoadAddress(valOrNull, expectedType);
ctx.LoadValue(expectedType.GetProperty("HasValue"));
}
else
{
ctx.LoadValue(valOrNull);
}
Compiler.CodeLabel @end = ctx.DefineLabel();
ctx.BranchIfFalse(@end, false);
if (Helpers.IsValueType(expectedType))
{
ctx.LoadAddress(valOrNull, expectedType);
ctx.EmitCall(expectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes));
}
else
{
ctx.LoadValue(valOrNull);
}
Tail.EmitWrite(ctx, null);
ctx.MarkLabel(@end);
ctx.LoadValue(token);
ctx.LoadReaderWriter();
ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("EndSubItem"));
}
}
示例11: EmitRead
protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
bool writeValue;
SanityCheck(ctx.Model, property, Tail, out writeValue, ctx.NonPublic, ctx.AllowInternal(property));
if (ExpectedType.IsValueType && valueFrom == null)
{
throw new InvalidOperationException("Attempt to mutate struct on the head of the stack; changes would be lost");
}
ctx.LoadAddress(valueFrom, ExpectedType); // stack is: old-addr
if (writeValue && Tail.RequiresOldValue)
{ // need to read and write
ctx.CopyValue();
}
// stack is: [old-addr]|old-addr
if (Tail.RequiresOldValue)
{
ctx.LoadValue(property); // stack is: [old-addr]|old-value
}
Type propertyType = property.PropertyType;
ctx.ReadNullCheckedTail(propertyType, Tail, null); // stack is [old-addr]|[new-value]
if (writeValue)
{
// stack is old-addr|new-value
Compiler.CodeLabel @skip = new Compiler.CodeLabel(), allDone = new Compiler.CodeLabel(); // <=== default structs
if (!propertyType.IsValueType)
{ // if the tail returns a null, intepret that as *no assign*
ctx.CopyValue(); // old-addr|new-value|new-value
@skip = ctx.DefineLabel();
allDone = ctx.DefineLabel();
ctx.BranchIfFalse(@skip, true); // old-addr|new-value
}
if (shadowSetter == null)
{
ctx.StoreValue(property);
}
else
{
ctx.EmitCall(shadowSetter);
}
if (!propertyType.IsValueType)
{
ctx.Branch(allDone, true);
ctx.MarkLabel(@skip); // old-addr|new-value
ctx.DiscardValue();
ctx.DiscardValue();
ctx.MarkLabel(allDone);
}
}
else
{ // don't want return value; drop it if anything there
// stack is [new-value]
if (Tail.ReturnsValue) { ctx.DiscardValue(); }
}
}
示例12: EmitRead
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
{
bool issueReferenceDirectives = !baseTupleAsReference && asReference;
Compiler.CodeLabel @end = ctx.DefineLabel();
using (Compiler.Local tupleKey = new Compiler.Local(ctx, typeof(int)))
using (Compiler.Local oldTuple = new Compiler.Local(ctx, typeof(object)))
using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
{
Compiler.Local[] locals = new Compiler.Local[members.Length];
try
{
if (issueReferenceDirectives)
{
//int tupleKey = 0;
ctx.LoadValue(0);
ctx.StoreValue(tupleKey);
//object oldTuple = null;
ctx.LoadNullRef();
ctx.StoreValue(oldTuple);
//tupleKey = (int)source.ReadUInt32();
ctx.LoadReaderWriter();
ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadUInt32"));
//ctx.CastToObject(typeof (int));
ctx.StoreValue(tupleKey);
Compiler.CodeLabel @objectNotFound = ctx.DefineLabel();
ctx.LoadValue(tupleKey);
ctx.LoadValue(0);
ctx.BranchIfEqual(@objectNotFound, true);
//// return source.NetCache.GetKeyedObject(tupleKey);
ctx.LoadReaderWriter();
ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache"));
ctx.LoadValue(tupleKey);
ctx.EmitCall(typeof (NetObjectCache).GetMethod("GetKeyedObject"));
ctx.CastFromObject(ExpectedType);
ctx.StoreValue(objValue);
ctx.Branch(@end, false);
ctx.MarkLabel(@objectNotFound);
ctx.EmitCtor(typeof (object));
ctx.StoreValue(oldTuple);
// tupleKey = source.NetCache.AddObjectKey(oldTuple, out dummy);
using (Compiler.Local dummy = new Compiler.Local(ctx, typeof (bool)))
{
ctx.LoadReaderWriter();
ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache"));
ctx.LoadValue(oldTuple);
ctx.LoadAddress(dummy, typeof (bool));
ctx.EmitCall(typeof (NetObjectCache).GetMethod("AddObjectKey", new Type[] { typeof(object), typeof(bool).MakeByRefType() }));
ctx.StoreValue(tupleKey);
}
}
for (int i = 0; i < locals.Length; i++)
{
Type type = GetMemberType(i);
bool store = true;
locals[i] = new Compiler.Local(ctx, type);
if (!ExpectedType.IsValueType)
{
// value-types always read the old value
if (type.IsValueType)
{
switch (Helpers.GetTypeCode(type))
{
case ProtoTypeCode.Boolean:
case ProtoTypeCode.Byte:
case ProtoTypeCode.Int16:
case ProtoTypeCode.Int32:
case ProtoTypeCode.SByte:
case ProtoTypeCode.UInt16:
case ProtoTypeCode.UInt32:
ctx.LoadValue(0);
break;
case ProtoTypeCode.Int64:
case ProtoTypeCode.UInt64:
ctx.LoadValue(0L);
break;
case ProtoTypeCode.Single:
ctx.LoadValue(0.0F);
break;
case ProtoTypeCode.Double:
ctx.LoadValue(0.0D);
break;
case ProtoTypeCode.Decimal:
ctx.LoadValue(0M);
break;
case ProtoTypeCode.Guid:
ctx.LoadValue(Guid.Empty);
break;
default:
ctx.LoadAddress(locals[i], type);
//.........这里部分代码省略.........
示例13: EmitCreateIfNull
private void EmitCreateIfNull(Compiler.CompilerContext ctx, Type type, Compiler.Local storage)
{
Helpers.DebugAssert(storage != null);
if (!type.IsValueType)
{
Compiler.CodeLabel afterNullCheck = ctx.DefineLabel();
ctx.LoadValue(storage);
ctx.BranchIfTrue(afterNullCheck, true);
// different ways of creating a new instance
if (!useConstructor)
{ // DataContractSerializer style
ctx.LoadValue(forType);
ctx.EmitCall(typeof(BclHelpers).GetMethod("GetUninitializedObject"));
ctx.Cast(forType);
} else if (type.IsClass && !type.IsAbstract && (
(type.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
null, Helpers.EmptyTypes, null)) != null))
{ // XmlSerializer style
ctx.EmitCtor(type);
}
else
{
ctx.LoadValue(type);
ctx.EmitCall(typeof(TypeModel).GetMethod("ThrowCannotCreateInstance",
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic));
ctx.LoadNullRef();
}
if (baseCtorCallbacks != null) {
for (int i = 0; i < baseCtorCallbacks.Length; i++) {
EmitInvokeCallback(ctx, baseCtorCallbacks[i]);
}
}
if (callbacks != null) EmitInvokeCallback(ctx, callbacks.BeforeDeserialize);
ctx.StoreValue(storage);
ctx.MarkLabel(afterNullCheck);
}
}
示例14: EmitRead
public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local incoming)
{
using (Compiler.Local objValue = ctx.GetLocalWithValue(ExpectedType, incoming))
{
Compiler.Local[] locals = new Compiler.Local[members.Length];
try
{
for (int i = 0; i < locals.Length; i++)
{
Type type = GetMemberType(i);
bool store = true;
locals[i] = new Compiler.Local(ctx, type);
if (!Helpers.IsValueType(ExpectedType))
{
// value-types always read the old value
if (Helpers.IsValueType(type))
{
switch (Helpers.GetTypeCode(type))
{
case ProtoTypeCode.Boolean:
case ProtoTypeCode.Byte:
case ProtoTypeCode.Int16:
case ProtoTypeCode.Int32:
case ProtoTypeCode.SByte:
case ProtoTypeCode.UInt16:
case ProtoTypeCode.UInt32:
ctx.LoadValue(0);
break;
case ProtoTypeCode.Int64:
case ProtoTypeCode.UInt64:
ctx.LoadValue(0L);
break;
case ProtoTypeCode.Single:
ctx.LoadValue(0.0F);
break;
case ProtoTypeCode.Double:
ctx.LoadValue(0.0D);
break;
case ProtoTypeCode.Decimal:
ctx.LoadValue(0M);
break;
case ProtoTypeCode.Guid:
ctx.LoadValue(Guid.Empty);
break;
default:
ctx.LoadAddress(locals[i], type);
ctx.EmitCtor(type);
store = false;
break;
}
}
else
{
ctx.LoadNullRef();
}
if (store)
{
ctx.StoreValue(locals[i]);
}
}
}
Compiler.CodeLabel skipOld = Helpers.IsValueType(ExpectedType)
? new Compiler.CodeLabel()
: ctx.DefineLabel();
if (!Helpers.IsValueType(ExpectedType))
{
ctx.LoadAddress(objValue, ExpectedType);
ctx.BranchIfFalse(skipOld, false);
}
for (int i = 0; i < members.Length; i++)
{
ctx.LoadAddress(objValue, ExpectedType);
if (members[i] is FieldInfo)
{
ctx.LoadValue((FieldInfo)members[i]);
}
else if (members[i] is PropertyInfo)
{
ctx.LoadValue((PropertyInfo)members[i]);
}
ctx.StoreValue(locals[i]);
}
if (!Helpers.IsValueType(ExpectedType)) ctx.MarkLabel(skipOld);
using (Compiler.Local fieldNumber = new Compiler.Local(ctx, ctx.MapType(typeof(int))))
{
Compiler.CodeLabel @continue = ctx.DefineLabel(),
processField = ctx.DefineLabel(),
notRecognised = ctx.DefineLabel();
ctx.Branch(@continue, false);
Compiler.CodeLabel[] handlers = new Compiler.CodeLabel[members.Length];
for (int i = 0; i < members.Length; i++)
{
handlers[i] = ctx.DefineLabel();
}
ctx.MarkLabel(processField);
//.........这里部分代码省略.........
示例15: GetTypeCode
void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
{
TypeCode typeCode = GetTypeCode();
if (map == null)
{
ctx.EmitBasicRead("ReadInt32", typeof(int));
ctx.ConvertFromInt32(typeCode);
}
else
{
int[] wireValues = new int[map.Length];
object[] values = new object[map.Length];
for (int i = 0; i < map.Length; i++)
{
wireValues[i] = map[i].WireValue;
values[i] = map[i].Value;
}
using (Compiler.Local result = new Compiler.Local(ctx, ExpectedType))
using (Compiler.Local wireValue = new Compiler.Local(ctx, typeof(int)))
{
ctx.EmitBasicRead("ReadInt32", typeof(int));
ctx.StoreValue(wireValue);
Compiler.CodeLabel @continue = ctx.DefineLabel();
foreach (BasicList.Group group in BasicList.GetContiguousGroups(wireValues, values))
{
Compiler.CodeLabel tryNextGroup = ctx.DefineLabel();
int groupItemCount = group.Items.Count;
if (groupItemCount == 1)
{
// discreet group; use an equality test
ctx.LoadValue(wireValue);
ctx.LoadValue(group.First);
Compiler.CodeLabel processThisValue = ctx.DefineLabel();
ctx.BranchIfEqual(processThisValue, true);
ctx.Branch(tryNextGroup, false);
WriteEnumValue(ctx, typeCode, processThisValue, @continue, group.Items[0], @result);
}
else
{
// implement as a jump-table-based switch
ctx.LoadValue(wireValue);
ctx.LoadValue(group.First);
ctx.Subtract(); // jump-tables are zero-based
Compiler.CodeLabel[] jmp = new Compiler.CodeLabel[groupItemCount];
for (int i = 0; i < groupItemCount; i++) {
jmp[i] = ctx.DefineLabel();
}
ctx.Switch(jmp);
// write the default...
ctx.Branch(tryNextGroup, false);
for (int i = 0; i < groupItemCount; i++)
{
WriteEnumValue(ctx, typeCode, jmp[i], @continue, group.Items[i], @result);
}
}
ctx.MarkLabel(tryNextGroup);
}
// throw source.CreateEnumException(ExpectedType, wireValue);
ctx.LoadReaderWriter();
ctx.LoadValue(ExpectedType);
ctx.LoadValue(wireValue);
ctx.EmitCall(typeof(ProtoReader).GetMethod("ThrowEnumException"));
ctx.MarkLabel(@continue);
ctx.LoadValue(result);
}
}
}