// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::add_load_queue(unsigned int, unsigned int)
void O3_CPU::add_load_queue(uint32_t rob_index, uint32_t data_index)
{
    // search for an empty slot 
    uint32_t lq_index = LQ.SIZE;
    for (uint32_t i=0; i<LQ.SIZE; i++) {
        if (LQ.entry[i].virtual_address == 0) {
            lq_index = i;
            break;
        }
    }

    // sanity check
    if (lq_index == LQ.SIZE) {
        cerr << "instr_id: " << ROB.entry[rob_index].instr_id << " no empty slot in the load queue!!!" << endl;
        assert(0);
    }

    // add it to the load queue
    ROB.entry[rob_index].lq_index[data_index] = lq_index;
    LQ.entry[lq_index].instr_id = ROB.entry[rob_index].instr_id;
    LQ.entry[lq_index].virtual_address = ROB.entry[rob_index].source_memory[data_index];
    LQ.entry[lq_index].ip = ROB.entry[rob_index].ip;
    LQ.entry[lq_index].data_index = data_index;
    LQ.entry[lq_index].rob_index = rob_index;
    LQ.entry[lq_index].asid[0] = ROB.entry[rob_index].asid[0];
    LQ.entry[lq_index].asid[1] = ROB.entry[rob_index].asid[1];
    LQ.entry[lq_index].event_cycle = current_core_cycle[cpu] + SCHEDULING_LATENCY;
    LQ.occupancy++;

    // check RAW dependency
    int prior = rob_index - 1;
    if (prior < 0)
        prior = ROB.SIZE - 1;

    if (rob_index != ROB.head) {
        if ((int)ROB.head <= prior) {
            for (int i=prior; i>=(int)ROB.head; i--) {
                if (LQ.entry[lq_index].producer_id != UINT64_MAX)
                    break;

                    mem_RAW_dependency(i, rob_index, data_index, lq_index);
            }
        }
        else {
            for (int i=prior; i>=0; i--) {
                if (LQ.entry[lq_index].producer_id != UINT64_MAX)
                    break;

                    mem_RAW_dependency(i, rob_index, data_index, lq_index);
            }
            for (int i=ROB.SIZE-1; i>=(int)ROB.head; i--) { 
                if (LQ.entry[lq_index].producer_id != UINT64_MAX)
                    break;

                    mem_RAW_dependency(i, rob_index, data_index, lq_index);
            }
        }
    }

    // check
    // 1) if store-to-load forwarding is possible
    // 2) if there is WAR that are not correctly executed
    uint32_t forwarding_index = SQ.SIZE;
    for (uint32_t i=0; i<SQ.SIZE; i++) {

        // skip empty slot
        if (SQ.entry[i].virtual_address == 0)
            continue;

        // forwarding should be done by the SQ entry that holds the same producer_id from RAW dependency check
        if (SQ.entry[i].virtual_address == LQ.entry[lq_index].virtual_address) { // store-to-load forwarding check

            // forwarding store is in the SQ
            if ((rob_index != ROB.head) && (LQ.entry[lq_index].producer_id == SQ.entry[i].instr_id)) { // RAW
                forwarding_index = i;
                break; // should be break
            }

            if ((LQ.entry[lq_index].producer_id == UINT64_MAX) && (LQ.entry[lq_index].instr_id <= SQ.entry[i].instr_id)) { // WAR 
                // a load is about to be added in the load queue and we found a store that is 
                // "logically later in the program order but already executed" => this is not correctly executed WAR
                // due to out-of-order execution, this case is possible, for example
                // 1) application is load intensive and load queue is full
                // 2) we have loads that can't be added in the load queue
                // 3) subsequent stores logically behind in the program order are added in the store queue first

                // thanks to the store buffer, data is not written back to the memory system until retirement
                // also due to in-order retirement, this "already executed store" cannot be retired until we finish the prior load instruction 
                // if we detect WAR when a load is added in the load queue, just let the load instruction to access the memory system
                // no need to mark any dependency because this is actually WAR not RAW

                // do not forward data from the store queue since this is WAR
                // just read correct data from data cache

                LQ.entry[lq_index].physical_address = 0;
                LQ.entry[lq_index].translated = 0;
                LQ.entry[lq_index].fetched = 0;
                
                DP(if(warmup_complete[cpu]) {
                cout << "[LQ] " << __func__ << " instr_id: " << LQ.entry[lq_index].instr_id << " reset fetched: " << +LQ.entry[lq_index].fetched;
                cout << " to obey WAR store instr_id: " << SQ.entry[i].instr_id << " cycle: " << current_core_cycle[cpu] << endl; });
            }
        }
    }

    if (forwarding_index != SQ.SIZE) { // we have a store-to-load forwarding

        if ((SQ.entry[forwarding_index].fetched == COMPLETED) && (SQ.entry[forwarding_index].event_cycle <= current_core_cycle[cpu])) {
            LQ.entry[lq_index].physical_address = (SQ.entry[forwarding_index].physical_address & ~(uint64_t) ((1 << LOG2_BLOCK_SIZE) - 1)) | (LQ.entry[lq_index].virtual_address & ((1 << LOG2_BLOCK_SIZE) - 1));
            LQ.entry[lq_index].translated = COMPLETED;
            LQ.entry[lq_index].fetched = COMPLETED;

            uint32_t fwr_rob_index = LQ.entry[lq_index].rob_index;
            ROB.entry[fwr_rob_index].num_mem_ops--;
            ROB.entry[fwr_rob_index].event_cycle = current_core_cycle[cpu];
            if (ROB.entry[fwr_rob_index].num_mem_ops < 0) {
                cerr << "instr_id: " << ROB.entry[fwr_rob_index].instr_id << endl;
                assert(0);
            }
            if (ROB.entry[fwr_rob_index].num_mem_ops == 0)
                inflight_mem_executions++;

            DP(if(warmup_complete[cpu]) {
            cout << "[LQ] " << __func__ << " instr_id: " << LQ.entry[lq_index].instr_id << hex;
            cout << " full_addr: " << LQ.entry[lq_index].physical_address << dec << " is forwarded by store instr_id: ";
            cout << SQ.entry[forwarding_index].instr_id << " remain_num_ops: " << ROB.entry[fwr_rob_index].num_mem_ops << " cycle: " << current_core_cycle[cpu] << endl; });

            release_load_queue(lq_index);
        }
        else
            ; // store is not executed yet, forwarding will be handled by execute_store()
    }

    // succesfully added to the load queue
    ROB.entry[rob_index].source_added[data_index] = 1;

    if (LQ.entry[lq_index].virtual_address && (LQ.entry[lq_index].producer_id == UINT64_MAX)) { // not released and no forwarding
        RTL0[RTL0_tail] = lq_index;
        RTL0_tail++;
        if (RTL0_tail == LQ_SIZE)
            RTL0_tail = 0;

        DP (if (warmup_complete[cpu]) {
        cout << "[RTL0] " << __func__ << " instr_id: " << LQ.entry[lq_index].instr_id << " rob_index: " << LQ.entry[lq_index].rob_index << " is added to RTL0";
        cout << " head: " << RTL0_head << " tail: " << RTL0_tail << endl; }); 
    }

    DP(if(warmup_complete[cpu]) {
    cout << "[LQ] " << __func__ << " instr_id: " << LQ.entry[lq_index].instr_id;
    cout << " is added in the LQ address: " << hex << LQ.entry[lq_index].virtual_address << dec << " translated: " << +LQ.entry[lq_index].translated;
    cout << " fetched: " << +LQ.entry[lq_index].fetched << " index: " << lq_index << " occupancy: " << LQ.occupancy << " cycle: " << current_core_cycle[cpu] << endl; });
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r15d
movq %rdi, %rbx
movl 0x9e0(%rdi), %ecx
testq %rcx, %rcx
je 0xe35a
movq 0x9f0(%rbx), %rax
shlq $0x6, %rcx
leaq (%rcx,%rcx,4), %rcx
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq $0x0, 0x10(%rax,%rbp)
je 0xe3c0
addq $0x140, %rbp            # imm = 0x140
incl %r14d
cmpq %rbp, %rcx
jne 0xe343
movq 0xbc8f(%rip), %rdi      # 0x19ff0
leaq 0x6be9(%rip), %rsi      # 0x14f51
callq 0x3250
movq 0x9b8(%rbx), %rcx
movl %r15d, %edx
imulq $0x758, %rdx, %rdx      # imm = 0x758
movq (%rcx,%rdx), %rsi
movq %rax, %rdi
callq 0x3090
leaq 0x6ac1(%rip), %rsi      # 0x14e52
movq %rax, %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3110
leaq 0x5cbf(%rip), %rdi      # 0x14067
leaq 0x6937(%rip), %rsi      # 0x14ce6
leaq 0x6ac0(%rip), %rcx      # 0x14e76
movl $0x369, %edx            # imm = 0x369
callq 0x3160
movq 0x9b8(%rbx), %rcx
movl %r15d, %esi
movl %edx, %edi
imulq $0x758, %rsi, %r8       # imm = 0x758
leaq (%rcx,%r8), %r9
movl %r14d, 0x728(%r9,%rdi,4)
movq (%rcx,%r8), %rsi
movq %rsi, (%rax,%rbp)
movq %rdi, 0x10(%rsp)
movq 0x5c8(%r9,%rdi,8), %rsi
movq %rsi, 0x10(%rax,%rbp)
movq 0x8(%rcx,%r8), %rsi
movq %rsi, 0x20(%rax,%rbp)
movl %edx, 0x4(%rsp)
movl %edx, 0x34(%rax,%rbp)
movl %r15d, 0x30(%rax,%rbp)
movb 0x5b(%rcx,%r8), %cl
movb %cl, 0x3e(%rax,%rbp)
movq 0x9b8(%rbx), %rax
movq 0x9f0(%rbx), %rcx
movq %r8, 0x8(%rsp)
movb 0x5c(%rax,%r8), %al
movb %al, 0x3f(%rcx,%rbp)
movl (%rbx), %eax
leaq 0x72a82(%rip), %rdx     # 0x80ec0
movl 0x72a8c(%rip), %ecx     # 0x80ed0
addq (%rdx,%rax,8), %rcx
movq 0x9f0(%rbx), %rax
movq %rcx, 0x28(%rax,%rbp)
incl 0x9e4(%rbx)
movl 0x958(%rbx), %ecx
movl %r15d, %eax
cmpl %r15d, %ecx
je 0xe568
leal -0x1(%r15), %r12d
movl 0x950(%rbx), %r13d
decl %r13d
testl %r12d, %r12d
cmovsl %r13d, %r12d
cmpl %r12d, %ecx
jle 0xe4dc
testl %r12d, %r12d
js 0xe4d0
movl 0x4(%rsp), %r13d
movq 0x9f0(%rbx), %rax
cmpq $-0x1, 0x8(%rax,%rbp)
jne 0xe4c0
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %edx
movl %r13d, %ecx
movl %r14d, %r8d
callq 0xe8c0
leal -0x1(%r12), %eax
testl %r12d, %r12d
movl %eax, %r12d
jg 0xe490
movl 0x950(%rbx), %r13d
movl 0x958(%rbx), %ecx
decl %r13d
cmpl %ecx, %r13d
jge 0xe523
movl %ecx, %eax
jmp 0xe568
movq 0x9f0(%rbx), %rax
cmpq $-0x1, 0x8(%rax,%rbp)
movl %ecx, %eax
jne 0xe568
jmp 0xe501
decl %r12d
movq 0x9f0(%rbx), %rcx
cmpq $-0x1, 0x8(%rcx,%rbp)
jne 0xe568
movq %rbx, %rdi
movl %r12d, %esi
movl %r15d, %edx
movl 0x4(%rsp), %ecx
movl %r14d, %r8d
callq 0xe8c0
movl 0x958(%rbx), %eax
cmpl %eax, %r12d
jg 0xe4ef
jmp 0xe568
movq 0x9f0(%rbx), %rax
cmpq $-0x1, 0x8(%rax,%rbp)
movl %ecx, %eax
jne 0xe568
jmp 0xe545
movq 0x9f0(%rbx), %rcx
cmpq $-0x1, 0x8(%rcx,%rbp)
jne 0xe568
movq %rbx, %rdi
movl %r13d, %esi
movl %r15d, %edx
movl 0x4(%rsp), %ecx
movl %r14d, %r8d
callq 0xe8c0
decl %r13d
movl 0x958(%rbx), %eax
cmpl %eax, %r13d
jge 0xe536
movl 0xa18(%rbx), %edx
testq %rdx, %rdx
je 0xe5dc
movq 0x9f0(%rbx), %rsi
movq 0xa28(%rbx), %rcx
shlq $0x6, %rdx
leaq (%rdx,%rdx,4), %rdi
xorl %edx, %edx
movq 0x10(%rcx,%rdx), %r8
testq %r8, %r8
je 0xe5d0
cmpq 0x10(%rsi,%rbp), %r8
jne 0xe5d0
movq 0x8(%rsi,%rbp), %r8
cmpl %r15d, %eax
je 0xe5b0
cmpq (%rcx,%rdx), %r8
je 0xe63c
cmpq $-0x1, %r8
jne 0xe5d0
movq (%rsi,%rbp), %r8
cmpq (%rcx,%rdx), %r8
ja 0xe5d0
movq $0x0, 0x18(%rsi,%rbp)
movw $0x0, 0x3c(%rsi,%rbp)
addq $0x140, %rdx            # imm = 0x140
cmpq %rdx, %rdi
jne 0xe58b
movq 0x8(%rsp), %rcx
addq 0x9b8(%rbx), %rcx
movq 0x10(%rsp), %rax
movb $0x1, 0x4e(%rax,%rcx)
movq 0x9f0(%rbx), %rax
cmpq $0x0, 0x10(%rax,%rbp)
je 0xe62d
cmpq $-0x1, 0x8(%rax,%rbp)
jne 0xe62d
movl 0x3374(%rbx), %eax
movl %r14d, 0x3250(%rbx,%rax,4)
movl 0x3374(%rbx), %eax
incl %eax
xorl %ecx, %ecx
cmpl $0x48, %eax
cmovnel %eax, %ecx
movl %ecx, 0x3374(%rbx)
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpb $0x2, 0x3d(%rcx,%rdx)
jne 0xe5dc
movq 0x28(%rcx,%rdx), %rax
movl (%rbx), %esi
leaq 0x7286f(%rip), %rdi     # 0x80ec0
cmpq (%rdi,%rsi,8), %rax
ja 0xe5dc
movq 0x18(%rcx,%rdx), %rcx
andq $-0x40, %rcx
movq 0x9f0(%rbx), %rax
movq 0x10(%rax,%rbp), %rdx
andl $0x3f, %edx
orq %rcx, %rdx
movq %rdx, 0x18(%rax,%rbp)
movw $0x202, 0x3c(%rax,%rbp) # imm = 0x202
movl 0x30(%rax,%rbp), %ecx
movq 0x9b8(%rbx), %rdi
imulq $0x758, %rcx, %r15      # imm = 0x758
movl 0x70(%rdi,%r15), %ecx
leal -0x1(%rcx), %edx
movl %edx, 0x70(%rdi,%r15)
leaq 0x7281c(%rip), %r8      # 0x80ec0
movq (%r8,%rsi,8), %rsi
movq %rsi, 0x40(%rdi,%r15)
testl %ecx, %ecx
jle 0xe70c
leaq (%rax,%rbp), %rcx
addq $0x10, %rcx
testl %edx, %edx
jne 0xe6c3
incl 0x924(%rbx)
xorl %edx, %edx
leaq (%rax,%rbp), %rdi
addq $0x40, %rdi
movq %rdx, -0x40(%rdi)
movq $-0x1, -0x38(%rdi)
movq %rdx, 0x20(%rcx)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rcx)
movups %xmm0, (%rcx)
movabsq $-0xffff00000001, %rax  # imm = 0xFFFF0000FFFFFFFF
movq %rax, -0x8(%rdi)
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3130
decl 0x9e4(%rbx)
jmp 0xe5dc
movq 0xb8dd(%rip), %rdi      # 0x19ff0
leaq 0x6837(%rip), %rsi      # 0x14f51
callq 0x3250
movq 0x9b8(%rbx), %rcx
movq (%rcx,%r15), %rsi
movq %rax, %rdi
callq 0x3090
movq %rax, %rdi
callq 0x3110
leaq 0x5926(%rip), %rdi      # 0x14067
leaq 0x659e(%rip), %rsi      # 0x14ce6
leaq 0x6727(%rip), %rcx      # 0x14e76
movl $0x3d0, %edx            # imm = 0x3D0
callq 0x3160
nop