01.03.2015 Views

Modern compiler design [PDF]

Modern compiler design [PDF]

Modern compiler design [PDF]

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8<br />

Generating Abstract Assembly in C<br />

Chapter Overview<br />

8.1 Creating Abstract Assembly Trees in C<br />

8.1.1 Assembly Language Labels<br />

8.1.2 Interface for Building Abstract Assembly Trees in C<br />

8.1.3 Adding Code to the Semantic Analyzer to Build Abstract Assembly Trees<br />

8.1.4 Functions That Return Abstract Assembly Trees<br />

8.1.5 Variables<br />

8.1.6 Function Prototypes and Function Definitions<br />

8.2 Building Assembly Trees for simpleJava in C<br />

8.2.1 Project Definition<br />

8.2.2 Project Difficulty<br />

8.2.3 Project “Gotcha’s”<br />

8.2.4 Provided Files<br />

8.1 Creating Abstract Assembly Trees in C<br />

To build abstract assembly trees in C, we will first implement an interface that builds assembly trees for<br />

simpleJava statements and expressions, using abstract assembly subtrees. We will then add calls to the<br />

functions defined in this interface to the semantic analyzer, so that the semantic analyzer will build abstract<br />

assembly trees as well as check for semantic errors.<br />

8.1.1 Assembly Language Labels<br />

We will need to generate assembly language labels when building abstract assembly – both for function calls<br />

and for implementing if statements and loops. The file label.h in Figure 8.1 contains prototypes for functions<br />

which allow us to generate both unique labels (such as ifEnd001, ifEnd002, etc), and specific labels (for calls<br />

to library functions, when we know the exact name of the label). The first call to newLabel("foo") returns<br />

the label foo001, the second call returns the label foo002, and so on.<br />

8.1.2 Interface for Building Abstract Assembly Trees in C<br />

The interface for building abstract assembly trees is in Figure 8.2. We will now see some examples of using<br />

these functions to build abstract assembly trees for various statements.<br />

Consider the following simpleJava program:<br />

void foo(int a) {<br />

int b;<br />

int c;<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!