Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brainstorm how to insert constants into code while constructing a contract #103

Open
SamWilsn opened this issue Aug 1, 2022 · 0 comments
Labels
A-assembler Area: assembler C-enhance Category: a request for an improvement E-medium Experience: of moderate difficulty

Comments

@SamWilsn
Copy link
Contributor

SamWilsn commented Aug 1, 2022

It'd be nice to make it easier to insert constants into code while constructing a contract.

These are pretty much off the top of my head, so bear with me.

I think I'm partial to Solution 1 because it seems the most general / least opinionated, but Solution 2 probably has less footguns.

Solution 1: Exported Labels

We modify %include to expose the labels of the included file.

main.etk

foo:
push32 0x0000000000000000000000000000000000000000000000000000000000000000

bar:
push32 0x0000000000000000000000000000000000000000000000000000000000000000

ctor.etk

# Copy the runtime code.
%push(end-start)
dup1
%push(start)
%push(0)
codecopy

# Set the constants.
caller
%push(start.foo - start + 1)
mstore

caller
%push(start.bar - start + 1)
mstore

# Return the adjusted code.
%push(0)
return
start:
    %include("main.etk")
end:

Solution 2: %const(...)

We add a new built-in instruction macro %const(n). It errors if compiled directly, but expands to a push32 with some sentinel value (0xdeadc0de...) when included.

Then we modify %include to take extra parameters. Each parameter is the name of a label pointing to where the constant should be written.

Then your initcode can reference those labels to write the constants:

main.etk

%const(0)
%const(1)

ctor.etk

# Copy the runtime code.
%push(end-start)
dup1
%push(start)
%push(0)
codecopy

# Set the constants.
caller
%push(foo - start)
mstore

caller
%push(bar - start)
mstore

# Return the adjusted code.
%push(0)
return
start:
    %include("main.etk", foo, bar)
end:
@SamWilsn SamWilsn added C-enhance Category: a request for an improvement A-assembler Area: assembler E-medium Experience: of moderate difficulty labels Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assembler Area: assembler C-enhance Category: a request for an improvement E-medium Experience: of moderate difficulty
Projects
None yet
Development

No branches or pull requests

1 participant