Skip to content

Refactor the ASR #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,22 @@ expr
| LogicalConstant(bool value, ttype type)

| ListConstant(expr* args, ttype type)
| ListLen(expr arg, ttype type, expr? value)
| ListConcat(expr left, expr right, ttype type, expr? value)

| ArrayConstant(expr* args, ttype type)

| SetConstant(expr* elements, ttype type)
| SetLen(expr arg, ttype type, expr? value)

| TupleConstant(expr* elements, ttype type)
| TupleLen(expr arg, ttype type, expr? value)

| StringConstant(string s, ttype type)
| StringLen(expr arg, ttype type, expr? value)

| DictConstant(expr* keys, expr* values, ttype type)
| DictLen(expr arg, ttype type, expr? value)
| Var(symbol v)
| ArrayRef(symbol v, array_index* args, ttype type, expr? value)
| DerivedRef(expr v, symbol m, ttype type, expr? value)
Expand Down
11 changes: 11 additions & 0 deletions src/libasr/asr_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ static inline ASR::ttype_t* expr_type(const ASR::expr_t *f)
case ASR::exprType::RealConstant: { return ((ASR::RealConstant_t*)f)->m_type; }
case ASR::exprType::ComplexConstant: { return ((ASR::ComplexConstant_t*)f)->m_type; }
case ASR::exprType::SetConstant: { return ((ASR::SetConstant_t*)f)->m_type; }
case ASR::exprType::SetLen: { return ((ASR::SetLen_t*)f)->m_type; }
case ASR::exprType::ListConstant: { return ((ASR::ListConstant_t*)f)->m_type; }
case ASR::exprType::ListConcat: { return ((ASR::ListConcat_t*)f)->m_type; }
case ASR::exprType::ListLen: { return ((ASR::ListLen_t*)f)->m_type; }
case ASR::exprType::TupleConstant: { return ((ASR::TupleConstant_t*)f)->m_type; }
case ASR::exprType::TupleLen: { return ((ASR::TupleLen_t*)f)->m_type; }
case ASR::exprType::LogicalConstant: { return ((ASR::LogicalConstant_t*)f)->m_type; }
case ASR::exprType::StringConstant: { return ((ASR::StringConstant_t*)f)->m_type; }
case ASR::exprType::StringLen: { return ((ASR::StringLen_t*)f)->m_type; }
case ASR::exprType::DictConstant: { return ((ASR::DictConstant_t*)f)->m_type; }
case ASR::exprType::DictLen: { return ((ASR::DictLen_t*)f)->m_type; }
case ASR::exprType::IntegerBOZ: { return ((ASR::IntegerBOZ_t*)f)->m_type; }
case ASR::exprType::Var: { return EXPR2VAR(f)->m_type; }
case ASR::exprType::ArrayRef: { return ((ASR::ArrayRef_t*)f)->m_type; }
Expand Down Expand Up @@ -270,6 +275,11 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
case ASR::exprType::Var: { return EXPR2VAR(f)->m_value; }
case ASR::exprType::StrOp: { return ASR::down_cast<ASR::StrOp_t>(f)->m_value; }
case ASR::exprType::ImpliedDoLoop: { return ASR::down_cast<ASR::ImpliedDoLoop_t>(f)->m_value; }
case ASR::exprType::StringLen: { return ASR::down_cast<ASR::StringLen_t>(f)->m_value; }
case ASR::exprType::DictLen: { return ASR::down_cast<ASR::DictLen_t>(f)->m_value; }
case ASR::exprType::ListLen: { return ASR::down_cast<ASR::ListLen_t>(f)->m_value; }
case ASR::exprType::TupleLen: { return ASR::down_cast<ASR::TupleLen_t>(f)->m_value; }
case ASR::exprType::SetLen: { return ASR::down_cast<ASR::SetLen_t>(f)->m_value; }
case ASR::exprType::ComplexRe: { return ASR::down_cast<ASR::ComplexRe_t>(f)->m_value; }
case ASR::exprType::ComplexIm: { return ASR::down_cast<ASR::ComplexIm_t>(f)->m_value; }
case ASR::exprType::ArrayConstant: // Drop through
Expand All @@ -280,6 +290,7 @@ static inline ASR::expr_t* expr_value(ASR::expr_t *f)
case ASR::exprType::TupleConstant: // Drop through
case ASR::exprType::DictConstant: // Drop through
case ASR::exprType::SetConstant: // Drop through
case ASR::exprType::ListConstant: // Drop through
case ASR::exprType::StringConstant:{ // For all Constants
return f;
}
Expand Down
11 changes: 11 additions & 0 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2860,6 +2860,17 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
}
}

void visit_StringLen(const ASR::StringLen_t &x) {
if (x.m_value) {
this->visit_expr_wrapper(x.m_value, true);
return;
}
this->visit_expr_wrapper(x.m_arg, true);
llvm::AllocaInst *parg = builder->CreateAlloca(character_type, nullptr);
builder->CreateStore(tmp, parg);
tmp = lfortran_str_len(parg);
}

void visit_BinOp(const ASR::BinOp_t &x) {
if( x.m_overloaded ) {
this->visit_expr(*x.m_overloaded);
Expand Down
64 changes: 64 additions & 0 deletions src/lpython/semantics/python_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,67 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
return nullptr;
}

ASR::asr_t* handle_intrinsic_len(Allocator &al, Vec<ASR::call_arg_t> args,
const Location &loc) {
if (args.size() != 1) {
throw SemanticError("len() takes exactly one argument (" +
std::to_string(args.size()) + " given)", loc);
}
ASR::expr_t *arg = args[0].m_value;
ASR::ttype_t *type = ASRUtils::expr_type(arg);
ASR::ttype_t *to_type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc,
4, nullptr, 0));
ASR::expr_t *value = nullptr;
if (ASRUtils::is_character(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
char* c = ASR::down_cast<ASR::StringConstant_t>(
ASRUtils::expr_value(arg))->m_s;
int64_t ival = std::string(c).size();
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
loc, ival, to_type));
}
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
ASR::make_StringLen_t(al, loc, arg, to_type, value));
} else if (ASR::is_a<ASR::Set_t>(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
int64_t ival = (int64_t)ASR::down_cast<ASR::SetConstant_t>(
ASRUtils::expr_value(arg))->n_elements;
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
loc, ival, to_type));
}
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
ASR::make_SetLen_t(al, loc, arg, to_type, value));
} else if (ASR::is_a<ASR::Tuple_t>(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
int64_t ival = (int64_t)ASR::down_cast<ASR::TupleConstant_t>(
ASRUtils::expr_value(arg))->n_elements;
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
loc, ival, to_type));
}
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
ASR::make_TupleLen_t(al, loc, arg, to_type, value));
} else if (ASR::is_a<ASR::List_t>(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
int64_t ival = (int64_t)ASR::down_cast<ASR::ListConstant_t>(
ASRUtils::expr_value(arg))->n_args;
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
loc, ival, to_type));
}
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
ASR::make_ListLen_t(al, loc, arg, to_type, value));
} else if (ASR::is_a<ASR::Dict_t>(*type)) {
if (ASRUtils::expr_value(arg) != nullptr) {
int64_t ival = (int64_t)ASR::down_cast<ASR::DictConstant_t>(
ASRUtils::expr_value(arg))->n_keys;
value = ASR::down_cast<ASR::expr_t>(make_IntegerConstant_t(al,
loc, ival, to_type));
}
return (ASR::asr_t *)ASR::down_cast<ASR::expr_t>(
ASR::make_DictLen_t(al, loc, arg, to_type, value));
}
throw SemanticError("len() is only supported for `str`, `set`, `dict`, `list` and `tuple`", loc);
}

void visit_Call(const AST::Call_t &x) {
std::string call_name;
Vec<ASR::call_arg_t> args;
Expand Down Expand Up @@ -2807,6 +2868,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
tmp = handle_intrinsic_float(al, args, x.base.base.loc);
}
return;
} else if (call_name == "len") {
tmp = handle_intrinsic_len(al, args, x.base.base.loc);
return;
} else {
// The function was not found and it is not intrinsic
throw SemanticError("Function '" + call_name + "' is not declared and not intrinsic",
Expand Down
2 changes: 1 addition & 1 deletion src/lpython/semantics/python_comptime_eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct PythonIntrinsicProcedures {
{"bool", {m_builtin, &eval_bool}},
{"chr", {m_builtin, &eval_chr}},
{"ord", {m_builtin, &eval_ord}},
{"len", {m_builtin, &eval_len}},
// {"len", {m_builtin, &eval_len}},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the dead code in a separate PR IMO. It gets confusing otherwise.

{"pow", {m_builtin, &eval_pow}},
// {"int", {m_builtin, &eval_int}},
// {"float", {m_builtin, &eval_float}},
Expand Down
1 change: 0 additions & 1 deletion src/runtime/lpython_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ def bool(c: c32) -> bool:
def bool(c: c64) -> bool:
return c.real != 0.0 or _lfortran_zaimag(c) != 0.0


@interface
def len(s: str) -> i32:
"""
Expand Down
18 changes: 11 additions & 7 deletions tests/constants1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ def test_len():
a = len('')
a = len('test')
a = len("this is a test")
# TODO(namannimmo10): These commented out tests should work
# a = len((1, 2, 3))
# a = len((("c", "b", 3.4), ("c", 3, 5.6)))
# a = len([1, 2, 3])
# a = len([[-4, -5, -6], [-1, -2, -3]])
# a = len({1, 2, 3})
# a = len({1: "c", 2: "b", 3: "c"})
a = len((1, 2, 3))
a = len((("c", "b", 3.4), ("c", 3, 5.6)))
a = len([1, 2, 3])
a = len([[-4, -5, -6], [-1, -2, -3]])
a = len({1, 2, 3})
a = len({1: "c", 2: "b", 3: "c"})
l: list[i32]
l = [1, 2, 3, 4]
a = len(l)
l.append(5)
a = len(l)


def test_bool():
Expand Down
4 changes: 2 additions & 2 deletions tests/reference/asr-constants1-5828e8a.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"basename": "asr-constants1-5828e8a",
"cmd": "lpython --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/constants1.py",
"infile_hash": "0410dc29468f90206a5c2aab56a9761c8d2ffcb239de210d3df2b14f",
"infile_hash": "680bb6f62b6e7d58fdddc891954816cea0c25b9201576498f149849e",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-constants1-5828e8a.stdout",
"stdout_hash": "1c0247f92f98131fbe521af9b0e3555ad3dd5adbe8a034617bd08d86",
"stdout_hash": "5de2d22da16f558bea2c729081ed44549d6639d26c8d1f2aba59c18b",
"stderr": null,
"stderr_hash": null,
"returncode": 0
Expand Down
Loading