INF5110 – Compiler Construction
Spring 2017
1 / 95
INF5110 Compiler Construction Spring 2017 1 / 95 Outline 1. - - PowerPoint PPT Presentation
INF5110 Compiler Construction Spring 2017 1 / 95 Outline 1. Run-time environments Intro Static layout Stack-based runtime environments Stack-based RTE with nested procedures Functions as parameters Virtual methods Parameter passing
1 / 95
2 / 95
3 / 95
4 / 95
5 / 95
6 / 95
7 / 95
8 / 95
9 / 95
10 / 95
11 / 95
12 / 95
13 / 95
14 / 95
aLater, we’ll encounter also static links (aka access links). 15 / 95
16 / 95
17 / 95
18 / 95
main() gcd(15,10) gcd(10,5) gcd(5,0)
main g(2) f(1) g(1) g(1) 19 / 95
20 / 95
21 / 95
22 / 95
23 / 95
24 / 95
25 / 95
26 / 95
27 / 95
aintegers are good for array-offsets, so they act as “references” 28 / 95
t y p e Int_Vector i s a r r a y (INTEGER r an ge <>)
INTEGER ; p r o c e d u r e Sum( low , high : INTEGER ; A: Int_Vector ) r e t u r n INTEGER i s i : i n t e g e r b e g i n . . . end Sum ;
29 / 95
v o i d p ( i n t x , d o u b l e y ) { c h a r a ; i n t i ; . . . ; A: { d o u b l e x ; i n t j ; . . . ; } . . . ; B: { c h a r ∗ a ; i n t k ; . . . ; } ; . . . ; }
30 / 95
31 / 95
program nonLocalRef ; p r o c e d u r e p ; v a r n : i n t e g e r ; p r o c e d u r e q ; b e g i n (∗ a r e f to n i s now non− l o c a l , non− g l o b a l ∗) end ; (∗ q ∗) p r o c e d u r e r ( n : i n t e g e r ) ; b e g i n q ; end ; (∗ r ∗) b e g i n (∗ p ∗) n := 1 ; r ( 2 ) ; end ; (∗ p ∗) b e g i n (∗ main ∗) p ; end .
32 / 95
33 / 95
34 / 95
35 / 95
36 / 95
37 / 95
38 / 95
AR of main (no access link) control link return addr. x:... access link control link return addr. access link control link return addr. no access link control link return addr. x:... access link control link return addr. access link control link fp return addr. sp ... after 2nd call to r
39 / 95
40 / 95
41 / 95
42 / 95
43 / 95
44 / 95
45 / 95
46 / 95
aResp.: at least the possibility to locate them.
47 / 95
48 / 95
49 / 95
50 / 95
51 / 95
52 / 95
53 / 95
54 / 95
55 / 95
1The stack discipline can be seen as a particularly simple (and efficient)
56 / 95
57 / 95
58 / 95
59 / 95
60 / 95
61 / 95
62 / 95
63 / 95
64 / 95
65 / 95
66 / 95
2CBV is in a way the prototypical, most dignified way of parameter passsing,
67 / 95
68 / 95
69 / 95
70 / 95
3One can ask though, if not call-by-reference is messed-up in the example
71 / 95
72 / 95
73 / 95
74 / 95
75 / 95
76 / 95
77 / 95
78 / 95
79 / 95
aStarting point slides from Ragnhild Kobro Runde, 2015.
80 / 95
81 / 95
i n t ∗ d a n g l e ( v o i d ) { i n t x ; // l o c a l v a r r e t u r n &x ; // a d d r e s s
x } t y p e d e f i n t (∗ proc ) ( v o i d ) ; proc g ( i n t x ) { i n t f ( v o i d ) { /∗ i l l e g a l ∗/ r e t u r n x ; } r e t u r n f ; } main ( ) { proc c ; c = g ( 2 ) ; p r i n t f ( "%d\n" , c ( ) ) ; /∗ 2? ∗/ r e t u r n 0 ; }
82 / 95
4Objects here are meant as heap-allocated entities, which in OO languages
83 / 95
84 / 95
85 / 95
86 / 95
87 / 95
88 / 95
89 / 95
90 / 95
91 / 95
92 / 95
93 / 95
94 / 95
[Louden, 1997] Louden, K. (1997). Compiler Construction, Principles and Practice. PWS Publishing. 95 / 95