solidity/libsolidity/inlineasm/AsmParser.cpp

226 lines
6.2 KiB
C++
Raw Normal View History

2016-02-22 01:13:41 +00:00
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2016
* Solidity inline assembly parser.
*/
#include <libsolidity/inlineasm/AsmParser.h>
#include <ctype.h>
#include <algorithm>
#include <libevmcore/Instruction.h>
#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;
return make_shared<assembly::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
{
expectToken(Token::LBrace);
Block block;
2016-02-22 01:13:41 +00:00
while (m_scanner->currentToken() != Token::RBrace)
block.statements.emplace_back(parseStatement());
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:
{
m_scanner->next();
expectToken(Token::Colon);
string name = m_scanner->currentLiteral();
expectToken(Token::Identifier);
return assembly::Assignment{assembly::Identifier{name}};
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:
return parseFunctionalInstruction(statement);
case Token::Colon:
{
if (statement.type() != typeid(assembly::Identifier))
2016-02-22 01:13:41 +00:00
fatalParserError("Label name / variable name must precede \":\".");
string const& name = boost::get<assembly::Identifier>(statement).name;
2016-02-22 01:13:41 +00:00
m_scanner->next();
if (m_scanner->currentToken() == Token::Assign)
{
// functional assignment
m_scanner->next();
unique_ptr<Statement> value;
value.reset(new Statement(parseExpression()));
return FunctionalAssignment{{std::move(name)}, std::move(value)};
2016-02-22 01:13:41 +00:00
}
else
// label
return Label{name};
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)
return parseFunctionalInstruction(operation);
else
return operation;
}
assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
2016-02-22 01:13:41 +00:00
{
// Allowed instructions, lowercase names.
static map<string, eth::Instruction> s_instructions;
if (s_instructions.empty())
for (auto const& instruction: eth::c_instructions)
{
if (
instruction.second == eth::Instruction::JUMPDEST ||
(eth::Instruction::PUSH1 <= instruction.second && instruction.second <= eth::Instruction::PUSH32)
)
continue;
string name = instruction.first;
if (instruction.second == eth::Instruction::SUICIDE)
name = "selfdestruct";
2016-02-22 01:13:41 +00:00
transform(name.begin(), name.end(), name.begin(), [](unsigned char _c) { return tolower(_c); });
s_instructions[name] = instruction.second;
}
//@TODO track location
switch (m_scanner->currentToken())
{
case Token::Identifier:
case Token::Return:
2016-04-05 12:57:40 +00:00
case Token::Byte:
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";
else
literal = m_scanner->currentLiteral();
2016-02-22 01:13:41 +00:00
// first search the set of instructions.
if (s_instructions.count(literal))
{
eth::Instruction const& instr = s_instructions[literal];
if (_onlySinglePusher)
{
eth::InstructionInfo info = eth::instructionInfo(instr);
if (info.ret != 1)
fatalParserError("Instruction " + info.name + " not allowed in this context.");
}
m_scanner->next();
return Instruction{instr};
2016-02-22 01:13:41 +00:00
}
else
m_scanner->next();
return Identifier{literal};
2016-02-22 01:13:41 +00:00
break;
}
case Token::StringLiteral:
case Token::Number:
{
Literal literal{
2016-02-22 01:13:41 +00:00
m_scanner->currentToken() == Token::Number,
m_scanner->currentLiteral()
};
m_scanner->next();
return literal;
}
default:
break;
}
fatalParserError("Expected elementary inline assembly operation.");
return {};
}
assembly::VariableDeclaration Parser::parseVariableDeclaration()
2016-02-22 01:13:41 +00:00
{
expectToken(Token::Let);
string name = m_scanner->currentLiteral();
expectToken(Token::Identifier);
expectToken(Token::Colon);
expectToken(Token::Assign);
unique_ptr<Statement> value;
value.reset(new Statement(parseExpression()));
return VariableDeclaration{name, std::move(value)};
2016-02-22 01:13:41 +00:00
}
FunctionalInstruction Parser::parseFunctionalInstruction(assembly::Statement const& _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 \"(\")");
eth::Instruction instr = boost::get<Instruction>(_instruction).instruction;
2016-02-22 01:13:41 +00:00
eth::InstructionInfo instrInfo = eth::instructionInfo(instr);
if (eth::Instruction::DUP1 <= instr && instr <= eth::Instruction::DUP16)
fatalParserError("DUPi instructions not allowed for functional notation");
if (eth::Instruction::SWAP1 <= instr && instr <= eth::Instruction::SWAP16)
fatalParserError("SWAPi instructions not allowed for functional notation");
expectToken(Token::LParen);
vector<Statement> arguments;
2016-02-22 01:13:41 +00:00
unsigned args = unsigned(instrInfo.args);
for (unsigned i = 0; i < args; ++i)
{
arguments.push_back(parseExpression());
if (i != args - 1)
expectToken(Token::Comma);
}
expectToken(Token::RParen);
return FunctionalInstruction{{instr}, std::move(arguments)};
2016-02-22 01:13:41 +00:00
}