1.32k likes | 1.6k Views
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. Contents. Sequence control Data control. Sequence Control. Expressions Statements Subprograms. Expressions. Control mechanism Syntax
E N D
Programming LanguagesImplementation of Control Structures Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM
Contents • Sequence control • Data control
Sequence Control • Expressions • Statements • Subprograms
Expressions • Control mechanism • Syntax • Execution-time representation • Evaluation
Control Mechanism • Functional composition: (A + B)*(C - A) * (+ (A, B), - (C, A))
Syntax • Infix: A * B + C • binary operations only • computation order ambiguity
Syntax • Prefix: • ordinary * (+ (A, B), - (C, A)) • Cambridge Polish (* (+ A B)(- C A)) • Polish * + A B - C A
Syntax • Prefix: • different numbers of operands • ordinary/ Cambridge Polish: cumbersome with parentheses • Polish: number of operands known in advance
Syntax • Postfix: • ordinary ((A, B) +,(C, A) -) * • Polish A B + C A - * • suitable execution-time representation
Execution-Time Representation • Interpretation: • tree structures * + - A B C A • prefix or postfix
Execution-Time Representation • Compilation: machine code sequences PUSHA PUSHB ADD PUSHC PUSHA SUB MUL A A A + B B A + B A + B A + B (A+B)*(C-A) C C C - A A
Execution-Time Representation • Compilation: machine code sequences PUSHA PUSHB ADD PUSHC PUSHA SUB MUL A A A + B B A B + C A - * A + B A + B A + B (A+B)*(C-A) C C C - A A
Evaluation • No simple uniform evaluation rule is satisfactory: * + - A B C A
Evaluation • Side effects: A*FOO(X) + A + * A A FOO(X)
Evaluation • Side effects: A*B*C = 1020 * 10-20 * 10-20 (A*B)*C = 1* 10-20 = 10-20 A*(B*C) = 1020 * 0 = 0
Evaluation • Short-circuit Boolean expressions: if (A = 0)or(B/A > C)then …
Evaluation • Short-circuit Boolean expressions: if (A = 0)or else(B/A > C)then …
Sequence Control • Expressions • Statements • Subprograms
Statements • GOTO • Sequencing • Selection • Iteration
GOTO GOTOL JMP L L
Sequencing begin S1; S2; … Sn; end S1 codes S2 codes Sn codes
Selection • if: if A = 0 then S1 else S2; S3; JEQ0 A L1 S2 codes JMP L2 L1 S1 codes L2 S3 codes
Selection E v • case: var E: 0..2; case E of 1: S1; 2: S2; else:S0 end; S3; JMPa+v a JMP L0 a+1 JUMP table JMP L1 a+2 JMP L2 L1 S1 codes JMP L3 L2 S2 codes JMP L3 L0 S0 codes L3 S3 codes
Iteration • for: for I := E1 to E2 doS; I := E1; L0: if I > E2 then GOTOL1; S; I := I + 1; GOTO L0; L1: …
Iteration • while: while C do S; L0: if not(C) then GOTOL1; S; GOTO L0; L1: …
Iteration • repeat: repeat S until C; L0: S; if not(C) then GOTOL0;
Sequence Control • Expressions • Statements • Subprograms
Subprograms • Simple call-return • Recursive calls
Simple Call-Return • No recursive calls • Explicit calls • Complete execution • Immediate control transfer • Single execution sequence
Simple Call-Return MAIN A B I0 I2 I4 call A call B I1 I3 return return end
Simple Call-Return MAIN I0 call A I1 end local data MAIN CIP(current instruction pointer) I0
Simple Call-Return MAIN A I0 I2 call A call B I1 I3 return end local data A local data MAIN I1 I2 CIP
Simple Call-Return MAIN A B I0 I2 I4 call A call B I1 I3 return return end local data B local data A local data MAIN I3 I1 I4 CIP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 -- -- local data MAIN CEP(current environment pointer) I0 CIP R0
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 -- I1 -- R0 local data MAIN local data A I2 CIP R1 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 -- I1 I3 -- R0 R1 local data MAIN local data A local data B I4 CIP R2 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 R3 -- I1 I3 I5 -- R0 R1 R2 local data MAIN local data A local data B local data A I2 CIP R3 CEP
Recursive Calls MAIN A B I0 I2 I4 call A call B call A I1 I3 I5 end return return R0 R1 R2 R3 -- I1 I3 I5 -- R0 R1 R2 local data MAIN local data A local data B local data A Dynamic chain I2 CIP R3 CEP
Central Stack MAIN I0 R0 CIP I0 -- MAIN -- call A CEP R0 local data I1 end
Central Stack MAIN I0 R0 CIP I2 -- -- MAIN call A CEP R1 local data I1 R1 I1 end R0 A A local data I2 call B I3 return
Central Stack A I2 R0 CIP I4 -- -- MAIN call B CEP R2 local data I3 R1 I1 return R0 A B local data R2 I4 I3 R1 B call A local data I5 return
Central Stack B R0 -- I4 CIP I2 -- MAIN call A local data CEP R3 I5 R1 I1 return R0 A local data A R2 I3 I2 R1 B call B local data R2 I3 I5 return R2 A local data
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.
Exercises • Given the following program: programMAIN; functionFAC(N:integer): integer; beginifN <= 1 thenFAC := 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.
Contents • Sequence control • Data control
Data Control • Basic concepts • Local data and environments • Shared data: dynamic scope • Shared data: block structure • Shared data: parameter transmission
Basic Concepts • Names • Referencing environments • Scope • Block structure
Names • Variable names • Formal parameter names • Subprogram names • Names for defined types • Names for defined constants
Referencing Environments • Association: Name-------->Data Object • Referencing environment = set of associations
Referencing Environments programMAIN; varX:integer; … X := 0; object Association