diff --git a/reid-llvm-lib/examples/libtest.rs b/reid-llvm-lib/examples/libtest.rs index d37e9ee..0630a55 100644 --- a/reid-llvm-lib/examples/libtest.rs +++ b/reid-llvm-lib/examples/libtest.rs @@ -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"); diff --git a/reid-llvm-lib/src/compile.rs b/reid-llvm-lib/src/compile.rs index c0c622d..c2a7dd7 100644 --- a/reid-llvm-lib/src/compile.rs +++ b/reid-llvm-lib/src/compile.rs @@ -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, } } } diff --git a/reid-llvm-lib/src/debug.rs b/reid-llvm-lib/src/debug.rs index 9051600..c353891 100644 --- a/reid-llvm-lib/src/debug.rs +++ b/reid-llvm-lib/src/debug.rs @@ -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, "!="), } } } diff --git a/reid-llvm-lib/src/lib.rs b/reid-llvm-lib/src/lib.rs index 16c81a9..fb611f4 100644 --- a/reid-llvm-lib/src/lib.rs +++ b/reid-llvm-lib/src/lib.rs @@ -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), /// Integer Comparison - ICmp(IntPredicate, InstructionValue, InstructionValue), + ICmp(CmpPredicate, InstructionValue, InstructionValue), FunctionCall(FunctionValue, Vec), } diff --git a/reid/examples/reid/fibonacci.reid b/reid/examples/reid/fibonacci.reid index 84625c7..35cc20b 100644 --- a/reid/examples/reid/fibonacci.reid +++ b/reid/examples/reid/fibonacci.reid @@ -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; } diff --git a/reid/examples/testcodegen.rs b/reid/examples/testcodegen.rs index 369b63a..0113853 100644 --- a/reid/examples/testcodegen.rs +++ b/reid/examples/testcodegen.rs @@ -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, diff --git a/reid/src/ast/mod.rs b/reid/src/ast/mod.rs index 6bd368f..038f5f5 100644 --- a/reid/src/ast/mod.rs +++ b/reid/src/ast/mod.rs @@ -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, } } } diff --git a/reid/src/ast/parse.rs b/reid/src/ast/parse.rs index 1da3c49..6bc7a71 100644 --- a/reid/src/ast/parse.rs +++ b/reid/src/ast/parse.rs @@ -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, diff --git a/reid/src/ast/process.rs b/reid/src/ast/process.rs index 8277d17..b5527f5 100644 --- a/reid/src/ast/process.rs +++ b/reid/src/ast/process.rs @@ -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), } } } diff --git a/reid/src/codegen.rs b/reid/src/codegen.rs index 00a03e7..18f2ae3 100644 --- a/reid/src/codegen.rs +++ b/reid/src/codegen.rs @@ -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, } } } diff --git a/reid/src/lexer.rs b/reid/src/lexer.rs index 20e128e..192077c 100644 --- a/reid/src/lexer.rs +++ b/reid/src/lexer.rs @@ -43,6 +43,8 @@ pub enum Token { LessThan, /// `&` Et, + /// `!` + Exclamation, /// `(` ParenOpen, @@ -190,6 +192,7 @@ pub fn tokenize>(to_tokenize: T) -> Result, Error '>' => Token::GreaterThan, '<' => Token::LessThan, '&' => Token::Et, + '!' => Token::Exclamation, '(' => Token::ParenOpen, ')' => Token::ParenClose, '{' => Token::BraceOpen, diff --git a/reid/src/mir/mod.rs b/reid/src/mir/mod.rs index 8e09a6a..1507cf1 100644 --- a/reid/src/mir/mod.rs +++ b/reid/src/mir/mod.rs @@ -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)]