// File: /workspace/github_cpp_makefile_star2_O3/NishadSaraf[P]Championship-Simulator/src/ooo_cpu.cc
// Function: O3_CPU::reg_dependency(unsigned int)
void O3_CPU::reg_dependency(uint32_t rob_index)
{
    // print out source/destination registers
    DP (if (warmup_complete[cpu]) {
    for (uint32_t i=0; i<NUM_INSTR_SOURCES; i++) {
        if (ROB.entry[rob_index].source_registers[i]) {
            cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id << " is_memory: " << +ROB.entry[rob_index].is_memory;
            cout << " load  reg_index: " << +ROB.entry[rob_index].source_registers[i] << endl;
        }
    }
    for (uint32_t i=0; i<MAX_INSTR_DESTINATIONS; i++) {
        if (ROB.entry[rob_index].destination_registers[i]) {
            cout << "[ROB] " << __func__ << " instr_id: " << ROB.entry[rob_index].instr_id << " is_memory: " << +ROB.entry[rob_index].is_memory;
            cout << " store reg_index: " << +ROB.entry[rob_index].destination_registers[i] << endl;
        }
    } }); 

    // 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 (ROB.entry[i].destination_registers[0] && (ROB.entry[i].executed != COMPLETED)) {
                if (ROB.entry[i].executed != COMPLETED) {
                    for (uint32_t j=0; j<NUM_INSTR_SOURCES; j++) {
                        if (ROB.entry[rob_index].source_registers[j] && (ROB.entry[rob_index].reg_RAW_checked[j] == 0))
                            reg_RAW_dependency(i, rob_index, j);
                    }
                }
            }
        }
        else {
            for (int i=prior; i>=0; i--) {
                //if (ROB.entry[i].destination_registers[0] && (ROB.entry[i].executed != COMPLETED)) {
                if (ROB.entry[i].executed != COMPLETED) {
                    for (uint32_t j=0; j<NUM_INSTR_SOURCES; j++) {
                        if (ROB.entry[rob_index].source_registers[j] && (ROB.entry[rob_index].reg_RAW_checked[j] == 0))
                            reg_RAW_dependency(i, rob_index, j);
                    }
                }
            }
            for (int i=ROB.SIZE-1; i>=(int)ROB.head; i--) {
                //if (ROB.entry[i].destination_registers[0] && (ROB.entry[i].executed != COMPLETED)) {
                if (ROB.entry[i].executed != COMPLETED) {
                    for (uint32_t j=0; j<NUM_INSTR_SOURCES; j++) {
                        if (ROB.entry[rob_index].source_registers[j] && (ROB.entry[rob_index].reg_RAW_checked[j] == 0))
                            reg_RAW_dependency(i, rob_index, j);
                    }
                }
            }
        }
    }
}

// Assembly:
movl 0x958(%rdi), %eax
cmpl %esi, %eax
je 0xd846
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
leal -0x1(%rbx), %ecx
movl 0x950(%rdi), %ebp
decl %ebp
testl %ecx, %ecx
cmovsl %ebp, %ecx
cmpl %ecx, %eax
jle 0xd7c8
testl %ecx, %ecx
js 0xd758
movl %ebx, %eax
movl %ecx, %r15d
imulq $0x758, %rax, %r13      # imm = 0x758
subq $-0x80, %r13
movq 0x9b8(%r14), %rax
imulq $0x758, %r15, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
je 0xd73c
xorl %r12d, %r12d
movq 0x9b8(%r14), %rax
addq %r13, %rax
cmpb $0x0, (%r12,%rax)
je 0xd733
cmpb $0x0, -0x23(%r12,%rax)
jne 0xd733
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xd848
incq %r12
cmpq $0x4, %r12
jne 0xd70a
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0xd6f2
movl 0x950(%r14), %ebp
movl 0x958(%r14), %eax
decl %ebp
cmpl %eax, %ebp
jl 0xd838
movl %ebx, %ecx
imulq $0x758, %rcx, %r12      # imm = 0x758
subq $-0x80, %r12
movq 0x9b8(%r14), %rcx
movslq %ebp, %rdx
imulq $0x758, %rdx, %rdx      # imm = 0x758
cmpl $0x2, 0x78(%rcx,%rdx)
je 0xd7c0
xorl %r15d, %r15d
movq 0x9b8(%r14), %rax
addq %r12, %rax
cmpb $0x0, (%r15,%rax)
je 0xd7b0
cmpb $0x0, -0x23(%r15,%rax)
jne 0xd7b0
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r15d, %ecx
callq 0xd848
incq %r15
cmpq $0x4, %r15
jne 0xd788
movl 0x958(%r14), %eax
decl %ebp
cmpl %eax, %ebp
jge 0xd76d
jmp 0xd838
movl %ebx, %edx
movslq %ecx, %r15
imulq $0x758, %rdx, %r13      # imm = 0x758
subq $-0x80, %r13
movq 0x9b8(%r14), %rcx
imulq $0x758, %r15, %rdx      # imm = 0x758
cmpl $0x2, 0x78(%rcx,%rdx)
je 0xd829
xorl %r12d, %r12d
movq 0x9b8(%r14), %rax
addq %r13, %rax
cmpb $0x0, (%r12,%rax)
je 0xd819
cmpb $0x0, -0x23(%r12,%rax)
jne 0xd819
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xd848
incq %r12
cmpq $0x4, %r12
jne 0xd7f0
movl 0x958(%r14), %eax
leaq -0x1(%r15), %rcx
movslq %eax, %rdx
cmpq %rdx, %r15
movq %rcx, %r15
jg 0xd7d8
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop