// File: gvaumour[P]cache-simulator/src/DBAMBPredictor.cc
// Function: DBAMBPredictor::updatePolicy(unsigned long, unsigned long, bool, Access, bool)
void
DBAMBPredictor::updatePolicy(uint64_t set, uint64_t index, bool inNVM, Access element, bool isWBrequest = false)
{

	//DPRINTF("DBAMBPredictor::updatePolicy set %ld , index %ld\n" , set, index);

	CacheEntry* current = NULL;
	if(inNVM)
		current = m_tableNVM[set][index];
	else 
		current = m_tableSRAM[set][index];
	
	int rd = computeRd(set, index , inNVM);

	current->policyInfo = m_cpt;

	DHPEntry* rap_current = lookup(current->signature);

	if(rap_current)
	{

//			//DPRINTF("DBAMBPredictor::updatePolicy Reuse distance of the pc %lx, RD = %d\n", current->m_pc , rd );
		rap_current->rd_history.push_back(convertRD(rd));
		rap_current->rw_history.push_back(element.isWrite());		
		
		//A learning cl shows some reuse so we quit dead state 
		if(current->isLearning && rap_current->des == BYPASS_CACHE)
		{
			// Reset the window 
			RW_TYPE old_rw = rap_current->state_rw;
			RD_TYPE old_rd = rap_current->state_rd;
			
			rap_current->des = ALLOCATE_PREEMPTIVELY;
			rap_current->state_rd = RD_NOT_ACCURATE;
			rap_current->state_rw = RW_NOT_ACCURATE;
			rap_current->dead_counter = 0;

			if(!m_isWarmup)
				stats_ClassErrors[old_rd + NUM_RD_TYPE*old_rw][rap_current->state_rd + NUM_RD_TYPE*rap_current->state_rw]++;
			
			dataset_file << "Dataset n°" << rap_current->id << ": Reuse detected on a learning Cl, switching to learning state" << endl;
		}
		else
		{
			updateWindow(rap_current);
		
			reportAccess(rap_current , element, current, current->isNVM, string("UPDATE"), string(str_RD_status[convertRD(rd)]));
			dataset_file << "Position is " << rd << endl;
			if(simu_parameters.enableMigration )
				current = checkLazyMigration(rap_current , current , set , inNVM , index, element.isWrite());
		}
	}
	
	m_cpt++;
	Predictor::updatePolicy(set , index , inNVM , element , isWBrequest);
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movb %r8b, %al
leaq 0x200(%rsp), %r8
movq %r8, 0xc8(%rsp)
movq %rdi, 0x1f0(%rsp)
movq %rsi, 0x1e8(%rsp)
movq %rdx, 0x1e0(%rsp)
andb $0x1, %cl
movb %cl, 0x1df(%rsp)
andb $0x1, %al
movb %al, 0x1de(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0xd0(%rsp)
movq $0x0, 0x1d0(%rsp)
testb $0x1, 0x1df(%rsp)
je 0x1c7b1
movq 0xd0(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x1e8(%rsp), %rsi
callq 0xf9a0
movq %rax, %rdi
movq 0x1e0(%rsp), %rsi
callq 0xf9c0
movq (%rax), %rax
movq %rax, 0x1d0(%rsp)
jmp 0x1c7e5
movq 0xd0(%rsp), %rax
movq 0x8(%rax), %rdi
movq 0x1e8(%rsp), %rsi
callq 0xf9a0
movq %rax, %rdi
movq 0x1e0(%rsp), %rsi
callq 0xf9c0
movq (%rax), %rax
movq %rax, 0x1d0(%rsp)
movq 0xd0(%rsp), %rdi
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movb 0x1df(%rsp), %al
andb $0x1, %al
movzbl %al, %ecx
callq 0x1ce20
movq 0xd0(%rsp), %rdi
movl %eax, 0x1cc(%rsp)
movq 0x270(%rdi), %rax
movl %eax, %ecx
movq 0x1d0(%rsp), %rax
movl %ecx, 0x38(%rax)
movq 0x1d0(%rsp), %rax
movq 0x18(%rax), %rsi
callq 0x1b150
movq %rax, 0x1c0(%rsp)
cmpq $0x0, 0x1c0(%rsp)
je 0x1cd43
movq 0xd0(%rsp), %rdi
movq 0x1c0(%rsp), %rax
addq $0x40, %rax
movq %rax, 0xb8(%rsp)
movl 0x1cc(%rsp), %esi
callq 0x1cfc0
movq 0xb8(%rsp), %rdi
movl %eax, 0x1bc(%rsp)
leaq 0x1bc(%rsp), %rsi
callq 0x20c20
movq 0xc8(%rsp), %rdi
movq 0x1c0(%rsp), %rax
addq $0x58, %rax
movq %rax, 0xc0(%rsp)
callq 0xf7c0
movq 0xc0(%rsp), %rdi
movzbl %al, %esi
andl $0x1, %esi
callq 0x20b40
movq 0x1d0(%rsp), %rax
testb $0x1, 0x148(%rax)
je 0x1ca1a
movq 0x1c0(%rsp), %rax
cmpl $0x2, 0x28(%rax)
jne 0x1ca1a
movq 0xd0(%rsp), %rax
movq 0x1c0(%rsp), %rcx
movl 0x98(%rcx), %ecx
movl %ecx, 0x1b8(%rsp)
movq 0x1c0(%rsp), %rcx
movl 0x9c(%rcx), %ecx
movl %ecx, 0x1b4(%rsp)
movq 0x1c0(%rsp), %rcx
movl $0x3, 0x28(%rcx)
movq 0x1c0(%rsp), %rcx
movl $0x2, 0x9c(%rcx)
movq 0x1c0(%rsp), %rcx
movl $0x4, 0x98(%rcx)
movq 0x1c0(%rsp), %rcx
movl $0x0, 0xb8(%rcx)
testb $0x1, 0x81(%rax)
jne 0x1c9d1
movq 0xd0(%rsp), %rdi
addq $0x3a0, %rdi            # imm = 0x3A0
movl 0x1b4(%rsp), %eax
imull $0x3, 0x1b8(%rsp), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0xef70
movq %rax, %rdi
movq 0x1c0(%rsp), %rax
movl 0x9c(%rax), %eax
movq 0x1c0(%rsp), %rcx
imull $0x3, 0x98(%rcx), %ecx
addl %ecx, %eax
movslq %eax, %rsi
callq 0xef90
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
leaq 0x76d30(%rip), %rdi     # 0x93708
leaq 0x55113(%rip), %rsi     # 0x71af2
callq 0x5380
movq %rax, %rdi
movq 0x1c0(%rsp), %rax
movl 0x18(%rax), %esi
callq 0x5680
movq %rax, %rdi
leaq 0x54fef(%rip), %rsi     # 0x719f0
callq 0x5380
movq %rax, %rdi
movq 0x765a8(%rip), %rsi     # 0x92fb8
callq 0x53d0
jmp 0x1cd41
movq 0xd0(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
callq 0x1ba30
movq 0xc8(%rsp), %rax
movq 0x1c0(%rsp), %rcx
movq %rcx, 0x98(%rsp)
movq 0x30(%rax), %rcx
movq %rcx, 0x1a0(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movaps %xmm2, 0x190(%rsp)
movaps %xmm1, 0x180(%rsp)
movaps %xmm0, 0x170(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movb 0x40(%rax), %al
movb %al, 0xaf(%rsp)
leaq 0x14f(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
callq 0x56a0
movq 0xb0(%rsp), %rdx
leaq 0x546e6(%rip), %rsi     # 0x7119a
leaq 0x150(%rsp), %rdi
callq 0x5530
jmp 0x1cac3
movq 0xd0(%rsp), %rdi
movl 0x1cc(%rsp), %esi
callq 0x1cfc0
movl %eax, 0x94(%rsp)
jmp 0x1cae0
movl 0x94(%rsp), %eax
movl %eax, %eax
movl %eax, %ecx
leaq 0x769de(%rip), %rax     # 0x934d0
movq (%rax,%rcx,8), %rax
movq %rax, 0x80(%rsp)
leaq 0x117(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x56a0
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x118(%rsp), %rdi
callq 0x5530
jmp 0x1cb32
movq 0xa0(%rsp), %rdx
movq 0x98(%rsp), %rsi
movq 0xd0(%rsp), %rdi
movb 0xaf(%rsp), %al
movq 0x1a0(%rsp), %r8
movq %rsp, %rcx
movq %r8, 0x30(%rcx)
movaps 0x170(%rsp), %xmm0
movaps 0x180(%rsp), %xmm1
movaps 0x190(%rsp), %xmm2
movups %xmm2, 0x20(%rcx)
movups %xmm1, 0x10(%rcx)
movups %xmm0, (%rcx)
movzbl %al, %ecx
andl $0x1, %ecx
leaq 0x150(%rsp), %r8
leaq 0x118(%rsp), %r9
callq 0x1bd50
jmp 0x1cba0
leaq 0x118(%rsp), %rdi
callq 0x5768
leaq 0x117(%rsp), %rdi
callq 0x53e0
leaq 0x150(%rsp), %rdi
callq 0x5768
leaq 0x14f(%rsp), %rdi
callq 0x53e0
leaq 0x76b2d(%rip), %rdi     # 0x93708
leaq 0x54e4d(%rip), %rsi     # 0x71a2f
callq 0x5380
movq %rax, %rdi
movl 0x1cc(%rsp), %esi
callq 0x5680
movq %rax, %rdi
movq 0x763b8(%rip), %rsi     # 0x92fb8
callq 0x53d0
leaq 0x773e4(%rip), %rax     # 0x93ff0
testb $0x1, 0xa9(%rax)
je 0x1cd3f
movq 0xc8(%rsp), %rdi
movq 0x1c0(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x68(%rsp)
movb 0x1df(%rsp), %al
movb %al, 0x7f(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x70(%rsp)
callq 0xf7c0
movq 0xd0(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x68(%rsp), %rcx
movq 0x70(%rsp), %r9
movb %al, %r8b
movb 0x7f(%rsp), %al
movzbl %r8b, %r10d
andl $0x1, %r10d
movq %rsp, %r8
movl %r10d, (%r8)
movzbl %al, %r8d
andl $0x1, %r8d
callq 0x1d060
movq %rax, 0x1d0(%rsp)
jmp 0x1cd3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0x1cd2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0x1cd20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
jmp 0x1cd13
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x140(%rsp)
movl %eax, 0x13c(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x5768
leaq 0x117(%rsp), %rdi
callq 0x53e0
leaq 0x150(%rsp), %rdi
callq 0x5768
leaq 0x14f(%rsp), %rdi
callq 0x53e0
jmp 0x1ce09
jmp 0x1cd41
jmp 0x1cd43
movq 0xd0(%rsp), %rax
movq 0xc8(%rsp), %rsi
movq 0x270(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x270(%rax)
movq 0x1e8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0x50(%rsp)
movb 0x1df(%rsp), %al
movb %al, 0x47(%rsp)
leaq 0xd8(%rsp), %rdi
movl $0x38, %edx
callq 0x52f0
movb 0x47(%rsp), %cl
movq 0xd0(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movb 0x1de(%rsp), %al
movq 0x108(%rsp), %r9
movq %rsp, %r8
movq %r9, 0x30(%r8)
movups 0xd8(%rsp), %xmm0
movups 0xe8(%rsp), %xmm1
movups 0xf8(%rsp), %xmm2
movups %xmm2, 0x20(%r8)
movups %xmm1, 0x10(%r8)
movups %xmm0, (%r8)
movzbl %cl, %ecx
andl $0x1, %ecx
movzbl %al, %r8d
andl $0x1, %r8d
callq 0x56ed0
addq $0x1f8, %rsp            # imm = 0x1F8
retq
movq 0x140(%rsp), %rdi
callq 0x5690
nopw %cs:(%rax,%rax)