// File: 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:
subq $0x278, %rsp            # imm = 0x278
movq %rdi, 0x270(%rsp)
movq %rsi, 0x268(%rsp)
movq %rdx, 0x260(%rsp)
movq 0x270(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x268(%rsp), %rax
movslq (%rax), %rsi
leaq 0x1262b7(%rip), %rdi    # 0x14c628
callq 0x183c0
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x258(%rsp)
movq 0x258(%rsp), %rcx
movq %rcx, 0x250(%rsp)
movq 0x250(%rsp), %rdi
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
callq 0x1f270
movq %rax, 0x240(%rsp)
leaq 0x240(%rsp), %rax
movq %rax, 0x248(%rsp)
movq 0x248(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x250(%rsp), %rdi
callq 0x109d0
movq 0x98(%rsp), %rdi
movq %rax, 0x238(%rsp)
leaq 0x238(%rsp), %rsi
callq 0x36e80
testb $0x1, %al
jne 0x2640d
jmp 0x269ce
movl $0x50, %edi
callq 0x8340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x88(%rsp)
movl $0x1, %esi
xorl %edx, %edx
callq 0x366c0
jmp 0x26433
movq 0x90(%rsp), %rax
movq 0x88(%rsp), %rcx
movq %rcx, 0x230(%rsp)
movq 0x10(%rax), %rax
addq $0x18, %rax
movq %rax, 0x218(%rsp)
movq 0x218(%rsp), %rdi
callq 0x14bc0
movq %rax, 0x210(%rsp)
movq 0x218(%rsp), %rdi
callq 0x14bf0
movq %rax, 0x208(%rsp)
leaq 0x210(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x14c20
testb $0x1, %al
jne 0x264a3
jmp 0x2653c
leaq 0x210(%rsp), %rdi
callq 0x14c60
movq %rax, 0x200(%rsp)
movq 0x200(%rsp), %rdi
callq 0x14c70
movq %rax, %rdi
movq 0x268(%rsp), %rsi
movq 0x260(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
movq 0x230(%rsp), %rdi
addq $0x8, %rdi
movq 0x268(%rsp), %rsi
addq $0x8, %rsi
addq $0x4, %rsi
callq 0x1f2a0
leaq 0x210(%rsp), %rdi
callq 0x14c80
jmp 0x26485
movq 0x88(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
movl $0x50, %esi
callq 0x8350
jmp 0x26a4c
movq 0x230(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x70(%rsp)
movq 0x230(%rsp), %rdi
addq $0x8, %rdi
callq 0x10a40
movq 0x70(%rsp), %rdi
movq %rax, %rsi
addq $0x1, %rsi
callq 0x36fa0
leaq 0xd4c12(%rip), %rax     # 0xfb188
movl (%rax), %eax
movl %eax, 0x84(%rsp)
movq 0x230(%rsp), %rax
addq $0x20, %rax
movq %rax, 0x78(%rsp)
movq 0x230(%rsp), %rdi
addq $0x20, %rdi
callq 0x10a40
movq 0x78(%rsp), %rdi
movq %rax, %rsi
subq $0x1, %rsi
callq 0x10a60
movl 0x84(%rsp), %ecx
movl %ecx, (%rax)
movq 0x230(%rsp), %rdi
addq $0x20, %rdi
callq 0x10a40
subq $0x2, %rax
movl %eax, 0x1fc(%rsp)
cmpl $0x0, 0x1fc(%rsp)
jl 0x26665
movq 0x230(%rsp), %rdi
addq $0x20, %rdi
movl 0x1fc(%rsp), %eax
addl $0x1, %eax
movslq %eax, %rsi
callq 0x10a60
movl (%rax), %eax
movl %eax, 0x68(%rsp)
movq 0x230(%rsp), %rdi
addq $0x8, %rdi
movslq 0x1fc(%rsp), %rsi
callq 0x10a60
movq %rax, %rcx
movl 0x68(%rsp), %eax
imull (%rcx), %eax
movl %eax, 0x6c(%rsp)
movq 0x230(%rsp), %rdi
addq $0x20, %rdi
movslq 0x1fc(%rsp), %rsi
callq 0x10a60
movl 0x6c(%rsp), %ecx
movl %ecx, (%rax)
movl 0x1fc(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x1fc(%rsp)
jmp 0x265d7
movq 0x268(%rsp), %rax
cmpl $0x0, (%rax)
jne 0x266f9
movl $0x1, 0x1f8(%rsp)
movl $0x0, 0x1f4(%rsp)
movslq 0x1f4(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x230(%rsp), %rdi
addq $0x8, %rdi
callq 0x10a40
movq %rax, %rcx
movq 0x60(%rsp), %rax
cmpq %rcx, %rax
jae 0x266f4
movq 0x230(%rsp), %rdi
addq $0x8, %rdi
movslq 0x1f4(%rsp), %rsi
callq 0x10a60
movl (%rax), %eax
imull 0x1f8(%rsp), %eax
movl %eax, 0x1f8(%rsp)
movl 0x1f4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1f4(%rsp)
jmp 0x2668c
jmp 0x26900
movq 0x260(%rsp), %rax
movq %rax, 0x50(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, %rcx
movq 0x90(%rsp), %rax
movq %rcx, 0x58(%rsp)
movb $0x1, 0x147(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x188(%rsp), %rdi
callq 0x81a0
jmp 0x26743
movq 0x268(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x44(%rsp)
movl $0x40, %edi
callq 0x8340
movq %rax, 0x48(%rsp)
jmp 0x26762
movq 0x48(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1f160
jmp 0x26775
movq 0x48(%rsp), %r8
movl 0x44(%rsp), %ecx
leaq 0x1a8(%rsp), %rdi
movl $0x5, %esi
leaq 0x188(%rsp), %rdx
callq 0x36610
jmp 0x2679a
movq 0x230(%rsp), %rdi
addq $0x20, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x10a60
movl (%rax), %edx
leaq 0x148(%rsp), %rdi
movl $0x1, %esi
callq 0x1f160
jmp 0x267c5
movq 0x58(%rsp), %rdi
movl $0x16, %esi
leaq 0x1a8(%rsp), %rdx
leaq 0x148(%rsp), %rcx
callq 0x36880
jmp 0x267e6
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rax
movb $0x0, 0x147(%rsp)
movq %rax, 0x1e8(%rsp)
leaq 0x1e8(%rsp), %rsi
callq 0x185b0
jmp 0x2680f
leaq 0x148(%rsp), %rdi
callq 0x1f210
leaq 0x1a8(%rsp), %rdi
callq 0x1f210
leaq 0x188(%rsp), %rdi
callq 0x8728
jmp 0x26900
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
jmp 0x268e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
jmp 0x268d3
movq 0x48(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x268d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
jmp 0x268c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x1f210
leaq 0x1a8(%rsp), %rdi
callq 0x1f210
leaq 0x188(%rsp), %rdi
callq 0x8728
testb $0x1, 0x147(%rsp)
jne 0x268ec
jmp 0x268fb
movq 0x58(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x26a4c
movq 0x90(%rsp), %rax
movq 0x250(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
movq 0x230(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x36ed0
movq 0x20(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x36670
movb %dl, 0x37(%rsp)
movq %rax, 0x38(%rsp)
jmp 0x26955
movb 0x37(%rsp), %al
movq 0x38(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movb %al, 0xc8(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x36810
movq 0x230(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x269a6
movq 0x18(%rsp), %rdi
callq 0x36840
movq 0x18(%rsp), %rdi
movl $0x50, %esi
callq 0x8350
jmp 0x26a44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x36810
jmp 0x26a4c
movq 0x90(%rsp), %rax
movl 0x8(%rax), %ecx
movl %ecx, 0xc(%rsp)
movq 0x10(%rax), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0xd419a(%rip), %rdx     # 0xfab8a
leaq 0xa0(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0xc720
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
callq 0x47ce0
jmp 0x26a12
leaq 0xa0(%rsp), %rdi
callq 0x8728
jmp 0x26a44
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x228(%rsp)
movl %eax, 0x224(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x8728
jmp 0x26a4c
addq $0x278, %rsp            # imm = 0x278
retq
movq 0x228(%rsp), %rdi
callq 0x8670
nopl (%rax)