Comment

Comment by mrkeen

I think the viewpoint that your're missing is: how do I make sure the machine code means the same thing as the source code?

Trying to one-shot 'print "hello world";' into

  .LC0:
        .string "hello"
  "square":
        push    rbp
        mov     rbp, rsp
  ...
is probably a non-starter, so compiler writers tend to attack the problem by breaking it down into lots of passes.

Parsing into an AST is obviously the first, then you might opt into other passes, like type-checking, handling closures, simplifying into some kind of IR, and finally generating code. Each of those stages can be effectively unit-tested (except maybe the last). If you make a mistake in parsing (e.g. mistaking a statement for an expression), you don't want to have to catch that error by inspecting machine code. It's nice if each pass can be easily tested. And one simple way to test these things is to write an interpreter for each pass (mostly because you get to enjoy the facilities of whatever host language you're writing in). Then you can start saying things like "for all inputs, interpreting the AST gives the same result as interpreting the type-checked-AST, gives the same results as interpreting the closured-AST, gives the same results as interpreting the list of IR instructions."

You mention "It ought to be easy because it is possible to only target the common subset of features these architectures support." <- That's your IR right there!

Once you have an IR that looks close enough to the target machine language, it shouldn't be too hard to translate the final step. Or just use LLVM.

I noticed in your code that you're using C, and really there's no need. Languages don't need to be stacked in a hierarchy (e.g. Clojure on top of the JVM which is C++ which compiles to asm). The compiler can be any language, because it only needs to be able to take in source code and spit out bytes.

parent mrkeen 6 days ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton