Ejemplo n.º 1
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.º 2
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.º 3
0
        public static Type RegisterType(CilClass cilClass, CilProgram program, CilManagedMemory managedMemory)
        {
            var typeName = $"{cilClass.Name.ClassName}";

            if (RegisteredTypes.ContainsKey(typeName))
            {
                return(RegisteredTypes[typeName]);
            }

            var assemblyName    = new AssemblyName("CILantroTypesAssembly");
            var assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.RunAndCollect);

            var moduleBuilder = assemblyBuilder.DefineDynamicModule("CILantroTypesModule");

            var parentType = ReflectionHelper.GetExternalType(cilClass.ExtendsName);

            var typeBuilder = moduleBuilder.DefineType(typeName, TypeAttributes.Class, parentType);

            RegisteredTypes.Add(typeName, typeBuilder);

            foreach (var field in cilClass.Fields)
            {
                Type resultType = null;
                if (field.Type is CilTypeClass fieldTypeClass)
                {
                    if (fieldTypeClass.ClassName.ToString() == cilClass.Name.ToString())
                    {
                        resultType = typeBuilder;
                    }
                }
                else if (field.Type is CilTypeValueType fieldTypeValueType)
                {
                    if (fieldTypeValueType.ClassName.ToString() == cilClass.Name.ToString())
                    {
                        resultType = typeBuilder;
                    }
                    else
                    {
                        var cilClass2 = program.AllClasses.Single(c => c.Name.ToString() == fieldTypeValueType.ClassName.ToString());
                        resultType = RegisterType(cilClass2, program, managedMemory);
                    }
                }
                else
                {
                    resultType = field.Type.GetRuntimeType(program);
                }

                var fieldBuilder = typeBuilder.DefineField(field.Name, resultType, field.GetRuntimeAttributes());

                if (field.IsStatic)
                {
                    fieldBuilder.SetConstant(field.InitValue.AsRuntime(field.Type, managedMemory, program));
                }
            }

            var type = typeBuilder.CreateType();

            RegisteredTypes[typeName] = type;

            _counter++;

            return(type);
        }
Ejemplo n.º 4
0
        public override void Init(AstContext context, ParseTreeNode parseNode)
        {
            // classHead + _("{") + classDecls + _("}")
            var classChildren = AstChildren.Empty()
                                .Add <ClassHeadAstNode>()
                                .Add("{")
                                .Add <ClassDeclsAstNode>()
                                .Add("}");

            if (classChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Class;

                ClassDecl = new CilClass
                {
                    Name = new CilClassName
                    {
                        ClassName = classChildren.Child1.ClassName
                    },
                    Methods     = classChildren.Child3.ClassDecls.Methods,
                    Fields      = classChildren.Child3.ClassDecls.Fields,
                    Classes     = classChildren.Child3.ClassDecls.Classes,
                    ExtendsName = classChildren.Child1.ExtendsClassName,
                    Attributes  = classChildren.Child1.ClassAttributes
                };

                return;
            }

            // assemblyHead + _("{") + assemblyDecls + _("}")
            var assemblyChildren = AstChildren.Empty()
                                   .Add <AssemblyHeadAstNode>()
                                   .Add("{")
                                   .Add <AssemblyDeclsAstNode>()
                                   .Add("}");

            if (assemblyChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Assembly;

                AssemblyDecl = new CilAssembly
                {
                    Name = assemblyChildren.Child1.AssemblyName
                };

                return;
            }

            // assemblyRefHead + _("{") + assemblyRefDecls + _("}")
            var assemblyRefChildren = AstChildren.Empty()
                                      .Add <AssemblyRefHeadAstNode>()
                                      .Add("{")
                                      .Add <AssemblyRefDeclsAstNode>()
                                      .Add("}");

            if (assemblyRefChildren.PopulateWith(parseNode))
            {
                DeclType        = Other.DeclType.AssemblyRef;
                AssemblyRefDecl = new CilAssemblyRef
                {
                    Name            = assemblyRefChildren.Child1.AssemblyName,
                    PublicKeyTokens = assemblyRefChildren.Child3.AssemblyRefDecls.PublicKeyTokens,
                    Versions        = assemblyRefChildren.Child3.AssemblyRefDecls.Versions
                };

                return;
            }

            // moduleHead
            var moduleChildren = AstChildren.Empty()
                                 .Add <ModuleHeadAstNode>();

            if (moduleChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Module;

                return;
            }

            // _(".file") + _("alignment") + int32
            var fileAlignmentChildren = AstChildren.Empty()
                                        .Add(".file")
                                        .Add("alignment")
                                        .Add <Int32AstNode>();

            if (fileAlignmentChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.FileAlignment;

                return;
            }

            // _(".subsystem") + int32
            var subsystemChildren = AstChildren.Empty()
                                    .Add(".subsystem")
                                    .Add <Int32AstNode>();

            if (subsystemChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Subsystem;

                return;
            }

            // _(".corflags") + int32
            var corFlagsChildren = AstChildren.Empty()
                                   .Add(".corflags")
                                   .Add <Int32AstNode>();

            if (corFlagsChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.CorFlags;

                return;
            }

            // _(".imagebase") + int64
            var imageBaseChildren = AstChildren.Empty()
                                    .Add(".imagebase")
                                    .Add <Int64AstNode>();

            if (imageBaseChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.ImageBase;

                return;
            }

            // _(".stackreserve") + int64
            var stackReserveChildren = AstChildren.Empty()
                                       .Add(".stackreserve")
                                       .Add <Int64AstNode>();

            if (stackReserveChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.StackReserve;

                return;
            }

            // manifestResHead + _("{") + manifestResDecls + _("}")
            var manifestResChildren = AstChildren.Empty()
                                      .Add <ManifestResHeadAstNode>()
                                      .Add("{")
                                      .Add <ManifestResDeclsAstNode>()
                                      .Add("}");

            if (manifestResChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.ManifestRes;

                return;
            }

            // methodHead + methodDecls + _("}")
            var methodChildren = AstChildren.Empty()
                                 .Add <MethodHeadAstNode>()
                                 .Add <MethodDeclsAstNode>()
                                 .Add("}");

            if (methodChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Method;

                MethodDecl = new CilMethod
                {
                    Name         = methodChildren.Child1.MethodName,
                    IsEntryPoint = methodChildren.Child2.MethodDecls.IsEntryPoint,
                    Instructions = methodChildren.Child2.MethodDecls.Instructions,
                    Locals       = methodChildren.Child2.MethodDecls.Locals
                };

                return;
            }

            // customAttrDecl
            var customAttrDeclChildren = AstChildren.Empty()
                                         .Add <CustomAttrDeclAstNode>();

            if (customAttrDeclChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.CustomAttribute;

                return;
            }

            // dataDecl
            var dataDeclChildren = AstChildren.Empty()
                                   .Add <DataDeclAstNode>();

            if (dataDeclChildren.PopulateWith(parseNode))
            {
                DeclType = Other.DeclType.Data;
                DataDecl = dataDeclChildren.Child1.Data;
                DataId   = dataDeclChildren.Child1.DataId;

                return;
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 5
0
        public override void Init(AstContext context, ParseTreeNode parseNode)
        {
            // methodHead + methodDecls + _("}")
            var methodChildren = AstChildren.Empty()
                                 .Add <MethodHeadAstNode>()
                                 .Add <MethodDeclsAstNode>()
                                 .Add("}");

            if (methodChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.Method;
                Method   = new CilMethod
                {
                    Name         = methodChildren.Child1.MethodName,
                    IsEntryPoint = methodChildren.Child2.MethodDecls.IsEntryPoint,
                    Instructions = methodChildren.Child2.MethodDecls.Instructions,
                    Locals       = methodChildren.Child2.MethodDecls.Locals,
                    Arguments    = methodChildren.Child1.Arguments,
                    CallConv     = methodChildren.Child1.CallConv,
                    ReturnType   = methodChildren.Child1.ReturnType
                };

                return;
            }

            // customAttrDecl
            var customAttrChildren = AstChildren.Empty()
                                     .Add <CustomAttrDeclAstNode>();

            if (customAttrChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.CustomAttr;

                return;
            }

            // fieldDecl
            var fieldDeclChildren = AstChildren.Empty()
                                    .Add <FieldDeclAstNode>();

            if (fieldDeclChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.Field;
                Field    = fieldDeclChildren.Child1.Field;

                return;
            }

            // _(".pack") + int32
            var packChildren = AstChildren.Empty()
                               .Add(".pack")
                               .Add <Int32AstNode>();

            if (packChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.Pack;

                return;
            }

            // _(".size") + int32
            var sizeChildren = AstChildren.Empty()
                               .Add(".size")
                               .Add <Int32AstNode>();

            if (sizeChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.Size;

                return;
            }

            // classHead + _("{") + classDecls + _("}")
            var classHeadChildren = AstChildren.Empty()
                                    .Add <ClassHeadAstNode>()
                                    .Add("{")
                                    .Add <ClassDeclsAstNode>()
                                    .Add("}");

            if (classHeadChildren.PopulateWith(parseNode))
            {
                DeclType = ClassDeclType.Class;

                ClassDecl = new CilClass
                {
                    Name = new CilClassName
                    {
                        ClassName = classHeadChildren.Child1.ClassName
                    },
                    Methods     = classHeadChildren.Child3.ClassDecls.Methods,
                    Fields      = classHeadChildren.Child3.ClassDecls.Fields,
                    ExtendsName = classHeadChildren.Child1.ExtendsClassName,
                    Attributes  = classHeadChildren.Child1.ClassAttributes
                };

                return;
            }

            throw new NotImplementedException();
        }