From 3a68154ae5a3620cc372143bd93510c5189043d8 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 23 Jul 2025 15:12:21 +0300 Subject: [PATCH] Add codegen for div and mod --- reid/src/codegen.rs | 58 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index ee84f13..dce8b5d 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -659,17 +659,57 @@ impl mir::Expression { (mir::BinaryOperator::And, _, _) => Instr::And(lhs, rhs), (mir::BinaryOperator::Cmp(i), _, false) => Instr::ICmp(i.predicate(), lhs, rhs), (mir::BinaryOperator::Cmp(i), _, true) => Instr::FCmp(i.predicate(), lhs, rhs), - (mir::BinaryOperator::Div, true, true) => todo!(), - (mir::BinaryOperator::Div, true, false) => todo!(), - (mir::BinaryOperator::Div, false, true) => todo!(), - (mir::BinaryOperator::Div, false, false) => todo!(), - (mir::BinaryOperator::Mod, true, true) => todo!(), - (mir::BinaryOperator::Mod, true, false) => todo!(), - (mir::BinaryOperator::Mod, false, true) => todo!(), - (mir::BinaryOperator::Mod, false, false) => todo!(), + (mir::BinaryOperator::Div, false, false) => Instr::UDiv(lhs, rhs), + (mir::BinaryOperator::Div, true, false) => Instr::SDiv(lhs, rhs), + (mir::BinaryOperator::Div, _, true) => Instr::FDiv(lhs, rhs), + (mir::BinaryOperator::Mod, false, false) => { + let div = scope + .block + .build(Instr::UDiv(lhs, rhs)) + .unwrap() + .maybe_location(&mut scope.block, location); + let mul = scope + .block + .build(Instr::Mul(rhs, div)) + .unwrap() + .maybe_location(&mut scope.block, location); + Instr::Sub(lhs, mul) + } + (mir::BinaryOperator::Mod, true, false) => { + let div = scope + .block + .build(Instr::SDiv(lhs, rhs)) + .unwrap() + .maybe_location(&mut scope.block, location); + let mul = scope + .block + .build(Instr::Mul(rhs, div)) + .unwrap() + .maybe_location(&mut scope.block, location); + Instr::Sub(lhs, mul) + } + (mir::BinaryOperator::Mod, _, true) => { + let div = scope + .block + .build(Instr::FDiv(lhs, rhs)) + .unwrap() + .maybe_location(&mut scope.block, location); + let mul = scope + .block + .build(Instr::Mul(rhs, div)) + .unwrap() + .maybe_location(&mut scope.block, location); + Instr::Sub(lhs, mul) + } }; Some(StackValue( - StackValueKind::Immutable(scope.block.build(instr).unwrap()), + StackValueKind::Immutable( + scope + .block + .build(instr) + .unwrap() + .maybe_location(&mut scope.block, location), + ), lhs_type, )) }