// File: /workspace/github_c_makefile_star2_O3/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 0x3bb0
movq %rsi, %r14
movl (%rbx), %eax
decl %eax
cmpl $0xb, %eax
ja 0x3eec
leaq 0x8597(%rip), %rcx      # 0xc11c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xf68b(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3abb
movq 0x20(%rax), %rdx
leaq 0x8ebe(%rip), %rsi      # 0xca69
jmp 0x3c66
movq %rbx, %rdi
callq 0xad7b
testb %al, %al
je 0x3bd8
cmpl $0x0, 0x10(%rbx)
jne 0x3bd8
movq 0x50, %rdi
leaq 0x8e61(%rip), %rsi      # 0xca32
xorl %eax, %eax
callq 0x52cd
leaq 0xf641(%rip), %rax      # 0x13220
movq (%rax), %r14
movq %rbx, %rdi
callq 0xae77
leaq 0x8e61(%rip), %rsi      # 0xca52
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 0xf60e(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3abb
movq 0x20(%rax), %rdx
leaq 0x8e4d(%rip), %rsi      # 0xca75
jmp 0x3c66
leaq 0xf5ef(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3abb
movq 0x20(%rax), %rdx
leaq 0x8e3a(%rip), %rsi      # 0xca81
jmp 0x3c66
leaq 0xf5d0(%rip), %rax      # 0x13220
movq (%rax), %rbx
movq %r14, %rdi
callq 0x3abb
movq 0x20(%rax), %rdx
leaq 0x8df7(%rip), %rsi      # 0xca5d
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 0x3df3
leaq 0xf582(%rip), %rax      # 0x13220
movq (%rax), %rdi
movl 0x28(%r14), %edx
leaq 0x8df1(%rip), %rsi      # 0xca9d
jmp 0x3bf6
cmpl $0x3, (%r14)
je 0x3cc8
leaq 0x9b2c(%rip), %rsi      # 0xd7ea
movq %r14, %rdi
xorl %eax, %eax
callq 0x5502
movq 0x48(%r14), %r14
movq 0x20(%rbx), %r12
movq %r14, %rdi
callq 0x3f3a
testl %eax, %eax
jle 0x3e93
xorl %r13d, %r13d
xorl %r15d, %r15d
movl 0x18(%r12), %edx
subl %r15d, %edx
jle 0x3d0d
leaq 0xf529(%rip), %rax      # 0x13220
movq (%rax), %rdi
leaq 0x8d51(%rip), %rsi      # 0xca52
xorl %eax, %eax
callq 0x10f0
movl 0x18(%r12), %r15d
movq 0x10(%r12), %rdi
movq (%r14), %rax
movq (%rax,%r13,8), %rsi
callq 0x3b5b
movq 0x10(%r12), %rdi
callq 0xae77
movl %eax, %ebp
addl %r15d, %ebp
incq %r13
movq (%r12), %r12
movq %r14, %rdi
callq 0x3f3a
cltq
movl %ebp, %r15d
cmpq %rax, %r13
jl 0x3ce6
jmp 0x3e95
movq 0x8(%rbx), %r15
movl (%r15), %ecx
xorl $0x1, %ecx
movl (%r14), %eax
movl %eax, %edx
xorl $0x2, %edx
orl %ecx, %edx
jne 0x3e43
leaq 0xf4b4(%rip), %r15      # 0x13220
movq (%r15), %rcx
leaq 0x8d17(%rip), %rdi      # 0xca8d
movl $0xa, %esi
movl $0x1, %edx
callq 0x1190
movq 0x50(%r14), %r12
movl $0x1, %ebp
testq %r12, %r12
je 0x3dc0
leaq 0x8cfe(%rip), %r14      # 0xca98
movq (%r15), %rdi
movl 0x28(%r12), %edx
movq 0x20(%r12), %rcx
movq %r14, %rsi
xorl %eax, %eax
callq 0x10f0
addl 0x28(%r12), %ebp
movq 0x8(%r12), %r12
testq %r12, %r12
jne 0x3d9a
movq (%r15), %rcx
leaq 0x936c(%rip), %rdi      # 0xd136
movl $0x2, %esi
movl $0x1, %edx
callq 0x1190
movl 0x10(%rbx), %edx
subl %ebp, %edx
jle 0x3eba
movq (%r15), %rdi
leaq 0x8c64(%rip), %rsi      # 0xca52
jmp 0x3bf6
cmpl $0x1c, %eax
je 0x3ec9
cmpl $0x16, %eax
jne 0x3eec
movq 0x8(%r14), %rax
cmpl $0x19, (%rax)
jne 0x3eec
leaq 0xf407(%rip), %rcx      # 0x13220
movq (%rcx), %rdi
movq 0x50(%rax), %rax
movl 0x28(%rax), %edx
movq 0x20(%rax), %rcx
leaq 0x8c7f(%rip), %rsi      # 0xcaad
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
cmpl $0x3, %eax
je 0x3e5d
leaq 0x999b(%rip), %rsi      # 0xd7ea
movq %r14, %rdi
xorl %eax, %eax
callq 0x5502
movq 0x8(%rbx), %r15
movq 0x48(%r14), %rbx
movq %rbx, %rdi
callq 0x3f3a
testl %eax, %eax
jle 0x3eba
xorl %r14d, %r14d
movq (%rbx), %rax
movq (%rax,%r14,8), %rsi
movq %r15, %rdi
callq 0x3b5b
incq %r14
movq %rbx, %rdi
callq 0x3f3a
cltq
cmpq %rax, %r14
jl 0x3e70
jmp 0x3eba
xorl %ebp, %ebp
movq %rbx, %rdi
callq 0xae77
cmpl %ebp, %eax
jle 0x3eba
leaq 0xf378(%rip), %rax      # 0x13220
movq (%rax), %r14
movq %rbx, %rdi
callq 0xae77
subl %ebp, %eax
jmp 0x3bea
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%r14), %rax
cmpl $0x1, (%rax)
jne 0x3eec
leaq 0xf347(%rip), %rcx      # 0x13220
movq (%rcx), %rdi
movq 0x20(%rax), %rdx
leaq 0x8b9a(%rip), %rsi      # 0xca81
jmp 0x3c69
leaq 0x98f7(%rip), %rsi      # 0xd7ea
movq %r14, %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x5502
nop