// File: /workspace/github_cpp_makefile_star2_O1/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 0x7dd55(%rip), %rax     # 0x90578
shlq $0x6, %rdi
addq (%rax), %rdi
addq $0x68, %rsi
callq 0x12c3c
movq %rax, %r14
cmpb $0x1, 0xc(%r15)
jne 0x128d2
movq 0x10(%r15), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
cmpl $0x1, 0x8(%rbx)
je 0x128af
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 0xca10
leaq 0x5d1e2(%rip), %rsi     # 0x6fa66
movq %r15, %rdi
callq 0x8590
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b12a
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x128af
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
leaq 0xc(%rbx), %rdx
movq 0x68(%r14), %rsi
cmpq 0x70(%r14), %rsi
je 0x12a74
movl (%rdx), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x68(%r14)
jmp 0x12a80
movq 0x90(%rbx), %rsi
movq 0x98(%rbx), %rax
movq %rax, %rdi
subq %rsi, %rdi
sarq $0x2, %rdi
cmpq %rsi, %rax
je 0x12916
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 0x12919
incq %r8
cmpq %r8, %r9
jne 0x12903
xorl %r8d, %r8d
movl 0x8c(%rbx), %eax
addl %eax, %r8d
movslq %r8d, %rcx
cmpq %rcx, %rdi
jae 0x1297b
movl 0x8(%r15), %ebx
leaq 0x18(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x5d13b(%rip), %rsi     # 0x6fa79
leaq 0x5d143(%rip), %rdx     # 0x6fa88
leaq 0x8(%rsp), %rdi
callq 0xcaa0
leaq 0x8(%rsp), %rsi
movl %ebx, %edi
callq 0x2b12a
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
je 0x12a86
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
jmp 0x12a86
movslq 0x88(%rbx), %rdx
movslq -0x4(%rsi,%rcx,4), %r13
addq %rdx, %r13
movq 0x18(%r15), %rcx
cmpq 0x20(%r15), %rcx
je 0x129f2
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 0x129f2
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 0x129c1
cmpl %r13d, 0x88(%rbx)
jle 0x12a43
movl 0x8(%r15), %ebp
leaq 0x18(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5d07a(%rip), %rsi     # 0x6fa89
leaq 0x5d08e(%rip), %rdx     # 0x6faa4
leaq 0x8(%rsp), %rdi
callq 0xcaa0
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x2b12a
movq 0x8(%rsp), %rdi
cmpq %r15, %rdi
je 0x12a43
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %r14, %rdi
addq $0x60, %rdi
movq 0x68(%r14), %rsi
movslq 0x88(%rbx), %rax
movq %r13, %rdx
subq %rax, %rdx
leaq 0x8(%rsp), %rcx
movl $0x0, (%rcx)
callq 0x12e74
movl %r13d, 0x88(%rbx)
jmp 0x12a86
addq $0x60, %r14
movq %r14, %rdi
callq 0x12d38
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 0x12ac9
jmp 0x12ad6
jmp 0x12ab7
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r14, %rdi
jne 0x12ac9
jmp 0x12ad6
jmp 0x12abc
movq %rax, %rbx
jmp 0x12ad6
movq %rax, %rbx
movq 0x8(%rsp), %rdi
cmpq %r12, %rdi
je 0x12ad6
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x82a0
movq %rbx, %rdi
callq 0x8540