Compiler Design Spring 2017

Similar documents
Process Layout and Function Calls

Machine Program: Procedure. Zhaoguo Wang

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Credits and Disclaimers

Assembly Language: Function Calls

1 Number Representation(10 points)

Assembly Language: Function Calls" Goals of this Lecture"

Assembly Language: Function Calls" Goals of this Lecture"

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

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

Compiler Design Spring 2017

Computer Systems C S Cynthia Lee

Process Layout, Function Calls, and the Heap

CS61 Section Solutions 3

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

x86 Assembly Crash Course Don Porter

W4118: PC Hardware and x86. Junfeng Yang

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

How Software Executes

Machine Programming 3: Procedures

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

Function Call Convention

Computer Systems Organization V Fall 2009

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

CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name:

Digital Forensics Lecture 3 - Reverse Engineering

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

University of Washington

Practical Malware Analysis

x86 assembly CS449 Fall 2017

Where We Are. Optimizations. Assembly code. generation. Lexical, Syntax, and Semantic Analysis IR Generation. Low-level IR code.

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

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

Generation. representation to the machine

Lecture 10 Return-oriented programming. Stephen Checkoway University of Illinois at Chicago Based on slides by Bailey, Brumley, and Miller

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

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

Overview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading

Second Part of the Course

THEORY OF COMPILATION

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

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

CPEG421/621 Tutorial

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

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

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

CSE 351 Midterm - Winter 2015 Solutions

CS429: Computer Organization and Architecture

Instructor: Alvin R. Lebeck

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

CS241 Computer Organization Spring 2015 IA

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

C to Assembly SPEED LIMIT LECTURE Performance Engineering of Software Systems. I-Ting Angelina Lee. September 13, 2012

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CS Bootcamp x86-64 Autumn 2015

Credits and Disclaimers

Run-time Environments

Run-time Environments

Assignment 11: functions, calling conventions, and the stack

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

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

Multiprocessor Solution

Sungkyunkwan University

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

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Machine-level Programs Procedure

CS 161 Computer Security. Week of January 22, 2018: GDB and x86 assembly

IA-32 & AMD64. Crash Dump Analysis 2015/2016. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics.

Winter Compiler Construction T10 IR part 3 + Activation records. Today. LIR language

CS-220 Spring 2018 Final Exam Version Practice May 10, Name:

Procedures and the Call Stack

Lecture 4 CIS 341: COMPILERS

Do not turn the page until 5:10.

JDart. Rémi Forax JVM Summit'12

Assembly I: Basic Operations. Jo, Heeseung

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

ASSEMBLY I: BASIC OPERATIONS. Jo, Heeseung

Java Internals. Frank Yellin Tim Lindholm JavaSoft

x86 architecture et similia

Compiler Design Spring 2017

Machine/Assembler Language Putting It All Together

How Software Executes

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

Instruction Set Architecture

CPS104 Recitation: Assembly Programming

CSE351 Spring 2018, Midterm Exam April 27, 2018

The Hardware/Software Interface CSE351 Spring 2013

Compiler Construction D7011E

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

UW CSE 351, Winter 2013 Midterm Exam

X86 Stack Calling Function POV

Introduction to 8086 Assembly

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

CS 318 Principles of Operating Systems

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

CSE 351 Midterm - Winter 2015

Transcription:

Compiler Design Spring 2017 6.0 Runtime system and object layout Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1

Runtime system Some open issues from last time Handling stack overflows Safepoint support Good examples for compiler-runtime coupling Not required for JavaLi 2

Generated code static int m(int i) { return m(i + 1); } SOE.m(I)I [0x00007f1eaa4851c0, 0x00007f1eaa485228] 104 bytes [Entry Point] [Verified Entry Point] [Constants] # {method} {0x00007f1e6fc01250} 'm' '(I)I' in 'SOE # parm0: rsi = int # [sp+0x20] (sp of caller) ;; N1: # B1 <- B2 B3 Freq: 1 ;; B1: # B3 B2 <- BLOCK HEAD IS JUNK Freq: 1 0x00007f1eaa4851c0: mov %eax,-0x16000(%rsp) 0x00007f1eaa4851c7: push %rbp 0x00007f1eaa4851c8: sub $0x10,%rsp ;*synchronization entry ; - SOE::m@-1 (line 3) 0x00007f1eaa4851cc: inc %esi ;*iadd {reexecute=0 rethrow=0 return_oop=0} ; - SOE::m@2 (line 3) 0x00007f1eaa4851ce: nop 0x00007f1eaa4851cf: callq 0x00007f1ea2ad29a0 ; ImmutableOopMap{} ;*invokestatic m {reexecute=0 rethrow=0 return_oop=0} ; - SOE::m@3 (line 3) ; {static_call} ;; B2: # N1 <- B1 Freq: 0.99998 0x00007f1eaa4851d4: add $0x10,%rsp 0x00007f1eaa4851d8: pop %rbp 0x00007f1eaa4851d9: test %eax,0x1c8dee21(%rip) # 0x00007f1ec6d64000 ; {poll_return} 0x00007f1eaa4851df: retq Categories of assembly instructions: program semantics reserve/release stack space set up frame stack overflow detection safepoint support ;*invokestatic m {reexecute=0 rethrow=0 return_oop=0} ; - SOE::m@3 (line 3) 6

Stack overflow support Requires a single instruction At the beginning of every compiled method mov %eax,-0x16000(%rsp) // AT&T syntax How does it work? Hint: Mechanism similar to implicit null checks Hint: Mechanism uses memory protection 7

8

Safepoint support Garbage collector (GC) modifies heap graph Not OK while program is running (in general) Program must stop to let GC proceed GC signals need for safe point E.g., Safe point needed because VM is running low on memory Once all program threads stopped: GC can proceed Notification mechanism: polling Program polls regularly if need to go to safepoint How to poll efficiently? 9

Safepoint support (cont d) Compiler inserts an extra instruction before method returns test %eax,0x1c8dee21(%rip) # 0x00007f1ec6d64000 Also to loop back-edges (ensures short wait times) How does it work? Hint: Mechanism similar to implicit null checks Hint: Mechanism uses memory protection 11

12

Outline 6.1 Introduction 6.2 Basic types 6.3 Object instances and references 6.4 Inheritance 6.5 Program startup 6.6 Handling stack overflows & GC safepoints Extra question 13

15

Extra question Look (again) at the code generated by the HotSpot JVM for the SOE::m(int i) method. Why did the compiler generate two method entry points? Entry Point Verified Entry Point 16

18

Extra question Look (again) at the code generated by the HotSpot JVM for the SOE::m(int i) method. Why did the compiler generate two method entry points? Entry Point Verified Entry Point Observation: There is no difference between the two entry points. (They both act as a label for the first instruction of the method.) Let s change method m(int i) from static to virtual (instance method) and see what happens. static int m(int i) { return m(i + 1); } 19

Generated code static int m(int i) { return m(i + 1); } SOE.m(I)I [0x00007f354a4851c0, 0x00007f354a485228] 104 bytes [Entry Point] [Constants] # {method} {0x00007f350db00258} 'm' '(I)I' in 'SOE # this: rsi:rsi = 'SOE # parm0: rdx = int # [sp+0x20] (sp of caller) ;; N34: # B1 <- BLOCK HEAD IS JUNK Freq: 1 0x00007f354a4851c0: cmp 0x8(%rsi),%rax 0x00007f354a4851c4: jne 0x00007f3542ad2220 ; {runtime_call ic_miss_stub} 0x00007f354a4851ca: nop 0x00007f354a4851cb: nop 0x00007f354a4851cc: nop 0x00007f354a4851cd: nop 0x00007f354a4851ce: nop 0x00007f354a4851cf: nop [Verified Entry Point] ;; B1: # B3 B2 <- BLOCK HEAD IS JUNK Freq: 1 0x00007f354a4851d0: mov %eax,-0x16000(%rsp) 0x00007f354a4851d7: push %rbp 0x00007f354a4851d8: sub $0x10,%rsp ;*synchronization entry ; - SOE::m@-1 (line 3) 0x00007f354a4851dc: inc %edx ;*iadd {reexecute=0 rethrow=0 return_oop=0} 21

Inline caching Call sites of method m()may contain a direct call to SOE::m() Costly vtable lookup avoided But m() is virtual: Call can be to m() in a sub/superclass of SOE Verification needed (Unverified) entry point performs a dynamic type check Is the receiver the same as the type declaring the method? YES: Continue execution NO: Update call site to perform proper vtable lookup Other actions possible as well Approach is called inline caching Implemented in different language VMs (Self, HotSpot, V8) 22

Compiler Design Spring 2017 7.0 Code generation Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 23

Code generation Input: IR (operator trees, AST) Output: Machine code (assembly) Crucial part of compiler In many compilers, most effort devoted to code generation (and establishing that conditions for optimization are met) Many compilers start with existing front-end/semantic analyzers Key requirement: correctness Sometimes: three requirements instead Performance, performance, performance Until correctness becomes important again (usually very quickly) 24

Code generator Major activities of a code generator 1. Code selection Decide on machine instruction(s) that are generated for a given IR node or group of IR nodes 25

Code selection Consider x = y x 1 Assume x is in %eax, y is in %ecx One possible code sequence (AT&T syntax) subl %eax, %ecx movl %ecx, %eax decl %eax movl %eax, x Another option notl %eax addl %ecx, %eax movl %eax, x 26

Code selection Swap x, y Assume x is in %eax, y is in %ecx Option 1: movl %eax, %edx movl %ecx, %eax movl %edx, %ecx Option 2 xchg %eax, %ecx Finding good sequences (maybe) far from easy Not always clear what sequence is fastest May depend on processor model 27

Code generator Major activities of a code generator 1. Code selection Decide on machine instruction(s) that are generated for a given IR node or group of IR nodes 2. Code scheduling Determines order of execution of (unrelated) instructions 28

Code scheduling Not much to do for single small tree Options for multiple trees Consider a = b + c; x = y + 1; Assume that memory read access takes 2 cycles 29

Code scheduling (cont d) a = b + c; x = y + 1; Option 1 movl b, %eax addl c, %eax movl %eax, a movl y, %ecx incl %ecx movl %ecx, x Option 2 movl b, %eax movl y, %ecx addl c, %eax incl %ecx movl %eax, a movl %ecx, x Not always clear which option is faster Processors reorder instructions on-the-fly Caching affects performance as well 30

Code generator Major activities of a code generator 1. Code selection Decide on machine instruction(s) that are generated for a given IR node or group of IR nodes 2. Code scheduling Determines order of execution of (unrelated) instructions 3. Register allocation and assignment Allocation: decide which operand goes into a register Assignment: decide which register holds a given operand 31

Register allocation Consider (in some class C) int inc(int x) { return x + 1; } int foo(int p) { int a, b, c; a = p + 1; b = inc(a); c = a + b; return c; } 32

Register allocation Assume that method inc() uses (and destroys) the %eax register. Return value in %eax Method foo() has two options int inc(int x) { return x + 1; } int foo(int p) { int a, b, c; a = p + 1; b = inc(a); c = a + b; return c; } 33

Options for register allocation Option 1: use %eax for first stmt Evaluate(p+1)into %eax Invoke inc() Reload a into %ebx Add %ebx to %eax Option 2: don t use %eax for first stmt Evaluate (p+1) into %ebx Invoke inc Add %ebx and %eax 2 nd option saves (re)loading a int inc(int x) { return x + 1; } int foo(int p) { int a, b, c; a = p + 1; b = inc(a); c = a + b; return c; } Register requirements of inc()may not be known at the time that foo() is compiled 34

Bad (interesting) news Code selection Register allocation Code scheduling Code selection depends on code scheduling Code scheduling depends on register allocation Register allocation depends on code selection Close coupling 35

Different register use for a = b + c; x = y + 1; Option 1 movl b, %eax addl c, %eax movl %eax, a movl y, %eax incl %eax movl %eax, x Option 2 movl b, %eax movl y, %eax addl c, %eax incl %eax movl %eax, a movl %eax, x 36