From efb7f4254b35ea104f7f79122ab1e7a58fdc569e Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Tue, 21 Jun 2022 18:37:55 +0530 Subject: [PATCH] Added LLVM support for bitwise XOR, AND and OR --- src/libasr/codegen/asr_to_llvm.cpp | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/libasr/codegen/asr_to_llvm.cpp b/src/libasr/codegen/asr_to_llvm.cpp index 784f5d6117..e2edf5a6af 100644 --- a/src/libasr/codegen/asr_to_llvm.cpp +++ b/src/libasr/codegen/asr_to_llvm.cpp @@ -4691,6 +4691,35 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor 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( @@ -4721,6 +4750,21 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor if( s == nullptr ) { s = ASR::down_cast(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){