1 / 125

Programming Languages Implementation of Control Structures

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

viho
Download Presentation

Programming Languages Implementation of Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  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 * + - A B C A • prefix or postfix

  11. 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

  12. 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

  13. Evaluation • No simple uniform evaluation rule is satisfactory: * + - A B C A

  14. Evaluation • Side effects: A*FOO(X) + A + * A A FOO(X)

  15. 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

  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 GOTOL JMP L L

  21. Sequencing begin S1; S2; … Sn; end S1 codes S2 codes Sn codes

  22. Selection • if: if A = 0 then S1 else S2; S3; JEQ0 A L1 S2 codes JMP L2 L1 S1 codes L2 S3 codes

  23. 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

  24. Iteration • for: for I := E1 to E2 doS; I := E1; L0: if I > E2 then GOTOL1; S; I := I + 1; GOTO L0; L1: …

  25. Iteration • while: while C do S; L0: if not(C) then GOTOL1; S; GOTO L0; L1: …

  26. Iteration • repeat: repeat S until C; L0: S; if not(C) then GOTOL0;

  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 MAIN A B I0 I2 I4 call A call B I1 I3 return return end

  31. Simple Call-Return MAIN I0 call A I1 end local data MAIN CIP(current instruction pointer) I0

  32. Simple Call-Return MAIN A I0 I2 call A call B I1 I3 return end local data A local data MAIN I1 I2 CIP

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. Central Stack MAIN I0 R0 CIP I0 -- MAIN -- call A CEP R0 local data I1 end

  40. 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

  41. 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

  42. 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

  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: 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.

  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 programMAIN; varX:integer; … X := 0; object Association

More Related