Skip to content

Commit

Permalink
Merge pull request #17 from lambdaclass/progress
Browse files Browse the repository at this point in the history
progress
  • Loading branch information
edg-l authored Dec 10, 2024
2 parents eec6cc1 + 16634f1 commit 546ac8d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion docs/src/mlir_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions docs/src/workshop_p3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
```
Expand Down
9 changes: 9 additions & 0 deletions docs/src/workshop_p7.md
Original file line number Diff line number Diff line change
@@ -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.
28 changes: 27 additions & 1 deletion docs/src/workshop_p8.md
Original file line number Diff line number Diff line change
@@ -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<String, Value> = HashMap::new();

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

// Create the func operation here.
}
```
4 changes: 4 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
pub enum Expr {
Number(i64),
Variable(String),
Call {
target: String,
args: Vec<Expr>,
},
Op(Box<Expr>, Opcode, Box<Expr>),
}

Expand Down
2 changes: 2 additions & 0 deletions src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
1 change: 1 addition & 0 deletions src/codegen/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
4 changes: 4 additions & 0 deletions src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Name: String = {
Term: Expr = {
Num => Expr::Number(<>),
<Name> => Expr::Variable(<>),
<target:Name> "(" <args:Comma<Expr>> ")" => Expr::Call {
target,
args
},
"(" <Expr> ")",
};

Expand Down

0 comments on commit 546ac8d

Please sign in to comment.