// File: /workspace/github_cpp_makefile_star2_O2/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, %r12
movq %rsi, %rbp
movslq (%rsi), %rbx
leaq 0x6f14d(%rip), %rax     # 0x81548
shlq $0x6, %rbx
addq (%rax), %rbx
movq %rdi, %r15
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq %rbx, %rdi
callq 0xf75e
testq %rax, %rax
je 0x1245b
movl 0x8(%r15), %ebx
movq 0x10(%r15), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x4473e(%rip), %rdx     # 0x56b73
leaq 0x48(%rsp), %r14
movq %r14, %rdi
callq 0xa334
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x48(%rsp), %rdi
callq 0x8578
jmp 0x12635
movq %rbx, 0x10(%rsp)
pushq $0x50
popq %rdi
callq 0x8250
movq %rax, %r14
pushq $0x1
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0x1a960
movq %r15, 0x18(%rsp)
movq 0x10(%r15), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %rbx
movq %r14, 0x20(%rsp)
leaq 0x8(%r14), %r13
leaq 0xc(%rbp), %r14
cmpq %rbx, %r15
je 0x124bb
movq (%r15), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq %r12, %rdx
callq *0x18(%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0xf720
addq $0x10, %r15
jmp 0x12496
movq 0x20(%rsp), %rbx
movq %rbx, %r15
addq $0x20, %r15
movq 0x10(%rbx), %rsi
subq 0x8(%rbx), %rsi
sarq $0x2, %rsi
incq %rsi
movq %r15, %rdi
callq 0x1acf6
leaq 0x44c5b(%rip), %rax     # 0x57140
movl (%rax), %ecx
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rdx
movl %ecx, -0x4(%rdx)
movq %rdx, %rcx
subq %rax, %rcx
shrq $0x2, %rcx
addl $-0x2, %ecx
movq 0x18(%rsp), %r14
testl %ecx, %ecx
js 0x1251d
movl %ecx, %edx
movq (%r13), %rsi
movl (%rsi,%rdx,4), %esi
imull 0x4(%rax,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
decl %ecx
jmp 0x12504
cmpl $0x0, (%rbp)
je 0x125ef
movl $0xe0, %edi
callq 0x8250
movq %rax, 0x8(%rsp)
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x28(%rsp), %rdi
callq 0x8390
movl (%rbp), %ebp
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %r13
pushq $0x1
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0xf6ba
leaq 0xf8(%rsp), %rdi
pushq $0x5
popq %rsi
leaq 0x28(%rsp), %rdx
movl %ebp, %ecx
movq %r13, %r8
callq 0x1a934
movq (%r15), %rax
movl (%rax), %edx
leaq 0xb8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movb $0x1, %bpl
pushq $0x16
popq %rsi
leaq 0xf8(%rsp), %rdx
leaq 0xb8(%rsp), %rcx
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0x1aa52
leaq 0x48(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %r12, %rdi
callq 0xe358
leaq 0xc0(%rsp), %rdi
callq 0x8578
leaq 0x100(%rsp), %rdi
callq 0x8578
leaq 0x28(%rsp), %rdi
callq 0x8578
movq 0x10(%r14), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x48(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rdx
callq 0x1ac9a
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1c154
leaq 0x48(%rsp), %rdi
callq 0x1aa18
movq %rbx, %rdi
callq 0x1aa2e
pushq $0x50
popq %rsi
movq %rbx, %rdi
callq 0x8260
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x1268e
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r13, %rdi
callq 0x8260
jmp 0x126a0
movq %rax, %rbx
jmp 0x126b2
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x1aa18
jmp 0x126d0
movq %rax, %rbx
pushq $0x50
popq %rsi
movq %r14, %rdi
jmp 0x126bc
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x8578
leaq 0x100(%rsp), %rdi
callq 0x8578
jmp 0x126a3
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x28(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x126d0
movl $0xe0, %esi
movq 0x8(%rsp), %rdi
callq 0x8260
jmp 0x126d0
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0