// File: marxn[P]pupp/src/parser/transaction.cpp
// Function: TransNode::Invoke(NodeContext*)
int TransNode::Invoke(NodeContext * context)
{
        list<TransIdentDesc>::iterator vd;
                
        for(vd = this->VarDefList.begin(); vd!= this->VarDefList.end(); vd++)
        {
                string varname = vd->SrcVardef->GetVarName();
                
                unsigned long src_layer = vd->SrcLayer;
                unsigned long src_index = vd->SrcVardef->GetVarIndex();
                unsigned long dst_index = vd->DstVarDef->GetVarIndex();
                
                Variable * var = context->GetVariable(0, dst_index);
                if(var==NULL)
                {
                        cerr<<"pupp runtime error: cannot find variable "<<varname<<" in current context."<<endl;
                        return NODE_RET_ERROR;
                }
                
                Variable * origin = context->GetVariable(src_layer, src_index);
                if(origin==NULL)
                {
                        cerr<<"pupp runtime error: cannot find variable: "<<varname<<" in outer context"<<endl;
                        return NODE_RET_ERROR;
                }

                //Copy the value of source variable to local transaction variable.
                //Note: var type must be figure out because any var type is determined in run-time.
                ConstValue * value = origin->GetValue();
                DataType type = origin->GetVarType();
                var->SetRef(value);
                var->SetVarType(type);
        }

        list<Node*>::iterator i;
        for(i = subnodelist->begin(); i != subnodelist->end(); i++)
        {
                context->AddFrame(*i);
                int cond = (*i)->Execute(context);
                context->PopFrame();

                if(cond==NODE_RET_NEEDROLLBACK)
                {
                        return NODE_RET_NORMAL;
                }
                else if(cond==NODE_RET_NEEDCOMMIT)
                {
                        list<TransIdentDesc>::iterator vd;
                        for(vd = this->VarDefList.begin(); vd!= this->VarDefList.end(); vd++)
                        {
                                string varname = vd->SrcVardef->GetVarName();
                                
                                unsigned long src_layer = vd->SrcLayer;
                                unsigned long src_index = vd->SrcVardef->GetVarIndex();
                                unsigned long dst_index = vd->DstVarDef->GetVarIndex();
                
                                Variable * var = context->GetVariable(0, dst_index);
                                
                                if(var==NULL)
                                {
                                        cerr<<"pupp runtime error: cannot find variable "<<varname<<" in current context."<<endl;
                                        return NODE_RET_ERROR;
                                }

                                Variable * origin = context->GetVariable(src_layer, src_index);
                                if(origin==NULL)
                                {
                                        cerr<<"pupp runtime error: cannot find variable: "<<varname<<" in outer context"<<endl;
                                        return NODE_RET_ERROR;
                                }

                                ConstValue * value = var->GetValue();
                                origin->SetRef(value);
                                //delete value;
                        }

                        return NODE_RET_NORMAL;
                }
                else if(cond!=NODE_RET_NORMAL)
                {
                        return cond;
                }
        }

        return NODE_RET_NORMAL;
}

// Assembly:
subq $0x218, %rsp            # imm = 0x218
movq %rdi, 0x208(%rsp)
movq %rsi, 0x200(%rsp)
movq 0x208(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x300d0
movq 0xd0(%rsp), %rdi
addq $0x70, %rdi
callq 0x300f0
movq %rax, 0x1f0(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x1f8(%rsp)
movq 0xd0(%rsp), %rdi
addq $0x70, %rdi
callq 0x30140
movq %rax, 0x1e8(%rsp)
leaq 0x1f8(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x30120
testb $0x1, %al
jne 0x2f614
jmp 0x2f920
leaq 0x1f8(%rsp), %rdi
movq %rdi, 0xc0(%rsp)
callq 0x30170
movq (%rax), %rsi
leaq 0x1c8(%rsp), %rdi
callq 0x31030
movq 0xc0(%rsp), %rdi
callq 0x30170
movq 0xc0(%rsp), %rdi
movq 0x10(%rax), %rax
movq %rax, 0x1c0(%rsp)
callq 0x30170
movq (%rax), %rdi
callq 0x310b0
movq %rax, 0xc8(%rsp)
jmp 0x2f671
movq 0xc8(%rsp), %rax
movq %rax, 0x1b8(%rsp)
leaq 0x1f8(%rsp), %rdi
callq 0x30170
movq 0x8(%rax), %rdi
callq 0x310b0
movq %rax, 0xb8(%rsp)
jmp 0x2f6a1
movq 0xb8(%rsp), %rax
movq %rax, 0x1a0(%rsp)
movq 0x200(%rsp), %rdi
movq 0x1a0(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x14950
movq %rax, 0xb0(%rsp)
jmp 0x2f6d4
movq 0xb0(%rsp), %rax
movq %rax, 0x198(%rsp)
cmpq $0x0, 0x198(%rsp)
jne 0x2f7a4
movq 0x468f6(%rip), %rdi     # 0x75ff0
leaq 0x319ab(%rip), %rsi     # 0x610ac
callq 0x62d0
movq %rax, 0xa8(%rsp)
jmp 0x2f710
movq 0xa8(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x62a0
movq %rax, 0xa0(%rsp)
jmp 0x2f72f
movq 0xa0(%rsp), %rdi
leaq 0x31998(%rip), %rsi     # 0x610d6
callq 0x62d0
movq %rax, 0x98(%rsp)
jmp 0x2f74d
movq 0x98(%rsp), %rdi
movq 0x4685c(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2f763
movl $0xffffffff, 0x214(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x194(%rsp)
jmp 0x2f8e3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x61e0
jmp 0x2fd6d
movq 0x200(%rsp), %rdi
movq 0x1c0(%rsp), %rsi
movq 0x1b8(%rsp), %rdx
callq 0x14950
movq %rax, 0x90(%rsp)
jmp 0x2f7cb
movq 0x90(%rsp), %rax
movq %rax, 0x188(%rsp)
cmpq $0x0, 0x188(%rsp)
jne 0x2f86c
movq 0x467ff(%rip), %rdi     # 0x75ff0
leaq 0x2fc0c(%rip), %rsi     # 0x5f404
callq 0x62d0
movq %rax, 0x88(%rsp)
jmp 0x2f807
movq 0x88(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x62a0
movq %rax, 0x80(%rsp)
jmp 0x2f826
movq 0x80(%rsp), %rdi
leaq 0x318b6(%rip), %rsi     # 0x610eb
callq 0x62d0
movq %rax, 0x78(%rsp)
jmp 0x2f841
movq 0x78(%rsp), %rdi
movq 0x4676b(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2f854
movl $0xffffffff, 0x214(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x194(%rsp)
jmp 0x2f8e3
movq 0x188(%rsp), %rdi
callq 0x30bc0
movq %rax, 0x70(%rsp)
jmp 0x2f880
movq 0x70(%rsp), %rax
movq %rax, 0x180(%rsp)
movq 0x188(%rsp), %rdi
callq 0x30cb0
movl %eax, 0x6c(%rsp)
jmp 0x2f8a0
movl 0x6c(%rsp), %eax
movl %eax, 0x17c(%rsp)
movq 0x198(%rsp), %rdi
movq 0x180(%rsp), %rsi
callq 0x30a40
jmp 0x2f8c2
movq 0x198(%rsp), %rdi
movl 0x17c(%rsp), %esi
callq 0x30c90
jmp 0x2f8d8
movl $0x0, 0x194(%rsp)
leaq 0x1c8(%rsp), %rdi
callq 0x61e0
movl 0x194(%rsp), %eax
testl %eax, %eax
je 0x2f902
jmp 0x2f8fd
jmp 0x2fd5e
jmp 0x2f904
leaq 0x1f8(%rsp), %rdi
xorl %esi, %esi
callq 0x30190
movq %rax, 0x170(%rsp)
jmp 0x2f5dd
leaq 0x168(%rsp), %rdi
callq 0xd4a0
movq 0xd0(%rsp), %rax
movq 0x60(%rax), %rdi
callq 0xd4c0
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x168(%rsp)
movq 0xd0(%rsp), %rax
movq 0x60(%rax), %rdi
callq 0xd510
movq %rax, 0x158(%rsp)
leaq 0x168(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0xd4f0
testb $0x1, %al
jne 0x2f98d
jmp 0x2fd53
movq 0x200(%rsp), %rax
movq %rax, 0x60(%rsp)
leaq 0x168(%rsp), %rdi
callq 0xd540
movq 0x60(%rsp), %rdi
movq (%rax), %rsi
callq 0x146f0
leaq 0x168(%rsp), %rdi
callq 0xd540
movq (%rax), %rdi
movq 0x200(%rsp), %rsi
callq 0x14130
movl %eax, 0x154(%rsp)
movq 0x200(%rsp), %rdi
callq 0x14610
cmpl $0x4, 0x154(%rsp)
jne 0x2f9ff
movl $0x0, 0x214(%rsp)
jmp 0x2fd5e
cmpl $0x5, 0x154(%rsp)
jne 0x2fd17
leaq 0x148(%rsp), %rdi
callq 0x300d0
movq 0xd0(%rsp), %rdi
addq $0x70, %rdi
callq 0x300f0
movq %rax, 0x140(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x148(%rsp)
movq 0xd0(%rsp), %rdi
addq $0x70, %rdi
callq 0x30140
movq %rax, 0x138(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x30120
testb $0x1, %al
jne 0x2fa7a
jmp 0x2fd0a
leaq 0x148(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x30170
movq (%rax), %rsi
leaq 0x118(%rsp), %rdi
callq 0x31030
movq 0x50(%rsp), %rdi
callq 0x30170
movq 0x50(%rsp), %rdi
movq 0x10(%rax), %rax
movq %rax, 0x110(%rsp)
callq 0x30170
movq (%rax), %rdi
callq 0x310b0
movq %rax, 0x58(%rsp)
jmp 0x2facb
movq 0x58(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x30170
movq 0x8(%rax), %rdi
callq 0x310b0
movq %rax, 0x48(%rsp)
jmp 0x2faf5
movq 0x48(%rsp), %rax
movq %rax, 0x100(%rsp)
movq 0x200(%rsp), %rdi
movq 0x100(%rsp), %rdx
xorl %eax, %eax
movl %eax, %esi
callq 0x14950
movq %rax, 0x40(%rsp)
jmp 0x2fb22
movq 0x40(%rsp), %rax
movq %rax, 0xf8(%rsp)
cmpq $0x0, 0xf8(%rsp)
jne 0x2fbdd
movq 0x464ab(%rip), %rdi     # 0x75ff0
leaq 0x31560(%rip), %rsi     # 0x610ac
callq 0x62d0
movq %rax, 0x38(%rsp)
jmp 0x2fb58
movq 0x38(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x62a0
movq %rax, 0x30(%rsp)
jmp 0x2fb71
movq 0x30(%rsp), %rdi
leaq 0x31559(%rip), %rsi     # 0x610d6
callq 0x62d0
movq %rax, 0x28(%rsp)
jmp 0x2fb89
movq 0x28(%rsp), %rdi
movq 0x46423(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2fb9c
movl $0xffffffff, 0x214(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x194(%rsp)
jmp 0x2fcd0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x61e0
jmp 0x2fd6d
movq 0x200(%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x108(%rsp), %rdx
callq 0x14950
movq %rax, 0x20(%rsp)
jmp 0x2fc01
movq 0x20(%rsp), %rax
movq %rax, 0xf0(%rsp)
cmpq $0x0, 0xf0(%rsp)
jne 0x2fc8f
movq 0x463d0(%rip), %rdi     # 0x75ff0
leaq 0x2f7dd(%rip), %rsi     # 0x5f404
callq 0x62d0
movq %rax, 0x18(%rsp)
jmp 0x2fc33
movq 0x18(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x62a0
movq %rax, 0x10(%rsp)
jmp 0x2fc4c
movq 0x10(%rsp), %rdi
leaq 0x31493(%rip), %rsi     # 0x610eb
callq 0x62d0
movq %rax, 0x8(%rsp)
jmp 0x2fc64
movq 0x8(%rsp), %rdi
movq 0x46348(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2fc77
movl $0xffffffff, 0x214(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x194(%rsp)
jmp 0x2fcd0
movq 0xf8(%rsp), %rdi
callq 0x30bc0
movq %rax, (%rsp)
jmp 0x2fca2
movq (%rsp), %rax
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
movq 0xe8(%rsp), %rsi
callq 0x30a40
jmp 0x2fcc5
movl $0x0, 0x194(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x61e0
movl 0x194(%rsp), %eax
testl %eax, %eax
je 0x2fcec
jmp 0x2fcea
jmp 0x2fd5e
jmp 0x2fcee
leaq 0x148(%rsp), %rdi
xorl %esi, %esi
callq 0x30190
movq %rax, 0xe0(%rsp)
jmp 0x2fa43
movl $0x0, 0x214(%rsp)
jmp 0x2fd5e
cmpl $0x0, 0x154(%rsp)
je 0x2fd31
movl 0x154(%rsp), %eax
movl %eax, 0x214(%rsp)
jmp 0x2fd5e
jmp 0x2fd33
jmp 0x2fd35
jmp 0x2fd37
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
callq 0xd570
movq %rax, 0xd8(%rsp)
jmp 0x2f956
movl $0x0, 0x214(%rsp)
movl 0x214(%rsp), %eax
addq $0x218, %rsp            # imm = 0x218
retq
movq 0x1b0(%rsp), %rdi
callq 0x6550
nopw (%rax,%rax)