public void DecompileFunction(StreamWriter output, int functionIndex)
        {
            FunctionBody      body      = WasmModule.FunctionBodies[functionIndex];
            FunctionSignature signature = WasmModule.FunctionTypes[WasmModule.Functions[functionIndex]];

            // get IR
            var context = new IntermediateContext(signature, WasmModule, output);
            List <IntermediateInstruction> instructions = new IntermediateConverter(WasmModule, body, signature).Convert();

            output.Write(signature.ToString($"fun_{functionIndex:X8}"));
            output.WriteLine(" {");

            // write all IR while simulating the stack
            foreach (IntermediateInstruction instruction in instructions)
            {
                HandleInstruction(ref context, instruction);
            }

            output.WriteLine("}");

            if (context.Indentation != 0)
            {
                throw new Exception("Function body has unbalanced indentation");
            }

            if (context.Stack.Count != 0)
            {
                throw new Exception($"Unbalanced stack, found {context.Stack.Count} remaining values");
            }

            output.WriteLine();
        }
예제 #2
0
        public void DecompileFunction(StreamWriter output, int functionIndex)
        {
            FunctionBody      body      = WasmModule.FunctionBodies[functionIndex];
            FunctionSignature signature = WasmModule.FunctionTypes[WasmModule.Functions[functionIndex]];

            List <IntermediateInstruction> instructions = new IntermediateConverter(WasmModule, body, signature).Convert();

            Graph graph = CreateGraph(instructions);

            output.Write(signature.ToString($"fun_{functionIndex:X8}"));
            output.WriteLine(" {");
            OutputAsCode(graph, output);
            output.WriteLine("}");
            output.WriteLine();

            // using var sw = new StreamWriter(File.Open("graph.dot", FileMode.Create));
            // new DotWriter(sw).Write(graph);
        }
예제 #3
0
 public ItemWrapper(int location, FunctionSignature signature)
 {
     Location  = location.ToString();
     Signature = signature.ToString();
 }