// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::ArrayDefineWithInit::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayDefineWithInit::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  auto &name = this->name_.name_.name_;
  auto &scope = ir::gScopes[ctx.scope_id_];
  auto &symbol_table = scope.symbol_table_;
  // NOTE: 只需在当前作用域查找即可 找到则重定义
  const auto &array_iter = symbol_table.find(name);
  if (array_iter == symbol_table.end()) {
    // NOTE: 常量数组不一定能删 因为可能会有变量下标
    // if (this->is_const_) {
    // symbol_table.insert({name, ir::SymbolTableItem(true, true, -1)});
    // } else {
    symbol_table.insert({name, ir::SymbolTableItem(true, this->is_const_)});
    // }
    auto &symbol_item = symbol_table.find(name)->second;
    // 填shape
    for (const auto &shape : this->name_.shape_list_) {
      shape->Evaluate(ctx, gIRList);
      // 如果Evaluate没有执行成功会直接退出程序
      // 如果执行成功说明返回的一定是立即数
      symbol_item.shape_.push_back(ctx.opn_.imm_num_);
    }

    // 填width和dim total num
    std::stack<int> temp_width;
    int width = ir::kIntWidth;
    temp_width.push(width);
    for (auto reiter = symbol_item.shape_.rbegin(); reiter != symbol_item.shape_.rend(); ++reiter) {
      width *= *reiter;
      temp_width.push(width);
    }
    while (!temp_width.empty()) {
      symbol_item.width_.push_back(temp_width.top());
      temp_width.pop();
    }

    // 完成赋值操作 或者 填写initval(global or const)
    // prepare for arrayinitval
    ctx.dim_total_num_.clear();
    for (const auto width : symbol_item.width_) {
      ctx.dim_total_num_.push_back(width / ir::kIntWidth);
    }
    // ctx.dim_total_num_.push_back(1);
    ctx.array_name_ = name;
    ctx.array_offset_ = 0;
    ctx.brace_num_ = 1;

    if (ctx.scope_id_ == 0) {
      this->value_.Evaluate(ctx, gIRList);
    } else {
      // GenIR ALLOCA
      gIRList.push_back(new ir::IR{IROpKind::ALLOCA,
                                   ir::Opn{OpnType::Array, name, ctx.scope_id_, new ir::Opn(OpnType::Imm, 0)},
                                   ir::Opn{OpnType::Imm, symbol_item.width_[0]}});
      gIRList.push_back(new ir::IR(IROpKind::PARAM, {OpnType::Imm, symbol_item.width_[0]}));
      gIRList.push_back(new ir::IR(IROpKind::PARAM, IMM_0_OPN));
      gIRList.push_back(
          new ir::IR(IROpKind::PARAM, {OpnType::Array, name, ctx.scope_id_, new ir::Opn(OpnType::Imm, 0)}));
      ir::Opn temp = ir::Opn(OpnType::Null);
      // ctx.opn_ = temp;
      // ctx.shape_.clear();
      // TODO: 这里用name会导致段错误
      auto opn1 = ir::Opn(OpnType::Func, std::string("memset"));
      auto opn2 = ir::Opn(OpnType::Imm, 3);
      gIRList.push_back(new ir::IR(IROpKind::CALL, opn1, opn2, temp));
      if (this->is_const_) {
        ctx.array_offset_ = 0;
        ctx.brace_num_ = 1;
        this->value_.Evaluate(ctx, gIRList);
      }
      ctx.array_offset_ = 0;
      ctx.brace_num_ = 1;
      this->value_.GenerateIR(ctx, gIRList);
    }
  } 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 $0x3d8, %rsp            # imm = 0x3D8
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r12
movq 0x10(%rdi), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rbp
movslq (%rsi), %r13
leaq 0x6ee40(%rip), %rax     # 0x81548
shlq $0x6, %r13
addq (%rax), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0xf75e
testq %rax, %rax
je 0x12764
movl 0x8(%r12), %ebx
movq 0x10(%r12), %rax
movq 0x10(%rax), %rax
movq 0x10(%rax), %rsi
leaq 0x4443b(%rip), %rdx     # 0x56b73
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0xa334
movl %ebx, %edi
movq %r14, %rsi
callq 0x1f699
leaq 0x80(%rsp), %rdi
callq 0x8578
jmp 0x12cfd
movzbl 0xc(%r12), %edx
leaq 0x30(%rsp), %r15
pushq $0x1
popq %rsi
movq %r15, %rdi
callq 0x1a960
leaq 0x80(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1acd0
movq %r12, 0x18(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x28(%rsp)
leaq 0x80(%rsp), %rsi
movq %r13, %rdi
callq 0x1c154
leaq 0x80(%rsp), %rdi
callq 0x1aa18
leaq 0x30(%rsp), %rdi
callq 0x1aa2e
movq %r13, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rsi
callq 0xf75e
movq %rax, %rbp
movq 0x18(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rax), %rbx
movq 0x20(%rax), %r14
leaq 0x30(%rbp), %r13
movq 0x10(%rsp), %rax
leaq 0xc(%rax), %r15
cmpq %r14, %rbx
je 0x1281d
movq (%rbx), %rdi
movq (%rdi), %rax
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
callq *0x18(%rax)
movq %r13, %rdi
movq %r15, %rsi
callq 0xf720
addq $0x10, %rbx
jmp 0x127f4
leaq 0x80(%rsp), %r15
movq %r15, %rdi
callq 0x1ad28
leaq 0x4490c(%rip), %rax     # 0x57140
movl (%rax), %ebx
leaq 0xc(%rsp), %rsi
movl %ebx, (%rsi)
movq %r15, %rdi
callq 0x1cd82
movq 0x38(%rbp), %r14
leaq 0x80(%rsp), %r15
leaq 0xc(%rsp), %r12
cmpq (%r13), %r14
je 0x1287a
movl 0xc(%rsp), %eax
imull -0x4(%r14), %eax
addq $-0x4, %r14
movl %eax, 0xc(%rsp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x1cd82
jmp 0x12856
movq %rbp, %r13
addq $0x48, %r13
movl $0x200, %r12d           # imm = 0x200
leaq 0x80(%rsp), %r15
movq 0x10(%rsp), %r14
movq 0xb0(%rsp), %rsi
cmpq 0x90(%rsp), %rsi
je 0x128d5
cmpq 0xb8(%rsp), %rsi
jne 0x128bf
movq 0xc8(%rsp), %rax
movq -0x8(%rax), %rsi
addq %r12, %rsi
addq $-0x4, %rsi
movq %r13, %rdi
callq 0xf720
movq %r15, %rdi
callq 0x1cfb6
jmp 0x12894
movq 0x90(%r14), %rax
cmpq %rax, 0x98(%r14)
je 0x128ec
movq %rax, 0x98(%r14)
leaq 0x90(%r14), %r15
movq 0x48(%rbp), %r12
movq 0x50(%rbp), %r14
leaq 0x30(%rsp), %rbp
cmpq %r14, %r12
je 0x12921
movl (%r12), %eax
cltd
idivl %ebx
movl %eax, 0x30(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x1c9ca
addq $0x4, %r12
jmp 0x12900
movq 0x10(%rsp), %rax
leaq 0x68(%rax), %rdi
movq 0x20(%rsp), %rsi
callq 0x81a0
movabsq $0x100000000, %rax      # imm = 0x100000000
movq 0x10(%rsp), %r14
movq %rax, 0x88(%r14)
cmpl $0x0, (%r14)
je 0x12cd9
movl $0xe0, %edi
callq 0x8250
movq 0x28(%rsp), %rbx
movq %rax, %r12
leaq 0x138(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x8390
movl (%r14), %ebp
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0xf6ba
leaq 0x398(%rsp), %rdi
pushq $0x5
popq %rsi
leaq 0x138(%rsp), %rdx
movl %ebp, %ecx
movq %r15, %r8
callq 0x1a934
movq (%r13), %rax
movl (%rax), %edx
leaq 0x358(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movb $0x1, %bpl
pushq $0x16
popq %rsi
leaq 0x398(%rsp), %rdx
leaq 0x358(%rsp), %rcx
movq %r12, %rdi
callq 0x1aa52
leaq 0x30(%rsp), %rsi
movq %r12, (%rsi)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe358
leaq 0x360(%rsp), %rdi
callq 0x8578
leaq 0x3a0(%rsp), %rdi
callq 0x8578
leaq 0x138(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %rbp
movq (%r13), %rax
movl (%rax), %edx
leaq 0x318(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0xf6ba
movb $0x1, %r15b
pushq $0x8
popq %rsi
leaq 0x318(%rsp), %rdx
movq %rbp, %rdi
callq 0x1abbe
leaq 0x30(%rsp), %rsi
movq %rbp, (%rsi)
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0xe358
leaq 0x320(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %r13
leaq 0x2d8(%rsp), %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0xf6ba
movb $0x1, %bpl
pushq $0x8
popq %rsi
leaq 0x2d8(%rsp), %rdx
movq %r13, %rdi
callq 0x1abbe
leaq 0x30(%rsp), %rsi
movq %r13, (%rsi)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe358
leaq 0x2e0(%rsp), %rdi
callq 0x8578
movl $0xe0, %edi
callq 0x8250
movq %rax, %r13
leaq 0x118(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x8390
movl (%r14), %ebp
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %r15
pushq $0x1
popq %rsi
movq %rax, %rdi
xorl %edx, %edx
callq 0xf6ba
leaq 0x298(%rsp), %rdi
pushq $0x5
popq %rsi
leaq 0x118(%rsp), %rdx
movl %ebp, %ecx
movq %r15, %r8
callq 0x1a934
movb $0x1, %bpl
pushq $0x8
popq %rsi
leaq 0x298(%rsp), %rdx
movq %r13, %rdi
callq 0x1abbe
leaq 0x30(%rsp), %rsi
movq %r13, (%rsi)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe358
leaq 0x2a0(%rsp), %rdi
callq 0x8578
leaq 0x118(%rsp), %rdi
callq 0x8578
leaq 0x30(%rsp), %rdi
pushq $0x4
popq %rsi
callq 0x1ac68
leaq 0x43d8c(%rip), %rsi     # 0x56903
leaq 0xf8(%rsp), %rdi
leaq 0x158(%rsp), %rdx
callq 0xa292
leaq 0x258(%rsp), %rdi
pushq $0x3
popq %rsi
leaq 0xf8(%rsp), %rdx
callq 0x1ac46
leaq 0xf8(%rsp), %rdi
callq 0x8578
leaq 0x158(%rsp), %rdi
pushq $0x1
popq %rsi
pushq $0x3
popq %rdx
callq 0xf6ba
movl $0xe0, %edi
callq 0x8250
movq %rax, %r12
leaq 0x218(%rsp), %rdi
leaq 0x258(%rsp), %rsi
callq 0x1da22
leaq 0x1d8(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x1da22
leaq 0x198(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x1da22
movb $0x1, %bpl
pushq $0x9
popq %rsi
leaq 0x218(%rsp), %rdx
leaq 0x1d8(%rsp), %rcx
leaq 0x198(%rsp), %r8
movq %r12, %rdi
callq 0x1ab2e
leaq 0xf0(%rsp), %rsi
movq %r12, (%rsi)
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xe358
leaq 0x1a0(%rsp), %rdi
callq 0x8578
leaq 0x1e0(%rsp), %rdi
callq 0x8578
leaq 0x220(%rsp), %rdi
callq 0x8578
movq 0x18(%rsp), %r15
cmpb $0x1, 0xc(%r15)
movabsq $0x100000000, %r12      # imm = 0x100000000
jne 0x12c9c
movq %r12, 0x88(%r14)
movq 0x18(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x18(%rax)
movq %r12, 0x88(%r14)
movq 0x18(%r15), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq %rbx, %rdx
callq *0x10(%rax)
leaq 0x160(%rsp), %rdi
callq 0x8578
leaq 0x260(%rsp), %rdi
callq 0x8578
leaq 0x38(%rsp), %rdi
callq 0x8578
jmp 0x12cf0
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movq (%rdi), %rax
movq %r14, %rsi
movq 0x28(%rsp), %rdx
callq *0x18(%rax)
leaq 0x80(%rsp), %rdi
callq 0x1b604
addq $0x3d8, %rsp            # imm = 0x3D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x12def
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x12dfc
movq %rax, %rbx
jmp 0x12e0e
jmp 0x12db6
movq %rax, %rbx
jmp 0x12e28
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
jmp 0x12e30
movq %rax, %rbx
jmp 0x12e35
jmp 0x12f06
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r15, %rdi
callq 0x8260
jmp 0x12e59
jmp 0x12d71
jmp 0x12f06
movq %rax, %rbx
jmp 0x12e7f
jmp 0x12f06
movq %rax, %rbx
jmp 0x12e9e
jmp 0x12f06
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x12eb8
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r15, %rdi
callq 0x8260
jmp 0x12eca
movq %rax, %rbx
jmp 0x12edf
jmp 0x12f06
movq %rax, %rbx
jmp 0x12e1b
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1aa18
jmp 0x12dd0
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x1aa2e
jmp 0x12f16
movq %rax, %rbx
leaq 0x1a0(%rsp), %rdi
callq 0x8578
leaq 0x1e0(%rsp), %rdi
callq 0x8578
leaq 0x220(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x12e1b
movl $0xe0, %esi
movq %r12, %rdi
callq 0x8260
leaq 0x160(%rsp), %rdi
callq 0x8578
leaq 0x260(%rsp), %rdi
callq 0x8578
leaq 0x38(%rsp), %rdi
callq 0x8578
jmp 0x12f09
movq %rax, %rbx
leaq 0x2a0(%rsp), %rdi
callq 0x8578
jmp 0x12e5c
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x118(%rsp), %rdi
jmp 0x12e71
movq %rax, %rbx
leaq 0x2e0(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x12f09
movl $0xe0, %esi
movq %r13, %rdi
jmp 0x12ee7
movq %rax, %rbx
leaq 0x320(%rsp), %rdi
callq 0x8578
testb %r15b, %r15b
je 0x12f09
movl $0xe0, %esi
movq %rbp, %rdi
jmp 0x12ee7
movq %rax, %rbx
leaq 0x360(%rsp), %rdi
callq 0x8578
leaq 0x3a0(%rsp), %rdi
callq 0x8578
jmp 0x12ecd
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x138(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x12f09
movl $0xe0, %esi
movq %r12, %rdi
callq 0x8260
jmp 0x12f09
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x8578
jmp 0x12f16
jmp 0x12f06
jmp 0x12f06
jmp 0x12f06
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x1b604
movq %rbx, %rdi
callq 0x84d0