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(); }
            }
        }
 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);
     }
 }
예제 #3
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();
                }
            }
        }
예제 #4
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);
                        }
                    }
                }
            }
        }
예제 #5
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(); }
                }
            }
        }
예제 #6
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
                    }
                }
            }

        }
예제 #7
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(); }
            }
        }
        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);
										ctx.EmitCtor(type);
										store = false;
										break;
								}
							}
							else
							{
								ctx.LoadNullRef();
							}
							if (store)
							{
								ctx.StoreValue(locals[i]);
							}
						}
					}

					Compiler.CodeLabel skipOld = ExpectedType.IsValueType
					                             	? new Compiler.CodeLabel()
					                             	: ctx.DefineLabel();
					if (!ExpectedType.IsValueType)
					{
						ctx.LoadAddress(objValue, ExpectedType);
						ctx.BranchIfFalse(skipOld, false);
					}

					for (int i = 0; i < members.Length; i++)
					{
						ctx.LoadAddress(objValue, ExpectedType);
						switch (members[i].MemberType)
						{
							case MemberTypes.Field:
								ctx.LoadValue((FieldInfo) members[i]);
								break;
							case MemberTypes.Property:
								ctx.LoadValue((PropertyInfo) members[i]);
								break;
						}
						ctx.StoreValue(locals[i]);
					}

					if (!ExpectedType.IsValueType) ctx.MarkLabel(skipOld);

					using (Compiler.Local fieldNumber = new Compiler.Local(ctx, typeof (int)))
					using (Compiler.Local j = new Compiler.Local(ctx, typeof (int)))
					{
						// j = 0
						ctx.LoadValue(0);
						ctx.StoreValue(j);

						Compiler.CodeLabel @continue = ctx.DefineLabel(),
						                   processField = ctx.DefineLabel(),
						                   notRecognised = ctx.DefineLabel(),
						                   @endWhileLoop = 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 (locals[i].Type.IsValueType)
								{
									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(typeof (ProtoReader).GetMethod("SkipField"));

						ctx.MarkLabel(@continue);

						// j < values.Length
						ctx.LoadValue(j);
						ctx.LoadValue(members.Length);
						ctx.BranchIfGreaterOrEqual(@endWhileLoop, false);

						//j++
						ctx.LoadValue(j);
						ctx.LoadValue(1);
						ctx.Add();
						ctx.StoreValue(j);

						// source.ReadNextFieldHack() > 0
						ctx.LoadReaderWriter();
						ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadNextFieldHack"));
						ctx.LoadValue(0);
						ctx.BranchIfLessOrEqual(@endWhileLoop, true);

						// field = source.ReadFieldHeader();
						ctx.LoadReaderWriter();
						ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadFieldHeader"));
						ctx.StoreValue(fieldNumber);
						ctx.Branch(processField, false);

						ctx.MarkLabel(@endWhileLoop);
					}

					for (int i = 0; i < locals.Length; i++)
					{
						ctx.LoadValue(locals[i]);
					}

					ctx.EmitCtor(ctor);
					ctx.StoreValue(objValue);

					if (issueReferenceDirectives)
					{
						//source.NetCache.UpdateKeyedObject(tupleKey, oldTuple, result);
						ctx.LoadReaderWriter();
						ctx.LoadValue(typeof (ProtoReader).GetProperty("NetCache"));
						ctx.LoadValue(tupleKey);
						ctx.LoadValue(oldTuple);
						ctx.LoadValue(objValue);
						ctx.CastToObject(ExpectedType);
						ctx.EmitCall(typeof (NetObjectCache).GetMethod("UpdateKeyedObject"));
					}

					if (forceIssueFakeHeader)
					{
						//source.ReadEndGroupFieldHeaderHack();
						ctx.LoadReaderWriter();
						ctx.EmitCall(typeof (ProtoReader).GetMethod("ReadEndGroupFieldHeaderHack"));
					}

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