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