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
omega-mv / om / compiler / osc_ownership.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note OSC semantic decisions. The checker supplies the live counts for the owner |
| 3 | note or, for a reborrow, the live child counts for its parent borrow. |
| 4 | note bridge c-define OSC_OWN_ALLOWED 0 |
| 5 | note bridge c-define OSC_OWN_MOVED 1 |
| 6 | note bridge c-define OSC_OWN_ALIAS 2 |
| 7 | note bridge c-define OSC_OWN_READ_ONLY 3 |
| 8 | note bridge c-define OSC_OWN_LOOP_MOVE 4 |
| 9 | note bridge c-type oscowner OscCheckedOwner |
| 10 | note bridge c-type oscbor OscCheckedBorrow |
| 11 | note bridge c-type snapdim OscSnapshotShape |
| 12 | note bridge c-define OSC_SNAP_OWNER_WORDS 3 |
| 13 | note bridge c-define OSC_SNAP_BORROW_WORDS 2 |
| 14 | om2 |
| 15 | |
| 16 | layout snapdim 16 |
| 17 | field owners uint at 0 little |
| 18 | field borrows uint at 4 little |
| 19 | field symbols uint at 8 little |
| 20 | field active uint at 12 little |
| 21 | end |
| 22 | |
| 23 | note The checker snapshots this record at each branch. sym is the owning |
| 24 | note symbol or -1, and arena1 is the arena symbol plus one or zero. |
| 25 | layout oscowner 20 |
| 26 | field moved uchar at 0 little |
| 27 | field n_shared ushort at 2 little |
| 28 | field n_mut ushort at 4 little |
| 29 | field moveline uint at 8 little |
| 30 | field sym int at 12 little |
| 31 | field arena1 int at 16 little |
| 32 | end |
| 33 | |
| 34 | note parent is -1 for a direct borrow. Nested borrows count against |
| 35 | note the parent borrow, direct borrows count against the owner. |
| 36 | layout oscbor 24 |
| 37 | field live uchar at 0 little |
| 38 | field mut uchar at 1 little |
| 39 | field obj int at 4 little |
| 40 | field parent int at 8 little |
| 41 | field n_shared ushort at 12 little |
| 42 | field n_mut ushort at 14 little |
| 43 | field line uint at 16 little |
| 44 | field sym int at 20 little |
| 45 | end |
| 46 | |
| 47 | const owner_live uchar 0 |
| 48 | const owner_moved uchar 1 |
| 49 | const owner_maybe uchar 3 |
| 50 | |
| 51 | const allowed ulong 0 |
| 52 | const moved_owner ulong 1 |
| 53 | const mutable_alias ulong 2 |
| 54 | const read_only ulong 3 |
| 55 | const loop_move ulong 4 |
| 56 | |
| 57 | fn 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 |
| 62 | end |
| 63 | |
| 64 | export 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 |
| 69 | end |
| 70 | |
| 71 | export 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 |
| 76 | end |
| 77 | |
| 78 | export 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 |
| 87 | end |
| 88 | |
| 89 | note Join two paths that reach the same point. Keep the diagnostic line from |
| 90 | note the already maybe-moved path when the other path definitely moved. |
| 91 | note Return one when the declaration needs a conditional release. |
| 92 | fn 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 |
| 94 | end |
| 95 | |
| 96 | export 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 |
| 107 | end |
| 108 | |
| 109 | note Handle provenance is definite only while every surviving path retains |
| 110 | note its original allocation. For a single surviving path pass before as other. |
| 111 | export 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 |
| 116 | end |
| 117 | |
| 118 | note A provenance is definitely freed only when it was freed on both paths. |
| 119 | export fn osc_join_frees taken view uchar * remaining edit uchar * to void |
| 120 | requires 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 |
| 128 | end |
| 129 | |
| 130 | export 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 |
| 137 | end |
| 138 | |
| 139 | fn 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 |
| 148 | end |
| 149 | |
| 150 | export 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 |
| 157 | end |
| 158 | |
| 159 | export 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 |
| 166 | end |
| 167 | |
| 168 | export 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 |
| 175 | end |
| 176 | |
| 177 | export 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 |
| 184 | end |
| 185 | |
| 186 | note Join all pre-branch records in one call. Only declarations requiring a |
| 187 | note conditional release are returned to the AST consumer. |
| 188 | export fn osc_join_owners taken view uchar * remaining edit uchar * changes edit int * to ulong |
| 189 | requires eq len taken mul len changes 20 |
| 190 | requires 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 |
| 209 | end |
| 210 | |
| 211 | note The first three owner words carry the move state, child counts and move |
| 212 | note line. Owner identity and arena affiliation are fixed at creation. For a |
| 213 | note borrow only liveness and child counts can change after it is bound. |
| 214 | note The remaining snapshot words hold each existing symbol's borrow index. |
| 215 | fn 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 |
| 223 | end |
| 224 | |
| 225 | fn 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 |
| 232 | end |
| 233 | |
| 234 | fn 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 |
| 238 | end |
| 239 | |
| 240 | export fn osc_snapshot_save shape view snapdim owners view uint 1280 borrows view uint 3072 symbols view uint 19200 saved edit uint * to void |
| 241 | requires le get shape owners 256 |
| 242 | requires le get shape borrows 512 |
| 243 | requires 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 |
| 249 | end |
| 250 | |
| 251 | fn 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 |
| 259 | end |
| 260 | |
| 261 | fn 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 |
| 274 | end |
| 275 | |
| 276 | fn 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 |
| 280 | end |
| 281 | |
| 282 | export fn osc_snapshot_restore shape view snapdim owners edit uint 1280 borrows edit uint 3072 symbols edit uint 19200 saved view uint * to void |
| 283 | requires le get shape owners 256 |
| 284 | requires le get shape borrows 512 |
| 285 | requires le get shape symbols 480 |
| 286 | requires ge get shape active get shape borrows |
| 287 | requires 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 |
| 293 | end |
| 294 | |
| 295 | note Join the entry owners present in both falling-through paths. The saved |
| 296 | note arm has three words per owner; the live checker has five. |
| 297 | fn 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 |
| 311 | end |
| 312 | |
| 313 | export fn osc_join_snapshot saved view uint * owners edit uint 1280 changes edit int * to ulong |
| 314 | requires 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 |
| 323 | end |
| 324 | |
| 325 | export fn osc_snapshot_moves saved view uint * owners view uint 1280 ids edit int * to ulong |
| 326 | requires 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 |
| 337 | end |
| 338 | |
| 339 | note When branches disagree about a move, the owning declaration needs a |
| 340 | note runtime drop flag. A borrow parameter's backing object has no owner. |
| 341 | export fn osc_join_branch_owners saved view uint * owners edit uint 1280 symbols view uint 19200 nodes edit uchar * to void |
| 342 | requires eq rem len saved 3 0 |
| 343 | requires le len saved 768 |
| 344 | requires 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 |
| 354 | end |