// File: 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:
subq $0x38, %rsp
movq %rdi, 0x30(%rsp)
movl %esi, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x2c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0x28(%rsp)
jge 0x16cdb
movq 0x8(%rsp), %rax
movl 0x950(%rax), %eax
subl $0x1, %eax
movl %eax, 0x28(%rsp)
movq 0x8(%rsp), %rcx
movl 0x2c(%rsp), %eax
cmpl 0x958(%rcx), %eax
je 0x16f9c
movq 0x8(%rsp), %rax
movl 0x958(%rax), %eax
cmpl 0x28(%rsp), %eax
jg 0x16de4
movl 0x28(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x8(%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl 0x958(%rcx), %eax
jl 0x16ddf
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movslq 0x24(%rsp), %rcx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x78(%rax)
je 0x16dcd
movl $0x0, 0x20(%rsp)
cmpl $0x4, 0x20(%rsp)
jae 0x16dcb
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x20(%rsp), %ecx
cmpb $0x0, 0x80(%rax,%rcx)
je 0x16dbc
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x20(%rsp), %ecx
movzbl 0x5d(%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x16dbc
movq 0x8(%rsp), %rdi
movl 0x24(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x20(%rsp), %ecx
callq 0x16fb0
jmp 0x16dbe
movl 0x20(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x20(%rsp)
jmp 0x16d4f
jmp 0x16dcd
jmp 0x16dcf
movl 0x24(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x16d0d
jmp 0x16f9a
movl 0x28(%rsp), %eax
movl %eax, 0x1c(%rsp)
cmpl $0x0, 0x1c(%rsp)
jl 0x16eb4
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movslq 0x1c(%rsp), %rcx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x78(%rax)
je 0x16ea2
movl $0x0, 0x18(%rsp)
cmpl $0x4, 0x18(%rsp)
jae 0x16ea0
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x18(%rsp), %ecx
cmpb $0x0, 0x80(%rax,%rcx)
je 0x16e91
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x18(%rsp), %ecx
movzbl 0x5d(%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x16e91
movq 0x8(%rsp), %rdi
movl 0x1c(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x18(%rsp), %ecx
callq 0x16fb0
jmp 0x16e93
movl 0x18(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18(%rsp)
jmp 0x16e24
jmp 0x16ea2
jmp 0x16ea4
movl 0x1c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1c(%rsp)
jmp 0x16dec
movq 0x8(%rsp), %rax
movl 0x950(%rax), %eax
subl $0x1, %eax
movl %eax, 0x14(%rsp)
movq 0x8(%rsp), %rcx
movl 0x14(%rsp), %eax
cmpl 0x958(%rcx), %eax
jl 0x16f98
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movslq 0x14(%rsp), %rcx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
cmpl $0x2, 0x78(%rax)
je 0x16f86
movl $0x0, 0x10(%rsp)
cmpl $0x4, 0x10(%rsp)
jae 0x16f84
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x10(%rsp), %ecx
cmpb $0x0, 0x80(%rax,%rcx)
je 0x16f75
movq 0x8(%rsp), %rax
movq 0x9b8(%rax), %rax
movl 0x2c(%rsp), %ecx
imulq $0x758, %rcx, %rcx      # imm = 0x758
addq %rcx, %rax
movl 0x10(%rsp), %ecx
movzbl 0x5d(%rax,%rcx), %eax
cmpl $0x0, %eax
jne 0x16f75
movq 0x8(%rsp), %rdi
movl 0x14(%rsp), %esi
movl 0x2c(%rsp), %edx
movl 0x10(%rsp), %ecx
callq 0x16fb0
jmp 0x16f77
movl 0x10(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10(%rsp)
jmp 0x16f08
jmp 0x16f86
jmp 0x16f88
movl 0x14(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x14(%rsp)
jmp 0x16ec6
jmp 0x16f9a
jmp 0x16f9c
addq $0x38, %rsp
retq
nopw %cs:(%rax,%rax)