concepts of programming languages · forth imperative language uses two stacks: data and return...

33
Table of Content Introduction Concepts Fields of Use Implementations Optimizat 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

Upload: others

Post on 16-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Concepts of Programming LanguagesStack based Paradimgs

Timo Luerweg

Universitat zu Lubeck

30. November 2015

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 1

Page 2: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Outline

1 Introduction

2 Concepts

3 Fields of Use

4 Implementations

5 Optimization

6 Conclusion

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 2

Page 3: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Introduction

What are Stack-based Languages?

Rely on one (or more)Stacks to executeprograms

Operations are executedon the top elements of thestack

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 3

Page 4: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Syntax

Stack based Languages use Reverse Polish Notation (Postfix)

Notation reflects operation

Doesn’t need parentheses

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 4

Page 5: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Syntax

Stack based Languages use Reverse Polish Notation (Postfix)

Notation reflects operation

Doesn’t need parentheses

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 4

Page 6: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 5

Page 7: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 5

Page 8: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 5

Page 9: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 6

Page 10: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 6

Page 11: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 6

Page 12: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 6

Page 13: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Meta Programming

Reasons for a possible use in this field are:

Conceptually simple

Extendible

Code generation and modification at runtime possible

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 7

Page 14: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 8

Page 15: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Forth

Example (Hello World)

: HELLO .”Hello World ” ;

Example (42 · 52)

4 dup ∗ 5 dup ∗ ∗ .

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 9

Page 16: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Joy

Functional language

Based on the composition of functions (not lambda calculus)

Programs can pass whole structures as parameters

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 10

Page 17: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 11

Page 18: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Variable Elimination

Speed up execution

Reduce Load and Store instructions

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 12

Page 19: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 13

Page 20: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

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 Lubeck) Concepts of Programming Languages 30. November 2015 13

Page 21: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Local Variable Elimination

Algorithms exist(see example)

Is performed on a single block of code

only removes redundant variables

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 14

Page 22: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Example

a = b * c;

b = a / 8;

c = a - b;

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 15

Page 23: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

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 87 (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 !

Page 24: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Example

1 ( −−) 76 LOCAL@2 ( 76 −−) 77 LOCAL@3 (76 77 −−) ∗4 ( 75 −−) DUP \ copy o f 755 (75 75 −−) 75 LOCAL!6 ( 75 −−) NOP \ 75 LOCAL@7 ( 75 −−) 88 (75 88 −−) /9 ( 76 −−) 76 LOCAL!

10 ( −−) 75 LOCAL@11 ( 75 −−) 76 LOCAL@12 (75 76 −−) −13 ( 77 −−) 77 LOCAL!

Page 25: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Example

1 ( −−) 76 LOCAL@2 ( 76 −−) 77 LOCAL@3 ( 76 77 −−) ∗4 ( 75 −−) DUP5 ( 75 75 −−) 75 LOCAL ! (DEAD)6 ( 75 −−) NOP7 ( 75 −−) 88 ( 75 88 −−) UNDER9 (75 75 88 −−) /

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

Page 26: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Example

1 ( −−) 76 LOCAL@2 ( 76 −−) 77 LOCAL@3 ( 76 77 −−) ∗4 ( 75 −−) DUP5 ( 75 75 −−) 75 LOCAL ! (DEAD)\ ∗6 ( 75 −−) NOP7 ( 75 −−) 88 ( 75 88 −−) UNDER9 (75 75 88 −−) /

10 ( 75 76 −−) TUCK \ copy o f 7611 (76 75 76 −−) 76 LOCAL!12 ( 76 75 −−) NOP13 ( 76 75 −−) SWAP \ 76 LOCAL@14 ( 75 76 −−) −15 ( 77 −−) 77 LOCAL!

Page 27: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Example

1 ( −−) 76 LOCAL@2 ( 76 −−) 77 LOCAL@3 ( 76 77 −−) ∗4 ( 75 −−) 85 ( 75 88 −−) UNDER6 (75 75 88 −−) /7 ( 75 76 −−) DUP8 (76 75 76 −−) 76 LOCAL!9 ( 75 76 −−) −

10 ( 77 −−) 77 LOCAL!

Page 28: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Global Variable Elimination

No algorithm at this point

Can be applied manually

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 21

Page 29: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Results of Variable Elimination

Results differ with different Stack depths

Some programs are hard to optimize

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 22

Page 30: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Results of Variable Elimination

Figure: 1 Intra-block stackscheduling removes mostredundant accesses to localvariables.

Figure: 2 The number of localvariable instructions in thecompiled code reducesdramatically with stackscheduling.

Page 31: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Thank you for your attention

Questions?

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 24

Page 32: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Conclusion

Stack based Languages are:

Easy to extend

Fast at execution

Conceptually simple

Compactly represented as Byte-code

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 25

Page 33: Concepts of Programming Languages · Forth Imperative Language Uses two stacks: Data and Return Stack Popular in Embedded Systems and Micro-controllers Di erent versions depending

Table of Content Introduction Concepts Fields of Use Implementations Optimization Conclusion

Conclusion

However they are not frequently used because:

Specialized on a certain field

Implicit passing

Unusual syntax

Timo Luerweg (Uni Lubeck) Concepts of Programming Languages 30. November 2015 26