// File: /workspace/github_cpp_makefile_star2_O2/zerolfx[P]P-Machine/sto.cpp
// Function: Sto::execute(StackMachine*)
void Sto::execute(StackMachine *stack)
{
	StackAddress p;
	
	if(stack->fSP < 1)
		throw ExecutionError("instruction sto: at least 2 stackelements are required for this operation");
		
	if(typeid(p) != typeid(*(stack->fStore[stack->fSP - 1])))
		throw ExecutionError("instruction sto: type pointed to by SP - 1 is not of type address.");
				
	switch(fType)
	{
		case integer:
		{
			StackInteger p1;

			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
			{
				throw ExecutionError("instruction sto: type pointed to by SP is not of type integer.");
			}
				
			break;
		}	
		case real:
		{
			StackReal p1;

			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
			{
				throw ExecutionError("instruction sto: type pointed to by SP is not of type real.");
			}

			break;
		}
		case boolean:
		{
			StackBoolean p1;
			
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
			{
				throw ExecutionError("instruction sto: type pointed to by SP is not of type boolean.");
			}

			break;
		}
		case character:
		{
			StackCharacter p1;
			
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
			{
				throw ExecutionError("instruction sto: type pointed to by SP is not of type character.");
			}


			break;
		}
		case address:
		{
			StackAddress p1;

			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
			{
				throw ExecutionError("instruction sto: type pointed to by SP is not of type address.");
			}
				
			break;
		}
	}
	
	if(stack->fStore[stack->fSP - 1]->heapAddress())
	{
		if(stack->fStore[stack->fSP - 1]->getValue() < stack->fNP)
		{
			throw ExecutionError("instruction sto: invalid heap address.");
		}
		else
		{
			delete stack->fHeap[-stack->fStore[stack->fSP - 1]->getValue() - 1];
			stack->fHeap[-stack->fStore[stack->fSP - 1]->getValue() - 1] = stack->fStore[stack->fSP];
		}
	}
	else
	{
		if(stack->fStore[stack->fSP - 1]->getValue() > stack->fSP - 2) 	
		{
			// -2 because the SP will be decreased by 2 as a result of this operation
			
			throw ExecutionError("instruction sto: invalid stack address.");
		}
		else
		{
			delete stack->fStore[stack->fStore[stack->fSP - 1]->getValue()];
			stack->fStore[stack->fStore[stack->fSP - 1]->getValue()] = stack->fStore[stack->fSP];
		}
	}
	
	stack->fStore.pop_back();
	delete stack->fStore[stack->fSP - 1];
	stack->fStore[stack->fSP - 1] = 0;
	stack->fStore.pop_back();

	stack->fSP -= 2;
	
	// adding cost of this instruction
	stack->fTime.count("sto");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x1704c
movslq 0xb4(%rbx), %rax
testq %rax, %rax
jle 0x14975
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rax
testq %rax, %rax
je 0x149c1
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a320(%rip), %rdi     # 0x2e960
callq 0x76ba
testb %al, %al
jne 0x149c6
movl 0x8(%r14), %eax
cmpq $0x4, %rax
ja 0x147e1
leaq 0xc4aa(%rip), %rcx      # 0x20b0c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0x30(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0x177d4
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x14ab4
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a52b(%rip), %rdi     # 0x2ebc8
callq 0x76ba
testb %al, %al
jne 0x14ba7
leaq -0x30(%rbp), %rdi
callq 0x17802
jmp 0x147e1
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x1704c
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x14aaa
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a277(%rip), %rdi     # 0x2e960
callq 0x76ba
testb %al, %al
jne 0x14ac3
leaq -0x30(%rbp), %rdi
callq 0x17070
jmp 0x147e1
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x17a18
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x14aaf
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a563(%rip), %rdi     # 0x2ec98
callq 0x76ba
testb %al, %al
jne 0x14b0f
leaq -0x30(%rbp), %rdi
callq 0x17a3c
jmp 0x147e1
leaq -0x30(%rbp), %rdi
pushq $0x61
popq %rsi
callq 0x17c08
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x14ab9
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a5e6(%rip), %rdi     # 0x2ed68
callq 0x76ba
testb %al, %al
jne 0x14b5b
leaq -0x30(%rbp), %rdi
callq 0x17c2c
jmp 0x147e1
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
callq 0x175ac
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x14abe
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1a32d(%rip), %rdi     # 0x2eaf8
callq 0x76ba
testb %al, %al
jne 0x14bf0
leaq -0x30(%rbp), %rdi
callq 0x175d0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x88(%rax)
movslq 0xb4(%rbx), %rcx
movq 0x18(%rbx), %rdx
movq -0x8(%rdx,%rcx,8), %rdi
movq (%rdi), %rcx
movq 0x68(%rcx), %rcx
testb %al, %al
je 0x1487e
callq *%rcx
cmpl 0xb8(%rbx), %eax
jl 0x14a12
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
leaq 0x30(%rbx), %r15
notl %eax
cltq
movq (%r15), %rcx
movq (%rcx,%rax,8), %r14
testq %r14, %r14
je 0x14855
movq %r14, %rdi
callq 0x17248
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x61e0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rcx,%rax,8), %r14
movq (%rdi), %rax
callq *0x68(%rax)
notl %eax
jmp 0x148df
callq *%rcx
movslq 0xb4(%rbx), %rcx
leal -0x2(%rcx), %edx
cmpl %edx, %eax
jg 0x14a5e
leaq 0x18(%rbx), %r15
movq (%r15), %rax
movq -0x8(%rax,%rcx,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cltq
movq (%r15), %rcx
movq (%rcx,%rax,8), %r14
testq %r14, %r14
je 0x148ba
movq %r14, %rdi
callq 0x17248
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x61e0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rcx,%rax,8), %r14
movq (%rdi), %rax
callq *0x68(%rax)
cltq
movq (%r15), %rcx
movq %r14, (%rcx,%rax,8)
addq $-0x8, 0x20(%rbx)
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %r14
testq %r14, %r14
je 0x1490a
movq %r14, %rdi
callq 0x17248
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x61e0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
andq $0x0, -0x8(%rcx,%rax,8)
addq $-0x8, 0x20(%rbx)
addl $-0x2, %eax
movl %eax, 0xb4(%rbx)
leaq 0xc400(%rip), %rsi      # 0x20d3b
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x72a6
addq $0x78, %rbx
leaq -0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x1925e
leaq -0x50(%rbp), %rdi
callq 0x60d0
leaq -0x60(%rbp), %rdi
callq 0x17070
addq $0x48, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc19e(%rip), %rsi      # 0x20b25
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x190db(%rip), %rsi     # 0x2da88
leaq 0x3366(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
callq 0x6220
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc197(%rip), %rsi      # 0x20b6f
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1908a(%rip), %rsi     # 0x2da88
leaq 0x3315(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc2c8(%rip), %rsi      # 0x20cec
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1903e(%rip), %rsi     # 0x2da88
leaq 0x32c9(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc2a3(%rip), %rsi      # 0x20d13
leaq -0x50(%rbp), %rdi
leaq -0x30(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18ff2(%rip), %rsi     # 0x2da88
leaq 0x327d(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
callq 0x6220
callq 0x6220
callq 0x6220
callq 0x6220
callq 0x6220
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc1d8(%rip), %rsi      # 0x20cad
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18f8d(%rip), %rsi     # 0x2da88
leaq 0x3218(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc10c(%rip), %rsi      # 0x20c2d
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18f41(%rip), %rsi     # 0x2da88
leaq 0x31cc(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc0ff(%rip), %rsi      # 0x20c6c
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18ef5(%rip), %rsi     # 0x2da88
leaq 0x3180(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xc038(%rip), %rsi      # 0x20bf1
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18ea9(%rip), %rsi     # 0x2da88
leaq 0x3134(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x14c37
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xbfb0(%rip), %rsi      # 0x20bb2
leaq -0x50(%rbp), %rdi
leaq -0x19(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x18e60(%rip), %rsi     # 0x2da88
leaq 0x30eb(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14c66
jmp 0x14ce0
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14c73
jmp 0x14cf1
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14ce0
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14cf1
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14c93
jmp 0x14cff
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14cff
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14cb3
jmp 0x14d0d
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14d0d
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14cd3
jmp 0x14d1b
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14d1b
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x175d0
jmp 0x14d6f
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x17802
jmp 0x14d6f
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x17c2c
jmp 0x14d6f
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x17a3c
jmp 0x14d6f
movq %rax, %rbx
leaq -0x30(%rbp), %rdi
callq 0x17070
jmp 0x14d6f
jmp 0x14d6c
jmp 0x14d6c
jmp 0x14d6c
jmp 0x14d6c
jmp 0x14d6c
jmp 0x14d3c
jmp 0x14d3c
jmp 0x14d4f
jmp 0x14d4f
jmp 0x14d3c
jmp 0x14d4f
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x14d52
jmp 0x14d6f
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x14d6f
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
jmp 0x14d6f
jmp 0x14d6c
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x17070
movq %rbx, %rdi
callq 0x63f0