/src/solidity/libyul/YulStack.h
Line | Count | Source (jump to first uncovered line) |
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 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * Full assembly stack that can support EVM-assembly and Yul as input and EVM, EVM1.5 and |
20 | | * Ewasm as output. |
21 | | */ |
22 | | |
23 | | #pragma once |
24 | | |
25 | | #include <liblangutil/CharStreamProvider.h> |
26 | | #include <liblangutil/DebugInfoSelection.h> |
27 | | #include <liblangutil/ErrorReporter.h> |
28 | | #include <liblangutil/EVMVersion.h> |
29 | | |
30 | | #include <libyul/Object.h> |
31 | | #include <libyul/ObjectParser.h> |
32 | | |
33 | | #include <libsolidity/interface/OptimiserSettings.h> |
34 | | |
35 | | #include <libevmasm/LinkerObject.h> |
36 | | |
37 | | #include <memory> |
38 | | #include <string> |
39 | | |
40 | | namespace solidity::evmasm |
41 | | { |
42 | | class Assembly; |
43 | | } |
44 | | |
45 | | namespace solidity::langutil |
46 | | { |
47 | | class Scanner; |
48 | | } |
49 | | |
50 | | namespace solidity::yul |
51 | | { |
52 | | class AbstractAssembly; |
53 | | |
54 | | |
55 | | struct MachineAssemblyObject |
56 | | { |
57 | | std::shared_ptr<evmasm::LinkerObject> bytecode; |
58 | | std::string assembly; |
59 | | std::unique_ptr<std::string> sourceMappings; |
60 | | }; |
61 | | |
62 | | /* |
63 | | * Full assembly stack that can support EVM-assembly and Yul as input and EVM, EVM1.5 and |
64 | | * Ewasm as output. |
65 | | */ |
66 | | class YulStack: public langutil::CharStreamProvider |
67 | | { |
68 | | public: |
69 | | enum class Language { Yul, Assembly, StrictAssembly, Ewasm }; |
70 | | enum class Machine { EVM, Ewasm }; |
71 | | |
72 | | YulStack(): |
73 | | YulStack( |
74 | | langutil::EVMVersion{}, |
75 | | Language::Assembly, |
76 | | solidity::frontend::OptimiserSettings::none(), |
77 | | langutil::DebugInfoSelection::Default() |
78 | | ) |
79 | 0 | {} |
80 | | |
81 | | YulStack( |
82 | | langutil::EVMVersion _evmVersion, |
83 | | Language _language, |
84 | | solidity::frontend::OptimiserSettings _optimiserSettings, |
85 | | langutil::DebugInfoSelection const& _debugInfoSelection |
86 | | ): |
87 | | m_language(_language), |
88 | | m_evmVersion(_evmVersion), |
89 | | m_optimiserSettings(std::move(_optimiserSettings)), |
90 | | m_debugInfoSelection(_debugInfoSelection), |
91 | | m_errorReporter(m_errors) |
92 | 2.80k | {} |
93 | | |
94 | | /// @returns the char stream used during parsing |
95 | | langutil::CharStream const& charStream(std::string const& _sourceName) const override; |
96 | | |
97 | | /// Runs parsing and analysis steps, returns false if input cannot be assembled. |
98 | | /// Multiple calls overwrite the previous state. |
99 | | bool parseAndAnalyze(std::string const& _sourceName, std::string const& _source); |
100 | | |
101 | | /// Run the optimizer suite. Can only be used with Yul or strict assembly. |
102 | | /// If the settings (see constructor) disabled the optimizer, nothing is done here. |
103 | | void optimize(); |
104 | | |
105 | | /// Translate the source to a different language / dialect. |
106 | | void translate(Language _targetLanguage); |
107 | | |
108 | | /// Run the assembly step (should only be called after parseAndAnalyze). |
109 | | MachineAssemblyObject assemble(Machine _machine) const; |
110 | | |
111 | | /// Run the assembly step (should only be called after parseAndAnalyze). |
112 | | /// In addition to the value returned by @a assemble, returns |
113 | | /// a second object that is the runtime code. |
114 | | /// Only available for EVM. |
115 | | std::pair<MachineAssemblyObject, MachineAssemblyObject> |
116 | | assembleWithDeployed( |
117 | | std::optional<std::string_view> _deployName = {} |
118 | | ) const; |
119 | | |
120 | | /// Run the assembly step (should only be called after parseAndAnalyze). |
121 | | /// Similar to @a assemblyWithDeployed, but returns EVM assembly objects. |
122 | | /// Only available for EVM. |
123 | | std::pair<std::shared_ptr<evmasm::Assembly>, std::shared_ptr<evmasm::Assembly>> |
124 | | assembleEVMWithDeployed( |
125 | | std::optional<std::string_view> _deployName = {} |
126 | | ) const; |
127 | | |
128 | | /// @returns the errors generated during parsing, analysis (and potentially assembly). |
129 | 0 | langutil::ErrorList const& errors() const { return m_errors; } |
130 | | |
131 | | /// Pretty-print the input after having parsed it. |
132 | | std::string print( |
133 | | langutil::CharStreamProvider const* _soliditySourceProvider = nullptr |
134 | | ) const; |
135 | | |
136 | | /// Return the parsed and analyzed object. |
137 | | std::shared_ptr<Object> parserResult() const; |
138 | | |
139 | | private: |
140 | | bool analyzeParsed(); |
141 | | bool analyzeParsed(yul::Object& _object); |
142 | | |
143 | | void compileEVM(yul::AbstractAssembly& _assembly, bool _optimize) const; |
144 | | |
145 | | void optimize(yul::Object& _object, bool _isCreation); |
146 | | |
147 | | Language m_language = Language::Assembly; |
148 | | langutil::EVMVersion m_evmVersion; |
149 | | solidity::frontend::OptimiserSettings m_optimiserSettings; |
150 | | langutil::DebugInfoSelection m_debugInfoSelection{}; |
151 | | |
152 | | std::unique_ptr<langutil::CharStream> m_charStream; |
153 | | |
154 | | bool m_analysisSuccessful = false; |
155 | | std::shared_ptr<yul::Object> m_parserResult; |
156 | | langutil::ErrorList m_errors; |
157 | | langutil::ErrorReporter m_errorReporter; |
158 | | |
159 | | std::unique_ptr<std::string> m_sourceMappings; |
160 | | }; |
161 | | |
162 | | } |