// File: /workspace/github_cpp_makefile_star2_O1/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::ArrayDefine::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayDefine::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  auto &scope = ir::gScopes[ctx.scope_id_];
  auto &symbol_table = scope.symbol_table_;
  // NOTE: 只需在当前作用域查找即可 找到则重定义
  const auto &var_iter = symbol_table.find(this->name_.name_.name_);
  if (var_iter == symbol_table.end()) {
    auto tmp = new ir::SymbolTableItem(true, false);
    // 计算shape和width shape的类型必定为常量表达式，所以opn一定是立即数
    for (const auto &shape : name_.shape_list_) {
      shape->Evaluate(ctx, gIRList);
      tmp->shape_.push_back(ctx.opn_.imm_num_);
    }
    tmp->width_.resize(tmp->shape_.size() + 1);
    // NOTE: 数组的width比shape多一项 最后一项存InitWidth 4
    tmp->width_[tmp->width_.size() - 1] = ir::kIntWidth;
    for (int i = tmp->width_.size() - 2; i >= 0; i--) tmp->width_[i] = tmp->width_[i + 1] * tmp->shape_[i];
    // 全局数组量需要展开数组并填初值0
    if (ctx.scope_id_ == 0) {
      int mul = 1;
      for (int i = 0; i < tmp->shape_.size(); i++) mul *= tmp->shape_[i];
      // NOTE HERE: 不填表示都是0 都填0会造成内存大量浪费
      // tmp->initval_.resize(mul, 0);
    } else {  // GenIR ALLOCA
      gIRList.push_back(new ir::IR{
          IROpKind::ALLOCA, ir::Opn{OpnType::Array, this->name_.name_.name_, ctx.scope_id_, new ir::Opn(IMM_0_OPN)},
          ir::Opn{OpnType::Imm, tmp->width_[0]}});
    }
    symbol_table.insert({name_.name_.name_, *tmp});
    delete tmp;
  } else {
    ir::SemanticError(this->line_no_, this->name_.name_.name_ + ": variable redefined");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, %r15
movq %rsi, %rbp
movslq (%rsi), %rbx
leaq 0x7903b(%rip), %rax     # 0x90578
shlq $0x6, %rbx
addq (%rax), %rbx
movq %rdi, %r14
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0x12c3c
testq %rax, %rax
je 0x175c7
movl 0x8(%r14), %ebx
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
leaq 0x58(%rsp), %r12
movq %r12, -0x10(%r12)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0xca10
leaq 0x5867b(%rip), %rsi     # 0x6fc13
movq %r14, %rdi
callq 0x8590
leaq 0x48(%rsp), %rsi
movl %ebx, %edi
callq 0x2b12a
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x178ed
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x178e8
movq %r15, 0x18(%rsp)
movq %rbx, 0x40(%rsp)
movl $0x50, %edi
callq 0x8290
movq %rax, %rbx
movq %rax, 0x10(%rsp)
movq %rax, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x24c9c
movq %r14, 0x8(%rsp)
movq 0x10(%r14), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %r13
cmpq %r13, %r15
je 0x17653
leaq 0xc(%rbp), %r14
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %r12
movq (%r15), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq 0x18(%rsp), %rdx
callq *0x18(%rax)
movq 0x10(%rbx), %rsi
cmpq 0x18(%rbx), %rsi
je 0x1763f
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rbx)
jmp 0x1764a
movq %r12, %rdi
movq %r14, %rdx
callq 0x12d38
addq $0x10, %r15
cmpq %r13, %r15
jne 0x17615
movq 0x10(%rsp), %r13
addq $0x20, %r13
movq 0x10(%rbx), %rsi
subq 0x8(%rbx), %rsi
sarq $0x2, %rsi
incq %rsi
movq %r13, %rdi
callq 0x25250
leaq 0x58b66(%rip), %rax     # 0x701e0
movl (%rax), %ecx
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rdx
movl %ecx, -0x4(%rdx)
subq %rax, %rdx
shrq $0x2, %rdx
addl $-0x2, %edx
js 0x176ae
movq 0x8(%rbx), %rcx
movl %edx, %edx
movl 0x4(%rax,%rdx,4), %esi
incq %rdx
imull -0x4(%rcx,%rdx,4), %esi
movl %esi, -0x4(%rax,%rdx,4)
decq %rdx
jg 0x176a0
cmpl $0x0, (%rbp)
movq 0x8(%rsp), %r14
je 0x17809
movl $0xe0, %edi
callq 0x8290
movq %rax, %r12
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
leaq 0x30(%rsp), %rcx
movq %rcx, -0x10(%rcx)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq 0x20(%rsp), %rdi
callq 0xca10
movl (%rbp), %r15d
movl $0x40, %edi
callq 0x8290
movq %rax, %rbp
movq %rax, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x12ade
leaq 0xc0(%rsp), %rdi
movl $0x5, -0x8(%rdi)
leaq 0xd0(%rsp), %r14
movq %r14, -0x10(%r14)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
addq %rsi, %rdx
callq 0xca10
movl %r15d, 0xe0(%rsp)
movq %rbp, 0xe8(%rsp)
movl $0xffffffff, 0xf0(%rsp) # imm = 0xFFFFFFFF
movq (%r13), %rax
movl (%rax), %edx
leaq 0xf8(%rsp), %rdi
movl $0x1, %esi
callq 0x12ade
movb $0x1, %bpl
leaq 0xb8(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
movq %r12, %r15
movq %r12, %rdi
movl $0x16, %esi
callq 0x24e94
leaq 0x48(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
callq 0x114b8
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x177cb
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x177e8
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
movq 0x8(%rsp), %r14
je 0x17809
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x25110
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x26522
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x1784f
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x1786c
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x17886
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x178a1
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x178b6
movq 0x48(%rbx), %rsi
subq %rdi, %rsi
callq 0x82a0
movq (%r13), %rdi
testq %rdi, %rdi
je 0x178cb
movq 0x30(%rbx), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x178e0
movq 0x18(%rbx), %rsi
subq %rdi, %rsi
callq 0x82a0
movl $0x50, %esi
movq %rbx, %rdi
callq 0x82a0
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movb $0x1, %bpl
jmp 0x1796a
movq %rax, %r15
movl $0x40, %esi
movq %rbp, %rdi
callq 0x82a0
jmp 0x1798c
movq %rax, %r15
movb $0x1, %bpl
jmp 0x179ab
movq %rax, %r15
leaq 0x48(%rsp), %rdi
callq 0x24df0
jmp 0x179d6
movq %rax, %r15
movl $0x50, %esi
movq %rbx, %rdi
jmp 0x179d1
movq %rax, %r15
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1796a
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x1798f
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x1798f
movq %rax, %r15
movb $0x1, %bpl
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x179ab
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x179d6
movl $0xe0, %esi
movq %r12, %rdi
jmp 0x179d1
jmp 0x179bc
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x179d6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r15, %rdi
callq 0x8540