new stuff

This commit is contained in:
Graham Kelly 2024-07-18 13:17:58 -04:00
parent 7e642e1f85
commit c36c85fa4f
14 changed files with 229 additions and 63 deletions

69
Cargo.lock generated
View file

@ -245,7 +245,7 @@ dependencies = [
"proc-macro2",
"quote",
"rustc_version",
"syn 2.0.66",
"syn 2.0.71",
]
[[package]]
@ -293,6 +293,26 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "externref"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b078507c9badc1c2304de402f373950b30ef80b793096cd2a8c0312c8c676cad"
dependencies = [
"externref-macro",
]
[[package]]
name = "externref-macro"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77107325d07454a4de518e92b1e061ded5af8aa32d52edb1fde60c813d1eff1b"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.71",
]
[[package]]
name = "fallible-iterator"
version = "0.3.0"
@ -483,6 +503,13 @@ dependencies = [
"ruzstd",
]
[[package]]
name = "pit-basic"
version = "0.1.0"
dependencies = [
"externref",
]
[[package]]
name = "pit-core"
version = "0.1.0"
@ -498,12 +525,28 @@ name = "pit-patch"
version = "0.1.0"
dependencies = [
"anyhow",
"hex",
"nom",
"pit-core",
"portal-pc-waffle",
"sha3",
"waffle-ast",
]
[[package]]
name = "pit-rust-generator"
version = "0.1.0"
dependencies = [
"anyhow",
"nom",
"pit-core",
"pit-rust-guest",
"prettyplease",
"proc-macro2",
"quote",
"syn 2.0.71",
]
[[package]]
name = "pit-rust-guest"
version = "0.1.0"
@ -514,7 +557,7 @@ dependencies = [
"quasiquote",
"quote",
"sha3",
"syn 2.0.66",
"syn 2.0.71",
]
[[package]]
@ -526,7 +569,7 @@ dependencies = [
"proc-macro2",
"quasiquote",
"quote",
"syn 2.0.66",
"syn 2.0.71",
]
[[package]]
@ -559,6 +602,16 @@ dependencies = [
"wasmparser",
]
[[package]]
name = "prettyplease"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e"
dependencies = [
"proc-macro2",
"syn 2.0.71",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -585,9 +638,9 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.85"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
@ -669,7 +722,7 @@ checksum = "bcc303e793d3734489387d205e9b186fac9c6cfacedd98cbb2e8a5943595f3e6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.66",
"syn 2.0.71",
]
[[package]]
@ -817,9 +870,9 @@ dependencies = [
[[package]]
name = "syn"
version = "2.0.66"
version = "2.0.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462"
dependencies = [
"proc-macro2",
"quote",

View file

@ -1,7 +1,7 @@
[workspace]
members=["pit-core", "pit-patch", "pit-rust-guest", "pit-rust-host", "pit-rust-host-lib"]
members=[ "pit-basic","pit-core", "pit-patch", "pit-rust-generator", "pit-rust-guest", "pit-rust-host", "pit-rust-host-lib"]
resolver="2"
[workspace.dependencies]
portal-pc-waffle = {git="https://github.com/portal-co/waffle-.git",branch="pr/changes2"}
waffle-ast = {git="https://github.com/portal-co/more_waffle.git",branch="master"}
waffle-ast = {git="https://github.com/portal-co/more_waffle.git",branch="master"}

5
buffer.pit Normal file
View file

@ -0,0 +1,5 @@
{
read8(I32) -> (I32);
write8(I32,I32) -> ();
size() -> (I32)
}

7
pit-basic/Cargo.toml Normal file
View file

@ -0,0 +1,7 @@
[package]
name = "pit-basic"
version = "0.1.0"
edition = "2021"
[dependencies]
externref = "0.2.0"

3
pit-basic/build.sh Normal file
View file

@ -0,0 +1,3 @@
set -e
cd $(dirname $0)
cargo run -p pit-rust-generator ../buffer.pit src/buffer.rs

1
pit-basic/src/buffer.rs Normal file
View file

@ -0,0 +1 @@
pub trait R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 { fn read8 (& self , p0 : u32) -> (u32) ; fn size (& self) -> (u32) ; fn write8 (& self , p0 : u32 , p1 : u32) -> () ; } const _ : () = { # [link_section = ".pit-types"] static SECTION_CONTENT : [u8 ; 60usize] = [123u8 , 114u8 , 101u8 , 97u8 , 100u8 , 56u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 59u8 , 115u8 , 105u8 , 122u8 , 101u8 , 40u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 73u8 , 51u8 , 50u8 , 41u8 , 59u8 , 119u8 , 114u8 , 105u8 , 116u8 , 101u8 , 56u8 , 40u8 , 73u8 , 51u8 , 50u8 , 44u8 , 73u8 , 51u8 , 50u8 , 41u8 , 32u8 , 45u8 , 62u8 , 32u8 , 40u8 , 41u8 , 125u8 , 0u8] ; impl R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> { fn read8 (& self , p0 : u32) -> (u32) { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "read8")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> , p0 : u32) -> (u32) ; } return unsafe { go (self ,p0) } ; } fn size (& self) -> (u32) { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "size")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >>) -> (u32) ; } return unsafe { go (self ,) } ; } fn write8 (& self , p0 : u32 , p1 : u32) -> () { # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "write8")] fn go (this : & :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> , p0 : u32 , p1 : u32) -> () ; } return unsafe { go (self ,p0 , p1) } ; } } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e.drop"] extern "C" fn _drop (a : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) { unsafe { Box :: from_raw (a) } ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/read8"] extern "C" fn read8 (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 > , p0 : u32) -> (u32) { return unsafe { & mut * id } .read8 (p0) ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/size"] extern "C" fn size (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> (u32) { return unsafe { & mut * id } .size () ; } # [:: externref :: externref (crate = ":: externref")] # [export_name = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5/~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e/write8"] extern "C" fn write8 (id : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 > , p0 : u32 , p1 : u32) -> () { return unsafe { & mut * id } .write8 (p0 , p1) ; } } ; mod R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5_utils { use super :: R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 ; # [:: externref :: externref (crate = ":: externref")] # [link (wasm_import_module = "pit/867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5")] extern "C" { # [link (wasm_import_name = "~8afbd04c549e07db517e034114e4b8ff8c76ce748f2e2d6e29fcaf48051eaf3e")] fn _push (a : * mut Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >>; } pub fn push (a : Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >) -> :: externref :: Resource < Box < dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 >> { return unsafe { _push (Box :: into_raw (Box :: new (a))) } } }

26
pit-basic/src/lib.rs Normal file
View file

@ -0,0 +1,26 @@
use std::{
ops::{Deref, DerefMut},
sync::Mutex,
};
include!("buffer.rs");
impl<T: Deref<Target = [u8]> + DerefMut>
R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5 for Mutex<T>
{
fn read8(&self, p0: u32) -> (u32) {
return self.lock().unwrap()[p0 as usize].into();
}
fn write8(&self, p0: u32, p1: u32) -> () {
self.lock().unwrap()[p0 as usize] = (p1 & 0xff) as u8;
}
fn size(&self) -> (u32) {
return self.lock().unwrap().len().try_into().unwrap();
}
}
pub fn slice(
x: &dyn R867207405fe87fda620c2d7a5485e8e5e274636a898a166fb674448b4391ffc5,
) -> impl Iterator<Item = u8> + '_ {
return (0..x.size()).map(|a| (x.read8(a) & 0xff) as u8);
}

View file

@ -1,11 +1,6 @@
use derive_more::Display;
use nom::{
bytes::complete::{is_not, tag, take, take_while_m_n},
character::complete::{alpha1, char, multispace0},
combinator::opt,
multi::{many0, separated_list0},
sequence::{delimited, tuple},
IResult,
bytes::complete::{is_not, tag, take, take_while_m_n}, character::complete::{alpha1, alphanumeric1, char, multispace0}, combinator::opt, error::Error, multi::{many0, separated_list0}, sequence::{delimited, tuple}, IResult
};
use sha3::{Digest, Sha3_256};
use std::{collections::BTreeMap, fmt::Display};
@ -52,10 +47,10 @@ pub enum Arg {
// Func(Sig),
}
pub fn parse_arg(a: &str) -> IResult<&str, Arg> {
let (_, a) = multispace0(a)?;
let (b, c) = take(1usize)(a)?;
match c {
"R" => {
let (a,_) = multispace0(a)?;
// let (c,b) = take(1usize)(a)?;
match a.strip_prefix("R") {
Some(b) => {
// if let Some(a) = b.strip_prefix("this"){
// let (a, k) = opt(tag("n"))(a)?;
// return Ok((
@ -82,14 +77,14 @@ pub fn parse_arg(a: &str) -> IResult<&str, Arg> {
// let (a, x) = parse_sig(a)?;
// return Ok((a, Arg::Func(x)));
// }
_ => {
let (c, a) = take(3usize)(a)?;
None => {
let (a,c) = take(3usize)(a)?;
match c {
"I32" => return Ok((a, Arg::I32)),
"I64" => return Ok((a, Arg::I64)),
"F32" => return Ok((a, Arg::F32)),
"F64" => return Ok((a, Arg::F64)),
_ => {}
_ => return Err(nom::Err::Error(Error::new(a, nom::error::ErrorKind::Tag)))
}
}
}
@ -121,19 +116,20 @@ pub struct Interface {
}
impl Display for Interface {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{{")?;
write!(f, "{}","{")?;
for (i, (a, b)) in self.methods.iter().enumerate() {
if i != 0 {
write!(f, ";")?;
}
write!(f, "{}{}", a, b)?;
}
return write!(f, "}}");
return write!(f, "{}","}");
}
}
pub fn parse_interface(a: &str) -> IResult<&str, Interface> {
pub fn go(a: &str) -> IResult<&str, Interface> {
let (a, s) = separated_list0(char(';'), tuple((multispace0, alpha1, parse_sig)))(a)?;
let (a, s) = separated_list0(char(';'), tuple((multispace0, alphanumeric1, parse_sig)))(a)?;
let (a,_) = multispace0(a)?;
return Ok((
a,
Interface {

View file

@ -7,7 +7,9 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
hex = "0.4.3"
nom = "7.1.3"
pit-core = { version = "0.1.0", path = "../pit-core" }
portal-pc-waffle.workspace = true
sha3 = "0.10.8"
waffle-ast.workspace = true

View file

@ -1,6 +1,7 @@
use std::{collections::BTreeMap, iter::once, mem::take};
use anyhow::Context;
use sha3::{Digest, Sha3_256};
use waffle::{
entity::EntityRef, util::new_sig, BlockTarget, Export, ExportKind, Func, FuncDecl,
FunctionBody, Import, ImportKind, Module, Operator, SignatureData, Type,
@ -162,3 +163,36 @@ pub fn canon(m: &mut Module, rid: &str, target: &str) -> anyhow::Result<()> {
}
Ok(())
}
pub fn jigger(m: &mut Module) -> anyhow::Result<()>{
let mut s = Sha3_256::default();
s.update(&m.to_wasm_bytes()?);
let s = s.finalize();
for i in m.imports.iter_mut(){
if !i.module.starts_with("pit/"){
continue;
}
if let Some(a) = i.name.strip_prefix("~"){
let a = format!("{a}-{s:?}");
let mut s = Sha3_256::default();
s.update(a.as_bytes());
let s = s.finalize();
let s = hex::encode(s);
i.name = format!("~{s}");
}
}
for x in m.exports.iter_mut(){
if let Some(a) = x.name.strip_prefix("pit/"){
if let Some((b,a)) = a.split_once("/~"){
if let Some((a,c)) = a.split_once("/"){
let a = format!("{a}-{s:?}");
let mut s = Sha3_256::default();
s.update(a.as_bytes());
let s = s.finalize();
let s = hex::encode(s);
x.name = format!("pit/{b}/~{s}/{c}");
}
}
}
}
Ok(())
}

View file

@ -0,0 +1,14 @@
[package]
name = "pit-rust-generator"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.86"
nom = "7.1.3"
pit-core = { version = "0.1.0", path = "../pit-core" }
pit-rust-guest = { version = "0.1.0", path = "../pit-rust-guest" }
prettyplease = "0.2.20"
proc-macro2 = "1.0.86"
quote = "1.0.36"
syn = "2.0.71"

View file

@ -0,0 +1,25 @@
use anyhow::{anyhow, Context};
use pit_rust_guest::Opts;
use quote::quote;
fn main() -> anyhow::Result<()>{
let mut a = std::env::args();
a.next();
let src = a.next().context("in getting the src")?;
let src = std::fs::read_to_string(src)?;
let (_,src) = pit_core::parse_interface(&src).map_err(|e|anyhow!("invalid interface: {e}"))?;
let mut x = Opts{
root: quote!{
},
salt: std::env::var("PIT_SALT").unwrap_or(format!("")).into_bytes(),
};
let v = pit_rust_guest::render(&x, &src);
// let v = prettyplease::unparse(&syn::parse_file(&v.to_string())?);
let Some(dst) = a.next() else{
println!("{v}");
return Ok(());
};
std::fs::write(dst, v.to_string())?;
Ok(())
}

View file

@ -18,30 +18,30 @@ pub fn render(opts: &Opts, i: &Interface) -> TokenStream{
ha.write(&opts.salt);
let ha = hex::encode(ha.finalize());
let id2 = format_ident!("R{}",i.rid_str());
let internal = format_ident!("{id}_utils");
let internal = format_ident!("R{id}_utils");
let methods = i.methods.iter().map(|(a,b)|quasiquote! {
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&self})}
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&self})};
});
let impl_dyns = i.methods.iter().map(|(a,b)|quasiquote! {
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {&self})}{
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[wasm_import_module = #{format!("pit/{}",i.rid_str())}]
#[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})]
extern "C"{
#[wasm_import_name = #a]
fn go #{render_sig(root, i,b, &quote! {&#root::externref::Resource<Box<dyn #id2>>})}
};
return go(self,#{
#[link(wasm_import_name = #a)]
fn go #{render_sig(root, i,b, &quote! {this: &#root::externref::Resource<Box<dyn #id2>>})};
}
return unsafe{go(self,#{
let params = b.params.iter().enumerate().map(|(a,_)|format_ident!("p{a}"));
quote! {
#(#params),*
}
});
})};
}
});
let chains2 = i.methods.iter().map(|(a,b)|quasiquote! {
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[export_name = #{format!("pit/{id}/~{ha}/{a}")}]
fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {id: *mut Box<dyn #id>})}{
extern "C" fn #{format_ident!("{a}")}#{render_sig(root,i,b,&quote! {id: *mut Box<dyn #id2>})}{
return unsafe{&mut *id}.#{format_ident!("{a}")}(#{
let params = b.params.iter().enumerate().map(|(a,_)|format_ident!("p{a}"));
quote! {
@ -50,45 +50,45 @@ pub fn render(opts: &Opts, i: &Interface) -> TokenStream{
});
}
});
let sb = i.to_string();
let sc = sb.as_bytes().iter().cloned().chain(once(0u8)).collect::<Vec<_>>();
quasiquote!{
pub trait #id2{
#(#methods)*
}
mod #internal{
const _: () = {
#[link_section = ".pit-types"]
static SECTION_CONTENT: [u8; _] = #{
let b = i.to_string();
let c = b.as_bytes().iter().cloned().chain(once(0u8));
static SECTION_CONTENT: [u8; #{sc.len()}] = #{
quote!{
[#(#c),*]
[#(#sc),*]
}
};
impl #id2 for #root::externref::Resource<Box<dyn #id2>>{
#(#impl_dyns)*
}
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[export_name = #{format!("pit/{id}/~{ha}.drop")}]
extern "C" fn _drop(a: *mut Box<dyn #id2>){
unsafe{
Box::from_raw(a)
};
}
#(#chains2)*
};
use super::#id2;
impl #id2 for #root::externref::Resource<Box<dyn #id2>>{
#(#impl_dyns),*
}
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[export_name = #{format!("pit/{id}/~{ha}.drop")}]
fn _drop(a: *mut Box<dyn #id>){
unsafe{
Box::from_raw(a)
mod #internal{
use super::#id2;
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})]
extern "C"{
#[link(wasm_import_name = #{format!("~{ha}")})]
fn _push(a: *mut Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>;
}
pub fn push(a: Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>{
return unsafe{
_push(Box::into_raw(Box::new(a)))
}
}
}
#(#chains2);*
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
#[wasm_import_module = #{format!("pit/{}",i.rid_str())}]
extern "C"{
#[wasm_import_name = #{format!("~{ha}")}]
fn _push(a: *mut Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>;
}
pub fn push(a: Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>{
return unsafe{
_push(Box::into_raw(Box::new(a)))
}
}
}
}
}
pub fn render_sig(root: &TokenStream,base: &Interface, s: &Sig, self_: &TokenStream) -> TokenStream{

View file

@ -25,7 +25,7 @@ pub fn render(root: &TokenStream, i: &Interface) -> TokenStream {
fn #{format_ident!("{a}")}#{render_sig(root,b,&quote! {&self})}{
let Some(#root::wasm_runtime_layer::Export::Func(f)) = self.instance.get_export(unsafe{
&*::std::cell::UnsafeCell::raw_get(self.ctx)
},#{format!("pit/{}/{}",i.rid_str(),a)}) else{
},#{format_ident!("pit/{}/{}",i.rid_str(),a)}) else{
panic!("invalid func")
};
let args = vec![#(#init),*];