// File: zerolfx[P]P-Machine/add.cpp
// Function: Add::execute(StackMachine*)
void Add::execute(StackMachine *stack) 
{
	// check if the stack contains at least two entries
	if(stack->fSP < 1)
		throw ExecutionError("instruction add: requires 2 stackelements to be present.");
		
	switch(fType)
	{
		case integer:
		{
			StackInteger p1;

			// check if the two uppermost stackentries are of type integer
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
				throw ExecutionError("instruction add: SP does not point to element of type integer.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction add: SP - 1 does not point to element of type integer.");

			break;
		}	
		case real:
		{
			StackReal p1;
			// check if the two uppermost stackentries are of type real
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
				throw ExecutionError("instruction add: SP does not point to element of type real.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction add: SP - 1 does not point to element of type real.");
			break;
		}
		default:
			cerr << "operation not supported for this type" << endl;
	}
	
	// actual addition
	stack->fStore[stack->fSP - 1]->add(stack->fStore[stack->fSP]);
	
	// SP := SP - 1
	delete stack->fStore[stack->fSP];
	stack->fStore[stack->fSP] = 0;
	stack->fStore.pop_back();
	--stack->fSP;
	
	// adding cost of this instruction
	stack->fTime.count("add");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x138(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0xb4(%rax)
jge 0x9b07
movb $0x1, -0x45(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x148(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x140(%rbp)
callq 0x74f0
movq -0x140(%rbp), %rdx
leaq 0x2d737(%rip), %rsi     # 0x371b9
leaq -0x30(%rbp), %rdi
callq 0x8940
jmp 0x9a8d
movq -0x148(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x283d0
jmp 0x9a9f
movq -0x148(%rbp), %rdi
movb $0x0, -0x45(%rbp)
leaq 0x3ffcf(%rip), %rsi     # 0x49a80
leaq 0x1e948(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xa1be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9ae5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x7170
leaq -0x31(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x45(%rbp)
jne 0x9af6
jmp 0x9b02
movq -0x148(%rbp), %rdi
callq 0x71c0
jmp 0xa1b5
movq -0x138(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x14c(%rbp)
testl %eax, %eax
je 0x9d98
jmp 0x9b21
movl -0x14c(%rbp), %eax
subl $0x1, %eax
jne 0xa01d
jmp 0x9b32
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x27340
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x158(%rbp)
cmpq $0x0, %rax
jne 0x9b6c
callq 0x7300
jmp 0x9b6c
movq -0x158(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x40f6f(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0x9b8f
jmp 0x9c58
movb $0x1, -0x7a(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x168(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x74f0
movq -0x160(%rbp), %rdx
leaq 0x2d630(%rip), %rsi     # 0x371f2
leaq -0x78(%rbp), %rdi
callq 0x8940
jmp 0x9bcd
movq -0x168(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x283d0
jmp 0x9bdf
movq -0x168(%rbp), %rdi
movb $0x0, -0x7a(%rbp)
leaq 0x3fe8f(%rip), %rsi     # 0x49a80
leaq 0x1e808(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xa1be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9d8a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9c36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x7170
leaq -0x79(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x7a(%rbp)
jne 0x9c47
jmp 0x9c53
movq -0x168(%rbp), %rdi
callq 0x71c0
jmp 0x9d8a
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x170(%rbp)
cmpq $0x0, %rax
jne 0x9c8c
callq 0x7300
jmp 0x9c8c
movq -0x170(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x40e4f(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0x9caf
jmp 0x9d7c
movb $0x1, -0xa2(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x180(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x178(%rbp)
callq 0x74f0
movq -0x178(%rbp), %rdx
leaq 0x2d549(%rip), %rsi     # 0x37231
leaq -0xa0(%rbp), %rdi
callq 0x8940
jmp 0x9cf6
movq -0x180(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x283d0
jmp 0x9d0b
movq -0x180(%rbp), %rdi
movb $0x0, -0xa2(%rbp)
leaq 0x3fd60(%rip), %rsi     # 0x49a80
leaq 0x1e6d9(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xa1be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9d57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x7170
leaq -0xa1(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xa2(%rbp)
jne 0x9d6e
jmp 0x9d7a
movq -0x180(%rbp), %rdi
callq 0x71c0
jmp 0x9d8a
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0xa03f
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0xa1b5
leaq -0xb8(%rbp), %rdi
xorps %xmm0, %xmm0
callq 0x27840
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x188(%rbp)
cmpq $0x0, %rax
jne 0x9dd6
callq 0x7300
jmp 0x9dd6
movq -0x188(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x40dd5(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0x9df9
jmp 0x9eda
movb $0x1, -0xda(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x198(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x74f0
movq -0x190(%rbp), %rdx
leaq 0x2d442(%rip), %rsi     # 0x37274
leaq -0xd8(%rbp), %rdi
callq 0x8940
jmp 0x9e40
movq -0x198(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x283d0
jmp 0x9e55
movq -0x198(%rbp), %rdi
movb $0x0, -0xda(%rbp)
leaq 0x3fc16(%rip), %rsi     # 0x49a80
leaq 0x1e58f(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xa1be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xa00c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9eb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x7170
leaq -0xd9(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xda(%rbp)
jne 0x9ec9
jmp 0x9ed5
movq -0x198(%rbp), %rdi
callq 0x71c0
jmp 0xa00c
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1a0(%rbp)
cmpq $0x0, %rax
jne 0x9f0e
callq 0x7300
jmp 0x9f0e
movq -0x1a0(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x40c9d(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0x9f31
jmp 0x9ffe
movb $0x1, -0x102(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1b0(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x74f0
movq -0x1a8(%rbp), %rdx
leaq 0x2d346(%rip), %rsi     # 0x372b0
leaq -0x100(%rbp), %rdi
callq 0x8940
jmp 0x9f78
movq -0x1b0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x283d0
jmp 0x9f8d
movq -0x1b0(%rbp), %rdi
movb $0x0, -0x102(%rbp)
leaq 0x3fade(%rip), %rsi     # 0x49a80
leaq 0x1e457(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xa1be
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x9fd9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x100(%rbp), %rdi
callq 0x7170
leaq -0x101(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x102(%rbp)
jne 0x9ff0
jmp 0x9ffc
movq -0x1b0(%rbp), %rdi
callq 0x71c0
jmp 0xa00c
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0xa03f
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0xa1b5
movq 0x40fcc(%rip), %rdi     # 0x4aff0
leaq 0x2d2c5(%rip), %rsi     # 0x372f0
callq 0x7250
movq %rax, %rdi
movq 0x40f6e(%rip), %rsi     # 0x4afa8
callq 0x72b0
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1c0(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq -0x1c0(%rbp), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
callq *(%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1b8(%rbp)
cmpq $0x0, %rax
je 0xa0d2
movq -0x1b8(%rbp), %rdi
callq 0x26ca0
movq -0x1b8(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq $0x0, (%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x8e90
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %ecx
decl %ecx
movl %ecx, 0xb4(%rax)
movq -0x10(%rbp), %rax
addq $0x78, %rax
movq %rax, -0x1d0(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x1c8(%rbp)
callq 0x74f0
movq -0x1c8(%rbp), %rdx
leaq 0x2d1d7(%rip), %rsi     # 0x37316
leaq -0x128(%rbp), %rdi
callq 0x8940
jmp 0xa14d
movq -0x1d0(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x2c4d0
jmp 0xa162
leaq -0x128(%rbp), %rdi
callq 0x7170
leaq -0x129(%rbp), %rdi
callq 0x72e0
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xa1a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x7170
leaq -0x129(%rbp), %rdi
callq 0x72e0
movq -0x40(%rbp), %rdi
callq 0x74e0
nop