CSc 520 Principles of Programming Languages

Similar documents
CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. ameter Passing Reference Parameter. Call-by-Value Parameters. 28 : Control Structures Parameters

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 520 Principles of Programming Languages

Chapter 9 :: Subroutines and Control Abstraction

CSc 520. Iterators. CLU-style Iterators... CLU-style Iterators. Principles of Programming Languages. Christian Collberg April 16, 2003

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

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

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

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Concurrent Programming

CSc 520 Principles of Programming Languages. Questions. rocedures as Control Abstractions... 30: Procedures Introduction

Control Abstraction. Hwansoo Han

CSc 520. Course Outline (Subject to change) Course Outline (Subject to change)... Principles of Programming Languages. Christian Collberg

CSc 520. Principles of Programming Languages 25: Types Introduction

CSc 520. Principles of Programming Languages 29: Control Flow Iterators

Intermediate Programming, Spring 2017*

CSc 453. Compilers and Systems Software. 18 : Interpreters. Department of Computer Science University of Arizona. Kinds of Interpreters

CSc 372. Comparative Programming Languages. 29 : Ruby Blocks. Department of Computer Science University of Arizona

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Implementation Alternatives. Lecture 2: Implementation Alternatives & Concurrent Programming. Current Implementation Alternatives

CSc 520 Principles of Programming Languages

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Chapter 9. Subprograms

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

Stack Vulnerabilities. CS4379/5375 System Security Assurance Dr. Jaime C. Acosta

CS429: Computer Organization and Architecture

Chap. 8 :: Subroutines and Control Abstraction

Implementing Subprograms

Course Administration

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

Programming Languages

CSc 520 Principles of Programming Languages

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

CSc 553. Principles of Compilation. 16 : OO Languages Polymorphism. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

CSc 520 Principles of Programming Languages

The ACK Modula-2 Compiler

CSc 520 Principles of Programming Languages

CSc 372 Comparative Programming Languages

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

What is Ruby? CSc 372. Comparative Programming Languages. 27 : Ruby Introduction. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 1 : Introduction. Department of Computer Science University of Arizona

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CSC 1600 Memory Layout for Unix Processes"

Inheritance. CSc 372. Comparative Programming Languages. 28 : Ruby Classes. Department of Computer Science University of Arizona.

MIPS Assembly (Functions)

CSc 372 Comparative Programming Languages

CSc 553. Principles of Compilation. 2 : Interpreters. Department of Computer Science University of Arizona

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

CSc 372. Comparative Programming Languages. 13 : Haskell Lazy Evaluation. Department of Computer Science University of Arizona

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Ruby Containers, Blocks, and Procs

EE 3170 Microcontroller Applications

Computer Systems Lecture 9

CSc 372 Comparative Programming Languages

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

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

Threads and Concurrency

Threads and Concurrency

Lecture 4: Threads; weaving control flow

Memory management COSC346

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

CSc 453 Interpreters & Interpretation

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

Today. Putting it all together

The Slide does not contain all the information and cannot be treated as a study material for Operating System. Please refer the text book for exams.

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

CSE 565 Computer Security Fall 2018

CSE 120 Principles of Operating Systems

CSc 372. Comparative Programming Languages. 3 : Haskell Introduction. Department of Computer Science University of Arizona

UNIVERSITY OF CALIFORNIA, RIVERSIDE

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

MIPS Assembly (FuncDons)

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

Reference Counting. Reference counting: a way to know whether a record has other users

ECE251: Tuesday September 18

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

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

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

Chapter 15: Information Flow

Compiling Code, Procedures and Stacks

CSC180: Lecture 2. Wael Aboulsaadat.

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

CSc 372 Comparative Programming Languages

Functions in MIPS. Functions in MIPS 1

The Kernel Abstraction

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSE Lecture In Class Example Handout

LECTURE 18. Control Flow

Run-time Environments - 2

CSc 620 Debugging, Profiling, Tracing, and Visualizing Programs. Getting started... Getting started. 2 : Java Bytecode BCEL

Transcription:

CSc 520 Principles of Programming Languages 32 : Control Structures Coroutines Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian Collberg April 9, 2008 1 Coroutines Coroutines are supported by Simula and Modula-2. They are similar to Java s threads, except the programmer has to explicitly transfer control from one execution context to another. Thus, like threads several coroutines can exist simultaneously but unlike threads there is no central scheduler that decides which coroutine should run next. A coroutine is represented by a closure. A special operation transfer(c) shifts control to the coroutine C, at the location where C last left off. 2 Coroutines vs. threads Coroutines are like threads, except that with a coroutine you have to explicitly say when you want to transfer from one process to another. If you have access to coroutines, you can build a thread library on top of it. A thread library makes sure that each process gets its fair share of the CPU. With coroutines we re forced to handle this ourselves, by making sure that we transfer control often enough to all coroutines. 3 Example The next slide shows an example from Scott where two coroutines execute concurrently, by explicitly transferring control between each other. In the example one coroutine displays a moving screen-saver, the other walks the file-system to check for corrupt files. 1

4 Example... var us, cfs: coroutine; coroutine update_screen() {... detach loop {... transfer(cfs)... coroutine check_file_system() {... main () {... 5 Coroutines... coroutine check_file_system() {... detach for all files do {... transfer(cfs)... transfer(cfs)... transfer(cfs)... main () { us := new update_screen(); cfs := new check_file_system(); transfer(us); Coroutines in Modula-2 6 Coroutines in Modula-2 Modula-2 s system module provides two functions to create and transfer between coroutines: PROCEDURE NEWPROCESS( proc: PROC; (* The procedure *) addr: ADDRESS; (* The stack *) size: CARDINAL; (* The stack size *) VAR new: ADDRESS); (* The coroutine *) PROCEDURE TRANSFER( VAR source: ADDRESS; (* Current coroutine *) VAR destination: ADDRESS); (* New coroutine *) 2

The first time TRANSFER is called source will be instantiated to the main (outermost) coroutine. 7 Coroutines in Modula-2... VAR crparams: CoroutineParameters; source: ADDRESS; (* current coroutine is called by this *) newcr: ADDRESS; (* coroutine just created by NEWPROCESS *) PROCEDURE Coroutine; VAR myparams: CoroutineParameters; BEGIN myparams := crparams; TRANSFER(newcr, source); (* return to calling coroutine *) (* rest of coroutine *) END Coroutine; PROCEDURE Setup(params: CoroutineParameters; proc: PROC); BEGIN NEWPROCESS(proc, addr, size, newcr); crparams := params; TRANSFER(source, newcr); END Setup; 3

Coroutines in Ruby 8 Coroutines in Ruby Ruby doesn t have co-routines per-se, but Marc De Scheemaecker has a simple library that we can use. class Coroutine # Creates a coroutine. The associated block # does not run yet. def initialize(&block) # Starts the block. It s an error to call # this method on a coroutine that has # already been started. def start # Switches context to another coroutine. You # need to call this method on the current coroutine. def switch(coroutine) 9 Coroutines in Ruby... # Returns true if the associated block is # started and has not yet finished def running? # Returns true if the associated block is # finished def finished? 10 Example c1 prints all letters, c2 prints the numbers 1...26. After printing a letter c1 swithces to c2, and vice versa. $c1 = Coroutine::new do for i in a.. z do printf "%s ", i $c1.switch($c2) $c2 = Coroutine::new do for i in 1..26 do printf "%i ", i 4

$c2.switch($c1) 11 Example... Running the example: $c1.start printf "\n" yields the result a 1 b 2 c 3 d 4 e 5 f 6 g 7 h 8 i 9 j 10 k 11 l 12 m 13 n 14 o 15 p 16 q 17 r 18 s 19 t 20 u 21 v 22 w 23 x 24 y 25 z 26 12 Iterators using coroutines If you have coroutines, implementing iterators becomes trivial! You need one coroutine to generate the values, and another as the main loop. Here, the iterate function creates a coroutine that generates the elements of an array. For simplicity, we store the result in a global variable $result: $result = 0 def iterate(arr,other) c = Coroutine::new do i = 0 while i < arr.length $result = arr[i] i += 1 c.switch(other) return c 13 Iterators using coroutines... Here s the main routine. It creates a coroutine, calls iterate to create the iterator coroutine, and then switches back and forth until the iterator is done: main = Coroutine::new do a = [1,2,3] iter = iterate(a,main) while not iter.finished? main.switch(iter) printf "%i ", $result printf "\n" 5

(This code is buggy. Please fix it for me!) 6

Implementing coroutines 14 Implementing coroutines Each coroutine needs its own stack. Each coroutine is represented by a context block. In our implementation, the context block contains only one value: the coroutine s stack pointer. 15 Implementing coroutines... When coroutine C2 issues a transfer(c1), the following happens: 1. transfer pushes all registers (including the return address RA on C2 s stack. 2. transfer saves the current stackpointer SP into C2 s context block. 3. transfer sets current coroutine to C1. 4. transfer sets SP to the stackpointer that was saved in C1 s context block. 5. transfer pops all saved registers off of C1 s stack, including the old return address, RA. 6. transfer does a normal procedure return, which has the effect of setting PC to RA, the location where C1 wants to continue executing. 16 Implementing coroutines... current_coroutine :=... PC :=... SP := transfer(c) { push R1,R2,RA *current_coroutine := SP current_coroutine := C SP := *C pop R1,R2,RA return 7

17 Step 1: Coroutine C2 is running C1 s code transfer(c2) C1 s stack R1 RA R2 C1 s context block SP1 C2 s code transfer(c1) C2 s stack C2 s context block SP2 PC SP current_coroutine 18 Step 2: Control is transferred to C2 C1 s code transfer(c2) C1 s stack C1 s context block SP1 C2 s code transfer(c1) C2 s stack RA R1 R2 C2 s context block SP2 19 Readings and References PC SP current_coroutine Read Scott, pp. 453 459 http://www.mathematik.uni-ulm.de/oberon/0.5/articles/coroutines.html The Ruby coroutine package is due to Marc De Scheemaecker, http://liber.sourceforge.net/ coroutines.rb. 8