// File: /workspace/github_cpp_makefile_star2_O1/ys1998[P]spsim/src/alu.cpp
// Function: ALU1::operate()
void ALU1::operate(void){
	int funct = std::get<1>(i.type());
	int opcode = std::get<0>(i.type());
	if(funct == FUNCT["add"]){
		res = in1 + in2;
	}else if(funct == FUNCT["sub"]){
		res = in1 - in2;
	}else if(opcode != 0){
		if ((opcode == OPCODE["beq"]) || (opcode == OPCODE["bne"]))	{	
			if(opcode == OPCODE["beq"]){
				res = (in1 == in2);
			}else if(opcode == OPCODE["bne"]){
				res = (in1 != in2);
			}
			// Branch prediction update
			int possible_jump_address = i.get_pc() + 1 + i.get_immediate();
			*(predict_addr + i.get_pc() % BRANCH_PREDICT_SLOTS) = possible_jump_address;
			
			if(res == 1){
				*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) += 1;
				if(*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) == STRONGLY_TAKEN + 1)
					*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) = STRONGLY_TAKEN;
			}else{
				*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) -= 1;
				if(*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) == STRONGLY_NOT_TAKEN - 1)
					*(predict + i.get_pc() % BRANCH_PREDICT_SLOTS) = STRONGLY_NOT_TAKEN;
			}
			
			if (res != i.predicted || ((res == 1) && (possible_jump_address != i.jumpAddressPred))){
				//	case of misprediction
				if(res == 0) possible_jump_address = i.get_pc()+1;
				// flush second ALU
				a->flush(i.get_id());
				a3->flush(i.get_id());
				// flush other entities
				f->flush(i.get_id(), possible_jump_address);
			}
				
		}	
	} 
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x40, %rsp
movq %rdi, %rbx
leaq 0x8(%rdi), %r14
movq %rsp, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xac8c
movl (%r15), %r12d
movq %rsp, %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0xac8c
movl 0x4(%r15), %ebp
leaq 0x10(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x1363a(%rip), %rsi     # 0x17004
leaq 0x13636(%rip), %rdx     # 0x17007
movq %rsp, %rdi
callq 0x5346
leaq 0x19860(%rip), %rdi     # 0x1d240
movq %rsp, %rsi
callq 0x517a
movl (%rax), %r15d
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3a01
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
cmpl %r15d, %r12d
jne 0x3a14
movl 0xb8(%rbx), %eax
addl 0xb4(%rbx), %eax
jmp 0x3a66
movq %rsp, %rdi
movq %r14, (%rdi)
leaq 0x135e7(%rip), %rsi     # 0x17008
leaq 0x135e3(%rip), %rdx     # 0x1700b
callq 0x5346
leaq 0x1980c(%rip), %rdi     # 0x1d240
movq %rsp, %rsi
callq 0x517a
movl (%rax), %r15d
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3a55
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
cmpl %r15d, %r12d
jne 0x3a79
movl 0xb4(%rbx), %eax
subl 0xb8(%rbx), %eax
movl %eax, 0xbc(%rbx)
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
testl %ebp, %ebp
je 0x3a6c
movq %rsp, %rdi
movq %r14, (%rdi)
leaq 0x13582(%rip), %rsi     # 0x1700c
leaq 0x1357e(%rip), %rdx     # 0x1700f
callq 0x5346
leaq 0x19773(%rip), %rdi     # 0x1d210
movq %rsp, %rsi
callq 0x517a
movl (%rax), %r12d
movb $0x1, %r15b
cmpl %r12d, %ebp
je 0x3ae8
leaq 0x30(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x13550(%rip), %rsi     # 0x17010
leaq 0x1354c(%rip), %rdx     # 0x17013
leaq 0x20(%rsp), %rdi
callq 0x5346
leaq 0x19738(%rip), %rdi     # 0x1d210
leaq 0x20(%rsp), %rsi
callq 0x517a
cmpl (%rax), %ebp
sete %r15b
cmpl %r12d, %ebp
je 0x3b08
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3b08
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3200
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3b1e
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
testb %r15b, %r15b
je 0x3a6c
movq %rsp, %rdi
movq %r14, (%rdi)
leaq 0x134d8(%rip), %rsi     # 0x1700c
leaq 0x134d4(%rip), %rdx     # 0x1700f
callq 0x5346
leaq 0x196c9(%rip), %rdi     # 0x1d210
movq %rsp, %rsi
callq 0x517a
movl (%rax), %r15d
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3b68
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
cmpl %r15d, %ebp
jne 0x3b7e
movl 0xb4(%rbx), %eax
cmpl 0xb8(%rbx), %eax
sete %al
jmp 0x3bd3
movq %rsp, %rdi
movq %r14, (%rdi)
leaq 0x13485(%rip), %rsi     # 0x17010
leaq 0x13481(%rip), %rdx     # 0x17013
callq 0x5346
leaq 0x19672(%rip), %rdi     # 0x1d210
movq %rsp, %rsi
callq 0x517a
movl (%rax), %r15d
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3bbf
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
cmpl %r15d, %ebp
jne 0x3bdc
movl 0xb4(%rbx), %eax
cmpl 0xb8(%rbx), %eax
setne %al
movzbl %al, %eax
movl %eax, 0xbc(%rbx)
movl 0xc(%rbx), %eax
movl 0x38(%rbx), %ecx
leal (%rax,%rcx), %ebp
incl %ebp
movq 0x100(%rbx), %rcx
leal 0x1f(%rax), %edx
testl %eax, %eax
cmovnsl %eax, %edx
andl $-0x20, %edx
subl %edx, %eax
cltq
movl %ebp, (%rcx,%rax,4)
movl 0xc(%rbx), %ecx
leal 0x1f(%rcx), %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
andl $-0x20, %eax
subl %eax, %ecx
cmpl $0x1, 0xbc(%rbx)
movq 0xf8(%rbx), %rax
movslq %ecx, %rcx
movl (%rax,%rcx,4), %edx
jne 0x3c4d
incl %edx
movl %edx, (%rax,%rcx,4)
movl 0xc(%rbx), %ecx
leal 0x1f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
andl $-0x20, %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $0x4, (%rax,%rcx,4)
jne 0x3c72
movl $0x3, (%rax,%rcx,4)
jmp 0x3c72
decl %edx
movl %edx, (%rax,%rcx,4)
movl 0xc(%rbx), %ecx
leal 0x1f(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
andl $-0x20, %edx
subl %edx, %ecx
movslq %ecx, %rcx
cmpl $-0x1, (%rax,%rcx,4)
jne 0x3c72
movl $0x0, (%rax,%rcx,4)
movl 0xbc(%rbx), %eax
cmpl 0x78(%rbx), %eax
jne 0x3c8f
cmpl $0x1, %eax
jne 0x3a6c
cmpl 0x7c(%rbx), %ebp
je 0x3a6c
movl 0x8(%rbx), %esi
movl 0xc(%rbx), %ecx
incl %ecx
testl %eax, %eax
cmovel %ecx, %ebp
movq 0xe0(%rbx), %rdi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0xe8(%rbx), %rdi
movl 0x8(%rbx), %esi
movq (%rdi), %rax
callq *0x10(%rax)
movq 0xf0(%rbx), %rdi
movl 0x8(%rbx), %esi
movl %ebp, %edx
callq 0x7664
jmp 0x3a6c
jmp 0x3cfd
jmp 0x3d18
jmp 0x3cfd
jmp 0x3d18
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x3d00
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x3d00
jmp 0x3cfd
jmp 0x3cfd
jmp 0x3d18
jmp 0x3cfd
jmp 0x3d18
movq %rax, %rbx
movq (%rsp), %rdi
cmpq %r14, %rdi
je 0x3d1b
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x3200
jmp 0x3d1b
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3340
nop