Beispiel #1
0
        public static AssemblyBuilder Compile(WasmModule module, string className, string assemblyName)
        {
            var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(assemblyName), AssemblyBuilderAccess.RunAndCollect);
            var m        = assembly.DefineDynamicModule(assemblyName);
            var type     = m.DefineType(className);

            var funcSection = module.ReadFunctionSection();
            var codeSection = module.ReadCodeSection();
            var typeSection = module.ReadTypeSection();

            for (var i = 0; i < funcSection.Entries.Count; i++)
            {
                var func = funcSection.Entries[0];
                var sig  = typeSection.Entries[(int)func];
                var code = codeSection.Bodies[i];

                var method = type.DefineMethod($"func_{i}", MethodAttributes.Private, Convert(sig.Return), sig.Parameters.Select(p => Convert(p)).ToArray());
                var ilGen  = method.GetILGenerator();

                var arg     = new WasmMSILArg(ilGen);
                var visitor = new WasmMSIL();
                foreach (var opcode in code.Opcodes)
                {
                    opcode.AcceptVistor(visitor, arg);
                }
            }
            return(assembly);
        }
Beispiel #2
0
        public IntermediateContext(FunctionSignature signature, WasmModule wasmModule, StreamWriter writer)
        {
            Indentation  = 0;
            Signature    = signature;
            WasmModule   = wasmModule;
            streamWriter = writer;
            Stack        = new Stack <Variable>();
            StackIndices = new Stack <int>();
            StackIndices.Push(0);
            varCount = 0;

            RestOfBlockUnreachable = false;
        }
Beispiel #3
0
        public static int Main(string[] args)
        {
            WasmModule module;

            using (FileStream input = File.Open(@".\sample.wasm", FileMode.Open, FileAccess.Read)) {
                module = WasmModule.Create(input);
            }
            bool validModule = module.Validate();

            using (FileStream output = File.Open(@".\sample.wat", FileMode.Create, FileAccess.Write)) {
                module.ToText(output);
            }
            return(0);
        }
Beispiel #4
0
        private static void Main(string[] args)
        {
            // print debug messages to console
            Trace.Listeners.Add(new ConsoleTraceListener());

            var arguments = Cli.Parse <CliArguments>(args);

            if (!File.Exists(arguments.InputFile))
            {
                Console.WriteLine("Pass me a file as argument");
                return;
            }

            var sw       = Stopwatch.StartNew();
            var wasmFile = WasmModule.Read(arguments.InputFile);

            sw.Stop();
            Console.WriteLine($"Read in {sw.Elapsed}");

            Console.WriteLine("wasm version: " + wasmFile.Version);

            sw = Stopwatch.StartNew();
            Stream outputStream = arguments.OutputFile is null
                ? Console.OpenStandardOutput()
                : File.Open(arguments.OutputFile, FileMode.Create);

            using var w = new StreamWriter(outputStream);
            IDecompiler dec = arguments.Decompiler switch {
                DecompilerKind.Disassembler => (IDecompiler) new DisassemblingDecompiler(wasmFile),
                DecompilerKind.IntermediateRepresentation => new IntermediateRepresentationDecompiler(wasmFile),
                DecompilerKind.Generic => new GenericDecompiler(wasmFile),
                _ => throw new Exception("Invalid decompiler type specified"),
            };

            for (uint i = arguments.Skip; i < Math.Min(arguments.Skip + arguments.Count, wasmFile.FunctionBodies.Length); i++)
            {
                Debug.WriteLine($"Decompiling function {i} (0x{i:X})");
                dec.DecompileFunction(w, (int)i);
            }

            sw.Stop();
            Console.WriteLine($"Written to {arguments.OutputFile} in {sw.Elapsed}");
        }
 public GenericDecompiler(WasmModule wasmModule)
 {
     WasmModule = wasmModule;
 }
Beispiel #6
0
 public Rewriter(WasmModule module, string @namespace, string @class)
 {
     Module    = module;
     Namespace = @namespace;
     Class     = @class;
 }
Beispiel #7
0
        public static ModuleNode Compile(WasmModule module)
        {
            var funcSection   = module.Function;
            var codeSection   = module.Code;
            var typeSection   = module.Type;
            var importSection = module.Import;
            var globalSection = module.Global;

            var moduleNode = new ModuleNode();

            var context = new WasmNodeContext {
                Module = moduleNode
            };

            foreach (var type in typeSection.Entries)
            {
                context.Types.Add(type);
            }

            foreach (var import in importSection.Entries)
            {
                switch (import.Kind)
                {
                case WasmExternalKind.Function:
                    var type     = typeSection.Entries[(int)import.TypeIndex];
                    var function = new FunctionNode(type)
                    {
                        Name = $"{import.Module}_{import.Field}"
                    };
                    moduleNode.ImportedFunctions.Add(function);
                    moduleNode.Imports.Add(new ImportNode {
                        Module = import.Module,
                        Field  = import.Field,
                        Node   = function
                    });
                    break;

                case WasmExternalKind.Global:
                    var global = new GlobalNode(import.Global.Type, import.Global.Mutable)
                    {
                        Name = $"{import.Module}_{import.Field}"
                    };
                    moduleNode.ImportedGlobals.Add(global);
                    moduleNode.Imports.Add(new ImportNode {
                        Module = import.Module,
                        Field  = import.Field,
                        Node   = global
                    });
                    break;
                }
            }

            AddGlobals(context, globalSection);

            DeclareFunctions(context, funcSection, typeSection);

            for (var i = 0; i < codeSection.Bodies.Count; i++)
            {
                var code = codeSection.Bodies[i];
                var func = moduleNode.Functions[i];

                var arg = new WasmFunctionNodeArg(func)
                {
                    Context = context
                };

                foreach (var local in code.Locals)
                {
                    func.AddLocals(local.Type, local.Count);
                }

                arg.PushBlock(func.Execution);
                var visitor = new WasmNode();
                foreach (var opcode in code.Opcodes)
                {
                    opcode.AcceptVistor(visitor, arg);
                }
            }

            return(moduleNode);
        }
 public WasmModuleInstance(WasmModule module)
 {
     Module  = module;
     Exports = new WasmModuleExports(this);
 }
 public IntermediateConverter(WasmModule wasmModule, FunctionBody function, FunctionSignature signature)
 {
     this.wasmModule = wasmModule;
     this.function   = function;
     this.signature  = signature;
 }
Beispiel #10
0
 public DisassemblingDecompiler(WasmModule wasmModule)
 {
     WasmModule = wasmModule;
 }
 public IntermediateRepresentationDecompiler(WasmModule wasmModule)
 {
     WasmModule = wasmModule;
 }