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

Similar documents
Run-time Environments

Run-time Environments

Status. We ll do code generation first... Outline

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

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

Run-Time Organization

Code Generation. Lecture 30

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

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

Concepts Introduced in Chapter 7

The remote testing experiment. It works! Code Generation. Lecture 12. Remote testing. From the cs164 newsgroup

Code Generation. Lecture 19

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

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

Code Generation Super Lectures

Naming in OOLs and Storage Layout Comp 412

Code Generation. Lecture 12

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

Compilers and computer architecture: A realistic compiler to MIPS

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

CSC 1600 Memory Layout for Unix Processes"

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

CSE 504: Compiler Design. Runtime Environments

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

Run-time Environment

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

CSCI Compiler Design

Run-Time Environments

Procedure and Object- Oriented Abstraction

CIT Week13 Lecture

Storage in Programs. largest. address. address

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

Parsing Scheme (+ (* 2 3) 1) * 1

Special Topics: Programming Languages

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

System Software Assignment 1 Runtime Support for Procedures

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

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

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

Topic 7: Activation Records

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

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

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

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

The Procedure Abstraction

High-Level Language VMs

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Lecture 7: Binding Time and Storage

Advanced Programming & C++ Language

G Programming Languages - Fall 2012

Principles of Compiler Design

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

Implementing Subroutines. Outline [1]

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

Programming Languages: Lecture 12

CA Compiler Construction

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

x86 assembly CS449 Fall 2017

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

Compilers and Code Optimization EDOARDO FUSELLA

Midterm II CS164, Spring 2006

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Instructions: Assembly Language

Programming Languages

Scope, Functions, and Storage Management

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

Run-time Environments - 2

Today's Topics. CISC 458 Winter J.R. Cordy

Code Generation & Parameter Passing

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

Intermediate Representations & Symbol Tables

Compiler Design. Spring Run-Time Environments. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Multi-dimensional Arrays

Dynamic Memory Management! Goals of this Lecture!

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

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

THEORY OF COMPILATION

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Optimizer. Defining and preserving the meaning of the program

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Compiler Theory. (Semantic Analysis and Run-Time Environments)

NOTE: Answer ANY FOUR of the following 6 sections:

In Java we have the keyword null, which is the value of an uninitialized reference type

Control Abstraction. Hwansoo Han

G Programming Languages - Fall 2012

Dynamic Memory Management

Run-Time Data Structures

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

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

Operational Semantics of Cool

ECE260: Fundamentals of Computer Engineering

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Chapter 2A Instructions: Language of the Computer

o Code, executable, and process o Main memory vs. virtual memory

CSC 2400: Computer Systems. Using the Stack for Function Calls

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Transcription:

Run-time Environments Lecture 13 by Prof. Vijay Ganesh) Lecture 13 1

What have we covered so far? We have covered the front-end phases Lexical analysis (Lexer, regular expressions,...) Parsing (CFG, Top-down, bottom-up, ) Semantic analysis (Type systems, semantic actions) Next are the back-end phases Optimization Code generation We ll do code generation first... 2

Run-time environments Before discussing code generation, we need to understand what we are trying to generate There are a number of standard techniques for structuring executable code that are widely used 3

Outline Management of run-time resources Correspondence between static (compile-time) and dynamic (run-time) structures Storage organization 4

Run-time Resources Execution of a program is initially under the control of the operating system When a program is invoked: The OS allocates space for the program The code is loaded into part of the space The OS jumps to the entry point (i.e., main ) 5

Memory Layout Code Low Address Memory Other Space High Address 6

Notes By tradition, pictures of machine organization have: Low address at the top High address at the bottom Lines delimiting areas for different kinds of data These pictures are simplifications E.g., not all memory need be contiguous 7

What is Other Space? Holds all data for the program Other Space = Data Space Compiler is responsible for: Generating code Orchestrating use of the data area 8

Code Generation Goals Two goals: Correctness Speed Most complications in code generation come from trying to be fast as well as correct 9

Assumptions about Execution 1. Execution is sequential; control moves from one point in a program to another in a welldefined order 2. When a procedure is called, control eventually returns to the point immediately after the call Do these assumptions always hold? 10

Activations An invocation of procedure P is an activation of P The lifetime of an activation of P is All the steps to execute P Including all the steps in procedures P calls 11

Lifetimes of Variables The lifetime of a variable x is the portion of execution in which x is defined Note that Lifetime is a dynamic (run-time) concept Scope is a static concept 12

Activation Trees Assumption (2) requires that when P calls Q, then Q returns before P does Lifetimes of procedure activations are properly nested Activation lifetimes can be depicted as a tree 13

Example 1 Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g f g 14

Example 2 Class Main { g() : Int { 1 }; f(x:int): Int { if x = 0 then g() else f(x - 1) fi}; main(): Int {{f(3); }}; } What is the activation tree for this example? 15

Notes The activation tree depends on run-time behavior The activation tree may be different for every program input Since activations are properly nested, a stack can track concurrently active procedures 16

Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main Stack Main 17

Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g Stack Main g 18

Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g f Stack Main f 19

Example Class Main { g() : Int { 1 }; f(): Int { g() }; main(): Int {{ g(); f(); }}; } Main g f g Stack Main f g 20

Revised Memory Layout Code Low Address Memory Stack High Address 21

Activation Records The information needed to manage one procedure activation is called an activation record (AR) or frame If procedure F calls G, then G s activation record contains a mix of info about F and G. 22

What is in G s AR when F calls G? F is suspended until G completes, at which point F resumes. G s AR contains information needed to resume execution of F. G s AR may also contain: G s return value (needed by F) Actual parameters to G (supplied by F) Space for G s local variables 23

The Contents of a Typical AR for G Space for G s return value Actual parameters Pointer to the previous activation record The control link; points to AR of caller of G Machine status prior to calling G Contents of registers & program counter Local variables Other temporary values 24

Example 2, Revisited Class Main { g() : Int { 1 }; f(x:int):int {if x=0 then g() else f(x - 1)(**)fi}; main(): Int {{f(3); (*) }};} result AR for f: argument control link return address 25

Stack After Two Calls to f Main f (result) 3 f (*) (result) 2 (**) 26

Notes Main has no argument or local variables and its result is never used; its AR is uninteresting (*) and (**) are return addresses of the invocations of f The return address is where execution resumes after a procedure call finishes This is only one of many possible AR designs Would also work for C, Pascal, FORTRAN, etc. 27

The Main Point The compiler must determine, at compile-time, the layout of activation records and generate code that correctly accesses locations in the activation record Thus, the AR layout and the code generator must be designed together! 28

Example The picture shows the state after the call to the 2nd invocation of f returns Main f (result) 3 (*) f 1 2 (**) 29

Discussion The advantage of placing the return value 1st in a frame is that the caller can find it at a fixed offset from its own frame There is nothing magic about this organization Can rearrange order of frame elements Can divide caller/callee responsibilities differently An organization is better if it improves execution speed or simplifies code generation 30

Discussion (Cont.) Real compilers hold as much of the frame as possible in registers Especially the method result and arguments 31

Globals All references to a global variable point to the same object Can t store a global in an activation record Globals are assigned a fixed address once Variables with fixed address are statically allocated Depending on the language, there may be other statically allocated values 32

Memory Layout with Static Data Code Low Address Memory Static Data Stack High Address 33

Heap Storage A value that outlives the procedure that creates it cannot be kept in the AR method foo() { new Bar } The Bar value must survive deallocation of foo s AR Languages with dynamically allocated data use a heap to store dynamic data 34

Notes The code area contains object code For most languages, fixed size and read only The static area contains data (not code) with fixed addresses (e.g., global data) Fixed size, may be readable or writable The stack contains an AR for each currently active procedure Each AR usually fixed size, contains locals Heap contains all other data In C, heap is managed by malloc and free 35

Notes (Cont.) Both the heap and the stack grow Must take care that they don t grow into each other Solution: start heap and stack at opposite ends of memory and let them grow towards each other 36

Memory Layout with Heap Code Low Address Memory Static Data Stack Heap High Address 37

Data Layout Low-level details of machine architecture are important in laying out data for correct code and maximum performance Chief among these concerns is alignment 38

Alignment Most modern machines are (still) 32 bit 8 bits in a byte 4 bytes in a word Machines are either byte or word addressable Data is word aligned if it begins at a word boundary Most machines have some alignment restrictions Or performance penalties for poor alignment 39

Alignment (Cont.) Example: A string Hello Takes 5 characters (without a terminating \0) To word align next datum, add 3 padding characters to the string The padding is not part of the string, it s just unused memory 40

Next Topic: Stack Machines A simple evaluation model No variables or registers A stack of values for intermediate results Each instruction: Takes its operands from the top of the stack Removes those operands from the stack Computes the required operation on them Pushes the result on the stack 41

Example of Stack Machine Operation The addition operation on a stack machine 5 5 7 9 7 9 12 9 pop add push 42

Example of a Stack Machine Program Consider two instructions push i - place the integer i on top of the stack add - pop two elements, add them and put the result back on the stack A program to compute 7 + 5: push 7 push 5 add 43

Why Use a Stack Machine? Each operation takes operands from the same place and puts results in the same place This means a uniform compilation scheme And therefore a simpler compiler 44

Why Use a Stack Machine? Location of the operands is implicit Always on the top of the stack No need to specify operands explicitly No need to specify the location of the result Instruction add as opposed to add r 1, r 2 Smaller encoding of instructions More compact programs This is one reason why Java Bytecodes use a stack evaluation model 45

Optimizing the Stack Machine The add instruction does 3 memory operations Two reads and one write to the stack The top of the stack is frequently accessed Idea: keep the top of the stack in a register (called accumulator) Register accesses are faster The add instruction is now acc acc + top_of_stack Only one memory operation! 46

Stack Machine with Accumulator Invariants The result of an expression is in the accumulator For op(e 1,,e n ) push the accumulator on the stack after computing e 1,,e n-1 After the operation pops n-1 values Expression evaluation preserves the stack 47

Stack Machine with Accumulator. Example Compute 7 + 5 using an accumulator acc 7 5 12 stack 7 7 acc 7 push acc acc 5 acc acc + top_of_stack pop 48

A Bigger Example: 3 + (7 + 5) Code Acc Stack acc 3 3 <init> push acc 3 3, <init> acc 7 7 3, <init> push acc 7 7, 3, <init> acc 5 5 7, 3, <init> acc acc + top_of_stack 12 7, 3, <init> pop 12 3, <init> acc acc + top_of_stack 15 3, <init> pop 15 <init> 49

Notes It is very important evaluation of a subexpression preserves the stack Stack before the evaluation of 7 + 5 is 3, <init> Stack after the evaluation of 7 + 5 is 3, <init> The first operand is on top of the stack 50