-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrefcounting.txt
199 lines (149 loc) · 4.65 KB
/
refcounting.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Function application
---------------------
code(f(e))
= v <- code(e) # Code to compute argument value
r <- f(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r # Return result
Function Definition
-------------------
code(def f(x) : bodyexpr)
= code(bodyexpr)
Function argument
-----------------
code(arg_i) # Where arg_i is the i-th function argument
= incr(arg_i)
arg_i
Basic non-collection literal
----------------------------
code(literal)
= res <- eval_literal
setrefcount(res, 1)
res
Tuple literal
--------------------
code([e]t)
= v <- code(e)
t = tuple_space
t[0] = v
t
Let block
---------------------
code(let x = e in body)
= v_x <- code(e)
code(body)
decr(v_x) # v_x gets decremented as it goes out of block scope.
Let variable
---------------------
code(v_i) # Where v_i is the i-th variable declared in a let block
= incr(val_for_v_i) # Where val_for_v_i is the value computed for let variable v_i.
val_for_v_i
Structure field access
------------------------
code(getField_x(s_expr))
= s_v <- code(s_expr) # Get structure value
r <- s_v.x # Get field from structure
incr(r) # Increment field value ref count
decr(s_v) # Decrement struct value rec count
r
Structure constructor (direct application/implementation)
---------------------
code(S_constructor(x_expr))
= x_v <- code(x_expr) # Compute field value
s <- alloc structure
setrefcount(s, 1)
s.x <- x_v # Set field
s
Structure constructor (as function)
----------------------
code(def S(arg_i))
= x_v <- arg_i # get arg
incr(arg_i) # incr arg
s <- alloc structure
setrefcount(s, 1)
s.x <- x_v # Set field
s
Application of structure constructor function
------------------------------------
code(S(x_expr))
= v <- code(x_expr)
x_v <- v # Structure constructor: get arg
incr(x_v) # Structure constructor: incr arg
s <- alloc structure
setrefcount(s, 1)
s.x <- x_v # Set field
r <- s
decr(v)
r
= v <- code(x_expr)
s <- alloc structure
setrefcount(s, 1)
s.x <- v # Set field
s
Which is the same as direct application/implementation
Example:
------------------------------------------
take the identity function I(x) = x
code (def I(x) : x)
= code(x)
= incr(x) # function argument code
x # function argument code
So, a function application of I(x) gives:
code(I(x))
= v <- code(x) # Code to compute argument value
r <- I(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r # Return result
= v <- code(x) # Code to compute argument value
incr(v) # From def I(x)
r <- v # From def I(x)
decr(v) # Consume (decrement) argument value
r
= v <- code(x)
r <- v
r
= code(x)
Example, where g is def g(x) : 1 (Assume integers are ref counted, assume initial rc(x) = 1
----------------------------------------------------------------------------------------------
code(g(x))
= v <- code(x) # Code to compute argument value
r <- g(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r # Return result
= v <- code(x)
r <- 1 # def g body expr
setrefcount(r, 1) # def g body expr
decr(v)
r
Example, function application where argument is an argument from the enclosing function
----------------------------------------------------------------------------------------
code(f(arg_i))
= v <- code(arg_i) # From function application
r <- f(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r
= incr(arg_i) # from function argument code
v <- arg_i # From function application
r <- f(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r
= r <- f(v)
r
Conclusion: when emitting code for a function expression/application, and an argument is an argument from the enclosing function,
can omit reference increment and decrement.
Example, function application where argument is a let variable from enclosing let block
----------------------------------------------------------------------------------------
code(f(v_i))
= v <- code(v_i_val) # From function application
r <- f(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r
= incr(v_i_val) # from let var code
v <- v_i_val # From function application
r <- f(v) # Call function with argument value
decr(v) # Consume (decrement) argument value
r
= r <- f(v_i_val)
r
rc(literal) = 1
rc(f(x)) = rc(literal) or rc(x)