From 35b747ea15a67d2005a55d8a00c286c0b2b0ae4a Mon Sep 17 00:00:00 2001 From: Michael Sippel <micha@fragmental.art> Date: Wed, 2 Apr 2025 14:31:47 +0200 Subject: [PATCH] fix local src/dst types in complex morphisms --- src/c_gen.rs | 150 +++------------------------------------------------ 1 file changed, 6 insertions(+), 144 deletions(-) diff --git a/src/c_gen.rs b/src/c_gen.rs index 5677c73..49c69b5 100644 --- a/src/c_gen.rs +++ b/src/c_gen.rs @@ -2,12 +2,6 @@ use { crate::{c_gen_types::{encode_morph_type_to_symbol, encode_type_to_symbol, encode_type_to_value, get_c_repr_type}, morphism::LdmcPrimMorph, struct_layout::{get_type_size, LayoutType, ObjectSize, StructLayout}}, chumsky::{chain::Chain, primitive::Container}, laddertypes::{morphism::{Morphism, MorphismInstance}, morphism_sugared::{MorphismInstance2, SugaredMorphism, SugaredMorphismType}, parser::*, substitution_sugared::SugaredSubstitution, unparser::*, Substitution, SugaredEnumVariant, SugaredStructMember, SugaredTypeTerm, TypeDict, TypeTerm}, std::collections::HashMap }; - - - - - - impl LdmcPrimMorph { pub fn instantiated_symbol_name(&self, dict: &mut impl TypeDict, σ: &impl SugaredSubstitution) -> String { let mut s = self.symbol.clone(); @@ -40,9 +34,6 @@ impl LdmcPrimMorph { pub fn generate_instantiation(&self, dict: &mut impl TypeDict, σ: &impl SugaredSubstitution) -> Option<String> { let mut s = String::new(); - let src_type = self.ty.src_type.clone().apply_subst(σ).clone(); - let dst_type = self.ty.dst_type.clone().apply_subst(σ).clone(); - let symbol = self.instantiated_symbol_name(dict, σ); let src_c_symbol = encode_type_to_symbol(dict, &self.ty.strip_halo().src_type); let dst_c_symbol = encode_type_to_symbol(dict, &self.ty.strip_halo().dst_type); @@ -174,8 +165,8 @@ impl LdmcCTargetMorph { if let Ok(inst) = self.add_instantiation(dict, morph.clone()) { let morph_symbol = inst.instantiated_symbol_name(dict, &morph.get_subst()); - let src_c_type = get_c_repr_type(dict, inst.get_type().strip_halo().src_type, true).expect("cant get c-repr type for src type"); - let dst_c_type = get_c_repr_type(dict, inst.get_type().strip_halo().dst_type, true).expect("cant get c-repr type for dst type"); + let src_c_type = encode_type_to_symbol(dict, &inst.get_type().strip_halo().src_type); + let dst_c_type = encode_type_to_symbol(dict, &inst.get_type().strip_halo().dst_type); let src_buf = if i == 0 { "src" } else if i%2 == 0 { "(void*)bufA" } else { "(void*)bufB" }; let dst_buf = if i+1 == path.len() { "dst" } else if i%2 == 0 { "(void*)bufB" } else { "(void*)bufA" }; @@ -218,8 +209,10 @@ impl LdmcCTargetMorph { let length_c_type = get_c_repr_type(dict, length_type.clone(), true).expect("cant c-repr type for array length"); let src_item_c_type = get_c_repr_type(dict, item_morph.get_haloless_type().src_type, true).expect("cant c-repr type for src item"); let dst_item_c_type = get_c_repr_type(dict, item_morph.get_haloless_type().dst_type, true).expect("cant c-repr type for dst item"); - let src_c_type = encode_type_to_symbol(dict, &ty.strip_halo().src_type); - let dst_c_type = encode_type_to_symbol(dict, &ty.strip_halo().dst_type); + let src_c_type = encode_type_to_symbol(dict, &ty.src_type); + let dst_c_type = encode_type_to_symbol(dict, &ty.dst_type); + + // todo: self.add_active_length_prefix_map() let map_fn = format!("length_prefix_{}_array_map_{}_to_{}", length_c_type, src_item_c_type, dst_item_c_type @@ -285,134 +278,3 @@ impl LdmcCTargetMorph { } } } - - -/* -pub fn generate_main( - type_dict: &mut impl TypeDict, - morph: MorphismInstance2<LdmcPrimMorph>, - src_type: TypeTerm, dst_type: TypeTerm -) { - let mut i = 0; - - - - /* todo: collect include files from morphism base */ - println!(r#" -#include <stdio.h> -#include <unistd.h> -#include <string.h> -#include <stdint.h> -#include <stdbool.h> -"#); - - let mut existing_instantiations = Vec::new(); - for morph_inst in path.iter() { - match &morph_inst.m { - LdmcMorphism::Primitive( item_morph ) => { - let name = item_morph.instantiated_symbol_name(type_dict, &morph_inst.σ); - if ! existing_instantiations.contains(&name) { - if let Some(s) = item_morph.generate_instantiation(type_dict, &morph_inst.σ) { - println!("{}", s); - } else { - eprintln!("couldnt generate instance {}", name); - } - existing_instantiations.push( name ); - } - } - LdmcMorphism::LengthPrefixMap { length_prefix_type, item_morph } => { - let name = item_morph.instantiated_symbol_name(type_dict, &morph_inst.σ); - if ! existing_instantiations.contains(&name) { - if let Some(s) = item_morph.generate_instantiation(type_dict, &morph_inst.σ) { - println!("{}", s); - } else { - eprintln!("couldnt generate instance {}", name); - } - - existing_instantiations.push( name ); - } - } - _ => {} - } - } - - println!(r#" -int main() {{ -uint8_t bufA[128]; -uint8_t bufB[128]; - -memset(bufA, 0, sizeof(bufA)); -memset(bufB, 0, sizeof(bufB)); - -char in_str[] = "read :: {} \n"; -char out_str[]= "write:: {} \n"; -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); - - "#, - type_dict.unparse(&src_type).replace("\\", "\\\\"), - type_dict.unparse(&dst_type).replace("\\", "\\\\") ); - - for morph_inst in path.iter() { - println!(r#" -/* 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(type_dict, &morph_inst.σ, i) - ); - - i += 1; - } - - let out_buf = if i%2==0 { "bufA" } else { "bufB" }; - - let is_string = false; - if let Ok((halo, σ)) = laddertypes::subtype_unify( - &dst_type, - &type_dict.parse("<Seq~<ValueTerminated 0> native.UInt8>").unwrap() - ) { - println!(r#" -printf("%s\n", {});"#, out_buf); - } else if let Ok((halo, σ)) = laddertypes::subtype_unify( - &dst_type, - &type_dict.parse("<Seq~<LengthPrefix native.UInt64> native.UInt8>").unwrap() - ) { - println!(r#" - /* write output - */ - {{ - LengthPrefix_uint64_t_Array_uint8_t * buf = (void*){}; - write(1, {}, sizeof(uint64_t) + buf->len); - }}"#, out_buf, out_buf); - } else { - println!(r#" -write(1, {}, sizeof({}));"#, - out_buf, - out_buf - ); - } - - - println!(r#" -return 0; -}} - "#); - - eprintln!("Success: generated C code"); -} -*/