Feature Guide for Yul

September 6, 2021 · View on GitHub

Feature Guide for Yul

Introduction

In this guide, we will list all Yul language features and briefly summarize the current status and limitations about SOLL implementation.

Grammar

Our parser already covered most parts of valid grammar base on 0.6.6. Your can use soll -lang=Yul -action=ASTDump ...*.yul verify parser functional behavior.

If you want to execute ewasm compiled from Yul, refer to the DevGuide.

Supported by SOLL

Grammar

  • Recognize Yul object structure.
  • Statement
    • Block
    • FunctionDefinition
    • VariableDeclaration
    • Assignment
    • Switch
    • If
    • ForLoop
    • BreakContinue
    • FunctionCall
    • Leave
  • Basic arithmetical, compare and storage/memory access built-in functions.
  • Partial state, object and misc built-in functions.

Additional Functions

InstructionExplanation
*datasize(x) [See below]-
*dataoffset(x) [See below]-
datacopy(t, f, l)-

datasize and dataoffset in our implementation have a constraint. They both couldn't be used apply on object itself.

EVM Dialect

Opcodes marked with - do not return a result and all others return exactly one value.

Opcodes marked with F, H, B, C or I are present since Frontier, Homestead, Byzantium, Constantinople or Istanbul, respectively.

InstructionExplanation
stop()-Fstop execution, identical to return(0, 0)
add(x, y)Fx + y
sub(x, y)Fx - y
mul(x, y)Fx * y
div(x, y)Fx / y or 0 if y == 0
sdiv(x, y)Fx / y, for signed numbers in two’s complement, 0 if y == 0
mod(x, y)Fx % y, 0 if y == 0
exp(x, y)Fx to the power of y
not(x)Fbitwise “not” of x (every bit of x is negated)
lt(x, y)F1 if x < y, 0 otherwise
gt(x, y)F1 if x > y, 0 otherwise
slt(x, y)F1 if x < y, 0 otherwise, for signed numbers in two’s complement
sgt(x, y)F1 if x > y, 0 otherwise, for signed numbers in two’s complement
eq(x, y)F1 if x == y, 0 otherwise
iszero(x)F1 if x == 0, 0 otherwise
and(x, y)Fbitwise “and” of x and y
or(x, y)Fbitwise “or” of x and y
xor(x, y)Fbitwise “xor” of x and y
shl(x, y)Clogical shift left y by x bits
shr(x, y)Clogical shift right y by x bits
sar(x, y)Csigned arithmetic shift right y by x bits
addmod(x, y, m)F(x + y) % m with arbitrary precision arithmetic, 0 if m == 0
mulmod(x, y, m)F(x * y) % m with arbitrary precision arithmetic, 0 if m == 0
signextend(i, x)Fsign extend from (i*8+7)th bit counting from least significant
keccak256(p, n)Fkeccak(mem[p…(p+n)))
mload(p)Fmem[p…(p+32))
mstore(p, v)-Fmem[p…(p+32)) := v
mstore8(p, v)-Fmem[p] := v & 0xff (only modifies a single byte)
sload(p)Fstorage[p]
sstore(p, v)-Fstorage[p] := v
msize()Fsize of memory, i.e. largest accessed memory index
gas()Fgas still available to execution
caller()Fcall sender (excluding delegatecall)
callvalue()Fwei sent together with the current call
calldataload(p)Fcall data starting from position p (32 bytes)
calldatasize()Fsize of call data in bytes
codecopy(t, f, s)-Fcopy s bytes from code at position f to mem at position t
return(p, s)-Fend execution, return data mem[p…(p+s))
revert(p, s)-Bend execution, revert state changes, return data mem[p…(p+s))
log0(p, s)-Flog without topics and data mem[p…(p+s))
log1(p, s, t1)-Flog with topic t1 and data mem[p…(p+s))
log2(p, s, t1, t2)-Flog with topics t1, t2 and data mem[p…(p+s))
log3(p, s, t1, t2, t3)-Flog with topics t1, t2, t3 and data mem[p…(p+s))
log4(p, s, t1, t2, t3, t4)-Flog with topics t1, t2, t3, t4 and data mem[p…(p+s))
origin()Ftransaction sender
gasprice()Fgas price of the transaction
coinbase()Fcurrent mining beneficiary
timestamp()Ftimestamp of the current block in seconds since the epoch
number()Fcurrent block number
difficulty()Fdifficulty of the current block
gaslimit()Fblock gas limit of the current block
address()Faddress of the current contract / execution context
balance(a)Fwei balance at address a
selfbalance()Iequivalent to balance(address()), but cheaper
calldatacopy(t, f, s)-Fcopy s bytes from calldata at position f to mem at position t
codesize()Fsize of the code of the current contract / execution context
extcodesize(a)Fsize of the code at address a
extcodecopy(a, t, f, s)-Flike codecopy(t, f, s) but take code at address a
returndatasize()Bsize of the last returndata
returndatacopy(t, f, s)-Bcopy s bytes from returndata at position f to mem at position t
smod(x, y)Fx % y, for signed numbers in two’s complement, 0 if y == 0
blockhash(b)Fhash of block nr b - only for last 256 blocks excluding current
byte(n, x)Fnth byte of x, where the most significant byte is the 0th byte
pop(x)-Fdiscard value x
extcodehash(a)Ccode hash of address a
create(v, p, n)Fcreate new contract with code mem[p…(p+n)) and send v wei and return the new address
call(g, a, v, in, insize, out, outsize)Fcall contract at address a with input mem[in…(in+insize)) providing g gas and v wei and output area mem[out…(out+outsize)) returning 0 on error (eg. out of gas) and 1 on success See more
callcode(g, a, v, in, insize, out, outsize)Fidentical to call but only use the code from a and stay in the context of the current contract otherwise See more
delegatecall(g, a, in, insize, out, outsize)Hidentical to callcode but also keep caller and callvalue See more
staticcall(g, a, in, insize, out, outsize)Bidentical to call(g, a, 0, in, insize, out, outsize) but do not allow state modifications See more
selfdestruct(a)-Fend execution, destroy current contract and send funds to a
invalid()-Fend execution with invalid instruction
create2(v, p, n, s)Ccreate new contract with code mem[p…(p+n)) at address keccak256(0xff . this . s . keccak256(mem[p…(p+n))) and send v wei and return the new address, where 0xff is a 1 byte value, this is the current contract’s address as a 20 byte value and s is a big-endian 256-bit value
chainid()IID of the executing chain (EIP 1344)

Future works

Below will describe some under implement language features.

Features

These features are not supported yet.

  • calculate dataoffset to the object itself
  • calculate datasize to the object itself
  • directly convert string to bool or int
  • Identifier : TypeName (e.g. var:u256)

Grammar

featureitemexample
StatementSwitch with string literalcase ""

Additional Functions

Some newly announced functions after v0.7.0 breaking update.

InstructionExplanation
setimmutable-
loadimmutable-
linkersymbol("fq_library_name")-