Skip to content

Commit b277564

Browse files
committed
feat: added lru MIR lowering
1 parent 541c99e commit b277564

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

compiler/astoir_mir/src/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ pub fn build_call(ctx: &mut MIRContext, func: usize, ind: usize, args: Vec<BaseM
434434

435435
for(arg, t) in args.iter().zip(func.arguments.iter()) {
436436
if !arg.vtype.is_truly_eq(t) {
437+
println!("{:#?} -> {:#?}", arg.vtype, t);
437438
unsure_panic!("invalid function argument types!");
438439
}
439440
}

compiler/astoir_mir_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod arrays;
1616
pub mod type_tools;
1717
pub mod introductions;
1818
pub mod casts;
19+
pub mod lru;
1920

2021
pub struct MIRLoweringContext {
2122
pub hir_ctx: HIRContext,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use astoir_hir::{nodes::{HIRNode, HIRNodeKind}, structs::StructLRUStep};
2+
use astoir_mir::{blocks::refer::MIRBlockReference, builder::build_field_pointer, vals::{base::BaseMIRValue, refer::MIRVariableReference}};
3+
use diagnostics::DiagnosticResult;
4+
5+
use crate::MIRLoweringContext;
6+
7+
pub fn lower_hir_lru_step(block: MIRBlockReference, step: StructLRUStep, ctx: &mut MIRLoweringContext, curr: Option<BaseMIRValue>) -> DiagnosticResult<BaseMIRValue> {
8+
if let StructLRUStep::VariableStep { variable } = step {
9+
if curr.is_none() {
10+
return Ok(ctx.mir_ctx.blocks[block].get_variable_ref(variable)?.as_pointer_ref()?.into());
11+
}
12+
13+
let ptr = curr.unwrap().as_ptr()?;
14+
15+
return Ok(build_field_pointer(&mut ctx.mir_ctx, ptr, variable)?.into())
16+
}
17+
18+
panic!("Invalid step!")
19+
}
20+
21+
pub fn lower_hir_lru(block: MIRBlockReference, node: Box<HIRNode>, ctx: &mut MIRLoweringContext) -> DiagnosticResult<BaseMIRValue> {
22+
if let HIRNodeKind::StructLRU { steps, last: _ } = node.kind {
23+
let mut curr = lower_hir_lru_step(block, steps[0].clone(), ctx, None)?;
24+
25+
for i in 1..steps.len() {
26+
curr = lower_hir_lru_step(block, steps[i].clone(), ctx, Some(curr))?
27+
}
28+
29+
let val = MIRVariableReference::from(curr.as_ptr()?);
30+
31+
return Ok(val.read(block, &mut ctx.mir_ctx)?);
32+
}
33+
34+
panic!("Invalid node!")
35+
}

compiler/astoir_mir_lowering/src/values/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use astoir_hir::nodes::{HIRNode, HIRNodeKind};
22
use astoir_mir::{blocks::refer::MIRBlockReference, builder::{build_static_array_const, build_static_array_one_const}, vals::base::BaseMIRValue};
33
use diagnostics::{DiagnosticResult, DiagnosticSpanOrigin, move_current_diagnostic_pos, unsure_panic};
44

5-
use crate::{MIRLoweringContext, arrays::lower_hir_aray_index_access, casts::lower_cast, funcs::lower_hir_function_call, math::lower_hir_math_operation, type_tools::{lower_hir_unwrap_cond, lower_hir_unwrap_value}, values::{booleans::{lower_hir_boolean_operator, lowering_hir_boolean_condition}, consts::lower_hir_literal, structs::lower_hir_struct_init}, vars::{lower_hir_variable_reference, lower_hir_variable_reference_value}};
5+
use crate::{MIRLoweringContext, arrays::lower_hir_aray_index_access, casts::lower_cast, funcs::lower_hir_function_call, lru::lower_hir_lru, math::lower_hir_math_operation, type_tools::{lower_hir_unwrap_cond, lower_hir_unwrap_value}, values::{booleans::{lower_hir_boolean_operator, lowering_hir_boolean_condition}, consts::lower_hir_literal, structs::lower_hir_struct_init}, vars::{lower_hir_variable_reference, lower_hir_variable_reference_value}};
66

77
pub mod consts;
88
pub mod booleans;
@@ -24,6 +24,7 @@ pub fn lower_hir_value(block: MIRBlockReference, node: Box<HIRNode>, ctx: &mut M
2424
HIRNodeKind::UnwrapValue { .. } => lower_hir_unwrap_value(block, node, ctx),
2525
HIRNodeKind::UnwrapCondition { .. } => lower_hir_unwrap_cond(block, node, ctx),
2626
HIRNodeKind::CastValue { .. } => lower_cast(block, node, ctx),
27+
HIRNodeKind::StructLRU { .. } => lower_hir_lru(block, node, ctx),
2728
HIRNodeKind::ArrayVariableInitializerValue { .. } | HIRNodeKind::ArrayVariableInitializerValueSameValue { .. } => lower_array_init(block, node, ctx),
2829
HIRNodeKind::FunctionCall { .. } => {
2930
let res = lower_hir_function_call(block, node, ctx)?;

0 commit comments

Comments
 (0)