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

	// Get the value from the top of the stack
	Value* builtin_value = this->lib_stack.back();

	// Get library
	std::string lib_name;// = new std::string;

	switch (builtin_value->type) {
		case Type::NUMBER: lib_name = "Number"; break;
		case Type::STRING: lib_name = "String"; break;
		default:
			std::cout << "call_builtin(): Can not call on " << builtin_value->print() << "\n";
			exit(0);
			break;
	}

	Value* lib = this->name_get(lib_name);

	// Initialize parameter vector with the first value beeing the bultin itself
	std::vector<Value*> params { builtin_value };

	for (auto param : ins->right) {
		params.push_back(this->run(param));
	}

	// Call the method
	return lib->exec_method(name->getString(), params);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %r14
movq 0x8(%rdi), %rax
movq -0x8(%rax), %r13
leaq 0x50(%rsp), %rax
movq %rax, -0x10(%rax)
movq $0x0, -0x8(%rax)
movb $0x0, (%rax)
movl 0x8(%r13), %eax
cmpl $0x1, %eax
je 0x92f1
cmpl $0x2, %eax
jne 0x9421
leaq 0x9442(%rip), %rcx      # 0x12731
jmp 0x92f8
leaq 0x9440(%rip), %rcx      # 0x12738
leaq 0x40(%rsp), %rdi
movl $0x6, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3320
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x7ab4
movq %rax, 0x38(%rsp)
leaq 0x30(%rsp), %rsi
movq %r13, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x1, %edx
callq 0xa064
movq 0x50(%r12), %rbp
movq 0x58(%r12), %rbx
cmpq %rbx, %rbp
je 0x9391
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %r13
movq (%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x891c
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x937d
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x9388
movq %r12, %rdi
movq %r13, %rdx
callq 0xc6a8
addq $0x8, %rbp
cmpq %rbx, %rbp
jne 0x9353
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0xd34a
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x9fe4
leaq 0x78(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq 0x38(%rsp), %rdi
callq 0xced6
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x93d3
callq 0x3230
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x93e9
callq 0x3230
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x93f8
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x940c
callq 0x3230
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x11ba8(%rip), %rdi     # 0x1afd0
leaq 0x9310(%rip), %rsi      # 0x1273f
movl $0x20, %edx
callq 0x32a0
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xd094
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x11b77(%rip), %rdi     # 0x1afd0
callq 0x32a0
leaq 0x948e(%rip), %rsi      # 0x128f3
movq %rax, %rdi
callq 0x3250
leaq 0x10(%rsp), %rdi
callq 0x3120
xorl %edi, %edi
callq 0x32c0
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
jne 0x94da
jmp 0x94df
jmp 0x94c8
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x94aa
callq 0x3230
jmp 0x94aa
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x94d0
callq 0x3230
jmp 0x94d0
jmp 0x94cd
jmp 0x94c8
jmp 0x94c8
movq %rax, %rbx
jmp 0x94df
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x94df
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x94f3
callq 0x3230
movq %rbx, %rdi
callq 0x33c0
nop