Skip to content

Commit

Permalink
Merge pull request #1764 from Smit-create/i-1588
Browse files Browse the repository at this point in the history
Add ``UnsignedInteger`` type in ASR
  • Loading branch information
czgdp1807 authored May 10, 2023
2 parents a04d456 + 1ca113f commit 40e7778
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 12 deletions.
1 change: 1 addition & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ RUN(NAME const_03 LABELS cpython llvm c
EXTRAFILES const_03b.c)
RUN(NAME const_04 LABELS cpython llvm c)
RUN(NAME expr_01 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_01u LABELS cpython llvm)
RUN(NAME expr_02 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_03 LABELS cpython llvm c wasm wasm_x64)
RUN(NAME expr_04 LABELS cpython llvm c wasm)
Expand Down
28 changes: 28 additions & 0 deletions integration_tests/expr_01u.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from lpython import inline, u32

@inline
def uadd(x: u32, y: u32) -> u32:
return x + y

@inline
def uand_op(x: u32, y: u32) -> u32:
return x & y

def main1():
x: u32
y: u32
z: u32
x = (u32(2)+u32(3))*u32(5)
y = uadd(x, u32(2))*u32(2)
assert x == u32(25)
assert y == u32(54)

z = uand_op(x, y)
assert z == u32(16)


main1()

# Not implemented yet in LPython:
#if __name__ == "__main__":
# main()
7 changes: 7 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ expr
| IntegerUnaryMinus(expr arg, ttype type, expr? value)
| IntegerCompare(expr left, cmpop op, expr right, ttype type, expr? value)
| IntegerBinOp(expr left, binop op, expr right, ttype type, expr? value)
| UnsignedIntegerConstant(int n, ttype type)
| UnsignedIntegerUnaryMinus(expr arg, ttype type, expr? value)
| UnsignedIntegerCompare(expr left, cmpop op, expr right, ttype type, expr? value)
| UnsignedIntegerBinOp(expr left, binop op, expr right, ttype type, expr? value)
| RealConstant(float r, ttype type)
| RealUnaryMinus(expr arg, ttype type, expr? value)
| RealCompare(expr left, cmpop op, expr right, ttype type, expr? value)
Expand Down Expand Up @@ -346,6 +350,7 @@ expr

ttype
= Integer(int kind, dimension* dims)
| UnsignedInteger(int kind, dimension* dims)
| Real(int kind, dimension* dims)
| Complex(int kind, dimension* dims)
| Character(int kind, int len, expr? len_expr, dimension* dims)
Expand Down Expand Up @@ -402,6 +407,8 @@ cast_kind
| RealToCharacter
| IntegerToCharacter
| LogicalToCharacter
| UnsignedIntegerToInteger
| IntegerToUnsignedInteger

dimension = (expr? start, expr? length)

Expand Down
4 changes: 4 additions & 0 deletions src/libasr/asr_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,10 @@ ASR::asr_t* make_Cast_t_value(Allocator &al, const Location &a_loc,
int64_t int_value = ASR::down_cast<ASR::IntegerConstant_t>(
ASRUtils::expr_value(a_arg))->m_n;
value = ASR::down_cast<ASR::expr_t>(ASR::make_IntegerConstant_t(al, a_loc, int_value, a_type));
} else if (a_kind == ASR::cast_kindType::IntegerToUnsignedInteger) {
int64_t int_value = ASR::down_cast<ASR::IntegerConstant_t>(
ASRUtils::expr_value(a_arg))->m_n;
value = ASR::down_cast<ASR::expr_t>(ASR::make_UnsignedIntegerConstant_t(al, a_loc, int_value, a_type));
} else if (a_kind == ASR::cast_kindType::IntegerToLogical) {
// TODO: implement
} else if (a_kind == ASR::cast_kindType::ComplexToComplex) {
Expand Down
61 changes: 60 additions & 1 deletion src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@ static inline bool extract_value(ASR::expr_t* value_expr, T& value) {
value = (T) const_int->m_n;
break;
}
case ASR::exprType::UnsignedIntegerConstant: {
ASR::UnsignedIntegerConstant_t* const_int = ASR::down_cast<ASR::UnsignedIntegerConstant_t>(value_expr);
value = (T) const_int->m_n;
break;
}
case ASR::exprType::RealConstant: {
ASR::RealConstant_t* const_real = ASR::down_cast<ASR::RealConstant_t>(value_expr);
value = (T) const_real->m_r;
Expand Down Expand Up @@ -924,6 +929,16 @@ static inline std::string get_type_code(const ASR::ttype_t *t, bool use_undersco
is_dimensional = integer->n_dims > 0;
break;
}
case ASR::ttypeType::UnsignedInteger: {
ASR::UnsignedInteger_t *integer = ASR::down_cast<ASR::UnsignedInteger_t>(t);
res = "u" + std::to_string(integer->m_kind * 8);
if( encode_dimensions_ ) {
encode_dimensions(integer->n_dims, res, use_underscore_sep);
return res;
}
is_dimensional = integer->n_dims > 0;
break;
}
case ASR::ttypeType::Real: {
ASR::Real_t *real = ASR::down_cast<ASR::Real_t>(t);
res = "r" + std::to_string(real->m_kind * 8);
Expand Down Expand Up @@ -1090,7 +1105,7 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
{
switch (t->type) {
case ASR::ttypeType::Integer: {
ASR::Integer_t *i = (ASR::Integer_t*)t;
ASR::Integer_t *i = ASR::down_cast<ASR::Integer_t>(t);
std::string res = "";
switch (i->m_kind) {
case 1: { res = "i8"; break; }
Expand All @@ -1104,6 +1119,21 @@ static inline std::string type_to_str_python(const ASR::ttype_t *t,
}
return res;
}
case ASR::ttypeType::UnsignedInteger: {
ASR::UnsignedInteger_t *i = ASR::down_cast<ASR::UnsignedInteger_t>(t);
std::string res = "";
switch (i->m_kind) {
case 1: { res = "u8"; break; }
case 2: { res = "u16"; break; }
case 4: { res = "u32"; break; }
case 8: { res = "u64"; break; }
default: { throw LCompilersException("UnsignedInteger kind not supported"); }
}
if (i->n_dims == 1 && for_error_message) {
res = type_python_1dim_helper(res, i->m_dims);
}
return res;
}
case ASR::ttypeType::Real: {
ASR::Real_t *r = (ASR::Real_t*)t;
std::string res = "";
Expand Down Expand Up @@ -1374,6 +1404,9 @@ static inline int extract_kind_from_ttype_t(const ASR::ttype_t* type) {
case ASR::ttypeType::Integer : {
return ASR::down_cast<ASR::Integer_t>(type)->m_kind;
}
case ASR::ttypeType::UnsignedInteger : {
return ASR::down_cast<ASR::UnsignedInteger_t>(type)->m_kind;
}
case ASR::ttypeType::Real : {
return ASR::down_cast<ASR::Real_t>(type)->m_kind;
}
Expand Down Expand Up @@ -1406,6 +1439,10 @@ static inline bool is_integer(ASR::ttype_t &x) {
return ASR::is_a<ASR::Integer_t>(*type_get_past_pointer(&x));
}

static inline bool is_unsigned_integer(ASR::ttype_t &x) {
return ASR::is_a<ASR::UnsignedInteger_t>(*type_get_past_pointer(&x));
}

static inline bool is_real(ASR::ttype_t &x) {
return ASR::is_a<ASR::Real_t>(*type_get_past_pointer(&x));
}
Expand Down Expand Up @@ -1485,6 +1522,12 @@ inline int extract_dimensions_from_ttype(ASR::ttype_t *x,
m_dims = Integer_type->m_dims;
break;
}
case ASR::ttypeType::UnsignedInteger: {
ASR::UnsignedInteger_t* Integer_type = ASR::down_cast<ASR::UnsignedInteger_t>(x);
n_dims = Integer_type->n_dims;
m_dims = Integer_type->m_dims;
break;
}
case ASR::ttypeType::Real: {
ASR::Real_t* Real_type = ASR::down_cast<ASR::Real_t>(x);
n_dims = Real_type->n_dims;
Expand Down Expand Up @@ -2122,6 +2165,22 @@ inline bool types_equal(ASR::ttype_t *a, ASR::ttype_t *b,
}
break;
}
case (ASR::ttypeType::UnsignedInteger) : {
ASR::UnsignedInteger_t *a2 = ASR::down_cast<ASR::UnsignedInteger_t>(a);
ASR::UnsignedInteger_t *b2 = ASR::down_cast<ASR::UnsignedInteger_t>(b);
if (a2->m_kind == b2->m_kind) {
if( check_for_dimensions ) {
return ASRUtils::dimensions_equal(
a2->m_dims, a2->n_dims,
b2->m_dims, b2->n_dims);
} else {
return true;
}
} else {
return false;
}
break;
}
case ASR::ttypeType::CPtr: {
return true;
}
Expand Down
Loading

0 comments on commit 40e7778

Please sign in to comment.