Compiler construction in4303 lecture 9

Similar documents
You may work with a partner on this quiz; both of you must submit your answers.

Compiler construction in4303 lecture 8

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Compiler Construction D7011E

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CPEG421/621 Tutorial

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

CS213. Machine-Level Programming III: Procedures

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

Machine-level Programming (3)

CPSC W Term 2 Problem Set #3 - Solution

Assembly Language: Function Calls

x86 assembly CS449 Fall 2017

Machine-Level Programming III: Procedures

Code Generation. Lecture 30

Assembly Language: Function Calls" Goals of this Lecture"

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

Homework. In-line Assembly Code Machine Language Program Efficiency Tricks Reading PAL, pp 3-6, Practice Exam 1

W4118: PC Hardware and x86. Junfeng Yang

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

Machine Code and Assemblers November 6

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

Compiler construction in4303 answers

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Programming Language Implementation

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Process Layout, Function Calls, and the Heap

Machine-Level Programming II: Control and Arithmetic

CS241 Computer Organization Spring Addresses & Pointers

Assembly I: Basic Operations. Jo, Heeseung

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

Turning C into Object Code Code in files p1.c p2.c Compile with command: gcc -O p1.c p2.c -o p Use optimizations (-O) Put resulting binary in file p

Assembly I: Basic Operations. Computer Systems Laboratory Sungkyunkwan University

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

COMP 210 Example Question Exam 2 (Solutions at the bottom)

CS241 Computer Organization Spring 2015 IA

Sungkyunkwan University

The Hardware/Software Interface CSE351 Spring 2013

THEORY OF COMPILATION

Compilers Crash Course

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Principles of Compiler Design

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Variables vs. Registers/Memory. Simple Approach. Register Allocation. Interference Graph. Register Allocation Algorithm CS412/CS413

More Code Generation and Optimization. Pat Morin COMP 3002

Intel assembly language using gcc

Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address = address of top element

Introduction to Computer Systems. Exam 1. February 22, This is an open-book exam. Notes are permitted, but not computers.

CS , Fall 2004 Exam 1

CS , Fall 2001 Exam 1

CSE 351: Week 4. Tom Bergan, TA

CMSC 313 Fall2009 Midterm Exam 2 Section 01 Nov 11, 2009

System Programming and Computer Architecture (Fall 2009)

Credits to Randy Bryant & Dave O Hallaron

Instruction Selection. Problems. DAG Tiling. Pentium ISA. Example Tiling CS412/CS413. Introduction to Compilers Tim Teitelbaum

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Machine Programming 3: Procedures

Machine Programming 4: Structured Data

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

Assembly level Programming. 198:211 Computer Architecture. (recall) Von Neumann Architecture. Simplified hardware view. Lecture 10 Fall 2012

1 /* file cpuid2.s */ 4.asciz "The processor Vendor ID is %s \n" 5.section.bss. 6.lcomm buffer, section.text. 8.globl _start.

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

Communicating with People (2.8)

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

EECS 213 Fall 2007 Midterm Exam

Machine-Level Programming II: Arithmetic & Control /18-243: Introduction to Computer Systems 6th Lecture, 5 June 2012

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

Machine Level Programming II: Arithmetic &Control

Assignment 11: functions, calling conventions, and the stack

CS241 Computer Organization Spring Loops & Arrays

Process Layout and Function Calls

Instruction Set Architecture

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

Sungkyunkwan University

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Homework 0: Given: k-bit exponent, n-bit fraction Find: Exponent E, Significand M, Fraction f, Value V, Bit representation

CS429: Computer Organization and Architecture

Machine Language, Assemblers and Linkers"

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

CS61 Section Solutions 3

CS 2505 Computer Organization I

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

Sungkyunkwan University

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Transcription:

Compiler construction in4303 lecture 9 Code generation Chapter 4.2.5, 4.2.7, 4.2.11 4.3

Overview Code generation for basic blocks instruction selection:[burs] register allocation: graph coloring instruction ordering: ladder sequences Assemblers & linkers library intermediate code code generator assembly assembler object file linker executable

Code generation for basic blocks improve quality of code emitted by simple code generation consider multiple AST nodes at a time basic block: a part of the control graph that contains no splits (jumps) or combines (labels) generate code for maximal basic blocks that cannot be extended by including adjacent AST nodes

Code generation for basic blocks a basic block consists of expressions and assignments { int n; } n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; fixed sequence (;) limits code generation an AST is too restrictive

{ int n; Example AST } n = a+1; x = (b+c) * n; n = n+1; y = (b+c) * n; ; ; ; =: =: =: =: + n * x + n * y a 1 + n n 1 + n b c b c

Dependency graph convert AST to a directed acyclic graph (dag) capturing essential data dependencies data flow inside expressions: operands must be evaluated before operator is applied data flow from a value assigned to variable V to the use of V: the usage of V is not affected by other assignments

AST to dependency graph AST replace arcs by downwards arrows (upwards for destination under assignment) insert data dependencies from use of V to preceding assignment to V insert data dependencies from each assignment to V to all previous uses add roots to the graph (output variables) remove ;-nodes and connecting arrows dependency graph check errata

Example dependency graph { int n; x y n = a+1; x = (b+c) * n; * * n = n+1; y = (b+c) * n; + + + + } b c a 1 b c 1

Common subexpression elimination common subexpressions occur multiple times and evaluate to the same value { int n; x y n = a+1; x = (b+c) * n; * * n = n+1; y = (b+c) * n; + + + + } b c a 1 b c 1

Exercise (7 (5 min.) given the code fragment x := a*a + 2*a*b;+ b*b; y := a*a 2*a*b;+ b*b; draw the dependency graph before and after common subexpression elimination.

Answers

Recognizing CSEs Problem: how do we identify nodes with the same type (operator) and dependencies (operands)? O(n 2 ) Solution: hash table with a hash function based on operator and operands. O(n)

From dependency graph to code target: register machine (lecture 8) with additional operations on memory reg op:= reg reg op:= mem Add_Reg R2, R1 Add_Mem x, R1 rewrite nodes with machine instruction templates, and linearize the result instruction ordering: register allocation: ladder sequences graph coloring

Linearization of the data dependency graph example: a - * d + c b Load_Mem a, R1 Add_Mem b, R1 Mul_Mem, c, R1 Sub_Mem d, R1 definition of a ladder sequence each root node is a ladder sequence a ladder sequence S ending in operator node N can be extended with the left operand of N if operator N is communitative then S may also extended with the right operand of N

Linearization of the data dependency graph code generation for a ladder sequence x x Store_Mem R 1, x * Mul_Reg * R T, R 1 + R T Add_Mem + c, R 1 R T b c Load_Mem b, b R 1 c instructions from bottom to top, one register

Linearization of the data dependency graph late evaluation don t occupy registers select ladder sequence S without additional incoming dependencies introduce temporary registers for non-leaf operands, which become additional roots generate code for S, using R1 as the ladder register remove S from the graph note: code blocks produced in reverse order

Example code generation x y 1) ladder: y, *, + Load_Const 1, R1 R2 * R3 * Add_Reg R3, R1 Mul_Reg, R2, R1 + + + Store_Mem R1, y b c a 1 1 2) ladder: x, * Load_Reg R2, R1 Mul_Reg R3, R1 Store_Mem R1, x 3) ladder: R2, + Load_Mem b, R1 Add_Mem c, R1 Load_Reg R1, R2 4) ladder: R3, + Load_Const 1, R1 Add_Mem a, R1 Load_Reg R1, R3

Register allocation by graph coloring procedure-wide register allocation only live variables require register storage dataflow analysis: a variable is live at node N if the value it holds is used on some path further down the control-flow graph; otherwise it is dead two variables(values) interfere when their live ranges overlap

Live analysis a := read(); b := read(); c := read(); d := a + b*c; a b c d a := read(); b := read(); c := read(); d := a + b*c; if (d < 10 ) then e := c+8; d < 10 print(c); else e e := c+8; f := 10; f f := 10; print(c); e e := f + d; e := f + d; print(f); print(f); fi print(e); print(e);

Register interference graph a := read(); a a b b := read(); b c := read(); c d := a + b*c; d c d d < 10 e f e e := c+8; f := 10; f print(c); e e := f + d; print(f); print(e);

Graph coloring NP complete problem a b heuristic: color easy nodes last find node N with lowest degree remove N from the graph color the simplified graph set color of N to the first color that is not used by any of N s neighbors c e d f

Exercise (7 min.) { int tmp_2ab = 2*a*b; int tmp_aa = a*a; int tmp_bb = b*b; } x := tmp_aa + tmp_2ab + tmp_bb; y := tmp_aa - tmp_2ab + tmp_bb; given that a and b are live on entry and dead on exit, and that x and y are live on exit: (a) construct the register interference graph (b) color the graph; how many register are needed?

Answers

Code optimization preprocessing constant folding a[1] *(a+4*1) *(a+4) strength reduction 4*i i<<2 in-lining... postprocessing peephole optimization: replace inefficient patterns Load_Reg R2, R1 Load_Reg R1, R2 Load_Reg R2, R1

Assemblers, linkers & loaders assembler symbolic object code binary object file linker multiple objects (libraries) executable loader load executable into memory library assembly assembler object file linker executable

Assemblers symbolic code binary machine code addl %edx, %ecx 0000 0001 11 010 001 opcode addl reg-reg edx ecx handle addresses internal: resolve external: store in relocation table

Handling internal addresses Two passes compute addresses generate object file One pass backpatch list disclaimer: only works for internal text labels and pc relative addressing clear_vect(int clear_vect: *a, int n) { pushl %ebp intmovl i; %esp, %ebp movl 8(%ebp), %ecx for( movli=0; i<n; 12(%ebp), i++) %edx testl a[i] = %edx, 0; %edx } jle.l5 movl $0, %eax.l4: movl $0, (%ecx,%eax,4) incl %eax cmpl %eax, %edx jne.l4.l5: popl %ebp ret

Handling external addresses relocation information entry points (text + data) unresolved references int **A; symbol type address int N; A entry 000000 data void foo() { int i; N foo entry entry 000004 000000 data text } for (i=0; i<n; i++) clear_vect(a[i], N); N A clear_vect ref ref ref 000005 000014 00001c text text text