// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
movq %rsp, %r12
movq $0x0, (%r12)
movl (%rsi), %ebp
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x58(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r13
movq %r13, %rdi
callq 0xc6dc
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x28507
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x11fa3
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rsp), %rdx
testq %rdx, %rdx
jne 0x1200e
movl 0x8(%r15), %ebp
movq 0x10(%r15), %rax
movq 0x10(%rax), %rax
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0xc6dc
leaq 0x5ca01(%rip), %rsi     # 0x6e9e0
movq %r12, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b6aa
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1200a
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq (%rsp), %rdx
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
movq %rcx, %rsi
subq %rax, %rsi
sarq $0x4, %rsi
movq 0x10(%rdx), %rdi
subq 0x8(%rdx), %rdi
sarq $0x2, %rdi
cmpq %rdi, %rsi
je 0x12097
movl 0x8(%r15), %ebp
movq 0x10(%r15), %rax
movq 0x10(%rax), %rax
leaq 0x18(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0xc6dc
leaq 0x5c9a2(%rip), %rsi     # 0x6ea06
movq %r12, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b6aa
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1208f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
cmpq %rax, %rcx
je 0x12100
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rax,%r13), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x18(%rax)
movl 0xc(%rbx), %edx
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
leaq -0x1(%rcx), %rsi
cmpq %rsi, %rbp
jne 0x120d1
shll $0x2, %edx
jmp 0x120de
movq (%rsp), %rsi
movq 0x20(%rsi), %rsi
imull 0x4(%rsi,%rbp,4), %edx
addl %edx, %r12d
incq %rbp
addq $0x10, %r13
cmpq %rbp, %rcx
ja 0x120a1
leal 0x3(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
sarl $0x2, %eax
cltq
jmp 0x12102
xorl %eax, %eax
movq (%rsp), %rcx
movq 0x38(%rcx), %rcx
movl (%rcx,%rax,4), %edx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movl $0x1, %esi
callq 0x1279c
addq $0x8, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x289a2
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12149
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x82a0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1215e
jmp 0x1215e
jmp 0x1215e
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x121a9
movq 0x18(%rsp), %rsi
jmp 0x121a1
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x121a9
movq 0x20(%rsp), %rsi
jmp 0x121a1
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x121a9
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540
nop