Example #1
0
        public void OperandTypeVar()
        {
            var methodBody = CreateDummyMethodBody();
            var image      = methodBody.Method.Image;

            var instructions = methodBody.Instructions;
            var var1         = new VariableSignature(image.TypeSystem.Boolean);
            var var2         = new VariableSignature(image.TypeSystem.Int32);

            methodBody.Signature = new StandAloneSignature(new LocalVariableSignature(new[] { var1, var2 }));

            instructions.AddRange(new[]
            {
                CilInstruction.Create(CilOpCodes.Ldloc, var1),
                CilInstruction.Create(CilOpCodes.Stloc, var1),
                CilInstruction.Create(CilOpCodes.Ldloc, var2),
                CilInstruction.Create(CilOpCodes.Stloc, var2),
                CilInstruction.Create(CilOpCodes.Ret)
            });

            var mapping = image.Header.UnlockMetadata();

            image = image.Header.LockMetadata();

            var newMethod = (MethodDefinition)image.ResolveMember(mapping[methodBody.Method]);

            instructions = newMethod.CilMethodBody.Instructions;

            Assert.Equal(var1.VariableType, ((VariableSignature)instructions[0].Operand).VariableType, _comparer);
            Assert.Equal(var1.VariableType, ((VariableSignature)instructions[1].Operand).VariableType, _comparer);
            Assert.Equal(var2.VariableType, ((VariableSignature)instructions[2].Operand).VariableType, _comparer);
            Assert.Equal(var2.VariableType, ((VariableSignature)instructions[3].Operand).VariableType, _comparer);
        }
Example #2
0
        public void PersistentVariables()
        {
            var methodBody = CreateDummyMethodBody();
            var image      = methodBody.Method.Image;
            var importer   = new ReferenceImporter(image);

            var var1 = new VariableSignature(image.TypeSystem.Int32);
            var var2 = new VariableSignature(importer.ImportTypeSignature(typeof(Stream)));

            methodBody.Signature = new StandAloneSignature(new LocalVariableSignature(new[] { var1, var2 }));

            var mapping = image.Header.UnlockMetadata();

            image = image.Header.LockMetadata();

            methodBody = ((MethodDefinition)image.ResolveMember(mapping[methodBody.Method])).CilMethodBody;
            Assert.NotNull(methodBody.Signature);
            Assert.IsType <LocalVariableSignature>(methodBody.Signature.Signature);

            var localVarSig = (LocalVariableSignature)methodBody.Signature.Signature;

            Assert.Equal(2, localVarSig.Variables.Count);
            Assert.Equal(var1.VariableType, localVarSig.Variables[0].VariableType, _comparer);
            Assert.Equal(var2.VariableType, localVarSig.Variables[1].VariableType, _comparer);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.Assignment"/> class.
        /// </summary>
        public Assignment()
        {
            VariableSignature signature = RegistrationManager.SelectReadWriteVariableAtRandom();

            this.readWriteVariable = new ReadWriteVariable(signature);
            this.rightStatement    = new RightStatement(signature.VariableType);
        }
        /// <summary>
        /// Retrieves the local stack operand at the specified <paramref name="index"/>.
        /// </summary>
        /// <param name="index">The index of the stack operand to retrieve.</param>
        /// <returns>The operand at the specified index.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">The <paramref name="index"/> is not valid.</exception>
        public Operand GetLocalOperand(int index)
        {
            // HACK: Returning a new instance here breaks object identity. We should reuse operands,
            // which represent the same memory location. If we need to move a variable in an optimization
            // stage to a different memory location, it should actually be a new one so sharing object
            // only saves runtime space/perf.
            // PG: Isn't that implemented below? Comment seems out of date with code

            Debug.Assert(localsSig != null, @"Method doesn't have locals.");
            Debug.Assert(index < localsSig.Locals.Length, @"Invalid local index requested.");

            if (localsSig == null || localsSig.Locals.Length < index)
            {
                throw new ArgumentOutOfRangeException(@"index", index, @"Invalid parameter index");
            }

            Operand local = locals[index];

            if (local == null)
            {
                VariableSignature localVariable = localsSig.Locals[index];

                //ScheduleDependencyForCompilation(localVariable.Type);

                local = new LocalVariableOperand(architecture.StackFrameRegister, String.Format("L_{0}", index), index, localVariable.Type);

                locals[index] = local;
            }

            return(local);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 public VariableSignature(VariableSignature signature)
     : base(signature)
 {
     this.customMods = signature.customMods;
     this.modifier = signature.modifier;
     this.type = signature.type;
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="token">The token.</param>
 public VariableSignature(VariableSignature signature)
     : base(signature)
 {
     this.customMods = signature.customMods;
     this.modifier   = signature.modifier;
     this.type       = signature.type;
 }
Example #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.Assignment"/> class.
        /// </summary>
        public Assignment(int depthIn)
        {
            this.depth = depthIn;
            VariableSignature signature = RegistrationManager.SelectReadWriteVariableAtRandom();

            this.readWriteVariable = new ReadWriteVariable(signature);
            this.rightStatement    = new RightStatement(signature.VariableType, depthIn + 1);
        }
        public int GetVariableIndex(VariableSignature variable)
        {
            var methodSignature = _methodBody.Signature;

            return(methodSignature?.Signature is LocalVariableSignature localVarSig
                ? localVarSig.Variables.IndexOf(variable)
                : -1);
        }
Example #9
0
 public static MsilInstruction Create(MsilOpCode code, VariableSignature operand)
 {
     if (code.OperandType != MsilOperandType.InlineVar && code.OperandType != MsilOperandType.ShortInlineVar)
     {
         throw new ArgumentException("Opcode does not accept a local variable operand.", "code");
     }
     return(new MsilInstruction(0, code, operand));
 }
Example #10
0
        public void VariablesTest()
        {
            // set up temp assembly.
            var assembly       = Utilities.CreateTempNetAssembly();
            var typeSystem     = assembly.NetDirectory.MetadataHeader.TypeSystem;
            var tableStream    = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>();
            var methodTable    = tableStream.GetTable <MethodDefinition>();
            var signatureTable = tableStream.GetTable <StandAloneSignature>();

            var variable  = new VariableSignature(typeSystem.String);
            var variable2 = new VariableSignature(typeSystem.Int32);

            // create localvarsig.
            var localVarSig = new LocalVariableSignature();

            localVarSig.Variables.Add(variable);
            localVarSig.Variables.Add(variable2);
            var signature = new StandAloneSignature(localVarSig);

            signatureTable.Add(signature);

            // write code.
            var body = methodTable[0].MethodBody;

            body.Signature = signature;

            body.Instructions.Clear();
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ldloc, variable2));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Pop));
            body.Instructions.Add(MsilInstruction.Create(MsilOpCodes.Ret));

            // build and validate.
            assembly    = Utilities.RebuildNetAssembly(assembly, true);
            methodTable = assembly.NetDirectory.MetadataHeader.GetStream <TableStream>().GetTable <MethodDefinition>();
            var newBody = methodTable[0].MethodBody;

            Assert.IsNotNull(newBody.Signature);
            Assert.IsInstanceOfType(newBody.Signature.Signature, typeof(LocalVariableSignature));

            var newLocalVarSig = (LocalVariableSignature)newBody.Signature.Signature;

            Assert.AreEqual(localVarSig.Variables.Count, newLocalVarSig.Variables.Count);

            for (int i = 0; i < localVarSig.Variables.Count; i++)
            {
                Utilities.ValidateType(localVarSig.Variables[i].VariableType, newLocalVarSig.Variables[i].VariableType);
            }

            Assert.IsInstanceOfType(newBody.Instructions[0].Operand, typeof(VariableSignature));
            Utilities.ValidateType(variable.VariableType,
                                   ((VariableSignature)newBody.Instructions[0].Operand).VariableType);

            Assert.IsInstanceOfType(newBody.Instructions[2].Operand, typeof(VariableSignature));
            Utilities.ValidateType(variable2.VariableType,
                                   ((VariableSignature)newBody.Instructions[2].Operand).VariableType);
        }
Example #11
0
            public int GetVariableIndex(VariableSignature variable)
            {
                var localVarSig = _owner.Signature != null ? _owner.Signature.Signature as LocalVariableSignature : null;

                if (localVarSig == null)
                {
                    throw new InvalidOperationException("Method body does not have a valid local variable signature.");
                }
                return(localVarSig.Variables.IndexOf(variable));
            }
        /// <summary>
        /// Reads from variable.
        /// </summary>
        /// <returns>The from variable.</returns>
        /// <param name="signature">Signature.</param>
        public byte ReadFromVariable(VariableSignature signature)
        {
            if (signature.VariableId == EntVariableEnum.Health)
            {
                return(this.Health);
            }

            if (signature.VariableId == EntVariableEnum.AttackStrength)
            {
                return(this.AttackStrength);
            }

            if (signature.VariableId == EntVariableEnum.GrowFoodStrength)
            {
                return(this.GrowFoodStrength);
            }

            if (signature.VariableId == EntVariableEnum.HelperInt1)
            {
                return(this.HelperInt1);
            }

            if (signature.VariableId == EntVariableEnum.HelperInt2)
            {
                return(this.HelperInt2);
            }

            if (signature.VariableId == EntVariableEnum.HelperBool1)
            {
                return(this.HelperBool1);
            }

            if (signature.VariableId == EntVariableEnum.HelperBool2)
            {
                return(this.HelperBool2);
            }

            if (signature.VariableId == EntVariableEnum.HelperGridDirection1)
            {
                return(this.HelperGridDirection1);
            }

            if (signature.VariableId == EntVariableEnum.HelperGridDirection2)
            {
                return(this.HelperGridDirection2);
            }

            LogUtility.LogErrorFormat(
                "No implementation found for read only variable: {0}",
                signature);
            return(0);
        }
Example #13
0
        /// <summary>
        /// Will possibly mutate this section of logic.
        /// </summary>
        public void PossiblyMutate()
        {
            if (GeneticLogicRoot.RollMutateDice())
            {
                VariableSignature signature = RegistrationManager.SelectReadWriteVariableAtRandom();
                this.readWriteVariable = new ReadWriteVariable(signature);
                this.rightStatement    = new RightStatement(signature.VariableType);
                return;
            }

            if (this.rightStatement != null)
            {
                this.rightStatement.PossiblyMutate();
            }
        }
        /// <summary>
        /// Writes to variable.
        /// </summary>
        /// <param name="signature">Signature.</param>
        /// <param name="value">Value.</param>
        public void WriteToVariable(VariableSignature signature, byte value)
        {
            if (signature.VariableId == EntVariableEnum.HelperInt1)
            {
                this.HelperInt1 = value;
                return;
            }

            if (signature.VariableId == EntVariableEnum.HelperInt2)
            {
                this.HelperInt2 = value;
                return;
            }

            if (signature.VariableId == EntVariableEnum.HelperBool1)
            {
                this.HelperBool1 = value;
                return;
            }

            if (signature.VariableId == EntVariableEnum.HelperBool2)
            {
                this.HelperBool2 = value;
                return;
            }

            if (signature.VariableId == EntVariableEnum.HelperGridDirection1)
            {
                this.HelperGridDirection1 = value;
                return;
            }

            if (signature.VariableId == EntVariableEnum.HelperGridDirection2)
            {
                this.HelperGridDirection2 = value;
                return;
            }

            LogUtility.LogErrorFormat(
                "No implementation found for writable variable: {0}",
                signature);
        }
Example #15
0
 public ReadOnlyVariable(VariableSignature signatureIn)
 {
     this.signature = signatureIn;
 }
Example #16
0
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="AssemblyCSharp.Scripts.EntLogic.SerializationObjects.ReadOnlyVariable"/> class.
 /// </summary>
 /// <param name="reader">Reader.</param>
 public ReadOnlyVariable(FileIOManager reader)
 {
     this.signature = new VariableSignature(reader);
 }
Example #17
0
 public ReadWriteVariable(VariableSignature signature) :
     base(signature)
 {
 }
Example #18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="signature">The signature.</param>
 /// <param name="genericArguments">The generic arguments.</param>
 public VariableSignature(VariableSignature signature, SigType[] genericArguments)
     : this(signature)
 {
     ApplyGenericArguments(genericArguments);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="VariableSignature"/> class.
 /// </summary>
 /// <param name="signature">The signature.</param>
 /// <param name="genericArguments">The generic arguments.</param>
 public VariableSignature(VariableSignature signature, SigType[] genericArguments)
     : this(signature)
 {
     ApplyGenericArguments(genericArguments);
 }
Example #20
0
 public int GetVariableIndex(VariableSignature variable)
 {
     // TODO: error avoidance.
     return(((LocalVariableSignature)_methodBody.Signature.Signature).Variables.IndexOf(variable));
 }