Search code examples
v8chromium

How to print the compiled instructions in V8 engine?


I have compiled the v8_hello_world sample and it could print "hello world" in the console. I know that V8 compiles the JavaScript with JIT, but I want to print the detailed message such as instructions it compiled. And I also want to know the types of these instructions, so what should I do?

Thank you very much~


Solution

  • Depending on the type(s) of generated code you're interested in, you'll have to pass the corresponding flag(s) to V8:

    • --print-code prints unoptimized machine code (created by "full codegen", the unoptimizing compiler)
    • --print-bytecode prints bytecode (created by the "Ignition" interpreter)
    • --print-opt-code prints optimized machine code (created by either the "Crankshaft" or "TurboFan" optimizing compilers)

    These flags (and many others) are documented by --help. Since major changes to the execution pipeline are currently ongoing, depending on which version of V8 you're using, you might see the same function compiled by different compilers.

    If you use the developer shell d8, you can pass these flags directly on the command-line. In your own embedding application, you can use v8::V8::SetFlagsFromCommandLine to pass argc and argv to V8. In d8.cc you can see an example for how to handle some flags yourself and pass on others to V8.


    Update one year later: "full codegen" and "Crankshaft" are gone. --print-bytecode still prints bytecode, --print-opt-code prints optimized machine code (now always from "Turbofan"). --print-code has less to do than before, but is still useful for generated regexp code and wasm code.