summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine ELIAS <antoine.elias@scilab-enterprises.com>2017-08-17 16:33:28 +0200
committerC├ędric DELAMARRE <cedric.delamarre@scilab-enterprises.com>2017-08-18 15:50:04 +0200
commit5cbdef02fbe4db10cda38022e2320f7c0f3d494b (patch)
treed591ba0b822a2c15a08276c66946509dcd832207
parent9a6f4c446949befa0f9f9fea5f1422c7c51b84be (diff)
downloadscilab-YaSp.zip
scilab-YaSp.tar.gz
add size in constantvisitorYaSp
//test function r = test(M) if size(M) == [2 3] then r = M + 1; else r = M + 2; end end tic();for i = 1:1d4, a = test([1 2 3, 4 5 6]);end,toc analyzerOptions(1); tic();for i = 1:1d4, a = test([1 2 3, 4 5 6]);end,toc Change-Id: I5b81fb83df6137c04cd542c4486d760b6a9b8ad8
-rw-r--r--scilab/modules/ast/src/cpp/analysis/ConstantVisitor.cpp54
-rw-r--r--scilab/modules/ast/src/cpp/analysis/SizeAnalyzer.cpp50
-rw-r--r--scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp14
3 files changed, 84 insertions, 34 deletions
diff --git a/scilab/modules/ast/src/cpp/analysis/ConstantVisitor.cpp b/scilab/modules/ast/src/cpp/analysis/ConstantVisitor.cpp
index eab77f9..89d6d6c 100644
--- a/scilab/modules/ast/src/cpp/analysis/ConstantVisitor.cpp
+++ b/scilab/modules/ast/src/cpp/analysis/ConstantVisitor.cpp
@@ -15,6 +15,7 @@
15 15
16#include "AnalysisVisitor.hxx" 16#include "AnalysisVisitor.hxx"
17#include "ConstantVisitor.hxx" 17#include "ConstantVisitor.hxx"
18#include "double.hxx"
18 19
19namespace analysis 20namespace analysis
20{ 21{
@@ -259,6 +260,59 @@ void ConstantVisitor::visit(ast::CallExp & e)
259 isConstant = true; 260 isConstant = true;
260 } 261 }
261 } 262 }
263 else if (name == L"size")
264 {
265 if (parent->getAnalyzer(sym)->analyze(*parent, lhs, e))
266 {
267 switch (lhs)
268 {
269 case 1: // a = size(x)
270 {
271 std::vector<Result> & res = parent->getLHSContainer();
272 double row;
273 res.front().getConstant().getDblValue(row);
274
275 double col;
276 res.back().getConstant().getDblValue(col);
277
278 types::Double* pIT = new types::Double(1, 2);
279 pIT->get()[0] = row;
280 pIT->get()[1] = col;
281 e.replace(new ast::DoubleExp(e.getLocation(), pIT));
282 isConstant = true;
283 break;
284 }
285 case 2: // [a, b] = size(x)
286 {
287 double val;
288 ast::exps_t * exps = new ast::exps_t();
289 exps->reserve(2);
290 std::vector<Result> & res = parent->getLHSContainer();
291 res.front().getConstant().getDblValue(val);
292 exps->push_back(new ast::DoubleExp(e.getLocation(), val));
293 res.back().getConstant().getDblValue(val);
294 exps->push_back(new ast::DoubleExp(e.getLocation(), val));
295 e.replace(new ast::ArrayListExp(e.getLocation(), *exps));
296 isConstant = true;
297 break;
298 }
299 }
300 }
301 }
302 }
303 else if (parent && args.size() == 2)
304 {
305 if (name == L"size")
306 {
307 if (parent->getAnalyzer(sym)->analyze(*parent, lhs, e))
308 {
309 //a = size(x, "dims") or a = size(x, dim)
310 double val;
311 parent->getResult().getConstant().getDblValue(val);
312 e.replace(new ast::DoubleExp(e.getLocation(), val));
313 isConstant = true;
314 }
315 }
262 } 316 }
263 } 317 }
264 } 318 }
diff --git a/scilab/modules/ast/src/cpp/analysis/SizeAnalyzer.cpp b/scilab/modules/ast/src/cpp/analysis/SizeAnalyzer.cpp
index 4bf9b23..8a9d56e 100644
--- a/scilab/modules/ast/src/cpp/analysis/SizeAnalyzer.cpp
+++ b/scilab/modules/ast/src/cpp/analysis/SizeAnalyzer.cpp
@@ -29,10 +29,8 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
29 } 29 }
30 30
31 const ast::exps_t args = e.getArgs(); 31 const ast::exps_t args = e.getArgs();
32 enum Kind 32 SizeCall::Kind kind = SizeCall::DUNNO;
33 { 33
34 ROWS, COLS, ROWSTIMESCOLS, ROWSCOLS, ONE, BOTH, DUNNO
35 } kind = DUNNO;
36 const std::size_t size = args.size(); 34 const std::size_t size = args.size();
37 if (size == 0 || size >= 3) 35 if (size == 0 || size >= 3)
38 { 36 {
@@ -57,11 +55,11 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
57 case 1: 55 case 1:
58 if (lhs == 1) 56 if (lhs == 1)
59 { 57 {
60 kind = BOTH; 58 kind = SizeCall::BOTH;
61 } 59 }
62 else if (lhs == 2) 60 else if (lhs == 2)
63 { 61 {
64 kind = ROWSCOLS; 62 kind = SizeCall::R_C;
65 } 63 }
66 break; 64 break;
67 case 2: 65 case 2:
@@ -74,15 +72,15 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
74 const std::wstring & arg2 = static_cast<ast::StringExp *>(second)->getValue(); 72 const std::wstring & arg2 = static_cast<ast::StringExp *>(second)->getValue();
75 if (arg2 == L"r") 73 if (arg2 == L"r")
76 { 74 {
77 kind = ROWS; 75 kind = SizeCall::R;
78 } 76 }
79 else if (arg2 == L"c") 77 else if (arg2 == L"c")
80 { 78 {
81 kind = COLS; 79 kind = SizeCall::C;
82 } 80 }
83 else if (arg2 == L"*") 81 else if (arg2 == L"*")
84 { 82 {
85 kind = ROWSTIMESCOLS; 83 kind = SizeCall::RC;
86 } 84 }
87 else 85 else
88 { 86 {
@@ -95,16 +93,16 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
95 const double arg2 = static_cast<ast::DoubleExp *>(second)->getValue(); 93 const double arg2 = static_cast<ast::DoubleExp *>(second)->getValue();
96 if (arg2 == 1) 94 if (arg2 == 1)
97 { 95 {
98 kind = ROWS; 96 kind = SizeCall::R;
99 } 97 }
100 else if (arg2 == 2) 98 else if (arg2 == 2)
101 { 99 {
102 kind = COLS; 100 kind = SizeCall::C;
103 } 101 }
104 else if (arg2 >= 3) 102 else if (arg2 >= 3)
105 { 103 {
106 // TODO: we should handle hypermatrix 104 // TODO: we should handle hypermatrix
107 kind = ONE; 105 kind = SizeCall::ONE;
108 } 106 }
109 else 107 else
110 { 108 {
@@ -129,7 +127,7 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
129 127
130 switch (kind) 128 switch (kind)
131 { 129 {
132 case ROWS: 130 case SizeCall::R:
133 { 131 {
134 SymbolicDimension & rows = res.getType().rows; 132 SymbolicDimension & rows = res.getType().rows;
135 Result & _res = e.getDecorator().setResult(type); 133 Result & _res = e.getDecorator().setResult(type);
@@ -138,7 +136,7 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
138 visitor.setResult(_res); 136 visitor.setResult(_res);
139 break; 137 break;
140 } 138 }
141 case COLS: 139 case SizeCall::C:
142 { 140 {
143 SymbolicDimension & cols = res.getType().cols; 141 SymbolicDimension & cols = res.getType().cols;
144 Result & _res = e.getDecorator().setResult(type); 142 Result & _res = e.getDecorator().setResult(type);
@@ -147,19 +145,27 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
147 visitor.setResult(_res); 145 visitor.setResult(_res);
148 break; 146 break;
149 } 147 }
150 case ROWSTIMESCOLS: 148 case SizeCall::RC:
151 { 149 {
152 SymbolicDimension & rows = res.getType().rows; 150 SymbolicDimension & rows = res.getType().rows;
153 SymbolicDimension & cols = res.getType().cols; 151 SymbolicDimension & cols = res.getType().cols;
154 SymbolicDimension prod = rows * cols; 152 SymbolicDimension prod = rows * cols;
153
155 Result & _res = e.getDecorator().setResult(type); 154 Result & _res = e.getDecorator().setResult(type);
156 _res.getConstant() = prod.getValue(); 155 _res.getConstant() = prod.getValue();
157 e.getDecorator().setCall(new SizeCall(SizeCall::RC)); 156 e.getDecorator().setCall(new SizeCall(SizeCall::RC));
158 visitor.setResult(_res); 157 visitor.setResult(_res);
159 break; 158 break;
160 } 159 }
161 case ROWSCOLS: 160 case SizeCall::R_C:
161 case SizeCall::BOTH:
162 { 162 {
163 if (kind == SizeCall::BOTH)
164 {
165 TIType _type(visitor.getGVN(), TIType::DOUBLE, 1, 2);
166 Result & _res = e.getDecorator().setResult(_type);
167 }
168
163 SymbolicDimension & rows = res.getType().rows; 169 SymbolicDimension & rows = res.getType().rows;
164 SymbolicDimension & cols = res.getType().cols; 170 SymbolicDimension & cols = res.getType().cols;
165 std::vector<Result> & mlhs = visitor.getLHSContainer(); 171 std::vector<Result> & mlhs = visitor.getLHSContainer();
@@ -170,10 +176,10 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
170 mlhs.emplace_back(type); 176 mlhs.emplace_back(type);
171 mlhs.back().getConstant() = cols.getValue(); 177 mlhs.back().getConstant() = cols.getValue();
172 178
173 e.getDecorator().setCall(new SizeCall(SizeCall::R_C)); 179 e.getDecorator().setCall(new SizeCall(kind));
174 break; 180 break;
175 } 181 }
176 case ONE: 182 case SizeCall::ONE:
177 { 183 {
178 Result & _res = e.getDecorator().setResult(type); 184 Result & _res = e.getDecorator().setResult(type);
179 _res.getConstant() = new types::Double(1); 185 _res.getConstant() = new types::Double(1);
@@ -181,14 +187,6 @@ bool SizeAnalyzer::analyze(AnalysisVisitor & visitor, const unsigned int lhs, as
181 visitor.setResult(_res); 187 visitor.setResult(_res);
182 break; 188 break;
183 } 189 }
184 case BOTH:
185 {
186 TIType _type(visitor.getGVN(), TIType::DOUBLE, 1, 2);
187 Result & _res = e.getDecorator().setResult(_type);
188 e.getDecorator().setCall(new SizeCall(SizeCall::BOTH));
189 visitor.setResult(_res);
190 break;
191 }
192 default: 190 default:
193 return false; 191 return false;
194 } 192 }
diff --git a/scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp b/scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp
index fc67b8f..73317fd 100644
--- a/scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp
+++ b/scilab/modules/ast/src/cpp/analysis/VisitAssignExp.cpp
@@ -48,14 +48,7 @@ void AnalysisVisitor::visit(ast::AssignExp & e)
48 48
49 if (e.getRightExp().isCallExp()) // A = foo(...) 49 if (e.getRightExp().isCallExp()) // A = foo(...)
50 { 50 {
51 if (e.getRightExp().isCallExp()) 51 visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ 1);
52 {
53 visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ 1);
54 }
55 else
56 {
57 e.getRightExp().accept(*this);
58 }
59 } 52 }
60 else // A = 1 + 2 53 else // A = 1 + 2
61 { 54 {
@@ -100,6 +93,11 @@ void AnalysisVisitor::visit(ast::AssignExp & e)
100 if (e.getRightExp().isCallExp()) 93 if (e.getRightExp().isCallExp())
101 { 94 {
102 const ast::exps_t & exps = ale.getExps(); 95 const ast::exps_t & exps = ale.getExps();
96
97 // apply the ConstantVisitor
98 cv.setLHS(exps.size());
99 e.getRightExp().accept(cv);
100
103 visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ exps.size()); 101 visit(static_cast<ast::CallExp &>(e.getRightExp()), /* LHS */ exps.size());
104 std::vector<Result>::iterator j = multipleLHS.begin(); 102 std::vector<Result>::iterator j = multipleLHS.begin();
105 for (const auto exp : exps) 103 for (const auto exp : exps)