// File: /workspace/github_cpp_makefile_star2_O1/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::ArrayIdentifier::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayIdentifier::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  bool is_assigned = ctx.is_assigned_;
  ctx.is_assigned_ = false;
  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");
    return;
  }

  // 结点中存的shape(可能比应有的shape的size小)比符号表中存的shape的size大
  // eg. int a[2][3]; a[1][3][4]=1; 使用数组时维度错误
  if (shape_list_.size() > s->shape_.size()) {
    ir::SemanticError(line_no_, name_.name_ + ": the dimension of array is not correct");
    return;
  }

  // for(int i = 0; i < s->width_.size(); ++i)
  // {
  //   printf("width: %d\n", s->width_[i]);
  // }

  {
    BinaryExpression *res = nullptr;
    BinaryExpression *add_exp;
    BinaryExpression *mul_exp;
    // 计算数组取值的偏移
    if (shape_list_.size() > 0) {
      res = new BinaryExpression(line_no_, MUL, shape_list_[0], *(new Number(line_no_, s->width_[1])));
    }
    for (int i = 1; i < shape_list_.size(); ++i) {
      //构造一个二元表达式
      Number *width = new Number(line_no_, s->width_[i + 1]);
      // printf("width: %d\n", s->width_[i + 1]);
      mul_exp = new BinaryExpression(line_no_, MUL, shape_list_[i], *width);
      add_exp = new BinaryExpression(line_no_, ADD, std::shared_ptr<Expression>(res), *mul_exp);
      res = add_exp;
    }
    if (nullptr != res) {
      res->GenerateIR(ctx, gIRList);
      // res -> PrintNode();
      delete res;
    }
  }

  // 设置当前arrayidentifier的维度
  // eg. int a[2][3][4]; a[0]'s shape is [3,4].
  ctx.shape_.clear();
  ctx.shape_ = std::vector<int>(s->shape_.begin() + shape_list_.size(), s->shape_.end());
  // std::cout << s->shape_.size() << " " << shape_list_.size() << " " << ctx.shape_.size() << std::endl;

  ir::Opn *offset = new ir::Opn(ctx.opn_);
  ctx.opn_ = ir::Opn(OpnType::Array, name_.name_, scope_id, offset);
  if (!is_assigned && ctx.shape_.empty()) {
    if (s->is_const_ && offset->type_ == OpnType::Imm) {  // 常量数组的使用 直接返回一个立即数
      ctx.opn_ = ir::Opn(OpnType::Imm, s->initval_[offset->imm_num_ / 4]);
    } else {
      // 如果是int类型而不是int[] 则需要生成一条=[]ir以区分地址和取值
      std::string res_var = ir::NewTemp();
      int scope_id = ctx.scope_id_;
      ir::gScopes[scope_id].symbol_table_.insert({res_var, {false, false}});
      ir::Opn temp = ir::Opn(OpnType::Var, res_var, scope_id);
      gIRList.push_back(new ir::IR(IROpKind::ASSIGN_OFFSET, ctx.opn_, temp));
      ctx.opn_ = temp;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x228, %rsp            # imm = 0x228
movq %rdx, %r12
movb 0x209(%rsi), %al
movb %al, 0xf(%rsp)
movb $0x0, 0x209(%rsi)
leaq 0x18(%rsp), %rbx
movq $0x0, (%rbx)
movq %rsi, 0xd0(%rsp)
movl (%rsi), %ebp
movq %rdi, 0x10(%rsp)
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x148(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x138(%rsp), %r14
movq %r14, %rdi
callq 0xca10
movl %ebp, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x27e6f
movl %eax, %ebp
movq 0x138(%rsp), %rdi
cmpq %r15, %rdi
je 0x13f42
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
movq 0x10(%rsp), %rdi
je 0x13fc0
movq 0x18(%rdi), %r15
movq 0x20(%rdi), %rax
movq %rax, %rdx
subq %r15, %rdx
sarq $0x4, %rdx
movq 0x10(%rcx), %rsi
subq 0x8(%rcx), %rsi
sarq $0x2, %rsi
cmpq %rsi, %rdx
jbe 0x14026
movl 0x8(%rdi), %ebx
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xca10
leaq 0x5ba5c(%rip), %rsi     # 0x6fa06
movq %r14, %rdi
callq 0x8590
leaq 0x28(%rsp), %rsi
movl %ebx, %edi
callq 0x2b12a
jmp 0x14006
movl 0x8(%rdi), %ebx
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x38(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0xca10
leaq 0x5b9ee(%rip), %rsi     # 0x6f9e0
movq %r14, %rdi
callq 0x8590
leaq 0x28(%rsp), %rsi
movl %ebx, %edi
callq 0x2b12a
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x14377
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x14377
cmpq %r15, %rax
je 0x1406d
movl $0x28, %edi
callq 0x8290
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ebx
movq (%r15), %rax
movq %rax, 0xc0(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0xc8(%rsp)
testq %rax, %rax
je 0x14076
movq 0x7bf55(%rip), %rcx     # 0x8ffb8
cmpb $0x0, (%rcx)
je 0x14072
incl 0x8(%rax)
jmp 0x14076
xorl %r14d, %r14d
jmp 0x140cb
lock
incl 0x8(%rax)
movl $0x10, %edi
callq 0x8290
movq %rax, %r15
movq 0x10(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x4(%rax), %edx
movq %r15, %rdi
callq 0xda58
leaq 0xc0(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %esi
movl $0x105, %edx            # imm = 0x105
movq %r15, %r8
callq 0xdf64
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x140cb
callq 0xfd66
movl %ebp, 0x24(%rsp)
movq %r12, 0x98(%rsp)
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rax
subq 0x18(%rcx), %rax
cmpq $0x11, %rax
jb 0x14213
movl $0x1, %ebp
movl $0x6, %r12d
movq 0x10(%rsp), %rbx
movl $0x10, %edi
callq 0x8290
movq %rax, %r15
movl 0x8(%rbx), %esi
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x4(%rax,%rbp,4), %edx
movq %r15, %rdi
callq 0xda58
movl $0x28, %edi
callq 0x8290
movq %rax, %r13
movl 0x8(%rbx), %esi
movq 0x18(%rbx), %rax
movq -0x8(%rax,%r12,4), %rcx
movq %rcx, 0xb0(%rsp)
movq (%rax,%r12,4), %rax
movq %rax, 0xb8(%rsp)
testq %rax, %rax
je 0x1416a
movq 0x7be5c(%rip), %rcx     # 0x8ffb8
cmpb $0x0, (%rcx)
je 0x14166
incl 0x8(%rax)
jmp 0x1416a
lock
incl 0x8(%rax)
movq %r13, %rdi
movl $0x105, %edx            # imm = 0x105
leaq 0xb0(%rsp), %rcx
movq %r15, %r8
callq 0xdf64
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x14194
callq 0xfd66
movl $0x28, %edi
callq 0x8290
movq %rax, %r15
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ebx
movq %r14, 0xa0(%rsp)
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x263ba
movq %r15, %rdi
movl %ebx, %esi
movl $0x102, %edx            # imm = 0x102
leaq 0xa0(%rsp), %rcx
movq %r13, %r8
callq 0xdf64
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %rbx
je 0x141f2
callq 0xfd66
incq %rbp
movq 0x20(%rbx), %rax
subq 0x18(%rbx), %rax
sarq $0x4, %rax
addq $0x4, %r12
movq %r15, %r14
cmpq %rbp, %rax
ja 0x140fe
jmp 0x14216
movq %r14, %r15
testq %r15, %r15
movq 0x98(%rsp), %rdx
movq 0xd0(%rsp), %rbp
je 0x14240
movq (%r15), %rax
movq %r15, %rdi
movq %rbp, %rsi
callq *0x10(%rax)
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
movq 0x50(%rbp), %rax
cmpq %rax, 0x58(%rbp)
movq 0x10(%rsp), %r14
je 0x14253
movq %rax, 0x58(%rbp)
movq 0x18(%rsp), %rax
movq 0x20(%r14), %rsi
subq 0x18(%r14), %rsi
sarq $0x2, %rsi
addq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
leaq 0x28(%rsp), %rdi
leaq 0x158(%rsp), %rcx
callq 0x24c5a
leaq 0x50(%rbp), %rdi
leaq 0x28(%rsp), %rbx
movq %rbx, %rsi
callq 0x264ea
movq (%rbx), %rdi
testq %rdi, %rdi
je 0x142a4
movq 0x38(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movl $0x40, %edi
callq 0x8290
movq %rax, %r13
leaq 0x8(%rbp), %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x28186
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
leaq 0x128(%rsp), %r14
movq %r14, -0x10(%r14)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x118(%rsp), %rbx
movq %rbx, %rdi
callq 0xca10
leaq 0x30(%rsp), %rdi
movl $0x5, -0x8(%rdi)
leaq 0x40(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
callq 0xca10
leaq 0x28(%rsp), %rsi
movl 0x24(%rsp), %eax
movl %eax, 0x28(%rsi)
movq %r13, 0x30(%rsi)
movl $0xffffffff, 0x38(%rsi) # imm = 0xFFFFFFFF
movq %r15, %rdi
callq 0x28312
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14349
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x14366
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x82a0
cmpb $0x0, 0xf(%rsp)
jne 0x14377
movq 0x50(%rbp), %rax
cmpq 0x58(%rbp), %rax
je 0x14389
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x18(%rsp), %rax
cmpb $0x1, 0x1(%rax)
jne 0x143e5
cmpl $0x1, (%r13)
jne 0x143e5
movl 0x4(%r13), %ecx
leal 0x3(%rcx), %edx
testl %ecx, %ecx
cmovnsl %ecx, %edx
sarl $0x2, %edx
movslq %edx, %rcx
movq 0x38(%rax), %rax
movl (%rax,%rcx,4), %edx
leaq 0x28(%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x12ade
movq %r15, %rdi
movq %rbx, %rsi
callq 0x28312
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14377
movq 0x40(%rsp), %rsi
jmp 0x14019
leaq 0xf8(%rsp), %rdi
callq 0x27ed3
movslq (%rbp), %r14
leaq 0x7c17b(%rip), %rax     # 0x90578
movq %r14, %rbx
shlq $0x6, %rbx
addq (%rax), %rbx
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x24c9c
leaq 0x28(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x24d34
leaq 0x28(%rsp), %rsi
movq %rbx, %rdi
callq 0x26522
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x1445c
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x14473
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x1448a
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x144a5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x190(%rsp), %rdi
testq %rdi, %rdi
je 0x144c2
movq 0x1a0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x178(%rsp), %rdi
testq %rdi, %rdi
je 0x144df
movq 0x188(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x144fc
movq 0x170(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0xe8(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0xf8(%rsp), %rsi
movq 0x100(%rsp), %rdx
addq %rsi, %rdx
leaq 0xd8(%rsp), %rdi
callq 0xca10
leaq 0x30(%rsp), %rdi
movl $0x0, -0x8(%rdi)
movq %r12, (%rdi)
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
addq %rsi, %rdx
callq 0xca10
movl %r14d, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
movl $0xffffffff, 0x60(%rsp) # imm = 0xFFFFFFFF
movq 0xd8(%rsp), %rdi
cmpq %r13, %rdi
je 0x14582
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x82a0
movl $0xe0, %edi
callq 0x8290
movq %rax, %r14
leaq 0x1e8(%rsp), %rdi
movq %r15, %rsi
callq 0x28186
leaq 0x1a8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x28186
movb $0x1, %bpl
leaq 0x1e8(%rsp), %rdx
leaq 0x1a8(%rsp), %rcx
movq %r14, %rdi
movl $0x14, %esi
callq 0x24e94
leaq 0x158(%rsp), %rsi
movq %r14, (%rsi)
xorl %ebp, %ebp
movq 0x98(%rsp), %rdi
callq 0x114b8
leaq 0x1c0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1460c
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1462d
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x28(%rsp), %rsi
movq %r15, %rdi
callq 0x2822e
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14651
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14377
movq 0x108(%rsp), %rsi
jmp 0x14019
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14892
movq 0x40(%rsp), %rsi
jmp 0x1483d
jmp 0x146a6
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x1471b
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x1473c
movq %rax, %rbx
jmp 0x1474e
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r13, %rdi
je 0x14765
movq 0xe8(%rsp), %rsi
jmp 0x1475d
jmp 0x146f2
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x24df0
jmp 0x146e3
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x24e4e
jmp 0x14765
movq %rax, %rbx
jmp 0x14765
movq %rax, %rbx
leaq 0x1c0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1471b
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1473c
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x1474e
movl $0xe0, %esi
movq %r14, %rdi
callq 0x82a0
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x14765
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14892
movq 0x108(%rsp), %rsi
jmp 0x1483d
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x82a0
jmp 0x14809
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r12, %rdi
je 0x147b8
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x147b8
movq %rax, %rbx
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x14892
movq 0x128(%rsp), %rsi
jmp 0x1483d
movq %rax, %rbx
movl $0x40, %esi
jmp 0x14866
movq %rax, %rbx
jmp 0x14892
jmp 0x147ee
jmp 0x147ee
jmp 0x147ee
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r15, %rdi
je 0x14892
movq 0x38(%rsp), %rsi
jmp 0x1483d
movq %rax, %rbx
movq 0xc8(%rsp), %rdi
testq %rdi, %rdi
je 0x1481b
callq 0xfd66
movl $0x28, %esi
movq %r14, %rdi
jmp 0x1488d
movq %rax, %rbx
movq 0x138(%rsp), %rdi
cmpq %r15, %rdi
je 0x14892
movq 0x148(%rsp), %rsi
incq %rsi
jmp 0x1488d
movq %rax, %rbx
movl $0x10, %esi
jmp 0x1488a
movq %rax, %rbx
movq 0xb8(%rsp), %rdi
testq %rdi, %rdi
je 0x14861
callq 0xfd66
movl $0x28, %esi
movq %r13, %rdi
jmp 0x1488d
movq %rax, %rbx
jmp 0x14885
movq %rax, %rbx
movq 0xa8(%rsp), %rdi
testq %rdi, %rdi
je 0x14885
callq 0xfd66
movl $0x28, %esi
movq %r15, %rdi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540