Exemple #1
0
        private static void AddGlobals(WasmNodeContext context, WasmGlobalSection section)
        {
            DeclareGlobals(context, section);
            for (var i = 0; i < section.Entries.Count; i++)
            {
                var global   = section.Entries[i];
                var variable = context.Module.Globals[i];

                var arg = new WasmNodeArg {
                    Context = context
                };
                arg.PushBlock(variable.Init);
                var visitor = new WasmNode();
                foreach (var opcode in global.Init.Opcodes)
                {
                    opcode.AcceptVistor(visitor, arg);
                }
            }
        }
Exemple #2
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);
        }