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

Emit custom machine code #9

Open
N00byEdge opened this issue May 19, 2023 · 4 comments
Open

Emit custom machine code #9

N00byEdge opened this issue May 19, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@N00byEdge
Copy link
Member

N00byEdge commented May 19, 2023

Something like the following:

fn fun() void {
    var local: u64 = undefined;
    @emit(.{
        .init = .{
            .rax = 4,
            .rcx = 5,
        },
        .bytes = .{1, 2, 3, 4},
        .clobbers = .{.rcx, .rdx},
        .post = {
            local = @fetch(.rax);
        },
    });
}
@48cf
Copy link
Collaborator

48cf commented Jul 19, 2023

maybe breaking it down into 2/3 separate builtins would be a better idea? i.e. @register which would allow for getting/setting the register's value and the @emit which would let you emit custom code + mark clobbered registers? i feel like implementing a builtin with so many user values might be a bit of a hassle... also the first builtin i proposed would be nice for writing isr routines ;^) you could get all the registers easily without a redundant @emit expression

example usage of @register and @emit with my proposed changes, this example should perform a linux write syscall and retrieve the return value to a local variable:

@register(.rax) = 1;
@register(.rdi) = 0;
@register(.rsi) = @ptr_to_int("Hello world!".&);
@register(.rdx) = 12;
@emit(.{0x0f, 0x05}, .{.rax});
const result = @register(.rax);

@N00byEdge
Copy link
Member Author

N00byEdge commented Jul 19, 2023

Uh, what I don't like about that is that we don't have a way to know that the inputs depend on the register assignments and that the outputs depends on the @emit which means things could start to break with optimizations (the register assignments can be optimized out, reordered to after the asm or whatever).

What you sent above is the IR that will be generated (but with some dependencies) anyways.

@48cf
Copy link
Collaborator

48cf commented Jul 19, 2023

good point, its very possible that you explained it to me already like this but i just forgot, what about making @register and @emit volatile? or would that screw with optimizations too much?

@N00byEdge
Copy link
Member Author

N00byEdge commented Jul 19, 2023

Yes, that would prevent a lot of optimizations like being able to remove an unused, non-volatile @emit statement or an unused @register result.

@N00byEdge N00byEdge added the enhancement New feature or request label Aug 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants