@@ -9,11 +9,6 @@ This assembly language can also be used as "inline assembly" inside Solidity
9
9
source code. We start with describing how to use inline assembly and how it
10
10
differs from standalone assembly and then specify assembly itself.
11
11
12
- .. note ::
13
- TODO: Write about how scoping rules of inline assembly are a bit different
14
- and the complications that arise when for example using internal functions
15
- of libraries. Furthermore, write about the symbols defined by the compiler.
16
-
17
12
.. _inline-assembly :
18
13
19
14
Inline Assembly
@@ -31,6 +26,7 @@ arising when writing manual assembly by the following features:
31
26
* access to external variables: ``function f(uint x) { assembly { x := sub(x, 1) } } ``
32
27
* labels: ``let x := 10 repeat: x := sub(x, 1) jumpi(repeat, eq(x, 0)) ``
33
28
* loops: ``for { let i := 0 } lt(i, x) { i := add(i, 1) } { y := mul(2, y) } ``
29
+ * if statements: ``if slt(x, 0) { x := sub(0, x) } ``
34
30
* switch statements: ``switch x case 0 { y := mul(x, 2) } default { y := 0 } ``
35
31
* function calls: ``function f(x) -> y { switch x case 0 { y := 1 } default { y := mul(x, f(sub(x, 1))) } } ``
36
32
@@ -41,6 +37,11 @@ We now want to describe the inline assembly language in detail.
41
37
at a low level. This discards several important safety
42
38
features of Solidity.
43
39
40
+ .. note ::
41
+ TODO: Write about how scoping rules of inline assembly are a bit different
42
+ and the complications that arise when for example using internal functions
43
+ of libraries. Furthermore, write about the symbols defined by the compiler.
44
+
44
45
Example
45
46
-------
46
47
@@ -400,7 +401,7 @@ Labels
400
401
Another problem in EVM assembly is that ``jump `` and ``jumpi `` use absolute addresses
401
402
which can change easily. Solidity inline assembly provides labels to make the use of
402
403
jumps easier. Note that labels are a low-level feature and it is possible to write
403
- efficient assembly without labels, just using assembly functions, loops and switch instructions
404
+ efficient assembly without labels, just using assembly functions, loops, if and switch instructions
404
405
(see below). The following code computes an element in the Fibonacci series.
405
406
406
407
.. code ::
@@ -523,6 +524,21 @@ is performed by replacing the variable's value on the stack by the new value.
523
524
=: v // instruction style assignment, puts the result of sload(10) into v
524
525
}
525
526
527
+ If
528
+ --
529
+
530
+ The if statement can be used for conditionally executing code.
531
+ There is no "else" part, consider using "switch" (see below) if
532
+ you need multiple alternatives.
533
+
534
+ .. code ::
535
+
536
+ {
537
+ if eq(value, 0) { revert(0, 0) }
538
+ }
539
+
540
+ The curly braces for the body are required.
541
+
526
542
Switch
527
543
------
528
544
@@ -622,7 +638,7 @@ Things to Avoid
622
638
---------------
623
639
624
640
Inline assembly might have a quite high-level look, but it actually is extremely
625
- low-level. Function calls, loops and switches are converted by simple
641
+ low-level. Function calls, loops, ifs and switches are converted by simple
626
642
rewriting rules and after that, the only thing the assembler does for you is re-arranging
627
643
functional-style opcodes, managing jump labels, counting stack height for
628
644
variable access and removing stack slots for assembly-local variables when the end
@@ -669,7 +685,7 @@ for the Solidity compiler. In this form, it tries to achieve several goals:
669
685
3. Control flow should be easy to detect to help in formal verification and optimization.
670
686
671
687
In order to achieve the first and last goal, assembly provides high-level constructs
672
- like ``for `` loops, ``switch `` statements and function calls. It should be possible
688
+ like ``for `` loops, ``if `` and `` switch `` statements and function calls. It should be possible
673
689
to write assembly programs that do not make use of explicit ``SWAP ``, ``DUP ``,
674
690
``JUMP `` and ``JUMPI `` statements, because the first two obfuscate the data flow
675
691
and the last two obfuscate control flow. Furthermore, functional statements of
@@ -875,6 +891,7 @@ Grammar::
875
891
FunctionalAssemblyAssignment |
876
892
AssemblyAssignment |
877
893
LabelDefinition |
894
+ AssemblyIf |
878
895
AssemblySwitch |
879
896
AssemblyFunctionDefinition |
880
897
AssemblyFor |
@@ -891,6 +908,7 @@ Grammar::
891
908
IdentifierList = Identifier ( ',' Identifier)*
892
909
AssemblyAssignment = '=:' Identifier
893
910
LabelDefinition = Identifier ':'
911
+ AssemblyIf = 'if' FunctionalAssemblyExpression AssemblyBlock
894
912
AssemblySwitch = 'switch' FunctionalAssemblyExpression AssemblyCase*
895
913
( 'default' AssemblyBlock )?
896
914
AssemblyCase = 'case' FunctionalAssemblyExpression AssemblyBlock
0 commit comments