// File: yiboyang[P]VirtualMemory/PageTable.cpp
// Function: PageTable::operator[](int)
int PageTable::operator[](const int pnum) {
	/*
	Get the number (index) of the frame in physical memory corresponding to given page number (pnum) in logical memory;
	this is done prior to every access to Memory. Page faults are resolved automatically.
	*/
	int fnum;
	pageFault = tlbMiss = false;
	if (tlb.contains(pnum)) // look in TLB first
		fnum = tlb[pnum];
	else { // TLB miss; look in page table
		tlbMiss = true;
		fnum = pt[pnum].first;

		if (fnum == -1) {
			// page fault!
			pageFault = true;
			numPageFaults++;
			// read the requested page from backing store into a free frame
			fnum = getFreeFrameNum();	// somehow find a free frame
			bs.read(pnum);		// read the page at pnum on backing store
			copy(bs.getBuff(), bs.getBuff() + FRAME_SIZE, Memory[fnum]);	// copy into the frame at fnum of physical memory
			pt[pnum].first = fnum;	// point the pnum enty to fnum in page table
		}

		tlb[pnum] = fnum;	// update TLB
	}


	if (pageFault) {
		// with either FIFO or LRU, if a page is newly brought into memory, its index is inserted at tail of queue
		q.push_back(pnum);
	}
	else {
		// in the case of no page fault, FIFO does nothing;
		// LRU will move the recently accessed page number to the tail of queue
		if (pageReplacementPolicy == "LRU") {
			q.remove(pnum);
			q.push_back(pnum);
		}
	}


	return fnum;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x18(%rbp)
movb $0x0, 0x3f8(%rdi)
movb $0x0, 0x3f0(%rdi)
addq $0x3a8, %rdi            # imm = 0x3A8
movl -0xc(%rbp), %esi
callq 0x67e0
cmpl $0x0, %eax
je 0x5fe6
movq -0x18(%rbp), %rdi
addq $0x3a8, %rdi            # imm = 0x3A8
movl -0xc(%rbp), %esi
callq 0x3600
movl (%rax), %eax
movl %eax, -0x10(%rbp)
jmp 0x60bd
movq -0x18(%rbp), %rdi
movb $0x1, 0x3f8(%rdi)
leaq -0xc(%rbp), %rsi
callq 0x64b0
movl (%rax), %eax
movl %eax, -0x10(%rbp)
cmpl $-0x1, -0x10(%rbp)
jne 0x609f
movq -0x18(%rbp), %rdi
movb $0x1, 0x3f0(%rdi)
movl 0x3f4(%rdi), %eax
addl $0x1, %eax
movl %eax, 0x3f4(%rdi)
callq 0x6130
movq -0x18(%rbp), %rdi
movl %eax, -0x10(%rbp)
addq $0x98, %rdi
movl -0xc(%rbp), %esi
callq 0x9430
movq -0x18(%rbp), %rdi
addq $0x98, %rdi
callq 0x6850
movq -0x18(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x98, %rdi
callq 0x6850
movq -0x28(%rbp), %rdi
movq %rax, %rsi
addq $0x100, %rsi            # imm = 0x100
movslq -0x10(%rbp), %rax
leaq 0xb1e5(%rip), %rdx      # 0x11260
shlq $0x8, %rax
addq %rax, %rdx
callq 0x6810
movq -0x18(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0x1c(%rbp)
leaq -0xc(%rbp), %rsi
callq 0x64b0
movl -0x1c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rdi
movl -0x10(%rbp), %eax
movl %eax, -0x2c(%rbp)
addq $0x3a8, %rdi            # imm = 0x3A8
movl -0xc(%rbp), %esi
callq 0x3600
movl -0x2c(%rbp), %ecx
movl %ecx, (%rax)
movq -0x18(%rbp), %rax
testb $0x1, 0x3f0(%rax)
je 0x60e0
movq -0x18(%rbp), %rdi
addq $0x80, %rdi
leaq -0xc(%rbp), %rsi
callq 0x3900
jmp 0x6124
movq -0x18(%rbp), %rdi
addq $0x60, %rdi
leaq 0x4f43(%rip), %rsi      # 0xb032
callq 0x6450
testb $0x1, %al
jne 0x60fa
jmp 0x6122
movq -0x18(%rbp), %rdi
addq $0x80, %rdi
leaq -0xc(%rbp), %rsi
callq 0x3720
movq -0x18(%rbp), %rdi
addq $0x80, %rdi
leaq -0xc(%rbp), %rsi
callq 0x3900
jmp 0x6124
movl -0x10(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)