Skip to content

Commit

Permalink
feat: add modules support, add name mangling
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 28, 2024
1 parent 478cdca commit 54f148b
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
11 changes: 7 additions & 4 deletions lib/edlang_codegen_llvm/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ fn compile_fn_signature(ctx: &ModuleCompileCtx<'_, '_>, fn_id: DefId) {
};

let fn_value = ctx.module.add_function(
&body.name,
&body.get_mangled_name(),
fn_type,
Some(if body.is_extern {
inkwell::module::Linkage::AvailableExternally
Expand Down Expand Up @@ -256,7 +256,7 @@ fn compile_fn_signature(ctx: &ModuleCompileCtx<'_, '_>, fn_id: DefId) {
let subprogram = ctx.di_builder.create_function(
ctx.di_namespace,
&body.name,
Some(&body.name),
Some(&body.get_mangled_name()),
ctx.di_unit.get_file(),
line as u32 + 1,
di_type,
Expand All @@ -273,7 +273,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError>
let body = ctx.ctx.program.functions.get(&fn_id).unwrap();
trace!("compiling fn body: {}", body.name);

let fn_value = ctx.module.get_function(&body.name).unwrap();
let fn_value = ctx.module.get_function(&body.get_mangled_name()).unwrap();
let di_program = fn_value.get_subprogram().unwrap();

let mut debug_loc = ctx.set_debug_loc(di_program.as_debug_info_scope(), body.fn_span);
Expand Down Expand Up @@ -528,7 +528,10 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError>
target,
} => {
let target_fn_body = ctx.ctx.program.functions.get(func).unwrap();
let fn_value = ctx.module.get_function(&target_fn_body.name).unwrap();
let fn_value = ctx
.module
.get_function(&target_fn_body.get_mangled_name())
.unwrap();
let args: Vec<_> = args
.iter()
.map(|x| compile_rvalue(ctx, fn_id, &locals, x).unwrap().0.into())
Expand Down
10 changes: 5 additions & 5 deletions lib/edlang_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ pub fn main() -> Result<(), Box<dyn Error>> {
let path = args.input.display().to_string();
let source = std::fs::read_to_string(&args.input)?;

let module = edlang_parser::parse_ast(&source);
let modules = edlang_parser::parse_ast(&source);

let module = match module {
Ok(module) => module,
let modules = match modules {
Ok(modules) => modules,
Err(error) => {
let report = edlang_parser::error_to_report(&path, &error)?;
edlang_parser::print_report(&path, &source, report)?;
Expand Down Expand Up @@ -121,11 +121,11 @@ pub fn main() -> Result<(), Box<dyn Error>> {
tracing::debug!("Debug Info: {:#?}", session.debug_info);

if args.ast {
println!("{:#?}", module);
println!("{:#?}", modules);
return Ok(());
}

let program_ir = match lower_modules(&[module.clone()]) {
let program_ir = match lower_modules(&modules) {
Ok(ir) => ir,
Err(error) => {
let report = edlang_check::lowering_error_to_report(error, &session);
Expand Down
4 changes: 2 additions & 2 deletions lib/edlang_driver/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn compile_program(
name: &str,
library: bool,
) -> Result<CompileResult, Box<dyn std::error::Error>> {
let module = edlang_parser::parse_ast(source).unwrap();
let modules = edlang_parser::parse_ast(source).unwrap();

let test_dir = tempfile::tempdir()?;
let test_dir_path = test_dir.path();
Expand Down Expand Up @@ -63,7 +63,7 @@ pub fn compile_program(
output_asm: false,
};

let program_ir = lower_modules(&[module])?;
let program_ir = lower_modules(&modules)?;

let object_path = edlang_codegen_llvm::compile(&session, &program_ir)?;

Expand Down
11 changes: 11 additions & 0 deletions lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ impl Body {
pub fn get_return_local(&self) -> Local {
self.locals[0].clone()
}

pub fn get_mangled_name(&self) -> String {
if self.name == "main" {
"main".to_string()
} else {
format!(
"{}@{}@{}",
self.name, self.def_id.program_id, self.def_id.id
)
}
}
}

#[derive(Debug, Clone)]
Expand Down
9 changes: 6 additions & 3 deletions lib/edlang_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ fn lower_module(
}
}

let body = ctx.body.modules.get(&id).unwrap();

// fill fn sigs
for content in &module.contents {
if let ModuleStatement::Function(fn_def) = content {
let body = ctx.body.modules.get(&id).unwrap();
let fn_id = *body.symbols.functions.get(&fn_def.name.name).unwrap();

let mut args = Vec::new();
Expand Down Expand Up @@ -92,7 +91,11 @@ fn lower_module(
ctx = lower_function(ctx, fn_def, id)?;
}
// ModuleStatement::Type(_) => todo!(),
ModuleStatement::Module(_mod_def) => {}
ModuleStatement::Module(mod_def) => {
let body = ctx.body.modules.get(&id).unwrap();
let id = *body.symbols.modules.get(&mod_def.name.name).unwrap();
ctx = lower_module(ctx, mod_def, id)?;
}
_ => {}
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/edlang_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ pub(crate) Import: ast::Import = {
}
}

pub Modules: Vec<ast::Module> = {
<Module+> => <>
}

pub Module: ast::Module = {
<lo:@L> "mod" <name:Ident> "{" <imports:List<Import>?> <contents:List<ModuleStatement>> "}" <hi:@R> => ast::Module {
name,
Expand Down
4 changes: 2 additions & 2 deletions lib/edlang_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub mod grammar {

pub fn parse_ast(
source: &str,
) -> Result<edlang_ast::Module, ParseError<usize, Token, LexicalError>> {
) -> Result<Vec<edlang_ast::Module>, ParseError<usize, Token, LexicalError>> {
let lexer = Lexer::new(source);
let parser = grammar::ModuleParser::new();
let parser = grammar::ModulesParser::new();
parser.parse(lexer)
}

Expand Down

0 comments on commit 54f148b

Please sign in to comment.