// File: /workspace/github_c_makefile_star2_O2/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 $0x28, %rsp
movl %edi, %ebx
leaq 0xa678(%rip), %r14      # 0xe3a0
movq %r14, %rdi
callq 0x66f4
movl (%r14), %eax
cmpl $0x37, %eax
je 0x3d8d
cmpl $0x36, %eax
jne 0x3f04
leaq 0xa688(%rip), %rdi      # 0xe3d0
cmpl $0x50, %ebx
jne 0x3d54
callq 0x7cc3
jmp 0x3d59
callq 0x7cd9
movq %rax, %r14
leaq 0xa63d(%rip), %r15      # 0xe3a0
movq %r15, %rdi
callq 0x66f4
cmpl $0x37, (%r15)
jne 0x3eff
testq %r14, %r14
je 0x3d8d
leaq 0x5cd3(%rip), %rdi      # 0x9a54
leaq 0xa648(%rip), %rsi      # 0xe3d0
callq 0x641f
cmpl $0x50, %ebx
movl %ebx, -0x3c(%rbp)
leaq 0xa636(%rip), %rdi      # 0xe3d0
jne 0x3da3
callq 0x7aa4
jmp 0x3da8
callq 0x7ae4
movq %rax, -0x30(%rbp)
leaq 0xa5ed(%rip), %rdi      # 0xe3a0
callq 0x66f4
leaq -0x38(%rbp), %r12
pushq $0x8
popq %r13
pushq $0x35
popq %r14
pushq $0x38
popq %rbx
leaq -0x48(%rbp), %r15
movq %r12, %rdi
movl %r13d, %esi
movl %r14d, %edx
movl %ebx, %ecx
movq %r15, %r8
callq 0x4043
cmpl $-0x1, %eax
jne 0x3def
leaq 0x5c8a(%rip), %rdi      # 0x9a74
callq 0x6515
leaq 0xa5aa(%rip), %rax      # 0xe3a0
movl (%rax), %eax
cmpl $0x35, %eax
jne 0x3e2c
movl %ebx, %r15d
movl %r14d, %ebx
movl %r13d, %r14d
movq %r12, %r13
leaq 0xa590(%rip), %r12      # 0xe3a0
movq %r12, %rdi
callq 0x66f4
movl (%r12), %eax
movq %r13, %r12
movl %r14d, %r13d
movl %ebx, %r14d
movl %r15d, %ebx
leaq -0x48(%rbp), %r15
cmpl $0x38, %eax
jne 0x3dcb
callq 0x64a7
leaq 0xa7d3(%rip), %rbx      # 0xe610
movq (%rbx), %rax
testq %rax, %rax
movq -0x30(%rbp), %r14
jne 0x3e5c
movq 0x8(%r14), %rsi
leaq 0x5c38(%rip), %rdi      # 0x9a8c
callq 0x641f
movq (%rbx), %rax
movq %rax, 0x40(%r14)
leaq 0xa7b1(%rip), %rcx      # 0xe618
andq $0x0, (%rcx)
andq $0x0, (%rbx)
movq %rax, -0x38(%rbp)
andl $0x0, (%rax)
movl 0x10(%rax), %edi
movq 0x18(%rax), %rsi
callq 0x87a1
movl %eax, %r12d
movq -0x38(%rbp), %r14
pushq $0x1
popq %r15
movl -0x3c(%rbp), %r13d
movq 0x38(%r14), %r14
testq %r14, %r14
je 0x3ef1
cmpl $0x50, %r13d
je 0x3ec8
andl $0x0, (%r14)
movl 0x10(%r14), %edi
movq 0x18(%r14), %rsi
callq 0x87a1
cmpl %r12d, %eax
jle 0x3e91
movl 0x10(%r14), %edi
movq 0x18(%r14), %rsi
callq 0x87a1
movl %eax, %r12d
jmp 0x3e91
movl 0x10(%r14), %edi
movl %r12d, %esi
movl %r15d, %edx
callq 0x5db3
movl %eax, %ebx
movl %eax, (%r14)
movl 0x10(%r14), %edi
movq 0x18(%r14), %rsi
callq 0x87a1
movl %eax, %r12d
addl %ebx, %r12d
jmp 0x3e91
movq %r14, -0x38(%rbp)
movq -0x30(%rbp), %r14
movl %r12d, 0x28(%r14)
jmp 0x3f1a
testq %r14, %r14
jne 0x3f1a
leaq 0x5b2f(%rip), %rdi      # 0x9a3a
leaq 0xa4be(%rip), %rsi      # 0xe3d0
callq 0x641f
xorl %r14d, %r14d
movq %r14, %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq