Code Generation Introduction

Similar documents
Code Generation: Introduction

Compiler Construction 2010 (6 credits)

Compiler Construction

Compiler Construction

02 B The Java Virtual Machine

Improving Java Performance

Recap: Printing Trees into Bytecodes

Compiler construction 2009

COMP3131/9102: Programming Languages and Compilers

Compiling Techniques

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

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Static Program Analysis

Improving Java Code Performance. Make your Java/Dalvik VM happier

JAM 16: The Instruction Set & Sample Programs

The Java Virtual Machine

Exercise 7 Bytecode Verification self-study exercise sheet

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

Run-time Program Management. Hwansoo Han

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

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

CSC 4181 Handout : JVM

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

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

Compiler construction 2009

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

Part VII : Code Generation

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

CSCE 314 Programming Languages

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Tutorial 3: Code Generation

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

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

Credits and Disclaimers

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

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

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

Java byte code verification

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

A Tour of Language Implementation

Programming. translate our algorithm into set of instructions machine can execute

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

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

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

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Let s make some Marc R. Hoffmann Eclipse Summit Europe

<Insert Picture Here> Maxine: A JVM Written in Java

Java Class Loading and Bytecode Verification

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 5. A Closer Look at Instruction Set Architectures

JoeQ Framework CS243, Winter 20156

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

Plan for Today. Safe Programming Languages. What is a secure programming language?

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

Joeq Analysis Framework. CS 243, Winter

The Java Virtual Machine

Second Part of the Course

CS213. Machine-Level Programming III: Procedures

CS 2505 Computer Organization I

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

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

Oak Intermediate Bytecodes

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Assembly III: Procedures. Jo, Heeseung

CPSC W Term 2 Problem Set #3 - Solution

Programming Language Systems

H.-S. Oh, B.-J. Kim, H.-K. Choi, S.-M. Moon. School of Electrical Engineering and Computer Science Seoul National University, Korea

Chapter 6: Code Generation

Space Exploration EECS /25

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Just In Time Compilation

CSE 431S Code Generation. Washington University Spring 2013

How Software Executes

Assignment 11: functions, calling conventions, and the stack

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

How Software Executes

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

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

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

Machine-level Programming (3)

CPS104 Recitation: Assembly Programming

Code Generation. Lecture 30

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

How do you create a programming language for the JVM?

The Hardware/Software Interface CSE351 Spring 2015

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

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

CS , Fall 2004 Exam 1

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

CMSC 330: Organization of Programming Languages

Compiler Construction 2011, Lecture 2

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

Transcription:

Code Generation Introduction

i = 0 LF w h i l e i=0 while (i < 10) { a[i] = 7*i+3 i = i + 1 lexer i = 0 while ( i < 10 ) source code (e.g. Scala, Java,C) easy to write Compiler (scalac, gcc) parser type check assign while characters words trees i 0 assign + a[i] * 7 i machine code (e.g. x86, arm, JVM) efficient to execute idea < i 10 3 mov R1,#0 mov R2,#40 mov R3,#3 jmp +12 mov (a+r1),r3 add R1, R1, #4 add R3, R3, #7 cmp R1, R2 blt -16 data-flow graphs optimizer code gen

Example: gcc #include <stdio.h> int main() { int i = 0; int j = 0; while (i < 10) { printf("%d\n", j); i = i + 1; j = j + 2*i+1; gcc test.c -S what does this do: gcc -O3 -S test.c.l3:.l2: jmp.l2 movl -8(%ebp), %eax movl %eax, 4(%esp) movl $.LC0, (%esp) call printf addl $1, -12(%ebp) movl -12(%ebp), %eax addl %eax, %eax addl -8(%ebp), %eax addl $1, %eax movl %eax, -8(%ebp) cmpl $9, -12(%ebp) jle.l3

Amusing Question What does this produce (with GCC 4.2.4) gcc -O3 test.c Loop was unrolled, giving a sequence corresponding to printf( %d\n, 0) printf( %d\n, 3) printf( %d\n, 8) printf( %d\n, 15) printf( %d\n, 24) printf( %d\n, 35) printf( %d\n, 48) printf( %d\n, 63) printf( %d\n, 80) printf( %d\n, 99)

LLVM: Another Interesting Compiler

i=0 while (i < 10) { a[i] = 7*i+3 i = i + 1 source code simplified Java-like language Your Project i = 0 LF w h i l e lexer i = 0 while ( i < 10 ) Your Compiler parser type check assign i 0 while assign + a[i] * 7 i < i 10 3 code gen characters words trees Java Virtual Machine (JVM) Bytecode 21: iload_2 22: iconst_2 23: iload_1 24: imul 25: iadd 26: iconst_1 27: iadd 28: istore_2

javac example while (i < 10) { System.out.println(j); i = i + 1; j = j + 2*i+1; javac Test.java javap c Test Guess what each JVM instruction for the highlighted expression does. 4: iload_1 5: bipush 10 7: if_icmpge 32 10: getstatic #2; //System.out 13: iload_2 14: invokevirtual #3; //println 17: iload_1 18: iconst_1 19: iadd 20: istore_1 21: iload_2 22: iconst_2 23: iload_1 24: imul 25: iadd 26: iconst_1 27: iadd 28: istore_2 29: goto 4 32: return Next phase: emit such bytecode instructions

Stack Machine: High-Level Machine Code instruction sequence: stack Let us step through top of stack program counter memory: 0 1 2 3 8... 21: iload_2 22: iconst_2 23: iload_1 24: imul 25: iadd 26: iconst_1 27: iadd 28: istore_2 29: goto 4...

Operands are consumed from stack and put back onto stack instruction sequence: 6 8 stack top of stack memory: 0 1 2 3 8... 21: iload_2 22: iconst_2 23: iload_1 24: imul 25: iadd 26: iconst_1 27: iadd 28: istore_2 29: goto 4...

Operands are consumed from stack and put back onto stack instruction sequence: 14 stack top of stack memory: 0 1 2 3 8... 21: iload_2 22: iconst_2 23: iload_1 24: imul 25: iadd 26: iconst_1 27: iadd 28: istore_2 29: goto 4...

Instructions in JVM Separate for each type, including integer types (iadd, imul, iload, istore, bipush) reference types (aload, astore) Why are they separate? Memory safety! Each reference points to a valid allocated object Conditionals and jumps Further high-level operations array operations object method and field access

Stack Machine Simulator var code : Array[Instruction] var pc : Int // program counter var local : Array[Int] // for local variables var operand : Array[Int] // operand stack var top : Int top 6 8 while (true) step def step = code(pc) match { case Iadd() => operand(top - 1) = operand(top - 1) + operand(top) top = top - 1 // two consumed, one produced case Imul() => operand(top - 1) = operand(top - 1) * operand(top) top = top - 1 // two consumed, one produced stack

Stack Machine Simulator: Moving Data case Bipush(c) => operand(top + 1) = c // put given constant 'c' onto stack top = top + 1 case Iload(n) => operand(top + 1) = local(n) // from memory onto stack top = top + 1 case Istore(n) => local(n) = operand(top) // from stack into memory top = top - 1 // consumed if (notjump(code(n))) pc = pc + 1 // by default go to next instructions

Actual Java Virtual Machine JVM Instruction Description from JavaTech book Official documentation: http://docs.oracle.com/javase/specs/ http://docs.oracle.com/javase/specs/jvms/se7/ html/index.html Use: javac -g *.java javap -c -l ClassName to compile to explore

Example: Twice class Expr1 { public static int twice(int x) { return x*2; javac -g Expr1.java; javap -c -l Expr1 public static int twice(int); Code: 0: iload_0 // load int from var 0 to top of stack 1: iconst_2 // push 2 on top of stack 2: imul // replace two topmost elements with their product 3: ireturn // return top of stack

Example: Area class Expr2 { public static int cubearea(int a, int b, int c) { return (a*b + b*c + a*c) * 2; javac -g Expr2.java; javap -c -l Expr2 LocalVariableTable: Start Length Slot Name Signature 0 14 0 a I 0 14 1 b I 0 14 2 c I public static int cubearea(int, int, int); Code: 0: iload_0 1: iload_1 2: imul 3: iload_1 4: iload_2 5: imul 6: iadd 7: iload_0 8: iload_2 9: imul 10: iadd 11: iconst_2 12: imul 13: ireturn

What Instructions Operate on operands that are part of instruction itself, following their op code (unique number for instruction - iconst) operand stack - used for computation (iadd) memory managed by the garbage collector (loading and storing fields) constant pool - used to store big values instead of in instruction stream e.g. string constants, method and field names mess!

CAFEBABE Library to make bytecode generation easy and fun! Named after magic code appearing in.class files when displayed in hexadecimal: More on that in the labs!