Skip to content

Commit fcf5366

Browse files
committed
feat: added mod operator
1 parent a9223e7 commit fcf5366

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

compiler/astoir_mir/src/builder.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ pub fn build_int_div(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue
121121
return res.as_int();
122122
}
123123

124+
pub fn build_int_mod(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
125+
if left.size != right.size {
126+
unsure_panic!("Tried using imod on different sized integers");
127+
}
128+
129+
let res = ctx.append_inst(MIRInstruction::IntegerMod { signed, left, right }).get()?;
130+
131+
return res.as_int();
132+
}
133+
124134
pub fn build_int_shift_left(ctx: &mut MIRContext, left: MIRIntValue, shift: MIRIntValue) -> DiagnosticResult<MIRIntValue> {
125135
let res = ctx.append_inst(MIRInstruction::ShiftLeft { a: left, shift }).get()?;
126136

@@ -181,6 +191,15 @@ pub fn build_float_div(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloa
181191
return res.as_float();
182192
}
183193

194+
pub fn build_float_mod(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
195+
if left.size != right.size {
196+
unsure_panic!("Tried using fmod on different sized integers");
197+
}
198+
199+
let res = ctx.append_inst(MIRInstruction::FloatMod { signed, left, right }).get()?;
200+
201+
return res.as_float();
202+
}
184203

185204
pub fn build_float_neg(ctx: &mut MIRContext, val: MIRFloatValue) -> DiagnosticResult<MIRFloatValue> {
186205
let res = ctx.append_inst(MIRInstruction::FloatNeg { val }).get()?;

compiler/astoir_mir/src/insts/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub enum MIRInstruction {
3434
FloatSub { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
3535
FloatMul { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
3636
FloatDiv { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
37+
FloatMod { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
3738
FloatNeg { val: MIRFloatValue },
3839

3940
// Bitwise (int typed)
@@ -230,6 +231,7 @@ impl Display for MIRInstruction {
230231
Self::FloatSub { signed, left, right } => writeln!(f, "fsub s{} {} {}", signed, left, right)?,
231232
Self::FloatMul { signed, left, right } => writeln!(f, "fmul s{} {} {}", signed, left, right)?,
232233
Self::FloatDiv { signed, left, right } => writeln!(f, "fdiv s{} {} {}", signed, left, right)?,
234+
Self::FloatMod { signed, left, right } => writeln!(f, "fmod s{} {} {}", signed, left, right)?,
233235
Self::FloatNeg { val } => writeln!(f, "fneg {}", val)?,
234236

235237
Self::BitwiseAnd { a, b } => writeln!(f, "and {} {}", a, b)?,

compiler/astoir_mir_lowering/src/math.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use astoir_hir::nodes::{HIRNode, HIRNodeKind};
2-
use astoir_mir::{blocks::refer::MIRBlockReference, builder::{build_float_add, build_float_div, build_float_mul, build_float_sub, build_int_add, build_int_div, build_int_mul, build_int_sub, build_shift_left, build_shift_right}, vals::base::BaseMIRValue};
2+
use astoir_mir::{blocks::refer::MIRBlockReference, builder::{build_float_add, build_float_div, build_float_mod, build_float_mul, build_float_sub, build_int_add, build_int_div, build_int_mod, build_int_mul, build_int_sub, build_shift_left, build_shift_right}, vals::base::BaseMIRValue};
33
use compiler_typing::raw::RawType;
44
use compiler_utils::operators::{MathOperator, MathOperatorType};
55
use diagnostics::{DiagnosticResult, builders::{make_math_operation_req_assign, make_req_type_kind}, unsure_panic};
@@ -55,7 +55,8 @@ pub fn lower_hir_math_operation_int(left: BaseMIRValue, right: BaseMIRValue, ope
5555
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed)?,
5656
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed)?,
5757
MathOperatorType::ShiftLeft => build_shift_left(&mut ctx.mir_ctx, left, right)?,
58-
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?
58+
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?,
59+
MathOperatorType::Modulo => build_int_mod(&mut ctx.mir_ctx, left, right, signed)?
5960
};
6061

6162
return Ok(res.into());
@@ -72,6 +73,7 @@ pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, o
7273
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed)?,
7374
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed)?,
7475
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?,
76+
MathOperatorType::Modulo => build_float_mod(&mut ctx.mir_ctx, left, right, signed)?,
7577

7678
_ => return Err(make_req_type_kind(node, &"integer".to_string()).into())
7779
};

0 commit comments

Comments
 (0)