Skip to content

Commit 4cf4391

Browse files
committed
feat: added MIR bridge support
1 parent b46a36c commit 4cf4391

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

compiler/astoir_mir/src/builder.rs

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

124+
pub fn build_int_shift_left(ctx: &mut MIRContext, left: MIRIntValue, shift: MIRIntValue) -> DiagnosticResult<MIRIntValue> {
125+
let res = ctx.append_inst(MIRInstruction::ShiftLeft { a: left, shift }).get()?;
126+
127+
return res.as_int();
128+
}
129+
130+
pub fn build_int_shift_right(ctx: &mut MIRContext, left: MIRIntValue, shift: MIRIntValue) -> DiagnosticResult<MIRIntValue> {
131+
let res = ctx.append_inst(MIRInstruction::ShiftRight { a: left, shift }).get()?;
132+
133+
return res.as_int();
134+
}
135+
124136
pub fn build_int_neg(ctx: &mut MIRContext, val: MIRIntValue) -> DiagnosticResult<MIRIntValue> {
125137
let res = ctx.append_inst(MIRInstruction::IntegerNeg { val }).get()?;
126138

compiler/astoir_mir_lowering/src/math.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
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}, vals::base::BaseMIRValue};
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};
33
use compiler_typing::raw::RawType;
44
use compiler_utils::operators::{MathOperator, MathOperatorType};
5-
use diagnostics::{DiagnosticResult, builders::make_math_operation_req_assign, unsure_panic};
5+
use diagnostics::{DiagnosticResult, builders::{make_math_operation_req_assign, make_req_type_kind}, unsure_panic};
66

77
use crate::{MIRLoweringContext, values::lower_hir_value, vars::lower_hir_variable_reference};
88

@@ -26,7 +26,7 @@ pub fn lower_hir_math_operation(block: MIRBlockReference, node: Box<HIRNode>, ct
2626

2727
let val = match left_val.vtype.get_generic(&ctx.hir_ctx.type_storage) {
2828
RawType::Integer(_, _) | RawType::FixedPoint(_, _, _) => lower_hir_math_operation_int(left_val, right_val, operation.clone(), ctx)?,
29-
RawType::Floating(_, _) => lower_hir_math_operation_float(left_val, right_val, operation.clone(), ctx)?,
29+
RawType::Floating(_, _) => lower_hir_math_operation_float(left_val, right_val, operation.clone(), ctx, &*node)?,
3030

3131
_ => unsure_panic!("Cannot use lower_hir_math_operator on this given value kind!")
3232
};
@@ -54,12 +54,14 @@ pub fn lower_hir_math_operation_int(left: BaseMIRValue, right: BaseMIRValue, ope
5454
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed)?,
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)?,
57+
MathOperatorType::ShiftLeft => build_shift_left(&mut ctx.mir_ctx, left, right)?,
58+
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?
5759
};
5860

5961
return Ok(res.into());
6062
}
6163

62-
pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, operator: MathOperator, ctx: &mut MIRLoweringContext) -> DiagnosticResult<BaseMIRValue> {
64+
pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, operator: MathOperator, ctx: &mut MIRLoweringContext, node: &HIRNode) -> DiagnosticResult<BaseMIRValue> {
6365
let left = left.as_float()?;
6466
let right = right.as_float()?;
6567

@@ -69,7 +71,9 @@ pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, o
6971
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed)?,
7072
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed)?,
7173
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed)?,
72-
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?
74+
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?,
75+
76+
_ => return Err(make_req_type_kind(node, &"integer".to_string()).into())
7377
};
7478

7579
return Ok(res.into());

0 commit comments

Comments
 (0)