Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I really feel it doesn’t make much sense to generate assembly any longer. It’s much more reasonable to generate IR (for example for llvm). Then you get free optimizations and at least some portability. It’s probably easier as well to not keep track of registers.


If you're writing the compiler as an exercise, skipping codegen means you're missing out on a lot of really good stuff.


Code generation for me is more fun than the rest of it, as you are likely dealing with an NP complete problem.


As a learning experience I don't think it is a bad thing, though I would generally agree with what you're saying for any new compilers. Really though, writing a new compiler doesn't really make much sense anyway. Adding a frontend to LLVM or GCC would likely give much better results (Though I can't really say which one is easier - writing a dumb compiler for a simple language isn't too hard).

It's also worth noting that his compiler creates an intermediate AST (As any sane compiler should), so it's really not that hard to write multiple backends if he wanted.


Not really, for many optimization algorithms an IR like SSA is required, ASTs are not that good for that.

AST are better suitable for the frontend, and even then, they only became a thing after we started having tons of RAM available.


I suppose I should clarify, I would consider a basic AST the sane choice when talking about creating toy compilers as a learning experience - as opposed to creating a compiler that just reads in text and spits out assembly without any real intermediate representation. Which, there are legitimate compilers that do that (And as you mentioned, older compilers tended to work this way as well), but for learning I don't really think you get nearly as much out of doing it that way.

That said, if you want to do something more advanced or use or use something better then a basic AST, I agree go for it. But if you're actually writing the compiler for a project that isn't just a toy or as a learning experience, you're almost definitely better off just building a frontend on an existing platform like LLVM.


Ah, got it.


Someone still needs to turn that IR into actual machine code, and learning how it works is quite valuable.

Besides, IR has always been around, it is a requirement for many optimization algorithms.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: