// File: /workspace/github_c_makefile_star2_O3/taeguk[P]Compiler-Proj-Cminus/src/cgen.c
// Function: codeGen
void codeGen(TreeNode *syntaxTree, FILE *codeStream)
{
  int i;
  TreeNode *t;

  fprintf(codeStream, ".data\n");
  fprintf(codeStream, "newline: .asciiz \"\\n\"\n");
  fprintf(codeStream, "output_str: .asciiz \"Output : \"\n");
  fprintf(codeStream, "input_str: .asciiz \"Input : \"\n");


  fprintf(codeStream, "\n.text\n");
  for(t = syntaxTree;
      t != NULL;
      t = t->sibling)
    {
      if(t->nodeKind == VariableDeclarationK)
        {
          t->attr.varDecl._var->symbolInfo->attr.intInfo.memloc = globalMemAlloc(sizeof(int));
          t->attr.varDecl._var->symbolInfo->attr.intInfo.globalFlag = TRUE;
        }
      else if(t->nodeKind == ArrayDeclarationK)
        {
          t->attr.arrDecl._var->symbolInfo->attr.arrInfo.memloc =
            globalMemAlloc(sizeof(int) * t->attr.arrDecl._var->symbolInfo->attr.arrInfo.arrLen);
          t->attr.arrDecl._var->symbolInfo->attr.arrInfo.globalFlag = TRUE;
        }
      else if(t->nodeKind == FunctionDeclarationK)
        {
          // Function labeling
          fprintf(codeStream, "# Function declaration\n");
          fprintf(codeStream, "%s:\n", t->attr.funcDecl._var->attr.ID);

          // Function parameter's total memory
          TreeNode *param;
          int accLoc = 0;
          for(param = t->attr.funcDecl.params;
              param != NULL;
              param = param->sibling)
            {
              TreeNode *var = param->attr.funcDecl._var;
              if(var->symbolInfo->nodeType == IntT)
                accLoc += sizeof(int);
              else if(var->nodeType == IntArrayT)
                accLoc += regSize;
            }

          // real location
          // ex. f(a, b, c)
          // a: 8(fp), b: 4(fp), c:0(fp)
          // pushed early <--> pushed late
          for(param = t->attr.funcDecl.params;
              param != NULL;
              param = param->sibling)
            {
              switch(param->nodeType)
                {
                case VariableParameterK:
                  accLoc -= sizeof(int);
                  param->attr.varParam._var->symbolInfo->attr.intInfo.memloc = accLoc;
                  break;
                case ArrayParameterK:
                  accLoc -= regSize;
                  // TODO:
                  param->attr.arrParam._var->symbolInfo->attr.arrInfo.memloc = accLoc;
                  break;
                default:
                  DONT_OCCUR_PRINT;
                }
            }

          // allocate stack
          fprintf(codeStream, "\n# Allocate stack\n");
          fprintf(codeStream, "addiu $sp, $sp, %d\n", -10 * regSize);

          // save register $fp
          fprintf(codeStream, "# Save registers\n");
          fprintf(codeStream, "sw $fp, %d($sp)\n", 0 * regSize);
          // save registers $s0~$s7
          for(i=0; i<8; ++i)
            fprintf(codeStream, "sw $s%d, %d($sp)\n", i, (i+1) * regSize);
          // save register $ra
          fprintf(codeStream, "sw $ra, %d($sp)\n", 9 * regSize);

          // set frame
          fprintf(codeStream, "addiu $fp, $sp, %d\n", 10 * regSize);

          L_cleanup = labelAlloc();

          // cmpd statement generation
          fprintf(codeStream, "\n# Compound statement for function\n");
          int updateStack = localCodeGen(t->attr.funcDecl.cmpd_stmt, codeStream, 10 * regSize, 1);
          if(updateStack != 10 * regSize)
            DONT_OCCUR_PRINT;

          // cleanup for function with no return
          fprintf(codeStream, "\n# Stack cleanup\n");
          fprintf(codeStream, "L%d:\n", L_cleanup);

          // cleanup remained local stack.
          fprintf(codeStream, "addiu $sp, $fp, %d\n", -10 * regSize);

          // load registers
          fprintf(codeStream, "lw $fp, %d($sp)\n", 0 * regSize);
          // load registers $s0~$s7
          for(i=0; i<8; ++i)
            fprintf(codeStream, "lw $s%d, %d($sp)\n", i, (i+1) * regSize);
          // load register $ra
          fprintf(codeStream, "lw $ra, %d($sp)\n", 9 * regSize);
          fprintf(codeStream, "addiu $sp, $sp, %d\n", 10 * regSize);

          // return
          fprintf(codeStream, "\n# Return to caller\n");
          fprintf(codeStream, "jr $ra\n\n");
        }
      else
        DONT_OCCUR_PRINT;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x46b9(%rip), %rdi      # 0x6614
movl $0x6, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x46a7(%rip), %rdi      # 0x661b
movl $0x16, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x46a5(%rip), %rdi      # 0x6632
movl $0x20, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x46ad(%rip), %rdi      # 0x6653
movl $0x1e, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x46b3(%rip), %rdi      # 0x6672
movl $0x7, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
testq %r14, %r14
je 0x22cf
leaq 0x46c9(%rip), %r12      # 0x66aa
movl 0xc(%r14), %eax
cmpl $0x3, %eax
je 0x2045
cmpl $0x2, %eax
je 0x2014
cmpl $0x1, %eax
jne 0x22c3
movl 0x9102(%rip), %eax      # 0xb100
leal 0x4(%rax), %ecx
movl %ecx, 0x90f9(%rip)      # 0xb100
movq 0x28(%r14), %rcx
movq 0x18(%rcx), %rcx
movl %eax, 0xc(%rcx)
jmp 0x2031
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
movl 0x90db(%rip), %edx      # 0xb100
leal (%rdx,%rcx,4), %ecx
movl %ecx, 0x90d2(%rip)      # 0xb100
movl %edx, 0xc(%rax)
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl $0x1, 0x10(%rax)
jmp 0x22c3
movl $0x17, %esi
movl $0x1, %edx
leaq 0x4624(%rip), %rdi      # 0x667a
movq %rbx, %rcx
callq 0x11c0
movq 0x28(%r14), %rax
movq 0x20(%rax), %rdx
movq %rbx, %rdi
leaq 0x4622(%rip), %rsi      # 0x6692
xorl %eax, %eax
callq 0x1120
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x20ce
xorl %ecx, %ecx
movq %rax, %rdx
movq 0x28(%rdx), %rsi
movq 0x18(%rsi), %rdi
cmpl $0x4, (%rdi)
jne 0x2097
addl $0x4, %ecx
jmp 0x20a3
cmpl $0x5, 0x10(%rsi)
leal 0x4(%rcx), %esi
cmovnel %ecx, %esi
movl %esi, %ecx
movq (%rdx), %rdx
testq %rdx, %rdx
jne 0x2085
jmp 0x20c9
movl 0x10(%rax), %edx
andl $-0x2, %edx
cmpl $0x4, %edx
jne 0x20c6
addl $-0x4, %ecx
movq 0x28(%rax), %rdx
movq 0x18(%rdx), %rdx
movl %ecx, 0xc(%rdx)
movq (%rax), %rax
testq %rax, %rax
jne 0x20ad
movq %r14, -0x30(%rbp)
movl $0x12, %esi
movl $0x1, %edx
leaq 0x45b4(%rip), %rdi      # 0x6697
movq %rbx, %rcx
callq 0x11c0
xorl %r15d, %r15d
movq %rbx, %rdi
movq %r12, %rsi
movl $0xffffffd8, %edx       # imm = 0xFFFFFFD8
xorl %eax, %eax
callq 0x1120
movl $0x11, %esi
movl $0x1, %edx
leaq 0x45ad(%rip), %rdi      # 0x66be
movq %rbx, %rcx
callq 0x11c0
movq %rbx, %rdi
leaq 0x45ad(%rip), %rsi      # 0x66d0
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
movl $0x4, %r12d
leaq 0x45a8(%rip), %r14      # 0x66e1
leal 0x1(%r15), %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
addl $0x4, %r12d
movl %r13d, %r15d
cmpl $0x8, %r13d
jne 0x2139
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x4589(%rip), %rsi      # 0x66f3
movl $0x24, %edx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x4584(%rip), %rsi      # 0x6704
movl $0x28, %edx
xorl %eax, %eax
callq 0x1120
movl 0x8f9a(%rip), %eax      # 0xb12c
leal 0x1(%rax), %ecx
movl %ecx, 0x8f91(%rip)      # 0xb12c
movl %eax, 0x8f87(%rip)      # 0xb128
movl $0x23, %esi
movl $0x1, %edx
leaq 0x4566(%rip), %rdi      # 0x6718
movq %rbx, %rcx
callq 0x11c0
movq -0x30(%rbp), %rax
movq 0x38(%rax), %rdi
movq %rbx, %rsi
movl $0x28, %edx
movl $0x1, %ecx
callq 0x22de
movl $0x11, %esi
movl $0x1, %edx
leaq 0x4557(%rip), %rdi      # 0x673c
movq %rbx, %rcx
callq 0x11c0
movl 0x8f35(%rip), %edx      # 0xb128
movq %rbx, %rdi
leaq 0x4551(%rip), %rsi      # 0x674e
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x4546(%rip), %rsi      # 0x6754
movl $0xffffffd8, %edx       # imm = 0xFFFFFFD8
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x4544(%rip), %rsi      # 0x6768
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
movl $0x4, %r12d
leaq 0x453f(%rip), %r14      # 0x6779
leal 0x1(%r15), %r13d
movq %rbx, %rdi
movq %r14, %rsi
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
addl $0x4, %r12d
movl %r13d, %r15d
cmpl $0x8, %r13d
jne 0x223a
movq %rbx, %rdi
leaq 0x4523(%rip), %rsi      # 0x678b
movl $0x24, %edx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x442c(%rip), %r12      # 0x66aa
movq %r12, %rsi
movl $0x28, %edx
xorl %eax, %eax
callq 0x1120
movl $0x14, %esi
movl $0x1, %edx
leaq 0x44fe(%rip), %rdi      # 0x679c
movq %rbx, %rcx
callq 0x11c0
movl $0x8, %esi
movl $0x1, %edx
leaq 0x44fa(%rip), %rdi      # 0x67b1
movq %rbx, %rcx
callq 0x11c0
movq -0x30(%rbp), %r14
movq (%r14), %r14
testq %r14, %r14
jne 0x1fe1
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq