private static void DeclareGlobals(WasmNodeContext context, WasmGlobalSection section) { foreach (var global in section.Entries) { var variable = new GlobalNode(global.Type.Type, global.Type.Mutable) { Name = $"global_{context.Module.Globals.Count}" }; context.Module.Globals.Add(variable); } }
public SetGlobalNode(GlobalNode variable, ExecutableNode value) { if (variable.Type != value.ResultType) { throw new WasmNodeException($"cannot assign {value.ResultType} to {variable.Type} variable"); } if (!variable.Mutable) { throw new WasmNodeException($"cannot assign {value.ResultType} to not mutable variable"); } Variable = variable; Value = value; }
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 GetGlobalNode(GlobalNode variable) { Variable = variable; }