5 ms·
* 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--typi
by wahern 7y ago
* 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.
- eridius 7y agoSwift 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.
- steveklabnik 7y agoRust has AST -> HIR -> MIR -> LLVM-IR. Two internal IRs and a third external one!
- eridius 7y agoGood point, I forgot about HIR, though isn't HIR largely just a desugared representation of the AST?
- steveklabnik 7y agoYes, but also post macro expansion and name resolution.
- monocasa 7y agoAnd compcert has at least seven different IRs, several of which are ASTs. http://compcert.inria.fr/diagram.png 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.
- TheAceOfHearts 7y agoDefinitely 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/ https://github.com/facebook/jscodeshift/ [1] https://astexplorer.net/ https://astexplorer.net/
- scarface74 7y agoEvery 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.
- pjmlp 7y agoIf 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.
- scarface74 7y agoSee “No True Scotsman”.... What next they aren’t a “true engineer” unless they can reverse a b-tree on the whiteboard?
- pjmlp 7y agoFor 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.
- scarface74 7y agoAnd 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?
- pjmlp 7y ago