improve symbol names in generated c code
This commit is contained in:
parent
583892f10d
commit
39cba1ee57
2 changed files with 51 additions and 34 deletions
72
src/c_gen.rs
72
src/c_gen.rs
|
@ -1,5 +1,5 @@
|
|||
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, SugaredMorphismType}, parser::*, substitution_sugared::SugaredSubstitution, unparser::*, Substitution, SugaredEnumVariant, SugaredStructMember, SugaredTypeTerm, TypeDict, TypeTerm}, std::collections::HashMap
|
||||
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
|
||||
};
|
||||
|
||||
|
||||
|
@ -43,19 +43,14 @@ impl LdmcPrimMorph {
|
|||
let src_type = self.ty.src_type.clone().apply_subst(σ).clone();
|
||||
let dst_type = self.ty.dst_type.clone().apply_subst(σ).clone();
|
||||
|
||||
let symbol = encode_morph_type_to_symbol(dict, &self.ty);
|
||||
let src_c_symbol = encode_type_to_symbol(dict, &self.ty.src_type);
|
||||
let dst_c_symbol = encode_type_to_symbol(dict, &self.ty.dst_type);
|
||||
|
||||
let src_c_type = get_c_repr_type(dict, self.ty.src_type.clone(), true).expect("cant get c-repr type for src type");
|
||||
let dst_c_type = get_c_repr_type(dict, self.ty.dst_type.clone(), true).expect("cant get c-repr type for src type");
|
||||
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);
|
||||
|
||||
s.push_str(&format!(r#"
|
||||
int {} ( {} const * restrict src, {} * restrict dst ) {{
|
||||
"#,
|
||||
self.instantiated_symbol_name(dict, σ),
|
||||
get_c_repr_type(dict, src_type, true).expect("cant get c-repr-type"),
|
||||
get_c_repr_type(dict, dst_type, true).expect("cant get c-repr-type"),
|
||||
symbol, src_c_symbol, dst_c_symbol,
|
||||
));
|
||||
s.push_str(&self.c_source);
|
||||
s.push_str(&format!(r#"
|
||||
|
@ -72,14 +67,18 @@ int {} ( {} const * restrict src, {} * restrict dst ) {{
|
|||
|
||||
pub struct LdmcCTargetMorph {
|
||||
includes: Vec< String >,
|
||||
active_types: Vec< SugaredTypeTerm >,
|
||||
active_instantiations: Vec< LdmcPrimMorph >,
|
||||
}
|
||||
|
||||
impl LdmcCTargetMorph {
|
||||
pub fn new() -> Self {
|
||||
LdmcCTargetMorph {
|
||||
includes: Vec::new(),
|
||||
active_instantiations: Vec::new()
|
||||
includes: vec![
|
||||
"#include <stdint.h>".into(),
|
||||
],
|
||||
active_instantiations: Vec::new(),
|
||||
active_types: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,6 +91,15 @@ impl LdmcCTargetMorph {
|
|||
if ! self.active_instantiations.contains(&new_inst) {
|
||||
self.active_instantiations.push(new_inst.clone());
|
||||
}
|
||||
|
||||
let ty = new_inst.get_type().strip_halo();
|
||||
if ! self.active_types.contains(&ty.src_type) {
|
||||
self.active_types.push(ty.src_type);
|
||||
}
|
||||
if ! self.active_types.contains(&ty.dst_type) {
|
||||
self.active_types.push(ty.dst_type);
|
||||
}
|
||||
|
||||
Ok(new_inst)
|
||||
}
|
||||
|
||||
|
@ -101,6 +109,14 @@ impl LdmcCTargetMorph {
|
|||
source.push_str(&inc);
|
||||
}
|
||||
|
||||
for ty in self.active_types {
|
||||
source.push_str(&format!("
|
||||
typedef {} {};
|
||||
",
|
||||
get_c_repr_type(dict, ty.clone(), false).expect("cant get c-repr type"),
|
||||
encode_type_to_symbol(dict, &ty)));
|
||||
}
|
||||
|
||||
for m in self.active_instantiations {
|
||||
source.push_str(&m.generate_instantiation(dict, &HashMap::new()).expect("cant create function"));
|
||||
}
|
||||
|
@ -119,7 +135,6 @@ impl LdmcCTargetMorph {
|
|||
let mut c_source = String::new();
|
||||
for (ty_id, kind) in morph.type_args.iter() {
|
||||
if let Some(val) = σ.get(ty_id) {
|
||||
eprintln!("val = {}", val.pretty(dict, 0));
|
||||
let type_var_value =
|
||||
if let Some(c_repr_type) = get_c_repr_type(dict, val.clone(), true) {
|
||||
c_repr_type
|
||||
|
@ -138,13 +153,13 @@ impl LdmcCTargetMorph {
|
|||
Ok(LdmcPrimMorph{
|
||||
symbol: morph.instantiated_symbol_name(dict, σ),
|
||||
type_args: Vec::new(),
|
||||
ty: morph_inst.get_type(),
|
||||
ty: morph_inst.get_haloless_type(),
|
||||
c_source
|
||||
})
|
||||
},
|
||||
MorphismInstance2::Chain { path } => {
|
||||
let ty = morph_inst.get_type();
|
||||
let symbol = encode_morph_type_to_symbol(dict, &ty);
|
||||
let ty = morph_inst.get_type().strip_halo();
|
||||
let symbol = encode_morph_type_to_symbol(dict, &morph_inst.get_haloless_type());
|
||||
|
||||
let mut c_source = String::new();
|
||||
|
||||
|
@ -159,8 +174,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, ty.src_type.clone(), true).expect("cant get c-repr type for src type");
|
||||
let dst_c_type = get_c_repr_type(dict, ty.dst_type.clone(), true).expect("cant get c-repr type for dst type");
|
||||
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_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" };
|
||||
|
@ -187,7 +202,7 @@ impl LdmcCTargetMorph {
|
|||
}
|
||||
MorphismInstance2::MapSeq { ψ, seq_repr, item_morph } => {
|
||||
let i = 0; // <--todo
|
||||
self.add_instantiation(dict, item_morph.as_ref().clone());
|
||||
self.add_instantiation(dict, item_morph.as_ref().clone()).expect("add instantiation");
|
||||
if let Some(seq_repr) = seq_repr {
|
||||
|
||||
dict.add_varname("LengthType".into());
|
||||
|
@ -196,15 +211,15 @@ impl LdmcCTargetMorph {
|
|||
seq_repr.as_ref()
|
||||
) {
|
||||
let length_type = γ.get(&dict.get_typeid(&"LengthType".into()).expect("")).expect("cant get LengthType");
|
||||
let ty = morph_inst.get_type();
|
||||
let symbol = encode_morph_type_to_symbol(dict, &ty);
|
||||
let item_morph_symbol = encode_morph_type_to_symbol(dict, &item_morph.get_type());
|
||||
let ty = morph_inst.get_type().strip_halo();
|
||||
let symbol = encode_morph_type_to_symbol(dict, &morph_inst.get_haloless_type());
|
||||
let item_morph_symbol = encode_morph_type_to_symbol(dict, &item_morph.get_haloless_type());
|
||||
|
||||
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_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_type().dst_type, true).expect("cant c-repr type for dst item");
|
||||
let src_c_type = get_c_repr_type(dict, ty.src_type.clone(), true).expect("cant get c-repr type for src type");
|
||||
let dst_c_type = get_c_repr_type(dict, ty.dst_type.clone(), true).expect("cant get c-repr type for dst type");
|
||||
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 map_fn = format!("length_prefix_{}_array_map_{}_to_{}",
|
||||
length_c_type, src_item_c_type, dst_item_c_type
|
||||
|
@ -238,13 +253,14 @@ impl LdmcCTargetMorph {
|
|||
}
|
||||
},
|
||||
MorphismInstance2::MapStruct { ψ, struct_repr, member_morph } => {
|
||||
let ty = morph_inst.get_type();
|
||||
let symbol =encode_morph_type_to_symbol(dict, &ty);
|
||||
let ty = morph_inst.get_type().strip_halo();
|
||||
let symbol =encode_morph_type_to_symbol(dict, &morph_inst.get_haloless_type());
|
||||
|
||||
let mut c_source = String::new();
|
||||
|
||||
for (name, morph) in member_morph.iter() {
|
||||
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
|
||||
let name = name.replace("-", "_").replace(".","_dot_");
|
||||
c_source.push_str(
|
||||
&format!("
|
||||
{} ( &src->{}, &dst->{} );
|
||||
|
|
|
@ -14,8 +14,6 @@ pub fn get_c_repr_kind(kind: String) -> Option<String> {
|
|||
/* for a given ladder type `t`, get the corresponding C type
|
||||
*/
|
||||
pub fn get_c_repr_type(dict: &mut impl TypeDict, t: SugaredTypeTerm, skip_pointer: bool) -> Option<String> {
|
||||
eprintln!("get c repr of {}", t.pretty(dict,0));
|
||||
|
||||
match t.get_floor_type().1.strip() {
|
||||
SugaredTypeTerm::TypeID(tyid) => {
|
||||
if tyid == dict.get_typeid_creat("native.UInt8") {
|
||||
|
@ -136,7 +134,7 @@ pub fn get_c_repr_type(dict: &mut impl TypeDict, t: SugaredTypeTerm, skip_pointe
|
|||
let field_c_type = get_c_repr_type(dict, ty, false).expect("");
|
||||
c_type.push_str(&format!(" {} {};\n",
|
||||
field_c_type,
|
||||
symbol
|
||||
symbol.replace("-", "_").replace(".","_dot_")
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -152,7 +150,7 @@ struct {{
|
|||
|
||||
for (i, SugaredEnumVariant{ symbol, ty }) in variants.iter().enumerate() {
|
||||
c_type.push_str(&format!(
|
||||
" {} = {}", symbol.to_uppercase(), i
|
||||
" {} = {}", symbol.replace("-", "_").replace(".","_dot_").to_uppercase(), i
|
||||
));
|
||||
|
||||
if i+1 < variants.len() {
|
||||
|
@ -169,7 +167,7 @@ struct {{
|
|||
for SugaredEnumVariant{ symbol, ty } in variants {
|
||||
let variant_c_type = get_c_repr_type(dict, ty, false).expect("cant get C-Repr type for variant");
|
||||
c_type.push_str(&format!(
|
||||
" {} {};\n", variant_c_type, symbol
|
||||
" {} {};\n", variant_c_type, symbol.replace("-", "_").replace(".","_dot_")
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -198,6 +196,7 @@ pub fn encode_type_to_symbol(dict: &mut impl TypeDict, t: &laddertypes::SugaredT
|
|||
laddertypes::TypeTerm::Char(c) => {
|
||||
match c {
|
||||
'.' => format!("_dot_"),
|
||||
'-' => format!("_minus_"),
|
||||
_ =>
|
||||
format!("{}", (c as u64))
|
||||
}
|
||||
|
@ -207,7 +206,9 @@ pub fn encode_type_to_symbol(dict: &mut impl TypeDict, t: &laddertypes::SugaredT
|
|||
}
|
||||
laddertypes::TypeTerm::TypeID(ty_id) => {
|
||||
if let Some(name) = dict.get_typename(&ty_id) {
|
||||
format!("{}", name.replace(".", "_dot_"))
|
||||
name
|
||||
.replace("-", "_")
|
||||
.replace(".", "_dot_")
|
||||
} else {
|
||||
format!("")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue