-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathAsmJsonImporter.cpp
359 lines (302 loc) · 11 KB
/
AsmJsonImporter.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @author julius <[email protected]>
* @date 2019
* Converts an inlineAssembly AST from JSON format to AsmData
*/
#include <libyul/AsmJsonImporter.h>
#include <libyul/AST.h>
#include <libyul/Dialect.h>
#include <libyul/Exceptions.h>
#include <libyul/Utilities.h>
#include <liblangutil/Exceptions.h>
#include <liblangutil/Scanner.h>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>
using namespace solidity::langutil;
namespace solidity::yul
{
using SourceLocation = langutil::SourceLocation;
SourceLocation const AsmJsonImporter::createSourceLocation(Json const& _node)
{
yulAssert(member(_node, "src").is_string(), "'src' must be a string");
return solidity::langutil::parseSourceLocation(_node["src"].get<std::string>(), m_sourceNames);
}
AST AsmJsonImporter::createAST(solidity::Json const& _node)
{
return {m_dialect, createBlock(_node)};
}
template <class T>
T AsmJsonImporter::createAsmNode(Json const& _node)
{
T r;
SourceLocation nativeLocation = createSourceLocation(_node);
yulAssert(nativeLocation.hasText(), "Invalid source location in Asm AST");
// TODO: We should add originLocation to the AST.
// While it's not included, we'll use nativeLocation for it because we only support importing
// inline assembly as a part of a Solidity AST and there these locations are always the same.
r.debugData = DebugData::create(nativeLocation, nativeLocation);
return r;
}
Json AsmJsonImporter::member(Json const& _node, std::string const& _name)
{
if (!_node.contains(_name))
return Json();
return _node[_name];
}
NameWithDebugData AsmJsonImporter::createNameWithDebugData(Json const& _node)
{
auto nameWithDebugData = createAsmNode<NameWithDebugData>(_node);
nameWithDebugData.name = YulName{member(_node, "name").get<std::string>()};
return nameWithDebugData;
}
Statement AsmJsonImporter::createStatement(Json const& _node)
{
Json jsonNodeType = member(_node, "nodeType");
yulAssert(jsonNodeType.is_string(), "Expected \"nodeType\" to be of type string!");
std::string nodeType = jsonNodeType.get<std::string>();
yulAssert(nodeType.substr(0, 3) == "Yul", "Invalid nodeType prefix");
nodeType = nodeType.substr(3);
if (nodeType == "ExpressionStatement")
return createExpressionStatement(_node);
else if (nodeType == "Assignment")
return createAssignment(_node);
else if (nodeType == "VariableDeclaration")
return createVariableDeclaration(_node);
else if (nodeType == "FunctionDefinition")
return createFunctionDefinition(_node);
else if (nodeType == "If")
return createIf(_node);
else if (nodeType == "Switch")
return createSwitch(_node);
else if (nodeType == "ForLoop")
return createForLoop(_node);
else if (nodeType == "Break")
return createBreak(_node);
else if (nodeType == "Continue")
return createContinue(_node);
else if (nodeType == "Leave")
return createLeave(_node);
else if (nodeType == "Block")
return createBlock(_node);
else
yulAssert(false, "Invalid nodeType as statement");
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
util::unreachable();
}
Expression AsmJsonImporter::createExpression(Json const& _node)
{
Json jsonNodeType = member(_node, "nodeType");
yulAssert(jsonNodeType.is_string(), "Expected \"nodeType\" to be of type string!");
std::string nodeType = jsonNodeType.get<std::string>();
yulAssert(nodeType.substr(0, 3) == "Yul", "Invalid nodeType prefix");
nodeType = nodeType.substr(3);
if (nodeType == "FunctionCall")
return createFunctionCall(_node);
else if (nodeType == "Identifier")
return createIdentifier(_node);
else if (nodeType == "Literal")
return createLiteral(_node);
else
yulAssert(false, "Invalid nodeType as expression");
// FIXME: Workaround for spurious GCC 12.1 warning (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105794)
util::unreachable();
}
std::vector<Expression> AsmJsonImporter::createExpressionVector(Json const& _array)
{
std::vector<Expression> ret;
for (auto& var: _array)
ret.emplace_back(createExpression(var));
return ret;
}
std::vector<Statement> AsmJsonImporter::createStatementVector(Json const& _array)
{
std::vector<Statement> ret;
for (auto& var: _array)
ret.emplace_back(createStatement(var));
return ret;
}
Block AsmJsonImporter::createBlock(Json const& _node)
{
auto block = createAsmNode<Block>(_node);
block.statements = createStatementVector(_node["statements"]);
return block;
}
Literal AsmJsonImporter::createLiteral(Json const& _node)
{
auto lit = createAsmNode<Literal>(_node);
std::string kind = member(_node, "kind").get<std::string>();
solAssert(member(_node, "hexValue").is_string() || member(_node, "value").is_string(), "");
std::string value;
if (_node.contains("hexValue"))
value = util::asString(util::fromHex(member(_node, "hexValue").get<std::string>()));
else
value = member(_node, "value").get<std::string>();
{
auto const typeNode = member(_node, "type");
yulAssert(
typeNode.empty() || typeNode.get<std::string>().empty(),
fmt::format(
"Expected literal types to be either empty or absent in the JSON. Got \"{}\".",
typeNode.get<std::string>()
)
);
}
if (kind == "number")
{
langutil::CharStream charStream(value, "");
langutil::Scanner scanner{charStream};
lit.kind = LiteralKind::Number;
yulAssert(
scanner.currentToken() == Token::Number,
"Expected number but got " + langutil::TokenTraits::friendlyName(scanner.currentToken()) + std::string(" while scanning ") + value
);
}
else if (kind == "bool")
{
langutil::CharStream charStream(value, "");
langutil::Scanner scanner{charStream};
lit.kind = LiteralKind::Boolean;
yulAssert(
scanner.currentToken() == Token::TrueLiteral ||
scanner.currentToken() == Token::FalseLiteral,
"Expected true/false literal!"
);
}
else if (kind == "string")
{
lit.kind = LiteralKind::String;
yulAssert(
value.size() <= 32,
"String literal too long (" + std::to_string(value.size()) + " > 32)"
);
}
else
yulAssert(false, "unknown type of literal");
// import only for inline assembly, no unlimited string literals there
lit.value = valueOfLiteral(value, lit.kind, false /* _unlimitedLiteralArgument */);
yulAssert(validLiteral(lit));
return lit;
}
Leave AsmJsonImporter::createLeave(Json const& _node)
{
return createAsmNode<Leave>(_node);
}
Identifier AsmJsonImporter::createIdentifier(Json const& _node)
{
auto identifier = createAsmNode<Identifier>(_node);
identifier.name = YulName(member(_node, "name").get<std::string>());
return identifier;
}
Assignment AsmJsonImporter::createAssignment(Json const& _node)
{
auto assignment = createAsmNode<Assignment>(_node);
if (_node.contains("variableNames"))
for (auto const& var: member(_node, "variableNames"))
assignment.variableNames.emplace_back(createIdentifier(var));
assignment.value = std::make_unique<Expression>(createExpression(member(_node, "value")));
return assignment;
}
FunctionCall AsmJsonImporter::createFunctionCall(Json const& _node)
{
auto functionCall = createAsmNode<FunctionCall>(_node);
for (auto const& var: member(_node, "arguments"))
functionCall.arguments.emplace_back(createExpression(var));
auto const functionNameNode = member(_node, "functionName");
auto const name = member(functionNameNode, "name").get<std::string>();
if (std::optional<BuiltinHandle> builtinHandle = m_dialect.findBuiltin(name))
{
auto builtin = createAsmNode<BuiltinName>(functionNameNode);
builtin.handle = *builtinHandle;
functionCall.functionName = builtin;
}
else
functionCall.functionName = createIdentifier(functionNameNode);
return functionCall;
}
ExpressionStatement AsmJsonImporter::createExpressionStatement(Json const& _node)
{
auto statement = createAsmNode<ExpressionStatement>(_node);
statement.expression = createExpression(member(_node, "expression"));
return statement;
}
VariableDeclaration AsmJsonImporter::createVariableDeclaration(Json const& _node)
{
auto varDec = createAsmNode<VariableDeclaration>(_node);
for (auto const& var: member(_node, "variables"))
varDec.variables.emplace_back(createNameWithDebugData(var));
if (_node.contains("value"))
varDec.value = std::make_unique<Expression>(createExpression(member(_node, "value")));
return varDec;
}
FunctionDefinition AsmJsonImporter::createFunctionDefinition(Json const& _node)
{
auto funcDef = createAsmNode<FunctionDefinition>(_node);
funcDef.name = YulName{member(_node, "name").get<std::string>()};
if (_node.contains("parameters"))
for (auto const& var: member(_node, "parameters"))
funcDef.parameters.emplace_back(createNameWithDebugData(var));
if (_node.contains("returnVariables"))
for (auto const& var: member(_node, "returnVariables"))
funcDef.returnVariables.emplace_back(createNameWithDebugData(var));
funcDef.body = createBlock(member(_node, "body"));
return funcDef;
}
If AsmJsonImporter::createIf(Json const& _node)
{
auto ifStatement = createAsmNode<If>(_node);
ifStatement.condition = std::make_unique<Expression>(createExpression(member(_node, "condition")));
ifStatement.body = createBlock(member(_node, "body"));
return ifStatement;
}
Case AsmJsonImporter::createCase(Json const& _node)
{
auto caseStatement = createAsmNode<Case>(_node);
auto const& value = member(_node, "value");
if (value.is_string())
yulAssert(value.get<std::string>() == "default", "Expected default case");
else
caseStatement.value = std::make_unique<Literal>(createLiteral(value));
caseStatement.body = createBlock(member(_node, "body"));
return caseStatement;
}
Switch AsmJsonImporter::createSwitch(Json const& _node)
{
auto switchStatement = createAsmNode<Switch>(_node);
switchStatement.expression = std::make_unique<Expression>(createExpression(member(_node, "expression")));
for (auto const& var: member(_node, "cases"))
switchStatement.cases.emplace_back(createCase(var));
return switchStatement;
}
ForLoop AsmJsonImporter::createForLoop(Json const& _node)
{
auto forLoop = createAsmNode<ForLoop>(_node);
forLoop.pre = createBlock(member(_node, "pre"));
forLoop.condition = std::make_unique<Expression>(createExpression(member(_node, "condition")));
forLoop.post = createBlock(member(_node, "post"));
forLoop.body = createBlock(member(_node, "body"));
return forLoop;
}
Break AsmJsonImporter::createBreak(Json const& _node)
{
return createAsmNode<Break>(_node);
}
Continue AsmJsonImporter::createContinue(Json const& _node)
{
return createAsmNode<Continue>(_node);
}
}