// File: /workspace/github_cpp_makefile_star2_O3/luooofan[P]hblzbd_compiler/src/evaluate.cpp
// Function: ast::ArrayInitVal::Evaluate(ir::ContextInfo&, std::vector<ir::IR*, std::allocator<ir::IR*>>&)
void ArrayInitVal::Evaluate(ir::ContextInfo& ctx, std::vector<ir::IR*>& gIRList) {
  auto& symbol_item = (*ir::gScopes[ctx.scope_id_].symbol_table_.find(ctx.array_name_)).second;
  if (this->is_exp_) {
    this->value_->Evaluate(ctx, gIRList);
    if (ctx.opn_.type_ != ir::Opn::Type::Imm) {  // 语义错误 非常量表达式
      ir::SemanticError(this->line_no_, ctx.opn_.name_ + "非常量表达式");
    }

    symbol_item.initval_.push_back(ctx.opn_.imm_num_);
    ++ctx.array_offset_;
  } 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]->Evaluate(ctx, gIRList);

        for (int i = 1; i < this->initval_list_.size(); ++i) {
          brace_num = 1;
          this->initval_list_[i]->Evaluate(ctx, gIRList);
        }
      }
      // 补0补到finaloffset
      // while (offset < final_offset) {
      //   symbol_item.initval_.push_back(0);
      //   ++offset;
      // }
      // 语义检查
      if (offset > final_offset) {  // 语义错误 初始值设定项值太多
        ir::SemanticError(this->line_no_, "初始值设定项值太多");
      }
      symbol_item.initval_.insert(symbol_item.initval_.end(), final_offset - offset, 0);
      offset = final_offset;
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r12
movq %rsi, %rbx
movq %rdi, %r15
movslq (%rsi), %rdi
leaq 0x7d097(%rip), %rax     # 0x8f578
shlq $0x6, %rdi
addq (%rax), %rdi
addq $0x68, %rsi
callq 0x128fa
movq %rax, %r14
cmpb $0x1, 0xc(%r15)
jne 0x12590
movq 0x10(%r15), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
cmpl $0x1, 0x8(%rbx)
je 0x1256d
movl 0x8(%r15), %ebp
leaq 0x18(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x10(%rbx), %rsi
movq 0x18(%rbx), %rdx
addq %rsi, %rdx
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xc6dc
leaq 0x5c524(%rip), %rsi     # 0x6ea66
movq %r15, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b6aa
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x1256d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xc(%rbx), %rdx
movq 0x68(%r14), %rsi
cmpq 0x70(%r14), %rsi
je 0x12732
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x68(%r14)
jmp 0x1273e
movq 0x90(%rbx), %rsi
movq 0x98(%rbx), %rax
movq %rax, %rdi
subq %rsi, %rdi
sarq $0x2, %rdi
cmpq %rsi, %rax
je 0x125d4
movl 0x88(%rbx), %ecx
cmpq $0x1, %rdi
movq %rdi, %r9
adcq $0x0, %r9
xorl %r8d, %r8d
movl %ecx, %eax
cltd
idivl (%rsi,%r8,4)
testl %edx, %edx
je 0x125d7
incq %r8
cmpq %r8, %r9
jne 0x125c1
xorl %r8d, %r8d
movl 0x8c(%rbx), %eax
addl %eax, %r8d
movslq %r8d, %rcx
cmpq %rcx, %rdi
jae 0x12639
movl 0x8(%r15), %ebx
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x5c47d(%rip), %rsi     # 0x6ea79
leaq 0x5c485(%rip), %rdx     # 0x6ea88
leaq 0x8(%rsp), %rdi
callq 0xc772
leaq 0x8(%rsp), %rsi
movl %ebx, %edi
callq 0x2b6aa
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x12744
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x12744
movl -0x4(%rsi,%rcx,4), %r13d
addl 0x88(%rbx), %r13d
movq 0x18(%r15), %rcx
cmpq 0x20(%r15), %rcx
je 0x126ad
incl %eax
movl %eax, 0x8c(%rbx)
movq (%rcx), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
cmpq $0x9, %rcx
jb 0x126ad
movl $0x1, %ebp
movl $0x1, 0x8c(%rbx)
movq (%rax,%rbp,8), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
incq %rbp
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
ja 0x1267c
movl 0x88(%rbx), %eax
cmpl %r13d, %eax
jle 0x12706
movl 0x8(%r15), %ebp
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5c3bd(%rip), %rsi     # 0x6ea89
leaq 0x5c3d1(%rip), %rdx     # 0x6eaa4
leaq 0x8(%rsp), %rdi
callq 0xc772
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b6aa
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x12700
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movl 0x88(%rbx), %eax
movq %r14, %rdi
addq $0x60, %rdi
movq 0x68(%r14), %rsi
movl %r13d, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
leaq 0x8(%rsp), %rcx
movl $0x0, (%rcx)
callq 0x12b32
movl %r13d, 0x88(%rbx)
jmp 0x12744
addq $0x60, %r14
movq %r14, %rdi
callq 0x129f6
incl 0x88(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
jne 0x12787
jmp 0x12794
jmp 0x12775
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
jne 0x12787
jmp 0x12794
jmp 0x1277a
movq %rax, %rbx
jmp 0x12794
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x12794
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540