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

User macros #69

Merged
merged 60 commits into from
Sep 28, 2021
Merged

User macros #69

merged 60 commits into from
Sep 28, 2021

Conversation

lightclient
Copy link
Contributor

@lightclient lightclient commented Jul 14, 2021

This PR introduces the following new features:

Expressions

Doing arithmetic on push data is a desirable attribute of an assembler.

push1 1+2
> 6003

push1 -1+2
> 6001

While constant expressions may improve readability, they aren't that useful. The big win is doing arithmetic on label addresses.

start:
caller
pop
end:
push1 end - start
> 33506002

Any normal operand type is available as an expression term!

push4 selector("foo()")
> 63c2985578

push4 selector("foo()") - 1
> 63c2985577

Expression Macros

Expressions significantly improve the usability of eas. Now that we have a rich format for expressing mathematical expressions, it would be great if we could reuse those expressions.

%def three()
    1+2
%end
push1 three()
> 6003

%def add_one(x)
    $x+1
%end
push1 add_one(1+1)
> 6003

%def add_one(x)
    $x+1
%end
push1 add_one(three())
> 6004

Instruction Macros

Instruction macros take things a step further. While expression macros are only valid as operands, instruction macros can output arbitrary instructions into the program. This allows users to avoid repetition in their code.

%macro selector()
    calldataload
    push1 0x28
    shr
%end
%selector()
> 3560281c

# assumes the value is already the top stack element
%macro mask_value(mask)
    push1 $mask
    and
%end
push1 0b11
%mask_value(0b10)
> 6003600216

@lightclient lightclient force-pushed the user-macros branch 2 times, most recently from be115ee to 0ccf646 Compare August 13, 2021 20:02
@lightclient
Copy link
Contributor Author

lightclient commented Aug 16, 2021

seems like the undefined macro error isn't triggering correctly fixed

@lightclient lightclient marked this pull request as ready for review September 21, 2021 21:01
etk-asm/src/asm.rs Outdated Show resolved Hide resolved
etk-asm/src/asm.rs Show resolved Hide resolved
etk-asm/src/asm.rs Show resolved Hide resolved
etk-asm/src/asm.rs Outdated Show resolved Hide resolved
etk-asm/src/asm.rs Outdated Show resolved Hide resolved
etk-asm/src/ops/expression.rs Show resolved Hide resolved
etk-asm/src/ops/expression.rs Outdated Show resolved Hide resolved
etk-asm/src/ops/expression.rs Outdated Show resolved Hide resolved
etk-asm/src/ops/macros.rs Show resolved Hide resolved
etk-asm/src/ops/macros.rs Outdated Show resolved Hide resolved
lightclient and others added 3 commits September 22, 2021 13:15
Co-authored-by: Sam Wilson <57262657+SamWilsn@users.noreply.github.com>
Co-authored-by: Sam Wilson <57262657+SamWilsn@users.noreply.github.com>
@lightclient
Copy link
Contributor Author

lightclient commented Sep 26, 2021

All feedback addressed, PTAL! 😃

@SamWilsn SamWilsn merged commit 6395b50 into master Sep 28, 2021
@SamWilsn SamWilsn deleted the user-macros branch September 28, 2021 01:44
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.

None yet

2 participants