Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Jun 19, 2024
1 parent d9c4561 commit aa18b7c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
4 changes: 3 additions & 1 deletion compiler/plc_driver/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ pub fn get_config_format(name: &str) -> Option<ConfigFormat> {

impl CompileParameters {
pub fn parse<T: AsRef<OsStr> + AsRef<str>>(args: &[T]) -> Result<CompileParameters, ParameterError> {
CompileParameters::try_parse_from(args).and_then(|result| {
CompileParameters::try_parse_from(args).and_then(|mut result| {
result.got_layout_file = Some(String::from("tmp.json"));

if result.sysroot.len() > result.target.len() {
let mut cmd = CompileParameters::command();
Err(cmd.error(
Expand Down
6 changes: 3 additions & 3 deletions compiler/plc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::{
use cli::{CompileParameters, ParameterError, SubCommands};
use pipelines::AnnotatedProject;
use plc::{
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel,
ErrorFormat, OptimizationLevel, Target, Threads,
codegen::CodegenContext, linker::LinkerType, output::FormatOption, ConfigFormat, DebugLevel, ErrorFormat,
OptimizationLevel, Target, Threads,
};

use plc_diagnostics::{diagnostician::Diagnostician, diagnostics::Diagnostic};
Expand Down Expand Up @@ -372,7 +372,7 @@ fn generate(
};
let project = annotated_project.get_project();
let output_name = project.get_output_name();
res.into_par_iter()
res.into_iter()
.map(|res| {
res.link(
project.get_objects(),
Expand Down
5 changes: 5 additions & 0 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
});
ensure_compile_dirs(targets, &compile_directory)?;
let targets = if targets.is_empty() { &[Target::System] } else { targets };

let res = targets
.par_iter()
.map(|target| {
Expand Down Expand Up @@ -364,6 +365,8 @@ impl<T: SourceContainer + Sync> AnnotatedProject<T> {
_ => format!("{}.o", output_name.to_string_lossy()),
};

dbg!(&output_name);

let context = CodegenContext::create(); //Create a build location for the generated object files
let module =
self.generate_module(&context, compile_options, unit, dependencies, literals)?;
Expand Down Expand Up @@ -419,6 +422,8 @@ fn ensure_compile_dirs(targets: &[Target], compile_directory: &Path) -> Result<(
pub struct GeneratedProject {
target: Target,
objects: Vec<Object>,
// Layout of the custom Global Offset Table for online-change
got_layout: HashMap<String, u64>,
}

impl GeneratedProject {
Expand Down
21 changes: 13 additions & 8 deletions src/codegen/generators/variable_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ pub fn read_got_layout(location: &str, format: ConfigFormat) -> Result<HashMap<S
let s =
read_to_string(location).map_err(|_| Diagnostic::new("GOT layout could not be read from file"))?;
match format {
ConfigFormat::JSON => serde_json::from_str(&s)
.map_err(|_| Diagnostic::new("Could not deserialize GOT layout from JSON")),
ConfigFormat::JSON => serde_json::from_str(&s).map_err(|e| {
dbg!(e);
Diagnostic::new("Could not deserialize GOT layout from JSON")
}),
ConfigFormat::TOML => {
toml::de::from_str(&s).map_err(|_| Diagnostic::new("Could not deserialize GOT layout from TOML"))
}
Expand Down Expand Up @@ -65,7 +67,7 @@ pub struct VariableGenerator<'ctx, 'b> {
annotations: &'b AstAnnotations,
types_index: &'b LlvmTypedIndex<'ctx>,
debug: &'b mut DebugBuilderEnum<'ctx>,
got_layout_file: Option<(String, ConfigFormat)>,
got_layout: Option<&HashMap<String, u64>>,
}

impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
Expand All @@ -76,9 +78,9 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
annotations: &'b AstAnnotations,
types_index: &'b LlvmTypedIndex<'ctx>,
debug: &'b mut DebugBuilderEnum<'ctx>,
got_layout_file: Option<(String, ConfigFormat)>,
got_layout: Option<&HashMap<String, u64>>,
) -> Self {
VariableGenerator { module, llvm, global_index, annotations, types_index, debug, got_layout_file }
VariableGenerator { module, llvm, global_index, annotations, types_index, debug, got_layout }
}

pub fn generate_global_variables(
Expand Down Expand Up @@ -140,8 +142,7 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
);
}

if let Some((location, format)) = &self.got_layout_file {
let got_entries = read_got_layout(location.as_str(), *format)?;
if let Some(got_entries) = &self.got_layout {
let mut new_globals = Vec::new();
let mut new_got_entries = HashMap::new();
let mut new_got = HashMap::new();
Expand All @@ -166,9 +167,13 @@ impl<'ctx, 'b> VariableGenerator<'ctx, 'b> {
}

// Now we can write new_got_entries back out to a file.
write_got_layout(new_got_entries, location.as_str(), *format)?;
// write_got_layout(new_got_entries, location.as_str(), *format)?;

index.associate_got_layout(new_got_entries);

// Construct our GOT as a new global array. We initialise this array in the loader code.
// FIXME: Is it okay to do this when we compile multiple object files at once but only want one resulting customGOT global variable?
// FIXME: Where should we do that otherwise?
let got_size = new_got.keys().max().map_or(0, |m| *m + 1);
let _got = self.llvm.create_global_variable(
self.module,
Expand Down
8 changes: 8 additions & 0 deletions src/codegen/llvm_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct LlvmTypedIndex<'ink> {
constants: FxHashMap<String, BasicValueEnum<'ink>>,
utf08_literals: FxHashMap<String, GlobalValue<'ink>>,
utf16_literals: FxHashMap<String, GlobalValue<'ink>>,
got_layout: HashMap<String, u64>,
}

impl<'ink> LlvmTypedIndex<'ink> {
Expand All @@ -35,6 +36,7 @@ impl<'ink> LlvmTypedIndex<'ink> {
constants: FxHashMap::default(),
utf08_literals: FxHashMap::default(),
utf16_literals: FxHashMap::default(),
got_layout: HashMap::new(),
}
}

Expand Down Expand Up @@ -63,6 +65,7 @@ impl<'ink> LlvmTypedIndex<'ink> {
self.constants.extend(other.constants);
self.utf08_literals.extend(other.utf08_literals);
self.utf16_literals.extend(other.utf16_literals);
self.got_layout.extend(other.got_layout);
}

pub fn associate_type(
Expand Down Expand Up @@ -213,4 +216,9 @@ impl<'ink> LlvmTypedIndex<'ink> {
.get(literal)
.or_else(|| self.parent_index.and_then(|it| it.find_utf16_literal_string(literal)))
}

// FIXME: Does this need a better API? something like `index.associate_got_index(variable_name, got_index)`?
pub fn associate_got_layout(&mut self, got_layout: HashMap<String, u64>) {
self.got_layout = got_layout;
}
}

0 comments on commit aa18b7c

Please sign in to comment.