| 1 | module scheme |
| 2 | |
| 3 | import reader |
| 4 | |
| 5 | @[heap] |
| 6 | struct GoopsMigrationState { |
| 7 | object Value |
| 8 | target &GoopsClass |
| 9 | snapshot &GoopsClass |
| 10 | version u64 |
| 11 | copy_plan bool |
| 12 | copy_source_version u64 |
| 13 | mut: |
| 14 | copy Value |
| 15 | copy_class &GoopsClass = unsafe { nil } |
| 16 | copy_version u64 |
| 17 | copy_initialized bool |
| 18 | copy_committed bool |
| 19 | copy_completed_by_callback bool |
| 20 | copy_completion_version u64 |
| 21 | } |
| 22 | |
| 23 | // One registration per dynamic entry, rather than mutable state in a captured |
| 24 | // continuation. The object only borrows this registration while it is active. |
| 25 | @[heap] |
| 26 | struct GoopsMigrationScope { |
| 27 | state &GoopsMigrationState |
| 28 | previous &GoopsMigrationScope = unsafe { nil } |
| 29 | previous_migrating bool |
| 30 | previous_copy_flag bool |
| 31 | } |
| 32 | |
| 33 | const goops_migration_bridge_code = [ |
| 34 | Instruction{ op: .goops_migration_start, origin: reader.no_node } |
| 35 | Instruction{ op: .goops_migration_retry, origin: reader.no_node } |
| 36 | ]! |
| 37 | |
| 38 | const goops_migration_bridge = Function{ |
| 39 | code: unsafe { nil } |
| 40 | params: 3 |
| 41 | slot_count: 3 |
| 42 | instructions: unsafe { static_bridge_instructions(&goops_migration_bridge_code[0], goops_migration_bridge_code.len) } |
| 43 | } |
| 44 | |
| 45 | @[inline] |
| 46 | fn goops_needs_migration(value Value) bool { |
| 47 | if value.goops_kind() == .goops_instance { |
| 48 | object := value.goops_instance_pointer() |
| 49 | return !object.migrating && object.layout_version != object.class.version |
| 50 | } |
| 51 | layout := value.goops_layout() |
| 52 | return !isnil(layout.class) && !layout.migrating && layout.version != layout.class.version |
| 53 | } |
| 54 | |
| 55 | fn (mut m Machine) prepare_goops_migration(vm &VM, value Value, base int) ! { |
| 56 | object := value.goops_layout() |
| 57 | if isnil(object.class) { return failure('expected a GOOPS object') } |
| 58 | vm.goops_class_for(value)! |
| 59 | vm.goops_require_mutation(object.allocation_owner)! |
| 60 | mut snapshot := unsafe { &GoopsClass(nil) } |
| 61 | for entry in object.class.history { |
| 62 | if entry.version == object.version { |
| 63 | snapshot = entry.snapshot |
| 64 | break |
| 65 | } |
| 66 | } |
| 67 | if isnil(snapshot) { return failure('instance layout history is unavailable') } |
| 68 | state := &GoopsMigrationState{ object: value, target: object.class, snapshot: snapshot, version: object.class.version } |
| 69 | procedure := m.stack[base] |
| 70 | args := list(m.arguments(base + 1)) |
| 71 | m.truncate(base) |
| 72 | m.push(procedure_value(&Procedure{ owner: vm.owner, mode: .closure, name: 'instance migration', function: &goops_migration_bridge })) |
| 73 | m.push(procedure) |
| 74 | m.push(args) |
| 75 | m.push(payload_value(.goops_migration, state)) |
| 76 | } |
| 77 | |
| 78 | fn (mut m Machine) start_goops_migration(mut vm VM) ! { |
| 79 | state_value := m.env.slots[2] |
| 80 | state := unsafe { &GoopsMigrationState(state_value.pointer()) } |
| 81 | m.push(vm.goops.migration_procedure) |
| 82 | m.push(state.object) |
| 83 | m.push(goops_class_value(state.target)) |
| 84 | for mode in [ProcedureKind.goops_migration_enter, .goops_migration_leave] { |
| 85 | m.push(procedure_value(&Procedure{ owner: vm.owner, mode: mode, env: &Environment{ slots: [state_value], escaped: true } })) |
| 86 | } |
| 87 | m.apply(mut vm, 4, false)! |
| 88 | } |
| 89 | |
| 90 | fn (mut m Machine) retry_goops_migration(mut vm VM) ! { |
| 91 | m.pop_value() |
| 92 | state := unsafe { &GoopsMigrationState(m.env.slots[2].pointer()) } |
| 93 | object := state.object.goops_layout() |
| 94 | if object.version == state.snapshot.version && state.target.version == state.version { |
| 95 | return failure('change-class did not update the obsolete instance') |
| 96 | } |
| 97 | procedure := m.env.slots[0] |
| 98 | args := m.env.slots[1] |
| 99 | count := args.list_length()! |
| 100 | m.push(procedure) |
| 101 | mut cursor := args |
| 102 | for _ in 0 .. count { |
| 103 | pair := cursor.pair_pointer() |
| 104 | m.push(pair.car) |
| 105 | cursor = pair.cdr |
| 106 | } |
| 107 | m.apply(mut vm, count, true)! |
| 108 | } |
| 109 | |
| 110 | fn (mut m Machine) enter_goops_migration(vm &VM, state &GoopsMigrationState) ! { |
| 111 | if state.copy_plan { return m.enter_goops_copy(vm, state) } |
| 112 | object := state.object.goops_layout() |
| 113 | vm.goops_class_for(state.object)! |
| 114 | vm.goops_require_mutation(object.allocation_owner)! |
| 115 | if state.target.version != state.version { |
| 116 | return failure('class changed while an instance migration was suspended') |
| 117 | } |
| 118 | if object.migrating { return failure('instance migration is already active') } |
| 119 | if voidptr(object.class) != voidptr(state.target) && voidptr(object.class) != voidptr(state.snapshot) { |
| 120 | return failure('instance changed class while its migration was suspended') |
| 121 | } |
| 122 | if object.version != state.version && object.version != state.snapshot.version { |
| 123 | return failure('instance changed while its migration was suspended') |
| 124 | } |
| 125 | scope := &GoopsMigrationScope{ state: state, previous: m.goops_migrations } |
| 126 | m.goops_migrations = scope |
| 127 | state.object.goops_pin_layout(if object.version != state.version { |
| 128 | state.snapshot |
| 129 | } else { |
| 130 | state.target |
| 131 | }, true) |
| 132 | } |
| 133 | |
| 134 | fn (mut m Machine) leave_goops_migration(state &GoopsMigrationState) { |
| 135 | if isnil(m.goops_migrations) || voidptr(m.goops_migrations.state) != voidptr(state) { return } |
| 136 | m.release_goops_migration() |
| 137 | } |
| 138 | |
| 139 | fn (mut m Machine) release_goops_migration() { |
| 140 | scope := m.goops_migrations |
| 141 | object := scope.state.object.goops_layout() |
| 142 | if scope.state.copy_plan { |
| 143 | class := if !scope.previous_migrating && !isnil(object.class.live_class) { |
| 144 | object.class.live_class |
| 145 | } else { |
| 146 | object.class |
| 147 | } |
| 148 | scope.state.object.goops_pin_layout(class, scope.previous_migrating) |
| 149 | // A native old view remains historical for the update hook. Once |
| 150 | // its dynamic extent ends, normal access can adopt the current class. |
| 151 | if scope.state.copy_committed && (scope.state.copy.goops_kind() != .goops_instance |
| 152 | || scope.state.object.goops_kind() == .goops_method) { |
| 153 | copy_layout := scope.state.copy.goops_layout() |
| 154 | if !isnil(copy_layout.class) && !isnil(copy_layout.class.live_class) { |
| 155 | scope.state.copy.goops_pin_layout(copy_layout.class.live_class, copy_layout.migrating) |
| 156 | } |
| 157 | } |
| 158 | scope.state.copy.goops_set_copy_flag(scope.previous_copy_flag) |
| 159 | m.goops_migrations = scope.previous |
| 160 | return |
| 161 | } |
| 162 | if object.migrating { |
| 163 | class := if voidptr(object.class) == voidptr(scope.state.snapshot) |
| 164 | || voidptr(object.class.live_class) == voidptr(scope.state.target) { |
| 165 | scope.state.target |
| 166 | } else { |
| 167 | object.class |
| 168 | } |
| 169 | scope.state.object.goops_pin_layout(class, false) |
| 170 | } |
| 171 | m.goops_migrations = scope.previous |
| 172 | } |
| 173 | |
| 174 | fn (m &Machine) goops_obsolete_argument(base int, argc int) Value { |
| 175 | for index in 0 .. argc { |
| 176 | value := m.stack[base + 1 + index] |
| 177 | if goops_needs_migration(value) { return value } |
| 178 | } |
| 179 | return Value{} |
| 180 | } |
| 181 | |
| 182 | fn (m &Machine) goops_native_obsolete_argument(mut vm VM, p &Procedure, base int, argc int) !Value { |
| 183 | if argc == 0 { return Value{} } |
| 184 | if ((p.native == goops_class_of_native || p.native == goops_instance_predicate) && argc == 1) |
| 185 | || ((p.native == goops_is_a_native || p.native == goops_slot_exists_native) && argc == 2) { |
| 186 | value := m.stack[base + 1] |
| 187 | return if goops_needs_migration(value) { value } else { Value{} } |
| 188 | } |
| 189 | if p.native == goops_compute_methods_native || p.native == goops_find_method_native || p.native == goops_sort_methods_native { |
| 190 | g := m.stack[base + 1] |
| 191 | if goops_needs_migration(g) { return g } |
| 192 | generic := g.goops_layout_generic() |
| 193 | if !isnil(generic) { |
| 194 | view := vm.goops_all_methods(generic)! |
| 195 | obsolete := goops_obsolete_method(view.values) |
| 196 | view.release() |
| 197 | if obsolete.tag != .unspecified { return obsolete } |
| 198 | } |
| 199 | if p.native == goops_sort_methods_native && argc == 3 { |
| 200 | mut methods := m.stack[base + 2] |
| 201 | _ = methods.list_length()! |
| 202 | for methods.tag == .pair { |
| 203 | pair := methods.pair_pointer() |
| 204 | if goops_needs_migration(pair.car) { return pair.car } |
| 205 | methods = pair.cdr |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | index := if (p.native == goops_compute_methods_native || p.native == goops_find_method_native) && argc == 2 { |
| 210 | 2 |
| 211 | } else if p.native == goops_sort_methods_native && argc == 3 { |
| 212 | 3 |
| 213 | } else { |
| 214 | return Value{} |
| 215 | } |
| 216 | mut cursor := m.stack[base + index] |
| 217 | _ = cursor.list_length()! |
| 218 | for cursor.tag == .pair { |
| 219 | pair := cursor.pair_pointer() |
| 220 | if goops_needs_migration(pair.car) { return pair.car } |
| 221 | cursor = pair.cdr |
| 222 | } |
| 223 | return Value{} |
| 224 | } |
| 225 | |
| 226 | fn (mut m Machine) cleanup_goops_migrations() { |
| 227 | for !isnil(m.goops_migrations) { m.release_goops_migration() } |
| 228 | } |
| 229 | |
| 230 | // Heap identities can die independently. An interior pointer into the other |
| 231 | // identity would retain its header and any obsolete inline values it contains. |
| 232 | fn (mut object GoopsInstance) detach_inline_fields() { |
| 233 | if object.field_count == 0 || voidptr(object.fields) != unsafe { voidptr(&u8(&object) + goops_field_offset()) } { return } |
| 234 | bytes := object.field_count * int(sizeof(Value)) |
| 235 | inline_fields := object.fields |
| 236 | object.fields = unsafe { &Value(vcalloc(bytes)) } |
| 237 | unsafe { |
| 238 | vmemcpy(object.fields, inline_fields, bytes) |
| 239 | vmemset(inline_fields, 0, bytes) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | fn goops_exchange_ordinary_layout(mut object GoopsInstance, mut proposed GoopsInstance) { |
| 244 | if voidptr(&object) == voidptr(&proposed) { return } |
| 245 | if isnil(object.physical_owner()) { |
| 246 | object.detach_inline_fields() |
| 247 | proposed.detach_inline_fields() |
| 248 | } |
| 249 | previous_class := object.class |
| 250 | previous_fields := object.fields |
| 251 | previous_count := object.field_count |
| 252 | previous_version := object.layout_version |
| 253 | object.class = proposed.class |
| 254 | object.fields = proposed.fields |
| 255 | object.field_count = proposed.field_count |
| 256 | object.layout_version = proposed.layout_version |
| 257 | proposed.class = previous_class |
| 258 | proposed.fields = previous_fields |
| 259 | proposed.field_count = previous_count |
| 260 | proposed.layout_version = previous_version |
| 261 | } |
| 262 | |
| 263 | // Exchange logical layouts only. Each physical allocation retains its owner |
| 264 | // and dynamic migration flag, and the allocator's copy becomes the old view. |
| 265 | fn goops_commit_instance_native(mut vm VM, args []Value) !Value { |
| 266 | check_arity('commit-instance', args.len, 3, 3)! |
| 267 | mut state := vm.checked_goops_copy_plan(args[2])! |
| 268 | if !state.copy_initialized || !eqv(args[0], state.object) || !eqv(args[1], state.copy) { |
| 269 | return failure('instance copy does not match its plan') |
| 270 | } |
| 271 | vm.goops_validate_copy_plan(state)! |
| 272 | if state.copy_committed || state.copy_completed_by_callback { return boolean(false) } |
| 273 | object_layout := state.object.goops_layout() |
| 274 | proposed_layout := state.copy.goops_layout() |
| 275 | if migration := vm.goops_current_migration(args[0]) { |
| 276 | if migration.target.version != migration.version { |
| 277 | return failure('class changed during instance migration') |
| 278 | } |
| 279 | if voidptr(proposed_layout.class) != voidptr(migration.target) |
| 280 | || (voidptr(object_layout.class) != voidptr(migration.target) |
| 281 | && voidptr(object_layout.class) != voidptr(migration.snapshot)) { |
| 282 | return failure('instance changed class during migration') |
| 283 | } |
| 284 | } |
| 285 | if state.object.goops_kind() != .goops_instance || state.copy.goops_kind() != .goops_instance { |
| 286 | previous := vm.goops_commit_native_layout(state.object, state.copy, state.snapshot)! |
| 287 | state.copy_committed = true |
| 288 | state.copy.goops_set_copy_flag(false) |
| 289 | return previous |
| 290 | } |
| 291 | mut object := vm.checked_goops_instance(state.object)! |
| 292 | mut proposed := vm.checked_goops_instance(state.copy)! |
| 293 | goops_exchange_ordinary_layout(mut object, mut proposed) |
| 294 | state.copy_committed = true |
| 295 | return state.copy |
| 296 | } |
| 297 | |
| 298 | fn (vm &VM) goops_current_migration(value Value) ?&GoopsMigrationState { |
| 299 | for index := vm.active.len - 1; index >= 0; index-- { |
| 300 | mut scope := vm.active[index].goops_migrations |
| 301 | for !isnil(scope) { |
| 302 | if !scope.state.copy_plan && eqv(scope.state.object, value) { return scope.state } |
| 303 | scope = scope.previous |
| 304 | } |
| 305 | } |
| 306 | return none |
| 307 | } |
| 308 | |
| 309 | fn (mut m Machine) redefine_goops_binding(mut vm VM, depth int, slot int) ! { |
| 310 | mut previous := Value{ tag: .uninitialized } |
| 311 | if depth >= 0 { |
| 312 | previous = m.env.ancestor(depth).slots[slot] |
| 313 | } else { |
| 314 | mut global_slot := m.function.global_slots[slot] |
| 315 | if global_slot < 0 { |
| 316 | if found := vm.global_index_in(m.function.code.namespace, m.function.names[slot]) { |
| 317 | global_slot = found |
| 318 | m.function.global_slots[slot] = found |
| 319 | } |
| 320 | } |
| 321 | if global_slot >= 0 { previous = vm.globals[global_slot] } |
| 322 | } |
| 323 | if previous.tag == .uninitialized { return } |
| 324 | created := m.pop_value() |
| 325 | m.push(vm.goops.class_redefinition) |
| 326 | m.push(previous) |
| 327 | m.push(created) |
| 328 | m.apply(mut vm, 2, false)! |
| 329 | } |
| 330 | |
| 331 | fn goops_obsolete_method(methods []&GoopsMethod) Value { |
| 332 | for method in methods { |
| 333 | value := goops_method_value(method) |
| 334 | if goops_needs_migration(value) { return value } |
| 335 | } |
| 336 | return Value{} |
| 337 | } |
| 338 | |