#llvm #wasm-module #llvm-ir #compiler #wasm-binary #parser

wasmlite-parser

该crate可以解析WebAssembly模块,并基于从模块中提取的数据生成LLVM IR

1 个不稳定版本

0.0.1 2019年2月14日

#20#llvm-ir


用于 2 crate

Apache-2.0

56KB
1K SLoC

WASMLITE (WASM TO LLVM)

该项目旨在提供编译wasm二进制到LLVM IR所需的工具,这些LLVM IR可以进一步编译为特定机器的代码。

该项目旨在使编译到wasm的语言能够利用LLVM基础设施变得更加容易。

它还允许去除昂贵的wasm运行时元素,以便在不希望完全遵守wasm规范的情况下。

最后,它是一个了解WebAssembly、LLVM以及它们如何协同工作的平台。

可能的API(非最终版本)

编译管道
// Create compiler flags.
let compiler_flags = Some(CompilerOptions {
    optimization_level: 3,
    exclude_passes: vec![
        LLVMPass::InstCombine,
    ],
    runtime_ignores: vec![
        RuntimeProperty::SignatureChecks,
        RuntimeProperty::MemoryBoundsChecks,
    ],
    strategy: CompilationStrategy::Normal,
});

// Create wasm instance options.
let instance_options = Some(InstanceOptions {
    compiler_flags,
    abi: vec![ABI::LLVMMusl],
});

// JIT compile module in current process.
let (module, instance) = Runtime::instantiate(wasm_binary, imports, instance_options);

// Get the exported main function from instance.
let main = instance.get_func("main");

// Store array of items in wasm memory 0
let wasm_array = instance.set_array(&arguments);

// Call the function.
main.call(5, wasm_array);
编译类型 AOT 编译
// Create compiler flags.
let compiler_flags = Some(CompilerOptions {
    strategy: CompilationStrategy::AheadOfTime,
    ..
});

// Create wasm instance options.
let instance_options = Some(InstanceOptions { compiler_flags, .. });

// instance holds an in-memory object code of the entire wasm program.
// Possibly generates a dylib for known imports as well.
let (module, instantiate) = Runtime::instantiate(wasm_binary, imports, instance_options);

// Create executables.
let (imports_dylib, wasm_exe) = module.create_executables();
懒编译
// Create compiler flags.
let compiler_flags = Some(CompilerOptions {
    strategy: CompilationStrategy::LazyCompilation,
    ..
});

// Create wasm instance options.
let instance_options = Some(InstanceOptions { compiler_flags, .. });

// Functions are not compiled until their first call.
let (module, instance) = Runtime::instantiate(wasm_binary, imports, instance_options);
REPL类型懒编译
// Create compiler flags.
let compiler_flags = Some(CompilerOptions {
    strategy: CompilationStrategy::REPL,
    ..
});

// Create wasm instance options.
let instance_options = Some(InstanceOptions { compiler_flags, .. });

// Lazily compiles the entire wasm instance.
let (module, instance) = Runtime::instantiate(wasm_binary, imports, instance_options);

// ???
let func = module.add_function(wasm_function_binary, instance);
let expression = module.add_expression(wasm_expression_binary, instance);

非目标

  • 拥有多个后端

策略

  • 单遍解析、验证和从wasm二进制到LLVM IR的代码生成

当前支持

  • 解析器

    • 前缀
    • 类型
    • 导入
    • 内部内存、表
    • 元素、数据、全局变量
    • 函数体
    • 导出
  • 代码生成

  • 运行时

  • 编译策略

  • 编译标志

  • 一个ABI

  • 其他功能

待办事项

依赖项

~0.3–0.8MB
~17K SLoC