// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x48, %rsp
movq %rdi, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movq 0x38(%rsp), %rax
movl (%rax), %eax
movq %rax, (%rsp)
subq $0x8, %rax
ja 0x687f
movq (%rsp), %rax
leaq 0x23a02(%rip), %rcx     # 0x2a070
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
jmp 0x687f
movq 0x38(%rsp), %rdi
addq $0x10, %rdi
movq 0x40(%rsp), %rsi
callq 0x3360
jmp 0x687f
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rax
movq 0x18(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
jmp 0x687f
jmp 0x687f
movq 0x38(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
addq $0x20, %rdi
movq 0x40(%rsp), %rsi
callq 0x3360
movq 0x30(%rsp), %rdi
addq $0x30, %rdi
movq 0x40(%rsp), %rsi
callq 0x2eb0
movq 0x30(%rsp), %rdi
addq $0x70, %rdi
movq 0x40(%rsp), %rsi
callq 0x3360
movq 0x30(%rsp), %rdi
addq $0x40, %rdi
movq 0x40(%rsp), %rsi
callq 0x3800
movq 0x30(%rsp), %rdi
addq $0x50, %rdi
movq 0x40(%rsp), %rsi
callq 0x3a50
movq 0x30(%rsp), %rdi
addq $0x60, %rdi
movq 0x40(%rsp), %rsi
callq 0x35b0
jmp 0x687f
movq 0x38(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rax
testb $0x1, 0x30(%rax)
jne 0x6792
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
movq 0x40(%rsp), %rsi
callq 0x3100
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rdi
addq $0x10, %rdi
movq 0x40(%rsp), %rsi
callq 0x2eb0
movq 0x40(%rsp), %rdi
movq 0x28(%rsp), %rax
movq 0x38(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
jmp 0x687f
movq 0x38(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x20(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
movq 0x40(%rsp), %rdi
movq 0x20(%rsp), %rax
movq 0x40(%rax), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
jmp 0x687f
movq 0x38(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
addq $0x28, %rdi
movq 0x40(%rsp), %rsi
callq 0x2eb0
jmp 0x687f
movq 0x38(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
testb $0x1, 0x18(%rax)
je 0x6846
movq 0x40(%rsp), %rax
cmpq $0x0, 0xe8(%rax)
je 0x6844
movq 0x40(%rsp), %rax
movq 0xe8(%rax), %rax
movq 0x40(%rsp), %rdi
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rsi
movq 0x10(%rsp), %rcx
movl 0x1c(%rcx), %edx
callq *%rax
jmp 0x687d
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
movq 0x40(%rsp), %rsi
callq 0x3360
movq 0x40(%rsp), %rdi
movq 0x8(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
jmp 0x687f
movq 0x40(%rsp), %rdi
movq 0x38(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x8f90
addq $0x48, %rsp
retq
nopw (%rax,%rax)