From dab4d47b8e08eda8441cf9806847c530ae25ee04 Mon Sep 17 00:00:00 2001 From: Sofia Date: Mon, 13 Apr 2026 20:46:43 +0300 Subject: [PATCH] Separate signed and unsigned comparisons correctly --- src/binops.cpp | 21 +++++++++++++++++++-- test.c | 5 +++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/binops.cpp b/src/binops.cpp index 383d8f0..a7653d5 100644 --- a/src/binops.cpp +++ b/src/binops.cpp @@ -12,8 +12,8 @@ namespace types { auto bool_ty = std::shared_ptr{ new types::FundamentalType{ types::FundamentalTypeKind::Bool } }; + // Integer arithmetic binops for (auto& ty : { int_ty, char_ty, bool_ty }) { - // Arithmetic binops definitions.push_back(BinopDefinition{ ty, types::BinOp::Add, ty, ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { @@ -25,8 +25,10 @@ namespace types { ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { return builder.builder->CreateSub(lhs, rhs, "sub"); } }); + } - // Comparisons + // Signed comparisons + for (auto& ty : { int_ty }) { definitions.push_back(BinopDefinition{ ty, types::BinOp::LessThan, ty, bool_ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { @@ -40,6 +42,21 @@ namespace types { } }); } + // Unsigned comparisons + for (auto& ty : { bool_ty, char_ty }) { + definitions.push_back(BinopDefinition{ + ty, types::BinOp::LessThan, ty, + bool_ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { + return builder.builder->CreateICmpULT(lhs, rhs, "icmpslt"); + } }); + + definitions.push_back(BinopDefinition{ + ty, types::BinOp::GreaterThan, ty, + bool_ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { + return builder.builder->CreateICmpUGT(lhs, rhs, "icmpsgt"); + } }); + } + return definitions; } diff --git a/test.c b/test.c index 6a49ee4..c12287e 100644 --- a/test.c +++ b/test.c @@ -6,7 +6,8 @@ int fibonacci(int n) { return fibonacci(n - 1) + fibonacci(n - 2); } -char main() { +int main() { printf("10th fibonacci number is %d!", fibonacci(10)); - return 0; + char res = 0; + return res; } \ No newline at end of file