Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resources on converting syntax tree to assembly?

Primarily as a learning exercise, I am writing a virtual machine, an assembler, and a compiler from scratch, depending on no external tools.

I believe I have a decent conceptual understanding of how the virtual machine and assembler will work, as well as some parts of the compiler.

Here's what I want to know: In the compiler, suppose I have turned the source code into a syntax tree. What process do I go through to then convert this syntax tree to assembly?

(Let's assume some simple language constructs, like if and while. I'm looking for a minimal and simple explanation here.)

I am not particularly interested in complex solutions, or solutions based on existing tools. Rather, I'd like something on the order of a 1-page, broad sweeping description of the ideas behind going from syntax tree to assembly.

Anyone know of such a resource?

Thanks :)

like image 321
MikeC8 Avatar asked Feb 08 '11 20:02

MikeC8


1 Answers

The obligatory response to a compiler question is to read the Dragon book (Compilers: Principles, Techniques and Tools). When you say that you have turned the source code into a syntax tree, what exactly do you mean? Usually the first stage in parsing is to create an abstract syntax tree (AST). The next step is usually to-do attribution. Attributes are properties of nodes in the AST that don't necessarily have anything to do with the source language, but are essential to code generation. Usually some form of type checking is done here to determine memory size requirements and, in object oriented languages, what function is to be called. For instance, if your source is obj1=obj2+obj3, You don't really know what to make of the plus sign until you determine the type of obj2.

So to give a shot at answering your question. 1) Parse source code to AST. 2) Do attribution on the AST. 3) Generate intermediate code (what imagine you are referring to as assembly).

Chapters 5 and 6 of the Dragon book cover this in good detail. Really the tricky part is figuring out what attributes you need for code generation. Also, there are some tricky issues with if statements. For instance if the if condition fails you know you need to jump over some code but, at least initially, you don't know how far. Back patching is one solution to this problem.

like image 52
Samsdram Avatar answered Oct 10 '22 09:10

Samsdram