1 Programming Languages Implementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
1 Compiler Construction Intermediate Code Generation.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
1 Programming Languages Storage Management Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Run time vs. Compile time
Chapter 8 . Sequence Control
Semantics of Calls and Returns
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Chapter 9: Subprogram Control
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
ISBN Chapter 10 Implementing Subprograms –Nested Subprograms –Blocks –Implementing Dynamic Scoping.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Imperative Programming
1 Programming Languages Implementation of Data Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
CS 2104 Prog. Lang. Concepts Subprograms
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
ISBN Chapter 10 Implementing Subprograms.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
CSC 8505 Compiler Construction Runtime Environments.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
ISBN Chapter 10 Implementing Subprograms.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Implementing Subprograms
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Final Review In Text: Chapters 1-3, 5-11,
Final Review In Text: Chapters 1-3, 5-10,
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Implementing Subprograms
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Languages and Compilers (SProg og Oversættere)
Course Overview PART I: overview material PART II: inside a compiler
Midterm Review In Text: Chapters 1-3, 5-11, 15.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

1 Programming Languages Implementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM

2 Contents Sequence control Data control

3 Sequence Control Expressions Statements Subprograms

4 Expressions Control mechanism Syntax Execution-time representation Evaluation

5 Control Mechanism Functional composition: (A + B)  (C - A)  (+ (A, B), - (C, A))

6 Syntax Infix: A  B + C  binary operations only  computation order ambiguity

7 Syntax Prefix:  ordinary  (+ (A, B), - (C, A))  Cambridge Polish (  (+ A B) (- C A))  Polish  + A B - C A

8 Syntax Prefix:  different numbers of operands  ordinary/ Cambridge Polish: cumbersome with parentheses  Polish: number of operands known in advance

9 Syntax Postfix:  ordinary  ((A, B) +, (C, A) -)   Polish A B + C A -   suitable execution-time representation

10 Execution-Time Representation Interpretation:  tree structures  + - ABCA  prefix or postfix

11 Execution-Time Representation Compilation: machine code sequences PUSH A PUSH B ADD PUSH C PUSH A SUB MUL AA B A + B C C A C - A (A+B)  (C-A)

12 Execution-Time Representation Compilation: machine code sequences PUSH A PUSH B ADD PUSH C PUSH A SUB MUL AA B A + B C C A C - A (A+B)  (C-A) A B + C A - 

13 Evaluation No simple uniform evaluation rule is satisfactory:  + - ABCA

14 Evaluation Side effects: A  FOO(X) + A +  AFOO(X) A

15 Evaluation Side effects: A  B  C =   (A  B)  C = 1  = A  (B  C) =  0 = 0

16 Evaluation Short-circuit Boolean expressions: if (A = 0) or (B/A > C) then …

17 Evaluation Short-circuit Boolean expressions: if (A = 0) or else (B/A > C) then …

18 Sequence Control Expressions Statements Subprograms

19 Statements GOTO Sequencing Selection Iteration

20 GOTO GOTO L JMP L L

21 Sequencing begin S 1 ; S 2 ; … S n ; end S 1 codes S 2 codes S n codes

22 Selection if A = 0 then S 1 else S 2 ; S 3 ; JEQ0 A L 1 S 2 codes JMP L 2 S 1 codes L1L1 S 3 codes L2L2 if:

23 Selection var E: 0..2; case E of 1: S 1 ; 2: S 2 ; else: S 0 end; S 3 ; JMP  +v E  v JMP L 0 JMP L 1 JMP L 2  S 1 codes L1L1 case:  +1 JMP L 3  +2 S 2 codes JMP L 3 S 0 codes S 3 codes L2L2 L0L0 JUMP table L3L3

24 Iteration for I := E 1 to E 2 do S; I := E 1 ; L 0 : if I > E 2 then GOTO L 1 ; S; I := I + 1; GOTO L 0 ; L 1 : … for:

25 Iteration while C do S; L 0 : if not(C) then GOTO L 1 ; S; GOTO L 0 ; L 1 : … while:

26 Iteration repeat S until C; L 0 : S; if not(C) then GOTO L 0 ; repeat:

27 Sequence Control Expressions Statements Subprograms

28 Subprograms Simple call-return Recursive calls

29 Simple Call-Return No recursive calls Explicit calls Complete execution Immediate control transfer Single execution sequence

30 Simple Call-Return call A I0I0 I1I1 call B I2I2 I3I3 end return I4I4 MAINAB

31 Simple Call-Return call A I0I0 I1I1 end MAIN local data MAIN I0I0 CIP (current instruction pointer)

32 Simple Call-Return call A I0I0 I1I1 call B I2I2 I3I3 end return MAINA local data MAIN local data A I1I1 I2I2 CIP

33 Simple Call-Return call A I0I0 I1I1 call B I2I2 I3I3 end return I4I4 MAINAB local data MAIN local data A local data B I1I1 I3I3 I4I4 CIP

34 Recursive Calls call A I0I0 I1I1 call B I2I2 I3I3 endreturn I4I4 MAINAB call A I5I5 local data MAIN -- R0R0 I0I0 CIP R0R0 CEP (current environment pointer)

35 Recursive Calls call A I0I0 I1I1 call B I2I2 I3I3 endreturn I4I4 MAINAB call A I5I5 local data MAIN -- local data A R0R0 I1I1 R0R0 R1R1 I2I2 CIP R1R1 CEP

36 Recursive Calls call A I0I0 I1I1 call B I2I2 I3I3 endreturn I4I4 MAINAB call A I5I5 local data MAIN -- local data A R0R0 I1I1 local data B R1R1 I3I3 R0R0 R1R1 R2R2 I4I4 CIP R2R2 CEP

37 Recursive Calls call A I0I0 I1I1 call B I2I2 I3I3 endreturn I4I4 MAINAB call A I5I5 local data MAIN -- local data A R0R0 I1I1 local data B R1R1 I3I3 local data A R2R2 I5I5 R0R0 R1R1 R2R2 R3R3 I2I2 CIP R3R3 CEP

38 Recursive Calls call A I0I0 I1I1 call B I2I2 I3I3 endreturn I4I4 MAINAB call A I5I5 local data MAIN -- local data A R0R0 I1I1 local data B R1R1 I3I3 local data A R2R2 I5I5 R0R0 R1R1 R2R2 R3R3 I2I2 CIP R3R3 CEP Dynamic chain

39 Central Stack local data -- R0R0 I0I0 CIP R0R0 CEP call A I0I0 I1I1 end MAIN

40 Central Stack local data -- R0R0 I2I2 CIP R1R1 CEP call B I2I2 I3I3 return A MAIN local data R0R0 I1I1 R1R1 A call A I0I0 I1I1 end MAIN

41 Central Stack local data -- R0R0 I4I4 CIP R2R2 CEP call A I4I4 I5I5 return B MAIN local data R0R0 I1I1 R1R1 A R1R1 I3I3 R2R2 B call B I2I2 I3I3 return A

42 Central Stack local data -- R0R0 I2I2 CIP R3R3 CEP call A I4I4 I5I5 return B MAIN local data R0R0 I1I1 R1R1 A R1R1 I3I3 R2R2 B call B I2I2 I3I3 return A local data R2R2 I5I5 R2R2 A

43 Exercises Illustrate the storage representation of A: array [0..1, 1..2, 1..3] of integer using the column-major order. Give the accessing formula for computing the location of A[I, J, K], supposing that the size of an integer is 2 bytes.

44 Exercises Given the following program: program MAIN; function FAC(N: integer): integer; begin if N <= 1 then FAC := 1 else FAC := N  FAC(N - 1) end; begin write(FAC(3)) end. Illustrate the code segment and activation records of MAIN and FAC in the central stack during execution of the program.

45 Contents Sequence control Data control

46 Data Control Basic concepts Local data and environments Shared data: dynamic scope Shared data: block structure Shared data: parameter transmission

47 Basic Concepts Names Referencing environments Scope Block structure

48 Names Variable names Formal parameter names Subprogram names Names for defined types Names for defined constants

49 Referencing Environments Association: Name > Data Object Referencing environment = set of associations

50 Referencing Environments program MAIN; var X: integer; … X := 0; object Association

51 Referencing Environments Local Non-Local Global Predefined

52 Local Environments program MAIN; var X: integer; … procedure SUB1; var X: real; … X := 1; object 2 object 1

53 Non-Local Environments program MAIN; var X: integer; … procedure SUB1; var X: real; … procedure SUB2; … X := 2; object 2 object 1

54 Global Environments program MAIN; var X: integer; … procedure SUB1; var X: real; … procedure SUB3; … X := 3; object 2 object 1

55 Predefined Environments program MAIN; var X: integer; … X := MAXINT - 1; … write(X) codes for “write”

56 Referencing Environments Visibility of an association Referencing operations Local, non-local, global references

57 Visibility program MAIN; var X: integer; … procedure SUB1; var X: real; … X := 1; object 2 object 1 Hidden Visible

58 Referencing Operations Name  Environment  Data Object

59 Referencing Operations program MAIN; var X: integer; … procedure SUB1; var X: real; … X := 1; object 2 object 1 X  (X  object 2 )  object 2

60 Referencing Environments Visibility of an association Referencing operations Local, non-local, global references

61 Local References program MAIN; var X: integer; … procedure SUB1; var X: real; … X := 1; object 2 object 1 X  (X  object 2 )  object 2

62 Non-Local References program MAIN; var X: integer; … procedure SUB1; var X: real; … procedure SUB3; … X := 2; object 2 object 1 X  (X  object 2 )  object 2

63 Global References program MAIN; var X: integer; … procedure SUB1; var X: real; … procedure SUB2; … X := 2; object 2 object 1 X  (X  object 1 )  object 1

64 Basic Concepts Names Referencing environments Scope Block structure

65 Scope The program part (text or execution) within which the binding is effective.

66 Dynamic Scope The subprogram activations within which the association is effective.

67 Dynamic Scope program MAIN; var X: integer; … procedure SUB1; var X: real; … X := 1; procedure SUB2; … X := 2; object 1 MAIN SUB1 SUB2

68 Static Scope The program text within which the declaration is effective.

69 program MAIN; var X: integer; X  integer … procedure SUB1; var X: real; … X := 1; procedure SUB2; … X := 2; Static Scope

70 program MAIN; var X: integer; X  integer … procedure SUB1; var X: real; … X := 1; procedure SUB2; … X := 2; Static Scope Static scopes define dynamic scopes

71 Static Referencing Environments Local, non-local, global environments Local, non-local, global references

72 Basic Concepts Names Referencing environments Scope Block structure

73 Block Structure program MAIN; … procedure SUB1; … procedure SUB3; … procedure SUB4; … procedure SUB2; … MAIN SUB1 SUB3 SUB2 SUB4

74 Static Scope Rules 1.The declarations at the head of each block defines the local referencing environment for that block MAIN SUB1 SUB3 SUB2 SUB4 X: real

75 Static Scope Rules 2.If no local declarations exists, then refer to the nearest enclosing block having the declaration in need MAIN SUB1 SUB3 SUB2 SUB4 X: real X: integer X := 1

76 Static Scope Rules 3.Any local declaration of a block is hidden from its outer blocks MAIN SUB1 SUB3 SUB2 SUB4 X: real

77 Static Scope Rules 4.The block name is part of the local referencing environment of the containing block MAIN SUB1 SUB3 SUB2 SUB4

78 Data Control Basic concepts Local data and environments Shared data: dynamic scope Shared data: block structure Shared data: parameter transmission

79 Local Data and Environments procedure SUB(X: integer); var Y: real; Z: array [1..3] of real; procedure SUB1; … begin … end; begin … end; object 2 SUB object 2 X Y Z SUB1 code segment

80 Data Control Basic concepts Local data and environments Shared data: dynamic scope Shared data: block structure Shared data: parameter transmission

81 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN SUB1 SUB2 SUB3

82 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y SUB1 Return point

83 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y B A U X SUB1 SUB2 Return point Dynamic chain

84 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y B A U Z Y A W V X SUB1 SUB2 SUB3 Return point Dynamic chain

85 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y B A U Z Y A W V X SUB1 SUB2 SUB3 Return point Dynamic chain

86 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y SUB1 Return point A B U V W X Y Z Central table

87 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y B A U X SUB1 SUB2 Return point A B U V W X Y Z Central table

88 Shared Data: Dynamic Scope program MAIN; procedure SUB1; var X, Y: real; … procedure SUB2; var B, A, U, X: integer; … procedure SUB3; var Z, Y, A, W, V: char; … MAIN X Y B A U Z Y A W V X SUB1 SUB2 SUB3 Return point Central table A B U V W X Y Z

89 Shared Data: Dynamic Scope MAIN X Y SUB1 Return point Central table Hidden stack A B U V W X Y Z

90 Shared Data: Dynamic Scope MAIN X Y B A U X SUB1 SUB2 Return point Central table Hidden stack X A B U V W X Y Z

91 Shared Data: Dynamic Scope MAIN X Y B A U X SUB1 SUB2 Return point Central table Hidden stack X A B U V W X Y Z Z Y A W V SUB3 Return point Y 1 A

92 Data Control Basic concepts Local data and environments Shared data: dynamic scope Shared data: block structure Shared data: parameter transmission

93 Shared Data: Block Structure program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … MAIN X, Y, Z: char SUB2 X, Y: integer SUB3 X: real SUB1 Y, Z: integer SUB4

94 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack  MAIN

95 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack  SUB2 X 1 Y

96 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack  SUB3 X 1 Y 1X

97 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack  SUB4 X 1 Y 1X

98 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack SUB4  X 1 Y 1X

99 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack SUB3  X 1 Y 1X

100 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack SUB2  X 1X

101 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack  SUB1 X 1X Y Z

102 Block Structure: Compile Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Symbol table X Y 1 Z Scope stack SUB1  1X

103 Block Structure: Run Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … MAIN SUB1 SUB2 SUB3

104 Block Structure: Run Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Y Z MAIN X --

105 Block Structure: Run Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Y Z SUB1 SCP Static chains Y Z MAIN X -- RP

106 Block Structure: Run Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Y Z X Y SUB1 SUB2 SCP Static chains Y Z MAIN X -- RP SCP RP

107 Block Structure: Run Time program MAIN; var X, Y, Z: char; … procedure SUB2; var X, Y: integer; … procedure SUB3; var X: real; … procedure SUB4; … procedure SUB1; var Y, Z: integer; … Y Z X Y X SUB1 SUB2 SUB3 SCP Static chains Y Z MAIN X -- RP SCP RP SCP RP

108 Block Structure: Run Time A B MAIN A B C A B C A B C A B C A B

109 Block Structure: Run Time Y Z X Y X SUB1 SUB2 SUB3 SCP Active static chain Y Z MAIN X -- RP SCP RP SCP RP Display loc Y = Display[1] + offset

110 Data Control Basic concepts Local data and environments Shared data: dynamic scope Shared data: block structure Shared data: parameter transmission

111 Parameter Transmission Parameters:  formal parameters  actual parameters Transmission:  by reference  by value

112 Parameter Transmission procedure SUB2(K: integer; var L: integer); begin K := K + 10; L := L + 10; write(K, L) end; procedure SUB1; begin I := 1; J := 2; SUB2(I, J); write(I, J) end; I J K L SUB1 SUB2 SCP RP SCP RP 1 2 1

113 Parameter Transmission procedure SUB2(K: integer; var L: integer); begin K := K + 10; L := L + 10; write(K, L) end; procedure SUB1; begin I := 1; J := 2; SUB2(I, J); write(I, J) end; I J K L SUB1 SUB2 SCP RP SCP RP

114 Parameter Transmission procedure SUB2(K: integer; var L: integer); begin K := K + 10; L := L + 10; write(K, L) end; procedure SUB1; begin I := 1; J := 2; SUB2(I, J); write(I, J) end; I J K L SUB1 SUB2 SCP RP SCP RP

115 Parameter Transmission procedure SUB2(K:integer; var L:integer); begin K := K + 10; L := L + 10; SUB3(K, L) write(K, L) end; procedure SUB1; begin I := 1; J := 2; SUB2(I, J); write(I, J) end; procedure SUB3(var M, N: integer); begin M := M + 10; N := N + 10; write(M, N) end; Actual parameters are formal parameters of the calling program

116 Parameter Transmission type VECT = array [1...3] of integer; procedurre SUB2 (C:VECT; var D:VECT); var I : integer; begin C [2] : = C [2] + 10; D [2] = D [2] + 10; for I : = 1 to 3 do write (C [I]); for I : = 1 to 3 do write (D [I]) end; procedurre SUB1; var A, B : VECT; J : integer; begin A [1] : = 7; A [2] = 8; A [3] : = 9; B [1] : = 7; B [2] = 8; B [3] : = 9; SUB2 (A, B); for J : = 1 to 3 do write (A [J]); for J : = 1 to 3 do write (B [J]); end; Actual parameters are structured data objects

117 Parameter Transmission type VECT = array [1...3] of integer procedurre SUB2 (I: integer; var J: integer); begin I : = I + 10; J : = J + 10; write (I, J); end; procedurre SUB1; var A : VECT; K : integer; begin A [1] := 7; A [2] := 8; A [3] := 9; SUB2 (A[1], A[2]); for K := 1 to 3 do write (A[K]) end; Actual parameters are components of structured data objects

118 Parameter Transmission type VECT = array [1...3] of integer procedurre SUB2 (var I, J: integer); begin I : = I + 1; J : = J + 1; write (I, J); end; procedurre SUB1; var A : VECT; K : integer; begin A [1] := 7; A [2] := 8; A [3] := 9; K := 2; SUB2 (K, A[K]); for K := 1 to 3 do write (A[K]) end; Actual parameters are array components with computed subscripts

119 Parameter Transmission type VECT = array [1...3] of integer; VECTPTR = ^ VECT; procedurre SUB2 (R:VECTPTR; var S:VECTPTR); begin R^[1] := R^[1] + 10; S^[1] := S^[1] + 10; if... then R := S else S := R end; procedurre SUB1; var A, B: VECT; P, Q: VECTPTR; begin A [1] = 7; A [2] := 8; A [3] := 9; B [1] := 7; B [2] := 8; B [3] := 9; P Q SUB2 (P, Q); end; Actual parameters are pointers

120 Parameter Transmission program MAIN; var X: real; procedurre SUB2 (X, Y: real; function F(U:real): real); var Z: real; begin Z := abs (Y - X); Z := (F(X) + F(Y)) * Z/2; write (Z) end; begin X := 3; SUB1 end. procedurre SUB1; var Y: real; function FUNC(V:real): real; begin FUNC := X*V + Y end; begin Y := 1; SUB2 (0, 1, FUNC). end; Actual parameters are subprograms

121 Parameter Transmission type VECT = array [1...3] of integer procedurre SUB2 (name I, J: integer); begin I : = I + 1; J : = J + 1; write (I, J); end; procedurre SUB1; var A : VECT; K : integer; begin A [1] := 7; A [2] := 8; A [3] := 9; K := 2; SUB2 (K, A[K]); for K := 1 to 3 do write (A[K]) end; Transmissiom by names

122 Formal Parameters and Aliases A data object may have more than one name, called aliases. Side effects: I := 1; J := 1; I := J + 10; J := J*10;

123 Formal Parameters and Aliases An actual parameter is a non-local variable and is trasmitted by reference. Actual parameters are of the same data object and transmitted by reference.

124 Exercises Given the following program: program MAIN; var F: real; procedure PROC(N: integer; var F: real); var F1, F2: real; begin if (N = 0) or (N = 1) then F := 1 else begin PROC(N-1, F1); PROC(N-2, F2); F := F1 + F2 end begin PROC(2, F); write(F) end.

125 Exercises Illustrate the code segment and activation records of MAIN and PROC. Illustrate the central stack during execution of this program.