package edu.cmu.cs.l3.codegen;

import edu.cmu.cs.l3.assem.*;
import edu.cmu.cs.l3.tree.*;
import edu.cmu.cs.l3.translate.*;
import java.util.*;
import edu.cmu.cs.l3.semant.*;
import edu.cmu.cs.l3.x86.*;
import edu.cmu.cs.l3.regalloc.*;
import edu.cmu.cs.l3.temp.*;

/** A class for testing out the code generator, for debugging purposes. */

public class CodegenMain {

  public static void go(String filename, RegisterAllocatorOptions regoptions)  {

      StringBuffer code = new StringBuffer();
      String codeline = "";
      int i,j;

      /* Perform the (mostly) machine-independent part of compilation */
      Translator tr = new Translator(new FrameIntel());
      List irfrags = tr.translate(filename, true);

      CodeGenerator cg = new X86CodeGenerator(regoptions.m_genspills);
      List cgfrags = cg.codegen(irfrags);

      // "Register Allocation"
      for (j = 0; j < cgfrags.size(); j++) {
          Fragment cfrag = (Fragment) cgfrags.get(j);
          List fragcode = cfrag.getAssemCode();
          for (i = 0; i < fragcode.size(); i++) {
              codeline = ((Instruction)fragcode.get(i)).format(new DefaultMap());
              code.append(codeline);
          }
      }

      System.out.println(code);
  }

}

