aboutsummaryrefslogtreecommitdiffstats
path: root/LValue.cpp
blob: 8e618e87f18bdca5aa15a46e52ee7c7f41062129 (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
/*
    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 2015
 * LValues for use in the expresison compiler.
 */

#include <libsolidity/LValue.h>
#include <libevmcore/Instruction.h>
#include <libsolidity/Types.h>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerUtils.h>

using namespace std;
using namespace dev;
using namespace solidity;


StackVariable::StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration):
    LValue(_compilerContext, *_declaration.getType()),
    m_baseStackOffset(m_context.getBaseStackOffsetOfVariable(_declaration)),
    m_size(m_dataType.getSizeOnStack())
{
}

void StackVariable::retrieveValue(SourceLocation const& _location, bool) const
{
    unsigned stackPos = m_context.baseToCurrentStackOffset(m_baseStackOffset);
    if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
        BOOST_THROW_EXCEPTION(CompilerError()
            << errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
    for (unsigned i = 0; i < m_size; ++i)
        m_context << eth::dupInstruction(stackPos + 1);
}

void StackVariable::storeValue(Type const&, SourceLocation const& _location, bool _move) const
{
    unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset) - m_size + 1;
    if (stackDiff > 16)
        BOOST_THROW_EXCEPTION(CompilerError()
            << errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
    else if (stackDiff > 0)
        for (unsigned i = 0; i < m_size; ++i)
            m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP;
    if (!_move)
        retrieveValue(_location);
}

void StackVariable::setToZero(SourceLocation const& _location, bool) const
{
    unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset);
    if (stackDiff > 16)
        BOOST_THROW_EXCEPTION(CompilerError()
            << errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
    solAssert(stackDiff >= m_size - 1, "");
    for (unsigned i = 0; i < m_size; ++i)
        m_context << u256(0) << eth::swapInstruction(stackDiff + 1 - i)
            << eth::Instruction::POP;
}


StorageItem::StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration):
    StorageItem(_compilerContext, *_declaration.getType())
{
    m_context << m_context.getStorageLocationOfVariable(_declaration) << u256(0);
}

StorageItem::StorageItem(CompilerContext& _compilerContext, Type const& _type):
    LValue(_compilerContext, _type)
{
    if (m_dataType.isValueType())
    {
        solAssert(m_dataType.getStorageSize() == m_dataType.getSizeOnStack(), "");
        //@todo the meaning of getStorageSize() probably changes
        solAssert(m_dataType.getStorageSize() == 1, "Invalid storage size.");
    }
}

void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
{
    // stack: storage_key storage_offset
    if (!m_dataType.isValueType())
        return; // no distinction between value and reference for non-value types
    if (!_remove)
        CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
    m_context
        << eth::Instruction::SWAP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1
        << u256(2) << eth::Instruction::EXP << eth::Instruction::SWAP1 << eth::Instruction::DIV;
    //@todo higher order bits might be dirty. Is this bad?
    //@todo this does not work for types that are left-aligned on the stack.
    // make those types right-aligned?
}

void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{
    // stack: value storage_key storage_offset
    if (m_dataType.isValueType())
    {
        //@todo OR the value into the storage like it is done for ByteArrayElement
        m_context
            << u256(2) << eth::Instruction::EXP << eth::Instruction::DUP3 << eth::Instruction::MUL
            << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
        if (_move)
            m_context << eth::Instruction::POP;
    }
    else
    {
        solAssert(
            _sourceType.getCategory() == m_dataType.getCategory(),
            "Wrong type conversation for assignment.");
        if (m_dataType.getCategory() == Type::Category::Array)
        {
            ArrayUtils(m_context).copyArrayToStorage(
                        dynamic_cast<ArrayType const&>(m_dataType),
                        dynamic_cast<ArrayType const&>(_sourceType));
            if (_move)
                CompilerUtils(m_context).popStackElement(_sourceType);
        }
        else if (m_dataType.getCategory() == Type::Category::Struct)
        {
            // stack layout: source_ref source_offset target_ref target_offset
            auto const& structType = dynamic_cast<StructType const&>(m_dataType);
            solAssert(structType == _sourceType, "Struct assignment with conversion.");
            for (auto const& member: structType.getMembers())
            {
                //@todo actually use offsets
                // assign each member that is not a mapping
                TypePointer const& memberType = member.second;
                if (memberType->getCategory() == Type::Category::Mapping)
                    continue;
                m_context << structType.getStorageOffsetOfMember(member.first)
                    << eth::Instruction::DUP5 << eth::Instruction::DUP2 << eth::Instruction::ADD;
                m_context << u256(0); // zero offset
                // stack: source_ref source_off target_ref target_off member_offset source_member_ref source_member_off
                StorageItem(m_context, *memberType).retrieveValue(_location, true);
                // stack: source_ref source_off target_ref target_off member_offset source_value...
                solAssert(2 + memberType->getSizeOnStack() <= 16, "Stack too deep.");
                m_context << eth::dupInstruction(3 + memberType->getSizeOnStack())
                    << eth::dupInstruction(2 + memberType->getSizeOnStack()) << eth::Instruction::ADD;
                // stack: source_ref source_off target_ref target_off member_offset source_value... target_member_ref
                m_context << u256(0); // zero offset
                StorageItem(m_context, *memberType).storeValue(*memberType, _location, true);
                m_context << eth::Instruction::POP;
            }
            if (_move)
                m_context
                    << eth::Instruction::POP << eth::Instruction::POP
                    << eth::Instruction::POP << eth::Instruction::POP;
            else
                m_context
                    << eth::Instruction::SWAP2 << eth::Instruction::POP
                    << eth::Instruction::SWAP2 << eth::Instruction::POP;
        }
        else
            BOOST_THROW_EXCEPTION(
                InternalCompilerError()
                    << errinfo_sourceLocation(_location)
                    << errinfo_comment("Invalid non-value type for assignment."));
    }
}

void StorageItem::setToZero(SourceLocation const&, bool _removeReference) const
{
    if (m_dataType.getCategory() == Type::Category::Array)
    {
        if (!_removeReference)
            CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
        ArrayUtils(m_context).clearArray(dynamic_cast<ArrayType const&>(m_dataType));
    }
    else if (m_dataType.getCategory() == Type::Category::Struct)
    {
        // stack layout: storage_key storage_offset
        auto const& structType = dynamic_cast<StructType const&>(m_dataType);
        for (auto const& member: structType.getMembers())
        {
            // zero each member that is not a mapping
            TypePointer const& memberType = member.second;
            if (memberType->getCategory() == Type::Category::Mapping)
                continue;
            // @todo actually use offset
            m_context
                << structType.getStorageOffsetOfMember(member.first)
                << eth::Instruction::DUP3 << eth::Instruction::ADD;
            m_context << u256(0);
            StorageItem(m_context, *memberType).setToZero();
        }
        if (_removeReference)
            m_context << eth::Instruction::POP << eth::Instruction::POP;
    }
    else
    {
        solAssert(m_dataType.isValueType(), "Clearing of unsupported type requested: " + m_dataType.toString());
        // @todo actually use offset
        if (!_removeReference)
            CompilerUtils(m_context).copyToStackTop(sizeOnStack(), sizeOnStack());
        m_context << eth::Instruction::POP << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
    }
}

/// Used in StorageByteArrayElement
static FixedBytesType byteType(1);

StorageByteArrayElement::StorageByteArrayElement(CompilerContext& _compilerContext):
    LValue(_compilerContext, byteType)
{
}

void StorageByteArrayElement::retrieveValue(SourceLocation const&, bool _remove) const
{
    // stack: ref byte_number
    if (_remove)
        m_context << eth::Instruction::SWAP1 << eth::Instruction::SLOAD
            << eth::Instruction::SWAP1 << eth::Instruction::BYTE;
    else
        m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD
            << eth::Instruction::DUP2 << eth::Instruction::BYTE;
    m_context << (u256(1) << (256 - 8)) << eth::Instruction::MUL;
}

void StorageByteArrayElement::storeValue(Type const&, SourceLocation const&, bool _move) const
{
    //@todo optimize this

    // stack: value ref byte_number
    m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
    // stack: value ref (1<<(8*(31-byte_number)))
    m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
    // stack: value ref (1<<(8*(31-byte_number))) old_full_value
    // clear byte in old value
    m_context << eth::Instruction::DUP2 << u256(0xff) << eth::Instruction::MUL
        << eth::Instruction::NOT << eth::Instruction::AND;
    // stack: value ref (1<<(32-byte_number)) old_full_value_with_cleared_byte
    m_context << eth::Instruction::SWAP1;
    m_context << (u256(1) << (256 - 8)) << eth::Instruction::DUP5 << eth::Instruction::DIV
        << eth::Instruction::MUL << eth::Instruction::OR;
    // stack: value ref new_full_value
    m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
    if (_move)
        m_context << eth::Instruction::POP;
}

void StorageByteArrayElement::setToZero(SourceLocation const&, bool _removeReference) const
{
    // stack: ref byte_number
    if (!_removeReference)
        m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
    m_context << u256(31) << eth::Instruction::SUB << u256(0x100) << eth::Instruction::EXP;
    // stack: ref (1<<(8*(31-byte_number)))
    m_context << eth::Instruction::DUP2 << eth::Instruction::SLOAD;
    // stack: ref (1<<(8*(31-byte_number))) old_full_value
    // clear byte in old value
    m_context << eth::Instruction::SWAP1 << u256(0xff) << eth::Instruction::MUL << eth::Instruction::AND;
    // stack: ref old_full_value_with_cleared_byte
    m_context << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
    if (!_removeReference)
        m_context << eth::Instruction::SWAP1;
    else
        m_context << eth::Instruction::POP;
}

StorageArrayLength::StorageArrayLength(CompilerContext& _compilerContext, const ArrayType& _arrayType):
    LValue(_compilerContext, *_arrayType.getMemberType("length")),
    m_arrayType(_arrayType)
{
    solAssert(m_arrayType.isDynamicallySized(), "");
    // storage byte offset must be zero
    m_context << eth::Instruction::POP;
}

void StorageArrayLength::retrieveValue(SourceLocation const&, bool _remove) const
{
    if (!_remove)
        m_context << eth::Instruction::DUP1;
    m_context << eth::Instruction::SLOAD;
}

void StorageArrayLength::storeValue(Type const&, SourceLocation const&, bool _move) const
{
    if (_move)
        m_context << eth::Instruction::SWAP1;
    else
        m_context << eth::Instruction::DUP2;
    ArrayUtils(m_context).resizeDynamicArray(m_arrayType);
}

void StorageArrayLength::setToZero(SourceLocation const&, bool _removeReference) const
{
    if (!_removeReference)
        m_context << eth::Instruction::DUP1;
    ArrayUtils(m_context).clearDynamicArray(m_arrayType);
}