interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / compiler / osc_ownership.om

354 lines · 11123 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note OSC semantic decisions. The checker supplies the live counts for the owner
3note or, for a reborrow, the live child counts for its parent borrow.
4note bridge c-define OSC_OWN_ALLOWED 0
5note bridge c-define OSC_OWN_MOVED 1
6note bridge c-define OSC_OWN_ALIAS 2
7note bridge c-define OSC_OWN_READ_ONLY 3
8note bridge c-define OSC_OWN_LOOP_MOVE 4
9note bridge c-type oscowner OscCheckedOwner
10note bridge c-type oscbor OscCheckedBorrow
11note bridge c-type snapdim OscSnapshotShape
12note bridge c-define OSC_SNAP_OWNER_WORDS 3
13note bridge c-define OSC_SNAP_BORROW_WORDS 2
14om2
15
16layout snapdim 16
17field owners uint at 0 little
18field borrows uint at 4 little
19field symbols uint at 8 little
20field active uint at 12 little
21end
22
23note The checker snapshots this record at each branch. sym is the owning
24note symbol or -1, and arena1 is the arena symbol plus one or zero.
25layout oscowner 20
26field moved uchar at 0 little
27field n_shared ushort at 2 little
28field n_mut ushort at 4 little
29field moveline uint at 8 little
30field sym int at 12 little
31field arena1 int at 16 little
32end
33
34note parent is -1 for a direct borrow. Nested borrows count against
35note the parent borrow, direct borrows count against the owner.
36layout oscbor 24
37field live uchar at 0 little
38field mut uchar at 1 little
39field obj int at 4 little
40field parent int at 8 little
41field n_shared ushort at 12 little
42field n_mut ushort at 14 little
43field line uint at 16 little
44field sym int at 20 little
45end
46
47const owner_live uchar 0
48const owner_moved uchar 1
49const owner_maybe uchar 3
50
51const allowed ulong 0
52const moved_owner ulong 1
53const mutable_alias ulong 2
54const read_only ulong 3
55const loop_move ulong 4
56
57fn check_alias exclusive ulong shared_count ulong mut_count ulong to ulong
58 if or ne mut_count 0 and ne exclusive 0 ne shared_count 0
59 ret mutable_alias
60 end
61 ret allowed
62end
63
64export fn osc_owner_access moved ulong exclusive ulong shared_count ulong mut_count ulong to ulong
65 if ne moved 0
66 ret moved_owner
67 end
68 ret check_alias exclusive shared_count mut_count
69end
70
71export fn osc_borrow_access writable ulong exclusive ulong shared_count ulong mut_count ulong to ulong
72 if and ne exclusive 0 eq writable 0
73 ret read_only
74 end
75 ret check_alias exclusive shared_count mut_count
76end
77
78export fn osc_owner_move moved ulong shared_count ulong mut_count ulong declaration_depth long loop_depth long to ulong
79 decision = osc_owner_access moved 1 shared_count mut_count
80 if ne decision allowed
81 ret decision
82 end
83 if lt declaration_depth loop_depth
84 ret loop_move
85 end
86 ret allowed
87end
88
89note Join two paths that reach the same point. Keep the diagnostic line from
90note the already maybe-moved path when the other path definitely moved.
91note Return one when the declaration needs a conditional release.
92fn keep_taken_line first uchar second uchar to bool
93 ret or eq second owner_live and eq second owner_moved eq first owner_maybe
94end
95
96export fn osc_join_owner taken view oscowner remaining edit oscowner to ulong
97 first = get taken moved
98 second = get remaining moved
99 if eq first second
100 ret 0
101 end
102 if keep_taken_line first second
103 put remaining moveline get taken moveline
104 end
105 put remaining moved owner_maybe
106 ret 1
107end
108
109note Handle provenance is definite only while every surviving path retains
110note its original allocation. For a single surviving path pass before as other.
111export fn osc_join_handle before long current long other long to long
112 if or ne current before ne other before
113 ret -1
114 end
115 ret before
116end
117
118note A provenance is definitely freed only when it was freed on both paths.
119export fn osc_join_frees taken view uchar * remaining edit uchar * to void
120requires eq len taken len remaining
121 for i ulong len remaining
122 if and ne get taken i 0 ne get remaining i 0
123 put remaining i 1
124 else
125 put remaining i 0
126 end
127 end
128end
129
130export fn osc_init_owner item edit oscowner symbol long to void
131 put item moved owner_live
132 put item n_shared 0
133 put item n_mut 0
134 put item moveline 0
135 put item sym int symbol
136 put item arena1 0
137end
138
139fn init_borrow child edit oscbor object long parent_id long mutable ulong source_line ulong to void
140 put child live 1
141 put child mut uchar mutable
142 put child obj int object
143 put child parent int parent_id
144 put child n_shared 0
145 put child n_mut 0
146 put child line uint source_line
147 put child sym -1
148end
149
150export fn osc_open_borrow owner edit oscowner child edit oscbor object long mutable ulong source_line ulong to void
151 init_borrow child object -1 mutable source_line
152 if ne mutable 0
153 put owner n_mut add get owner n_mut 1
154 else
155 put owner n_shared add get owner n_shared 1
156 end
157end
158
159export fn osc_open_reborrow parent edit oscbor child edit oscbor object long parent_id long mutable ulong source_line ulong to void
160 init_borrow child object parent_id mutable source_line
161 if ne mutable 0
162 put parent n_mut add get parent n_mut 1
163 else
164 put parent n_shared add get parent n_shared 1
165 end
166end
167
168export fn osc_close_borrow owner edit oscowner child edit oscbor to void
169 put child live 0
170 if ne get child mut 0
171 put owner n_mut sub get owner n_mut 1
172 else
173 put owner n_shared sub get owner n_shared 1
174 end
175end
176
177export fn osc_close_reborrow parent edit oscbor child edit oscbor to void
178 put child live 0
179 if ne get child mut 0
180 put parent n_mut sub get parent n_mut 1
181 else
182 put parent n_shared sub get parent n_shared 1
183 end
184end
185
186note Join all pre-branch records in one call. Only declarations requiring a
187note conditional release are returned to the AST consumer.
188export fn osc_join_owners taken view uchar * remaining edit uchar * changes edit int * to ulong
189requires eq len taken mul len changes 20
190requires eq len remaining len taken
191 count = ulong 0
192 for i ulong len changes
193 offset = mul i 20
194 first = get taken offset
195 second = get remaining offset
196 if ne first second
197 if keep_taken_line first second
198 for j ulong 4
199 atline = add add offset 8 j
200 put remaining atline get taken atline
201 end
202 end
203 put remaining offset owner_maybe
204 put changes count int i
205 count = add count 1
206 end
207 end
208 ret count
209end
210
211note The first three owner words carry the move state, child counts and move
212note line. Owner identity and arena affiliation are fixed at creation. For a
213note borrow only liveness and child counts can change after it is bound.
214note The remaining snapshot words hold each existing symbol's borrow index.
215fn save_owner_words owners view uint 1280 saved edit uint * count ulong to void
216 for i ulong count
217 src = mul i 5
218 dst = mul i 3
219 put saved dst get owners src
220 put saved add dst 1 get owners add src 1
221 put saved add dst 2 get owners add src 2
222 end
223end
224
225fn save_borrow_words borrows view uint 3072 saved edit uint * base ulong count ulong to void
226 for i ulong count
227 src = mul i 6
228 dst = add base mul i 2
229 put saved dst get borrows src
230 put saved add dst 1 get borrows add src 3
231 end
232end
233
234fn save_bindings symbols view uint 19200 saved edit uint * base ulong count ulong to void
235 for j ulong count
236 put saved add base j get symbols add mul j 40 24
237 end
238end
239
240export fn osc_snapshot_save shape view snapdim owners view uint 1280 borrows view uint 3072 symbols view uint 19200 saved edit uint * to void
241requires le get shape owners 256
242requires le get shape borrows 512
243requires le get shape symbols 480
244 save_owner_words owners saved ulong get shape owners
245 base = mul ulong get shape owners 3
246 save_borrow_words borrows saved base ulong get shape borrows
247 base = add base mul ulong get shape borrows 2
248 save_bindings symbols saved base ulong get shape symbols
249end
250
251fn restore_owner_words saved view uint * owners edit uint 1280 count ulong to void
252 for i ulong count
253 src = mul i 3
254 dst = mul i 5
255 put owners dst get saved src
256 put owners add dst 1 get saved add src 1
257 put owners add dst 2 get saved add src 2
258 end
259end
260
261fn restore_borrow_words shape view snapdim saved view uint * borrows edit uint 3072 base ulong to void
262 count = ulong get shape borrows
263 for i ulong count
264 src = add base mul i 2
265 dst = mul i 6
266 put borrows dst get saved src
267 put borrows add dst 3 get saved add src 1
268 end
269 note Preserve fresh IDs and all metadata. Only their live byte is cleared.
270 for j ulong sub ulong get shape active count
271 pos = mul add count j 6
272 put borrows pos and get borrows pos 4294967040
273 end
274end
275
276fn restore_bindings symbols edit uint 19200 saved view uint * base ulong count ulong to void
277 for j ulong count
278 put symbols add mul j 40 24 get saved add base j
279 end
280end
281
282export fn osc_snapshot_restore shape view snapdim owners edit uint 1280 borrows edit uint 3072 symbols edit uint 19200 saved view uint * to void
283requires le get shape owners 256
284requires le get shape borrows 512
285requires le get shape symbols 480
286requires ge get shape active get shape borrows
287requires le get shape active 512
288 restore_owner_words saved owners ulong get shape owners
289 base = mul ulong get shape owners 3
290 restore_borrow_words shape saved borrows base
291 base = add base mul ulong get shape borrows 2
292 restore_bindings symbols saved base ulong get shape symbols
293end
294
295note Join the entry owners present in both falling-through paths. The saved
296note arm has three words per owner; the live checker has five.
297fn join_saved_owner saved view uint * owners edit uint 1280 i ulong to bool
298 pos = mul i 5
299 src = mul i 3
300 record = get owners pos
301 current = uchar and record 255
302 taken = uchar and get saved src 255
303 if eq taken current
304 ret false
305 end
306 if keep_taken_line taken current
307 put owners add pos 2 get saved add src 2
308 end
309 put owners pos or and record 4294967040 uint owner_maybe
310 ret true
311end
312
313export fn osc_join_snapshot saved view uint * owners edit uint 1280 changes edit int * to ulong
314requires eq len saved mul len changes 3
315 count = ulong 0
316 for i ulong len changes
317 if join_saved_owner saved owners i
318 put changes count int i
319 count = add count 1
320 end
321 end
322 ret count
323end
324
325export fn osc_snapshot_moves saved view uint * owners view uint 1280 ids edit int * to ulong
326requires eq len saved mul len ids 3
327 count = ulong 0
328 for i ulong len ids
329 before = and get saved mul i 3 255
330 now = and get owners mul i 5 255
331 if and ne before 1 eq now 1
332 put ids count int i
333 count = add count 1
334 end
335 end
336 ret count
337end
338
339note When branches disagree about a move, the owning declaration needs a
340note runtime drop flag. A borrow parameter's backing object has no owner.
341export fn osc_join_branch_owners saved view uint * owners edit uint 1280 symbols view uint 19200 nodes edit uchar * to void
342requires eq rem len saved 3 0
343requires le len saved 768
344requires eq rem len nodes 104 0
345 for id ulong div len saved 3
346 if join_saved_owner saved owners id
347 symbol = get owners add mul id 5 3
348 if ne symbol 4294967295
349 decl = get symbols add mul ulong symbol 40 38
350 put nodes add mul ulong decl 104 74 1
351 end
352 end
353 end
354end