// File: /workspace/github_cpp_makefile_star2_O1/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, 0x68(%rsp)
xorl %esi, %esi
callq 0x2e70a
movq %rax, %r15
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1da98(%rip), %rsi     # 0x35388
leaq 0x1da9f(%rip), %rdx     # 0x35396
leaq 0x38(%rsp), %rdi
callq 0xccac
leaq 0x2e648(%rip), %rbp     # 0x45f50
leaq 0x8(%rbp), %r12
leaq 0x38(%rsp), %rsi
movq %r12, %rdi
callq 0x182c0
movq %rax, %r13
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x1792b
callq 0x52a0
leaq 0xf0(%rsp), %r14
leaq 0x10(%rbp), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, %r13
je 0x17965
movl 0x1c(%r14), %eax
movl 0x20(%r14), %ecx
leaq 0x2d9e0(%rip), %rdx     # 0x45330
movq (%rdx,%rax,8), %rdx
leaq 0x1dcc8(%rip), %rdi     # 0x35623
movq %r15, %rsi
xorl %eax, %eax
callq 0x5050
movl 0x20(%r14), %eax
movl 0x34(%rbx), %ecx
xorl %edx, %edx
divl %ecx
movq 0x10(%r14), %rax
movq %rax, 0x58(%rbx)
cmpl %ecx, %edx
jge 0x17eec
movl %edx, %ebp
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13236
movq %rax, %r13
testq %rax, %rax
jne 0x179af
movq 0x20(%rbx), %rdi
xorl %edx, %edx
cmpl $0x2, 0x1c(%r14)
setb %dl
movq %r15, %rsi
callq 0x1311c
movq %rax, %r13
movq %r12, 0x78(%rsp)
testq %r13, %r13
je 0x17f0b
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13236
movl 0x38(%rax), %r12d
movq %r13, %rdi
callq 0x12fb0
cmpl $0x8, %r12d
jne 0x17a93
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x1332e
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 0x1ec36
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 0x1ec36
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
movq 0x20(%rbx), %rdi
leal (%rcx,%rcx,2), %edx
addl $0x2, %edx
movq %r15, %rsi
callq 0x13482
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13730
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x17a87
cmpl $0x3, %eax
jne 0x17c36
incq 0x88(%rbx)
jmp 0x17c3d
movl %r12d, %eax
andl $-0x2, %eax
cmpl $0x6, %eax
jne 0x17b5b
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x1332e
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 0x1ec36
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 0x1ec36
movq 0x18(%rbx), %rdi
movq 0x68(%rsp), %r12
movq %r12, %rsi
callq 0x1ef12
movzbl %al, %eax
addq %rax, 0x98(%rbx)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0x1ef0a
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x13482
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13730
jmp 0x17c3d
cmpl $0x5, %r12d
ja 0x17c3d
movl $0x36, %eax
btl %r12d, %eax
jae 0x17da5
movq 0x20(%rbx), %rsi
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
callq 0x1337e
cmpq $0x1, 0x28(%r13)
jne 0x17f2a
movq 0x50(%rsp), %rax
cmpl %ebp, 0x20(%rax)
jne 0x17f49
movl 0x1c(%r14), %eax
cmpl $0x3, %eax
sete %cl
cmpl $0x6, %eax
sete %r13b
orb %cl, %r13b
cmpb $0x1, %r13b
jne 0x17bd2
cmpl $0x5, %r12d
jne 0x17bd2
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x4, %edx
callq 0x13482
cmpl $0x2, %r12d
sete %al
andb %al, %r13b
cmpb $0x1, %r13b
jne 0x17bf3
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x1, %edx
callq 0x13482
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 0x1ec36
leaq 0x38(%rsp), %rdi
callq 0x138d6
jmp 0x17d1a
incq 0x80(%rbx)
leaq 0x2e30c(%rip), %rax     # 0x45f50
cmpb $0x1, 0x8b(%rax)
jne 0x17d1a
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq 0x68(%rsp), %rdx
callq *(%rax)
movq (%r15), %r12
movq 0x8(%r15), %r15
cmpq %r15, %r12
je 0x17d0b
xorl %eax, %eax
cmpl $0x2, 0x1c(%r14)
setb %al
leal (%rax,%rax,2), %ebp
incl %ebp
movq (%r12), %rax
movq %rax, 0x80(%rsp)
movl $0x4, 0x88(%rsp)
movq $0x0, 0x90(%rsp)
movl %ebp, 0x9c(%rsp)
movl $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 0x176c4
addq $0x8, %r12
cmpq %r15, %r12
jne 0x17c86
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x17d1a
callq 0x52a0
leaq 0x2e1f7(%rip), %r14     # 0x45f18
movq (%r14), %rax
subq 0x68(%rbx), %rax
cmpq $0x186a1, %rax          # imm = 0x186A1
jb 0x17d3f
movq %rbx, %rdi
callq 0x17fb8
movq (%r14), %rax
movq %rax, 0x68(%rbx)
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %r14
movq %r14, (%rdi)
leaq 0x1d635(%rip), %rsi     # 0x35388
leaq 0x1d63c(%rip), %rdx     # 0x35396
callq 0xccac
movq 0x78(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x182c0
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x17d80
callq 0x52a0
cmpq 0x70(%rsp), %rbx
je 0x17d93
leaq 0x1d9c1(%rip), %rdi     # 0x3574f
callq 0x54a0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
jne 0x17c3d
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x17dc0
cmpl $0x3, %eax
jne 0x17ea5
movq 0x20(%rbx), %rsi
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x1337e
movq 0x18(%r12), %r12
leaq 0x40(%rsp), %r13
cmpq %r13, %r12
je 0x17e07
movslq 0x20(%r12), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq %r15, %rsi
callq 0x1ec7a
movq %r12, %rdi
callq 0x5360
movq %rax, %r12
cmpq %r13, %rax
jne 0x17de3
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x134b4
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x1332e
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 0x1ec36
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13730
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 0x1ec36
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x13482
jmp 0x17c27
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x13590
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 0x1ec36
jmp 0x17d1a
leaq 0x1d769(%rip), %rdi     # 0x3565c
leaq 0x1d5c8(%rip), %rsi     # 0x354c2
leaq 0x1d774(%rip), %rcx     # 0x35675
movl $0x11a, %edx            # imm = 0x11A
callq 0x51d0
leaq 0x1ecaa(%rip), %rdi     # 0x36bbc
leaq 0x1d5a9(%rip), %rsi     # 0x354c2
leaq 0x1d755(%rip), %rcx     # 0x35675
movl $0x120, %edx            # imm = 0x120
callq 0x51d0
leaq 0x1d769(%rip), %rdi     # 0x3569a
leaq 0x1d58a(%rip), %rsi     # 0x354c2
leaq 0x1d736(%rip), %rcx     # 0x35675
movl $0x14e, %edx            # imm = 0x14E
callq 0x51d0
leaq 0x1d75c(%rip), %rdi     # 0x356ac
leaq 0x1d56b(%rip), %rsi     # 0x354c2
leaq 0x1d717(%rip), %rcx     # 0x35675
movl $0x153, %edx            # imm = 0x153
callq 0x51d0
jmp 0x17f6c
jmp 0x17f6c
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x138d6
jmp 0x17fa5
jmp 0x17f7f
jmp 0x17f8e
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
jne 0x17fa0
jmp 0x17fa5
movq %rax, %rbx
jmp 0x17fa5
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x17fa5
callq 0x52a0
movq %rbx, %rdi
callq 0x55a0
nop