Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make annotations available in codegen #251

Merged
merged 1 commit into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use self::{
},
llvm_index::LlvmTypedIndex,
};
use crate::compile_error::CompileError;
use crate::{compile_error::CompileError, resolver::AnnotationMap};

use super::ast::*;
use super::index::*;
Expand Down Expand Up @@ -93,7 +93,8 @@ impl<'ink> CodeGen<'ink> {
/// generates all TYPEs, GLOBAL-sections and POUs of the given CompilationUnit
pub fn generate(
&self,
unit: CompilationUnit,
unit: &CompilationUnit,
_annotations: &AnnotationMap,
global_index: &Index,
) -> Result<String, CompileError> {
//Associate the index type with LLVM types
Expand All @@ -103,10 +104,10 @@ impl<'ink> CodeGen<'ink> {
let llvm = Llvm::new(self.context, self.context.create_builder());
let pou_generator = PouGenerator::new(llvm, global_index, &llvm_index);
//Generate the POU stubs in the first go to make sure they can be referenced.
for implementation in unit.implementations {
for implementation in &unit.implementations {
//Don't generate external functions
if implementation.linkage != LinkageType::External {
pou_generator.generate_implementation(&implementation)?;
pou_generator.generate_implementation(implementation)?;
}
}
Ok(self.module.print_to_string().to_string())
Expand Down
6 changes: 4 additions & 2 deletions src/codegen/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ macro_rules! codegen_wihout_unwrap {
let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
let index = crate::index::visitor::visit(&ast);
let annotations = crate::resolver::TypeAnnotator::visit_unit(&index, &ast);
let code_generator = crate::codegen::CodeGen::new(&context, "main");
code_generator.generate(ast, &index)
code_generator.generate(&ast, &annotations, &index)
}};
}

Expand All @@ -26,8 +27,9 @@ macro_rules! codegen {
let context = inkwell::context::Context::create();
crate::ast::pre_process(&mut ast);
let index = crate::index::visitor::visit(&ast);
let annotations = crate::resolver::TypeAnnotator::visit_unit(&index, &ast);
let code_generator = crate::codegen::CodeGen::new(&context, "main");
code_generator.generate(ast, &index).unwrap()
code_generator.generate(&ast, &annotations, &index).unwrap()
}};
}

Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use std::{fs::File, io::Read};
use validation::Validator;

use crate::ast::CompilationUnit;
use crate::resolver::AnnotationMap;
mod ast;
pub mod cli;
mod codegen;
Expand Down Expand Up @@ -402,25 +403,26 @@ pub fn compile_module<'c, T: SourceContainer>(

// ### PHASE 2 ###
// annotation & validation everything
for (file_id, syntax_errors, container) in all_units.iter() {
let annotations = TypeAnnotator::visit_unit(&full_index, container);
type AnnotatedAst<'a> = (&'a CompilationUnit, AnnotationMap);
let mut annotated_units: Vec<AnnotatedAst> = Vec::new();
for (file_id, syntax_errors, unit) in all_units.iter() {
let annotations = TypeAnnotator::visit_unit(&full_index, unit);

let mut validator = Validator::new();
validator.visit_unit(&annotations, container);
validator.visit_unit(&annotations, unit);
//log errors
report_diagnostics(*file_id, syntax_errors.iter(), &files)?;
report_diagnostics(*file_id, validator.diagnostics().iter(), &files)?;

annotated_units.push((unit, annotations));
}

// ### PHASE 3 ###
// - codegen
let mut code_gen_unit = CompilationUnit::default();
for (_, _, u) in all_units {
code_gen_unit.import(u);
}

let code_generator = codegen::CodeGen::new(context, "main");
code_generator.generate(code_gen_unit, &full_index)?;
for (unit, annotations) in annotated_units {
code_generator.generate(unit, &annotations, &full_index)?;
}
Ok(code_generator)
}

Expand Down
6 changes: 6 additions & 0 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl AnnotationMap {
}
}

impl Default for AnnotationMap {
fn default() -> Self {
Self::new()
}
}

impl<'i> TypeAnnotator<'i> {
/// constructs a new TypeAnnotater that works with the given index for type-lookups
fn new(index: &'i Index) -> TypeAnnotator<'i> {
Expand Down