// File: 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:
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, 0x4d0(%rsp)
movq %rsi, 0x4c8(%rsp)
movq %rdx, 0x4c0(%rsp)
movq 0x4d0(%rsp), %rax
movq %rax, 0xf0(%rsp)
movq 0x4c8(%rsp), %rcx
movb 0x209(%rcx), %cl
andb $0x1, %cl
movb %cl, 0x4bf(%rsp)
movq 0x4c8(%rsp), %rcx
movb $0x0, 0x209(%rcx)
movq $0x0, 0x4b0(%rsp)
movq 0x4c8(%rsp), %rcx
movl (%rcx), %ecx
movl %ecx, 0xfc(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x488(%rsp), %rdi
movq %rdi, 0x100(%rsp)
callq 0x81a0
movl 0xfc(%rsp), %edi
movq 0x100(%rsp), %rsi
leaq 0x4b0(%rsp), %rdx
callq 0x42f30
movl %eax, 0x108(%rsp)
jmp 0x22565
leaq 0x488(%rsp), %rdi
callq 0x8728
movl 0x108(%rsp), %eax
movl %eax, 0x4ac(%rsp)
cmpq $0x0, 0x4b0(%rsp)
jne 0x22639
movq 0xf0(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0xe4(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xd83bc(%rip), %rdx     # 0xfa96c
leaq 0x458(%rsp), %rdi
movq %rdi, 0xe8(%rsp)
callq 0xc720
movl 0xe4(%rsp), %edi
movq 0xe8(%rsp), %rsi
callq 0x47ce0
jmp 0x225db
leaq 0x458(%rsp), %rdi
callq 0x8728
jmp 0x231ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x488(%rsp), %rdi
callq 0x8728
jmp 0x231b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x458(%rsp), %rdi
callq 0x8728
jmp 0x231b3
movq 0xf0(%rsp), %rdi
addq $0x18, %rdi
callq 0x1f230
movq %rax, 0xd8(%rsp)
movq 0x4b0(%rsp), %rdi
addq $0x8, %rdi
callq 0x10a40
movq %rax, %rcx
movq 0xd8(%rsp), %rax
cmpq %rcx, %rax
jbe 0x226fb
movq 0xf0(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0xcc(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xd82fa(%rip), %rdx     # 0xfa992
leaq 0x438(%rsp), %rdi
movq %rdi, 0xd0(%rsp)
callq 0xc720
movl 0xcc(%rsp), %edi
movq 0xd0(%rsp), %rsi
callq 0x47ce0
jmp 0x226c3
leaq 0x438(%rsp), %rdi
callq 0x8728
jmp 0x231ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x438(%rsp), %rdi
callq 0x8728
jmp 0x231b3
movq 0xf0(%rsp), %rdi
movq $0x0, 0x430(%rsp)
addq $0x18, %rdi
callq 0x1f230
cmpq $0x0, %rax
jbe 0x2287d
movl $0x28, %edi
callq 0x8340
movq 0xf0(%rsp), %rdi
movq %rax, 0xb0(%rsp)
movb $0x1, 0x40f(%rsp)
movl 0x8(%rdi), %eax
movl %eax, 0xbc(%rsp)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f250
movq %rax, %rsi
leaq 0x410(%rsp), %rdi
callq 0x364e0
movl $0x10, %edi
callq 0x8340
movq %rax, 0xc0(%rsp)
jmp 0x2277f
movq 0xf0(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0xac(%rsp)
movq 0x4b0(%rsp), %rdi
addq $0x20, %rdi
movl $0x1, %esi
callq 0x10a60
movq 0xc0(%rsp), %rdi
movl 0xac(%rsp), %esi
movl (%rax), %edx
callq 0x118f0
jmp 0x227bf
movq 0xc0(%rsp), %r8
movl 0xbc(%rsp), %esi
movq 0xb0(%rsp), %rdi
movl $0x105, %edx            # imm = 0x105
leaq 0x410(%rsp), %rcx
callq 0x12000
jmp 0x227ea
movq 0xb0(%rsp), %rax
movb $0x0, 0x40f(%rsp)
movq %rax, 0x430(%rsp)
leaq 0x410(%rsp), %rdi
callq 0x14cd0
jmp 0x2287d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x2284d
movq 0xc0(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
movl $0x10, %esi
callq 0x8350
leaq 0x410(%rsp), %rdi
callq 0x14cd0
testb $0x1, 0x40f(%rsp)
jne 0x22866
jmp 0x22878
movq 0xb0(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0x231b3
movl $0x1, 0x408(%rsp)
movq 0xf0(%rsp), %rdi
movslq 0x408(%rsp), %rax
movq %rax, 0xa0(%rsp)
addq $0x18, %rdi
callq 0x1f230
movq %rax, %rcx
movq 0xa0(%rsp), %rax
cmpq %rcx, %rax
jae 0x22b3c
movl $0x10, %edi
callq 0x8340
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x9c(%rsp)
movq 0x4b0(%rsp), %rdi
addq $0x20, %rdi
movl 0x408(%rsp), %eax
incl %eax
movslq %eax, %rsi
callq 0x10a60
movq 0x90(%rsp), %rdi
movl 0x9c(%rsp), %esi
movl (%rax), %edx
callq 0x118f0
jmp 0x22921
movq 0x88(%rsp), %rax
movq %rax, 0x400(%rsp)
movl $0x28, %edi
callq 0x8340
movq 0xf0(%rsp), %rdi
movq %rax, 0x70(%rsp)
movq %rax, 0x68(%rsp)
movb $0x1, 0x3ef(%rsp)
movl 0x8(%rdi), %eax
movl %eax, 0x7c(%rsp)
addq $0x18, %rdi
movslq 0x408(%rsp), %rsi
callq 0x1f250
movq %rax, %rsi
leaq 0x3f0(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x364e0
movq 0x70(%rsp), %rdi
movl 0x7c(%rsp), %esi
movq 0x80(%rsp), %rcx
movq 0x400(%rsp), %r8
movl $0x105, %edx            # imm = 0x105
callq 0x12000
jmp 0x229aa
movq 0x68(%rsp), %rax
movb $0x0, 0x3ef(%rsp)
movq %rax, 0x420(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x14cd0
movl $0x28, %edi
callq 0x8340
movq %rax, %rcx
movq 0xf0(%rsp), %rax
movq %rcx, 0x58(%rsp)
movb $0x1, 0x3d7(%rsp)
movl 0x8(%rax), %eax
movl %eax, 0x64(%rsp)
movq 0x430(%rsp), %rsi
leaq 0x3d8(%rsp), %rdi
callq 0x36510
jmp 0x22a0c
movl 0x64(%rsp), %esi
movq 0x58(%rsp), %rdi
movq 0x420(%rsp), %r8
movl $0x102, %edx            # imm = 0x102
leaq 0x3d8(%rsp), %rcx
callq 0x12000
jmp 0x22a31
movq 0x58(%rsp), %rax
movb $0x0, 0x3d7(%rsp)
movq %rax, 0x428(%rsp)
leaq 0x3d8(%rsp), %rdi
callq 0x14cd0
movq 0x428(%rsp), %rax
movq %rax, 0x430(%rsp)
movl 0x408(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x408(%rsp)
jmp 0x22888
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
movl $0x10, %esi
callq 0x8350
jmp 0x231b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x3f0(%rsp), %rdi
callq 0x14cd0
testb $0x1, 0x3ef(%rsp)
jne 0x22ad1
jmp 0x22ae0
movq 0x68(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0x231b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x22b1c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x3d8(%rsp), %rdi
callq 0x14cd0
testb $0x1, 0x3d7(%rsp)
jne 0x22b28
jmp 0x22b37
movq 0x58(%rsp), %rdi
movl $0x28, %esi
callq 0x8350
jmp 0x231b3
xorl %eax, %eax
cmpq 0x430(%rsp), %rax
je 0x22b86
movq 0x430(%rsp), %rdi
movq 0x4c8(%rsp), %rsi
movq 0x4c0(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x430(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, %rax
je 0x22b84
movq 0x50(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x22b86
movq 0x4c8(%rsp), %rdi
addq $0x50, %rdi
callq 0x361a0
movq 0x4b0(%rsp), %rdi
addq $0x8, %rdi
callq 0x1fc30
movq 0xf0(%rsp), %rdi
movq %rax, 0x3a8(%rsp)
addq $0x18, %rdi
callq 0x1f230
movq %rax, %rsi
leaq 0x3a8(%rsp), %rdi
callq 0x20880
movq %rax, 0x3b0(%rsp)
movq 0x4b0(%rsp), %rdi
addq $0x8, %rdi
callq 0x1f470
movq %rax, 0x3a0(%rsp)
leaq 0x39f(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x35bc0
movq 0x48(%rsp), %rcx
movq 0x3b0(%rsp), %rsi
movq 0x3a0(%rsp), %rdx
leaq 0x3b8(%rsp), %rdi
callq 0x36540
jmp 0x22c28
movq 0x4c8(%rsp), %rdi
addq $0x50, %rdi
leaq 0x3b8(%rsp), %rsi
movq %rsi, 0x38(%rsp)
callq 0x365d0
movq 0x38(%rsp), %rdi
callq 0x35c60
leaq 0x39f(%rsp), %rdi
callq 0x35cc0
movl $0x40, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x40(%rsp)
movq 0x4c8(%rsp), %rsi
addq $0x8, %rsi
callq 0x43130
jmp 0x22c85
movq 0xf0(%rsp), %rax
movq 0x40(%rsp), %rcx
movq %rcx, 0x390(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x330(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x81a0
movq 0x30(%rsp), %rdx
movl 0x4ac(%rsp), %ecx
movq 0x390(%rsp), %r8
leaq 0x350(%rsp), %rdi
movl $0x5, %esi
callq 0x36610
jmp 0x22cdc
movq 0x4c8(%rsp), %rdi
addq $0x8, %rdi
leaq 0x350(%rsp), %rsi
callq 0x433c0
jmp 0x22cf7
leaq 0x350(%rsp), %rdi
callq 0x1f210
leaq 0x330(%rsp), %rdi
callq 0x8728
testb $0x1, 0x4bf(%rsp)
jne 0x231ab
movq 0x4c8(%rsp), %rdi
addq $0x50, %rdi
callq 0x10a80
testb $0x1, %al
jne 0x22d39
jmp 0x231ab
movq 0x4b0(%rsp), %rax
testb $0x1, 0x1(%rax)
je 0x22e88
movq 0x390(%rsp), %rax
cmpl $0x1, (%rax)
jne 0x22e88
movq 0x4b0(%rsp), %rdi
addq $0x38, %rdi
movq 0x390(%rsp), %rax
movl 0x4(%rax), %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 0x2f0(%rsp), %rdi
movq %rdi, 0x28(%rsp)
movl $0x1, %esi
callq 0x1f160
movq 0x28(%rsp), %rsi
movq 0x4c8(%rsp), %rdi
addq $0x8, %rdi
callq 0x433c0
jmp 0x22db9
leaq 0x2f0(%rsp), %rdi
callq 0x1f210
jmp 0x231a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x39f(%rsp), %rdi
callq 0x35cc0
jmp 0x231b3
movq 0x40(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x231b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x22e50
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x350(%rsp), %rdi
callq 0x1f210
leaq 0x330(%rsp), %rdi
callq 0x8728
jmp 0x231b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x2f0(%rsp), %rdi
callq 0x1f210
jmp 0x231b3
leaq 0x2d0(%rsp), %rdi
callq 0x43010
movq 0x4c8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x2cc(%rsp)
movslq 0x2cc(%rsp), %rsi
leaq 0x129773(%rip), %rdi    # 0x14c628
callq 0x183c0
movq %rax, 0x20(%rsp)
leaq 0x208(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x366c0
jmp 0x22ed2
leaq 0x258(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
leaq 0x208(%rsp), %rdx
callq 0x367a0
jmp 0x22ef1
movq 0x20(%rsp), %rdi
leaq 0x258(%rsp), %rsi
callq 0x36670
movb %dl, 0x17(%rsp)
movq %rax, 0x18(%rsp)
jmp 0x22f0e
movb 0x17(%rsp), %al
movq 0x18(%rsp), %rcx
movq %rcx, 0x1f8(%rsp)
movb %al, 0x200(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x36810
leaq 0x208(%rsp), %rdi
callq 0x36840
leaq 0x198(%rsp), %rdi
leaq 0x2d0(%rsp), %rsi
callq 0x81a0
jmp 0x22f57
movl 0x2cc(%rsp), %ecx
leaq 0x1b8(%rsp), %rdi
xorl %esi, %esi
leaq 0x198(%rsp), %rdx
callq 0x36480
jmp 0x22f77
leaq 0x198(%rsp), %rdi
callq 0x8728
movq 0x4c0(%rsp), %rax
movq %rax, (%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x8(%rsp)
jmp 0x22fa1
movb $0x1, 0x10f(%rsp)
movq 0x4c8(%rsp), %rsi
addq $0x8, %rsi
leaq 0x150(%rsp), %rdi
callq 0x43130
jmp 0x22fc4
leaq 0x110(%rsp), %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x43130
jmp 0x22fdb
movq 0x8(%rsp), %rdi
movl $0x14, %esi
leaq 0x150(%rsp), %rdx
leaq 0x110(%rsp), %rcx
callq 0x36880
jmp 0x22ffc
movq (%rsp), %rdi
movq 0x8(%rsp), %rax
movb $0x0, 0x10f(%rsp)
movq %rax, 0x190(%rsp)
leaq 0x190(%rsp), %rsi
callq 0x185b0
jmp 0x23024
leaq 0x110(%rsp), %rdi
callq 0x1f210
leaq 0x150(%rsp), %rdi
callq 0x1f210
movq 0x4c8(%rsp), %rdi
addq $0x8, %rdi
leaq 0x1b8(%rsp), %rsi
callq 0x43230
jmp 0x23059
leaq 0x1b8(%rsp), %rdi
callq 0x1f210
leaq 0x2d0(%rsp), %rdi
callq 0x8728
jmp 0x231a9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x2319a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x230c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x36810
leaq 0x208(%rsp), %rdi
callq 0x36840
jmp 0x2319a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x198(%rsp), %rdi
callq 0x8728
jmp 0x2319a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x2318d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x23170
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
jmp 0x23163
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x480(%rsp)
movl %eax, 0x47c(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x1f210
leaq 0x150(%rsp), %rdi
callq 0x1f210
testb $0x1, 0x10f(%rsp)
jne 0x2317c
jmp 0x2318b
movq 0x8(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x2318d
leaq 0x1b8(%rsp), %rdi
callq 0x1f210
leaq 0x2d0(%rsp), %rdi
callq 0x8728
jmp 0x231b3
jmp 0x231ab
addq $0x4d8, %rsp            # imm = 0x4D8
retq
movq 0x480(%rsp), %rdi
callq 0x8670