Intermediate Code - Compilers - Code Generation - Lecture Slides, Slides of Compilers

Some concept of Compilers - Code Generation are Activation Records, Basic Blocks, Instruction Selection, Intermediate Code, IR Optimisations, Semantic Analysis. Main points of this lecture are: Intermediate Code , Intermediate Representation, Machine-Independent, Instructions, Translate, Easier, Optimisations, Machine Code, Integer Constant, Symbolic Constant

Typology: Slides

2012/2013

Uploaded on 04/27/2013

balraj
balraj 🇮🇳

4.6

(8)

36 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMPILERS
Intermediate Code
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Intermediate Code - Compilers - Code Generation - Lecture Slides and more Slides Compilers in PDF only on Docsity!

COMPILERS

Intermediate Code

IR Trees

 An Intermediate Representation is a

machine-independent representation of

the instructions that must be generated.

 We translate ASTs into IR trees using a set

of rules for each of the nodes.

 Why use IR?

 (^) IR is easier to apply optimisations to.  (^) IR is simpler than real machine code.  (^) Separation of front-end and back-end.

IR Trees – Expressions 2/

BINOP

op (^) e1 e

e1 op e2 - Binary operator Evaluate e1, then e2, then apply op to e1 and e

CALL

f (e1….en)

Procedure call:evaluate f then the arguments in order, then call f

ESEQ

s e

Evaluate s for side effects then e for the result

IR Trees – Statements 1/

MOVE

TEMP

t

e

Evaluate e then move the result to temporary t

MOVE

MEM e e

Evaluate e1 giving address a, then evaluate e2 and move the move the result to address a

EXP

e

Evaluate e then discard the result

BINOP

PLUS TEMP fp CONST k

MEM

TEMP fp CONST k

MEM

Translation

 Simple Variables

 (^) simple variable v in the current procedure’s stack frame

 (^) could be abbreviated to:

Expression Example

 (^) Consider the statement:  (^) A = (B + 23) * 4;  (^) This would get translated into the statement:  (^) MOVE ( MEM ( +(TEMP fp, CONST k_A) ),

  • (
  • ( MEM ( +(TEMP fp, CONST k_B) ), CONST 23 ), CONST 4 ) )

Array creation

 t[e1] of e2:

 (^) externalCall(”initArray”, [e1, e2])

General 1-Dimensional Arrays

 var a : ARRAY [2..5] of integer;

 a[e] translates to:

 (^) MEM(+(TEMP fp, +(CONST k-2w,x(CONST w, e))))  (^) where k is offset of static array from fp, w is word size

 In Pascal, multidimensional arrays are

treated as arrays of arrays, so A[i,j] is

equivalent to A[i][j], so can translate as

above.

Multidimensional Arrays 2/

 array [L 1 ..U 1 ,L 2 ..U 2 ] of T

 Equivalent to array [L 1 ..U 1 ] of array [L 2 ..U 2 ] of T

 no. of elements in dimension j:

 (^) Dj = Uj - Lj + 1

 Memory address of A[i 1 , ...,in]:

 (^) Memory addr. of A[L 1 ,…, Ln] + sizeof(T) * [

  • (in - Ln)
  • (in-1 - Ln-1) * Dn
  • (in-2 - Ln-2) * Dn * Dn-
  • (i 1 - L 1 ) * Dn * Dn-1 * … * D 2 ]

 which can be rewritten as

 (^) i 1 *D 2 Dn + i 2 *D 3 Dn + … + in-1 * Dn + in

 - (L 1 *D 2 Dn + L 2 *D 3 Dn + … + Ln-1 * Dn + Ln)

 address of A[i 1 ,…,in]:

 (^) address(A) + ((variable part - constant part) * element size)

Variable part

Constant part

Multidimensional Arrays 3/

Record Creation

 t{f1=e1;f2=e2;….;fn=en} in the

(preferably garbage collected) heap, first

allocate the space then initialize it:

 (^) ESEQ(SEQ(MOVE(TEMP r, externalCall(”allocRecord”,[CONST n])), SEQ(MOVE(MEM(TEMP r), e1)), SEQ(..., MOVE(MEM(+(TEMP r, CONST (n-1)w)),en))), TEMP r)  (^) where w is the word size

String Literals

 Statically allocated, so just use the string’s

label

 (^) NAME( label)

 where the literal will be emitted as:

 (^) .word 11  (^) label: .ascii "hello world"

Generating IR Code 1/

 Walk the tree and generate a replacement

tree in the new language

 Example:

Source tree: (^) IF

CMP

VARIABLE

a

VARIABLE

b

ASSIGN

VARIABLE

c

INTEGER

Generating IR Code 2/

IR tree:

TEMP

MEM

SEQ

CJUMP

SEQ

SEQ

SEQ

fp

CONST

K_A

TEMP

MEM

fp

CONST

K_B

LT CONST

NAME

t

NAME

f

LABEL

t

LABEL

LABEL f NULL

TEMP

MEM

fp

CONST

K_C

CONST

MOVE