// File: /workspace/github_cpp_makefile_star2_O2/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 0xacea
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
cmovnsl %ecx, %ebp
movl %esi, %ecx
cmpl %ebp, %eax
jle 0xac0a
imulq $0x758, %rcx, %r12      # imm = 0x758
subq $-0x80, %r12
testl %ebp, %ebp
js 0xac7c
movq 0x9b8(%r14), %rax
movl %ebp, %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
jne 0xabd4
decl %ebp
jmp 0xabb1
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0xabd0
movq 0x9b8(%r14), %rax
addq %r12, %rax
cmpb $0x0, (%r15,%rax)
je 0xac05
cmpb $0x0, -0x23(%r15,%rax)
jne 0xac05
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r15d, %ecx
callq 0xacec
incq %r15
jmp 0xabd7
movslq %ebp, %r15
imulq $0x758, %rcx, %r13      # imm = 0x758
subq $-0x80, %r13
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0xacdc
movq 0x9b8(%r14), %rcx
imulq $0x758, %r15, %rdx      # imm = 0x758
cmpl $0x2, 0x78(%rcx,%rdx)
je 0xac77
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0xac70
movq 0x9b8(%r14), %rax
addq %r13, %rax
cmpb $0x0, (%r12,%rax)
je 0xac6b
cmpb $0x0, -0x23(%r12,%rax)
jne 0xac6b
movq %r14, %rdi
movl %r15d, %esi
movl %ebx, %edx
movl %r12d, %ecx
callq 0xacec
incq %r12
jmp 0xac3c
movl 0x958(%r14), %eax
decq %r15
jmp 0xac18
movl 0x950(%r14), %ebp
decl %ebp
cmpl 0x958(%r14), %ebp
jl 0xacdc
movq 0x9b8(%r14), %rax
movslq %ebp, %rcx
imulq $0x758, %rcx, %rcx      # imm = 0x758
cmpl $0x2, 0x78(%rax,%rcx)
je 0xac83
xorl %r15d, %r15d
cmpq $0x4, %r15
je 0xac83
movq 0x9b8(%r14), %rax
addq %r12, %rax
cmpb $0x0, (%r15,%rax)
je 0xacd7
cmpb $0x0, -0x23(%r15,%rax)
jne 0xacd7
movq %r14, %rdi
movl %ebp, %esi
movl %ebx, %edx
movl %r15d, %ecx
callq 0xacec
incq %r15
jmp 0xaca9
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop