// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %rax
movq %rsi, %rbx
movq %rdi, %r12
leaq 0x6254d(%rip), %rsi     # 0x6f329
movl $0xd, %edx
movq %rbx, %rdi
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
leaq 0x62520(%rip), %rsi     # 0x6f337
movl $0x4, %edx
movq %rbx, %rdi
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
leaq 0x624ea(%rip), %rsi     # 0x6f33c
movl $0xa, %edx
movq %rbx, %rdi
callq 0x8330
movq 0x8(%r12), %rsi
movq 0x10(%r12), %rdx
movq %rbx, %rdi
callq 0x8330
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %r15, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
leaq 0x6247e(%rip), %rsi     # 0x6f347
movl $0x6c, %edx
movq %rbx, %rdi
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq 0x40(%r12), %rax
cmpq $0x0, 0x18(%rax)
je 0xd3bd
movq 0x10(%rax), %r13
testq %r13, %r13
je 0xd3bd
leaq 0x6246c(%rip), %r14     # 0x6f3b4
leaq 0x6246f(%rip), %r15     # 0x6f3be
leaq 0x8(%r13), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
movq %r15, %rsi
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
leaq 0x62444(%rip), %rsi     # 0x6f3c7
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
leaq 0x62436(%rip), %rsi     # 0x6f3d0
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
leaq 0x62427(%rip), %rsi     # 0x6f3d8
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
leaq 0x62418(%rip), %rsi     # 0x6f3e0
callq 0x8100
testl %eax, %eax
je 0xd3b0
movq %rbp, %rdi
leaq 0x62409(%rip), %rsi     # 0x6f3e8
callq 0x8100
testl %eax, %eax
je 0xd3b0
cmpb $0x1, 0x29(%r13)
jne 0xcffe
cmpb $0x1, 0x28(%r13)
jne 0xd3b0
movq %r12, (%rsp)
movq 0x60(%r13), %rax
movq 0x68(%r13), %rdx
subq %rax, %rdx
je 0xd03c
sarq $0x2, %rdx
cmpq $0x1, %rdx
adcq $0x0, %rdx
xorl %esi, %esi
movb $0x1, %cl
xorl %r12d, %r12d
xorl %edi, %edi
cmpl $0x0, (%rax,%rsi,4)
cmovnel %esi, %r12d
movzbl %cl, %ecx
cmovnel %edi, %ecx
incq %rsi
cmpq %rsi, %rdx
jne 0xd024
jmp 0xd041
movb $0x1, %cl
xorl %r12d, %r12d
testb $0x1, %cl
je 0xd096
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x6239c(%rip), %rsi     # 0x6f3f1
callq 0x8330
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
movq %rbx, %rdi
callq 0x8330
movq %rax, %rbp
movl $0x2, %edx
movq %rax, %rdi
leaq 0x62163(%rip), %rsi     # 0x6f1df
callq 0x8330
movq 0x48(%r13), %rax
cmpq 0x50(%r13), %rax
je 0xd339
movl (%rax), %esi
jmp 0xd33e
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x62357(%rip), %rsi     # 0x6f3fc
callq 0x8330
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
movq %rbx, %rdi
callq 0x8330
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x62313(%rip), %rsi     # 0x6f406
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x622df(%rip), %rsi     # 0x6f40d
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x622ae(%rip), %rsi     # 0x6f417
callq 0x8330
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
movq %rbx, %rdi
callq 0x8330
movq %rax, %rbp
movl $0x9, %edx
movq %rax, %rdi
leaq 0x6228f(%rip), %rsi     # 0x6f41f
callq 0x8330
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq 0x8(%r13), %rsi
movq 0x10(%r13), %rdx
movq %rbx, %rdi
callq 0x8330
movq %rax, %rbp
movl $0x1, %edx
movq %rax, %rdi
leaq 0x631e1(%rip), %rsi     # 0x703c0
callq 0x8330
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq 0x60(%r13), %rax
cmpq %rax, 0x68(%r13)
je 0xd2f0
movl %r12d, %r14d
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpl $0x0, (%rax,%r15,4)
je 0xd2ce
testl %ebp, %ebp
je 0xd27c
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x621e9(%rip), %rsi     # 0x6f429
callq 0x8330
shll $0x2, %ebp
movq %rbx, %rdi
movl %ebp, %esi
callq 0x8520
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movl $0x7, %edx
movq %rbx, %rdi
leaq 0x621a7(%rip), %rsi     # 0x6f432
callq 0x8330
movq 0x60(%r13), %rax
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x8520
movq %rax, %rbp
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
xorl %ebp, %ebp
jmp 0xd2d0
incl %ebp
cmpq %r15, %r14
je 0xd2f0
incq %r15
movq 0x60(%r13), %rax
movq 0x68(%r13), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %r15, %rcx
ja 0xd222
movl 0x68(%r13), %eax
subl 0x60(%r13), %eax
notl %r12d
leal (%rax,%r12,4), %ebp
andl $-0x4, %ebp
movq (%rsp), %r12
leaq 0x620a7(%rip), %r14     # 0x6f3b4
leaq 0x620aa(%rip), %r15     # 0x6f3be
je 0xd389
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x62104(%rip), %rsi     # 0x6f429
callq 0x8330
movq %rbx, %rdi
movl %ebp, %esi
callq 0x8520
movq %rax, %rbp
jmp 0xd361
movl $0x4, %esi
movq (%rsp), %r12
movq %rbp, %rdi
callq 0x8520
movq %rax, %rbp
movl $0x3, %edx
movq %rax, %rdi
leaq 0x6209c(%rip), %rsi     # 0x6f3f8
callq 0x8330
movq (%rbp), %rax
movq -0x18(%rax), %rdi
addq %rbp, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbp, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq (%r13), %r13
testq %r13, %r13
jne 0xcf4f
leaq 0x62076(%rip), %rsi     # 0x6f43a
movl $0xe, %edx
movq %rbx, %rdi
callq 0x8330
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
movq 0x28(%r12), %r14
movq 0x30(%r12), %r15
cmpq %r15, %r14
je 0xd466
movq (%r14), %rdi
movq %rbx, %rsi
callq 0xd476
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x8270
movsbl %al, %esi
movq %rbx, %rdi
callq 0x8030
movq %rax, %rdi
callq 0x81f0
addq $0x8, %r14
jmp 0xd429
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop