aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/inlineasm/AsmCodeGen.cpp
blob: 1eab5df0f5396c53ca1254aace6d5d77bb0ee4ea (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
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
/*
    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/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2016
 * Code-generating part of inline assembly.
 */

#include <libsolidity/inlineasm/AsmCodeGen.h>
#include <memory>
#include <functional>
#include <libdevcore/CommonIO.h>
#include <libevmasm/Assembly.h>
#include <libevmasm/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libsolidity/inlineasm/AsmParser.h>
#include <libsolidity/inlineasm/AsmData.h>

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

struct GeneratorState
{
    GeneratorState(ErrorList& _errors, eth::Assembly& _assembly):
        errors(_errors), assembly(_assembly) {}

    void addError(Error::Type _type, std::string const& _description, SourceLocation const& _location = SourceLocation())
    {
        auto err = make_shared<Error>(_type);
        if (!_location.isEmpty())
            *err << errinfo_sourceLocation(_location);
        *err << errinfo_comment(_description);
        errors.push_back(err);
    }

    int const* findVariable(string const& _variableName) const
    {
        auto localVariable = find_if(
            variables.rbegin(),
            variables.rend(),
            [&](pair<string, int> const& _var) { return _var.first == _variableName; }
        );
        return localVariable != variables.rend() ? &localVariable->second : nullptr;
    }
    eth::AssemblyItem const* findLabel(string const& _labelName) const
    {
        auto label = find_if(
            labels.begin(),
            labels.end(),
            [&](pair<string, eth::AssemblyItem> const& _label) { return _label.first == _labelName; }
        );
        return label != labels.end() ? &label->second : nullptr;
    }

    map<string, eth::AssemblyItem> labels;
    vector<pair<string, int>> variables; ///< name plus stack height
    ErrorList& errors;
    eth::Assembly& assembly;
};

/**
 * Scans the inline assembly data for labels, creates tags in the assembly and searches for
 * duplicate labels.
 */
class LabelOrganizer: public boost::static_visitor<>
{
public:
    LabelOrganizer(GeneratorState& _state): m_state(_state)
    {
        // Make the Solidity ErrorTag available to inline assembly
        m_state.labels.insert(make_pair("invalidJumpLabel", m_state.assembly.errorTag()));
    }

    template <class T>
    void operator()(T const& /*_item*/) { }
    void operator()(Label const& _item)
    {
        if (m_state.labels.count(_item.name))
            //@TODO secondary location
            m_state.addError(
                Error::Type::DeclarationError,
                "Label " + _item.name + " declared twice.",
                _item.location
            );
        m_state.labels.insert(make_pair(_item.name, m_state.assembly.newTag()));
    }
    void operator()(assembly::Block const& _block)
    {
        std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this));
    }

private:
    GeneratorState& m_state;
};

class CodeTransform: public boost::static_visitor<>
{
public:
    /// Create the code transformer which appends assembly to _state.assembly when called
    /// with parsed assembly data.
    /// @param _identifierAccess used to resolve identifiers external to the inline assembly
    explicit CodeTransform(
        GeneratorState& _state,
        assembly::CodeGenerator::IdentifierAccess const& _identifierAccess = assembly::CodeGenerator::IdentifierAccess()
    ):
        m_state(_state)
    {
        if (_identifierAccess)
            m_identifierAccess = _identifierAccess;
        else
            m_identifierAccess = [](assembly::Identifier const&, eth::Assembly&, CodeGenerator::IdentifierContext) { return false; };
    }

    void operator()(assembly::Instruction const& _instruction)
    {
        m_state.assembly.setSourceLocation(_instruction.location);
        m_state.assembly.append(_instruction.instruction);
    }
    void operator()(assembly::Literal const& _literal)
    {
        m_state.assembly.setSourceLocation(_literal.location);
        if (_literal.isNumber)
            m_state.assembly.append(u256(_literal.value));
        else if (_literal.value.size() > 32)
        {
            m_state.addError(
                Error::Type::TypeError,
                "String literal too long (" + boost::lexical_cast<string>(_literal.value.size()) + " > 32)"
            );
            m_state.assembly.append(u256(0));
        }
        else
            m_state.assembly.append(_literal.value);
    }
    void operator()(assembly::Identifier const& _identifier)
    {
        m_state.assembly.setSourceLocation(_identifier.location);
        // First search local variables, then labels, then externals.
        if (int const* stackHeight = m_state.findVariable(_identifier.name))
        {
            int heightDiff = m_state.assembly.deposit() - *stackHeight;
            if (heightDiff <= 0 || heightDiff > 16)
            {
                m_state.addError(
                    Error::Type::TypeError,
                    "Variable inaccessible, too deep inside stack (" + boost::lexical_cast<string>(heightDiff) + ")",
                    _identifier.location
                );
                m_state.assembly.append(u256(0));
            }
            else
                m_state.assembly.append(solidity::dupInstruction(heightDiff));
            return;
        }
        else if (eth::AssemblyItem const* label = m_state.findLabel(_identifier.name))
            m_state.assembly.append(label->pushTag());
        else if (!m_identifierAccess(_identifier, m_state.assembly, CodeGenerator::IdentifierContext::RValue))
        {
            m_state.addError(
                Error::Type::DeclarationError,
                "Identifier not found or not unique",
                _identifier.location
            );
            m_state.assembly.append(u256(0));
        }
    }
    void operator()(FunctionalInstruction const& _instr)
    {
        for (auto it = _instr.arguments.rbegin(); it != _instr.arguments.rend(); ++it)
        {
            int height = m_state.assembly.deposit();
            boost::apply_visitor(*this, *it);
            expectDeposit(1, height, locationOf(*it));
        }
        (*this)(_instr.instruction);
    }
    void operator()(Label const& _label)
    {
        m_state.assembly.setSourceLocation(_label.location);
        m_state.assembly.append(m_state.labels.at(_label.name));
    }
    void operator()(assembly::Assignment const& _assignment)
    {
        m_state.assembly.setSourceLocation(_assignment.location);
        generateAssignment(_assignment.variableName, _assignment.location);
    }
    void operator()(FunctionalAssignment const& _assignment)
    {
        int height = m_state.assembly.deposit();
        boost::apply_visitor(*this, *_assignment.value);
        expectDeposit(1, height, locationOf(*_assignment.value));
        m_state.assembly.setSourceLocation(_assignment.location);
        generateAssignment(_assignment.variableName, _assignment.location);
    }
    void operator()(assembly::VariableDeclaration const& _varDecl)
    {
        int height = m_state.assembly.deposit();
        boost::apply_visitor(*this, *_varDecl.value);
        expectDeposit(1, height, locationOf(*_varDecl.value));
        m_state.variables.push_back(make_pair(_varDecl.name, height));
    }
    void operator()(assembly::Block const& _block)
    {
        size_t numVariables = m_state.variables.size();
        int deposit = m_state.assembly.deposit();
        std::for_each(_block.statements.begin(), _block.statements.end(), boost::apply_visitor(*this));

        // pop variables
        while (m_state.variables.size() > numVariables)
        {
            m_state.assembly.append(solidity::Instruction::POP);
            m_state.variables.pop_back();
        }

        m_state.assembly.setSourceLocation(_block.location);

        deposit = m_state.assembly.deposit() - deposit;

        // issue warnings for stack height discrepancies
        if (deposit < 0)
        {
            m_state.addError(
                Error::Type::Warning,
                "Inline assembly block is not balanced. It takes " + toString(-deposit) + " item(s) from the stack.",
                _block.location
            );
        }
        else if (deposit > 0)
        {
            m_state.addError(
                Error::Type::Warning,
                "Inline assembly block is not balanced. It leaves " + toString(deposit) + " item(s) on the stack.",
                _block.location
            );
        }
    }
    void operator()(assembly::FunctionDefinition const&)
    {
        solAssert(false, "Function definition not removed during desugaring phase.");
    }

private:
    void generateAssignment(assembly::Identifier const& _variableName, SourceLocation const& _location)
    {
        if (int const* stackHeight = m_state.findVariable(_variableName.name))
        {
            int heightDiff = m_state.assembly.deposit() - *stackHeight - 1;
            if (heightDiff <= 0 || heightDiff > 16)
                m_state.addError(
                    Error::Type::TypeError,
                    "Variable inaccessible, too deep inside stack (" + boost::lexical_cast<string>(heightDiff) + ")",
                    _location
                );
            else
                m_state.assembly.append(solidity::swapInstruction(heightDiff));
            m_state.assembly.append(solidity::Instruction::POP);
            return;
        }
        else if (!m_identifierAccess(_variableName, m_state.assembly, CodeGenerator::IdentifierContext::LValue))
            m_state.addError(
                Error::Type::DeclarationError,
                "Identifier \"" + string(_variableName.name) + "\" not found, not unique or not lvalue."
            );
    }

    void expectDeposit(int _deposit, int _oldHeight, SourceLocation const& _location)
    {
        if (m_state.assembly.deposit() != _oldHeight + 1)
            m_state.addError(Error::Type::TypeError,
                "Expected instruction(s) to deposit " +
                boost::lexical_cast<string>(_deposit) +
                " item(s) to the stack, but did deposit " +
                boost::lexical_cast<string>(m_state.assembly.deposit() - _oldHeight) +
                " item(s).",
                _location
            );
    }

    GeneratorState& m_state;
    assembly::CodeGenerator::IdentifierAccess m_identifierAccess;
};

bool assembly::CodeGenerator::typeCheck(assembly::CodeGenerator::IdentifierAccess const& _identifierAccess)
{
    size_t initialErrorLen = m_errors.size();
    eth::Assembly assembly;
    GeneratorState state(m_errors, assembly);
    (LabelOrganizer(state))(m_parsedData);
    (CodeTransform(state, _identifierAccess))(m_parsedData);
    return m_errors.size() == initialErrorLen;
}

eth::Assembly assembly::CodeGenerator::assemble(assembly::CodeGenerator::IdentifierAccess const& _identifierAccess)
{
    eth::Assembly assembly;
    GeneratorState state(m_errors, assembly);
    (LabelOrganizer(state))(m_parsedData);
    (CodeTransform(state, _identifierAccess))(m_parsedData);
    return assembly;
}

void assembly::CodeGenerator::assemble(eth::Assembly& _assembly, assembly::CodeGenerator::IdentifierAccess const& _identifierAccess)
{
    GeneratorState state(m_errors, _assembly);
    (LabelOrganizer(state))(m_parsedData);
    (CodeTransform(state, _identifierAccess))(m_parsedData);
}