0

I tried to build a small compiler and I finished Lexer and Parser. And now I need to create Assembly Code Generator part of that. So i have some problems!

Actually I can C well and I learned NASM Assembly for this task. Now i can make some programs using NASM. But I can't understand how to implement assembly for a compiler, I mean assembly generation part.

  • Do I need to be an Assembly Language proficient for that?
  • Or are there tutorials for that? ( Compiler Development <-> Implement Assembly )

Thank You ^-^

1 Answers1

2

The next step depends on the design goals, the current format of the program being compiled, and the original language.

Immediately after parsing, the format of the program being compiled is probably some kind of abstract syntax tree and not much else. If that's the case, then you'll want to generate tables for various purposes - e.g. maybe a type table to keep track of the name and the definition of any user defined types (and structures and classes?); and a symbol table to keep track of the names, qualifiers ("const", "final", "volatile", whatever) and types of functions/methods and variables.

Note that this can be complicated by a few languages features - name spaces (where you want "fully qualified names" in the symbol table not just the short name) and any overloading (e.g. function overloading, where entries in the symbol table would need to be adorned with information, like input and output parameter types, needed to differentiate between different entries with the same name).

Once you have the relevant tables; you could probably generate assembly language (or better, raw machine code) directly from the "abstract syntax tree + tables". More often you'd optimize and simplify the contents of the abstract syntax tree, then convert the abstract syntax tree into another intermediate form (e.g. static single assignment) so it can be optimized a lot more, then convert into another lower level intermediate form that's closer to the target machine's instructions, then do more optimization (register allocation, peep-hole, instruction scheduling), then convert that into the final assembly language (or raw machine code).

Do I need to be an Assembly Language proficient for that?

You need a basic understanding of assembly language (even just to visually check that the compiler's output is a correct translation of the original source code). Depending on how well you want to optimize and how you plan to optimize, you might or might not need to be very proficient at assembly language.

Or are there tutorials for that?

There's always a tutorial (e.g. dodgy nonsense slapped together by a clueless person and/or basic introductory info).

I don't know if there's a good tutorial, but I doubt that it's possible. There's far too much information needed to cover everything (all the different source languages and language features, all the different destination languages, all the different techniques that are possible, etc); so any "good tutorial" would be so large that it'd become full blown book/s (and not a tutorial).

Brendan
  • 35,656
  • 2
  • 39
  • 66