Skip to content

Commit 73e7b84

Browse files
Merge pull request #13381 from ethereum/cleanupDocs
Clarify variable cleanup in docs.
2 parents 2f33105 + 27dba5c commit 73e7b84

File tree

1 file changed

+84
-25
lines changed

1 file changed

+84
-25
lines changed

docs/internals/variable_cleanup.rst

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
Cleaning Up Variables
55
*********************
66

7-
When a value is shorter than 256 bit, in some cases the remaining bits
8-
must be cleaned.
9-
The Solidity compiler is designed to clean such remaining bits before any operations
7+
Ultimately, all values in the EVM are stored in 256 bit words.
8+
Thus, in some cases, when the type of a value has less than 256 bits,
9+
it is necessary to clean the remaining bits.
10+
The Solidity compiler is designed to do such cleaning before any operations
1011
that might be adversely affected by the potential garbage in the remaining bits.
1112
For example, before writing a value to memory, the remaining bits need
1213
to be cleared because the memory contents can be used for computing
@@ -28,25 +29,83 @@ the boolean values before they are used as the condition for
2829
In addition to the design principle above, the Solidity compiler
2930
cleans input data when it is loaded onto the stack.
3031

31-
Different types have different rules for cleaning up invalid values:
32-
33-
+---------------+---------------+-------------------+
34-
|Type |Valid Values |Invalid Values Mean|
35-
+===============+===============+===================+
36-
|enum of n |0 until n - 1 |exception |
37-
|members | | |
38-
+---------------+---------------+-------------------+
39-
|bool |0 or 1 |1 |
40-
+---------------+---------------+-------------------+
41-
|signed integers|sign-extended |currently silently |
42-
| |word |wraps; in the |
43-
| | |future exceptions |
44-
| | |will be thrown |
45-
| | | |
46-
| | | |
47-
+---------------+---------------+-------------------+
48-
|unsigned |higher bits |currently silently |
49-
|integers |zeroed |wraps; in the |
50-
| | |future exceptions |
51-
| | |will be thrown |
52-
+---------------+---------------+-------------------+
32+
The following table describes the cleaning rules applied to different types,
33+
where ``higher bits`` refers to the remaining bits in case the type has less than 256 bits.
34+
35+
+---------------+---------------+-------------------------+
36+
|Type |Valid Values |Cleanup of Invalid Values|
37+
+===============+===============+=========================+
38+
|enum of n |0 until n - 1 |throws exception |
39+
|members | | |
40+
+---------------+---------------+-------------------------+
41+
|bool |0 or 1 |results in 1 |
42+
+---------------+---------------+-------------------------+
43+
|signed integers|higher bits |currently silently |
44+
| |set to the |signextends to a valid |
45+
| |sign bit |value, i.e. all higher |
46+
| | |bits are set to the sign |
47+
| | |bit; may throw an |
48+
| | |exception in the future |
49+
+---------------+---------------+-------------------------+
50+
|unsigned |higher bits |currently silently masks |
51+
|integers |zeroed |to a valid value, i.e. |
52+
| | |all higher bits are set |
53+
| | |to zero; may throw an |
54+
| | |exception in the future |
55+
+---------------+---------------+-------------------------+
56+
57+
Note that valid and invalid values are dependent on their type size.
58+
Consider ``uint8``, the unsigned 8-bit type, which has the following valid values:
59+
60+
.. code-block:: none
61+
62+
0000...0000 0000 0000
63+
0000...0000 0000 0001
64+
0000...0000 0000 0010
65+
....
66+
0000...0000 1111 1111
67+
68+
Any invalid value will have the higher bits set to zero:
69+
70+
.. code-block:: none
71+
72+
0101...1101 0010 1010 invalid value
73+
0000...0000 0010 1010 cleaned value
74+
75+
For ``int8``, the signed 8-bit type, the valid values are:
76+
77+
Negative
78+
79+
.. code-block:: none
80+
81+
1111...1111 1111 1111
82+
1111...1111 1111 1110
83+
....
84+
1111...1111 1000 0000
85+
86+
Positive
87+
88+
.. code-block:: none
89+
90+
0000...0000 0000 0000
91+
0000...0000 0000 0001
92+
0000...0000 0000 0010
93+
....
94+
0000...0000 1111 1111
95+
96+
The compiler will ``signextend`` the sign bit, which is 1 for negative and 0 for
97+
positive values, overwriting the higher bits:
98+
99+
Negative
100+
101+
.. code-block:: none
102+
103+
0010...1010 1111 1111 invalid value
104+
1111...1111 1111 1111 cleaned value
105+
106+
Positive
107+
108+
.. code-block:: none
109+
110+
1101...0101 0000 0100 invalid value
111+
0000...0000 0000 0100 cleaned value

0 commit comments

Comments
 (0)