上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Parameter Passing Mechanisms

Parameter Passing Mechanisms CS308 Compiler Theory
Parameter Passing Mechanisms CS308 Compiler Theory

Terms and Definitions Formal Parameters:specified(together with type)when procedure is declared (a.k.a.formals) Actual Parameters:values which are passed to a procedure at call site (a.k.a.actuals) I-value:storage location represented by an expression (e.g.a register,a memory location,etc) r-value:value contained at the storage location ·I-andr-refer to the“left”and“right"side of an assignment int factorial((nt n) formal if (n =0)return 1; else return n factorial(n 1) } actual factorial(42) CS308 Compiler Theory 2
Terms and Definitions • Formal Parameters: specified (together with type) when procedure is d l d( k dec lare d (a. k.a. f l orma ls ) • Actual Parameters: values which are passed to a procedure at call site ( k a. k.a. act l ua ls ) • l-value: storage location represented by an expression (e.g. a register, a memory location etc) memory location, etc) • r-value: value contained at the storage location • l - and r - refer to the refer to the “left ” and “right ” side of an assignment side of an assignment int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); formal } … actual 2 factorial(42); CS308 Compiler Theory

Call-by-value Simplest possible approach: a formal is treated the same as a local (i.e.storage is allocated on the stack or in a register) the caller evaluates the actuals and passes the result to the callee Operations on the formals do not affect values in the stack frame of the caller,so the following will not work: void swap(int a,int b){ int temp; temp a;a =b;b temp; CS308 Compiler Theory 3
Call-by-value • Simplest possible approach: – a formal is treated the same as a local (i.e. storage is allocated on the stack or in a register) – the caller evaluates the actuals and passes the result to the callee • Operations on the formals Operations on the formals do not affect values in the stack frame of the affect values in the stack frame of the caller, so the following will not work: void swap( , ) { int a, int b) { int temp; temp = a; a = b; b = temp; } 3 CS308 Compiler Theory

Call-by-reference Also known as:call-by-address,call-by-location The location of the actual is passed,rather then its value: if the actual is a variable (i.e.corresponds to an assignable location in memory)its address is passed -if the actual is an expression,the expression is evaluated to a temporary location and the address of that location is passed (the compiler will warn you since this is not what you usually want) Operations on the formals do affect the values in the caller,so procedure swap now works: void swap(int&a,int&b){ int temp; temp =a;a =b;b temp; CS308 Compiler Theory 4
Call-by-reference • Also known as: call-by-address, call-by-location • The location of the actual is passed, rather then its value: – if the actual is a variable (i.e. corresponds to an assignable location in memory) its address is passed is passed – if the actual is an expression, the expression is evaluated to a temporary location and the address of that location is passed (the compiler will warn you since this is not what you usually want) usually want) • Operations on the formals do affect the values in the caller, so p ocedu e rocedure sw pa now wo s: rk void swap(int& a, int& b) { int temp; temp = a; a = b; b = temp; } 4 CS308 Compiler Theory

Copy-restore Also known as:copy-in copy-out,value-result Combination of call-by-value and call-by-reference: the actuals are evaluated before the call the expressions having only r-values, the expressions having l-values are passed by reference CS308 Compiler Theory 5
Copy-restore • Also known as: copy-in copy-out, value-result • Combination of call-by-value and call-by-reference: – the actuals are evaluated before the call – the e pressions ha ing onl r the expressions having only r-val es u , – the expressions having l-values are passed by reference 5 CS308 Compiler Theory

Call-by-Name The evaluation of actuals is delayed until their use in callee Can be thought of as in-line expansion (but isn't!) Can be implemented by using parameterless evaluation procedures (sometimes called thunks)that are created for each actual: int thunk 1()( return 1 2; } void foo(int a,int b){ int thunk 2()( …a…b… return 3; } } void foo(proc f,proc g){ fo0(1+2,3); …p()…q()… foo(thunk 1,thunk 2); CS308 Compiler Theory 6
Call-by-Name • The evaluation of actuals is delayed until their use in callee • Can be thought of as in-line expansion (but isn’t!) • Can be implemented by using parameterless evaluation procedures (sometimes called thunks) that are created for each actual: int thunk_1() { return 1 + 2; } void foo(int a, int b) { … a … b … } int thunk_2() { return 3; } … foo(1+2, 3); void foo(proc f, proc g) { … p() … q() … } … … foo(thunk_1, thunk_2); … 6 CS308 Compiler Theory

Procedures as Parameters Some languages(such as C and Scheme)proved first-class procedure values:such values may be stored in a variable or returned by functions This creates many run-time issues: int main(){ proc make incrementer(int n){ int incrementer(int x){ return x n; 3 return incrementer; } proc my incr make incrementer(42); int y my incr(5); CS308 Compiler Theory 7
Procedures as Parameters • Some languages (such as C and Scheme) proved first-class procedure values: such values may be stored in a variable or returned by functions • This creates many run-time issues: int main() { proc make incrementer(int n) { proc make _ incrementer(int n) { int incrementer(int x) { return x + n; } return incrementer; } … proc my incr make incrementer(42); proc my _ incr = make _ incrementer(42); int y = my_incr(5); } 7 CS308 Compiler Theory

Call Scenario Consider what happens when we call cllallllllllllllllllllllllle main () local:my incr local:y main ()'s stack frame is set up control link call to make_incrementer ( access link return from make incrementer ( formal:n -PROBLEM:can't call my_incr (because the stack frame in which n resides is destroyed!!! CS308 Compiler Theory
Call Scenario • Consider what happens when we call main(): i ()’ t kf i t local: my_incr local: y control link access link formal: n – main()’s stack frame is set up – call to make_incrementer() – return from make_incrementer() control link access link formal: n – PROBLEM: can’t call my_incr() because the stack frame in which n resides is destroyed!!! 8 CS308 Compiler Theory

Solutions Solution 1 (a la C):disallow nested procedures: when we have no nested procedures,we never access non-static non-locals and so we need not preserve our caller's stack frame Solution 2(a la Pascal):disallow returning procedures from the scope where they are created: when we call a procedure indirectly,we are guaranteed that the stack frames for scopes within which it is nested exist on stack this requires eliminating assignment of procedures to local variables,but allows for passing procedures as parameters Solution 3(a la Lisp/Scheme):use tree instead of stack for procedure's local data this creates a record for each procedure(called closure)when a procedure is defined(not when it is called) allocating closures is costly;freeing requires a garbage collector (to be discussed later in the course) CS308 Compiler Theory
Solutions • Solution 1 (a la C): disallow nested procedures: – when we have no nested procedures, we never access non-static non-locals and so we need not preserve our caller’s stack frame • Solution 2 (a la Pascal): disallow returning procedures from the scope Solution 2 (a la Pascal): disallow returning procedures from the scope where they are created: – when we call a procedure indirectly, we are guaranteed that the stack frames for scopes within which it is nested exist on stack – this requires eliminating assignment of procedures to local variables, but allows for passing procedures as parameters • Solution 3 (a la Lisp/Scheme): use tree instead of stack for procedure’s local data – this creates a record for each procedure (called closure) when a procedure is defined (not when it is called) – allocating closures is costly; freeing requires a garbage collector (to be discussed later in 9 the course) CS308 Compiler Theory

Implementing Object-Oriented Features CS308 Compiler Theory
Implementing Object-Oriented Features CS308 Compiler Theory
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_CS308 Compiler Theory.pdf
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT15 并发与多线程.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT14 Python GUI工具包:Tkinter.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT13 算法设计分析.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT12 面向对象设计.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT11 数据集合体.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT10 类的定义.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT09 模拟设计.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT08 控制结构——循环语句.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT07 控制结构——条件语句.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT06 函数.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT05 面向对象与图形编程.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT04 字符串计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT03 数值计算.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT02 程序构件.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(PPT课件讲稿)CT01 绪论.ppt
- 上海交通大学:《程序设计思想与方法》课程教学资源(课程教材)简明Python教程(PDF版).pdf
- 《程序设计思想与方法》课程教学资源(课程教材)Python Programming:An Introduction to Computer Science.pdf
- 《程序设计思想与方法》课程教学资源(课程教材)How to Think Like a Computer Scientist(Learning with Python).pdf
- 上海交通大学:《面向对象分析与设计 Object Oriented Analysis and Design》课程教学资源(讲稿)软件开发过程.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_Introduction to Compilin.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第一周讲义_A Simple Syntax-Directed Translator.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第七周讲义_Code Generation Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Top-Down Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第三周讲义_Bottom-Up Parsing.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_CS308 Compiler Theor.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第九周讲义_Machine-Independent Optimizations Ⅳ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Lexical Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_Syntax Analyzer.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第二周讲义_lex.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第五周讲义_Type Checking.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅰ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅱ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第八周讲义_Machine-Independent Optimizations Ⅲ.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Intermediate Code Generation.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Heap Management.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第六周讲义_Run-Time Environments.pdf
- 上海交通大学:《编译原理》教学资源_教学资料_第四周讲义_Syntax-Directed Translation.pdf
- 上海交通大学:《计算机辅助设计》教学资源_Product Visualization.doc