matter-labs / era-compiler-llvm

ZKsync fork of the LLVM framework.
Other
33 stars 16 forks source link

[EraVM] Add disassembler C-API. #692

Closed PavelKopyl closed 2 months ago

PavelKopyl commented 3 months ago

Example: Default mode:

       add  128, r0, r3
    stm.h   64, r3
    and!    1, r2, r0
    jump.ne 30
    add r1, r0, r2
    shr.s   96, r2, r2
    and code[11], r2, r2
    sub.s!  4, r2, r0
    jump.lt 38
    ldp r1, r3
    and code[13], r3, r3
    sub.s!  code[14], r3, r0
    jump.ne 38
    sub.s!  68, r2, r0
    jump.lt 38
    ldvl    r2
    sub!    r2, r0, r0
    jump.ne 38
    addp.s  36, r1, r2
    ldp r2, r2
    rol code[15], r2, r3
    sub.s   1, r0, r4
    sub.s!  255, r2, r0
    add.le  r3, r0, r4
    addp.s  4, r1, r1
    ldp r1, r1
    and r4, r1, r1
    stm.h   128, r1
    add code[16], r0, r1
    retl 41
    ldvl    r1
    sub!    r1, r0, r0
    jump.ne 38
    add 32, r0, r1
    stm.ah  256, r1
    stm.ah  288, r0
    add code[12], r0, r1
    retl 41
    add r0, r0, r1
    revl 42
    pncl    40
    retl 41
    revl 42
    <unknown> // zero fill for padding
11:
    .cell 4294967295
12:
    .cell 53919893334301279589334030174039261352344891250716429051063678533632
13:
    .cell -26959946667150639794667015087019630673637144422540572481103610249216
14:
    .cell 6775656105105473604099007350066834251830933750474818692318744137155055976448
15:
    .cell 18446744073709551614
16:
    .cell 2535301202817642044428229017600
    <unknown> // Next four octets are zero fill for padding
    <unknown>
    <unknown>
    <unknown>
    <unknown> // Next four octets are the hash. 
    <unknown>
    <unknown>
    <unknown>

Verbose mode:

 0: 00 00 00 80 03 00 00 39 add 128, r0, r3
       8: 00 00 00 40 00 30 04 3f   stm.h   64, r3
      10: 00 00 00 01 00 20 01 90   and!    1, r2, r0
      18: 00 00 00 1e 00 00 c1 3d   jump.ne 30
      20: 00 00 00 00 02 01 00 19   add r1, r0, r2
      28: 00 00 00 60 02 20 02 70   shr.s   96, r2, r2
      30: 00 00 00 0b 02 20 01 97   and code[11], r2, r2
      38: 00 00 00 04 00 20 00 8c   sub.s!  4, r2, r0
      40: 00 00 00 26 00 00 41 3d   jump.lt 38
      48: 00 00 00 00 03 01 04 3b   ldp r1, r3
      50: 00 00 00 0d 03 30 01 97   and code[13], r3, r3
      58: 00 00 00 0e 00 30 00 9c   sub.s!  code[14], r3, r0
      60: 00 00 00 26 00 00 c1 3d   jump.ne 38
      68: 00 00 00 44 00 20 00 8c   sub.s!  68, r2, r0
      70: 00 00 00 26 00 00 41 3d   jump.lt 38
      78: 00 00 00 00 02 00 04 16   ldvl    r2
      80: 00 00 00 00 00 02 00 4b   sub!    r2, r0, r0
      88: 00 00 00 26 00 00 c1 3d   jump.ne 38
      90: 00 00 00 24 02 10 03 70   addp.s  36, r1, r2
      98: 00 00 00 00 02 02 04 3b   ldp r2, r2
      a0: 00 00 00 0f 03 20 02 df   rol code[15], r2, r3
      a8: 00 00 00 01 04 00 00 8a   sub.s   1, r0, r4
      b0: 00 00 00 ff 00 20 00 8c   sub.s!  255, r2, r0
      b8: 00 00 00 00 04 03 a0 19   add.le  r3, r0, r4
      c0: 00 00 00 04 01 10 03 70   addp.s  4, r1, r1
      c8: 00 00 00 00 01 01 04 3b   ldp r1, r1
      d0: 00 00 00 00 01 14 01 6f   and r4, r1, r1
      d8: 00 00 00 80 00 10 04 3f   stm.h   128, r1
      e0: 00 00 00 10 01 00 00 41   add code[16], r0, r1
      e8: 00 00 00 29 00 01 04 2e   retl 41
      f0: 00 00 00 00 01 00 04 16   ldvl    r1
      f8: 00 00 00 00 00 01 00 4b   sub!    r1, r0, r0
     100: 00 00 00 26 00 00 c1 3d   jump.ne 38
     108: 00 00 00 20 01 00 00 39   add 32, r0, r1
     110: 00 00 01 00 00 10 04 43   stm.ah  256, r1
     118: 00 00 01 20 00 00 04 43   stm.ah  288, r0
     120: 00 00 00 0c 01 00 00 41   add code[12], r0, r1
     128: 00 00 00 29 00 01 04 2e   retl 41
     130: 00 00 00 00 01 00 00 19   add r0, r0, r1
     138: 00 00 00 2a 00 01 04 30   revl 42
     140: 00 00 00 28 00 00 04 32   pncl    40
     148: 00 00 00 29 00 01 04 2e   retl 41
     150: 00 00 00 2a 00 01 04 30   revl 42
     158: 00 00 00 00 00 00 00 00   <unknown>
11:
    .cell 4294967295
12:
    .cell 53919893334301279589334030174039261352344891250716429051063678533632
13:
    .cell -26959946667150639794667015087019630673637144422540572481103610249216
14:
    .cell 6775656105105473604099007350066834251830933750474818692318744137155055976448
15:
    .cell 18446744073709551614
16:
    .cell 2535301202817642044428229017600
     220: 00 00 00 00 00 00 00 00   <unknown>
     228: 00 00 00 00 00 00 00 00   <unknown>
     230: 00 00 00 00 00 00 00 00   <unknown>
     238: 00 00 00 00 00 00 00 00   <unknown>
     240: 3a c2 25 16 8d f5 42 12   <unknown>
     248: a2 5c 1c 01 fd 35 be bf   <unknown>
     250: ea 40 8f da c2 e3 1d dd   <unknown>
     258: 6f 80 a4 bb f9 a5 f1 cb   <unknown>

Code Review Checklist

Purpose

Ticket Number

Requirements

Implementation

Logic Errors and Bugs

Error Handling and Logging

Maintainability

Dependencies

Security

Performance

Testing and Testability

Readability

Documentation

Best Practices

Experts' Opinion

github-actions[bot] commented 3 months ago

Benchmark results eravm:

╔═╡ Size (-%) ╞════════════════╡ All M3B3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞══════════════╡ All M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞════════════════╡ All M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════════════╡ All MzB3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞══════════════╡ All MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞════════════════╡ All MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs/gas ╞══════╡ EVMInterpreter M3B3 ╞═╣
║ ADD                                44.750 ║
║ MUL                                26.850 ║
║ SUB                                44.750 ║
║ DIV                                29.250 ║
║ SDIV                               44.850 ║
║ MOD                                29.250 ║
║ SMOD                               42.450 ║
║ ADDMOD                             23.656 ║
║ MULMOD                             25.906 ║
║ EXP                                 7.338 ║
║ SIGNEXTEND                         26.850 ║
║ LT                                 48.750 ║
║ GT                                 48.750 ║
║ SLT                                72.750 ║
║ SGT                                70.750 ║
║ EQ                                 48.750 ║
║ ISZERO                             40.417 ║
║ AND                                44.750 ║
║ OR                                 44.750 ║
║ XOR                                44.750 ║
║ NOT                                36.417 ║
║ BYTE                               52.750 ║
║ SHL                                50.750 ║
║ SHR                                52.750 ║
║ SAR                                66.750 ║
║ SGT                                70.750 ║
║ SHA3                               27.051 ║
║ ADDRESS                            53.812 ║
║ BALANCE                            72.062 ║
║ ORIGIN                           1349.500 ║
║ CALLER                             53.812 ║
║ CALLVALUE                          53.812 ║
║ CALLDATALOAD                       38.750 ║
║ CALLDATASIZE                       54.125 ║
║ CALLDATACOPY                       69.185 ║
║ CODESIZE                           54.625 ║
║ CODECOPY                           62.456 ║
║ GASPRICE                         1352.312 ║
║ EXTCODESIZE                         4.996 ║
║ EXTCODECOPY                         5.060 ║
║ RETURNDATASIZE                     52.500 ║
║ RETURNDATACOPY                     45.222 ║
║ EXTCODEHASH                         6.153 ║
║ BLOCKHASH                         241.019 ║
║ COINBASE                         1352.500 ║
║ TIMESTAMP                        1349.500 ║
║ NUMBER                           1349.500 ║
║ PREVRANDAO                       1349.500 ║
║ GASLIMIT                         1355.500 ║
║ CHAINID                          1349.500 ║
║ SELFBALANCE                       642.125 ║
║ BASEFEE                          1349.500 ║
║ POP                                44.625 ║
║ MLOAD                              55.248 ║
║ MSTORE                             60.733 ║
║ MSTORE8                            68.480 ║
║ SLOAD                              26.001 ║
║ SSTORE                              8.920 ║
║ JUMP                               17.667 ║
║ JUMPI                              17.273 ║
║ PC                                 54.312 ║
║ MSIZE                              60.812 ║
║ GAS                                51.312 ║
║ JUMPDEST                           77.625 ║
║ PUSH0                              51.312 ║
║ PUSH1                              45.958 ║
║ PUSH2                              49.375 ║
║ PUSH4                              52.208 ║
║ PUSH5                              53.625 ║
║ PUSH6                              55.042 ║
║ PUSH7                              56.458 ║
║ PUSH8                              57.875 ║
║ PUSH9                              59.292 ║
║ PUSH10                             60.708 ║
║ PUSH11                             62.125 ║
║ PUSH12                             63.542 ║
║ PUSH13                             64.958 ║
║ PUSH14                             66.375 ║
║ PUSH15                             67.792 ║
║ PUSH16                             69.208 ║
║ PUSH17                             70.625 ║
║ PUSH18                             72.042 ║
║ PUSH19                             73.458 ║
║ PUSH20                             74.875 ║
║ PUSH21                             76.292 ║
║ PUSH22                             77.708 ║
║ PUSH23                             79.125 ║
║ PUSH24                             80.542 ║
║ PUSH25                             81.958 ║
║ PUSH26                             83.375 ║
║ PUSH27                             84.792 ║
║ PUSH28                             86.208 ║
║ PUSH29                             87.625 ║
║ PUSH30                             89.042 ║
║ PUSH31                             90.458 ║
║ PUSH32                             89.875 ║
║ DUP1                               36.417 ║
║ DUP2                               44.417 ║
║ DUP3                               44.417 ║
║ DUP4                               44.417 ║
║ DUP5                               44.417 ║
║ DUP6                               44.417 ║
║ DUP7                               44.417 ║
║ DUP8                               44.417 ║
║ DUP9                               44.417 ║
║ DUP10                              44.417 ║
║ DUP11                              44.417 ║
║ DUP12                              44.417 ║
║ DUP13                              44.417 ║
║ DUP14                              44.417 ║
║ DUP15                              44.417 ║
║ DUP16                              44.417 ║
║ SWAP1                              43.083 ║
║ SWAP2                              45.083 ║
║ SWAP3                              45.083 ║
║ SWAP4                              45.083 ║
║ SWAP5                              45.083 ║
║ SWAP6                              45.083 ║
║ SWAP7                              45.083 ║
║ SWAP8                              45.083 ║
║ SWAP9                              45.083 ║
║ SWAP10                             45.083 ║
║ SWAP11                             45.083 ║
║ SWAP12                             45.083 ║
║ SWAP13                             45.083 ║
║ SWAP14                             45.083 ║
║ SWAP15                             45.083 ║
║ SWAP16                             45.083 ║
║ CALL                               55.193 ║
║ STATICCALL                         54.227 ║
║ DELEGATECALL                       54.215 ║
║ CREATE                              4.804 ║
║ CREATE2                             6.899 ║
║ RETURN                              1.000 ║
║ REVERT                              1.000 ║
╠═╡ Ergs/gas (-%) ╞═╡ EVMInterpreter M3B3 ╞═╣
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter MzB3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════╡ Precompiles M3B3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞══════╡ Precompiles M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞════════╡ Precompiles M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════╡ Precompiles MzB3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞══════╡ Precompiles MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞════════╡ Precompiles MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞══════════╡ Real life M3B3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞════════╡ Real life M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞══════════╡ Real life M3B3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞══════════╡ Real life MzB3 ╞═╗
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞════════╡ Real life MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Ergs (-%) ╞══════════╡ Real life MzB3 ╞═╣
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝
PavelKopyl commented 2 months ago

We could disassemble a constant and find that it 'references' something else in the code section. Then we mistakingly emit it as a .cell.

Could you please, clarify that? We identify the location of a constant by analyzing code[word_num] arguments of instructions. If a such instruction exists, then at the location word_num, the we definitely have to emit .cellwith the constant.

github-actions[bot] commented 2 months ago

:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.