Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Similar documents
Compiling Techniques

Compiler construction 2009

CMSC430 Spring 2014 Midterm 2 Solutions

Compiler construction 2009

CSc 453 Interpreters & Interpretation

Intermediate Code Generation

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Running class Timing on Java HotSpot VM, 1

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Intermediate Code & Local Optimizations

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

JAM 16: The Instruction Set & Sample Programs

Part VII : Code Generation

CSE 431S Final Review. Washington University Spring 2013

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CODE GENERATION Monday, May 31, 2010

Recap: Printing Trees into Bytecodes

CSC 4181 Handout : JVM

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

EXAMINATIONS 2008 MID-YEAR COMP431 COMPILERS. Instructions: Read each question carefully before attempting it.

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Principles of Compiler Design

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

High-Level Language VMs

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

Copyright 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 16

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

CS 432 Fall Mike Lam, Professor. Code Generation

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Memory Management. Frédéric Haziza Spring Department of Computer Systems Uppsala University

55:132/22C:160, HPCA Spring 2011

Code Generation. Lecture 30

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Code Analysis and Optimization. Pat Morin COMP 3002

Intermediate Code & Local Optimizations. Lecture 20

CMSC430 Spring 2009 Midterm 2 (Solutions)

An Overview of Compilation

Intermediate Code Generation

CSE 431S Code Generation. Washington University Spring 2013

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

CSCE 314 Programming Languages

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

Compiler Construction

Java: framework overview and in-the-small features

Intermediate Representations

Principles of Compiler Design

CSE 504: Compiler Design. Code Generation

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compilers. Lecture 2 Overview. (original slides by Sam

Compiler Construction I

Java Instrumentation for Dynamic Analysis

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Compilers and computer architecture Code-generation (2): register-machines

Instruction Set Architecture

Lecture 4: MIPS Instruction Set

Intermediate Representations

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

TACi: Three-Address Code Interpreter (version 1.0)

Compiler Optimization Intermediate Representation

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

Exercise 7 Bytecode Verification self-study exercise sheet

Topics. Structured Computer Organization. Assembly language. IJVM instruction set. Mic-1 simulator programming

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Chapter 2 A Quick Tour

Paging & Segmentation

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Compiler Construction

Tutorial 3: Code Generation

Appendix A The DL Language

Intermediate representation

Parallelism of Java Bytecode Programs and a Java ILP Processor Architecture

Register Allocation. CS 502 Lecture 14 11/25/08

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Assembly Language: Overview!

Write a P-Code Assembler. Program Files: File Description pasm.cpp. Project:

EITF20: Computer Architecture Part2.1.1: Instruction Set Architecture

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Chapter 10 Language Translation

Intermediate Representations

1 Little Man Computer

Concepts Introduced in Chapter 6

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Tour of common optimizations

Compiler Code Generation COMP360

An Introduction to Multicodes. Ben Stephenson Department of Computer Science University of Western Ontario

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Oak Intermediate Bytecodes

Compiler Construction

CENG3420 Lecture 03 Review

Communicating with People (2.8)

Transcription:

Code Generation Frédéric Haziza <daz@it.uu.se> Department of Computer Systems Uppsala University Spring 2008

Operating Systems Process Management Memory Management Storage Management Compilers Compiling process & Lexical analysis Parsing Semantic & Code generation

Analysis Lexical analysis Syntax analysis Semantics analysis Synthesis Machine independant code generation Optimization of machine independant code Storage Allocation Machine code generation Optimization of machine code

Outline 1 Intermediate code 2 Machine code 3 Optimizations 4 OSKomp 08 Code Generation

Good IR easy to translate from AST easy to translate to assembly/machine code easy to optimize easy to retarget 6 OSKomp 08 Code Generation

Well-known examples 1 Three-address code 2 P-code (for Pascal) 3 Bytecode (for Java) 7 OSKomp 08 Code Generation

P-Code Stack-based intermediate code, for Pascal. Instruction format: F P Q F is a function P, Q may be absent P used to specify a static block level Q: offset within a frame or immediate operand (ie constant) Compile-time address are (static level, offset) 8 OSKomp 08 Code Generation

Instructions Instructions with no parameter operate on the stack s top AND, DIF, NGI, FLT, FLO, INN One or two-address instruction to load/store a value on the top of the stack LDCI, LODI, LDA, STRI Jump instructions: UJP L 7 : unconditional jump FJP L 8 : jump to L 8 if top of the stack is false Labels 9 OSKomp 08 Code Generation

if(expression) statement 1 else statement 2 Code to put the value of expression on top of the stack FJP L 1 Code to implement statement 1 UJP L 2 L 1 Code to implement statement 2 L 2 while(expression) statement L 1 Code to put the value of expression on top of the stack FJP L 2 Code to implement statement UJP L 1 L 2

Java Run-Time system Execution engine (executes bytecode instructions) Memory manager (manages heap in which all objects and arrays are stored) Error and Exception manager (used to catch runtime failures in a planned and systematic manner) Threads interface (handles concurrency) Class Loader (loads, links and initializes classes) Security manager deals with attempts to run hostile programs 11 OSKomp 08 Code Generation

Principal types of bytecode instructions Stack manipulation Performing arithmetic Handling objects and arrays Control flow Method invocation Handling exceptions and concurrency 12 OSKomp 08 Code Generation

Manipulating the stack Instructions Meanings iconst_4 iload_4 pop dup swap load the integer constant 4 on to the stack load the value of local variable number 4 on to the stack discard top value of the stack duplicate top item on the stack interchange top two values of the stack istore_4 store the value on top of the stack in the local variable number 4 13 OSKomp 08 Code Generation

Arithmetic and Array Instructions iadd fadd fmul Meanings add the two integers on the top of the stack add the two floats on the top of the stack multiply the two floats on the top of the stack Instructions iaload Meanings puts the value of an array element on top of the stack, assuming the array reference and the index of the array are already on the stack 14 OSKomp 08 Code Generation

Conditional and unconditional Branches Instructions Meanings ifeq L 1 if_icmpne L 1 jump to L 1 if the integer value on top of the stack is zero jump to L 1 if two integer values on top of the stack are not equal goto L 1 jump to L 1 15 OSKomp 08 Code Generation

if(expression) statement 1 else statement 2 Bytecode to put the value of expression on top of the stack ifeq L 1 Bytecode to implement statement 1 goto L 2 L 1 Bytecode to implement statement 2 L 2 while(expression) statement L 1 Bytecode to put the value of expression on top of the stack ifeq L 2 Bytecode to implement statement goto L 1 L 2

Considerations CISC complex instruction set computer RISC reduced instruction set computer * Wide range of addressing mode * Small number of registers (<16) * Many special purpose registers * 2-address instructions A + B A * Variable length instructions * Instructions with side-effect * Different exec time for instr * Simple addressing mode (with registers) * Many registers (>32) * All registers are general purpose * 3-address instructions r 3 = r 1 + r 2 * Fixed length instructions (32 bits) * No side-effect, one result per instr * Similar exec time for instr Instruction selection Register allocation 18 OSKomp 08 Code Generation

Example t 1 = a + b t 2 = c + d t 3 = t 1 t 2 Must keep t 1 and t 2 until t 3 is evaluated 19 OSKomp 08 Code Generation

Example 2 a b + c d + e f Temporaries Register t 1 = a b t 2 = c d t 1 1 t 3 = t 1 + t 2 t 2 2 t 4 = e f t 3 3 t 5 = t 3 + t 4 t 4 1 t 5 2 Do we really need distinct registers? 20 OSKomp 08 Code Generation

1) n = 0 2) sum 2 = 0 3) sum 3 = 0 4) t 1 = n < 10 : L 1 5) t 2 = not t 1 6) if t 2 goto L 2 7) n = n + 1 8) m = n n 9) sum 2 = sum 2 + m 10) t 3 = m n 11) sum 3 = sum 3 + t 3 12) goto L 1 13) L 2 n = 0; sum2 = 0; sum3 = 0; while (n<10){ n = n+1; m = n*n; sum2 = sum2 + m; sum3 = sum3 + m*n; } Variable Live Register n 1..12 1 sum 2 2..12 2 sum 3 3..12 3 t 1 4..5 4 m 8..10 4 t 2 5..6 4 t 3 10..11 4 21 OSKomp 08 Code Generation

Liveness analysis Equations of the form: 1 in n = use n (out n \def n ) 2 out n = s succ(n) in s where use n: out n: def n: in n: set of all variables whose values are used in statement n set of all variables that are live on leaving statement n set of all variables that are defined in statement n set of all variables that are live on reaching statement n 22 OSKomp 08 Code Generation

Typical local optimizations Constant folding Strength reduction Elimination of unnecessary instructions 24 OSKomp 08 Code Generation

Typical global optimizations Analysis of control and data flow: Dead code elimination Common subexpression elimination Loop optimizations 25 OSKomp 08 Code Generation