Skip to content

Commit 1a77fbc

Browse files
committed
fix: fixed MIR lowering layer
1 parent 586eab1 commit 1a77fbc

6 files changed

Lines changed: 152 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/astoir_mir_lowering/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
astoir_mir = {path = "../astoir_mir"}
8-
astoir_hir = {path = "../astoir_hir"}
7+
astoir_mir = { path = "../astoir_mir" }
8+
astoir_hir = { path = "../astoir_hir" }
99
diagnostics = { path = "../diagnostics" }
1010
compiler_typing = { path = "../compiler_typing" }
11-
compiler_utils = { path = "../compiler_utils" }
11+
compiler_utils = { path = "../compiler_utils" }
12+
compiler_global_scope = { path = "../compiler_global_scope" }

compiler/astoir_mir_lowering/src/funcs.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use astoir_mir::{
55
funcs::MIRFunction,
66
vals::base::BaseMIRValue,
77
};
8-
use diagnostics::DiagnosticResult;
8+
use compiler_typing::TypedGlobalScopeEntry;
9+
use diagnostics::{DiagnosticResult, builders::make_expected_simple_error_originless};
910

1011
use crate::{MIRLoweringContext, body::lower_hir_body, lower_hir_type, values::lower_hir_value};
1112

@@ -36,7 +37,30 @@ pub fn lower_hir_function_decl(
3637
ret_type = None
3738
}
3839

39-
let name = cctx.hir_ctx.functions.vals[func_name].2.clone();
40+
let fns_ind = match &cctx.hir_ctx.global_scope.scope.entries[func_name].entry_type {
41+
TypedGlobalScopeEntry::Function {
42+
descriptor_ind,
43+
impl_ind: _,
44+
} => descriptor_ind,
45+
TypedGlobalScopeEntry::ImplLessFunction(ind) => ind,
46+
TypedGlobalScopeEntry::StructFunction {
47+
descriptor_ind,
48+
impl_ind: _,
49+
struct_type: _,
50+
} => descriptor_ind,
51+
52+
_ => {
53+
return Err(make_expected_simple_error_originless(
54+
&"function".to_string(),
55+
&cctx.hir_ctx.global_scope.scope.entries[func_name].entry_type,
56+
)
57+
.into());
58+
}
59+
};
60+
61+
let fns = cctx.hir_ctx.global_scope.descriptors[*fns_ind].clone();
62+
63+
let name = fns.2.clone();
4064

4165
let mut func = MIRFunction::new(name, args, ret_type, requires_this, &cctx.mir_ctx);
4266
let block = func.append_entry_block(&mut cctx.mir_ctx);
@@ -69,7 +93,30 @@ pub fn lower_hir_shadow_decl(
6993
return_type,
7094
} = node.kind.clone()
7195
{
72-
let name = ctx.hir_ctx.functions.vals[func_name].2.clone();
96+
let fns_ind = match &ctx.hir_ctx.global_scope.scope.entries[func_name].entry_type {
97+
TypedGlobalScopeEntry::Function {
98+
descriptor_ind,
99+
impl_ind: _,
100+
} => descriptor_ind,
101+
TypedGlobalScopeEntry::ImplLessFunction(ind) => ind,
102+
TypedGlobalScopeEntry::StructFunction {
103+
descriptor_ind,
104+
impl_ind: _,
105+
struct_type: _,
106+
} => descriptor_ind,
107+
108+
_ => {
109+
return Err(make_expected_simple_error_originless(
110+
&"function".to_string(),
111+
&ctx.hir_ctx.global_scope.scope.entries[func_name].entry_type,
112+
)
113+
.into());
114+
}
115+
};
116+
117+
let fns = ctx.hir_ctx.global_scope.descriptors[*fns_ind].clone();
118+
119+
let name = fns.2.clone();
73120

74121
let mut args = vec![];
75122

compiler/astoir_mir_lowering/src/introductions.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use astoir_mir::{
33
blocks::{MIRBlockVariableSSAHint, MIRBlockVariableType, refer::MIRBlockReference},
44
builder::{build_stack_alloc, build_store},
55
};
6-
use compiler_typing::SizedType;
7-
use diagnostics::MaybeDiagnostic;
6+
use compiler_typing::{SizedType, TypedGlobalScopeEntry};
7+
use diagnostics::{MaybeDiagnostic, builders::make_expected_simple_error_originless};
88

99
use crate::{
1010
MIRLoweringContext, lower_hir_type, type_tools::cast_to_enum_child, values::lower_hir_value,
@@ -26,10 +26,30 @@ pub fn handle_var_introduction_queue(
2626
let new_type = lower_hir_type(ctx, new_type)?;
2727
let func = ctx.mir_ctx.block_to_func[&block];
2828
let new_var = new_var.unwrap();
29-
let eligible = ctx.hir_ctx.function_contexts[func]
30-
.as_ref()
31-
.unwrap()
32-
.is_eligible_for_ssa(new_var);
29+
30+
let fns_ind = match &ctx.hir_ctx.global_scope.scope.entries[func].entry_type {
31+
TypedGlobalScopeEntry::Function {
32+
descriptor_ind: _,
33+
impl_ind,
34+
} => impl_ind,
35+
TypedGlobalScopeEntry::StructFunction {
36+
descriptor_ind: _,
37+
impl_ind,
38+
struct_type: _,
39+
} => impl_ind,
40+
41+
_ => {
42+
return Err(make_expected_simple_error_originless(
43+
&"function".to_string(),
44+
&ctx.hir_ctx.global_scope.scope.entries[func].entry_type,
45+
)
46+
.into());
47+
}
48+
};
49+
50+
let fns = &ctx.hir_ctx.global_scope.implementations[*fns_ind].0;
51+
52+
let eligible = fns.is_eligible_for_ssa(new_var);
3353

3454
let casted = cast_to_enum_child(block, original, new_type.as_generic(), ctx, &*node)?;
3555

compiler/astoir_mir_lowering/src/lib.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use astoir_hir::{
44
ctx::HIRContext,
55
nodes::{HIRNode, HIRNodeKind},
66
};
7-
use astoir_mir::ctx::MIRContext;
8-
use compiler_typing::{SizedType, raw::RawType, structs::LoweredStructTypeContainer, tree::Type};
7+
use astoir_mir::{ctx::MIRContext, funcs::MIRFunction};
8+
use compiler_typing::{
9+
SizedType, TypedGlobalScopeEntry, raw::RawType, structs::LoweredStructTypeContainer, tree::Type,
10+
};
911
use compiler_utils::utils::indexed::IndexStorage;
1012
use diagnostics::{DiagnosticResult, unsure_panic};
1113

@@ -53,12 +55,49 @@ pub fn lower_hir(ctx: HIRContext) -> DiagnosticResult<MIRContext> {
5355
block_introduction_var_queue: vec![],
5456
};
5557

56-
let declarations = lowering_ctx.hir_ctx.function_declarations.clone();
58+
for entry in lowering_ctx.hir_ctx.global_scope.scope.entries.clone() {
59+
match entry.entry_type {
60+
TypedGlobalScopeEntry::Function {
61+
descriptor_ind: _,
62+
impl_ind,
63+
} => {
64+
let node = lowering_ctx.hir_ctx.global_scope.implementations[impl_ind]
65+
.1
66+
.clone();
67+
68+
lower_hir_top_level(node, &mut lowering_ctx)?;
69+
}
5770

58-
for decl in declarations {
59-
if let Some(node) = decl {
60-
lower_hir_top_level(node, &mut lowering_ctx)?;
61-
}
71+
TypedGlobalScopeEntry::ImplLessFunction(descriptor_ind) => {
72+
let descriptor =
73+
lowering_ctx.hir_ctx.global_scope.descriptors[descriptor_ind].clone();
74+
75+
let name = descriptor.2.clone();
76+
77+
let mut args = vec![];
78+
79+
for argument in descriptor.1 {
80+
args.push(lower_hir_type(&mut lowering_ctx, argument.1)?);
81+
}
82+
83+
let ret_type;
84+
85+
if descriptor.0.is_some() {
86+
ret_type = Some(lower_hir_type(
87+
&mut lowering_ctx,
88+
descriptor.0.clone().unwrap(),
89+
)?)
90+
} else {
91+
ret_type = None
92+
}
93+
94+
let func = MIRFunction::new(name, args, ret_type, false, &mut lowering_ctx.mir_ctx);
95+
96+
lowering_ctx.mir_ctx.append_function(func);
97+
}
98+
99+
_ => todo!("Add support for remaining nodes"),
100+
};
62101
}
63102

64103
return Ok(lowering_ctx.mir_ctx);

compiler/astoir_mir_lowering/src/vars.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use astoir_mir::{
66
builder::{build_stack_alloc, build_store},
77
vals::{base::BaseMIRValue, refer::MIRVariableReference},
88
};
9-
use compiler_typing::SizedType;
10-
use diagnostics::DiagnosticResult;
9+
use compiler_typing::{SizedType, TypedGlobalScopeEntry};
10+
use diagnostics::{DiagnosticResult, builders::make_expected_simple_error_originless};
1111

1212
use crate::{MIRLoweringContext, lower_hir_type, values::lower_hir_value};
1313

@@ -24,7 +24,29 @@ pub fn lower_hir_variable_declaration(
2424
{
2525
let func = ctx.mir_ctx.block_to_func[&block_id];
2626

27-
let local_ctx = ctx.hir_ctx.function_contexts[func].as_ref().unwrap();
27+
//let local_ctx = ctx.hir_ctx.function_contexts[func].as_ref().unwrap();
28+
29+
let fns_ind = match &ctx.hir_ctx.global_scope.scope.entries[func].entry_type {
30+
TypedGlobalScopeEntry::Function {
31+
descriptor_ind,
32+
impl_ind,
33+
} => impl_ind,
34+
TypedGlobalScopeEntry::StructFunction {
35+
descriptor_ind,
36+
impl_ind,
37+
struct_type,
38+
} => impl_ind,
39+
40+
_ => {
41+
return Err(make_expected_simple_error_originless(
42+
&"function".to_string(),
43+
&ctx.hir_ctx.global_scope.scope.entries[func].entry_type,
44+
)
45+
.into());
46+
}
47+
};
48+
49+
let local_ctx = ctx.hir_ctx.global_scope.implementations[*fns_ind].0.clone();
2850

2951
if local_ctx.is_eligible_for_ssa(variable) {
3052
if default_val.is_some() {

0 commit comments

Comments
 (0)