Skip to content

Commit 9c5b4e0

Browse files
committed
LLVM/C: Implement CPtrCompare
1 parent d3f3fe4 commit 9c5b4e0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,10 @@ R"(#include <stdio.h>
15561556
handle_Compare(x);
15571557
}
15581558

1559+
void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
1560+
handle_Compare(x);
1561+
}
1562+
15591563
template<typename T>
15601564
void handle_Compare(const T &x) {
15611565
CHECK_FAST_C_CPP(compiler_options, x)
@@ -1663,6 +1667,10 @@ R"(#include <stdio.h>
16631667
}
16641668
}
16651669

1670+
void visit_PointerNullConstant(const ASR::PointerNullConstant_t& /*x*/) {
1671+
src = "NULL";
1672+
}
1673+
16661674
void visit_GetPointer(const ASR::GetPointer_t& x) {
16671675
CHECK_FAST_C_CPP(compiler_options, x)
16681676
self().visit_expr(*x.m_arg);

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5134,6 +5134,49 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
51345134
}
51355135
}
51365136

5137+
void visit_CPtrCompare(const ASR::CPtrCompare_t &x) {
5138+
if (x.m_value) {
5139+
this->visit_expr_wrapper(x.m_value, true);
5140+
return;
5141+
}
5142+
this->visit_expr_wrapper(x.m_left, true);
5143+
llvm::Value *left = tmp;
5144+
left = builder->CreatePtrToInt(left, getIntType(8, false));
5145+
this->visit_expr_wrapper(x.m_right, true);
5146+
llvm::Value *right = tmp;
5147+
right = builder->CreatePtrToInt(right, getIntType(8, false));
5148+
switch (x.m_op) {
5149+
case (ASR::cmpopType::Eq) : {
5150+
tmp = builder->CreateICmpEQ(left, right);
5151+
break;
5152+
}
5153+
case (ASR::cmpopType::Gt) : {
5154+
tmp = builder->CreateICmpSGT(left, right);
5155+
break;
5156+
}
5157+
case (ASR::cmpopType::GtE) : {
5158+
tmp = builder->CreateICmpSGE(left, right);
5159+
break;
5160+
}
5161+
case (ASR::cmpopType::Lt) : {
5162+
tmp = builder->CreateICmpSLT(left, right);
5163+
break;
5164+
}
5165+
case (ASR::cmpopType::LtE) : {
5166+
tmp = builder->CreateICmpSLE(left, right);
5167+
break;
5168+
}
5169+
case (ASR::cmpopType::NotEq) : {
5170+
tmp = builder->CreateICmpNE(left, right);
5171+
break;
5172+
}
5173+
default : {
5174+
throw CodeGenError("Comparison operator not implemented",
5175+
x.base.base.loc);
5176+
}
5177+
}
5178+
}
5179+
51375180
void visit_RealCompare(const ASR::RealCompare_t &x) {
51385181
if (x.m_value) {
51395182
this->visit_expr_wrapper(x.m_value, true);

0 commit comments

Comments
 (0)