Example #1
0
        public virtual void Emit(EmitContext ec)
        {
            if (!IsByRef)
            {
                Expr.Emit(ec);
                return;
            }

            AddressOp mode = AddressOp.Store;

            if (ArgType == AType.Ref)
            {
                mode |= AddressOp.Load;
            }

            IMemoryLocation    ml = (IMemoryLocation)Expr;
            ParameterReference pr = ml as ParameterReference;

            //
            // ParameterReferences might already be references, so we want
            // to pass just the value
            //
            if (pr != null && pr.IsRef)
            {
                pr.EmitLoad(ec);
            }
            else
            {
                ml.AddressOf(ec, mode);
            }
        }
Example #2
0
        public virtual void Emit(EmitContext ec)
        {
            if (!IsByRef)
            {
                if (ArgType == AType.ExtensionTypeConditionalAccess)
                {
                    var ie = new InstanceEmitter(Expr, false);
                    ie.Emit(ec, true);
                }
                else
                {
                    Expr.Emit(ec);
                }

                return;
            }

            AddressOp mode = AddressOp.Store;

            if (ArgType == AType.Ref)
            {
                mode |= AddressOp.Load;
            }

            IMemoryLocation ml = (IMemoryLocation)Expr;

            ml.AddressOf(ec, mode);
        }
Example #3
0
        public byte GetByte(IMemoryLocation location, ushort offset)
        {
            SetBytePointer(location.StartAddress + offset);
            var ret = Bytes[BytePointer.Value];

            IncreaseBytePointer();
            return(ret);
        }
Example #4
0
        public byte[] GetBytes(IMemoryLocation location)
        {
            var ret = new byte[location.Length];

            SetBytePointer(location.StartAddress);
            for (var i = 0; i < ret.Length; i++)
            {
                ret[i] = GetByte();
            }
            return(ret);
        }
Example #5
0
        public void SaveRange(string filename, IMemoryLocation addressRange)
        {
            var startBytes = BitConverter.GetBytes(addressRange.StartAddress.Value);

            using (var sw = new FileStream(filename, FileMode.Create))
            {
                sw.Write(startBytes, 0, 2);
                sw.Write(Bytes, addressRange.StartAddress.Value, addressRange.Length);
                sw.Flush();
                sw.Close();
            }
        }
Example #6
0
        public void AddressOf(EmitContext ec, AddressOp mode)
        {
            IMemoryLocation ml = expr as VariableReference;

            if (ml != null)
            {
                ml.AddressOf(ec, mode);
            }
            else
            {
                LocalVariable.AddressOf(ec, mode);
            }
        }
Example #7
0
 public void SetBytes(IMemoryLocation location, params byte[] bytes)
 {
     if (bytes == null)
     {
         return;
     }
     if (bytes.Length <= 0)
     {
         return;
     }
     SetBytePointer(location.StartAddress);
     foreach (var t in bytes)
     {
         SetByte(t);
     }
 }
Example #8
0
        public virtual void Emit(EmitContext ec)
        {
            if (!IsByRef)
            {
                Expr.Emit(ec);
                return;
            }

            AddressOp mode = AddressOp.Store;

            if (ArgType == AType.Ref)
            {
                mode |= AddressOp.Load;
            }

            IMemoryLocation ml = (IMemoryLocation)Expr;

            ml.AddressOf(ec, mode);
        }
Example #9
0
 public ByteVariable CreateByteVariable(string name, IMemoryLocation address) =>
 Variables.CreateByteVariable(name, address.StartAddress);
Example #10
0
        public void Emit(EmitContext ec, bool conditionalAccess)
        {
            Label NullOperatorLabel;

            Nullable.Unwrap unwrap;

            if (conditionalAccess && Expression.IsNeverNull(instance))
            {
                conditionalAccess = false;
            }

            if (conditionalAccess)
            {
                NullOperatorLabel = ec.DefineLabel();
                unwrap            = instance as Nullable.Unwrap;
            }
            else
            {
                NullOperatorLabel = new Label();
                unwrap            = null;
            }

            IMemoryLocation instance_address       = null;
            bool            conditional_access_dup = false;

            if (unwrap != null)
            {
                unwrap.Store(ec);
                unwrap.EmitCheck(ec);
                ec.Emit(OpCodes.Brtrue_S, NullOperatorLabel);
            }
            else
            {
                if (conditionalAccess && addressRequired)
                {
                    //
                    // Don't allocate temp variable when instance load is cheap and load and load-address
                    // operate on same memory
                    //
                    instance_address = instance as VariableReference;
                    if (instance_address == null)
                    {
                        instance_address = instance as LocalTemporary;
                    }

                    if (instance_address == null)
                    {
                        EmitLoad(ec, false);
                        ec.Emit(OpCodes.Dup);
                        ec.EmitLoadFromPtr(instance.Type);

                        conditional_access_dup = true;
                    }
                    else
                    {
                        instance.Emit(ec);
                    }
                }
                else
                {
                    EmitLoad(ec, !conditionalAccess);

                    if (conditionalAccess)
                    {
                        conditional_access_dup = !IsInexpensiveLoad();
                        if (conditional_access_dup)
                        {
                            ec.Emit(OpCodes.Dup);
                        }
                    }
                }

                if (conditionalAccess)
                {
                    if (instance.Type.Kind == MemberKind.TypeParameter)
                    {
                        ec.Emit(OpCodes.Box, instance.Type);
                    }

                    ec.Emit(OpCodes.Brtrue_S, NullOperatorLabel);

                    if (conditional_access_dup)
                    {
                        ec.Emit(OpCodes.Pop);
                    }
                }
            }

            if (conditionalAccess)
            {
                if (!ec.ConditionalAccess.Statement)
                {
                    if (ec.ConditionalAccess.Type.IsNullableType)
                    {
                        Nullable.LiftedNull.Create(ec.ConditionalAccess.Type, Location.Null).Emit(ec);
                    }
                    else
                    {
                        ec.EmitNull();
                    }
                }

                ec.Emit(OpCodes.Br, ec.ConditionalAccess.EndLabel);
                ec.MarkLabel(NullOperatorLabel);

                if (instance_address != null)
                {
                    instance_address.AddressOf(ec, AddressOp.Load);
                }
                else if (unwrap != null)
                {
                    unwrap.Emit(ec);
                    var tmp = ec.GetTemporaryLocal(unwrap.Type);
                    ec.Emit(OpCodes.Stloc, tmp);
                    ec.Emit(OpCodes.Ldloca, tmp);
                    ec.FreeTemporaryLocal(tmp, unwrap.Type);
                }
                else if (!conditional_access_dup)
                {
                    instance.Emit(ec);
                }
            }
        }
Example #11
0
 public Types.Byte GetBits(IMemoryLocation location) =>
 new Types.Byte(GetByte(location.StartAddress));
Example #12
0
 public void SetBits(IMemoryLocation location, BitValue b7, BitValue b6, BitValue b5, BitValue b4, BitValue b3, BitValue b2, BitValue b1, BitValue b0)
 {
     SetBytePointer(location.StartAddress);
     SetBits(b7, b6, b5, b4, b3, b2, b1, b0);
 }
Example #13
0
 public void SetByte(IMemoryLocation location, ushort offset, byte value)
 {
     SetBytePointer(location.StartAddress + offset);
     Bytes[BytePointer.Value] = value;
     IncreaseBytePointer();
 }
Example #14
0
 public WordVariable CreateWordVariable(string name, IMemoryLocation address) =>
 CreateWordVariable(name, address.StartAddress);
Example #15
0
 public void SetBytePointer(IMemoryLocation l, ushort offset) =>
 SetBytePointer(l.StartAddress + offset);
		public override bool Emit (EmitContext ec, IMemoryLocation target)
		{
			bool left_on_stack = base.Emit (ec, target);

			if (initializers.IsEmpty)
				return left_on_stack;

			LocalTemporary temp = target as LocalTemporary;
			if (temp == null) {
				if (!left_on_stack) {
					VariableReference vr = target as VariableReference;
					
					// FIXME: This still does not work correctly for pre-set variables
					if (vr != null && vr.IsRef)
						target.AddressOf (ec, AddressOp.Load);

					((Expression) target).Emit (ec);
					left_on_stack = true;
				}

				temp = new LocalTemporary (type);
			}

			instance = temp;
			if (left_on_stack)
				temp.Store (ec);

			initializers.Emit (ec);

			if (left_on_stack) {
				temp.Emit (ec);
				temp.Release (ec);
			}

			return left_on_stack;
		}
		protected override IMemoryLocation EmitAddressOf (EmitContext ec, AddressOp Mode)
		{
			instance = base.EmitAddressOf (ec, Mode);

			if (!initializers.IsEmpty)
				initializers.Emit (ec);

			return instance;
		}
		//
		// This Emit can be invoked in two contexts:
		//    * As a mechanism that will leave a value on the stack (new object)
		//    * As one that wont (init struct)
		//
		// If we are dealing with a ValueType, we have a few
		// situations to deal with:
		//
		//    * The target is a ValueType, and we have been provided
		//      the instance (this is easy, we are being assigned).
		//
		//    * The target of New is being passed as an argument,
		//      to a boxing operation or a function that takes a
		//      ValueType.
		//
		//      In this case, we need to create a temporary variable
		//      that is the argument of New.
		//
		// Returns whether a value is left on the stack
		//
		// *** Implementation note ***
		//
		// To benefit from this optimization, each assignable expression
		// has to manually cast to New and call this Emit.
		//
		// TODO: It's worth to implement it for arrays and fields
		//
		public virtual bool Emit (EmitContext ec, IMemoryLocation target)
		{
			bool is_value_type = TypeManager.IsValueType (type);
			ILGenerator ig = ec.ig;
			VariableReference vr = target as VariableReference;

			if (target != null && is_value_type && (vr != null || method == null)) {
				target.AddressOf (ec, AddressOp.Store);
			} else if (vr != null && vr.IsRef) {
				vr.EmitLoad (ec);
			}
			
			if (Arguments != null)
				Arguments.Emit (ec);

			if (is_value_type) {
				if (method == null) {
					ig.Emit (OpCodes.Initobj, type);
					return false;
				}

				if (vr != null) {
					ig.Emit (OpCodes.Call, (ConstructorInfo) method);
					return false;
				}
			}
			
			if (is_type_parameter)
				return DoEmitTypeParameter (ec);			

			ConstructorInfo ci = (ConstructorInfo) method;
#if MS_COMPATIBLE
			if (TypeManager.IsGenericType (type) && type.IsGenericTypeDefinition)
				ci = TypeBuilder.GetConstructor (type, ci);
#endif

			ig.Emit (OpCodes.Newobj, ci);
			return true;
		}
Example #19
0
		public override bool Emit (EmitContext ec, IMemoryLocation target)
		{
			bool left_on_stack = base.Emit (ec, target);

			if (initializers.IsEmpty)
				return left_on_stack;

			LocalTemporary temp = null;

			//
			// If target is non-hoisted variable, let's use it
			//
			VariableReference variable = target as VariableReference;
			if (variable != null) {
				instance = target;

				if (left_on_stack) {
					if (variable.IsRef)
						StoreFromPtr (ec.ig, type);
					else
						variable.EmitAssign (ec, EmptyExpression.Null, false, false);

					left_on_stack = false;
				}
			} else {
				temp = target as LocalTemporary;
				if (temp == null) {
					if (!left_on_stack)
						throw new NotImplementedException ();

					temp = new LocalTemporary (type);
				}

				instance = temp;
				if (left_on_stack)
					temp.Store (ec);
			}

			initializers.Emit (ec);

			if (left_on_stack) {
				temp.Emit (ec);
				temp.Release (ec);
			}

			return left_on_stack;
		}