Compilation 2012 Code Generation

Similar documents
COMP 520 Fall 2013 Code generation (1) Code generation

The Java Virtual Machine

COMP3131/9102: Programming Languages and Compilers

CSC 4181 Handout : JVM

Exercise 7 Bytecode Verification self-study exercise sheet

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

javac 29: pop 30: iconst_0 31: istore_3 32: jsr [label_51]

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

Programming Language Systems

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

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

Part VII : Code Generation

Let s make some Marc R. Hoffmann Eclipse Summit Europe

02 B The Java Virtual Machine

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

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

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

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

Compiler construction 2009

Compilation 2012 The What and Why of Compilers

Compilation 2012 Static Type Checking

Improving Java Performance

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

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

Code Generation COMP 520: Compiler Design (4 credits) Professor Laurie Hendren

Compilation 2012 Static Analysis

Tutorial 3: Code Generation

301AA - Advanced Programming [AP-2017]

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

Static Type Checking. Static Type Checking. The Type Checker. Type Annotations. Types Describe Possible Values

Java Class Loading and Bytecode Verification

The Java Virtual Machine

Static Program Analysis

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

Translating JVM Code to MIPS Code 1 / 43

COMP3131/9102: Programming Languages and Compilers

Problem: Too Many Platforms!

Java byte code verification

Space Exploration EECS /25

CSc 620 Debugging, Profiling, Tracing, and Visualizing Programs. Compiling Java. The Java Class File Format 1 : JVM

A Quantitative Analysis of Java Bytecode Sequences

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

Delft-Java Dynamic Translation

Introduction to Compiler Construction in a Java World

Recap: Printing Trees into Bytecodes

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011

Static Analysis of Dynamic Languages. Jennifer Strater

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

CSE 431S Final Review. Washington University Spring 2013

Compiling Faster, Compiling Better with Falcon. Iván

Code Generation Introduction

Compiling Techniques

Code Profiling. CSE260, Computer Science B: Honors Stony Brook University

Mnemonics Type-Safe Bytecode Generation in Scala

Run-time Program Management. Hwansoo Han

Reverse engineering of binary programs for custom virtual machines

Jaos - Java on Aos. Oberon Event 03 Patrik Reali

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

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

Scala: Byte-code Fancypants. David Pollak JVM Language Summit 2009

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

Shared Mutable State SWEN-220

Virtual Machines. COMP 520: Compiler Design (4 credits) Alexander Krolik MWF 9:30-10:30, TR

Michael Rasmussen ZeroTurnaround

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

Joeq Analysis Framework. CS 243, Winter

Advances in Programming Languages: Generics, interoperability and implementation

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

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

JoeQ Framework CS243, Winter 20156

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

Compiler construction 2009

Practical VM exploiting based on CACAO

Java Card Platform. Virtual Machine Specification, Classic Edition. Version 3.1. January 2019

Taming the Java Virtual Machine. Li Haoyi, Chicago Scala Meetup, 19 Apr 2017

1 Compilation (Chapter 1) 2. 2 Lexical Analysis (Chapter 2) 4. 3 Parsing (Chapter 3) 7. 4 Type Checking (Chapter 4) 13

Java TM. Multi-Dispatch in the. Virtual Machine: Design and Implementation. Computing Science University of Saskatchewan

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

Oak Intermediate Bytecodes

Java Code Coverage Mechanics. by Evgeny Mandrikov at EclipseCon Europe 2017

Java Code Coverage Mechanics

Bytecode-based Analysis for Increasing Class-Component Testability

Java Code Coverage Mechanics Evgeny Mandrikov Marc Hoffmann #JokerConf 2017, Saint-Petersburg

JAM 16: The Instruction Set & Sample Programs

to perform dependence analysis on Java bytecode, we must extend existing dependence analysis techniques for adapting Java bytecode. In this paper we p

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

CSE 431S Code Generation. Washington University Spring 2013

Characterizing the SPEC JVM98 Benchmarks On The Java Virtual Machine

Last name:... First name:... Department (if not D-INFK):...

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

CPSC213/2014W1 Midterm EXTRA Practice

CSc 372 Comparative Programming Languages. Getting started... Getting started. B: Java Bytecode BCEL

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

Introduction Basic elements of Java

Transcription:

Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University

Phases Computing resources, such as: layout of data structures offsets register allocation Generating an internal representation of machine code for statements and expressions Optimizing the generated code (ignored for now) Emitting the code to files in assembler format Assembling the emitted code to binary format 2

Joos Compute offsets and signatures Generate code for static initializers Generate code for statements and expressions Optimize the generated code (ignored for now) Compute locals and stack limits Emit Jasmin code Assemble Jasmin code to class files 3

Computing Offsets Each formal and local variables must have an offset in the stack frame The this object always has offset 0 The naive solution: enumerate all formals and locals The better solution: reuse offsets for locals in disjoint scopes The clever solution: exploit liveness information must still respect the runtime types of locals Each slot in the local array must have a unique type at each location (but not necessarily unique across the whole method) 4

Naive Offsets public void m(int p 1, int q 2, Object r 3 ) { 4 int x = 42; int w 5 ; { int z 6 ; z = 87; { boolean a ; Object o 8 ; { boolean b 9 ; int z 10 ; b = true; boolean c 11 ; c = b && (x==87); { int y 12 ; y = x; 7 max = 12 5

Better Offsets public void m(int p 1, int q 2, Object r 3 ) { 4 int x = 42; int w 5 ; { int z 6 ; z = 87; { boolean a ; Object o 7 ; { boolean b 8 ; int z 9 ; b = true; boolean c 10 ; c = b && (x==87); { int y 8 ; y = x; 6 max = 10 6

Clever Offsets public void m(int p 1, int q 2, Object r 3 ) { 1 int x = 42; int w 2 ; { int z 2 ; z = 87; { boolean a ; Object o 2 ; { boolean b 2 ; int z 3 ; b = true; boolean c 3 ; c = b && (x==87); { int y 2 ; y = x; 2 max = 3 7

Computing Signatures (1/2) The function sig(σ) encodes a type: sig(void) = V sig(short) = S sig(char) = C sig(σ[]) = [desc(σ) sig(byte) = B sig(int) = I sig(boolean) = Z sig(c 1.C 2.....C k ) = C 1 /C 2 /.../C k desc(void) = V desc(byte) = B desc(short) = S desc(int) = I desc(char) = C desc(boolean) = Z desc(σ[]) = [desc(σ) desc(c 1.C 2.....C k ) = LC 1 /C 2 /.../C k ; 8

Computing Signatures (2/2) This extends to fields, methods, and constructors The field named x in class C: sig(c)/x The method σ m(σ 1 x 1,..., σ k x k ) in class C: sig(c)/m(desc(σ 1 )...desc(σ k ))desc(σ) The constructor C(σ 1 x 1,..., σ k x k ) in class C: sig(c)/<init>(desc(σ 1 )...desc(σ k ))V 9

Static Initializers (1/3) Initialization of static fields is performed when the class is loaded by the JVM All static fields are first given default values The code for initialization is written in a special method with the name <clinit> Fields that are static final and constant valued must then be initialized Finally, all other static fields are initialized 10

Static Initializers (2/3) public class A { public static int x = A.y+1; public static final int y = 42; public static void main(string[] args) { System.out.print(A.x); 43 public class A { public static int x = A.y+1; public static int y = 42; public static void main(string[] args) { System.out.print(A.x); 1 public class A { public static int x = A.y+1; public static final int y = A.fortytwo(); public static int fortytwo() { return 42; public static void main(string[] args) { System.out.print(A.x); 1 11

Static Initializers (3/3) class A { public static final int y = z; public static final int z = y; public static void main(string args[]) { System.out.println(y+z); Error: Use before definition class A { public static final int y = A.z; public static final int z = A.y; public static void main(string args[]) { System.out.println(y+z); 0

Generating Code Each statement and expression generates a sequence of bytecodes A code template shows how to generate bytecodes for a given language construct The template ignores the surrounding context And it ignores uniqueness of label names The given label names are symbolic; you have to make sure they are unique via Instruction.make_label This yields a simple, recursive strategy for the code generation 13

Code Template Invariants A statement and a void expression leaves the stack height unchanged A non-void expression increases the stack height by one This is a local property of each template The generated code must be verifiable This is not a local property, since the verifier performs a global static analysis 14

Code Templates (1/12) if(e) S 1 else S 2 E ifeq false S 1 goto endif false: S 2 endif: nop if(e) S E ifeq false S false: nop nop not strictly necessary b/c reachability constraints guarantee this is not the last instruction while(e) S goto cond: loop: S cond: E ifne loop while(true) S loop: S goto loop 15

Code Templates (2/12) { σ n = E; S E σstore offset(n) S σstore is either istore or astore depending on σ { σ n; S S E; E type(e) = void E; E pop type(e) void throw E; E athrow return E; E σreturn return; return 16

Code Templates (3/12) new C(E 1,...,E k ) @ δ new sig(c) dup E 1... E k invokespecial sig(δ) this(e 1,...,E k ) @ δ aload 0 E 1... E k invokespecial sig(δ) @ δ indicates that δ is the corresponding resolved declaration 17

Code Templates (4/12) super(e 1,...,E k ) @ δ aload 0 E 1 The current class contains the non-static field initializations: σ 1 x 1 = I 1 ;... σ n x n = I n ;... E k invokespecial sig(δ) aload 0 I 1 putfield sig(x 1 ) desc(σ 1 )... aload 0 I n putfield sig(x n ) desc(σ n ) 18

Code Templates (5/12) E.m(E 1,...,E k ) @ δ sig(δ) is on a class E.m(E 1,...,E k ) @ δ sig(δ) is on an interface E E 1... E k invokevirtual sig(δ) E E 1... E k invokeinterface sig(δ) C.m(E 1,...,E k ) @ δ E 1... E k invokestatic sig(δ) 19

Code Templates (6/12) (C)E E checkcast sig(c) (char)e E i2c E instanceof C E instanceof sig(c) 20

Code Templates (7/12) this aload 0 n type(n) = σ σload offset(n) E.f E getfield sig(f) desc(type(e.f)) C.f getstatic sig(f) desc(type(c.f)) E 1 [E 2 ] E 1 E 2 σaload type(e 1 [E 2 ]) = σ σaload is either iaload, baload, saload, caload, or aaload depending on σ 21

Code Templates (8/12) n = E type(n) = σ E dup σstore offset(n) E 1.f = E 2 E 1 E 2 dup_x1 putfield sig(f) desc(type(e 1.f)) C.f = E E dup putstatic sig(f) desc(type(c.f)) E 1 [E 2 ] = E 3 E 1 E 2 type(e 1 [E 2 ]) = σ E 3 dup_x2 σastore 22

Code Templates (9/12) new σ[e] E multianewarray desc(σ ) 1 type(new σ[e]) = σ E.length E arraylength E.clone() E invokevirtual sig(type(e))/clone()ljava/lang/object; 23

Code Templates (10/12) 42 ldc_int 42 true ldc_int 1 null aconst_null "abc" ldc_string "abc" 24

Code Templates (11/12) E 1 + E 2 E 1 type(e 1 +E 2 ) = int E 2 iadd E 1 + E 2 E 1 E type(e 2 1 +E 2 ) = String invokevirtual S/concat(LS;)LS; S java/lang/string - E E ineg 25

Code Templates (12/12) E 1 E 2 E 1 dup ifne firsttrue pop E 2 firsttrue: nop E 1 && E 2 E 1 dup ifeq firstfalse pop E 2 firstfalse: nop nops not strictly necessary b/c reachability constraints guarantee this is not the last instruction 26

Stack and Locals Limits The generated code must explicitly state: the maximal number of local and formal offsets the maximal local stack height This is used to determine the size of the frame The locals limit is the maximal offset + 1 The stack limit is computed by a static analysis 27

Stack Limit Analysis Consider the control flow graph of the bytecodes succ(s i ) denotes the set of successor bytecodes Δ(S i ) denotes the change in stack height by S i S 0 denotes the first bytecode For every bytecode S i we define the following integer-valued properties: B[[S i ]] denotes the stack height before S i A[[S i ]] denotes the stack height after S i 28

Dataflow Constraints B[[S 0 ]] = 0 A[[S i ]] = B[[S i ]] + Δ(S i ) x succ(s i ): A[[S i ]] = B[[x]] A[[S i ]] 0 These constraints must have a solution The stack limit is the largest value of any A[[S i ]] 29

Jasmin Class Format (1/3) The overall structure of a Jasmin file is:.source sourcefile.class modifiers name.super sig(superclass).implements sig(interface).field modifiers desc(type) constructors methods 30

Jasmin Class Format (2/3) The structure of a constructor is:.method modifiers sig(constructor).throws sig(exception).limit stack stacklimit.limit locals localslimit bytecodes.end method 31

Jasmin Class Format (3/3) The structure of a method is:.method modifiers sig(method).throws sig(exception).limit stack stacklimit.limit locals localslimit bytecodes.end method 32

A Tiny Class public class Foo { public int y = 42; public Foo(int z) { y = y+z; public String print(int n) { if (n==0) return new Integer(y).toString(); else return new Foo(y).print(n-1); 33

The Generated Code.source Foo.java.class public Foo.super java/lang/object.field public "y" I.method public <init>(i)v.limit stack 3.limit locals 2 aload_0 invokespecial java/lang/object/<init>()v aload_0 bipush 42 putfield Foo/y I aload_0 aload_0 getfield Foo/y I iload_1 iadd dup_x1 putfield Foo/y I pop return.end method.method public print(i)ljava/lang/string;.limit stack 3.limit locals 2 iload_1 ifne false0 new java/lang/integer dup aload_0 getfield Foo/y I invokespecial java/lang/integer/<init>(i)v invokevirtual java/lang/integer/tostring()ljava/lang/string; areturn false0: new Foo dup aload_0 getfield Foo/y I invokespecial Foo/<init>(I)V iload_1 iconst_1 isub invokevirtual Foo/print(I)Ljava/lang/String; areturn.end method 34

The Binary Class File cafe babe 0000 002e 001e 0c00 0a00 1901 0011 6a61 7661 2f6c 616e 672f 496e 7465 6765 7201 0010 6a61 7661 2f6c 616e 672f 4f62 6a65 6374 0900 0900 1701 0006 3c69 6e69 743e 0700 030c 0005 000d 0100 0346 6f6f 0700 0801 0005 7072 696e 740c 0016 001c 0a00 1d00 1501 0003 2829 5601 0004 436f 6465 0100 0179 0100 0a53 6f75 7263 6546 696c 6501 0001 4901 0004 2849 2956 0a00 0900 010a 0006 0007 0c00 0500 1201 0008 746f 5374 7269 6e67 0c00 0f00 1101 0008 466f 6f2e 6a61 7661 0100 1528 4929 4c6a 6176 612f 6c61 6e67 2f53 7472 696e 673b 0a00 0900 150a 001d 000b 0100 1428 294c 6a61 7661 2f6c 616e 672f 5374 7269 6e67 3b07 0002 0021 0009 0006 0000 0001 0001 000f 0011 0000 0002 0001 0005 0012 0001 000e 0000 0023 0003 0002 0000 0017 2ab7 0014 2a10 2ab5 0004 2a2a b400 041b 605a b500 0457 b100 0000 0000 0100 0a00 1900 0100 0e00 0000 3a00 0300 0200 0000 2e1b 039f 0007 03a7 0004 0499 0012 bb00 1d59 2ab4 0004 b700 0cb6 001b b0bb 0009 592a b400 04b7 001a 1b04 64b6 0013 b000 0000 0000 0100 1000 0000 0200 1800 35