// File: /workspace/github_cpp_makefile_star2_O2/ys1998[P]spsim/src/decoder.cpp
// Function: Decoder::decode_instr(Instruction)
int Decoder::decode_instr(Instruction instr){
	std::tuple<int, int, int> reg = instr.logical_regs();
	std::tuple<int,int> instruction_type = instr.type();
	int opcode = std::get<0>(instruction_type);
	
	int rs = std::get<0>(reg);
	int rt = std::get<1>(reg);
	int rd = std::get<2>(reg);
	
	int temp, rs_, rt_, rd_;
	temp = rs_ = rt_ = rd_ = -1;
	bool found[3] = {false, false, false};

	if(*(r + rs) == -1){
		if((temp = f->find()) == -1)
			return -1;
		*(r + rs) = rs_ = temp;
		f->remove(rs_);
		found[0] = true;
	} else {
		rs_ = *(r + rs);
	}
	if(*(r + rt) == -1){
		if((temp = f->find()) == -1){
			if(found[0]){
				f->add(rs_);
				*(r + rs) = -1;	
			} 	// add already removed register
			return -1;
		}
		*(r + rt) = rt_ = temp;
		f->remove(rt_);
		found[1] = true;
	} else {
		rt_ = *(r + rt);
	}
	if(opcode == OPCODE["lw"] || opcode == 0){
		if((temp = f->find()) == -1){
			if(found[0]) {
				f->add(rs_);
				*(r + rs) = -1;
			}	// add already removed register
			if(found[1]) {
				f->add(rt_);
				*(r + rt) = -1;
			}
			return -1;
		} 
		rd_ = temp;
		f->remove(rd_);	
	}
	if(rd != -1)
		instr.map(std::make_tuple(rs_, rt_, rd_, *(r + rd)));
	else
		instr.map(std::make_tuple(rs_, rt_, rd_, -1));
	
	if(opcode == OPCODE["lw"] || opcode == 0){
		*(r + rd) = temp;
		*(b + rd_) = true;
	}

	int ret = 0;
	if(opcode == OPCODE["beq"] || opcode == OPCODE["bne"]){
		auto pr = predict_branch(instr.get_pc());
		instr.predicted = std::get<0>(pr);
		instr.jumpAddressPred = std::get<1>(pr);
		if(instr.predicted){
			ret = 1;
			ft->update_PC(instr.jumpAddressPred);
		}
	}

	instr.DE = CLOCK;
	instr.RF1 = CLOCK + 1;
	instr.set_id();

	if(!(opcode == OPCODE["lw"] || opcode == OPCODE["sw"]))
		iq->add(instr); // TODO check return value
	else
		aq->add(instr);
	
	a->push(instr); // TODO check return value
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x84(%rsp), %r15
movq %r15, %rdi
callq 0x7c64
leaq 0x7c(%rsp), %r12
movq %r12, %rdi
movq %r14, 0x70(%rsp)
movq %r14, %rsi
callq 0x7c7c
movl 0x4(%r12), %r13d
movslq 0x8(%r15), %rcx
movslq 0x4(%r15), %r12
movslq (%r15), %r15
movq 0x70(%rbx), %rax
movq %rcx, 0x18(%rsp)
movl (%rax,%rcx,4), %r14d
cmpl $-0x1, %r14d
movq %rbx, 0x10(%rsp)
je 0x52ca
movq %r15, 0x48(%rsp)
pushq $-0x1
popq %r15
movl %r14d, 0xc(%rsp)
jmp 0x5305
movq 0x60(%rbx), %rdi
callq 0x8dd2
cmpl $-0x1, %eax
je 0x566a
movl %eax, %ebp
movq %r15, 0x48(%rsp)
movq 0x70(%rbx), %rax
movq 0x18(%rsp), %rcx
movl %ebp, (%rax,%rcx,4)
movq 0x60(%rbx), %rdi
movl %ebp, %esi
callq 0x8ad6
movq 0x70(%rbx), %rax
movl %ebp, 0xc(%rsp)
movl %ebp, %r15d
movq %r12, 0x40(%rsp)
movl (%rax,%r12,4), %r12d
movl %r12d, %ebx
cmpl $-0x1, %r12d
jne 0x534a
movq 0x10(%rsp), %rbp
movq 0x60(%rbp), %rdi
callq 0x8dd2
cmpl $-0x1, %eax
je 0x56b8
movl %eax, %ebx
movq 0x70(%rbp), %rax
movq 0x40(%rsp), %rcx
movl %ebx, (%rax,%rcx,4)
movq 0x60(%rbp), %rdi
movl %ebx, %esi
callq 0x8ad6
movl %ebx, %r15d
leaq 0xad1c(%rip), %rsi      # 0x1006d
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x4860
leaq 0x11ea1(%rip), %rdi     # 0x17208
leaq 0x20(%rsp), %rsi
callq 0x47fa
movl (%rax), %ebp
leaq 0x20(%rsp), %rdi
callq 0x3100
testl %r13d, %r13d
je 0x5394
cmpl %ebp, %r13d
je 0x5394
movl %r15d, 0x18(%rsp)
movl %r13d, %r15d
pushq $-0x1
popq %rbp
jmp 0x53bf
movl %r13d, %r15d
movq 0x10(%rsp), %r13
movq 0x60(%r13), %rdi
callq 0x8dd2
cmpl $-0x1, %eax
je 0x566f
movl %eax, %ebp
movq 0x60(%r13), %rdi
movl %eax, %esi
callq 0x8ad6
movl %ebp, 0x18(%rsp)
movq 0x48(%rsp), %r12
cmpl $-0x1, %r12d
movq 0x70(%rsp), %r14
je 0x53e8
movq 0x10(%rsp), %rax
movq 0x70(%rax), %rax
movl (%rax,%r12,4), %eax
leaq 0xa0(%rsp), %rsi
movl %eax, (%rsi)
jmp 0x53f3
leaq 0x90(%rsp), %rsi
orl $-0x1, (%rsi)
movl %ebp, 0x4(%rsi)
movl %ebx, 0x8(%rsi)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rsi)
movq %r14, %rdi
callq 0x7c3a
leaq 0xac5e(%rip), %rsi      # 0x1006d
leaq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rdx
callq 0x4860
leaq 0x11de3(%rip), %rdi     # 0x17208
leaq 0x20(%rsp), %rsi
callq 0x47fa
movq 0x10(%rsp), %r13
movl (%rax), %ebx
leaq 0x20(%rsp), %rdi
callq 0x3100
testl %r15d, %r15d
je 0x544a
cmpl %ebx, %r15d
jne 0x5461
movq 0x70(%r13), %rax
movl 0x18(%rsp), %ecx
movl %ecx, (%rax,%r12,4)
movq 0x78(%r13), %rax
movslq %ebp, %rcx
movb $0x1, (%rax,%rcx)
leaq 0xaba4(%rip), %rsi      # 0x1000c
leaq 0x20(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x4860
leaq 0x11d8a(%rip), %rdi     # 0x17208
leaq 0x20(%rsp), %rsi
callq 0x47fa
cmpl (%rax), %r15d
jne 0x5499
leaq 0x20(%rsp), %rdi
callq 0x3100
jmp 0x54db
leaq 0xab70(%rip), %rsi      # 0x10010
leaq 0x50(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x4860
leaq 0x11d52(%rip), %rdi     # 0x17208
leaq 0x50(%rsp), %rsi
callq 0x47fa
movl (%rax), %ebx
leaq 0x50(%rsp), %rdi
callq 0x3100
leaq 0x20(%rsp), %rdi
callq 0x3100
cmpl %ebx, %r15d
jne 0x5515
movl 0x4(%r14), %eax
pushq $0x20
popq %rcx
cltd
idivl %ecx
movslq %edx, %rsi
movq 0x98(%r13), %rax
movq 0xa0(%r13), %rdi
movl (%rax,%rsi,4), %ecx
pushq $0x2
popq %r8
movl %ecx, %eax
cltd
idivl %r8d
movl (%rdi,%rsi,4), %esi
movl %eax, 0x70(%r14)
movl %esi, 0x74(%r14)
incl %ecx
cmpl $0x3, %ecx
jae 0x5519
xorl %ebp, %ebp
jmp 0x5528
movq 0xa8(%r13), %rdi
callq 0x6994
pushq $0x1
popq %rbp
leaq 0x11ccd(%rip), %rax     # 0x171fc
movl (%rax), %eax
movl %eax, 0x38(%r14)
incl %eax
movl %eax, 0x3c(%r14)
leaq 0x11caa(%rip), %rax     # 0x171ec
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
movl %ecx, (%r14)
leaq 0xab1a(%rip), %rsi      # 0x1006d
leaq 0x20(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x4860
leaq 0x11c9f(%rip), %rdi     # 0x17208
leaq 0x20(%rsp), %rsi
callq 0x47fa
cmpl (%rax), %r15d
jne 0x5584
leaq 0x20(%rsp), %rdi
callq 0x3100
jmp 0x55c6
leaq 0xaa92(%rip), %rsi      # 0x1001d
leaq 0x50(%rsp), %rdi
leaq 0xa(%rsp), %rdx
callq 0x4860
leaq 0x11c67(%rip), %rdi     # 0x17208
leaq 0x50(%rsp), %rsi
callq 0x47fa
movl (%rax), %ebx
leaq 0x50(%rsp), %rdi
callq 0x3100
leaq 0x20(%rsp), %rdi
callq 0x3100
cmpl %ebx, %r15d
jne 0x55f5
movq 0x88(%r13), %r15
leaq 0x128(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x49ba
movq %r15, %rdi
movq %r12, %rsi
callq 0xd0c0
leaq 0x178(%rsp), %rdi
jmp 0x5622
movq 0x80(%r13), %r15
leaq 0x1a0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x49ba
movq %r15, %rdi
movq %r12, %rsi
callq 0xc854
leaq 0x1f0(%rsp), %rdi
callq 0x3100
movq 0x68(%r13), %rbx
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x49ba
movq %rbx, %rdi
movq %r15, %rsi
callq 0x8e24
leaq 0x100(%rsp), %rdi
callq 0x3100
movl %ebp, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rbp
jmp 0x5656
cmpl $-0x1, %r14d
jne 0x5694
movq 0x10(%rsp), %r14
movq 0x60(%r14), %rdi
movl 0xc(%rsp), %esi
callq 0x87da
movq 0x70(%r14), %rax
movq 0x18(%rsp), %rcx
orl $-0x1, (%rax,%rcx,4)
pushq $-0x1
popq %rbp
cmpl $-0x1, %r12d
jne 0x5656
movq 0x10(%rsp), %r14
movq 0x60(%r14), %rdi
movl %ebx, %esi
callq 0x87da
movq 0x70(%r14), %rax
movq 0x40(%rsp), %rcx
jmp 0x56dc
pushq $-0x1
popq %rbp
cmpl $-0x1, %r14d
jne 0x5656
movq 0x10(%rsp), %rbx
movq 0x60(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0x87da
movq 0x70(%rbx), %rax
movq 0x18(%rsp), %rcx
orl $-0x1, (%rax,%rcx,4)
jmp 0x5656
movq %rax, %rbx
leaq 0x1f0(%rsp), %rdi
jmp 0x5735
jmp 0x56f6
jmp 0x572d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x3100
jmp 0x5730
jmp 0x572d
movq %rax, %rbx
leaq 0x178(%rsp), %rdi
jmp 0x5735
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x5735
jmp 0x572d
jmp 0x573c
jmp 0x572d
jmp 0x573c
jmp 0x572d
jmp 0x573c
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3100
jmp 0x573f
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3320
nop