-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathBlockHasher.h
151 lines (128 loc) · 4.43 KB
/
BlockHasher.h
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
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Optimiser component that calculates hash values for blocks.
*/
#pragma once
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/ASTForward.h>
#include <libyul/YulName.h>
namespace solidity::yul
{
class HasherBase
{
public:
static constexpr uint64_t fnvPrime = 1099511628211u;
static constexpr uint64_t fnvEmptyHash = 14695981039346656037u;
protected:
void hash8(uint8_t _value)
{
m_hash *= fnvPrime;
m_hash ^= _value;
}
void hash16(uint16_t _value)
{
hash8(static_cast<uint8_t>(_value & 0xFF));
hash8(static_cast<uint8_t>(_value >> 8));
}
void hash32(uint32_t _value)
{
hash16(static_cast<uint16_t>(_value & 0xFFFF));
hash16(static_cast<uint16_t>(_value >> 16));
}
void hash64(uint64_t _value)
{
hash32(static_cast<uint32_t>(_value & 0xFFFFFFFF));
hash32(static_cast<uint32_t>(_value >> 32));
}
uint64_t m_hash = fnvEmptyHash;
};
class ASTHasherBase: public HasherBase
{
protected:
void hashLiteral(solidity::yul::Literal const& _literal);
void hashFunctionCall(FunctionCall const& _functionCall);
};
/**
* Optimiser component that calculates hash values for blocks.
* Syntactically equal blocks will have identical hashes and
* blocks with equal hashes will likely be syntactically equal.
*
* The names of internally declared variables are replaced by
* a simple counter, so differing names are not taken into account,
* but only the order of references to declared variables.
*
* Similarly, the names of referenced external variables are not considered,
* but replaced by a (distinct) counter as well.
*
* Prerequisite: Disambiguator, ForLoopInitRewriter
*/
class BlockHasher: public ASTWalker, public ASTHasherBase
{
public:
using ASTWalker::operator();
void operator()(Literal const&) override;
void operator()(Identifier const&) override;
void operator()(FunctionCall const& _funCall) override;
void operator()(ExpressionStatement const& _statement) override;
void operator()(Assignment const& _assignment) override;
void operator()(VariableDeclaration const& _varDecl) override;
void operator()(If const& _if) override;
void operator()(Switch const& _switch) override;
void operator()(FunctionDefinition const&) override;
void operator()(ForLoop const&) override;
void operator()(Break const&) override;
void operator()(Continue const&) override;
void operator()(Leave const&) override;
void operator()(Block const& _block) override;
static std::map<Block const*, uint64_t> run(Block const& _block);
private:
BlockHasher(std::map<Block const*, uint64_t>& _blockHashes): m_blockHashes(_blockHashes) {}
std::map<Block const*, uint64_t>& m_blockHashes;
struct VariableReference
{
size_t id = 0;
bool isExternal = false;
};
std::map<YulName, VariableReference> m_variableReferences;
std::vector<YulName> m_externalReferences;
size_t m_externalIdentifierCount = 0;
size_t m_internalIdentifierCount = 0;
};
/**
* Computes hashes of expressions that are likely different for syntactically different expressions.
* In contrast to the BlockHasher, hashes of identifiers are likely different if the identifiers
* have a different name and the same if the name matches.
* This means this hasher should only be used on disambiguated sources.
*/
class ExpressionHasher: public ASTWalker, public ASTHasherBase
{
public:
/// Computes a hash of an expression that (in contrast to the behaviour of the class)
/// distinguishes (up to hash collisions) variables with different names.
static uint64_t run(Expression const& _e);
using ASTWalker::operator();
void operator()(Literal const&) override;
void operator()(Identifier const&) override;
void operator()(FunctionCall const& _funCall) override;
};
struct ExpressionHash
{
uint64_t operator()(Expression const& _expression) const
{
return ExpressionHasher::run(_expression);
}
};
}