| 1 |
# So You Want to Add a Bytecode Op |
| 2 |
|
| 3 |
Occasionally we need to add a new bytecode operation in order to express or optimize |
| 4 |
some feature of JavaScript. This document is intended to assist you in determining if |
| 5 |
you ought to add a bytecode op, and what kind of changes and integrations will be |
| 6 |
required if you do this. |
| 7 |
|
| 8 |
Think of this as a more specialized version of the feature addition document. |
| 9 |
|
| 10 |
## First: Do you need a new bytecode op? |
| 11 |
|
| 12 |
There are alternatives to increasing the bytecode space! |
| 13 |
|
| 14 |
* [Self-hosted intrinsics][intrinsic] can be accessed directly from the bytecode |
| 15 |
generator using the `GetIntrinsic` bytecode. For an example see |
| 16 |
[`BytecodeEmitter::emitCopyDataProperties`][emitCopy]. Calls to intrinsics can be |
| 17 |
[(optionally) optimized with CacheIR as well][optimize]. |
| 18 |
* Desugar the behavior you want into a sequence of already existing ops. If this is |
| 19 |
possible this is often the right choice: Support for everything comes along mostly |
| 20 |
for free. |
| 21 |
* Is it possible to teach the JIT compilers to recognize your sequence of bytecode |
| 22 |
if special handling is required? If that kind of idiom recognition is too costly, |
| 23 |
you may be better served by a new bytecode op. |
| 24 |
|
| 25 |
|
| 26 |
## Second: Can we make your bytecode fast? |
| 27 |
|
| 28 |
To implement bytecode in our JIT compilers we put a few constraints on our bytecode. |
| 29 |
e.g. Each bytecode must be atomic, to support bailouts and exception behaviour. This |
| 30 |
means that your op must not create torn objects or invalid states. |
| 31 |
|
| 32 |
Ideally there would be a fast inline cache possible to implement your bytecode, as |
| 33 |
this brings speed and performance to our BaselineInterpreter layer, rather than |
| 34 |
waiting for Warp. |
| 35 |
|
| 36 |
ICs are valuable when behaviour varies substantially at runtime (ie, we choose |
| 37 |
different paths based on operand types). Examples of where we've used this are |
| 38 |
`JSOp::OptimizeSpreadCall` and `JSOp::CloseIter`, both of which let us generate a |
| 39 |
fastpath in the common case or fall back to something more expensive if necessary. |
| 40 |
|
| 41 |
|
| 42 |
[intrinsic]: https://searchfox.org/mozilla-central/search?q=intrinsic_&path=js%2Fsrc%2F&case=false®exp=false |
| 43 |
[emitCopy]: https://searchfox.org/mozilla-central/rev/650c19c96529eb28d081062c1ca274bc50ef3635/js/src/frontend/BytecodeEmitter.cpp#5018,5027,5039,5045,5050,5055,5067 |
| 44 |
[optimize]: https://searchfox.org/mozilla-central/rev/c1180ea13e73eb985a49b15c0d90e977a1aa919c/js/src/jit/CacheIR.cpp#10140 |