// File: /workspace/github_cpp_makefile_star2_O3/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 0x782bb(%rip), %rax     # 0x8f578
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 0x128fa
testq %rax, %rax
je 0x17347
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 0xc6dc
leaq 0x578fb(%rip), %rsi     # 0x6ec13
movq %r14, %rdi
callq 0x8590
leaq 0x48(%rsp), %rsi
movl %ebx, %edi
callq 0x2b6aa
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x17669
movq 0x58(%rsp), %rsi
incq %rsi
jmp 0x17664
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 0x25368
movq %r14, 0x8(%rsp)
movq 0x10(%r14), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %r13
cmpq %r13, %r15
je 0x173d3
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 0x173bf
movl (%r14), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x10(%rbx)
jmp 0x173ca
movq %r12, %rdi
movq %r14, %rdx
callq 0x129f6
addq $0x10, %r15
cmpq %r13, %r15
jne 0x17395
movq 0x10(%rsp), %r13
addq $0x20, %r13
movq 0x10(%rbx), %rsi
subq 0x8(%rbx), %rsi
sarq $0x2, %rsi
incq %rsi
movq %r13, %rdi
callq 0x2591c
leaq 0x57de6(%rip), %rax     # 0x6f1e0
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 0x1742a
movq 0x8(%rbx), %rcx
movl %edx, %edx
movl 0x4(%rax,%rdx,4), %esi
imull (%rcx,%rdx,4), %esi
movl %esi, (%rax,%rdx,4)
addq $-0x1, %rdx
jb 0x1741d
cmpl $0x0, (%rbp)
movq 0x8(%rsp), %r14
je 0x17585
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 0xc6dc
movl (%rbp), %r15d
movl $0x40, %edi
callq 0x8290
movq %rax, %rbp
movq %rax, %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1279c
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 0xc6dc
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 0x1279c
movb $0x1, %bpl
leaq 0xb8(%rsp), %rdx
leaq 0xf8(%rsp), %rcx
movq %r12, %r15
movq %r12, %rdi
movl $0x16, %esi
callq 0x25560
leaq 0x48(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq 0x18(%rsp), %rdi
callq 0x11182
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x17547
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x17564
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
movq 0x8(%rsp), %r14
je 0x17585
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 0x257dc
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x26bbc
movq 0xa0(%rsp), %rdi
testq %rdi, %rdi
je 0x175cb
movq 0xb0(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x175e8
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x17602
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x82a0
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1761d
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0x38(%rbx), %rdi
testq %rdi, %rdi
je 0x17632
movq 0x48(%rbx), %rsi
subq %rdi, %rsi
callq 0x82a0
movq (%r13), %rdi
testq %rdi, %rdi
je 0x17647
movq 0x30(%rbx), %rsi
subq %rdi, %rsi
callq 0x82a0
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x1765c
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 0x176e3
movq %rax, %r15
movl $0x40, %esi
movq %rbp, %rdi
callq 0x82a0
jmp 0x17705
movq %rax, %r15
jmp 0x17729
movq %rax, %r15
leaq 0x48(%rsp), %rdi
callq 0x254bc
jmp 0x1774f
movq %rax, %r15
movl $0x50, %esi
movq %rbx, %rdi
jmp 0x1774a
movq %rax, %r15
leaq 0x110(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x176e3
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x82a0
movq 0xc0(%rsp), %rdi
cmpq %r14, %rdi
je 0x17708
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x17708
movq %rax, %r15
movb $0x1, %bpl
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x17724
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x82a0
testb %bpl, %bpl
je 0x1774f
movl $0xe0, %esi
movq %r12, %rdi
jmp 0x1774a
jmp 0x17735
movq %rax, %r15
movq 0x48(%rsp), %rdi
cmpq %r12, %rdi
je 0x1774f
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r15, %rdi
callq 0x8540
nop