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 {
|
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 src_type = self.ty.src_type.clone().apply_subst(σ).clone();
|
||||||
let dst_type = self.ty.dst_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 symbol = self.instantiated_symbol_name(dict, σ);
|
||||||
let src_c_symbol = encode_type_to_symbol(dict, &self.ty.src_type);
|
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.dst_type);
|
let dst_c_symbol = encode_type_to_symbol(dict, &self.ty.strip_halo().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");
|
|
||||||
|
|
||||||
s.push_str(&format!(r#"
|
s.push_str(&format!(r#"
|
||||||
int {} ( {} const * restrict src, {} * restrict dst ) {{
|
int {} ( {} const * restrict src, {} * restrict dst ) {{
|
||||||
"#,
|
"#,
|
||||||
self.instantiated_symbol_name(dict, σ),
|
symbol, src_c_symbol, dst_c_symbol,
|
||||||
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"),
|
|
||||||
));
|
));
|
||||||
s.push_str(&self.c_source);
|
s.push_str(&self.c_source);
|
||||||
s.push_str(&format!(r#"
|
s.push_str(&format!(r#"
|
||||||
|
@ -72,14 +67,18 @@ int {} ( {} const * restrict src, {} * restrict dst ) {{
|
||||||
|
|
||||||
pub struct LdmcCTargetMorph {
|
pub struct LdmcCTargetMorph {
|
||||||
includes: Vec< String >,
|
includes: Vec< String >,
|
||||||
|
active_types: Vec< SugaredTypeTerm >,
|
||||||
active_instantiations: Vec< LdmcPrimMorph >,
|
active_instantiations: Vec< LdmcPrimMorph >,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LdmcCTargetMorph {
|
impl LdmcCTargetMorph {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
LdmcCTargetMorph {
|
LdmcCTargetMorph {
|
||||||
includes: Vec::new(),
|
includes: vec![
|
||||||
active_instantiations: Vec::new()
|
"#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) {
|
if ! self.active_instantiations.contains(&new_inst) {
|
||||||
self.active_instantiations.push(new_inst.clone());
|
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)
|
Ok(new_inst)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,6 +109,14 @@ impl LdmcCTargetMorph {
|
||||||
source.push_str(&inc);
|
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 {
|
for m in self.active_instantiations {
|
||||||
source.push_str(&m.generate_instantiation(dict, &HashMap::new()).expect("cant create function"));
|
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();
|
let mut c_source = String::new();
|
||||||
for (ty_id, kind) in morph.type_args.iter() {
|
for (ty_id, kind) in morph.type_args.iter() {
|
||||||
if let Some(val) = σ.get(ty_id) {
|
if let Some(val) = σ.get(ty_id) {
|
||||||
eprintln!("val = {}", val.pretty(dict, 0));
|
|
||||||
let type_var_value =
|
let type_var_value =
|
||||||
if let Some(c_repr_type) = get_c_repr_type(dict, val.clone(), true) {
|
if let Some(c_repr_type) = get_c_repr_type(dict, val.clone(), true) {
|
||||||
c_repr_type
|
c_repr_type
|
||||||
|
@ -138,13 +153,13 @@ impl LdmcCTargetMorph {
|
||||||
Ok(LdmcPrimMorph{
|
Ok(LdmcPrimMorph{
|
||||||
symbol: morph.instantiated_symbol_name(dict, σ),
|
symbol: morph.instantiated_symbol_name(dict, σ),
|
||||||
type_args: Vec::new(),
|
type_args: Vec::new(),
|
||||||
ty: morph_inst.get_type(),
|
ty: morph_inst.get_haloless_type(),
|
||||||
c_source
|
c_source
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
MorphismInstance2::Chain { path } => {
|
MorphismInstance2::Chain { path } => {
|
||||||
let ty = morph_inst.get_type();
|
let ty = morph_inst.get_type().strip_halo();
|
||||||
let symbol = encode_morph_type_to_symbol(dict, &ty);
|
let symbol = encode_morph_type_to_symbol(dict, &morph_inst.get_haloless_type());
|
||||||
|
|
||||||
let mut c_source = String::new();
|
let mut c_source = String::new();
|
||||||
|
|
||||||
|
@ -159,8 +174,8 @@ impl LdmcCTargetMorph {
|
||||||
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
|
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
|
||||||
let morph_symbol = inst.instantiated_symbol_name(dict, &morph.get_subst());
|
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 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, ty.dst_type.clone(), true).expect("cant get c-repr type for dst 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 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" };
|
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 } => {
|
MorphismInstance2::MapSeq { ψ, seq_repr, item_morph } => {
|
||||||
let i = 0; // <--todo
|
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 {
|
if let Some(seq_repr) = seq_repr {
|
||||||
|
|
||||||
dict.add_varname("LengthType".into());
|
dict.add_varname("LengthType".into());
|
||||||
|
@ -196,15 +211,15 @@ impl LdmcCTargetMorph {
|
||||||
seq_repr.as_ref()
|
seq_repr.as_ref()
|
||||||
) {
|
) {
|
||||||
let length_type = γ.get(&dict.get_typeid(&"LengthType".into()).expect("")).expect("cant get LengthType");
|
let length_type = γ.get(&dict.get_typeid(&"LengthType".into()).expect("")).expect("cant get LengthType");
|
||||||
let ty = morph_inst.get_type();
|
let ty = morph_inst.get_type().strip_halo();
|
||||||
let symbol = encode_morph_type_to_symbol(dict, &ty);
|
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_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 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 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_type().dst_type, true).expect("cant c-repr type for dst 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 = get_c_repr_type(dict, ty.src_type.clone(), true).expect("cant get c-repr type for src type");
|
let src_c_type = encode_type_to_symbol(dict, &ty.strip_halo().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 dst_c_type = encode_type_to_symbol(dict, &ty.strip_halo().dst_type);
|
||||||
|
|
||||||
let map_fn = format!("length_prefix_{}_array_map_{}_to_{}",
|
let map_fn = format!("length_prefix_{}_array_map_{}_to_{}",
|
||||||
length_c_type, src_item_c_type, dst_item_c_type
|
length_c_type, src_item_c_type, dst_item_c_type
|
||||||
|
@ -238,13 +253,14 @@ impl LdmcCTargetMorph {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
MorphismInstance2::MapStruct { ψ, struct_repr, member_morph } => {
|
MorphismInstance2::MapStruct { ψ, struct_repr, member_morph } => {
|
||||||
let ty = morph_inst.get_type();
|
let ty = morph_inst.get_type().strip_halo();
|
||||||
let symbol =encode_morph_type_to_symbol(dict, &ty);
|
let symbol =encode_morph_type_to_symbol(dict, &morph_inst.get_haloless_type());
|
||||||
|
|
||||||
let mut c_source = String::new();
|
let mut c_source = String::new();
|
||||||
|
|
||||||
for (name, morph) in member_morph.iter() {
|
for (name, morph) in member_morph.iter() {
|
||||||
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
|
if let Ok(inst) = self.add_instantiation(dict, morph.clone()) {
|
||||||
|
let name = name.replace("-", "_").replace(".","_dot_");
|
||||||
c_source.push_str(
|
c_source.push_str(
|
||||||
&format!("
|
&format!("
|
||||||
{} ( &src->{}, &dst->{} );
|
{} ( &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
|
/* 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> {
|
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() {
|
match t.get_floor_type().1.strip() {
|
||||||
SugaredTypeTerm::TypeID(tyid) => {
|
SugaredTypeTerm::TypeID(tyid) => {
|
||||||
if tyid == dict.get_typeid_creat("native.UInt8") {
|
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("");
|
let field_c_type = get_c_repr_type(dict, ty, false).expect("");
|
||||||
c_type.push_str(&format!(" {} {};\n",
|
c_type.push_str(&format!(" {} {};\n",
|
||||||
field_c_type,
|
field_c_type,
|
||||||
symbol
|
symbol.replace("-", "_").replace(".","_dot_")
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,7 +150,7 @@ struct {{
|
||||||
|
|
||||||
for (i, SugaredEnumVariant{ symbol, ty }) in variants.iter().enumerate() {
|
for (i, SugaredEnumVariant{ symbol, ty }) in variants.iter().enumerate() {
|
||||||
c_type.push_str(&format!(
|
c_type.push_str(&format!(
|
||||||
" {} = {}", symbol.to_uppercase(), i
|
" {} = {}", symbol.replace("-", "_").replace(".","_dot_").to_uppercase(), i
|
||||||
));
|
));
|
||||||
|
|
||||||
if i+1 < variants.len() {
|
if i+1 < variants.len() {
|
||||||
|
@ -169,7 +167,7 @@ struct {{
|
||||||
for SugaredEnumVariant{ symbol, ty } in variants {
|
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");
|
let variant_c_type = get_c_repr_type(dict, ty, false).expect("cant get C-Repr type for variant");
|
||||||
c_type.push_str(&format!(
|
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) => {
|
laddertypes::TypeTerm::Char(c) => {
|
||||||
match c {
|
match c {
|
||||||
'.' => format!("_dot_"),
|
'.' => format!("_dot_"),
|
||||||
|
'-' => format!("_minus_"),
|
||||||
_ =>
|
_ =>
|
||||||
format!("{}", (c as u64))
|
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) => {
|
laddertypes::TypeTerm::TypeID(ty_id) => {
|
||||||
if let Some(name) = dict.get_typename(&ty_id) {
|
if let Some(name) = dict.get_typename(&ty_id) {
|
||||||
format!("{}", name.replace(".", "_dot_"))
|
name
|
||||||
|
.replace("-", "_")
|
||||||
|
.replace(".", "_dot_")
|
||||||
} else {
|
} else {
|
||||||
format!("")
|
format!("")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue