// File: /workspace/github_cpp_makefile_star2_O3/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 0x1a9d4
leaq -0x58(%rbp), %rdi
xorl %esi, %esi
callq 0x1a104
movslq 0xb4(%rbx), %r15
movq 0x18(%rbx), %r12
movq (%r12,%r15,8), %r14
testq %r14, %r14
je 0x14cd7
movq (%r14), %r13
movq -0x8(%r13), %rax
leaq 0x1f0ac(%rip), %rcx     # 0x33af8
movq 0x8(%rcx), %rdi
movq 0x8(%rax), %rsi
cmpq %rsi, %rdi
je 0x14a6f
cmpb $0x2a, (%rdi)
je 0x14cdc
callq 0x6310
testl %eax, %eax
jne 0x14cdc
movq -0x8(%r12,%r15,8), %rax
testq %rax, %rax
je 0x14cd7
movq (%rax), %rax
movq -0x8(%rax), %rax
leaq 0x1eed5(%rip), %rcx     # 0x33960
movq 0x8(%rcx), %rdi
movq 0x8(%rax), %rsi
cmpq %rsi, %rdi
je 0x14aae
cmpb $0x2a, (%rdi)
je 0x14d35
callq 0x6310
testl %eax, %eax
jne 0x14d35
leaq 0x30(%rbx), %r15
xorl %r12d, %r12d
movq %r14, %rdi
callq *0x68(%r13)
cmpl %eax, %r12d
jge 0x14afd
movl $0x8, %edi
callq 0x6200
movq %rax, %r14
movq %rax, %rdi
callq 0x1a2f0
movq %r14, -0x48(%rbp)
movq %r15, %rdi
leaq -0x48(%rbp), %rsi
callq 0xa334
incl %r12d
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %r14
movq (%r14), %r13
jmp 0x14ab5
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 0x14b6d
callq *%rcx
cmpl 0xb8(%rbx), %eax
jl 0x14d8e
movl 0xb4(%rbx), %ecx
jmp 0x14b80
callq *%rcx
movl 0xb4(%rbx), %ecx
leal -0x2(%rcx), %edx
cmpl %edx, %eax
jg 0x14de4
movq 0x18(%rbx), %rax
movslq %ecx, %rcx
movq -0x8(%rax,%rcx,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cltq
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %r14
testq %r14, %r14
je 0x14ba9
movq %r14, %rdi
callq 0x1a300
movl $0x8, %esi
movq %r14, %rdi
callq 0x6210
movl $0x10, %edi
callq 0x6200
movq %rax, %r14
movl 0xb8(%rbx), %esi
movq %rax, %rdi
callq 0x1a104
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 0x14c0d
movq %r14, %rdi
callq 0x1a300
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 0x14c43
movq %r14, %rdi
callq 0x1a300
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 0x10963(%rip), %rsi     # 0x255e4
leaq 0x1095f(%rip), %rdx     # 0x255e7
leaq -0x48(%rbp), %rdi
callq 0x746c
addq $0x78, %rbx
leaq -0x48(%rbp), %rsi
movq %rbx, %rdi
callq 0x1cbc8
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x14cb6
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x6210
leaq -0x58(%rbp), %rdi
callq 0x1a128
leaq -0x68(%rbp), %rdi
callq 0x1a9f8
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), %r15
movq %r15, -0x10(%r15)
leaq 0x107df(%rip), %rsi     # 0x254d7
leaq 0x1081b(%rip), %rdx     # 0x2551a
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r12b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b14c
xorl %r12d, %r12d
leaq 0x1dd67(%rip), %rsi     # 0x32a88
leaq 0x6442(%rip), %rdx      # 0x1b16a
movq %r14, %rdi
callq 0x63e0
jmp 0x14e38
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x107ca(%rip), %rsi     # 0x2551b
leaq 0x10808(%rip), %rdx     # 0x25560
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r12b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b14c
xorl %r12d, %r12d
leaq 0x1dd0e(%rip), %rsi     # 0x32a88
leaq 0x63e9(%rip), %rdx      # 0x1b16a
movq %r14, %rdi
callq 0x63e0
jmp 0x14e38
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x107b7(%rip), %rsi     # 0x25561
leaq 0x107f2(%rip), %rdx     # 0x255a3
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r12b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b14c
xorl %r12d, %r12d
leaq 0x1dcb5(%rip), %rsi     # 0x32a88
leaq 0x6390(%rip), %rdx      # 0x1b16a
movq %r14, %rdi
callq 0x63e0
jmp 0x14e38
movl $0x20, %edi
callq 0x6100
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x107a4(%rip), %rsi     # 0x255a4
leaq 0x107dc(%rip), %rdx     # 0x255e3
leaq -0x48(%rbp), %rdi
callq 0x746c
movb $0x1, %r12b
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x1b14c
xorl %r12d, %r12d
leaq 0x1dc5f(%rip), %rsi     # 0x32a88
leaq 0x633a(%rip), %rdx      # 0x1b16a
movq %r14, %rdi
callq 0x63e0
jmp 0x14e44
jmp 0x14e44
jmp 0x14e63
jmp 0x14e63
jmp 0x14e44
jmp 0x14e63
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0x14e5c
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x6210
testb %r12b, %r12b
jne 0x14e66
jmp 0x14eaf
movq %rax, %rbx
movq %r14, %rdi
callq 0x6170
jmp 0x14eaf
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r14, %rdi
je 0x14eaf
movq -0x38(%rbp), %rsi
incq %rsi
jmp 0x14ea3
jmp 0x14eac
movq %rax, %rbx
movl $0x10, %esi
jmp 0x14ea0
movq %rax, %rbx
jmp 0x14eb8
jmp 0x14eac
movq %rax, %rbx
movl $0x8, %esi
movq %r14, %rdi
callq 0x6210
jmp 0x14eaf
jmp 0x14eac
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x1a128
leaq -0x68(%rbp), %rdi
callq 0x1a9f8
movq %rbx, %rdi
callq 0x6410
nop