solidity/libsolidity/inlineasm/AsmParser.cpp

269 lines
8.0 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 <ctype.h>
#include <algorithm>
#include <libsolidity/parsing/Scanner.h>
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)
2016-02-22 01:13:41 +00:00
{
try
{
m_scanner = _scanner;
2016-04-18 11:47:40 +00:00
return make_shared<Block>(parseBlock());
2016-02-22 01:13:41 +00:00
}
catch (FatalError const&)
{
if (m_errors.empty())
throw; // Something is weird here, rather throw again.
}
return nullptr;
}
assembly::Block Parser::parseBlock()
2016-02-22 01:13:41 +00:00
{
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 (m_scanner->currentToken() != Token::RBrace)
block.statements.emplace_back(parseStatement());
2016-04-18 11:47:40 +00:00
block.location.end = endPosition();
2016-02-22 01:13:41 +00:00
m_scanner->next();
return block;
}
assembly::Statement Parser::parseStatement()
2016-02-22 01:13:41 +00:00
{
switch (m_scanner->currentToken())
{
case Token::Let:
return parseVariableDeclaration();
case Token::LBrace:
return parseBlock();
case Token::Assign:
{
2016-04-18 11:47:40 +00:00
assembly::Assignment assignment = createWithLocation<assembly::Assignment>();
2016-02-22 01:13:41 +00:00
m_scanner->next();
expectToken(Token::Colon);
2016-04-18 11:47:40 +00:00
assignment.variableName.location = location();
assignment.variableName.name = m_scanner->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
}
case Token::Return: // opcode
2016-04-05 12:57:40 +00:00
case Token::Byte: // opcode
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)
Statement statement(parseElementaryOperation());
2016-02-22 01:13:41 +00:00
switch (m_scanner->currentToken())
{
case Token::LParen:
2016-04-18 11:47:40 +00:00
return parseFunctionalInstruction(std::move(statement));
2016-02-22 01:13:41 +00:00
case Token::Colon:
{
if (statement.type() != typeid(assembly::Identifier))
2016-02-22 01:13:41 +00:00
fatalParserError("Label name / variable name must precede \":\".");
2016-04-18 11:47:40 +00:00
assembly::Identifier const& identifier = boost::get<assembly::Identifier>(statement);
2016-02-22 01:13:41 +00:00
m_scanner->next();
// identifier:=: should be parsed as identifier: =: (i.e. a label),
// while identifier:= (being followed by a non-colon) as identifier := (assignment).
if (m_scanner->currentToken() == Token::Assign && m_scanner->peekNextToken() != Token::Colon)
2016-02-22 01:13:41 +00:00
{
// functional assignment
2016-04-18 11:47:40 +00:00
FunctionalAssignment funAss = createWithLocation<FunctionalAssignment>(identifier.location);
2017-01-25 16:29:06 +00:00
if (instructions().count(identifier.name))
fatalParserError("Cannot use instruction names for identifier names.");
2016-02-22 01:13:41 +00:00
m_scanner->next();
2016-04-18 11:47:40 +00:00
funAss.variableName = identifier;
funAss.value.reset(new Statement(parseExpression()));
funAss.location.end = locationOf(*funAss.value).end;
return funAss;
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
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:
break;
}
return statement;
}
assembly::Statement Parser::parseExpression()
2016-02-22 01:13:41 +00:00
{
Statement operation = parseElementaryOperation(true);
2016-02-22 01:13:41 +00:00
if (m_scanner->currentToken() == Token::LParen)
2016-04-18 11:47:40 +00:00
return parseFunctionalInstruction(std::move(operation));
2016-02-22 01:13:41 +00:00
else
return operation;
}
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 ||
(solidity::Instruction::PUSH1 <= instruction.second && instruction.second <= solidity::Instruction::PUSH32)
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;
}
// add alias for selfdestruct
s_instructions["selfdestruct"] = solidity::Instruction::SUICIDE;
}
return s_instructions;
}
assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
{
2016-04-18 11:47:40 +00:00
Statement ret;
2016-02-22 01:13:41 +00:00
switch (m_scanner->currentToken())
{
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 (m_scanner->currentToken() == Token::Return)
literal = "return";
2016-04-05 12:57:40 +00:00
else if (m_scanner->currentToken() == Token::Byte)
literal = "byte";
2016-10-05 10:47:56 +00:00
else if (m_scanner->currentToken() == Token::Address)
literal = "address";
else
literal = m_scanner->currentLiteral();
2016-02-22 01:13:41 +00:00
// first search the set of instructions.
2017-01-25 16:29:06 +00:00
if (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-02-22 01:13:41 +00:00
if (_onlySinglePusher)
{
2016-04-04 11:27:09 +00:00
InstructionInfo info = dev::solidity::instructionInfo(instr);
2016-02-22 01:13:41 +00:00
if (info.ret != 1)
fatalParserError("Instruction " + info.name + " not allowed in this context.");
}
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};
2016-02-22 01:13:41 +00:00
break;
}
case Token::StringLiteral:
case Token::Number:
{
2016-04-18 11:47:40 +00:00
ret = Literal{
location(),
2016-02-22 01:13:41 +00:00
m_scanner->currentToken() == Token::Number,
m_scanner->currentLiteral()
};
2016-04-18 11:47:40 +00:00
break;
2016-02-22 01:13:41 +00:00
}
default:
2016-04-18 11:47:40 +00:00
fatalParserError("Expected elementary inline assembly operation.");
2016-02-22 01:13:41 +00:00
}
2016-04-18 11:47:40 +00:00
m_scanner->next();
return ret;
2016-02-22 01:13:41 +00:00
}
assembly::VariableDeclaration Parser::parseVariableDeclaration()
2016-02-22 01:13:41 +00:00
{
2016-04-18 11:47:40 +00:00
VariableDeclaration varDecl = createWithLocation<VariableDeclaration>();
2016-02-22 01:13:41 +00:00
expectToken(Token::Let);
2016-04-18 11:47:40 +00:00
varDecl.name = m_scanner->currentLiteral();
2017-01-25 16:29:06 +00:00
if (instructions().count(varDecl.name))
2017-01-25 16:24:43 +00:00
fatalParserError("Cannot use instruction names for identifier names.");
2016-02-22 01:13:41 +00:00
expectToken(Token::Identifier);
expectToken(Token::Colon);
expectToken(Token::Assign);
2016-04-18 11:47:40 +00:00
varDecl.value.reset(new Statement(parseExpression()));
varDecl.location.end = locationOf(*varDecl.value).end;
return varDecl;
2016-02-22 01:13:41 +00:00
}
2016-04-19 17:00:23 +00:00
FunctionalInstruction Parser::parseFunctionalInstruction(assembly::Statement&& _instruction)
2016-02-22 01:13:41 +00:00
{
if (_instruction.type() != typeid(Instruction))
2016-02-22 01:13:41 +00:00
fatalParserError("Assembly instruction required in front of \"(\")");
2016-04-18 11:47:40 +00:00
FunctionalInstruction ret;
ret.instruction = std::move(boost::get<Instruction>(_instruction));
ret.location = ret.instruction.location;
solidity::Instruction instr = ret.instruction.instruction;
2016-04-04 11:27:09 +00:00
InstructionInfo instrInfo = instructionInfo(instr);
if (solidity::Instruction::DUP1 <= instr && instr <= solidity::Instruction::DUP16)
2016-02-22 01:13:41 +00:00
fatalParserError("DUPi instructions not allowed for functional notation");
if (solidity::Instruction::SWAP1 <= instr && instr <= solidity::Instruction::SWAP16)
2016-02-22 01:13:41 +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
ret.arguments.emplace_back(parseExpression());
2016-02-22 01:13:41 +00:00
if (i != args - 1)
2016-04-18 11:47:40 +00:00
{
if (m_scanner->currentToken() != Token::Comma)
fatalParserError(string(
"Expected comma (" +
instrInfo.name +
" expects " +
boost::lexical_cast<string>(args) +
" arguments)"
));
else
m_scanner->next();
}
2016-02-22 01:13:41 +00:00
}
2016-04-18 11:47:40 +00:00
ret.location.end = endPosition();
if (m_scanner->currentToken() == Token::Comma)
fatalParserError(
string("Expected ')' (" + instrInfo.name + " expects " + boost::lexical_cast<string>(args) + " arguments)")
);
2016-02-22 01:13:41 +00:00
expectToken(Token::RParen);
2016-04-18 11:47:40 +00:00
return ret;
2016-02-22 01:13:41 +00:00
}