Runtime Support for Algol-Like Languages Comp 412

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

Runtime Support for OOLs Object Records, Code Vectors, Inheritance Comp 412

Naming in OOLs and Storage Layout Comp 412

Arrays and Functions

The Procedure Abstraction

Optimizer. Defining and preserving the meaning of the program

What about Object-Oriented Languages?

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

Implementing Control Flow Constructs Comp 412

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

Run Time Environment. Activation Records Procedure Linkage Name Translation and Variable Access

CSE 504: Compiler Design. Runtime Environments

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Intermediate Representations

CS 314 Principles of Programming Languages

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Local Register Allocation (critical content for Lab 2) Comp 412

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Lab 3, Tutorial 1 Comp 412

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

The Processor Memory Hierarchy

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Parsing II Top-down parsing. Comp 412

Handling Assignment Comp 412

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

The Software Stack: From Assembly Language to Machine Code

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Instruction Selection: Preliminaries. Comp 412

CS 314 Principles of Programming Languages. Lecture 13

The Procedure Abstraction Part I: Basics

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Concepts Introduced in Chapter 7

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

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

Combining Optimizations: Sparse Conditional Constant Propagation

Runtime Environments I. Basilio B. Fraguela

Just-In-Time Compilers & Runtime Optimizers

Intermediate Representations

Syntax Analysis, III Comp 412

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Procedure and Object- Oriented Abstraction

The Procedure Abstraction Part I Basics

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Topic 7: Intermediate Representations

Special Topics: Programming Languages

Chapter 10 Implementing Subprograms

Chapter 9. Def: The subprogram call and return operations of a language are together called its subprogram linkage

CS 314 Principles of Programming Languages. Lecture 11

Lecture 7: Binding Time and Storage

CS 406/534 Compiler Construction Putting It All Together

Run-time Environments - 2

Implementing Subprograms

Chapter 3:: Names, Scopes, and Bindings

Class Information ANNOUCEMENTS

Programming Languages

Code Shape Comp 412 COMP 412 FALL Chapters 4, 5, 6 & 7 in EaC2e. source code. IR IR target. code. Front End Optimizer Back End

Building a Parser Part III

CA Compiler Construction

NOTE: Answer ANY FOUR of the following 6 sections:

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Overview of the Class

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

Control Abstraction. Hwansoo Han

Intermediate Representations & Symbol Tables

Habanero Extreme Scale Software Research Project

Syntax Analysis, III Comp 412

Instruction Selection, II Tree-pattern matching

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

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

Chap. 8 :: Subroutines and Control Abstraction

Run-time Environments

CMPE 152 Compiler Design

Run-time Environments

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Chapter 10. Implementing Subprograms

Programming Languages: Lecture 12

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

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

12/4/18. Outline. Implementing Subprograms. Semantics of a subroutine call. Storage of Information. Semantics of a subroutine return

Chapter 9 :: Subroutines and Control Abstraction

Code Shape II Expressions & Assignment

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation

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

CS/SE 153 Concepts of Compiler Design

Chapter 10. Implementing Subprograms ISBN

CSCI 565 Compiler Design and Implementation Spring 2014

Overview of the Class

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

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

CS 4100 Block Structured Languages. Fixed vs Variable. Activation Record. Activation Records. State of an Activation

Object Oriented Languages. Hwansoo Han

Compiler Design. Code Shaping. Hwansoo Han

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Topic 3-a. Calling Convention 2/29/2008 1

Transcription:

COMP 412 FALL 2018 Runtime Support for Algol-Like Languages Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Chapter 6 in EaC2e

Lab 3 Schedule Sunday Monday Tuesday Wednesday Thursday Friday Saturday Oct 21 22 23 24 25 26 Docs Available Lab Lecture Start work 27 You are here 28 29 30 31 1 2 Tutorial 4PM Complete & Correct Dependence Graph 4 5 6 7 8 9 10 Correct Schedule 3 11 12 13 14 15 16 17 The goal is here Improve Schedules 18 19 20 Lab 3 Due Date 21 22 23 24 No Class Thanksgiving Break (Walk) Late day Not a late day Not a late day Not a late day COMP 412, Fall 2018 1

Support for Name Spaces In an ALL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for In an OOL, the compiler needs Compile-time mechanism for name resolution Runtime mechanism to compute an address from a name Compiler must emit code that builds & maintains the runtime structures for This lecture focuses on runtime support for ALLs. COMP 412, Fall 2018 2

Where Do Variables Live? We have seen ARs, static data areas, global data areas, How does the compiler decide where to place each variable? A combination of visibility and lifetime determines that placement Lifetime Scope Location automatic local declaring procedure s AR static any named static data area for its lifetime dynamic any heap Variable length items? Keep descriptor in its natural place & allocate space in AR or heap Requires one level of indirection (a pointer ) Allows uniform In AR if AR is extendible On heap if AR is not extendible Automatic: Static: Lifetime matches procedure activation Lifetime may be as long as entire execution Dynamic: Lifetime is under program control & not COMP 412, Fall 2018 known at compile time 3

How Do We Address Variables? Local variables Need a mechanism to locate the local data area in appropriate AR Represent the variable as a static coordinate: <level, offset> Level is the lexical nesting level of the procedure that declares the variable Offset is the offset within the AR s local data area for that procedure Mechanism takes static coordinate to run-time address Static variables (including global) Need a base-address, offset pair (mangled name for base address) Emit a load of the base address Emit an add of offset and a load (loadai if offset is small enough) Must know that it is non-local case (represent differently) runtime compile time emit code at compile time; execute code at runtime compile time Dynamic variables Programmer manages access through pointers, names, not the compiler s problem COMP 412, Fall 2018 4

How Do We Address Variables? Local variable of current procedure Convert to static coordinate Add offset to r ARP and load Local variable in surrounding lexical scope Convert to static coordinate Convert level to ARP for procedure at that level Add offset to that ARP and load Must find the correct AR Local variables of surrounding scopes, static variables at various scopes, and global variables are often called free variables in the PL literature COMP 412, Fall 2018 5

Finding the Correct AR Each AR has a field for ARP r ARP r ARP is a physical register, dedicated to holding the ARP One common scheme uses access links Each AR has a link to the AR of its lexical ancestor (surrounding scope) Lexical ancestor need not be the procedure that called it Compiler can use static coordinate to generate code that chases the chain of access links to find the correct AR Reference to <p, 16> runs up chain to find level p s ARP, then adds 16 Number of derefences is (c p), where c is the current lexical level Cost of access is proportional to lexical distance between c and p COMP 412, Fall 2018 6

Using Access Links Finding the Correct AR ARP AR for foe AR for fie AR for fee procedure fee { procedure fie { procedure foe { } call foe() } call fie() } Static Coord Generated Code <3, 8> loadai r ARP,8 => r 10 <2,12> loadai r ARP,4 => r 10 loadai r 10,12 => r 10 <1,16> loadai r ARP,4 => r 10 loadai r 10,4 => r 10 loadai r 10,16 => r 10 COMP 412, Fall 2018 7

Finding the Correct AR Maintaining Access Links ARP At each call: In pre-call sequence, caller must find the appropriate ARP Assume caller is at level k Callee at level k+1 Use current ARP as link Callee at level j, 0 < j k Walk up access link chain to level j-1 Use that ARP as link Callee is global (e.g., at level 0) Use NULL as link Caller & callee share no lexical context As long as the code is correct Caller s chain will be long enough Callee cannot walk off end of chain COMP 412, Fall 2018 8

Finding the Correct AR Each AR has a field for ARP The Linkage Can Also Use A Display Global vector of pointers to the ARs of surrounding procedures Any nameable AR can be reached in constant time Current AR is reached through ARP Surrounding scopes are reached by treating display as a vector Reference to <p, 16>, for p < k, looks up the level p ARP in the display and adds 16 to it Cost of access is constant COMP 412, Fall 2018 9

Using A Display Finding the Correct AR _display_: level 1 level 2 level 3 ARP AR for foe AR for fie AR for fee procedure fee { procedure fie { procedure foe { } call foe() } call fie() } Static Coord Generated Code <3, 8> loadai r ARP,8 => r 10 <2,12> loadai _display_ => r 10 loadai r 10,4 => r 10 loadai r 10,12 => r 10 <1,16> loadi _display_ => r 10 load r 10 => r 10 loadai r 10,16 => r 10 COMP 412, Fall 2018 10

Finding the Correct AR ARP Maintaining A Display Display maintenance is simple, as long as the compiler writer is willing to waste one display slot In prolog of each level j procedure Save the level j display entry into the procedure s own AR ( slot) Store current ARP into level j display slot In epilog of each level j procedure Restore old level j display entry from the current AR s slot Simple, clean, and constant time COMP 412, Fall 2018 11

Access Links versus Display Access Links Maintenance adds overhead to each call Overhead depends on nesting Adds overhead on each reference to a local in surrounding scope Overhead depends on nesting If ARs outlive procedure, access links still work Access links between ARs ensures that they are live (& uncollected) Display Maintenance adds overhead to each call & return Overhead is constant Adds overhead on each reference to a local in surrounding scope Overhead is constant If ARs outlive procedure, display links are not preserved Need to preserve them in a way that ensures ARs are live (collector issue) Your mileage will vary Depends on ratio of non-local accesses to calls Depends on demand for (available register to hold _display_?) COMP 412, Fall 2018 12

Access Links and Displays The Meta Issue Decision to use one or the other is made when the linkage is designed May be beyond the control of the compiler writer Lexical levels for procedures and variables are determined in parsing Requires the use of a lexically scoped symbol table Code to maintain access link or display is emitted at compile time When compiler generates a procedure or a call, it emits the code Access links: Lexical levels are known at compile time, so compiler can generate the code to find a level j display (knows how far to run up the chain) Display: Lexical levels are known at compile time, so compiler can generate the save (in prolog) and restore (in epilog) Either an access link or a display is a runtime object Exists at runtime, Points to ARs, which are runtime objects COMP 412, Fall 2018 13

Other Runtime Services Stacks Growth space for stacks Heap Code Globals Java Memory Layout We have talked about storage for ARs, for static & global data, for heap allocated data. What other runtime services are needed? Input & output subsystems, including files systems, sockets, streams, pipes, DMA, network connections, and the like Process management, including concurrency Interfaces to accelerators, such as GPUs, FPGAs, other processors Heap management, including allocation, deallocation, and collection COMP 412, Fall 2018 14

12 10 COMP 412, MidTerm Exam, Fall 2018 Max 97.5 Min 23 Mean 66.7 Median 73 Std Dev. 19.5 Number of Students 8 6 4 2 0 95 to 100 90 to 94 85 to 89 80 to 84 75 to 79 70 to 79 65 to 69 60 to 64 55 to 59 50 to 54 45 to 49 40 to 44 35 to 39 30 to 34 25 to 29 20 to 24 COMP 412, Fall 2018 15