// File: /workspace/github_c_makefile_star2_O0/adamjedlicka[P]blu/src/vm/memory.c
// Function: bluGrayObject
void bluGrayObject(bluVM* vm, bluObj* object) {
	if (object == NULL) return;

	if (object->isDark) return;

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

	object->isDark = true;

	bluGrayObject(vm, (bluObj*)object->class);

	switch (object->type) {

	case OBJ_ARRAY: {
		bluObjArray* array = (bluObjArray*)object;
		for (int32_t i = 0; i < array->len; i++) {
			bluGrayValue(vm, array->data[i]);
		}
		break;
	}

	case OBJ_BOUND_METHOD: {
		bluObjBoundMethod* boundMethod = (bluObjBoundMethod*)object;
		bluGrayValue(vm, boundMethod->receiver);
		bluGrayObject(vm, (bluObj*)boundMethod->closure);
		break;
	}

	case OBJ_CLASS: {
		bluObjClass* class = (bluObjClass*)object;
		bluGrayObject(vm, (bluObj*)class->name);
		bluGrayTable(vm, &class->methods);
		bluGrayTable(vm, &class->fields);
		break;
	}

	case OBJ_CLOSURE: {
		bluObjClosure* closure = (bluObjClosure*)object;
		bluGrayObject(vm, (bluObj*)closure->function);
		for (int32_t i = 0; i < closure->upvalues.count; i++) {
			bluGrayObject(vm, &closure->upvalues.data[i]->obj);
		}
		break;
	}

	case OBJ_FUNCTION: {
		bluObjFunction* function = (bluObjFunction*)object;
		bluGrayObject(vm, (bluObj*)function->name);
		bluGrayValueBuffer(vm, &function->chunk.constants);
		break;
	}

	case OBJ_INSTANCE: {
		bluObjInstance* instance = (bluObjInstance*)object;
		bluGrayTable(vm, &instance->fields);
		break;
	}

	case OBJ_NATIVE: {
		break;
	}

	case OBJ_UPVALUE: {
		bluObjUpvalue* upvalue = (bluObjUpvalue*)object;
		bluGrayValue(vm, upvalue->closed);
		break;
	}

	case OBJ_STRING: {
		break;
	}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpq $0x0, -0x10(%rbp)
jne 0x6bdc
jmp 0x6daa
movq -0x10(%rbp), %rax
testb $0x1, 0x10(%rax)
je 0x6beb
jmp 0x6daa
movq -0x10(%rbp), %rax
movb $0x1, 0x10(%rax)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x6bc0
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x60(%rbp)
subq $0x8, %rax
ja 0x6daa
movq -0x60(%rbp), %rax
leaq 0x8b71(%rip), %rcx      # 0xf794
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x10(%rbp), %rax
movq %rax, -0x18(%rbp)
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jge 0x6c74
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x1c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %esi
movq 0x8(%rax), %rdx
callq 0x6b90
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x6c3b
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movl 0x20(%rax), %esi
movq 0x28(%rax), %rdx
callq 0x6b90
movq -0x8(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x30(%rax), %rsi
callq 0x6bc0
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x6bc0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x30, %rsi
callq 0x6db0
movq -0x8(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x40, %rsi
callq 0x6db0
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rsi
callq 0x6bc0
movl $0x0, -0x3c(%rbp)
movl -0x3c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpl 0x30(%rcx), %eax
jge 0x6d3b
movq -0x8(%rbp), %rdi
movq -0x38(%rbp), %rax
movq 0x28(%rax), %rax
movslq -0x3c(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x6bc0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6d0b
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rax
movq 0x78(%rax), %rsi
callq 0x6bc0
movq -0x8(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x28, %rsi
addq $0x40, %rsi
callq 0x6e20
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x20, %rsi
callq 0x6db0
jmp 0x6daa
jmp 0x6daa
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl 0x28(%rax), %esi
movq 0x30(%rax), %rdx
callq 0x6b90
jmp 0x6daa
jmp 0x6daa
addq $0x60, %rsp
popq %rbp
retq