# lec20 - CS415 Compilers Code Generation These slides are...

This preview shows pages 1–9. Sign up to view the full content.

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

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

View Full Document
Lecture 20 2 cs415, spring 10 Project 2 Consists of two parts: 1 – syntax errors and type checking (static semantics) 2 – simple code generation - common subexpressions within single statements - common subexpressions across basic blocks Use of bison (yacc) in C.
Lecture 20 3 cs415, spring 10 How does the compiler handle A [i,j] ? First, must agree on a storage scheme Row-major order (most languages) Lay out as a sequence of consecutive rows Rightmost subscript varies fastest A[1,1], A[1,2], A[1,3], A[2,1], A[2,2], A[2,3] Column-major order (Fortran) Lay out as a sequence of columns Leftmost subscript varies fastest A[1,1], A[2,1], A[1,2], A[2,2], A[1,3], A[2,3] Indirection vectors (Java) Vector of pointers to pointers to … to values Takes much more space, trades indirection for arithmetic Not amenable to analysis

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

View Full Document
Lecture 20 4 cs415, spring 10 Laying Out Arrays The Concept Row-major order Column-major order Indirection vectors 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 A 1,1 2,1 1,2 2,2 1,3 2,3 1,4 2,4 A 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 A 1,1 1,2 1,3 1,4 2,1 2,2 2,3 2,4 A These have distinct & different cache behavior
Lecture 20 5 cs415, spring 10 Computing an Array Address A[ i ] @A + ( i – low ) x sizeof(A[1]) In general: base(A) + ( i – low ) x sizeof(A[1]) Declaration: A[low . . high] of …

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

View Full Document
Lecture 20 6 cs415, spring 10 Computing an Array Address A[ i ] @A + ( i – low ) x sizeof(A[1]) In general: base(A) + ( i – low ) x sizeof(A[1]) Almost always a power of 2, known at compile-time use a shift for speed int A[1:10] low is 1 Make low 0 for faster access (saves a ) Declaration: A[low . . high] of …
Lecture 20 7 cs415, spring 10 Computing an Array Address A[ i ] @A + ( i – low ) x sizeof(A[1]) In general: base(A) + ( i – low ) x sizeof(A[1]) What about A[i 1 ,i 2 ] ? Row-major order, two dimensions @A + (( i 1 low 1 ) x ( high 2 –low 2 + 1 ) + i 2 low 2 ) x sizeof(A[1]) Column-major order, two dimensions @A + (( i 2 low 2 ) x ( high 1 –low 1 + 1 ) + i 1 low 1 ) x sizeof(A[1]) Indirection vectors, two dimensions *(A[i 1 ] )[i 2 ] —where ±± A[i 1 ] is, itself, a 1-d array reference This stuff looks expensive! Lots of implicit +, -, x ops Declaration: A[ low1 . . high1, low2 . . high2]

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

View Full Document
Lecture 20 8 cs415, spring 10 where w = sizeof(A[1,1]) In row-major order @A + (i– low 1 )( high 2 –low 2 +1 ) x w + (j – low 2 ) x w Which can be factored into @A + i x ( high 2 –low 2 +1 ) x w + j x w (low 1 x (high 2 –low 2 +1) x w) + (low 2 x w) If low i , high i , and w are known, the last term is a constant Define @A 0 as @A – (low 1 x (high 2 –low 2 +1) x w + low 2 x w And len 2 as (high 2 -low 2 +1) Then, the address expression becomes @A 0 + (i x len 2 + j ) x w Optimizing Address Calculation for A [i,j] Compile-time constants
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/29/2010 for the course CS 198:415 taught by Professor Kremer,u. during the Spring '10 term at Rutgers.

### Page1 / 38

lec20 - CS415 Compilers Code Generation These slides are...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online