b9org / b9

An educational JS virtual machine based on Eclipse OMR
http://www.base9.xyz
Apache License 2.0
45 stars 24 forks source link

Reduce the number of conditional jump operands #35

Open rwy7 opened 7 years ago

rwy7 commented 7 years ago

Right now, the interpreter has a whole pile of conditional jump bytecodes. Can we reduce the number and help get us back to 9 bytecodes?

Possible implementations:

  1. Encode the different logic modes (GT, LT, GTE, etc) as an immediate to the jump (cheating maybe).
  2. Reduce the code to a single jump-if-true bytecode, and rely on external comparison methods to push a boolean on the operand stack.
  3. Use smalltalk-style conditionals: implement a send bytcode, and use to send the if-true/if-false continuation to a boxed boolean object for conditional evaluation.
jduimovich commented 7 years ago

Well, you still need 4 bits for 9 bytecodes but you can encode the OpCode two nibbles (4:4 ).

So JMP as JMP: fits in a single byte and you have enough for LT,GT and all the the others including an ALWAYS. For math could encode a BinaryOP nibble BOP:<add,sub,etc>

but essentially the 9 become categories and you will have more than "9" bytecodes.

We should look at the format anyhow, we currently encode it at 8:24 so everything is the same size but the param(24) is unused in many bytecodes, so if we change it to variable size instructions we can get tighter packed instructions.