// File: /workspace/github_c_makefile_star2_O1/atomlang[P]atomlang/src/pk_var.c
// Function: freeObject
void freeObject(PKVM* vm, Object* self) {
  // TODO: Debug trace memory here.

  // First clean the object's references, but we're not recursively
  // deallocating them because they're not marked and will be cleaned later.
  // Example: List's `elements` is VarBuffer that contain a heap allocated
  // array of `var*` which will be cleaned below but the actual `var` elements
  // will won't be freed here instead they haven't marked at all, and will be
  // removed at the sweeping phase of the garbage collection.
  switch (self->type) {
    case OBJ_STRING:
      break;

    case OBJ_LIST:
      pkVarBufferClear(&(((List*)self)->elements), vm);
      break;

    case OBJ_MAP:
      DEALLOCATE(vm, ((Map*)self)->entries);
      break;

    case OBJ_RANGE:
      break;

    case OBJ_SCRIPT: {
      Script* scr = (Script*)self;
      pkVarBufferClear(&scr->globals, vm);
      pkUintBufferClear(&scr->global_names, vm);
      pkVarBufferClear(&scr->literals, vm);
      pkFunctionBufferClear(&scr->functions, vm);
      pkClassBufferClear(&scr->classes, vm);
      pkStringBufferClear(&scr->names, vm);
    } break;

    case OBJ_FUNC: {
      Function* func = (Function*)self;
      if (!func->is_native) {
        pkByteBufferClear(&func->fn->opcodes, vm);
        pkUintBufferClear(&func->fn->oplines, vm);
        DEALLOCATE(vm, func->fn);
      }
    } break;

    case OBJ_FIBER: {
      Fiber* fiber = (Fiber*)self;
      DEALLOCATE(vm, fiber->stack);
      DEALLOCATE(vm, fiber->frames);
    } break;

    case OBJ_CLASS: {
      Class* type = (Class*)self;
      pkUintBufferClear(&type->field_names, vm);
    } break;

    case OBJ_INST:
    {
      Instance* inst = (Instance*)self;

      if (inst->is_native) {
        if (vm->config.inst_free_fn != NULL) {
          // TODO: Allow user to set error when freeing the object.
          vm->config.inst_free_fn(vm, inst->native, inst->native_id);
        }

      } else {
        Inst* ins = inst->ins;
        pkVarBufferClear(&ins->fields, vm);
        DEALLOCATE(vm, ins);
      }

      break;
    }
  }

  DEALLOCATE(vm, self);
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
movq %rsi, %rbx
movq %rdi, %r14
movl (%rsi), %eax
decl %eax
cmpl $0x7, %eax
ja 0xa466
leaq 0x12d55(%rip), %rcx     # 0x1d028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x10(%rbx), %rsi
movl 0x1c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rbx)
jmp 0xa466
movq 0x28(%rbx), %rsi
movl 0x34(%rbx), %edx
shlq $0x2, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x28(%rbx)
jmp 0xa466
movq 0x18(%rbx), %rsi
jmp 0xa45a
cmpb $0x1, 0x18(%rbx)
jne 0xa47a
movq 0xe8(%r14), %rax
testq %rax, %rax
je 0xa466
movq 0x20(%rbx), %rsi
movl 0x1c(%rbx), %edx
movq %r14, %rdi
callq *%rax
jmp 0xa466
movq 0x20(%rbx), %rsi
movl 0x2c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x20(%rbx)
movq 0x30(%rbx), %rsi
movl 0x3c(%rbx), %edx
shlq $0x2, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x30(%rbx)
movq 0x70(%rbx), %rsi
movl 0x7c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x70(%rbx)
movq 0x40(%rbx), %rsi
movl 0x4c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x40(%rbx)
movq 0x50(%rbx), %rsi
movl 0x5c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x50(%rbx)
movq 0x60(%rbx), %rsi
movl 0x6c(%rbx), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x60(%rbx)
jmp 0xa466
cmpb $0x0, 0x30(%rbx)
jne 0xa466
movq 0x38(%rbx), %r15
movq (%r15), %rsi
movl 0xc(%r15), %edx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, (%r15)
movq 0x38(%rbx), %r15
movq 0x10(%r15), %rsi
movl 0x1c(%r15), %edx
shlq $0x2, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%r15)
movq 0x38(%rbx), %rsi
jmp 0xa45a
movq 0x20(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xb668
movq 0x40(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0xb668
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
popq %rbx
popq %r14
popq %r15
jmp 0xb668
movq 0x20(%rbx), %r15
movq 0x8(%r15), %rsi
movl 0x14(%r15), %edx
shlq $0x3, %rdx
movq %r14, %rdi
xorl %ecx, %ecx
callq 0xb668
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r15)
movq %r14, %rdi
movq %r15, %rsi
jmp 0xa45d