240 likes | 378 Views
This lecture explores the fundamentals of code generation in compiler construction, focusing on basic blocks and flow graphs. Key topics include defining basic blocks, identifying leaders, analyzing control flow, and optimizing code through techniques such as common subexpression elimination and dead-code elimination. The lecture also discusses the implications of target architectures (CISC vs. RISC), instruction selection, and memory management. Students will gain insights into the algorithmic approaches required for efficient code generation and the structure of quad tuples in programming.
E N D
Lecture 23 Basic Blocks CSCE 531 Compiler Construction • Topics • Code Generation • Readings: 9 April 17, 2006
Overview • Last Time – Lec22 slides 1-14, 15-16 • Finishing touches on Arrays in expressions • Project 5 • Today’s Lecture • Questions on Project 5 – Functions • Code Generation • References: Chapter 9
Code Generation • Chapter 9 • Issues in Code Generation • Input to code generator • Target programs • Memory management • Instruction selection • Register allocation
Input to code generator • Quadruples • Triples • Indirect triples • Parse trees • Syntax trees • Mixtures – some trees some quads
Target programs • Target Architectures • CISC • RISC • Target Architectures • Object modules • Assembly
Target Machine Architecture • RISC vs CISC • Byte addressable, word addressable? • Byte order? Big Endian vs little • Address Modes supported by architecture • Absolute • Register • Indexed d(R) d + contents(R) • Indirect register *d(R) contents(d + contents(R)) • Cost of address modes in references to memory
Instruction Costs • Floating point costs • FPadd = 2 time units • FPmultiply = 5 time units • FPdivide=9 time units • Page 521 – costs measured in instruction length (somewhat dated, but simple to understand) • mov R0, R1 - cost = 1 • mov R5, mem - cost = 2 • add $1, r3 - cost = 2 • sub 4(R0), *12(R1) - cost =3
Instruction Costs of Blocks: a = b + c • mov b, R0 • add c, R0 • mov R0, a
Heap • Really Operating System Problem not Compilers job • System Calls • ptr = malloc(size) • free(ptr) • Free space list • What functionality the compiler needs to supply? • Just pointers, function calls
Basic Blocks and Flow Graphs • To generate better code, we will need to analyze the structure of code written as list of quadruples • A Basic Block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without possibility of branching except at the end • Define/Use: a:= b + c • This statement defines “a” • It uses “b” and “c” • A name (identifier) is said to be live at a given point if its value is after that point in the program
Leaders of the Block • A leader is the first statement of a basic block. • The algorithm for Basic Blocks • Determine the leaders first. • The first statement is a leader. • Any statement that is the target of a branch is a leader. • Any statement immediately following a branch is a leader. • For each leader the block extends from the leader up to the statement just prior to the next leader.
Transformations on Basic Blocks • Common subexpression elimination • Dead-code elimination • Renaming temporary variables • Reordering independent statements
Flow Graphs • Successor block • Predecessor block • Loops • Strongly connected • Inner loop
Computing Next Uses • Scan block backwards from last statement to leader • For statement (i) x := y op z • Attach to statement I, current info from symbol table on the liveness of x y and z • In the symbol table Mark x “not live” = “no next use” • In the symbol table change next uses of y and z to (i) “statement number i”