Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming languages

1,080 views

Published on

Slides for compiler construction course

Published in: Software
  • Be the first to comment

  • Be the first to like this

Programming languages

  1. 1. IN4303 2016-2017 Compiler Construction Programming Languages imperative & object-oriented Guido Wachsmuth, Eelco Visser
  2. 2. Imperative and Object-Oriented Languages 2
  3. 3. Imperative and Object-Oriented Languages 3 interpreter software language compiler software language machine language
  4. 4. Imperative and Object-Oriented Languages natural language natural language translator 4 compiler software language machine language
  5. 5. Imperative and Object-Oriented Languages 5 Traditional compilers architecture source code errors parse generate check machine code
  6. 6. Imperative and Object-Oriented Languages 6 Modern compilers in IDEs architecture source code parse generate machine code check
  7. 7. Imperative and Object-Oriented Languages 7 Compilers are inverted abstractions architecture source code machine code abstract
  8. 8. Imperative and Object-Oriented Languages Compilers Invert Abstractions Computers speak machine language • basic instructions to move around and combine small things Humans do not speak machine language very well • “mov; loadi; jump” anyone? Programming languages abstract from machine language • capture common programming patterns in language constructs • abstract from uninteresting variation in machines Compilers invert the abstractions of programming languages 8
  9. 9. Imperative and Object-Oriented Languages 9 Portability: Platform Independence architecture source code parse generate GPU code check generate x86 code
  10. 10. Imperative and Object-Oriented Languages Understanding Compilers Requires … Understanding machine languages • machine architecture, instruction sets Understanding programming language (abstraction)s • memory, control-flow, procedures, modules, … • safety mechanisms: type systems & other static analyses Understanding how to define mapping from PL to ML • semantics of such mappings • techniques to implement such mappings 10
  11. 11. Imperative and Object-Oriented Languages 11 Imperative Languages state & statements procedures types Object-Oriented Languages objects & messages types
  12. 12. Imperative and Object-Oriented Languages 12 Imperative Languages state & statements
  13. 13. Imperative and Object-Oriented Languages 13 machine code
  14. 14. basic concepts Imperative and Object-Oriented Languages State Machine state • a pile of data stored in memory • memory hierarchy: registers, RAM, disk, network, … Imperative program • computation is series of changes to memory • basic operations on memory (increment register) • controlling such operations (jump, return address, …) • control represented by state (program counter, stack, …) 14
  15. 15. registers Imperative and Object-Oriented Languages x86 family general purpose registers • accumulator AX - arithmetic operations • counter CX - shift/rotate instructions, loops • data DX - arithmetic operations, I/O • base BX - pointer to data • stack pointer SP, base pointer BP - top and base of stack • source SI, destination DI - stream operations 15
  16. 16. registers Imperative and Object-Oriented Languages x86 family general purpose registers • accumulator AX - arithmetic operations • counter CX - shift/rotate instructions, loops • data DX - arithmetic operations, I/O • base BX - pointer to data • stack pointer SP, base pointer BP - top and base of stack • source SI, destination DI - stream operations special purpose registers • segments SS, CS, DS, ES, FS, GS • flags EFLAGS 15
  17. 17. mov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16
  18. 18. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16
  19. 19. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory
  20. 20. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory calculation
  21. 21. read memorymov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX basic concepts Imperative and Object-Oriented Languages Example: x86 Assembler 16 write memory calculation jump
  22. 22. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17
  23. 23. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory
  24. 24. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory calculation
  25. 25. .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn basic concepts Imperative and Object-Oriented Languages Example: Java Bytecode 17 read memory calculation jump
  26. 26. basic concepts Imperative and Object-Oriented Languages Memory & Control Abstractions Memory abstractions • variables: abstract over data storage • expressions: combine data into new data • assignment: abstract over storage operations Control-flow abstractions • structured control-flow: abstract over unstructured jumps • ‘go to statement considered harmful’ Edgser Dijkstra, 1968 18
  27. 27. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19
  28. 28. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable
  29. 29. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression
  30. 30. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression assignment
  31. 31. states & statements Imperative and Object-Oriented Languages Example: C int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } 19 variable expression assignment control flow
  32. 32. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20
  33. 33. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable
  34. 34. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression
  35. 35. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression assignment
  36. 36. states & statements Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end 20 variable expression assignment control flow
  37. 37. Imperative and Object-Oriented Languages 21 Imperative Languages procedures
  38. 38. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret Imperative and Object-Oriented Languages Example: x86 Assembler 22 modularity
  39. 39. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 modularity
  40. 40. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 new stack frame modularity
  41. 41. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity
  42. 42. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity old stack frame
  43. 43. push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter Imperative and Object-Oriented Languages Example: x86 Assembler 22 access parameter new stack frame modularity old stack frame free parameters
  44. 44. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23
  45. 45. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter
  46. 46. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter
  47. 47. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter local variable
  48. 48. procedures Imperative and Object-Oriented Languages Example: C #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } 23 formal parameter actual parameter local variable recursive call
  49. 49. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24
  50. 50. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter
  51. 51. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter
  52. 52. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter local variable
  53. 53. procedures Imperative and Object-Oriented Languages Example: Tiger /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end 24 formal parameter actual parameter local variable recursive call
  54. 54. call by value vs. call by reference Imperative and Object-Oriented Languages Example: Tiger let type vector = array of int function init(v: vector) = v := vector[5] of 0 function upto(v: vector, l: int) = for i := 0 to l do v[i] := i var v : vector := vector[5] of 1 in init(v) ; upto(v, 5) end 25
  55. 55. Imperative and Object-Oriented Languages 26 Imperative Languages types
  56. 56. Subtitle Text Imperative and Object-Oriented Languages Types & Type Checking Type • type is category of values • operation can be applied to all values in some category Type checking • safety: ensure that operation is applied to right input values • static: check during compile time • dynamic: check during execution 27
  57. 57. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code • memory: no type information • instructions: assume values of certain types 28
  58. 58. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code • memory: no type information • instructions: assume values of certain types Dynamically typed languages • typed values • run-time checking & run-time errors 28
  59. 59. dynamic & static typing Imperative and Object-Oriented Languages Type Systems Machine code • memory: no type information • instructions: assume values of certain types Dynamically typed languages • typed values • run-time checking & run-time errors Statically typed languages • typed expressions • compile-time checking & compile-time errors 28
  60. 60. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility • value/expression: actual type • context: expected type 29
  61. 61. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility • value/expression: actual type • context: expected type Type equivalence • structural type systems • nominal type systems 29
  62. 62. compatibility Imperative and Object-Oriented Languages Type Systems Type compatibility • value/expression: actual type • context: expected type Type equivalence • structural type systems • nominal type systems Subtyping • relation between types • value/expression: multiple types 29
  63. 63. type compatibility Imperative and Object-Oriented Languages Example: Tiger let type A = int type B = int type V = array of A type W = V type X = array of A type Y = array of B var a: A := 42 var b: B := a var v: V := V[42] of b var w: W := v var x: X := w var y: Y := x in y end 30
  64. 64. record types Imperative and Object-Oriented Languages Type Systems Record • consecutively stored values • fields accessible via different offsets Record type • fields by name, type, position in record • structural subtyping: width vs. depth 31 type R1 = {f1 : int, f2 : int} type R2 = {f1 : int, f2 : int, f3 : int} type R3 = {f1 : byte, f2 : byte}
  65. 65. biology Imperative and Object-Oriented Languages Polymorphism 32
  66. 66. biology Imperative and Object-Oriented Languages Polymorphism 32
  67. 67. biology Imperative and Object-Oriented Languages Polymorphism 32
  68. 68. biology Imperative and Object-Oriented Languages Polymorphism 32 the occurrence of more than one form
  69. 69. biology Imperative and Object-Oriented Languages Polymorphism 33
  70. 70. biology Imperative and Object-Oriented Languages Polymorphism 34 can give blood to can give plasma to
  71. 71. programming languages Imperative and Object-Oriented Languages Polymorphism 35
  72. 72. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 35
  73. 73. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 35
  74. 74. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 "foo" + "bar" 35
  75. 75. programming languages Imperative and Object-Oriented Languages Polymorphism print(42) print(42.0) print("foo") 36
  76. 76. programming languages Imperative and Object-Oriented Languages Polymorphism 21 + 21 21.0 + 21.0 21 + 21.0 21 + "bar" 37
  77. 77. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading • same name, different types, same operation • same name, different types, different operations type coercion • implicit conversion Universal polymorphism subtype polymorphism • substitution principle parametric polymorphism 38 21 + 21 21.0 + 21.0 print(42) print(42.0) "foo" + "bar" 21 + "bar"
  78. 78. Imperative and Object-Oriented Languages 39 Object-Oriented Languages objects & messages
  79. 79. objects & messages Imperative and Object-Oriented Languages Modularity Objects • generalization of records • identity • state • behavior 40
  80. 80. objects & messages Imperative and Object-Oriented Languages Modularity Objects • generalization of records • identity • state • behavior Messages • objects send and receive messages • trigger behavior • imperative realization: method calls 40
  81. 81. Imperative and Object-Oriented Languages 41 Object-Oriented Languages types
  82. 82. classes Imperative and Object-Oriented Languages Modularity Classes • generalization of record types • characteristics of objects: attributes, fields, properties • behavior of objects: methods, operations, features 42 public class C { public int f1; private int f2; public void m1() { return; } private C m2(C c) { return c; } }
  83. 83. classes Imperative and Object-Oriented Languages Modularity Classes • generalization of record types • characteristics of objects: attributes, fields, properties • behavior of objects: methods, operations, features Encapsulation • interface exposure • hide attributes & methods • hide implementation 42 public class C { public int f1; private int f2; public void m1() { return; } private C m2(C c) { return c; } }
  84. 84. inheritance vs. interfaces Imperative and Object-Oriented Languages Modularity Inheritance • inherit attributes & methods • additional attributes & methods • override behavior • nominal subtyping 43 public class C { public int f1; public void m1() {…} public void m2() {…} } public class D extends C { public int f2; public void m2() {…} public void m3() {…} } public interface I { public int f; public void m(); } public class E implements I { public int f; public void m() {…} public void m’() {…} }
  85. 85. inheritance vs. interfaces Imperative and Object-Oriented Languages Modularity Inheritance • inherit attributes & methods • additional attributes & methods • override behavior • nominal subtyping Interfaces • avoid multiple inheritance • interface: contract for attributes & methods • class: provides attributes & methods • nominal subtyping 43 public class C { public int f1; public void m1() {…} public void m2() {…} } public class D extends C { public int f2; public void m2() {…} public void m3() {…} } public interface I { public int f; public void m(); } public class E implements I { public int f; public void m() {…} public void m’() {…} }
  86. 86. polymorphism Imperative and Object-Oriented Languages Type Systems 44
  87. 87. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading • same method name, independent classes • same method name, same class, different parameter types overriding • same method name, subclass, compatible types 44
  88. 88. polymorphism Imperative and Object-Oriented Languages Type Systems Ad-hoc polymorphism overloading • same method name, independent classes • same method name, same class, different parameter types overriding • same method name, subclass, compatible types Universal polymorphism subtype polymorphism • inheritance, interfaces parametric polymorphism 44
  89. 89. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch • link method call to method 45
  90. 90. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch • link method call to method Static dispatch • type information at compile-time 45
  91. 91. static vs. dynamic dispatch Imperative and Object-Oriented Languages Type Systems Dispatch • link method call to method Static dispatch • type information at compile-time Dynamic dispatch • type information at run-time • single dispatch: one parameter • multiple dispatch: more parameters 45
  92. 92. single dispatch Imperative and Object-Oriented Languages Example: Java public class A {} public class B extends A {} public class C extends B {} public class D { public A m(A a) { System.out.println("D.m(A a)"); return a; } public A m(B b) { System.out.println("D.m(B b)"); return b; } } public class E extends D { public A m(A a) { System.out.println("E.m(A a)"); return a; } public B m(B b) { System.out.println("E.m(B b)"); return b; } } 46 A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E(); A ab = b; A ac = c; D de = e; d. m(a); d. m(b); d. m(ab); d. m(c); d. m(ac); e. m(a); e. m(b); e. m(ab); e. m(c); e. m(ac); de.m(a); de.m(b); de.m(ab); de.m(c); de.m(ac);
  93. 93. overloading vs. overriding Imperative and Object-Oriented Languages Example: Java public class F { public A m(B b) { System.out.println("F.m(B b)"); return b; } } public class G extends F { public A m(A a) { System.out.println("G.m(A a)"); return a; } } public class H extends G { public B m(B b) { System.out.println("H.m(B b)"); return b; } } 47 A a = new A(); B b = new B(); F f = new F(); G g = new G(); H h = new H(); A ab = b; f.m(b); g.m(a); g.m(b); g.m(ab); h.m(a); h.m(b); h.m(ab);
  94. 94. Imperative and Object-Oriented Languages 48 Except where otherwise noted, this work is licensed under
  95. 95. copyrights Imperative and Object-Oriented Languages Pictures Slide 1: Popular C++ by Itkovian, some rights reserved Slide 4: PICOL icons by Melih Bilgil, some rights reserved Slides 7, 9, 13: Dual Processor Module by roobarb!, some rights reserved Slides 11, 14: The C Programming Language by Bill Bradford, some rights reserved Slides 12, 15, 16, 19: Tiger by Bernard Landgraf, some rights reserved Slide 21: Adam and Eva by Albrecht Dürer, public domain Slide 22: ABO blood type by InvictaHOG, public domain Slide 23: Blood Compability and Plasma donation compatibility path by InvictaHOG, public domain Slide 28: Delice de France by Dominica Williamson, some rights reserved Slide 46: Nieuwe Kerk by Arne Kuilman, some rights reserved 49

×