Optimize CPU instructions to use less memory. #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I tried to optimize the virtual CPU to use less memory for its code.
I took inspiration from what I remember of the Z80 CPU assembly.
This simple optimization creates specific opcodes for CPU instructions for each register they are operating on, shedding the need to pass the register as an argument in most cases.
It makes little sense to apply such optimization for indirect references, since the CPU supports offsets that would anyway need to be passed as an argument, so no memory saving there.
With these changes the compiled Hello World example uses 12 bytes less.
These changes make the virtual CPU incompatible with binaries for the previous one, also due to me renumbering all opcodes.
In theory it would be possible to make it binary compatible by creating some compatibility opcodes with the correct numbers and shifting the new optimized ones. It would also require compatibility code to handle those "old" opcodes, It looks overkill in this case.
The assembler interface is unchanged though.