From 6b34187d6bcc480fc057e7de63507a1917f0bffd Mon Sep 17 00:00:00 2001 From: nafeij Date: Thu, 4 Apr 2024 12:52:16 +0800 Subject: [PATCH] init melior expr --- .vscode/settings.json | 2 + Cargo.lock | 7 ++ conqiler/Cargo.toml | 1 + conqiler/src/codegen.rs | 141 ++++++++++++++++++++++++++++++++++++++++ conqiler/src/main.rs | 68 +++++-------------- 5 files changed, 169 insertions(+), 50 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 conqiler/src/codegen.rs diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7a73a41 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index a7b7a1b..9255241 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "anyhow" +version = "1.0.81" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247" + [[package]] name = "anymap2" version = "0.13.0" @@ -351,6 +357,7 @@ dependencies = [ name = "conqiler" version = "0.1.0" dependencies = [ + "anyhow", "chumsky", "melior", "rand", diff --git a/conqiler/Cargo.toml b/conqiler/Cargo.toml index 87a4595..be6cade 100644 --- a/conqiler/Cargo.toml +++ b/conqiler/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" chumsky = "0.9.3" melior = "0.17.0" rand = "0.8.5" +anyhow = "1.0.81" \ No newline at end of file diff --git a/conqiler/src/codegen.rs b/conqiler/src/codegen.rs new file mode 100644 index 0000000..2427c0d --- /dev/null +++ b/conqiler/src/codegen.rs @@ -0,0 +1,141 @@ +use std::{ + cell::RefCell, + collections::{BTreeMap, HashMap}, +}; + +use melior::{ + dialect::{arith::constant, func, DialectRegistry}, + ir::{ + attribute::IntegerAttribute, + block::{self, Block}, + r#type::{IntegerType, Type}, + Location, Module, Region, + }, + utility::{register_all_dialects, register_all_llvm_translations}, + Context, ExecutionEngine, +}; + +use crate::ast::*; + +use anyhow::Result; + +struct CodeGen<'ctx, 'module> { + context: &'ctx Context, + module: &'module Module<'ctx>, + annon_string_counter: RefCell, + // type_store: BTreeMap, + // var_to_type: HashMap, +} + +impl<'ctx, 'module> CodeGen<'ctx, 'module> { + fn gen_ast_code(&'ctx self, ast: Ast, _emit_mlir: bool) -> Result<()> { + let location = Location::unknown(&self.context); + + match ast { + Ast::Expr(expr) => { + let block = Block::new(&[(Type::index(&self.context), location)]); + let region = Region::new(); + + let value = self.gen_expr_code(expr, &block)?.unwrap(); + block.append_operation(func::r#return(&[value], location)); + + // todo append block + } + _ => todo!(), + }; + + Ok(()) + } + + fn gen_expr_code<'a>( + &'ctx self, + expr: Expr, + block: &'a Block<'ctx>, + ) -> Result>> { + let location = Location::unknown(&self.context); + + let val = match expr { + Expr::Literal(Value::Int(val)) => Some( + block + .append_operation(constant( + &self.context, + IntegerAttribute::new( + IntegerType::new(&self.context, 64).into(), + val as i64, + ) + .into(), + location, + )) + .result(0)? + .into(), + ), + _ => todo!(), + }; + Ok(val) + } +} + +pub fn gen_mlir<'c>(ast: Ast, emit: bool) -> ExecutionEngine { + let registry = DialectRegistry::new(); + register_all_dialects(®istry); + + let context = Context::new(); + context.append_dialect_registry(®istry); + context.load_all_available_dialects(); + register_all_llvm_translations(&context); + + context.attach_diagnostic_handler(|diagnostic| { + eprintln!("{}", diagnostic); + true + }); + + // let index_type = Type::index(&context); + + // module.body().append_operation(func::func( + // &context, + // StringAttribute::new(&context, "add"), + // TypeAttribute::new( + // FunctionType::new(&context, &[index_type, index_type], &[index_type]).into(), + // ), + // { + // let block = Block::new(&[(index_type, location), (index_type, location)]); + + // let sum = block.append_operation(arith::addi( + // block.argument(0).unwrap().into(), + // block.argument(1).unwrap().into(), + // location, + // )); + + // block.append_operation(func::r#return(&[sum.result(0).unwrap().into()], location)); + + // let region = Region::new(); + // region.append_block(block); + // region + // }, + // &[], + // location, + // )); + + let mut module = Module::new(melior::ir::Location::unknown(&context)); + + let code_gen = CodeGen { + context: &context, + module: &module, + annon_string_counter: RefCell::new(0), + // type_store: BTreeMap::new(), + // var_to_type: HashMap::new(), + }; + + code_gen.gen_ast_code(ast, emit).unwrap(); + + if !emit { + assert!(module.as_operation().verify()); + } + + if emit { + println!("{}", module.as_operation()); + } + + let engine = ExecutionEngine::new(&module, 0, &[], true); + engine +} diff --git a/conqiler/src/main.rs b/conqiler/src/main.rs index 9c825e0..bfbbf95 100644 --- a/conqiler/src/main.rs +++ b/conqiler/src/main.rs @@ -1,56 +1,24 @@ -use melior::{ - dialect::{arith, func, DialectRegistry}, - ir::{ - attribute::{StringAttribute, TypeAttribute}, - r#type::FunctionType, - *, - }, - utility::register_all_dialects, - Context, -}; +// use melior::{ +// dialect::{arith, func, DialectRegistry}, +// ir::{ +// attribute::{StringAttribute, TypeAttribute}, +// r#type::FunctionType, +// *, +// }, +// utility::register_all_dialects, +// Context, +// }; + +use codegen::gen_mlir; +use parser::parse; mod ast; +mod codegen; mod parser; fn main() { - let registry = DialectRegistry::new(); - register_all_dialects(®istry); - - let context = Context::new(); - context.append_dialect_registry(®istry); - context.load_all_available_dialects(); - - let location = Location::unknown(&context); - let module = Module::new(location); - - let index_type = Type::index(&context); - - module.body().append_operation(func::func( - &context, - StringAttribute::new(&context, "add"), - TypeAttribute::new( - FunctionType::new(&context, &[index_type, index_type], &[index_type]).into(), - ), - { - let block = Block::new(&[(index_type, location), (index_type, location)]); - - let sum = block.append_operation(arith::addi( - block.argument(0).unwrap().into(), - block.argument(1).unwrap().into(), - location, - )); - - block.append_operation(func::r#return(&[sum.result(0).unwrap().into()], location)); - - let region = Region::new(); - region.append_block(block); - region - }, - &[], - location, - )); - - assert!(module.as_operation().verify()); - let llir = module.as_operation().to_string(); - println!("{}", llir); + let _ = gen_mlir( + parse("fn main() { let x = 1; }"), + true, + ); }