DV1465 / DV1505 / DV1511:

Compiler and Interpreter Technology

08:15 Monday, April 4th, 2016

Building Intermediate Representations.

Table of Contents
Evaluation OrdersTranslating Statements
Translating ExpressionsBuilding the CFG.

1. Roadmap type stuff

2. Overview of translating expression trees


All of Chapter 5 describes the contraints for the global ordering process.
Figure 6.19 shows an example of the local rules.

3. What's in a name?

4. Names from trees

Key Observation
The order that we process nodes during translation is the same as during interpretation.

5. Evaluation order

Consequence of the key observation
When expressed as a tree walk the translation process is a form of interpreter

6. Evaluation order: Semantic Rules

7. Evaluation order: Trivial Actions

8. Evaluation order: Attribute Dependencies

9. Evaluation order: Dependency Constraints

9b. Code for the previous example

DeclNode *processDecl(ParseNode *node) { Type *t = processType(node->left); if(!validCombination(t)) throw("Bad tree"); IdList *ids = processIdList(node->right, t); return new DeclNode(ids); }

10. Evaluation order: Summary

11. Allocate names (addresses).

%3 x x add (_t1) + add->x mul (_t2) * add->mul y y mul->y z z mul->z
counter = 0 function names(node current) if current is a leaf current.output = variable name else current.output = "_t"+counter++ names(current.left) names(current.right) names(root)

12. Emit 3-address instructions

%3 x x add t1 <- x + t2 add->x mul t2 <- y * z add->mul y y mul->y z z mul->z
function process(node current) if current is not a leaf process(current.left) process(current.right) output current.name output "<-" output current.left.name output current.operator output current.right.name process(root)
t2 <- y * z t1 <- x + t2

13. Inherited Attributes

14. Constants

Break (15mins)





Intermission

15. Building the IR

16. Translating assignment statements

17. The other kinds of statements

18. Representation of basic blocks

class BBlock { std::list contents; public: BBlock *trueExit, *falseExit; int numExits; ... std::set< std::string > symbols(); std::string lastValueWritten(); ... };

19. Comparison to assembly language

20. Outline of statement processing

21. Translation of sequences

22. Translation of If-then-else

23. Translation of while loops

24. Example of de-sugaring

25. Nesting

26. Nesting

27. Final Thought