Chapter 9 Subroutines and Control Abstraction. June 22, 2016

Similar documents
G Programming Languages - Fall 2012

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

Control Abstraction. Hwansoo Han

Programming Languages

Chapter 9 :: Subroutines and Control Abstraction

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

Chap. 8 :: Subroutines and Control Abstraction

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

Chapter 6 Control Flow. June 9, 2015

LECTURE 19. Subroutines and Parameter Passing

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

The Procedure Abstraction Part I: Basics

Functions in MIPS. Functions in MIPS 1

This section provides some reminders and some terminology with which you might not be familiar.

Chapter 3:: Names, Scopes, and Bindings (cont.)

Concepts Introduced in Chapter 7

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Procedures and Stacks

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

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

Compilers and computer architecture: A realistic compiler to MIPS

Programmiersprachen (Programming Languages)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Implementing Procedure Calls

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

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

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

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

CS558 Programming Languages

Today. Putting it all together

Weeks 6&7: Procedures and Parameter Passing

Programming Languages Third Edition. Chapter 7 Basic Semantics

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Chapter 3:: Names, Scopes, and Bindings

CA Compiler Construction

Chapter 10. Implementing Subprograms

G Programming Languages - Fall 2012

Lecture 5. Announcements: Today: Finish up functions in MIPS

Run-time Environments

Run-time Environment

Run-time Environments

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

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

Implementing Subroutines. Outline [1]

CS 61c: Great Ideas in Computer Architecture

The Procedure Abstraction Part I Basics

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

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

The Procedure Abstraction

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

Principles of Programming Languages

Code Generation & Parameter Passing

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

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

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

QUIZ Friends class Y;

Systems I. Machine-Level Programming V: Procedures

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

5. Semantic Analysis!

Run-Time Data Structures

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

CS 314 Principles of Programming Languages. Lecture 11

Functions CHAPTER 5. FIGURE 1. Concrete syntax for the P 2 subset of Python. (In addition to that of P 1.)

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

Topic 7: Intermediate Representations

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

COP4020 Programming Languages. Subroutines and Parameter Passing Prof. Robert van Engelen

Homework #3: Functions Calling Functions, and Recursion

Functions and Procedures

Optimizer. Defining and preserving the meaning of the program

Run-Time Environments

Semantic Analysis and Type Checking

Lecture 7: Binding Time and Storage

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

CS64 Week 5 Lecture 1. Kyle Dewey

Chapter 10. Implementing Subprograms ISBN

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

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

CSE 504: Compiler Design. Runtime Environments

CS A331 Programming Language Concepts

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

CS 314 Principles of Programming Languages

Compilers. Prerequisites

Slides for Lecture 6

6.001 Notes: Section 15.1

Languages and Compilers

CS 2210 Programming Project (Part IV)

CSE 401 Final Exam. December 16, 2010

Short Notes of CS201

Runtime Environments I. Basilio B. Fraguela

Implementing Subprograms

CS201 - Introduction to Programming Glossary By

Ch. 11: References & the Copy-Constructor. - continued -

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

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

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

Transcription:

Chapter 9 Subroutines and Control Abstraction June 22, 2016

Stack layout Common to have subroutine activation record allocated on a stack Typical fare for a frame: arguments, return value, saved registers, local variables, temporaries... Maybe the best part of using a stack: it s easy to deallocate (also, decent hardware support usually)

Stack layout Processors almost always support at least one stack; generally there is a register devoted to that stack called the stack pointer (usually abbreviated something like SP.) Multiple stack computer designs aren t as common, but allow for clean separation of return addresses, expression evaluation, argument lists, and so forth.

Stack layout While a stack pointer points to the active end of the stack, a frame pointer generally is used to point somewhere in the stack (usually at the current activation record). In the x86/x86 64 family, this register is usually BP/EBP/RBP.

Static and dynamic links

Static and dynamic links Dynamic links allow one to walk back the frame pointer linearly down the call stack. Static links allow one to walk back the frame pointer from a lexical viewpoint.

Calling sequence The maintenance of stacks (or, indeed, anything else, particularly registers) prior to and at the end of a subroutine invocation is called the calling sequence. While in assembly language programming this is often ad hoc, in higher languages, it is generally quite rigid. In most languages today, a single united stack is the center of the action.

A prologue The housekeeping which is done before entry into a subroutine is called the prologue; typically involve any needed set up of parameters, saving the return address (though usually this taken care by the CALL instruction), modifying the program counter (again, usually the bailiwick of the CALL instruction), moving the stack pointer for space allocation, saving registers, moving the frame pointer, and perhaps some initialization code.

An epilogue The housekeeping which must be done after a routine has finished is called the epilogue; return values have to adjudicated, stacks allocated must be deallocated, registers restored, and of course, moving the program counter (that is usually done by a RETURN instruction).

A matter of decisions As the text so correctly points out, figuring out who does what with the registers is critical to any calling sequence; indeed, with assembly language programming, it s usually the only significant issue in the calling sequence. Architectures offering large amounts of registers mean that this bounty can simply be split among the caller and callee.

Architecture (the actuality of the machine) plays a part with most implementations Ignoring the hardware is generally the wrong answer. Compilers on CISC machines tend to pass arguments on the stack; on RISC machines, they tend to use registers Compilers on CISC machines tend to dedicate a register for the frame; less likely to see this on RISC architecture As you might surmise, compilers on CISC architectures attempt usually to make use of that complexity.

In-line expansion Instead of actually calling a very simple non-generally-recursive routine with all of the calling sequences costs inherent, it often makes sense to simply do the code in place. Some languages offer the ability to hint to a compiler that such in-lining makes sense, such as some members of the C family and even Ada.

Parameter passing Generally we distinguish the formal parameters (those specified in the subroutine s definition) from the actual parameters passed (though such distinctions certainly don t exist in all languages, and don t exist in assembly language programming.) Call-by-value versus call-by-reference: in languages that have a value model, you have a bit of a dilemma when passing parameters: should you just pass the value, or should you pass a pointer to the value? If you pass the latter, it certainly makes it simple for the called subroutine to modify the underlying data. However, you quickly get quagmires associated with aliasing though you could remedy that by then not allowing any modifications of the state of any variable.

Parameter passing Closures as parameters: languages that allowing nesting and allow subroutines to be passed as parameters need closures to pass both the subroutine and its referencing environment.

Position parameters versus named parameters Instead of merely matching actual and formal parameters by their position, languages like Ada allow one to name parameters which is quite useful when you have the ability to give parameters default values; you just name values for the parameters that either do not have a default value, or ones that you wish to use a non-default value.

Variable numbers of arguments Recall the code for our RecursiveDescent parser: parser.c. At the end, we have an emit() function that allows a variable number of argments. This turns out to be fairly useful, though languages with a native list type already have a powerful mechanism for expressing a similar idea.

Returns In functional languages, generally the value of the body of the function specifies what is returned. In imperative languages, it s more common to have a explicit return() ; some languages allow the function to specify its return value by either allowing an assignment to the function s name, or having some syntax to specify a special name to refer to the value of a function.

Generics Very useful for creating containers, generics allow a programmer to specify a set of routines that can be defined over arbitrary types, and are quite analogous to macros in assembly language. Indeed, the most common implementation for generics is literally macro expansion, just as in assembly language. Your text distinguishes the two by the level of the rewriter: pure macro expansion is done outside the language as text-rewriting (for example, m4 could be used to do this), but generic expansion is done by the compilation environment, giving it an ability to make syntactic and semantic distinctions that m4 could not.

Exception handling and unwinding the stack Exceptions are unexpected/unusual situations that are not easily handled locally. Run-time errors, particularly those related to I/O, are often awkward at the point of contact, and often are more cleanly handled elsewhere. If the elsewhere is up the stack in a parent activation record, then the stack needs to be unwound to that point. Unwinding means not only popping off all those activation frames, but also restoring the state at the point of recovery in the propagation process.

Note on page 424 Exception-handling mechanisms are among the most complex aspects of modern language design, from both a semantic and a pragmatic point of view. Programmers have used subroutines since before there were compilers (they appear, among other places, in the 19th-century notes of Countess Ada Augusta Byron). Structured exceptions, by contrast, were not invented until the 1970s and did not become commonplace until the 1980s.

setjmp and longjmp Between the ad hoc methods often employed in languages like Pascal, which do not have explicit exception-handling, and structured exceptions lies the C solution of setjmp() and longjmp(); as the manual page says: setjmp() and sigsetjmp() make programs hard to understand and maintain. If possible an alternative should be used.

Co-routines These are pretty rare; I don t remember ever seeing these actually used anywhere (well, that is outside of assembly language; co-routines are trivial in assembly language), though apparently some languages do like to use these to implement iterators. As your text notes, threads are quite similar in many ways, and offer more functionality at a very modest price.