2025-01-29 11:33:01 +01:00
|
|
|
|
use {
|
|
|
|
|
ariadne::{Color, Label, Report, ReportKind, Source},
|
|
|
|
|
chumsky::{
|
|
|
|
|
prelude::*, text::*
|
2025-01-31 17:56:08 +01:00
|
|
|
|
},
|
|
|
|
|
laddertypes::{
|
2025-02-14 14:07:18 +01:00
|
|
|
|
dict::TypeDict, parser::ParseLadderType, subtype_unify, unparser::UnparseLadderType, BimapTypeDict, Morphism, MorphismType
|
2025-01-31 17:56:08 +01:00
|
|
|
|
},
|
|
|
|
|
std::sync::{Arc, RwLock}
|
2025-01-29 11:33:01 +01:00
|
|
|
|
};
|
|
|
|
|
|
2025-02-03 17:55:18 +01:00
|
|
|
|
/* for a given ladder type `t`, get the corresponding C type
|
|
|
|
|
*/
|
|
|
|
|
pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_pointer: bool) -> Option<String> {
|
|
|
|
|
let lnf = t.normalize().decurry().get_lnf_vec();
|
|
|
|
|
|
|
|
|
|
match lnf.last() {
|
|
|
|
|
Some(t) => {
|
|
|
|
|
if t == &dict.parse("Byte").expect("parse")
|
|
|
|
|
|| t == &dict.parse("x86.UInt8").expect("parse")
|
|
|
|
|
|| t == &dict.parse("<StaticLength 8 Bit>").expect("parse")
|
|
|
|
|
{
|
|
|
|
|
Some("uint8_t".into())
|
|
|
|
|
} else if t == &dict.parse("x86.UInt16").expect("parse") {
|
|
|
|
|
Some("uint16_t".into())
|
|
|
|
|
} else if t == &dict.parse("x86.UInt32").expect("parse") {
|
|
|
|
|
Some("uint32_t".into())
|
|
|
|
|
} else if t == &dict.parse("x86.UInt64").expect("parse") {
|
|
|
|
|
Some("uint64_t".into())
|
|
|
|
|
} else {
|
|
|
|
|
match t {
|
|
|
|
|
laddertypes::TypeTerm::App(args) => {
|
|
|
|
|
if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap())
|
|
|
|
|
{
|
2025-02-05 11:26:55 +01:00
|
|
|
|
let _length_type = args[1].clone();
|
2025-02-03 17:55:18 +01:00
|
|
|
|
let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?;
|
|
|
|
|
match item_c_type.as_str() {
|
2025-02-03 19:21:53 +01:00
|
|
|
|
"uint8_t" => Some(format!("struct LengthPrefixUInt8Array")),
|
|
|
|
|
"uint16_t" => Some(format!("struct LengthPrefixUInt16Array")),
|
|
|
|
|
"uint32_t" => Some(format!("struct LengthPrefixUInt32Array")),
|
|
|
|
|
"uint64_t" => Some(format!("struct LengthPrefixUInt64Array")),
|
2025-02-03 17:55:18 +01:00
|
|
|
|
_ => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueDelim".into()).unwrap())
|
|
|
|
|
{
|
2025-02-05 11:26:55 +01:00
|
|
|
|
let _delim_val = args[1].clone();
|
2025-02-03 17:55:18 +01:00
|
|
|
|
let c_type = get_c_repr_type(dict, args[2].clone(), false)?;
|
|
|
|
|
if skip_pointer {
|
|
|
|
|
Some(c_type)
|
|
|
|
|
} else {
|
|
|
|
|
Some(format!("{} *", c_type))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 19:21:53 +01:00
|
|
|
|
#[derive(Clone, Debug)]
|
2025-02-05 11:26:55 +01:00
|
|
|
|
struct LdmcPrimCMorphism {
|
2025-01-29 11:33:01 +01:00
|
|
|
|
symbol: String,
|
|
|
|
|
type_args: Vec<(String, String)>,
|
2025-01-31 17:56:08 +01:00
|
|
|
|
src_type: laddertypes::TypeTerm,
|
|
|
|
|
dst_type: laddertypes::TypeTerm,
|
2025-01-29 11:33:01 +01:00
|
|
|
|
locations: Vec<String>
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:26:55 +01:00
|
|
|
|
impl LdmcPrimCMorphism {
|
2025-02-03 17:55:18 +01:00
|
|
|
|
pub fn expected_c_type_signature(&self, dict: &mut impl TypeDict) -> String {
|
|
|
|
|
format!("int {} ({} const * restrict src, {} * restrict dst);",
|
|
|
|
|
self.symbol,
|
|
|
|
|
get_c_repr_type(dict, self.src_type.clone(), true).expect("cant get c-repr type for src type"),
|
|
|
|
|
get_c_repr_type(dict, self.dst_type.clone(), true).expect("cant get c-repr type for dst type"))
|
|
|
|
|
}
|
2025-02-05 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
|
enum LdmcMorphism {
|
|
|
|
|
Primitive( LdmcPrimCMorphism ),
|
|
|
|
|
LengthPrefixMap{
|
|
|
|
|
length_prefix_type: laddertypes::TypeTerm,
|
|
|
|
|
item_morph: Box<LdmcPrimCMorphism>
|
|
|
|
|
},
|
|
|
|
|
ValueDelimMap{
|
|
|
|
|
delim: u64,
|
2025-02-14 14:07:18 +01:00
|
|
|
|
item_morph: Box<LdmcPrimCMorphism>
|
2025-02-03 17:55:18 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:26:55 +01:00
|
|
|
|
impl LdmcMorphism {
|
|
|
|
|
pub fn generate_call(&self, dict: &mut impl TypeDict, i: u64) {
|
|
|
|
|
match self {
|
|
|
|
|
LdmcMorphism::Primitive(prim_morph) => {
|
|
|
|
|
let src_c_type = get_c_repr_type(dict, prim_morph.src_type.clone(), true).expect("cant get c-repr type for src type");
|
|
|
|
|
let dst_c_type = get_c_repr_type(dict, prim_morph.dst_type.clone(), true).expect("cant get c-repr type for dst type");
|
|
|
|
|
|
|
|
|
|
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
|
|
|
|
|
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
|
|
|
|
|
println!(r#"
|
|
|
|
|
{}
|
|
|
|
|
{} const * restrict src = (void*) {};
|
|
|
|
|
{} * restrict dst = (void*) {};
|
|
|
|
|
{} ( src, dst );
|
|
|
|
|
{}"#,
|
|
|
|
|
'{',
|
|
|
|
|
src_c_type, src_buf,
|
|
|
|
|
dst_c_type, dst_buf,
|
|
|
|
|
prim_morph.symbol,
|
|
|
|
|
'}');
|
|
|
|
|
}
|
|
|
|
|
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
|
2025-02-14 14:07:18 +01:00
|
|
|
|
let src_c_type = get_c_repr_type(dict, self.get_type().src_type, true).expect("cant get c-repr type for src type");
|
|
|
|
|
let dst_c_type = get_c_repr_type(dict, self.get_type().dst_type, true).expect("cant get c-repr type for dst type");
|
|
|
|
|
|
|
|
|
|
let map_fn = match (src_c_type.as_str(), dst_c_type.as_str()) {
|
|
|
|
|
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt64Array") => {
|
|
|
|
|
"length_prefix_array_map_64_to_64"
|
|
|
|
|
},
|
|
|
|
|
("struct LengthPrefixUInt8Array", "struct LengthPrefixUInt64Array") => {
|
|
|
|
|
"length_prefix_array_map_8_to_64"
|
|
|
|
|
},
|
|
|
|
|
("struct LengthPrefixUInt64Array", "struct LengthPrefixUInt8Array") => {
|
|
|
|
|
"length_prefix_array_map_64_to_8"
|
|
|
|
|
},
|
|
|
|
|
_ => {
|
|
|
|
|
"{{ ERROR: no map function implemented }}"
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-02-05 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
|
|
|
|
|
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
|
|
|
|
|
println!(r#"
|
|
|
|
|
{}
|
|
|
|
|
{} const * restrict src = (void*) {};
|
|
|
|
|
{} * restrict dst = (void*) {};
|
2025-02-14 14:07:18 +01:00
|
|
|
|
{} ( {}, src, dst );
|
2025-02-05 11:26:55 +01:00
|
|
|
|
{}"#,
|
|
|
|
|
'{',
|
|
|
|
|
src_c_type, src_buf,
|
|
|
|
|
dst_c_type, dst_buf,
|
2025-02-14 14:07:18 +01:00
|
|
|
|
map_fn,
|
2025-02-05 11:26:55 +01:00
|
|
|
|
item_morph.symbol,
|
|
|
|
|
'}');
|
|
|
|
|
}
|
|
|
|
|
LdmcMorphism::ValueDelimMap { delim, item_morph } => {
|
2025-02-14 14:07:18 +01:00
|
|
|
|
let src_c_type = get_c_repr_type(dict, item_morph.src_type.clone(), false).expect("cant get c-repr type for src type");
|
|
|
|
|
let dst_c_type = get_c_repr_type(dict, item_morph.dst_type.clone(), false).expect("cant get c-repr type for dst type");
|
2025-02-05 11:26:55 +01:00
|
|
|
|
|
2025-02-14 14:07:18 +01:00
|
|
|
|
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
|
|
|
|
|
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };
|
|
|
|
|
println!(r#"
|
|
|
|
|
{}
|
|
|
|
|
{} const * restrict src = (void*) {};
|
|
|
|
|
{} * restrict dst = (void*) {};
|
|
|
|
|
value_delim_array_map_8_to_64( {}, src, dst );
|
|
|
|
|
{}"#,
|
|
|
|
|
'{',
|
|
|
|
|
src_c_type, src_buf,
|
|
|
|
|
dst_c_type, dst_buf,
|
|
|
|
|
item_morph.symbol,
|
|
|
|
|
'}');
|
2025-02-05 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Morphism for LdmcMorphism {
|
2025-02-03 19:21:53 +01:00
|
|
|
|
fn weight(&self) -> u64 {
|
|
|
|
|
1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_type(&self) -> laddertypes::MorphismType {
|
2025-02-05 11:26:55 +01:00
|
|
|
|
match self {
|
|
|
|
|
LdmcMorphism::Primitive(prim_morph) =>
|
|
|
|
|
laddertypes::MorphismType {
|
|
|
|
|
src_type: prim_morph.src_type.clone().normalize(),
|
|
|
|
|
dst_type: prim_morph.dst_type.clone().normalize()
|
|
|
|
|
},
|
|
|
|
|
LdmcMorphism::LengthPrefixMap{ length_prefix_type, item_morph } => {
|
|
|
|
|
laddertypes::MorphismType {
|
|
|
|
|
src_type: laddertypes::TypeTerm::App(vec![ length_prefix_type.clone(), item_morph.src_type.clone() ]),
|
|
|
|
|
dst_type: laddertypes::TypeTerm::App(vec![ length_prefix_type.clone(), item_morph.dst_type.clone() ]),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
LdmcMorphism::ValueDelimMap{ delim, item_morph } => {
|
|
|
|
|
let value_delim_type = laddertypes::TypeTerm::App(vec![]);
|
|
|
|
|
laddertypes::MorphismType {
|
2025-02-14 14:07:18 +01:00
|
|
|
|
src_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph.src_type.clone() ]),
|
|
|
|
|
dst_type: laddertypes::TypeTerm::App(vec![ value_delim_type.clone(), item_morph.dst_type.clone() ]),
|
2025-02-05 11:26:55 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-14 14:07:18 +01:00
|
|
|
|
}.normalize()
|
2025-02-03 19:21:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-05 11:26:55 +01:00
|
|
|
|
fn map_morphism(&self, seq_type: laddertypes::TypeTerm) -> Option< Self > {
|
|
|
|
|
match self {
|
|
|
|
|
LdmcMorphism::Primitive(prim) => {
|
|
|
|
|
let item_morph = Box::new(prim.clone());
|
2025-02-14 14:07:18 +01:00
|
|
|
|
/*
|
|
|
|
|
if seq_type == self.length_prefix_type {
|
|
|
|
|
*/
|
2025-02-05 11:26:55 +01:00
|
|
|
|
Some(LdmcMorphism::LengthPrefixMap{
|
|
|
|
|
length_prefix_type: seq_type,
|
|
|
|
|
item_morph,
|
|
|
|
|
})
|
|
|
|
|
/*
|
|
|
|
|
} else if seq_type == self.value_delim_type {
|
|
|
|
|
Some(LdmcMorphism::ValueDelimMap { delim, item_morph })
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
_ => None
|
|
|
|
|
}
|
2025-02-03 19:21:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
|
/* morphism-base text format:
|
|
|
|
|
* NAME '(' [TYPE-ARG-NAME ':' KIND] ')'
|
|
|
|
|
* SRC-TYPE
|
|
|
|
|
* '-->' DST-TYPE
|
|
|
|
|
* '@' [ LOCATION ':' ]
|
|
|
|
|
*/
|
2025-01-31 17:56:08 +01:00
|
|
|
|
fn parser(
|
|
|
|
|
type_dict: Arc<RwLock< BimapTypeDict >>
|
2025-02-05 11:26:55 +01:00
|
|
|
|
) -> impl Parser<char, Vec<LdmcPrimCMorphism>, Error = Simple<char>> {
|
2025-01-31 17:56:08 +01:00
|
|
|
|
|
2025-01-29 11:33:01 +01:00
|
|
|
|
ident().padded()
|
|
|
|
|
.then(
|
|
|
|
|
ident().padded()
|
|
|
|
|
.then_ignore(just(':').padded())
|
|
|
|
|
.then(none_of(",)").repeated().padded())
|
|
|
|
|
.separated_by(just(',').padded())
|
|
|
|
|
.delimited_by(just('('), just(')'))
|
|
|
|
|
)
|
|
|
|
|
.then_ignore(just('\n'))
|
|
|
|
|
.then(
|
|
|
|
|
take_until(just("-->").ignored())
|
|
|
|
|
.then(take_until(just('@').ignored()))
|
|
|
|
|
)
|
|
|
|
|
.then(
|
|
|
|
|
none_of(":\n").repeated().separated_by(just(':'))
|
|
|
|
|
)
|
2025-01-31 17:56:08 +01:00
|
|
|
|
.map(
|
|
|
|
|
move |(((symbol, type_args), ((src_type, _), (dst_type, _))), locations)| {
|
|
|
|
|
let mut type_dict = type_dict.write().unwrap();
|
|
|
|
|
let type_args : Vec<_> = type_args.into_iter().map(|(v,k)| (v,k.into_iter().collect())).collect();
|
|
|
|
|
for (var, kind) in type_args.iter() {
|
|
|
|
|
type_dict.add_varname(var.clone());
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 19:21:53 +01:00
|
|
|
|
let mut src_type = type_dict.parse(&src_type.iter().collect::<String>()).expect("couldnt parse src type");
|
|
|
|
|
let mut dst_type = type_dict.parse(&dst_type.iter().collect::<String>()).expect("couldnt parse dst type");
|
2025-01-31 17:56:08 +01:00
|
|
|
|
|
2025-02-05 11:26:55 +01:00
|
|
|
|
LdmcPrimCMorphism {
|
2025-01-31 17:56:08 +01:00
|
|
|
|
symbol,
|
|
|
|
|
type_args,
|
|
|
|
|
src_type,
|
|
|
|
|
dst_type,
|
|
|
|
|
locations: locations.into_iter().map(|l| l.into_iter().collect()).collect()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.separated_by(text::newline())
|
2025-01-29 11:33:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2025-02-03 19:21:53 +01:00
|
|
|
|
let mut type_dict = Arc::new(RwLock::new(BimapTypeDict::new()));
|
2025-02-05 11:26:55 +01:00
|
|
|
|
let mut morphism_base = laddertypes::MorphismBase::<LdmcMorphism>::new(vec![
|
2025-02-14 14:07:18 +01:00
|
|
|
|
type_dict.parse("Seq~<ValueDelim '\\0'>").expect(""),
|
|
|
|
|
type_dict.parse("Seq~<LengthPrefix x86.UInt64>").expect("")
|
2025-02-05 11:26:55 +01:00
|
|
|
|
]);
|
|
|
|
|
|
2025-02-14 14:07:18 +01:00
|
|
|
|
let mut args = std::env::args().skip(1);
|
|
|
|
|
let src_type_arg = args.next().expect("src type expected");
|
|
|
|
|
let dst_type_arg = args.next().expect("dst type expected");
|
2025-02-03 18:41:14 +01:00
|
|
|
|
|
2025-02-14 14:07:18 +01:00
|
|
|
|
for mb_path in args {
|
|
|
|
|
let src = std::fs::read_to_string(mb_path).expect("read");
|
2025-02-03 18:41:14 +01:00
|
|
|
|
let result = parser(type_dict.clone()).parse(src.clone());
|
|
|
|
|
match result {
|
|
|
|
|
Ok(morphisms) => {
|
2025-02-03 19:21:53 +01:00
|
|
|
|
eprintln!("parse ok.");
|
2025-02-03 18:41:14 +01:00
|
|
|
|
for m in morphisms {
|
2025-02-05 11:26:55 +01:00
|
|
|
|
morphism_base.add_morphism(LdmcMorphism::Primitive(m));
|
2025-02-03 18:41:14 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(errs) => {
|
|
|
|
|
errs.into_iter().for_each(|e| {
|
|
|
|
|
Report::build(ReportKind::Error, (), e.span().start)
|
|
|
|
|
.with_message(e.to_string())
|
|
|
|
|
.with_label(
|
|
|
|
|
Label::new(e.span())
|
|
|
|
|
.with_message(e)
|
|
|
|
|
.with_color(Color::Red),
|
|
|
|
|
)
|
|
|
|
|
.finish()
|
|
|
|
|
.print(Source::from(&src))
|
|
|
|
|
.unwrap()
|
|
|
|
|
});
|
2025-01-31 17:56:08 +01:00
|
|
|
|
}
|
2025-01-29 11:33:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-03 19:21:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-02-14 14:07:18 +01:00
|
|
|
|
let path = morphism_base.find_morphism_path(MorphismType {
|
|
|
|
|
src_type: type_dict.parse( src_type_arg.as_str() ).expect(""),
|
|
|
|
|
dst_type: type_dict.parse( dst_type_arg.as_str() ).expect(""),
|
|
|
|
|
},
|
|
|
|
|
&mut *type_dict.write().unwrap()
|
|
|
|
|
);
|
|
|
|
|
|
2025-02-03 19:21:53 +01:00
|
|
|
|
match path {
|
|
|
|
|
Some(path) => {
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
println!(r#"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <morphisms/length-prefix.h>
|
|
|
|
|
#include <morphisms/posint.h>
|
|
|
|
|
|
2025-02-14 14:07:18 +01:00
|
|
|
|
int main() {{
|
2025-02-03 19:21:53 +01:00
|
|
|
|
uint8_t bufA[1024];
|
|
|
|
|
uint8_t bufB[1024];
|
|
|
|
|
|
|
|
|
|
scanf("%s", bufA);
|
2025-02-14 14:07:18 +01:00
|
|
|
|
"#);
|
|
|
|
|
for morph_inst in path {
|
2025-02-05 11:26:55 +01:00
|
|
|
|
println!(r#"
|
2025-02-14 14:07:18 +01:00
|
|
|
|
/* morph to {}
|
|
|
|
|
|
|
|
|
|
...with
|
|
|
|
|
morph {}
|
|
|
|
|
---> {},
|
|
|
|
|
subst σ = {{ {:?} }},
|
|
|
|
|
halo Ψ = {}
|
|
|
|
|
*/"#,
|
|
|
|
|
type_dict.unparse(&morph_inst.get_type().dst_type.param_normalize().decurry()),
|
|
|
|
|
type_dict.unparse(&morph_inst.m.get_type().src_type),
|
|
|
|
|
type_dict.unparse(&morph_inst.m.get_type().dst_type),
|
|
|
|
|
morph_inst.σ,
|
|
|
|
|
type_dict.unparse(&morph_inst.halo),
|
|
|
|
|
);
|
|
|
|
|
morph_inst.m.generate_call(&mut type_dict, i);
|
2025-02-05 11:26:55 +01:00
|
|
|
|
i += 1;
|
2025-02-03 19:21:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let out_buf = if i%2==0 { "bufA" } else { "bufB" };
|
|
|
|
|
println!(r#"
|
|
|
|
|
printf("%s\n", {});
|
|
|
|
|
|
|
|
|
|
return 0;
|
2025-02-14 14:07:18 +01:00
|
|
|
|
}}
|
|
|
|
|
"#, out_buf);
|
2025-02-03 19:21:53 +01:00
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
eprintln!("Error: could not find morphism path");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-29 11:33:01 +01:00
|
|
|
|
}
|