COMP 181

Lecture 1 The view from 35000 feet

Monday, Sept. 12, 2005

Introduction

 What is this artifact? The Rosetta Stone

 Significance? Same document in Greek and Egyptian hieroglyphics

 Am I in the wrong class? No, compilers are translators

Tufts University Computer Science 2

1 Language translation

 How does translation work?

Meaning

Semantics Semantics

Syntax Syntax

High-level language Assembly/machine code

Tufts University Computer Science 3

Compiler overview

Responsibilities :  Recognize legal programs  Generate correct code  Manage hardware resources  Registers  Memory  Cooperate with OS  System calls  Object code, virtual memory layout

Tufts University Computer Science 4

2 Traditional Two-pass

Source Front IR Back Machine code End End code

Errors Implications  Use an intermediate representation ( IR )  Front end maps legal source code into IR  Back end maps IR into target machine code  Admits multiple front ends & multiple passes (better code ) Typically, front end is O(n) or O(n log n), while back end is NPC

Tufts University Computer Science 5

A Common Fallacy

Front Back Fortran Target 1 end end Front Scheme end Back Target 2 end Front Java end

Front Back Target 3 Smalltalk end end Can we build n*m compilers with n+m components?  Must encode all language knowledge in each front end  Must encode all features in a single IR  Must encode all target knowledge in each back end Limited success in systems with very low-level IR s

Tufts University Computer Science 6

3 The Front End

Source tokens IR Scanner Parser code

Errors Responsibilities

 Recognize legal (& illegal) programs  Report errors in a useful way  Produce IR & preliminary storage map  Shape the code for the back end Much of front end construction can be automated

Tufts University Computer Science 7

The Front End

Source tokens IR Scanner Parser code

Errors Scanner  Maps character stream into words—the basic unit of syntax  Produces pairs — a word & its part of speech x = x + y; becomes = < id ,x> + < id ,y> ;  word ≅ lexeme, part of speech ≅ token type  In casual speech, we call the pair a token  Typical tokens include number , identifier , +, –, while , if  Scanner eliminates white space ( including comments )  Speed is important

Tufts University Computer Science 8

4 The Front End

Source tokens IR Scanner Parser code

Errors Parser  Recognizes context-free syntax & reports errors  Guides context-sensitive (“semantic”) analysis (type checking )  Builds IR for source program  Most parsers built using automated tools Hand-coded parsers not too difficult to build

Tufts University Computer Science 9

The Front End

 Context-free syntax is specified with a grammar sentence → subject verb object subject → noun | proper-noun

 It is written in a variant of Backus–Naur Form (BNF)

 Formally, a grammar G = (S,N,T,P)  N is a set of non-terminal symbols  T is a set of terminal symbols or words  S is the start symbol – chosen from N  P is a set of productions or rewrite rules (P : N → N ∪T )

Tufts University Computer Science 10

5 The Front End

Application to programming languages:

1. goal → expr 2. expr → expr op term S = goal 3. | term T = { number , id , +, - } 4. term → number N = { goal , expr , term , op } 5. | id 6. op → + P = { 1, 2, 3, 4, 5, 6, 7 } 7. | -

 This grammar defines simple expressions with addition & subtraction over “number” and “id”  This grammar, like many, falls in a class called “context- free grammars”

Tufts University Computer Science 11

The Front End

 Given a grammar, we Production Result can derive sentences goal 1 expr by repeated 2 expr op term substitution 5 expr op y 7 expr - y 2 expr op term - y 4 expr op 2 - y  To recognize a valid 6 expr + 2 - y sentence in some 3 term + 2 - y CFG, we reverse this 5 x + 2 -y process and build up a parse

Tufts University Computer Science 12

6 The Front End

 A parse can be goal represented by a tree (parse tree or syntax tree ) expr

x + 2 - y expr op term

expr op term -

+ term 1. goal → expr 2. expr → expr op term 3. | term 4. term → number  Contains a lot of 5. | id 6. op → + unneeded information. 7. | -

Tufts University Computer Science 13

The Front End

 Compilers often use an abstract syntax tree

-

The AST summarizes + grammatical structure, without including detail about the derivation

 This is much more concise

 AST s are one kind of intermediate representation ( IR )

Tufts University Computer Science 14

7 Front end to back end

 Code shape and lowering  Start with high-level AST  Dismantle complex structures into simple ones if if (x > 0) { t0 = x > 0 br t0 label1 y = a + b * c; > goto label2 z = d + b * c; x 0 label1: } t1 = b * c = = y = a + t1 y + z = d + t1 z + label2: a * d * b c b c

Tufts University Computer Science 15

The Back End

IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code

Errors

 Responsibilities  Translate IR into target machine code  Choose instructions to implement each IR operation  Decide which value to keep in registers  Ensure conformance with system interfaces Automation has been less successful in back end

Tufts University Computer Science 16

8 The Back End

IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code

Errors  Produce fast, compact code  Take advantage of target features Addressing modes, special instructions (e.g., multadd )  Often viewed as a pattern matching problem ad hoc and automated methods  Used to be a bigger problem on CISC machines – VAX-11 Orthogonality of RISC simplified this problem

Tufts University Computer Science 17

The Back End

 Example: RISC instructions

load @b => r1 ... load @c => r2 label1: mult r1, r2 => r3 t1 = b * c load @a => r4 y = a + t1 add r3, r4 => r5 z = d + t1 store r5 => @y ... load @d => r6 add r3, r6 => r7 store r7 => @z  Notice:  Explicit loads and stores  Lots of registers – “virtual registers”

Tufts University Computer Science 18

9 The Back End

IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code

Errors

Register Allocation PentiumPentium 4 4  Have each value in a register when it is used Registers 1 cycle  Manage a limited set of resourcesRegisters 1 cycle Cache 3-8 cycles  Can change instruction choicesCache & insert LOAD 3-8s & cycles STORE s Memory 30-150 cycles  Optimal allocation is NP-CompleteMemory (1 or k 30-150registers) cycles

Compilers approximate solutions to NP-Complete problems

Tufts University Computer Science 19

The Back End

IR Instruction IR Register IR Instruction Machine Selection Allocation Scheduling code

Errors  Avoid hardware stalls and interlocks  Use all functional units productively  Can increase lifetime of variables (changing the allocation)

Optimal scheduling is NP-Complete in nearly all cases Heuristic techniques are well developed

Tufts University Computer Science 20

10 The Back End

 Conflicting goals:  Move loads early to avoid waiting  Move operations together to reduce registers load @b => r1 load @b => r1 load @c => r2 load @c => r2 mult r1, r2 => r3 load @a => r4 load @a => r1 load @d => r5 add r3, r1 => r1 mult r1, r2 => r3 store r1 => @y add r3, r4 => r4 load @d => r1 store r4 => @y add r3, r1 => r1 add r3, r5 => r5 store r1 => @z store r5 => @z

Uses only 3 registers, Start loads early, hide but may stall on loads latency, but need 5 registers Tufts University Computer Science 21

Traditional 3-pass compiler

Source Front IR Middle IR Back Machine Code End End End code

ErrorsUnderstood misnomer Code Improvement (or optimization )  Analyzes IR and rewrites (or transforms) IR  Primary goal is to reduce running time  May also improve space, power consumption, …  Must preserve “meaning” of the code Next semester...

Tufts University Computer Science 22

11 The Optimizer (or Middle End)

IROpt IROpt IROpt IR... Opt IR 1 2 3 n

Errors Modern optimizers are structured as a series of passes

Typical Transformations  Discover & propagate some constant value  Move a computation to a less frequently executed place  Specialize some computation based on context  Discover a redundant computation & remove it  Remove useless or  Encode an idiom in some particularly efficient form

Tufts University Computer Science 23

Optimization

 Example: Array accesses

for (i = 0; i < N; i++) for (i = 0; i < N; i++) for (j = 0; j < M; j++) for (j = 0; j < M; j++){ A[i][j] = A[i][j] + C; t0 = &A + (i * M) + j (*t0) += C; }

for (i = 0; i < N; i++) { t1 = 0; t1 = ii ** M;M for (i = 0; i < N; i++) { for (j = 0; j < M; j++){ t1 = t1t1 + M;M; t0 = &A + t1 + j for (j = 0; j < M; j++){ (*t0) += C; t0 = &A + t1 + j } (*t0) += C; } } }

Tufts University Computer Science 24

12 Role of the Run-time System

 Memory management services  Allocate  In the heap or in an activation record ( stack frame )  Deallocate  Collect garbage  Run-time type checking  Error processing (exception handling)  Interface to the operating system  Input and output  Support of parallelism  Parallel thread initiation  Communication and synchronization

Tufts University Computer Science 25

Related systems

 Interpreters  Scripting languages  XML processing  Just-in-time compilers  Java JVM  Microsoft CLR  Binary instrumentation  PIN  valgrind

Tufts University Computer Science 26

13 A bit about my research

 Getting more out of compilers:  High-level optimizations  Library-level optimization  Checking for errors and security vulnerabilities  Approximating vulnerability  Cooperation with run-time system  Compiler-assisted garbage collection  Object inlining

Compilers are an active research area!

Tufts University Computer Science 27

Next time...

 Sign up on mailing list:

https://www.eecs.tufts.edu/mailman/listinfo/comp181

 Dive down into the details

 Scanning

 Projects

Tufts University Computer Science 28

14 Tufts University Computer Science 29

High-level View of a Compiler

Source Machine Compiler code code

Errors Implications  Must recognize legal (and illegal) programs  Must generate correct code  Must manage storage of all variables (and code)  Must agree with OS & linker on format for object code Big step up from assembly language—use higher level notations

Tufts University Computer Science 30

15 Modern Restructuring Compiler

HL HL Opt + Source Front AST AST IR IR Machine Restructurer Back Code End Gen code End

Errors

Typical Restructuring (source-to-source) Transformations:  Blocking for and register reuse  Vectorization  Parallelization  All based on dependence  Also full and partial inlining Possible subject of CS 516

Tufts University Computer Science 31

Example

• Optimization of Subscript Expressions in Fortran Address(A(I,J)) = address(A(0,0)) + J * (column size) + I

Does the user realize a multiplication is generated here?

Tufts University Computer Science 32

16 Example

 Optimization of Subscript Expressions in Fortran Address(A(I,J)) = address(A(0,0)) + J * (column size) + I

Does the user realize a multiplication is generated here?

DO I = 1, M A(I,J) = A(I,J) + C ENDDO

Tufts University Computer Science 33

Example

• Optimization of Subscript Expressions in Fortran Address(A(I,J)) = address(A(0,0)) + J * (column size) + I

Does the user realize a multiplication is generated here?

compute addr(A(0,J) DO I = 1, M DO I = 1, M A(I,J) = A(I,J) + C add 1 to get addr(A(I,J) ENDDO A(I,J) = A(I,J) + C ENDDO

Tufts University Computer Science 34

17 Classic Compilers

1957: The FORTRAN Automatic Coding System

Index Code Front Flow Register Final Optimiz’n Merge End Analysis Allocation Assembly bookkeeping

Front End Middle End Back End

 Six passes in a fixed order  Generated good code Assumed unlimited index registers Code motion out of loops, with ifs and gotos Did flow analysis & Tufts University Computer Science 35

Classic Compilers 1980: I BM ’s PL.8 Compiler

Front Middle End Back End End Global cse  Many passes, one front end, several back ends CodeMulti-level motion IR  Collection of 10 or more passes Constanthas become folding Strengthcommon reduction wisdom Repeat some passes and analyses Represent complex operations at 2 levels elimination Below machine-level IR Code straightening Trap elimination Algebraic reassociation

* Tufts University Computer Science 36

18 Classic Compilers 1986: HP’s PA-RISC Compiler

Front Middle End Back End End

 Several front ends, an optimizer, and a back end  Four fixed-order choices for optimization (9 passes)  Coloring allocator, instruction scheduler, peephole optimizer

Tufts University Computer Science 37

Classic Compilers 1999: The SUIF Compiler System

Fortran 77 C/Fortran

C & C++ Alpha

Java

Front End Middle End Back End

Another classically-built compiler DataSSA construction dependence analysis  3 front ends, 3 back ends ScalarDead code & array elimination privitization ReductionPartial redundancy recognition elimination  18 passes, configurable order PointerConstant analysis propagation  Two-level IR (High S UIF , Low S UIF ) AffineGlobal loop value transformations numbering BlockingStrength reduction  Intended as research infrastructure CapturingReassociation object definitions VirtualInstruction function scheduling call eliminationRegister allocation Tufts University Computer Science Garbage collection 38

19 Classic Compilers 2000: The SGI Pro64 Compiler (now FortranOpen64 from Intel) Interpr. Loop Global Code Anal. & Nest Optim’n Gen. C & C++ Optim’n Optim’n

Java

Front End Middle End Back End Interprocedural DependenceGlobalCode Generation Optimization analysis ParallelizationClassicIf conversion analysis & predication InliningSSA-based (user analysis & library & opt’n code) LoopConstantCode transformationsmotion propagation, (fission, PRE, fusion,CloningScheduling interchange, (constants (inc. sw & pipelining)peeling, locality) DeadOSR+LFTR, function DVNT, elimination DCE tiling,(alsoAllocation used unroll by &other jam) phases) ArrayDeadPeephole variableprivitization optimization elimination Open source for IA 64 Tufts University Computer Science 39  3 front ends, 1 back end

Classic Compilers

Even a 2000 JIT fits the mold, albeit with fewer passes

native bytecode code

Middle Back End End Java Environment

 Front end tasks are handled elsewhere  Few (if any) optimizations Avoid expensive analysis Emphasis on generating native code Compilation must be profitable

Tufts University Computer Science 40

20