solidity/libsolidity/inlineasm/AsmParser.cpp

617 lines
18 KiB
C++
Raw Normal View History

2016-02-22 01:13:41 +00:00
/*
This file is part of solidity.
2016-02-22 01:13:41 +00:00
solidity is free software: you can redistribute it and/or modify
2016-02-22 01:13:41 +00:00
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,
2016-02-22 01:13:41 +00:00
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/>.
2016-02-22 01:13:41 +00:00
*/
/**
* @author Christian <c@ethdev.com>
* @date 2016
* Solidity inline assembly parser.
*/
#include <libsolidity/inlineasm/AsmParser.h>
#include <libsolidity/parsing/Scanner.h>
#include <libsolidity/interface/ErrorReporter.h>
#include <boost/algorithm/string.hpp>
2016-02-22 01:13:41 +00:00
#include <ctype.h>
#include <algorithm>
using namespace std;
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::assembly;
2016-02-22 01:13:41 +00:00
shared_ptr<assembly::Block> Parser::parse(std::shared_ptr<Scanner> const& _scanner, bool _reuseScanner)
2016-02-22 01:13:41 +00:00
{
m_recursionDepth = 0;
2016-02-22 01:13:41 +00:00
try
{
m_scanner = _scanner;
auto block = make_shared<Block>(parseBlock());
if (!_reuseScanner)
expectToken(Token::EOS);
return block;
2016-02-22 01:13:41 +00:00
}
catch (FatalError const&)
{
if (m_errorReporter.errors().empty())
2016-02-22 01:13:41 +00:00
throw; // Something is weird here, rather throw again.
}
return nullptr;
}
assembly::Block Parser::parseBlock()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2016-04-18 11:47:40 +00:00
assembly::Block block = createWithLocation<Block>();
2016-02-22 01:13:41 +00:00
expectToken(Token::LBrace);
while (currentToken() != Token::RBrace)
2016-02-22 01:13:41 +00:00
block.statements.emplace_back(parseStatement());
2016-04-18 11:47:40 +00:00
block.location.end = endPosition();
advance();
2016-02-22 01:13:41 +00:00
return block;
}
assembly::Statement Parser::parseStatement()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::Let:
return parseVariableDeclaration();
2017-01-31 22:59:41 +00:00
case Token::Function:
return parseFunctionDefinition();
2016-02-22 01:13:41 +00:00
case Token::LBrace:
return parseBlock();
case Token::If:
{
assembly::If _if = createWithLocation<assembly::If>();
m_scanner->next();
2017-12-08 13:01:22 +00:00
_if.condition = make_shared<Expression>(parseExpression());
_if.body = parseBlock();
return _if;
}
case Token::Switch:
{
assembly::Switch _switch = createWithLocation<assembly::Switch>();
m_scanner->next();
2017-12-08 13:01:22 +00:00
_switch.expression = make_shared<Expression>(parseExpression());
while (m_scanner->currentToken() == Token::Case)
_switch.cases.emplace_back(parseCase());
if (m_scanner->currentToken() == Token::Default)
2017-05-19 17:04:40 +00:00
_switch.cases.emplace_back(parseCase());
if (m_scanner->currentToken() == Token::Default)
fatalParserError("Only one default case allowed.");
else if (m_scanner->currentToken() == Token::Case)
fatalParserError("Case not allowed after default case.");
if (_switch.cases.size() == 0)
fatalParserError("Switch statement without any cases.");
_switch.location.end = _switch.cases.back().body.location.end;
return _switch;
}
case Token::For:
return parseForLoop();
2016-02-22 01:13:41 +00:00
case Token::Assign:
{
if (m_flavour != AsmFlavour::Loose)
break;
2017-05-24 00:02:11 +00:00
assembly::StackAssignment assignment = createWithLocation<assembly::StackAssignment>();
advance();
2016-02-22 01:13:41 +00:00
expectToken(Token::Colon);
2016-04-18 11:47:40 +00:00
assignment.variableName.location = location();
assignment.variableName.name = currentLiteral();
if (instructions().count(assignment.variableName.name))
2017-01-26 12:52:02 +00:00
fatalParserError("Identifier expected, got instruction name.");
2016-04-18 11:47:40 +00:00
assignment.location.end = endPosition();
2016-02-22 01:13:41 +00:00
expectToken(Token::Identifier);
2016-04-18 11:47:40 +00:00
return assignment;
2016-02-22 01:13:41 +00:00
}
default:
break;
}
// Options left:
// Simple instruction (might turn into functional),
// literal,
// identifier (might turn into label or functional assignment)
ElementaryOperation elementary(parseElementaryOperation());
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::LParen:
2017-12-08 13:01:22 +00:00
{
Expression expr = parseCall(std::move(elementary));
return ExpressionStatement{locationOf(expr), expr};
}
case Token::Comma:
{
// if a comma follows, a multiple assignment is assumed
2017-12-08 13:01:22 +00:00
if (elementary.type() != typeid(assembly::Identifier))
fatalParserError("Label name / variable name must precede \",\" (multiple assignment).");
2017-12-08 13:01:22 +00:00
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(elementary);
Assignment assignment = createWithLocation<Assignment>(identifier.location);
assignment.variableNames.emplace_back(identifier);
do
{
expectToken(Token::Comma);
elementary = parseElementaryOperation();
2017-12-08 13:01:22 +00:00
if (elementary.type() != typeid(assembly::Identifier))
fatalParserError("Variable name expected in multiple assignment.");
2017-12-08 13:01:22 +00:00
assignment.variableNames.emplace_back(boost::get<assembly::Identifier>(elementary));
}
while (currentToken() == Token::Comma);
expectToken(Token::Colon);
expectToken(Token::Assign);
2017-12-08 13:01:22 +00:00
assignment.value.reset(new Expression(parseExpression()));
assignment.location.end = locationOf(*assignment.value).end;
return assignment;
}
2016-02-22 01:13:41 +00:00
case Token::Colon:
{
2017-12-08 13:01:22 +00:00
if (elementary.type() != typeid(assembly::Identifier))
2016-02-22 01:13:41 +00:00
fatalParserError("Label name / variable name must precede \":\".");
2017-12-08 13:01:22 +00:00
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(elementary);
advance();
// identifier:=: should be parsed as identifier: =: (i.e. a label),
// while identifier:= (being followed by a non-colon) as identifier := (assignment).
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
2016-02-22 01:13:41 +00:00
{
assembly::Assignment assignment = createWithLocation<assembly::Assignment>(identifier.location);
if (m_flavour != AsmFlavour::Yul && instructions().count(identifier.name))
2017-01-25 16:29:06 +00:00
fatalParserError("Cannot use instruction names for identifier names.");
advance();
assignment.variableNames.emplace_back(identifier);
2017-12-08 13:01:22 +00:00
assignment.value.reset(new Expression(parseExpression()));
assignment.location.end = locationOf(*assignment.value).end;
return assignment;
2016-02-22 01:13:41 +00:00
}
else
2016-04-18 11:47:40 +00:00
{
2016-02-22 01:13:41 +00:00
// label
if (m_flavour != AsmFlavour::Loose)
2017-05-01 16:40:37 +00:00
fatalParserError("Labels are not supported.");
2016-04-18 11:47:40 +00:00
Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name;
return label;
}
2016-02-22 01:13:41 +00:00
}
default:
if (m_flavour != AsmFlavour::Loose)
2017-05-02 08:24:35 +00:00
fatalParserError("Call or assignment expected.");
2016-02-22 01:13:41 +00:00
break;
}
2017-12-08 13:01:22 +00:00
if (elementary.type() == typeid(assembly::Identifier))
{
Expression expr = boost::get<assembly::Identifier>(elementary);
return ExpressionStatement{locationOf(expr), expr};
}
else if (elementary.type() == typeid(assembly::Literal))
{
Expression expr = boost::get<assembly::Literal>(elementary);
return ExpressionStatement{locationOf(expr), expr};
}
else
{
solAssert(elementary.type() == typeid(assembly::Instruction), "Invalid elementary operation.");
return boost::get<assembly::Instruction>(elementary);
}
2016-02-22 01:13:41 +00:00
}
2017-05-19 17:04:40 +00:00
assembly::Case Parser::parseCase()
{
RecursionGuard recursionGuard(*this);
assembly::Case _case = createWithLocation<assembly::Case>();
2017-05-19 17:04:40 +00:00
if (m_scanner->currentToken() == Token::Default)
m_scanner->next();
else if (m_scanner->currentToken() == Token::Case)
{
2017-05-19 17:04:40 +00:00
m_scanner->next();
2017-12-08 13:01:22 +00:00
ElementaryOperation literal = parseElementaryOperation();
if (literal.type() != typeid(assembly::Literal))
2017-05-17 10:21:37 +00:00
fatalParserError("Literal expected.");
2017-12-08 13:01:22 +00:00
_case.value = make_shared<Literal>(boost::get<assembly::Literal>(std::move(literal)));
}
2017-05-19 17:04:40 +00:00
else
fatalParserError("Case or default case expected.");
_case.body = parseBlock();
_case.location.end = _case.body.location.end;
return _case;
}
assembly::ForLoop Parser::parseForLoop()
{
RecursionGuard recursionGuard(*this);
ForLoop forLoop = createWithLocation<ForLoop>();
expectToken(Token::For);
forLoop.pre = parseBlock();
2017-12-08 13:01:22 +00:00
forLoop.condition = make_shared<Expression>(parseExpression());
forLoop.post = parseBlock();
forLoop.body = parseBlock();
forLoop.location.end = forLoop.body.location.end;
return forLoop;
}
2017-12-08 13:01:22 +00:00
assembly::Expression Parser::parseExpression()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
// In strict mode, this might parse a plain Instruction, but
// it will be converted to a FunctionalInstruction inside
// parseCall below.
ElementaryOperation operation = parseElementaryOperation();
if (operation.type() == typeid(Instruction))
{
Instruction const& instr = boost::get<Instruction>(operation);
// Disallow instructions returning multiple values (and DUP/SWAP) as expression.
if (
instructionInfo(instr.instruction).ret != 1 ||
isDupInstruction(instr.instruction) ||
isSwapInstruction(instr.instruction)
)
fatalParserError(
"Instruction \"" +
instructionNames().at(instr.instruction) +
"\" not allowed in this context."
);
if (m_flavour != AsmFlavour::Loose && currentToken() != Token::LParen)
fatalParserError(
"Non-functional instructions are not allowed in this context."
);
// Enforce functional notation for instructions requiring multiple arguments.
int args = instructionInfo(instr.instruction).args;
if (args > 0 && currentToken() != Token::LParen)
fatalParserError(string(
2018-05-02 18:59:05 +00:00
"Expected '(' (instruction \"" +
instructionNames().at(instr.instruction) +
"\" expects " +
boost::lexical_cast<string>(args) +
" arguments)"
));
}
if (currentToken() == Token::LParen)
return parseCall(std::move(operation));
2017-12-08 13:01:22 +00:00
else if (operation.type() == typeid(Instruction))
{
// Instructions not taking arguments are allowed as expressions.
solAssert(m_flavour == AsmFlavour::Loose, "");
2017-12-08 13:01:22 +00:00
Instruction& instr = boost::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
}
else if (operation.type() == typeid(assembly::Identifier))
return boost::get<assembly::Identifier>(operation);
2016-02-22 01:13:41 +00:00
else
2017-12-08 13:01:22 +00:00
{
solAssert(operation.type() == typeid(assembly::Literal), "");
return boost::get<assembly::Literal>(operation);
}
2016-02-22 01:13:41 +00:00
}
2017-01-25 16:24:50 +00:00
std::map<string, dev::solidity::Instruction> const& Parser::instructions()
2016-02-22 01:13:41 +00:00
{
// Allowed instructions, lowercase names.
static map<string, dev::solidity::Instruction> s_instructions;
2016-02-22 01:13:41 +00:00
if (s_instructions.empty())
{
for (auto const& instruction: solidity::c_instructions)
2016-02-22 01:13:41 +00:00
{
if (
instruction.second == solidity::Instruction::JUMPDEST ||
2017-10-02 09:22:58 +00:00
solidity::isPushInstruction(instruction.second)
2016-02-22 01:13:41 +00:00
)
continue;
string name = instruction.first;
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
s_instructions[name] = instruction.second;
}
}
return s_instructions;
}
std::map<dev::solidity::Instruction, string> const& Parser::instructionNames()
{
static map<dev::solidity::Instruction, string> s_instructionNames;
if (s_instructionNames.empty())
{
for (auto const& instr: instructions())
s_instructionNames[instr.second] = instr.first;
// set the ambiguous instructions to a clear default
s_instructionNames[solidity::Instruction::SELFDESTRUCT] = "selfdestruct";
s_instructionNames[solidity::Instruction::KECCAK256] = "keccak256";
}
return s_instructionNames;
}
Parser::ElementaryOperation Parser::parseElementaryOperation()
{
RecursionGuard recursionGuard(*this);
2017-12-08 13:01:22 +00:00
ElementaryOperation ret;
switch (currentToken())
2016-02-22 01:13:41 +00:00
{
case Token::Identifier:
case Token::Return:
2016-04-05 12:57:40 +00:00
case Token::Byte:
2016-10-05 10:47:56 +00:00
case Token::Address:
2016-02-22 01:13:41 +00:00
{
string literal;
if (currentToken() == Token::Return)
literal = "return";
else if (currentToken() == Token::Byte)
2016-04-05 12:57:40 +00:00
literal = "byte";
else if (currentToken() == Token::Address)
2016-10-05 10:47:56 +00:00
literal = "address";
else
literal = currentLiteral();
2016-02-22 01:13:41 +00:00
// first search the set of instructions.
if (m_flavour != AsmFlavour::Yul && instructions().count(literal))
2016-02-22 01:13:41 +00:00
{
2017-01-25 16:29:06 +00:00
dev::solidity::Instruction const& instr = instructions().at(literal);
2016-04-18 11:47:40 +00:00
ret = Instruction{location(), instr};
2016-02-22 01:13:41 +00:00
}
else
2016-04-18 11:47:40 +00:00
ret = Identifier{location(), literal};
advance();
2016-02-22 01:13:41 +00:00
break;
}
case Token::StringLiteral:
case Token::Number:
case Token::TrueLiteral:
case Token::FalseLiteral:
2016-02-22 01:13:41 +00:00
{
LiteralKind kind = LiteralKind::Number;
switch (currentToken())
{
case Token::StringLiteral:
kind = LiteralKind::String;
break;
case Token::Number:
if (!isValidNumberLiteral(currentLiteral()))
fatalParserError("Invalid number literal.");
kind = LiteralKind::Number;
break;
case Token::TrueLiteral:
case Token::FalseLiteral:
kind = LiteralKind::Boolean;
break;
default:
break;
}
2017-04-26 22:58:34 +00:00
Literal literal{
2016-04-18 11:47:40 +00:00
location(),
kind,
currentLiteral(),
2017-04-26 22:58:34 +00:00
""
2016-02-22 01:13:41 +00:00
};
advance();
if (m_flavour == AsmFlavour::Yul)
2017-04-26 22:58:34 +00:00
{
expectToken(Token::Colon);
literal.location.end = endPosition();
literal.type = expectAsmIdentifier();
}
else if (kind == LiteralKind::Boolean)
fatalParserError("True and false are not valid literals.");
2017-04-26 22:58:34 +00:00
ret = std::move(literal);
2016-04-18 11:47:40 +00:00
break;
2016-02-22 01:13:41 +00:00
}
default:
fatalParserError(
m_flavour == AsmFlavour::Yul ?
"Literal or identifier expected." :
"Literal, identifier or instruction expected."
);
2016-02-22 01:13:41 +00:00
}
2016-04-18 11:47:40 +00:00
return ret;
2016-02-22 01:13:41 +00:00
}
assembly::VariableDeclaration Parser::parseVariableDeclaration()
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2016-04-18 11:47:40 +00:00
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
2016-02-22 01:13:41 +00:00
expectToken(Token::Let);
while (true)
{
varDecl.variables.emplace_back(parseTypedName());
if (currentToken() == Token::Comma)
expectToken(Token::Comma);
else
break;
}
if (currentToken() == Token::Colon)
{
expectToken(Token::Colon);
expectToken(Token::Assign);
2017-12-08 13:01:22 +00:00
varDecl.value.reset(new Expression(parseExpression()));
varDecl.location.end = locationOf(*varDecl.value).end;
}
else
varDecl.location.end = varDecl.variables.back().location.end;
2016-04-18 11:47:40 +00:00
return varDecl;
2016-02-22 01:13:41 +00:00
}
2017-01-31 22:59:41 +00:00
assembly::FunctionDefinition Parser::parseFunctionDefinition()
{
RecursionGuard recursionGuard(*this);
2017-01-31 22:59:41 +00:00
FunctionDefinition funDef = createWithLocation<FunctionDefinition>();
expectToken(Token::Function);
funDef.name = expectAsmIdentifier();
expectToken(Token::LParen);
while (currentToken() != Token::RParen)
2017-01-31 22:59:41 +00:00
{
funDef.parameters.emplace_back(parseTypedName());
if (currentToken() == Token::RParen)
2017-01-31 22:59:41 +00:00
break;
expectToken(Token::Comma);
}
expectToken(Token::RParen);
if (currentToken() == Token::Sub)
2017-01-31 22:59:41 +00:00
{
expectToken(Token::Sub);
expectToken(Token::GreaterThan);
while (true)
{
funDef.returnVariables.emplace_back(parseTypedName());
if (currentToken() == Token::LBrace)
2017-01-31 22:59:41 +00:00
break;
expectToken(Token::Comma);
}
}
funDef.body = parseBlock();
funDef.location.end = funDef.body.location.end;
return funDef;
}
2017-12-08 13:01:22 +00:00
assembly::Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
2016-02-22 01:13:41 +00:00
{
RecursionGuard recursionGuard(*this);
2017-12-08 13:01:22 +00:00
if (_initialOp.type() == typeid(Instruction))
2016-02-22 01:13:41 +00:00
{
solAssert(m_flavour != AsmFlavour::Yul, "Instructions are invalid in Yul");
2017-12-08 13:01:22 +00:00
Instruction& instruction = boost::get<Instruction>(_initialOp);
2017-02-01 20:20:21 +00:00
FunctionalInstruction ret;
ret.instruction = instruction.instruction;
ret.location = std::move(instruction.location);
solidity::Instruction instr = ret.instruction;
2017-02-01 20:20:21 +00:00
InstructionInfo instrInfo = instructionInfo(instr);
2017-10-02 09:22:58 +00:00
if (solidity::isDupInstruction(instr))
2017-02-01 20:20:21 +00:00
fatalParserError("DUPi instructions not allowed for functional notation");
2017-10-02 09:22:58 +00:00
if (solidity::isSwapInstruction(instr))
2017-02-01 20:20:21 +00:00
fatalParserError("SWAPi instructions not allowed for functional notation");
expectToken(Token::LParen);
unsigned args = unsigned(instrInfo.args);
for (unsigned i = 0; i < args; ++i)
2016-04-18 11:47:40 +00:00
{
/// check for premature closing parentheses
if (currentToken() == Token::RParen)
fatalParserError(string(
2018-05-02 18:59:05 +00:00
"Expected expression (instruction \"" +
instructionNames().at(instr) +
"\" expects " +
boost::lexical_cast<string>(args) +
2017-05-24 12:27:58 +00:00
" arguments)"
));
2017-02-01 20:20:21 +00:00
ret.arguments.emplace_back(parseExpression());
if (i != args - 1)
{
if (currentToken() != Token::Comma)
2017-02-01 20:20:21 +00:00
fatalParserError(string(
2018-05-02 18:59:05 +00:00
"Expected ',' (instruction \"" +
instructionNames().at(instr) +
"\" expects " +
2017-02-01 20:20:21 +00:00
boost::lexical_cast<string>(args) +
" arguments)"
));
else
advance();
2017-02-01 20:20:21 +00:00
}
2016-04-18 11:47:40 +00:00
}
2017-02-01 20:20:21 +00:00
ret.location.end = endPosition();
if (currentToken() == Token::Comma)
fatalParserError(string(
2018-05-02 18:59:05 +00:00
"Expected ')' (instruction \"" +
instructionNames().at(instr) +
"\" expects " +
boost::lexical_cast<string>(args) +
" arguments)"
));
2017-02-01 20:20:21 +00:00
expectToken(Token::RParen);
return ret;
2016-02-22 01:13:41 +00:00
}
2017-12-08 13:01:22 +00:00
else if (_initialOp.type() == typeid(Identifier))
2017-02-01 20:20:21 +00:00
{
FunctionCall ret;
2017-12-08 13:01:22 +00:00
ret.functionName = std::move(boost::get<Identifier>(_initialOp));
2017-02-01 20:20:21 +00:00
ret.location = ret.functionName.location;
expectToken(Token::LParen);
while (currentToken() != Token::RParen)
2017-02-01 20:20:21 +00:00
{
ret.arguments.emplace_back(parseExpression());
if (currentToken() == Token::RParen)
2017-02-01 20:20:21 +00:00
break;
expectToken(Token::Comma);
}
ret.location.end = endPosition();
expectToken(Token::RParen);
return ret;
}
else
fatalParserError(
m_flavour == AsmFlavour::Yul ?
"Function name expected." :
"Assembly instruction or function name required in front of \"(\")"
);
2017-02-01 20:20:21 +00:00
return {};
2016-02-22 01:13:41 +00:00
}
2017-01-31 22:59:41 +00:00
2017-04-26 22:58:34 +00:00
TypedName Parser::parseTypedName()
{
RecursionGuard recursionGuard(*this);
2017-04-26 22:58:34 +00:00
TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier();
if (m_flavour == AsmFlavour::Yul)
2017-04-26 22:58:34 +00:00
{
expectToken(Token::Colon);
typedName.location.end = endPosition();
typedName.type = expectAsmIdentifier();
}
return typedName;
}
2017-01-31 22:59:41 +00:00
string Parser::expectAsmIdentifier()
{
string name = currentLiteral();
if (m_flavour == AsmFlavour::Yul)
2017-05-17 12:33:05 +00:00
{
switch (currentToken())
2017-05-17 12:33:05 +00:00
{
case Token::Return:
case Token::Byte:
case Token::Address:
case Token::Bool:
advance();
2017-05-17 12:33:05 +00:00
return name;
default:
break;
2017-05-17 12:33:05 +00:00
}
}
else if (instructions().count(name))
2017-01-31 22:59:41 +00:00
fatalParserError("Cannot use instruction names for identifier names.");
expectToken(Token::Identifier);
return name;
}
bool Parser::isValidNumberLiteral(string const& _literal)
{
try
{
// Try to convert _literal to u256.
auto tmp = u256(_literal);
(void) tmp;
}
catch (...)
{
return false;
}
if (boost::starts_with(_literal, "0x"))
return true;
else
return _literal.find_first_not_of("0123456789") == string::npos;
}