aboutsummaryrefslogtreecommitdiffstats
path: root/Compiler.cpp
blob: a0cad5374719ab4001e129583adc0fa16222a4e6 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
    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 2014
 * Solidity compiler.
 */

#include <algorithm>
#include <libevmcore/Instruction.h>
#include <libevmcore/Assembly.h>
#include <libsolidity/AST.h>
#include <libsolidity/Compiler.h>
#include <libsolidity/ExpressionCompiler.h>
#include <libsolidity/CompilerUtils.h>

using namespace std;

namespace dev {
namespace solidity {

void Compiler::compileContract(ContractDefinition const& _contract, vector<MagicVariableDeclaration const*> const& _magicGlobals,
                               map<ContractDefinition const*, bytes const*> const& _contracts)
{
    m_context = CompilerContext(); // clear it just in case
    m_context.setCompiledContracts(_contracts);

    for (MagicVariableDeclaration const* variable: _magicGlobals)
        m_context.addMagicGlobal(*variable);

    for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
        if (function->getName() != _contract.getName()) // don't add the constructor here
            m_context.addFunction(*function);
    registerStateVariables(_contract);

    appendFunctionSelector(_contract);
    for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
        if (function->getName() != _contract.getName()) // don't add the constructor here
            function->accept(*this);

    packIntoContractCreator(_contract, _contracts);
}

void Compiler::packIntoContractCreator(ContractDefinition const& _contract,
                                       map<ContractDefinition const*, bytes const*> const& _contracts)
{
    CompilerContext runtimeContext;
    runtimeContext.setCompiledContracts(_contracts);
    swap(m_context, runtimeContext);

    registerStateVariables(_contract);

    FunctionDefinition* constructor = nullptr;
    for (ASTPointer<FunctionDefinition> const& function: _contract.getDefinedFunctions())
        if (function->getName() == _contract.getName())
        {
            constructor = function.get();
            break;
        }
    eth::AssemblyItem sub = m_context.addSubroutine(runtimeContext.getAssembly());
    // stack contains sub size
    if (constructor)
    {
        eth::AssemblyItem returnTag = m_context.pushNewTag();
        m_context.addFunction(*constructor); // note that it cannot be called due to syntactic reasons
        // copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
        unsigned argumentSize = 0;
        for (ASTPointer<VariableDeclaration> const& var: constructor->getParameters())
            argumentSize += var->getType()->getCalldataEncodedSize();
        if (argumentSize > 0)
        {
            m_context << u256(argumentSize);
            m_context.appendProgramSize();
            m_context << u256(1); // copy it to byte one as expected for ABI calls
            m_context << eth::Instruction::CODECOPY;
            appendCalldataUnpacker(*constructor, true);
        }
        //@todo calling other functions inside the constructor should either trigger a parse error
        //or we should copy them here (register them above and call "accept") - detecting which
        // functions are referenced / called needs to be done in a recursive way.
        m_context.appendJumpTo(m_context.getFunctionEntryLabel(*constructor));
        constructor->accept(*this);
        m_context << returnTag;
    }

    m_context << eth::Instruction::DUP1 << sub << u256(0) << eth::Instruction::CODECOPY;
    m_context << u256(0) << eth::Instruction::RETURN;
}

void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
{
    vector<FunctionDefinition const*> interfaceFunctions = _contract.getInterfaceFunctions();
    vector<eth::AssemblyItem> callDataUnpackerEntryPoints;

    if (interfaceFunctions.size() > 255)
        BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("More than 255 public functions for contract."));

    // retrieve the first byte of the call data, which determines the called function
    // @todo This code had a jump table in a previous version which was more efficient but also
    // error prone (due to the optimizer and variable length tag addresses)
    m_context << u256(1) << u256(0) // some constants
              << eth::dupInstruction(1) << eth::Instruction::CALLDATALOAD
              << eth::dupInstruction(2) << eth::Instruction::BYTE
              << eth::dupInstruction(2);

    // stack here: 1 0 <funid> 0, stack top will be counted up until it matches funid
    for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid)
    {
        callDataUnpackerEntryPoints.push_back(m_context.newTag());
        m_context << eth::dupInstruction(2) << eth::dupInstruction(2) << eth::Instruction::EQ;
        m_context.appendConditionalJumpTo(callDataUnpackerEntryPoints.back());
        if (funid < interfaceFunctions.size() - 1)
            m_context << eth::dupInstruction(4) << eth::Instruction::ADD;
    }
    m_context << eth::Instruction::STOP; // function not found

    for (unsigned funid = 0; funid < interfaceFunctions.size(); ++funid)
    {
        FunctionDefinition const& function = *interfaceFunctions[funid];
        m_context << callDataUnpackerEntryPoints[funid];
        eth::AssemblyItem returnTag = m_context.pushNewTag();
        appendCalldataUnpacker(function);
        m_context.appendJumpTo(m_context.getFunctionEntryLabel(function));
        m_context << returnTag;
        appendReturnValuePacker(function);
    }
}

unsigned Compiler::appendCalldataUnpacker(FunctionDefinition const& _function, bool _fromMemory)
{
    // We do not check the calldata size, everything is zero-padded.
    unsigned dataOffset = 1;
    //@todo this can be done more efficiently, saving some CALLDATALOAD calls
    for (ASTPointer<VariableDeclaration> const& var: _function.getParameters())
    {
        unsigned const numBytes = var->getType()->getCalldataEncodedSize();
        if (numBytes > 32)
            BOOST_THROW_EXCEPTION(CompilerError()
                                  << errinfo_sourceLocation(var->getLocation())
                                  << errinfo_comment("Type " + var->getType()->toString() + " not yet supported."));
        bool leftAligned = var->getType()->getCategory() == Type::Category::STRING;
        CompilerUtils(m_context).loadFromMemory(dataOffset, numBytes, leftAligned, !_fromMemory);
        dataOffset += numBytes;
    }
    return dataOffset;
}

void Compiler::appendReturnValuePacker(FunctionDefinition const& _function)
{
    //@todo this can be also done more efficiently
    unsigned dataOffset = 0;
    vector<ASTPointer<VariableDeclaration>> const& parameters = _function.getReturnParameters();
    unsigned stackDepth = CompilerUtils(m_context).getSizeOnStack(parameters);
    for (unsigned i = 0; i < parameters.size(); ++i)
    {
        Type const& paramType = *parameters[i]->getType();
        unsigned numBytes = paramType.getCalldataEncodedSize();
        if (numBytes > 32)
            BOOST_THROW_EXCEPTION(CompilerError()
                                  << errinfo_sourceLocation(parameters[i]->getLocation())
                                  << errinfo_comment("Type " + paramType.toString() + " not yet supported."));
        CompilerUtils(m_context).copyToStackTop(stackDepth, paramType);
        bool const leftAligned = paramType.getCategory() == Type::Category::STRING;
        CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned);
        stackDepth -= paramType.getSizeOnStack();
        dataOffset += numBytes;
    }
    // note that the stack is not cleaned up here
    m_context << u256(dataOffset) << u256(0) << eth::Instruction::RETURN;
}

void Compiler::registerStateVariables(ContractDefinition const& _contract)
{
    //@todo sort them?
    for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
        m_context.addStateVariable(*variable);
}

bool Compiler::visit(FunctionDefinition const& _function)
{
    //@todo to simplify this, the calling convention could by changed such that
    // caller puts: [retarg0] ... [retargm] [return address] [arg0] ... [argn]
    // although note that this reduces the size of the visible stack

    m_context.startNewFunction();
    m_returnTag = m_context.newTag();
    m_breakTags.clear();
    m_continueTags.clear();

    m_context << m_context.getFunctionEntryLabel(_function);

    // stack upon entry: [return address] [arg0] [arg1] ... [argn]
    // reserve additional slots: [retarg0] ... [retargm] [localvar0] ... [localvarp]

    for (ASTPointer<VariableDeclaration const> const& variable: _function.getParameters())
        m_context.addVariable(*variable);
    for (ASTPointer<VariableDeclaration const> const& variable: _function.getReturnParameters())
        m_context.addAndInitializeVariable(*variable);
    for (VariableDeclaration const* localVariable: _function.getLocalVariables())
        m_context.addAndInitializeVariable(*localVariable);

    _function.getBody().accept(*this);

    m_context << m_returnTag;

    // Now we need to re-shuffle the stack. For this we keep a record of the stack layout
    // that shows the target positions of the elements, where "-1" denotes that this element needs
    // to be removed from the stack.
    // Note that the fact that the return arguments are of increasing index is vital for this
    // algorithm to work.

    unsigned const argumentsSize = CompilerUtils::getSizeOnStack(_function.getParameters());
    unsigned const returnValuesSize = CompilerUtils::getSizeOnStack(_function.getReturnParameters());
    unsigned const localVariablesSize = CompilerUtils::getSizeOnStack(_function.getLocalVariables());

    vector<int> stackLayout;
    stackLayout.push_back(returnValuesSize); // target of return address
    stackLayout += vector<int>(argumentsSize, -1); // discard all arguments
    for (unsigned i = 0; i < returnValuesSize; ++i)
        stackLayout.push_back(i);
    stackLayout += vector<int>(localVariablesSize, -1);

    while (stackLayout.back() != int(stackLayout.size() - 1))
        if (stackLayout.back() < 0)
        {
            m_context << eth::Instruction::POP;
            stackLayout.pop_back();
        }
        else
        {
            m_context << eth::swapInstruction(stackLayout.size() - stackLayout.back() - 1);
            swap(stackLayout[stackLayout.back()], stackLayout.back());
        }
    //@todo assert that everything is in place now

    m_context << eth::Instruction::JUMP;

    return false;
}

bool Compiler::visit(IfStatement const& _ifStatement)
{
    compileExpression(_ifStatement.getCondition());
    eth::AssemblyItem trueTag = m_context.appendConditionalJump();
    if (_ifStatement.getFalseStatement())
        _ifStatement.getFalseStatement()->accept(*this);
    eth::AssemblyItem endTag = m_context.appendJumpToNew();
    m_context << trueTag;
    _ifStatement.getTrueStatement().accept(*this);
    m_context << endTag;
    return false;
}

bool Compiler::visit(WhileStatement const& _whileStatement)
{
    eth::AssemblyItem loopStart = m_context.newTag();
    eth::AssemblyItem loopEnd = m_context.newTag();
    m_continueTags.push_back(loopStart);
    m_breakTags.push_back(loopEnd);

    m_context << loopStart;
    compileExpression(_whileStatement.getCondition());
    m_context << eth::Instruction::ISZERO;
    m_context.appendConditionalJumpTo(loopEnd);

    _whileStatement.getBody().accept(*this);

    m_context.appendJumpTo(loopStart);
    m_context << loopEnd;

    m_continueTags.pop_back();
    m_breakTags.pop_back();
    return false;
}

bool Compiler::visit(ForStatement const& _forStatement)
{
    // LTODO
    (void) _forStatement;
    return false;
}

bool Compiler::visit(Continue const&)
{
    if (!m_continueTags.empty())
        m_context.appendJumpTo(m_continueTags.back());
    return false;
}

bool Compiler::visit(Break const&)
{
    if (!m_breakTags.empty())
        m_context.appendJumpTo(m_breakTags.back());
    return false;
}

bool Compiler::visit(Return const& _return)
{
    //@todo modifications are needed to make this work with functions returning multiple values
    if (Expression const* expression = _return.getExpression())
    {
        compileExpression(*expression);
        VariableDeclaration const& firstVariable = *_return.getFunctionReturnParameters().getParameters().front();
        ExpressionCompiler::appendTypeConversion(m_context, *expression->getType(), *firstVariable.getType());

        CompilerUtils(m_context).moveToStackVariable(firstVariable);
    }
    m_context.appendJumpTo(m_returnTag);
    return false;
}

bool Compiler::visit(VariableDefinition const& _variableDefinition)
{
    if (Expression const* expression = _variableDefinition.getExpression())
    {
        compileExpression(*expression);
        ExpressionCompiler::appendTypeConversion(m_context,
                                                 *expression->getType(),
                                                 *_variableDefinition.getDeclaration().getType());
        CompilerUtils(m_context).moveToStackVariable(_variableDefinition.getDeclaration());
    }
    return false;
}

bool Compiler::visit(ExpressionStatement const& _expressionStatement)
{
    Expression const& expression = _expressionStatement.getExpression();
    compileExpression(expression);
    CompilerUtils(m_context).popStackElement(*expression.getType());
    return false;
}

void Compiler::compileExpression(Expression const& _expression)
{
    ExpressionCompiler::compileExpression(m_context, _expression, m_optimize);
}

}
}