From 9eb2dd8057ed4eb88c700d46ca2769a29e0fa5c6 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 19:01:24 -0300 Subject: [PATCH] Add lower constant dummy impl --- crates/concrete_ir/src/lowering.rs | 78 +++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 6 deletions(-) diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index 748a7b5..898792c 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, ConstData, ConstKind, ConstValue, ConstantBody, 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,74 @@ fn lower_module(mut ctx: BuildCtx, module: &Module, id: DefId) -> Result Result { - todo!() +fn lower_constant( + mut ctx: BuildCtx, + constant: &ConstantDef, + module_id: DefId, +) -> Result { + let mut body = ConstantBody { + id: { + let body = ctx.body.modules.get(&module_id).unwrap(); + *body + .symbols + .constants + .get(&constant.decl.name.name) + .unwrap() + }, + name: constant.decl.name.name.clone(), + basic_block: BasicBlock { + statements: Vec::new(), + terminator: Box::new(Terminator { + span: None, + kind: TerminatorKind::Return, + }), + }, + locals: Vec::new(), + }; + + let (value, value_ty) = match &constant.value { + Expression::Value(value, _) => match value { + ValueExpr::ConstInt(number, _) => ( + Rvalue::Use(Operand::Const(ConstData { + ty: Ty { + span: None, + kind: TyKind::Int(IntTy::I32), + }, + data: ConstKind::Value(ValueTree::Leaf(ConstValue::I32( + (*number).try_into().expect("value out of range"), + ))), + })), + Ty { + span: None, + kind: TyKind::Int(IntTy::I32), + }, + ), + _ => unreachable!(), + }, + _ => unreachable!(), + }; + + body.locals.push(Local { + span: None, + debug_name: None, + ty: value_ty, + kind: LocalKind::ReturnPointer, + }); + + body.basic_block.statements.push(Statement { + span: None, + kind: StatementKind::Assign( + Place { + local: 0, + projection: Vec::new(), + }, + value, + ), + }); + + ctx.body.constants.insert(body.id, body); + + Ok(ctx) } fn lower_struct(