// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rdx, %r14
movq %rsi, %rbx
movq %rdi, %r15
cmpb $0x1, 0xc(%rdi)
jne 0x14921
movq 0x10(%r15), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
movq 0x50(%rbx), %rax
cmpq 0x58(%rbx), %rax
setne %cl
leaq 0x8(%rbx), %r12
movl 0x8(%rbx), %eax
cmpl $0x4, %eax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x1484d
movl 0x8(%r15), %ebp
leaq 0x10(%rbx), %rsi
leaq 0x42a68(%rip), %rdx     # 0x57250
leaq 0x68(%rsp), %r15
movq %r15, %rdi
callq 0xa334
leaq 0x4263c(%rip), %rdx     # 0x56e38
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0xa440
leaq 0x422d5(%rip), %rdx     # 0x56ae5
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0xa440
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
leaq 0x28(%rsp), %rdi
callq 0x8578
leaq 0x68(%rsp), %rdi
callq 0x8578
movl (%r12), %eax
cmpl $0x1, %eax
jne 0x1485c
cmpl $0x0, 0xc(%rbx)
je 0x14916
movl $0xe0, %edi
callq 0x8250
movq %rax, %r15
leaq 0xc8(%rsp), %rdi
movq %r12, %rsi
callq 0x1da22
leaq 0x68(%rbx), %rsi
leaq 0x48(%rsp), %rdi
callq 0x8390
movl (%rbx), %ebp
pushq $0x40
popq %rdi
callq 0x8250
movq %rax, %r12
movl 0x88(%rbx), %edx
shll $0x2, %edx
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0xf6ba
leaq 0x88(%rsp), %rdi
pushq $0x5
popq %rsi
leaq 0x48(%rsp), %rdx
movl %ebp, %ecx
movq %r12, %r8
callq 0x1a934
movb $0x1, %bpl
pushq $0xc
popq %rsi
leaq 0xc8(%rsp), %rdx
leaq 0x88(%rsp), %rcx
movq %r15, %rdi
callq 0x1aa52
leaq 0x8(%rsp), %rsi
movq %r15, (%rsi)
xorl %ebp, %ebp
movq %r14, %rdi
callq 0xe358
leaq 0x90(%rsp), %rdi
callq 0x8578
leaq 0x48(%rsp), %rdi
callq 0x8578
leaq 0xd0(%rsp), %rdi
callq 0x8578
incl 0x88(%rbx)
jmp 0x14a42
movq 0x98(%rbx), %rdi
movq 0x90(%rbx), %rsi
subq %rsi, %rdi
sarq $0x2, %rdi
movl 0x88(%rbx), %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0x1495a
movl %ecx, %eax
cltd
idivl (%rsi,%r9,4)
testl %edx, %edx
je 0x14957
incq %r9
jmp 0x14942
movl %r9d, %r8d
movl 0x8c(%rbx), %eax
addl %eax, %r8d
movslq %r8d, %rdx
cmpq %rdx, %rdi
jae 0x149a0
movl 0x8(%r15), %ebx
leaq 0x42067(%rip), %rsi     # 0x569dd
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xa292
leaq 0x8(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
jmp 0x14a42
movl -0x4(%rsi,%rdx,4), %r12d
addl %ecx, %r12d
movq 0x18(%r15), %rdx
cmpq 0x20(%r15), %rdx
je 0x14a06
incl %eax
movl %eax, 0x8c(%rbx)
movq (%rdx), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
pushq $0x1
popq %r13
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %r13, %rcx
jbe 0x14a00
movl $0x1, 0x8c(%rbx)
movq (%rax,%r13,8), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r14, %rdx
callq *0x10(%rax)
incq %r13
jmp 0x149cd
movl 0x88(%rbx), %ecx
cmpl %r12d, %ecx
jle 0x14a3b
movl 0x8(%r15), %ebp
leaq 0x41fd7(%rip), %rsi     # 0x569ed
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rdx
callq 0xa292
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
movl %r12d, 0x88(%rbx)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14a84
jmp 0x14a9d
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r12, %rdi
callq 0x8260
jmp 0x14ac6
movq %rax, %rbx
movb $0x1, %bpl
jmp 0x14ad3
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x8578
jmp 0x14a91
movq %rax, %rbx
jmp 0x14ae5
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x14aaa
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x8578
jmp 0x14aa5
movq %rax, %rbx
jmp 0x14af2
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x8578
jmp 0x14af2
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x8578
jmp 0x14ac9
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x48(%rsp), %rdi
callq 0x8578
leaq 0xd0(%rsp), %rdi
callq 0x8578
testb %bpl, %bpl
je 0x14af2
movl $0xe0, %esi
movq %r15, %rdi
callq 0x8260
movq %rbx, %rdi
callq 0x84d0