// File: taurheim[P]AgNES/codeGenerator.cpp
// Function: CodeGenerator::generateAddition(TAC)
void CodeGenerator::generateAddition(TAC tac) {
    code << "; add " << tac.first << " := " << tac.second << " + " << tac.third << nl;
    CodeVar assignTo = lookup(tac.first);
    CodeVar arg1 = lookup(tac.second);
    CodeVar arg2 = lookup(tac.third);

    // For now, use $0 as our adder
    // This is bad and should not be done this way
    // Reset A to 0
    loadConstIntoA(0);
    code << "STA $0" << nl;

    // Add arg1
    loadLocalIntoA(arg1.value);
    code << "CLC" << nl;
    code << "ADC $0" << nl;
    code << "STA $0" << nl;

    // Add arg2
    loadLocalIntoA(arg2.value);
    code << "CLC" << nl;
    code << "ADC $0" << nl;

    // Store the output
    storeAIntoLocal(assignTo.value);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rsi, -0xc0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
subq $-0x80, %rdi
leaq 0x453e(%rip), %rsi      # 0x24aae
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x8, %rsi
movq %rsi, -0xb8(%rbp)
callq 0x32a0
movq %rax, %rdi
leaq 0x429d(%rip), %rsi      # 0x24836
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x28, %rsi
callq 0x32a0
movq %rax, %rdi
leaq 0x4280(%rip), %rsi      # 0x2483b
callq 0x32e0
movq -0xc0(%rbp), %rsi
movq %rax, %rdi
addq $0x48, %rsi
callq 0x32a0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb8(%rbp), %rsi
leaq -0x38(%rbp), %rdi
movq %rdi, -0xa8(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xa8(%rbp), %rsi
callq 0x21250
movq %rax, -0xa0(%rbp)
jmp 0x20615
movq -0xa0(%rbp), %rax
movq %rax, -0x18(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31d0
movq -0xc0(%rbp), %rsi
addq $0x28, %rsi
leaq -0x70(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x21250
movq %rax, -0xc8(%rbp)
jmp 0x20660
movq -0xc8(%rbp), %rax
movq %rax, -0x4c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
movq -0xc0(%rbp), %rsi
addq $0x48, %rsi
leaq -0x98(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x3140
movq -0xb0(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x21250
movq %rax, -0xd8(%rbp)
jmp 0x206ae
movq -0xd8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31d0
movq -0xb0(%rbp), %rdi
xorl %esi, %esi
callq 0x21600
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x43cd(%rip), %rsi      # 0x24ab5
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x214d0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x439c(%rip), %rsi      # 0x24abc
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4377(%rip), %rsi      # 0x24ac0
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4343(%rip), %rsi      # 0x24ab5
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x74(%rbp), %esi
callq 0x214d0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x4312(%rip), %rsi      # 0x24abc
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
addq $0x80, %rdi
leaq 0x42ed(%rip), %rsi      # 0x24ac0
callq 0x32e0
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x32a0
movq -0xb0(%rbp), %rdi
movl -0x14(%rbp), %esi
callq 0x21650
addq $0xe0, %rsp
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x38(%rbp), %rdi
callq 0x31d0
jmp 0x20845
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x31d0
jmp 0x20845
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x31d0
movq -0x40(%rbp), %rdi
callq 0x3520
nop