// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::checkLazyMigration(DHPEntry*, CacheEntry*, unsigned long, bool, unsigned long, bool)
CacheEntry*
DBAMBPredictor::checkLazyMigration(DHPEntry* rap_current , CacheEntry* current ,uint64_t set,bool inNVM, uint64_t index, bool isWrite)
{
	if( inNVM == false && rap_current->des == ALLOCATE_IN_NVM)
	{
		//cl is SRAM , check whether we should migrate to NVM 
		if(simu_parameters.enableDatasetSpilling && m_isNVMbusy[set])
		{
			reportSpilling(rap_current, current->address, false ,  inNVM);
			stats_busyness_migrate_change++; //If NVM tab is busy , don't migrate
		}
		else
		{
			//Trigger Migration
			int id_assoc = evictPolicy(set, true);//Select LRU candidate from NVM cache
			//DPRINTF("DBAMBPredictor:: Migration Triggered from SRAM, index %ld, id_assoc %d \n" , index, id_assoc);

			CacheEntry* replaced_entry = m_tableNVM[set][id_assoc];
					
			/** Migration incurs one read and one extra write */ 
			replaced_entry->nbWrite++;
			current->nbRead++;
		
			/* Record the write error migration */ 
			Predictor::migrationRecording();
		
			reportMigration(rap_current, current, false);
		
			m_cache->triggerMigration(set, index , id_assoc , false);
			if(!m_isWarmup)
				stats_nbMigrationsFromNVM.back()[FROMSRAM]++;


			current = replaced_entry;
		}

	}
	else if( rap_current->des == ALLOCATE_IN_SRAM && inNVM == true)
	{
		//cl is in NVM , check whether we should migrate to SRAM 
			
		//If SRAM tab is busy , don't migrate
		if(simu_parameters.enableDatasetSpilling && m_isSRAMbusy[set])
		{
			reportSpilling(rap_current, current->address, false ,  inNVM);
			stats_busyness_migrate_change++; //If NVM tab is busy , don't migrate
		}
		else 
		{		
			int id_assoc = evictPolicy(set, false);
		
			CacheEntry* replaced_entry = m_tableSRAM[set][id_assoc];

			/** Migration incurs one read and one extra write */ 
			replaced_entry->nbWrite++;
			current->nbRead++;
			reportMigration(rap_current, current, true);
	
			m_cache->triggerMigration(set, id_assoc , index , true);
		
			if(!m_isWarmup)
				stats_nbMigrationsFromNVM.back()[FROMNVM]++;

			current = replaced_entry;		
		}

	}
	return current;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %r9, %r15
movq %rcx, %r12
movq %rdx, %r14
movq %rsi, %r13
movq %rdi, %rbx
movl 0x28(%rsi), %eax
testl %r8d, %r8d
je 0xd099
testl %eax, %eax
setne %al
xorb $0x1, %r8b
orb %al, %r8b
jne 0xd1dc
leaq 0x30ee3(%rip), %rax     # 0x3df50
cmpb $0x1, 0x158(%rax)
jne 0xd0eb
leaq 0xe8(%rbx), %rdi
movq %r12, %rsi
callq 0x9f66
testq %rdx, (%rax)
je 0xd0eb
movq 0x8(%r14), %rdx
pushq $0x1
popq %r8
movq %r13, %rsi
xorl %ecx, %ecx
jmp 0xd0da
cmpl $0x1, %eax
jne 0xd1dc
leaq 0x30ea7(%rip), %rax     # 0x3df50
cmpb $0x1, 0x158(%rax)
jne 0xd162
leaq 0x110(%rbx), %rdi
movq %r12, %rsi
callq 0x9f66
testq %rdx, (%rax)
je 0xd162
movq 0x8(%r14), %rdx
movq %r13, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0xc226
incq 0x380(%rbx)
jmp 0xd1dc
movq (%rbx), %rax
movq %rbx, %rdi
movl %r12d, %esi
xorl %edx, %edx
callq *0x28(%rax)
movq 0x8(%rbx), %rcx
movq (%rcx), %rcx
imulq $0x18, %r12, %rdx
movslq %eax, %rbp
movq (%rcx,%rdx), %rax
movq %r15, (%rsp)
movq (%rax,%rbp,8), %r15
incl 0x11c(%r15)
incl 0x120(%r14)
pushq $0x1
popq %rcx
movq %r13, %rsi
movq %r14, %rdx
movl %ecx, %r14d
callq 0xd4fa
movq 0x70(%rbx), %rdi
movl %r12d, %esi
movl %ebp, %edx
movq (%rsp), %rcx
movl %r14d, %r8d
callq 0x163be
cmpb $0x0, 0x81(%rbx)
jne 0xd15d
movq 0x3c0(%rbx), %rax
movq -0x18(%rax), %rax
incl (%rax)
movq %r15, %r14
jmp 0xd1dc
movq (%rbx), %rax
pushq $0x1
popq %rdx
movq %rbx, %rdi
movl %r12d, %esi
callq *0x28(%rax)
movq 0x10(%rbx), %rcx
movq (%rcx), %rcx
imulq $0x18, %r12, %rdx
movslq %eax, %rbp
movq (%rcx,%rdx), %rax
movq (%rax,%rbp,8), %rax
movq %rax, (%rsp)
incl 0x11c(%rax)
incl 0x120(%r14)
movq %rbx, %rdi
callq 0x1fe30
movq %r13, %rsi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0xd4fa
movq 0x70(%rbx), %rdi
movl %r12d, %esi
movl %r15d, %edx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x163be
cmpb $0x0, 0x81(%rbx)
jne 0xd1d8
movq 0x3c0(%rbx), %rax
movq -0x18(%rax), %rax
incl 0x4(%rax)
movq (%rsp), %r14
movq %r14, %rax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq