wiz-lang / wiz

A high-level assembly language for writing homebrew software and games on retro console platforms.
http://wiz-lang.org/
Other
409 stars 40 forks source link

Synthesize load instructions between register pairs (Z80, GB) #109

Open Bananattack opened 3 years ago

Bananattack commented 3 years ago

(On Z80, GB, but could maybe pertain to other hardware) It would be nice if Wiz could automatically decompose load instructions on register pairs into load instructions on the individual parts, when a more suitable instruction doesn't exist.

As a simple example, this instruction isn't possible, because there is no load instruction that moves from one 16-bit register to another.

hl = bc;

This annoyingly produces an error. To a newcomer, they may think they need push and pop to load hl with bc, like so:

push(bc);
hl = pop();

But this isn't the most efficient way, and requiring temporary stack memory like this moves this particular step into something Wiz will never do implicitly.

Specifically for hl, there's this possibility, but this is also not the most size or cycle-efficient way:

hl = 0;
hl += bc;

There's a much more straightforward method. 16-bit registers (excepting SP and AF) can be split into their 8-bit parts. Load instructions exist between the general 8-bit registers, so hl = bc can be accomplished like this:

l = c;
h = b;

Wiz could automatically decompose the missing 16-bit load instruction into two 8-bit load instructions. Since this is the most efficient way to synthesize this, and since no registers other than those named in the assignment are accessed, there shouldn't be problems to do this.