forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparam_to_const.cpp
More file actions
204 lines (172 loc) · 5.63 KB
/
Copy pathparam_to_const.cpp
File metadata and controls
204 lines (172 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <libasr/asr.h>
#include <libasr/containers.h>
#include <libasr/exception.h>
#include <libasr/asr_utils.h>
#include <libasr/asr_verify.h>
#include <libasr/pass/replace_param_to_const.h>
namespace LCompilers {
using ASR::down_cast;
using ASR::is_a;
/*
This ASR pass replaces initializer expressions with evaluated values. The function
`pass_replace_param_to_const` transforms the ASR tree in-place.
Converts:
integer, parameter :: i = 20
integer :: a = i**2 + i
to:
integer, parameter :: i = 20
integer :: a = 20**2 + 20
*/
class VarVisitor : public ASR::BaseWalkVisitor<VarVisitor>
{
private:
ASR::expr_t* asr;
public:
// Currently `al` is not used, but we might need it in the future:
Allocator &al;
VarVisitor(Allocator &al) : asr{nullptr}, al{al} {
}
void visit_Program(const ASR::Program_t &x) {
for (auto &item : x.m_symtab->get_scope()) {
if (is_a<ASR::Variable_t>(*item.second)) {
ASR::Variable_t *v = down_cast<ASR::Variable_t>(item.second);
visit_Variable(*v);
}
}
}
void visit_IntegerUnaryMinus(const ASR::IntegerUnaryMinus_t &x) {
visit_UnaryOp(x);
}
void visit_RealUnaryMinus(const ASR::RealUnaryMinus_t &x) {
visit_UnaryOp(x);
}
void visit_ComplexUnaryMinus(const ASR::ComplexUnaryMinus_t &x) {
visit_UnaryOp(x);
}
void visit_IntegerBitNot(const ASR::IntegerBitNot_t &x) {
visit_UnaryOp(x);
}
void visit_LogicalNot(const ASR::LogicalNot_t &x) {
visit_UnaryOp(x);
}
template<typename T>
void visit_UnaryOp(const T& x) {
T& x_unconst = const_cast<T&>(x);
asr = nullptr;
this->visit_expr(*x.m_arg);
if( asr != nullptr ) {
x_unconst.m_arg = asr;
}
asr = const_cast<ASR::expr_t*>(&(x.base));
}
void visit_IntegerCompare(const ASR::IntegerCompare_t& x) {
handle_Compare(x);
}
void visit_RealCompare(const ASR::RealCompare_t &x) {
handle_Compare(x);
}
void visit_ComplexCompare(const ASR::ComplexCompare_t &x) {
handle_Compare(x);
}
void visit_LogicalCompare(const ASR::LogicalCompare_t &x) {
handle_Compare(x);
}
void visit_StringCompare(const ASR::StringCompare_t &x) {
handle_Compare(x);
}
template <typename T>
void handle_Compare(const T& x) {
T& x_unconst = const_cast<T&>(x);
asr = nullptr;
this->visit_expr(*x.m_left);
if( asr != nullptr ) {
x_unconst.m_left = asr;
}
asr = nullptr;
this->visit_expr(*x.m_right);
if( asr != nullptr ) {
x_unconst.m_right = asr;
}
asr = const_cast<ASR::expr_t*>(&(x.base));
}
void visit_IntegerBinOp(const ASR::IntegerBinOp_t& x) {
handle_BinOp(x);
}
void visit_RealBinOp(const ASR::RealBinOp_t& x) {
handle_BinOp(x);
}
void visit_ComplexBinOp(const ASR::ComplexBinOp_t& x) {
handle_BinOp(x);
}
void visit_LogicalBinOp(const ASR::LogicalBinOp_t& x) {
handle_BinOp(x);
}
template <typename T>
void handle_BinOp(const T& x) {
T& x_unconst = const_cast<T&>(x);
asr = nullptr;
this->visit_expr(*x.m_left);
if( asr != nullptr ) {
x_unconst.m_left = asr;
}
asr = nullptr;
this->visit_expr(*x.m_right);
if( asr != nullptr ) {
x_unconst.m_right = asr;
}
asr = const_cast<ASR::expr_t*>(&(x.base));
}
void visit_Cast(const ASR::Cast_t& x) {
/*
asr = nullptr;
this->visit_expr(*x.m_arg);
ASR::Cast_t& x_unconst = const_cast<ASR::Cast_t&>(x);
if( asr != nullptr ) {
x_unconst.m_arg = asr;
}
*/
asr = const_cast<ASR::expr_t*>(&(x.base));
}
void visit_Var(const ASR::Var_t& x) {
if (is_a<ASR::Variable_t>(*ASRUtils::symbol_get_past_external(x.m_v))) {
ASR::Variable_t *init_var = ASR::down_cast<ASR::Variable_t>(ASRUtils::symbol_get_past_external(x.m_v));
if( init_var->m_storage == ASR::storage_typeType::Parameter ) {
if( init_var->m_symbolic_value == nullptr ) {
asr = init_var->m_symbolic_value;
} else {
switch( init_var->m_symbolic_value->type ) {
case ASR::exprType::IntegerConstant:
case ASR::exprType::RealConstant:
case ASR::exprType::ComplexConstant:
case ASR::exprType::LogicalConstant:
case ASR::exprType::StringConstant: {
asr = init_var->m_symbolic_value;
break;
}
default: {
this->visit_expr(*(init_var->m_symbolic_value));
}
}
}
}
}
}
void visit_Variable(const ASR::Variable_t& x) {
ASR::Variable_t& x_unconst = const_cast<ASR::Variable_t&>(x);
if( x.m_symbolic_value != nullptr ) {
asr = nullptr;
visit_expr(*(x.m_symbolic_value));
if( asr != nullptr ) {
x_unconst.m_symbolic_value = asr;
asr = nullptr;
}
}
}
};
void pass_replace_param_to_const(Allocator &al, ASR::TranslationUnit_t &unit,
const LCompilers::PassOptions &/*pass_options*/
) {
VarVisitor v(al);
v.visit_TranslationUnit(unit);
}
} // namespace LCompilers