// File: /workspace/github_c_makefile_star2_O1/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
movq $0x0, 0x8(%rdi)
movq 0x130(%rdi), %rsi
callq 0x8b42
cmpl $0x0, 0x5e8(%rbx)
je 0xe45b
leaq 0x148(%rbx), %r14
xorl %r15d, %r15d
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x8b42
incq %r15
movl 0x5e8(%rbx), %eax
addq $0x18, %r14
cmpq %rax, %r15
jb 0xe43e
movq 0x128(%rbx), %rsi
movq %rbx, %rdi
callq 0x8b42
cmpl $0x0, 0xb8(%rbx)
jle 0xe492
xorl %r14d, %r14d
movq 0x38(%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0x8b42
incq %r14
movslq 0xb8(%rbx), %rax
cmpq %rax, %r14
jl 0xe476
movq 0xc0(%rbx), %r14
testq %r14, %r14
je 0xe4af
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x8ba0
movq 0x10(%r14), %r14
jmp 0xe499
movq 0x120(%rbx), %rsi
testq %rsi, %rsi
je 0xe4c3
movq %rbx, %rdi
callq 0x1130a
movq 0x5f0(%rbx), %rsi
testq %rsi, %rsi
je 0xe4d7
movq %rbx, %rdi
callq 0x8b42
movq %rbx, %rdi
callq 0x8cc9
movq (%rbx), %rsi
testq %rsi, %rsi
je 0xe514
movq %rbx, %r14
cmpb $0x0, 0x4(%rsi)
je 0xe4fd
movb $0x0, 0x4(%rsi)
addq $0x8, %rsi
movq %rsi, %r14
jmp 0xe50c
movq 0x8(%rsi), %rax
movq %rax, (%r14)
movq %rbx, %rdi
callq 0xa2b4
movq (%r14), %rsi
testq %rsi, %rsi
jne 0xe4ea
movslq 0x20(%rbx), %rax
movq 0x8(%rbx), %rcx
movq 0x18(%rbx), %rsi
imulq %rcx, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rdx # imm = 0x28F5C28F5C28F5C3
mulq %rdx
shrq $0x2, %rdx
addq %rcx, %rdx
cmpq %rsi, %rdx
cmovbeq %rsi, %rdx
movq %rdx, 0x10(%rbx)
popq %rbx
popq %r14
popq %r15
retq