Add shortcut translation API

This commit is contained in:
Rerumu 2022-05-21 21:57:21 -04:00
parent f02070a1b3
commit ca57cf915a
10 changed files with 46 additions and 34 deletions

View File

@ -1,6 +1,6 @@
pub static RUNTIME: &str = include_str!("../runtime/runtime.lua");
pub use translator::{from_inst_list, from_module};
pub use translator::{from_inst_list, from_module_typed, from_module_untyped};
mod analyzer;
mod backend;

View File

@ -329,7 +329,7 @@ pub fn from_inst_list(code: &[Instruction], type_info: &TypeInfo, w: &mut dyn Wr
/// # Errors
/// Returns `Err` if writing to `Write` failed.
pub fn from_module(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Result<()> {
pub fn from_module_typed(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Result<()> {
let func_list = build_func_list(wasm, type_info);
write_localize_used(&func_list, w)?;
@ -345,3 +345,11 @@ pub fn from_module(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Re
write_func_list(wasm, type_info, &func_list, w)?;
write_module_start(wasm, type_info, &mem_list, w)
}
/// # Errors
/// Returns `Err` if writing to `Write` failed.
pub fn from_module_untyped(wasm: &Module, w: &mut dyn Write) -> Result<()> {
let type_info = TypeInfo::from_module(wasm);
from_module_typed(wasm, &type_info, w)
}

View File

@ -5,7 +5,7 @@ pub static RUNTIME: &str = concat!(
include_str!("../runtime/runtime.lua")
);
pub use translator::{from_inst_list, from_module};
pub use translator::{from_inst_list, from_module_typed, from_module_untyped};
mod analyzer;
mod backend;

View File

@ -329,7 +329,7 @@ pub fn from_inst_list(code: &[Instruction], type_info: &TypeInfo, w: &mut dyn Wr
/// # Errors
/// Returns `Err` if writing to `Write` failed.
pub fn from_module(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Result<()> {
pub fn from_module_typed(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Result<()> {
let func_list = build_func_list(wasm, type_info);
write_localize_used(&func_list, w)?;
@ -344,3 +344,11 @@ pub fn from_module(wasm: &Module, type_info: &TypeInfo, w: &mut dyn Write) -> Re
write_func_list(wasm, type_info, &func_list, w)?;
write_module_start(wasm, type_info, &mem_list, w)
}
/// # Errors
/// Returns `Err` if writing to `Write` failed.
pub fn from_module_untyped(wasm: &Module, w: &mut dyn Write) -> Result<()> {
let type_info = TypeInfo::from_module(wasm);
from_module_typed(wasm, &type_info, w)
}

View File

@ -10,7 +10,6 @@ cargo-fuzz = true
[dependencies]
libfuzzer-sys = "0.4"
wasm-smith = "0.8.0"
wasm-ast = { path = "../wasm-ast" }
codegen-luajit = { path = "../codegen-luajit" }
codegen-luau = { path = "../codegen-luau" }
@ -21,6 +20,7 @@ features = ["multi_value", "sign_ext"]
[dev-dependencies]
test-generator = "0.3.0"
wast = "41.0.0"
wasm-ast = { path = "../wasm-ast" }
[[bin]]
name = "luajit_translate"

View File

@ -1,6 +1,5 @@
#![no_main]
use wasm_ast::builder::TypeInfo;
use wasm_smith::Module;
// We are not interested in parity_wasm errors.
@ -11,8 +10,7 @@ libfuzzer_sys::fuzz_target!(|module: Module| {
Err(_) => return,
};
let type_info = TypeInfo::from_module(&wasm);
let sink = &mut std::io::sink();
codegen_luajit::from_module(&wasm, &type_info, sink).expect("LuaJIT should succeed");
codegen_luajit::from_module_untyped(&wasm, sink).expect("LuaJIT should succeed");
});

View File

@ -1,6 +1,5 @@
#![no_main]
use wasm_ast::builder::TypeInfo;
use wasm_smith::Module;
// We are not interested in parity_wasm errors.
@ -11,8 +10,7 @@ libfuzzer_sys::fuzz_target!(|module: Module| {
Err(_) => return,
};
let type_info = TypeInfo::from_module(&wasm);
let sink = &mut std::io::sink();
codegen_luau::from_module(&wasm, &type_info, sink).expect("Luau should succeed");
codegen_luau::from_module_untyped(&wasm, sink).expect("Luau should succeed");
});

View File

@ -64,7 +64,7 @@ impl Target for LuaJIT {
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
write!(w, "storage[\"${}\"] = (function() ", data.name)?;
codegen_luajit::translate(data.module, &data.type_info, w)?;
codegen_luajit::from_module_typed(data.module, &data.type_info, w)?;
writeln!(w, "end)(nil)")
}
}
@ -89,7 +89,7 @@ impl Target for Luau {
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
write!(w, "storage[\"${}\"] = (function() ", data.name)?;
codegen_luau::translate(data.module, &data.type_info, w)?;
codegen_luau::from_module_typed(data.module, &data.type_info, w)?;
writeln!(w, "end)(nil)")
}
}

View File

@ -9,9 +9,6 @@ edition = "2021"
git = "https://github.com/paritytech/parity-wasm.git"
features = ["multi_value", "sign_ext"]
[dependencies.wasm-ast]
path = "../wasm-ast"
[dependencies.codegen-luajit]
path = "../codegen-luajit"

View File

@ -1,31 +1,34 @@
use std::io::{Result, Write};
use parity_wasm::{deserialize_file, elements::Module};
use wasm_ast::builder::TypeInfo;
type Translate = fn(&Module, &TypeInfo, &mut dyn Write) -> Result<()>;
type FromUntyped = fn(&Module, &mut dyn Write) -> Result<()>;
fn parse_module(name: &str) -> Module {
let wasm = deserialize_file(name).expect("Failed to parse Wasm file");
fn run_with(file: &str, runtime: &str, from_untyped: FromUntyped) -> Result<()> {
let wasm = deserialize_file(file)
.expect("Failed to parse Wasm file")
.parse_names()
.unwrap_or_else(|v| v.1);
wasm.parse_names().unwrap_or_else(|v| v.1)
}
fn run_translator(wasm: &Module, runtime: &str, translate: Translate) -> Result<()> {
let pipe = std::io::stdout();
let lock = &mut pipe.lock();
let type_info = TypeInfo::from_module(wasm);
let lock = &mut std::io::stdout().lock();
write!(lock, "local rt = (function() {runtime} end)() ")?;
translate(wasm, &type_info, lock)
from_untyped(&wasm, lock)
}
fn do_translate(name: &str, file: &str) {
let wasm = &parse_module(file);
let result = match name.to_lowercase().as_str() {
"luajit" => run_translator(wasm, codegen_luajit::RUNTIME, codegen_luajit::from_module),
"luau" => run_translator(wasm, codegen_luau::RUNTIME, codegen_luau::from_module),
_ => panic!("Bad language: {name}"),
fn do_translate(lang: &str, file: &str) {
let result = match lang.to_lowercase().as_str() {
"luajit" => run_with(
file,
codegen_luajit::RUNTIME,
codegen_luajit::from_module_untyped,
),
"luau" => run_with(
file,
codegen_luau::RUNTIME,
codegen_luau::from_module_untyped,
),
_ => panic!("Bad language: {lang}"),
};
result.expect("Failed to translate file");