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

Similar documents
CSE 504: Compiler Design. Runtime Environments

Procedure and Object- Oriented Abstraction

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

THEORY OF COMPILATION

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

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

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

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

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

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

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

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

Compiler Construction

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

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

G Programming Languages - Fall 2012

Lecture 4 CIS 341: COMPILERS

Compiler Construction

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

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

Runtime Environments I. Basilio B. Fraguela

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

x86 assembly CS449 Fall 2017

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Assembly Language: Function Calls

LECTURE 19. Subroutines and Parameter Passing

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

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Assembly Language: Function Calls" Goals of this Lecture"

IA-32 Architecture. CS 4440/7440 Malware Analysis and Defense

PRESENTED BY: SANTOSH SANGUMANI & SHARAN NARANG

Concepts Introduced in Chapter 7

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

Assembly Language: Function Calls" Goals of this Lecture"

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

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

Chapter 9 :: Subroutines and Control Abstraction

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

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

Subprograms: Local Variables

x86 Assembly Tutorial COS 318: Fall 2017

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

The x86 Architecture

The Activation Record (AR)

Run-Time Environments/Garbage Collection

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

Procedure-Calling Conventions October 30

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

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

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 segment Stack segment

Code Generation & Parameter Passing

Special Topics: Programming Languages

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

Digital Forensics Lecture 3 - Reverse Engineering

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

Module 3 Instruction Set Architecture (ISA)

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

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

Practical Malware Analysis

Implementing Subroutines. Outline [1]

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

System Software Assignment 1 Runtime Support for Procedures

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

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

Implementing Subprograms

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

CA Compiler Construction

An Introduction to x86 ASM

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Functions and Procedures

Run-time Environment

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

Addressing Modes on the x86

The Procedure Abstraction

Microcomputer Architecture..Second Year (Sem.2).Lecture(2) مدرس المادة : م. سندس العزاوي... قسم / الحاسبات

CS 314 Principles of Programming Languages. Lecture 13

Topic 7: Activation Records

The Procedure Abstraction Part I Basics

CIT Week13 Lecture

Chapter 9. Subprograms

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Status. Lecture #15: Introduction to Runtime Organization. Run-time environments. Code Generation Goals and Considerations

Chapter 10. Implementing Subprograms

Run Time Environments

Scott M. Lewandowski CS295-2: Advanced Topics in Debugging September 21, 1998

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

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Control Abstraction. Hwansoo Han

CS401 Assembly Language Solved MCQS From Midterm Papers

Reverse Engineering II: Basics. Gergely Erdélyi Senior Antivirus Researcher

The IA-32 Stack and Function Calls. CS4379/5375 Software Reverse Engineering Dr. Jaime C. Acosta

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

CS61C : Machine Structures

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

Lecture 13: Garbage Collection

The Procedure Abstraction Part I: Basics

Computer Architecture and System Software Lecture 06: Assembly Language Programming

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

Transcription:

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

Subprograms General characteristics Single entry point Caller is suspended while subprogram is executing Control returns to caller when subprogram completes Procedure vs. function Functions have return values

Subprograms New-ish terms Header: signaling syntax for defining a subprogram Parameter profile: number, types, and order of parameters Signature/protocol: parameter types and return type(s) Prototype: declaration without a full definition Referencing environment: variables visible inside a subprogram Name space / scope: set of visible names Call site: location of a subprogram invocation Return address: destination in caller after call completes

Parameters Formal vs. actual parameters Formal: parameter inside subprogram definition Actual: parameter at call site Semantic models: in, out, in-out Implementations (key differences are when values are copied and exactly what is being copied) Pass-by-value (in, value) Pass-by-result (out, value) Pass-by-copy (in-out, value) Pass-by-reference (in-out, reference) Pass-by-name (in-out, name)

Parameters Pass-by-value Pro: simple Con: costs of allocation and copying Often the default Pass-by-reference Pro: efficient (only copy 32/64 bits) Con: hard to reason about, extra layer of indirection, aliasing issues Often used in object-oriented languages Pass-by-name Pro: powerful Con: expensive to implement, very difficult to reason about Rarely used!

Other Design Issues How are name spaces defined? Lexical vs. dynamic scope How are formal/actual parameters associated? Positionally, by name, or both? Are parameter default values allowed? Are method parameters type-checked? Statically or dynamically?

Other Design Issues Are local variables statically or dynamically allocated? Can subprograms be passed as parameters? How is this implemented? Shallow/dynamic, deep/static, or ad-hoc referencing environment? Can subprograms be nested? Can subprograms be polymorphic? Ad-hoc/manual, subtype, or parametric/generic? Are function side effects allowed? Can a function return multiple values?

Misc. Topics Macros Call-by-name, executed at compile time Closures A subprogram and its referencing environment Coroutines Co-operating procedures Just-in-time (JIT) compilation Defer compilation of each function until it is called

Subprogram Activation Call semantics: Save caller status Compute and save parameters Save return address Transfer control to callee Return semantics: Linkage contract (caller and callee must agree) Save return value(s) and out parameters Restore caller status Transfer control back to the caller Activation record: data for a single subprogram execution Local variables Parameters Return address Dynamic link

Standard Linkages Caller and callee must agree Standard contract: Caller: precall sequence Evaluate and push parameters Save return address Transfer control to callee Callee: prologue sequence Save & initialize base pointer Allocate space for local variables Callee: epilogue sequence De-allocate activation record Transfer control back to caller Caller: postreturn sequence Clean up parameters Caller Prologue Precall Postreturn Epilogue Callee Prologue Epilogue

x86 Stack Layout Address space Code, static, stack, heap Instruction Pointer (IP) Current instruction Stack pointer (SP) Top of stack Base pointer (BP) Start of current frame "cdecl" calling conventions callee must preserve bx, bp, sp, si (save/restore if used) callee may use ax, cx, dx, flags, st0-7, mm0-7, xmm0-15 parameters may be passed in di, si, dx, cx, r8-15, etc. return value saved in ax function stack frame starting at base pointer (bp)

x86 Calling Conventions Prologue: push %ebp mov %esp, %ebp sub X, %esp Within function: +OFFSET(%ebp) -OFFSET(%ebp) ; save old base pointer ; save top of stack as base pointer ; reserve X bytes for local vars ; function parameter ; local variable Epilogue: <optional: save return value in %eax> leave ; mov %ebp, %esp ; pop %ebp ret ; pop stack and jump to popped address Function calling: <push parameters> <push return address> <jump to fname> <pop parameters> ; precall ; call ; postreturn x86_64 "red zone" (128 bytes reserved below SP) - optimization: do not explicitly build frame (no SP manipulation)

Decaf Calling Conventions param instruction to pass parameters Pushed on system stack Accessible in function using [bp+offset] No need to manually pop after call call instruction to transfer control Save return address on stack Set up stack frame (BP and SP) Set IP to function entry point return instruction to return to caller Tear down stack frame (BP and SP) Set IP to return address Return value saved in "ret" special register

Heap Management Desired properties Space efficiency Exploitation of locality (time and space) Low overhead Allocation (malloc/new) First-fit vs. best-fit vs. next-fit Coalescing free space (defragmentation) Manual deallocation (free/delete) Dangling pointers Memory leaks

Automatic De-allocation Criteria: overhead, pause time, space usage, locality impact Basic problem: finding reachable structures Root set: static and stack pointers Follow pointers through heap structures Reference counting Catch the transition to unreachable Has trouble with cyclic data structures Mark and sweep (tracing) Occasionally pause and detect reachable High overhead and undesirable "pause the world" semantics Incremental collection: interleave computation and collection Partial collection: collect only a subset of memory on each run Generational collection: collect newer objects more often Collection can be parallelized to a certain extent

Object-Oriented Languages Classes vs. objects Inheritance relationships (subclass/superclass) Single vs. multiple inheritance Closed vs. open class structure Visibility: public vs. private vs. protected Static vs. dynamic dispatch Object-records and virtual method tables