// File: kelwinfc[P]ci4721-Fernandez-Gyomrey/src/AST/to_tac.cpp
// Function: AST::to_tac()
Tac* AST::to_tac(){

    Tac *t = new Tac();

    AST_declaration* m = 0;
    uint nsize = declarations.size();
    
    queue<AST_declaration*> global_variables;
    
    for ( uint i = 0; i != nsize; i++){
        
        // El main se genera de ultimo
        if (   typeid(*declarations[i]) == typeid(AST_function)
            && ((AST_function*)declarations[i])->func->getName().compare("main") 
                    == 0)
        {
            m = declarations[i];
        }
        // La inicialización de variables se deja para antes del main
        else if ( typeid(*declarations[i]) != typeid(AST_function) ){
            global_variables.push(declarations[i]);
        }
        // Las demas funciones se generan de una vez
        else {
            declarations[i]->to_tac(t);
        }
    }
    
    first_instruction = t->next_instruction();
    
    while( !global_variables.empty() ){
        AST_declaration* d = global_variables.front();
        global_variables.pop();
        
        d->to_tac(t);
    }
    m->to_tac(t);

    return t;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xf0(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x8, %edi
callq 0x6390
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
callq 0x4de10
jmp 0x1a548
movq 0x30(%rsp), %rdi
movq 0x38(%rsp), %rax
movq %rax, 0xe8(%rsp)
movq $0x0, 0xd0(%rsp)
addq $0x10, %rdi
callq 0x20010
movl %eax, 0xcc(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x20e10
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0xcc(%rsp), %eax
je 0x1a778
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movb $0x0, 0x4f(%rsp)
cmpq $0x0, %rax
jne 0x1a5c7
callq 0x6420
jmp 0x1a5c7
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rdi
leaq 0x5298e(%rip), %rsi     # 0x6cf68
callq 0x20340
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x27(%rsp)
jne 0x1a5ee
jmp 0x1a638
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq (%rax), %rax
movq 0x98(%rax), %rsi
leaq 0x50(%rsp), %rdi
callq 0x295b0
jmp 0x1a618
movb $0x1, 0x4f(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x3853c(%rip), %rsi     # 0x52b65
callq 0x6110
cmpl $0x0, %eax
sete %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
movb %al, 0x26(%rsp)
testb $0x1, 0x4f(%rsp)
jne 0x1a649
jmp 0x1a653
leaq 0x50(%rsp), %rdi
callq 0x6778
movb 0x26(%rsp), %al
testb $0x1, %al
jne 0x1a65d
jmp 0x1a6cc
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq (%rax), %rax
movq %rax, 0xd0(%rsp)
jmp 0x1a766
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
movl $0x8, %esi
callq 0x63a0
jmp 0x1a835
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x20eb0
jmp 0x1a835
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq (%rax), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
jne 0x1a6f5
callq 0x6420
jmp 0x1a6f5
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq -0x8(%rax), %rdi
leaq 0x52860(%rip), %rsi     # 0x6cf68
callq 0x201f0
testb $0x1, %al
jne 0x1a713
jmp 0x1a738
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq %rax, %rsi
leaq 0x78(%rsp), %rdi
callq 0x20e40
jmp 0x1a736
jmp 0x1a764
movq 0x30(%rsp), %rdi
addq $0x10, %rdi
movl 0x74(%rsp), %eax
movl %eax, %esi
callq 0x20030
movq (%rax), %rdi
movq 0xe8(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x1a762
jmp 0x1a764
jmp 0x1a766
jmp 0x1a768
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x1a588
movq 0xe8(%rsp), %rdi
callq 0x4dfb0
movl %eax, 0x14(%rsp)
jmp 0x1a78b
movl 0x14(%rsp), %ecx
leaq 0x53cc2(%rip), %rax     # 0x6e458
movl %ecx, (%rax)
leaq 0x78(%rsp), %rdi
callq 0x20e70
movb %al, 0x13(%rsp)
jmp 0x1a7a8
movb 0x13(%rsp), %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1a7b4
jmp 0x1a7f8
leaq 0x78(%rsp), %rdi
callq 0x20e90
movq %rax, 0x8(%rsp)
jmp 0x1a7c5
movq 0x8(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x20ea0
jmp 0x1a7de
movq 0x40(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x1a7f6
jmp 0x1a798
movq 0xd0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x1a813
movq 0xe8(%rsp), %rax
movq %rax, (%rsp)
leaq 0x78(%rsp), %rdi
callq 0x20eb0
movq (%rsp), %rax
addq $0xf8, %rsp
retq
movq 0xe0(%rsp), %rdi
callq 0x66b0
nopw %cs:(%rax,%rax)