Add a bunch of new integer comparison operators
This commit is contained in:
parent
46560d8541
commit
49df6c9ed9
@ -1,4 +1,4 @@
|
||||
use reid_lib::{ConstValue, Context, InstructionKind, IntPredicate, TerminatorKind, Type};
|
||||
use reid_lib::{ConstValue, Context, InstructionKind, CmpPredicate, TerminatorKind, Type};
|
||||
|
||||
fn main() {
|
||||
use ConstValue::*;
|
||||
@ -26,7 +26,7 @@ fn main() {
|
||||
let num_3 = f_entry.build(Constant(I32(3))).unwrap();
|
||||
let param_n = f_entry.build(Param(0)).unwrap();
|
||||
let cond = f_entry
|
||||
.build(ICmp(IntPredicate::LessThan, param_n, num_3))
|
||||
.build(ICmp(CmpPredicate::LT, param_n, num_3))
|
||||
.unwrap();
|
||||
|
||||
let mut then_b = fibonacci.block("then");
|
||||
|
@ -18,7 +18,7 @@ use llvm_sys::{
|
||||
use crate::util::{ErrorMessageHolder, from_cstring, into_cstring};
|
||||
|
||||
use super::{
|
||||
ConstValue, Context, IntPredicate, TerminatorKind, Type,
|
||||
CmpPredicate, ConstValue, Context, TerminatorKind, Type,
|
||||
builder::{
|
||||
BlockHolder, BlockValue, Builder, FunctionHolder, FunctionValue, InstructionHolder,
|
||||
InstructionValue, ModuleHolder,
|
||||
@ -269,7 +269,7 @@ impl InstructionHolder {
|
||||
LLVMBuildICmp(
|
||||
module.builder_ref,
|
||||
// Signedness from LHS
|
||||
pred.as_llvm(lhs._ty.signed()),
|
||||
pred.as_llvm_int(lhs._ty.signed()),
|
||||
lhs.value_ref,
|
||||
rhs_val,
|
||||
c"icmp".as_ptr(),
|
||||
@ -353,15 +353,21 @@ impl TerminatorKind {
|
||||
}
|
||||
}
|
||||
|
||||
impl IntPredicate {
|
||||
fn as_llvm(&self, signed: bool) -> LLVMIntPredicate {
|
||||
use IntPredicate::*;
|
||||
impl CmpPredicate {
|
||||
fn as_llvm_int(&self, signed: bool) -> LLVMIntPredicate {
|
||||
use CmpPredicate::*;
|
||||
use LLVMIntPredicate::*;
|
||||
match (self, signed) {
|
||||
(LessThan, true) => LLVMIntSLT,
|
||||
(GreaterThan, true) => LLVMIntSGT,
|
||||
(LessThan, false) => LLVMIntULT,
|
||||
(GreaterThan, false) => LLVMIntUGT,
|
||||
(LT, true) => LLVMIntSLT,
|
||||
(GT, true) => LLVMIntSGT,
|
||||
(LE, true) => LLVMIntSLE,
|
||||
(GE, true) => LLVMIntSGE,
|
||||
(LT, false) => LLVMIntULT,
|
||||
(GT, false) => LLVMIntUGT,
|
||||
(LE, false) => LLVMIntULE,
|
||||
(GE, false) => LLVMIntUGE,
|
||||
(EQ, _) => LLVMIntEQ,
|
||||
(NE, _) => LLVMIntNE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::{InstructionData, InstructionKind, IntPredicate, TerminatorKind, builder::*};
|
||||
use crate::{CmpPredicate, InstructionData, InstructionKind, TerminatorKind, builder::*};
|
||||
|
||||
impl Debug for ModuleHolder {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
@ -86,11 +86,15 @@ impl Debug for InstructionKind {
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for IntPredicate {
|
||||
impl Debug for CmpPredicate {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::LessThan => write!(f, "<"),
|
||||
Self::GreaterThan => write!(f, ">"),
|
||||
Self::LT => write!(f, "<"),
|
||||
Self::GT => write!(f, ">"),
|
||||
Self::LE => write!(f, "<="),
|
||||
Self::GE => write!(f, ">="),
|
||||
Self::EQ => write!(f, "=="),
|
||||
Self::NE => write!(f, "!="),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -137,9 +137,13 @@ pub struct InstructionData {
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Hash)]
|
||||
pub enum IntPredicate {
|
||||
LessThan,
|
||||
GreaterThan,
|
||||
pub enum CmpPredicate {
|
||||
LT,
|
||||
LE,
|
||||
GT,
|
||||
GE,
|
||||
EQ,
|
||||
NE,
|
||||
}
|
||||
|
||||
#[derive(Clone, Hash)]
|
||||
@ -151,7 +155,7 @@ pub enum InstructionKind {
|
||||
Phi(Vec<InstructionValue>),
|
||||
|
||||
/// Integer Comparison
|
||||
ICmp(IntPredicate, InstructionValue, InstructionValue),
|
||||
ICmp(CmpPredicate, InstructionValue, InstructionValue),
|
||||
|
||||
FunctionCall(FunctionValue, Vec<InstructionValue>),
|
||||
}
|
||||
|
@ -1,11 +1,10 @@
|
||||
// Main
|
||||
fn main() -> u8 {
|
||||
let a = fibonacci(3);
|
||||
return a;
|
||||
fn main() -> bool {
|
||||
return 1 == fibonacci(3);
|
||||
}
|
||||
|
||||
// Fibonacci
|
||||
fn fibonacci(value: u8) -> u8 {
|
||||
fn fibonacci(value: u16) -> u16 {
|
||||
if value < 3 {
|
||||
return 1;
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ fn main() {
|
||||
// If N < 3
|
||||
Box::new(Expression(
|
||||
ExprKind::BinOp(
|
||||
BinaryOperator::Logic(LogicOperator::GreaterThan),
|
||||
BinaryOperator::Logic(LogicOperator::GT),
|
||||
Box::new(Expression(
|
||||
ExprKind::Variable(VariableReference(
|
||||
TypeKind::I32,
|
||||
|
@ -46,7 +46,12 @@ pub enum BinaryOperator {
|
||||
Mult,
|
||||
|
||||
And,
|
||||
LessThan,
|
||||
LT,
|
||||
LE,
|
||||
GT,
|
||||
GE,
|
||||
EQ,
|
||||
NE,
|
||||
}
|
||||
|
||||
impl BinaryOperator {
|
||||
@ -57,7 +62,12 @@ impl BinaryOperator {
|
||||
Minus => 10,
|
||||
Mult => 20,
|
||||
And => 100,
|
||||
LessThan => 100,
|
||||
LT => 100,
|
||||
LE => 100,
|
||||
GT => 100,
|
||||
GE => 100,
|
||||
EQ => 100,
|
||||
NE => 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +137,25 @@ impl Parse for BinaryOperator {
|
||||
stream.next();
|
||||
BinaryOperator::And
|
||||
}
|
||||
(Some(Token::LessThan), _) => BinaryOperator::LessThan,
|
||||
(Some(Token::LessThan), Some(Token::Equals)) => {
|
||||
stream.next();
|
||||
BinaryOperator::LE
|
||||
}
|
||||
(Some(Token::GreaterThan), Some(Token::Equals)) => {
|
||||
stream.next();
|
||||
BinaryOperator::GE
|
||||
}
|
||||
(Some(Token::Equals), Some(Token::Equals)) => {
|
||||
stream.next();
|
||||
BinaryOperator::EQ
|
||||
}
|
||||
(Some(Token::Exclamation), Some(Token::Equals)) => {
|
||||
stream.next();
|
||||
BinaryOperator::NE
|
||||
}
|
||||
|
||||
(Some(Token::LessThan), _) => BinaryOperator::LT,
|
||||
(Some(Token::GreaterThan), _) => BinaryOperator::GT,
|
||||
|
||||
(Some(Token::Plus), _) => BinaryOperator::Add,
|
||||
(Some(Token::Minus), _) => BinaryOperator::Minus,
|
||||
|
@ -150,9 +150,12 @@ impl ast::BinaryOperator {
|
||||
ast::BinaryOperator::Minus => mir::BinaryOperator::Minus,
|
||||
ast::BinaryOperator::Mult => mir::BinaryOperator::Mult,
|
||||
ast::BinaryOperator::And => mir::BinaryOperator::And,
|
||||
ast::BinaryOperator::LessThan => {
|
||||
mir::BinaryOperator::Logic(mir::LogicOperator::LessThan)
|
||||
}
|
||||
ast::BinaryOperator::LT => mir::BinaryOperator::Logic(mir::LogicOperator::LT),
|
||||
ast::BinaryOperator::LE => mir::BinaryOperator::Logic(mir::LogicOperator::LE),
|
||||
ast::BinaryOperator::GT => mir::BinaryOperator::Logic(mir::LogicOperator::GT),
|
||||
ast::BinaryOperator::GE => mir::BinaryOperator::Logic(mir::LogicOperator::GE),
|
||||
ast::BinaryOperator::EQ => mir::BinaryOperator::Logic(mir::LogicOperator::EQ),
|
||||
ast::BinaryOperator::NE => mir::BinaryOperator::Logic(mir::LogicOperator::NE),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::{collections::HashMap, mem};
|
||||
|
||||
use reid_lib::{
|
||||
builder::InstructionValue, Block, ConstValue, Context, Function, InstructionKind, IntPredicate,
|
||||
builder::InstructionValue, Block, CmpPredicate, ConstValue, Context, Function, InstructionKind,
|
||||
Module, TerminatorKind, Type,
|
||||
};
|
||||
|
||||
@ -287,10 +287,14 @@ impl mir::Expression {
|
||||
}
|
||||
|
||||
impl mir::LogicOperator {
|
||||
fn int_predicate(&self) -> IntPredicate {
|
||||
fn int_predicate(&self) -> CmpPredicate {
|
||||
match self {
|
||||
mir::LogicOperator::LessThan => IntPredicate::LessThan,
|
||||
mir::LogicOperator::GreaterThan => IntPredicate::GreaterThan,
|
||||
mir::LogicOperator::LT => CmpPredicate::LT,
|
||||
mir::LogicOperator::GT => CmpPredicate::GT,
|
||||
mir::LogicOperator::LE => CmpPredicate::LT,
|
||||
mir::LogicOperator::GE => CmpPredicate::GE,
|
||||
mir::LogicOperator::EQ => CmpPredicate::EQ,
|
||||
mir::LogicOperator::NE => CmpPredicate::NE,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,8 @@ pub enum Token {
|
||||
LessThan,
|
||||
/// `&`
|
||||
Et,
|
||||
/// `!`
|
||||
Exclamation,
|
||||
|
||||
/// `(`
|
||||
ParenOpen,
|
||||
@ -190,6 +192,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
|
||||
'>' => Token::GreaterThan,
|
||||
'<' => Token::LessThan,
|
||||
'&' => Token::Et,
|
||||
'!' => Token::Exclamation,
|
||||
'(' => Token::ParenOpen,
|
||||
')' => Token::ParenClose,
|
||||
'{' => Token::BraceOpen,
|
||||
|
@ -170,8 +170,12 @@ pub enum BinaryOperator {
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum LogicOperator {
|
||||
LessThan,
|
||||
GreaterThan,
|
||||
LT,
|
||||
LE,
|
||||
GT,
|
||||
GE,
|
||||
EQ,
|
||||
NE,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
Loading…
Reference in New Issue
Block a user