News for hackers. Stuff that matters.
Re:A New Bytecode Format for JavaScriptCore by cogman10 on Friday June 21, 2019 @02:49PM (#20245297)
I'm unclear, why transform to a bytecode as a first step? Wouldn't it be simpler to instead to transform to an AST and work against that for everything? Wouldn't it make sense to generate the bytecode as sort of a last step before doing heavy duty optimizations?

Seems like with JS you would be constantly transforming from bytecode -> AST -> bytecode -> machine code, such as every time a method adds a new field to an object or some optimization assumption is violated. It doesn't seem like it would be easier or faster to work with. I'm guessing there wouldn't be a whole lot of memory benefits either.

Would someone mind illuminating me on this design choice? (Granted, I've not taken a compilers course, so feel free to call me an idiot for not knowing something basic about how compilers work).

Re:A New Bytecode Format for JavaScriptCore by wahern on Friday June 21, 2019 @03:41PM (#20245707)
* Adding a new field or method to an object at runtime doesn't change the AST.

* You don't translate from bytecode to AST.

* In tiered JIT architectures--typical for dynamically-typed languages like JavaScript where as you point out effective types can mutate at runtime--most code is executed by an interpreter, not as compiled machine code.[1] Interpreters are more efficient when executing a bytecode than walking an AST.

* Translating an AST straight to machine code is much more difficult than translating a low-level intermediate representation, and such representations are often easy to express as simple bytecodes. Compilers like GCC and LLVM use intermediate representations: RTL and IR. Both are effectively assembly languages, and like all assembly languages trivial to express as bytecode. GCC also has GIMPLE, a higher-level intermediate representation earlier in the pipeline which expresses a generic AST. The fact that GIMPLE and RTL coexist drives home the point that you don't want to be translating tree-like representations straight to machine code.

* You don't need to take my word for this, or the word of anyone else. Every programmer should have experience writing parsers, compilers, and interpreters, regardless of whether they took a class or even went to university. By doing this most of the reasons for the way things are done will become immediately clear to you. Note that splitting strings with regular expressions does not count as parsing, not for these purposes. A good project, however, would be to write a regular expression parser, compiler, and interpreter. There are many examples to follow and you can copy their designs exactly; just don't copy+paste as actually going through the motions and understanding the complexities inherent in the implementations is how everything will become clear.

[1] How this mutation is handled and why tiered architectures are preferable is another topic entirely.

Re:A New Bytecode Format for JavaScriptCore by TheAceOfHearts on Friday June 21, 2019 @05:38PM (#20246508)
Definitely agree that most programmers would benefit from studying these topics. They have broader applications than many realize.

You can use tools like jscodeshift [0] to refactor code! I've previously written codemods to migrate away from a bad design decisions. The React team has also used codemods to introduce breaking changes. AST Explorer [1] is the best tool I've found for writing codemods since it gives you nearly instant feedback and allows for a good amount of exploration.

[0] https://github.com/facebook/jscodeshift/

[1] https://astexplorer.net/

Re:A New Bytecode Format for JavaScriptCore by eridius on Friday June 21, 2019 @04:26PM (#20246033)
Swift and Rust also have their own higher-level intermediate representations that they translate their AST to before even going to LLVM IR (SIL and MIR respectively). Rust used to translate their AST directly into LLVM IR but they switched to going through MIR first because it unlocks a lot of optimizations as well as improved user-facing functionality such as non-lexical lifetimes in the borrow checker.
Re:A New Bytecode Format for JavaScriptCore by steveklabnik on Friday June 21, 2019 @05:44PM (#20246539)
Rust has AST -> HIR -> MIR -> LLVM-IR. Two internal IRs and a third external one!
Re:A New Bytecode Format for JavaScriptCore by eridius on Friday June 21, 2019 @06:32PM (#20246805)
Good point, I forgot about HIR, though isn't HIR largely just a desugared representation of the AST?
Re:A New Bytecode Format for JavaScriptCore by steveklabnik on Friday June 21, 2019 @07:19PM (#20247045)
Yes, but also post macro expansion and name resolution.
Re:A New Bytecode Format for JavaScriptCore by monocasa on Saturday June 22, 2019 @12:13AM (#20247984)
And compcert has at least seven different IRs, several of which are ASTs. http://compcert.inria.fr/diagram.png

That being said, consistency of internal data structures is probably one of those areas that separates a JIT from an offline compiler. I can see the benefit of switching to a bytecode representation pretty early in that circumstance.

Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @01:26AM (#20248177)
Every programmer should have experience writing parsers, compilers, and interpreters, regardless of whether they took a class or even went to university.

The majority of developers doing yet another software as a service CRUD app or your typical “dark matter developer” who will write software that will never see the light of day outside of the company they work for would never benefit from writing a compiler or interpreter.

Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @02:51AM (#20248370)
If they feel like calling themselves engineers, then they must know this stuff, as it is part of any proper engineering degree.

Now if they followed other path into programming, then by all means.

Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @04:02AM (#20248542)
See “No True Scotsman”....

What next they aren’t a “true engineer” unless they can reverse a b-tree on the whiteboard?

Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @04:18AM (#20248580)
For sure they aren't one if they don't have the degree from an university acknowledged by the Engineers Order.

Which most likely will have a good class in algorithms and data structures.

So while I don't expect every engineer to actually be able to do it, I expect him/her to be able to explain how to attack the problem.

Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @04:22AM (#20248592)
And you personally have studied the curriculum of every single computer science/computer engineering degree and can say that every single one required writing a compiler or interpreter and that no person who hasn’t written one can call themselves an engineer?
Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @04:23AM (#20248594)
The engineering Order has taken that job twice.

When they certify the university and then when they issue the professional titles exam.

Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @04:31AM (#20248614)
So they “certify universities” worldwide?
Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @05:01AM (#20248706)
Each one certifies the universities of the respective country, naturally.

I am well aware that US doesn't seem to value proper engineering education, and everyone is allowed to call themselves engineers.

Thankfully not every country sees it that way.

Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @08:42AM (#20249310)
Yes I guess that’s why the US is such a backwater when it comes to innovation and why we have so few successful technology startups....
Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @10:02AM (#20249593)
Should we do an exercise of which US tech was actually researched by immigrants with a proper engineering degree, and how much of that tech is actually built on US?
Re:A New Bytecode Format for JavaScriptCore by scarface74 on Saturday June 22, 2019 @10:24AM (#20249727)
Should we do an exercise in how much knowing how to write compilers helps in the day to day life of your average SAAS CRUD developer or bespoke app developer - my original statement?
Re:A New Bytecode Format for JavaScriptCore by pjmlp on Saturday June 22, 2019 @12:08PM (#20250307)
It depends if the bespoke develop ever needs to parse some kind of data file to import into their CRUD application, provide customization points to their customers or configuration file parser.

Then there is the whole point if HR is hiring actual engineers.

Re:A New Bytecode Format for JavaScriptCore by olliej on Friday June 21, 2019 @03:56PM (#20245827)
There are a slew of problems.

For a very long time JSC did use an AST, and interpreting and executing an AST is expensive. The very first version of the byte code interpreter (all hail squirrelfish) was in the order of twice the speed of the AST, and the AST had been extensively micro-optimized at that point, to the extent that the dispatch cost itself (a single indirect call) was more than 10% of execution time.

There were also a bunch of correctness issues:

* Handling exceptions required manually check call results everywhere. There was an unending stream of minor correctness bugs caused by this. Looping semantics were similarly exciting. Because every call technically required those checks there was a significant cost there, even though there were numerous cases they were missed.

* Changes in language semantics required duplicate changes in many different places

* New language features require a lot of very similar behaviour, with minor variations that resulted in duplicated code, introducing new places to accidentally miss exception and loop checks.

* Memory overhead was massive, both in terms of actual usage, and also allocation costs.

* Significant reductions in maximum recursion depth. Back in the days of the AST interpreter JSC had a hard limit of 500 levels of recursion, with plenty of checks, and it would still periodically blow the system stack and crash.

The cost of producing the byte code in the first place is super low, and because the byte code is all essentially the most primitive of JS operations, new language features generally don't require the optimizing passes to be aware of any changes, because they're mostly just syntax around existing primitive operations.

Re:A New Bytecode Format for JavaScriptCore by pizlonator on Friday June 21, 2019 @03:04PM (#20245408)
AST is less transformable. We transform our bytecode as if it was just an IR.

ASTs are a pretty annoying form to use as a source of shared truth for OSR. JSC has 4 tiers so we need a convenient-to-use shared truth IR. That’s what bytecode is for. For example, bytecode offers high-scalability answers to questions like “where should I exit” and “what is live when I get there”.

Re:A New Bytecode Format for JavaScriptCore by fouc on Friday June 21, 2019 @07:44PM (#20247145)
Isn't bytecode essentially a machine language for a simplified VM? Why would it be more transformable than a tree?
Re:A New Bytecode Format for JavaScriptCore by pizlonator on Saturday June 22, 2019 @01:18AM (#20248161)
Because it’s not a tree.

Compiler IRs tend to have the property that operands are not themselves expressions; they can only be variables or maybe constants. That makes it easy to identify sequence points, which makes it easier to insert code or reshape the control flow graph.

Re:A New Bytecode Format for JavaScriptCore by steveklabnik on Friday June 21, 2019 @02:57PM (#20245357)
A bytecode is easier to keep stable than an AST is; an AST changes as the language changes, but that doesn't mean you have to change the bytecode, only change the transformation from the AST to the bytecode.

I have no idea if this is the reason, but it's a possible one.

Re:A New Bytecode Format for JavaScriptCore by mhh__ on Friday June 21, 2019 @03:16PM (#20245524)
It's a bit of a micro-optimisation but ASTs are also usually represented fairly sparsely, so processing on bytecode could be much faster due to more efficient cache utilisation etc - some benefits from memory locality and also from bytecode usually being smaller in memory)

(I don't know JSC does it so that could be wrong)

Re:A New Bytecode Format for JavaScriptCore by pizlonator on Friday June 21, 2019 @03:04PM (#20245411)
That’s a pretty good reason also.
Re:A New Bytecode Format for JavaScriptCore by fpoling on Friday June 21, 2019 @03:13PM (#20245490)
Besides already mentioned benefits the new bytecode is serializable so it is very easy to cache it.

Serializable format of AST is a hard problem. WASM was supposed to be like that, but in the final design it became a stack machine.