// File: luooofan[P]hblzbd_compiler/src/generate_ir.cpp
// Function: ast::ArrayInitVal::GenerateIR(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayInitVal::GenerateIR(ir::ContextInfo &ctx, std::vector<ir::IR *> &gIRList) {
  if (this->is_exp_) {
    this->value_->GenerateIR(ctx, gIRList);
    CHECK_OPN_INT("exp")
    // genir([]=,expvalue,-,arrayname offset)
    if (ctx.opn_.type_ != OpnType::Imm || 0 != ctx.opn_.imm_num_) {
      gIRList.push_back(new ir::IR{
          IROpKind::/*OFFSET_*/ ASSIGN,
          ctx.opn_,
          {OpnType::Array, ctx.array_name_, ctx.scope_id_, new ir::Opn(OpnType::Imm, ctx.array_offset_ * 4)}});
    }
    ctx.array_offset_ += 1;
  } else {
    // 此时该结点表示一对大括号 {}
    int &offset = ctx.array_offset_;
    int &brace_num = ctx.brace_num_;

    // 根据offset和brace_num算出finaloffset
    int temp_level = 0;
    const auto &dim_total_num = ctx.dim_total_num_;
    for (int i = 0; i < dim_total_num.size(); ++i) {
      if (offset % dim_total_num[i] == 0) {
        temp_level = i;
        break;
      }
    }
    temp_level += brace_num;

    if (temp_level > dim_total_num.size()) {  // 语义错误 大括号过多
      ir::SemanticError(this->line_no_, "多余大括号");
    } else {
      int final_offset = offset + dim_total_num[temp_level - 1];
      if (!this->initval_list_.empty()) {
        ++brace_num;
        this->initval_list_[0]->GenerateIR(ctx, gIRList);

        for (int i = 1; i < this->initval_list_.size(); ++i) {
          brace_num = 1;
          this->initval_list_[i]->GenerateIR(ctx, gIRList);
        }
      }
      if (offset > final_offset) {
        ir::SemanticError(this->line_no_, "初始值设定项值太多");
      }
      offset = final_offset;
    }
  }
}

// Assembly:
subq $0x238, %rsp            # imm = 0x238
movq %rdi, 0x230(%rsp)
movq %rsi, 0x228(%rsp)
movq %rdx, 0x220(%rsp)
movq 0x230(%rsp), %rax
movq %rax, 0x78(%rsp)
testb $0x1, 0xc(%rax)
je 0x2aeda
movq 0x78(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x228(%rsp), %rsi
movq 0x220(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movq 0x228(%rsp), %rdi
addq $0x50, %rdi
callq 0x10a80
testb $0x1, %al
jne 0x2aca2
movq 0x228(%rsp), %rax
cmpl $0x4, 0x8(%rax)
jne 0x2aca2
movq 0x78(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x6c(%rsp)
movq 0x228(%rsp), %rsi
addq $0x10, %rsi
leaq 0xd06d1(%rip), %rdx     # 0xfb280
leaq 0x1c0(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0xc720
movq 0x70(%rsp), %rsi
leaq 0xd0282(%rip), %rdx     # 0xfae4f
leaq 0x1e0(%rsp), %rdi
callq 0xcbd0
jmp 0x2abdc
leaq 0xcff19(%rip), %rdx     # 0xfaafc
leaq 0x200(%rsp), %rdi
leaq 0x1e0(%rsp), %rsi
callq 0xcbd0
jmp 0x2abfa
movl 0x6c(%rsp), %edi
leaq 0x200(%rsp), %rsi
callq 0x47ce0
jmp 0x2ac0d
leaq 0x200(%rsp), %rdi
callq 0x8728
leaq 0x1e0(%rsp), %rdi
callq 0x8728
leaq 0x1c0(%rsp), %rdi
callq 0x8728
jmp 0x2aca2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2ac90
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2ac83
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
leaq 0x200(%rsp), %rdi
callq 0x8728
leaq 0x1e0(%rsp), %rdi
callq 0x8728
leaq 0x1c0(%rsp), %rdi
callq 0x8728
jmp 0x2b294
movq 0x228(%rsp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x2acc3
movq 0x228(%rsp), %rcx
xorl %eax, %eax
cmpl 0xc(%rcx), %eax
je 0x2aebe
movq 0x220(%rsp), %rax
movq %rax, 0x58(%rsp)
movl $0xe0, %edi
callq 0x8340
movq %rax, 0x60(%rsp)
movb $0x1, 0x107(%rsp)
movq 0x228(%rsp), %rsi
addq $0x8, %rsi
leaq 0x168(%rsp), %rdi
callq 0x43130
jmp 0x2ad02
movq 0x228(%rsp), %rsi
addq $0x68, %rsi
leaq 0x108(%rsp), %rdi
callq 0x81a0
jmp 0x2ad1d
movq 0x228(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x4c(%rsp)
movl $0x40, %edi
callq 0x8340
movq %rax, 0x50(%rsp)
jmp 0x2ad3c
movq 0x50(%rsp), %rdi
movq 0x228(%rsp), %rax
movl 0x88(%rax), %edx
shll $0x2, %edx
movl $0x1, %esi
callq 0x1f160
jmp 0x2ad5e
movq 0x50(%rsp), %r8
movl 0x4c(%rsp), %ecx
leaq 0x128(%rsp), %rdi
movl $0x5, %esi
leaq 0x108(%rsp), %rdx
callq 0x36610
jmp 0x2ad83
movq 0x60(%rsp), %rdi
movl $0xc, %esi
leaq 0x168(%rsp), %rdx
leaq 0x128(%rsp), %rcx
callq 0x36880
jmp 0x2ada4
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rax
movb $0x0, 0x107(%rsp)
movq %rax, 0x1a8(%rsp)
leaq 0x1a8(%rsp), %rsi
callq 0x185b0
jmp 0x2adcd
leaq 0x128(%rsp), %rdi
callq 0x1f210
leaq 0x108(%rsp), %rdi
callq 0x8728
leaq 0x168(%rsp), %rdi
callq 0x1f210
jmp 0x2aebe
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2ae9e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2ae91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2ae84
movq 0x50(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
movl $0x40, %esi
callq 0x8350
jmp 0x2ae84
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x1f210
leaq 0x108(%rsp), %rdi
callq 0x8728
leaq 0x168(%rsp), %rdi
callq 0x1f210
testb $0x1, 0x107(%rsp)
jne 0x2aeaa
jmp 0x2aeb9
movq 0x60(%rsp), %rdi
movl $0xe0, %esi
callq 0x8350
jmp 0x2b294
movq 0x228(%rsp), %rax
movl 0x88(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x88(%rax)
jmp 0x2b28c
movq 0x228(%rsp), %rax
addq $0x88, %rax
movq %rax, 0xf8(%rsp)
movq 0x228(%rsp), %rax
addq $0x8c, %rax
movq %rax, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movq 0x228(%rsp), %rax
addq $0x90, %rax
movq %rax, 0xe0(%rsp)
movl $0x0, 0xdc(%rsp)
movslq 0xdc(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x10a40
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jae 0x2afb0
movq 0xf8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x3c(%rsp)
movq 0xe0(%rsp), %rdi
movslq 0xdc(%rsp), %rsi
callq 0x1f310
movq %rax, %rcx
movl 0x3c(%rsp), %eax
cltd
idivl (%rcx)
cmpl $0x0, %edx
jne 0x2af9b
movl 0xdc(%rsp), %eax
movl %eax, 0xec(%rsp)
jmp 0x2afb0
jmp 0x2af9d
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x2af32
movq 0xf0(%rsp), %rax
movl (%rax), %eax
addl 0xec(%rsp), %eax
movl %eax, 0xec(%rsp)
movslq 0xec(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x10a40
movq %rax, %rcx
movq 0x30(%rsp), %rax
cmpq %rcx, %rax
jbe 0x2b0a7
movq 0x78(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0x24(%rsp)
leaq 0xb7(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x8680
movq 0x28(%rsp), %rdx
leaq 0xcf9e8(%rip), %rsi     # 0xfaa05
leaq 0xb8(%rsp), %rdi
callq 0xc570
jmp 0x2b02c
movl 0x24(%rsp), %edi
leaq 0xb8(%rsp), %rsi
callq 0x47ce0
jmp 0x2b03f
leaq 0xb8(%rsp), %rdi
callq 0x8728
leaq 0xb7(%rsp), %rdi
callq 0x83c0
jmp 0x2b28a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2b095
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x8728
leaq 0xb7(%rsp), %rdi
callq 0x83c0
jmp 0x2b294
movq 0xf8(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x20(%rsp)
movq 0xe0(%rsp), %rdi
movl 0xec(%rsp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x1f310
movq 0x78(%rsp), %rdi
movq %rax, %rcx
movl 0x20(%rsp), %eax
addl (%rcx), %eax
movl %eax, 0xb0(%rsp)
addq $0x18, %rdi
callq 0x1f330
testb $0x1, %al
jne 0x2b1b4
movq 0x78(%rsp), %rdi
movq 0xf0(%rsp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x18, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x1f380
movq (%rax), %rdi
movq 0x228(%rsp), %rsi
movq 0x220(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movl $0x1, 0xac(%rsp)
movq 0x78(%rsp), %rdi
movslq 0xac(%rsp), %rax
movq %rax, 0x18(%rsp)
addq $0x18, %rdi
callq 0x1f3a0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x2b1b2
movq 0x78(%rsp), %rdi
movq 0xf0(%rsp), %rax
movl $0x1, (%rax)
addq $0x18, %rdi
movslq 0xac(%rsp), %rsi
callq 0x1f380
movq (%rax), %rdi
movq 0x228(%rsp), %rsi
movq 0x220(%rsp), %rdx
movq (%rdi), %rax
callq *0x10(%rax)
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x2b13a
jmp 0x2b1b4
movq 0xf8(%rsp), %rax
movl (%rax), %eax
cmpl 0xb0(%rsp), %eax
jle 0x2b279
movq 0x78(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, 0xc(%rsp)
leaq 0x87(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x8680
movq 0x10(%rsp), %rdx
leaq 0xcf820(%rip), %rsi     # 0xfaa15
leaq 0x88(%rsp), %rdi
callq 0xc570
jmp 0x2b204
movl 0xc(%rsp), %edi
leaq 0x88(%rsp), %rsi
callq 0x47ce0
jmp 0x2b217
leaq 0x88(%rsp), %rdi
callq 0x8728
leaq 0x87(%rsp), %rdi
callq 0x83c0
jmp 0x2b279
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
jmp 0x2b26a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b8(%rsp)
movl %eax, 0x1b4(%rsp)
leaq 0x88(%rsp), %rdi
callq 0x8728
leaq 0x87(%rsp), %rdi
callq 0x83c0
jmp 0x2b294
movl 0xb0(%rsp), %ecx
movq 0xf8(%rsp), %rax
movl %ecx, (%rax)
jmp 0x2b28c
addq $0x238, %rsp            # imm = 0x238
retq
movq 0x1b8(%rsp), %rdi
callq 0x8670
nopw %cs:(%rax,%rax)