generate all length prefix array variants via macro

This commit is contained in:
Michael Sippel 2025-03-20 16:16:41 +01:00
parent 4c7302c4a3
commit e29a5a3475
Signed by: senvas
GPG key ID: F96CF119C34B64A6
7 changed files with 175 additions and 241 deletions

View file

@ -39,15 +39,31 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: laddertypes::TypeTerm, skip_
laddertypes::TypeTerm::App(args) => {
if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"LengthPrefix".into()).unwrap())
{
let _length_type = args[1].clone();
let length_c_type : String = get_c_repr_type(dict, args[1].clone(), false)?;
let item_c_type : String = get_c_repr_type(dict, args[2].clone(), false)?;
match item_c_type.as_str() {
"uint8_t" => Some(format!("struct LengthPrefixUInt8Array")),
"uint16_t" => Some(format!("struct LengthPrefixUInt16Array")),
"uint32_t" => Some(format!("struct LengthPrefixUInt32Array")),
"uint64_t" => Some(format!("struct LengthPrefixUInt64Array")),
_ => None
match length_c_type.as_str() {
"uint8_t" |
"uint16_t" |
"uint32_t" |
"uint64_t" => {}
_ => {
eprintln!("invalid length type!");
return None;
}
}
match item_c_type.as_str() {
"uint8_t" |
"uint16_t" |
"uint32_t" |
"uint64_t" => {}
_ => {
eprintln!("invalid item type!");
return None;
}
}
Some(format!("LengthPrefix_{}_Array_{}", length_c_type, item_c_type))
}
else if args[0] == laddertypes::TypeTerm::TypeID(dict.get_typeid(&"ValueTerminated".into()).unwrap())
{
@ -174,7 +190,7 @@ write(2, in_str, strlen(in_str));
write(2, out_str, strlen(out_str));
int l = read(0, bufA, sizeof(bufA));
fprintf(stderr, "read %d bytes\n", l);
//fprintf(stderr, "read %d bytes\n", l);
"#,
type_dict.unparse(&src_type).replace("\\", "\\\\"),
@ -217,14 +233,14 @@ printf("%s\n", {});"#, out_buf);
/* write output
*/
{{
struct LengthPrefixUInt8Array * buf = (void*){};
LengthPrefix_uint64_t_Array_uint8_t * buf = (void*){};
write(1, {}, sizeof(uint64_t) + buf->len);
}}"#, out_buf, out_buf);
} else {
println!(r#"
write(1, {}, {});"#,
write(1, {}, sizeof({}));"#,
out_buf,
"sizeof(bufA)"
out_buf
);
}
@ -319,18 +335,42 @@ impl LdmcMorphism {
'}');
}
LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => {
let src_c_type = get_c_repr_type(dict, self.get_type().src_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone(), true).expect("cant get c-repr type for src type");
let dst_c_type = get_c_repr_type(dict, self.get_type().dst_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone(), true).expect("cant get c-repr type for dst type");
let src_type = self.get_type().src_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone();
let dst_type = self.get_type().dst_type.clone().apply_substitution(&|k| σ.get(k).cloned()).clone();
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",
("struct LengthPrefixUInt8Array", "struct LengthPrefixUInt8Array") => "length_prefix_array_map_8_to_8",
_ => {
"{{ ERROR: no map function implemented }}"
}
};
eprintln!("length prefix type ={:?}", length_prefix_type);
eprintln!("src_type ={:?}", src_type);
eprintln!("dst_type = {:?}", dst_type);
dict.add_varname("S".into());
let γ_src = laddertypes::unify(
&dict.parse("<Seq~<LengthPrefix S> T>").expect("parse template"),
&src_type
).expect("cant get src item type");
let γ_dst = laddertypes::unify(
&dict.parse("<Seq~<LengthPrefix S> T>").expect("parse template"),
&dst_type
).expect("cant get dst item type");
let src_length_type = γ_src.get(&dict.get_typeid(&"S".into()).unwrap()).expect("cant get src-length type").clone();
let dst_length_type = γ_src.get(&dict.get_typeid(&"S".into()).unwrap()).expect("cant get dst-length type").clone();
let length_type = src_length_type;
let src_item_type = γ_src.get(&dict.get_typeid(&"T".into()).unwrap()).expect("cant get src-item type").clone();
let dst_item_type = γ_dst.get(&dict.get_typeid(&"T".into()).unwrap()).expect("cant get src-item type").clone();
let length_c_type = get_c_repr_type(dict, length_type, true).expect("cant c-repr type for array length");
let src_item_c_type = get_c_repr_type(dict, src_item_type, true).expect("cant c-repr type for src item");
let dst_item_c_type = get_c_repr_type(dict, dst_item_type, true).expect("cant c-repr type for dst item");
let src_c_type = get_c_repr_type(dict, src_type, true).expect("cant get c-repr type for src type");
let dst_c_type = get_c_repr_type(dict, dst_type, true).expect("cant get c-repr type for dst type");
let map_fn = format!("length_prefix_{}_array_map_{}_to_{}",
length_c_type, src_item_c_type, dst_item_c_type
);
let src_buf = if i%2 == 0 { "bufA" } else { "bufB" };
let dst_buf = if i%2 == 0 { "bufB" } else { "bufA" };