cadet1620 wrote
As a first idea where to look to improve your VM translation, here is the breakdown of the number of various VM commands used in the OS.
@cadet1620
I found your list of OS VM command frequencies and was inspired to check the number of instructions needed for each command. The figures are specific to my own implementation of course. I should state that this is for a straight forward implementation based on the design suggestions in Chapters 7 and 8. No optimisation tricks.
This information was relatively easy to gather, as I had pushed the ASM implementation of each VM command into an array before writing to file.
Command Type Count Inst Inst% Inst/Comm
-----------------+------+------+------+----------
total 3927 37719 100.0 9.6
C_PUSH 2177 14793 39.2 6.8
constant >1 950 5700 15.1 6.0
indirect 577 5193 13.8 9.0
constant 0,1 539 3234 8.6 6.0
PUSHnPOP 132 2397 6.4 18.2
direct 111 666 1.8 6.0
C_CALL 232 10168 27.0 43.8
C_ARITHMETIC 451 4495 11.9 10.0
eq,gt,lt 122 2440 6.5 20.0
add,sub,and,or 267 1869 5.0 7.0
neg,not 62 186 0.5 3.0
C_POP 535 4457 11.8 8.3
indirect 198 2772 7.3 14.0
direct 337 1685 4.5 5.0
C_RETURN 59 2773 7.4 47.0
C_IF 93 465 1.2 5.0
C_FUNCTION 58 294 0.8 5.1
C_GOTO 114 228 0.6 2.0
Init 1 46 0.1 46.0
C_LABEL 207 0 0.0 0.0
A couple of notes:
- This list is sorted numerically descending by total Instructions used per Command Type (the Inst column).
- C_LABEL is a pseudo-command, it doesn't generate any instructions once assembled (I account for this).
- The total number of instructions is indeed > 32K in this implementation.
- Some command types have been broken down and grouped (like eq,gt,lt which all generate almost identical code).
- PUSHnPOP is a count of how many times a push is immediately followed by a pop (only accounts for 6.4% of all instructions).
- Clearly C_CALL with 43.8 Instructions per Command, and 27% of all instructions is the first optimisation candidate.
- Although C_RETURN is expensive to implement at 47 Instructions per Command, it is only 7.4% of all instructions.