// File: /workspace/github_cpp_makefile_star2_O3/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 0xa8a0
leaq 0x7c(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0xa8b8
movl 0x4(%r12), %ebp
movslq 0x8(%r15), %rcx
movslq 0x4(%r15), %r13
movslq (%r15), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rbx), %rax
movq %rcx, 0x40(%rsp)
movl (%rax,%rcx,4), %r15d
movl $0xffffffff, 0x8(%rsp)  # imm = 0xFFFFFFFF
movl %r15d, %r12d
cmpl $-0x1, %r15d
jne 0x5ad1
movq 0x60(%rbx), %rdi
callq 0xc978
cmpl $-0x1, %eax
je 0x5f73
movl %eax, %r12d
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl %r12d, (%rax,%rcx,4)
movq 0x60(%rbx), %rdi
movl %r12d, %esi
callq 0xc0f6
movq 0x70(%rbx), %rax
movl %r12d, 0x8(%rsp)
movl %r12d, 0xc(%rsp)
movl %ebp, 0x3c(%rsp)
movq %r13, 0x68(%rsp)
movl (%rax,%r13,4), %r12d
movl %r12d, %r13d
cmpl $-0x1, %r12d
jne 0x5b1f
movq 0x60(%rbx), %rdi
callq 0xc978
cmpl $-0x1, %eax
je 0x5fbf
movl %eax, %r13d
movq 0x70(%rbx), %rax
movq 0x68(%rsp), %rcx
movl %r13d, (%rax,%rcx,4)
movq 0x60(%rbx), %rdi
movl %r13d, %esi
callq 0xc0f6
movl %r13d, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1053e(%rip), %rsi     # 0x1606d
leaq 0x10539(%rip), %rdx     # 0x1606f
leaq 0x10(%rsp), %rdi
callq 0x530a
movq %rbx, 0x30(%rsp)
leaq 0x166c4(%rip), %rdi     # 0x1c210
leaq 0x10(%rsp), %rsi
callq 0x5136
movl 0x3c(%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 0x5b82
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
testb %bl, %bl
movq 0x30(%rsp), %rbx
je 0x5bb3
movq 0x60(%rbx), %rdi
callq 0xc978
cmpl $-0x1, %eax
je 0x5f7a
movl %eax, %ebp
movq 0x60(%rbx), %rdi
movl %eax, %esi
callq 0xc0f6
movl %ebp, 0x8(%rsp)
movq 0x70(%rsp), %r15
cmpl $-0x1, %r15d
je 0x5bd2
movq 0x70(%rbx), %rax
movl (%rax,%r15,4), %eax
leaq 0xa0(%rsp), %rsi
movl %eax, (%rsi)
jmp 0x5be0
leaq 0x90(%rsp), %rsi
movl $0xffffffff, (%rsi)     # imm = 0xFFFFFFFF
leaq 0x20(%rsp), %r12
movl %ebp, 0x4(%rsi)
movl %r13d, 0x8(%rsi)
movl 0xc(%rsp), %eax
movl %eax, 0xc(%rsi)
movq %r14, %rdi
callq 0xa876
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x10463(%rip), %rsi     # 0x1606d
leaq 0x1045e(%rip), %rdx     # 0x1606f
callq 0x530a
leaq 0x165f3(%rip), %rdi     # 0x1c210
leaq 0x10(%rsp), %rsi
callq 0x5136
movl 0x3c(%rsp), %r13d
testl %r13d, %r13d
sete %cl
cmpl (%rax), %r13d
sete %bl
orb %cl, %bl
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5c51
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
testb %bl, %bl
movq 0x30(%rsp), %rcx
je 0x5c71
movq 0x70(%rcx), %rax
movl 0x8(%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 0x1038c(%rip), %rsi     # 0x1600c
leaq 0x10388(%rip), %rdx     # 0x1600f
callq 0x530a
leaq 0x1657d(%rip), %rdi     # 0x1c210
leaq 0x10(%rsp), %rsi
callq 0x5136
movb $0x1, %bl
cmpl (%rax), %r13d
je 0x5cf3
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1035c(%rip), %rsi     # 0x16010
leaq 0x10358(%rip), %rdx     # 0x16013
leaq 0x48(%rsp), %rdi
callq 0x530a
leaq 0x16544(%rip), %rdi     # 0x1c210
leaq 0x48(%rsp), %rsi
callq 0x5136
cmpl (%rax), %r13d
sete %bl
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x5cf3
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5d0a
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
xorl %ebp, %ebp
testb %bl, %bl
je 0x5d65
movl 0x4(%r14), %eax
leal 0x1f(%rax), %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
andl $-0x20, %ecx
subl %ecx, %eax
cltq
movq 0x30(%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 0x5d65
movq 0xa8(%r8), %rdi
callq 0x749c
movl $0x1, %ebp
leaq 0x16498(%rip), %rax     # 0x1c204
movl (%rax), %eax
movl %eax, 0x38(%r14)
incl %eax
movl %eax, 0x3c(%r14)
leaq 0x16475(%rip), %rax     # 0x1c1f4
movl (%rax), %ecx
leal 0x1(%rcx), %edx
movl %edx, (%rax)
movl %ecx, (%r14)
leaq 0x10(%rsp), %rdi
movq %r12, (%rdi)
leaq 0x102d5(%rip), %rsi     # 0x1606d
leaq 0x102d0(%rip), %rdx     # 0x1606f
callq 0x530a
leaq 0x16465(%rip), %rdi     # 0x1c210
leaq 0x10(%rsp), %rsi
callq 0x5136
cmpl (%rax), %r13d
jne 0x5dbe
xorl %ebx, %ebx
jmp 0x5e0d
leaq 0x58(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x1024f(%rip), %rsi     # 0x1601d
leaq 0x1024a(%rip), %rdx     # 0x1601f
leaq 0x48(%rsp), %rdi
callq 0x530a
leaq 0x1642a(%rip), %rdi     # 0x1c210
leaq 0x48(%rsp), %rsi
callq 0x5136
cmpl (%rax), %r13d
setne %bl
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x5e0d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5e24
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
testb %bl, %bl
je 0x5e8b
movq 0x30(%rsp), %rbx
movq 0x80(%rbx), %r15
leaq 0x1a0(%rsp), %r12
movl $0xa, %ecx
movq %r12, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x1f0(%rsp), %rdi
leaq 0x200(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x5274
movq 0x70(%r14), %rax
movq %rax, 0x10(%r13)
movq %r15, %rdi
movq %r12, %rsi
callq 0x12084
movq 0x1f0(%rsp), %rdi
jmp 0x5eec
movq 0x30(%rsp), %rbx
movq 0x88(%rbx), %r15
leaq 0x128(%rsp), %r12
movl $0xa, %ecx
movq %r12, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x178(%rsp), %rdi
leaq 0x188(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x5274
movq 0x70(%r14), %rax
movq %rax, 0x10(%r13)
movq %r15, %rdi
movq %r12, %rsi
callq 0x12dfc
movq 0x178(%rsp), %rdi
cmpq %r13, %rdi
je 0x5efd
movq (%r13), %rsi
incq %rsi
callq 0x3200
movq 0x68(%rbx), %rbx
leaq 0xb0(%rsp), %r15
movl $0xa, %ecx
movq %r15, %rdi
movq %r14, %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq 0x100(%rsp), %rdi
leaq 0x110(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x50(%r14), %rsi
movq 0x58(%r14), %rdx
addq %rsi, %rdx
callq 0x5274
movq 0x70(%r14), %rax
movq %rax, 0x10(%r12)
movq %rbx, %rdi
movq %r15, %rsi
callq 0xc9e0
movq 0x100(%rsp), %rdi
cmpq %r12, %rdi
je 0x5fe7
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x5fe7
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x5fe7
cmpl $-0x1, %r15d
jne 0x5f9d
movq 0x60(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0xb870
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %r12d
jne 0x5fe7
movq 0x60(%rbx), %rdi
movl %r13d, %esi
callq 0xb870
movq 0x70(%rbx), %rax
movq 0x68(%rsp), %rcx
jmp 0x5fe0
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $-0x1, %r15d
jne 0x5fe7
movq 0x60(%rbx), %rdi
movl 0xc(%rsp), %esi
callq 0xb870
movq 0x70(%rbx), %rax
movq 0x40(%rsp), %rcx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x178(%rsp), %rdi
cmpq %r13, %rdi
je 0x60b3
movq 0x188(%rsp), %rsi
jmp 0x60a6
movq %rax, %rbx
movq 0x1f0(%rsp), %rdi
cmpq %r13, %rdi
je 0x60b3
movq 0x200(%rsp), %rsi
jmp 0x60a6
jmp 0x603e
jmp 0x608f
movq %rax, %rbx
movq 0x48(%rsp), %rdi
cmpq %r15, %rdi
je 0x6092
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x6092
jmp 0x608f
movq %rax, %rbx
movq 0x100(%rsp), %rdi
cmpq %r12, %rdi
je 0x60b3
movq 0x110(%rsp), %rsi
jmp 0x60a6
jmp 0x608f
jmp 0x60b0
jmp 0x608f
jmp 0x60b0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
jne 0x60a1
jmp 0x60b3
jmp 0x60b0
movq %rax, %rbx
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x60b3
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x60b3
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3340
nop