From 6dbc34e16ee8bda0e156ccb20a3fb8cb6ff52c92 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 21 Nov 2017 13:36:41 +0100 Subject: If statement for Iulia / inline assembly. --- docs/assembly.rst | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/assembly.rst b/docs/assembly.rst index 00bfb388..c233985b 100644 --- a/docs/assembly.rst +++ b/docs/assembly.rst @@ -26,6 +26,7 @@ arising when writing manual assembly by the following features: * 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))`` * 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) }`` * switch statements: ``switch x case 0 { y := mul(x, 2) } default { y := 0 }`` * function calls: ``function f(x) -> y { switch x case 0 { y := 1 } default { y := mul(x, f(sub(x, 1))) } }`` @@ -400,7 +401,7 @@ Labels Another problem in EVM assembly is that ``jump`` and ``jumpi`` use absolute addresses which can change easily. Solidity inline assembly provides labels to make the use of jumps easier. Note that labels are a low-level feature and it is possible to write -efficient assembly without labels, just using assembly functions, loops and switch instructions +efficient assembly without labels, just using assembly functions, loops, if and switch instructions (see below). The following code computes an element in the Fibonacci series. .. code:: @@ -523,6 +524,21 @@ is performed by replacing the variable's value on the stack by the new value. =: v // instruction style assignment, puts the result of sload(10) into v } +If +-- + +The if statement can be used for conditionally executing code. +There is no "else" part, consider using "switch" (see below) if +you need multiple alternatives. + +.. code:: + + { + if eq(value, 0) { revert(0, 0) } + } + +The curly braces for the body are required. + Switch ------ @@ -622,7 +638,7 @@ Things to Avoid --------------- Inline assembly might have a quite high-level look, but it actually is extremely -low-level. Function calls, loops and switches are converted by simple +low-level. Function calls, loops, ifs and switches are converted by simple rewriting rules and after that, 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 @@ -669,7 +685,7 @@ for the Solidity compiler. In this form, it tries to achieve several goals: 3. Control flow should be easy to detect to help in formal verification and optimization. In order to achieve the first and last goal, assembly provides high-level constructs -like ``for`` loops, ``switch`` statements and function calls. It should be possible +like ``for`` loops, ``if`` and ``switch`` statements and function calls. It should be possible to write assembly programs that do not make use of explicit ``SWAP``, ``DUP``, ``JUMP`` and ``JUMPI`` statements, because the first two obfuscate the data flow and the last two obfuscate control flow. Furthermore, functional statements of @@ -875,6 +891,7 @@ Grammar:: FunctionalAssemblyAssignment | AssemblyAssignment | LabelDefinition | + AssemblyIf | AssemblySwitch | AssemblyFunctionDefinition | AssemblyFor | @@ -891,6 +908,7 @@ Grammar:: IdentifierList = Identifier ( ',' Identifier)* AssemblyAssignment = '=:' Identifier LabelDefinition = Identifier ':' + AssemblyIf = 'if' FunctionalAssemblyExpression AssemblyBlock AssemblySwitch = 'switch' FunctionalAssemblyExpression AssemblyCase* ( 'default' AssemblyBlock )? AssemblyCase = 'case' FunctionalAssemblyExpression AssemblyBlock -- cgit v1.2.3 From e15918d8b6fd106d147d00d0c3405df88ab39b64 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 22 Nov 2017 14:17:31 +0100 Subject: Add if statement to Julia specification. --- docs/julia.rst | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/julia.rst b/docs/julia.rst index cf798363..014cd00b 100644 --- a/docs/julia.rst +++ b/docs/julia.rst @@ -15,7 +15,7 @@ future versions of the Solidity compiler will even use JULIA as intermediate language. It should also be easy to build high-level optimizer stages for JULIA. The core components of JULIA are functions, blocks, variables, literals, -for-loops, switch-statements, expressions and assignments to variables. +for-loops, if-statements, switch-statements, expressions and assignments to variables. JULIA is typed, both variables and literals must specify the type with postfix notation. The supported types are ``bool``, ``u8``, ``s8``, ``u32``, ``s32``, @@ -88,6 +88,8 @@ Grammar:: IdentifierList ':=' Expression Expression = FunctionCall | Identifier | Literal + If = + 'if' Expression Block Switch = 'switch' Expression Case* ( 'default' Block )? Case = @@ -248,8 +250,14 @@ We will use a destructuring notation for the AST nodes. G, L, break E(G, L, continue: BreakContinue) = G, L, continue + E(G, L, : If) = + let G0, L0, v = E(G, L, condition) + if v is true or non-zero: + E(G0, L0, body) + else: + G0, L0, regular E(G, L, : Switch) = - E(G, L, switch condition case l1:t1 st1 ... case ln:tn stn default {}) = + E(G, L, switch condition case l1:t1 st1 ... case ln:tn stn default {}) E(G, L, : Switch) = let G0, L0, v = E(G, L, condition) // i = 1 .. n -- cgit v1.2.3 From 1d91b65b726d4757b866124d75834f28a9bc9eb9 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 23 Nov 2017 18:52:04 +0100 Subject: Force condition to be bool in iulia mode. --- docs/julia.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/julia.rst b/docs/julia.rst index 014cd00b..309e6b36 100644 --- a/docs/julia.rst +++ b/docs/julia.rst @@ -252,7 +252,7 @@ We will use a destructuring notation for the AST nodes. G, L, continue E(G, L, : If) = let G0, L0, v = E(G, L, condition) - if v is true or non-zero: + if v is true: E(G0, L0, body) else: G0, L0, regular -- cgit v1.2.3