Skip to content

Commit

Permalink
fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Dec 9, 2024
1 parent 0afcc86 commit 29e1a42
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 37 deletions.
2 changes: 1 addition & 1 deletion docs/src/mlir_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ A region holds one or multiple blocks, it depends on the operation whether there
Usually multiple regions are used in higher level dialects, like SCF, which has while and for constructs, the CF dialect instead works
with blocks.

A region is more isolated than a block, you can easily use a value from a predecessor block within a given block, but taking a value from another region requires passing it as argument to the operation/block. This makes operations that work with regions like SCF a bit harder to work with in some contexts.
A region is more isolated than a block, you can easily use a value from a predecessor block within a given block, but taking a value from another region that is not a parent requires passing it as argument to the operation/block. This makes operations that work with regions like SCF a bit harder to work with in some contexts.

```rust
let region = Region::new();
Expand Down
41 changes: 24 additions & 17 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, path::Path};
use ifelse_stmt::compile_if;
use let_stmt::compile_let;
use melior::{
ir::{Block, BlockRef, Module, Region, Value},
ir::{r#type::IntegerType, Block, BlockRef, Location, Module, Region, Type, Value},
Context,
};
use return_stmt::compile_return;
Expand All @@ -20,7 +20,7 @@ pub mod return_stmt;

pub struct ModuleCtx<'c> {
pub ctx: &'c Context,
pub module: Module<'c>,
pub module: &'c Module<'c>,
}

pub fn compile_program(ctx: &ModuleCtx, program: &Program, optlevel: OptLevel, out_name: &Path) {
Expand All @@ -37,33 +37,40 @@ pub fn compile_program(ctx: &ModuleCtx, program: &Program, optlevel: OptLevel, o
link_binary(&[out_obj], out_name).unwrap();
}

pub struct FunctionCtx<'c> {
pub ctx: &'c Context,
pub module: Module<'c>,
pub region: Region<'c>,
}

fn compile_function<'ctx>(ctx: &ModuleCtx<'ctx>, func: &Function) {
let mut locals: HashMap<String, Value<'ctx, '_>> = HashMap::new();
let mut args: Vec<(Type, Location)> = vec![];

for _ in &func.args {
args.push((
IntegerType::new(&ctx.ctx, 64).into(),
Location::unknown(&ctx.ctx),
));
}

let ctx = todo!("implement me");
let region = todo!("implement me");
let block = todo!("implement me");
let region = Region::new();
let mut block = region.append_block(Block::new(&args));
let mut locals: HashMap<String, Value> = HashMap::new();

for stmt in &func.body.stmts {
compile_statement(ctx, &mut locals, block, stmt);
compile_statement(&ctx, &mut locals, &block, stmt);
}
}

fn compile_statement<'ctx: 'parent, 'parent>(
ctx: &FunctionCtx<'ctx>,
ctx: &ModuleCtx<'ctx>,
locals: &mut HashMap<String, Value<'ctx, 'parent>>,
block: &'parent Block<'ctx>,
stmt: &Statement,
) {
match stmt {
Statement::Let(let_stmt) => compile_let(ctx, locals, block, let_stmt),
Statement::If(if_stmt) => compile_if(ctx, locals, block, if_stmt),
Statement::Return(return_stmt) => compile_return(ctx, locals, block, return_stmt),
Statement::Let(let_stmt) => {
compile_let(ctx, locals, block, let_stmt);
}
Statement::If(if_stmt) => {
compile_if(ctx, locals, block, if_stmt);
}
Statement::Return(return_stmt) => {
compile_return(ctx, locals, &block, return_stmt);
}
}
}
44 changes: 39 additions & 5 deletions src/codegen/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,54 @@
use std::collections::HashMap;

use melior::ir::{Block, BlockRef, Value};
use melior::{
dialect::{arith, llvm},
ir::{
attribute::IntegerAttribute, r#type::IntegerType, Block, BlockRef, Location, Type, Value,
},
};

use crate::ast::{Expr, Opcode};

use super::FunctionCtx;
use super::ModuleCtx;

pub fn compile_expr<'ctx: 'parent, 'parent>(
ctx: &FunctionCtx<'ctx>,
ctx: &ModuleCtx<'ctx>,
locals: &HashMap<String, Value<'ctx, 'parent>>,
block: &'parent Block<'ctx>,
expr: &Expr,
) -> Value<'ctx, 'parent> {
let location = Location::unknown(ctx.ctx);
let i64_type: Type = IntegerType::new(ctx.ctx, 64).into();
match expr {
Expr::Number(x) => todo!(),
Expr::Variable(name) => todo!(),
Expr::Number(value) => {
let value = block
.append_operation(arith::constant(
ctx.ctx,
IntegerAttribute::new(IntegerType::new(ctx.ctx, 64).into(), *value).into(),
location,
))
.result(0)
.unwrap()
.into();

value
}
Expr::Variable(name) => {
let local_ptr = *locals.get(name).unwrap();
let value = block
.append_operation(llvm::load(
ctx.ctx,
local_ptr,
i64_type,
location,
Default::default(),
))
.result(0)
.unwrap()
.into();

value
}
Expr::Op(lhs_expr, opcode, rhs_expr) => {
let lhs = compile_expr(ctx, locals, block, lhs_expr);
let rhs = compile_expr(ctx, locals, block, rhs_expr);
Expand Down
14 changes: 9 additions & 5 deletions src/codegen/ifelse_stmt.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::collections::HashMap;
use std::{collections::HashMap, ops::Deref};

use melior::ir::{Block, BlockRef, Value};
use melior::{
dialect::scf,
ir::{Block, BlockRef, Region, Value},
};

use crate::ast::IfStmt;

use super::FunctionCtx;
use super::{expressions::compile_expr, ModuleCtx};

pub fn compile_if<'ctx: 'parent, 'parent>(
ctx: &FunctionCtx<'ctx>,
pub fn compile_if<'ctx, 'parent>(
ctx: &ModuleCtx<'ctx>,
locals: &mut HashMap<String, Value<'ctx, 'parent>>,
block: &'parent Block<'ctx>,
stmt: &IfStmt,
) {
let cond = compile_expr(ctx, locals, block, &stmt.cond);
}
8 changes: 5 additions & 3 deletions src/codegen/let_stmt.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::collections::HashMap;

use melior::ir::{Block, BlockRef, Value};
use melior::ir::{Block, Value};

use crate::ast::LetStmt;

use super::FunctionCtx;
use super::{expressions::compile_expr, ModuleCtx};

pub fn compile_let<'ctx: 'parent, 'parent>(
ctx: &FunctionCtx<'ctx>,
ctx: &ModuleCtx<'ctx>,
locals: &mut HashMap<String, Value<'ctx, 'parent>>,
block: &'parent Block<'ctx>,
stmt: &LetStmt,
) {
let value = compile_expr(ctx, locals, block, &stmt.expr);
locals.insert(stmt.variable.clone(), value);
}
6 changes: 3 additions & 3 deletions src/codegen/return_stmt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::collections::HashMap;

use melior::ir::{Block, Value};
use melior::ir::{Block, BlockRef, Value};

use crate::ast::ReturnStmt;

use super::FunctionCtx;
use super::ModuleCtx;

pub fn compile_return<'ctx, 'parent>(
ctx: &FunctionCtx<'ctx>,
ctx: &ModuleCtx<'ctx>,
locals: &HashMap<String, Value>,
block: &'parent Block<'ctx>,
stmt: &ReturnStmt,
Expand Down
4 changes: 2 additions & 2 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ SemiColon<T>: Vec<T> = { // (1)
}
};

Num: i32 = {
r"[0-9]+" => i32::from_str(<>).unwrap(),
Num: i64 = {
r"[0-9]+" => i64::from_str(<>).unwrap(),
};

Name: String = {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ fn main() {
let context = Context::new();
context.append_dialect_registry(&registry);
context.load_all_available_dialects();

let module = Module::new(Location::unknown(&context));
let ctx = ModuleCtx {
ctx: &context,
module: Module::new(Location::unknown(&context)),
module: &module,
};

compile_program(&ctx, &program, args.optlevel.into(), &args.output);
Expand Down

0 comments on commit 29e1a42

Please sign in to comment.