コード例 #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);
        }
コード例 #2
0
ファイル: CilTypeClass.cs プロジェクト: kvasnyk/CILantro
        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());
        }
コード例 #3
0
ファイル: CilTypeArray.cs プロジェクト: kvasnyk/CILantro
        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);
        }
コード例 #4
0
        public object AsRuntime(CilType cilType, CilManagedMemory managedMemory, CilProgram program)
        {
            if (cilType is CilTypeUInt32)
            {
                return(Value);
            }

            throw new System.NotImplementedException();
        }
コード例 #5
0
        public static Type GetExternalType(CilTypeSpec typeSpec, CilProgram program)
        {
            if (typeSpec.ClassName != null)
            {
                return(GetExternalType(typeSpec.ClassName));
            }

            return(typeSpec.Type.GetRuntimeType(program));
        }
コード例 #6
0
        public override Type GetValueType(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                return(typeof(CilValueExternal));
            }

            return(typeof(CilValueValueType));
        }
コード例 #7
0
ファイル: CilTypeClass.cs プロジェクト: kvasnyk/CILantro
        public override Type GetRuntimeType(CilProgram program)
        {
            if (program.IsExternalType(ClassName))
            {
                return(ReflectionHelper.GetExternalType(ClassName));
            }

            throw new NotImplementedException();
        }
コード例 #8
0
ファイル: CilTypeClass.cs プロジェクト: kvasnyk/CILantro
        public override Type GetValueType(CilProgram program)
        {
            if (IsValueType(program))
            {
                return(typeof(CilValueValueType));
            }

            return(typeof(CilValueReference));
        }
コード例 #9
0
ファイル: CilArray.cs プロジェクト: kvasnyk/CILantro
        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;
        }
コード例 #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);
            }
        }
コード例 #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);
            }
        }
コード例 #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();
        }
コード例 #13
0
ファイル: CilClassInstance.cs プロジェクト: kvasnyk/CILantro
        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);
        }
コード例 #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));
        }
コード例 #15
0
ファイル: CilClassInstance.cs プロジェクト: kvasnyk/CILantro
        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();
            }
        }
コード例 #16
0
        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);
        }
コード例 #17
0
ファイル: CilInterpreter.cs プロジェクト: kvasnyk/CILantro
        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);
        }
コード例 #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));
            }
        }
コード例 #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();
        }
コード例 #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
            });
        }
コード例 #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();
        }
コード例 #22
0
ファイル: CilValueValueType.cs プロジェクト: kvasnyk/CILantro
 public object AsRuntime(CilType cilType, CilManagedMemory managedMemory, CilProgram program)
 {
     throw new NotImplementedException();
 }
コード例 #23
0
        public InstructionMethodInterpreterVisitor(CilProgram program, CilExecutionState executionState)
        {
            _executionState = executionState;

            _program = program;
        }
コード例 #24
0
 public override IValue Unbox(CilObject obj, CilManagedMemory managedMemory, CilProgram program)
 {
     throw new NotImplementedException();
 }
コード例 #25
0
 public override IValue CreateDefaultValue(CilProgram program)
 {
     return(new CilValueInt8(default(sbyte)));
 }
コード例 #26
0
        public override IValue CreateValueFromRuntime(object obj, CilManagedMemory managedMemory, CilProgram program)
        {
            var value = new CilValueInt8((sbyte)obj);

            return(value);
        }
コード例 #27
0
 public override Type GetValueType(CilProgram program)
 {
     return(typeof(CilValueInt8));
 }
コード例 #28
0
 public override Type GetRuntimeType(CilProgram program)
 {
     return(typeof(sbyte));
 }
コード例 #29
0
 public override bool IsValueType(CilProgram program)
 {
     throw new NotImplementedException();
 }
コード例 #30
0
        public void Interpret(CilProgram program)
        {
            var interpreter = new CilInterpreter(program);

            interpreter.Interpret();
        }