예제 #1
0
int asm_main(int argc, char **argv) {
  string archString("8w32/32/8"), outFileName("a.out.HOF"),
         inFileName(argv[argc-1]);
  bool showHelp;  

  /* Get command line arguments. */
  CommandLineArgFlag          fh("-h", "--help", "", showHelp);
  CommandLineArgSetter<string>fo("-o", "--output", "", outFileName);
  CommandLineArgSetter<string>fa("-a", "--arch", "", archString);

  CommandLineArg::readArgs(argc-1, argv);

  if (showHelp || argc == 0) {
    cout << Help::asmHelp;
    exit(0);
  }

  ArchDef arch(archString);

  D(0, "Created ArchDef for " << string(arch));

  /* Create an appropriate encoder. */
  Encoder *enc;
  switch (arch.getEncChar()) {
    case 'b': enc = new ByteEncoder(arch); break;
    case 'w': enc = new WordEncoder(arch); break;
    defaulet:
      cout << "Unknown encoding type, \"" << arch.getEncChar() << "\"\n";
      exit(1);
  }

  /* Open files. */
  if (outFileName == "") {
    cout << "HARP Assembler: No output filename given.\n";
    exit(1);
  }

  ifstream asmFile(inFileName.c_str());
  ofstream outFile(outFileName.c_str());

  if (!asmFile) {
    cout << "Could not open \"" << inFileName << "\" for reading.\n";
    exit(1);
  }

  if (!outFile) {
    cout << "Could not open \"" << outFileName << "\" for writing.\n";
    exit(1);
  }

  /* Read an Obj from the assembly file. */
  D(0, "Passing AsmReader ArchDef: " << string(arch));
  AsmReader ar(arch);
  Obj *o = ar.read(asmFile);

  /* Encode the text chunks read from the assembly file. */
  for (Size j = 0; j < o->chunks.size(); j++) {
    Chunk *&c = o->chunks[j];
    TextChunk *tc;
    DataChunk *dc;
    if ((tc = dynamic_cast<TextChunk*>(c)) != NULL) {
      /* Encode it. */
      dc = new DataChunk(tc->name);
      enc->encodeChunk(*dc, *tc);

      /* Delete the text chunk. */
      delete tc;

      /* Do the switch. */
      c = dc;
    }
  }
  asmFile.close();
  delete enc;

  /* Write a HOF binary. */
  D(0, "Creating a HOFWriter, passing it ArchDef: " << string(arch));
  HOFWriter hw(arch);
  hw.write(outFile, *o);
  outFile.close();

  delete o;

  return 0;
}