Compiler Design Spring 2017

Similar documents
: Compiler Design

Compiler Design Spring 2017

System Software Assignment 1 Runtime Support for Procedures

Compiler Design Spring 2017

LECTURE 19. Subroutines and Parameter Passing

CS 314 Principles of Programming Languages. Lecture 13

: Compiler Design

Run-time Environments - 2

Compiler Design Spring 2017

Run-time Environments -Part 1

G Programming Languages - Fall 2012

Assembly Language: Function Calls

CA Compiler Construction

Compiler Design Spring 2018

Calvin Lin The University of Texas at Austin

Code Generation & Parameter Passing

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Programming Languages

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Run-time Environments - 3

Interprocedural Analysis. Motivation. Interprocedural Analysis. Function Calls and Pointers

Memory Management and Run-Time Systems

CSE 504: Compiler Design. Runtime Environments

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Lecture 5: Procedure Calls

Implementing Subroutines. Outline [1]

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

Principles of Programming Languages

Compiling Techniques

Chap. 8 :: Subroutines and Control Abstraction

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

Profilers and Debuggers. Introductory Material. One-Slide Summary

CS 314 Principles of Programming Languages

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

CS558 Programming Languages

Weeks 6&7: Procedures and Parameter Passing

Wednesday, October 15, 14. Functions

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

The Procedure Abstraction

Control Abstraction. Hwansoo Han

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

CIT Week13 Lecture

Arrays and Functions

Lecture 5: Procedure Calls

Chapter 9 :: Subroutines and Control Abstraction

Run-time Environments

CSE Lecture In Class Example Handout

Run-time Environments

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Code Generation. Lecture 12

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Chapter 9 Subprograms

Machine Programming 3: Procedures

Procedure and Object- Oriented Abstraction

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Administration CS 412/413. Advanced Language Support. First-class vs. Second-class. First-class functions. Function Types

Code Generation. Lecture 19

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Programming Languages: Lecture 12

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

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

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Run-Time Environments

Lecture 3: C Programm

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Systems I. Machine-Level Programming V: Procedures

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

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

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

Lecture 15a Persistent Memory & Shared Pointers

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Programming Languages: Lecture 11

Procedure and Function Calls, Part II. Comp 412 COMP 412 FALL Chapter 6 in EaC2e. target code. source code Front End Optimizer Back End

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

Module 27 Switch-case statements and Run-time storage management

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

Chapter 6 Introduction to Defining Classes

Compiler Design Spring 2017

Computer Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Chapter 10. Implementing Subprograms

18-600: Recitation #3

Chapter 5. Names, Bindings, and Scopes

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

a translator to convert your AST representation to a TAC intermediate representation; and

ECE 15B COMPUTER ORGANIZATION

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Compilers. 8. Run-time Support. Laszlo Böszörmenyi Compilers Run-time - 1

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

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

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Transcription:

Compiler Design Spring 2017 7.5 Method invocation Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation 1

Admin issues There will be a recitation session today In CAB G 11 @ 15:15 Discussion of Homework 4 (code generation) Feedback for HWP (design patterns) available Check your subversion repository Next lecture takes place next Thursday (May 4) 2

Outline 7.5.1 Call-by-value 7.5.2 Call-by-reference 7.5.3 Call-by-result 7.5.4 Call-by-name 7.5.5 Profiling 3

4

7.5.2 Call-by-reference Caller passes address to callee Could be address of an object a field a local variable an array element Indicate with keyword or symbol if a parameter is passed by reference Call-by-value usually supported as well Each parameter can be handled differently Example void foo(ref type x) { } // definition foo(ref y) // call site 5

7.5.2 Call-by-reference (cont d) Caller must evaluate address of parameter(s) Pass address from caller to callee Usually uses space that holds parameter Callee de-references actual to get value at address or to update storage location at the address Two steps Retrieve effective address Use address to read/write parameter 6

Discussion Advantages of call-by-reference Callee can modify variables in caller s scope Efficient passing of large objects/arrays [Maybe] restrict callee access to caller s objects Disadvantages of call-by-reference Callee can modify variables in caller s scope May be able to access any location Bug in callee may have global effect Aliasing the norm 8

Disadvantages (continued) Possible overhead and/or difficulties in optimization void foo (ref int x, ref int y) { int z; x = x + z ; y = z + 1 ; = x... ; } One extra step for each access Cannot assume that x is unchanged Must prohibit calls like foo(k m, ref x) Or (maybe) generate temporary for k m in caller 9

7.5.3 Call-by-result Call-by-result is a combination of Call-by-value (inside the callee) and Call-by-reference (upon reaching the end of the callee) Callee copies the local value of a parameter (value inside the callee) back to the caller Parameter x is like a local variable initialized by caller void foo(result int x) Effect of call obtained by inspecting x after return 11

void bar (result int x, result int y) { int a, b; x = x + 1; a = 2; y = y 1; b = 4; x = x + a; } // somewhere int k = 1; int m = 2; bar (k, m) // k == 4, m == 1 12

Discussion void foo(result int x) Callee needs address of x To store result at the end Implement passing of parameter(s) like call-by-reference Cannot allow foo(a b) Do we need a return statement? Yes: indication that parameters have meaningful value(s) No: value(s) copied back to caller in any case 13

Implementation void foo(result int x) Access to x inside method foo() different from call-byreference Callee cannot modify caller s context Need to create a temporary, initialize temporary with parameter value Upon return copy value from temporary to caller 14

Implementation (cont d) void bar(result int x, result int y) Use temporary (say xt, yt) Body of bar: xt = x; yt = y; xt = xt + 1; a = 2; yt = yt 1; b = 4; xt = xt + a; x = xt; y = yt; 15

Observation Final result depends on order of copying results Consider int k = 1; bar (k, k) // k == 4 or k == 0 void bar (result int x, result int y) { int a, b; x = x + 1; a = 2; y = y 1; b = 4; x = x + a; } Should be decided by language reference manual Either from y 1 y n or y n y 1 16

Discussion Advantages of call-by-result Multiple return values for a function Disadvantages of call-by-result Implementation overhead Aliasing makes programs difficult to understand 17

7.5.4 Call-by-name Idea: textual substitution of the formal parameter by the actual parameter Hand-off a variable to callee Example void foo (name int x, name int k) { k = 2; x = 5; k = 3; x = 1; } 18

Example (continued) Call site int [] A = new int[10]; int j; foo (A [j * 2], j) With call-by-name, the call means foo(a [j * 2], j) Execution k = 2; j = 2; x = 5; A[j*2] = 5; // A[4] k = 3; j = 3; x = 1; A[j*2] = 1; // A[6] void foo (name int x, name int k) { k = 2; x = 5; k = 3; x = 1; } 19

Discussion Who wants that? How does the compiler implement call-by-name? 20

Option 1: interpreter Use just-in-time compiler Invoke compiler at each call site, for each invocation Compiler automatically captures actual parameters 21

Option 2: stub generation Ahead-of-time compiler produces (for each call-by-name parameter) a stub that generates the address of the parameter Callee invokes stub to access a call-by-name parameter For each parameter P i the stub EP i yields the address 22

Example, continued foo(a [j * 2], j) EP 2 Get address of j Put address into location L 2 Continue as in call-by-reference EP 1 Get address of j Get value, compute 2, put value into temporary location T Get address of A[T] Put address into location L 1 Continue as in call-by-reference 23

Discussion Difficult to implement In ahead-of-time framework Potentially expensive execution Many indirections Frequent compilations (in just-in-time framework) Destroys modularity Cannot deduce effect of method foo() by inspecting body of foo() 24

Comparison (Not really JavaLi) No need to implement But you should know concepts Example Uses global variables Could be in an instance int j; int [] B = new int[2]; 25

x is passed by void quest( int x){ j = 0; x = x + 2; B[j] = 10; j = 1; x = x + 2; } void main() { B[0] = 1; B[1] = 1; j = 0; quest(b[j]); } x = 1 j = 0 x = 3 value reference result name B[0]=10 B[0]=10 B[0]=10 B[0]=10 j = 1 x = 5 } 10 1 1 @B[0]:1 @B[0]:1 B[j] j = 0 j = 0 j = 0 B[0]=3 j = 1 j = 1 j = 1 B[0]=12 } 12 1 1 xt = 3 xt = 5 }B[0]=xt 5 1 1 B[0]=3 B[1]=3 } 10 3 1 30

7.5.5 Profiling Gather information about a program s runtime behavior Today: Where does a program spend its execution time? Where == in which method(s) Guides optimization effort by programmer Profiling performed by external tools By inspecting stack layout E.g., perf_events on Linux, pstack on Solaris Requires close coupling between profilers / compilers Stack layout must allow inspection Symbol information must be available (at well-known location) 31

callee caller caller s caller Temp k... Temp 0 Local m Local 0 old SP old FP Return address target (parameter y0) parameter y1 parameter yn Return value Temp q Local 0 old SP old FP Return address Activation records revisited Stack pointer (%rsp) Frame pointer (%rbp) Question 1: How to get return address? Question 2: Where does old FP point to? Question 3: Where does Return address point to? 33

If an external tool were to interrupt program looking at %rip (instruction pointer) identifies currently executing method E.g., for the call hierarchy m1() à m2() à m3() à m4() %rip identifies m4() Looking at %rbp (frame pointer) gives access to Return address in m4() s activation record Identifies caller of m4() (i.e., m3()) Frame pointer of m3() Looking at frame pointer of m3() gives access to Return address of m3() s activation record and so on 34

Gathering information Interrupt program at regular time intervals Inspect stack (walk the stack), record data Interrupt #1 m1() à m2() à m3() à m4() Interrupt #2 m1() à m2() à m3() à m5() Interrupt #3 m1() à m2() à m3() à m4() Interrupt #4 m6() à m7() à m8() à m9() 35

Presenting result Many different ways to present data to user (programmer) Option #1: Present number of samples m1 m2 m3 m4 m5 m6 m7 m8 m9 # of samples 3 3 3 2 1 1 1 1 1 % of total 19% 19% 19% 13% 6% 6% 6% 6% 6% Problem: Not obvious that call chain m1() à m2() à m3() accounts for 75% of the execution time Other (better?) option: Flame graphs 36

Flame graphs Visualization that gives intuitive insight into call chains Many details missing: Lots of flat regions in Java part JVM JIT compilers do not maintain walkable stack activation records Frame pointer (%rbp) not maintained, used as general purpose register Also done by GCC (-fomit-frame-pointer) Temp k... Temp 0 callee Local m Local 0 old SP old FP Return address Frame pointer (%rbp) 37

Frame pointer support in the JVM Coupling JIT compilers / runtime system / profilers Change compilers to maintain the frame pointer Store program symbols at well-known location Where profiling tool can find them Available since Java 8u60 38

Flame graphs More on the topic http://techblog.netflix.com/2015/07/java-in-flames.html Includes before/after graphs shown in the lecture 39