Jump to content
Visual Boy Advance-M

dynamic recompiler to transform THUMB instructions to ARM instructions

Recommended Posts

Would it be possible to write a dynamic recompiler that translates from THUMB instructions to ARM instructions, so that existing instructions from the ARM core can be reused by the THUMB core? Would that give any benefits beside less code?

Share this post

Link to post
Share on other sites

Yes, if you're willing to invite all of the challenges associated with dynamic recompilers, like dealing with self-modifying code and branches.


When you just want a one to one correspondence you can use a direct mapped decode cache. This is what the author of Gemulator likes to do (see emulators.com). This makes it so you don't have to perform and block tracking and mapping, but you still have to invalidate cache lines on writes to accommodate self-modifying code.


In this case, it's actually just as well to just use a lookup table to convert any Thumb opcode to an ARM equivalent (or special undefined ARM code where there's no equivalent). The input is only 16 bits, and the output is nominally 32 bits. You'll likely need another bit of output, though, to determine how whether the PC offsets should be halfword aligned or not, because Thumb behavior regarding this is inconsistent. This method would probably be superior to any kind of cached conversion.


For your purposes, that is, only using interpreters, eliminating code is really the only benefit. Performance would be substantially worse because of the time needed to convert from Thumb to ARM, and also because ARM instructions are just a lot more expensive to emulate in general. In a usual GBA game over half the instructions executed will be Thumb, so you'd be hurting speed a ton across the board.


I went with this technique in the emulator I'm currently writing, but only because I was never planning on relying on the interpreter for speed (did a recompiler for that) and I wanted to have less points of failure. For recompilation, where the speed isn't that big of a deal, it's nice to have less code to work with, and having one instruction set instead of two means you don't have to write a bunch of analysis and optimization code twice. Another benefit is that it gives you room to perform propagation from Thumb to ARM, resulting in ultimately better code.


So in short, I think you shouldn't do it.

Share this post

Link to post
Share on other sites

Thank you very much for your detailed opinion.


It requires a lot of time to understand a system and the emulation techniques, which I currently don't have, so your experience is very appreciated.


I guess I should think my plans over. Maybe it's better to steal / borrow good [open source] code from other emulators instead of rewriting it from scratch.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now