// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movq 0x150(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x134e0
movq (%rax), %rax
movq %rax, 0x138(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x33a0
movq 0x138(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x44(%rsp)
subl $0x1, %eax
je 0x1271e
jmp 0x126df
movl 0x44(%rsp), %eax
subl $0x2, %eax
jne 0x12739
jmp 0x126ea
leaq 0x18fae(%rip), %rsi     # 0x2b69f
leaq 0x118(%rsp), %rdi
callq 0x3380
jmp 0x12700
jmp 0x127d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x12a3f
leaq 0x18f81(%rip), %rsi     # 0x2b6a6
leaq 0x118(%rsp), %rdi
callq 0x3380
jmp 0x12734
jmp 0x127d2
movq 0x27898(%rip), %rdi     # 0x39fd8
leaq 0x18f66(%rip), %rsi     # 0x2b6ad
callq 0x32d0
movq %rax, 0x30(%rsp)
jmp 0x12753
movq 0x138(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
xorl %edx, %edx
callq 0x1e430
jmp 0x1276c
movq 0x30(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x32a0
movq %rax, 0x28(%rsp)
jmp 0x12785
movq 0x28(%rsp), %rdi
leaq 0x190d0(%rip), %rsi     # 0x2b861
callq 0x32d0
jmp 0x12798
leaq 0xe8(%rsp), %rdi
callq 0x31f0
xorl %edi, %edi
callq 0x3330
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x31f0
jmp 0x12a3f
movq 0x38(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0xfea0
movq %rax, 0x20(%rsp)
jmp 0x127eb
movq 0x20(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq $0x1, 0xc0(%rsp)
leaq 0xaf(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x5730
movq 0x18(%rsp), %rcx
movq 0xb8(%rsp), %rsi
movq 0xc0(%rsp), %rdx
leaq 0xc8(%rsp), %rdi
callq 0x14380
jmp 0x1285a
leaq 0xaf(%rsp), %rdi
callq 0x5c20
movq 0x148(%rsp), %rax
addq $0x50, %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x4240
movq %rax, 0x98(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x4270
movq %rax, 0x90(%rsp)
leaq 0x98(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x42a0
testb $0x1, %al
jne 0x128c3
jmp 0x1296b
leaq 0x98(%rsp), %rdi
callq 0x42e0
movq 0x38(%rsp), %rdi
movq (%rax), %rax
movq %rax, 0x88(%rsp)
movq 0x88(%rsp), %rsi
callq 0x10220
movq %rax, 0x10(%rsp)
jmp 0x128f4
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x141e0
jmp 0x12918
jmp 0x1291a
leaq 0x98(%rsp), %rdi
callq 0x5410
jmp 0x128a5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0xaf(%rsp), %rdi
callq 0x5c20
jmp 0x12a3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x12a32
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x140(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x1e9e0
jmp 0x1298c
leaq 0x48(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x14280
jmp 0x129a0
movq 0x8(%rsp), %rdi
leaq 0x60(%rsp), %rsi
leaq 0x48(%rsp), %rdx
callq 0x1e140
movq %rax, (%rsp)
jmp 0x129ba
leaq 0x48(%rsp), %rdi
callq 0x5850
leaq 0x60(%rsp), %rdi
callq 0x31f0
leaq 0xc8(%rsp), %rdi
callq 0x5850
leaq 0x118(%rsp), %rdi
callq 0x31f0
movq (%rsp), %rax
addq $0x158, %rsp            # imm = 0x158
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x12a28
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x5850
leaq 0x60(%rsp), %rdi
callq 0x31f0
leaq 0xc8(%rsp), %rdi
callq 0x5850
leaq 0x118(%rsp), %rdi
callq 0x31f0
movq 0x110(%rsp), %rdi
callq 0x3490
nopl (%rax)