Skip to content
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

Enable data implied do #3

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Prev Previous commit
progress update
konradha committed Oct 19, 2022
commit 6202f721de620a6944d3a7ea4939332186f67903
55 changes: 36 additions & 19 deletions src/lfortran/semantics/ast_body_visitor.cpp
Original file line number Diff line number Diff line change
@@ -97,6 +97,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
body.push_back(al, impl_decl);
}
}

body.push_back(al, tmp_stmt);
}
// To avoid last statement to be entered twice once we exit this node
@@ -467,6 +468,17 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
incr = AST::down_cast<AST::Num_t>(x.m_increment);
}


ASR::expr_t *s, *e, *step;
visit_expr(*x.m_start); s = ASRUtils::EXPR(tmp);
visit_expr(*x.m_end); e = ASRUtils::EXPR(tmp);
if (x.m_increment != nullptr) {
visit_expr(*x.m_increment);
step = ASRUtils::EXPR(tmp);
} else {
step = nullptr;
}

// std::array<{AST::FuncCallOrArray_t, std::array<std::variant<ASR::IntegerConstant, ASR::Variable_t>> of size (args.n-1)}>

std::map<AST::FuncCallOrArray_t *, std::vector<ASR::expr_t *> > func_calls;
@@ -512,6 +524,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
if (incr != nullptr) iter = incr->m_n;
auto els = func_calls[arr];
std::cout << "start = " << start->m_n << ", end = " << end->m_n << ", iter = " << iter << "\n";
size_t r = (end->m_n - start->m_n) % iter;
size_t steps = (end->m_n - start->m_n) / iter;
if (r) steps += r;

// Vec<array_index_t> v_args;
// ASR::asr_t* duplicate_ArrayItem(ArrayItem_t* x)
@@ -533,6 +548,24 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
}
}

// make_ArrayItem_t(Allocator &al, const Location &a_loc, expr_t* a_v, array_index_t* a_args, size_t n_args, ttype_t* a_type, expr_t* a_value)

Vec<ASR::array_index_t> arr_args;
arr_args.reserve(al, 1);
auto var_exp = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, arr_symbol));
ASR::array_index_t ai;
ai.loc = x.base.base.loc;
ai.m_left = s;
ai.m_right = e;
ai.m_step = step;
arr_args.push_back(al, ai);

Vec<ASR::dimension_t> empty_dims;
empty_dims.reserve(al, 1);
duplicated_type = ASRUtils::duplicate_type(al, duplicated_type, &empty_dims);

tmp = ASR::make_ArrayItem_t(al, x.base.base.loc, var_exp, arr_args.p, arr_args.n, duplicated_type, nullptr);

// ASR::ttype_t *t = LFortran::ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
// 4, nullptr, 0));

@@ -554,25 +587,7 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// auto var_var = ASRUtils::EXPR(ASR::make_Var_t(al, x.base.base.loc, arr_symbol));


Vec<ASR::dimension_t> dims;
dims.reserve(al, 1);
ASR::dimension_t dim;
dim.loc = x.base.base.loc;
ASR::ttype_t *int32_type = ASRUtils::TYPE(ASR::make_Integer_t(al, x.base.base.loc,
4, nullptr, 0));
ASR::expr_t* one = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc, 1, int32_type));
ASR::expr_t* x_n_args = ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, x.base.base.loc,
1, int32_type));
Vec<ASR::expr_t*> body;
body.reserve(al, 1);
dim.m_start = one;
dim.m_length = x_n_args;
dims.push_back(al, dim);
duplicated_type = ASRUtils::duplicate_type(al, duplicated_type, &dims);
tmp = ASR::make_ArrayConstant_t(al, x.base.base.loc, body.p,
body.n, duplicated_type);

return;

// TODO
// ASR::make_Array

@@ -614,7 +629,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
// std::string array = to_lower(arr->m_func);
// auto sym = current_scope->resolve_symbol(array);
// if (sym == nullptr /*&& !compiler_options.implicit_typing*/) throw SemanticError("Data Statement Variable not declared.", x.base.base.loc);

} else if (AST::is_a<AST::DataImpliedDo_t>(*obj)) {
// TODO this case needs to be solved!
throw SemanticError("Data implied do can be nested, apparently.", x.base.base.loc);
} else {
std::cout << "type is " << obj->type << "\n";
23 changes: 18 additions & 5 deletions tests/fixed_form/implied_do1.f
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
subroutine dt1()
double precision coef(5,4)
double precision cpy_coef(5,4)

double precision coef2(5, 4, 4)

@@ -18,12 +19,24 @@ subroutine dt1()
integer i
!DATA (coef(i,1),i=1,5)/2*0.0D0, 1*1.0D0, 2*2.0D0/
!DATA (coef(i,1),i=1,5)/0.0D0, 0.0D0, 1.0D0, 2.0D0, 2.0D0/
DATA (coef(i,1),i=1,1)/0.0D0/
coef2(1,1,1) = 1.0d00

do i=1,5
print *, coef(i, 1)
end do


! EXAMPLE THAT NEEDS TO WORK
!DATA (coef(i,1),i=1,4,2)/0.0D0/

! workaround with current bug
DATA (coef(i,1),i=1,4,2)/0.0D0, 1.0D0/

! sanity check line
!DATA cpy_coef(1,1), coef(2,1), coef(4,1)/0.0D0, 0.0D0, 1.0D0/

! same thing, different dimensions
!DATA coef2(1,1,1) / 1.0d00 /

!do i=1,5
! print *, coef(i, 1)
!end do
END


105 changes: 105 additions & 0 deletions understand_dims
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
(Real 8
[
(
(
IntegerConstant 1 (Integer 4 [])
)

(
IntegerConstant 5 (Integer 4 [])
)
)

(
(
IntegerConstant 1 (Integer 4 [])
)
(
IntegerConstant 4 (Integer 4 [])
)
)
]
)


and the actual dimension declaration is


double precision coef(5,4)


















DATA cpy_coef(1,1), coef(2,1), coef(4,1)/0.0D0, 0.0D0, 0.0D0/

gets represented as


[(=
(ArrayItem
(Var 2 cpy_coef)
[(()
(IntegerConstant 1 (Integer 4 []))
())
(()
(IntegerConstant 1 (Integer 4 []))
())]
(Real 8 [])
()
)
(RealConstant
0.000000
(Real 8 [])
)
()
)
(=
(ArrayItem
(Var 2 coef)
[(()
(IntegerConstant 2 (Integer 4 []))
())
(()
(IntegerConstant 1 (Integer 4 []))
())]
(Real 8 [])
()
)
(RealConstant
0.000000
(Real 8 [])
)
()
)
(=
(ArrayItem
(Var 2 coef)
[(()
(IntegerConstant 4 (Integer 4 []))
())
(()
(IntegerConstant 1 (Integer 4 []))
())]
(Real 8 [])
()
)
(RealConstant
0.000000
(Real 8 [])
)
()
)]