Separate signed and unsigned comparisons correctly

This commit is contained in:
Sofia 2026-04-13 20:46:43 +03:00
parent e6a16ab667
commit dab4d47b8e
2 changed files with 22 additions and 4 deletions

View File

@ -12,8 +12,8 @@ namespace types {
auto bool_ty = std::shared_ptr<types::Type>{ auto bool_ty = std::shared_ptr<types::Type>{
new types::FundamentalType{ types::FundamentalTypeKind::Bool } }; new types::FundamentalType{ types::FundamentalTypeKind::Bool } };
// Integer arithmetic binops
for (auto& ty : { int_ty, char_ty, bool_ty }) { for (auto& ty : { int_ty, char_ty, bool_ty }) {
// Arithmetic binops
definitions.push_back(BinopDefinition{ definitions.push_back(BinopDefinition{
ty, types::BinOp::Add, ty, ty, types::BinOp::Add, ty,
ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { 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) { ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) {
return builder.builder->CreateSub(lhs, rhs, "sub"); return builder.builder->CreateSub(lhs, rhs, "sub");
} }); } });
}
// Comparisons // Signed comparisons
for (auto& ty : { int_ty }) {
definitions.push_back(BinopDefinition{ definitions.push_back(BinopDefinition{
ty, types::BinOp::LessThan, ty, ty, types::BinOp::LessThan, ty,
bool_ty, [](codegen::Builder& builder, llvm::Value* lhs, llvm::Value* rhs) { 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; return definitions;
} }

5
test.c
View File

@ -6,7 +6,8 @@ int fibonacci(int n) {
return fibonacci(n - 1) + fibonacci(n - 2); return fibonacci(n - 1) + fibonacci(n - 2);
} }
char main() { int main() {
printf("10th fibonacci number is %d!", fibonacci(10)); printf("10th fibonacci number is %d!", fibonacci(10));
return 0; char res = 0;
return res;
} }