// File: marxn[P]pupp/src/parser/statement.cpp
// Function: VarDefinitionStatement::Invoke(NodeContext*)
int VarDefinitionStatement::Invoke(NodeContext * context)
{
        string name = *(this->Ident);
        Variable * var = context->GetVariable(1, this->VarDef->GetVarIndex());

        if(var==NULL)
        {
                cerr<<"pupp runtime error: cannot find variable:"<<name<<endl;
                return NODE_RET_ERROR;
        }

        if(this->VarType->GetVarType()==Array)
        {
                list<Expression*> * dim = this->VarType->GetDimentions();
                vector<long> desc;
                long size = 1;

                list<Expression*>::iterator i;
                for(i = dim->begin(); i!= dim->end(); i++)
                {
                        ConstValue * dsize = (*i)->Calculate(context);
                        if(dsize->GetType()!=Integer || static_cast<IntegerValue*>(dsize)->GetValue()<=0)
                        {
                                cerr<<"pupp runtime error: Size of an array must be a positive integer."<<endl;
                                delete dsize;
                                return NODE_RET_ERROR;
                        }

                        delete dsize;

                        long n = static_cast<IntegerValue*>(dsize)->GetValue();
                        size *= n;
                        desc.push_back(n);
                }

                ArrayValue * val = new ArrayValue(this->VarType->GetElementType(), desc, size);
                var->SetRef(val);
                return NODE_RET_NORMAL;
        }

        if(this->InitExpr!=NULL)
        {
                ConstValue * value = this->InitExpr->Calculate(context);
                if(value==NULL)
                {
                        return NODE_RET_ERROR;
                }

                if(value->GetType()==Null)
                {
                        cerr<<"pupp runtime error: variable type cannot be determined."<<endl;
                        delete value;
                        return NODE_RET_ERROR;
                }
                var->SetVarType(value->GetType());
                var->SetRef(value);
        }

        return NODE_RET_NORMAL;
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0x70(%rax), %rsi
leaq 0x120(%rsp), %rdi
callq 0x6160
movq 0x88(%rsp), %rax
movq 0x140(%rsp), %rcx
movq %rcx, 0x90(%rsp)
movq 0x60(%rax), %rdi
callq 0x310b0
movq %rax, 0x98(%rsp)
jmp 0x2e223
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rdi
movl $0x1, %esi
callq 0x14950
movq %rax, 0x80(%rsp)
jmp 0x2e247
movq 0x80(%rsp), %rax
movq %rax, 0x118(%rsp)
cmpq $0x0, 0x118(%rsp)
jne 0x2e2dc
movq 0x47d87(%rip), %rdi     # 0x75ff0
leaq 0x30eb7(%rip), %rsi     # 0x5f127
callq 0x62d0
movq %rax, 0x78(%rsp)
jmp 0x2e27c
movq 0x78(%rsp), %rdi
leaq 0x120(%rsp), %rsi
callq 0x62a0
movq %rax, 0x70(%rsp)
jmp 0x2e295
movq 0x70(%rsp), %rdi
movq 0x47d17(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2e2a8
movl $0xffffffff, 0x154(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x108(%rsp)
jmp 0x2e717
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x2e733
movq 0x88(%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x2e0e0
cmpl $0x7, %eax
jne 0x2e5d1
movq 0x88(%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x2e150
movq %rax, 0x100(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0xadc0
movq $0x1, 0xe0(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x11270
movq 0x100(%rsp), %rdi
callq 0x11290
movq %rax, 0xd0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x100(%rsp), %rdi
callq 0x112e0
movq %rax, 0xc8(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x112c0
testb $0x1, %al
jne 0x2e38d
jmp 0x2e50c
leaq 0xd8(%rsp), %rdi
callq 0x11310
movq (%rax), %rdi
movq 0x140(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, 0x68(%rsp)
jmp 0x2e3b5
movq 0x68(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x7870
movl %eax, 0x64(%rsp)
jmp 0x2e3d5
movl 0x64(%rsp), %eax
cmpl $0x1, %eax
jne 0x2e3fd
movq 0xc0(%rsp), %rdi
callq 0x7fe0
movq %rax, 0x58(%rsp)
jmp 0x2e3f2
movq 0x58(%rsp), %rax
cmpq $0x0, %rax
jg 0x2e47c
movq 0x47bec(%rip), %rdi     # 0x75ff0
leaq 0x32bba(%rip), %rsi     # 0x60fc5
callq 0x62d0
movq %rax, 0x50(%rsp)
jmp 0x2e417
movq 0x50(%rsp), %rdi
movq 0x47b95(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2e42a
movq 0xc0(%rsp), %rax
movq %rax, 0x48(%rsp)
cmpq $0x0, %rax
je 0x2e448
movq 0x48(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0xffffffff, 0x154(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x108(%rsp)
jmp 0x2e5ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
jmp 0x2e5bf
movq 0xc0(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, %rax
je 0x2e49a
movq 0x40(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq 0xc0(%rsp), %rdi
callq 0x7fe0
movq %rax, 0x38(%rsp)
jmp 0x2e4ae
movq 0x38(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rcx
movq 0xe0(%rsp), %rax
imulq %rcx, %rax
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x2f040
jmp 0x2e4ee
jmp 0x2e4f0
leaq 0xd8(%rsp), %rdi
xorl %esi, %esi
callq 0x11380
movq %rax, 0xb0(%rsp)
jmp 0x2e35a
movl $0x38, %edi
callq 0x62f0
movq %rax, 0x30(%rsp)
jmp 0x2e51d
movq 0x88(%rsp), %rax
movq 0x78(%rax), %rdi
callq 0x2e0f0
movq 0x30(%rsp), %rdi
movl %eax, %esi
movq 0xe0(%rsp), %rcx
leaq 0xe8(%rsp), %rdx
callq 0x9df0
jmp 0x2e54c
movq 0x30(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x118(%rsp), %rdi
movq 0xa8(%rsp), %rsi
callq 0x30a40
jmp 0x2e570
movl $0x0, 0x154(%rsp)
movl $0x1, 0x108(%rsp)
jmp 0x2e5ad
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x110(%rsp)
movl %eax, 0x10c(%rsp)
movl $0x38, %esi
callq 0x6300
jmp 0x2e5bf
leaq 0xe8(%rsp), %rdi
callq 0xb090
jmp 0x2e717
leaq 0xe8(%rsp), %rdi
callq 0xb090
jmp 0x2e733
movq 0x88(%rsp), %rax
cmpq $0x0, 0x68(%rax)
je 0x2e701
movq 0x88(%rsp), %rax
movq 0x68(%rax), %rdi
movq 0x140(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, 0x28(%rsp)
jmp 0x2e608
movq 0x28(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x2e63b
movl $0xffffffff, 0x154(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x108(%rsp)
jmp 0x2e717
movq 0xa0(%rsp), %rdi
callq 0x7870
movl %eax, 0x24(%rsp)
jmp 0x2e64e
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x2e6ba
movq 0x47992(%rip), %rdi     # 0x75ff0
leaq 0x329a1(%rip), %rsi     # 0x61006
callq 0x62d0
movq %rax, 0x18(%rsp)
jmp 0x2e671
movq 0x18(%rsp), %rdi
movq 0x4793b(%rip), %rsi     # 0x75fb8
callq 0x6320
jmp 0x2e684
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x2e6a2
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl $0xffffffff, 0x154(%rsp) # imm = 0xFFFFFFFF
movl $0x1, 0x108(%rsp)
jmp 0x2e717
movq 0x118(%rsp), %rax
movq %rax, (%rsp)
movq 0xa0(%rsp), %rdi
callq 0x7870
movl %eax, 0xc(%rsp)
jmp 0x2e6d9
movl 0xc(%rsp), %esi
movq (%rsp), %rdi
callq 0x30c90
jmp 0x2e6e8
movq 0x118(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x30a40
jmp 0x2e6ff
jmp 0x2e701
movl $0x0, 0x154(%rsp)
movl $0x1, 0x108(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x61e0
movl 0x154(%rsp), %eax
addq $0x158, %rsp            # imm = 0x158
retq
leaq 0x120(%rsp), %rdi
callq 0x61e0
movq 0x110(%rsp), %rdi
callq 0x6550
nopl (%rax)