// File: 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
subq $0x1e0, %rsp            # imm = 0x1E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x20(%rbp), %rdi
xorl %esi, %esi
movl %esi, -0x110(%rbp)
callq 0x27340
movl -0x110(%rbp), %esi
leaq -0x30(%rbp), %rdi
callq 0x26800
jmp 0x1dfa5
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x118(%rbp)
cmpq $0x0, %rax
jne 0x1dfd4
callq 0x7300
jmp 0x1dfd4
movq -0x118(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2cb07(%rip), %rdi     # 0x4aaf0
callq 0x9370
testb $0x1, %al
jne 0x1dff7
jmp 0x1e0d1
movb $0x1, -0x62(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x128(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x74f0
movq -0x120(%rbp), %rdx
leaq 0x1b4ad(%rip), %rsi     # 0x394d7
leaq -0x60(%rbp), %rdi
callq 0x8940
jmp 0x1e035
movq -0x128(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x283d0
jmp 0x1e047
movq -0x128(%rbp), %rdi
movb $0x0, -0x62(%rbp)
leaq 0x2ba27(%rip), %rsi     # 0x49a80
leaq 0xa3a0(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1e885
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e873
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e86a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e0af
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x7170
leaq -0x61(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x62(%rbp)
jne 0x1e0c0
jmp 0x1e0cc
movq -0x128(%rbp), %rdi
callq 0x71c0
jmp 0x1e86a
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, -0x130(%rbp)
cmpq $0x0, %rax
jne 0x1e105
callq 0x7300
jmp 0x1e105
movq -0x130(%rbp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rsi
leaq 0x2c83e(%rip), %rdi     # 0x4a958
callq 0x9370
testb $0x1, %al
jne 0x1e128
jmp 0x1e1f8
movb $0x1, -0x8a(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x140(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x138(%rbp)
callq 0x74f0
movq -0x138(%rbp), %rdx
leaq 0x1b3ba(%rip), %rsi     # 0x3951b
leaq -0x88(%rbp), %rdi
callq 0x8940
jmp 0x1e16f
movq -0x140(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x283d0
jmp 0x1e184
movq -0x140(%rbp), %rdi
movb $0x0, -0x8a(%rbp)
leaq 0x2b8e7(%rip), %rsi     # 0x49a80
leaq 0xa260(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1e885
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e1d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x88(%rbp), %rdi
callq 0x7170
leaq -0x89(%rbp), %rdi
callq 0x72e0
testb $0x1, -0x8a(%rbp)
jne 0x1e1e7
jmp 0x1e1f3
movq -0x140(%rbp), %rdi
callq 0x71c0
jmp 0x1e86a
movl $0x0, -0x90(%rbp)
movl -0x90(%rbp), %eax
movl %eax, -0x148(%rbp)
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x144(%rbp)
jmp 0x1e239
movl -0x148(%rbp), %eax
movl -0x144(%rbp), %ecx
cmpl %ecx, %eax
jge 0x1e2d8
movq -0x10(%rbp), %rax
addq $0x30, %rax
movq %rax, -0x158(%rbp)
movl $0x8, %edi
callq 0x7270
movq %rax, -0x150(%rbp)
jmp 0x1e26f
movq -0x150(%rbp), %rdi
callq 0x26c80
jmp 0x1e27d
movq -0x158(%rbp), %rdi
movq -0x150(%rbp), %rax
movq %rax, -0x98(%rbp)
leaq -0x98(%rbp), %rsi
callq 0xd2a0
jmp 0x1e2a0
jmp 0x1e2a2
movl -0x90(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x90(%rbp)
jmp 0x1e202
movq -0x150(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x8, %esi
callq 0x7280
jmp 0x1e86a
movq -0x10(%rbp), %rax
movl 0xb8(%rax), %ecx
movl %ecx, -0x160(%rbp)
movq %rax, %rdi
addq $0x18, %rdi
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x15c(%rbp)
jmp 0x1e30f
movl -0x15c(%rbp), %eax
movl -0x160(%rbp), %ecx
subl %eax, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xb8(%rax)
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x88(%rax), %rax
callq *%rax
movb %al, -0x161(%rbp)
jmp 0x1e359
movb -0x161(%rbp), %al
testb $0x1, %al
jne 0x1e368
jmp 0x1e482
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x168(%rbp)
jmp 0x1e397
movl -0x168(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0xb8(%rcx), %eax
jge 0x1e47d
movb $0x1, -0xba(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x178(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x74f0
movq -0x170(%rbp), %rdx
leaq 0x1b17b(%rip), %rsi     # 0x39561
leaq -0xb8(%rbp), %rdi
callq 0x8940
jmp 0x1e3f4
movq -0x178(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
callq 0x283d0
jmp 0x1e409
movq -0x178(%rbp), %rdi
movb $0x0, -0xba(%rbp)
leaq 0x2b662(%rip), %rsi     # 0x49a80
leaq 0x9fdb(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1e885
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e455
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x7170
leaq -0xb9(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xba(%rbp)
jne 0x1e46c
jmp 0x1e478
movq -0x178(%rbp), %rdi
callq 0x71c0
jmp 0x1e86a
jmp 0x1e59e
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x17c(%rbp)
jmp 0x1e4b1
movl -0x17c(%rbp), %eax
movq -0x10(%rbp), %rcx
movl 0xb4(%rcx), %ecx
subl $0x2, %ecx
cmpl %ecx, %eax
jle 0x1e59c
movb $0x1, -0xe2(%rbp)
movl $0x20, %edi
callq 0x7110
movq %rax, -0x190(%rbp)
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x74f0
movq -0x188(%rbp), %rdx
leaq 0x1b09f(%rip), %rsi     # 0x395a4
leaq -0xe0(%rbp), %rdi
callq 0x8940
jmp 0x1e513
movq -0x190(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x283d0
jmp 0x1e528
movq -0x190(%rbp), %rdi
movb $0x0, -0xe2(%rbp)
leaq 0x2b543(%rip), %rsi     # 0x49a80
leaq 0x9ebc(%rip), %rdx      # 0x28400
callq 0x74a0
jmp 0x1e885
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e574
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x7170
leaq -0xe1(%rbp), %rdi
callq 0x72e0
testb $0x1, -0xe2(%rbp)
jne 0x1e58b
jmp 0x1e597
movq -0x190(%rbp), %rdi
callq 0x71c0
jmp 0x1e86a
jmp 0x1e59e
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movq %rdi, -0x1a0(%rbp)
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x194(%rbp)
jmp 0x1e5d4
movq -0x1a0(%rbp), %rdi
movl -0x194(%rbp), %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1a8(%rbp)
cmpq $0x0, %rax
je 0x1e616
movq -0x1a8(%rbp), %rdi
callq 0x26ca0
movq -0x1a8(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movl $0x10, %edi
callq 0x7270
movq %rax, -0x1b0(%rbp)
jmp 0x1e629
movq -0x1b0(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0xb8(%rax), %esi
callq 0x26800
jmp 0x1e641
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movq %rdi, -0x1c0(%rbp)
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq (%rax), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
callq *%rax
movl %eax, -0x1b4(%rbp)
jmp 0x1e677
movq -0x1c0(%rbp), %rdi
movl -0x1b4(%rbp), %eax
movslq %eax, %rsi
callq 0x8e70
movq -0x1b0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq (%rax), %rax
movq %rax, -0x1c8(%rbp)
cmpq $0x0, %rax
je 0x1e6db
movq -0x1c8(%rbp), %rdi
callq 0x26ca0
movq -0x1c8(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
movq -0x10(%rbp), %rax
movslq 0xb4(%rax), %rsi
callq 0x8e70
movq $0x0, (%rax)
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, -0x1d0(%rbp)
cmpq $0x0, %rax
je 0x1e744
movq -0x1d0(%rbp), %rdi
callq 0x26ca0
movq -0x1d0(%rbp), %rdi
movl $0x8, %esi
callq 0x7280
movq -0x10(%rbp), %rax
movq %rax, %rdi
addq $0x18, %rdi
movl 0xb4(%rax), %eax
decl %eax
movslq %eax, %rsi
callq 0x8e70
movq $0x0, (%rax)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x8e90
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
callq 0x8e90
movq -0x10(%rbp), %rax
movl 0xb4(%rax), %ecx
addl $-0x2, %ecx
movl %ecx, 0xb4(%rax)
movq -0x10(%rbp), %rax
addq $0x78, %rax
movq %rax, -0x1e0(%rbp)
leaq -0x109(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x74f0
movq -0x1d8(%rbp), %rdx
leaq 0x1ae21(%rip), %rsi     # 0x395e4
leaq -0x108(%rbp), %rdi
callq 0x8940
jmp 0x1e7d1
movq -0x1e0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x2c4d0
jmp 0x1e7e6
leaq -0x108(%rbp), %rdi
callq 0x7170
leaq -0x109(%rbp), %rdi
callq 0x72e0
leaq -0x30(%rbp), %rdi
callq 0x26840
leaq -0x20(%rbp), %rdi
callq 0x27380
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0x1b0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
movl $0x10, %esi
callq 0x7280
jmp 0x1e86a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1e85e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x7170
leaq -0x109(%rbp), %rdi
callq 0x72e0
leaq -0x30(%rbp), %rdi
callq 0x26840
leaq -0x20(%rbp), %rdi
callq 0x27380
movq -0x38(%rbp), %rdi
callq 0x74e0
nopw %cs:(%rax,%rax)