CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Size: px
Start display at page:

Download "CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction"

Transcription

1 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

2 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

3 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

4 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

5 Today s Goals Intermediate representations Procedure Abstraction CS406/534 Fall 2004, Prof. Li Xu 5 5

6 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

7 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

8 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

9 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

10 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 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

11 Level of Abstraction Structural IRs are usually considered high-level Linear IRs are usually considered low-level Not necessarily true: load Low level AST + loadarray A,i,j High level linear code * i 1 j 1 CS406/534 Fall 2004, Prof. Li Xu 11 11

12 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

13 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

14 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

15 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

16 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 Y 2 2 X RISC assembly code Quadruples CS406/534 Fall 2004, Prof. Li Xu 16 16

17 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

18 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

19 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 y 2 y 1 + x 2 if(x 2 < k) goto loop next: CS406/534 Fall 2004, Prof. Li Xu 19 19

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

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 CS406/534 Fall 2004, Prof. Li Xu 32 32

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);... } CS406/534 Fall 2004, Prof. Li Xu 33 33

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 34 34

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 35 35

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; } CS406/534 Fall 2004, Prof. Li Xu 36 36

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 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

57 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

58 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

59 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

60 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

61 Summary Intermediate Representation Graph-based Linear code hybrid Procedure Abstraction Control abstraction Name space Interface CS406/534 Fall 2004, Prof. Li Xu 61 61

62 Next Class Procedure Abstraction, Part 2 CS406/534 Fall 2004, Prof. Li Xu 62 62

Optimizer. Defining and preserving the meaning of the program

Optimizer. Defining and preserving the meaning of the program Where are we? Well understood Engineering Source Code Front End IR Optimizer IR Back End Machine code Errors The latter half of a compiler contains more open problems, more challenges, and more gray areas

More information

The Procedure Abstraction Part I: Basics

The Procedure Abstraction Part I: Basics The Procedure Abstraction Part I: Basics Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing

More information

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Procedure Abstractions These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Where are we? Well understood Engineering Source Code

More information

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

The Procedure Abstraction

The Procedure Abstraction The Procedure Abstraction Procedure Abstraction Begins Chapter 6 in EAC The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures

More information

Intermediate Representations

Intermediate Representations Intermediate Representations Intermediate Representations (EaC Chaper 5) Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end

More information

The Procedure Abstraction Part I Basics

The Procedure Abstraction Part I Basics The Procedure Abstraction Part I Basics Procedure Abstraction The compiler must deal with interface between compile time and run time Most of the tricky issues arise in implementing procedures Procedures

More information

CS415 Compilers. Intermediate Represeation & Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation CS415 Compilers Intermediate Represeation & Code Generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review - Types of Intermediate Representations

More information

Intermediate Representations

Intermediate Representations Most of the material in this lecture comes from Chapter 5 of EaC2 Intermediate Representations Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP

More information

Intermediate Representations

Intermediate Representations COMP 506 Rice University Spring 2018 Intermediate Representations source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

Intermediate Representations

Intermediate Representations Intermediate Representations Where In The Course Are We? Rest of the course: compiler writer needs to choose among alternatives Choices affect the quality of compiled code time to compile There may be

More information

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End COMP 412 FALL 2017 Procedure and Function Calls, Part II Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access Run Time Environment Activation Records Procedure Linkage Name Translation and Variable Access Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University

More information

Intermediate Representations Part II

Intermediate Representations Part II Intermediate Representations Part II Types of Intermediate Representations Three major categories Structural Linear Hybrid Directed Acyclic Graph A directed acyclic graph (DAG) is an AST with a unique

More information

Naming in OOLs and Storage Layout Comp 412

Naming in OOLs and Storage Layout Comp 412 COMP 412 FALL 2018 Naming in OOLs and Storage Layout Comp 412 source IR IR target Front End Optimizer Back End Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together 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

More information

Runtime Support for Algol-Like Languages Comp 412

Runtime Support for Algol-Like Languages Comp 412 COMP 412 FALL 2018 Runtime Support for Algol-Like Languages Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into Intermediate representations source code front end ir opt. ir back end target code front end produce an intermediate representation (IR) for the program. optimizer transforms the code in IR form into an

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

More information

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction Procedure Abstraction Run Time Environment Records Procedure Linkage Name Translation and Variable Access Copyright 2010, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at

More information

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature:

More information

Intermediate Representation (IR)

Intermediate Representation (IR) Intermediate Representation (IR) Components and Design Goals for an IR IR encodes all knowledge the compiler has derived about source program. Simple compiler structure source code More typical compiler

More information

Procedure and Object- Oriented Abstraction

Procedure and Object- Oriented Abstraction Procedure and Object- Oriented Abstraction Scope and storage management cs5363 1 Procedure abstractions Procedures are fundamental programming abstractions They are used to support dynamically nested blocks

More information

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables

CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables CS415 Compilers Context-Sensitive Analysis Type checking Symbol tables These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 18 1 Announcements

More information

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

CS 314 Principles of Programming Languages. Lecture 13

CS 314 Principles of Programming Languages. Lecture 13 CS 314 Principles of Programming Languages Lecture 13 Zheng Zhang Department of Computer Science Rutgers University Wednesday 19 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

CSE 504: Compiler Design. Runtime Environments

CSE 504: Compiler Design. Runtime Environments Runtime Environments Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Procedure Abstractions Mechanisms to manage procedures and procedure calls from compiler s perspective Runtime Environment Choices

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target. COMP 412 FALL 2017 Generating Code for Assignment Statements back to work Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

More information

Arrays and Functions

Arrays and Functions COMP 506 Rice University Spring 2018 Arrays and Functions source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

Code Shape II Expressions & Assignment

Code Shape II Expressions & Assignment Code Shape II Expressions & Assignment Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make

More information

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Register Allocation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: The Back End IR Instruction Selection IR Register

More information

The Software Stack: From Assembly Language to Machine Code

The Software Stack: From Assembly Language to Machine Code COMP 506 Rice University Spring 2018 The Software Stack: From Assembly Language to Machine Code source code IR Front End Optimizer Back End IR target code Somewhere Out Here Copyright 2018, Keith D. Cooper

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Intermediate Representations Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 G-1 Agenda Survey of Intermediate Representations Graphical Concrete/Abstract Syntax Trees (ASTs)

More information

Front End. Hwansoo Han

Front End. Hwansoo Han Front nd Hwansoo Han Traditional Two-pass Compiler Source code Front nd IR Back nd Machine code rrors High level functions Recognize legal program, generate correct code (OS & linker can accept) Manage

More information

Compiler Design. Code Shaping. Hwansoo Han

Compiler Design. Code Shaping. Hwansoo Han Compiler Design Code Shaping Hwansoo Han Code Shape Definition All those nebulous properties of the code that impact performance & code quality Includes code, approach for different constructs, cost, storage

More information

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V. Runtime management CS3300 - Compiler Design Runtime management V Krishna Nandivada IIT Madras Copyright c 2001 by Antony L Hosking Permission to make digital or hard copies of part or all of this work

More information

CS /534 Compiler Construction University of Massachusetts Lowell

CS /534 Compiler Construction University of Massachusetts Lowell CS 91.406/534 Compiler Construction University of Massachusetts Lowell Professor Li Xu Fall 2004 Lab Project 2: Parser and Type Checker for NOTHING Due: Sunday, November 14, 2004, 11:59 PM 1 Introduction

More information

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask? Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the

More information

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13 Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1 What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down,

More information

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 COMP 412 FALL 2017 Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412 source IR Front End Optimizer Back End IR target Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

More information

Instruction Selection: Preliminaries. Comp 412

Instruction Selection: Preliminaries. Comp 412 COMP 412 FALL 2017 Instruction Selection: Preliminaries Comp 412 source code Front End Optimizer Back End target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

What about Object-Oriented Languages?

What about Object-Oriented Languages? What about Object-Oriented Languages? What is an OOL? A language that supports object-oriented programming How does an OOL differ from an ALL? (ALGOL-Like Language) Data-centric name scopes for values

More information

The View from 35,000 Feet

The View from 35,000 Feet The View from 35,000 Feet This lecture is taken directly from the Engineering a Compiler web site with only minor adaptations for EECS 6083 at University of Cincinnati Copyright 2003, Keith D. Cooper,

More information

Implementing Control Flow Constructs Comp 412

Implementing Control Flow Constructs Comp 412 COMP 412 FALL 2018 Implementing Control Flow Constructs Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 4 Thomas Wies New York University Review Last week Control Structures Selection Loops Adding Invariants Outline Subprograms Calling Sequences Parameter

More information

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:

More information

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code COMP 412 FALL 2017 Local Optimization: Value Numbering The Desert Island Optimization Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon,

More information

CS 406/534 Compiler Construction Parsing Part I

CS 406/534 Compiler Construction Parsing Part I CS 406/534 Compiler Construction Parsing Part I 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.

More information

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How? A Binding Question! Variables are bound (dynamically) to values Subprogram Activation! Those values must be stored somewhere! Therefore, variables must somehow be bound to memory locations! How? Function

More information

6. Intermediate Representation

6. Intermediate Representation 6. Intermediate Representation Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 13: Names, Scopes and Bindings Zheng (Eddy) Zhang Rutgers University February 28, 2018 Review: Names, Scopes and Binding What s in a name? Each name means

More information

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview COMP 181 Compilers Lecture 2 Overview September 7, 2006 Administrative Book? Hopefully: Compilers by Aho, Lam, Sethi, Ullman Mailing list Handouts? Programming assignments For next time, write a hello,

More information

Parsing II Top-down parsing. Comp 412

Parsing II Top-down parsing. Comp 412 COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

More information

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 4 Robert Grimm, New York University 1 Review Last week Control Structures Selection Loops 2 Outline Subprograms Calling Sequences Parameter Passing

More information

Compiler construction

Compiler construction Compiler construction Martin Steffen March 13, 2017 Contents 1 Abstract 1 1.1 Symbol tables. 1 1.1.1 Introduction 1 1.1.2 Symbol table design and interface.. 2 1.1.3 Implementing symbol tables 3 1.1.4

More information

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure

More information

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End COMP 412 FALL 2017 Code Shape Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target. COMP 412 FALL 2017 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

More information

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Traditional Three-pass Compiler

More information

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End COMP 412 FALL 2018 Code Shape Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at

More information

6. Intermediate Representation!

6. Intermediate Representation! 6. Intermediate Representation! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/!

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

CSE 504: Compiler Design. Intermediate Representations Symbol Table

CSE 504: Compiler Design. Intermediate Representations Symbol Table Intermediate Representations Symbol Table Pradipta De pradipta.de@sunykorea.ac.kr Current Topic Intermediate Representations Graphical IRs Linear IRs Symbol Table Information in a Program Compiler manages

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Symbol Tables Symbol Table: In computer science, a symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is

More information

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL 2018 Computing Inside The Parser Syntax-Directed Translation, II Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

More information

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation CS 91.406/534 Compiler Construction University of Massachusetts Lowell Professor Li Xu Fall 2004 NOTHING: A Language for Practice Implementation 1 Introduction NOTHING is a programming language designed

More information

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table SYMBOL TABLE: A symbol table is a data structure used by a language translator such as a compiler or interpreter, where each identifier in a program's source code is associated with information relating

More information

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target. COMP 412 FALL 20167 Computing Inside The Parser Syntax-Directed Translation, II Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL 2018 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

More information

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

Local Register Allocation (critical content for Lab 2) Comp 412

Local Register Allocation (critical content for Lab 2) Comp 412 Updated After Tutorial COMP 412 FALL 2018 Local Register Allocation (critical content for Lab 2) Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda

More information

Compilers. Lecture 2 Overview. (original slides by Sam

Compilers. Lecture 2 Overview. (original slides by Sam Compilers Lecture 2 Overview Yannis Smaragdakis, U. Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Last time The compilation problem Source language High-level abstractions Easy

More information

Handling Assignment Comp 412

Handling Assignment Comp 412 COMP 412 FALL 2018 Handling Assignment Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

More information

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

CS 406/534 Compiler Construction Instruction Scheduling

CS 406/534 Compiler Construction Instruction Scheduling CS 406/534 Compiler Construction Instruction Scheduling 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

More information

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Overview of the Course These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Critical Facts Welcome to CS415 Compilers Topics in the

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.) Chapter 3:: Names, Scopes, and Bindings (cont.) Programming Language Pragmatics Michael L. Scott Review What is a regular expression? What is a context-free grammar? What is BNF? What is a derivation?

More information

Compilers and Interpreters

Compilers and Interpreters Overview Roadmap Language Translators: Interpreters & Compilers Context of a compiler Phases of a compiler Compiler Construction tools Terminology How related to other CS Goals of a good compiler 1 Compilers

More information

Run-time Environments

Run-time Environments Run-time Environments Status We have so far covered the front-end phases Lexical analysis Parsing Semantic analysis Next come the back-end phases Code generation Optimization Register allocation Instruction

More information

Intermediate representation

Intermediate representation Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.

More information

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?

More information

Concepts Introduced in Chapter 7

Concepts Introduced in Chapter 7 Concepts Introduced in Chapter 7 Storage Allocation Strategies Static Stack Heap Activation Records Access to Nonlocal Names Access links followed by Fig. 7.1 EECS 665 Compiler Construction 1 Activation

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

5. Semantic Analysis!

5. Semantic Analysis! 5. Semantic Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!

More information

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on

More information

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Chapter 9 Subroutines and Control Abstraction. June 22, 2016 Chapter 9 Subroutines and Control Abstraction June 22, 2016 Stack layout Common to have subroutine activation record allocated on a stack Typical fare for a frame: arguments, return value, saved registers,

More information

Building a Parser Part III

Building a Parser Part III COMP 506 Rice University Spring 2018 Building a Parser Part III With Practical Application To Lab One source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda

More information

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.) Chapter 3:: Names, Scopes, and Bindings (cont.) Programming Language Pragmatics Michael L. Scott Review What is a regular expression? What is a context-free grammar? What is BNF? What is a derivation?

More information

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives: CPS311 Lecture: Procedures Last revised 9/9/13 Objectives: 1. To introduce general issues that any architecture must address in terms of calling/returning from procedures, passing parameters (including

More information

CS5363 Final Review. cs5363 1

CS5363 Final Review. cs5363 1 CS5363 Final Review cs5363 1 Programming language implementation Programming languages Tools for describing data and algorithms Instructing machines what to do Communicate between computers and programmers

More information