CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr. Linda Torczon s teaching materials at Rice University. All rights reserved. 1
Administravia HW1, Lab1, Midterm graded Sample solutions posted on the board Sample implementation for lab1 (thanks to Ben), including lab report Briefly walk through hw1 and midterm Need more effort from some CS406/534 Fall 2004, Prof. Li Xu 2 2
Today s Plan First half: Q&A on HW1, Midterm, Lab2 Second half: Intermediate Representation and Procedure Abstraction CS406/534 Fall 2004, Prof. Li Xu 3 3
What We Did Last Time Parsing wrap-up Build LR(1) tables Context-sensitive analysis Attribute grammar Ad-hoc syntax-directed translation CS406/534 Fall 2004, Prof. Li Xu 4 4
Today s Goals Intermediate representations Procedure Abstraction CS406/534 Fall 2004, Prof. Li Xu 5 5
Intermediate Representations Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end - transforms the IR into an equivalent IR that runs more efficiently Back end - transforms the IR into native code IR encodes the compiler s knowledge of the program Middle end usually consists of several passes CS406/534 Fall 2004, Prof. Li Xu 6 6
IR: Software Engineering Befefits Front Middle End Back End 7 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 7 Analysis
Intermediate Representations Decisions in IR design affect the speed and efficiency of the compiler Some important IR properties Ease of generation Ease of manipulation IR size Expressive power Level of abstraction The importance of different properties varies between compilers Selecting an appropriate IR for a compiler is critical CS406/534 Fall 2004, Prof. Li Xu 8 8
Types of IRs Three major categories Structural Graphically oriented Heavily used in source-to-source translators Tend to be large Linear Pseudo-code for an abstract machine Level of abstraction varies Simple, compact data structures Easier to rearrange Hybrid Combination of graphs and linear code Example: control-flow graph Examples: Trees, DAGs Examples: 3 address code Stack machine code Example: Control-flow graph CS406/534 Fall 2004, Prof. Li Xu 9 9
Level of Abstraction The level of detail exposed in an IR influences the profitability and feasibility of different optimizations. Two different representations of an array reference: subscript A i j High level AST: Good for memory disambiguation loadi 1 => r 1 sub r j,r 1 => r 2 loadi 10 => r 3 mult r 2,r 3 => r 4 sub r i,r 1 => r 5 add r 4,r 5 => r 6 loadi @ A => r 7 Add r 7,r 6 => r 8 load r 8 => r Aij Low level linear code: Good for address calculation CS406/534 Fall 2004, Prof. Li Xu 10 10
Level of Abstraction Structural IRs are usually considered high-level Linear IRs are usually considered low-level Not necessarily true: load Low level AST + + @A loadarray A,i,j High level linear code * - - 10 i 1 j 1 CS406/534 Fall 2004, Prof. Li Xu 11 11
Abstract Syntax Tree An abstract syntax tree is the parse tree with the nodes for most non-terminal nodes removed - x * 2 y x - 2 * y Can use linearized form of the tree Easier to manipulate than pointers x 2 y * - in postfix form -x*2 y in prefix form CS406/534 Fall 2004, Prof. Li Xu 12 12
Directed Acyclic Graph A directed acyclic graph (DAG) is an AST with a unique node for each value - z w / x * 2 y z x - 2 * y w x / 2 Makes sharing explicit Encodes redundancy Same expression twice means that the compiler might arrange to evaluate it just once! CS406/534 Fall 2004, Prof. Li Xu 13 13
Stack Machine Code Originally used for stack-based computers, now Java Example: x - 2 * y becomes Advantages Compact form Introduced names are implicit, not explicit Simple to generate and execute code push x push 2 push y multiply subtract Useful where code is transmitted over slow communication links (the net ) Implicit names take up no space, where explicit ones do! CS406/534 Fall 2004, Prof. Li Xu 14 14
3-Address Code Several different representations of three address code In general, three address code has statements of the form: x y op z With 1 operator (op ) and, at most, 3 names (x, y, & z) Example: z x - 2 * y becomes t 2 * y z x -t Advantages: Resembles many machines Introduces a new set of names Compact form * CS406/534 Fall 2004, Prof. Li Xu 15 15
3-Address Code: Quadruples Naïve representation of three address code Table of k * 4 small integers Simple record structure Easy to reorder Explicit names load r1, y loadi r2, 2 mult r3,r2,r1 load r4, x sub r5,r4,r3 load loadi mult load sub 1 2 3 4 5 Y 2 2 X 4 1 2 RISC assembly code Quadruples CS406/534 Fall 2004, Prof. Li Xu 16 16
3-Address Code: Triples Index used as implicit name 25% less space consumed than quads Much harder to reorder (1) load y (2) loadi 2 (3) mult (1) (2) (4) load x (5) sub (4) (3) Implicit names take no space! CS406/534 Fall 2004, Prof. Li Xu 17 17
3-Address Code: Indirect Triples List first triple in each statement Implicit name space Uses more space than triples, but easier to reorder (100) (100) load y (105) (101) loadi 2 (102) mult (100) (101) (103) load x (104) sub (103) (102) Major tradeoff between quads and triples is compactness versus ease of manipulation In the past compile-time space was critical Today, speed may be more important CS406/534 Fall 2004, Prof. Li Xu 18 18
Static Single Assignment Form The main idea: each name defined exactly once Introduce φ-functions to make it work Original SSA-form x y while (x < k) x x + 1 y y + x Strengths of SSA-form Sharper analysis φ-functions give hints about placement (sometimes) faster algorithms x 0 y 0 if(x 0 > k) goto next loop: x 1 φ(x 0,x 2 ) y 1 φ(y 0,y 2 ) x 2 x 1 + 1 y 2 y 1 + x 2 if(x 2 < k) goto loop next: CS406/534 Fall 2004, Prof. Li Xu 19 19
2-Address Code Allows statements of the form x x op y Has 1 operator (op ) and, at most, 2 names (x and y) Example: z x - 2 * y Can be very compact becomes t 1 2 t 2 load y t 2 t 2 *t 1 z load x z z -t 2 Problems Machines no longer rely on destructive operations Difficult name space Destructive operations make reuse hard For machines with destructive ops (PDP-11) CS406/534 Fall 2004, Prof. Li Xu 20 20
Control-flow Graph Models the transfer of control in the procedure Nodes in the graph are basic blocks Can be represented with quads or any other linear representation Edges in the graph represent control flow Example a 2 b 5 if (x = y) a 3 b 4 Basic blocks Maximal length sequences of straight-line code c a * b CS406/534 Fall 2004, Prof. Li Xu 21 21
Using Multiple Representations Source Code Front End IR 1 Middle IR 2 Middle IR 3 End End Back End Target Code Repeatedly lower the level of the intermediate representation Each intermediate representation is suited towards certain optimizations Example: the Open64 compiler WHIRL intermediate format Consists of 5 different IRs that are progressively more detailed CS406/534 Fall 2004, Prof. Li Xu 22 22
Two major models Register-to-register model Memory Models Keep all values that can legally be stored in a register in registers Ignore machine limitations on number of registers Compiler back-end must insert loads and stores Memory-to-memory model Keep all values in memory Only promote values to registers directly before they are used Compiler back-end can remove loads and stores Compilers for RISC machines usually use register-toregister Reflects programming model Easier to determine when registers are used CS406/534 Fall 2004, Prof. Li Xu 23 23
The Rest of the Story Representing the code is only part of an IR There are other necessary components Symbol table Constant table Representation, type Storage class, offset Storage map Overall storage layout Overlap information Virtual register assignments CS406/534 Fall 2004, Prof. Li Xu 24 24
Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time (static versus dynamic) Most of the tricky issues arise in implementing procedures Issues Compile-time versus run-time behavior Finding storage for EVERYTHING, and mapping names to addresses Generating code to compute addresses that the compiler cannot know! Interfaces with other programs, other languages, and the OS Efficiency of implementation CS406/534 Fall 2004, Prof. Li Xu 25 25
Where are we? Well understood Engineering Source Code Front End IR Middle End IR Back End Machine code Errors The latter half of a compiler contains more open problems, more challenges, and more gray areas than the front half This is compilation, as opposed to parsing or translation Implementing promised behavior What defines the meaning of the program Managing target machine resources Registers, memory, issue slots, locality, power, These issues determine the quality of the compiler CS406/534 Fall 2004, Prof. Li Xu 26 26
Procedure: Three Abstractions Control Abstraction Well defined entries & exits Mechanism to return control to caller Some notion of parameterization (usually) Clean Name Space Clean slate for writing locally visible names Local names may obscure identical, non-local names Local names cannot be seen outside External Interface Access is by procedure name & parameters Clear protection for both caller & callee Invoked procedure can ignore calling context Procedures permit a critical separation of concerns CS406/534 Fall 2004, Prof. Li Xu 27 27
The Procedure Procedures are the key to building large systems Requires system-wide support Conventions on memory layout, protection, resource allocation calling sequences, & error handling Must involve architecture (ISA), OS, & compiler Provides shared access to system-wide facilities Storage management, flow of control, interrupts Interface to input/output devices, protection facilities, timers, synchronization flags, counters, Establishes a private context Create private storage for each procedure invocation Encapsulate information about control flow & data abstractions CS406/534 Fall 2004, Prof. Li Xu 28 28
The Procedure Procedures allow us to use separate compilation Separate compilation allows us to build non-trivial programs Keeps compile times reasonable Lets multiple programmers collaborate Requires independent procedures Without separate compilation, we would not build large systems The procedure linkage convention Ensures that each procedure inherits a valid run-time environment and that the callers environment is restored on return The compiler must generate code to ensure this happens according to conventions established by the system CS406/534 Fall 2004, Prof. Li Xu 29 29
The Procedure A procedure is an abstract structure constructed via software Underlying hardware directly supports little of the abstraction it understands bits, bytes, integers, reals, and addresses, but not: Entries and exits Interfaces Call and return mechanisms may be a special instruction to save context at point of call Name space Nested scopes All these are established by a carefully-crafted system of mechanisms provided by compiler, run-time system, linkage editor and loader, and OS CS406/534 Fall 2004, Prof. Li Xu 30 30
Run Time versus Compile Time These concepts are often confusing to the newcomer Linkages execute at run time Code for the linkage is emitted at compile time The linkage is designed long before either of these This issue (compile time versus run time) confuses students more than any other issue in Comp 412 Keith Cooper CS406/534 Fall 2004, Prof. Li Xu 31 31
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation CS406/534 Fall 2004, Prof. Li Xu 32 32
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } CS406/534 Fall 2004, Prof. Li Xu 33 33
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } CS406/534 Fall 2004, Prof. Li Xu 34 34
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } CS406/534 Fall 2004, Prof. Li Xu 35 35
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } CS406/534 Fall 2004, Prof. Li Xu 36 36
Procedure as a Control Abstraction Procedures have well-defined control-flow The Algol-60 procedure call Invoked at a call site, with some set of actual parameters Control returns to call site, immediately after invocation s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } Most languages allow recursion CS406/534 Fall 2004, Prof. Li Xu 37 37
Procedure as a Control Abstraction Implementing procedures with this behavior Requires code to save and restore a return address Must map actual parameters to formal parameters (c x, b y) Must create storage for local variables (&, maybe, parameters) p needs space for d (&, maybe, a, b, & c) where does this space go in recursive invocations? s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } Compiler emits code that causes all this to happen at run time CS406/534 Fall 2004, Prof. Li Xu 38 38
Procedure as a Control Abstraction Implementing procedures with this behavior Must preserve p s state while q executes recursion causes the real problem here Strategy: Create unique location for each procedure activation Can use a stack of memory blocks to hold local storage and return addresses s = p(10,t,u); int p(a,b,c) int a, b, c; { int d; d = q(c,b);... } int q(x,y) int x,y; { return x + y; } Compiler emits code that causes all this to happen at run time CS406/534 Fall 2004, Prof. Li Xu 39 39
Procedure as a Name Space Each procedure creates its own name space Any name (almost) can be declared locally Local names obscure identical non-local names Local names cannot be seen outside the procedure Nested procedures are inside by definition We call this set of rules & conventions lexical scoping Examples C has global, static, local, and block scopes (Fortran-like) Blocks can be nested, procedures cannot Scheme has global, procedure-wide, and nested scopes (let) Procedure scope (typically) contains formal parameters CS406/534 Fall 2004, Prof. Li Xu 40 40
Procedure as a Name Space Why introduce lexical scoping? Provides a compile-time mechanism for binding free variables Simplifies rules for naming & resolves conflicts How can the compiler keep track of all those names? The Problem At point p, which declaration of x is current? At run-time, where is x found? As parser goes in & out of scopes, how does it delete x? The Answer Lexically scoped symbol tables (see 5.7.3) CS406/534 Fall 2004, Prof. Li Xu 41 41
Do People Use This Stuff? C macro from the MSCP compiler #define fix_inequality(oper, new_opcode) \ if (value0 < value1) \ { \ Unsigned_Int temp = value0; \ value0 = value1; \ value1 = temp; \ opcode_name = new_opcode; \ temp = oper->arguments[0]; \ oper->arguments[0] = oper->arguments[1]; \ oper->arguments[1] = temp; \ oper->opcode = new_opcode; \ } Declares a new name CS406/534 Fall 2004, Prof. Li Xu 42 42
Do People Use This Stuff? C code from the MSCP implementation static Void phi_node_printer(block *block) { Phi_Node *phi_node; Block_ForAllPhiNodes(phi_node, block) { if (phi_node->old_name < register_count) { Unsigned_Int i; } fprintf(stderr, "Phi node for r%d: [", phi_node->old_name); for (i = 0; i < block->pred_count; i++) fprintf(stderr, " r%d", phi_node->parms[i]); fprintf(stderr, " ] => r%d\n", phi_node->new_name); } else { Unsigned_Int2 *arg_ptr; fprintf(stderr, "Phi node for %s: [", Expr_Get_String(Tag_Unmap( phi_node->old_name))); Phi_Node_ForAllParms(arg_ptr, phi_node) fprintf(stderr, " %d", *arg_ptr); fprintf(stderr, " ] => %d\n", phi_node->new_name); CS406/534 Fall 2004, Prof. Li Xu 43 } } More local declarations! 43
Lexically-scoped Symbol Tables The problem The compiler needs a distinct record for each declaration Nested lexical scopes admit duplicate declarations The interface insert(name, level ) creates record for name at level lookup(name, level ) returns pointer or index delete(level ) removes all names declared at level Many implementation schemes have been proposed (see B.4) We ll stay at the conceptual level Hash table implementation is tricky, detailed, & fun Symbol tables are compile-time structures the compiler use to resolve references to names. We ll see the corresponding run-time structures that are used to establish addressability later. CS406/534 Fall 2004, Prof. Li Xu 44 44
Example procedure p { int a, b, c procedure q { int v, b, x, w procedure r { int x, y, z. } procedure s { int x, a, v } r s } q } B0: { int a, b, c B1: { int v, b, x, w B2: { int x, y, z. } B3: { int x, a, v } } } CS406/534 Fall 2004, Prof. Li Xu 45 45
Lexically-scoped Symbol Tables High-level idea Create a new table for each scope Chain them together for lookup r x y q v b x w... p a b... c Sheaf of tables implementation insert() may need to create table it always inserts at current level lookup() walks chain of tables & returns first occurrence of name delete() throws away table for level p, if it is top table in the chain If the compiler must preserve the table (for, say, the debugger ), this idea is actually practical. z Individual tables can be hash tables. CS406/534 Fall 2004, Prof. Li Xu 46 46
Implementing Scoped Tables Stack organization nextfree r (level 2) q (level 1) p (level 0) z y x w x b v c b a growth Implementation insert () creates new level pointer if needed and inserts at nextfree lookup () searches linearly from nextfree 1 forward delete () sets nextfree to the equal the start location of the level deleted. Advantage Uses much less space Disadvantage Lookups can be expensive CS406/534 Fall 2004, Prof. Li Xu 47 47
Implementing Scoped Tables Threaded stack organization h(x) z y x w x b v c b a growth r q p Implementation insert () puts new entry at the head of the list for the name lookup () goes direct to location delete () processes each element in level being deleted to remove from head of list Advantage lookup is fast Disadvantage delete takes time proportional to number of declared variables in level CS406/534 Fall 2004, Prof. Li Xu 48 48
Procedure as an Interface OS needs a way to start the program s execution Programmer needs a way to indicate where it begins The main procedure in most languaages When user invokes grep at a command line OS finds the executable OS creates a process and arranges for it to run grep grep is code from the compiler, linked with run-time system Starts the run-time environment & calls main After main, it shuts down run-time environment & returns When grep needs system services It makes a system call, such as fopen() CS406/534 Fall 2004, Prof. Li Xu 49 49
Where Do All These Variables Go? Automatic & Local Keep them in the procedure activation record or in a register Automatic lifetime matches procedure s lifetime Static Procedure scope storage area affixed with procedure name &_p.x File scope storage area affixed with file name Lifetime is entire execution Global One or more named global data areas One per variable, or per file, or per program, Lifetime is entire execution CS406/534 Fall 2004, Prof. Li Xu 50 50
Placing Run-time Data Structures Classic Organization C o d e S G t l a & o t b i a c l H e a p 0 high Single Logical Address Space S t a c k Better utilization if stack & heap grow toward each other Very old result (Knuth) Code & data separate or interleaved Uses address space, not allocated memory Code, static, & global data have known size Use symbolic labels in the code Heap & stack both grow & shrink over time This is a virtual address space CS406/534 Fall 2004, Prof. Li Xu 51 51
How Does This Really Work? The Big Picture Compiler s view virtual address spaces C o d e S G t l a & o t b i a c l H e a p S t a c k C o d e S G t l a & o t b i a c l H e a p S t a c k C o d e S G t l a & o t b i a c l H e a p S t a c k... C o d e S G t l a & o t b i a c l H e a p S t a c k... 0 high Hardware s view Physical address space_ CS406/534 Fall 2004, Prof. Li Xu 52 52
Where Do Local Variables Live? A Simplistic model Allocate a data area for each distinct scope One data area per sheaf in scoped table What about recursion? Need a data area per invocation (or activation) of a scope We call this the scope s activation record The compiler can also store control information there! More complex scheme One activation record (AR) per procedure instance All the procedure s scopes share a single AR (may share space) Static relationship between scopes in single procedure Used this way, static means knowable at compile time (and, therefore, fixed). CS406/534 Fall 2004, Prof. Li Xu 53 53
Translating Local Names How does the compiler represent a specific instance of x? Name is translated into a static coordinate < level,offset > pair level is lexical nesting level of the procedure offset is unique within that scope Subsequent code will use the static coordinate to generate addresses and references level is a function of the table in which x is found Stored in the entry for each x offset must be assigned and stored in the symbol table Assigned at compile time Known at compile time Used to generate code that executes at run-time CS406/534 Fall 2004, Prof. Li Xu 54 54
Storage for Single Procedure B0: { int a, b, c B1: { int v, b, x, w B2: { int x, y, z. } B3: { int x, a, v } } } Fixed length data can always be at a constant offset from the beginning of a procedure In our example, the a declared at level 0 will always be the first data element, stored at byte 0 in the fixed-length data area The x declared at level 1 will always be the sixth data item, stored at byte 20 in the fixed data area The x declared at level 2 will always be the eighth data item, stored at byte 28 in the fixed data area But what about the a declared in the second block at level 2? CS406/534 Fall 2004, Prof. Li Xu 55 55
B0: { int a, b assign value to a B1: { int v(a), b, x B2: { int x, y(8). } Variable-length Data Arrays If size is fixed at compile time, store in fixed-length data area If size is variable, store descriptor in fixed length area, with pointer to variable length area Variable-length data area is assigned at the end of the fixed length area for block in which it is allocated a b v b x x y(8) v(a) Includes variable length data for Variable-length data all blocks in the procedure CS406/534 Fall 2004, Prof. Li Xu 56 56
Activation Record Basics ARP parameters register save area return value return address addressability caller s ARP local variables Space for parameters to the current routine Saved register contents If function, space for return value Address to resume caller Help with non-local access To restore caller s AR on a return Space for local values & variables (including spills) One AR for each invocation of a procedure CS406/534 Fall 2004, Prof. Li Xu 57 57
Activation Record Details How does the compiler find the variables? They are at known offsets from the AR pointer The static coordinate leads to a loadai operation Level specifies an ARP, offset is the constant Variable-length data If AR can be extended, put it below local variables Leave a pointer at a known offset from ARP Otherwise, put variable-length data on the heap Initializing local variables Must generate explicit code to store the values Among the procedure s first actions CS406/534 Fall 2004, Prof. Li Xu 58 58
Activation Record Details Where do activation records live? If lifetime of AR matches lifetime of invocation, AND If code normally executes a return Keep ARs on a stack If a procedure can outlive its caller, OR If it can return an object that can reference its execution state ARs mustbe kept in the heap C o d e S G t l a & o t b i a c l H e a p S t a c k If a procedure makes no calls AR can be allocated statically Efficiency prefers static, stack, then heap CS406/534 Fall 2004, Prof. Li Xu 59 59
Communicate Between Procedures Most languages provide a parameter passing mechanism Expression used at call site becomes variable in callee Two common binding mechanisms Call-by-reference passes a pointer to actual parameter Requires slot in the AR (for address of parameter) Multiple names with the same address? Call-by-value passes a copy of its value at time of call Requires slot in the AR Each name gets a unique location (may have same value) Arrays are mostly passed by reference, not value Can always use global variables CS406/534 Fall 2004, Prof. Li Xu 60 60
Summary Intermediate Representation Graph-based Linear code hybrid Procedure Abstraction Control abstraction Name space Interface CS406/534 Fall 2004, Prof. Li Xu 61 61
Next Class Procedure Abstraction, Part 2 CS406/534 Fall 2004, Prof. Li Xu 62 62