Skip to content

Sync libasr with LFortran #1535

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 4 commits into from
Feb 21, 2023
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
15 changes: 9 additions & 6 deletions src/libasr/ASR.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ stmt
| Associate(expr target, expr value)
| Cycle()
-- deallocates if allocated otherwise throws a runtime error
| ExplicitDeallocate(symbol* vars)
| ExplicitDeallocate(expr* vars)
-- deallocates if allocated otherwise does nothing
| ImplicitDeallocate(symbol* vars)
| DoConcurrentLoop(do_loop_head head, stmt* body)
Expand Down Expand Up @@ -286,6 +286,7 @@ expr
| ArrayPack(expr array, expr mask, expr? vector, ttype type, expr? value)
| ArrayReshape(expr array, expr shape, ttype type, expr? value)
| ArrayMaxloc(expr array, expr? dim, expr? mask, expr? kind, expr? back, ttype type, expr? value)
| ArrayAll(expr mask, expr? dim, ttype type, expr? value)

| BitCast(expr source, expr mold, expr? size, ttype type, expr? value)
| StructInstanceMember(expr v, symbol m, ttype type, expr? value)
Expand Down Expand Up @@ -318,6 +319,8 @@ expr
| PointerNullConstant(ttype type)
| PointerAssociated(expr ptr, expr? tgt, ttype type, expr? value)

| IntrinsicFunctionSqrt(expr arg, ttype type, expr? value)


-- `len` in Character:
-- >=0 ... the length of the string, known at compile time
Expand Down Expand Up @@ -357,9 +360,9 @@ ttype
| CPtr()
| TypeParameter(identifier param, dimension* dims)
| FunctionType(ttype* arg_types, ttype? return_var_type,
abi abi, deftype deftype, string? bindc_name, bool elemental,
bool pure, bool module, bool inline, bool static, ttype* type_params,
symbol* restrictions, bool is_restriction)
abi abi, deftype deftype, string? bindc_name, bool elemental,
bool pure, bool module, bool inline, bool static, ttype* type_params,
symbol* restrictions, bool is_restriction)

restriction_arg = RestrictionArg(identifier restriction_name, symbol restriction_func)

Expand Down Expand Up @@ -399,7 +402,7 @@ cast_kind

dimension = (expr? start, expr? length)

alloc_arg = (symbol a, dimension* dims)
alloc_arg = (expr a, dimension* dims)

attribute = Attribute(identifier name, attribute_arg *args)

Expand All @@ -415,7 +418,7 @@ do_loop_head = (expr? v, expr? start, expr? end, expr? increment)

case_stmt = CaseStmt(expr* test, stmt* body) | CaseStmt_Range(expr? start, expr? end, stmt* body)

type_stmt = TypeStmt(symbol sym, stmt* body)
type_stmt = TypeStmtName(symbol sym, stmt* body) | TypeStmtType(ttype type, stmt* body)

enumtype = IntegerConsecutiveFromZero | IntegerUnique | IntegerNotUnique | NonInteger

Expand Down
1 change: 1 addition & 0 deletions src/libasr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(SRC
pass/implied_do_loops.cpp
pass/array_op.cpp
pass/subroutine_from_function.cpp
pass/transform_optional_argument_functions.cpp
pass/class_constructor.cpp
pass/arr_slice.cpp
pass/print_arr.cpp
Expand Down
45 changes: 39 additions & 6 deletions src/libasr/asdl_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,9 +797,11 @@ class ExprStmtDuplicatorVisitor(ASDLVisitor):
def __init__(self, stream, data):
self.duplicate_stmt = []
self.duplicate_expr = []
self.duplicate_ttype = []
self.duplicate_case_stmt = []
self.is_stmt = False
self.is_expr = False
self.is_ttype = False
self.is_case_stmt = False
self.is_product = False
super(ExprStmtDuplicatorVisitor, self).__init__(stream, data)
Expand Down Expand Up @@ -834,6 +836,13 @@ def visitModule(self, mod):
self.duplicate_expr.append(("", 0))
self.duplicate_expr.append((" switch(x->type) {", 1))

self.duplicate_ttype.append((" ASR::ttype_t* duplicate_ttype(ASR::ttype_t* x) {", 0))
self.duplicate_ttype.append((" if( !x ) {", 1))
self.duplicate_ttype.append((" return nullptr;", 2))
self.duplicate_ttype.append((" }", 1))
self.duplicate_ttype.append(("", 0))
self.duplicate_ttype.append((" switch(x->type) {", 1))

self.duplicate_case_stmt.append((" ASR::case_stmt_t* duplicate_case_stmt(ASR::case_stmt_t* x) {", 0))
self.duplicate_case_stmt.append((" if( !x ) {", 1))
self.duplicate_case_stmt.append((" return nullptr;", 2))
Expand All @@ -858,6 +867,14 @@ def visitModule(self, mod):
self.duplicate_expr.append((" return nullptr;", 1))
self.duplicate_expr.append((" }", 0))

self.duplicate_ttype.append((" default: {", 2))
self.duplicate_ttype.append((' LCOMPILERS_ASSERT_MSG(false, "Duplication of " + std::to_string(x->type) + " type is not supported yet.");', 3))
self.duplicate_ttype.append((" }", 2))
self.duplicate_ttype.append((" }", 1))
self.duplicate_ttype.append(("", 0))
self.duplicate_ttype.append((" return nullptr;", 1))
self.duplicate_ttype.append((" }", 0))

self.duplicate_case_stmt.append((" default: {", 2))
self.duplicate_case_stmt.append((' LCOMPILERS_ASSERT_MSG(false, "Duplication of " + std::to_string(x->type) + " case statement is not supported yet.");', 3))
self.duplicate_case_stmt.append((" }", 2))
Expand All @@ -872,6 +889,9 @@ def visitModule(self, mod):
for line, level in self.duplicate_expr:
self.emit(line, level=level)
self.emit("")
for line, level in self.duplicate_ttype:
self.emit(line, level=level)
self.emit("")
for line, level in self.duplicate_case_stmt:
self.emit(line, level=level)
self.emit("")
Expand All @@ -885,8 +905,9 @@ def visitType(self, tp):
def visitSum(self, sum, *args):
self.is_stmt = args[0] == 'stmt'
self.is_expr = args[0] == 'expr'
self.is_ttype = args[0] == "ttype"
self.is_case_stmt = args[0] == 'case_stmt'
if self.is_stmt or self.is_expr or self.is_case_stmt:
if self.is_stmt or self.is_expr or self.is_case_stmt or self.is_ttype:
for tp in sum.types:
self.visit(tp, *args)

Expand Down Expand Up @@ -933,6 +954,10 @@ def make_visitor(self, name, fields):
self.duplicate_expr.append((" }", 3))
self.duplicate_expr.append((" return down_cast<ASR::expr_t>(self().duplicate_%s(down_cast<ASR::%s_t>(x)));" % (name, name), 3))
self.duplicate_expr.append((" }", 2))
elif self.is_ttype:
self.duplicate_ttype.append((" case ASR::ttypeType::%s: {" % name, 2))
self.duplicate_ttype.append((" return down_cast<ASR::ttype_t>(self().duplicate_%s(down_cast<ASR::%s_t>(x)));" % (name, name), 3))
self.duplicate_ttype.append((" }", 2))
elif self.is_case_stmt:
self.duplicate_case_stmt.append((" case ASR::case_stmtType::%s: {" % name, 2))
self.duplicate_case_stmt.append((" return down_cast<ASR::case_stmt_t>(self().duplicate_%s(down_cast<ASR::%s_t>(x)));" % (name, name), 3))
Expand All @@ -949,7 +974,8 @@ def visitField(self, field):
field.type == "do_loop_head" or
field.type == "array_index" or
field.type == "alloc_arg" or
field.type == "case_stmt"):
field.type == "case_stmt" or
field.type == "ttype"):
level = 2
if field.seq:
self.used = True
Expand Down Expand Up @@ -1107,10 +1133,12 @@ def visitField(self, field):
self.used = True
self.emit("for (size_t i = 0; i < x->n_%s; i++) {" % field.name, level)
if field.type == "call_arg":
self.emit(" ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level)
self.emit(" current_expr = &(x->m_%s[i].m_value);" % (field.name), level)
self.emit(" self().replace_expr(x->m_%s[i].m_value);"%(field.name), level)
self.emit(" current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level)
self.emit(" if (x->m_%s[i].m_value != nullptr) {" % (field.name), level)
self.emit(" ASR::expr_t** current_expr_copy_%d = current_expr;" % (self.current_expr_copy_variable_count), level + 1)
self.emit(" current_expr = &(x->m_%s[i].m_value);" % (field.name), level + 1)
self.emit(" self().replace_expr(x->m_%s[i].m_value);"%(field.name), level + 1)
self.emit(" current_expr = current_expr_copy_%d;" % (self.current_expr_copy_variable_count), level + 1)
self.emit(" }", level)
self.current_expr_copy_variable_count += 1
self.emit("}", level)
else:
Expand Down Expand Up @@ -2310,6 +2338,8 @@ def make_visitor(self, name, fields):
LCOMPILERS_ASSERT(e->m_external);
LCOMPILERS_ASSERT(!ASR::is_a<ASR::ExternalSymbol_t>(*e->m_external));
s = e->m_external;
} else if (s->type == ASR::symbolType::Function) {
return ASR::down_cast<ASR::Function_t>(s)->m_function_signature;
}
return ASR::down_cast<ASR::Variable_t>(s)->m_type;
}""" \
Expand Down Expand Up @@ -2529,6 +2559,9 @@ def main(argv):
subs["MOD"] = "LPython::AST"
subs["mod"] = "ast"
subs["lcompiler"] = "lpython"
elif subs["MOD"] == "AST":
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shouldn't it be "LFORTRAN" instead of "AST"?

Copy link
Contributor

Choose a reason for hiding this comment

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

We haven't been consistent in the asdl if I remember well, so I would not do any big changes in the sync PRs, but we can fix things up later.

subs["MOD"] = "LFortran::AST"
subs["lcompiler"] = "lfortran"
else:
subs["lcompiler"] = "lfortran"
is_asr = (mod.name.upper() == "ASR")
Expand Down
Loading