// File: gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::allocateInNVM(unsigned long, Access)
allocDecision
DBAMBPredictor::allocateInNVM(uint64_t set, Access element)
{
	
	if(element.isInstFetch())
		return ALLOCATE_IN_NVM;
		
	uint64_t signature = hashingSignature(element);
	
	DHPEntry* rap_current = lookup(signature);
	if(rap_current  == NULL) // Miss in the DHP table, create the entry
	{
		if(!m_isWarmup)
			stats_RAP_miss++;	
		
		int index = indexFunction(signature);
		int assoc = m_rap_policy->evictPolicy(index);
		
		rap_current = m_DHPTable[index][assoc];
		
		rap_current->initEntry(element);
		rap_current->id = id_DATASET++;
		rap_current->signature = signature;
		dataset_file << "ID Dataset:" << rap_current->id << "\t" << signature << endl;
	}
	else
	{	
		if(!m_isWarmup)
			stats_RAP_hits++;
		rap_current->nbAccess++;
	}	
	
	m_rap_policy->updatePolicy(rap_current->index , rap_current->assoc);
	
	if(rap_current->des == BYPASS_CACHE)
	{
		rap_current->cptBypassLearning++;
		if(simu_parameters.printDebug)
		{
			dataset_file << "Dataset n°" << rap_current->id << ": BYPASS, Address: " << std::hex << \
			element.m_address << std::dec << "; Threshold=" << rap_current->cptBypassLearning << endl;		
		}
			
		if(rap_current->cptBypassLearning ==  simu_parameters.learningTH){
			rap_current->cptBypassLearning = 0;

			if(simu_parameters.printDebug)
			{
				dataset_file << "Dataset n°" << rap_current->id << ": Insert Learning Cl, " \
				<< std::hex << element.m_address << std::dec  << endl;			
			}
			return ALLOCATE_IN_NVM;		
		}
	}
	
	if(rap_current->des == ALLOCATE_PREEMPTIVELY)
	{
		if(element.isWrite())
			return ALLOCATE_IN_SRAM;		
		else
			return ALLOCATE_IN_NVM;
	}
	else
	{

		if(simu_parameters.enableDatasetSpilling)
		{
			if(rap_current->des == ALLOCATE_IN_NVM)
			{
				if( (m_isNVMbusy[set] && !m_isSRAMbusy[set]))
				{
					reportSpilling(rap_current , element.m_address , true , false);
					stats_busyness_alloc_change++;
					return ALLOCATE_IN_SRAM;
				}
				else
					return ALLOCATE_IN_NVM;			
			}
			else if(rap_current->des == ALLOCATE_IN_SRAM)
			{
				if(!m_isNVMbusy[set] && m_isSRAMbusy[set] && simu_parameters.enableDatasetSpilling)
				{
					stats_busyness_alloc_change++;
					reportSpilling(rap_current , element.m_address , true , true);
					return ALLOCATE_IN_NVM;
				}
				else
					return ALLOCATE_IN_SRAM;		
			}
			
		}
		return rap_current->des;
	}
}

// Assembly:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rax
leaq 0x140(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movq %rax, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq 0x128(%rsp), %rax
movq %rax, 0x50(%rsp)
callq 0x20960
testb $0x1, %al
jne 0x1a99f
jmp 0x1a9af
movl $0x1, 0x134(%rsp)
jmp 0x1afdb
movq 0x48(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
movl $0x38, %edx
callq 0x52f0
movq 0x50(%rsp), %rdi
movq 0x110(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x30(%rax)
movups 0xe0(%rsp), %xmm0
movups 0xf0(%rsp), %xmm1
movups 0x100(%rsp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x1aff0
movq 0x50(%rsp), %rdi
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rsi
callq 0x1b150
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x1abab
movq 0x50(%rsp), %rax
testb $0x1, 0x81(%rax)
jne 0x1aa55
movq 0x50(%rsp), %rax
movl 0x358(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x358(%rax)
movq 0x50(%rsp), %rdi
movq 0x118(%rsp), %rsi
callq 0x1b220
movq %rax, %rcx
movq 0x50(%rsp), %rax
movl %ecx, 0xd4(%rsp)
movq 0x300(%rax), %rdi
movl 0xd4(%rsp), %esi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x50(%rsp), %rdi
movl %eax, 0xd0(%rsp)
addq $0x2e8, %rdi            # imm = 0x2E8
movslq 0xd4(%rsp), %rsi
callq 0x20270
movq %rax, %rdi
movslq 0xd0(%rsp), %rsi
callq 0x20430
movq 0x48(%rsp), %rsi
movq (%rax), %rax
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x98(%rsp), %rdi
movl $0x38, %edx
callq 0x52f0
movq 0x40(%rsp), %rdi
movq 0xc8(%rsp), %rcx
movq %rsp, %rax
movq %rcx, 0x30(%rax)
movups 0x98(%rsp), %xmm0
movups 0xa8(%rsp), %xmm1
movups 0xb8(%rsp), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x209a0
movl 0x78fed(%rip), %ecx     # 0x93b18
movl %ecx, %eax
addl $0x1, %eax
movl %eax, 0x78fe2(%rip)     # 0x93b18
movq 0xd8(%rsp), %rax
movl %ecx, 0x18(%rax)
movq 0x118(%rsp), %rcx
movq 0xd8(%rsp), %rax
movq %rcx, 0x20(%rax)
leaq 0x78bac(%rip), %rdi     # 0x93708
leaq 0x566fc(%rip), %rsi     # 0x7125f
callq 0x5380
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, %rdi
leaq 0x580de(%rip), %rsi     # 0x72c63
callq 0x5380
movq %rax, %rdi
movq 0x118(%rsp), %rsi
callq 0x50f0
movq %rax, %rdi
movq 0x78414(%rip), %rsi     # 0x92fb8
callq 0x53d0
jmp 0x1abe4
movq 0x50(%rsp), %rax
testb $0x1, 0x81(%rax)
jne 0x1abcd
movq 0x50(%rsp), %rax
movl 0x35c(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x35c(%rax)
movq 0xd8(%rsp), %rax
movl 0x80(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x80(%rax)
movq 0x50(%rsp), %rax
movq 0x300(%rax), %rdi
movq 0xd8(%rsp), %rax
movslq 0x30(%rax), %rsi
movq 0xd8(%rsp), %rax
movslq 0x34(%rax), %rdx
movq (%rdi), %rax
callq *(%rax)
movq 0xd8(%rsp), %rax
cmpl $0x2, 0x28(%rax)
jne 0x1ad91
movq 0xd8(%rsp), %rax
movl 0x38(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x38(%rax)
leaq 0x793b9(%rip), %rax     # 0x93ff0
testb $0x1, (%rax)
je 0x1acd4
leaq 0x78ac1(%rip), %rdi     # 0x93708
leaq 0x56ea4(%rip), %rsi     # 0x71af2
callq 0x5380
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, %rdi
leaq 0x56d48(%rip), %rsi     # 0x719b8
callq 0x5380
movq %rax, %rdi
leaq -0xb47f(%rip), %rsi     # 0xf800
callq 0x5160
movq %rax, %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rsi
callq 0x50f0
movq %rax, %rdi
leaq -0xb47e(%rip), %rsi     # 0xf820
callq 0x5160
movq %rax, %rdi
leaq 0x56d1f(%rip), %rsi     # 0x719cc
callq 0x5380
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x38(%rax), %esi
callq 0x5680
movq %rax, %rdi
movq 0x782e9(%rip), %rsi     # 0x92fb8
callq 0x53d0
movq 0xd8(%rsp), %rax
movl 0x38(%rax), %eax
leaq 0x7930a(%rip), %rcx     # 0x93ff0
cmpl 0xa4(%rcx), %eax
jne 0x1ad8f
movq 0xd8(%rsp), %rax
movl $0x0, 0x38(%rax)
leaq 0x792e8(%rip), %rax     # 0x93ff0
testb $0x1, (%rax)
je 0x1ad7f
leaq 0x789f4(%rip), %rdi     # 0x93708
leaq 0x56dd7(%rip), %rsi     # 0x71af2
callq 0x5380
movq %rax, %rdi
movq 0xd8(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, %rdi
leaq 0x56c9c(%rip), %rsi     # 0x719d9
callq 0x5380
movq %rax, %rdi
leaq -0xb54c(%rip), %rsi     # 0xf800
callq 0x5160
movq %rax, %rdi
movq 0x48(%rsp), %rax
movq (%rax), %rsi
callq 0x50f0
movq %rax, %rdi
leaq -0xb54b(%rip), %rsi     # 0xf820
callq 0x5160
movq %rax, %rdi
movq 0x7823e(%rip), %rsi     # 0x92fb8
callq 0x53d0
movl $0x1, 0x134(%rsp)
jmp 0x1afdb
jmp 0x1ad91
movq 0xd8(%rsp), %rax
cmpl $0x3, 0x28(%rax)
jne 0x1adcf
movq 0x48(%rsp), %rdi
callq 0xf7c0
testb $0x1, %al
jne 0x1adaf
jmp 0x1adbf
movl $0x0, 0x134(%rsp)
jmp 0x1afdb
movl $0x1, 0x134(%rsp)
jmp 0x1afdb
leaq 0x7921a(%rip), %rax     # 0x93ff0
testb $0x1, 0x158(%rax)
je 0x1afc9
movq 0xd8(%rsp), %rax
cmpl $0x1, 0x28(%rax)
jne 0x1aed3
movq 0x50(%rsp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq 0x120(%rsp), %rsi
callq 0xfa90
movq %rax, 0x88(%rsp)
movq %rdx, 0x90(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xfaf0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x3f(%rsp)
jne 0x1ae3a
jmp 0x1ae70
movq 0x50(%rsp), %rdi
addq $0xe8, %rdi
movq 0x120(%rsp), %rsi
callq 0xfa90
movq %rax, 0x78(%rsp)
movq %rdx, 0x80(%rsp)
leaq 0x78(%rsp), %rdi
callq 0xfaf0
xorb $-0x1, %al
movb %al, 0x3f(%rsp)
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x1ae7a
jmp 0x1aec3
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rax
movq 0xd8(%rsp), %rsi
movq (%rax), %rdx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x1b250
movq 0x50(%rsp), %rax
movq 0x378(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x378(%rax)
movl $0x0, 0x134(%rsp)
jmp 0x1afdb
movl $0x1, 0x134(%rsp)
jmp 0x1afdb
movq 0xd8(%rsp), %rax
cmpl $0x0, 0x28(%rax)
jne 0x1afc5
movq 0x50(%rsp), %rdi
addq $0x110, %rdi            # imm = 0x110
movq 0x120(%rsp), %rsi
callq 0xfa90
movq %rax, 0x68(%rsp)
movq %rdx, 0x70(%rsp)
leaq 0x68(%rsp), %rdi
callq 0xfaf0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x3e(%rsp)
jne 0x1af6c
movq 0x50(%rsp), %rdi
addq $0xe8, %rdi
movq 0x120(%rsp), %rsi
callq 0xfa90
movq %rax, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
leaq 0x58(%rsp), %rdi
callq 0xfaf0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x3e(%rsp)
jne 0x1af5b
jmp 0x1af6c
leaq 0x7908e(%rip), %rax     # 0x93ff0
movb 0x158(%rax), %al
movb %al, 0x3e(%rsp)
movb 0x3e(%rsp), %al
testb $0x1, %al
jne 0x1af76
jmp 0x1afb8
movq 0x50(%rsp), %rdi
movq 0x48(%rsp), %rax
movq 0x378(%rdi), %rcx
addq $0x1, %rcx
movq %rcx, 0x378(%rdi)
movq 0xd8(%rsp), %rsi
movq (%rax), %rdx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x1b250
movl $0x1, 0x134(%rsp)
jmp 0x1afdb
movl $0x0, 0x134(%rsp)
jmp 0x1afdb
jmp 0x1afc7
jmp 0x1afc9
movq 0xd8(%rsp), %rax
movl 0x28(%rax), %eax
movl %eax, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
nopw (%rax,%rax)