Skip to content

Commit

Permalink
Merge pull request lfortran#3744 from HarshitaKalani/asrbuilder
Browse files Browse the repository at this point in the history
enh: Refactor ASR builder
  • Loading branch information
certik authored Apr 22, 2024
2 parents 27b07a1 + 13a8227 commit 8c12ec7
Show file tree
Hide file tree
Showing 8 changed files with 714 additions and 899 deletions.
8 changes: 4 additions & 4 deletions src/lfortran/semantics/ast_common_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3630,7 +3630,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
// use 0 based indexing for string slice, so subtract 1 from left index
int32_t offset = 1;
ASRUtils::ASRBuilder b(al, loc);
ASR::expr_t* const_1 = b.i(offset, int_type);
ASR::expr_t* const_1 = b.i_t(offset, int_type);
ASR::expr_t* a_value = nullptr;
if (ASR::is_a<ASR::IntegerConstant_t>(*args[0].m_left)) {
int64_t a = ASR::down_cast<ASR::IntegerConstant_t>(
Expand All @@ -3639,7 +3639,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
a, int_type)));
}
ASR::expr_t* casted_left = CastingUtil::perform_casting(args[0].m_left, int_type, al, loc);
l = b.i_vSub(casted_left, const_1, a_value);
l = b.Sub(casted_left, const_1, a_value);
}
if (m_args[0].m_end) {
r = args[0].m_right;
Expand All @@ -3655,7 +3655,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
if (l && r) {
// TODO: Handle `args[0].m_step`
ASRUtils::ASRBuilder b(al, loc);
a_len_expr = b.iSub(r, l);
a_len_expr = b.Sub(r, l);
a_len = -3;
}
char_type = ASRUtils::TYPE(ASR::make_Character_t(al, loc,
Expand Down Expand Up @@ -5220,7 +5220,7 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
int kind = ASRUtils::extract_kind_from_ttype_t(ASRUtils::expr_type(args[0]));
ASR::ttype_t *type = ASRUtils::TYPE(ASR::make_Integer_t(al, loc, kind));
ASRUtils::ASRBuilder b(al, loc);
args.push_back(al, b.i(kind, type));
args.push_back(al, b.i_t(kind, type));
}
}
}
Expand Down
758 changes: 293 additions & 465 deletions src/libasr/asr_builder.h

Large diffs are not rendered by default.

9 changes: 3 additions & 6 deletions src/libasr/pass/array_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,6 @@ class ReplaceArrayOp: public ASR::BaseExprReplacer<ReplaceArrayOp> {
ASR::expr_t* start_value = ASRUtils::expr_value(x_dim.m_start);
ASR::expr_t* end_value = ASRUtils::expr_value(x->m_args[i].m_right);
ASR::expr_t* step_value = ASRUtils::expr_value(x->m_args[i].m_step);
ASR::expr_t* length_value = nullptr;
if( ASRUtils::is_value_constant(start_value) &&
ASRUtils::is_value_constant(end_value) &&
ASRUtils::is_value_constant(step_value) ) {
Expand All @@ -731,8 +730,6 @@ class ReplaceArrayOp: public ASR::BaseExprReplacer<ReplaceArrayOp> {
if( !ASRUtils::extract_value(step_value, const_step) ) {
LCOMPILERS_ASSERT(false);
}
length_value = make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t,
((const_end - const_start)/const_step) + 1, 4, loc);
}

ASR::expr_t* m_right = x->m_args[i].m_right;
Expand All @@ -741,9 +738,9 @@ class ReplaceArrayOp: public ASR::BaseExprReplacer<ReplaceArrayOp> {
m_right = CastingUtil::perform_casting(m_right, int32_type, al, loc);
m_left = CastingUtil::perform_casting(m_left, int32_type, al, loc);
m_step = CastingUtil::perform_casting(m_step, int32_type, al, loc);
x_dim.m_length = builder.ElementalAdd(builder.ElementalDiv(
builder.ElementalSub(m_right, m_left, loc),
m_step, loc), i32_one, loc, length_value);
x_dim.m_length = builder.Add(builder.Div(
builder.Sub(m_right, m_left),
m_step), i32_one);
x_dims.push_back(al, x_dim);
}
}
Expand Down
49 changes: 25 additions & 24 deletions src/libasr/pass/implied_do_loops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
ASR::expr_t* d = implied_doloop->m_increment;
ASR::expr_t* implied_doloop_size = nullptr;
int kind = ASRUtils::extract_kind_from_ttype_t(ASRUtils::expr_type(end));
start = builder.i2i_t(start, ASRUtils::expr_type(end));
if( d == nullptr ) {
implied_doloop_size = builder.ElementalAdd(
builder.ElementalSub(end, start, loc),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, kind, loc), loc);
implied_doloop_size = builder.Add(
builder.Sub(end, start),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, kind, loc));
} else {
implied_doloop_size = builder.ElementalAdd(builder.ElementalDiv(
builder.ElementalSub(end, start, loc), d, loc),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, kind, loc), loc);
implied_doloop_size = builder.Add(builder.Div(
builder.Sub(end, start), d),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, kind, loc));
}
int const_elements = 0;
ASR::expr_t* implied_doloop_size_ = nullptr;
Expand All @@ -64,9 +65,9 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
implied_doloop_size_ = get_ImpliedDoLoop_size(
ASR::down_cast<ASR::ImpliedDoLoop_t>(implied_doloop->m_values[i]));
} else {
implied_doloop_size_ = builder.ElementalAdd(get_ImpliedDoLoop_size(
implied_doloop_size_ = builder.Add(get_ImpliedDoLoop_size(
ASR::down_cast<ASR::ImpliedDoLoop_t>(implied_doloop->m_values[i])),
implied_doloop_size_, loc);
implied_doloop_size_);
}
} else {
const_elements += 1;
Expand All @@ -77,14 +78,14 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
implied_doloop_size_ = make_ConstantWithKind(make_IntegerConstant_t,
make_Integer_t, const_elements, kind, loc);
} else {
implied_doloop_size_ = builder.ElementalAdd(
implied_doloop_size_ = builder.Add(
make_ConstantWithKind(make_IntegerConstant_t,
make_Integer_t, const_elements, kind, loc),
implied_doloop_size_, loc);
implied_doloop_size_);
}
}
if( implied_doloop_size_ ) {
implied_doloop_size = builder.ElementalMul(implied_doloop_size_, implied_doloop_size, loc);
implied_doloop_size = builder.Mul(implied_doloop_size_, implied_doloop_size);
}
return implied_doloop_size;
}
Expand All @@ -111,8 +112,8 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
if( array_size == nullptr ) {
array_size = element_array_size;
} else {
array_size = builder.ElementalAdd(array_size,
element_array_size, x->base.base.loc);
array_size = builder.Add(array_size,
element_array_size);
}
}
} else if( ASR::is_a<ASR::ArrayConstructor_t>(*element) ) {
Expand All @@ -121,8 +122,8 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
if( array_size == nullptr ) {
array_size = element_array_size;
} else {
array_size = builder.ElementalAdd(array_size,
element_array_size, x->base.base.loc);
array_size = builder.Add(array_size,
element_array_size);
}
} else if( ASR::is_a<ASR::Var_t>(*element) ) {
ASR::ttype_t* element_type = ASRUtils::type_get_past_allocatable(
Expand All @@ -137,8 +138,8 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
if( array_size == nullptr ) {
array_size = element_array_size;
} else {
array_size = builder.ElementalAdd(array_size,
element_array_size, x->base.base.loc);
array_size = builder.Add(array_size,
element_array_size);
}
}
} else {
Expand All @@ -148,7 +149,7 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
ASR::expr_t* implied_doloop_size = get_ImpliedDoLoop_size(
ASR::down_cast<ASR::ImpliedDoLoop_t>(element));
if( array_size ) {
array_size = builder.ElementalAdd(implied_doloop_size, array_size, loc);
array_size = builder.Add(implied_doloop_size, array_size);
} else {
array_size = implied_doloop_size;
}
Expand All @@ -162,19 +163,19 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
if( d == nullptr ) {
continue;
}
ASR::expr_t* dim_size = builder.ElementalAdd(builder.ElementalDiv(
builder.ElementalSub(end, start, loc), d, loc),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, 4, loc), loc);
ASR::expr_t* dim_size = builder.Add(builder.Div(
builder.Sub(end, start), d),
make_ConstantWithKind(make_IntegerConstant_t, make_Integer_t, 1, 4, loc));
if( array_section_size == nullptr ) {
array_section_size = dim_size;
} else {
array_section_size = builder.ElementalMul(array_section_size, dim_size, loc);
array_section_size = builder.Mul(array_section_size, dim_size);
}
}
if( array_size == nullptr ) {
array_size = array_section_size;
} else {
builder.ElementalAdd(array_section_size, array_size, loc);
builder.Add(array_section_size, array_size);
}
} else {
constant_size += 1;
Expand All @@ -192,7 +193,7 @@ class ReplaceArrayConstant: public ASR::BaseExprReplacer<ReplaceArrayConstant> {
}
}
if( constant_size_asr ) {
array_size = builder.ElementalAdd(array_size, constant_size_asr, x->base.base.loc);
array_size = builder.Add(array_size, constant_size_asr);
}
is_allocatable = true;
if( array_size == nullptr ) {
Expand Down
Loading

0 comments on commit 8c12ec7

Please sign in to comment.