// File: /workspace/github_cpp_makefile_star2_O2/luooofan[P]hblzbd_compiler/src/arm_struct.cpp
// Function: ArmModule::EmitCode(std::ostream&)
void ArmModule::EmitCode(std::ostream& out) {
  out << ".arch armv7ve" << std::endl;
  out << ".arm" << std::endl;
  out << "@ module: " << this->name_ << std::endl;
  out << std::endl;
  out << R"(
.macro mov32, cond, reg, val
    movw\cond \reg, #:lower16:\val
    movt\cond \reg, #:upper16:\val
.endm
  )" << std::endl;
  out << std::endl;
  // .data
  auto& global_symtab = this->global_scope_.symbol_table_;
  if (!global_symtab.empty()) {
    // out << ".section .data" << std::endl;
    // out << ".align 4" << std::endl;
    // out << std::endl;
    // TODO: order?
    for (auto& symbol : global_symtab) {
      if (symbol.first == "_sysy_idx" || symbol.first == "_sysy_l1" || symbol.first == "_sysy_l2" ||
          symbol.first == "_sysy_h" || symbol.first == "_sysy_m" || symbol.first == "_sysy_s" ||
          symbol.first == "_sysy_us")
        continue;
      // NOTE: 数组常量不能删 可能会用到变量下标
      if (symbol.second.is_const_ && !symbol.second.is_array_) continue;
      bool in_bss = true;
      int last_not0 = 0;
      for (int i = 0; i < symbol.second.initval_.size(); ++i) {
        if (0 != symbol.second.initval_[i]) {
          in_bss = false;
          last_not0 = i;
        }
      }
      if (in_bss) {
        // .comm symbol length align
        out << ".comm " << symbol.first << ", " << (symbol.second.width_.empty() ? 4 : symbol.second.width_[0]) << ", 4"
            << std::endl;
      } else {
        out << "\t.global " << symbol.first << std::endl;
        out << "\t.data" << std::endl;
        out << "\t.align 4" << std::endl;
        out << "\t.type " << symbol.first << ", %object" << std::endl;
        out << symbol.first << ":" << std::endl;
        int cnt0 = 0;
        for (int i = 0; i < symbol.second.initval_.size(); ++i) {
          if (0 == symbol.second.initval_[i]) {
            ++cnt0;
          } else {
            if (0 != cnt0) {
              out << "\t.space " << cnt0 * 4 << std::endl;
              cnt0 = 0;
            }
            out << "\t.word " << symbol.second.initval_[i] << std::endl;
          }
          if (i == last_not0) break;
        }
        int space = (symbol.second.initval_.size() - last_not0 - 1) * 4;
        if (0 != space) {
          out << "\t.space " << space << std::endl;
        }
      }
      out << std::endl;
    }
  }

  // .text
  out << ".section .text" << std::endl;
  out << std::endl;
  // out << ".syntax unified" << std::endl << std::endl;
  for (auto func : this->func_list_) {
    func->EmitCode(out);
    out << std::endl;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x4b4e6(%rip), %rsi     # 0x562a9
movq %rbx, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
leaq 0x4b4dd(%rip), %rsi     # 0x562b7
movq %rbx, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
leaq 0x4b4cb(%rip), %rsi     # 0x562bc
movq %rbx, %rdi
callq 0x8240
leaq 0x8(%r12), %rsi
movq %rax, %rdi
callq 0x8230
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
callq 0x8170
leaq 0x4b4aa(%rip), %rsi     # 0x562c7
movq %rbx, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
callq 0x8170
movq 0x40(%r12), %r13
cmpq $0x0, 0x18(%r13)
je 0xb10d
addq $0x10, %r13
leaq 0x4b4e4(%rip), %r15     # 0x56334
leaq 0x4b4e7(%rip), %r14     # 0x5633e
movq (%r13), %r13
testq %r13, %r13
je 0xb10d
leaq 0x8(%r13), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
movq %r14, %rsi
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
leaq 0x4b4b7(%rip), %rsi     # 0x56347
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
leaq 0x4b4ad(%rip), %rsi     # 0x56350
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
leaq 0x4b4a2(%rip), %rsi     # 0x56358
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
leaq 0x4b497(%rip), %rsi     # 0x56360
callq 0xa469
testb %al, %al
jne 0xae57
movq %rbp, %rdi
leaq 0x4b48c(%rip), %rsi     # 0x56368
callq 0xa469
testb %al, %al
jne 0xae57
cmpb $0x1, 0x29(%r13)
jne 0xaefb
cmpb $0x1, 0x28(%r13)
jne 0xae57
movq 0x60(%r13), %rax
movq 0x68(%r13), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
movb $0x1, %dl
xorl %esi, %esi
xorl %r14d, %r14d
cmpq %rsi, %rcx
je 0xaf2a
cmpl $0x0, (%rax,%rsi,4)
sete %dil
cmovnel %esi, %r14d
andb %dil, %dl
incq %rsi
jmp 0xaf11
testb $0x1, %dl
je 0xaf8a
movq %rbx, %rdi
leaq 0x4b438(%rip), %rsi     # 0x56371
callq 0x8240
movq %rax, %rdi
movq %rbp, %rsi
callq 0x8230
movq %rax, %rdi
leaq 0x4b20c(%rip), %rsi     # 0x5615f
callq 0x8240
movq 0x48(%r13), %rcx
pushq $0x4
popq %rsi
cmpq 0x50(%r13), %rcx
je 0xaf67
movl (%rcx), %esi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
leaq 0x4b3ff(%rip), %rsi     # 0x56378
callq 0x8240
leaq 0x4b3b9(%rip), %r14     # 0x5633e
jmp 0xb0f8
movq %r12, 0x8(%rsp)
movq %rbx, %rdi
leaq 0x4b3e3(%rip), %rsi     # 0x5637c
callq 0x8240
movq %rax, %rdi
movq %rbp, %rsi
callq 0x8230
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
leaq 0x4b3cb(%rip), %rsi     # 0x56386
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
leaq 0x4b3bb(%rip), %rsi     # 0x5638d
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
leaq 0x4b3ae(%rip), %rsi     # 0x56397
callq 0x8240
movq %rax, %rdi
movq %rbp, %rsi
callq 0x8230
movq %rax, %rdi
leaq 0x4b39c(%rip), %rsi     # 0x5639f
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x8230
movq %rax, %rdi
leaq 0x4c2fb(%rip), %rsi     # 0x57320
callq 0x8240
movq %rax, %rdi
callq 0x8170
movl %r14d, %eax
incq %rax
movq %rax, 0x10(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x60(%r13), %rcx
movq 0x68(%r13), %rax
subq %rcx, %rax
movq %rax, %rdx
sarq $0x2, %rdx
cmpq %r12, %rdx
jbe 0xb0c7
cmpl $0x0, (%rcx,%r12,4)
je 0xb0b3
testl %ebp, %ebp
je 0xb088
movq %rbx, %rdi
leaq 0x4b33b(%rip), %rsi     # 0x563a9
callq 0x8240
shll $0x2, %ebp
movq %rax, %rdi
movl %ebp, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
leaq 0x4b320(%rip), %rsi     # 0x563b2
callq 0x8240
movq 0x60(%r13), %rcx
movl (%rcx,%r12,4), %esi
movq %rax, %rdi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
xorl %ebp, %ebp
jmp 0xb0b5
incl %ebp
incq %r12
cmpq %r12, 0x10(%rsp)
jne 0xb042
movq 0x68(%r13), %rax
subq 0x60(%r13), %rax
notl %r14d
leal (%rax,%r14,4), %ebp
andl $-0x4, %ebp
movq 0x8(%rsp), %r12
leaq 0x4b261(%rip), %r14     # 0x5633e
je 0xb100
movq %rbx, %rdi
leaq 0x4b2c0(%rip), %rsi     # 0x563a9
callq 0x8240
movq %rax, %rdi
movl %ebp, %esi
callq 0x84b0
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
callq 0x8170
jmp 0xae57
leaq 0x4b2a6(%rip), %rsi     # 0x563ba
movq %rbx, %rdi
callq 0x8240
movq %rax, %rdi
callq 0x8170
movq %rbx, %rdi
callq 0x8170
movq 0x28(%r12), %r14
movq 0x30(%r12), %r15
cmpq %r15, %r14
je 0xb154
movq (%r14), %rdi
movq %rbx, %rsi
callq 0xb164
movq %rbx, %rdi
callq 0x8170
addq $0x8, %r14
jmp 0xb136
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop