diff --git a/src/codegen.rs b/src/codegen.rs index 9f9e415392..d0fd4ba3eb 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -139,14 +139,18 @@ impl<'ink> CodeGen<'ink> { // functions and assign them indices in the GOT, taking into account prior indices. let program_globals = global_index.get_program_instances().into_iter().fold(Vec::new(), |mut acc, p| { - acc.push(p.get_name()); - acc.push(p.get_qualified_name()); + acc.push(p.get_name().to_owned()); + acc.push(p.get_qualified_name().to_owned()); + acc.push(format!("{}_instance", p.get_name())); acc }); + let test: Vec<_> = + program_globals.clone().into_iter().map(|s| crate::index::get_initializer_name(&s)).collect(); + let functions = global_index.get_pous().values().filter_map(|p| match p { PouIndexEntry::Function { name, linkage: LinkageType::Internal, is_generated: false, .. } | PouIndexEntry::FunctionBlock { name, linkage: LinkageType::Internal, .. } => { - Some(name.as_ref()) + Some(String::from(name)) } _ => None, }); @@ -154,12 +158,18 @@ impl<'ink> CodeGen<'ink> { .get_globals() .values() .map(VariableIndexEntry::get_qualified_name) + .map(String::from) .chain(program_globals) .chain(functions) - .map(str::to_lowercase); + .map(|s| s.to_lowercase()) + .map(|s| (crate::index::get_initializer_name(&s), s)) + .fold(Vec::new(), |mut acc, (s, s1)| { + acc.push(s); + acc.push(s1); + acc + }); - let all_names: Vec<_> = all_names.collect(); - dbg!(all_names.len()); + // let all_names: Vec<_> = all_names.collect(); if let Some(got_entries) = &mut *got_layout.lock().unwrap() { // let got_entries = read_got_layout(location.as_str(), *format)?; diff --git a/src/codegen/generators/pou_generator.rs b/src/codegen/generators/pou_generator.rs index a9c2d01caf..cada91f3b1 100644 --- a/src/codegen/generators/pou_generator.rs +++ b/src/codegen/generators/pou_generator.rs @@ -133,6 +133,7 @@ pub fn generate_global_constants_for_pou_members<'ink>( .make_constant() .set_initial_value(Some(value), variable_type); local_llvm_index.associate_global(&name, global_value)?; + local_llvm_index.insert_new_got_index(&name)?; } } } @@ -154,7 +155,7 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> { } fn mangle_function(&self, implementation: &ImplementationIndexEntry) -> Result { - let ctx = SectionMangler::function(implementation.get_call_name()); + let ctx = SectionMangler::function(implementation.get_call_name().to_lowercase()); let params = self.index.get_declared_parameters(implementation.get_call_name()); diff --git a/src/codegen/generators/variable_generator.rs b/src/codegen/generators/variable_generator.rs index 25c96c4298..cd9222f797 100644 --- a/src/codegen/generators/variable_generator.rs +++ b/src/codegen/generators/variable_generator.rs @@ -10,6 +10,7 @@ use indexmap::IndexSet; use inkwell::{module::Module, values::GlobalValue}; use plc_ast::ast::LinkageType; use plc_diagnostics::diagnostics::Diagnostic; +use section_mangler::SectionMangler; use super::{ data_type_generator::get_default_for, @@ -171,8 +172,15 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> { } } - let section = section_mangler::SectionMangler::variable( - global_variable.get_qualified_name(), + let global_name = if global_variable.get_name().ends_with("instance") { + global_variable.get_name() + } else { + global_variable.get_qualified_name() + }; + let global_name = global_name.to_lowercase(); + + let section = SectionMangler::variable( + global_name, section_names::mangle_type( self.global_index, self.global_index.get_effective_type_by_name(global_variable.get_type_name())?, diff --git a/src/codegen/llvm_index.rs b/src/codegen/llvm_index.rs index 4cd4682fc0..4b1679e5ac 100644 --- a/src/codegen/llvm_index.rs +++ b/src/codegen/llvm_index.rs @@ -166,6 +166,9 @@ impl<'ink> LlvmTypedIndex<'ink> { self.global_values.insert(variable_name.to_lowercase(), global_variable); self.initial_value_associations .insert(variable_name.to_lowercase(), global_variable.as_pointer_value().into()); + + // FIXME: Do we want to call .insert_new_got_index() here? + Ok(()) } @@ -174,6 +177,16 @@ impl<'ink> LlvmTypedIndex<'ink> { Ok(()) } + pub fn insert_new_got_index(&mut self, variable_name: &str) -> Result<(), Diagnostic> { + eprintln!("llvm_index: inserting {variable_name}"); + // FIXME: Does that start at 0 or 1? + let idx = self.got_indices.values().max().copied().unwrap_or(0); + + self.got_indices.insert(variable_name.to_lowercase(), idx); + + Ok(()) + } + pub fn associate_implementation( &mut self, callable_name: &str,