Coverage Report

Created: 2022-08-24 06:40

/src/solidity/libyul/CompilabilityChecker.cpp
Line
Count
Source
1
/*(
2
  This file is part of solidity.
3
4
  solidity is free software: you can redistribute it and/or modify
5
  it under the terms of the GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
9
  solidity is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  GNU General Public License for more details.
13
14
  You should have received a copy of the GNU General Public License
15
  along with solidity.  If not, see <http://www.gnu.org/licenses/>.
16
*/
17
/**
18
 * Component that checks whether all variables are reachable on the stack.
19
 */
20
21
#include <libyul/CompilabilityChecker.h>
22
23
#include <libyul/AsmAnalysis.h>
24
#include <libyul/AsmAnalysisInfo.h>
25
26
#include <libyul/backends/evm/EVMCodeTransform.h>
27
#include <libyul/backends/evm/NoOutputAssembly.h>
28
29
using namespace std;
30
using namespace solidity;
31
using namespace solidity::yul;
32
using namespace solidity::util;
33
34
CompilabilityChecker::CompilabilityChecker(
35
  Dialect const& _dialect,
36
  Object const& _object,
37
  bool _optimizeStackAllocation
38
)
39
13.1k
{
40
13.1k
  if (auto const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
41
13.1k
  {
42
13.1k
    NoOutputEVMDialect noOutputDialect(*evmDialect);
43
44
13.1k
    yul::AsmAnalysisInfo analysisInfo =
45
13.1k
      yul::AsmAnalyzer::analyzeStrictAssertCorrect(noOutputDialect, _object);
46
47
13.1k
    BuiltinContext builtinContext;
48
13.1k
    builtinContext.currentObject = &_object;
49
13.1k
    if (!_object.name.empty())
50
13.1k
      builtinContext.subIDs[_object.name] = 1;
51
13.1k
    for (auto const& subNode: _object.subObjects)
52
629
      builtinContext.subIDs[subNode->name] = 1;
53
13.1k
    NoOutputAssembly assembly;
54
13.1k
    CodeTransform transform(
55
13.1k
      assembly,
56
13.1k
      analysisInfo,
57
13.1k
      *_object.code,
58
13.1k
      noOutputDialect,
59
13.1k
      builtinContext,
60
13.1k
      _optimizeStackAllocation
61
13.1k
    );
62
13.1k
    transform(*_object.code);
63
64
13.1k
    for (StackTooDeepError const& error: transform.stackErrors())
65
20.4k
    {
66
20.4k
      unreachableVariables[error.functionName].emplace(error.variable);
67
20.4k
      int& deficit = stackDeficit[error.functionName];
68
20.4k
      deficit = std::max(error.depth, deficit);
69
20.4k
    }
70
13.1k
  }
71
13.1k
}