cs160-lec16-17

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

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

View Full Document Right Arrow Icon
CMPSC 160 Translation of Programming Languages Lectures 16 and 17: Code Generation
Background image of page 1

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

View Full DocumentRight Arrow Icon
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)
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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, …)
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
Three-Address Code Assignments – x := y – x := y op z op : binary arithmetic or logical operators
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online