// File: /workspace/github_cpp_makefile_star2_O2/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 -0x70(%rbp), %rdi
xorl %esi, %esi
callq 0x175ac
leaq -0x60(%rbp), %rdi
xorl %esi, %esi
callq 0x1704c
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rax
testq %rax, %rax
je 0x12984
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1c3b6(%rip), %rdi     # 0x2eaf8
callq 0x76ba
testb %al, %al
jne 0x12989
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %rax
testq %rax, %rax
je 0x12984
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x1c1ea(%rip), %rdi     # 0x2e960
callq 0x76ba
testb %al, %al
jne 0x129d5
leaq 0x30(%rbx), %r14
xorl %r15d, %r15d
leaq -0x50(%rbp), %r12
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq (%rdi), %rax
callq *0x68(%rax)
cmpl %eax, %r15d
jge 0x127cf
pushq $0x8
popq %rdi
callq 0x61d0
movq %rax, %r13
movq %rax, %rdi
callq 0x17238
movq %r13, -0x50(%rbp)
movq %r14, %rdi
movq %r12, %rsi
callq 0x99a0
incl %r15d
jmp 0x1278e
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 0x1283f
callq *%rcx
cmpl 0xb8(%rbx), %eax
jl 0x12a21
movl 0xb4(%rbx), %ecx
jmp 0x12852
callq *%rcx
movl 0xb4(%rbx), %ecx
leal -0x2(%rcx), %edx
cmpl %edx, %eax
jg 0x12a6a
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 0x1287b
movq %r14, %rdi
callq 0x17248
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x61e0
pushq $0x10
popq %rdi
callq 0x61d0
movq %rax, %r14
movl 0xb8(%rbx), %esi
movq %rax, %rdi
callq 0x1704c
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 0x128db
movq %r14, %rdi
callq 0x17248
pushq $0x8
popq %rsi
movq %r14, %rdi
callq 0x61e0
movslq 0xb4(%rbx), %rax
movq 0x18(%rbx), %rcx
andq $0x0, (%rcx,%rax,8)
movq 0x18(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %r14
testq %r14, %r14
je 0x1290c
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 $-0x10, 0x20(%rbx)
addl $-0x2, %eax
movl %eax, 0xb4(%rbx)
leaq 0xdca7(%rip), %rsi      # 0x205e4
leaq -0x50(%rbp), %rdi
leaq -0x29(%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
leaq -0x70(%rbp), %rdi
callq 0x175d0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x6220
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xdb3c(%rip), %rsi      # 0x204d7
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1b0c7(%rip), %rsi     # 0x2da88
leaq 0x5352(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x12ab1
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xdb34(%rip), %rsi      # 0x2051b
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1b07b(%rip), %rsi     # 0x2da88
leaq 0x5306(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x12ab1
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xdb2e(%rip), %rsi      # 0x20561
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1b02f(%rip), %rsi     # 0x2da88
leaq 0x52ba(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x12ab1
pushq $0x20
popq %rdi
callq 0x60f0
movq %rax, %r14
leaq 0xdb28(%rip), %rsi      # 0x205a4
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x72a6
movb $0x1, %r15b
leaq -0x50(%rbp), %rsi
movq %r14, %rdi
callq 0x17d10
xorl %r15d, %r15d
leaq 0x1afe6(%rip), %rsi     # 0x2da88
leaq 0x5271(%rip), %rdx      # 0x17d1a
movq %r14, %rdi
callq 0x63c0
jmp 0x12abd
jmp 0x12abd
jmp 0x12ad0
jmp 0x12ad0
jmp 0x12abd
jmp 0x12ad0
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
testb %r15b, %r15b
jne 0x12ad3
jmp 0x12b14
movq %rax, %rbx
movq %r14, %rdi
callq 0x6150
jmp 0x12b14
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x60d0
jmp 0x12b14
jmp 0x12b11
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r14, %rdi
jmp 0x12b08
movq %rax, %rbx
jmp 0x12b1d
jmp 0x12b11
movq %rax, %rbx
pushq $0x8
popq %rsi
movq %r13, %rdi
callq 0x61e0
jmp 0x12b14
jmp 0x12b11
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x17070
leaq -0x70(%rbp), %rdi
callq 0x175d0
movq %rbx, %rdi
callq 0x63f0