diff options
Diffstat (limited to 'docs/assembly.rst')
-rw-r--r-- | docs/assembly.rst | 43 |
1 files changed, 9 insertions, 34 deletions
diff --git a/docs/assembly.rst b/docs/assembly.rst index c233985b..825892b3 100644 --- a/docs/assembly.rst +++ b/docs/assembly.rst @@ -23,7 +23,7 @@ arising when writing manual assembly by the following features: * functional-style opcodes: ``mul(1, add(2, 3))`` instead of ``push1 3 push1 2 add push1 1 mul`` * assembly-local variables: ``let x := add(2, 3) let y := mload(0x40) x := add(x, y)`` -* access to external variables: ``function f(uint x) { assembly { x := sub(x, 1) } }`` +* access to external variables: ``function f(uint x) public { assembly { x := sub(x, 1) } }`` * labels: ``let x := 10 repeat: x := sub(x, 1) jumpi(repeat, eq(x, 0))`` * loops: ``for { let i := 0 } lt(i, x) { i := add(i, 1) } { y := mul(2, y) }`` * if statements: ``if slt(x, 0) { x := sub(0, x) }`` @@ -54,7 +54,7 @@ idea is that assembly libraries will be used to enhance the language in such way pragma solidity ^0.4.0; library GetCode { - function at(address _addr) returns (bytes o_code) { + function at(address _addr) public returns (bytes o_code) { assembly { // retrieve the size of the code, this needs assembly let size := extcodesize(_addr) @@ -83,7 +83,7 @@ you really know what you are doing. library VectorSum { // This function is less efficient because the optimizer currently fails to // remove the bounds checks in array access. - function sumSolidity(uint[] _data) returns (uint o_sum) { + function sumSolidity(uint[] _data) public returns (uint o_sum) { for (uint i = 0; i < _data.length; ++i) o_sum += _data[i]; } @@ -91,7 +91,7 @@ you really know what you are doing. // We know that we only access the array in bounds, so we can avoid the check. // 0x20 needs to be added to an array because the first slot contains the // array length. - function sumAsm(uint[] _data) returns (uint o_sum) { + function sumAsm(uint[] _data) public returns (uint o_sum) { for (uint i = 0; i < _data.length; ++i) { assembly { o_sum := add(o_sum, mload(add(add(_data, 0x20), mul(i, 0x20)))) @@ -100,7 +100,7 @@ you really know what you are doing. } // Same as above, but accomplish the entire code within inline assembly. - function sumPureAsm(uint[] _data) returns (uint o_sum) { + function sumPureAsm(uint[] _data) public returns (uint o_sum) { assembly { // Load the length (first 32 bytes) let len := mload(_data) @@ -388,7 +388,7 @@ changes during the call, and thus references to local variables will be wrong. contract C { uint b; - function f(uint x) returns (uint r) { + function f(uint x) public returns (uint r) { assembly { r := mul(x, sload(b_slot)) // ignore the offset, we know it is zero } @@ -447,31 +447,6 @@ will have a wrong impression about the stack height at label ``two``: three: } -This problem can be fixed by manually adjusting the stack height for the -assembler - you can provide a stack height delta that is added -to the stack height just prior to the label. -Note that you will not have to care about these things if you just use -loops and assembly-level functions. - -As an example how this can be done in extreme cases, please see the following. - -.. code:: - - { - let x := 8 - jump(two) - 0 // This code is unreachable but will adjust the stack height correctly - one: - x := 9 // Now x can be accessed properly. - jump(three) - pop // Similar negative correction. - two: - 7 // push something onto the stack - jump(one) - three: - pop // We have to pop the manually pushed value here again. - } - Declaring Assembly-Local Variables ---------------------------------- @@ -487,7 +462,7 @@ be just ``0``, but it can also be a complex functional-style expression. pragma solidity ^0.4.0; contract C { - function f(uint x) returns (uint b) { + function f(uint x) public returns (uint b) { assembly { let v := add(x, 1) mstore(0x80, v) @@ -599,7 +574,7 @@ Simply leave the initialization and post-iteration parts empty. x := add(x, mload(i)) i := add(i, 0x20) } - } + } Functions --------- @@ -738,7 +713,7 @@ We consider the runtime bytecode of the following Solidity program:: pragma solidity ^0.4.0; contract C { - function f(uint x) returns (uint y) { + function f(uint x) public returns (uint y) { y = 1; for (uint i = 0; i < x; i++) y = 2 * y; |