> For the complete documentation index, see [llms.txt](https://validium.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://validium.gitbook.io/docs/components/compiler/specification/instructions/extensions/validium-network-extension-simulation-verbatim.md).

# Validium Network Extension Simulation (verbatim)

***

NOTES:

* changed META - it can be used for MSIZE simulation
* setting ergs per pubdata is done by separate opcode now (not part of `near_call`)
* incrementing TX counter is done by separate opcode now (not part of `far_call`)

Our VM has some opcodes that are not expressible in Solidity, but we can simulate them on compiler level by abusing “CALL” instruction. We use 2nd parameter of “CALL” (address) as a marker, and remaining 6 parameters as input parameters (we use “address”-like field since it’s kind of shorter type, if assembly block cares about types in Solidity). Unfortunately “CALL” returns only 1 stack parameter, but it looks sufficient for our purposes.

Please note, that some of the methods don’t modify state, so STATICCALL instead of CALL should be used for them. The type of the needed method is indicated in the rightmost column.

Call types are not validated and do not affect the simulation behavior, unless specified otherwise, like in `raw_far_call` and `system_call` simulations, where the call type is passed through.

For some simulations below we assume that there exist a hidden global pseudo-variable called `ACTIVE_PTR` for manipulations, since one can not easily load pointer value into Solidity’s variable.

| Simulated opcode                                                                | CALL param 0 (gas)     | CALL param 1 (address) | CALL param 2 (value)                                      | CALL param 3 (input offset) | CALL param 4 (input length)                | CALL param 5 (output offset) | CALL param 6 (output length) | Return value                                                                                                                                        | call type                                   | LLVM implementation                                                                                                        | Motivation                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------------------------------------------------------- | ---------------------- | ---------------------- | --------------------------------------------------------- | --------------------------- | ------------------------------------------ | ---------------------------- | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| to\_l1(is\_first, in1, in2)                                                     | if\_first (bool)       | 0xFFFF                 | in1 (u256)                                                | in2 (u256)                  | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | \_                                                                                                                                                  | call                                        | @llvm.syncvm.tol1(i256 %in1, i256 %in2, i256 %is\_first)                                                                   | Send messages to L1                                                                                                                                                                                                                                                                                                                                     |
| code\_source                                                                    | 0                      | 0xFFFE                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | address                                                                                                                                             | staticcall                                  | @llvm.syncvm.context(i256 %param) ; param == 2 (see SyncVM.h)                                                              | Largely to be able to catch “delegatecalls” in system contracts (by comparing this == code\_source)                                                                                                                                                                                                                                                     |
| precompile(in1, ergs\_to\_burn, out0)                                           | in1 (u256)             | 0xFFFD                 | -                                                         | ergs\_to\_burn (u32)        | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | out0                                                                                                                                                | staticcall                                  | @llvm.syncvm.precompile(i256 %in1, i256 %ergs)                                                                             | way to trigger call to precompile in VM                                                                                                                                                                                                                                                                                                                 |
| decommit(versioned\_hash, ergs\_to\_burn, out0)                                 | versioned\_hash (u256) | 0xFFDD                 | -                                                         | ergs\_to\_burn (u32)        | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | out0                                                                                                                                                | staticcall                                  | saves the result pointer to @ptr\_decommit                                                                                 | way to trigger decommit in VM                                                                                                                                                                                                                                                                                                                           |
| meta                                                                            | 0                      | 0xFFFC                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | [u256 tight packing](https://github.com/matter-labs/EraVM_opcode_defs/blob/b000abebc27f88919e0087b7604b8c71ba5b3daf/src/definitions/abi/meta.rs#L6) | staticcall                                  | @llvm.syncvm.context(i256 %param) ; param == 3 (see SyncVM.h)                                                              | way to trigger call to meta information about some small pieces of the state in VM                                                                                                                                                                                                                                                                      |
| mimic\_call(to, abi\_data, implicit r5 = who to mimic)                          | who\_to\_call          | 0xFFFB                 | 0                                                         | abi\_data                   | who\_to\_mimic                             | 0                            | 0                            | WILL mess up the registers and WILL use r1-r4 for our standard ABI convention and r5 for the extra who\_to\_mimic argument                          | any in the code; mimic call in the bytecode | Runtime {i256, i1} \_\_mimiccall(i256, i256, i256, {i256, i1})                                                             |                                                                                                                                                                                                                                                                                                                                                         |
| system\_mimic\_call(to, abi\_data, implicit r3, r4, r5 = who to mimic)          | who\_to\_call          | 0xFFFA                 | 0                                                         | abi\_data                   | who\_to\_mimic                             | value\_to\_put\_into\_r3     | value\_to\_put\_into\_r4     | WILL mess up the registers and WILL use r1-r4 for our standard ABI convention and r5 for the extra who\_to\_mimic argument                          | any in the code; mimic call in the bytecode | Runtime {i256, i1} \_\_mimiccall(i256, i256, i256, {i256, i1})                                                             |                                                                                                                                                                                                                                                                                                                                                         |
| mimic\_call\_byref(to, ACTIVE\_PTR, implicit r5 = who to mimic)                 | who\_to\_call          | 0xFFF9                 | 0                                                         | 0                           | who\_to\_mimic                             | 0                            | 0                            | WILL mess up the registers and WILL use r1-r4 for our standard ABI convention and r5 for the extra who\_to\_mimic argument                          | any in the code; mimic call in the bytecode | Runtime {i256, i1} \_\_mimiccall(i8 addrspace(3), i256, i256, {i256, i1})                                                  | Same as one above, but takes ABI data from ACTIVE\_PTR                                                                                                                                                                                                                                                                                                  |
| system\_mimic\_call\_byref(to, ACTIVE\_PTR, implicit r3, r4, r5 = who to mimic) | who\_to\_call          | 0xFFF8                 | 0                                                         | 0                           | who\_to\_mimic                             | value\_to\_put\_into\_r3     | value\_to\_put\_into\_r4     | WILL mess up the registers and WILL use r1-r4 for our standard ABI convention and r5 for the extra who\_to\_mimic argument                          | any in the code; mimic call in the bytecode | Runtime {i256, i1} \_\_mimiccall(i8 addrspace(3), i256, i256, {i256, i1})                                                  | Same as one above, but takes ABI data from ACTIVE\_PTR                                                                                                                                                                                                                                                                                                  |
| raw\_far\_call                                                                  | who\_to\_call          | 0xFFF7                 | 0                                                         | 0                           | abi\_data (CAN be with “to system = true”) | output\_offset               | output\_length               | Same as for EVM call                                                                                                                                | call                                        | static                                                                                                                     | <p>delegate (the call type is preserved)<br>It’s very similar to “system\_call” described below, but for the cases when we only need to have to\_system = true set in ABI (responsibility of the user, NOT the compiler), but we do not actually need to pass anything through r3 and r4 (so we can save on setting them or zeroing them, whatever)</p> |
| raw\_far\_call\_byref                                                           | who\_to\_call          | 0xFFF6                 | 0                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | output\_offset               | output\_length               | Same as for EVM call                                                                                                                                | call                                        | static                                                                                                                     | <p>delegate (the call type is preserved)<br>Same as one above, but takes ABI data from ACTIVE\_PTR</p>                                                                                                                                                                                                                                                  |
| system\_call                                                                    | who\_to\_call          | 0xFFF5                 | value\_to\_put\_into\_r3 (only for call with 7 arguments) | value\_to\_put\_into\_r4    | abi\_data (MUST have “to system” set)      | value\_to\_put\_into\_r5     | value\_to\_put\_into\_r6     | Same as for EVM call                                                                                                                                | call                                        | static                                                                                                                     | <p>delegate (the call type is preserved)<br>to call system contracts, like MSG\_VALUE\_SIMULATOR. We may need 4 different formal definitions for cases when we would want to have integer/ptr in r3 and r4</p>                                                                                                                                          |
| system\_call\_byref                                                             | who\_to\_call          | 0xFFF4                 | value\_to\_put\_into\_r3 (only for call with 7 arguments) | value\_to\_put\_into\_r4    | 0xFFFF to prevent optimizing out by Yul    | value\_to\_put\_into\_r5     | value\_to\_put\_into\_r6     | Same as for EVM call                                                                                                                                | call                                        | static                                                                                                                     | <p>delegate (the call type is preserved)<br>to call system contracts, like MSG\_VALUE\_SIMULATOR. We may need 4 different formal definitions for cases when we would want to have integer/ptr in r3 and r4<br>Same as one above, but takes ABI data from ACTIVE\_PTR</p>                                                                                |
| set\_context\_u128                                                              | 0                      | 0xFFF3                 | value                                                     | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | -                                                                                                                                                   | call                                        |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| set\_pubdata\_price                                                             | in1                    | 0xFFF2                 | 0                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | -                                                                                                                                                   | call                                        | context.set\_ergs\_per\_pubdata in1 in assembly                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| increment\_tx\_counter                                                          | 0                      | 0xFFF1                 | 0                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | -                                                                                                                                                   | call                                        | context.inc\_tx\_num in assembly                                                                                           |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_calldata                                                                   | 0                      | 0xFFF0                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | one passed in r1 on far\_call to the callee (save in very first instructions on entry)                                     | Loads as INTEGER!                                                                                                                                                                                                                                                                                                                                       |
| call\_flags                                                                     | 0                      | 0xFFEF                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | one passed in r2 on far\_call to the callee (save in very first instructions on entry)                                     |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_return\_data                                                               | 0                      | 0xFFEE                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | one passed in r1 on return from far\_call back to the caller (save in very first instruction in the corresponding branch!) | Loads as INTEGER!                                                                                                                                                                                                                                                                                                                                       |
| event\_initialize                                                               | in1                    | 0xFFED                 | -                                                         | in2                         | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | call                                        |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| event\_write                                                                    | in1                    | 0xFFEC                 | -                                                         | in2                         | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | call                                        |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| load\_calldata\_into\_active\_ptr                                               | 0                      | 0xFFEB                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | loads value of @calldataptr (from r1 at the entry point of the contract into virtual ACTIVE\_PTR)ACTIVE\_PTR               |                                                                                                                                                                                                                                                                                                                                                         |
| load\_returndata\_into\_active\_ptr                                             | 0                      | 0xFFEA                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | loads value of the latest @returndataptr (from the r1 at the point of return from the child into virtual ACTIVE\_PTR)      |                                                                                                                                                                                                                                                                                                                                                         |
| load\_decommit\_into\_active\_ptr                                               | 0                      | 0xFFDC                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | loads value of the @ptr\_decommit into virtual ACTIVE\_PTR                                                                 |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_add\_into\_active                                                          | in1                    | 0xFFE9                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | performs ptr.add ACTIVE\_PTR, in1, ACTIVE\_PTR                                                                             |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_shrink\_into\_active                                                       | in1                    | 0xFFE8                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | performs ptr.shrink ACTIVE\_PTR, in1, ACTIVE\_PTR                                                                          |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_pack\_into\_active                                                         | in1                    | 0xFFE7                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | performs ptr.pack ACTIVE\_PTR, in1, ACTIVE\_PTR                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| multiplication\_high                                                            | in1                    | 0xFFE6                 | -                                                         | in2                         | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            | Returns the higher register (the overflown part)                                                                                                    | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| extra\_abi\_data                                                                | in1                    | 0xFFE5                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | ones passed in r3-r12 on far\_call to the callee (saved in the very first instructions in the entry)                       |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_data\_load                                                                 | offset                 | 0xFFE4                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_data\_copy                                                                 | destination            | 0xFFE3                 | -                                                         | source                      | size                                       | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| ptr\_data\_size                                                                 | 0                      | 0xFFE2                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| active\_ptr\_swap                                                               | index\_1               | 0xFFD9                 | -                                                         | index\_2                    | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | swaps active pointers                                                                                                      |                                                                                                                                                                                                                                                                                                                                                         |
| const\_array\_declare                                                           | index(constant)        | 0xFFE1                 | -                                                         | size(constant)              | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| const\_array\_set                                                               | index(constant)        | 0xFFE0                 | -                                                         | offset(constant)            | 0xFFFF to prevent optimizing out by Yul    | value(constant)              | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| const\_array\_finalize                                                          | index(constant)        | 0xFFDF                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| const\_array\_get                                                               | index(constant)        | 0xFFDE                 | -                                                         | offset                      | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  |                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                         |
| return\_forward                                                                 | 0                      | 0xFFDB                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | generates a return forwarding the active pointer                                                                           |                                                                                                                                                                                                                                                                                                                                                         |
| revert\_forward                                                                 | 0                      | 0xFFDA                 | -                                                         | 0                           | 0xFFFF to prevent optimizing out by Yul    | 0                            | 0                            |                                                                                                                                                     | staticcall                                  | generates a revert forwarding the active pointer                                                                           |                                                                                                                                                                                                                                                                                                                                                         |

#### Requirements for calling system contracts <a href="#requirements-for-calling-system-contracts" id="requirements-for-calling-system-contracts"></a>

By default, all system contracts up to the address `0xFFFF` require that the call was done via system call (i.e. `call_flags&2 != 0` .

**Exceptions:**

* BOOTLOADER\_FORMAL address as the users need to be able to send money there.

**Meaning of ABI params:**

* MSG\_VALUE\_SIMULATOR: `extra_abi_data_1 = value || whether_the_call_is_system`, where || denotes the concatenation, value should occupy first 128 bits, while `whether_the_call_is_system` is a 1-bit flag that denotes whether the call should be a system call. `extra_abi_data_2` is the address of the callee.
* No meaning for the rest.

<br>
