Skip to content

Commit

Permalink
more hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Jul 10, 2024
1 parent a501648 commit 56d835b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,27 +139,37 @@ 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,
});
let all_names = global_index
.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)?;
Expand Down
3 changes: 2 additions & 1 deletion src/codegen/generators/pou_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
}
}
Expand All @@ -154,7 +155,7 @@ impl<'ink, 'cg> PouGenerator<'ink, 'cg> {
}

fn mangle_function(&self, implementation: &ImplementationIndexEntry) -> Result<String, Diagnostic> {
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());

Expand Down
12 changes: 10 additions & 2 deletions src/codegen/generators/variable_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())?,
Expand Down
13 changes: 13 additions & 0 deletions src/codegen/llvm_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

Expand All @@ -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,
Expand Down

0 comments on commit 56d835b

Please sign in to comment.