






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Li; Class: LANG COMPILER DESIGN; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 Code Generation 1 / 1
IR code → Code Generator → target code
Goal: Generating correct and high-quality target code.
Main Issue: Instruction selection (The mapping from an IR code to target code is not unique.)
Factors that affect selection:
t1 := fp[-12] (MOVE (MEM (BINOP + (NAME fp) 8)) fp[8] := t1 (MEM (BINOP - (NAME fp) 12)))
Target code:
add %fp,-12,%r3 ld [%fp-12],%r ld [%r3], %r5 st %r5,[%fp+8] add %fp,8,%r st %r5,%r4 # CISC: move [%fp-12],[%fp+8]
Jingke Li (Portland State University) CS322 Code Generation 3 / 1
x := y op z
Jingke Li (Portland State University) CS322 Code Generation 7 / 1
a := b[i]
b in register %rb b in memory Mb b in stack slot Sb mov [%rb+i],a mov Mb,%r mov [%fp+Sb],%r mov %r+i,a mov [%r+i],a
a[i] := b
a in register %ra a in memory Ma a in stack slot Sa mov b,[%ra+i] mov Ma,%r mov [%fp+Sa],%r mov b,[%r+i] mov b,[%r+i]
if x rop y goto z
t := a - b u := a - c v := t + u d := v + u
Statement Code Generated Register Descriptor Address Descriptor registers empty t := a-b mov a, %r0 %r0 contains t t in %r sub b, %r u := a-c mov a, %r1 %r0 contains t t in %r sub c, %r1 %r1 contains u u in %r v := t+u add %r1, %r0 %r0 contains v v in %r %r1 contains u u in %r d := v+u add %r1 %r0 %r0 contains d d in %r mov %r0, d d in %r0, memory
Jingke Li (Portland State University) CS322 Code Generation 9 / 1
Goal: Try to generate optimal code for a broad class of register machines.
Machine Model:
Cost Vector: C (E ) = (c 0 c 1 · · · cr ) — it’s defined for an expression E , where c 0 — the cost of computing E into memory, with the use of unbounded number of registers. ci — the cost of computing E into a register, with the use of up to i registers.
Example: (a − b) + c ∗ (d/e)
− ∗ a b c / d e
Target Machine Model: Two registers: r 0 , r 1 Uniform-cost instructions: ri := M ri := ri op rj ri := ri op M ri := rj M := ri Cost Vector: C = (c 0 c 1 c 2 ). Jingke Li (Portland State University) CS322 Code Generation 13 / 1
Step 1:
−
a b c’ = (0 1 1) c” = (0 1 1)
c =?
Select the best from the 9 possible cases: c 0 ′ c 0 ′′ ⇒ c =(3 2 2) c 0 ′ c 1 ′′ ⇒ c =(4 3 3) c 0 ′ c 2 ′′ ⇒ c =(4 3 3) c 1 ′ c 0 ′′ ⇒ c =(3 2 2) c 1 ′ c 1 ′′ ⇒ c =(3 2 2) c 1 ′ c 2 ′′ ⇒ c =(3 2 2) c 2 ′ c 0 ′′ ⇒ c =(3 2 2) c 2 ′ c 1 ′′ ⇒ c =(3 2 2) c 2 ′ c 2 ′′ ⇒ c =(3 2 2)
Results for the whole tree:
− ∗
a b c /
d e
(0 1 1) (0 1 1) (0 1 1)
(0 1 1) (0 1 1)
(3 2 2)
(3 2 2)
(5 5 4)
(8 8 7)
Step 2:
− ∗
a b c /
d e
(0 1 1) ( 0 1 1) (0 1 1)
(0 1 1) ( 0 1 1)
(3 2 2)
(3 2 2)
(5 5 4 )
(8 8 7 )
R 1
R 1 R 0
R 1 R 0 R 1
R 1
Step 3: r 0 := c r 1 := d r 1 := r 1 /e r 0 := r 0 ∗ r 1 r 1 := a r 1 := r 1 − b r 1 := r 1 + r 0
Jingke Li (Portland State University) CS322 Code Generation 15 / 1
Assume an IR tree language.
(MEM
(BINOP + (MEM (BINOP + (TEMP tFP) (CONST a)))
(BINOP MUL (TEMP i) (CONST 4) )))
(BINOP + (TEMP tFP) (CONST x))) )
load r 1 ← M[fp + a] addi r 2 ← r 0 + 4 mul r 2 ← ri × r 2 add r 1 ← r 1 + r 2 load r 2 ← fp + x movem M[r 1 ] ← M[r 2 ]
Jingke Li (Portland State University) CS322 Code Generation 19 / 1
We can associate a cost to each tile, then we can measure quantitatively the quality of a tiling of an IR tree — the best tiling corresponds to an instruction sequence of least cost.
Two Levels of Optimality:
For finding an optimal tiling.
“Largest” means most nodes. If two tiles of equal size match at the root, the choice between them is arbitrary.
Example: Applying this algorithm to the tree on the previous page will result in Solution 2.
Jingke Li (Portland State University) CS322 Code Generation 21 / 1
For finding the optimum tiling. The algorithm assigns a cost to every node in the tree, which is the sum of the instructions costs of the best instruction sequence that can tile the subtree rooted at that node. It works bottom-up. For each tile t of cost c that matches at node n, there will be zero or more subtrees si corresponding the leaves of the tile. Assume the cost for the subtree si is ci (which has already been computed), then the total cost of matching tile t at node n is c +
ci.
Jingke Liat (Portland State University)^ n. CS322 Code Generation 22 / 1