// File: /workspace/github_cpp_makefile_star2_O3/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 0x6185e(%rip), %rsi     # 0x6e329
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 0x61831(%rip), %rsi     # 0x6e337
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 0x617fb(%rip), %rsi     # 0x6e33c
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 0x6178f(%rip), %rsi     # 0x6e347
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 0xd0ab
movq 0x10(%rax), %r13
testq %r13, %r13
je 0xd0ab
leaq 0x6177d(%rip), %r15     # 0x6e3b4
leaq 0x61780(%rip), %r14     # 0x6e3be
leaq 0x8(%r13), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
movq %r14, %rsi
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
leaq 0x61755(%rip), %rsi     # 0x6e3c7
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
leaq 0x61747(%rip), %rsi     # 0x6e3d0
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
leaq 0x61738(%rip), %rsi     # 0x6e3d8
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
leaq 0x61729(%rip), %rsi     # 0x6e3e0
callq 0x8100
testl %eax, %eax
je 0xd09e
movq %rbp, %rdi
leaq 0x6171a(%rip), %rsi     # 0x6e3e8
callq 0x8100
testl %eax, %eax
je 0xd09e
cmpb $0x1, 0x29(%r13)
jne 0xcced
cmpb $0x1, 0x28(%r13)
jne 0xd09e
movq %r12, 0x8(%rsp)
movq 0x60(%r13), %rax
movq 0x68(%r13), %rcx
subq %rax, %rcx
je 0xcd2d
sarq $0x2, %rcx
cmpq $0x1, %rcx
adcq $0x0, %rcx
xorl %esi, %esi
movb $0x1, %dl
xorl %r12d, %r12d
cmpl $0x0, (%rax,%rsi,4)
cmovnel %esi, %r12d
sete %dil
andb %dil, %dl
incq %rsi
cmpq %rsi, %rcx
jne 0xcd12
testb %dl, %dl
je 0xcd7d
movl $0x6, %edx
movq %rbx, %rdi
leaq 0x616b5(%rip), %rsi     # 0x6e3f1
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 0x6147c(%rip), %rsi     # 0x6e1df
callq 0x8330
movq 0x48(%r13), %rax
cmpq 0x50(%r13), %rax
je 0xcfe6
movl (%rax), %esi
jmp 0xcfeb
movl $0x9, %edx
movq %rbx, %rdi
leaq 0x61670(%rip), %rsi     # 0x6e3fc
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 0x6162c(%rip), %rsi     # 0x6e406
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 0x615f8(%rip), %rsi     # 0x6e40d
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 0x615c7(%rip), %rsi     # 0x6e417
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 0x615a8(%rip), %rsi     # 0x6e41f
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 0x624fa(%rip), %rsi     # 0x6f3c0
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), %rcx
movq 0x68(%r13), %rax
cmpq %rcx, %rax
je 0xd011
movl %r12d, %edx
movq %rdx, 0x10(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpl $0x0, (%rcx,%r14,4)
je 0xcfc5
testl %ebp, %ebp
je 0xcf6b
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x614fa(%rip), %rsi     # 0x6e429
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 0x614b8(%rip), %rsi     # 0x6e432
callq 0x8330
movq 0x60(%r13), %rax
movl (%rax,%r14,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
movq 0x60(%r13), %rcx
movq 0x68(%r13), %rax
xorl %ebp, %ebp
jmp 0xcfc7
incl %ebp
cmpq %r14, 0x10(%rsp)
je 0xd014
incq %r14
movq %rax, %rdx
subq %rcx, %rdx
sarq $0x2, %rdx
cmpq %r14, %rdx
ja 0xcf11
jmp 0xd014
movl $0x4, %esi
movq %rbp, %rdi
callq 0x8520
movq %rax, %rbp
movl $0x3, %edx
movq %rax, %rdi
leaq 0x613f3(%rip), %rsi     # 0x6e3f8
callq 0x8330
movq 0x8(%rsp), %r12
jmp 0xd04f
movq %rax, %rcx
subl %ecx, %eax
notl %r12d
leal (%rax,%r12,4), %ebp
andl $-0x4, %ebp
movq 0x8(%rsp), %r12
leaq 0x61392(%rip), %r14     # 0x6e3be
je 0xd077
movl $0x8, %edx
movq %rbx, %rdi
leaq 0x613ec(%rip), %rsi     # 0x6e429
callq 0x8330
movq %rbx, %rdi
movl %ebp, %esi
callq 0x8520
movq %rax, %rbp
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 0xcc3e
leaq 0x61388(%rip), %rsi     # 0x6e43a
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 0xd154
movq (%r14), %rdi
movq %rbx, %rsi
callq 0xd164
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 0xd117
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop