aboutsummaryrefslogtreecommitdiffstats
path: root/test/libjulia/Rematerialiser.cpp
blob: ae254ad93d38568aec0a6411e50fb8d992b51fc3 (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
/*
    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/>.
*/
/**
 * @date 2017
 * Unit tests for the rematerialiser optimizer stage.
 */

#include <test/libjulia/Common.h>

#include <libjulia/optimiser/Rematerialiser.h>

#include <libsolidity/inlineasm/AsmPrinter.h>

#include <boost/test/unit_test.hpp>

#include <boost/range/adaptors.hpp>
#include <boost/algorithm/string/join.hpp>

using namespace std;
using namespace dev;
using namespace dev::julia;
using namespace dev::julia::test;
using namespace dev::solidity;


#define CHECK(_original, _expectation)\
do\
{\
    assembly::AsmPrinter p;\
    Block b = disambiguate(_original, false);\
    (Rematerialiser{})(b);\
    string result = p(b);\
    BOOST_CHECK_EQUAL(result, format(_expectation, false));\
}\
while(false)

BOOST_AUTO_TEST_SUITE(IuliaRematerialiser)

BOOST_AUTO_TEST_CASE(smoke_test)
{
    CHECK("{ }", "{ }");
}

BOOST_AUTO_TEST_CASE(trivial)
{
    CHECK(
        "{ let a := 1 let b := a mstore(0, b) }",
        "{ let a := 1 let b := 1 mstore(0, 1) }"
    );
}

BOOST_AUTO_TEST_CASE(expression)
{
    CHECK(
        "{ let a := add(mul(calldatasize(), 2), number()) let b := add(a, a) }",
        "{ let a := add(mul(calldatasize(), 2), number()) let b := add("
            "add(mul(calldatasize(), 2), number()),"
            "add(mul(calldatasize(), 2), number())"
        ") }"
    );
}

BOOST_AUTO_TEST_CASE(non_movable_instr)
{
    CHECK(
        "{ let a := 1 let b := mload(a) let c := a mstore(add(a, b), c) }",
        "{ let a := 1 let b := mload(1) let c := 1 mstore(add(1, b), 1) }"
    );
}

BOOST_AUTO_TEST_CASE(non_movable_fun)
{
    CHECK(
        "{ function f(x) -> y {} let a := 1 let b := f(a) let c := a mstore(add(a, b), c) }",
        "{ function f(x) -> y {} let a := 1 let b := f(1) let c := 1 mstore(add(1, b), 1) }"
    );
}

BOOST_AUTO_TEST_CASE(branches_if)
{
    CHECK(
        "{ let a := 1 let b := 2 if b { pop(b) b := a } let c := b }",
        "{ let a := 1 let b := 2 if 2 { pop(2) b := 1 } let c := b }"
    );
}

BOOST_AUTO_TEST_CASE(branches_switch)
{
    CHECK(
        "{ let a := 1 let b := 2 switch number() case 1 { b := a } default { let x := a let y := b b := a } pop(add(a, b)) }",
        "{ let a := 1 let b := 2 switch number() case 1 { b := 1 } default { let x := 1 let y := b b := 1 } pop(add(1, b)) }"
    );
}

BOOST_AUTO_TEST_CASE(branches_for)
{
    CHECK(
        "{ let a := 1 for { pop(a) } a { pop(a) } { pop(a) } }",
        "{ let a := 1 for { pop(1) } 1 { pop(1) } { pop(1) } }"
    );
    CHECK(
        "{ let a := 1 for { pop(a) } a { pop(a) } { a := 7 let c := a } let x := a }",
        "{ let a := 1 for { pop(1) } a { pop(7) } { a := 7 let c := 7 } let x := a }"
    );
}

BOOST_AUTO_TEST_CASE(reassignment)
{
    CHECK(
        "{ let a := 1 pop(a) if a { a := 2 } let b := mload(a) pop(b) }",
        "{ let a := 1 pop(1) if 1 { a := 2 } let b := mload(a) pop(b) }"
    );
}

BOOST_AUTO_TEST_SUITE_END()