Beispiel #1
0
        public override void Write(ModuleWriter writer)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Create the member header.
            MemberHeader mheader = new MemberHeader();

            mheader.memberType       = (byte)MemberHeaderType.TypeInstance;
            mheader.memberFlags      = (uint)GetFlags();
            mheader.memberName       = 0;
            mheader.memberSize       = (uint)(8 + genericInstance.GetSize());
            mheader.memberAttributes = 0;

            // Write the member header.
            mheader.Write(writer);

            // Write the template id.
            writer.Write(module.RegisterMember(template));

            // Write the factory id.
            writer.Write(module.RegisterMember(factory));

            // Write the template parameters.
            genericInstance.Write(writer, GetModule());
        }
Beispiel #2
0
        public override void Write(ModuleWriter writer)
        {
            // The global namespace hasn't name.
            string name = GetName();

            if (parentScope == null)
            {
                name = string.Empty;
            }

            // Write the header.
            MemberHeader header = new MemberHeader();

            header.memberName  = (uint)GetModule().RegisterString(name);
            header.memberType  = (byte)MemberHeaderType.Namespace;
            header.memberFlags = (uint)GetFlags();
            header.memberSize  = (uint)(members.Count * 4);
            header.Write(writer);

            // Write the member ids.
            foreach (ScopeMember member in this.members.Values)
            {
                writer.Write((uint)member.GetSerialId());
            }
        }
Beispiel #3
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Read the type.
            type = module.GetType(reader.ReadUInt());

            // Read the number of indices.
            int numIndices = reader.ReadByte();

            if (numIndices > 0)
            {
                indices = new IChelaType[numIndices];
            }

            // Read the indices.
            for (int i = 0; i < numIndices; ++i)
            {
                indices[i] = module.GetType(reader.ReadUInt());
            }

            // Read the get accessor.
            getAccessor = (Function)module.GetMember(reader.ReadUInt());

            // Read the set accessor.
            setAccessor = (Function)module.GetMember(reader.ReadUInt());
        }
Beispiel #4
0
        internal override void Read2(ModuleReader reader, MemberHeader header)
        {
            // Skip the data.
            reader.Skip(header.memberSize);

            // Handle type instance dependencies.
            ReadData();
        }
Beispiel #5
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Read the type.
            actualType = module.GetType(reader.ReadUInt());
        }
Beispiel #6
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Read the functions into the member list.
            memberList = new List <Function> ();
            int numfunctions = (int)header.memberSize / 4;

            for (int i = 0; i < numfunctions; ++i)
            {
                memberList.Add((Function)GetModule().GetMember(reader.ReadUInt()));
            }
        }
Beispiel #7
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Read the children.
            uint nummembers = header.memberSize / 4u;

            for (uint i = 0; i < nummembers; ++i)
            {
                uint child = reader.ReadUInt();
                AddMember(GetModule().GetMember(child));
            }
        }
Beispiel #8
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Read the types into the member list.
            memberList = new List <Structure>();
            int numtypes = (int)header.memberSize / 4;

            for (int i = 0; i < numtypes; ++i)
            {
                memberList.Add((Structure)GetModule().GetMember(reader.ReadUInt()));
            }
        }
Beispiel #9
0
        internal static new void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal structure and register it.
            Class clazz = new Class(module);
            module.RegisterMember(clazz);

            // Read the name.
            clazz.name = module.GetString(header.memberName);
            clazz.flags = (MemberFlags)header.memberFlags;

            // Skip the class elements.
            reader.Skip(header.memberSize);
        }
Beispiel #10
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal namespace and register it.
            Namespace space = new Namespace(module);

            module.RegisterMember(space);

            // Read the name.
            space.name = module.GetString(header.memberName);

            // Skip the namespace elements.
            reader.Skip(header.memberSize);
        }
Beispiel #11
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal event and register it.
            TypeGroup tgroup = new TypeGroup(module);

            module.RegisterMember(tgroup);

            // Read the name.
            tgroup.name = module.GetString(header.memberName);

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #12
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal event and register it.
            PropertyVariable prop = new PropertyVariable(module);

            module.RegisterMember(prop);

            // Read the name and flags.
            prop.SetName(module.GetString(header.memberName));
            prop.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #13
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();

            header.memberType  = (byte)MemberHeaderType.TypeName;
            header.memberName  = GetModule().RegisterString(GetName());
            header.memberFlags = (uint)GetFlags();
            header.memberSize  = 4;
            header.Write(writer);

            // Write the type and accessors..
            writer.Write((uint)GetModule().RegisterType(GetActualType()));
        }
Beispiel #14
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal type name and register it.
            TypeNameMember typeName = new TypeNameMember(module);

            module.RegisterMember(typeName);

            // Read the name and flags.
            typeName.name  = module.GetString(header.memberName);
            typeName.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #15
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();

            header.memberName  = GetModule().RegisterString(GetName());
            header.memberType  = (byte)MemberHeaderType.FunctionGroup;
            header.memberFlags = (uint)GetFlags();
            header.memberSize  = (uint)(functions.Count * 4);
            header.Write(writer);

            // Write the functions ids.
            foreach (FunctionGroupName gname in functions)
            {
                writer.Write((uint)gname.GetFunction().GetSerialId());
            }
        }
Beispiel #16
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();

            header.memberType  = (byte)MemberHeaderType.Property;
            header.memberName  = GetModule().RegisterString(GetName());
            header.memberFlags = (uint)GetFlags();
            header.memberSize  = (uint)(13 + 4 * GetIndexCount());
            header.Write(writer);

            // Write the type.
            ChelaModule module = GetModule();

            writer.Write((uint)module.RegisterType(GetVariableType()));

            // Write the indices.
            byte numIndices = (byte)GetIndexCount();

            writer.Write(numIndices);
            for (int i = 0; i < numIndices; ++i)
            {
                writer.Write(module.RegisterType((IChelaType)indices[i]));
            }

            // Write the get accessor.
            if (getAccessor != null)
            {
                writer.Write((uint)getAccessor.GetSerialId());
            }
            else
            {
                writer.Write((uint)0);
            }

            // Write the set accessor.
            if (setAccessor != null)
            {
                writer.Write((uint)setAccessor.GetSerialId());
            }
            else
            {
                writer.Write((uint)0);
            }
        }
Beispiel #17
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Check if the function is a method.
            MemberFlags instanceFlags = (MemberFlags)header.memberFlags & MemberFlags.InstanceMask;
            bool        method        = instanceFlags != MemberFlags.Static &&
                                        instanceFlags != MemberFlags.StaticConstructor;

            // Create the temporal function and register it.
            Function function = method ? new Method(module) : new Function(module);

            module.RegisterMember(function);

            // Read the name and flags.
            function.name  = module.GetString(header.memberName);
            function.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #18
0
        public override void Write(ModuleWriter writer)
        {
            if (mergedGroup)
            {
                throw new ModuleException("Cannot write temporal type group " + GetFullName());
            }

            // Write the header.
            MemberHeader header = new MemberHeader();

            header.memberName  = GetModule().RegisterString(GetName());
            header.memberType  = (byte)MemberHeaderType.TypeGroup;
            header.memberFlags = (uint)GetFlags();
            header.memberSize  = (uint)(types.Count * 4);
            header.Write(writer);

            // Write the functions ids.
            foreach (TypeGroupName gname in types)
            {
                writer.Write((uint)gname.GetBuilding().GetSerialId());
            }
        }
Beispiel #19
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Read the function header.
            FunctionHeader fheader = new FunctionHeader();

            fheader.Read(reader);

            // Read the function type.
            type = (FunctionType)GetModule().GetType(fheader.functionType);

            // Read the generic prototype.
            genericPrototype = GenericPrototype.Read(reader, GetModule());

            // Read the vslot.
            if (IsMethod())
            {
                Method method = (Method)this;
                method.vslot = fheader.vslot;
            }

            // Skip the elements.
            reader.Skip(header.memberSize - FunctionHeader.HeaderSize - genericPrototype.GetSize());
        }
Beispiel #20
0
        public override void Write(ModuleWriter writer)
        {
            // Count the block size.
            int blockSize = 0;

            foreach (BasicBlock bb in basicBlocks)
            {
                blockSize += bb.GetRawBlockSize();
            }

            // Count the argument size.
            int argSize = 0;

            for (int i = 0; i < arguments.Length; ++i)
            {
                argSize += arguments[i].GetSize();
            }

            // Count the exceptions.
            int numexceptions = CountExceptionContexts();
            int exceptionSize = 0;

            if (numexceptions > 0)
            {
                exceptionSize = exceptionContext.GetFullSize();
            }

            // Create the member header.
            MemberHeader mheader = new MemberHeader();

            mheader.memberType  = (byte)MemberHeaderType.Function;
            mheader.memberFlags = (uint)GetFlags();
            mheader.memberName  = GetModule().RegisterString(GetName());
            mheader.memberSize  = (uint)(FunctionHeader.HeaderSize + blockSize +
                                         argSize + nextLocalId * LocalVariable.LocalDataSize + exceptionSize +
                                         genericPrototype.GetSize());
            mheader.memberAttributes = GetAttributeCount();

            // Write the member header.
            mheader.Write(writer);

            // Write the attributes.
            WriteAttributes(writer);

            // Create the header.
            FunctionHeader header = new FunctionHeader();

            header.functionType  = GetModule().RegisterType(GetFunctionType());
            header.numargs       = (ushort)arguments.Length;
            header.numlocals     = (ushort)nextLocalId;
            header.numblocks     = (ushort)basicBlocks.Count;
            header.numexceptions = (byte)numexceptions;
            if (IsMethod() && !IsStatic())
            {
                Method method = (Method)this;
                if (method.IsAbstract() || method.IsContract() ||
                    method.IsOverride() || method.IsVirtual())
                {
                    header.vslot = (short)method.GetVSlot();
                }
            }

            // Write the header.
            header.Write(writer);

            // Write the generic signature.
            genericPrototype.Write(writer, GetModule());

            // Write the argument names.
            ChelaModule module = GetModule();

            for (int i = 0; i < arguments.Length; ++i)
            {
                arguments[i].Write(writer, module);
            }

            // Write the locals.
            foreach (LocalVariable local in locals)
            {
                if (!local.IsPseudoLocal)
                {
                    local.Write(writer);
                }
            }

            // Write the basic blocks.
            foreach (BasicBlock bb in basicBlocks)
            {
                bb.Write(GetModule(), writer);
            }

            // Write the exception contexts.
            if (exceptionContext != null)
            {
                exceptionContext.Write(writer);
            }
        }
Beispiel #21
0
 internal override void Read(ModuleReader reader, MemberHeader header)
 {
     // Read the types into the member list.
     memberList = new List<Structure>();
     int numtypes = (int)header.memberSize / 4;
     for (int i = 0; i < numtypes; ++i)
         memberList.Add((Structure)GetModule().GetMember (reader.ReadUInt ()));
 }
Beispiel #22
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal event and register it.
            TypeGroup tgroup = new TypeGroup (module);
            module.RegisterMember (tgroup);

            // Read the name.
            tgroup.name = module.GetString (header.memberName);

            // Skip the structure elements.
            reader.Skip (header.memberSize);
        }
Beispiel #23
0
        public override void Write(ModuleWriter writer)
        {
            if(mergedGroup)
                throw new ModuleException("Cannot write temporal type group " + GetFullName());

            // Write the header.
            MemberHeader header = new MemberHeader ();
            header.memberName = GetModule ().RegisterString (GetName ());
            header.memberType = (byte)MemberHeaderType.TypeGroup;
            header.memberFlags = (uint)GetFlags ();
            header.memberSize = (uint)(types.Count * 4);
            header.Write (writer);

            // Write the functions ids.
            foreach (TypeGroupName gname in types)
                writer.Write ((uint)gname.GetBuilding().GetSerialId ());
        }
Beispiel #24
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal event and register it.
            PropertyVariable prop = new PropertyVariable(module);
            module.RegisterMember(prop);

            // Read the name and flags.
            prop.SetName(module.GetString(header.memberName));
            prop.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #25
0
        public override void Write(ModuleWriter writer)
        {
            // Count the block size.
            int blockSize = 0;
            foreach(BasicBlock bb in basicBlocks)
                blockSize += bb.GetRawBlockSize();

            // Count the argument size.
            int argSize = 0;
            for(int i = 0; i < arguments.Length; ++i)
                argSize += arguments[i].GetSize();

            // Count the exceptions.
            int numexceptions = CountExceptionContexts();
            int exceptionSize = 0;
            if(numexceptions > 0)
                exceptionSize = exceptionContext.GetFullSize();

            // Create the member header.
            MemberHeader mheader = new MemberHeader();
            mheader.memberType = (byte)MemberHeaderType.Function;
            mheader.memberFlags = (uint) GetFlags();
            mheader.memberName = GetModule().RegisterString(GetName());
            mheader.memberSize = (uint)(FunctionHeader.HeaderSize + blockSize +
                argSize + nextLocalId*LocalVariable.LocalDataSize + exceptionSize +
                genericPrototype.GetSize());
            mheader.memberAttributes = GetAttributeCount();

            // Write the member header.
            mheader.Write(writer);

            // Write the attributes.
            WriteAttributes(writer);

            // Create the header.
            FunctionHeader header = new FunctionHeader();
            header.functionType = GetModule().RegisterType(GetFunctionType());
            header.numargs = (ushort)arguments.Length;
            header.numlocals = (ushort)nextLocalId;
            header.numblocks = (ushort)basicBlocks.Count;
            header.numexceptions = (byte)numexceptions;
            if(IsMethod() && !IsStatic())
            {
                Method method = (Method)this;
                if(method.IsAbstract() || method.IsContract() ||
                   method.IsOverride() || method.IsVirtual())
                    header.vslot = (short)method.GetVSlot();
            }

            // Write the header.
            header.Write(writer);

            // Write the generic signature.
            genericPrototype.Write(writer, GetModule());

            // Write the argument names.
            ChelaModule module = GetModule();
            for(int i = 0; i < arguments.Length; ++i)
                arguments[i].Write(writer, module);

            // Write the locals.
            foreach(LocalVariable local in locals)
            {
                if(!local.IsPseudoLocal)
                    local.Write(writer);
            }

            // Write the basic blocks.
            foreach(BasicBlock bb in basicBlocks)
                bb.Write(GetModule(), writer);

            // Write the exception contexts.
            if(exceptionContext != null)
                exceptionContext.Write(writer);
        }
Beispiel #26
0
        public void Write(ModuleWriter writer)
        {
            // Create the header.
            ModuleHeader header = new ModuleHeader();

            // Populate it.

            // Store the module type.
            header.moduleType = (uint)moduleType;

            // Write the header.
            header.Write(writer);

            // Prepare the members.
            globalNamespace.PrepareSerialization();
            foreach(ScopeMember instance in genericInstances)
                instance.PrepareSerialization();

            // Prepare type referemnces.
            PrepareTypeReferences();

            // Prepare resource writing.
            PrepareResources();

            // Prepare debug information.
            DebugEmitter debugEmitter = null;
            if(debugBuild)
            {
                debugEmitter = new DebugEmitter(this);
                debugEmitter.Prepare();
                globalNamespace.PrepareDebug(debugEmitter);
            }

            // Don't allow registering members.
            writingModule = true;

            // Write the members.
            header.memberTableOffset = writer.GetPosition();
            foreach(ScopeMember member in memberTable)
            {
                // Write local members, and a pointer for the externals.
                ChelaModule memberModule = member.GetModule();
                if(memberModule == this)
                {
                    member.Write(writer);
                }
                else
                {
                    // Don't write external generic instances.
                    if(member.GetGenericInstance() != null)
                        throw new ModuleException("Write external generic instance.");

                    // Create the member reference header.
                    MemberHeader mheader = new MemberHeader();
                    mheader.memberSize = 1;
                    mheader.memberName = RegisterString(member.GetFullName()); // TODO: Use mangled name.
                    mheader.memberType = (byte)MemberHeaderType.Reference;

                    // Write the reference header and module id.
                    mheader.Write(writer);
                    writer.Write((byte)AddReference(memberModule));
                }
            }
            header.memberTableSize = writer.GetPosition() - header.memberTableOffset;

            // Write module references.
            header.moduleRefTableOffset = writer.GetPosition();
            header.moduleRefTableEntries = (uint) (referencedModules.Count + 1);

            // Write myself.
            ModuleReference modRef = new ModuleReference();
            modRef.moduleName = RegisterString(GetName());
            modRef.Write(writer);

            // Write module references.
            foreach(ChelaModule mod in referencedModules)
            {
                modRef.moduleName = RegisterString(mod.GetName());
                modRef.Write(writer);
            }

            // Write the libraries table.
            header.libTableOffset = writer.GetPosition();
            header.libTableEntries = (uint)nativeLibraries.Count;
            foreach(string libname in nativeLibraries)
                writer.Write(RegisterString(libname));

            // Write the anonymous types.
            header.anonTypeTableOffset = writer.GetPosition();
            WriteAnonType(writer);
            header.anonTypeTableSize = writer.GetPosition() - header.anonTypeTableOffset;

            // Write the type table.
            header.typeTableOffset = writer.GetPosition();
            header.typeTableEntries = (uint)typeTable.Count;
            TypeReference typeRef = new TypeReference();
            foreach(IChelaType type in typeTable)
            {
                // Write the type kind.
                if(type.IsTypeInstance())
                    typeRef.typeKind = (byte)TypeKind.Instance;
                else if(type.IsStructure())
                    typeRef.typeKind = (byte)TypeKind.Structure;
                else if(type.IsClass())
                    typeRef.typeKind = (byte)TypeKind.Class;
                else if(type.IsInterface())
                    typeRef.typeKind = (byte)TypeKind.Interface;
                else if(type.IsFunction())
                    typeRef.typeKind = (byte)TypeKind.Function;
                else if(type.IsReference())
                    typeRef.typeKind = (byte)TypeKind.Reference;
                else if(type.IsPointer())
                    typeRef.typeKind = (byte)TypeKind.Pointer;
                else if(type.IsConstant())
                    typeRef.typeKind = (byte)TypeKind.Constant;
                else if(type.IsArray())
                    typeRef.typeKind = (byte)TypeKind.Array;
                else if(type.IsVector())
                    typeRef.typeKind = (byte)TypeKind.Vector;
                else if(type.IsMatrix())
                    typeRef.typeKind = (byte)TypeKind.Matrix;
                else if(type.IsPlaceHolderType())
                    typeRef.typeKind = (byte)TypeKind.PlaceHolder;

                if(!type.IsStructure() && !type.IsClass() && !type.IsInterface() &&
                   !type.IsTypeInstance())
                {
                    // Write a reference into the anonymous types.
                    typeRef.memberId = (uint)anonymousTypeMap[type];
                    typeRef.typeName = 0;//RegisterString(type.GetName());
                }
                else
                {
                    // Write the type member reference.
                    ScopeMember member = (ScopeMember)type;
                    typeRef.memberId = RegisterMember(member);
                    typeRef.typeName = 0;//RegisterString(type.GetName());
                }

                // Write the type reference.
                typeRef.Write(writer);
            }

            // Write the string table.
            header.stringTableOffset = writer.GetPosition();
            header.stringTableEntries = (uint)stringTable.Count;
            foreach(string s in stringTable)
                writer.Write(s);

            // Write the debug info
            if(debugBuild)
            {
                header.debugInfoOffset = writer.GetPosition();
                debugEmitter.Write(writer);
                header.debugInfoSize = writer.GetPosition() - header.debugInfoOffset;
            }

            // Write the resources.
            header.resourceDataOffset = writer.GetPosition();
            WriteResources(writer);
            header.resourceDataSize = writer.GetPosition() - header.resourceDataOffset;

            // Store the module size.
            header.moduleSize = writer.GetPosition();

            // Store the entry point.
            if(mainFunction != null)
                header.entryPoint = mainFunction.GetSerialId();

            // Write the header again.
            writer.MoveBegin();
            header.Write(writer);

            // Allow more modifications from here.
            writingModule = false;
        }
Beispiel #27
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal namespace and register it.
            Namespace space = new Namespace(module);
            module.RegisterMember(space);

            // Read the name.
            space.name = module.GetString(header.memberName);

            // Skip the namespace elements.
            reader.Skip(header.memberSize);
        }
Beispiel #28
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Read the type.
            actualType = module.GetType(reader.ReadUInt());
        }
Beispiel #29
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Read the function header.
            FunctionHeader fheader = new FunctionHeader();
            fheader.Read(reader);

            // Read the function type.
            type = (FunctionType)GetModule().GetType(fheader.functionType);

            // Read the generic prototype.
            genericPrototype = GenericPrototype.Read(reader, GetModule());

            // Read the vslot.
            if(IsMethod())
            {
                Method method = (Method)this;
                method.vslot = fheader.vslot;
            }

            // Skip the elements.
            reader.Skip(header.memberSize - FunctionHeader.HeaderSize - genericPrototype.GetSize());
        }
Beispiel #30
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();
            header.memberType = (byte) MemberHeaderType.TypeName;
            header.memberName = GetModule().RegisterString(GetName());
            header.memberFlags = (uint) GetFlags();
            header.memberSize = 4;
            header.Write(writer);

            // Write the type and accessors..
            writer.Write((uint)GetModule().RegisterType(GetActualType()));
        }
Beispiel #31
0
        public override void Write(ModuleWriter writer)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Create the member header.
            MemberHeader mheader = new MemberHeader();
            mheader.memberType = (byte)MemberHeaderType.TypeInstance;
            mheader.memberFlags = (uint) GetFlags();
            mheader.memberName = 0;
            mheader.memberSize = (uint)(8 + genericInstance.GetSize());
            mheader.memberAttributes = 0;

            // Write the member header.
            mheader.Write(writer);

            // Write the template id.
            writer.Write(module.RegisterMember(template));

            // Write the factory id.
            writer.Write(module.RegisterMember(factory));

            // Write the template parameters.
            genericInstance.Write(writer, GetModule());
        }
Beispiel #32
0
        internal override void Read(ModuleReader reader, MemberHeader header)
        {
            // Get the module.
            ChelaModule module = GetModule();

            // Read the type.
            type = module.GetType(reader.ReadUInt());

            // Read the number of indices.
            int numIndices = reader.ReadByte();
            if(numIndices > 0)
                indices = new IChelaType[numIndices];

            // Read the indices.
            for(int i = 0; i < numIndices; ++i)
                indices[i] = module.GetType(reader.ReadUInt());

            // Read the get accessor.
            getAccessor = (Function)module.GetMember(reader.ReadUInt());

            // Read the set accessor.
            setAccessor = (Function)module.GetMember(reader.ReadUInt());
        }
Beispiel #33
0
        public static ChelaModule LoadModule(ModuleReader reader, bool fullLoad)
        {
            // Try to guess the file type.
            uint moduleStart = reader.GetPosition();
            byte[] signature;
            reader.Read(out signature, 8);

            // Check for elf file type.
            bool isElf = true;
            for(int i = 0; i < 4; ++i)
            {
                if(signature[i] != ElfFormat.ElfMagic[i])
                {
                    isElf = false;
                    break;
                }
            }

            // Check for PE.
            bool isPE = signature[0] == 'M' && signature[1] == 'Z';

            // Find the module content.
            reader.SetPosition(moduleStart);
            if(isElf)
            {
                // Move to the embedded module.
                ElfFormat.GetEmbeddedModule(reader);
                reader.FixBase();
            }
            else if(isPE)
            {
                // Move to the embedded module.
                PEFormat.GetEmbeddedModule(reader);
                reader.FixBase();
            }

            // Read the module header.
            ModuleHeader header = new ModuleHeader();
            header.Read(reader);

            // Create the output module.
            ChelaModule result = new ChelaModule();

            // Read the string table.
            reader.SetPosition(header.stringTableOffset);
            for(uint i = 0; i < header.stringTableEntries; ++i)
            {
                uint id = i+1u;
                string s = reader.ReadString();
                result.stringTable.Add(s);
                result.registeredStrings.Add(s, id);
            }

            // Read myself data.
            ModuleReference modref = new ModuleReference();
            reader.SetPosition(header.moduleRefTableOffset);
            modref.Read(reader);
            result.SetName(result.GetString(modref.moduleName));

            // Read the module type.
            result.moduleType = (ModuleType)header.moduleType;

            // Read the referenced modules.
            for(uint i = 1; i < header.moduleRefTableEntries; ++i)
            {
                // Read the referenced module.
                modref.Read(reader);

                // Load it.
                ChelaModule referencedModule = LoadNamedModule(result.GetString(modref.moduleName));
                result.referencedModules.Add(referencedModule);
            }

            // Preload the members.
            reader.SetPosition(header.memberTableOffset);
            uint end = header.memberTableOffset + header.memberTableSize;
            MemberHeader mheader = new MemberHeader();
            while(reader.GetPosition() < end)
            {
                // Read the member header.
                mheader.Read(reader);

                // Skip the attributes.
                SkipAttributes(reader, mheader.memberAttributes);

                // Preload the member.
                MemberHeaderType mtype = (MemberHeaderType)mheader.memberType;
                switch(mtype)
                {
                case MemberHeaderType.Namespace:
                    Namespace.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Structure:
                    Structure.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Class:
                    Class.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Interface:
                    Interface.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Function:
                    Function.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Field:
                    FieldVariable.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.FunctionGroup:
                    FunctionGroup.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.TypeInstance:
                    StructureInstance.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Property:
                    PropertyVariable.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Event:
                    EventVariable.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.TypeName:
                    TypeNameMember.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.MemberInstance:
                case MemberHeaderType.FunctionInstance:
                    // Ignore the member instance.
                    result.memberTable.Add(null);
                    reader.Skip(mheader.memberSize);
                    break;
                case MemberHeaderType.TypeGroup:
                    TypeGroup.PreloadMember(result, reader, mheader);
                    break;
                case MemberHeaderType.Reference:
                    {
                        // Read the module id.
                        byte moduleId = reader.ReadByte();

                        // Get the referenced module.
                        ChelaModule refMod = result.GetReferenced(moduleId);

                        // Find the member there.
                        ScopeMember member = refMod.GetMember(result.GetString(mheader.memberName));
                        result.memberTable.Add(member);
                    }
                    break;
                default:
                    // Unknown member.
                    System.Console.WriteLine("add unknown member {0}", mtype);
                    result.memberTable.Add(null);
                    reader.Skip(mheader.memberSize);
                    break;
                }
            }

            // Read the type table.
            reader.SetPosition(header.typeTableOffset);
            for(uint i = 0; i < header.typeTableEntries; ++i)
            {
                // Read the type reference.
                TypeReference typeRef = new TypeReference();
                typeRef.Read(reader);

                // Try to load direct members.
                TypeKind kind = (ChelaModule.TypeKind)typeRef.typeKind;
                if(kind == TypeKind.Class || kind == TypeKind.Interface ||
                   kind == TypeKind.Structure || kind == TypeKind.Instance)
                {
                    // Store the actual type.
                    result.typeTable.Add(result.GetMember(typeRef.memberId));
                }
                else
                {
                    // Store the reference for delayed loading.
                    result.typeTable.Add(typeRef);
                }
            }

            // Read the anonymous types.
            reader.SetPosition(header.anonTypeTableOffset);
            end = header.anonTypeTableOffset + header.anonTypeTableSize;
            while(reader.GetPosition() < end)
            {
                // Read the anonymous type data.
                AnonTypeHeader anonType = new AnonTypeHeader();
                anonType.Read(reader);

                // Store it in the anonymous type table.
                result.anonymousTypes.Add(anonType);
            }

            // Now, read the actual members.
            reader.SetPosition(header.memberTableOffset);
            for(int i = 0; i < result.memberTable.Count; ++i)
            {
                // Get the preloaded member.
                ScopeMember member = result.memberTable[i];

                // Read his header.
                mheader.Read(reader);

                // Skip the attributes.
                SkipAttributes(reader, mheader.memberAttributes);

                // Read the member.
                if(member != null && mheader.memberType != (int)MemberHeaderType.Reference)
                    member.Read(reader, mheader);
                else
                    reader.Skip(mheader.memberSize); // Skip unknown members.
            }

            // The first member must be the global namespace.
            result.globalNamespace = (Namespace)result.GetMember(1);

            // Update the parent children relationship.
            result.globalNamespace.UpdateParent(null);

            // Now, perform second read pass, required by function groups.
            reader.SetPosition(header.memberTableOffset);
            for(int i = 0; i < result.memberTable.Count; ++i)
            {
                // Get the preloaded member.
                ScopeMember member = result.memberTable[i];

                // Read his header.
                mheader.Read(reader);

                // Skip the attributes.
                SkipAttributes(reader, mheader.memberAttributes);

                // Read the member.
                if(member != null && mheader.memberType != (int)MemberHeaderType.Reference)
                    member.Read2(reader, mheader);
                else
                    reader.Skip(mheader.memberSize); // Skip unknown members.
            }

            // Finish all of the loading.
            result.globalNamespace.FinishLoad();

            // Register the member names.
            for(int i = 0; i < result.memberTable.Count; ++i)
            {
                ScopeMember member = result.memberTable[i];
                if(member != null)
                    result.memberNameTable[member.GetFullName()] = member;
            }

            // Load the runtime classes.
            if(result.referencedModules.Count == 0)
                result.LoadRuntimeReferences(result);

            // Dump the readed module.
            //result.Dump();

            // Return the result module.
            return result;
        }
Beispiel #34
0
 internal override void Read(ModuleReader reader, MemberHeader header)
 {
     reader.Read(out rawData, (int)header.memberSize);
 }
Beispiel #35
0
        internal new static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal structure and register it.
            Class clazz = new Class(module);

            module.RegisterMember(clazz);

            // Read the name.
            clazz.name  = module.GetString(header.memberName);
            clazz.flags = (MemberFlags)header.memberFlags;

            // Skip the class elements.
            reader.Skip(header.memberSize);
        }
Beispiel #36
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();
            header.memberName = GetModule().RegisterString(GetName());
            header.memberType = (byte)MemberHeaderType.FunctionGroup;
            header.memberFlags = (uint)GetFlags();
            header.memberSize = (uint)(functions.Count*4);
            header.Write(writer);

            // Write the functions ids.
            foreach(FunctionGroupName gname in functions)
                writer.Write((uint)gname.GetFunction().GetSerialId());
        }
Beispiel #37
0
 internal override void Read(ModuleReader reader, MemberHeader header)
 {
     // Read the children.
     uint nummembers = header.memberSize/4u;
     for(uint i = 0; i < nummembers; ++i)
     {
         uint child = reader.ReadUInt();
         AddMember(GetModule().GetMember(child));
     }
 }
Beispiel #38
0
 internal virtual void Read2(ModuleReader reader, MemberHeader header)
 {
     reader.Skip(header.memberSize);
 }
Beispiel #39
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Check if the function is a method.
            MemberFlags instanceFlags = (MemberFlags)header.memberFlags & MemberFlags.InstanceMask;
            bool method = instanceFlags != MemberFlags.Static &&
                          instanceFlags != MemberFlags.StaticConstructor;

            // Create the temporal function and register it.
            Function function = method ? new Method(module) : new Function(module);
            module.RegisterMember(function);

            // Read the name and flags.
            function.name = module.GetString(header.memberName);
            function.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #40
0
        public override void Write(ModuleWriter writer)
        {
            // Write the header.
            MemberHeader header = new MemberHeader();
            header.memberType = (byte) MemberHeaderType.Property;
            header.memberName = GetModule().RegisterString(GetName());
            header.memberFlags = (uint) GetFlags();
            header.memberSize = (uint) (13 + 4*GetIndexCount());
            header.Write(writer);

            // Write the type.
            ChelaModule module = GetModule();
            writer.Write((uint)module.RegisterType(GetVariableType()));

            // Write the indices.
            byte numIndices = (byte)GetIndexCount();
            writer.Write(numIndices);
            for(int i = 0; i < numIndices; ++i)
                writer.Write(module.RegisterType((IChelaType)indices[i]));

            // Write the get accessor.
            if(getAccessor != null)
                writer.Write((uint)getAccessor.GetSerialId());
            else
                writer.Write((uint)0);

            // Write the set accessor.
            if(setAccessor != null)
                writer.Write((uint)setAccessor.GetSerialId());
            else
                writer.Write((uint)0);
        }
Beispiel #41
0
 internal override void Read(ModuleReader reader, MemberHeader header)
 {
     // Read the functions into the member list.
     memberList = new List<Function> ();
     int numfunctions = (int)header.memberSize/4;
     for(int i = 0; i < numfunctions; ++i)
         memberList.Add((Function)GetModule().GetMember(reader.ReadUInt()));
 }
Beispiel #42
0
        internal static new void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal structure instance.
            StructureInstance instance = new StructureInstance(module);

            module.RegisterMember(instance);

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #43
0
        internal static new void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal structure instance.
            StructureInstance instance = new StructureInstance(module);
            module.RegisterMember(instance);

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #44
0
        public override void Write(ModuleWriter writer)
        {
            // The global namespace hasn't name.
            string name = GetName();
            if(parentScope == null)
                name = string.Empty;

            // Write the header.
            MemberHeader header = new MemberHeader();
            header.memberName = (uint)GetModule().RegisterString(name);
            header.memberType = (byte)MemberHeaderType.Namespace;
            header.memberFlags = (uint)GetFlags();
            header.memberSize = (uint) (members.Count*4);
            header.Write(writer);

            // Write the member ids.
            foreach(ScopeMember member in this.members.Values)
                writer.Write((uint)member.GetSerialId());
        }
Beispiel #45
0
        internal static void PreloadMember(ChelaModule module, ModuleReader reader, MemberHeader header)
        {
            // Create the temporal type name and register it.
            TypeNameMember typeName = new TypeNameMember(module);
            module.RegisterMember(typeName);

            // Read the name and flags.
            typeName.name = module.GetString(header.memberName);
            typeName.flags = (MemberFlags)header.memberFlags;

            // Skip the structure elements.
            reader.Skip(header.memberSize);
        }
Beispiel #46
0
 internal override void Read(ModuleReader reader, MemberHeader header)
 {
     reader.Read(out rawData, (int)header.memberSize);
 }
Beispiel #47
0
        internal override void Read2(ModuleReader reader, MemberHeader header)
        {
            // Skip the data.
            reader.Skip(header.memberSize);

            // Handle type instance dependencies.
            ReadData();
        }
Beispiel #48
0
 internal virtual void Read2(ModuleReader reader, MemberHeader header)
 {
     reader.Skip(header.memberSize);
 }