2015-12-07 20:16:25 +00:00
##################################
Expressions and Control Structures
##################################
2016-07-30 07:13:05 +00:00
.. index :: if, else, while, do/while, for, break, continue, return, switch, goto
2015-12-07 20:16:25 +00:00
Control Structures
===================
2016-10-12 08:25:17 +00:00
Most of the control structures from C or JavaScript are available in Solidity
2016-05-24 17:57:36 +00:00
except for `` switch `` and `` goto `` . So
2016-07-30 07:13:05 +00:00
there is: `` if `` , `` else `` , `` while `` , `` do `` , `` for `` , `` break `` , `` continue `` , `` return `` , `` ? : `` , with
2016-08-16 13:06:08 +00:00
the usual semantics known from C or JavaScript.
2015-12-07 20:16:25 +00:00
Parentheses can *not* be omitted for conditionals, but curly brances can be omitted
around single-statement bodies.
Note that there is no type conversion from non-boolean to boolean types as
2016-05-24 17:57:36 +00:00
there is in C and JavaScript, so `` if (1) { ... } `` is *not* valid Solidity.
2015-12-07 20:16:25 +00:00
.. index :: ! function;call, function;internal, function;external
2016-02-19 10:30:00 +00:00
.. _function-calls:
2015-12-07 20:16:25 +00:00
Function Calls
==============
Internal Function Calls
-----------------------
Functions of the current contract can be called directly ("internally"), also recursively, as seen in
this nonsensical example::
2016-05-18 15:11:39 +00:00
contract C {
2016-05-11 19:53:45 +00:00
function g(uint a) returns (uint ret) { return f(); }
function f() returns (uint ret) { return g(7) + f(); }
2015-12-07 20:16:25 +00:00
}
These function calls are translated into simple jumps inside the EVM. This has
the effect that the current memory is not cleared, i.e. passing memory references
to internally-called functions is very efficient. Only functions of the same
contract can be called internally.
External Function Calls
-----------------------
2016-09-05 14:29:08 +00:00
The expressions `` this.g(8); `` and `` c.g(2); `` (where `` g `` is a contract
instance) are also valid function calls, but this time, the function
2015-12-07 20:16:25 +00:00
will be called "externally", via a message call and not directly via jumps.
2016-09-05 14:29:08 +00:00
Please note that function calls on `` this `` cannot be used in the constructor, as the
actual contract has not been created yet.
2015-12-07 20:16:25 +00:00
Functions of other contracts have to be called externally. For an external call,
all function arguments have to be copied to memory.
When calling functions
of other contracts, the amount of Wei sent with the call and the gas can be specified::
2016-05-11 19:48:59 +00:00
2015-12-07 20:16:25 +00:00
contract InfoFeed {
2016-09-05 14:29:08 +00:00
function info() payable returns (uint ret) { return 42; }
2015-12-07 20:16:25 +00:00
}
2016-05-18 15:05:28 +00:00
2015-12-07 20:16:25 +00:00
contract Consumer {
2016-05-11 19:53:45 +00:00
InfoFeed feed;
function setFeed(address addr) { feed = InfoFeed(addr); }
function callFeed() { feed.info.value(10).gas(800)(); }
2015-12-07 20:16:25 +00:00
}
2016-09-05 14:29:08 +00:00
The modifier `` payable `` has to be used for `` info `` , because otherwise,
we would not be able to send Ether to it in the call `` feed.info.value(10).gas(800)() `` .
2016-05-24 17:57:36 +00:00
Note that the expression `` InfoFeed(addr) `` performs an explicit type conversion stating
that "we know that the type of the contract at the given address is `` InfoFeed `` " and
2016-09-05 14:29:08 +00:00
this does not execute a constructor. Explicit type conversions have to be
handled with extreme caution. Never call a function on a contract where you
are not sure about its type.
We could also have used `` function setFeed(InfoFeed _feed) { feed = _feed; } `` directly.
Be careful about the fact that `` feed.info.value(10).gas(800) ``
2015-12-07 20:16:25 +00:00
only (locally) sets the value and amount of gas sent with the function call and only the
parentheses at the end perform the actual call.
2016-08-07 17:46:11 +00:00
Function calls cause exceptions if the called contract does not exist (in the
sense that the account does not contain code) or if the called contract itself
throws an exception or goes out of gas.
2016-06-28 15:29:08 +00:00
.. warning ::
2016-06-29 18:12:07 +00:00
Any interaction with another contract imposes a potential danger, especially
2016-06-28 15:29:08 +00:00
if the source code of the contract is not known in advance. The current
2016-06-29 18:12:07 +00:00
contract hands over control to the called contract and that may potentially
2016-07-07 20:34:05 +00:00
do just about anything. Even if the called contract inherits from a known parent contract,
the inheriting contract is only required to have a correct interface. The
implementation of the contract, however, can be completely arbitrary and thus,
pose a danger. In addition, be prepared in case it calls into other contracts of
2016-06-29 18:12:07 +00:00
your system or even back into the calling contract before the first
2016-06-28 15:29:08 +00:00
call returns. This means
2016-07-07 17:53:57 +00:00
that the called contract can change state variables of the calling contract
via its functions. Write your functions in a way that, for example, calls to
2016-06-29 18:12:07 +00:00
external functions happen after any changes to state variables in your contract
2016-08-16 09:22:24 +00:00
so your contract is not vulnerable to a reentrancy exploit.
2016-06-28 15:29:08 +00:00
2015-12-07 20:16:25 +00:00
Named Calls and Anonymous Function Parameters
---------------------------------------------
2016-08-17 09:04:40 +00:00
Function call arguments can also be given by name, in any order,
if they are enclosed in `` { } `` as can be seen in the following
example. The argument list has to coincide by name with the list of
parameters from the function declaration, but can be in arbitrary order.
2015-12-07 20:16:25 +00:00
::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-05-18 15:11:39 +00:00
contract C {
2016-05-11 19:53:45 +00:00
function f(uint key, uint value) { ... }
2016-05-18 15:05:28 +00:00
2016-05-11 19:53:45 +00:00
function g() {
// named arguments
f({value: 2, key: 3});
}
2016-08-16 13:30:44 +00:00
}
2016-05-18 15:05:28 +00:00
2016-08-17 09:04:40 +00:00
Omitted Function Parameter Names
--------------------------------
The names of unused parameters (especially return parameters) can be omitted.
Those names will still be present on the stack, but they are inaccessible.
2016-05-18 15:05:28 +00:00
2016-08-16 13:30:44 +00:00
::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-08-16 13:30:44 +00:00
contract C {
2016-08-16 07:41:50 +00:00
// omitted name for parameter
2016-05-11 19:53:45 +00:00
function func(uint k, uint) returns(uint) {
return k;
}
2015-12-07 20:16:25 +00:00
}
2016-08-16 07:41:50 +00:00
2015-12-07 20:16:25 +00:00
2016-07-11 13:04:33 +00:00
.. index :: ! new, contracts;creating
.. _creating-contracts:
2016-08-18 17:03:41 +00:00
Creating Contracts via `` new ``
==============================
2016-07-11 13:04:33 +00:00
A contract can create a new contract using the `` new `` keyword. The full
2016-08-18 17:03:41 +00:00
code of the contract being created has to be known and, thus, recursive
2016-07-11 13:04:33 +00:00
creation-dependencies are now possible.
::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-07-11 13:04:33 +00:00
contract D {
uint x;
2016-09-05 14:29:08 +00:00
function D(uint a) payable {
2016-07-11 13:04:33 +00:00
x = a;
}
}
2016-08-18 17:03:41 +00:00
2016-07-11 13:04:33 +00:00
contract C {
D d = new D(4); // will be executed as part of C's constructor
function createD(uint arg) {
D newD = new D(arg);
}
function createAndEndowD(uint arg, uint amount) {
// Send ether along with the creation
D newD = (new D).value(amount)(arg);
}
}
As seen in the example, it is possible to forward Ether to the creation,
but it is not possible to limit the amount of gas. If the creation fails
(due to out-of-stack, not enough balance or other problems), an exception
is thrown.
2015-12-07 20:16:25 +00:00
Order of Evaluation of Expressions
==================================
The evaluation order of expressions is not specified (more formally, the order
in which the children of one node in the expression tree are evaluated is not
specified, but they are of course evaluated before the node itself). It is only
guaranteed that statements are executed in order and short-circuiting for
2016-07-28 19:16:52 +00:00
boolean expressions is done. See :ref: `order` for more information.
2015-12-07 20:16:25 +00:00
.. index :: ! assignment
Assignment
==========
.. index :: ! assignment;destructuring
Destructuring Assignments and Returning Multiple Values
-------------------------------------------------------
Solidity internally allows tuple types, i.e. a list of objects of potentially different types whose size is a constant at compile-time. Those tuples can be used to return multiple values at the same time and also assign them to multiple variables (or LValues in general) at the same time::
contract C {
2016-05-11 19:53:45 +00:00
uint[] data;
function f() returns (uint, bool, uint) {
return (7, true, 2);
}
function g() {
// Declares and assigns the variables. Specifying the type explicitly is not possible.
var (x, b, y) = f();
// Assigns to a pre-existing variable.
(x, y) = (2, 7);
// Common trick to swap values -- does not work for non-value storage types.
(x, y) = (y, x);
// Components can be left out (also for variable declarations).
// If the tuple ends in an empty component,
// the rest of the values are discarded.
(data.length,) = f(); // Sets the length to 7
// The same can be done on the left side.
(,data[3]) = f(); // Sets data[3] to 2
// Components can only be left out at the left-hand-side of assignments, with
// one exception:
(x,) = (1,);
// (1,) is the only way to specify a 1-component tuple, because (1) is
// equivalent to 1.
}
2015-12-07 20:16:25 +00:00
}
Complications for Arrays and Structs
------------------------------------
The semantics of assignment are a bit more complicated for non-value types like arrays and structs.
2016-05-24 17:57:36 +00:00
Assigning *to* a state variable always creates an independent copy. On the other hand, assigning to a local variable creates an independent copy only for elementary types, i.e. static types that fit into 32 bytes. If structs or arrays (including `` bytes `` and `` string `` ) are assigned from a state variable to a local variable, the local variable holds a reference to the original state variable. A second assignment to the local variable does not modify the state but only changes the reference. Assignments to members (or elements) of the local variable *do* change the state.
2015-12-07 20:16:25 +00:00
2016-07-04 14:51:02 +00:00
.. index :: ! scoping, declarations, default value
.. _default-value:
2015-12-07 20:16:25 +00:00
2016-05-11 21:02:28 +00:00
Scoping and Declarations
========================
2016-05-11 19:47:05 +00:00
2016-07-05 15:44:39 +00:00
A variable which is declared will have an initial default value whose byte-representation is all zeros.
2016-06-22 17:25:26 +00:00
The "default values" of variables are the typical "zero-state" of whatever the type is. For example, the default value for a `` bool ``
2016-07-05 15:44:39 +00:00
is `` false `` . The default value for the `` uint `` or `` int `` types is `` 0 `` . For statically-sized arrays and `` bytes1 `` to `` bytes32 `` , each individual
2016-06-22 17:25:26 +00:00
element will be initialized to the default value corresponding to its type. Finally, for dynamically-sized arrays, `` bytes ``
2016-07-08 18:07:58 +00:00
and `` string `` , the default value is an empty array or string.
2016-05-11 19:47:05 +00:00
2016-06-22 17:25:26 +00:00
A variable declared anywhere within a function will be in scope for the *entire function* , regardless of where it is declared.
2016-05-12 13:59:29 +00:00
This happens because Solidity inherits its scoping rules from JavaScript.
2016-05-11 21:07:25 +00:00
This is in contrast to many languages where variables are only scoped where they are declared until the end of the semantic block.
2016-05-24 17:57:36 +00:00
As a result, the following code is illegal and cause the compiler to throw an error, `` Identifier already declared `` ::
2016-05-11 21:07:25 +00:00
2016-09-05 14:29:08 +00:00
pragma solidity ^0.4.0;
2016-05-11 21:07:25 +00:00
contract ScopingErrors {
function scoping() {
uint i = 0;
2016-05-12 13:48:56 +00:00
2016-05-11 21:07:25 +00:00
while (i++ < 1) {
uint same1 = 0;
}
2016-05-12 13:48:56 +00:00
2016-05-11 21:07:25 +00:00
while (i++ < 2) {
uint same1 = 0;// Illegal, second declaration of same1
}
}
2016-05-12 13:48:56 +00:00
2016-05-11 21:07:25 +00:00
function minimalScoping() {
{
uint same2 = 0;
}
2016-05-12 13:48:56 +00:00
2016-05-11 21:07:25 +00:00
{
uint same2 = 0;// Illegal, second declaration of same2
}
}
function forLoopScoping() {
for (uint same3 = 0; same3 < 1; same3++) {
}
for (uint same3 = 0; same3 < 1; same3++) {// Illegal, second declaration of same3
}
}
}
In addition to this, if a variable is declared, it will be initialized at the beginning of the function to its default value.
2016-05-12 17:25:00 +00:00
As a result, the following code is legal, despite being poorly written::
2016-05-11 21:07:25 +00:00
function foo() returns (uint) {
// baz is implicitly initialized as 0
2016-05-12 13:49:49 +00:00
uint bar = 5;
2016-05-11 21:07:25 +00:00
if (true) {
bar += baz;
2016-08-24 19:28:07 +00:00
} else {
2016-05-11 21:07:25 +00:00
uint baz = 10;// never executes
}
return bar;// returns 5
}
2016-07-04 14:51:02 +00:00
.. index :: ! exception, ! throw
2015-12-07 20:16:25 +00:00
Exceptions
==========
2016-05-24 17:57:36 +00:00
There are some cases where exceptions are thrown automatically (see below). You can use the `` throw `` instruction to throw an exception manually. The effect of an exception is that the currently executing call is stopped and reverted (i.e. all changes to the state and balances are undone) and the exception is also "bubbled up" through Solidity function calls (exceptions are `` send `` and the low-level functions `` call `` , `` delegatecall `` and `` callcode `` , those return `` false `` in case of an exception).
2015-12-07 20:16:25 +00:00
Catching exceptions is not yet possible.
2016-05-24 17:57:36 +00:00
In the following example, we show how `` throw `` can be used to easily revert an Ether transfer and also how to check the return value of `` send `` ::
2015-12-07 20:16:25 +00:00
2016-09-05 14:29:08 +00:00
pragma solidity ^0.4.0;
2015-12-07 20:16:25 +00:00
contract Sharer {
2016-09-05 14:29:08 +00:00
function sendHalf(address addr) payable returns (uint balance) {
2016-05-18 15:05:28 +00:00
if (!addr.send(msg.value / 2))
2015-12-07 20:16:25 +00:00
throw; // also reverts the transfer to Sharer
return this.balance;
}
}
2016-10-13 10:12:40 +00:00
Currently, there are situations, where exceptions happen automatically in Solidity:
2015-12-07 20:16:25 +00:00
2016-10-13 10:16:14 +00:00
1. If you access an array at a too large or negative index (i.e. `` x[i] `` where `` i >= x.length `` or `` i < 0 `` ).
2. If you access a fixed-length `` bytesN `` at a too large or negative index.
2016-10-13 10:44:27 +00:00
3. If you call a function via a message call but it does not finish properly (i.e. it runs out of gas, has no matching function, or throws an exception itself), except when a low level operation `` call `` , `` send `` , `` delegatecall `` or `` callcode `` is used. The low level operations never throw exceptions but indicate failures by returning `` false `` .
4. If you create a contract using the `` new `` keyword but the contract creation does not finish properly (see above for the definition of "not finish properly").
5. If you divide or modulo by zero (e.g. `` 5 / 0 `` or `` 23 % 0 `` ).
2016-11-09 09:47:54 +00:00
6. If you convert a value too big or negative into an enum type.
7. If you perform an external function call targeting a contract that contains no code.
8. If your contract receives Ether via a public function without `` payable `` modifier (including the constructor and the fallback function).
9. If your contract receives Ether via a public accessor function.
2015-12-07 20:16:25 +00:00
Internally, Solidity performs an "invalid jump" when an exception is thrown and thus causes the EVM to revert all changes made to the state. The reason for this is that there is no safe way to continue execution, because an expected effect did not occur. Because we want to retain the atomicity of transactions, the safest thing to do is to revert all changes and make the whole transaction (or at least call) without effect.
2016-03-22 14:57:00 +00:00
.. index :: ! assembly, ! asm, ! evmasm
Inline Assembly
===============
For more fine-grained control especially in order to enhance the language by writing libraries,
it is possible to interleave Solidity statements with inline assembly in a language close
to the one of the virtual machine. Due to the fact that the EVM is a stack machine, it is
often hard to address the correct stack slot and provide arguments to opcodes at the correct
point on the stack. Solidity's inline assembly tries to facilitate that and other issues
arising when writing manual assembly by the following features:
2016-05-24 17:57:36 +00:00
* 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) } } ``
* labels: `` let x := 10 repeat: x := sub(x, 1) jumpi(repeat, eq(x, 0)) ``
2016-03-22 14:57:00 +00:00
We now want to describe the inline assembly language in detail.
.. warning ::
2016-09-05 14:29:08 +00:00
Inline assembly is a way to access the Ethereum Virtual Machine
2016-09-07 10:20:35 +00:00
at a low level. This discards several important safety
2016-09-05 14:29:08 +00:00
features of Solidity.
2016-03-22 14:57:00 +00:00
Example
-------
The following example provides library code to access the code of another contract and
2016-05-24 17:57:36 +00:00
load it into a `` bytes `` variable. This is not possible at all with "plain Solidity" and the
2016-03-22 14:57:00 +00:00
idea is that assembly libraries will be used to enhance the language in such ways.
.. code ::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-05-11 19:55:07 +00:00
library GetCode {
function at(address _addr) returns (bytes o_code) {
assembly {
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
// allocate output byte array - this could also be done without assembly
// by using o_code = new bytes(size)
o_code := mload(0x40)
// new "memory end" including padding
2016-05-18 19:53:14 +00:00
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
2016-05-11 19:55:07 +00:00
// store length in memory
mstore(o_code, size)
// actually retrieve the code, this needs assembly
extcodecopy(_addr, add(o_code, 0x20), 0, size)
}
}
}
2016-03-22 14:57:00 +00:00
2016-05-20 14:18:03 +00:00
Inline assembly could also be beneficial in cases where the optimizer fails to produce
2016-03-22 14:57:00 +00:00
efficient code. Please be aware that assembly is much more difficult to write because
the compiler does not perform checks, so you should use it for complex things only if
you really know what you are doing.
.. code ::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-05-11 19:55:07 +00:00
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) {
for (uint i = 0; i < _data.length; ++i)
o_sum += _data[i];
}
2016-05-18 15:05:28 +00:00
2016-05-11 19:55:07 +00:00
// 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) {
for (uint i = 0; i < _data.length; ++i) {
assembly {
2016-05-11 19:54:45 +00:00
o_sum := mload(add(add(_data, 0x20), i))
}
}
2016-05-11 19:55:07 +00:00
}
}
2016-03-22 14:57:00 +00:00
Syntax
------
Inline assembly parses comments, literals and identifiers exactly as Solidity, so you can use the
2016-05-24 17:57:36 +00:00
usual `` // `` and `` /* */ `` comments. Inline assembly is initiated by `` assembly { ... } `` and inside
2016-03-22 14:57:00 +00:00
these curly braces, the following can be used (see the later sections for more details)
2016-08-10 15:35:18 +00:00
- literals, e.g. `` 0x123 `` , `` 42 `` or `` "abc" `` (strings up to 32 characters)
2016-05-24 17:57:36 +00:00
- opcodes (in "instruction style"), e.g. `` mload sload dup1 sstore `` , for a list see below
2016-08-08 17:33:09 +00:00
- opcodes in functional style, e.g. `` add(1, mlod(0)) ``
2016-05-24 17:57:36 +00:00
- labels, e.g. `` name: ``
- variable declarations, e.g. `` let x := 7 `` or `` let x := add(y, 3) ``
- identifiers (externals, labels or assembly-local variables), e.g. `` jump(name) `` , `` 3 x add ``
- assignments (in "instruction style"), e.g. `` 3 =: x ``
- assignments in functional style, e.g. `` x := add(y, 3) ``
- blocks where local variables are scoped inside, e.g. `` { let x := 3 { let y := add(x, 1) } } ``
2016-03-22 14:57:00 +00:00
Opcodes
-------
This document does not want to be a full description of the Ethereum virtual machine, but the
following list can be used as a reference of its opcodes.
If an opcode takes arguments (always from the top of the stack), they are given in parentheses.
2016-08-08 17:30:30 +00:00
Note that the order of arguments can be seen as being reversed compared to the instructional style (explained below).
2016-05-24 17:57:36 +00:00
Opcodes marked with `` - `` do not push an item onto the stack, those marked with `` * `` are
2016-03-22 14:57:00 +00:00
special and all others push exactly one item onte the stack.
2016-05-24 17:57:36 +00:00
In the following, `` mem[a...b) `` signifies the bytes of memory starting at position `` a `` up to
(excluding) position `` b `` and `` storage[p] `` signifies the storage contents at position `` p `` .
2016-03-22 14:57:00 +00:00
2016-05-24 17:57:36 +00:00
The opcodes `` pushi `` and `` jumpdest `` cannot be used directly.
2016-03-22 14:57:00 +00:00
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| stop + `-` | stop execution, identical to return(0,0) |
+-------------------------+------+-----------------------------------------------------------------+
| add(x, y) | | x + y |
+-------------------------+------+-----------------------------------------------------------------+
| sub(x, y) | | x - y |
+-------------------------+------+-----------------------------------------------------------------+
| mul(x, y) | | x * y |
+-------------------------+------+-----------------------------------------------------------------+
| div(x, y) | | x / y |
+-------------------------+------+-----------------------------------------------------------------+
| sdiv(x, y) | | x / y, for signed numbers in two's complement |
+-------------------------+------+-----------------------------------------------------------------+
| mod(x, y) | | x % y |
+-------------------------+------+-----------------------------------------------------------------+
| smod(x, y) | | x % y, for signed numbers in two's complement |
+-------------------------+------+-----------------------------------------------------------------+
| exp(x, y) | | x to the power of y |
+-------------------------+------+-----------------------------------------------------------------+
2016-05-18 19:53:14 +00:00
| not(x) | | ~x, every bit of x is negated |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| lt(x, y) | | 1 if x < y, 0 otherwise |
+-------------------------+------+-----------------------------------------------------------------+
| gt(x, y) | | 1 if x > y, 0 otherwise |
+-------------------------+------+-----------------------------------------------------------------+
| slt(x, y) | | 1 if x < y, 0 otherwise, for signed numbers in two's complement |
+-------------------------+------+-----------------------------------------------------------------+
| sgt(x, y) | | 1 if x > y, 0 otherwise, for signed numbers in two's complement |
+-------------------------+------+-----------------------------------------------------------------+
| eq(x, y) | | 1 if x == y, 0 otherwise |
+-------------------------+------+-----------------------------------------------------------------+
2016-05-18 19:53:14 +00:00
| iszero(x) | | 1 if x == 0, 0 otherwise |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| and(x, y) | | bitwise and of x and y |
+-------------------------+------+-----------------------------------------------------------------+
| or(x, y) | | bitwise or of x and y |
+-------------------------+------+-----------------------------------------------------------------+
| xor(x, y) | | bitwise xor of x and y |
+-------------------------+------+-----------------------------------------------------------------+
| byte(n, x) | | nth byte of x, where the most significant byte is the 0th byte |
+-------------------------+------+-----------------------------------------------------------------+
| addmod(x, y, m) | | (x + y) % m with arbitrary precision arithmetics |
+-------------------------+------+-----------------------------------------------------------------+
| mulmod(x, y, m) | | (x * y) % m with arbitrary precision arithmetics |
+-------------------------+------+-----------------------------------------------------------------+
| signextend(i, x) | | sign extend from (i*8+7)th bit counting from least significant |
+-------------------------+------+-----------------------------------------------------------------+
| sha3(p, n) | | keccak(mem[p...(p+n))) |
+-------------------------+------+-----------------------------------------------------------------+
| jump(label) | `-` | jump to label / code position |
+-------------------------+------+-----------------------------------------------------------------+
| jumpi(label, cond) | `-` | jump to label if cond is nonzero |
+-------------------------+------+-----------------------------------------------------------------+
| pc | | current position in code |
+-------------------------+------+-----------------------------------------------------------------+
2016-10-13 17:30:43 +00:00
| pop(x) | `-` | remove the element pushed by x |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| dup1 ... dup16 | | copy ith stack slot to the top (counting from top) |
+-------------------------+------+-----------------------------------------------------------------+
2016-07-11 21:04:51 +00:00
| swap1 ... swap16 | `*` | swap topmost and ith stack slot below it |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| mload(p) | | mem[p..(p+32)) |
+-------------------------+------+-----------------------------------------------------------------+
| mstore(p, v) | `-` | mem[p..(p+32)) := v |
+-------------------------+------+-----------------------------------------------------------------+
| mstore8(p, v) | `-` | mem[p] := v & 0xff - only modifies a single byte |
+-------------------------+------+-----------------------------------------------------------------+
| sload(p) | | storage[p] |
+-------------------------+------+-----------------------------------------------------------------+
| sstore(p, v) | `-` | storage[p] := v |
+-------------------------+------+-----------------------------------------------------------------+
| msize | | size of memory, i.e. largest accessed memory index |
+-------------------------+------+-----------------------------------------------------------------+
| gas | | gas still available to execution |
+-------------------------+------+-----------------------------------------------------------------+
| address | | address of the current contract / execution context |
+-------------------------+------+-----------------------------------------------------------------+
| balance(a) | | wei balance at address a |
+-------------------------+------+-----------------------------------------------------------------+
| caller | | call sender (excluding delegatecall) |
+-------------------------+------+-----------------------------------------------------------------+
| callvalue | | wei sent together with the current call |
+-------------------------+------+-----------------------------------------------------------------+
2016-08-17 20:42:43 +00:00
| calldataload(p) | | calldata starting from position p (32 bytes) |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
2016-08-17 20:42:43 +00:00
| calldatasize | | size of calldata in bytes |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| calldatacopy(t, f, s) | `-` | copy s bytes from calldata at position f to mem at position t |
+-------------------------+------+-----------------------------------------------------------------+
| codesize | | size of the code of the current contract / execution context |
+-------------------------+------+-----------------------------------------------------------------+
| codecopy(t, f, s) | `-` | copy s bytes from code at position f to mem at position t |
+-------------------------+------+-----------------------------------------------------------------+
| extcodesize(a) | | size of the code at address a |
+-------------------------+------+-----------------------------------------------------------------+
| extcodecopy(a, t, f, s) | `-` | like codecopy(t, f, s) but take code at address a |
+-------------------------+------+-----------------------------------------------------------------+
| create(v, p, s) | | create new contract with code mem[p..(p+s)) and send v wei |
| | | and return the new address |
+-------------------------+------+-----------------------------------------------------------------+
2016-08-16 17:48:54 +00:00
| call(g, a, v, in, | | call contract at address a with input mem[in..(in+insize)) |
2016-05-11 20:07:12 +00:00
| insize, out, outsize) | | providing g gas and v wei and output area |
2016-09-06 10:19:46 +00:00
| | | mem[out..(out+outsize)) returning 0 on error (eg. out of gas) |
| | | and 1 on success |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
2016-08-16 17:48:54 +00:00
| callcode(g, a, v, in, | | identical to `call` but only use the code from a and stay |
2016-05-11 20:07:12 +00:00
| insize, out, outsize) | | in the context of the current contract otherwise |
+-------------------------+------+-----------------------------------------------------------------+
2016-08-16 17:48:54 +00:00
| delegatecall(g, a, in, | | identical to `callcode` but also keep `` caller `` |
2016-05-24 17:57:36 +00:00
| insize, out, outsize) | | and `` callvalue `` |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
2016-10-13 17:30:43 +00:00
| return(p, s) | `-` | end execution, return data mem[p..(p+s)) |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
2016-10-13 17:30:43 +00:00
| selfdestruct(a) | `-` | end execution, destroy current contract and send funds to a |
2016-05-11 20:07:12 +00:00
+-------------------------+------+-----------------------------------------------------------------+
| log0(p, s) | `-` | log without topics and data mem[p..(p+s)) |
+-------------------------+------+-----------------------------------------------------------------+
| log1(p, s, t1) | `-` | log with topic t1 and data mem[p..(p+s)) |
+-------------------------+------+-----------------------------------------------------------------+
| log2(p, s, t1, t2) | `-` | log with topics t1, t2 and data mem[p..(p+s)) |
+-------------------------+------+-----------------------------------------------------------------+
| log3(p, s, t1, t2, t3) | `-` | log with topics t1, t2, t3 and data mem[p..(p+s)) |
+-------------------------+------+-----------------------------------------------------------------+
| log4(p, s, t1, t2, t3, | `-` | log with topics t1, t2, t3, t4 and data mem[p..(p+s)) |
| t4) | | |
+-------------------------+------+-----------------------------------------------------------------+
| origin | | transaction sender |
+-------------------------+------+-----------------------------------------------------------------+
| gasprice | | gas price of the transaction |
+-------------------------+------+-----------------------------------------------------------------+
| blockhash(b) | | hash of block nr b - only for last 256 blocks excluding current |
+-------------------------+------+-----------------------------------------------------------------+
| coinbase | | current mining beneficiary |
+-------------------------+------+-----------------------------------------------------------------+
| timestamp | | timestamp of the current block in seconds since the epoch |
+-------------------------+------+-----------------------------------------------------------------+
| number | | current block number |
+-------------------------+------+-----------------------------------------------------------------+
| difficulty | | difficulty of the current block |
+-------------------------+------+-----------------------------------------------------------------+
| gaslimit | | block gas limit of the current block |
+-------------------------+------+-----------------------------------------------------------------+
2016-03-22 14:57:00 +00:00
Literals
--------
You can use integer constants by typing them in decimal or hexadecimal notation and an
2016-05-24 17:57:36 +00:00
appropriate `` PUSHi `` instruction will automatically be generated. The following creates code
2016-03-22 14:57:00 +00:00
to add 2 and 3 resulting in 5 and then computes the bitwise and with the string "abc".
Strings are stored left-aligned and cannot be longer than 32 bytes.
.. code ::
2016-05-11 19:55:07 +00:00
assembly { 2 3 add "abc" and }
2016-03-22 14:57:00 +00:00
2016-03-30 16:32:39 +00:00
Functional Style
2016-03-22 14:57:00 +00:00
-----------------
You can type opcode after opcode in the same way they will end up in bytecode. For example
2016-05-24 17:57:36 +00:00
adding `` 3 `` to the contents in memory at position `` 0x80 `` would be
2016-03-22 14:57:00 +00:00
.. code ::
2016-05-11 19:55:07 +00:00
3 0x80 mload add 0x80 mstore
2016-03-22 14:57:00 +00:00
As it is often hard to see what the actual arguments for certain opcodes are,
Solidity inline assembly also provides a "functional style" notation where the same code
would be written as follows
.. code ::
2016-05-11 19:55:07 +00:00
mstore(0x80, add(mload(0x80), 3))
2016-03-22 14:57:00 +00:00
Functional style and instructional style can be mixed, but any opcode inside a
functional style expression has to return exactly one stack slot (most of the opcodes do).
Note that the order of arguments is reversed in functional-style as opposed to the instruction-style
way. If you use functional-style, the first argument will end up on the stack top.
2016-05-08 14:24:47 +00:00
Access to External Variables and Functions
------------------------------------------
2016-03-22 14:57:00 +00:00
Solidity variables and other identifiers can be accessed by simply using their name.
For storage and memory variables, this will push the address and not the value onto the
stack. Also note that non-struct and non-array storage variable addresses occupy two slots
on the stack: One for the address and one for the byte offset inside the storage slot.
In assignments (see below), we can even use local Solidity variables to assign to.
2016-05-17 15:05:03 +00:00
Functions external to inline assembly can also be accessed: The assembly will
2016-05-08 14:24:47 +00:00
push their entry label (with virtual function resolution applied). The calling semantics
in solidity are:
- the caller pushes return label, arg1, arg2, ..., argn
- the call returns with ret1, ret2, ..., retn
This feature is still a bit cumbersome to use, because the stack offset essentially
changes during the call, and thus references to local variables will be wrong.
It is planned that the stack height changes can be specified in inline assembly.
2016-03-22 14:57:00 +00:00
.. code ::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-05-18 15:11:39 +00:00
contract C {
2016-05-11 19:55:07 +00:00
uint b;
function f(uint x) returns (uint r) {
assembly {
b pop // remove the offset, we know it is zero
sload
x
mul
=: r // assign to return variable r
}
}
}
2016-03-22 14:57:00 +00:00
Labels
------
2016-05-24 17:57:36 +00:00
Another problem in EVM assembly is that `` jump `` and `` jumpi `` use absolute addresses
2016-03-22 14:57:00 +00:00
which can change easily. Solidity inline assembly provides labels to make the use of
jumps easier. The following code computes an element in the Fibonacci series.
.. code ::
2016-05-11 19:55:07 +00:00
{
let n := calldataload(4)
let a := 1
let b := a
loop:
jumpi(loopend, eq(n, 0))
a add swap1
n := sub(n, 1)
jump(loop)
loopend:
mstore(0, a)
return(0, 0x20)
}
2016-03-22 14:57:00 +00:00
Please note that automatically accessing stack variables can only work if the
assembler knows the current stack height. This fails to work if the jump source
and target have different stack heights. It is still fine to use such jumps,
you should just not access any stack variables (even assembly variables) in that case.
Furthermore, the stack height analyser goes through the code opcode by opcode
(and not according to control flow), so in the following case, the assembler
2016-05-24 17:57:36 +00:00
will have a wrong impression about the stack height at label `` two `` :
2016-03-22 14:57:00 +00:00
.. code ::
2016-05-11 19:55:07 +00:00
{
jump(two)
one:
// Here the stack height is 1 (because we pushed 7),
// but the assembler thinks it is 0 because it reads
// from top to bottom.
// Accessing stack variables here will lead to errors.
jump(three)
two:
7 // push something onto the stack
jump(one)
three:
}
2016-03-22 14:57:00 +00:00
2016-11-09 01:18:10 +00:00
.. note ::
2016-11-15 10:12:03 +00:00
`` invalidJumpLabel `` is a pre-defined label. Jumping to this location will always
2016-11-09 01:18:10 +00:00
result in an invalid jump, effectively aborting execution of the code.
2016-03-22 14:57:00 +00:00
Declaring Assembly-Local Variables
----------------------------------
2016-05-24 17:57:36 +00:00
You can use the `` let `` keyword to declare variables that are only visible in
inline assembly and actually only in the current `` {...} `` -block. What happens
is that the `` let `` instruction will create a new stack slot that is reserved
2016-03-22 14:57:00 +00:00
for the variable and automatically removed again when the end of the block
is reached. You need to provide an initial value for the variable which can
2016-05-24 17:57:36 +00:00
be just `` 0 `` , but it can also be a complex functional-style expression.
2016-03-22 14:57:00 +00:00
.. code ::
2016-09-05 11:54:54 +00:00
pragma solidity ^0.4.0;
2016-05-18 15:11:39 +00:00
contract C {
2016-05-11 19:55:07 +00:00
function f(uint x) returns (uint b) {
assembly {
let v := add(x, 1)
mstore(0x80, v)
{
let y := add(sload(v), 1)
b := y
} // y is "deallocated" here
b := add(b, v)
} // v is "deallocated" here
}
}
2016-03-22 14:57:00 +00:00
Assignments
-----------
Assignments are possible to assembly-local variables and to function-local
variables. Take care that when you assign to variables that point to
memory or storage, you will only change the pointer and not the data.
There are two kinds of assignments: Functional-style and instruction-style.
2016-07-11 21:04:51 +00:00
For functional-style assignments (`` variable := value `` ), you need to provide a value in a
2016-03-22 14:57:00 +00:00
functional-style expression that results in exactly one stack value
2016-05-24 17:57:36 +00:00
and for instruction-style (`` =: variable `` ), the value is just taken from the stack top.
2016-03-22 14:57:00 +00:00
For both ways, the colon points to the name of the variable.
.. code ::
2016-05-11 19:55:07 +00:00
assembly {
let v := 0 // functional-style assignment as part of variable declaration
let g := add(v, 2)
sload(10)
=: v // instruction style assignment, puts the result of sload(10) into v
}
2016-03-22 14:57:00 +00:00
Things to Avoid
---------------
Inline assembly might have a quite high-level look, but it actually is extremely
low-level. The only thing the assembler does for you is re-arranging
functional-style opcodes, managing jump labels, counting stack height for
variable access and removing stack slots for assembly-local variables when the end
of their block is reached. Especially for those two last cases, it is important
to know that the assembler only counts stack height from top to bottom, not
necessarily following control flow. Furthermore, operations like swap will only
swap the contents of the stack but not the location of variables.
Conventions in Solidity
-----------------------
In contrast to EVM assembly, Solidity knows types which are narrower than 256 bits,
2016-05-24 17:57:36 +00:00
e.g. `` uint24 `` . In order to make them more efficient, most arithmetic operations just
2016-10-12 08:25:17 +00:00
treat them as 256-bit numbers and the higher-order bits are only cleaned at the
2016-03-22 14:57:00 +00:00
point where it is necessary, i.e. just shortly before they are written to memory
or before comparisons are performed. This means that if you access such a variable
from within inline assembly, you might have to manually clean the higher order bits
first.
Solidity manages memory in a very simple way: There is a "free memory pointer"
2016-05-24 17:57:36 +00:00
at position `` 0x40 `` in memory. If you want to allocate memory, just use the memory
2016-03-22 14:57:00 +00:00
from that point on and update the pointer accordingly.
Elements in memory arrays in Solidity always occupy multiples of 32 bytes (yes, this is
2016-05-24 17:57:36 +00:00
even true for `` byte[] `` , but not for `` bytes `` and `` string `` ). Multi-dimensional memory
2016-03-22 14:57:00 +00:00
arrays are pointers to memory arrays. The length of a dynamic array is stored at the
first slot of the array and then only the array elements follow.
.. warning ::
2016-05-11 19:55:07 +00:00
Statically-sized memory arrays do not have a length field, but it will be added soon
to allow better convertibility between statically- and dynamically-sized arrays, so
please do not rely on that.