Restructure and compartmentalize the project

This commit is contained in:
Rerumu 2022-06-23 20:14:04 -04:00
parent 59a5a3219f
commit 223895e617
34 changed files with 105 additions and 93 deletions

View File

@ -1,8 +1,7 @@
[workspace] [workspace]
members = [ members = [
"codegen-luajit", "codegen/luajit",
"codegen-luau", "codegen/luau",
"dev-test", "dev-test",
"wasm-ast", "wasm-ast"
"wasm-synth"
] ]

View File

@ -1,12 +0,0 @@
pub static RUNTIME: &str = concat!(
"local I64 = (function() ",
include_str!("../runtime/numeric.lua"),
" end)()\n",
include_str!("../runtime/runtime.lua")
);
pub use translator::{from_inst_list, from_module_typed, from_module_untyped};
mod analyzer;
mod backend;
mod translator;

View File

@ -4,8 +4,11 @@ version = "0.8.0"
edition = "2021" edition = "2021"
[dependencies.wasm-ast] [dependencies.wasm-ast]
path = "../wasm-ast" path = "../../wasm-ast"
[dependencies.parity-wasm] [dependencies.parity-wasm]
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"]
[[bin]]
name = "wasm2luajit"

View File

@ -0,0 +1,34 @@
use std::io::{Result, Write};
use parity_wasm::{deserialize_file, elements::Module};
fn load_module(name: &str) -> Module {
deserialize_file(name)
.expect("Failed to parse WebAssembly file")
.parse_names()
.unwrap_or_else(|v| v.1)
}
fn do_runtime(lock: &mut dyn Write) -> Result<()> {
let runtime = codegen_luajit::RUNTIME;
writeln!(lock, "local rt = (function()")?;
writeln!(lock, "{runtime}")?;
writeln!(lock, "end)()")
}
fn main() -> Result<()> {
let wasm = match std::env::args().nth(1) {
Some(name) => load_module(&name),
None => {
eprintln!("usage: wasm2luajit <file>");
return Ok(());
}
};
let lock = &mut std::io::stdout().lock();
do_runtime(lock)?;
codegen_luajit::from_module_untyped(&wasm, lock)
}

View File

@ -4,8 +4,11 @@ version = "0.5.0"
edition = "2021" edition = "2021"
[dependencies.wasm-ast] [dependencies.wasm-ast]
path = "../wasm-ast" path = "../../wasm-ast"
[dependencies.parity-wasm] [dependencies.parity-wasm]
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"]
[[bin]]
name = "wasm2luau"

View File

@ -0,0 +1,38 @@
use std::io::{Result, Write};
use parity_wasm::{deserialize_file, elements::Module};
fn load_module(name: &str) -> Module {
deserialize_file(name)
.expect("Failed to parse WebAssembly file")
.parse_names()
.unwrap_or_else(|v| v.1)
}
fn do_runtime(lock: &mut dyn Write) -> Result<()> {
let runtime = codegen_luau::RUNTIME;
let numeric = codegen_luau::NUMERIC;
writeln!(lock, "local rt = (function()")?;
writeln!(lock, "local I64 = (function()")?;
writeln!(lock, "{numeric}")?;
writeln!(lock, "end)()")?;
writeln!(lock, "{runtime}")?;
writeln!(lock, "end)()")
}
fn main() -> Result<()> {
let wasm = match std::env::args().nth(1) {
Some(name) => load_module(&name),
None => {
eprintln!("usage: wasm2luau <file>");
return Ok(());
}
};
let lock = &mut std::io::stdout().lock();
do_runtime(lock)?;
codegen_luau::from_module_untyped(&wasm, lock)
}

8
codegen/luau/src/lib.rs Normal file
View File

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

View File

@ -10,8 +10,8 @@ cargo-fuzz = true
[dependencies] [dependencies]
libfuzzer-sys = "0.4" libfuzzer-sys = "0.4"
wasm-smith = "0.8.0" wasm-smith = "0.8.0"
codegen-luajit = { path = "../codegen-luajit" } codegen-luajit = { path = "../codegen/luajit" }
codegen-luau = { path = "../codegen-luau" } codegen-luau = { path = "../codegen/luau" }
[dependencies.parity-wasm] [dependencies.parity-wasm]
git = "https://github.com/paritytech/parity-wasm.git" git = "https://github.com/paritytech/parity-wasm.git"

View File

@ -134,7 +134,10 @@ impl Target for LuaJIT {
fn write_runtime(w: &mut dyn Write) -> Result<()> { fn write_runtime(w: &mut dyn Write) -> Result<()> {
let runtime = codegen_luajit::RUNTIME; let runtime = codegen_luajit::RUNTIME;
writeln!(w, "local rt = (function() {runtime} end)()")?; writeln!(w, "local rt = (function()")?;
writeln!(w, "{runtime}")?;
writeln!(w, "end)()")?;
writeln!(w, "{ASSERTION}") writeln!(w, "{ASSERTION}")
} }

View File

@ -146,8 +146,15 @@ impl Target for Luau {
fn write_runtime(w: &mut dyn Write) -> Result<()> { fn write_runtime(w: &mut dyn Write) -> Result<()> {
let runtime = codegen_luau::RUNTIME; let runtime = codegen_luau::RUNTIME;
let numeric = codegen_luau::NUMERIC;
writeln!(w, "local rt = (function()")?;
writeln!(w, "local I64 = (function()")?;
writeln!(w, "{numeric}")?;
writeln!(w, "end)()")?;
writeln!(w, "{runtime}")?;
writeln!(w, "end)()")?;
writeln!(w, "local rt = (function() {runtime} end)()")?;
writeln!(w, "{ASSERTION}") writeln!(w, "{ASSERTION}")
} }

View File

@ -1,14 +0,0 @@
[package]
name = "wasm-synth"
version = "0.5.0"
edition = "2021"
[dependencies.parity-wasm]
git = "https://github.com/paritytech/parity-wasm.git"
features = ["multi_value", "sign_ext"]
[dependencies.codegen-luajit]
path = "../codegen-luajit"
[dependencies.codegen-luau]
path = "../codegen-luau"

View File

@ -1,57 +0,0 @@
use std::io::{Result, Write};
use parity_wasm::{deserialize_file, elements::Module};
type FromUntyped = fn(&Module, &mut dyn Write) -> Result<()>;
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);
let lock = &mut std::io::stdout().lock();
write!(lock, "local rt = (function() {runtime} end)() ")?;
from_untyped(&wasm, lock)
}
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");
}
fn do_help() {
println!("usage: program to <lang> <file>");
println!(" or: program help");
}
fn main() {
let mut args = std::env::args().skip(1);
match args.next().as_deref().unwrap_or("help") {
"help" => do_help(),
"to" => {
let lang = args.next().expect("No language specified");
let file = args.next().expect("No file specified");
do_translate(&lang, &file);
}
bad => {
eprintln!("Bad action `{bad}`; try `help`");
}
}
}