// File: /workspace/github_c_makefile_star2_O2/sisshiki1969[P]monocc/src/codegen.c
// Function: emit_global
void emit_global(Type *type, Node *init) {
  if (!init) {
    // char str[];  => incomplete type
    if (is_array(type) && type->array_size == 0)
      error_at_token(init->token, "Incomplete type is not allowed.");
    fprintf(output, "\t.zero %d\n", sizeof_type(type));
    return;
  }
  long i = 0;
  Vector *vec;
  MemberInfo *member;
  int offset = 0;
  switch (type->ty) {
    case CHAR:
      fprintf(output, "\t.byte %ld\n", eval_const(init)->num_val);
      return;
    case SHORT:
    case USHORT:
      fprintf(output, "\t.word %ld\n", eval_const(init)->num_val);
      return;
    case INT:
    case UINT:
      fprintf(output, "\t.long %ld\n", eval_const(init)->num_val);
      return;
    case LONG:
    case ULONG:
      fprintf(output, "\t.quad %ld\n", eval_const(init)->num_val);
      return;

    case ARRAY:
      if (type->ptr_to->ty == CHAR) {
        if (init->kind == ND_STR) {
          // char str[] = "sample";
          int str_size = 1;
          fprintf(output, "\t.string \"");
          for (Token *t = init->token; t; t = t->next) {
            fprintf(output, "%.*s", t->len, t->str);
            str_size += t->len;
          }
          fprintf(output, "\"\n");
          int ary_size = type->array_size;
          if (ary_size > str_size)
            fprintf(output, "\t.zero %d\n", ary_size - str_size);
          return;
        }
      }
      if (init->kind != ND_INIT)
        error_at_node(init, "Unsupported initializer.");
      vec = init->nodes;
      type = type->ptr_to;
      for (int i = 0; i < vec_len(vec); i++) {
        emit_global(type, vec->data[i]);
      }
      return;

    case STRUCT:
      if (init->kind != ND_INIT)
        error_at_node(init, "Unsupported initializer.");
      vec = init->nodes;
      member = type->member;
      for (int i = 0; i < vec_len(vec); i++) {
        if (member->offset > offset) {
          fprintf(output, "\t.zero %d\n", member->offset - offset);
          offset = member->offset;
        }
        emit_global(member->type, vec->data[i]);
        offset += sizeof_type(member->type);
        member = member->next;
      }
      if (sizeof_type(type) > offset) {
        fprintf(output, "\t.zero %d\n", sizeof_type(type) - offset);
      }
      return;

    case PTR:
      if (type->ptr_to->ty == CHAR && init->kind == ND_STR) {
        // char *str = "sample";
        fprintf(output, "\t.quad .LS%06d\n", init->str_id);
        return;
      }
      if (init->kind == ND_ADDR && init->lhs->kind == ND_GVAR) {
        fprintf(output, "\t.quad %.*s\n", init->lhs->token->len,
                init->lhs->token->str);
        return;
      }
      if (init->kind == ND_CAST && init->lhs->kind == ND_NUM) {
        fprintf(output, "\t.quad %ld\n", init->lhs->num_val);
        return;
      }
      error_at_node(init, "Unsupported initializer.");
      return;

    default:
      error_at_node(init, "Unsupported initializer.");
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
testq %rsi, %rsi
je 0x3199
movq %rsi, %r14
movl (%rbx), %eax
decl %eax
cmpl $0xb, %eax
ja 0x34a7
leaq 0x6fae(%rip), %rcx      # 0xa11c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xe0a2(%rip), %rax      # 0x11220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x30a4
movq 0x20(%rax), %rdx
leaq 0x78d5(%rip), %rsi      # 0xaa69
jmp 0x324f
movq %rbx, %rdi
callq 0x919f
testb %al, %al
je 0x31c1
cmpl $0x0, 0x10(%rbx)
jne 0x31c1
movq 0x50, %rdi
leaq 0x7878(%rip), %rsi      # 0xaa32
xorl %eax, %eax
callq 0x48b3
leaq 0xe058(%rip), %rax      # 0x11220
movq (%rax), %r14
movq %rbx, %rdi
callq 0x929b
leaq 0x7878(%rip), %rsi      # 0xaa52
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
leaq 0xe025(%rip), %rax      # 0x11220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x30a4
movq 0x20(%rax), %rdx
leaq 0x7864(%rip), %rsi      # 0xaa75
jmp 0x324f
leaq 0xe006(%rip), %rax      # 0x11220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x30a4
movq 0x20(%rax), %rdx
leaq 0x7851(%rip), %rsi      # 0xaa81
jmp 0x324f
leaq 0xdfe7(%rip), %rax      # 0x11220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x30a4
movq 0x20(%rax), %rdx
leaq 0x780e(%rip), %rsi      # 0xaa5d
movq %rbx, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
movq 0x8(%rbx), %rax
movl (%rax), %ecx
xorl $0x1, %ecx
movl (%r14), %eax
movl %eax, %edx
xorl $0x2, %edx
orl %ecx, %edx
jne 0x33b5
leaq 0xdf99(%rip), %rax      # 0x11220
movq (%rax), %rdi
movl 0x28(%r14), %edx
leaq 0x7808(%rip), %rsi      # 0xaa9d
jmp 0x31df
cmpl $0x3, (%r14)
je 0x32b1
leaq 0x852b(%rip), %rsi      # 0xb7d2
movq %r14, %rdi
xorl %eax, %eax
callq 0x4a9e
movq 0x48(%r14), %r14
leaq 0x20(%rbx), %r13
leaq 0x7792(%rip), %r15      # 0xaa52
xorl %r12d, %r12d
xorl %ebp, %ebp
movq (%r13), %r13
movq %r14, %rdi
callq 0x34f2
cltq
cmpq %rax, %r12
jge 0x3318
movl 0x18(%r13), %edx
subl %ebp, %edx
jle 0x32f8
leaq 0xdf39(%rip), %rax      # 0x11220
movq (%rax), %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10f0
movl 0x18(%r13), %ebp
movq 0x10(%r13), %rdi
movq (%r14), %rax
movq (%rax,%r12,8), %rsi
callq 0x3144
movq 0x10(%r13), %rdi
callq 0x929b
addl %eax, %ebp
incq %r12
jmp 0x32c5
movq %rbx, %rdi
callq 0x929b
cmpl %ebp, %eax
jle 0x3475
leaq 0xdef1(%rip), %rax      # 0x11220
movq (%rax), %r14
movq %rbx, %rdi
callq 0x929b
subl %ebp, %eax
jmp 0x31d3
movq 0x8(%rbx), %r15
movl (%r15), %ecx
xorl $0x1, %ecx
movl (%r14), %eax
movl %eax, %edx
xorl $0x2, %edx
orl %ecx, %edx
jne 0x3405
leaq 0xdebe(%rip), %r12      # 0x11220
movq (%r12), %rcx
leaq 0x7720(%rip), %rdi      # 0xaa8d
pushq $0xa
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
addq $0x50, %r14
pushq $0x1
popq %rbp
leaq 0x7712(%rip), %r15      # 0xaa98
movq (%r14), %r14
movq (%r12), %rax
testq %r14, %r14
je 0x3449
movl 0x28(%r14), %edx
movq 0x20(%r14), %rcx
movq %rax, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x10f0
addl 0x28(%r14), %ebp
addq $0x8, %r14
jmp 0x3386
cmpl $0x1c, %eax
je 0x3484
cmpl $0x16, %eax
jne 0x34a7
movq 0x8(%r14), %rax
cmpl $0x19, (%rax)
jne 0x34a7
leaq 0xde45(%rip), %rcx      # 0x11220
movq (%rcx), %rdi
movq 0x50(%rax), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
leaq 0x76bd(%rip), %rsi      # 0xaaad
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
cmpl $0x3, %eax
je 0x341f
leaq 0x83c1(%rip), %rsi      # 0xb7d2
movq %r14, %rdi
xorl %eax, %eax
callq 0x4a9e
movq 0x8(%rbx), %r15
movq 0x48(%r14), %rbx
xorl %r14d, %r14d
movq %rbx, %rdi
callq 0x34f2
cltq
cmpq %rax, %r14
jge 0x3475
movq (%rbx), %rax
movq (%rax,%r14,8), %rsi
movq %r15, %rdi
callq 0x3144
incq %r14
jmp 0x3426
leaq 0x7ce6(%rip), %rdi      # 0xb136
pushq $0x2
popq %rsi
pushq $0x1
popq %rdx
movq %rax, %rcx
callq 0x1190
movl 0x10(%rbx), %edx
subl %ebp, %edx
jle 0x3475
movq (%r12), %rdi
leaq 0x75e2(%rip), %rsi      # 0xaa52
jmp 0x31df
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rax
cmpl $0x1, (%rax)
jne 0x34a7
leaq 0xdd8c(%rip), %rcx      # 0x11220
movq (%rcx), %rdi
movq 0x20(%rax), %rdx
leaq 0x75df(%rip), %rsi      # 0xaa81
jmp 0x3252
leaq 0x8324(%rip), %rsi      # 0xb7d2
movq %r14, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4a9e
nop