// File: /workspace/github_c_makefile_star2_O1/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 0xb3d3(%rip), %r15      # 0xf3a0
movq %r15, %rdi
callq 0x6a78
cmpl $0x36, (%r15)
jne 0x3fee
leaq 0xb3ee(%rip), %rdi      # 0xf3d0
cmpl $0x50, %ebx
jne 0x3ff3
callq 0x82f8
jmp 0x3ff8
xorl %r13d, %r13d
jmp 0x4007
callq 0x8339
movq %rax, %r13
leaq 0xb39e(%rip), %rdi      # 0xf3a0
callq 0x6a78
cmpl $0x37, (%r15)
jne 0x4038
testq %r13, %r13
je 0x4025
leaq 0x6a3b(%rip), %rdi      # 0xaa54
leaq 0xb3b0(%rip), %rsi      # 0xf3d0
callq 0x6797
leaq 0xb3a4(%rip), %rdi      # 0xf3d0
cmpl $0x50, %ebx
jne 0x4059
callq 0x7f51
jmp 0x405e
testq %r13, %r13
jne 0x4192
leaq 0x69f2(%rip), %rdi      # 0xaa3a
leaq 0xb381(%rip), %rsi      # 0xf3d0
callq 0x6797
jmp 0x4192
callq 0x7fc0
movq %rax, -0x38(%rbp)
movq %r15, %rdi
callq 0x6a78
leaq -0x30(%rbp), %r12
leaq -0x40(%rbp), %r13
leaq 0x69fb(%rip), %r14      # 0xaa74
movq %r12, %rdi
movl $0x8, %esi
movl $0x35, %edx
movl $0x38, %ecx
movq %r13, %r8
callq 0x42bb
cmpl $-0x1, %eax
jne 0x40a0
movq %r14, %rdi
callq 0x688f
cmpl $0x35, (%r15)
jne 0x40ae
movq %r15, %rdi
callq 0x6a78
cmpl $0x38, (%r15)
jne 0x4079
callq 0x6821
leaq 0xb550(%rip), %r14      # 0xf610
cmpq $0x0, (%r14)
movq -0x38(%rbp), %r13
jne 0x40da
movq 0x8(%r13), %rsi
leaq 0x69b7(%rip), %rdi      # 0xaa8c
callq 0x6797
movq (%r14), %rax
movq %rax, 0x40(%r13)
leaq 0xb530(%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 0x8e29
movl %eax, %r15d
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x30(%rbp)
testq %rax, %rax
je 0x418e
movq -0x30(%rbp), %r12
cmpl $0x50, %ebx
jne 0x4151
movl 0x10(%rax), %edi
movl %r15d, %esi
movl $0x1, %edx
callq 0x6176
movl %eax, %r14d
movl %eax, (%r12)
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8e29
movl %eax, %r15d
addl %r14d, %r15d
jmp 0x417d
movl $0x0, (%rax)
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8e29
cmpl %r15d, %eax
jle 0x417d
movl 0x10(%r12), %edi
movq 0x18(%r12), %rsi
callq 0x8e29
movl %eax, %r15d
movq 0x38(%r12), %r12
movq %r12, %rax
testq %r12, %r12
jne 0x411e
movq %r12, -0x30(%rbp)
movl %r15d, 0x28(%r13)
movq %r13, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq