// File: /workspace/github_cpp_makefile_star2_O1/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), %ecx
cmpl %esi, %ecx
je 0xd90a
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebx
movq %rdi, %r14
leal -0x1(%rbx), %edx
movl 0x950(%rdi), %eax
decl %eax
testl %edx, %edx
cmovnsl %edx, %eax
cmpl %eax, %ecx
jle 0xd886
testl %eax, %eax
js 0xd80a
movl %ebx, %ecx
movl %eax, %r15d
imulq $0x758, %rcx, %r13      # imm = 0x758
subq $-0x80, %r13
movq 0x9b8(%r14), %rax
imulq $0x758, %r15, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
je 0xd7fe
xorl %r12d, %r12d
movq 0x9b8(%r14), %rax
addq %r13, %rax
cmpb $0x0, (%r12,%rax)
je 0xd7f5
cmpb $0x0, -0x23(%r12,%rax)
jne 0xd7f5
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xd90c
incq %r12
cmpq $0x4, %r12
jne 0xd7cc
leaq -0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
jg 0xd7b4
movl 0x950(%r14), %ebp
decl %ebp
cmpl 0x958(%r14), %ebp
jl 0xd8fc
movl %ebx, %eax
imulq $0x758, %rax, %r12      # imm = 0x758
subq $-0x80, %r12
movq 0x9b8(%r14), %rax
movslq %ebp, %rcx
imulq $0x758, %rcx, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
je 0xd879
xorl %r15d, %r15d
movq 0x9b8(%r14), %rax
addq %r12, %rax
cmpb $0x0, (%r15,%rax)
je 0xd870
cmpb $0x0, -0x23(%r15,%rax)
jne 0xd870
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r15d, %ecx
callq 0xd90c
incq %r15
cmpq $0x4, %r15
jne 0xd848
decl %ebp
cmpl 0x958(%r14), %ebp
jge 0xd82d
jmp 0xd8fc
cmpl 0x958(%r14), %eax
jl 0xd8fc
movl %ebx, %ecx
movslq %eax, %r15
imulq $0x758, %rcx, %r13      # imm = 0x758
subq $-0x80, %r13
movq 0x9b8(%r14), %rax
imulq $0x758, %r15, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
je 0xd8e9
xorl %r12d, %r12d
movq 0x9b8(%r14), %rax
addq %r13, %rax
cmpb $0x0, (%r12,%rax)
je 0xd8e0
cmpb $0x0, -0x23(%r12,%rax)
jne 0xd8e0
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xd90c
incq %r12
cmpq $0x4, %r12
jne 0xd8b7
leaq -0x1(%r15), %rax
movslq 0x958(%r14), %rcx
cmpq %rcx, %r15
movq %rax, %r15
jg 0xd89f
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop