aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/inlineasm/AsmParser.cpp
blob: 6805073360a0ee61f36c4978f1f0dcc55238e5b7 (plain) (blame)
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
/*
    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 <libsolidity/parsing/Scanner.h>

using namespace std;
using namespace dev;
using namespace dev::solidity;
using namespace dev::solidity::assembly;

shared_ptr<assembly::Block> Parser::parse(std::shared_ptr<Scanner> const& _scanner)
{
    try
    {
        m_scanner = _scanner;
        return make_shared<assembly::Block>(parseBlock());
    }
    catch (FatalError const&)
    {
        if (m_errors.empty())
            throw; // Something is weird here, rather throw again.
    }
    return nullptr;
}

assembly::Block Parser::parseBlock()
{
    expectToken(Token::LBrace);
    Block block;
    while (m_scanner->currentToken() != Token::RBrace)
        block.statements.emplace_back(parseStatement());
    m_scanner->next();
    return block;
}

assembly::Statement Parser::parseStatement()
{
    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}};
    }
    case Token::Return: // opcode
    default:
        break;
    }
    // Options left:
    // Simple instruction (might turn into functional),
    // literal,
    // identifier (might turn into label or functional assignment)
    Statement statement(parseElementaryOperation());
    switch (m_scanner->currentToken())
    {
    case Token::LParen:
        return parseFunctionalInstruction(statement);
    case Token::Colon:
    {
        if (statement.type() != typeid(assembly::Identifier))
            fatalParserError("Label name / variable name must precede \":\".");
        string const& name = boost::get<assembly::Identifier>(statement).name;
        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)};
        }
        else
            // label
            return Label{name};
    }
    default:
        break;
    }
    return statement;
}

assembly::Statement Parser::parseExpression()
{
    Statement operation = parseElementaryOperation(true);
    if (m_scanner->currentToken() == Token::LParen)
        return parseFunctionalInstruction(operation);
    else
        return operation;
}

assembly::Statement Parser::parseElementaryOperation(bool _onlySinglePusher)
{
    // Allowed instructions, lowercase names.
    static map<string, dev::solidity::Instruction> s_instructions;
    if (s_instructions.empty())
        for (auto const& instruction: solidity::c_instructions)
        {
            if (
                instruction.second == solidity::Instruction::JUMPDEST ||
                (solidity::Instruction::PUSH1 <= instruction.second && instruction.second <= solidity::Instruction::PUSH32)
            )
                continue;
            string name = instruction.first;
            if (instruction.second == solidity::Instruction::SUICIDE)
                name = "selfdestruct";
            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:
    {
        string literal;
        if (m_scanner->currentToken() == Token::Return)
            literal = "return";
        else
            literal = m_scanner->currentLiteral();
        // first search the set of instructions.
        if (s_instructions.count(literal))
        {
            dev::solidity::Instruction const& instr = s_instructions[literal];
            if (_onlySinglePusher)
            {
                InstructionInfo info = dev::solidity::instructionInfo(instr);
                if (info.ret != 1)
                    fatalParserError("Instruction " + info.name + " not allowed in this context.");
            }
            m_scanner->next();
            return Instruction{instr};
        }
        else
            m_scanner->next();
            return Identifier{literal};
        break;
    }
    case Token::StringLiteral:
    case Token::Number:
    {
        Literal literal{
            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()
{
    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)};
}

FunctionalInstruction Parser::parseFunctionalInstruction(assembly::Statement const& _instruction)
{
    if (_instruction.type() != typeid(Instruction))
        fatalParserError("Assembly instruction required in front of \"(\")");
    solidity::Instruction instr = boost::get<solidity::assembly::Instruction>(_instruction).instruction;
    InstructionInfo instrInfo = instructionInfo(instr);
    if (solidity::Instruction::DUP1 <= instr && instr <= solidity::Instruction::DUP16)
        fatalParserError("DUPi instructions not allowed for functional notation");
    if (solidity::Instruction::SWAP1 <= instr && instr <= solidity::Instruction::SWAP16)
        fatalParserError("SWAPi instructions not allowed for functional notation");

    expectToken(Token::LParen);
    vector<Statement> arguments;
    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)};
}