// File: zerolfx[P]P-Machine/cupi.cpp
// Function: Cupi::execute(StackMachine*)
void Cupi::execute(StackMachine *stack)
{	
	/*
	if(stack->fMP + 4 > stack->fSP)
	{
		for(int i = 0; i < stack->fMP + 4 - stack->fSP; ++i)
		{
			stack->fStore.push_back(new StackElement());
			++stack->fSP;
		}
	}
	else
	{
		delete stack->fStore[stack->fMP + 4];
		stack->fStore[stack->fMP + 4] = 0;
	}
	*/
	
	stack->fStore[stack->fMP + 4] = new StackAddress(stack->fPC);
	
	if(stack->base(fP, stack->fStore[stack->fMP + 2]->getValue()) + fQ > stack->fSP)
	{
		throw ExecutionError("instruction cupi: trying to access a memorylocation above STORE[SP]");
	}
	stack->fPC = stack->fStore[stack->base(fP, stack->fStore[stack->fMP + 2]->getValue()) + fQ]->getValue();
	
	// adding cost of this instruction
	stack->fTime.count("cupi");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x80(%rbp)
movl $0x10, %edi
callq 0x7270
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x78(%rbp)
movq -0x10(%rbp), %rax
movl 0xb0(%rax), %esi
callq 0x26800
jmp 0xaf70
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
addl $0x4, %eax
movslq %eax, %rsi
callq 0x8e70
movq -0x78(%rbp), %rdx
movq %rax, %rcx
movq -0x80(%rbp), %rax
movq %rdx, (%rcx)
movq -0x10(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movl 0x8(%rax), %eax
movl %eax, -0x84(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xc0(%rax), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
movq -0x90(%rbp), %rdi
movl -0x84(%rbp), %esi
movl %eax, %edx
callq 0x2d050
movq -0x80(%rbp), %rcx
addl 0xc(%rcx), %eax
movq -0x10(%rbp), %rcx
cmpl 0xb4(%rcx), %eax
jle 0xb0d7
movb $0x1, -0x42(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0xa0(%rbp)
leaq -0x41(%rbp), %rdi
movq %rdi, -0x98(%rbp)
callq 0x74f0
movq -0x98(%rbp), %rdx
leaq 0x2c43b(%rip), %rsi     # 0x3746e
leaq -0x40(%rbp), %rdi
callq 0x8940
jmp 0xb03e
movq -0xa0(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0x283d0
jmp 0xb050
movq -0xa0(%rbp), %rdi
movb $0x0, -0x42(%rbp)
leaq 0x3ea1e(%rip), %rsi     # 0x49a80
leaq 0x1d397(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xb1f8
movq -0x78(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
movl $0x10, %esi
callq 0x7280
jmp 0xb1ef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xb0b5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x7170
leaq -0x41(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x42(%rbp)
jne 0xb0c6
jmp 0xb0d2
movq -0xa0(%rbp), %rdi
callq 0x71c0
jmp 0xb1ef
movq -0x80(%rbp), %rcx
movq -0x10(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq %rax, %rdi
addq $0x18, %rdi
movq %rdi, -0xb8(%rbp)
movl 0x8(%rcx), %ecx
movl %ecx, -0xbc(%rbp)
movl 0xc0(%rax), %eax
addl $0x2, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %esi
movl %eax, %edx
callq 0x2d050
movq -0x80(%rbp), %rcx
movq -0xb8(%rbp), %rdi
movl 0xc(%rcx), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xb0(%rax)
movq -0x10(%rbp), %rax
addq $0x78, %rax
movq %rax, -0xb0(%rbp)
leaq -0x69(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x74f0
movq -0xa8(%rbp), %rdx
leaq 0x2c327(%rip), %rsi     # 0x374b2
leaq -0x68(%rbp), %rdi
callq 0x8940
jmp 0xb196
movq -0xb0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2c4d0
jmp 0xb1a8
leaq -0x68(%rbp), %rdi
callq 0x7170
leaq -0x69(%rbp), %rdi
callq 0x72e0
addq $0xd0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0xb1e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x7170
leaq -0x69(%rbp), %rdi
callq 0x72e0
movq -0x18(%rbp), %rdi
callq 0x74e0
nopl (%rax,%rax)