diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 10a9435..4aceebf 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -9,4 +9,4 @@ - [Workshop: Compiling Return](./workshop_p5.md) - [Workshop: Compiling If/Else](./workshop_p6.md) - [Workshop: Compiling Function calls](./workshop_p7.md) -- [Workshop: Extras](./workshop_p8.md) +- [Workshop: Compiling Functions](./workshop_p8.md) diff --git a/docs/src/mlir_basics.md b/docs/src/mlir_basics.md index 6d21243..1ca9f16 100644 --- a/docs/src/mlir_basics.md +++ b/docs/src/mlir_basics.md @@ -126,7 +126,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 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. +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 an 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(); diff --git a/docs/src/workshop_p3.md b/docs/src/workshop_p3.md index e34b6d6..5eb0bd8 100644 --- a/docs/src/workshop_p3.md +++ b/docs/src/workshop_p3.md @@ -34,6 +34,7 @@ pub fn compile_expr<'ctx: 'parent, 'parent>( Opcode::Eq => todo!("implement eq"), Opcode::Neq => todo!("implement neq"), }, + Expr::Call { target, args } => todo!("implement function call"), } } ``` diff --git a/docs/src/workshop_p7.md b/docs/src/workshop_p7.md index 6584552..1ee817b 100644 --- a/docs/src/workshop_p7.md +++ b/docs/src/workshop_p7.md @@ -1 +1,10 @@ # Workshop: Compiling Function calls + + +```rust +// src/codegen/expressions.rs +Expr::Call { target, args } => todo!("implement function call"), +``` + +Since all arguments are of the same type, and for simplicity +sake we don't verify the number of arguments matches the function this should be relatively simple using the `func` dialect. diff --git a/docs/src/workshop_p8.md b/docs/src/workshop_p8.md index 507c519..6327fb2 100644 --- a/docs/src/workshop_p8.md +++ b/docs/src/workshop_p8.md @@ -1 +1,27 @@ -# Workshop: Extras +# Workshop: Compiling Functions + +Now to wrap up the function itself needs to be created, using the `func` dialect and adding it to the `module` `body()` block. (The module is available under the ctx variable.) + +```rust +// src/codegen.rs:60+ +fn compile_function(ctx: &ModuleCtx<'_>, func: &Function) { + let mut args: Vec<(Type, Location)> = vec![]; + + for _ in &func.args { + args.push(( + IntegerType::new(ctx.ctx, 64).into(), + Location::unknown(ctx.ctx), + )); + } + + let region = Region::new(); + let block = region.append_block(Block::new(&args)); + let mut locals: HashMap = HashMap::new(); + + for stmt in &func.body.stmts { + compile_statement(ctx, &mut locals, &block, stmt); + } + + // Create the func operation here. +} +``` diff --git a/src/ast.rs b/src/ast.rs index 0f55e0e..b5acec3 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -4,6 +4,10 @@ pub enum Expr { Number(i64), Variable(String), + Call { + target: String, + args: Vec, + }, Op(Box, Opcode, Box), } diff --git a/src/codegen.rs b/src/codegen.rs index 3988042..8faa7b8 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -77,6 +77,8 @@ fn compile_function(ctx: &ModuleCtx<'_>, func: &Function) { for stmt in &func.body.stmts { compile_statement(ctx, &mut locals, &block, stmt); } + + // Create the func operation here. } fn compile_statement<'ctx: 'parent, 'parent>( diff --git a/src/codegen/expressions.rs b/src/codegen/expressions.rs index 33bd41d..bfd564c 100644 --- a/src/codegen/expressions.rs +++ b/src/codegen/expressions.rs @@ -36,5 +36,6 @@ pub fn compile_expr<'ctx: 'parent, 'parent>( Opcode::Eq => todo!("implement eq"), Opcode::Neq => todo!("implement neq"), }, + Expr::Call { target, args } => todo!("implement function call"), } } diff --git a/src/grammar.lalrpop b/src/grammar.lalrpop index 9143d05..9ad9620 100644 --- a/src/grammar.lalrpop +++ b/src/grammar.lalrpop @@ -34,6 +34,10 @@ Name: String = { Term: Expr = { Num => Expr::Number(<>), => Expr::Variable(<>), + "(" > ")" => Expr::Call { + target, + args + }, "(" ")", };