aboutsummaryrefslogtreecommitdiffstats
path: root/ExpressionCompiler.cpp
blob: 6bf14f559277091ce7b856719550a12ec9abe37b (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/*
    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 AST to EVM bytecode compiler for expressions.
 */

#include <utility>
#include <numeric>
#include <libdevcore/Common.h>
#include <libsolidity/AST.h>
#include <libsolidity/ExpressionCompiler.h>
#include <libsolidity/CompilerContext.h>
#include <libsolidity/CompilerUtils.h>

using namespace std;

namespace dev
{
namespace solidity
{

void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize)
{
    ExpressionCompiler compiler(_context, _optimize);
    _expression.accept(compiler);
}

void ExpressionCompiler::appendTypeConversion(CompilerContext& _context,
                                              Type const& _typeOnStack, Type const& _targetType)
{
    ExpressionCompiler compiler(_context);
    compiler.appendTypeConversion(_typeOnStack, _targetType);
}

bool ExpressionCompiler::visit(Assignment const& _assignment)
{
    _assignment.getRightHandSide().accept(*this);
    appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType());
    _assignment.getLeftHandSide().accept(*this);
    solAssert(m_currentLValue.isValid(), "LValue not retrieved.");

    Token::Value op = _assignment.getAssignmentOperator();
    if (op != Token::ASSIGN) // compound assignment
    {
        if (m_currentLValue.storesReferenceOnStack())
            m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
        m_currentLValue.retrieveValue(_assignment, true);
        appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.getType());
        if (m_currentLValue.storesReferenceOnStack())
            m_context << eth::Instruction::SWAP1;
    }
    m_currentLValue.storeValue(_assignment);
    m_currentLValue.reset();

    return false;
}

void ExpressionCompiler::endVisit(UnaryOperation const& _unaryOperation)
{
    //@todo type checking and creating code for an operator should be in the same place:
    // the operator should know how to convert itself and to which types it applies, so
    // put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
    // represents the operator
    switch (_unaryOperation.getOperator())
    {
    case Token::NOT: // !
        m_context << eth::Instruction::ISZERO;
        break;
    case Token::BIT_NOT: // ~
        m_context << eth::Instruction::NOT;
        break;
    case Token::DELETE: // delete
        // @todo semantics change for complex types
        solAssert(m_currentLValue.isValid(), "LValue not retrieved.");

        m_context << u256(0);
        if (m_currentLValue.storesReferenceOnStack())
            m_context << eth::Instruction::SWAP1;
        m_currentLValue.storeValue(_unaryOperation);
        m_currentLValue.reset();
        break;
    case Token::INC: // ++ (pre- or postfix)
    case Token::DEC: // -- (pre- or postfix)
        solAssert(m_currentLValue.isValid(), "LValue not retrieved.");
        m_currentLValue.retrieveValue(_unaryOperation);
        if (!_unaryOperation.isPrefixOperation())
        {
            if (m_currentLValue.storesReferenceOnStack())
                m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
            else
                m_context << eth::Instruction::DUP1;
        }
        m_context << u256(1);
        if (_unaryOperation.getOperator() == Token::INC)
            m_context << eth::Instruction::ADD;
        else
            m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap
        // Stack for prefix: [ref] (*ref)+-1
        // Stack for postfix: *ref [ref] (*ref)+-1
        if (m_currentLValue.storesReferenceOnStack())
            m_context << eth::Instruction::SWAP1;
        m_currentLValue.storeValue(_unaryOperation, !_unaryOperation.isPrefixOperation());
        m_currentLValue.reset();
        break;
    case Token::ADD: // +
        // unary add, so basically no-op
        break;
    case Token::SUB: // -
        m_context << u256(0) << eth::Instruction::SUB;
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid unary operator: " +
                                                                         string(Token::toString(_unaryOperation.getOperator()))));
    }
}

bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
{
    Expression const& leftExpression = _binaryOperation.getLeftExpression();
    Expression const& rightExpression = _binaryOperation.getRightExpression();
    Type const& commonType = _binaryOperation.getCommonType();
    Token::Value const op = _binaryOperation.getOperator();

    if (op == Token::AND || op == Token::OR) // special case: short-circuiting
        appendAndOrOperatorCode(_binaryOperation);
    else
    {
        bool cleanupNeeded = false;
        if (commonType.getCategory() == Type::Category::INTEGER)
            if (Token::isCompareOp(op) || op == Token::DIV || op == Token::MOD)
                cleanupNeeded = true;

        // for commutative operators, push the literal as late as possible to allow improved optimization
        //@todo this has to be extended for literal expressions
        bool swap = (m_optimize && Token::isCommutativeOp(op) && dynamic_cast<Literal const*>(&rightExpression)
                     && !dynamic_cast<Literal const*>(&leftExpression));
        if (swap)
        {
            leftExpression.accept(*this);
            appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
            rightExpression.accept(*this);
            appendTypeConversion(*rightExpression.getType(), commonType, cleanupNeeded);
        }
        else
        {
            rightExpression.accept(*this);
            appendTypeConversion(*rightExpression.getType(), commonType, cleanupNeeded);
            leftExpression.accept(*this);
            appendTypeConversion(*leftExpression.getType(), commonType, cleanupNeeded);
        }
        if (Token::isCompareOp(op))
            appendCompareOperatorCode(op, commonType);
        else
            appendOrdinaryBinaryOperatorCode(op, commonType);
    }

    // do not visit the child nodes, we already did that explicitly
    return false;
}

bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
{
    using Location = FunctionType::Location;
    if (_functionCall.isTypeConversion())
    {
        //@todo struct construction
        solAssert(_functionCall.getArguments().size() == 1, "");
        Expression const& firstArgument = *_functionCall.getArguments().front();
        firstArgument.accept(*this);
        if (firstArgument.getType()->getCategory() == Type::Category::CONTRACT &&
                _functionCall.getType()->getCategory() == Type::Category::INTEGER)
        {
            // explicit type conversion contract -> address, nothing to do.
        }
        else
            appendTypeConversion(*firstArgument.getType(), *_functionCall.getType());
    }
    else
    {
        FunctionType const& function = dynamic_cast<FunctionType const&>(*_functionCall.getExpression().getType());
        vector<ASTPointer<Expression const>> arguments = _functionCall.getArguments();
        solAssert(arguments.size() == function.getParameterTypes().size(), "");

        switch (function.getLocation())
        {
        case Location::INTERNAL:
        {
            // Calling convention: Caller pushes return address and arguments
            // Callee removes them and pushes return values

            eth::AssemblyItem returnLabel = m_context.pushNewTag();
            for (unsigned i = 0; i < arguments.size(); ++i)
            {
                arguments[i]->accept(*this);
                appendTypeConversion(*arguments[i]->getType(), *function.getParameterTypes()[i]);
            }
            _functionCall.getExpression().accept(*this);

            m_context.appendJump();
            m_context << returnLabel;

            unsigned returnParametersSize = CompilerUtils::getSizeOnStack(function.getReturnParameterTypes());
            // callee adds return parameters, but removes arguments and return label
            m_context.adjustStackOffset(returnParametersSize - CompilerUtils::getSizeOnStack(arguments) - 1);

            // @todo for now, the return value of a function is its first return value, so remove
            // all others
            for (unsigned i = 1; i < function.getReturnParameterTypes().size(); ++i)
                CompilerUtils(m_context).popStackElement(*function.getReturnParameterTypes()[i]);
            break;
        }
        case Location::EXTERNAL:
        case Location::BARE:
        {
            FunctionCallOptions options;
            options.bare = function.getLocation() == Location::BARE;
            options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); };
            appendExternalFunctionCall(function, arguments, options);
            break;
        }
        case Location::SEND:
        {
            FunctionCallOptions options;
            options.bare = true;
            options.obtainAddress = [&]() { _functionCall.getExpression().accept(*this); };
            options.obtainValue = [&]() { arguments.front()->accept(*this); };
            appendExternalFunctionCall(FunctionType({}, {}, Location::EXTERNAL), {}, options);
            break;
        }
        case Location::SUICIDE:
            arguments.front()->accept(*this);
            //@todo might not be necessary
            appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
            m_context << eth::Instruction::SUICIDE;
            break;
        case Location::SHA3:
            arguments.front()->accept(*this);
            appendTypeConversion(*arguments.front()->getType(), *function.getParameterTypes().front(), true);
            // @todo move this once we actually use memory
            CompilerUtils(m_context).storeInMemory(0);
            m_context << u256(32) << u256(0) << eth::Instruction::SHA3;
            break;
        case Location::ECRECOVER:
        case Location::SHA256:
        case Location::RIPEMD160:
        {
            static const map<Location, u256> contractAddresses{{Location::ECRECOVER, 1},
                                                               {Location::SHA256, 2},
                                                               {Location::RIPEMD160, 3}};
            u256 contractAddress = contractAddresses.find(function.getLocation())->second;
            FunctionCallOptions options;
            options.bare = true;
            options.obtainAddress = [&]() { m_context << contractAddress; };
            options.packDensely = false;
            appendExternalFunctionCall(function, arguments, options);
            break;
        }
        default:
            BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid function type."));
        }
    }
    return false;
}

bool ExpressionCompiler::visit(NewExpression const& _newExpression)
{
    ContractType const* type = dynamic_cast<ContractType const*>(_newExpression.getType().get());
    solAssert(type, "");
    TypePointers const& types = type->getConstructorType()->getParameterTypes();
    vector<ASTPointer<Expression const>> arguments = _newExpression.getArguments();
    solAssert(arguments.size() == types.size(), "");

    // copy the contracts code into memory
    bytes const& bytecode = m_context.getCompiledContract(*_newExpression.getContract());
    m_context << u256(bytecode.size());
    //@todo could be done by actually appending the Assembly, but then we probably need to compile
    // multiple times. Will revisit once external fuctions are inlined.
    m_context.appendData(bytecode);
    //@todo copy to memory position 0, shift as soon as we use memory
    m_context << u256(0) << eth::Instruction::CODECOPY;

    unsigned dataOffset = bytecode.size();
    for (unsigned i = 0; i < arguments.size(); ++i)
    {
        arguments[i]->accept(*this);
        appendTypeConversion(*arguments[i]->getType(), *types[i]);
        unsigned const numBytes = types[i]->getCalldataEncodedSize();
        if (numBytes > 32)
            BOOST_THROW_EXCEPTION(CompilerError()
                                  << errinfo_sourceLocation(arguments[i]->getLocation())
                                  << errinfo_comment("Type " + types[i]->toString() + " not yet supported."));
        bool const leftAligned = types[i]->getCategory() == Type::Category::STRING;
        CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned);
        dataOffset += numBytes;
    }
    // size, offset, endowment
    m_context << u256(dataOffset) << u256(0) << u256(0) << eth::Instruction::CREATE;
    return false;
}

void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
{
    ASTString const& member = _memberAccess.getMemberName();
    switch (_memberAccess.getExpression().getType()->getCategory())
    {
    case Type::Category::INTEGER:
        if (member == "balance")
        {
            appendTypeConversion(*_memberAccess.getExpression().getType(),
                                 IntegerType(0, IntegerType::Modifier::ADDRESS), true);
            m_context << eth::Instruction::BALANCE;
        }
        else if (member == "send" || member.substr(0, min<size_t>(member.size(), 4)) == "call")
            appendTypeConversion(*_memberAccess.getExpression().getType(),
                                 IntegerType(0, IntegerType::Modifier::ADDRESS), true);
        else
            BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid member access to integer."));
        break;
    case Type::Category::CONTRACT:
    {
        ContractType const& type = dynamic_cast<ContractType const&>(*_memberAccess.getExpression().getType());
        m_context << type.getFunctionIndex(member);
        break;
    }
    case Type::Category::MAGIC:
        // we can ignore the kind of magic and only look at the name of the member
        if (member == "coinbase")
            m_context << eth::Instruction::COINBASE;
        else if (member == "timestamp")
            m_context << eth::Instruction::TIMESTAMP;
/*      else if (member == "blockhash")
            m_context << eth::Instruction::BLOCKHASH;
*/      else if (member == "difficulty")
            m_context << eth::Instruction::DIFFICULTY;
        else if (member == "number")
            m_context << eth::Instruction::NUMBER;
        else if (member == "gaslimit")
            m_context << eth::Instruction::GASLIMIT;
        else if (member == "sender")
            m_context << eth::Instruction::CALLER;
        else if (member == "value")
            m_context << eth::Instruction::CALLVALUE;
        else if (member == "origin")
            m_context << eth::Instruction::ORIGIN;
        else if (member == "gas")
            m_context << eth::Instruction::GAS;
        else if (member == "gasprice")
            m_context << eth::Instruction::GASPRICE;
        else
            BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown magic member."));
        break;
    case Type::Category::STRUCT:
    {
        StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType());
        m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD;
        m_currentLValue = LValue(m_context, LValue::STORAGE, *_memberAccess.getType());
        m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
        break;
    }
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Member access to unknown type."));
    }
}

bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
{
    _indexAccess.getBaseExpression().accept(*this);
    _indexAccess.getIndexExpression().accept(*this);
    appendTypeConversion(*_indexAccess.getIndexExpression().getType(),
                         *dynamic_cast<MappingType const&>(*_indexAccess.getBaseExpression().getType()).getKeyType(),
                         true);
    // @todo move this once we actually use memory
    CompilerUtils(m_context).storeInMemory(0);
    CompilerUtils(m_context).storeInMemory(32);
    m_context << u256(64) << u256(0) << eth::Instruction::SHA3;

    m_currentLValue = LValue(m_context, LValue::STORAGE, *_indexAccess.getType());
    m_currentLValue.retrieveValueIfLValueNotRequested(_indexAccess);

    return false;
}

void ExpressionCompiler::endVisit(Identifier const& _identifier)
{
    Declaration const* declaration = _identifier.getReferencedDeclaration();
    if (MagicVariableDeclaration const* magicVar = dynamic_cast<MagicVariableDeclaration const*>(declaration))
    {
        if (magicVar->getType()->getCategory() == Type::Category::CONTRACT) // must be "this"
            m_context << eth::Instruction::ADDRESS;
        return;
    }
    if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
    {
        m_context << m_context.getFunctionEntryLabel(*functionDef).pushTag();
        return;
    }
    if (dynamic_cast<VariableDeclaration const*>(declaration))
    {
        m_currentLValue.fromIdentifier(_identifier, *declaration);
        m_currentLValue.retrieveValueIfLValueNotRequested(_identifier);
        return;
    }
    BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Identifier type not expected in expression context."));
}

void ExpressionCompiler::endVisit(Literal const& _literal)
{
    switch (_literal.getType()->getCategory())
    {
    case Type::Category::INTEGER:
    case Type::Category::BOOL:
    case Type::Category::STRING:
        m_context << _literal.getType()->literalValue(_literal);
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Only integer, boolean and string literals implemented for now."));
    }
}

void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation const& _binaryOperation)
{
    Token::Value const op = _binaryOperation.getOperator();
    solAssert(op == Token::OR || op == Token::AND, "");

    _binaryOperation.getLeftExpression().accept(*this);
    m_context << eth::Instruction::DUP1;
    if (op == Token::AND)
        m_context << eth::Instruction::ISZERO;
    eth::AssemblyItem endLabel = m_context.appendConditionalJump();
    m_context << eth::Instruction::POP;
    _binaryOperation.getRightExpression().accept(*this);
    m_context << endLabel;
}

void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type const& _type)
{
    if (_operator == Token::EQ || _operator == Token::NE)
    {
        m_context << eth::Instruction::EQ;
        if (_operator == Token::NE)
            m_context << eth::Instruction::ISZERO;
    }
    else
    {
        IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
        bool const isSigned = type.isSigned();

        switch (_operator)
        {
        case Token::GTE:
            m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT)
                      << eth::Instruction::ISZERO;
            break;
        case Token::LTE:
            m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT)
                      << eth::Instruction::ISZERO;
            break;
        case Token::GT:
            m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT);
            break;
        case Token::LT:
            m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT);
            break;
        default:
            BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown comparison operator."));
        }
    }
}

void ExpressionCompiler::appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type)
{
    if (Token::isArithmeticOp(_operator))
        appendArithmeticOperatorCode(_operator, _type);
    else if (Token::isBitOp(_operator))
        appendBitOperatorCode(_operator);
    else if (Token::isShiftOp(_operator))
        appendShiftOperatorCode(_operator);
    else
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown binary operator."));
}

void ExpressionCompiler::appendArithmeticOperatorCode(Token::Value _operator, Type const& _type)
{
    IntegerType const& type = dynamic_cast<IntegerType const&>(_type);
    bool const isSigned = type.isSigned();

    switch (_operator)
    {
    case Token::ADD:
        m_context << eth::Instruction::ADD;
        break;
    case Token::SUB:
        m_context << eth::Instruction::SUB;
        break;
    case Token::MUL:
        m_context << eth::Instruction::MUL;
        break;
    case Token::DIV:
        m_context  << (isSigned ? eth::Instruction::SDIV : eth::Instruction::DIV);
        break;
    case Token::MOD:
        m_context << (isSigned ? eth::Instruction::SMOD : eth::Instruction::MOD);
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown arithmetic operator."));
    }
}

void ExpressionCompiler::appendBitOperatorCode(Token::Value _operator)
{
    switch (_operator)
    {
    case Token::BIT_OR:
        m_context << eth::Instruction::OR;
        break;
    case Token::BIT_AND:
        m_context << eth::Instruction::AND;
        break;
    case Token::BIT_XOR:
        m_context << eth::Instruction::XOR;
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown bit operator."));
    }
}

void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator)
{
    BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Shift operators not yet implemented."));
    switch (_operator)
    {
    case Token::SHL:
        break;
    case Token::SAR:
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Unknown shift operator."));
    }
}

void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
{
    // For a type extension, we need to remove all higher-order bits that we might have ignored in
    // previous operations.
    // @todo: store in the AST whether the operand might have "dirty" higher order bits

    if (_typeOnStack == _targetType && !_cleanupNeeded)
        return;
    if (_typeOnStack.getCategory() == Type::Category::INTEGER)
        appendHighBitsCleanup(dynamic_cast<IntegerType const&>(_typeOnStack));
    else if (_typeOnStack.getCategory() == Type::Category::STRING)
    {
        // nothing to do, strings are high-order-bit-aligned
        //@todo clear lower-order bytes if we allow explicit conversion to shorter strings
    }
    else if (_typeOnStack != _targetType)
        // All other types should not be convertible to non-equal types.
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid type conversion requested."));
}

void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack)
{
    if (_typeOnStack.getNumBits() == 256)
        return;
    else if (_typeOnStack.isSigned())
        m_context << u256(_typeOnStack.getNumBits() / 8 - 1) << eth::Instruction::SIGNEXTEND;
    else
        m_context << ((u256(1) << _typeOnStack.getNumBits()) - 1) << eth::Instruction::AND;
}

void ExpressionCompiler::appendExternalFunctionCall(FunctionType const& _functionType,
                                                    vector<ASTPointer<Expression const>> const& _arguments,
                                                    FunctionCallOptions const& _options)
{
    solAssert(_arguments.size() == _functionType.getParameterTypes().size(), "");

    unsigned dataOffset = _options.bare ? 0 : 1; // reserve one byte for the function index
    for (unsigned i = 0; i < _arguments.size(); ++i)
    {
        _arguments[i]->accept(*this);
        Type const& type = *_functionType.getParameterTypes()[i];
        appendTypeConversion(*_arguments[i]->getType(), type);
        unsigned const numBytes = _options.packDensely ? type.getCalldataEncodedSize() : 32;
        if (numBytes == 0 || numBytes > 32)
            BOOST_THROW_EXCEPTION(CompilerError()
                                  << errinfo_sourceLocation(_arguments[i]->getLocation())
                                  << errinfo_comment("Type " + type.toString() + " not yet supported."));
        bool const leftAligned = type.getCategory() == Type::Category::STRING;
        CompilerUtils(m_context).storeInMemory(dataOffset, numBytes, leftAligned);
        dataOffset += numBytes;
    }
    //@todo only return the first return value for now
    Type const* firstType = _functionType.getReturnParameterTypes().empty() ? nullptr :
                            _functionType.getReturnParameterTypes().front().get();
    unsigned retSize = firstType ? firstType->getCalldataEncodedSize() : 0;
    if (!_options.packDensely && retSize > 0)
        retSize = 32;
    // CALL arguments: outSize, outOff, inSize, inOff, value, addr, gas (stack top)
    m_context << u256(retSize) << u256(0) << u256(dataOffset) << u256(0);
    if (_options.obtainValue)
        _options.obtainValue();
    else
        m_context << u256(0);
    _options.obtainAddress();
    if (!_options.bare)
        m_context << u256(0) << eth::Instruction::MSTORE8;
    m_context << u256(25) << eth::Instruction::GAS << eth::Instruction::SUB
              << eth::Instruction::CALL
              << eth::Instruction::POP; // @todo do not ignore failure indicator
    if (retSize > 0)
    {
        bool const leftAligned = firstType->getCategory() == Type::Category::STRING;
        CompilerUtils(m_context).loadFromMemory(0, retSize, leftAligned);
    }
}

ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, Type const& _dataType,
                                   unsigned _baseStackOffset):
    m_context(&_compilerContext), m_type(_type), m_baseStackOffset(_baseStackOffset),
    m_stackSize(_dataType.getSizeOnStack())
{
}

void ExpressionCompiler::LValue::retrieveValue(Expression const& _expression, bool _remove) const
{
    switch (m_type)
    {
    case STACK:
    {
        unsigned stackPos = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset));
        if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
            BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                  << errinfo_comment("Stack too deep."));
        for (unsigned i = 0; i < m_stackSize; ++i)
            *m_context << eth::dupInstruction(stackPos + 1);
        break;
    }
    case STORAGE:
        if (!_expression.getType()->isValueType())
            break; // no distinction between value and reference for non-value types
        if (!_remove)
            *m_context << eth::Instruction::DUP1;
        if (m_stackSize == 1)
            *m_context << eth::Instruction::SLOAD;
        else
            for (unsigned i = 0; i < m_stackSize; ++i)
            {
                *m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1;
                if (i + 1 < m_stackSize)
                     *m_context << u256(1) << eth::Instruction::ADD;
                else
                    *m_context << eth::Instruction::POP;
            }
        break;
    case MEMORY:
        if (!_expression.getType()->isValueType())
            break; // no distinction between value and reference for non-value types
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                      << errinfo_comment("Location type not yet implemented."));
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                      << errinfo_comment("Unsupported location type."));
        break;
    }
}

void ExpressionCompiler::LValue::storeValue(Expression const& _expression, bool _move) const
{
    switch (m_type)
    {
    case STACK:
    {
        unsigned stackDiff = m_context->baseToCurrentStackOffset(unsigned(m_baseStackOffset)) - m_stackSize + 1;
        if (stackDiff > 16)
            BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                  << errinfo_comment("Stack too deep."));
        else if (stackDiff > 0)
            for (unsigned i = 0; i < m_stackSize; ++i)
                *m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP;
        if (!_move)
            retrieveValue(_expression);
        break;
    }
    case LValue::STORAGE:
        if (!_expression.getType()->isValueType())
            break; // no distinction between value and reference for non-value types
        // stack layout: value value ... value ref
        if (!_move) // copy values
        {
            if (m_stackSize + 1 > 16)
                BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                      << errinfo_comment("Stack too deep."));
            for (unsigned i = 0; i < m_stackSize; ++i)
                *m_context << eth::dupInstruction(m_stackSize + 1) << eth::Instruction::SWAP1;
        }
        if (m_stackSize > 0) // store high index value first
            *m_context << u256(m_stackSize - 1) << eth::Instruction::ADD;
        for (unsigned i = 0; i < m_stackSize; ++i)
        {
            if (i + 1 >= m_stackSize)
                *m_context << eth::Instruction::SSTORE;
            else
                // v v ... v v r+x
                *m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2
                           << eth::Instruction::SSTORE
                           << u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
        }
        break;
    case LValue::MEMORY:
        if (!_expression.getType()->isValueType())
            break; // no distinction between value and reference for non-value types
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                      << errinfo_comment("Location type not yet implemented."));
        break;
    default:
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_expression.getLocation())
                                                      << errinfo_comment("Unsupported location type."));
        break;
    }
}

void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(Expression const& _expression)
{
    if (!_expression.lvalueRequested())
    {
        retrieveValue(_expression, true);
        reset();
    }
}

void ExpressionCompiler::LValue::fromIdentifier(Identifier const& _identifier, Declaration const& _declaration)
{
    m_stackSize = _identifier.getType()->getSizeOnStack();
    if (m_context->isLocalVariable(&_declaration))
    {
        m_type = STACK;
        m_baseStackOffset = m_context->getBaseStackOffsetOfVariable(_declaration);
    }
    else if (m_context->isStateVariable(&_declaration))
    {
        m_type = STORAGE;
        *m_context << m_context->getStorageLocationOfVariable(_declaration);
    }
    else
        BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_identifier.getLocation())
                                                      << errinfo_comment("Identifier type not supported or identifier not found."));
}

}
}