// File: /workspace/github_cpp_makefile_star2_O2/gvaumour[P]cache-simulator/src/Hierarchy.cc
// Function: Hierarchy::handleAccess(Access)
void
Hierarchy::handleAccess(Access element)
{
	uint64_t addr = element.m_address;
	uint64_t block_addr = bitRemove(addr , 0 , m_start_index+1);
	
	DPRINTF(DebugHierarchy, "HIERARCHY::New Access : Block %#lx Req %s, Id_Thread %d\n", block_addr , memCmd_str[element.m_type], element.m_idthread);


	int id_thread = (int) element.m_idthread;
	int id_core = convertThreadIDtoCore(id_thread);
	m_currentInst = element.m_pc; 
	
	assert(id_core < (int)m_nbCores);	
	
	DirectoryEntry* entry = m_directory->getEntry(block_addr);
	if(entry == NULL){
		entry = m_directory->addEntry(block_addr, element.isInstFetch());
	}
	assert(entry != NULL);
		
	DirectoryState dir_state = m_directory->getEntry(block_addr)->coherence_state;
	
	entry->print();
	bool doPrefetch = true;
//	bool isInHierarchy = true;
	
	if(dir_state == NOT_PRESENT)
	{
		m_directory->setTrackerToEntry(block_addr, id_core);			

		m_private_caches[id_core]->handleAccess(element);		
		
		allocDecision des = m_LLC->handleAccess(element);
		if( des != BYPASS_CACHE)
			m_directory->setCoherenceState(block_addr, EXCLUSIVE_L1);	
		else
			m_directory->setCoherenceState(block_addr, EXCLUSIVE_L1_NOT_LLC);			
				
		m_directory->updateEntry(block_addr);
		if(element.isWrite())
			stats_writeMainMem++;
		else
			stats_readMainMem++;	
	}
	else if(dir_state == CLEAN_LLC || dir_state == DIRTY_LLC)
	{
		m_directory->setTrackerToEntry(block_addr, id_core);			

		m_private_caches[id_core]->handleAccess(element);
		m_LLC->handleAccess(element);
		
		/* Prefetch stats collection */ 
		stats_hitsPrefetch += m_LLC->isPrefetchBlock(element.m_address) ? 1 : 0;
		m_LLC->resetPrefetchFlag(element.m_address);
		
		m_directory->setCoherenceState(block_addr, EXCLUSIVE_L1);
		m_directory->updateEntry(block_addr);
		
	}
	else if( dir_state == MODIFIED_L1 || dir_state == MODIFIED_L1_NOT_LLC || \
			 dir_state == EXCLUSIVE_L1 || dir_state == EXCLUSIVE_L1_NOT_LLC)
	{
		doPrefetch = false;
		set<int> nodes = m_directory->getTrackers(block_addr);
		assert(nodes.size() == 1);
		int node = *(nodes.begin());

		if(node != id_core)
		{
			assert(false && "Not implemented yet");
			m_private_caches[node]->sendInvalidation(block_addr, element.isInstFetch() );
			//If it is a write, deallocate it from one core to allocate it to the other
			if(element.isWrite()){
				m_private_caches[node]->deallocate(block_addr);
				m_directory->setTrackerToEntry(block_addr, id_core);
				m_directory->setCoherenceState(block_addr, EXCLUSIVE_L1);

			}
			else
			{	//If it is a read, add id_core to the tracker list 
				m_directory->addTrackerToEntry(block_addr, id_core);
				m_directory->setCoherenceState(block_addr, SHARED_L1);

			}
			m_LLC->handleAccess(element);
			m_directory->updateEntry(block_addr);
			m_private_caches[id_core]->handleAccess(element);
		
		}
		else{
			// Transistion from Exclusive to Modified 
			if(element.isWrite() && dir_state == EXCLUSIVE_L1_NOT_LLC)
				m_directory->setCoherenceState(block_addr, MODIFIED_L1_NOT_LLC);
			if(element.isWrite() && dir_state == EXCLUSIVE_L1)
				m_directory->setCoherenceState(block_addr, MODIFIED_L1);

			m_private_caches[id_core]->handleAccess(element);
		
		}
	}
	else if(dir_state == SHARED_L1)
	{
		doPrefetch = false;
		if(element.isWrite())
		{
			//We invalidate all the sharers
			set<int> nodes = m_directory->getTrackers(block_addr);
			for(auto node : nodes)
				m_private_caches[node]->deallocate(block_addr);

			m_directory->resetTrackersToEntry(block_addr);
			m_directory->setTrackerToEntry(block_addr, id_core);
			
			//We write the new version to the LLC 
			m_LLC->handleAccess(element);
			m_directory->updateEntry(block_addr);
			m_private_caches[id_core]->handleAccess(element);
			m_directory->setCoherenceState(block_addr, EXCLUSIVE_L1);
		}
		else
		{
			// We add id_core to tracker list
			m_directory->addTrackerToEntry(block_addr, id_core);				
			m_private_caches[id_core]->handleAccess(element);
		}
	}


	if(doPrefetch && simu_parameters.enablePrefetch) //We generate prefetch only for LLC demand accesses 
	{
		vector<uint64_t> prefetch_addresses = m_prefetcher->getNextAddress(element.m_address);
		for(auto p : prefetch_addresses)
		{
			Access request;
			request.m_address = p;
			request.m_size = 4;
			request.m_type = element.isInstFetch() ? MemCmd::DATA_PREFETCH : MemCmd::INST_PREFETCH;
			prefetchAddress(request);
		}
	}	 
	
	if( (cpt_time - stats_beginTimeFrame) > PREDICTOR_TIME_FRAME)
	{
		openNewTimeFrame();
//		cout << "TimeFrame " << i++ << endl;
		stats_beginTimeFrame = cpt_time;	
	}
	
	DPRINTF(DebugHierarchy, "HIERARCHY::End of handleAccess\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
movq 0xf0(%rsp), %rdi
movl 0x60(%rbx), %edx
incl %edx
movq %rdi, 0x70(%rsp)
xorl %esi, %esi
callq 0x24f70
movq %rax, %r15
leaq 0x1721b(%rip), %rsi     # 0x2a304
leaq 0x40(%rsp), %rdi
leaq 0x3f(%rsp), %rdx
callq 0x5140
leaq 0x2ae51(%rip), %rax     # 0x3df50
leaq 0x8(%rax), %r12
leaq 0x40(%rsp), %rsi
movq %r12, %rdi
callq 0x138f8
movq %rax, %r13
leaq 0xf0(%rsp), %r14
leaq 0x40(%rsp), %rdi
callq 0x5618
testq %r13, %r13
je 0x13152
movl 0x1c(%r14), %eax
movl 0x20(%r14), %ebp
leaq 0x2a1f7(%rip), %rcx     # 0x3d330
movq (%rcx,%rax,8), %rdx
leaq 0x1745b(%rip), %rdi     # 0x2a59f
movq %r15, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x5040
jmp 0x13156
movl 0x20(%r14), %ebp
movl 0x34(%rbx), %ecx
movl %ebp, %eax
xorl %edx, %edx
divl %ecx
movq 0x10(%r14), %rax
movq %rax, 0x58(%rbx)
cmpl %ecx, %edx
jge 0x13693
movl %edx, %ebp
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0xfc52
testq %rax, %rax
movq %r12, 0x78(%rsp)
jne 0x131a6
movq 0x20(%rbx), %rdi
xorl %edx, %edx
cmpl $0x2, 0x1c(%r14)
setb %dl
movq %r15, %rsi
callq 0xfb94
testq %rax, %rax
je 0x136b2
movq %rax, %r13
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0xfc52
movl 0x38(%rax), %r12d
movq %r13, %rdi
callq 0xfa48
cmpl $0x8, %r12d
jne 0x1327f
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0xfd28
movslq %ebp, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
movq 0x18(%rbx), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
movq 0x20(%rbx), %rdi
leal (%rcx,%rcx,2), %edx
addl $0x2, %edx
movq %r15, %rsi
callq 0xfe58
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x10032
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x13273
cmpl $0x3, %eax
jne 0x133ad
incq 0x88(%rbx)
jmp 0x133b4
movl %r12d, %eax
andl $-0x2, %eax
cmpl $0x6, %eax
jne 0x13342
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0xfd28
movslq %ebp, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
movq 0x18(%rbx), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
movq 0x18(%rbx), %rdi
movq 0x70(%rsp), %r12
movq %r12, %rsi
callq 0x196c2
movzbl %al, %eax
addq %rax, 0x98(%rbx)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0x196ba
movq 0x20(%rbx), %rdi
pushq $0x2
popq %rdx
movq %r15, %rsi
callq 0xfe58
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x10032
jmp 0x133b4
cmpl $0x5, %r12d
ja 0x133b4
pushq $0x36
popq %rax
btl %r12d, %eax
jae 0x13551
movq 0x20(%rbx), %rsi
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
callq 0xfd76
cmpq $0x1, 0x28(%r13)
jne 0x136d1
movq 0x58(%rsp), %rax
cmpl %ebp, 0x20(%rax)
jne 0x136f0
movl 0x1c(%r14), %eax
cmpl $0x3, %eax
sete %cl
cmpl $0x6, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x1347d
cmpl $0x5, %r12d
jne 0x1347d
pushq $0x4
jmp 0x1348c
incq 0x80(%rbx)
leaq 0x2ab95(%rip), %rax     # 0x3df50
cmpb $0x1, 0x8b(%rax)
jne 0x134d7
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x40(%rsp), %r15
movq %r15, %rdi
movq 0x70(%rsp), %rdx
callq *(%rax)
movq (%r15), %r12
movq 0x8(%r15), %r15
xorl %eax, %eax
cmpl $0x2, 0x1c(%r14)
setb %al
leal (%rax,%rax,2), %ebp
incl %ebp
cmpq %r15, %r12
je 0x13471
movq (%r12), %rax
movq %rax, 0x80(%rsp)
movl $0x4, 0x88(%rsp)
andq $0x0, 0x90(%rsp)
movl %ebp, 0x9c(%rsp)
andl $0x0, 0xa0(%rsp)
movb $0x0, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x80(%rsp), %xmm0
movups 0x90(%rsp), %xmm1
movups 0xa0(%rsp), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq %rbx, %rdi
callq 0x12efc
addq $0x8, %r12
jmp 0x133f4
leaq 0x40(%rsp), %rdi
callq 0xa4a8
jmp 0x134d7
cmpl $0x2, %r12d
sete %cl
andb %cl, %al
cmpb $0x1, %al
jne 0x13499
pushq $0x1
popq %rdx
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0xfe58
movslq %ebp, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
leaq 0x40(%rsp), %rdi
callq 0x101d6
leaq 0x2aa3a(%rip), %r14     # 0x3df18
movq (%r14), %rax
subq 0x68(%rbx), %rax
cmpq $0x186a1, %rax          # imm = 0x186A1
jb 0x134fc
movq %rbx, %rdi
callq 0x1375a
movq (%r14), %rax
movq %rax, 0x68(%rbx)
leaq 0x16e01(%rip), %rsi     # 0x2a304
leaq 0x40(%rsp), %rdi
leaq 0x3f(%rsp), %rdx
callq 0x5140
movq 0x78(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x138f8
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5618
testq %rbx, %rbx
je 0x1353f
leaq 0x17191(%rip), %rdi     # 0x2a6cb
callq 0x5480
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
jne 0x133b4
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x1356c
cmpl $0x3, %eax
jne 0x1364c
movq 0x20(%rbx), %rsi
leaq 0x40(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0xfd76
movq 0x18(%r12), %r12
leaq 0x48(%rsp), %r13
cmpq %r13, %r12
je 0x135b0
movslq 0x20(%r12), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq %r15, %rsi
callq 0x194ba
movq %r12, %rdi
callq 0x5340
movq %rax, %r12
jmp 0x1358a
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0xfe8a
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0xfd28
movq 0x18(%rbx), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x10032
movslq %ebp, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
movq 0x20(%rbx), %rdi
pushq $0x2
popq %rdx
movq %r15, %rsi
callq 0xfe58
jmp 0x134cd
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0xff4c
movslq %ebp, %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq 0x30(%r14), %rax
movq %rax, 0x30(%rsp)
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movups 0x20(%r14), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x19476
jmp 0x134d7
leaq 0x16f3e(%rip), %rdi     # 0x2a5d8
leaq 0x16d9d(%rip), %rsi     # 0x2a43e
leaq 0x16f49(%rip), %rcx     # 0x2a5f1
movl $0x11a, %edx            # imm = 0x11A
callq 0x51e0
leaq 0x184ab(%rip), %rdi     # 0x2bb64
leaq 0x16d7e(%rip), %rsi     # 0x2a43e
leaq 0x16f2a(%rip), %rcx     # 0x2a5f1
movl $0x120, %edx            # imm = 0x120
callq 0x51e0
leaq 0x16f3e(%rip), %rdi     # 0x2a616
leaq 0x16d5f(%rip), %rsi     # 0x2a43e
leaq 0x16f0b(%rip), %rcx     # 0x2a5f1
movl $0x14e, %edx            # imm = 0x14E
callq 0x51e0
leaq 0x16f31(%rip), %rdi     # 0x2a628
leaq 0x16d40(%rip), %rsi     # 0x2a43e
leaq 0x16eec(%rip), %rcx     # 0x2a5f1
movl $0x153, %edx            # imm = 0x153
callq 0x51e0
jmp 0x13713
jmp 0x13713
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x101d6
jmp 0x13747
jmp 0x13726
jmp 0x13735
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x5618
jmp 0x13747
movq %rax, %rbx
jmp 0x13747
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xa4a8
movq %rbx, %rdi
callq 0x5570
nop