6 ms·
Understanding V8’s Bytecode
- tyingq 9y agoLooks like Wikipedia's V8 page needs an update. https://en.m.wikipedia.org/wiki/Chrome_V8 https://en.m.wikipedia.org/wiki/Chrome_V8 "V8 compiles JavaScript directly to native machine code before executing it, instead of more traditional techniques such as interpreting bytecode"
- DiThi 9y agoYep, Ignition and Turbofan have completely replaced Full-codegen and Crankshaft, the old V8 JIT compilers, which grew excessively big with many edge cases.
- amelius 9y agoV8's central documentation could use some work too. Why do we need to read about internals in a Medium post first?
- hyperpape 9y agoWhich languages have thorough first-party documentation of how the runtime works in an easily accessible form that's available to the public? I don't think Python or Ruby do. Lua maybe, but that's "cheating" because the runtime is small enough to comfortably read through. It seems to me like Hotspot has more resources and better documentation of its architecture than most, but when it comes to things like inlining policies, the specific compiler optimizations that happen, safepoints, etc. it's mostly a matter of tracking down random blog-posts, most of which come from third parties. Edit: Python is better than I remember.
- amelius 9y agoCheck the documentation of Mozilla's Spidermonkey in comparison: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Internals https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Sp...
- johncolanduoni 9y agoWow! The last time I looked at the SpiderMonkey API documentation (a few months ago) it was like reading the rings on a tree, so much so that I gave up and went with V8 even though there were actively developed SpiderMonkey bindings for the language I was using (Rust). Nice to see things have improved greatly!
- fernly 9y agoPython just revels in introspection. The standard module dis[1] lets the programmer disassemble and display bytecode, and includes a full list of the opcodes (which appear to be much simpler than the V8 ones described). The standard module ast[2] documents the AST used by the compiler. [1] https://docs.python.org/3.5/library/dis.html#module-dis https://docs.python.org/3.5/library/dis.html#module-dis [2] https://docs.python.org/3.5/library/ast.html#module-ast https://docs.python.org/3.5/library/ast.html#module-ast
- stcredzero 9y agoWhich languages have thorough first-party documentation of how the runtime works in an easily accessible form that's available to the public? Smalltalk, though you might have to go to a library and check out a dead tree book.
- colejohnson66 9y ago.NET bytecode (CLI) is standardized in ECMA-335[0] [0]: https://www.ecma-international.org/publications/standards/Ecma-335.htm https://www.ecma-international.org/publications/standards/Ec...
- pitaj 9y agoI'd love to see a tutorial on how a certain optimization pattern is coded into the compiler.
- CalChris 9y agoI don't really like this article. Bytecode is an abstraction of machine code. No, it isn't an abstraction if a few paragraphs later the author gives SuspendGenerator as an example bytecode. Abstraction is the wrong word here. A bytecode is a machine instruction for a virtual machine. In this case, the V8 bytecode is a higher level ISA than is for example, ARMv8; that would be more accurate. System 38 MI is a higher level ISA as well. Ignition, the interpreter, generates bytecode from this syntax tree. No, interpreters don't generate bytecode. They interpret bytecode; that's why they're called interpreters. Code generators generate code; that's why they're called code generators. However that said, this must be a strange description internal to the V8 group since it shows up here as well: https://v8project.blogspot.com/2016/08/firing-up-ignition-interpreter.html https://v8project.blogspot.com/2016/08/firing-up-ignition-in... You can generate code, bytecode, directly from an AST. You can do that. That is probably what is happening and what is meant here. On the other hand, LLVM converts an AST into an SSA IR and optimizes then lowers that. V8 is a JIT pipeline and LLVM is a traditional compiler pipeline.
- infogulch 9y agoIt's possible that the code generator and interpreter are bundled. (Though I have no idea in this specific case.) For example, it could be that the parser always passes the AST result to the interpreter which generates and starts to interpret the resulting bytecode directly. Then later it's passed to the JIT from there.
- pmelendez 9y ago"They interpret bytecode; that's why they're called interpreters." This is not universally true. Back in the day, interpreters execute machine code directly from source code. I still remember the debates about whether the JVM should be consider an interpreter. It is a bit of a pointless debate in my opinion anyway.
- dahart 9y ago> No, interpreters don't generate bytecode. They interpret bytecode; that's why they're called interpreters. Code generators generate code; FWIW, personally I prefer the article's idea of interpreters over yours, and I speculate the article's is more representative of what most people think. I've never heard interpreters described the way you just did, as the part that interprets bytecode. (But I did find discussion of "bytecode interpreters" in the WP article, link below). Normally, an interpreter is a complete alternative to a compiler. Usually, an interpreter is more or less synonymous with a programming language "shell", or even the whole programming language. The Python shell would be an interpreter, for example. In my mind, virtual machines are what you call the thing that executes bytecode, not interpreters. I'd assume that someone talking about an "interpreter" without any other qualification was talking about a program that reads & runs a another program written in a scripting language. As time passes, these lines are getting blurrier. V8 is liberally mixing concepts from compilers and interpreters, so it's dangerous to draw hard lines. But you might be interested to double-check the broad strokes. https://en.m.wikipedia.org/wiki/Interpreter_(computing) https://en.m.wikipedia.org/wiki/Interpreter_(computing) > Abstraction is the wrong word here. The bytecode binary runs without modification on multiple hardware platforms. It's pretty reasonable to call that an abstraction of machine code. Because it is.
- Annatar 9y agoIf they have all the plumbing, why are they forcing us all to go through the virtual machine? Why didn't they finish the job and produced a proper compiler which generates straight machine code, like a C compiler does?
- bobinatorino 9y agoSeriously though. Omitting this detail seems strange; providing an actual compiler seems like such an obvious thing to do - 1. In the browser, even if you can't allow raw machine code (for security reasons), you could at least compile your V8 bytecode into LLVM IR and then into WebAssembly. It would make front end code way faster running (b/c of compiler optimizations on the ENTIRE code base) and loading (smaller sizes, probably). 2. On the back end, you can compile straight to machine code instead of V8 compiling stuff at runtime. It would certainly be faster, as that compilation is happening in advance versus at runtime. There has to be some reason why this isn't being done. Perhaps the information at runtime is required, and a proper compiler would be too slow? Maybe there would be no major performance benefit? I could be wrong about the above two ideas. The V8 team has a ton of really smart people, so I can't imagine they haven't already considered this.
- tmzt 9y agoI glanced at the title late last night and thought it said VB bytecode, as in PCode.