dynasm-rs icon indicating copy to clipboard operation
dynasm-rs copied to clipboard

Dynamic Memory Operands

Open paul1999 opened this issue 11 months ago • 3 comments

Greetings!

I am currently porting a simple JIT, used in a compiler construction class, from C++ to Rust. In C++, I used the asmjit project. In asmjit, memory operands and registers can be used as values, i.e. I can assign a memory operands to a variable and later use it in an assembler call

auto foo = x86::qword_ptr(x86::rbp, -off_s);
as.mov(foo, 42);

If I want to use a value stored in memory at the moment, I have to repeat

let a = Foo::from_rbp_offset(8);
let b = Foo::from_rbp_offset(16);
dynasm!(self.as
	; mov rax, QWORD [rbp - a.offset]
	; add rax, QWORD [rbp - b.offset]);

I.e. I have to repeat the memory reference each time.

It would be really handy to have a feature similar to the asmjit one in this crate, i.e. being able to do something like

enum Location {
	Register(reg),
	RegisterStaticOffset(reg, offset),
	RegisterDynamicOffset(reg, reg),
}

impl MemoryReference for Foo {
	fn location(&self) -> Location;
}

fn load_to_rax(mut self, loc: Foo) {
	dynasm!(self.as
		; mov rax, QWORD [loc]);
} 

Because then you could encapsulate where certain values are stored (relative to rbp, relative to rsp, etc.) in the Trait Implementation.

I do not know too much about Rust macros, so I apologize if something like this is not feasible to implement. On the other hand, I'd be willing to tinker with this myself, I would just need a pointer (or reference) in the right direction.

Best regards!

paul1999 avatar Mar 01 '24 11:03 paul1999