// File: /workspace/github_c_makefile_star2_O3/ejunjsh[P]mycc/decl.c
// Function: composite_declaration
static struct symtable *composite_declaration(int type) {
  struct symtable *ctype = NULL;
  struct symtable *m;
  struct ASTnode *unused;
  int offset;
  int t;

  // 跳过struct/union关键字
  scan(&Token);

  // 看看是否接下来是个struct/union名
  if (Token.token == T_IDENT) {
    // 查找任何匹配的复合类型
    if (type == P_STRUCT)
      ctype = findstruct(Text);
    else
      ctype = findunion(Text);
    scan(&Token);
  }

  // 如果接下来不是是左括号，代表现在在用已存在的struct/union类型
  // 直接返回指向类型的指针
  if (Token.token != T_LBRACE) {
    if (ctype == NULL)
      fatals("unknown struct/union type", Text);
    return (ctype);
  }

  // 保证struct/union类型之前没有被定义
  if (ctype)
    fatals("previously defined struct/union", Text);

  // 创建这个复合类型，跳过左括号
  if (type == P_STRUCT)
    ctype = addstruct(Text);
  else
    ctype = addunion(Text);
  scan(&Token);

  // 扫描成员列表
  while (1) {
    // 获得下个成员放在变量m
    t = declaration_list(&m, C_MEMBER, T_SEMI, T_RBRACE, &unused);
    if (t == -1)
      fatal("Bad type in member list");
    if (Token.token == T_SEMI)
      scan(&Token);
    if (Token.token == T_RBRACE)
      break;
  }

  // 把成员列表附着在复合类型节点上
  rbrace();
  if (Membhead == NULL)
    fatals("No members in struct", ctype->name);
  ctype->member = Membhead;
  Membhead = Membtail = NULL;

  // 设置第一个成员的位置，然后找到下一个空闲字节的位移
  m = ctype->member;
  m->stu.st_posn = 0;
  offset = typesize(m->type, m->ctype);

  // 设置每个成员的位置
  // union很简单，都是0，对于struct，要对齐成员，并找出下个空闲字节的位移
  for (m = m->next; m != NULL; m = m->next) {
    // 为成员设置所在位置
    if (type == P_STRUCT){
      m->stu.st_posn = genalign(m->type, offset, 1);
      offset = m->stu.st_posn + typesize(m->type, m->ctype); // 获得当前成员下个空闲字节的位移
    }
    else{
      m->stu.st_posn = 0;
      offset = typesize(m->type, m->ctype) > offset ? typesize(m->type, m->ctype) : offset; // 对于union来说，取最大的那个成员作为大小
    }
  }

  // 设置复合类型的大小
  // TODO：check this later !done
  ctype->size = offset;
  return (ctype);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %edi, %ebx
leaq 0xb386(%rip), %r15      # 0xf3a0
movq %r15, %rdi
callq 0x6b20
movl (%r15), %eax
cmpl $0x37, %eax
je 0x4078
cmpl $0x36, %eax
jne 0x41d1
leaq 0xb396(%rip), %rdi      # 0xf3d0
cmpl $0x50, %ebx
jne 0x4046
callq 0x8364
jmp 0x404b
callq 0x83a5
movq %rax, %r13
movq %r15, %rdi
callq 0x6b20
cmpl $0x37, (%r15)
jne 0x41cc
testq %r13, %r13
je 0x4078
leaq 0x69e8(%rip), %rdi      # 0xaa54
leaq 0xb35d(%rip), %rsi      # 0xf3d0
callq 0x683f
leaq 0xb351(%rip), %rdi      # 0xf3d0
cmpl $0x50, %ebx
jne 0x408b
callq 0x7fd7
jmp 0x4090
callq 0x8046
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x6b20
leaq -0x30(%rbp), %r12
leaq -0x40(%rbp), %r13
leaq 0x69c9(%rip), %r14      # 0xaa74
movq %r12, %rdi
movl $0x8, %esi
movl $0x35, %edx
movl $0x38, %ecx
movq %r13, %r8
callq 0x4312
cmpl $-0x1, %eax
jne 0x40d2
movq %r14, %rdi
callq 0x6937
movl (%r15), %eax
cmpl $0x35, %eax
jne 0x40e5
movq %r15, %rdi
callq 0x6b20
movl (%r15), %eax
cmpl $0x38, %eax
jne 0x40ab
callq 0x68c9
leaq 0xb51a(%rip), %r14      # 0xf610
movq (%r14), %rax
testq %rax, %rax
movq -0x38(%rbp), %r13
jne 0x4115
movq 0x8(%r13), %rsi
leaq 0x697f(%rip), %rdi      # 0xaa8c
callq 0x683f
movq (%r14), %rax
movq %rax, 0x40(%r13)
leaq 0xb4f8(%rip), %rcx      # 0xf618
xorl %edx, %edx
movq %rdx, (%rcx)
movq %rdx, (%r14)
movq %rax, -0x30(%rbp)
movl $0x0, (%rax)
movl 0x10(%rax), %edi
movq 0x18(%rax), %rsi
callq 0x8ea1
movl %eax, %r15d
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x41c6
movq -0x30(%rbp), %r12
cmpl $0x50, %ebx
jne 0x4189
movl 0x10(%rax), %edi
movl %r15d, %esi
movl $0x1, %edx
callq 0x623b
movl %eax, %r14d
movl %eax, (%r12)
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8ea1
movl %eax, %r15d
addl %r14d, %r15d
jmp 0x41b5
movl $0x0, (%rax)
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8ea1
cmpl %r15d, %eax
jle 0x41b5
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8ea1
movl %eax, %r15d
movq 0x38(%r12), %r12
movq %r12, %rax
testq %r12, %r12
jne 0x4156
movq %r12, -0x30(%rbp)
movl %r15d, 0x28(%r13)
jmp 0x41e7
testq %r13, %r13
jne 0x41e7
leaq 0x6862(%rip), %rdi      # 0xaa3a
leaq 0xb1f1(%rip), %rsi      # 0xf3d0
callq 0x683f
xorl %r13d, %r13d
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq