cs160-lec16-17

# cs160-lec16-17 - CMPSC 160 Translation of Programming...

This preview shows pages 1–10. Sign up to view the full content.

CMPSC 160 Translation of Programming Languages Lectures 16 and 17: Code Generation

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Code Generation Intermediate Representations There is more that one way to represent code as it is being generated, analyzed, and optimized (we use ASTs) How code runs The way code runs on a machine depends on if the code is compiled or interpreted, and if it is statically or dynamically linked Code Generation We will discuss code generation for three-address code and stack code We will discuss how to deal with Boolean values and control flow (such as loops and if-then-else statements)
Intermediate Representations Abstract Syntax Trees (AST) Directed Acyclic Graphs (DAG) Control Flow Graphs (CFG) Static Single Assignment Form (SSA) Stack Machine Code Three Address Code Hybrid approaches mix graphical and linear representations MIPS compilers use ASTs for loops if-statements and array references Use three-address code in basic blocks in control flow graphs high-level low-level Graphical IRs Linear IRs

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Abstract Syntax Trees (ASTs) if (x < y) x = 5*y + 5*y/3; else y = 5; x = x+y; Statements < AssignStmt + * x IfStmt AssignStmt AssignStmt x x y + y x y / 5 y 3 * 5 y 5
Directed Acyclic Graphs (DAGs) Use directed acyclic graphs to represent expressions Use a single node for identical subexpressions if (x < y) x = 5*y + 5*y/3; else y = 5; x = x+y; Statements < AssignStmt * IfStmt AssignStmt AssignStmt x + y / 5 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Control Flow Graphs (CFGs) Nodes in the control flow graph are basic blocks A basic block is a sequence of statements always entered at the beginning of the block and exited at the end Edges in the control flow graph represent the control flow if (x < y) x = 5*y + 5*y/3; else y = 5; x = x + y; if (x < y) goto B 1 else goto B 2 x = 5*y + 5*y/3 y = 5 x = x + y B 1 B 2 B 0 B 3 Each block has a sequence of statements No jump from or to the middle of the block Once a block starts executing, it will execute till the end
Code Generation To generate actual code that can run on a processor (such as gcc) or on a virtual machine (such as javac) we need to understand what code for each of these machines looks like. Rather than worry about the exact syntax of a given assembly language, we instead use a type of pseudo-assembly that is close to the underlying machine. In this class, we will focus on 2 different types of code Stack based code: Similar to the Java Virtual Machine Three-address code: Similar to most processors (x86, Sparc, ARM, …)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory address or a temporary We have to break large statements into little operations that use temporary variables x=(2+3)+4 turns into to t1=2+3; x=t1+4; Temporary variables store the results at the internal nodes in the AST
Three-Address Code Assignments – x := y – x := y op z op : binary arithmetic or logical operators

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/19/2012 for the course ENGR 361 taught by Professor Drexel during the Spring '12 term at Bloomsburg.

### Page1 / 42

cs160-lec16-17 - CMPSC 160 Translation of Programming...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online