// File: /workspace/github_cpp_makefile_star2_O1/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 0x9397
cmpl $0x2, %eax
jne 0x94c7
leaq 0xa3b0(%rip), %rcx      # 0x13745
jmp 0x939e
leaq 0xa3ae(%rip), %rcx      # 0x1374c
leaq 0x40(%rsp), %rdi
movl $0x6, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x3320
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x7b08
movq %rax, 0x38(%rsp)
leaq 0x30(%rsp), %rsi
movq %r13, (%rsi)
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movl $0x1, %edx
callq 0xa11c
movq 0x50(%r12), %rbp
movq 0x58(%r12), %rbx
cmpq %rbx, %rbp
je 0x9437
leaq 0x10(%rsp), %r12
leaq 0x30(%rsp), %r13
movq (%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x89c2
movq %rax, 0x30(%rsp)
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x9423
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x942e
movq %r12, %rdi
movq %r13, %rdx
callq 0xc7ee
addq $0x8, %rbp
cmpq %rbx, %rbp
jne 0x93f9
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0xd4ba
leaq 0x60(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa09c
leaq 0x78(%rsp), %rsi
leaq 0x60(%rsp), %rdx
movq 0x38(%rsp), %rdi
callq 0xd046
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x9479
callq 0x3230
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x948f
callq 0x3230
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x949e
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x94b2
callq 0x3230
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x12b02(%rip), %rdi     # 0x1bfd0
leaq 0xa27e(%rip), %rsi      # 0x13753
movl $0x20, %edx
callq 0x32a0
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
xorl %edx, %edx
callq 0xd204
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x12ad1(%rip), %rdi     # 0x1bfd0
callq 0x32a0
leaq 0xa3fc(%rip), %rsi      # 0x13907
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 0x9580
jmp 0x9585
jmp 0x956e
movq %rax, %rbx
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x9550
callq 0x3230
jmp 0x9550
movq %rax, %rbx
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x9576
callq 0x3230
jmp 0x9576
jmp 0x9573
jmp 0x956e
jmp 0x956e
movq %rax, %rbx
jmp 0x9585
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x9585
callq 0x3230
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x9599
callq 0x3230
movq %rbx, %rdi
callq 0x33c0
nop