void ProtoBuf.Serializers.IProtoSerializer.EmitWrite(CompilerContext ctx, Local valueFrom) { ProtoTypeCode typeCode = this.GetTypeCode(); if (this.map == null) { ctx.LoadValue(valueFrom); ctx.ConvertToInt32(typeCode, false); ctx.EmitBasicWrite("WriteInt32", null); return; } using (Local localWithValue = ctx.GetLocalWithValue(this.ExpectedType, valueFrom)) { CodeLabel codeLabel = ctx.DefineLabel(); for (int i = 0; i < (int)this.map.Length; i++) { CodeLabel codeLabel1 = ctx.DefineLabel(); CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.LoadValue(localWithValue); EnumSerializer.WriteEnumValue(ctx, typeCode, this.map[i].RawValue); ctx.BranchIfEqual(codeLabel2, true); ctx.Branch(codeLabel1, true); ctx.MarkLabel(codeLabel2); ctx.LoadValue(this.map[i].WireValue); ctx.EmitBasicWrite("WriteInt32", null); ctx.Branch(codeLabel, false); ctx.MarkLabel(codeLabel1); } ctx.LoadReaderWriter(); ctx.LoadValue(localWithValue); ctx.CastToObject(this.ExpectedType); ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("ThrowEnumException")); ctx.MarkLabel(codeLabel); } }
private void EmitBeq(CompilerContext ctx, CodeLabel label, Type type) { switch (Helpers.GetTypeCode(type)) { case ProtoTypeCode.Boolean: case ProtoTypeCode.Char: case ProtoTypeCode.SByte: case ProtoTypeCode.Byte: case ProtoTypeCode.Int16: case ProtoTypeCode.UInt16: case ProtoTypeCode.Int32: case ProtoTypeCode.UInt32: case ProtoTypeCode.Int64: case ProtoTypeCode.UInt64: case ProtoTypeCode.Single: case ProtoTypeCode.Double: ctx.BranchIfEqual(label, false); return; } MethodInfo method = type.GetMethod("op_Equality", BindingFlags.Public | BindingFlags.Static, null, new Type[] { type, type }, null); if ((method == null) || (method.ReturnType != ctx.MapType(typeof(bool)))) { throw new InvalidOperationException("No suitable equality operator found for default-values of type: " + type.FullName); } ctx.EmitCall(method); ctx.BranchIfTrue(label, false); }
protected override void EmitRead(CompilerContext ctx, Local valueFrom) { using (Local localWithValue = ctx.GetLocalWithValue(this.expectedType, valueFrom)) { using (Local local = new Local(ctx, ctx.MapType(typeof(SubItemToken)))) { using (Local local1 = new Local(ctx, ctx.MapType(typeof(int)))) { ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("StartSubItem")); ctx.StoreValue(local); CodeLabel codeLabel = ctx.DefineLabel(); CodeLabel codeLabel1 = ctx.DefineLabel(); CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.MarkLabel(codeLabel); ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int))); ctx.CopyValue(); ctx.StoreValue(local1); ctx.LoadValue(1); ctx.BranchIfEqual(codeLabel1, true); ctx.LoadValue(local1); ctx.LoadValue(1); ctx.BranchIfLess(codeLabel2, false); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField")); ctx.Branch(codeLabel, true); ctx.MarkLabel(codeLabel1); if (this.Tail.RequiresOldValue) { if (!this.expectedType.IsValueType) { ctx.LoadValue(localWithValue); } else { ctx.LoadAddress(localWithValue, this.expectedType); ctx.EmitCall(this.expectedType.GetMethod("GetValueOrDefault", Helpers.EmptyTypes)); } } this.Tail.EmitRead(ctx, null); if (this.expectedType.IsValueType) { Type type = this.expectedType; Type[] expectedType = new Type[] { this.Tail.ExpectedType }; ctx.EmitCtor(type, expectedType); } ctx.StoreValue(localWithValue); ctx.Branch(codeLabel, false); ctx.MarkLabel(codeLabel2); ctx.LoadValue(local); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("EndSubItem")); ctx.LoadValue(localWithValue); } } } }
protected override void EmitRead(CompilerContext ctx, Local valueFrom) { bool returnList = this.ReturnList; bool castListForAdd = !this.add.DeclaringType.IsAssignableFrom(this.declaredType); using (Local local = this.AppendToCollection ? ctx.GetLocalWithValue(this.ExpectedType, valueFrom) : new Local(ctx, this.declaredType)) { using (Local local2 = (returnList && this.AppendToCollection) ? new Local(ctx, this.ExpectedType) : null) { if (!this.AppendToCollection) { ctx.LoadNullRef(); ctx.StoreValue(local); } else if (returnList) { ctx.LoadValue(local); ctx.StoreValue(local2); } if (this.concreteType != null) { ctx.LoadValue(local); CodeLabel label = ctx.DefineLabel(); ctx.BranchIfTrue(label, true); ctx.EmitCtor(this.concreteType); ctx.StoreValue(local); ctx.MarkLabel(label); } EmitReadList(ctx, local, base.Tail, this.add, this.packedWireType, castListForAdd); if (returnList) { if (this.AppendToCollection) { ctx.LoadValue(local2); ctx.LoadValue(local); CodeLabel label2 = ctx.DefineLabel(); CodeLabel label3 = ctx.DefineLabel(); ctx.BranchIfEqual(label2, true); ctx.LoadValue(local); ctx.Branch(label3, true); ctx.MarkLabel(label2); ctx.LoadNullRef(); ctx.MarkLabel(label3); } else { ctx.LoadValue(local); } } } } }
internal static void EmitReadList(CompilerContext ctx, Local list, IProtoSerializer tail, MethodInfo add, WireType packedWireType, bool castListForAdd) { using (Local local = new Local(ctx, ctx.MapType(typeof(int)))) { CodeLabel codeLabel = (packedWireType == WireType.None ? new CodeLabel() : ctx.DefineLabel()); if (packedWireType != WireType.None) { ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("WireType")); ctx.LoadValue(2); ctx.BranchIfEqual(codeLabel, false); } ctx.LoadReaderWriter(); ctx.LoadValue(typeof(ProtoReader).GetProperty("FieldNumber")); ctx.StoreValue(local); CodeLabel codeLabel1 = ctx.DefineLabel(); ctx.MarkLabel(codeLabel1); ListDecorator.EmitReadAndAddItem(ctx, list, tail, add, castListForAdd); ctx.LoadReaderWriter(); ctx.LoadValue(local); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("TryReadFieldHeader")); ctx.BranchIfTrue(codeLabel1, false); if (packedWireType != WireType.None) { CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.Branch(codeLabel2, false); ctx.MarkLabel(codeLabel); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("StartSubItem")); CodeLabel codeLabel3 = ctx.DefineLabel(); CodeLabel codeLabel4 = ctx.DefineLabel(); ctx.MarkLabel(codeLabel3); ctx.LoadValue((int)packedWireType); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("HasSubValue")); ctx.BranchIfFalse(codeLabel4, false); ListDecorator.EmitReadAndAddItem(ctx, list, tail, add, castListForAdd); ctx.Branch(codeLabel3, false); ctx.MarkLabel(codeLabel4); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("EndSubItem")); ctx.MarkLabel(codeLabel2); } } }
protected override void EmitRead(CompilerContext ctx, Local valueFrom) { Local local; bool returnList = this.ReturnList; using (Local local1 = (this.AppendToCollection ? ctx.GetLocalWithValue(this.ExpectedType, valueFrom) : new Local(ctx, this.declaredType))) { if (!returnList || !this.AppendToCollection) { local = null; } else { local = new Local(ctx, this.ExpectedType); } using (Local local2 = local) { if (!this.AppendToCollection) { ctx.LoadNullRef(); ctx.StoreValue(local1); } else if (returnList) { ctx.LoadValue(local1); ctx.StoreValue(local2); } if (this.concreteType != null) { ctx.LoadValue(local1); CodeLabel codeLabel = ctx.DefineLabel(); ctx.BranchIfTrue(codeLabel, true); ctx.EmitCtor(this.concreteType); ctx.StoreValue(local1); ctx.MarkLabel(codeLabel); } bool flag = [email protected](this.declaredType); ListDecorator.EmitReadList(ctx, local1, this.Tail, this.@add, this.packedWireType, flag); if (returnList) { if (!this.AppendToCollection) { ctx.LoadValue(local1); } else { ctx.LoadValue(local2); ctx.LoadValue(local1); CodeLabel codeLabel1 = ctx.DefineLabel(); CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.BranchIfEqual(codeLabel1, true); ctx.LoadValue(local1); ctx.Branch(codeLabel2, true); ctx.MarkLabel(codeLabel1); ctx.LoadNullRef(); ctx.MarkLabel(codeLabel2); } } } } }
void IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom) { ProtoTypeCode typeCode = this.GetTypeCode(); if (this.map == null) { ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int))); ctx.ConvertFromInt32(typeCode, false); } else { int[] keys = new int[this.map.Length]; object[] values = new object[this.map.Length]; for (int i = 0; i < this.map.Length; i++) { keys[i] = this.map[i].WireValue; values[i] = this.map[i].RawValue; } using (Local local = new Local(ctx, this.ExpectedType)) { using (Local local2 = new Local(ctx, ctx.MapType(typeof(int)))) { ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int))); ctx.StoreValue(local2); CodeLabel label = ctx.DefineLabel(); BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(keys, values).GetEnumerator(); while (enumerator.MoveNext()) { BasicList.Group current = (BasicList.Group)enumerator.Current; CodeLabel label2 = ctx.DefineLabel(); int count = current.Items.Count; if (count == 1) { ctx.LoadValue(local2); ctx.LoadValue(current.First); CodeLabel label3 = ctx.DefineLabel(); ctx.BranchIfEqual(label3, true); ctx.Branch(label2, false); WriteEnumValue(ctx, typeCode, label3, label, current.Items[0], local); } else { ctx.LoadValue(local2); ctx.LoadValue(current.First); ctx.Subtract(); CodeLabel[] jumpTable = new CodeLabel[count]; for (int j = 0; j < count; j++) { jumpTable[j] = ctx.DefineLabel(); } ctx.Switch(jumpTable); ctx.Branch(label2, false); for (int k = 0; k < count; k++) { WriteEnumValue(ctx, typeCode, jumpTable[k], label, current.Items[k], local); } } ctx.MarkLabel(label2); } ctx.LoadReaderWriter(); ctx.LoadValue(this.ExpectedType); ctx.LoadValue(local2); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException")); ctx.MarkLabel(label); ctx.LoadValue(local); } } } }
void IProtoSerializer.EmitWrite(CompilerContext ctx, Local valueFrom) { Type expectedType = this.ExpectedType; using (Local local = ctx.GetLocalWithValue(expectedType, valueFrom)) { this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.BeforeSerialize); CodeLabel label = ctx.DefineLabel(); if (!this.CanHaveInheritance) { goto Label_0206; } for (int i = 0; i < this.serializers.Length; i++) { IProtoSerializer serializer = this.serializers[i]; Type type = serializer.ExpectedType; if (type != this.forType) { CodeLabel label2 = ctx.DefineLabel(); CodeLabel label3 = ctx.DefineLabel(); ctx.LoadValue(local); ctx.TryCast(type); ctx.CopyValue(); ctx.BranchIfTrue(label2, true); ctx.DiscardValue(); ctx.Branch(label3, true); ctx.MarkLabel(label2); serializer.EmitWrite(ctx, null); ctx.Branch(label, false); ctx.MarkLabel(label3); } } if ((this.constructType != null) && (this.constructType != this.forType)) { using (Local local2 = new Local(ctx, ctx.MapType(typeof(Type)))) { ctx.LoadValue(local); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.CopyValue(); ctx.StoreValue(local2); ctx.LoadValue(this.forType); ctx.BranchIfEqual(label, true); ctx.LoadValue(local2); ctx.LoadValue(this.constructType); ctx.BranchIfEqual(label, true); goto Label_01B1; } } ctx.LoadValue(local); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.LoadValue(this.forType); ctx.BranchIfEqual(label, true); Label_01B1: ctx.LoadValue(this.forType); ctx.LoadValue(local); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.EmitCall(ctx.MapType(typeof(TypeModel)).GetMethod("ThrowUnexpectedSubtype", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)); Label_0206: ctx.MarkLabel(label); for (int j = 0; j < this.serializers.Length; j++) { IProtoSerializer serializer2 = this.serializers[j]; if (serializer2.ExpectedType == this.forType) { serializer2.EmitWrite(ctx, local); } } if (this.isExtensible) { ctx.LoadValue(local); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("AppendExtensionData")); } this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.AfterSerialize); } }
void IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom) { Type expectedType = this.ExpectedType; using (Local local = ctx.GetLocalWithValue(expectedType, valueFrom)) { using (Local local2 = new Local(ctx, ctx.MapType(typeof(int)))) { if (this.HasCallbacks(TypeModel.CallbackType.BeforeDeserialize)) { if (this.ExpectedType.IsValueType) { this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.BeforeDeserialize); } else { CodeLabel label = ctx.DefineLabel(); ctx.LoadValue(local); ctx.BranchIfFalse(label, false); this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.BeforeDeserialize); ctx.MarkLabel(label); } } CodeLabel label2 = ctx.DefineLabel(); CodeLabel label3 = ctx.DefineLabel(); ctx.Branch(label2, false); ctx.MarkLabel(label3); BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(this.fieldNumbers, this.serializers).GetEnumerator(); while (enumerator.MoveNext()) { BasicList.Group current = (BasicList.Group)enumerator.Current; CodeLabel label4 = ctx.DefineLabel(); int count = current.Items.Count; if (count == 1) { ctx.LoadValue(local2); ctx.LoadValue(current.First); CodeLabel label5 = ctx.DefineLabel(); ctx.BranchIfEqual(label5, true); ctx.Branch(label4, false); this.WriteFieldHandler(ctx, expectedType, local, label5, label2, (IProtoSerializer)current.Items[0]); } else { ctx.LoadValue(local2); ctx.LoadValue(current.First); ctx.Subtract(); CodeLabel[] jumpTable = new CodeLabel[count]; for (int i = 0; i < count; i++) { jumpTable[i] = ctx.DefineLabel(); } ctx.Switch(jumpTable); ctx.Branch(label4, false); for (int j = 0; j < count; j++) { this.WriteFieldHandler(ctx, expectedType, local, jumpTable[j], label2, (IProtoSerializer)current.Items[j]); } } ctx.MarkLabel(label4); } this.EmitCreateIfNull(ctx, local); ctx.LoadReaderWriter(); if (this.isExtensible) { ctx.LoadValue(local); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("AppendExtensionData")); } else { ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField")); } ctx.MarkLabel(label2); ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int))); ctx.CopyValue(); ctx.StoreValue(local2); ctx.LoadValue(0); ctx.BranchIfGreater(label3, false); this.EmitCreateIfNull(ctx, local); this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.AfterDeserialize); if ((valueFrom != null) && !local.IsSame(valueFrom)) { ctx.LoadValue(local); ctx.Cast(valueFrom.Type); ctx.StoreValue(valueFrom); } } } }
void ProtoBuf.Serializers.IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom) { ProtoTypeCode typeCode = this.GetTypeCode(); if (this.map == null) { ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int))); ctx.ConvertFromInt32(typeCode, false); return; } int[] wireValue = new int[(int)this.map.Length]; object[] rawValue = new object[(int)this.map.Length]; for (int i = 0; i < (int)this.map.Length; i++) { wireValue[i] = this.map[i].WireValue; rawValue[i] = this.map[i].RawValue; } using (Local local = new Local(ctx, this.ExpectedType)) { using (Local local1 = new Local(ctx, ctx.MapType(typeof(int)))) { ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int))); ctx.StoreValue(local1); CodeLabel codeLabel = ctx.DefineLabel(); BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(wireValue, rawValue).GetEnumerator(); while (enumerator.MoveNext()) { BasicList.Group current = (BasicList.Group)enumerator.Current; CodeLabel codeLabel1 = ctx.DefineLabel(); int count = current.Items.Count; if (count != 1) { ctx.LoadValue(local1); ctx.LoadValue(current.First); ctx.Subtract(); CodeLabel[] codeLabelArray = new CodeLabel[count]; for (int j = 0; j < count; j++) { codeLabelArray[j] = ctx.DefineLabel(); } ctx.Switch(codeLabelArray); ctx.Branch(codeLabel1, false); for (int k = 0; k < count; k++) { EnumSerializer.WriteEnumValue(ctx, typeCode, codeLabelArray[k], codeLabel, current.Items[k], local); } } else { ctx.LoadValue(local1); ctx.LoadValue(current.First); CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.BranchIfEqual(codeLabel2, true); ctx.Branch(codeLabel1, false); EnumSerializer.WriteEnumValue(ctx, typeCode, codeLabel2, codeLabel, current.Items[0], local); } ctx.MarkLabel(codeLabel1); } ctx.LoadReaderWriter(); ctx.LoadValue(this.ExpectedType); ctx.LoadValue(local1); ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException")); ctx.MarkLabel(codeLabel); ctx.LoadValue(local); } } }
void ProtoBuf.Serializers.IProtoSerializer.EmitWrite(CompilerContext ctx, Local valueFrom) { using (Local localWithValue = ctx.GetLocalWithValue(this.ExpectedType, valueFrom)) { this.EmitCallbackIfNeeded(ctx, localWithValue, TypeModel.CallbackType.BeforeSerialize); CodeLabel codeLabel = ctx.DefineLabel(); if (this.CanHaveInheritance) { for (int i = 0; i < (int)this.serializers.Length; i++) { IProtoSerializer protoSerializer = this.serializers[i]; Type expectedType = protoSerializer.ExpectedType; if (expectedType != this.forType) { CodeLabel codeLabel1 = ctx.DefineLabel(); CodeLabel codeLabel2 = ctx.DefineLabel(); ctx.LoadValue(localWithValue); ctx.TryCast(expectedType); ctx.CopyValue(); ctx.BranchIfTrue(codeLabel1, true); ctx.DiscardValue(); ctx.Branch(codeLabel2, true); ctx.MarkLabel(codeLabel1); protoSerializer.EmitWrite(ctx, null); ctx.Branch(codeLabel, false); ctx.MarkLabel(codeLabel2); } } if (this.constructType == null || this.constructType == this.forType) { ctx.LoadValue(localWithValue); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.LoadValue(this.forType); ctx.BranchIfEqual(codeLabel, true); } else { using (Local local = new Local(ctx, ctx.MapType(typeof(Type)))) { ctx.LoadValue(localWithValue); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.CopyValue(); ctx.StoreValue(local); ctx.LoadValue(this.forType); ctx.BranchIfEqual(codeLabel, true); ctx.LoadValue(local); ctx.LoadValue(this.constructType); ctx.BranchIfEqual(codeLabel, true); } } ctx.LoadValue(this.forType); ctx.LoadValue(localWithValue); ctx.EmitCall(ctx.MapType(typeof(object)).GetMethod("GetType")); ctx.EmitCall(ctx.MapType(typeof(TypeModel)).GetMethod("ThrowUnexpectedSubtype", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)); } ctx.MarkLabel(codeLabel); for (int j = 0; j < (int)this.serializers.Length; j++) { IProtoSerializer protoSerializer1 = this.serializers[j]; if (protoSerializer1.ExpectedType == this.forType) { protoSerializer1.EmitWrite(ctx, localWithValue); } } if (this.isExtensible) { ctx.LoadValue(localWithValue); ctx.LoadReaderWriter(); ctx.EmitCall(ctx.MapType(typeof(ProtoWriter)).GetMethod("AppendExtensionData")); } this.EmitCallbackIfNeeded(ctx, localWithValue, TypeModel.CallbackType.AfterSerialize); } }