// File: /workspace/github_cpp_makefile_star2_O2/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 0x7207f(%rip), %rax     # 0x81548
shlq $0x6, %rdi
addq (%rax), %rdi
addq $0x68, %rsi
callq 0xf75e
movq %rax, %r14
cmpb $0x1, 0xc(%r15)
jne 0xf544
movq 0x10(%r15), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
cmpl $0x1, 0x8(%rbx)
je 0xf529
movl 0x8(%r15), %ebp
leaq 0x10(%rbx), %rsi
leaq 0x474c2(%rip), %rdx     # 0x569ca
leaq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xa334
movl %ebp, %edi
movq %r15, %rsi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
addq $0x60, %r14
leaq 0xc(%rbx), %rsi
movq %r14, %rdi
callq 0xf720
incl 0x88(%rbx)
jmp 0xf68a
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 0xf57d
movl %ecx, %eax
cltd
idivl (%rsi,%r9,4)
testl %edx, %edx
je 0xf57a
incq %r9
jmp 0xf565
movl %r9d, %r8d
movl 0x8c(%rbx), %eax
addl %eax, %r8d
movslq %r8d, %rdx
cmpq %rdx, %rdi
jae 0xf5c3
movl 0x8(%r15), %ebx
leaq 0x47444(%rip), %rsi     # 0x569dd
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0xa292
leaq 0x8(%rsp), %rsi
movl %ebx, %edi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
jmp 0xf68a
movl -0x4(%rsi,%rdx,4), %r13d
addl %ecx, %r13d
movq 0x18(%r15), %rdx
cmpq 0x20(%r15), %rdx
je 0xf628
incl %eax
movl %eax, 0x8c(%rbx)
movq (%rdx), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
pushq $0x1
popq %rbp
movq 0x18(%r15), %rax
movq 0x20(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rbp, %rcx
jbe 0xf622
movl $0x1, 0x8c(%rbx)
movq (%rax,%rbp,8), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
movq %r12, %rdx
callq *0x18(%rax)
incq %rbp
jmp 0xf5ef
movl 0x88(%rbx), %ecx
cmpl %r13d, %ecx
jle 0xf663
movl 0x8(%r15), %ebp
leaq 0x473b5(%rip), %rsi     # 0x569ed
leaq 0x8(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0xa292
leaq 0x8(%rsp), %rsi
movl %ebp, %edi
callq 0x1f699
leaq 0x8(%rsp), %rdi
callq 0x8578
movl 0x88(%rbx), %ecx
movq %r14, %rdi
addq $0x60, %rdi
movq 0x68(%r14), %rsi
movl %r13d, %eax
subl %ecx, %eax
movslq %eax, %rdx
leaq 0x8(%rsp), %rcx
andl $0x0, (%rcx)
callq 0xf73e
movl %r13d, 0x88(%rbx)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf6a4
jmp 0xf69f
jmp 0xf6a4
movq %rax, %rbx
jmp 0xf6b1
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x8578
movq %rbx, %rdi
callq 0x84d0
nop