예제 #1
0
        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
            }
        }
        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
0
 protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue((int)fieldNumber);
     ctx.LoadValue((int)wireType);
     ctx.LoadReaderWriter();
     ctx.EmitCall(typeof(ProtoWriter).GetMethod("WriteFieldHeader"));
     Tail.EmitWrite(ctx, valueFrom);    
 }
예제 #4
0
 public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue(valueFrom);
     ctx.CastToObject(type);
     ctx.LoadReaderWriter();
     ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(key));
     ctx.LoadValue((int)options);
     ctx.EmitCall(ctx.MapType(typeof(BclHelpers)).GetMethod("WriteNetObject"));
 }
예제 #5
0
 public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue(valueFrom);
     ctx.CastToObject(type);
     ctx.LoadReaderWriter();
     ctx.LoadValue(ctx.MapMetaKeyToCompiledKey(key));
     if (type ==  ctx.MapType(typeof(object))) ctx.LoadNullRef();
     else ctx.LoadValue(type);
     ctx.LoadValue((int)options);
     ctx.EmitCall(ctx.MapType(typeof(BclHelpers)).GetMethod("ReadNetObject"));
     ctx.CastFromObject(type);
 }
        protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {

            bool writeValue;
            SanityCheck(property, Tail, out writeValue, ctx.NonPublic);
            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
            }
            ctx.ReadNullCheckedTail(property.PropertyType, Tail, null); // stack is [old-addr]|[new-value]
            
            if (writeValue)
            {   // stack is old-addr|new-value
                ctx.StoreValue(property);
            }
            else
            { // don't want return value; drop it if anything there
                // stack is [new-value]
                if (Tail.ReturnsValue) { ctx.DiscardValue(); }
            }
        }
예제 #7
0
        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);
        }
        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
            }
        }
        public void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            ctx.LoadValue(valueFrom);
            ctx.CastToObject(type);
            ctx.LoadReaderWriter();
            ctx.LoadValue(key);
            
			if (type == typeof(object))
            {
            	ctx.LoadNullRef();
            }
            else
            {
            	ctx.LoadValue(type);
            }

            ctx.LoadValue((int)options);
            ctx.EmitCall(typeof(BclHelpers).GetMethod("ReadNetObject"));
            ctx.CastFromObject(type);
        }
예제 #10
0
        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);
                        }
                    }
                }
            }
        }
예제 #11
0
 void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     if (overwriteList)
     {
         ctx.LoadNullRef();
     }
     else
     {
         ctx.LoadValue(valueFrom);
     }
     ctx.LoadReaderWriter();
     ctx.EmitCall(typeof(ProtoReader).GetMethod("AppendBytes"));
 }
예제 #12
0
 protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     if (setSpecified == null)
     {
         Tail.EmitRead(ctx, valueFrom);
         return;
     }
     using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
     {
         Tail.EmitRead(ctx, loc);
         ctx.LoadAddress(loc, ExpectedType);
         ctx.LoadValue(1); // true
         ctx.EmitCall(setSpecified);
     }
 }
예제 #13
0
 protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     Tail.EmitRead(ctx, valueFrom);
     ctx.CopyValue();
     Compiler.CodeLabel @nonEmpty = ctx.DefineLabel(), @end = ctx.DefineLabel();
     ctx.LoadValue(typeof(string).GetProperty("Length"));
     ctx.BranchIfTrue(@nonEmpty, true);
     ctx.DiscardValue();
     ctx.LoadNullRef();
     ctx.Branch(@end, true);
     ctx.MarkLabel(@nonEmpty);
     ctx.EmitCtor(typeof(Uri), typeof(string));
     ctx.MarkLabel(@end);
     
 }
예제 #14
0
 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);
     }
 }
예제 #15
0
        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();
                }
            }
        }
 protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     Compiler.CodeLabel done = ctx.DefineLabel();
     if (valueFrom == null)
     {
         ctx.CopyValue(); // on the stack
         Compiler.CodeLabel needToPop = ctx.DefineLabel();
         EmitBranchIfDefaultValue(ctx, needToPop);
         Tail.EmitWrite(ctx, null);
         ctx.Branch(done, true);
         ctx.MarkLabel(needToPop);
         ctx.DiscardValue();
     }
     else
     {
         ctx.LoadValue(valueFrom); // variable/parameter
         EmitBranchIfDefaultValue(ctx, done);
         Tail.EmitWrite(ctx, valueFrom);
     }
     ctx.MarkLabel(done);
 }
        private void EmitBranchIfDefaultValue(Compiler.CompilerContext ctx, Compiler.CodeLabel label)
        {
            switch (Helpers.GetTypeCode(ExpectedType))
            {
                case ProtoTypeCode.Boolean:
                    if ((bool)defaultValue)
                    {
                        ctx.BranchIfTrue(label, false);
                    }
                    else
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    break;
                case ProtoTypeCode.Byte:
                    if ((byte)defaultValue == (byte)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(byte)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.SByte:
                    if ((sbyte)defaultValue == (sbyte)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(sbyte)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.Int16:
                    if ((short)defaultValue == (short)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(short)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.UInt16:
                    if ((ushort)defaultValue == (ushort)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(ushort)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.Int32:
                    if ((int)defaultValue == (int)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.UInt32:
                    if ((uint)defaultValue == (uint)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(uint)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.Char:
                    if ((char)defaultValue == (char)0)
                    {
                        ctx.BranchIfFalse(label, false);
                    }
                    else
                    {
                        ctx.LoadValue((int)(char)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.Int64:
                    ctx.LoadValue((long)defaultValue);
                    EmitBeq(ctx, label, ExpectedType);
                    break;
                case ProtoTypeCode.UInt64:
                    ctx.LoadValue((long)(ulong)defaultValue);
                    EmitBeq(ctx, label, ExpectedType);
                    break;
                case ProtoTypeCode.Double:
                    ctx.LoadValue((double)defaultValue);
                    EmitBeq(ctx, label, ExpectedType);
                    break;
                case ProtoTypeCode.Single:
                    ctx.LoadValue((float)defaultValue);
                    EmitBeq(ctx, label, ExpectedType);
                    break;
                case ProtoTypeCode.String:
                    ctx.LoadValue((string)defaultValue);
                    EmitBeq(ctx, label, ExpectedType);
                    break;
                case ProtoTypeCode.Decimal:
                    {
                        decimal d = (decimal)defaultValue;
                        ctx.LoadValue(d);
                        EmitBeq(ctx, label, ExpectedType);
                    }
                    break;
                case ProtoTypeCode.TimeSpan:
                    {
                        TimeSpan ts = (TimeSpan)defaultValue;
                        if (ts == TimeSpan.Zero)
                        {
                            ctx.LoadValue(typeof(TimeSpan).GetField("Zero"));
                        }
                        else
                        {
                            ctx.LoadValue(ts.Ticks);
                            ctx.EmitCall(typeof(TimeSpan).GetMethod("FromTicks"));
                        }
                        EmitBeq(ctx, label, ExpectedType);
                        break;
                    }
                case ProtoTypeCode.Guid:
                    {
                        ctx.LoadValue((Guid)defaultValue);
                        EmitBeq(ctx, label, ExpectedType);
                        break;
                    }
                case ProtoTypeCode.DateTime:
                    {
#if FX11
                        ctx.LoadValue(((DateTime)defaultValue).ToFileTime());
                        ctx.EmitCall(typeof(DateTime).GetMethod("FromFileTime"));                      
#else
                        ctx.LoadValue(((DateTime)defaultValue).ToBinary());
                        ctx.EmitCall(typeof(DateTime).GetMethod("FromBinary"));
#endif
                        
                        EmitBeq(ctx, label, ExpectedType);
                        break;
                    }
                default:
                    throw new NotSupportedException("Type cannot be represented as a default value: " + ExpectedType.FullName);
            }
        }
예제 #18
0
 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);
         }
     }
 }
예제 #19
0
 private static void WriteEnumValue(Compiler.CompilerContext ctx, TypeCode typeCode, object value)
 {
     switch (typeCode)
     {
         case TypeCode.Byte: ctx.LoadValue((int)(byte)value); break;
         case TypeCode.SByte: ctx.LoadValue((int)(sbyte)value); break;
         case TypeCode.Int16: ctx.LoadValue((int)(short)value); break;
         case TypeCode.Int32: ctx.LoadValue((int)(int)value); break;
         case TypeCode.Int64: ctx.LoadValue((long)(long)value); break;
         case TypeCode.UInt16: ctx.LoadValue((int)(ushort)value); break;
         case TypeCode.UInt32: ctx.LoadValue((int)(uint)value); break;
         case TypeCode.UInt64: ctx.LoadValue((long)(ulong)value); break;
         default: throw new InvalidOperationException();
     }
 }
예제 #20
0
 void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadValue(valueFrom);
     ctx.EmitCall(toTail);
     rootTail.EmitWrite(ctx, null);
 }
예제 #21
0
 void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     TypeCode typeCode = GetTypeCode();
     if (map == null)
     {
         ctx.LoadValue(valueFrom);
         ctx.ConvertToInt32(typeCode);
         ctx.EmitBasicWrite("WriteInt32", null);
     }
     else
     {
         using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
         {
             Compiler.CodeLabel @continue = ctx.DefineLabel();
             for (int i = 0; i < map.Length; i++)
             {
                 Compiler.CodeLabel tryNextValue = ctx.DefineLabel(), processThisValue = ctx.DefineLabel();
                 ctx.LoadValue(loc);
                 WriteEnumValue(ctx, typeCode, map[i].Value);
                 ctx.BranchIfEqual(processThisValue, true);
                 ctx.Branch(tryNextValue, true);
                 ctx.MarkLabel(processThisValue);
                 ctx.LoadValue(map[i].WireValue);
                 ctx.EmitBasicWrite("WriteInt32", null);
                 ctx.Branch(@continue, false);
                 ctx.MarkLabel(tryNextValue);
             }
             ctx.LoadReaderWriter();
             ctx.LoadValue(loc);
             ctx.CastToObject(ExpectedType);
             ctx.EmitCall(typeof(ProtoWriter).GetMethod("ThrowEnumException"));
             ctx.MarkLabel(@continue);
         }
     }
     
 }
예제 #22
0
        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");
            }

            using (Compiler.Local loc = ctx.GetLocalWithValue(ExpectedType, valueFrom))
            {
                if (Tail.RequiresOldValue)
                {
                    ctx.LoadAddress(loc, ExpectedType); // stack is: old-addr
                    ctx.LoadValue(property); // stack is: old-value
                }
                Type propertyType = property.PropertyType;
                ctx.ReadNullCheckedTail(propertyType, Tail, null); // stack is [new-value]

                if (writeValue)
                {
                    using (Compiler.Local newVal = new Compiler.Local(ctx, property.PropertyType))
                    {
                        ctx.StoreValue(newVal); // stack is empty

                        Compiler.CodeLabel allDone = new Compiler.CodeLabel(); // <=== default structs
                        if (!propertyType.IsValueType)
                        { // if the tail returns a null, intepret that as *no assign*
                            allDone = ctx.DefineLabel();
                            ctx.LoadValue(newVal); // stack is: new-value
                            ctx.BranchIfFalse(@allDone, true); // stack is empty
                        }
                        // assign the value
                        ctx.LoadAddress(loc, ExpectedType); // parent-addr
                        ctx.LoadValue(newVal); // parent-obj|new-value
                        if (shadowSetter == null)
                        {
                            ctx.StoreValue(property); // empty
                        }
                        else
                        {
                            ctx.EmitCall(shadowSetter); // empty
                        }
                        if (!propertyType.IsValueType)
                        {
                            ctx.MarkLabel(allDone);
                        }
                    }

                }
                else
                { // don't want return value; drop it if anything there
                    // stack is [new-value]
                    if (Tail.ReturnsValue) { ctx.DiscardValue(); }
                }
            }
        }
예제 #23
0
 protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     ctx.LoadAddress(valueFrom, ExpectedType);
     ctx.LoadValue(property);
     ctx.WriteNullCheckedTail(property.PropertyType, Tail, null);
 }
예제 #24
0
 private static void EmitReadAndAddItem(Compiler.CompilerContext ctx, Compiler.Local list, IProtoSerializer tail, MethodInfo add)
 {
     ctx.LoadValue(list);
     Type itemType = tail.ExpectedType;
     if (tail.RequiresOldValue)
     {
         if (itemType.IsValueType || !tail.ReturnsValue)
         {
             // going to need a variable
             using (Compiler.Local item = new Compiler.Local(ctx, itemType))
             {
                 if (itemType.IsValueType)
                 {   // initialise the struct
                     ctx.LoadAddress(item, itemType);
                     ctx.EmitCtor(itemType);
                 }
                 else
                 {   // assign null
                     ctx.LoadNullRef();
                     ctx.StoreValue(item);
                 }
                 tail.EmitRead(ctx, item);
                 if (!tail.ReturnsValue) { ctx.LoadValue(item); }
             }
         }
         else
         {    // no variable; pass the null on the stack and take the value *off* the stack
             ctx.LoadNullRef();
             tail.EmitRead(ctx, null);
         }
     }
     else
     {
         if (tail.ReturnsValue)
         {   // out only (on the stack); just emit it
             tail.EmitRead(ctx, null);
         }
         else
         {   // doesn't take anything in nor return anything! WTF?
             throw new InvalidOperationException();
         }
     }
     // our "Add" is chosen either to take the correct type, or to take "object";
     // we may need to box the value
         
     Type addParamType = add.GetParameters()[0].ParameterType;
     if(addParamType != itemType) {
         if (addParamType == typeof(object))
         {
             ctx.CastToObject(itemType);
         }
         else
         {
             throw new InvalidOperationException("Conflicting item/add type");
         }
     }
     ctx.EmitCall(add);
     if (add.ReturnType != typeof(void))
     {
         ctx.DiscardValue();
     }
 }
예제 #25
0
        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(); }
            }
        }
예제 #26
0
        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);

                        ctx.LoadValue(fieldNumber);
                        ctx.LoadValue(1);
                        ctx.Subtract(); // jump-table is zero-based
                        ctx.Switch(handlers);

                        // and the default:
                        ctx.Branch(notRecognised, false);
                        for (int i = 0; i < handlers.Length; i++)
                        {
                            ctx.MarkLabel(handlers[i]);
                            IProtoSerializer tail = tails[i];
                            Compiler.Local oldValIfNeeded = tail.RequiresOldValue ? locals[i] : null;
                            ctx.ReadNullCheckedTail(locals[i].Type, tail, oldValIfNeeded);
                            if (tail.ReturnsValue)
                            {
                                if (Helpers.IsValueType(locals[i].Type))
                                {
                                    ctx.StoreValue(locals[i]);
                                }
                                else
                                {
                                    Compiler.CodeLabel hasValue = ctx.DefineLabel(), allDone = ctx.DefineLabel();

                                    ctx.CopyValue();
                                    ctx.BranchIfTrue(hasValue, true); // interpret null as "don't assign"
                                    ctx.DiscardValue();
                                    ctx.Branch(allDone, true);
                                    ctx.MarkLabel(hasValue);
                                    ctx.StoreValue(locals[i]);
                                    ctx.MarkLabel(allDone);
                                }
                            }
                            ctx.Branch(@continue, false);
                        }

                        ctx.MarkLabel(notRecognised);
                        ctx.LoadReaderWriter();
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));

                        ctx.MarkLabel(@continue);
                        ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
                        ctx.CopyValue();
                        ctx.StoreValue(fieldNumber);
                        ctx.LoadValue(0);
                        ctx.BranchIfGreater(processField, false);
                    }
                    for (int i = 0; i < locals.Length; i++)
                    {
                        ctx.LoadValue(locals[i]);
                    }

                    ctx.EmitCtor(ctor);
                    ctx.StoreValue(objValue);
                }
                finally
                {
                    for (int i = 0; i < locals.Length; i++)
                    {
                        if (locals[i] != null)
                            locals[i].Dispose(); // release for re-use
                    }
                }
            }

        }
예제 #27
0
        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"));
            }
        }
 void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     if (!EmitDedicatedMethod(ctx, valueFrom, true))
     {
         ctx.LoadValue(valueFrom);
         if (type.IsValueType) ctx.CastToObject(type);
         ctx.LoadValue(key);
         ctx.LoadReaderWriter();
         ctx.EmitCall(typeof(ProtoReader).GetMethod("ReadObject"));
         ctx.CastFromObject(type);
     }
 }
        protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
        {
            using (Compiler.Local oldList = AppendToCollection ? ctx.GetLocalWithValue(ExpectedType, valueFrom) : null)
            using(Compiler.Local builder = new Compiler.Local(ctx, builderFactory.ReturnType))
            {
                ctx.EmitCall(builderFactory);
                ctx.StoreValue(builder);

                if(AppendToCollection)
                {
                    Compiler.CodeLabel done = ctx.DefineLabel();
                    if(!ExpectedType.IsValueType)
                    {
                        ctx.LoadValue(oldList);
                        ctx.BranchIfFalse(done, false); // old value null; nothing to add
                    }
                    PropertyInfo prop = Helpers.GetProperty(ExpectedType, "Length", false);
                    if(prop == null) prop = Helpers.GetProperty(ExpectedType, "Count", false);
#if !NO_GENERICS
                    if (prop == null) prop = Helpers.GetProperty(ResolveIReadOnlyCollection(ExpectedType, Tail.ExpectedType), "Count", false);
#endif
                    ctx.LoadAddress(oldList, oldList.Type);
                    ctx.EmitCall(Helpers.GetGetMethod(prop, false, false));
                    ctx.BranchIfFalse(done, false); // old list is empty; nothing to add

                    Type voidType = ctx.MapType(typeof(void));
                    if(addRange != null)
                    {
                        ctx.LoadValue(builder);
                        ctx.LoadValue(oldList);
                        ctx.EmitCall(addRange);
                        if (addRange.ReturnType != null && add.ReturnType != voidType) ctx.DiscardValue();
                    }
                    else
                    {
                        // loop and call Add repeatedly
                        MethodInfo moveNext, current, getEnumerator = GetEnumeratorInfo(ctx.Model, out moveNext, out current);
                        Helpers.DebugAssert(moveNext != null);
                        Helpers.DebugAssert(current != null);
                        Helpers.DebugAssert(getEnumerator != null);

                        Type enumeratorType = getEnumerator.ReturnType;
                        using (Compiler.Local iter = new Compiler.Local(ctx, enumeratorType))
                        {
                            ctx.LoadAddress(oldList, ExpectedType);
                            ctx.EmitCall(getEnumerator);
                            ctx.StoreValue(iter);
                            using (ctx.Using(iter))
                            {
                                Compiler.CodeLabel body = ctx.DefineLabel(), next = ctx.DefineLabel();
                                ctx.Branch(next, false);

                                ctx.MarkLabel(body);
                                ctx.LoadAddress(builder, builder.Type);
                                ctx.LoadAddress(iter, enumeratorType);                                
                                ctx.EmitCall(current);
                                ctx.EmitCall(add);
                                if (add.ReturnType != null && add.ReturnType != voidType) ctx.DiscardValue();

                                ctx.MarkLabel(@next);
                                ctx.LoadAddress(iter, enumeratorType);
                                ctx.EmitCall(moveNext);
                                ctx.BranchIfTrue(body, false);
                            }
                        }
                    }


                    ctx.MarkLabel(done);
                }

                EmitReadList(ctx, builder, Tail, add, packedWireType, false);

                ctx.LoadAddress(builder, builder.Type);
                ctx.EmitCall(finish);
                if(ExpectedType != finish.ReturnType)
                {
                    ctx.Cast(ExpectedType);
                }
            }
        }
예제 #30
0
 public void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
 {
     using (Compiler.Local loc = ctx.GetLocalWithValue(ctor.DeclaringType, valueFrom))
     {
         for (int i = 0; i < tails.Length; i++)
         {
             Type type = GetMemberType(i);
             ctx.LoadAddress(loc, ExpectedType);
             if (members[i] is FieldInfo)
             {
                 ctx.LoadValue((FieldInfo)members[i]);
             }
             else if (members[i] is PropertyInfo)
             {
                 ctx.LoadValue((PropertyInfo)members[i]);
             }
             ctx.WriteNullCheckedTail(type, tails[i], null);
         }
     }
 }