// File: /workspace/github_cpp_makefile_star2_O3/Davidc2525[P]Leaf/compiler/backend/x86_64_linux/generator.cpp
// Function: x86_64_linux_generator_visitor::visit(LEAF::AST::TryStatementNode*)
void x86_64_linux_generator_visitor::visit(TryStatementNode *_try_)
{
    UPDATE_STATICS
    Section *st = assembly->get_program()->section(Sections::text);

    DInstruccion *i = new DInstruccion(";start try ");
    i->add_operand(new RegisterOperand(_try_->end_label->c_str()));
    st->add(i);

    //prepare try instruccion

    bool rax_bussy = rp->isBussy(rp->get_by_name("rax")->id);
    if (rax_bussy)
    {
        i = new DInstruccion("push ");
        i->add_operand(new RegisterOperand("rax"));
        st->add(i);
    }

    i = new DInstruccion("new_list");
    i->add_operand(new InmediateIntOperand(_try_->_caths_.size()));
    st->add(i);

    Register *tmp = rp->reserve();
    i = new DInstruccion("mov");
    i->add_operand(new RegisterOperand(tmp->getName()));
    i->add_operand(new RegisterOperand("rax"));
    st->add(i);

    vector<ASTU>::iterator _catchs_ = _try_->_caths_.begin();
    int index = 0;
    for (; _catchs_ != _try_->_caths_.end(); _catchs_++)
    {
        i = new DInstruccion("new_handler");
        int type_id = symbol_table::get_id(((CatchStatementNode *)(*_catchs_))->ex_var->obj->type, 0);
        i->add_operand(new InmediateIntOperand(type_id));
        string *label = ((CatchStatementNode *)(*_catchs_))->start_label;
        i->add_operand(new RegisterOperand(label->c_str()));
        st->add(i);

        i = new DInstruccion("list_set");
        i->add_operand(new RegisterOperand(tmp->getName()));
        i->add_operand(new InmediateIntOperand(index));
        i->add_operand(new RegisterOperand("rax"));
        st->add(i);

        index++;
    }
    i = new DInstruccion("_push_");
    i->add_operand(new RegisterOperand(tmp->getName()));
    st->add(i);

    i = new DInstruccion("call _dump_stack");
    //st->add(i);
    rp->free(tmp->id);

    if (rax_bussy)
    {
        i = new DInstruccion("pop ");
        i->add_operand(new RegisterOperand("rax"));
        st->add(i);
    }
    i = new DInstruccion(";end prepare try ");
    i->add_operand(new RegisterOperand(_try_->end_label->c_str()));
    st->add(i);
    //

    //_try_->child->accept(this);

    vector<ASTU>::iterator x = _try_->nodos.begin();
    for (; x != _try_->nodos.end(); x++)
    {
        (*x)->accept(this);
    }

    i = new DInstruccion("jmp");
    i->add_operand(new RegisterOperand(_try_->end_label->c_str()));
    st->add(i);

    _catchs_ = _try_->_caths_.begin();
    for (; _catchs_ != _try_->_caths_.end(); _catchs_++)
    {
        (*_catchs_)->accept(this);
        DInstruccion *i = new DInstruccion("jmp");
        i->add_operand(new RegisterOperand(_try_->end_label_wout_pop->c_str()));
        st->add(i);
    }

    i = new DInstruccion(";end try ");
    i->add_operand(new RegisterOperand(_try_->end_label->c_str()));
    st->add(i);

    Label *try_label = new Label((char *)_try_->end_label->c_str());
    st->add(try_label);

    i = new DInstruccion("_pop_");
    st->add(i);

    try_label = new Label((char *)_try_->end_label_wout_pop->c_str());
    st->add(try_label);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %r13
incl 0x8(%rdi)
movq %rsi, %r14
movq 0x10(%rdi), %rax
movq (%rax), %rdi
movl $0x2, %esi
callq 0x10346
movq %rax, %rbx
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x453e(%rip), %rsi      # 0x1b40b
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movq 0x30(%r13), %r15
movq (%r15), %rax
leaq 0x402d(%rip), %rsi      # 0x1af44
movq %r15, %rdi
callq *0x10(%rax)
movl (%rax), %esi
movq (%r15), %rax
movq %r15, %rdi
callq *(%rax)
movl %eax, %ebp
testb %al, %al
je 0x16f7d
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x44d6(%rip), %rsi      # 0x1b417
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3fe7(%rip), %rsi      # 0x1af44
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x448c(%rip), %rsi      # 0x1b41d
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x48(%r14), %rsi
movq %r14, 0x18(%rsp)
subq 0x40(%r14), %rsi
shrq $0x3, %rsi
movq %rax, %rdi
callq 0xfe48
movb %bpl, 0xf(%rsp)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf294
movq %r13, 0x20(%rsp)
movq 0x30(%r13), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
movq %rax, %r12
movl $0x28, %edi
callq 0x5290
movq %rax, %r13
leaq 0x43dd(%rip), %rsi      # 0x1b3e4
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %rbp
movq %r12, %rdi
callq 0x118ec
movq %rbp, %rdi
movq %rax, %rsi
callq 0xfe82
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3ef4(%rip), %rsi      # 0x1af44
movq %rax, %rdi
callq 0xfe82
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xf294
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rbx
cmpq 0x48(%rax), %rbx
je 0x171dc
xorl %r13d, %r13d
movq %r12, %r14
movl $0x28, %edi
callq 0x5290
movq %rax, %rbp
movq %rax, %rdi
leaq 0x4384(%rip), %rsi      # 0x1b426
callq 0xfc44
movq (%rbx), %rax
movq 0x58(%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
xorl %esi, %esi
callq 0x10f8c
movl %eax, %r12d
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %rax, %rdi
movl %r12d, %esi
callq 0xfe48
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq (%rbx), %rax
movq 0x40(%rax), %r12
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq (%r12), %rsi
movq %rax, %rdi
callq 0xfe82
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %rbp
movq %rax, %rdi
leaq 0x42fc(%rip), %rsi      # 0x1b432
callq 0xfc44
movq %r14, %r12
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %r14, %rdi
callq 0x118ec
movq %r15, %rdi
movq %rax, %rsi
callq 0xfe82
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %rax, %rdi
movl %r13d, %esi
callq 0xfe48
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %rax, %rdi
leaq 0x3d9d(%rip), %rsi      # 0x1af44
callq 0xfe82
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf294
incl %r13d
addq $0x8, %rbx
movq 0x18(%rsp), %rax
cmpq 0x48(%rax), %rbx
jne 0x17088
movl $0x28, %edi
callq 0x5290
movq %rax, %r13
leaq 0x424b(%rip), %rsi      # 0x1b43b
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %r12, %rdi
callq 0x118ec
movq 0x10(%rsp), %rbp
movq %r15, %rdi
movq %rax, %rsi
callq 0xfe82
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x41f9(%rip), %rsi      # 0x1b442
movq %rax, %rdi
callq 0xfc44
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r13
movq 0x30(%r13), %rdi
movl (%r12), %esi
movq (%rdi), %rax
callq *0x28(%rax)
cmpb $0x0, 0xf(%rsp)
je 0x172c0
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x41cf(%rip), %rsi      # 0x1b453
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3ca4(%rip), %rsi      # 0x1af44
movq %rax, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x4184(%rip), %rsi      # 0x1b458
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf294
movq 0x28(%r14), %rbx
cmpq 0x30(%r14), %rbx
je 0x1732b
movq (%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *(%rax)
addq $0x8, %rbx
jmp 0x17314
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x412b(%rip), %rsi      # 0x1b46a
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe82
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf294
movq 0x40(%r14), %rbx
cmpq 0x48(%r14), %rbx
je 0x173f2
leaq 0x40de(%rip), %r12      # 0x1b46a
movq (%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *(%rax)
movl $0x28, %edi
callq 0x5290
movq %rax, %r13
movq %rax, %rdi
movq %r12, %rsi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x68(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe82
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf294
addq $0x8, %rbx
cmpq 0x48(%r14), %rbx
movq 0x20(%rsp), %r13
jne 0x1738c
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x4068(%rip), %rsi      # 0x1b46e
movq %rax, %rdi
callq 0xfc44
movl $0x10, %edi
callq 0x5290
movq %rax, %r12
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xfe82
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf294
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xf844
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf294
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3ffc(%rip), %rsi      # 0x1b478
movq %rax, %rdi
callq 0xfc44
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf294
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x68(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xf844
movq %rbp, %rdi
movq %r15, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf294
jmp 0x1753b
jmp 0x174fc
jmp 0x1753b
jmp 0x174fc
jmp 0x1753b
jmp 0x174e6
jmp 0x1753b
movq %rax, %rbx
movl $0x10, %esi
jmp 0x17504
jmp 0x174e6
jmp 0x1753b
jmp 0x174fc
jmp 0x1753b
jmp 0x174fc
movq %rax, %rbx
movl $0x28, %esi
jmp 0x17543
jmp 0x1752c
jmp 0x1753b
jmp 0x1752c
jmp 0x1753b
jmp 0x174fc
jmp 0x1753b
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x17546
jmp 0x1753b
movq %rax, %rbx
movl $0x10, %esi
jmp 0x17523
jmp 0x1751b
jmp 0x1753b
jmp 0x1753b
movq %rax, %rbx
movl $0x28, %esi
movq %rbp, %rdi
jmp 0x17546
jmp 0x1753b
jmp 0x1753b
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x17546
jmp 0x1753b
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5470
nop