Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2fa782a

Browse files
committed
done
1 parent 98101a2 commit 2fa782a

File tree

8 files changed

+521
-362
lines changed

8 files changed

+521
-362
lines changed

src/libasr/asr_utils.h

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ static inline ASR::abiType symbol_abi(const ASR::symbol_t *f)
237237
case ASR::symbolType::ExternalSymbol: {
238238
return symbol_abi(ASR::down_cast<ASR::ExternalSymbol_t>(f)->m_external);
239239
}
240+
case ASR::symbolType::Function: {
241+
return ASRUtils::get_FunctionType(*ASR::down_cast<ASR::Function_t>(f))->m_abi;
242+
}
240243
default: {
241244
throw LCompilersException("Cannot return ABI of, " +
242245
std::to_string(f->type) + " symbol.");
@@ -1982,6 +1985,74 @@ static inline bool is_only_upper_bound_empty(ASR::dimension_t& dim) {
19821985
return (dim.m_start != nullptr && dim.m_length == nullptr);
19831986
}
19841987

1988+
class ExprDependentOnlyOnArguments: public ASR::BaseWalkVisitor<ExprDependentOnlyOnArguments> {
1989+
1990+
public:
1991+
1992+
bool is_dependent_only_on_argument;
1993+
1994+
ExprDependentOnlyOnArguments(): is_dependent_only_on_argument(false)
1995+
{}
1996+
1997+
void visit_Var(const ASR::Var_t& x) {
1998+
if( ASR::is_a<ASR::Variable_t>(*x.m_v) ) {
1999+
ASR::Variable_t* x_m_v = ASR::down_cast<ASR::Variable_t>(x.m_v);
2000+
is_dependent_only_on_argument = is_dependent_only_on_argument && ASRUtils::is_arg_dummy(x_m_v->m_intent);
2001+
} else {
2002+
is_dependent_only_on_argument = false;
2003+
}
2004+
}
2005+
};
2006+
2007+
static inline bool is_dimension_dependent_only_on_arguments(ASR::dimension_t* m_dims, size_t n_dims) {
2008+
ExprDependentOnlyOnArguments visitor;
2009+
for( size_t i = 0; i < n_dims; i++ ) {
2010+
visitor.is_dependent_only_on_argument = true;
2011+
if( m_dims[i].m_length == nullptr ) {
2012+
return false;
2013+
}
2014+
visitor.visit_expr(*m_dims[i].m_length);
2015+
if( !visitor.is_dependent_only_on_argument ) {
2016+
return false;
2017+
}
2018+
}
2019+
return true;
2020+
}
2021+
2022+
static inline ASR::asr_t* make_ArraySize_t_util(
2023+
Allocator &al, const Location &a_loc, ASR::expr_t* a_v,
2024+
ASR::expr_t* a_dim, ASR::ttype_t* a_type, ASR::expr_t* a_value,
2025+
bool for_type=true) {
2026+
if( ASR::is_a<ASR::ArrayPhysicalCast_t>(*a_v) ) {
2027+
a_v = ASR::down_cast<ASR::ArrayPhysicalCast_t>(a_v)->m_arg;
2028+
}
2029+
2030+
ASR::dimension_t* m_dims = nullptr;
2031+
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(a_v), m_dims);
2032+
bool is_dimension_dependent_only_on_arguments_ = is_dimension_dependent_only_on_arguments(m_dims, n_dims);
2033+
int dim = -1;
2034+
bool is_dimension_constant = (a_dim != nullptr) && ASRUtils::extract_value(ASRUtils::expr_value(a_dim), dim);
2035+
2036+
bool compute_size = (is_dimension_dependent_only_on_arguments_ &&
2037+
(is_dimension_constant || a_dim == nullptr));
2038+
if( compute_size && for_type ) {
2039+
ASR::dimension_t* m_dims = nullptr;
2040+
size_t n_dims = ASRUtils::extract_dimensions_from_ttype(ASRUtils::expr_type(a_v), m_dims);
2041+
if( a_dim == nullptr ) {
2042+
ASR::asr_t* size = ASR::make_IntegerConstant_t(al, a_loc, 1, a_type);
2043+
for( size_t i = 0; i < n_dims; i++ ) {
2044+
size = ASR::make_IntegerBinOp_t(al, a_loc, ASRUtils::EXPR(size),
2045+
ASR::binopType::Mul, m_dims[i].m_length, a_type, nullptr);
2046+
}
2047+
return size;
2048+
} else if( is_dimension_constant ) {
2049+
return (ASR::asr_t*) m_dims[dim - 1].m_length;
2050+
}
2051+
}
2052+
2053+
return ASR::make_ArraySize_t(al, a_loc, a_v, a_dim, a_type, a_value);
2054+
}
2055+
19852056
inline ASR::ttype_t* make_Array_t_util(Allocator& al, const Location& loc,
19862057
ASR::ttype_t* type, ASR::dimension_t* m_dims, size_t n_dims,
19872058
ASR::abiType abi=ASR::abiType::Source, bool is_argument=false,
@@ -1991,6 +2062,14 @@ inline ASR::ttype_t* make_Array_t_util(Allocator& al, const Location& loc,
19912062
return type;
19922063
}
19932064

2065+
for( size_t i = 0; i < n_dims; i++ ) {
2066+
if( m_dims[i].m_length && ASR::is_a<ASR::ArraySize_t>(*m_dims[i].m_length) ) {
2067+
ASR::ArraySize_t* as = ASR::down_cast<ASR::ArraySize_t>(m_dims[i].m_length);
2068+
m_dims[i].m_length = ASRUtils::EXPR(ASRUtils::make_ArraySize_t_util(
2069+
al, as->base.base.loc, as->m_v, as->m_dim, as->m_type, nullptr));
2070+
}
2071+
}
2072+
19942073
if( !override_physical_type ) {
19952074
if( abi == ASR::abiType::BindC ) {
19962075
physical_type = ASR::array_physical_typeType::PointerToDataArray;
@@ -3228,40 +3307,6 @@ class ReplaceFunctionParamVisitor: public ASR::BaseExprReplacer<ReplaceFunctionP
32283307

32293308
};
32303309

3231-
class ExprDependentOnlyOnArguments: public ASR::BaseWalkVisitor<ExprDependentOnlyOnArguments> {
3232-
3233-
public:
3234-
3235-
bool is_dependent_only_on_argument;
3236-
3237-
ExprDependentOnlyOnArguments(): is_dependent_only_on_argument(false)
3238-
{}
3239-
3240-
void visit_Var(const ASR::Var_t& x) {
3241-
if( ASR::is_a<ASR::Variable_t>(*x.m_v) ) {
3242-
ASR::Variable_t* x_m_v = ASR::down_cast<ASR::Variable_t>(x.m_v);
3243-
is_dependent_only_on_argument = is_dependent_only_on_argument && ASRUtils::is_arg_dummy(x_m_v->m_intent);
3244-
} else {
3245-
is_dependent_only_on_argument = false;
3246-
}
3247-
}
3248-
};
3249-
3250-
static inline bool is_dimension_dependent_only_on_arguments(ASR::dimension_t* m_dims, size_t n_dims) {
3251-
ExprDependentOnlyOnArguments visitor;
3252-
for( size_t i = 0; i < n_dims; i++ ) {
3253-
visitor.is_dependent_only_on_argument = true;
3254-
if( m_dims[i].m_length == nullptr ) {
3255-
return false;
3256-
}
3257-
visitor.visit_expr(*m_dims[i].m_length);
3258-
if( !visitor.is_dependent_only_on_argument ) {
3259-
return false;
3260-
}
3261-
}
3262-
return true;
3263-
}
3264-
32653310
inline ASR::asr_t* make_FunctionType_t_util(Allocator &al,
32663311
const Location &a_loc, ASR::expr_t** a_args, size_t n_args,
32673312
ASR::expr_t* a_return_var, ASR::abiType a_abi, ASR::deftypeType a_deftype,
@@ -3881,16 +3926,6 @@ static inline ASR::expr_t* get_bound(ASR::expr_t* arr_expr, int dim,
38813926
int32_type, bound_type, bound_value));
38823927
}
38833928

3884-
static inline ASR::asr_t* make_ArraySize_t_util(
3885-
Allocator &al, const Location &a_loc, ASR::expr_t* a_v,
3886-
ASR::expr_t* a_dim, ASR::ttype_t* a_type, ASR::expr_t* a_value) {
3887-
if( ASR::is_a<ASR::ArrayPhysicalCast_t>(*a_v) ) {
3888-
a_v = ASR::down_cast<ASR::ArrayPhysicalCast_t>(a_v)->m_arg;
3889-
}
3890-
3891-
return ASR::make_ArraySize_t(al, a_loc, a_v, a_dim, a_type, a_value);
3892-
}
3893-
38943929
static inline ASR::expr_t* get_size(ASR::expr_t* arr_expr, int dim,
38953930
Allocator& al) {
38963931
ASR::ttype_t* int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, arr_expr->base.loc, 4));

src/libasr/codegen/asr_to_llvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8925,7 +8925,7 @@ Result<std::unique_ptr<LLVMModule>> asr_to_llvm(ASR::TranslationUnit_t &asr,
89258925
pass_manager.apply_passes(al, &asr, pass_options, diagnostics);
89268926

89278927
// Uncomment for debugging the ASR after the transformation
8928-
// std::cout << LCompilers::pickle(asr, true, false, false) << std::endl;
8928+
// std::cout << LCompilers::pickle(asr, false, false, false) << std::endl;
89298929

89308930
try {
89318931
v.visit_asr((ASR::asr_t&)asr);

src/libasr/codegen/llvm_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,7 @@ namespace LCompilers {
11421142
break;
11431143
}
11441144
case ASR::array_physical_typeType::FixedSizeArray: {
1145+
LCOMPILERS_ASSERT(ASRUtils::is_fixed_size_array(v_type->m_dims, v_type->n_dims));
11451146
llvm_type = llvm::ArrayType::get(get_el_type(v_type->m_type, module),
11461147
ASRUtils::get_fixed_size_of_array(
11471148
v_type->m_dims, v_type->n_dims));

0 commit comments

Comments
 (0)