INF5110 – Compiler Construction
Run-time environments Spring 2016
1 / 92
INF5110 Compiler Construction Run-time environments Spring 2016 1 - - PowerPoint PPT Presentation
INF5110 Compiler Construction Run-time environments Spring 2016 1 / 92 Outline 1. Run-time environments Intro Static layout Stack-based runtime environments Stack-based RTE with nested procedures Functions as parameters Virtual
1 / 92
2 / 92
3 / 92
4 / 92
5 / 92
6 / 92
7 / 92
8 / 92
9 / 92
10 / 92
11 / 92
12 / 92
aLater, we’ll encounter also static links (aka access links). 13 / 92
14 / 92
15 / 92
16 / 92
main() gcd(15,10) gcd(10,5) gcd(5,0)
main g(2) f(1) g(1) g(1) 17 / 92
18 / 92
19 / 92
20 / 92
21 / 92
22 / 92
23 / 92
24 / 92
25 / 92
aintegers are good for array-offsets, so they act as “references” 26 / 92
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 ;
27 / 92
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 ; . . . ; } ; . . . ; }
28 / 92
29 / 92
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 .
30 / 92
31 / 92
32 / 92
33 / 92
34 / 92
35 / 92
36 / 92
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
37 / 92
38 / 92
39 / 92
40 / 92
41 / 92
42 / 92
43 / 92
44 / 92
aResp.: at least the possibility to locate that.
45 / 92
46 / 92
47 / 92
48 / 92
49 / 92
50 / 92
51 / 92
52 / 92
53 / 92
1The stack discipline can be seen as a particularly simple (and efficient)
54 / 92
55 / 92
56 / 92
57 / 92
58 / 92
59 / 92
60 / 92
61 / 92
62 / 92
63 / 92
64 / 92
2CBV is in a way the prototypical, most dignified way of parameter passsing,
65 / 92
66 / 92
67 / 92
68 / 92
3One can ask though, if not call-by-reference is messed-up in the example
69 / 92
70 / 92
71 / 92
72 / 92
73 / 92
74 / 92
75 / 92
76 / 92
77 / 92
aStarting point slides from Ragnhild Kobro Runde, 2015.
78 / 92
79 / 92
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 ; }
80 / 92
4Objects here are meant as heap-allocated entities, which in OO languages
81 / 92
82 / 92
83 / 92
84 / 92
85 / 92
86 / 92
87 / 92
88 / 92
89 / 92
90 / 92
91 / 92
[Louden, 1997] Louden, K. (1997). Compiler Construction, Principles and Practice. PWS Publishing. 92 / 92