// File: /workspace/github_c_makefile_star2_O2/atomlang[P]atomlang/src/pk_vm.c
// Function: vmCollectGarbage
void vmCollectGarbage(PKVM* vm) {

  // Reset VM's bytes_allocated value and count it again so that we don't
  // required to know the size of each object that'll be freeing.
  vm->bytes_allocated = 0;

  // Mark the core libs and builtin functions.
  markObject(vm, &vm->core_libs->_super);
  for (uint32_t i = 0; i < vm->builtins_count; i++) {
    markObject(vm, &vm->builtins[i].fn->_super);
  }

  // Mark the scripts cache.
  markObject(vm, &vm->scripts->_super);

  // Mark temp references.
  for (int i = 0; i < vm->temp_reference_count; i++) {
    markObject(vm, vm->temp_reference[i]);
  }

  // Mark the handles.
  for (PkHandle* h = vm->handles; h != NULL; h = h->next) {
    markValue(vm, h->value);
  }

  // Garbage collection triggered at the middle of a compilation.
  if (vm->compiler != NULL) {
    compilerMarkObjects(vm, vm->compiler);
  }

  if (vm->fiber != NULL) {
    markObject(vm, &vm->fiber->_super);
  }

  // Pop the marked objects from the working set and push all of it's
  // referenced objects. This will repeat till no more objects left in the
  // working set.
  popMarkedObjects(vm);

  // Now sweep all the un-marked objects in then link list and remove them
  // from the chain.

  // [ptr] is an Object* reference that should be equal to the next
  // non-garbage Object*.
  Object** ptr = &vm->first;
  while (*ptr != NULL) {

    // If the object the pointer points to wasn't marked it's unreachable.
    // Clean it. And update the pointer points to the next object.
    if (!(*ptr)->is_marked) {
      Object* garbage = *ptr;
      *ptr = garbage->next;
      freeObject(vm, garbage);

    } else {
      // Unmark the object for the next garbage collection.
      (*ptr)->is_marked = false;
      ptr = &(*ptr)->next;
    }
  }

  // Next GC heap size will be change depends on the byte we've left with now,
  // and the [heap_fill_percent].
  vm->next_gc = vm->bytes_allocated + (
    (vm->bytes_allocated * vm->heap_fill_percent) / 100);
  if (vm->next_gc < vm->min_heap_size) vm->next_gc = vm->min_heap_size;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rdi, %rbx
andq $0x0, 0x8(%rdi)
movq 0x130(%rdi), %rsi
callq 0x30a4
leaq 0x148(%rbx), %r14
xorl %r15d, %r15d
movl 0x5e8(%rbx), %eax
cmpq %rax, %r15
jae 0x9af7
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x30a4
incq %r15
addq $0x18, %r14
jmp 0x9ad8
movq 0x128(%rbx), %rsi
movq %rbx, %rdi
callq 0x30a4
xorl %r14d, %r14d
movslq 0xb8(%rbx), %rax
cmpq %rax, %r14
jge 0x9b27
movq 0x38(%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0x30a4
incq %r14
jmp 0x9b09
leaq 0xc0(%rbx), %r14
movq (%r14), %r14
testq %r14, %r14
je 0x9b47
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x3109
addq $0x10, %r14
jmp 0x9b2e
movq 0x120(%rbx), %rsi
testq %rsi, %rsi
je 0x9b5b
movq %rbx, %rdi
callq 0xc6e3
movq 0x5f0(%rbx), %rsi
testq %rsi, %rsi
je 0x9b6f
movq %rbx, %rdi
callq 0x30a4
movq %rbx, %rdi
callq 0x31fe
movq %rbx, %r14
movq (%r14), %rsi
testq %rsi, %rsi
je 0x9ba6
cmpb $0x0, 0x4(%rsi)
jne 0x9b99
movq 0x8(%rsi), %rax
movq %rax, (%r14)
movq %rbx, %rdi
callq 0x46f0
jmp 0x9b7a
movb $0x0, 0x4(%rsi)
addq $0x8, %rsi
movq %rsi, %r14
jmp 0x9b7a
movslq 0x20(%rbx), %rax
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rsi
imulq %rcx, %rax
pushq $0x64
popq %rdi
xorl %edx, %edx
divq %rdi
addq %rcx, %rax
cmpq %rsi, %rax
cmovbeq %rsi, %rax
movq %rax, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq