CensoredUsername / dynasm-rs

A dynasm-like tool for rust.
https://censoredusername.github.io/dynasm-rs/language/index.html
Mozilla Public License 2.0
716 stars 52 forks source link

omit known zero offset #71

Open HookedBehemoth opened 2 years ago

HookedBehemoth commented 2 years ago

Found that when writing code like ; mov QWORD [buffer + offset as i32], temp where offset is a immediate value this crate will unconditionally emit relative instructions. When explicitly checking for when offset is zero and emitting ; mov QWORD [buffer], temp reduces my emitted code significantly (~10%). Maybe this crate could check if immediate values are zero and choose to emit smaller instructions.

HookedBehemoth commented 2 years ago

To aid with size reduction it would also be nice to have a "smallest" size annotation. The assembler could then choose to use add/sub/jmp instructions with the smallest possible size for the immediate value. i.e. ; add r14, AUTO s.len() as _ with s.len() < 255 -> 49 83 c6 XX else -> 49 81 c6 XX XX XX XX

CensoredUsername commented 2 years ago

The crate is built with the idea in mind that instruction sizes (and the generated instruction) should be predictable from the code, to allow things like instruction patching / hotswapping. The behaviour you mentioned first would need some kind of annotation as well to allow it.

I'm having trouble thinking of how to implement this properly though. The x64 instruction set is not regular at all, and just selecting the right instruction is already extremely hairy. Would probably have to just generate the three size possibilities, pass them each through it and see which compiles. And then figure out how to generate branching code from the backend (which also expects somewhat stable sizes).

Summarizing: I might consider it at some point but this is fairly low priority, especially considering the same behaviour can be implemented by the user easily.