// File: /workspace/github_cpp_makefile_star2_O1/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 0x102f0
movq %rax, %rbx
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x42d4(%rip), %rsi      # 0x1b3ab
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movq 0x30(%r13), %r15
movq (%r15), %rax
leaq 0x3dc3(%rip), %rsi      # 0x1aee4
movq %r15, %rdi
callq *0x10(%rax)
movl (%rax), %esi
movq (%r15), %rax
movq %r15, %rdi
callq *(%rax)
movl %eax, %ebp
testb %al, %al
je 0x17187
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x426c(%rip), %rsi      # 0x1b3b7
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3d7d(%rip), %rsi      # 0x1aee4
movq %rax, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x4222(%rip), %rsi      # 0x1b3bd
movq %rax, %rdi
callq 0xfc18
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 0xfe14
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 0xf258
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 0x4173(%rip), %rsi      # 0x1b384
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %rbp
movq %r12, %rdi
callq 0x1196c
movq %rbp, %rdi
movq %rax, %rsi
callq 0xfe4e
movq (%r13), %rax
movq %r13, %rdi
movq %rbp, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3c8a(%rip), %rsi      # 0x1aee4
movq %rax, %rdi
callq 0xfe4e
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %r13, %rsi
callq 0xf258
movq 0x18(%rsp), %rax
movq 0x40(%rax), %rbx
cmpq 0x48(%rax), %rbx
je 0x173e6
xorl %r13d, %r13d
movq %r12, %r14
movl $0x28, %edi
callq 0x5290
movq %rax, %rbp
movq %rax, %rdi
leaq 0x411a(%rip), %rsi      # 0x1b3c6
callq 0xfc18
movq (%rbx), %rax
movq 0x58(%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
xorl %esi, %esi
callq 0x10fac
movl %eax, %r12d
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %rax, %rdi
movl %r12d, %esi
callq 0xfe14
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 0xfe4e
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %rbp
movq %rax, %rdi
leaq 0x4092(%rip), %rsi      # 0x1b3d2
callq 0xfc18
movq %r14, %r12
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %r14, %rdi
callq 0x1196c
movq %r15, %rdi
movq %rax, %rsi
callq 0xfe4e
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 0xfe14
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %rax, %rdi
leaq 0x3b33(%rip), %rsi      # 0x1aee4
callq 0xfe4e
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0xf258
incl %r13d
addq $0x8, %rbx
movq 0x18(%rsp), %rax
cmpq 0x48(%rax), %rbx
jne 0x17292
movl $0x28, %edi
callq 0x5290
movq %rax, %r13
leaq 0x3fe1(%rip), %rsi      # 0x1b3db
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq %r12, %rdi
callq 0x1196c
movq 0x10(%rsp), %rbp
movq %r15, %rdi
movq %rax, %rsi
callq 0xfe4e
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3f8f(%rip), %rsi      # 0x1b3e2
movq %rax, %rdi
callq 0xfc18
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 0x174ca
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x3f65(%rip), %rsi      # 0x1b3f3
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3a3a(%rip), %rsi      # 0x1aee4
movq %rax, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x3f1a(%rip), %rsi      # 0x1b3f8
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf258
movq 0x28(%r14), %rbx
cmpq 0x30(%r14), %rbx
je 0x17535
movq (%rbx), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *(%rax)
addq $0x8, %rbx
jmp 0x1751e
movl $0x28, %edi
callq 0x5290
movq %rax, %r12
leaq 0x3ec1(%rip), %rsi      # 0x1b40a
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe4e
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xf258
movq 0x40(%r14), %rbx
cmpq 0x48(%r14), %rbx
je 0x175fc
leaq 0x3e74(%rip), %r12      # 0x1b40a
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 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x68(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xfe4e
movq (%r13), %rax
movq %r13, %rdi
movq %r15, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r13, %rsi
callq 0xf258
addq $0x8, %rbx
cmpq 0x48(%r14), %rbx
movq 0x20(%rsp), %r13
jne 0x17596
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3dfe(%rip), %rsi      # 0x1b40e
movq %rax, %rdi
callq 0xfc18
movl $0x10, %edi
callq 0x5290
movq %rax, %r12
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r12, %rdi
callq 0xfe4e
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf258
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xf81c
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf258
movl $0x28, %edi
callq 0x5290
movq %rax, %r15
leaq 0x3d92(%rip), %rsi      # 0x1b418
movq %rax, %rdi
callq 0xfc18
movq %rbp, %rdi
movq %r15, %rsi
callq 0xf258
movl $0x10, %edi
callq 0x5290
movq %rax, %r15
movq 0x68(%r14), %rax
movq (%rax), %rsi
movq %r15, %rdi
callq 0xf81c
movq %rbp, %rdi
movq %r15, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xf258
jmp 0x17745
jmp 0x17706
jmp 0x17745
jmp 0x17706
jmp 0x17745
jmp 0x176f0
jmp 0x17745
movq %rax, %rbx
movl $0x10, %esi
jmp 0x1770e
jmp 0x176f0
jmp 0x17745
jmp 0x17706
jmp 0x17745
jmp 0x17706
movq %rax, %rbx
movl $0x28, %esi
jmp 0x1774d
jmp 0x17736
jmp 0x17745
jmp 0x17736
jmp 0x17745
jmp 0x17706
jmp 0x17745
movq %rax, %rbx
movl $0x28, %esi
movq %r12, %rdi
jmp 0x17750
jmp 0x17745
movq %rax, %rbx
movl $0x10, %esi
jmp 0x1772d
jmp 0x17725
jmp 0x17745
jmp 0x17745
movq %rax, %rbx
movl $0x28, %esi
movq %rbp, %rdi
jmp 0x17750
jmp 0x17745
jmp 0x17745
movq %rax, %rbx
movl $0x28, %esi
movq %r13, %rdi
jmp 0x17750
jmp 0x17745
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x52a0
movq %rbx, %rdi
callq 0x5470
nop