// File: /workspace/github_cpp_makefile_star2_O1/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 0xca10
movl %ebp, %edi
movq %r13, %rsi
movq %r12, %rdx
callq 0x27e6f
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x122e5
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
cmpq $0x0, (%rsp)
jne 0x1234a
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 0xca10
leaq 0x5d6c1(%rip), %rsi     # 0x6f9e0
movq %r12, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b12a
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1234a
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%r15), %rax
subq 0x18(%r15), %rax
sarq $0x4, %rax
movq (%rsp), %rcx
movq 0x10(%rcx), %rdx
subq 0x8(%rcx), %rdx
sarq $0x2, %rdx
cmpq %rdx, %rax
je 0x123c9
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 0xca10
leaq 0x5d668(%rip), %rsi     # 0x6fa06
movq %r12, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b12a
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x123c9
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x18(%r15), %rax
cmpq %rax, 0x20(%r15)
je 0x12442
xorl %r13d, %r13d
xorl %ebp, %ebp
movq (%rax,%r13), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x18(%rax)
movq 0x20(%r15), %rcx
subq 0x18(%r15), %rcx
movl 0xc(%rbx), %eax
sarq $0x4, %rcx
decq %rcx
cmpq %rcx, %rbp
jne 0x12404
shll $0x2, %eax
jmp 0x12411
movq (%rsp), %rcx
movq 0x20(%rcx), %rcx
imull 0x4(%rcx,%rbp,4), %eax
addl %eax, %r12d
incq %rbp
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x4, %rcx
addq $0x10, %r13
cmpq %rbp, %rcx
ja 0x123d8
leal 0x3(%r12), %eax
testl %r12d, %r12d
cmovnsl %r12d, %eax
sarl $0x2, %eax
cltq
jmp 0x12444
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 0x12ade
addq $0x8, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28312
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1248b
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 0x124a0
jmp 0x124a0
jmp 0x124a0
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r13, %rdi
je 0x124eb
movq 0x18(%rsp), %rsi
jmp 0x124e3
movq %rax, %rbx
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x124eb
movq 0x20(%rsp), %rsi
jmp 0x124e3
movq %rax, %rbx
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x124eb
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540
nop