// File: taurheim[P]AgNES/irGenerator.cpp
// Function: IRGenerator::genTACExpression[abi:cxx11](ASTNode*)
std::string IRGenerator::genTACExpression(ASTNode * expr) {
    TAC code = TAC();
    if (expr->children.size() == 1) {
        ASTNode * onlyChild = expr->children[0];
        if(onlyChild->nodeType == AST_NUMBER) {
            // Case 1: cgen(k) where k is a constant
            code.op = IR_ASSIGN;
            code.first = genAddress();
            code.second = std::to_string(((NumberNode *)onlyChild)->value);
            intermediateCode.push_back(code);
        } else if (onlyChild->nodeType == AST_IDENTIFIER) {
            // Case 2: cgen(id) where id is an identifier
            code.op = IR_ASSIGN;
            code.first = genAddress();
            code.second = ((IdentifierNode *)onlyChild)->name;
            intermediateCode.push_back(code);
        }
    } else {
        ASTNode * secondChild = expr->children[1];
        //Check if binary op
        if (secondChild->nodeType == AST_OPERATOR) {
            code.second = genTACExpression(expr->children[0]);
            code.third = genTACExpression(expr->children[2]);
            OpNode * opNode = (OpNode *)expr->children[1];
            code.op = opStringToEnum[opNode->operation];
            code.first = genAddress();
            intermediateCode.push_back(code);
        }
        //Could be unary op, function, etc..
    }
    return code.first;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1c0, %rsp            # imm = 0x1C0
movq %rdi, -0x180(%rbp)
movq %rdi, %rax
movq %rax, -0x178(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x170(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x68, %edx
callq 0x3190
leaq -0x80(%rbp), %rdi
callq 0x1e4d0
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
callq 0x17d80
cmpq $0x1, %rax
jne 0x1cca3
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x19310
movq (%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rax
cmpl $0x5, (%rax)
jne 0x1cc23
movq -0x170(%rbp), %rsi
movl $0x2, -0x80(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x1e390
jmp 0x1cb8a
leaq -0x78(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
movq %rsi, -0x188(%rbp)
callq 0x33a0
movq -0x188(%rbp), %rdi
callq 0x31d0
movq -0x88(%rbp), %rax
movl 0x20(%rax), %esi
leaq -0xd8(%rbp), %rdi
callq 0x14c90
jmp 0x1cbc5
leaq -0x58(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
movq %rsi, -0x190(%rbp)
callq 0x33a0
movq -0x190(%rbp), %rdi
callq 0x31d0
movq -0x170(%rbp), %rdi
addq $0x10, %rdi
leaq -0x80(%rbp), %rsi
callq 0x1e510
jmp 0x1cbfe
jmp 0x1cc9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xb0(%rbp)
movl %eax, -0xb4(%rbp)
leaq -0x80(%rbp), %rdi
callq 0x94e0
jmp 0x1ce2d
movq -0x88(%rbp), %rax
cmpl $0x4, (%rax)
jne 0x1cc9c
movq -0x170(%rbp), %rsi
movl $0x2, -0x80(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x1e390
jmp 0x1cc4b
leaq -0x78(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
movq %rsi, -0x198(%rbp)
callq 0x33a0
movq -0x198(%rbp), %rdi
callq 0x31d0
movq -0x88(%rbp), %rsi
addq $0x20, %rsi
leaq -0x58(%rbp), %rdi
callq 0x3040
jmp 0x1cc84
movq -0x170(%rbp), %rdi
addq $0x10, %rdi
leaq -0x80(%rbp), %rsi
callq 0x1e510
jmp 0x1cc9a
jmp 0x1cc9c
jmp 0x1cc9e
jmp 0x1ce02
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x1, %esi
callq 0x19310
movq (%rax), %rax
movq %rax, -0x100(%rbp)
movq -0x100(%rbp), %rax
cmpl $0xd, (%rax)
jne 0x1ce00
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x19310
movq -0x170(%rbp), %rsi
movq (%rax), %rdx
leaq -0x120(%rbp), %rdi
callq 0x1cae0
jmp 0x1ccf8
leaq -0x58(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movq %rsi, -0x1a0(%rbp)
callq 0x33a0
movq -0x1a0(%rbp), %rdi
callq 0x31d0
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x2, %esi
callq 0x19310
movq -0x170(%rbp), %rsi
movq (%rax), %rdx
leaq -0x140(%rbp), %rdi
callq 0x1cae0
jmp 0x1cd45
leaq -0x38(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movq %rsi, -0x1b0(%rbp)
callq 0x33a0
movq -0x1b0(%rbp), %rdi
callq 0x31d0
movq -0x18(%rbp), %rdi
addq $0x8, %rdi
movl $0x1, %esi
callq 0x19310
movq (%rax), %rax
movq %rax, -0x148(%rbp)
movq -0x148(%rbp), %rsi
addq $0x20, %rsi
leaq 0x18842(%rip), %rdi     # 0x355d8
callq 0x1e550
movq %rax, -0x1a8(%rbp)
jmp 0x1cda4
movq -0x170(%rbp), %rsi
movq -0x1a8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x80(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x1e390
jmp 0x1cdc5
leaq -0x78(%rbp), %rdi
leaq -0x168(%rbp), %rsi
movq %rsi, -0x1b8(%rbp)
callq 0x33a0
movq -0x1b8(%rbp), %rdi
callq 0x31d0
movq -0x170(%rbp), %rdi
addq $0x10, %rdi
leaq -0x80(%rbp), %rsi
callq 0x1e510
jmp 0x1cdfe
jmp 0x1ce00
jmp 0x1ce02
movq -0x180(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x3140
jmp 0x1ce14
leaq -0x80(%rbp), %rdi
callq 0x94e0
movq -0x178(%rbp), %rax
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbp
retq
movq -0xb0(%rbp), %rdi
callq 0x3520
nopl (%rax)