// File: /workspace/github_cpp_makefile_star2_O1/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 $0x238, %rsp            # imm = 0x238
movq %rsi, %r14
movq %rdi, %rbx
leaq 0xa4(%rsp), %r15
movq %r15, %rdi
callq 0xac76
leaq 0x9c(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xac8c
movl 0x4(%r12), %ebp
movslq 0x8(%r15), %rcx
movslq 0x4(%r15), %r13
movslq (%r15), %rax
movq %rax, 0x50(%rsp)
movq 0x70(%rbx), %rax
movq %rcx, 0x40(%rsp)
movl (%rax,%rcx,4), %r15d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %r15d, %r12d
cmpl $-0x1, %r15d
jne 0x5b03
movq 0x60(%rbx), %rdi
callq 0xcd84
cmpl $-0x1, %eax
je 0x5fca
movl %eax, %r12d
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl %r12d, (%rax,%rcx,4)
movq 0x60(%rbx), %rdi
movl %r12d, %esi
callq 0xc4fa
movl %r12d, %eax
movl %eax, 0xc(%rsp)
movl %r12d, 0x8(%rsp)
movl %ebp, 0x34(%rsp)
movq 0x70(%rbx), %rax
movq %r13, 0x48(%rsp)
movl (%rax,%r13,4), %r12d
movl %r12d, %r13d
cmpl $-0x1, %r12d
jne 0x5b59
movq 0x60(%rbx), %rdi
callq 0xcd84
cmpl $-0x1, %eax
je 0x6016
movl %eax, %r13d
movq 0x70(%rbx), %rax
movq 0x48(%rsp), %rcx
movl %r13d, (%rax,%rcx,4)
movq 0x60(%rbx), %rdi
movl %r13d, %esi
callq 0xc4fa
movl %r13d, 0xc(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x11504(%rip), %rsi     # 0x1706d
leaq 0x114ff(%rip), %rdx     # 0x1706f
leaq 0x10(%rsp), %rdi
callq 0x5346
movq %rbx, 0x38(%rsp)
leaq 0x1768a(%rip), %rdi     # 0x1d210
leaq 0x10(%rsp), %rsi
callq 0x517a
movl 0x34(%rsp), %ecx
cmpl (%rax), %ecx
sete %al
testl %ecx, %ecx
sete %bl
orb %al, %bl
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x5bbc
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb %bl, %bl
movq 0x38(%rsp), %rbx
je 0x5bed
movq 0x60(%rbx), %rdi
callq 0xcd84
cmpl $-0x1, %eax
je 0x5fd1
movl %eax, %ebp
movq 0x60(%rbx), %rdi
movl %eax, %esi
callq 0xc4fa
movl %ebp, 0xc(%rsp)
movq 0x50(%rsp), %r15
cmpl $-0x1, %r15d
je 0x5c0c
movq 0x70(%rbx), %rax
movl (%rax,%r15,4), %eax
leaq 0xc0(%rsp), %rsi
movl %eax, (%rsi)
jmp 0x5c1a
leaq 0xb0(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %r12
movl %ebp, 0x4(%rsi)
movl %r13d, 0x8(%rsi)
movl 0x8(%rsp), %eax
movl %eax, 0xc(%rsi)
movq %r14, %rdi
callq 0xac42
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x11429(%rip), %rsi     # 0x1706d
leaq 0x11424(%rip), %rdx     # 0x1706f
callq 0x5346
leaq 0x175b9(%rip), %rdi     # 0x1d210
leaq 0x10(%rsp), %rsi
callq 0x517a
movl 0x34(%rsp), %r13d
testl %r13d, %r13d
sete %cl
cmpl (%rax), %r13d
sete %bl
orb %cl, %bl
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5c8b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
testb %bl, %bl
movq 0x38(%rsp), %rcx
je 0x5cab
movq 0x70(%rcx), %rax
movl 0xc(%rsp), %edx
movl %edx, (%rax,%r15,4)
movq 0x78(%rcx), %rax
movslq %ebp, %rcx
movb $0x1, (%rax,%rcx)
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x11352(%rip), %rsi     # 0x1700c
leaq 0x1134e(%rip), %rdx     # 0x1700f
callq 0x5346
leaq 0x17543(%rip), %rdi     # 0x1d210
leaq 0x10(%rsp), %rsi
callq 0x517a
movl (%rax), %ebp
movb $0x1, %bl
cmpl %ebp, %r13d
je 0x5d1b
leaq 0x88(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1131d(%rip), %rsi     # 0x17010
leaq 0x11319(%rip), %rdx     # 0x17013
leaq 0x78(%rsp), %rdi
callq 0x5346
leaq 0x17505(%rip), %rdi     # 0x1d210
leaq 0x78(%rsp), %rsi
callq 0x517a
cmpl (%rax), %r13d
sete %bl
cmpl %ebp, %r13d
je 0x5d41
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5d41
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5d58
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
xorl %ebp, %ebp
testb %bl, %bl
je 0x5db3
movl 0x4(%r14), %eax
leal 0x1f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x20, %ecx
subl %ecx, %eax
cltq
movq 0x38(%rsp), %r8
movq 0x98(%r8), %rcx
movq 0xa0(%r8), %rdx
movl (%rcx,%rax,4), %ecx
movl %ecx, %edi
shrl $0x1f, %edi
addl %ecx, %edi
sarl %edi
movl (%rdx,%rax,4), %esi
movl %edi, 0x70(%r14)
movl %esi, 0x74(%r14)
incl %ecx
cmpl $0x3, %ecx
jb 0x5db3
movq 0xa8(%r8), %rdi
callq 0x764e
movl $0x1, %ebp
leaq 0x1744a(%rip), %rax     # 0x1d204
movl (%rax), %eax
movl %eax, 0x38(%r14)
incl %eax
movl %eax, 0x3c(%r14)
leaq 0x17427(%rip), %rax     # 0x1d1f4
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
movl %ecx, (%r14)
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x11287(%rip), %rsi     # 0x1706d
leaq 0x11282(%rip), %rdx     # 0x1706f
callq 0x5346
leaq 0x17417(%rip), %rdi     # 0x1d210
leaq 0x10(%rsp), %rsi
callq 0x517a
movl (%rax), %ebx
cmpl %ebx, %r13d
jne 0x5e0f
xorl %r15d, %r15d
jmp 0x5e48
leaq 0x68(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x111fe(%rip), %rsi     # 0x1701d
leaq 0x111f9(%rip), %rdx     # 0x1701f
leaq 0x58(%rsp), %rdi
callq 0x5346
leaq 0x173d9(%rip), %rdi     # 0x1d210
leaq 0x58(%rsp), %rsi
callq 0x517a
cmpl (%rax), %r13d
setne %r15b
cmpl %ebx, %r13d
movq 0x38(%rsp), %rbx
je 0x5e6d
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5e6d
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5e84
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
testb %r15b, %r15b
je 0x5ee7
movq 0x80(%rbx), %r15
leaq 0x1c0(%rsp), %r12
movl $0xa, %ecx
movq %r12, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x210(%rsp), %rdi
leaq 0x220(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x52b6
movq 0x70(%r14), %rax
movq %rax, 0x10(%r13)
movq %r15, %rdi
movq %r12, %rsi
callq 0x125b4
movq 0x210(%rsp), %rdi
jmp 0x5f43
movq 0x88(%rbx), %r15
leaq 0x148(%rsp), %r12
movl $0xa, %ecx
movq %r12, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x198(%rsp), %rdi
leaq 0x1a8(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x52b6
movq 0x70(%r14), %rax
movq %rax, 0x10(%r13)
movq %r15, %rdi
movq %r12, %rsi
callq 0x13624
movq 0x198(%rsp), %rdi
cmpq %r13, %rdi
je 0x5f54
movq (%r13), %rsi
incq %rsi
callq 0x3200
movq 0x68(%rbx), %rbx
leaq 0xd0(%rsp), %r15
movl $0xa, %ecx
movq %r15, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x120(%rsp), %rdi
leaq 0x130(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x52b6
movq 0x70(%r14), %rax
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xcdec
movq 0x120(%rsp), %rdi
cmpq %r12, %rdi
je 0x603e
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x603e
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x603e
cmpl $-0x1, %r15d
jne 0x5ff4
movq 0x60(%rbx), %rdi
movl 0x8(%rsp), %esi
callq 0xbc6c
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %r12d
jne 0x603e
movq 0x60(%rbx), %rdi
movl %r13d, %esi
callq 0xbc6c
movq 0x70(%rbx), %rax
movq 0x48(%rsp), %rcx
jmp 0x6037
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %r15d
jne 0x603e
movq 0x60(%rbx), %rdi
movl 0x8(%rsp), %esi
callq 0xbc6c
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x198(%rsp), %rdi
cmpq %r13, %rdi
je 0x6122
movq 0x1a8(%rsp), %rsi
jmp 0x6115
movq %rax, %rbx
movq 0x210(%rsp), %rdi
cmpq %r13, %rdi
je 0x6122
movq 0x220(%rsp), %rsi
jmp 0x6115
movq %rax, %rbx
movq 0x58(%rsp), %rdi
cmpq %r15, %rdi
je 0x6101
movq 0x68(%rsp), %rsi
jmp 0x60bf
jmp 0x60fe
movq %rax, %rbx
movq 0x78(%rsp), %rdi
cmpq %r15, %rdi
je 0x6101
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x6101
jmp 0x60fe
movq %rax, %rbx
movq 0x120(%rsp), %rdi
cmpq %r12, %rdi
je 0x6122
movq 0x130(%rsp), %rsi
jmp 0x6115
jmp 0x60fe
jmp 0x611f
jmp 0x60fe
jmp 0x611f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
jne 0x6110
jmp 0x6122
jmp 0x611f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x6122
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x6122
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3340