private static void DeclareFunctions(WasmNodeContext context, WasmFunctionSection funcSection, WasmTypeSection typeSection) { for (var i = 0; i < funcSection.Entries.Count; i++) { var func = funcSection.Entries[i]; var sig = typeSection.Entries[(int)func]; var node = new FunctionNode(sig) { Name = $"func_{i}", Execution = new NodesList(sig.Return) }; context.Module.Functions.Add(node); } }
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 CallNode(FunctionNode func) { Function = func; }