// File: /workspace/github_c_makefile_star2_O3/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 0x8a84
cmpl $0x0, 0x5e8(%rbx)
je 0xdffb
leaq 0x148(%rbx), %r14
xorl %r15d, %r15d
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x8a84
incq %r15
movl 0x5e8(%rbx), %eax
addq $0x18, %r14
cmpq %rax, %r15
jb 0xdfde
movq 0x128(%rbx), %rsi
movq %rbx, %rdi
callq 0x8a84
cmpl $0x0, 0xb8(%rbx)
jle 0xe032
xorl %r14d, %r14d
movq 0x38(%rbx,%r14,8), %rsi
movq %rbx, %rdi
callq 0x8a84
incq %r14
movslq 0xb8(%rbx), %rax
cmpq %rax, %r14
jl 0xe016
movq 0xc0(%rbx), %r14
testq %r14, %r14
je 0xe04f
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x8ae9
movq 0x10(%r14), %r14
jmp 0xe039
movq 0x120(%rbx), %rsi
testq %rsi, %rsi
je 0xe063
movq %rbx, %rdi
callq 0x10f6d
movq 0x5f0(%rbx), %rsi
testq %rsi, %rsi
je 0xe077
movq %rbx, %rdi
callq 0x8a84
movq %rbx, %rdi
callq 0x8c16
movq (%rbx), %rsi
testq %rsi, %rsi
je 0xe0b4
movq %rbx, %r14
cmpb $0x0, 0x4(%rsi)
je 0xe09d
movb $0x0, 0x4(%rsi)
addq $0x8, %rsi
movq %rsi, %r14
jmp 0xe0ac
movq 0x8(%rsi), %rax
movq %rax, (%r14)
movq %rbx, %rdi
callq 0xa18e
movq (%r14), %rsi
testq %rsi, %rsi
jne 0xe08a
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