// File: /workspace/github_c_makefile_star2_O0/adamjedlicka[P]blu/src/vm/memory.c
// Function: freeObject
static void freeObject(bluVM* vm, bluObj* object) {

#ifdef DEBUG_GC_TRACE
	printf("%p free ", object);
	bluPrintValue(OBJ_VAL(object));
	printf("\n");
#endif

	switch (object->type) {

	case OBJ_ARRAY: {
		bluObjArray* array = (bluObjArray*)object;
		bluDeallocate(vm, array->data, (sizeof(bluValue) * array->cap));
		bluDeallocate(vm, array, sizeof(bluObjArray));
		break;
	}

	case OBJ_BOUND_METHOD: {
		bluObjBoundMethod* boundMethod = (bluObjBoundMethod*)object;
		bluDeallocate(vm, boundMethod, sizeof(bluObjBoundMethod));
		break;
	}

	case OBJ_CLASS: {
		bluObjClass* class = (bluObjClass*)object;
		bluTableFree(vm, &class->methods);
		bluTableFree(vm, &class->fields);
		bluDeallocate(vm, class, sizeof(bluObjClass));
		break;
	}

	case OBJ_CLOSURE: {
		bluObjClosure* closure = (bluObjClosure*)object;
		bluObjUpvalueBufferFree(&closure->upvalues);
		bluDeallocate(vm, closure, sizeof(bluObjClosure));
		break;
	}

	case OBJ_FUNCTION: {
		bluObjFunction* function = (bluObjFunction*)object;
		bluChunkFree(&function->chunk);
		bluDeallocate(vm, function, sizeof(bluObjFunction));
		break;
	}

	case OBJ_INSTANCE: {
		bluObjInstance* instance = (bluObjInstance*)object;

		if (instance->obj.class->destruct != NULL) {
			instance->obj.class->destruct(vm, instance);
		}

		bluTableFree(vm, &instance->fields);
		bluDeallocate(vm, instance, sizeof(bluObjInstance));
		break;
	}

	case OBJ_NATIVE: {
		bluObjNative* native = (bluObjNative*)object;
		bluDeallocate(vm, native, sizeof(bluObjNative));
		break;
	}

	case OBJ_UPVALUE: {
		bluObjUpvalue* upvalue = (bluObjUpvalue*)object;
		bluDeallocate(vm, upvalue, sizeof(bluObjUpvalue));
		break;
	}

	case OBJ_STRING: {
		bluObjString* string = (bluObjString*)object;
		bluDeallocate(vm, string, sizeof(bluObjString) + (sizeof(char) * (string->length + 1)));
		break;
	}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
subq $0x8, %rax
ja 0x73e0
movq -0x60(%rbp), %rax
leaq 0x8589(%rip), %rcx      # 0xf7b8
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x18(%rbp), %rax
movslq 0x24(%rax), %rdx
shlq $0x4, %rdx
callq 0x6f40
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl $0x30, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl $0x38, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x30, %rsi
callq 0x9360
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x40, %rsi
callq 0x9360
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl $0x60, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
addq $0x28, %rdi
callq 0x7470
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl $0x38, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
addq $0x28, %rdi
callq 0x1b40
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rsi
movl $0x80, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
cmpq $0x0, 0x58(%rax)
je 0x7359
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x58(%rax), %rax
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
callq *%rax
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
addq $0x20, %rsi
callq 0x9360
movq -0x8(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl $0x38, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
movl $0x30, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
movl $0x40, %edx
callq 0x6f40
jmp 0x73e0
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x58(%rbp), %rax
movl 0x20(%rax), %eax
addl $0x1, %eax
movslq %eax, %rdx
shlq $0x0, %rdx
addq $0x28, %rdx
callq 0x6f40
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)