// File: /workspace/github_cpp_makefile_star2_O3/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, 0xa0(%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 0xc6dc
movl %ebp, %edi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x28507
movl %eax, %ebp
movq 0x138(%rsp), %rdi
cmpq %r15, %rdi
je 0x13c08
movq 0x148(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
movq 0x10(%rsp), %rdi
je 0x13c86
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 0x13cec
movl 0x8(%rdi), %ebx
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x30(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xc6dc
leaq 0x5ad96(%rip), %rsi     # 0x6ea06
movq %r14, %rdi
callq 0x8590
leaq 0x20(%rsp), %rsi
movl %ebx, %edi
callq 0x2b6aa
jmp 0x13ccc
movl 0x8(%rdi), %ebx
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
leaq 0x30(%rsp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xc6dc
leaq 0x5ad28(%rip), %rsi     # 0x6e9e0
movq %r14, %rdi
callq 0x8590
leaq 0x20(%rsp), %rsi
movl %ebx, %edi
callq 0x2b6aa
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x14071
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x14071
cmpq %r15, %rax
je 0x13d33
movl $0x28, %edi
callq 0x8290
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ebx
movq (%r15), %rax
movq %rax, 0xc8(%rsp)
movq 0x8(%r15), %rax
movq %rax, 0xd0(%rsp)
testq %rax, %rax
je 0x13d3c
movq 0x7b287(%rip), %rcx     # 0x8efb0
cmpb $0x0, (%rcx)
je 0x13d38
incl 0x8(%rax)
jmp 0x13d3c
xorl %r14d, %r14d
jmp 0x13d91
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 0xd756
leaq 0xc8(%rsp), %rcx
movq %r14, %rdi
movl %ebx, %esi
movl $0x105, %edx            # imm = 0x105
movq %r15, %r8
callq 0xdc60
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x13d91
callq 0xfa86
movl %ebp, 0x94(%rsp)
movq %r12, 0x98(%rsp)
movq 0x10(%rsp), %rcx
movq 0x20(%rcx), %rax
subq 0x18(%rcx), %rax
cmpq $0x11, %rax
jb 0x13edf
movl $0x1, %ebp
movl $0x6, %r12d
movq %r14, %r15
movq 0x10(%rsp), %rbx
movl $0x10, %edi
callq 0x8290
movq %rax, %r14
movl 0x8(%rbx), %esi
movq 0x18(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x4(%rax,%rbp,4), %edx
movq %r14, %rdi
callq 0xd756
movl $0x28, %edi
callq 0x8290
movq %rax, %r13
movl 0x8(%rbx), %esi
movq 0x18(%rbx), %rax
movq -0x8(%rax,%r12,4), %rcx
movq %rcx, 0xb8(%rsp)
movq (%rax,%r12,4), %rax
movq %rax, 0xc0(%rsp)
testq %rax, %rax
je 0x13e36
movq 0x7b188(%rip), %rcx     # 0x8efb0
cmpb $0x0, (%rcx)
je 0x13e32
incl 0x8(%rax)
jmp 0x13e36
lock
incl 0x8(%rax)
movq %r13, %rdi
movl $0x105, %edx            # imm = 0x105
leaq 0xb8(%rsp), %rcx
movq %r14, %r8
callq 0xdc60
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x13e60
callq 0xfa86
movl $0x28, %edi
callq 0x8290
movq %rax, %r14
movq 0x10(%rsp), %rax
movl 0x8(%rax), %ebx
movq %r15, 0xa8(%rsp)
leaq 0xb0(%rsp), %rdi
movq %r15, %rsi
callq 0x26a9e
movq %r14, %rdi
movl %ebx, %esi
movl $0x102, %edx            # imm = 0x102
leaq 0xa8(%rsp), %rcx
movq %r13, %r8
callq 0xdc60
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x13eb9
callq 0xfa86
incq %rbp
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rax
subq 0x18(%rbx), %rax
sarq $0x4, %rax
addq $0x4, %r12
movq %r14, %r15
cmpq %rbp, %rax
ja 0x13dca
jmp 0x13ee4
testq %r14, %r14
je 0x13f06
movq (%r14), %rax
movq %r14, %rdi
movq 0xa0(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq *0x10(%rax)
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movq 0xa0(%rsp), %rbp
movq 0x50(%rbp), %rax
cmpq %rax, 0x58(%rbp)
je 0x13f1c
movq %rax, 0x58(%rbp)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rsi
subq 0x18(%rbx), %rsi
sarq $0x2, %rsi
addq 0x8(%rax), %rsi
movq 0x10(%rax), %rdx
leaq 0x20(%rsp), %rdi
leaq 0x158(%rsp), %rcx
callq 0x25326
movq 0x50(%rbp), %rdi
movq 0x60(%rbp), %rsi
movaps 0x20(%rsp), %xmm0
movups %xmm0, 0x50(%rbp)
movq 0x30(%rsp), %rax
movq %rax, 0x60(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
movq $0x0, 0x30(%rsp)
testq %rdi, %rdi
je 0x13f9b
subq %rdi, %rsi
callq 0x82a0
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x13f9b
movq 0x30(%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 0x28816
movq 0x10(%rbx), %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 0xc6dc
leaq 0x28(%rsp), %rdi
movl $0x5, -0x8(%rdi)
leaq 0x38(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rbx), %rsi
movq 0x8(%rbx), %rdx
addq %rsi, %rdx
callq 0xc6dc
leaq 0x20(%rsp), %rsi
movl 0x94(%rsp), %eax
movl %eax, 0x28(%rsi)
movq %r13, 0x30(%rsi)
movl $0xffffffff, 0x38(%rsi) # imm = 0xFFFFFFFF
movq %r15, %rdi
callq 0x289a2
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x14043
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x14060
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x82a0
cmpb $0x0, 0xf(%rsp)
jne 0x14071
movq 0x50(%rbp), %rax
cmpq 0x58(%rbp), %rax
je 0x14083
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 0x140df
cmpl $0x1, (%r13)
jne 0x140df
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 0x20(%rsp), %rbx
movq %rbx, %rdi
movl $0x1, %esi
callq 0x1279c
movq %r15, %rdi
movq %rbx, %rsi
callq 0x289a2
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x14071
movq 0x38(%rsp), %rsi
jmp 0x13cdf
leaq 0xf8(%rsp), %rdi
callq 0x28564
movslq (%rbp), %r14
leaq 0x7b481(%rip), %rax     # 0x8f578
movq %r14, %rbx
shlq $0x6, %rbx
addq (%rax), %rbx
leaq 0x158(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x25368
leaq 0x20(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0x158(%rsp), %rdx
callq 0x25400
leaq 0x20(%rsp), %rsi
movq %rbx, %rdi
callq 0x26bbc
movq 0x78(%rsp), %rdi
testq %rdi, %rdi
je 0x14153
movq 0x88(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x1416a
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x48(%rsp), %rdi
testq %rdi, %rdi
je 0x14181
movq 0x58(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1419c
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x190(%rsp), %rdi
testq %rdi, %rdi
je 0x141b9
movq 0x1a0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x178(%rsp), %rdi
testq %rdi, %rdi
je 0x141d6
movq 0x188(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x160(%rsp), %rdi
testq %rdi, %rdi
je 0x141f3
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 0xc6dc
leaq 0x28(%rsp), %rdi
movl $0x0, -0x8(%rdi)
movq %r12, (%rdi)
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
addq %rsi, %rdx
callq 0xc6dc
movl %r14d, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movl $0xffffffff, 0x58(%rsp) # imm = 0xFFFFFFFF
movq 0xd8(%rsp), %rdi
cmpq %r13, %rdi
je 0x14279
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x82a0
movl $0xe0, %edi
callq 0x8290
movq %rax, %r14
leaq 0x1a8(%rsp), %rdi
movq %r15, %rsi
callq 0x28816
leaq 0x1e8(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x28816
movb $0x1, %bpl
leaq 0x1a8(%rsp), %rdx
leaq 0x1e8(%rsp), %rcx
movq %r14, %rdi
movl $0x14, %esi
callq 0x25560
leaq 0x158(%rsp), %rsi
movq %r14, (%rsi)
xorl %ebp, %ebp
movq 0x98(%rsp), %rdi
callq 0x11182
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14303
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x1c0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14324
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x20(%rsp), %rsi
movq %r15, %rdi
callq 0x288be
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x14348
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14071
movq 0x108(%rsp), %rsi
jmp 0x13cdf
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x14593
movq 0x38(%rsp), %rsi
jmp 0x1453e
jmp 0x143bc
movq %rax, %rbx
leaq 0x1c0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14457
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x14457
movq %rax, %rbx
jmp 0x14457
movq %rax, %rbx
jmp 0x14464
movq %rax, %rbx
movq 0xd8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1447b
movq 0xe8(%rsp), %rsi
jmp 0x14473
jmp 0x14408
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x254bc
jmp 0x143f9
movq %rax, %rbx
leaq 0x158(%rsp), %rdi
callq 0x2551a
jmp 0x1447b
movq %rax, %rbx
jmp 0x1447b
movq %rax, %rbx
leaq 0x200(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14431
movq 0x200(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x1c0(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14452
movq 0x1c0(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x14464
movl $0xe0, %esi
movq %r14, %rdi
callq 0x82a0
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x1447b
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14593
movq 0x108(%rsp), %rsi
jmp 0x1453e
movq %rax, %rbx
movl $0x10, %esi
movq %r15, %rdi
callq 0x82a0
jmp 0x1451c
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r12, %rdi
je 0x144ce
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x144ce
movq %rax, %rbx
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x14593
movq 0x128(%rsp), %rsi
jmp 0x1453e
movq %rax, %rbx
movl $0x40, %esi
jmp 0x14567
movq %rax, %rbx
jmp 0x14593
jmp 0x14501
jmp 0x14501
jmp 0x14501
movq %rax, %rbx
movq 0x20(%rsp), %rdi
cmpq %r15, %rdi
je 0x14593
movq 0x30(%rsp), %rsi
jmp 0x1453e
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
jmp 0x1457c
movq %rax, %rbx
movq 0x138(%rsp), %rdi
cmpq %r15, %rdi
je 0x14593
movq 0x148(%rsp), %rsi
incq %rsi
jmp 0x1458e
movq %rax, %rbx
movl $0x10, %esi
jmp 0x1458b
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
testq %rdi, %rdi
je 0x14562
callq 0xfa86
movl $0x28, %esi
movq %r13, %rdi
jmp 0x1458e
movq %rax, %rbx
jmp 0x14586
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x14586
callq 0xfa86
movl $0x28, %esi
movq %r14, %rdi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540
nop