Ejemplo n.º 1
0
        public void PopValue(CilProgram program, CilType cilType, out IValue val)
        {
            Pop(out var stackVal);
            var valType = cilType.GetValueType(program);

            val = ConvertToValue(stackVal, valType);
        }
Ejemplo n.º 2
0
        public override IValue CreateDefaultValue(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                var type = ReflectionHelper.GetExternalType(ClassName);

                var getDefault   = GetType().GetMethod(nameof(GetDefaultGeneric), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(type);
                var defaultValue = getDefault.Invoke(null, null);

                return(new CilValueExternal(defaultValue));
            }

            var @class = program.AllClasses.Single(c => c.Name.ToString() == ClassName.ToString());

            if (@class.IsInterface)
            {
                return(new CilValueNull());
            }

            if (IsValueType(program))
            {
                var emptyInstance = new CilClassInstance(@class, program, null);
                return(new CilValueValueType(emptyInstance));
            }

            return(new CilValueNull());
        }
Ejemplo n.º 3
0
        public override Type GetRuntimeType(CilProgram program)
        {
            var lengths   = Enumerable.Repeat(0, Dimensions).ToArray();
            var fakeArray = Array.CreateInstance(ElementType.GetRuntimeType(program), lengths);
            var result    = fakeArray.GetType();

            return(result);
        }
Ejemplo n.º 4
0
        public object AsRuntime(CilType cilType, CilManagedMemory managedMemory, CilProgram program)
        {
            if (cilType is CilTypeUInt32)
            {
                return(Value);
            }

            throw new System.NotImplementedException();
        }
Ejemplo n.º 5
0
        public static Type GetExternalType(CilTypeSpec typeSpec, CilProgram program)
        {
            if (typeSpec.ClassName != null)
            {
                return(GetExternalType(typeSpec.ClassName));
            }

            return(typeSpec.Type.GetRuntimeType(program));
        }
Ejemplo n.º 6
0
        public override Type GetValueType(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                return(typeof(CilValueExternal));
            }

            return(typeof(CilValueValueType));
        }
Ejemplo n.º 7
0
        public override Type GetRuntimeType(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                return(ReflectionHelper.GetExternalType(ClassName));
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 8
0
        public override Type GetValueType(CilProgram program)
        {
            if (IsValueType(program))
            {
                return(typeof(CilValueValueType));
            }

            return(typeof(CilValueReference));
        }
Ejemplo n.º 9
0
        public CilArray(CilType type, int numElems, CilProgram program)
        {
            _array = new IValue[numElems];
            for (int i = 0; i < numElems; i++)
            {
                _array[i] = type.CreateDefaultValue(program);
            }

            _type = type;
        }
Ejemplo n.º 10
0
        public CilOrderedDictionary(List <CilSigArg> localsSigArgs, CilProgram program)
        {
            _dict      = new OrderedDictionary(localsSigArgs.Count);
            _typesDict = new OrderedDictionary(localsSigArgs.Count);

            foreach (var sigArg in localsSigArgs)
            {
                _dict.Add(sigArg.Id, sigArg.Type.CreateDefaultValue(program));
                _typesDict.Add(sigArg.Id, sigArg.Type);
            }
        }
Ejemplo n.º 11
0
        public CilClassStaticInstance(CilClass @class, CilProgram program)
        {
            _class = @class;

            StaticFields = new Dictionary <string, IValue>();
            foreach (var field in @class.Fields.Where(f => f.IsStatic))
            {
                var initValue = field.InitValue ?? field.Type.CreateDefaultValue(program);
                StaticFields.Add(field.Name, initValue);
            }
        }
Ejemplo n.º 12
0
        public override IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program)
        {
            if (obj is CilObjectExternal objExternal)
            {
                if (objExternal.ExternalObject.GetType().IsEnum)
                {
                    var result = Convert.ToInt32(objExternal.ExternalObject);
                    return(new CilValueInt32(result));
                }
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 13
0
        public CilClassInstance(CilClass @class, CilProgram program, CilManagedMemory managedMemory)
        {
            Class = @class;

            Fields = new Dictionary <string, IValue>();
            foreach (var field in @class.Fields.Where(f => !f.IsStatic))
            {
                Fields.Add(field.Name, field.Type.CreateDefaultValue(program));
            }

            var runtimeType = program.IsValueType(@class.Name) ? Class.BuildRuntimeType(program, managedMemory) : Class.BuildRuntimeProxy(program);

            _externalInstance = FormatterServices.GetUninitializedObject(runtimeType);
        }
Ejemplo n.º 14
0
        public override IValue CreateDefaultValue(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                var type = ReflectionHelper.GetExternalType(ClassName);

                var getDefault   = GetType().GetMethod(nameof(GetDefaultGeneric), BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(type);
                var defaultValue = getDefault.Invoke(null, null);

                return(new CilValueExternal(defaultValue));
            }

            var classType = new CilTypeClass(ClassName);

            return(classType.CreateDefaultValue(program));
        }
Ejemplo n.º 15
0
        public bool IsInstanceOf(CilTypeSpec typeSpec, CilProgram program)
        {
            var cilType = typeSpec.GetCilType(program);

            if (cilType is CilTypeClass cilTypeClass)
            {
                if (cilTypeClass.ClassName.ToString() == this.Class.Name.ToString())
                {
                    return(true);
                }

                throw new System.NotImplementedException();
            }
            else
            {
                throw new System.NotImplementedException();
            }
        }
        public CilInterpreterInstructionsVisitor(CilProgram program)
        {
            var state         = new CilControlState(program);
            var managedMemory = new CilDictionaryManagedMemory();

            _executionState = new CilExecutionState(state, managedMemory);

            _instructionNoneVisitor              = new InstructionNoneInterpreterVisitor(program, _executionState);
            _instructionMethodVisitor            = new InstructionMethodInterpreterVisitor(program, _executionState);
            _instructionStringVisitor            = new InstructionStringInterpreterVisitor(program, _executionState);
            _instructionIVisitor                 = new InstructionIInterpreterVisitor(program, _executionState);
            _instructionTypeVisitor              = new InstructionTypeInterpreterVisitor(program, _executionState);
            _instructionVarVisitor               = new InstructionVarInterpreterVisitor(program, _executionState);
            _instructionRVisitor                 = new InstructionRInterpreterVisitor(program, _executionState);
            _instructionBrVisitor                = new InstructionBrInterpreterVisitor(program, _executionState);
            _instructionFieldVisitor             = new InstructionFieldInterpreterVisitor(program, _executionState);
            _instructionI8InterpreterVisitor     = new InstructionI8InterpreterVisitor(program, _executionState);
            _instructionSwitchInterpreterVisitor = new InstructionSwitchInterpreterVisitor(program, _executionState);
            _instructionTokInterpreterVisitor    = new InstructionTokInterpreterVisitor(program, _executionState);
        }
Ejemplo n.º 17
0
        private bool CheckSupported(CilProgram program)
        {
            foreach (var method in program.AllMethods)
            {
                foreach (var instruction in method.Instructions)
                {
                    if (!instruction.IsSupported)
                    {
                        throw new InstructionNotSupportedException(instruction);
                    }
                }
            }

            if (program.Datas.Count > 0)
            {
                throw new FeatureNotSupportedException(".data");
            }

            return(true);
        }
Ejemplo n.º 18
0
        public CilControlState(CilProgram program)
        {
            CallStack = new Stack <CilMethodState>();
            CallStack.Push(new CilMethodState(program.EntryPoint, new List <CilSigArg>(), new List <IValue>(), program));

            var cctors = program.Classes
                         .Select(c => c.Methods.FirstOrDefault(m => m.Name == ".cctor"))
                         .Where(m => m != null)
                         .ToList();

            foreach (var cctor in cctors)
            {
                CallStack.Push(new CilMethodState(cctor, new List <CilSigArg>(), new List <IValue>(), program));
            }

            StaticInstances = new Dictionary <string, CilClassStaticInstance>();
            foreach (var @class in program.Classes)
            {
                StaticInstances.Add(@class.Name.ToString(), new CilClassStaticInstance(@class, program));
            }
        }
Ejemplo n.º 19
0
        public object AsRuntime(CilType cilType, CilManagedMemory managedMemory, CilProgram program)
        {
            if (cilType is CilTypeChar)
            {
                return((char)Value);
            }
            if (cilType is CilTypeInt16)
            {
                return(Value);
            }
            if (cilType is CilTypeUInt16)
            {
                return((ushort)Value);
            }
            if (cilType is CilTypeValueType)
            {
                return(Value);
            }

            throw new System.NotImplementedException();
        }
Ejemplo n.º 20
0
        public CilParserResult Parse(string sourceCode)
        {
            var ironyParseTree = _ironyParser.Parse(sourceCode);

            if (ironyParseTree.Status == ParseTreeStatus.Error)
            {
                return(new CilParserResult
                {
                    Status = CilParserStatus.ParsingError,
                    Errors = ironyParseTree.ParserMessages.Select(pm => BuildIronyParserMessage(pm)).ToList()
                });
            }

            var decls      = (ironyParseTree.Root.AstNode as DeclsAstNode).Decls;
            var cilProgram = new CilProgram(decls);

            return(new CilParserResult
            {
                Status = CilParserStatus.Success,
                Program = cilProgram
            });
        }
Ejemplo n.º 21
0
        public override object AsRuntime(CilType type, CilManagedMemory managedMemory, CilProgram program)
        {
            if (type is CilTypeObject)
            {
                return(ExternalObject);
            }

            if (type.GetRuntimeType(program).IsAssignableFrom(ExternalObject.GetType()))
            {
                return(ExternalObject);
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 22
0
 public object AsRuntime(CilType cilType, CilManagedMemory managedMemory, CilProgram program)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 23
0
        public InstructionMethodInterpreterVisitor(CilProgram program, CilExecutionState executionState)
        {
            _executionState = executionState;

            _program = program;
        }
Ejemplo n.º 24
0
 public override IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 25
0
 public override IValue CreateDefaultValue(CilProgram program)
 {
     return(new CilValueInt8(default(sbyte)));
 }
Ejemplo n.º 26
0
        public override IValue CreateValueFromRuntime(object obj, CilManagedMemory managedMemory, CilProgram program)
        {
            var value = new CilValueInt8((sbyte)obj);

            return(value);
        }
Ejemplo n.º 27
0
 public override Type GetValueType(CilProgram program)
 {
     return(typeof(CilValueInt8));
 }
Ejemplo n.º 28
0
 public override Type GetRuntimeType(CilProgram program)
 {
     return(typeof(sbyte));
 }
Ejemplo n.º 29
0
 public override bool IsValueType(CilProgram program)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 30
0
        public void Interpret(CilProgram program)
        {
            var interpreter = new CilInterpreter(program);

            interpreter.Interpret();
        }