Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions compiler/ast/src/operators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ pub fn parse_math_operator(tokens: &Vec<LexerToken>, ind: &mut usize) -> Diagnos
_ => false
};

*ind += 1;
if assigns {
*ind += 1;
}

let fast = match tokens[*ind].tok_type {
LexerTokenType::Tidle => true,
_ => false
};

*ind += 1;
if fast {
*ind += 1;
}

return Ok(MathOperator { operator: op, assigns, fast });
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/ast_parser/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub fn parse_math_operation(tokens: &Vec<LexerToken>, ind: &mut usize, original:
if !oper.assigns && restricts_to_assigns {
return Err(tokens[*ind].make_simple_diagnostic(MATH_OPERATION_ASSIGNS.0, Level::Error, MATH_OPERATION_ASSIGNS.1.to_string(), None, vec![], vec!["consider assigning this to variable".to_string()], vec!["add = at the end of the operator".to_string()]).into())
}

*ind += 1;
println!("{:#?}", tokens[*ind].tok_type);

let right_member = parse_ast_value(tokens, ind)?;

Expand Down
51 changes: 35 additions & 16 deletions compiler/astoir_mir/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,42 +81,52 @@ pub fn build_upcast_float(ctx: &mut MIRContext, val: MIRFloatValue, size: usize)
return res.as_float();
}

pub fn build_int_add(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
pub fn build_int_add(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
if left.size != right.size {
unsure_panic!("Tried using iadd on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::IntegerAdd { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::IntegerAdd { signed, fast, left, right }).get()?;

return res.as_int();
}

pub fn build_int_sub(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
pub fn build_int_sub(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
if left.size != right.size {
unsure_panic!("Tried using isub on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::IntegerSub { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::IntegerSub { signed, fast, left, right }).get()?;

return res.as_int();
}

pub fn build_int_mul(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
pub fn build_int_mul(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
if left.size != right.size {
unsure_panic!("Tried using imul on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::IntegerMul { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::IntegerMul { signed, fast, left, right }).get()?;

return res.as_int();
}

pub fn build_int_div(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool) -> DiagnosticResult<MIRIntValue> {
pub fn build_int_div(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
if left.size != right.size {
unsure_panic!("Tried using idiv on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, fast, left, right }).get()?;

return res.as_int();
}

pub fn build_int_mod(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: bool) -> DiagnosticResult<MIRIntValue> {
if left.size != right.size {
unsure_panic!("Tried using imod on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::IntegerMod { signed, fast, left, right }).get()?;

return res.as_int();
}
Expand All @@ -139,48 +149,57 @@ pub fn build_int_neg(ctx: &mut MIRContext, val: MIRIntValue) -> DiagnosticResult
return res.as_int();
}

pub fn build_float_add(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
pub fn build_float_add(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
if left.size != right.size {
unsure_panic!("Tried using fadd on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::FloatAdd { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::FloatAdd { signed, fast, left, right }).get()?;

return res.as_float();
}

pub fn build_float_sub(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
pub fn build_float_sub(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
if left.size != right.size {
unsure_panic!("Tried using fsub on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::FloatSub { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::FloatSub { signed, fast, left, right }).get()?;

return res.as_float();
}


pub fn build_float_mul(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
pub fn build_float_mul(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
if left.size != right.size {
unsure_panic!("Tried using fmul on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::FloatMul { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::FloatMul { signed, fast, left, right }).get()?;

return res.as_float();
}


pub fn build_float_div(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool) -> DiagnosticResult<MIRFloatValue> {
pub fn build_float_div(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
if left.size != right.size {
unsure_panic!("Tried using fdiv on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::FloatDiv { signed, left, right }).get()?;
let res = ctx.append_inst(MIRInstruction::FloatDiv { signed, fast, left, right }).get()?;

return res.as_float();
}

pub fn build_float_mod(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: bool) -> DiagnosticResult<MIRFloatValue> {
if left.size != right.size {
unsure_panic!("Tried using fmod on different sized integers");
}

let res = ctx.append_inst(MIRInstruction::FloatMod { signed, fast, left, right }).get()?;

return res.as_float();
}

pub fn build_float_neg(ctx: &mut MIRContext, val: MIRFloatValue) -> DiagnosticResult<MIRFloatValue> {
let res = ctx.append_inst(MIRInstruction::FloatNeg { val }).get()?;
Expand Down
56 changes: 29 additions & 27 deletions compiler/astoir_mir/src/insts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ pub enum MIRInstruction {
UpcastFloat { val: MIRFloatValue, size: usize },

// Arithmetrics
IntegerAdd { signed: bool, left: MIRIntValue, right: MIRIntValue },
IntegerSub { signed: bool, left: MIRIntValue, right: MIRIntValue },
IntegerMul { signed: bool, left: MIRIntValue, right: MIRIntValue },
IntegerDiv { signed: bool, left: MIRIntValue, right: MIRIntValue },
IntegerMod { signed: bool, left: MIRIntValue, right: MIRIntValue },
IntegerAdd { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
IntegerSub { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
IntegerMul { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
IntegerDiv { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
IntegerMod { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
IntegerNeg { val: MIRIntValue },

FloatAdd { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatSub { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatMul { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatDiv { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatAdd { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatSub { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatMul { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatDiv { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatMod { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
FloatNeg { val: MIRFloatValue },

// Bitwise (int typed)
Expand Down Expand Up @@ -138,17 +139,17 @@ impl MIRInstruction {
Self::DowncastFloat { val, size } => return Type::GenericLowered(RawType::Floating(*size, val.signed)),
Self::UpcastFloat { val, size } => return Type::GenericLowered(RawType::Floating(*size, val.signed)),

Self::IntegerAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerMod { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerMod { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
Self::IntegerNeg { val } => return Type::GenericLowered(RawType::Integer(val.size, true)),

Self::FloatAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
Self::FloatNeg { val } => return Type::GenericLowered(RawType::Floating(val.size, true)),

Self::BitwiseAnd { a, b: _ } => return Type::GenericLowered(RawType::Integer(a.size, a.signed)),
Expand Down Expand Up @@ -219,17 +220,18 @@ impl Display for MIRInstruction {
Self::UpcastInteger { val, size } => writeln!(f, "uintcast {} {}", val, size)?,
Self::UpcastFloat { val, size } => writeln!(f, "ufcast {} {}", val, size)?,

Self::IntegerAdd { signed, left, right } => writeln!(f, "iadd s{} {} {}", signed, left, right)?,
Self::IntegerSub { signed, left, right } => writeln!(f, "isub s{} {} {}", signed, left, right)?,
Self::IntegerMul { signed, left, right } => writeln!(f, "imul s{} {} {}", signed, left, right)?,
Self::IntegerDiv { signed, left, right } => writeln!(f, "idiv s{} {} {}", signed, left, right)?,
Self::IntegerMod { signed, left, right } => writeln!(f, "imod s{} {} {}", signed, left, right)?,
Self::IntegerAdd { signed, fast, left, right } => writeln!(f, "iadd s{} f{} {} {}", signed, fast, left, right)?,
Self::IntegerSub { signed, fast, left, right } => writeln!(f, "isub s{} f{} {} {}", signed, fast, left, right)?,
Self::IntegerMul { signed, fast, left, right } => writeln!(f, "imul s{} f{} {} {}", signed, fast, left, right)?,
Self::IntegerDiv { signed, fast, left, right } => writeln!(f, "idiv s{} f{} {} {}", signed, fast, left, right)?,
Self::IntegerMod { signed, fast, left, right } => writeln!(f, "imod s{} f{} {} {}", signed, fast, left, right)?,
Self::IntegerNeg { val } => writeln!(f, "ineg {}", val)?,

Self::FloatAdd { signed, left, right } => writeln!(f, "fadd s{} {} {}", signed, left, right)?,
Self::FloatSub { signed, left, right } => writeln!(f, "fsub s{} {} {}", signed, left, right)?,
Self::FloatMul { signed, left, right } => writeln!(f, "fmul s{} {} {}", signed, left, right)?,
Self::FloatDiv { signed, left, right } => writeln!(f, "fdiv s{} {} {}", signed, left, right)?,
Self::FloatAdd { signed, fast, left, right } => writeln!(f, "fadd s{} f{} {} {}", signed, fast, left, right)?,
Self::FloatSub { signed, fast, left, right } => writeln!(f, "fsub s{} f{} {} {}", signed, fast, left, right)?,
Self::FloatMul { signed, fast, left, right } => writeln!(f, "fmul s{} f{} {} {}", signed, fast, left, right)?,
Self::FloatDiv { signed, fast, left, right } => writeln!(f, "fdiv s{} f{} {} {}", signed, fast, left, right)?,
Self::FloatMod { signed, fast, left, right } => writeln!(f, "fmod s{} f{} {} {}", signed, fast, left, right)?,
Self::FloatNeg { val } => writeln!(f, "fneg {}", val)?,

Self::BitwiseAnd { a, b } => writeln!(f, "and {} {}", a, b)?,
Expand Down
22 changes: 12 additions & 10 deletions compiler/astoir_mir_lowering/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use astoir_hir::nodes::{HIRNode, HIRNodeKind};
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};
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};
use compiler_typing::raw::RawType;
use compiler_utils::operators::{MathOperator, MathOperatorType};
use diagnostics::{DiagnosticResult, builders::{make_math_operation_req_assign, make_req_type_kind}, unsure_panic};
Expand Down Expand Up @@ -50,12 +50,13 @@ pub fn lower_hir_math_operation_int(left: BaseMIRValue, right: BaseMIRValue, ope
let signed = left.signed;

let res = match operator.operator {
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::ShiftLeft => build_shift_left(&mut ctx.mir_ctx, left, right)?,
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?
MathOperatorType::ShiftRight => build_shift_right(&mut ctx.mir_ctx, left, right)?,
MathOperatorType::Modulo => build_int_mod(&mut ctx.mir_ctx, left, right, signed, operator.fast)?
};

return Ok(res.into());
Expand All @@ -68,10 +69,11 @@ pub fn lower_hir_math_operation_float(left: BaseMIRValue, right: BaseMIRValue, o
let signed = left.signed;

let res = match operator.operator {
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?,
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
MathOperatorType::Modulo => build_float_mod(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,

_ => return Err(make_req_type_kind(node, &"integer".to_string()).into())
};
Expand Down
Loading