Skip to content

Commit 64b5052

Browse files
authored
Merge pull request #87 from Quickfall/chore/ast-operators
(chore) AST operators
2 parents d47924a + a19bfd6 commit 64b5052

6 files changed

Lines changed: 177 additions & 68 deletions

File tree

compiler/ast/src/operators.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,18 @@ pub fn parse_math_operator(tokens: &Vec<LexerToken>, ind: &mut usize) -> Diagnos
3838
_ => false
3939
};
4040

41-
*ind += 1;
41+
if assigns {
42+
*ind += 1;
43+
}
4244

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

48-
*ind += 1;
50+
if fast {
51+
*ind += 1;
52+
}
4953

5054
return Ok(MathOperator { operator: op, assigns, fast });
5155
}

compiler/ast_parser/src/math.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pub fn parse_math_operation(tokens: &Vec<LexerToken>, ind: &mut usize, original:
1010
if !oper.assigns && restricts_to_assigns {
1111
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())
1212
}
13-
14-
*ind += 1;
13+
14+
println!("{:#?}", tokens[*ind].tok_type);
1515

1616
let right_member = parse_ast_value(tokens, ind)?;
1717

compiler/astoir_mir/src/builder.rs

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,42 +81,52 @@ pub fn build_upcast_float(ctx: &mut MIRContext, val: MIRFloatValue, size: usize)
8181
return res.as_float();
8282
}
8383

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

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

9191
return res.as_int();
9292
}
9393

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

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

101101
return res.as_int();
102102
}
103103

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

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

111111
return res.as_int();
112112
}
113113

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

119-
let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, left, right }).get()?;
119+
let res = ctx.append_inst(MIRInstruction::IntegerDiv { signed, fast, left, right }).get()?;
120+
121+
return res.as_int();
122+
}
123+
124+
pub fn build_int_mod(ctx: &mut MIRContext, left: MIRIntValue, right: MIRIntValue, signed: bool, fast: 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, fast, left, right }).get()?;
120130

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

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

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

149159
return res.as_float();
150160
}
151161

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

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

159169
return res.as_float();
160170
}
161171

162172

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

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

170180
return res.as_float();
171181
}
172182

173183

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

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

181191
return res.as_float();
182192
}
183193

194+
pub fn build_float_mod(ctx: &mut MIRContext, left: MIRFloatValue, right: MIRFloatValue, signed: bool, fast: 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, fast, 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: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ pub enum MIRInstruction {
2323
UpcastFloat { val: MIRFloatValue, size: usize },
2424

2525
// Arithmetrics
26-
IntegerAdd { signed: bool, left: MIRIntValue, right: MIRIntValue },
27-
IntegerSub { signed: bool, left: MIRIntValue, right: MIRIntValue },
28-
IntegerMul { signed: bool, left: MIRIntValue, right: MIRIntValue },
29-
IntegerDiv { signed: bool, left: MIRIntValue, right: MIRIntValue },
30-
IntegerMod { signed: bool, left: MIRIntValue, right: MIRIntValue },
26+
IntegerAdd { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
27+
IntegerSub { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
28+
IntegerMul { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
29+
IntegerDiv { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
30+
IntegerMod { signed: bool, fast: bool, left: MIRIntValue, right: MIRIntValue },
3131
IntegerNeg { val: MIRIntValue },
3232

33-
FloatAdd { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
34-
FloatSub { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
35-
FloatMul { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
36-
FloatDiv { signed: bool, left: MIRFloatValue, right: MIRFloatValue },
33+
FloatAdd { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
34+
FloatSub { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
35+
FloatMul { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
36+
FloatDiv { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
37+
FloatMod { signed: bool, fast: bool, left: MIRFloatValue, right: MIRFloatValue },
3738
FloatNeg { val: MIRFloatValue },
3839

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

141-
Self::IntegerAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
142-
Self::IntegerSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
143-
Self::IntegerMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
144-
Self::IntegerDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
145-
Self::IntegerMod { signed, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
142+
Self::IntegerAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
143+
Self::IntegerSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
144+
Self::IntegerMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
145+
Self::IntegerDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
146+
Self::IntegerMod { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Integer(left.size, *signed)),
146147
Self::IntegerNeg { val } => return Type::GenericLowered(RawType::Integer(val.size, true)),
147148

148-
Self::FloatAdd { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
149-
Self::FloatSub { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
150-
Self::FloatMul { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
151-
Self::FloatDiv { signed, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
149+
Self::FloatAdd { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
150+
Self::FloatSub { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
151+
Self::FloatMul { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
152+
Self::FloatDiv { signed, fast: _, left, right: _ } => return Type::GenericLowered(RawType::Floating(left.size, *signed)),
152153
Self::FloatNeg { val } => return Type::GenericLowered(RawType::Floating(val.size, true)),
153154

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

222-
Self::IntegerAdd { signed, left, right } => writeln!(f, "iadd s{} {} {}", signed, left, right)?,
223-
Self::IntegerSub { signed, left, right } => writeln!(f, "isub s{} {} {}", signed, left, right)?,
224-
Self::IntegerMul { signed, left, right } => writeln!(f, "imul s{} {} {}", signed, left, right)?,
225-
Self::IntegerDiv { signed, left, right } => writeln!(f, "idiv s{} {} {}", signed, left, right)?,
226-
Self::IntegerMod { signed, left, right } => writeln!(f, "imod s{} {} {}", signed, left, right)?,
223+
Self::IntegerAdd { signed, fast, left, right } => writeln!(f, "iadd s{} f{} {} {}", signed, fast, left, right)?,
224+
Self::IntegerSub { signed, fast, left, right } => writeln!(f, "isub s{} f{} {} {}", signed, fast, left, right)?,
225+
Self::IntegerMul { signed, fast, left, right } => writeln!(f, "imul s{} f{} {} {}", signed, fast, left, right)?,
226+
Self::IntegerDiv { signed, fast, left, right } => writeln!(f, "idiv s{} f{} {} {}", signed, fast, left, right)?,
227+
Self::IntegerMod { signed, fast, left, right } => writeln!(f, "imod s{} f{} {} {}", signed, fast, left, right)?,
227228
Self::IntegerNeg { val } => writeln!(f, "ineg {}", val)?,
228229

229-
Self::FloatAdd { signed, left, right } => writeln!(f, "fadd s{} {} {}", signed, left, right)?,
230-
Self::FloatSub { signed, left, right } => writeln!(f, "fsub s{} {} {}", signed, left, right)?,
231-
Self::FloatMul { signed, left, right } => writeln!(f, "fmul s{} {} {}", signed, left, right)?,
232-
Self::FloatDiv { signed, left, right } => writeln!(f, "fdiv s{} {} {}", signed, left, right)?,
230+
Self::FloatAdd { signed, fast, left, right } => writeln!(f, "fadd s{} f{} {} {}", signed, fast, left, right)?,
231+
Self::FloatSub { signed, fast, left, right } => writeln!(f, "fsub s{} f{} {} {}", signed, fast, left, right)?,
232+
Self::FloatMul { signed, fast, left, right } => writeln!(f, "fmul s{} f{} {} {}", signed, fast, left, right)?,
233+
Self::FloatDiv { signed, fast, left, right } => writeln!(f, "fdiv s{} f{} {} {}", signed, fast, left, right)?,
234+
Self::FloatMod { signed, fast, left, right } => writeln!(f, "fmod s{} f{} {} {}", signed, fast, 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: 12 additions & 10 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};
@@ -50,12 +50,13 @@ pub fn lower_hir_math_operation_int(left: BaseMIRValue, right: BaseMIRValue, ope
5050
let signed = left.signed;
5151

5252
let res = match operator.operator {
53-
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed)?,
54-
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed)?,
55-
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed)?,
56-
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed)?,
53+
MathOperatorType::Add => build_int_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
54+
MathOperatorType::Subtract => build_int_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
55+
MathOperatorType::Multiply => build_int_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
56+
MathOperatorType::Divide => build_int_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
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, operator.fast)?
5960
};
6061

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

7071
let res = match operator.operator {
71-
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed)?,
72-
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed)?,
73-
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed)?,
74-
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed)?,
72+
MathOperatorType::Add => build_float_add(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
73+
MathOperatorType::Subtract => build_float_sub(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
74+
MathOperatorType::Multiply => build_float_mul(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
75+
MathOperatorType::Divide => build_float_div(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
76+
MathOperatorType::Modulo => build_float_mod(&mut ctx.mir_ctx, left, right, signed, operator.fast)?,
7577

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

0 commit comments

Comments
 (0)