Skip to content

Conversation

d-xo
Copy link
Contributor

@d-xo d-xo commented Sep 16, 2025

This adds the core datatype definitions and class / instance definitions that allow us to desugar a surface level contract definition into a single entrypoint that dispatches to the contracts methods based on it's function selector hash.

An example desugaring can be seen at the end of the file. At a high level each surface level contract object will have a main generated that calls RunContract.exec on an instance of the Contract datatype reflecting the structure of the given contract. The defined typeclass instances will handle the dispatch generation and abi encoding/decoding.

Still to do:

  • receive()
  • move callvalue check to the top level if all methods are not payable
  • public / external

Pending a decision on our implementation of compile time eval it also relies on the desugaring stage to precompute the selector hash and insert that directly into the bytecode.

You can test the desugaring logic as follows. This will call the add2 method with 2 and 3 abi encoded, and then display the result:

$ ./runsol.sh std/dispatch.solc --calldata "add2(uint256,uint256)" 2 3
Processing: std/dispatch.solc
Compiling to core...
Emitting core for contract C
Writing to output1.core
Generating Yul...
found main
writing output to dispatch.yul
Compiling to bytecode...
Hex output: dispatch.hex
Executing...
"Return: 0x0000000000000000000000000000000000000000000000000000000000000005"
Decoded: 5


// --- Preliminaries ---

data Bool = True | False;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the builtin bool type gave me a bunch of specializer errors I didn't understand. I'll try to find a minimal reproducer and submit a bug.

@d-xo d-xo force-pushed the dispatch-desugaring-target branch 5 times, most recently from 814fdef to b2c9d2e Compare September 18, 2025 11:18
This adds the core datatype definitions and class / instance definitions
that allow us to desugar a surface level `contract` definition into a
single entrypoint that dispatches to the contracts methods based on it's
function selector hash.

An example desugaring can be seen at the end of the file. Still to do:

- receive()
- move callvalue check to the top level if all methods are not payable
- `public` / `external`
@d-xo d-xo force-pushed the dispatch-desugaring-target branch from f88dd77 to 5023704 Compare September 25, 2025 14:25
Copy link
Collaborator

@mbenke mbenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record

  • fails if a contract has more than one method
  • problems with argumentless methods (e.g. function one() -> uint256 { return uint256(1); }

It also would be good to split the library part from the example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants