Concepts of Programming Languages Stack based Paradimgs Timo - - PowerPoint PPT Presentation

concepts of programming languages
SMART_READER_LITE
LIVE PREVIEW

Concepts of Programming Languages Stack based Paradimgs Timo - - PowerPoint PPT Presentation

Table of Content Introduction Concepts Fields of Use Implementations Optimization Concepts of Programming Languages Stack based Paradimgs Timo Luerweg Universit at zu L ubeck 30. November 2015 Timo Luerweg (Uni L ubeck) Concepts


slide-1
SLIDE 1

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Concepts of Programming Languages

Stack based Paradimgs Timo Luerweg

Universit¨ at zu L¨ ubeck

  • 30. November 2015

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

1

slide-2
SLIDE 2

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Outline

1

Introduction

2

Concepts

3

Fields of Use

4

Implementations

5

Optimization

6

Conclusion

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

2

slide-3
SLIDE 3

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Introduction

What are Stack-based Languages? Rely on one (or more) Stacks to execute programs Operations are executed

  • n the top elements of the

stack

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

3

slide-4
SLIDE 4

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Syntax

Stack based Languages use Reverse Polish Notation (Postfix)

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

4

slide-5
SLIDE 5

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Syntax

Stack based Languages use Reverse Polish Notation (Postfix) Notation reflects operation Doesn’t need parentheses

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

4

slide-6
SLIDE 6

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Infix and Postfix

Example (Infix to Postfix)

4 + 5 · 8 · (3 + 6) + 2

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

5

slide-7
SLIDE 7

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Infix and Postfix

Example (Infix to Postfix)

4 + 5 · 8 · (3 + 6) + 2 ([4 ([5 8·][3 6 +] · ) + ]2 + )

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

5

slide-8
SLIDE 8

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Infix and Postfix

Example (Infix to Postfix)

4 + 5 · 8 · (3 + 6) + 2 ([4 ([5 8·][3 6 +] · ) + ]2 + ) 4 5 8 · 3 6 + · + 2+

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

5

slide-9
SLIDE 9

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Intermediate Languages

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

6

slide-10
SLIDE 10

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Intermediate Languages

Reasons for a frequent use are: Easy to generate code from Compact representation as Byte-Code Reduced set of instructions

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

6

slide-11
SLIDE 11

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Intermediate Languages

Reasons for a frequent use are: Easy to generate code from Compact representation as Byte-Code Reduced set of instructions Conversion with Shunting-yard algorithm

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

6

slide-12
SLIDE 12

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Intermediate Languages

Reasons for a frequent use are: Easy to generate code from Compact representation as Byte-Code Reduced set of instructions Conversion with Shunting-yard algorithm Examples: Java Byte code ( .class) Elisp byte-code PostScript

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

6

slide-13
SLIDE 13

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Meta Programming

Reasons for a possible use in this field are: Conceptually simple Extendible Code generation and modification at runtime possible

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

7

slide-14
SLIDE 14

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Forth

Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Different versions depending on “stack size” Untyped (5 + ”a” is possible)

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

8

slide-15
SLIDE 15

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Forth

Example (Hello World)

: HELLO .”Hello World ” ;

Example (42 · 52)

4 dup ∗ 5 dup ∗ ∗ .

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

9

slide-16
SLIDE 16

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Joy

Functional language Based on the composition of functions (not lambda calculus) Programs can pass whole structures as parameters

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

10

slide-17
SLIDE 17

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Joy

Example (Hello World)

”Hello world” (or) ”Hello” ” world” concat

Example (42 · 52)

4 dup ∗ 5 dup ∗ ∗ (or) [4 5] [dup ∗]map 5 get get ∗

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

11

slide-18
SLIDE 18

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Variable Elimination

Speed up execution Reduce Load and Store instructions

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

12

slide-19
SLIDE 19

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Variable Elimination

Requirements: Split able in blocks (of same size) Different branches leave same variables on the stack The stack is in the same state as before (Optional)

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

13

slide-20
SLIDE 20

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Variable Elimination

Requirements: Split able in blocks (of same size) Different branches leave same variables on the stack The stack is in the same state as before (Optional) Difference between Local and Global eliminaiton

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

13

slide-21
SLIDE 21

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Local Variable Elimination

Algorithms exist(see example) Is performed on a single block of code

  • nly removes redundant variables

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

14

slide-22
SLIDE 22

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Example

a = b * c; b = a / 8; c = a - b;

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

15

slide-23
SLIDE 23

Example

1 ( −−) 76 LOCAL@ \ b @ 2 ( 76 −−) 77 LOCAL@ \ c @ 3 (76 77 −−) ∗ 4 ( 75 −−) 75 LOCAL! \ a ! 5 ( −−) 75 LOCAL@ \ a @ 6 ( 75 −−) 8 \ l i t e r a l 8 7 (75 88 −−) / 8 ( 76 −−) 76 LOCAL! \ b ! 9 ( −−) 75 LOCAL@ \ a @ (DEAD) 10 ( 75 −−) 76 LOCAL@ \ b @ 11 (75 76 −−) − 12 ( 77 −−) 77 LOCAL! \ c !

slide-24
SLIDE 24

Example

1 ( −−) 76 LOCAL@ 2 ( 76 −−) 77 LOCAL@ 3 (76 77 −−) ∗ 4 ( 75 −−) DUP \ copy

  • f 75

5 (75 75 −−) 75 LOCAL! 6 ( 75 −−) NOP \ 75 LOCAL@ 7 ( 75 −−) 8 8 (75 88 −−) / 9 ( 76 −−) 76 LOCAL! 10 ( −−) 75 LOCAL@ 11 ( 75 −−) 76 LOCAL@ 12 (75 76 −−) − 13 ( 77 −−) 77 LOCAL!

slide-25
SLIDE 25

Example

1 ( −−) 76 LOCAL@ 2 ( 76 −−) 77 LOCAL@ 3 ( 76 77 −−) ∗ 4 ( 75 −−) DUP 5 ( 75 75 −−) 75 LOCAL! (DEAD) 6 ( 75 −−) NOP 7 ( 75 −−) 8 8 ( 75 88 −−) UNDER 9 (75 75 88 −−) / 10 ( 75 76 −−) 76 LOCAL! 11 ( 75 −−) NOP \ 75 LOCAL@ 12 ( 75 −−) 76 LOCAL@ 13 ( 75 76 −−) − 14 ( 77 −−) 77 LOCAL!

slide-26
SLIDE 26

Example

1 ( −−) 76 LOCAL@ 2 ( 76 −−) 77 LOCAL@ 3 ( 76 77 −−) ∗ 4 ( 75 −−) DUP 5 ( 75 75 −−) 75 LOCAL! (DEAD)\ ∗ 6 ( 75 −−) NOP 7 ( 75 −−) 8 8 ( 75 88 −−) UNDER 9 (75 75 88 −−) / 10 ( 75 76 −−) TUCK \ copy

  • f 76

11 (76 75 76 −−) 76 LOCAL! 12 ( 76 75 −−) NOP 13 ( 76 75 −−) SWAP \ 76 LOCAL@ 14 ( 75 76 −−) − 15 ( 77 −−) 77 LOCAL!

slide-27
SLIDE 27

Example

1 ( −−) 76 LOCAL@ 2 ( 76 −−) 77 LOCAL@ 3 ( 76 77 −−) ∗ 4 ( 75 −−) 8 5 ( 75 88 −−) UNDER 6 (75 75 88 −−) / 7 ( 75 76 −−) DUP 8 (76 75 76 −−) 76 LOCAL! 9 ( 75 76 −−) − 10 ( 77 −−) 77 LOCAL!

slide-28
SLIDE 28

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Global Variable Elimination

No algorithm at this point Can be applied manually

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

21

slide-29
SLIDE 29

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Results of Variable Elimination

Results differ with different Stack depths Some programs are hard to optimize

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

22

slide-30
SLIDE 30

Results of Variable Elimination

Figure: 1 Intra-block stack scheduling removes most redundant accesses to local variables. Figure: 2 The number of local variable instructions in the compiled code reduces dramatically with stack scheduling.

slide-31
SLIDE 31

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Thank you for your attention

Questions?

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

24

slide-32
SLIDE 32

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Conclusion

Stack based Languages are: Easy to extend Fast at execution Conceptually simple Compactly represented as Byte-code

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

25

slide-33
SLIDE 33

Table of Content Introduction Concepts Fields of Use Implementations Optimization

Conclusion

However they are not frequently used because: Specialized on a certain field Implicit passing Unusual syntax

Timo Luerweg (Uni L¨ ubeck) Concepts of Programming Languages

  • 30. November 2015

26