// File: /workspace/github_cpp_makefile_star2_O2/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 $0x238, %rsp            # imm = 0x238
movq %rdx, 0x28(%rsp)
movb 0x209(%rsi), %r12b
movb $0x0, 0x209(%rsi)
leaq 0x10(%rsp), %rbx
andq $0x0, (%rbx)
movq %rdi, %r15
movq %rsi, 0x30(%rsp)
movl (%rsi), %ebp
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rsi
leaq 0x148(%rsp), %r14
movq %r14, %rdi
callq 0x8390
movl %ebp, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x1d90c
movl %eax, %ebp
leaq 0x148(%rsp), %rdi
callq 0x8578
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x1046d
movb %r12b, 0xf(%rsp)
movq 0x18(%r15), %r12
movq 0x20(%r15), %rax
movq %rax, %rdx
subq %r12, %rdx
sarq $0x4, %rdx
movq 0x10(%rcx), %rsi
subq 0x8(%rcx), %rsi
sarq $0x2, %rsi
cmpq %rsi, %rdx
jbe 0x104b3
movl 0x8(%r15), %ebx
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
leaq 0x46516(%rip), %rdx     # 0x5696a
leaq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0xa334
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
jmp 0x10497
movl 0x8(%r15), %ebx
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
leaq 0x464c4(%rip), %rdx     # 0x56944
leaq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0xa334
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x68(%rsp), %rdi
callq 0x8578
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, 0x18(%rsp)
cmpq %r12, %rax
movl %ebp, 0x24(%rsp)
je 0x1052b
pushq $0x28
popq %rdi
callq 0x8250
movq %rax, %r15
movq 0x18(%rsp), %rax
movl 0x8(%rax), %ebx
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
callq 0x1b418
pushq $0x10
popq %rdi
callq 0x8250
movq %rax, %r12
movq 0x18(%rsp), %rax
movl 0x8(%rax), %esi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x4(%rax), %edx
movq %r12, %rdi
callq 0xb5c6
leaq 0x58(%rsp), %rcx
movq %r15, %rdi
movl %ebx, %esi
movl $0x105, %edx            # imm = 0x105
movq %r12, %r8
callq 0xb960
leaq 0x60(%rsp), %rdi
callq 0xcf18
jmp 0x1052e
xorl %r15d, %r15d
pushq $0x10
popq %rax
pushq $0x1
popq %rbx
movq %rax, 0x38(%rsp)
movq 0x18(%rsp), %r14
movq 0x20(%r14), %rax
subq 0x18(%r14), %rax
sarq $0x4, %rax
cmpq %rbx, %rax
jbe 0x1061e
pushq $0x10
popq %rdi
callq 0x8250
movq %rax, %r12
movl 0x8(%r14), %esi
movq 0x10(%rsp), %rax
movq 0x20(%rax), %rax
movq %rbx, 0x40(%rsp)
movl 0x4(%rax,%rbx,4), %edx
movq %r12, %rdi
callq 0xb5c6
pushq $0x28
popq %rbx
movq %rbx, %rdi
callq 0x8250
movq %rax, %rbp
movl 0x8(%r14), %r13d
movq 0x18(%r14), %rsi
addq 0x38(%rsp), %rsi
leaq 0x48(%rsp), %rdi
callq 0x1b418
movq %rbp, %rdi
movl %r13d, %esi
movl $0x105, %edx            # imm = 0x105
leaq 0x48(%rsp), %rcx
movq %r12, %r8
callq 0xb960
leaq 0x50(%rsp), %rdi
callq 0xcf18
movq %rbx, %rdi
callq 0x8250
movq %rax, %r12
movl 0x8(%r14), %r13d
leaq 0xd8(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1c004
movq %r12, %rdi
movl %r13d, %esi
movl $0x102, %edx            # imm = 0x102
movq %rbx, %rcx
movq %rbp, %r8
callq 0xb960
movq 0x40(%rsp), %rbx
leaq 0xe0(%rsp), %rdi
callq 0xcf18
addq $0x10, 0x38(%rsp)
incq %rbx
movq %r12, %r15
jmp 0x1053e
testq %r15, %r15
movq 0x30(%rsp), %r14
je 0x10642
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq 0x28(%rsp), %rdx
callq *0x10(%rax)
movq (%r15), %rax
movq %r15, %rdi
callq *0x8(%rax)
leaq 0x50(%r14), %rbx
movq 0x50(%r14), %rax
cmpq %rax, 0x58(%r14)
je 0x10654
movq %rax, 0x58(%r14)
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %r15
movq 0x20(%r15), %rsi
subq 0x18(%r15), %rsi
sarq $0x2, %rsi
addq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
movq %r14, %r12
leaq 0x68(%rsp), %r14
leaq 0x168(%rsp), %rcx
movq %r14, %rdi
callq 0x1a902
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1c102
movq %r14, %rdi
callq 0x1b2b6
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %rbx
addq $0x8, %r12
movq %rax, %rdi
movq %r12, %rsi
callq 0x1da22
movq 0x10(%r15), %rax
movq 0x10(%rax), %rsi
leaq 0x128(%rsp), %r14
movq %r14, %rdi
callq 0x8390
leaq 0x68(%rsp), %rdi
pushq $0x5
popq %rsi
movq %r14, %rdx
movl 0x24(%rsp), %ecx
movq %rbx, %r8
callq 0x1a934
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x1db84
leaq 0x70(%rsp), %rdi
callq 0x8578
leaq 0x128(%rsp), %rdi
callq 0x8578
cmpb $0x0, 0xf(%rsp)
movq 0x30(%rsp), %r14
jne 0x104a1
movq 0x50(%r14), %rax
cmpq 0x58(%r14), %rax
jne 0x104a1
movq 0x10(%rsp), %rcx
cmpb $0x1, 0x1(%rcx)
jne 0x1076f
cmpl $0x1, (%rbx)
jne 0x1076f
movl 0x4(%rbx), %eax
pushq $0x4
popq %rsi
cltd
idivl %esi
cltq
movq 0x38(%rcx), %rcx
movl (%rcx,%rax,4), %edx
leaq 0x68(%rsp), %rbx
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0xf6ba
movq %r12, %rdi
movq %rbx, %rsi
callq 0x1db84
leaq 0x70(%rsp), %rdi
jmp 0x1049c
leaq 0xe8(%rsp), %rdi
callq 0x1d95d
movslq (%r14), %rbx
leaq 0x70dc2(%rip), %rax     # 0x81548
movq %rbx, %r14
shlq $0x6, %r14
addq (%rax), %r14
leaq 0x168(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x1a960
leaq 0x68(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
leaq 0x168(%rsp), %rdx
callq 0x1a9e2
leaq 0x68(%rsp), %rsi
movq %r14, %rdi
callq 0x1c154
leaq 0x68(%rsp), %rdi
callq 0x1aa18
leaq 0x168(%rsp), %rdi
callq 0x1aa2e
leaq 0x108(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x8390
leaq 0x68(%rsp), %rdi
leaq 0x108(%rsp), %rdx
xorl %esi, %esi
movl %ebx, %ecx
callq 0x1a8d4
leaq 0x108(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbx
leaq 0x1f8(%rsp), %rdi
movq %r12, %rsi
callq 0x1da22
leaq 0x1b8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x1da22
movb $0x1, %bpl
pushq $0x14
popq %rsi
leaq 0x1f8(%rsp), %rdx
leaq 0x1b8(%rsp), %rcx
movq %rbx, %rdi
callq 0x1aa52
leaq 0x168(%rsp), %rsi
movq %rbx, (%rsi)
xorl %ebp, %ebp
movq 0x28(%rsp), %rdi
callq 0xe358
leaq 0x1c0(%rsp), %rdi
callq 0x8578
leaq 0x200(%rsp), %rdi
callq 0x8578
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x1daba
leaq 0x70(%rsp), %rdi
callq 0x8578
leaq 0xe8(%rsp), %rdi
jmp 0x1049c
movq %rax, %r15
leaq 0x70(%rsp), %rdi
jmp 0x109bb
jmp 0x108d5
movq %rax, %r15
movb $0x1, %bpl
jmp 0x1091f
movq %rax, %r15
jmp 0x10931
movq %rax, %r15
jmp 0x1093e
movq %rax, %r15
leaq 0x108(%rsp), %rdi
jmp 0x10943
jmp 0x1090a
movq %rax, %r15
leaq 0x68(%rsp), %rdi
callq 0x1aa18
jmp 0x108fb
movq %rax, %r15
leaq 0x168(%rsp), %rdi
callq 0x1aa2e
jmp 0x10948
movq %rax, %r15
jmp 0x10948
movq %rax, %r15
leaq 0x1c0(%rsp), %rdi
callq 0x8578
leaq 0x200(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x1093e
movl $0xe0, %esi
movq %rbx, %rdi
callq 0x8260
leaq 0x70(%rsp), %rdi
callq 0x8578
leaq 0xe8(%rsp), %rdi
jmp 0x109bb
movq %r15, %r13
movq %rax, %r15
pushq $0x10
popq %rsi
movq %r12, %rdi
callq 0x8260
jmp 0x1099e
movq %rax, %r15
leaq 0x70(%rsp), %rdi
callq 0x8578
jmp 0x10977
movq %rax, %r15
leaq 0x128(%rsp), %rdi
jmp 0x109bb
movq %rax, %r15
pushq $0x40
popq %rsi
movq %rbx, %rdi
jmp 0x109f9
jmp 0x1098e
movq %rax, %r15
leaq 0x68(%rsp), %rdi
jmp 0x109bb
movq %r15, %r13
movq %rax, %r15
leaq 0x60(%rsp), %rdi
callq 0xcf18
pushq $0x28
popq %rsi
movq %r13, %rdi
jmp 0x109f9
movq %rax, %r15
leaq 0x148(%rsp), %rdi
callq 0x8578
jmp 0x109fe
movq %rax, %r15
leaq 0xe0(%rsp), %rdi
callq 0xcf18
jmp 0x109d7
movq %rax, %r15
pushq $0x28
jmp 0x109e0
movq %rax, %r15
pushq $0x10
popq %rsi
movq %r12, %rdi
jmp 0x109f9
movq %rax, %r15
leaq 0x50(%rsp), %rdi
callq 0xcf18
pushq $0x28
popq %rsi
movq %rbp, %rdi
callq 0x8260
movq %r15, %rdi
callq 0x84d0