Add shortcut translation API
This commit is contained in:
parent
f02070a1b3
commit
ca57cf915a
@ -1,6 +1,6 @@
|
|||||||
pub static RUNTIME: &str = include_str!("../runtime/runtime.lua");
|
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 analyzer;
|
||||||
mod backend;
|
mod backend;
|
||||||
|
@ -329,7 +329,7 @@ pub fn from_inst_list(code: &[Instruction], type_info: &TypeInfo, w: &mut dyn Wr
|
|||||||
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// Returns `Err` if writing to `Write` failed.
|
/// 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);
|
let func_list = build_func_list(wasm, type_info);
|
||||||
|
|
||||||
write_localize_used(&func_list, w)?;
|
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_func_list(wasm, type_info, &func_list, w)?;
|
||||||
write_module_start(wasm, type_info, &mem_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)
|
||||||
|
}
|
||||||
|
@ -5,7 +5,7 @@ pub static RUNTIME: &str = concat!(
|
|||||||
include_str!("../runtime/runtime.lua")
|
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 analyzer;
|
||||||
mod backend;
|
mod backend;
|
||||||
|
@ -329,7 +329,7 @@ pub fn from_inst_list(code: &[Instruction], type_info: &TypeInfo, w: &mut dyn Wr
|
|||||||
|
|
||||||
/// # Errors
|
/// # Errors
|
||||||
/// Returns `Err` if writing to `Write` failed.
|
/// 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);
|
let func_list = build_func_list(wasm, type_info);
|
||||||
|
|
||||||
write_localize_used(&func_list, w)?;
|
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_func_list(wasm, type_info, &func_list, w)?;
|
||||||
write_module_start(wasm, type_info, &mem_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)
|
||||||
|
}
|
||||||
|
@ -10,7 +10,6 @@ cargo-fuzz = true
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
libfuzzer-sys = "0.4"
|
libfuzzer-sys = "0.4"
|
||||||
wasm-smith = "0.8.0"
|
wasm-smith = "0.8.0"
|
||||||
wasm-ast = { path = "../wasm-ast" }
|
|
||||||
codegen-luajit = { path = "../codegen-luajit" }
|
codegen-luajit = { path = "../codegen-luajit" }
|
||||||
codegen-luau = { path = "../codegen-luau" }
|
codegen-luau = { path = "../codegen-luau" }
|
||||||
|
|
||||||
@ -21,6 +20,7 @@ features = ["multi_value", "sign_ext"]
|
|||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
test-generator = "0.3.0"
|
test-generator = "0.3.0"
|
||||||
wast = "41.0.0"
|
wast = "41.0.0"
|
||||||
|
wasm-ast = { path = "../wasm-ast" }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "luajit_translate"
|
name = "luajit_translate"
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use wasm_ast::builder::TypeInfo;
|
|
||||||
use wasm_smith::Module;
|
use wasm_smith::Module;
|
||||||
|
|
||||||
// We are not interested in parity_wasm errors.
|
// We are not interested in parity_wasm errors.
|
||||||
@ -11,8 +10,7 @@ libfuzzer_sys::fuzz_target!(|module: Module| {
|
|||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let type_info = TypeInfo::from_module(&wasm);
|
|
||||||
let sink = &mut std::io::sink();
|
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");
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use wasm_ast::builder::TypeInfo;
|
|
||||||
use wasm_smith::Module;
|
use wasm_smith::Module;
|
||||||
|
|
||||||
// We are not interested in parity_wasm errors.
|
// We are not interested in parity_wasm errors.
|
||||||
@ -11,8 +10,7 @@ libfuzzer_sys::fuzz_target!(|module: Module| {
|
|||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let type_info = TypeInfo::from_module(&wasm);
|
|
||||||
let sink = &mut std::io::sink();
|
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");
|
||||||
});
|
});
|
||||||
|
@ -64,7 +64,7 @@ impl Target for LuaJIT {
|
|||||||
|
|
||||||
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
|
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
|
||||||
write!(w, "storage[\"${}\"] = (function() ", data.name)?;
|
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)")
|
writeln!(w, "end)(nil)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ impl Target for Luau {
|
|||||||
|
|
||||||
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
|
fn write_module(data: TypedModule, w: &mut dyn Write) -> IResult<()> {
|
||||||
write!(w, "storage[\"${}\"] = (function() ", data.name)?;
|
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)")
|
writeln!(w, "end)(nil)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,6 @@ edition = "2021"
|
|||||||
git = "https://github.com/paritytech/parity-wasm.git"
|
git = "https://github.com/paritytech/parity-wasm.git"
|
||||||
features = ["multi_value", "sign_ext"]
|
features = ["multi_value", "sign_ext"]
|
||||||
|
|
||||||
[dependencies.wasm-ast]
|
|
||||||
path = "../wasm-ast"
|
|
||||||
|
|
||||||
[dependencies.codegen-luajit]
|
[dependencies.codegen-luajit]
|
||||||
path = "../codegen-luajit"
|
path = "../codegen-luajit"
|
||||||
|
|
||||||
|
@ -1,31 +1,34 @@
|
|||||||
use std::io::{Result, Write};
|
use std::io::{Result, Write};
|
||||||
|
|
||||||
use parity_wasm::{deserialize_file, elements::Module};
|
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 {
|
fn run_with(file: &str, runtime: &str, from_untyped: FromUntyped) -> Result<()> {
|
||||||
let wasm = deserialize_file(name).expect("Failed to parse Wasm file");
|
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)
|
let lock = &mut std::io::stdout().lock();
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
write!(lock, "local rt = (function() {runtime} end)() ")?;
|
write!(lock, "local rt = (function() {runtime} end)() ")?;
|
||||||
translate(wasm, &type_info, lock)
|
from_untyped(&wasm, lock)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_translate(name: &str, file: &str) {
|
fn do_translate(lang: &str, file: &str) {
|
||||||
let wasm = &parse_module(file);
|
let result = match lang.to_lowercase().as_str() {
|
||||||
let result = match name.to_lowercase().as_str() {
|
"luajit" => run_with(
|
||||||
"luajit" => run_translator(wasm, codegen_luajit::RUNTIME, codegen_luajit::from_module),
|
file,
|
||||||
"luau" => run_translator(wasm, codegen_luau::RUNTIME, codegen_luau::from_module),
|
codegen_luajit::RUNTIME,
|
||||||
_ => panic!("Bad language: {name}"),
|
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");
|
result.expect("Failed to translate file");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user