This commit is contained in:
Edgar 2024-01-20 16:44:34 +01:00
parent 54950fc2ad
commit 9333638933
No known key found for this signature in database
GPG key ID: 70ADAE8F35904387
4 changed files with 56 additions and 12 deletions

2
.clangd Normal file
View file

@ -0,0 +1,2 @@
CompileFlags: # Tweak the parse settings
Add: [-std=c++17, -xc++, -Wall, -I/home/edgar/storage/llvm-17/include]

View file

@ -160,6 +160,22 @@ fn get_location<'c>(context: &'c MeliorContext, session: &Session, offset: usize
) )
} }
fn get_di_file<'c>(context: &'c MeliorContext, session: &Session) -> Attribute<'c> {
Attribute::parse(
context,
&format!(r#"#llvm.di_file<"{}">"#, session.file_path.display()),
)
.unwrap()
}
fn get_di<'c>(context: &'c MeliorContext, session: &Session) -> Attribute<'c> {
Attribute::parse(
context,
&format!(r#"#llvm.di_file<"{}">"#, session.file_path.display()),
)
.unwrap()
}
fn compile_function_def<'ctx, 'parent>( fn compile_function_def<'ctx, 'parent>(
session: &Session, session: &Session,
context: &'ctx MeliorContext, context: &'ctx MeliorContext,
@ -171,7 +187,6 @@ fn compile_function_def<'ctx, 'parent>(
let region = Region::new(); let region = Region::new();
let location = get_location(context, session, info.name.span.lo); let location = get_location(context, session, info.name.span.lo);
let location = Location::name(context, &info.name.name, location);
let mut args = Vec::with_capacity(info.params.len()); let mut args = Vec::with_capacity(info.params.len());
let mut fn_args_types = Vec::with_capacity(info.params.len()); let mut fn_args_types = Vec::with_capacity(info.params.len());
@ -179,7 +194,7 @@ fn compile_function_def<'ctx, 'parent>(
for param in &info.params { for param in &info.params {
let param_type = scope_ctx.resolve_type(context, &param.arg_type)?; let param_type = scope_ctx.resolve_type(context, &param.arg_type)?;
let loc = get_location(context, session, param.name.span.lo); let loc = get_location(context, session, param.name.span.lo);
let loc = Location::name(context, &param.name.name, loc); // let loc = Location::name(context, &param.name.name, loc);
args.push((param_type, loc)); args.push((param_type, loc));
fn_args_types.push(param_type); fn_args_types.push(param_type);
} }
@ -223,11 +238,7 @@ fn compile_function_def<'ctx, 'parent>(
if final_block.terminator().is_none() { if final_block.terminator().is_none() {
final_block.append_operation(func::r#return( final_block.append_operation(func::r#return(
&[], &[],
Location::name( get_location(context, session, info.span.hi),
context,
"return",
get_location(context, session, info.span.hi),
),
)); ));
} }
} }
@ -383,7 +394,6 @@ fn compile_return<'ctx, 'parent: 'ctx>(
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
tracing::debug!("compiling return"); tracing::debug!("compiling return");
let location = get_location(context, session, info.span.lo); let location = get_location(context, session, info.span.lo);
let location = Location::name(context, "return", location);
let ret_type = scope_ctx.function.and_then(|x| x.return_type.clone()); let ret_type = scope_ctx.function.and_then(|x| x.return_type.clone());
@ -471,7 +481,6 @@ fn compile_expression<'ctx, 'parent: 'ctx>(
.expect("local not found"); .expect("local not found");
let location = get_location(context, session, path.first.span.lo); let location = get_location(context, session, path.first.span.lo);
let location = Location::name(context, &path.first.name, location);
if local.is_alloca { if local.is_alloca {
let k0 = block let k0 = block
@ -638,13 +647,18 @@ fn compile_fn_call<'ctx, 'parent: 'ctx>(
) -> Result<Value<'ctx, 'parent>, Box<dyn Error>> { ) -> Result<Value<'ctx, 'parent>, Box<dyn Error>> {
let mut args = Vec::with_capacity(info.params.len()); let mut args = Vec::with_capacity(info.params.len());
let location = get_location(context, session, info.name.span.lo); let location = get_location(context, session, info.name.span.lo);
/*
let location_callee = Location::name(context, &info.name.name, location); let location_callee = Location::name(context, &info.name.name, location);
let location_caller = Location::name( let location_caller = Location::name(
context, context,
&info.name.name, &info.name.name,
get_location(context, session, scope_ctx.function.unwrap().span.lo), get_location(context, session, scope_ctx.function.unwrap().span.lo),
); );
let location = Location::call_site(location_callee, location_caller); */
let location = Location::call_site(
location,
get_location(context, session, scope_ctx.function.unwrap().span.lo),
);
let target_fn = scope_ctx let target_fn = scope_ctx
.functions .functions
@ -718,7 +732,7 @@ fn compile_if_stmt<'c, 'this: 'c>(
else_successor, else_successor,
&[], &[],
&[], &[],
Location::name(context, "if", location), location,
)); ));
let mut then_successor = then_successor; let mut then_successor = then_successor;

View file

@ -24,7 +24,7 @@ use llvm_sys::{
LLVMTargetRef, LLVMTargetRef,
}, },
}; };
use melior::ir::Module as MeliorModule; use melior::ir::{operation::OperationPrintingFlags, Module as MeliorModule};
use crate::ffi::mlirTranslateModuleToLLVMIR; use crate::ffi::mlirTranslateModuleToLLVMIR;
@ -33,6 +33,18 @@ mod context;
mod ffi; mod ffi;
pub mod linker; pub mod linker;
pub fn compile_mlir(
session: &Session,
program: &Module,
) -> Result<String, Box<dyn std::error::Error>> {
let context = Context::new();
let mlir_module = context.compile(session, program)?;
Ok(mlir_module
.as_operation()
.to_string_with_flags(OperationPrintingFlags::new().enable_debug_info(true, false))?)
}
pub fn compile(session: &Session, program: &Module) -> Result<PathBuf, Box<dyn std::error::Error>> { pub fn compile(session: &Session, program: &Module) -> Result<PathBuf, Box<dyn std::error::Error>> {
let context = Context::new(); let context = Context::new();
let mlir_module = context.compile(session, program)?; let mlir_module = context.compile(session, program)?;

View file

@ -18,6 +18,12 @@ pub struct CompilerArgs {
/// Build as a library. /// Build as a library.
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]
library: bool, library: bool,
#[arg(long, default_value_t = false)]
mlir: bool,
#[arg(long, default_value_t = false)]
ast: bool,
} }
pub fn main() -> Result<(), Box<dyn Error>> { pub fn main() -> Result<(), Box<dyn Error>> {
@ -70,6 +76,16 @@ pub fn main() -> Result<(), Box<dyn Error>> {
}; };
tracing::debug!("Compiling with session: {:#?}", session); tracing::debug!("Compiling with session: {:#?}", session);
if args.ast {
println!("{:#?}", module);
return Ok(());
}
if args.mlir {
println!("{}", edlang_codegen_mlir::compile_mlir(&session, &module)?);
return Ok(());
}
let object_path = edlang_codegen_mlir::compile(&session, &module)?; let object_path = edlang_codegen_mlir::compile(&session, &module)?;
if session.library { if session.library {