From 5376149a4dba5968ce2310d1d589a4921561d0b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 7 May 2024 14:18:56 -0300 Subject: [PATCH 01/14] Add constant example --- examples/constants.con | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 examples/constants.con diff --git a/examples/constants.con b/examples/constants.con new file mode 100644 index 0000000..1c30cc9 --- /dev/null +++ b/examples/constants.con @@ -0,0 +1,9 @@ +mod Example { + const foo: i32 = 10; + const var: i32 = 5; + + fn main() -> i32 { + let vix: i32 = foo + 5; + return vix + var; + } +} From 3c826f51cebae8cbf0ad191c636ac00b9fe770e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 8 May 2024 18:40:54 -0300 Subject: [PATCH 02/14] Add lower constant layout --- crates/concrete_ir/src/lowering.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 6cf620e..6efae70 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use common::{BuildCtx, FnBodyBuilder, IdGenerator}; use concrete_ast::{ common::Span, + constants::ConstantDef, expressions::{ ArithOp, BinaryOp, BitwiseOp, CmpOp, Expression, FnCallOp, IfExpr, LogicOp, PathOp, PathSegment, ValueExpr, @@ -75,7 +76,9 @@ fn lower_module(mut ctx: BuildCtx, module: &Module, id: DefId) -> Result todo!(), + ModuleDefItem::Constant(info) => { + ctx = lower_constant(ctx, info, id)?; + } ModuleDefItem::Struct(info) => { ctx = lower_struct(ctx, info, id)?; } @@ -157,7 +160,7 @@ fn lower_module(mut ctx: BuildCtx, module: &Module, id: DefId) -> Result todo!(), + ModuleDefItem::Constant(_) => { /* already processed */ } ModuleDefItem::Function(fn_def) => { ctx = lower_func(ctx, fn_def, id)?; } @@ -179,6 +182,11 @@ fn lower_module(mut ctx: BuildCtx, module: &Module, id: DefId) -> Result Result { + let _ = (ctx, info, id); + todo!() +} + fn lower_struct( mut ctx: BuildCtx, info: &StructDecl, From 4dbf6fff516696a0aae9b4c65d1c10fd47509786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 8 May 2024 19:06:14 -0300 Subject: [PATCH 03/14] Add catching on UseOfUndeclaredVariable after lower_path This allows to look for defined constants if no path was found --- crates/concrete_ir/src/lowering.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 6efae70..ce5345d 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -1480,10 +1480,11 @@ fn lower_value_expr( (Rvalue::Use(Operand::Const(data)), ty) } ValueExpr::ConstStr(_, _) => todo!(), - ValueExpr::Path(info) => { - let (place, place_ty, _span) = lower_path(builder, info)?; - (Rvalue::Use(Operand::Place(place.clone())), place_ty) - } + ValueExpr::Path(info) => match lower_path(builder, info) { + Ok((place, place_ty, _span)) => (Rvalue::Use(Operand::Place(place.clone())), place_ty), + Err(err @ LoweringError::UseOfUndeclaredVariable { .. }) => return Err(err), + Err(err) => return Err(err), + }, }) } From fe17111942bc57fd683ebe1f516410fcbedff7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 11:20:42 -0300 Subject: [PATCH 04/14] Add ConstBody definition --- crates/concrete_ir/src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/concrete_ir/src/lib.rs b/crates/concrete_ir/src/lib.rs index 7be040f..75dc627 100644 --- a/crates/concrete_ir/src/lib.rs +++ b/crates/concrete_ir/src/lib.rs @@ -38,6 +38,7 @@ pub struct ProgramBody { /// This stores all the structs from all modules pub structs: BTreeMap, /// The function signatures. + pub constants: BTreeMap, pub function_signatures: HashMap, Ty)>, /// The file paths (program_id from the DefId) -> path. pub file_paths: HashMap, @@ -270,6 +271,13 @@ pub struct VariantDef { pub ty: Ty, } +#[derive(Debug, Clone)] +pub struct ConstBody { + pub id: DefId, + pub name: String, + pub value: ConstData, +} + // A definition id. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] pub struct DefId { From 3ffb7b9bd54b07a6689d9347b913074098c6ee16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 11:28:49 -0300 Subject: [PATCH 05/14] Add lower_constant implementation --- crates/concrete_ir/src/lowering.rs | 40 +++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index ce5345d..e88f9c0 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -17,10 +17,10 @@ use concrete_ast::{ }; use crate::{ - AdtBody, BasicBlock, BinOp, ConstData, ConstKind, ConstValue, DefId, FloatTy, FnBody, IntTy, - Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, ProgramBody, Rvalue, Statement, - StatementKind, SwitchTargets, Terminator, TerminatorKind, Ty, TyKind, UintTy, ValueTree, - VariantDef, + AdtBody, BasicBlock, BinOp, ConstBody, ConstData, ConstKind, ConstValue, DefId, FloatTy, + FnBody, IntTy, Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, ProgramBody, + Rvalue, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, Ty, TyKind, + UintTy, ValueTree, VariantDef, }; use self::errors::LoweringError; @@ -182,8 +182,36 @@ fn lower_module(mut ctx: BuildCtx, module: &Module, id: DefId) -> Result Result { - let _ = (ctx, info, id); +fn lower_constant( + mut ctx: BuildCtx, + info: &ConstantDef, + module_id: DefId, +) -> Result { + let name = info.decl.name.name.clone(); + + let id = { + let module = ctx + .body + .modules + .get(&module_id) + .expect("module should exist"); + *module + .symbols + .constants + .get(&name) + .expect("constant should exist") + }; + + let value = lower_constant_expression(&info.value)?; + + let body = ConstBody { id, name, value }; + + ctx.body.constants.insert(body.id, body); + + Ok(ctx) +} + +fn lower_constant_expression(value: &Expression) -> Result { todo!() } From 2f301186c5b5920d60a2ff69437f7fb2015381a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 11:45:37 -0300 Subject: [PATCH 06/14] Implement dummy lower_constant_expression --- crates/concrete_ir/src/lowering.rs | 31 +++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index e88f9c0..3410c6b 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -202,7 +202,9 @@ fn lower_constant( .expect("constant should exist") }; - let value = lower_constant_expression(&info.value)?; + let value_ty = lower_type(&ctx, &info.decl.r#type, module_id)?; + + let value = lower_constant_expression(&info.value, value_ty)?; let body = ConstBody { id, name, value }; @@ -211,8 +213,31 @@ fn lower_constant( Ok(ctx) } -fn lower_constant_expression(value: &Expression) -> Result { - todo!() +fn lower_constant_expression( + expression: &Expression, + type_hint: Ty, +) -> Result { + match expression { + Expression::Value(value, _) => match value { + ValueExpr::ConstInt(int, _) => match type_hint.kind { + TyKind::Int(int_ty) => match int_ty { + int_ty @ IntTy::I32 => Ok(ConstData { + ty: Ty { + span: None, + kind: TyKind::Int(int_ty), + }, + data: ConstKind::Value(ValueTree::Leaf(ConstValue::I32( + (*int).try_into().expect("value out of range"), + ))), + }), + _ => unimplemented!(), + }, + _ => unimplemented!(), + }, + _ => unimplemented!(), + }, + _ => unimplemented!(), + } } fn lower_struct( From 331166778e96279cb7580c1c425b121b5ac76bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 12:01:09 -0300 Subject: [PATCH 07/14] Add reference to constant in function body --- crates/concrete_ir/src/lowering.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 3410c6b..d3683f0 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -1535,7 +1535,29 @@ fn lower_value_expr( ValueExpr::ConstStr(_, _) => todo!(), ValueExpr::Path(info) => match lower_path(builder, info) { Ok((place, place_ty, _span)) => (Rvalue::Use(Operand::Place(place.clone())), place_ty), - Err(err @ LoweringError::UseOfUndeclaredVariable { .. }) => return Err(err), + Err(err @ LoweringError::UseOfUndeclaredVariable { .. }) => { + let mod_body = builder.get_module_body(); + + let constant_id = + if let Some(constant_id) = mod_body.symbols.constants.get(&info.first.name) { + *constant_id + } else { + return Err(err); + }; + + let constant_value = builder + .ctx + .body + .constants + .get(&constant_id) + .expect("constant should exist") + .value + .clone(); + + let ty = constant_value.ty.clone(); + + (Rvalue::Use(Operand::Const(constant_value)), ty) + } Err(err) => return Err(err), }, }) From 16506215c8a52dcfc28ddceada1c87f85ca85596 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 12:20:24 -0300 Subject: [PATCH 08/14] Add support for multiple types in constant literals --- crates/concrete_ir/src/lowering.rs | 77 ++++++++++++++++++++++++------ examples/constants.con | 4 +- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index d3683f0..f27aacb 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -219,21 +219,68 @@ fn lower_constant_expression( ) -> Result { match expression { Expression::Value(value, _) => match value { - ValueExpr::ConstInt(int, _) => match type_hint.kind { - TyKind::Int(int_ty) => match int_ty { - int_ty @ IntTy::I32 => Ok(ConstData { - ty: Ty { - span: None, - kind: TyKind::Int(int_ty), - }, - data: ConstKind::Value(ValueTree::Leaf(ConstValue::I32( - (*int).try_into().expect("value out of range"), - ))), - }), - _ => unimplemented!(), - }, - _ => unimplemented!(), - }, + ValueExpr::ConstBool(value, _) => Ok(ConstData { + ty: type_hint.clone(), + data: ConstKind::Value(ValueTree::Leaf(ConstValue::Bool(*value))), + }), + ValueExpr::ConstChar(value, _) => Ok(ConstData { + ty: type_hint.clone(), + data: ConstKind::Value(ValueTree::Leaf(ConstValue::U32((*value) as u32))), + }), + ValueExpr::ConstInt(value, _) => Ok(ConstData { + ty: type_hint.clone(), + data: ConstKind::Value(ValueTree::Leaf(match type_hint.kind { + TyKind::Int(ty) => match ty { + IntTy::I8 => { + ConstValue::I8((*value).try_into().expect("value out of range")) + } + IntTy::I16 => { + ConstValue::I16((*value).try_into().expect("value out of range")) + } + IntTy::I32 => { + ConstValue::I32((*value).try_into().expect("value out of range")) + } + IntTy::I64 => { + ConstValue::I64((*value).try_into().expect("value out of range")) + } + IntTy::I128 => { + ConstValue::I128((*value).try_into().expect("value out of range")) + } + }, + TyKind::Uint(ty) => match ty { + UintTy::U8 => { + ConstValue::U8((*value).try_into().expect("value out of range")) + } + UintTy::U16 => { + ConstValue::U16((*value).try_into().expect("value out of range")) + } + UintTy::U32 => { + ConstValue::U32((*value).try_into().expect("value out of range")) + } + UintTy::U64 => { + ConstValue::U64((*value).try_into().expect("value out of range")) + } + UintTy::U128 => ConstValue::U128(*value), + }, + TyKind::Bool => ConstValue::Bool(*value != 0), + x => unreachable!("{:?}", x), + })), + }), + ValueExpr::ConstFloat(value, _) => Ok(ConstData { + ty: type_hint.clone(), + data: ConstKind::Value(ValueTree::Leaf(match &type_hint.kind { + TyKind::Float(ty) => match ty { + FloatTy::F32 => { + ConstValue::F32(value.parse().expect("error parsing float")) + } + FloatTy::F64 => { + ConstValue::F64(value.parse().expect("error parsing float")) + } + }, + _ => unreachable!(), + })), + }), + ValueExpr::ConstStr(_, _) => todo!(), _ => unimplemented!(), }, _ => unimplemented!(), diff --git a/examples/constants.con b/examples/constants.con index 1c30cc9..44dd6f0 100644 --- a/examples/constants.con +++ b/examples/constants.con @@ -1,9 +1,9 @@ mod Example { const foo: i32 = 10; - const var: i32 = 5; + const var: i64 = 5; fn main() -> i32 { let vix: i32 = foo + 5; - return vix + var; + return vix + (var as i32); } } From 7ad235eb75f2fa0e0146d2826c92096ccf1eec37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 13:29:26 -0300 Subject: [PATCH 09/14] Refactor path lowering --- crates/concrete_ir/src/lowering.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index f27aacb..d47d847 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -1580,16 +1580,22 @@ fn lower_value_expr( (Rvalue::Use(Operand::Const(data)), ty) } ValueExpr::ConstStr(_, _) => todo!(), - ValueExpr::Path(info) => match lower_path(builder, info) { - Ok((place, place_ty, _span)) => (Rvalue::Use(Operand::Place(place.clone())), place_ty), - Err(err @ LoweringError::UseOfUndeclaredVariable { .. }) => { + ValueExpr::Path(info) => { + if builder.name_to_local.contains_key(&info.first.name) { + let (place, place_ty, _span) = lower_path(builder, info)?; + (Rvalue::Use(Operand::Place(place.clone())), place_ty) + } else { let mod_body = builder.get_module_body(); let constant_id = if let Some(constant_id) = mod_body.symbols.constants.get(&info.first.name) { *constant_id } else { - return Err(err); + return Err(LoweringError::UseOfUndeclaredVariable { + span: info.span, + name: info.first.name.clone(), + program_id: builder.local_module.program_id, + }); }; let constant_value = builder @@ -1605,8 +1611,7 @@ fn lower_value_expr( (Rvalue::Use(Operand::Const(constant_value)), ty) } - Err(err) => return Err(err), - }, + } }) } From 9e67a1a84936000fe4340af6171f5ce7a801a431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 13:39:58 -0300 Subject: [PATCH 10/14] Refactor lower_constant_expression --- crates/concrete_ir/src/lowering.rs | 111 ++++++++++++----------------- 1 file changed, 44 insertions(+), 67 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index d47d847..de86dd6 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -213,78 +213,55 @@ fn lower_constant( Ok(ctx) } -fn lower_constant_expression( - expression: &Expression, - type_hint: Ty, -) -> Result { - match expression { +fn lower_constant_expression(expression: &Expression, ty: Ty) -> Result { + let data = match expression { Expression::Value(value, _) => match value { - ValueExpr::ConstBool(value, _) => Ok(ConstData { - ty: type_hint.clone(), - data: ConstKind::Value(ValueTree::Leaf(ConstValue::Bool(*value))), - }), - ValueExpr::ConstChar(value, _) => Ok(ConstData { - ty: type_hint.clone(), - data: ConstKind::Value(ValueTree::Leaf(ConstValue::U32((*value) as u32))), - }), - ValueExpr::ConstInt(value, _) => Ok(ConstData { - ty: type_hint.clone(), - data: ConstKind::Value(ValueTree::Leaf(match type_hint.kind { - TyKind::Int(ty) => match ty { - IntTy::I8 => { - ConstValue::I8((*value).try_into().expect("value out of range")) - } - IntTy::I16 => { - ConstValue::I16((*value).try_into().expect("value out of range")) - } - IntTy::I32 => { - ConstValue::I32((*value).try_into().expect("value out of range")) - } - IntTy::I64 => { - ConstValue::I64((*value).try_into().expect("value out of range")) - } - IntTy::I128 => { - ConstValue::I128((*value).try_into().expect("value out of range")) - } - }, - TyKind::Uint(ty) => match ty { - UintTy::U8 => { - ConstValue::U8((*value).try_into().expect("value out of range")) - } - UintTy::U16 => { - ConstValue::U16((*value).try_into().expect("value out of range")) - } - UintTy::U32 => { - ConstValue::U32((*value).try_into().expect("value out of range")) - } - UintTy::U64 => { - ConstValue::U64((*value).try_into().expect("value out of range")) - } - UintTy::U128 => ConstValue::U128(*value), - }, - TyKind::Bool => ConstValue::Bool(*value != 0), - x => unreachable!("{:?}", x), - })), - }), - ValueExpr::ConstFloat(value, _) => Ok(ConstData { - ty: type_hint.clone(), - data: ConstKind::Value(ValueTree::Leaf(match &type_hint.kind { - TyKind::Float(ty) => match ty { - FloatTy::F32 => { - ConstValue::F32(value.parse().expect("error parsing float")) - } - FloatTy::F64 => { - ConstValue::F64(value.parse().expect("error parsing float")) - } - }, - _ => unreachable!(), - })), - }), + ValueExpr::ConstBool(value, _) => { + ConstKind::Value(ValueTree::Leaf(ConstValue::Bool(*value))) + } + ValueExpr::ConstChar(value, _) => { + ConstKind::Value(ValueTree::Leaf(ConstValue::U32((*value) as u32))) + } + ValueExpr::ConstInt(value, _) => ConstKind::Value(ValueTree::Leaf(match ty.kind { + TyKind::Int(ty) => match ty { + IntTy::I8 => ConstValue::I8((*value).try_into().expect("value out of range")), + IntTy::I16 => ConstValue::I16((*value).try_into().expect("value out of range")), + IntTy::I32 => ConstValue::I32((*value).try_into().expect("value out of range")), + IntTy::I64 => ConstValue::I64((*value).try_into().expect("value out of range")), + IntTy::I128 => { + ConstValue::I128((*value).try_into().expect("value out of range")) + } + }, + TyKind::Uint(ty) => match ty { + UintTy::U8 => ConstValue::U8((*value).try_into().expect("value out of range")), + UintTy::U16 => { + ConstValue::U16((*value).try_into().expect("value out of range")) + } + UintTy::U32 => { + ConstValue::U32((*value).try_into().expect("value out of range")) + } + UintTy::U64 => { + ConstValue::U64((*value).try_into().expect("value out of range")) + } + UintTy::U128 => ConstValue::U128(*value), + }, + TyKind::Bool => ConstValue::Bool(*value != 0), + x => unreachable!("{:?}", x), + })), + ValueExpr::ConstFloat(value, _) => ConstKind::Value(ValueTree::Leaf(match &ty.kind { + TyKind::Float(ty) => match ty { + FloatTy::F32 => ConstValue::F32(value.parse().expect("error parsing float")), + FloatTy::F64 => ConstValue::F64(value.parse().expect("error parsing float")), + }, + x => unreachable!("{:?}", x), + })), ValueExpr::ConstStr(_, _) => todo!(), _ => unimplemented!(), }, _ => unimplemented!(), - } + }; + + Ok(ConstData { ty, data }) } fn lower_struct( From cbfc4d0831bb9e6e4d990cb848c48250611f60b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 16:17:26 -0300 Subject: [PATCH 11/14] Add constants example to test --- crates/concrete_driver/tests/examples.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/concrete_driver/tests/examples.rs b/crates/concrete_driver/tests/examples.rs index 455f58e..57267c8 100644 --- a/crates/concrete_driver/tests/examples.rs +++ b/crates/concrete_driver/tests/examples.rs @@ -21,6 +21,7 @@ mod common; #[test_case(include_str!("../../../examples/for.con"), "for", false, 10 ; "for.con")] #[test_case(include_str!("../../../examples/for_while.con"), "for_while", false, 10 ; "for_while.con")] #[test_case(include_str!("../../../examples/arrays.con"), "arrays", false, 5 ; "arrays.con")] +#[test_case(include_str!("../../../examples/constants.con"), "constants", false, 20 ; "constants.con")] fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) { assert_eq!( status_code, From 1da68767ad9db374df791af3b2790c877d2c0c6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 16:17:41 -0300 Subject: [PATCH 12/14] Simplify error handling --- crates/concrete_ir/src/lowering.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index de86dd6..18d9ccd 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -1564,16 +1564,13 @@ fn lower_value_expr( } else { let mod_body = builder.get_module_body(); - let constant_id = - if let Some(constant_id) = mod_body.symbols.constants.get(&info.first.name) { - *constant_id - } else { - return Err(LoweringError::UseOfUndeclaredVariable { - span: info.span, - name: info.first.name.clone(), - program_id: builder.local_module.program_id, - }); - }; + let Some(&constant_id) = mod_body.symbols.constants.get(&info.first.name) else { + return Err(LoweringError::UseOfUndeclaredVariable { + span: info.span, + name: info.first.name.clone(), + program_id: builder.local_module.program_id, + }); + }; let constant_value = builder .ctx From 45c0b18e53783ee9045ffce91efbcb1458478946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 9 May 2024 16:21:01 -0300 Subject: [PATCH 13/14] Fix imports --- crates/concrete_ir/src/lowering.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 3797c14..c6f2ee1 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -17,8 +17,8 @@ use concrete_ast::{ }; use crate::{ - AdtBody, BasicBlock, BinOp, ConcreteIntrinsic, ConstData, ConstKind, ConstValue, DefId, - FloatTy, FnBody, IntTy, Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, + AdtBody, BasicBlock, BinOp, ConcreteIntrinsic, ConstBody, ConstData, ConstKind, ConstValue, + DefId, FloatTy, FnBody, IntTy, Local, LocalKind, LogOp, Mutability, Operand, Place, PlaceElem, ProgramBody, Rvalue, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, Ty, TyKind, UintTy, ValueTree, VariantDef, }; From 350d3f696abc1fe1c5790aa0d346bcb816b99524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 10 May 2024 11:36:32 -0300 Subject: [PATCH 14/14] Abstract to function lower_constant_ref --- crates/concrete_ir/src/lowering.rs | 50 +++++++++++++++++------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 0c8d9b3..6dabc96 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -1636,33 +1636,41 @@ fn lower_value_expr( let (place, place_ty, _span) = lower_path(builder, info)?; (Rvalue::Use(Operand::Place(place.clone())), place_ty) } else { - let mod_body = builder.get_module_body(); - - let Some(&constant_id) = mod_body.symbols.constants.get(&info.first.name) else { - return Err(LoweringError::UseOfUndeclaredVariable { - span: info.span, - name: info.first.name.clone(), - program_id: builder.local_module.program_id, - }); - }; - - let constant_value = builder - .ctx - .body - .constants - .get(&constant_id) - .expect("constant should exist") - .value - .clone(); - - let ty = constant_value.ty.clone(); - + let (constant_value, ty) = lower_constant_ref(builder, info)?; (Rvalue::Use(Operand::Const(constant_value)), ty) } } }) } +fn lower_constant_ref( + builder: &mut FnBodyBuilder, + info: &PathOp, +) -> Result<(ConstData, Ty), LoweringError> { + let mod_body = builder.get_module_body(); + + let Some(&constant_id) = mod_body.symbols.constants.get(&info.first.name) else { + return Err(LoweringError::UseOfUndeclaredVariable { + span: info.span, + name: info.first.name.clone(), + program_id: builder.local_module.program_id, + }); + }; + + let constant_value = builder + .ctx + .body + .constants + .get(&constant_id) + .expect("constant should exist") + .value + .clone(); + + let ty = constant_value.ty.clone(); + + Ok((constant_value, ty)) +} + pub fn lower_path( builder: &mut FnBodyBuilder, info: &PathOp,