// File: /workspace/github_cpp_makefile_star2_O1/zerolfx[P]P-Machine/new.cpp
// Function: New::execute(StackMachine*)
void New::execute(StackMachine *stack)
{
	StackInteger p1;
	StackAddress p2;
	
	if(typeid(p1) != typeid(*(stack->fStore[stack->fSP])))
		throw ExecutionError("instruction new: STORE[SP] does not contain element of type integer");
		
	if(typeid(p2) != typeid(*stack->fStore[stack->fSP - 1]))
		throw ExecutionError("instruction new: STORE[SP-1] does not contain element of type address");
	
	// prepare the heap by adding empty pointers to the vector
	for(int i = 0; i < stack->fStore[stack->fSP]->getValue(); ++i)
	{
		stack->fHeap.push_back(new StackElement());
	}
	
	stack->fNP = stack->fNP - stack->fStore[stack->fSP]->getValue();
	
	if(stack->fStore[stack->fSP - 1]->heapAddress())
	{
		if(stack->fStore[stack->fSP - 1]->getValue() < stack->fNP)
		{
			throw ExecutionError("instruction new: STORE[SP-1] points to a non-existing heap address");
		}
	}
	else
	{
		if(stack->fStore[stack->fSP - 1]->getValue() > stack->fSP - 2)
		{
			throw ExecutionError("instruction new: STORE[SP-1] points to an address which is > SP");
		}
	}
	
	delete stack->fStore[stack->fStore[stack->fSP - 1]->getValue()];
	stack->fStore[stack->fStore[stack->fSP - 1]->getValue()] = new StackAddress(stack->fNP);
	delete stack->fStore[stack->fSP];
	stack->fStore[stack->fSP] = 0;
	delete stack->fStore[stack->fSP - 1];
	stack->fStore[stack->fSP - 1] = 0;
	stack->fStore.pop_back();
	stack->fStore.pop_back();
	stack->fSP -= 2;
	
	// adding cost of this instruction
	stack->fTime.count("new");

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rsi, %rbx
leaq -0x68(%rbp), %rdi
xorl %esi, %esi
callq 0x1aca0
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x1a3d0
movslq 0xb4(%rbx), %r14
movq 0x18(%rbx), %r15
movq (%r15,%r14,8), %rax
testq %rax, %rax
je 0x14f18
movq (%rax), %rax
movq -0x8(%rax), %rax
leaq 0x1eebc(%rip), %rcx     # 0x33af8
movq 0x8(%rcx), %rdi
movq 0x8(%rax), %rsi
cmpq %rsi, %rdi
je 0x14c5f
cmpb $0x2a, (%rdi)
je 0x14f1d
callq 0x6310
testl %eax, %eax
jne 0x14f1d
movq -0x8(%r15,%r14,8), %rax
testq %rax, %rax
je 0x14f18
movq (%rax), %rax
movq -0x8(%rax), %rax
leaq 0x1ece5(%rip), %rcx     # 0x33960
movq 0x8(%rcx), %rdi
movq 0x8(%rax), %rsi
cmpq %rsi, %rdi
je 0x14c9e
cmpb $0x2a, (%rdi)
je 0x14f77
callq 0x6310
testl %eax, %eax
jne 0x14f77
leaq 0x30(%rbx), %r14
xorl %r13d, %r13d
leaq -0x48(%rbp), %r15
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cmpl %eax, %r13d
jge 0x14cec
movl $0x8, %edi
callq 0x6200
movq %rax, %r12
movq %rax, %rdi
callq 0x1a5bc
movq %r12, -0x48(%rbp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa3ae
incl %r13d
jmp 0x14ca9
movl 0xb8(%rbx), %r14d
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
subl %eax, %r14d
movl %r14d, 0xb8(%rbx)
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 0x14daa
callq *%rcx
cmpl 0xb8(%rbx), %eax
jge 0x14dbd
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x107f4(%rip), %rsi     # 0x25561
leaq 0x1082f(%rip), %rdx     # 0x255a3
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r15b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b418
xorl %r15d, %r15d
leaq 0x1dcf2(%rip), %rsi     # 0x32a88
leaq 0x6699(%rip), %rdx      # 0x1b436
movq %r14, %rdi
callq 0x63e0
jmp 0x15023
callq *%rcx
movl 0xb4(%rbx), %ecx
addl $-0x2, %ecx
cmpl %ecx, %eax
jg 0x14fce
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cltq
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %r14
testq %r14, %r14
je 0x14df7
movq %r14, %rdi
callq 0x1a5cc
movl $0x8, %esi
movq %r14, %rdi
callq 0x6210
movl $0x10, %edi
callq 0x6200
movq %rax, %r14
movl 0xb8(%rbx), %esi
movq %rax, %rdi
callq 0x1a3d0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cltq
movq 0x18(%rbx), %rcx
movq %r14, (%rcx,%rax,8)
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %r14
testq %r14, %r14
je 0x14e5b
movq %r14, %rdi
callq 0x1a5cc
movl $0x8, %esi
movq %r14, %rdi
callq 0x6210
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq $0x0, (%rcx,%rax,8)
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %r14
testq %r14, %r14
je 0x14e91
movq %r14, %rdi
callq 0x1a5cc
movl $0x8, %esi
movq %r14, %rdi
callq 0x6210
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq $0x0, -0x8(%rcx,%rax,8)
addq $-0x10, 0x20(%rbx)
addl $-0x2, %eax
movl %eax, 0xb4(%rbx)
leaq -0x38(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x10722(%rip), %rsi     # 0x255e4
leaq 0x1071e(%rip), %rdx     # 0x255e7
leaq -0x48(%rbp), %rdi
callq 0x746c
addq $0x78, %rbx
leaq -0x48(%rbp), %rsi
movq %rbx, %rdi
callq 0x1ce78
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x14ef7
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x6210
leaq -0x58(%rbp), %rdi
callq 0x1a3f4
leaq -0x68(%rbp), %rdi
callq 0x1acc4
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6250
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x1059d(%rip), %rsi     # 0x254d7
leaq 0x105d9(%rip), %rdx     # 0x2551a
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r15b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b418
xorl %r15d, %r15d
leaq 0x1db25(%rip), %rsi     # 0x32a88
leaq 0x64cc(%rip), %rdx      # 0x1b436
movq %r14, %rdi
callq 0x63e0
jmp 0x15023
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x10587(%rip), %rsi     # 0x2551b
leaq 0x105c5(%rip), %rdx     # 0x25560
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r15b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b418
xorl %r15d, %r15d
leaq 0x1dacb(%rip), %rsi     # 0x32a88
leaq 0x6472(%rip), %rdx      # 0x1b436
movq %r14, %rdi
callq 0x63e0
jmp 0x15023
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x105b9(%rip), %rsi     # 0x255a4
leaq 0x105f1(%rip), %rdx     # 0x255e3
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r15b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b418
xorl %r15d, %r15d
leaq 0x1da74(%rip), %rsi     # 0x32a88
leaq 0x641b(%rip), %rdx      # 0x1b436
movq %r14, %rdi
callq 0x63e0
jmp 0x1502f
jmp 0x1502f
jmp 0x15049
jmp 0x15049
jmp 0x1502f
jmp 0x15049
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
je 0x1504f
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x6210
jmp 0x1504f
movq %rax, %rbx
movb $0x1, %r15b
testb %r15b, %r15b
je 0x150a0
movq %r14, %rdi
callq 0x6170
jmp 0x150a0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x150a0
movq -0x38(%rbp), %rsi
incq %rsi
jmp 0x15094
jmp 0x1509d
movq %rax, %rbx
movl $0x10, %esi
movq %r14, %rdi
jmp 0x15094
movq %rax, %rbx
jmp 0x150a9
jmp 0x1509d
movq %rax, %rbx
movl $0x8, %esi
movq %r12, %rdi
callq 0x6210
jmp 0x150a0
jmp 0x1509d
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x1a3f4
leaq -0x68(%rbp), %rdi
callq 0x1acc4
movq %rbx, %rdi
callq 0x6410