Skip to content

Commit

Permalink
Added LLVM support for bitwise XOR, AND and OR
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Jun 21, 2022
1 parent 2614f5f commit efb7f42
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4691,6 +4691,35 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
pop_nested_stack(s);
}

void handle_bitwise_args(const ASR::FunctionCall_t& x, llvm::Value*& arg1,
llvm::Value*& arg2) {
LFORTRAN_ASSERT(x.n_args == 2);
tmp = nullptr;
this->visit_expr_wrapper(x.m_args[0].m_value, true);
arg1 = tmp;
tmp = nullptr;
this->visit_expr_wrapper(x.m_args[1].m_value, true);
arg2 = tmp;
}

void handle_bitwise_xor(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateXor(arg1, arg2);
}

void handle_bitwise_and(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateAnd(arg1, arg2);
}

void handle_bitwise_or(const ASR::FunctionCall_t& x) {
llvm::Value *arg1 = nullptr, *arg2 = nullptr;
handle_bitwise_args(x, arg1, arg2);
tmp = builder->CreateOr(arg1, arg2);
}

void visit_FunctionCall(const ASR::FunctionCall_t &x) {
if( ASRUtils::is_intrinsic_optimization(x.m_name) ) {
ASR::Function_t* routine = ASR::down_cast<ASR::Function_t>(
Expand Down Expand Up @@ -4721,6 +4750,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
if( s == nullptr ) {
s = ASR::down_cast<ASR::Function_t>(symbol_get_past_external(x.m_name));
}
if( ASRUtils::is_intrinsic_function2(s) ) {
std::string symbol_name = ASRUtils::symbol_name(x.m_name);
if( startswith(symbol_name, "_bitwise_xor") ) {
handle_bitwise_xor(x);
return ;
}
if( startswith(symbol_name, "_bitwise_and") ) {
handle_bitwise_and(x);
return ;
}
if( startswith(symbol_name, "_bitwise_or") ) {
handle_bitwise_or(x);
return ;
}
}
if (parent_function){
push_nested_stack(parent_function);
} else if (parent_subroutine){
Expand Down

0 comments on commit efb7f42

Please sign in to comment.