// File: /workspace/github_c_makefile_star2_O2/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 0x475d(%rip), %rdi      # 0x6614
pushq $0x6
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
leaq 0x474b(%rip), %rdi      # 0x661b
pushq $0x16
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
leaq 0x474d(%rip), %rdi      # 0x6632
pushq $0x20
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
leaq 0x4759(%rip), %rdi      # 0x6653
pushq $0x1e
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
leaq 0x4763(%rip), %rdi      # 0x6672
pushq $0x7
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
testq %r14, %r14
je 0x21e1
movl 0xc(%r14), %eax
cmpl $0x3, %eax
je 0x1f8a
cmpl $0x2, %eax
je 0x1f59
cmpl $0x1, %eax
jne 0x21d9
movl 0x91bd(%rip), %eax      # 0xb100
leal 0x4(%rax), %ecx
movl %ecx, 0x91b4(%rip)      # 0xb100
movq 0x28(%r14), %rcx
movq 0x18(%rcx), %rcx
movl %eax, 0xc(%rcx)
jmp 0x1f76
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl 0x14(%rax), %ecx
movl 0x9196(%rip), %edx      # 0xb100
leal (%rdx,%rcx,4), %ecx
movl %ecx, 0x918d(%rip)      # 0xb100
movl %edx, 0xc(%rax)
movq 0x28(%r14), %rax
movq 0x18(%rax), %rax
movl $0x1, 0x10(%rax)
jmp 0x21d9
leaq 0x46e9(%rip), %rdi      # 0x667a
pushq $0x17
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
movq 0x28(%r14), %rax
movq 0x20(%rax), %rdx
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x46de(%rip), %rsi      # 0x6692
xorl %eax, %eax
callq 0x1120
leaq 0x30(%r14), %rax
movq %rax, %rcx
movq (%rcx), %rcx
testq %rcx, %rcx
je 0x2009
movq 0x28(%rcx), %rdx
movq 0x18(%rdx), %rsi
cmpl $0x4, (%rsi)
jne 0x1fdd
addl $0x4, %r15d
jmp 0x1fc2
cmpl $0x5, 0x10(%rdx)
leal 0x4(%r15), %edx
cmovnel %r15d, %edx
movl %edx, %r15d
jmp 0x1fc2
movl 0x10(%rax), %ecx
andl $-0x2, %ecx
cmpl $0x4, %ecx
jne 0x2009
addl $-0x4, %r15d
movq 0x28(%rax), %rcx
movq 0x18(%rcx), %rcx
movl %r15d, 0xc(%rcx)
movq (%rax), %rax
testq %rax, %rax
jne 0x1fee
leaq 0x467f(%rip), %rdi      # 0x6697
pushq $0x12
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x4677(%rip), %rsi      # 0x66aa
pushq $-0x28
popq %rdx
xorl %eax, %eax
callq 0x1120
leaq 0x467a(%rip), %rdi      # 0x66be
pushq $0x11
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
movq %rbx, %rdi
leaq 0x4674(%rip), %rsi      # 0x66d0
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
pushq $0x4
popq %r12
cmpl $0x24, %r12d
je 0x2093
leal 0x1(%r15), %r13d
movq %rbx, %rdi
leaq 0x4664(%rip), %rsi      # 0x66e1
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
addl $0x4, %r12d
movl %r13d, %r15d
jmp 0x2069
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x4653(%rip), %rsi      # 0x66f3
pushq $0x24
popq %rdx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x4650(%rip), %rsi      # 0x6704
pushq $0x28
popq %r13
movl %r13d, %edx
xorl %eax, %eax
callq 0x1120
movl 0x9064(%rip), %eax      # 0xb12c
leal 0x1(%rax), %ecx
movl %ecx, 0x905b(%rip)      # 0xb12c
movl %eax, 0x9051(%rip)      # 0xb128
leaq 0x463a(%rip), %rdi      # 0x6718
pushq $0x23
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
movq 0x38(%r14), %rdi
movq %rbx, %rsi
movl %r13d, %edx
pushq $0x1
popq %rcx
callq 0x21f0
leaq 0x4633(%rip), %rdi      # 0x673c
pushq $0x11
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
movl 0x900b(%rip), %edx      # 0xb128
movq %rbx, %rdi
leaq 0x4627(%rip), %rsi      # 0x674e
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x461c(%rip), %rsi      # 0x6754
pushq $-0x28
popq %rdx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x461c(%rip), %rsi      # 0x6768
xorl %edx, %edx
xorl %eax, %eax
callq 0x1120
pushq $0x4
popq %r12
cmpl $0x24, %r12d
je 0x2183
leal 0x1(%r15), %r13d
movq %rbx, %rdi
leaq 0x460c(%rip), %rsi      # 0x6779
movl %r15d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1120
addl $0x4, %r12d
movl %r13d, %r15d
jmp 0x2159
movq %rbx, %rdi
leaq 0x45fe(%rip), %rsi      # 0x678b
pushq $0x24
popq %rdx
xorl %eax, %eax
callq 0x1120
movq %rbx, %rdi
leaq 0x4509(%rip), %rsi      # 0x66aa
pushq $0x28
popq %rdx
xorl %eax, %eax
callq 0x1120
leaq 0x45ea(%rip), %rdi      # 0x679c
pushq $0x14
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
leaq 0x45e6(%rip), %rdi      # 0x67b1
pushq $0x8
popq %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x11c0
movq (%r14), %r14
jmp 0x1f1d
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq