Skip to content

Commit 18392f4

Browse files
ubaidskcertik
authored andcommitted
ASR,LLVM,C: Support unsigned int unary not
1 parent dee5200 commit 18392f4

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

src/libasr/ASR.asdl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ cast_kind
430430
| LogicalToCharacter
431431
| UnsignedIntegerToInteger
432432
| UnsignedIntegerToReal
433+
| UnsignedIntegerToLogical
433434
| IntegerToUnsignedInteger
434435
| RealToUnsignedInteger
435436
| CPtrToUnsignedInteger

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,8 @@ PyMODINIT_FUNC PyInit_lpython_module_)" + fn_name + R"((void) {
18301830
last_expr_precedence = 2;
18311831
break;
18321832
}
1833-
case (ASR::cast_kindType::IntegerToLogical) : {
1833+
case (ASR::cast_kindType::IntegerToLogical) :
1834+
case (ASR::cast_kindType::UnsignedIntegerToLogical) : {
18341835
src = "(bool)(" + src + ")";
18351836
last_expr_precedence = 2;
18361837
break;

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6777,7 +6777,8 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
67776777
tmp = complex_from_floats(tmp, zero, complex_type);
67786778
break;
67796779
}
6780-
case (ASR::cast_kindType::IntegerToLogical) : {
6780+
case (ASR::cast_kindType::IntegerToLogical) :
6781+
case (ASR::cast_kindType::UnsignedIntegerToLogical) : {
67816782
ASR::ttype_t* curr_type = extract_ttype_t_from_expr(x.m_arg);
67826783
LCOMPILERS_ASSERT(curr_type != nullptr)
67836784
int a_kind = ASRUtils::extract_kind_from_ttype_t(curr_type);

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,18 @@ class CommonVisitor : public AST::BaseVisitor<Struct> {
34613461
logical_arg = ASR::down_cast<ASR::expr_t>(ASR::make_Cast_t(
34623462
al, x.base.base.loc, operand, ASR::cast_kindType::IntegerToLogical,
34633463
logical_type, value));
3464+
} else if (ASRUtils::is_unsigned_integer(*operand_type)) {
3465+
if (ASRUtils::expr_value(operand) != nullptr) {
3466+
int64_t op_value = ASR::down_cast<ASR::UnsignedIntegerConstant_t>(
3467+
ASRUtils::expr_value(operand))->m_n;
3468+
bool b = (op_value == 0);
3469+
value = ASR::down_cast<ASR::expr_t>(ASR::make_LogicalConstant_t(
3470+
al, x.base.base.loc, b, logical_type));
3471+
}
3472+
// cast UnsignedInteger to Logical
3473+
logical_arg = ASR::down_cast<ASR::expr_t>(ASR::make_Cast_t(
3474+
al, x.base.base.loc, operand, ASR::cast_kindType::UnsignedIntegerToLogical,
3475+
logical_type, value));
34643476
} else if (ASRUtils::is_real(*operand_type)) {
34653477
if (ASRUtils::expr_value(operand) != nullptr) {
34663478
double op_value = ASR::down_cast<ASR::RealConstant_t>(

0 commit comments

Comments
 (0)