Skip to content

Commit

Permalink
implement mul and div instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 22, 2024
1 parent 68a7e08 commit 621b77f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/codegen/llvm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ impl<'module, 'compiler, 'ink> FunctionGenerator<'module, 'compiler, 'ink> {
match op {
BinaryOp::Add => self.builder.build_int_add(lhs, rhs, "add_result").unwrap(),
BinaryOp::Sub => self.builder.build_int_sub(lhs, rhs, "sub_result").unwrap(),
BinaryOp::Multiply => self.builder.build_int_mul(lhs, rhs, "mul_result").unwrap(),
BinaryOp::Divide => self
.builder
.build_int_signed_div(lhs, rhs, "div_result")
.unwrap(),
BinaryOp::Eq => self
.builder
.build_int_compare(IntPredicate::EQ, lhs, rhs, "eq_result")
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ fn main() {
let n = 0;
let counter = 0;
loop {
if counter >= 20 {
if counter >= 2 * 2 * 10 / 2 {
break;
}
counter += 1;
if counter == 10 {
if counter == 2 * 5 {
continue;
}
n += 1;
}
let result1 = fib1(n);
let result2 = fib2(19);
let result2 = fib2(38 / 2);
if result1 == result2 {
return result1;
Expand Down
4 changes: 4 additions & 0 deletions src/repr/ast/base/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use super::Expression;
pub enum InfixOperation {
Minus,
Plus,
Multiply,
Divide,
Eq,
NotEq,
Greater,
Expand Down Expand Up @@ -33,6 +35,8 @@ impl TryFrom<Token> for InfixOperation {
match token {
Token::Plus => Ok(InfixOperation::Plus),
Token::Minus => Ok(InfixOperation::Minus),
Token::Asterix => Ok(InfixOperation::Multiply),
Token::ForwardSlash => Ok(InfixOperation::Divide),
Token::DoubleEq => Ok(InfixOperation::Eq),
Token::NotEq => Ok(InfixOperation::NotEq),
Token::LeftAngle => Ok(InfixOperation::Less),
Expand Down
4 changes: 4 additions & 0 deletions src/repr/ir/triple/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::repr::ast::typed as ast;
pub enum BinaryOp {
Add,
Sub,
Multiply,
Divide,
Eq,
NotEq,
Greater,
Expand All @@ -19,6 +21,8 @@ impl From<&ast::InfixOperation> for BinaryOp {
match value {
ast::InfixOperation::Plus => Self::Add,
ast::InfixOperation::Minus => Self::Sub,
ast::InfixOperation::Multiply => Self::Multiply,
ast::InfixOperation::Divide => Self::Divide,
ast::InfixOperation::Eq => Self::Eq,
ast::InfixOperation::NotEq => Self::NotEq,
ast::InfixOperation::Greater => Self::Greater,
Expand Down
6 changes: 6 additions & 0 deletions src/repr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub enum Token {
Plus,
#[token("-")]
Minus,
#[token("*")]
Asterix,
#[token("/")]
ForwardSlash,
#[token("==")]
DoubleEq,
#[token("!=")]
Expand Down Expand Up @@ -124,6 +128,8 @@ impl Display for Token {
match self {
Token::Plus => write!(f, "+"),
Token::Minus => write!(f, "-"),
Token::Asterix => write!(f, "*"),
Token::ForwardSlash => write!(f, "/"),
Token::DoubleEq => write!(f, "=="),
Token::NotEq => write!(f, "!="),
Token::And => write!(f, "&&"),
Expand Down
2 changes: 2 additions & 0 deletions src/stage/parse/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ pub enum Precedence {
Binary,
Equality,
Sum,
Multiply,
Call,
}

impl Precedence {
pub fn of(token: &Token) -> Self {
match token {
Token::Minus | Token::Plus => Precedence::Sum,
Token::Asterix | Token::ForwardSlash => Precedence::Multiply,
Token::And | Token::Or => Precedence::Binary,
Token::DoubleEq
| Token::NotEq
Expand Down
2 changes: 1 addition & 1 deletion src/stage/type_check/expression/infix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ impl InfixOperation {
use InfixOperation::*;

match (self, left, right) {
(Plus | Minus, Ty::Int, Ty::Int) => Ok(Ty::Int),
(Plus | Minus | Multiply | Divide, Ty::Int, Ty::Int) => Ok(Ty::Int),
(Eq | NotEq | Greater | Less | GreaterEq | LessEq, left, right)
if left.check(right) =>
{
Expand Down

0 comments on commit 621b77f

Please sign in to comment.