// File: luooofan[P]hblzbd_compiler/src/evaluate.cpp
// Function: ast::ArrayIdentifier::Evaluate(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayIdentifier::Evaluate(ir::ContextInfo& ctx, std::vector<ir::IR*>& gIRList) {
  ir::SymbolTableItem* s = nullptr;
  int scope_id = ir::FindSymbol(ctx.scope_id_, name_.name_, s);
  if (!s) {
    ir::SemanticError(line_no_, name_.name_ + ": undefined variable");
  }
  if (shape_list_.size() != s->shape_.size()) {
    ir::SemanticError(line_no_, name_.name_ + ": the dimension of array is not correct");
  }

  int index, t;

  for (int i = 0; i < shape_list_.size(); ++i) {
    shape_list_[i]->Evaluate(ctx, gIRList);
    t = ctx.opn_.imm_num_;
    if (i == shape_list_.size() - 1) {
      index += t * 4;
    } else {
      index += t * s->width_[i + 1];
    }
  }

  ctx.opn_ = ir::Opn(ir::Opn::Type::Imm, s->initval_[index / 4]);
}

// Assembly:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x150(%rsp)
movq %rsi, 0x148(%rsp)
movq %rdx, 0x140(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x138(%rsp)
movq 0x148(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0x5c(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x110(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x81a0
movl 0x5c(%rsp), %edi
movq 0x60(%rsp), %rsi
leaq 0x138(%rsp), %rdx
callq 0x42f30
movl %eax, 0x6c(%rsp)
jmp 0x1e40c
leaq 0x110(%rsp), %rdi
callq 0x8728
movl 0x6c(%rsp), %eax
movl %eax, 0x134(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x1e4cb
movq 0x50(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0x44(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xdc51e(%rip), %rdx     # 0xfa96c
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xc720
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x47ce0
jmp 0x1e470
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x1e4cb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x8728
jmp 0x1e713
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x8728
jmp 0x1e713
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
callq 0x1f230
movq %rax, 0x38(%rsp)
movq 0x138(%rsp), %rdi
addq $0x8, %rdi
callq 0x10a40
movq %rax, %rcx
movq 0x38(%rsp), %rax
cmpq %rcx, %rax
je 0x1e56e
movq 0x50(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0x2c(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xdc47b(%rip), %rdx     # 0xfa992
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xc720
movl 0x2c(%rsp), %edi
movq 0x30(%rsp), %rsi
callq 0x47ce0
jmp 0x1e539
leaq 0xc0(%rsp), %rdi
callq 0x8728
jmp 0x1e56e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x8728
jmp 0x1e713
movl $0x0, 0xb4(%rsp)
movq 0x50(%rsp), %rdi
movslq 0xb4(%rsp), %rax
movq %rax, 0x20(%rsp)
addq $0x18, %rdi
callq 0x1f230
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x1e68d
movq 0x50(%rsp), %rdi
addq $0x18, %rdi
movslq 0xb4(%rsp), %rsi
callq 0x1f250
movq %rax, %rdi
callq 0x14c70
movq %rax, %rdi
movq 0x148(%rsp), %rsi
movq 0x140(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x50(%rsp), %rdi
movq 0x148(%rsp), %rax
movl 0xc(%rax), %eax
movl %eax, 0xb8(%rsp)
movslq 0xb4(%rsp), %rax
movq %rax, 0x18(%rsp)
addq $0x18, %rdi
callq 0x1f230
movq %rax, %rcx
movq 0x18(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jne 0x1e634
movl 0xb8(%rsp), %eax
shll $0x2, %eax
addl 0xbc(%rsp), %eax
movl %eax, 0xbc(%rsp)
jmp 0x1e675
movl 0xb8(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x138(%rsp), %rdi
addq $0x20, %rdi
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x10a60
movq %rax, %rcx
movl 0x14(%rsp), %eax
imull (%rcx), %eax
addl 0xbc(%rsp), %eax
movl %eax, 0xbc(%rsp)
jmp 0x1e677
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x1e579
movq 0x138(%rsp), %rdi
addq $0x38, %rdi
movl 0xbc(%rsp), %eax
movl %eax, %ecx
addl $0x3, %ecx
testl %eax, %eax
cmovsl %ecx, %eax
sarl $0x2, %eax
movslq %eax, %rsi
callq 0x10a60
movl (%rax), %edx
leaq 0x70(%rsp), %rdi
movq %rdi, 0x8(%rsp)
movl $0x1, %esi
callq 0x1f160
movq 0x8(%rsp), %rsi
movq 0x148(%rsp), %rdi
addq $0x8, %rdi
callq 0x433c0
jmp 0x1e6e3
leaq 0x70(%rsp), %rdi
callq 0x1f210
addq $0x158, %rsp            # imm = 0x158
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x108(%rsp)
movl %eax, 0x104(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x1f210
movq 0x108(%rsp), %rdi
callq 0x8670