// File: /workspace/github_cpp_makefile_star2_O3/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 0x2e38e
movq %rax, %r15
leaq 0x48(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1dd0e(%rip), %rsi     # 0x35358
leaq 0x1dd15(%rip), %rdx     # 0x35366
leaq 0x38(%rsp), %rdi
callq 0xcc6c
leaq 0x2e8ee(%rip), %r12     # 0x45f50
leaq 0x8(%r12), %rdi
leaq 0x38(%rsp), %rsi
movq %rdi, 0x78(%rsp)
callq 0x1800c
movq %rax, %r13
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x17688
callq 0x52b0
leaq 0xf0(%rsp), %r14
leaq 0x10(%r12), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, %r13
je 0x176c7
movl 0x1c(%r14), %eax
movl 0x20(%r14), %ebp
leaq 0x2dc82(%rip), %rcx     # 0x45330
movq (%rcx,%rax,8), %rdx
leaq 0x1df3a(%rip), %rdi     # 0x355f3
movq %r15, %rsi
movl %ebp, %ecx
xorl %eax, %eax
callq 0x5050
jmp 0x176cb
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 0x17c34
movl %edx, %ebp
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13184
testq %rax, %rax
jne 0x17716
movq 0x20(%rbx), %rdi
xorl %edx, %edx
cmpl $0x2, 0x1c(%r14)
setb %dl
movq %r15, %rsi
callq 0x13074
testq %rax, %rax
je 0x17c53
movq %rax, %r13
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x13184
movl 0x38(%rax), %r12d
movq %r13, %rdi
callq 0x12f08
cmpl $0x8, %r12d
jne 0x177ef
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x13264
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 0x1eb00
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 0x1eb00
xorl %ecx, %ecx
cmpl $0x2, %eax
sete %cl
movq 0x20(%rbx), %rdi
leal (%rcx,%rcx,2), %edx
addl $0x2, %edx
movq %r15, %rsi
callq 0x133aa
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x1365e
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x177e3
cmpl $0x3, %eax
jne 0x1797f
incq 0x88(%rbx)
jmp 0x17986
movl %r12d, %eax
andl $-0x2, %eax
cmpl $0x6, %eax
jne 0x178b7
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x13264
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 0x1eb00
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 0x1eb00
movq 0x18(%rbx), %rdi
movq 0x68(%rsp), %r12
movq %r12, %rsi
callq 0x1eddc
movzbl %al, %eax
addq %rax, 0x98(%rbx)
movq 0x18(%rbx), %rdi
movq %r12, %rsi
callq 0x1edd4
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x133aa
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x1365e
jmp 0x17986
cmpl $0x5, %r12d
ja 0x17986
movl $0x36, %eax
btl %r12d, %eax
jae 0x17aed
movq 0x20(%rbx), %rsi
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
callq 0x132b4
cmpq $0x1, 0x28(%r13)
jne 0x17c72
movq 0x50(%rsp), %rax
cmpl %ebp, 0x20(%rax)
jne 0x17c91
movl 0x1c(%r14), %eax
cmpl $0x3, %eax
sete %cl
cmpl $0x6, %eax
sete %al
orb %cl, %al
cmpb $0x1, %al
jne 0x1791e
movl $0x4, %edx
cmpl $0x5, %r12d
je 0x17930
cmpl $0x2, %r12d
sete %cl
andb %cl, %al
movl $0x1, %edx
cmpb $0x1, %al
jne 0x1793c
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x133aa
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 0x1eb00
leaq 0x38(%rsp), %rdi
callq 0x1380a
jmp 0x17a62
incq 0x80(%rbx)
leaq 0x2e5c3(%rip), %rax     # 0x45f50
cmpb $0x1, 0x8b(%rax)
jne 0x17a62
movq 0x28(%rbx), %rsi
movq (%rsi), %rax
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq 0x68(%rsp), %rdx
callq *(%rax)
movq (%r13), %r15
movq 0x8(%r13), %r12
cmpq %r12, %r15
je 0x17a55
xorl %eax, %eax
cmpl $0x2, 0x1c(%r14)
setb %al
leal (%rax,%rax,2), %ebp
incl %ebp
movq (%r15), %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 0x17410
addq $0x8, %r15
cmpq %r12, %r15
jne 0x179d0
movq 0x38(%rsp), %r15
testq %r15, %r15
je 0x17a62
movq %r15, %rdi
callq 0x52b0
leaq 0x2e4af(%rip), %r14     # 0x45f18
movq (%r14), %rax
subq 0x68(%rbx), %rax
cmpq $0x186a1, %rax          # imm = 0x186A1
jb 0x17a87
movq %rbx, %rdi
callq 0x17d00
movq (%r14), %rax
movq %rax, 0x68(%rbx)
leaq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %r14
movq %r14, (%rdi)
leaq 0x1d8bd(%rip), %rsi     # 0x35358
leaq 0x1d8c4(%rip), %rdx     # 0x35366
callq 0xcc6c
leaq 0x38(%rsp), %rsi
movq 0x78(%rsp), %rdi
callq 0x1800c
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
je 0x17ac8
callq 0x52b0
cmpq 0x70(%rsp), %rbx
je 0x17adb
leaq 0x1dc49(%rip), %rdi     # 0x3571f
callq 0x54a0
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r12d, %r12d
jne 0x17986
movl 0x1c(%r14), %eax
cmpl $0x6, %eax
je 0x17b08
cmpl $0x3, %eax
jne 0x17bed
movq 0x20(%rbx), %rsi
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %r15, %rdx
callq 0x132b4
movq 0x18(%r12), %r12
leaq 0x40(%rsp), %r13
cmpq %r13, %r12
je 0x17b4f
movslq 0x20(%r12), %rax
movq (%rbx), %rcx
movq (%rcx,%rax,8), %rdi
movq %r15, %rsi
callq 0x1eb44
movq %r12, %rdi
callq 0x5370
movq %rax, %r12
cmpq %r13, %rax
jne 0x17b2b
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x133dc
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x13264
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 0x1eb00
movq 0x20(%rbx), %rdi
movq %r15, %rsi
callq 0x1365e
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 0x1eb00
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl $0x2, %edx
callq 0x133aa
jmp 0x17970
movq 0x20(%rbx), %rdi
movq %r15, %rsi
movl %ebp, %edx
callq 0x134be
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 0x1eb00
jmp 0x17a62
leaq 0x1d9f1(%rip), %rdi     # 0x3562c
leaq 0x1d850(%rip), %rsi     # 0x35492
leaq 0x1d9fc(%rip), %rcx     # 0x35645
movl $0x11a, %edx            # imm = 0x11A
callq 0x51d0
leaq 0x1ef5a(%rip), %rdi     # 0x36bb4
leaq 0x1d831(%rip), %rsi     # 0x35492
leaq 0x1d9dd(%rip), %rcx     # 0x35645
movl $0x120, %edx            # imm = 0x120
callq 0x51d0
leaq 0x1d9f1(%rip), %rdi     # 0x3566a
leaq 0x1d812(%rip), %rsi     # 0x35492
leaq 0x1d9be(%rip), %rcx     # 0x35645
movl $0x14e, %edx            # imm = 0x14E
callq 0x51d0
leaq 0x1d9e4(%rip), %rdi     # 0x3567c
leaq 0x1d7f3(%rip), %rsi     # 0x35492
leaq 0x1d99f(%rip), %rcx     # 0x35645
movl $0x153, %edx            # imm = 0x153
callq 0x51d0
jmp 0x17cb4
jmp 0x17cb4
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x1380a
jmp 0x17ced
jmp 0x17cc7
jmp 0x17cd6
movq %rax, %rbx
movq 0x38(%rsp), %rdi
cmpq %r14, %rdi
jne 0x17ce8
jmp 0x17ced
movq %rax, %rbx
jmp 0x17ced
movq %rax, %rbx
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x17ced
callq 0x52b0
movq %rbx, %rdi
callq 0x55a0
nop