// File: /workspace/github_c_makefile_star2_O2/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 0x4800
leaq 0x13919(%rip), %rcx     # 0x18028
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x10(%rbx), %rdi
movq %r14, %rsi
callq 0x2c81
jmp 0x4800
leaq 0x28(%rbx), %rdi
movq %r14, %rsi
callq 0x2aac
jmp 0x4800
movq 0x18(%rbx), %rsi
jmp 0x47f4
cmpb $0x1, 0x18(%rbx)
jne 0x4814
movq 0xe8(%r14), %rax
testq %rax, %rax
je 0x4800
movq 0x20(%rbx), %rsi
movl 0x1c(%rbx), %edx
movq %r14, %rdi
callq *%rax
jmp 0x4800
leaq 0x20(%rbx), %rdi
movq %r14, %rsi
callq 0x2c81
leaq 0x30(%rbx), %rdi
movq %r14, %rsi
callq 0x2aac
leaq 0x70(%rbx), %rdi
movq %r14, %rsi
callq 0x2c81
leaq 0x40(%rbx), %rdi
movq %r14, %rsi
callq 0x2e6f
leaq 0x50(%rbx), %rdi
movq %r14, %rsi
callq 0x2f66
leaq 0x60(%rbx), %rdi
movq %r14, %rsi
callq 0x2d78
jmp 0x4800
cmpb $0x0, 0x30(%rbx)
jne 0x4800
movq 0x38(%rbx), %rdi
movq %r14, %rsi
callq 0x2ba1
movq 0x38(%rbx), %rdi
addq $0x10, %rdi
movq %r14, %rsi
callq 0x2aac
movq 0x38(%rbx), %rsi
jmp 0x47f4
movq 0x20(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5a41
movq 0x40(%rbx), %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x5a41
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
popq %rbx
popq %r14
popq %r15
jmp 0x5a41
movq 0x20(%rbx), %r15
leaq 0x8(%r15), %rdi
movq %r14, %rsi
callq 0x2c81
movq %r14, %rdi
movq %r15, %rsi
jmp 0x47f7