// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::retire_rob()
void O3_CPU::retire_rob()
{
    for (uint32_t n=0; n<RETIRE_WIDTH; n++) {
        if (ROB.entry[ROB.head].ip == 0)
            return;

        // retire is in-order
        if (ROB.entry[ROB.head].executed != COMPLETED) { 
            DP ( if (warmup_complete[cpu]) {
            cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[ROB.head].instr_id << " head: " << ROB.head << " is not executed yet" << endl; });
            return;
        }

        // check store instruction
        uint32_t num_store = 0;
        for (uint32_t i=0; i<MAX_INSTR_DESTINATIONS; i++) {
            if (ROB.entry[ROB.head].destination_memory[i])
                num_store++;
        }

        if (num_store) {
            if ((L1D.WQ.occupancy + num_store) <= L1D.WQ.SIZE) {
                for (uint32_t i=0; i<MAX_INSTR_DESTINATIONS; i++) {
                    if (ROB.entry[ROB.head].destination_memory[i]) {

                        PACKET data_packet;
                        uint32_t sq_index = ROB.entry[ROB.head].sq_index[i];

                        // sq_index and rob_index are no longer available after retirement
                        // but we pass this information to avoid segmentation fault
                        data_packet.fill_level = FILL_L1;
                        data_packet.cpu = cpu;
                        data_packet.data_index = SQ.entry[sq_index].data_index;
                        data_packet.sq_index = sq_index;
                        data_packet.address = SQ.entry[sq_index].physical_address >> LOG2_BLOCK_SIZE;
                        data_packet.full_addr = SQ.entry[sq_index].physical_address;
                        data_packet.instr_id = SQ.entry[sq_index].instr_id;
                        data_packet.rob_index = SQ.entry[sq_index].rob_index;
                        data_packet.ip = SQ.entry[sq_index].ip;
                        data_packet.type = RFO;
                        data_packet.asid[0] = SQ.entry[sq_index].asid[0];
                        data_packet.asid[1] = SQ.entry[sq_index].asid[1];
                        data_packet.event_cycle = current_core_cycle[cpu];

                        L1D.add_wq(&data_packet);
                    }
                }
            }
            else {
                DP ( if (warmup_complete[cpu]) {
                cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[ROB.head].instr_id << " L1D WQ is full" << endl; });

                L1D.WQ.FULL++;
                L1D.STALL[RFO]++;

                return;
            }
        }

        // release SQ entries
        for (uint32_t i=0; i<MAX_INSTR_DESTINATIONS; i++) {
            if (ROB.entry[ROB.head].sq_index[i] != UINT32_MAX) {
                uint32_t sq_index = ROB.entry[ROB.head].sq_index[i];

                DP ( if (warmup_complete[cpu]) {
                cout << "[SQ] " << __func__ << " instr_id: " << ROB.entry[ROB.head].instr_id << " releases sq_index: " << sq_index;
                cout << hex << " address: " << (SQ.entry[sq_index].physical_address>>LOG2_BLOCK_SIZE);
                cout << " full_addr: " << SQ.entry[sq_index].physical_address << dec << endl; });

                LSQ_ENTRY empty_entry;
                memcpy (&SQ.entry[sq_index], &empty_entry, sizeof(LSQ_ENTRY));
                
                SQ.occupancy--;
                SQ.head++;
                if (SQ.head == SQ.SIZE)
                    SQ.head = 0;
            }
        }

        // release ROB entry
        DP ( if (warmup_complete[cpu]) {
        cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[ROB.head].instr_id << " is retired" << endl; });

        ooo_model_instr empty_entry;
        memcpy(&ROB.entry[ROB.head], &empty_entry, sizeof(ooo_model_instr));

        ROB.head++;
        if (ROB.head == ROB.SIZE)
            ROB.head = 0;
        ROB.occupancy--;
        completed_executions--;
        num_retired++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x768, %rsp            # imm = 0x768
movq %rdi, %rbx
leaq 0x44530(%rdi), %rax
movq %rax, 0x8(%rsp)
leaq 0x18(%rsp), %r13
movl 0x958(%rdi), %eax
xorl %ebp, %ebp
leaq 0xa692(%rip), %r15      # 0x1a210
xorl %r12d, %r12d
movq 0x9b8(%rbx), %rcx
movl %eax, %eax
imulq $0x758, %rax, %rdx      # imm = 0x758
cmpq $0x0, 0x8(%rcx,%rdx)
je 0xfe93
leaq (%rcx,%rdx), %rax
cmpl $0x2, 0x78(%rax)
jne 0xfe93
movzbl (%r15), %eax
testq %rax, %rax
je 0xfe1d
addq %rcx, %rdx
addq $0x5a8, %rdx            # imm = 0x5A8
xorl %esi, %esi
xorl %ecx, %ecx
cmpq $0x1, (%rdx,%rsi,8)
sbbl $-0x1, %ecx
incq %rsi
cmpq %rsi, %rax
jne 0xfbc6
testl %ecx, %ecx
je 0xfd67
addl 0x4b90c(%rbx), %ecx
cmpl 0x4b8f8(%rbx), %ecx
ja 0xfe85
testb %al, %al
je 0xfe1d
xorl %r14d, %r14d
movl 0x958(%rbx), %ecx
imulq $0x758, %rcx, %r15      # imm = 0x758
addq 0x9b8(%rbx), %r15
cmpq $0x0, 0x5a8(%r15,%r14,8)
je 0xfd58
movw $0xff00, 0x33c(%rsp)    # imm = 0xFF00
movl %ebp, 0x10(%rsp)
movl %ebp, 0x13(%rsp)
movb $-0x1, 0x33e(%rsp)
pcmpeqd %xmm0, %xmm0
movdqu %xmm0, (%r13)
movl $0x314, %edx            # imm = 0x314
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x3130
movl $0x1, %ecx
movl %ecx, 0x340(%rsp)
pxor %xmm0, %xmm0
movdqu %xmm0, 0x32c(%r13)
movdqu %xmm0, 0x33c(%r13)
movl %ebp, 0x34c(%r13)
movdqu %xmm0, 0x358(%r13)
movq $0x0, 0x368(%r13)
movq $-0x1, 0x388(%rsp)
movl 0x738(%r15,%r14,4), %eax
movl %ecx, 0x18(%rsp)
movl (%rbx), %ecx
movl %ecx, 0x340(%rsp)
movq 0xa28(%rbx), %rdx
leaq (%rax,%rax,4), %rsi
shlq $0x6, %rsi
movl 0x34(%rdx,%rsi), %edi
movl %edi, 0x344(%rsp)
movl %eax, 0x34c(%rsp)
movq 0x18(%rdx,%rsi), %rax
movq %rax, %rdi
shrq $0x6, %rdi
movq %rdi, 0x350(%rsp)
movq %rax, 0x358(%rsp)
movq (%rdx,%rsi), %rax
movq %rax, 0x378(%rsp)
movl 0x30(%rdx,%rsi), %eax
movl %eax, 0x20(%rsp)
movq 0x20(%rdx,%rsi), %rax
movq %rax, 0x380(%rsp)
movb $0x1, 0x33f(%rsp)
movb 0x3e(%rdx,%rsi), %al
movb %al, 0x33d(%rsp)
movb 0x3f(%rdx,%rsi), %al
movb %al, 0x33e(%rsp)
leaq 0x7118c(%rip), %rax     # 0x80ec0
movq (%rax,%rcx,8), %rax
movq %rax, 0x388(%rsp)
movq 0x8(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0x5ea8
leaq 0xa4ba(%rip), %rax      # 0x1a210
movb (%rax), %al
incq %r14
movzbl %al, %ecx
cmpq %rcx, %r14
jb 0xfbfb
testb %al, %al
leaq 0xa4a0(%rip), %r15      # 0x1a210
je 0xfe1d
xorl %r14d, %r14d
movl 0x958(%rbx), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq 0x9b8(%rbx), %rcx
movl 0x738(%rcx,%r14,4), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0xfe0e
movq 0xa28(%rbx), %rax
leaq (%rcx,%rcx,4), %rcx
shlq $0x6, %rcx
leaq (%rax,%rcx), %rdi
addq $0x40, %rdi
movq %rbp, -0x40(%rdi)
movq $-0x1, -0x38(%rdi)
pxor %xmm0, %xmm0
movdqu %xmm0, -0x30(%rdi)
movdqu %xmm0, -0x20(%rdi)
movq %rbp, -0x10(%rdi)
movabsq $-0xffff00000001, %rax  # imm = 0xFFFF0000FFFFFFFF
movq %rax, -0x8(%rdi)
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
callq 0x3130
decl 0xa1c(%rbx)
movl 0xa20(%rbx), %eax
incl %eax
cmpl 0xa18(%rbx), %eax
cmovel %ebp, %eax
movl %eax, 0xa20(%rbx)
movb (%r15), %al
incq %r14
movzbl %al, %ecx
cmpq %rcx, %r14
jb 0xfd79
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0x10550
movl 0x958(%rbx), %eax
imulq $0x758, %rax, %rdi      # imm = 0x758
addq 0x9b8(%rbx), %rdi
movl $0x758, %edx            # imm = 0x758
movq %r14, %rsi
callq 0x31c0
movl 0x958(%rbx), %eax
incl %eax
cmpl 0x950(%rbx), %eax
cmovel %ebp, %eax
movl %eax, 0x958(%rbx)
decl 0x960(%rbx)
decq 0x8b8(%rbx)
incq 0x918(%rbx)
incl %r12d
cmpl $0x4, %r12d
jne 0xfb81
jmp 0xfe93
incq 0x4b968(%rbx)
incq 0x4b860(%rbx)
addq $0x768, %rsp            # imm = 0x768
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop