Example #1
0
        public static Module FromObjectFile(Vm vm, ObjectFile file)
        {
            Module result = new Module(vm);

            result.Types        = file.Types;
            result.Instructions = file.Instructions;

            if (file.ShareMP)
            {
                if (_sharedMP == null)
                {
                    _sharedMP = ExpandData(vm, file.Data);
                }
                result.MP = _sharedMP;
            }
            else
            {
                result.MP = ExpandData(vm, file.Data);
            }

            result.Exports         = file.Link;
            result.ImportedModules = file.Imports;

            result.EntryPC        = file.EntryPC;
            result.EntryFrameType = file.EntryFrameType;

            return(result);
        }
Example #2
0
        static void Main(string[] args)
        {
            Vm vm = new Vm();

            vm.RegisterModule("$Sys", new SysModule(vm));

            ObjectFile f = ObjectFile.FromFile(@"C:\inferno\test.dis");
            Module     m = Module.FromObjectFile(vm, f);

            vm.StartMainThread(m);
            while (vm.Step())
            {
                ;
            }
        }
Example #3
0
        IModule GetModule(string path, ModuleImportDescriptor descriptor)
        {
            IModule result;

            if (!_loadedModules.TryGetValue(path, out result))
            {
                ObjectFile obj = ObjectFile.FromFile(path);
                if (obj == null)
                {
                    return(null);
                }

                result = Module.FromObjectFile(this, obj);
                _loadedModules[path] = result;
            }

            if (!result.ValidateImports(descriptor))
            {
                return(null);
            }

            return(result);
        }
Example #4
0
        public static ObjectFile FromReader(DisReader reader)
        {
            int magic = reader.ReadOP();

            if ((magic != XMAGIC) && (magic != SMAGIC))
            {
                throw new InvalidDataException("Invalid magic number");
            }

            if (magic == SMAGIC)
            {
                int    slength   = reader.ReadOP();
                byte[] signature = reader.ReadBytes(slength);
            }

            int runtimeFlag = reader.ReadOP();
            int stackExtent = reader.ReadOP();
            int codeSize    = reader.ReadOP();
            int dataSize    = reader.ReadOP();
            int typeSize    = reader.ReadOP();
            int linkSize    = reader.ReadOP();
            int entryPc     = reader.ReadOP();
            int entryType   = reader.ReadOP();

            ObjectFile result = new ObjectFile();

            result.ShareMP        = ((runtimeFlag & FLAG_SHAREMP) != 0);
            result.StackExtent    = stackExtent;
            result.EntryPC        = entryPc;
            result.EntryFrameType = entryType;

            result.Instructions = new Instruction[codeSize];
            for (int i = 0; i < codeSize; i++)
            {
                Instruction inst = Instruction.FromReader(reader);
                result.Instructions[i] = inst;
            }

            result.Types = new TypeDescriptor[typeSize];
            for (int i = 0; i < typeSize; i++)
            {
                result.Types[i] = TypeDescriptor.FromReader(reader);
            }


            List <DataDescriptor> data = new List <DataDescriptor> ();

            while (true)
            {
                DataDescriptor descriptor = DataDescriptor.FromReader(reader);
                if (descriptor.Code == 0)
                {
                    break;
                }

                data.Add(descriptor);
            }
            result.Data = data.ToArray();

            result.Name = reader.ReadString();

            result.Link = new ExportDescriptor[linkSize];
            for (int i = 0; i < linkSize; i++)
            {
                ExportDescriptor desc = ExportDescriptor.FromReader(reader);
                desc.FrameSize = result.Types[desc.FrameTypeId].Size;

                result.Link[i] = desc;
            }

            if (!reader.AtEndOfStream)
            {
                int importCount = reader.ReadOP();

                List <ModuleImportDescriptor> imports = new List <ModuleImportDescriptor> (importCount);
                for (int i = 0; i < importCount; i++)
                {
                    imports.Add(ModuleImportDescriptor.FromReader(reader));
                }

                result.Imports = imports.ToArray();
            }

            if (!reader.AtEndOfStream)
            {
                int handlerCount = reader.ReadOP();

                List <HandlerDescriptor> handlers = new List <HandlerDescriptor> (handlerCount);
                for (int i = 0; i < handlerCount; i++)
                {
                    handlers.Add(HandlerDescriptor.FromReader(reader));
                }

                result.Handlers = handlers.ToArray();
            }

            return(result);
        }
Example #5
0
 public static string DumpInstructions(ObjectFile obj)
 {
     return(string.Join(Environment.NewLine, obj.Instructions
                        .Select(i => DumpInstruction(i))
                        .ToArray()));
 }