// File: kram[P]kram/./src/vm/vm.cpp
// Function: VM::name(Instruction*, vm::ON)
Value* VM::name(Instruction* ins, vm::ON on) {

	// Check if the value exists on the pushed class
	if (on == vm::ON::PUSHED_CLASS) {
		Value* top = this->lib_stack.back();

		// Get value from Class directly
		if (top->type == Type::CLASS) {
			Class* cl = static_cast<Class*>(top);
			return cl->get_value(ins->name);
		}

		// Create a Value of type NAME that can be used later
		if (top->type == Type::REFERENCE) {

			if (top->has_method(ins->name)) {
				return new Value(Type::NAME, ins->name);
			}

			std::cout << "REFERENCE: Undefined name: " << ins->name << "\n";
			exit(0);
		}

		// The type is something else, usually a builtin (eg. NUMBER)
		return new Value(Type::NAME, ins->name);
	}

	// Check if the value exists on the stack
	return this->name_get(ins->name);
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movq %rsi, 0x100(%rsp)
movl %edx, 0xfc(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpl $0x1, 0xfc(%rsp)
jne 0x106c9
movq 0x40(%rsp), %rdi
callq 0x134e0
movq (%rax), %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
cmpl $0x6, 0x8(%rax)
jne 0x104a6
movq 0xf0(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
leaq 0xc8(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x3140
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x13940
movq %rax, 0x38(%rsp)
jmp 0x10461
movq 0x38(%rsp), %rax
movq %rax, 0x110(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x31f0
jmp 0x106e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x31f0
jmp 0x106f7
movq 0xf0(%rsp), %rax
cmpl $0x4, 0x8(%rax)
jne 0x10624
movq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
leaq 0x98(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x3140
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1e940
movb %al, 0x27(%rsp)
jmp 0x104f8
leaq 0x98(%rsp), %rdi
callq 0x31f0
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x10512
jmp 0x105e7
movl $0x10, %edi
callq 0x32e0
movq %rax, 0x8(%rsp)
movb $0x1, 0x77(%rsp)
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
leaq 0x78(%rsp), %rdi
callq 0x3140
jmp 0x1053e
movq 0x8(%rsp), %rdi
movl $0x7, %esi
leaq 0x78(%rsp), %rdx
callq 0x1df60
jmp 0x10554
movq 0x8(%rsp), %rax
movb $0x0, 0x77(%rsp)
movq %rax, 0x110(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x31f0
jmp 0x106e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x31f0
jmp 0x106f7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x105cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x31f0
testb $0x1, 0x77(%rsp)
jne 0x105d8
jmp 0x105e2
movq 0x8(%rsp), %rdi
callq 0x32b0
jmp 0x106f7
movq 0x299ea(%rip), %rdi     # 0x39fd8
leaq 0x1af05(%rip), %rsi     # 0x2b4fa
callq 0x32d0
movq %rax, %rdi
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
callq 0x32a0
movq %rax, %rdi
leaq 0x1b249(%rip), %rsi     # 0x2b861
callq 0x32d0
xorl %edi, %edi
callq 0x3330
movl $0x10, %edi
callq 0x32e0
movq %rax, (%rsp)
movb $0x1, 0x4f(%rsp)
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
leaq 0x50(%rsp), %rdi
callq 0x3140
jmp 0x1064f
movq (%rsp), %rdi
movl $0x7, %esi
leaq 0x50(%rsp), %rdx
callq 0x1df60
jmp 0x10664
movq (%rsp), %rax
movb $0x0, 0x4f(%rsp)
movq %rax, 0x110(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x31f0
jmp 0x106e7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
jmp 0x106b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0x50(%rsp), %rdi
callq 0x31f0
testb $0x1, 0x4f(%rsp)
jne 0x106be
jmp 0x106c7
movq (%rsp), %rdi
callq 0x32b0
jmp 0x106f7
movq 0x40(%rsp), %rdi
movq 0x100(%rsp), %rsi
addq $0x8, %rsi
callq 0xfea0
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xc0(%rsp), %rdi
callq 0x3490
nopw %cs:(%rax,%rax)