// File: zerolfx[P]P-Machine/div.cpp
// Function: Div::execute(StackMachine*)
void Div::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 div: SP does not point to element of type integer.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction div: 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 div: SP does not point to element of type real.");
			if(typeid(p1) != typeid(*(stack->fStore[stack->fSP - 1])))
				throw ExecutionError("instruction div: SP - 1 does not point to element of type real.");
			break;
		}
		default:
			cerr << "operation not supported for this type" << endl;
	}
	
	// first check for a possible division by zero
	if(stack->fStore[stack->fSP]->isZero())
		throw ExecutionError("instruction div: illegal action: divide by zero.");

	// actual division
	stack->fStore[stack->fSP - 1]->div(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("div");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x160(%rbp)
movq -0x10(%rbp), %rax
cmpl $0x1, 0xb4(%rax)
jge 0xbde7
movb $0x1, -0x45(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x170(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x168(%rbp)
callq 0x74f0
movq -0x168(%rbp), %rdx
leaq 0x2b457(%rip), %rsi     # 0x371b9
leaq -0x30(%rbp), %rdi
callq 0x8940
jmp 0xbd6d
movq -0x170(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x283d0
jmp 0xbd7f
movq -0x170(%rbp), %rdi
movb $0x0, -0x45(%rbp)
leaq 0x3dcef(%rip), %rsi     # 0x49a80
leaq 0x1c668(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xbdc5
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 0xbdd6
jmp 0xbde2
movq -0x170(%rbp), %rdi
callq 0x71c0
jmp 0xc593
movq -0x160(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0x174(%rbp)
testl %eax, %eax
je 0xc078
jmp 0xbe01
movl -0x174(%rbp), %eax
subl $0x1, %eax
jne 0xc2fd
jmp 0xbe12
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, -0x180(%rbp)
cmpq $0x0, %rax
jne 0xbe4c
callq 0x7300
jmp 0xbe4c
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x3ec8f(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0xbe6f
jmp 0xbf38
movb $0x1, -0x7a(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x190(%rbp)
leaq -0x79(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x74f0
movq -0x188(%rbp), %rdx
leaq 0x2b7b6(%rip), %rsi     # 0x37658
leaq -0x78(%rbp), %rdi
callq 0x8940
jmp 0xbead
movq -0x190(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x283d0
jmp 0xbebf
movq -0x190(%rbp), %rdi
movb $0x0, -0x7a(%rbp)
leaq 0x3dbaf(%rip), %rsi     # 0x49a80
leaq 0x1c528(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc06a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xbf16
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 0xbf27
jmp 0xbf33
movq -0x190(%rbp), %rdi
callq 0x71c0
jmp 0xc06a
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, -0x198(%rbp)
cmpq $0x0, %rax
jne 0xbf6c
callq 0x7300
jmp 0xbf6c
movq -0x198(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x3eb6f(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0xbf8f
jmp 0xc05c
movb $0x1, -0xa2(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1a8(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x74f0
movq -0x1a0(%rbp), %rdx
leaq 0x2b6cf(%rip), %rsi     # 0x37697
leaq -0xa0(%rbp), %rdi
callq 0x8940
jmp 0xbfd6
movq -0x1a8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x283d0
jmp 0xbfeb
movq -0x1a8(%rbp), %rdi
movb $0x0, -0xa2(%rbp)
leaq 0x3da80(%rip), %rsi     # 0x49a80
leaq 0x1c3f9(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc037
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 0xc04e
jmp 0xc05a
movq -0x1a8(%rbp), %rdi
callq 0x71c0
jmp 0xc06a
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0xc31f
leaq -0x58(%rbp), %rdi
callq 0x27380
jmp 0xc593
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, -0x1b0(%rbp)
cmpq $0x0, %rax
jne 0xc0b6
callq 0x7300
jmp 0xc0b6
movq -0x1b0(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x3eaf5(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0xc0d9
jmp 0xc1ba
movb $0x1, -0xda(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1c0(%rbp)
leaq -0xd9(%rbp), %rdi
movq %rdi, -0x1b8(%rbp)
callq 0x74f0
movq -0x1b8(%rbp), %rdx
leaq 0x2b5c8(%rip), %rsi     # 0x376da
leaq -0xd8(%rbp), %rdi
callq 0x8940
jmp 0xc120
movq -0x1c0(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x283d0
jmp 0xc135
movq -0x1c0(%rbp), %rdi
movb $0x0, -0xda(%rbp)
leaq 0x3d936(%rip), %rsi     # 0x49a80
leaq 0x1c2af(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc2ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc192
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 0xc1a9
jmp 0xc1b5
movq -0x1c0(%rbp), %rdi
callq 0x71c0
jmp 0xc2ec
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, -0x1c8(%rbp)
cmpq $0x0, %rax
jne 0xc1ee
callq 0x7300
jmp 0xc1ee
movq -0x1c8(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x3e9bd(%rip), %rdi     # 0x4abc0
callq 0x9370
testb $0x1, %al
jne 0xc211
jmp 0xc2de
movb $0x1, -0x102(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1d8(%rbp)
leaq -0x101(%rbp), %rdi
movq %rdi, -0x1d0(%rbp)
callq 0x74f0
movq -0x1d0(%rbp), %rdx
leaq 0x2b4cc(%rip), %rsi     # 0x37716
leaq -0x100(%rbp), %rdi
callq 0x8940
jmp 0xc258
movq -0x1d8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0x283d0
jmp 0xc26d
movq -0x1d8(%rbp), %rdi
movb $0x0, -0x102(%rbp)
leaq 0x3d7fe(%rip), %rsi     # 0x49a80
leaq 0x1c177(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc2b9
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 0xc2d0
jmp 0xc2dc
movq -0x1d8(%rbp), %rdi
callq 0x71c0
jmp 0xc2ec
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0xc31f
leaq -0xb8(%rbp), %rdi
callq 0x27880
jmp 0xc593
movq 0x3ecec(%rip), %rdi     # 0x4aff0
leaq 0x2afe5(%rip), %rsi     # 0x372f0
callq 0x7250
movq %rax, %rdi
movq 0x3ec8e(%rip), %rsi     # 0x4afa8
callq 0x72b0
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
callq *0x90(%rax)
testb $0x1, %al
jne 0xc34c
jmp 0xc41c
movb $0x1, -0x12a(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x1e8(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x74f0
movq -0x1e0(%rbp), %rdx
leaq 0x2b3d1(%rip), %rsi     # 0x37756
leaq -0x128(%rbp), %rdi
callq 0x8940
jmp 0xc393
movq -0x1e8(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x283d0
jmp 0xc3a8
movq -0x1e8(%rbp), %rdi
movb $0x0, -0x12a(%rbp)
leaq 0x3d6c3(%rip), %rsi     # 0x49a80
leaq 0x1c03c(%rip), %rdx     # 0x28400
callq 0x74a0
jmp 0xc59c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc3f4
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
testb $0x1, -0x12a(%rbp)
jne 0xc40b
jmp 0xc417
movq -0x1e8(%rbp), %rdi
callq 0x71c0
jmp 0xc593
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, -0x1f8(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq -0x1f8(%rbp), %rdi
movq (%rax), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1f0(%rbp)
cmpq $0x0, %rax
je 0xc4b0
movq -0x1f0(%rbp), %rdi
callq 0x26ca0
movq -0x1f0(%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, -0x208(%rbp)
leaq -0x151(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x74f0
movq -0x200(%rbp), %rdx
leaq 0x2b26a(%rip), %rsi     # 0x37787
leaq -0x150(%rbp), %rdi
callq 0x8940
jmp 0xc52b
movq -0x208(%rbp), %rdi
leaq -0x150(%rbp), %rsi
callq 0x2c4d0
jmp 0xc540
leaq -0x150(%rbp), %rdi
callq 0x7170
leaq -0x151(%rbp), %rdi
callq 0x72e0
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0xc587
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x7170
leaq -0x151(%rbp), %rdi
callq 0x72e0
movq -0x40(%rbp), %rdi
callq 0x74e0
nopl (%rax)