3

I am confused about how v8 works and how it execute js code.

I understand that v8 first parse the code and create AST then take this AST introducing it to the interpreter as input and produce byte code then this byte code introduced to turbofan compiler that converts this intermediate code to machine code that computer understands.

I am reading an article about this topic which is good but it turned out that something is confusing to me

  1. Execution Phase: The byte code is executed by using the Memory heap and the Call Stack of the V8 engine’s runtime environment

I thought that machine code is the one that get executed otherwise why we need to create that code if the byte code is the one that is executed??

Code Eagle
  • 1,293
  • 1
  • 17
  • 34
  • 2
    This is a big topic, but modern interpreters employ a strategy called "threaded code", where *parts* of the code are translated to "bare metal" machine code, while other parts are not. It's all very complicated and not terribly useful knowledge for day-to-day programming for most people. – Pointy Sep 12 '21 at 17:14
  • 2
    Byte code is executed by the interpreter (part of V8), machine code is executed by the CPU. One can compile the AST into byte code or machine code or both, it's just a different trade-off (wrt. to compilation time and execution speed). – Bergi Sep 12 '21 at 17:15
  • @Bergi you mean that v8 may chosse to compile some code to machine code to add some performance?? – Code Eagle Sep 12 '21 at 17:27
  • 1
    @CodeEagle Yes, precisely that. It always starts with a simple compiler and interpretation, to reduce time until the first statement is executed. Then it may choose to spend more time to compile performance-sensitive code in a different manner, to make it run faster. How exactly the two compilers differ (or even how many there are), and what the exact trade-offs are, changes over the years of development of V8. – Bergi Sep 12 '21 at 17:34

1 Answers1

7

(V8 developer here.)

To confirm what @Bergi said in his comments: bytecode primarily serves the purpose of being executed by the interpreter, and that is in fact all that's needed to execute JavaScript. V8 (nowadays) doesn't compile all functions to machine code, only those that run hot enough for optimized compilation to (likely) be worth the time investment. As a particular detail of the implementation, the optimizing compiler uses the bytecode as input, which gives bytecode a secondary purpose; but that's really just a detail that could have been solved differently, such as by parsing the original source to an AST again when the optimizing compiler kicks in, which is how V8 used to do it until a couple of years ago.

(The article you linked to does mention most of these concepts, but I agree that it's written somewhat confusingly, and I'd disagree with some of its characterizations. See v8.dev/blog for official descriptions of how things work.)

jmrk
  • 34,271
  • 7
  • 59
  • 74
  • I understand this part now but this evolved another question in my mind , how byte code run on the machine without being converted to machine code?? – Code Eagle Sep 12 '21 at 21:36
  • 1
    @CodeEagle It is interpreted by a program, which reads in instruction after instruction and does what the instruction says. Not too different from the [fetch-execute cycle](https://en.wikipedia.org/wiki/Instruction_cycle) of the CPU, just on a different level (you might say "virtualised" or "simulated"), instead of being cast in silicon (actually: programmed in microcode). – Bergi Sep 12 '21 at 21:40
  • 2
    Right; the CPU can't execute bytecode directly, but the interpreter can interpret (=execute) it. – jmrk Sep 12 '21 at 21:51
  • @jmrk So there is a separate bytecode interpreter for this purpose. Instead of converting into bytecode why not convert directly to machine code? – Shamseer K Nov 24 '21 at 01:27
  • 2
    @ShamseerK: the biggest reason why we switched from a baseline compiler to a bytecode interpreter a couple of years ago is that bytecode takes a lot less memory than machine code. – jmrk Nov 24 '21 at 12:26