// File: /workspace/github_c_makefile_star2_O1/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 0x463d(%rip), %rdi      # 0x6614
movl $0x6, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x462b(%rip), %rdi      # 0x661b
movl $0x16, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x4629(%rip), %rdi      # 0x6632
movl $0x20, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x4631(%rip), %rdi      # 0x6653
movl $0x1e, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
leaq 0x4637(%rip), %rdi      # 0x6672
movl $0x7, %esi
movl $0x1, %edx
movq %rbx, %rcx
callq 0x11c0
testq %r14, %r14
je 0x2356
leaq 0x464d(%rip), %r12      # 0x66aa
movl 0xc(%r14), %eax
cmpl $0x3, %eax
je 0x20c1
cmpl $0x2, %eax
je 0x2090
cmpl $0x1, %eax
jne 0x234a
movl 0x9086(%rip), %eax      # 0xb100
leal 0x4(%rax), %ecx
movl %ecx, 0x907d(%rip)      # 0xb100
movq 0x28(%r14), %rcx
movq 0x18(%rcx), %rcx
movl %eax, 0xc(%rcx)
jmp 0x20ad
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
movl 0x905f(%rip), %edx      # 0xb100
leal (%rdx,%rcx,4), %ecx
movl %ecx, 0x9056(%rip)      # 0xb100
movl %edx, 0xc(%rax)
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl $0x1, 0x10(%rax)
jmp 0x234a
movl $0x17, %esi
movl $0x1, %edx
leaq 0x45a8(%rip), %rdi      # 0x667a
movq %rbx, %rcx
callq 0x11c0
movq 0x28(%r14), %rax
movq 0x20(%rax), %rdx
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x45a3(%rip), %rsi      # 0x6692
xorl %eax, %eax
callq 0x1120
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x212c
xorl %r15d, %r15d
movq 0x28(%rax), %rcx
movq 0x18(%rcx), %rdx
cmpl $0x4, (%rdx)
jne 0x2115
addl $0x4, %r15d
jmp 0x2124
cmpl $0x5, 0x10(%rcx)
leal 0x4(%r15), %ecx
cmovnel %r15d, %ecx
movl %ecx, %r15d
movq (%rax), %rax
testq %rax, %rax
jne 0x2102
movq 0x30(%r14), %rax
testq %rax, %rax
je 0x2155
movl 0x10(%rax), %ecx
andl $-0x2, %ecx
cmpl $0x4, %ecx
jne 0x2150
addl $-0x4, %r15d
movq 0x28(%rax), %rcx
movq 0x18(%rcx), %rcx
movl %r15d, 0xc(%rcx)
movq (%rax), %rax
jmp 0x2130
movq %r14, -0x30(%rbp)
movl $0x12, %esi
movl $0x1, %edx
leaq 0x452d(%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 0x4526(%rip), %rdi      # 0x66be
movq %rbx, %rcx
callq 0x11c0
movq %rbx, %rdi
leaq 0x4526(%rip), %rsi      # 0x66d0
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
movl $0x4, %r12d
leaq 0x4521(%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 0x21c0
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x4502(%rip), %rsi      # 0x66f3
movl $0x24, %edx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x44fd(%rip), %rsi      # 0x6704
movl $0x28, %edx
xorl %eax, %eax
callq 0x1120
movl 0x8f13(%rip), %eax      # 0xb12c
leal 0x1(%rax), %ecx
movl %ecx, 0x8f0a(%rip)      # 0xb12c
movl %eax, 0x8f00(%rip)      # 0xb128
movl $0x23, %esi
movl $0x1, %edx
leaq 0x44df(%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 0x2365
movl $0x11, %esi
movl $0x1, %edx
leaq 0x44d0(%rip), %rdi      # 0x673c
movq %rbx, %rcx
callq 0x11c0
movl 0x8eae(%rip), %edx      # 0xb128
movq %rbx, %rdi
leaq 0x44ca(%rip), %rsi      # 0x674e
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x44bf(%rip), %rsi      # 0x6754
movl $0xffffffd8, %edx       # imm = 0xFFFFFFD8
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x44bd(%rip), %rsi      # 0x6768
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
movl $0x4, %r12d
leaq 0x44b8(%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 0x22c1
movq %rbx, %rdi
leaq 0x449c(%rip), %rsi      # 0x678b
movl $0x24, %edx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x43a5(%rip), %r12      # 0x66aa
movq %r12, %rsi
movl $0x28, %edx
xorl %eax, %eax
callq 0x1120
movl $0x14, %esi
movl $0x1, %edx
leaq 0x4477(%rip), %rdi      # 0x679c
movq %rbx, %rcx
callq 0x11c0
movl $0x8, %esi
movl $0x1, %edx
leaq 0x4473(%rip), %rdi      # 0x67b1
movq %rbx, %rcx
callq 0x11c0
movq -0x30(%rbp), %r14
movq (%r14), %r14
testq %r14, %r14
jne 0x205d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq