// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
incl 0x8(%rdi)
movq %rsi, %r14
movq 0x10(%rdi), %rax
movq (%rax), %rdi
pushq $0x2
popq %rsi
callq 0xe61c
movq %rax, %rbp
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3eb0(%rip), %rsi      # 0x1842b
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
movq 0x30(%r12), %rbx
movq (%rbx), %rax
leaq 0x39a1(%rip), %rsi      # 0x17f64
movq %rbx, %rdi
callq *0x10(%rax)
movl (%rax), %esi
movq (%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
movb %al, 0x7(%rsp)
testb %al, %al
je 0x14626
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3e4a(%rip), %rsi      # 0x18437
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
leaq 0x395d(%rip), %rsi      # 0x17f64
movq %rax, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3e05(%rip), %rsi      # 0x1843d
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x48(%r14), %rsi
subq 0x40(%r14), %rsi
shrq $0x3, %rsi
movq %rax, %rdi
callq 0xe17c
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
movq %r12, 0x18(%rsp)
movq 0x30(%r12), %rdi
movq (%rdi), %rax
callq *0x50(%rax)
movq %rax, %rbx
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3d69(%rip), %rsi      # 0x18404
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %r12
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0xfa1e
movq %r12, %rdi
movq %rax, %rsi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x20(%rax)
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
leaq 0x3880(%rip), %rsi      # 0x17f64
movq %rax, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
movq %r14, 0x8(%rsp)
movq 0x40(%r14), %r14
xorl %r13d, %r13d
pushq $0x10
popq %r15
movq %rbp, 0x20(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rbx
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r12
cmpq %rbx, %r14
je 0x14861
movq %r12, %rdi
leaq 0x3d07(%rip), %rsi      # 0x18446
callq 0xdf60
movq (%r14), %rax
movq 0x58(%rax), %rax
movq 0x8(%rax), %rax
movq 0x18(%rax), %rdi
xorl %esi, %esi
callq 0xf264
movl %eax, %ebp
movq %r15, %rdi
callq 0x5260
movq %rax, %rbx
movq %rax, %rdi
movl %ebp, %esi
callq 0xe17c
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq (%r14), %rax
movq 0x40(%rax), %rbp
movq %r15, %rdi
callq 0x5260
movq %rax, %rbx
movq (%rbp), %rsi
movq %rax, %rdi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r12
movq %rax, %rdi
leaq 0x3c84(%rip), %rsi      # 0x18452
callq 0xdf60
movq %r15, %rdi
callq 0x5260
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0xfa1e
movq %rbx, %rdi
movq %rax, %rsi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %r15, %rdi
callq 0x5260
movq %rax, %rbx
movq %rax, %rdi
movl %r13d, %esi
callq 0xe17c
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %r15, %rdi
callq 0x5260
movq %rax, %rbx
movq %rax, %rdi
leaq 0x372c(%rip), %rsi      # 0x17f64
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd750
incl %r13d
addq $0x8, %r14
jmp 0x14718
leaq 0x3bf3(%rip), %rsi      # 0x1845b
movq %r12, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x10(%rsp), %rdi
callq 0xfa1e
movq 0x8(%rsp), %r14
movq %rbx, %rdi
movq %rax, %rsi
callq 0xe1b2
movq (%r12), %rax
movq %r12, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r12, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %rbx
leaq 0x3ba3(%rip), %rsi      # 0x18462
movq %rax, %rdi
callq 0xdf60
movq 0x18(%rsp), %r12
movq 0x30(%r12), %rdi
movq 0x10(%rsp), %rax
movl (%rax), %esi
movq (%rdi), %rax
callq *0x28(%rax)
cmpb $0x0, 0x7(%rsp)
je 0x14930
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3b7c(%rip), %rsi      # 0x18473
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
leaq 0x3653(%rip), %rsi      # 0x17f64
movq %rax, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3b36(%rip), %rsi      # 0x18478
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
movq 0x28(%r14), %rbx
cmpq 0x30(%r14), %rbx
je 0x14996
movq (%rbx), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *(%rax)
addq $0x8, %rbx
jmp 0x1497f
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3ae2(%rip), %rsi      # 0x1848a
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
movq 0x40(%r14), %r14
pushq $0x28
popq %r15
leaq 0x3a9a(%rip), %r12      # 0x1848a
pushq $0x10
popq %r13
movq 0x8(%rsp), %rax
cmpq 0x48(%rax), %r14
je 0x14a64
movq (%r14), %rdi
movq (%rdi), %rax
movq 0x18(%rsp), %rsi
callq *(%rax)
movq %r15, %rdi
callq 0x5260
movq %rax, %rbp
movq %rax, %rdi
movq %r12, %rsi
callq 0xdf60
movq %r13, %rdi
callq 0x5260
movq %rax, %rbx
movq 0x8(%rsp), %rax
movq 0x68(%rax), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xe1b2
movq (%rbp), %rax
movq %rbp, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
movq %rdi, %rbp
callq 0xd750
addq $0x8, %r14
jmp 0x149f4
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %r15
leaq 0x3a18(%rip), %rsi      # 0x1848e
movq %rax, %rdi
callq 0xdf60
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x8(%rsp), %r14
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xe1b2
movq (%r15), %rax
movq %r15, %rdi
movq %rbx, %rsi
callq *0x20(%rax)
movq %rbp, %rdi
movq %r15, %rsi
callq 0xd750
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x60(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xdc40
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xd750
pushq $0x28
popq %rdi
callq 0x5260
movq %rax, %rbx
leaq 0x39ad(%rip), %rsi      # 0x18498
movq %rax, %rdi
callq 0xdf60
movq %rbp, %rdi
movq %rbx, %rsi
callq 0xd750
pushq $0x10
popq %rdi
callq 0x5260
movq %rax, %rbx
movq 0x68(%r14), %rax
movq (%rax), %rsi
movq %rbx, %rdi
callq 0xdc40
movq %rbp, %rdi
movq %rbx, %rsi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd750
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
jmp 0x14b4b
jmp 0x14b94
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
jmp 0x14b5e
movq %rax, %r14
pushq $0x28
jmp 0x14b99
jmp 0x14b89
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
jmp 0x14b5e
jmp 0x14b94
movq %rax, %r14
pushq $0x28
popq %rsi
movq %r15, %rdi
jmp 0x14b9d
jmp 0x14b94
movq %rax, %r14
pushq $0x10
jmp 0x14b8e
movq %rax, %r14
pushq $0x28
popq %rsi
movq %rbp, %rdi
jmp 0x14b9d
jmp 0x14b94
jmp 0x14b94
jmp 0x14b94
jmp 0x14b89
jmp 0x14b94
jmp 0x14b94
movq %rax, %r14
pushq $0x28
popq %rsi
movq %r12, %rdi
jmp 0x14b9d
movq %rax, %r14
pushq $0x10
popq %rsi
movq %rbx, %rdi
callq 0x5270
movq %r14, %rdi
callq 0x5420