Exemplo n.º 1
0
  void stackOutput() {
    vector<const void *> stack;
    stackDump(&stack);
    
    vector<pair<string, string> > tokens;
    if(FLAGS_addr2line) {
      string line = "addr2line -f -e " + exeName() + " ";
      for(int i = 0; i < stack.size(); i++)
        line += Format("%08x ", (unsigned int)stack[i]);
      line += "> addr2linetmp.txt";
      int rv = system(line.c_str());
      if(!rv) {
        {
          ifstream ifs("addr2linetmp.txt");
          string lin;
          while(getline(ifs, lin)) {
            string tlin;
            getline(ifs, tlin);
            tokens.push_back(make_pair(lin, tlin));
          }
        }
        unlink("addr2linetmp.txt");
      } else {
        dprintf("Couldn't call addr2line\n");
        return;
      }
    
      {
        string line = "c++filt -n -s gnu-v3 ";
        for(int i = 0; i < tokens.size(); i++)
          line += tokens[i].first + " ";
        line += "> cppfilttmp.txt";
        int rv = system(line.c_str());
        if(!rv) {
          {
            ifstream ifs("cppfilttmp.txt");
            string lin;
            int ct = 0;
            while(getline(ifs, lin)) {
              if(lin.size() && lin[0] == '_')
                lin.erase(lin.begin());
              dprintf("  %s - %s", tokens[ct].second.c_str(), lin.c_str());
              ct++;
            }
          }
          unlink("cppfilttmp.txt");
        } else {
          dprintf("Couldn't call c++filt\n");
          return;
        }
      }
    } else {
      for(int i = 0; i < stack.size(); i++)
        dprintf("  %08x", (unsigned int)stack[i]);
    }

    dprintf("\n");
  }
int main(int argc, const char* argv[])
{
  std::string exeName("TEBNFCodeGenerator");
  if(argc < 4)
  {
    if(1 == argc)
    {
      std::string strPath(argv[0]);
      auto pos = strPath.find_last_of("/\\");
      exeName = std::string::npos != pos ? strPath.substr(pos + 1) : strPath;
    }
    std::cout << "Usage: " << exeName << " <source> <destination> <name>\n"
      << "Arguments:\n"
      << "  source - Path of file containing TEBNF grammar, including file name.\n"
      << "  destination - Path of the location on disk to write generated files.\n"
      << "  name - Name to give to the generated application.\n";
  }
  else
  {
    try
    {
      std::string srcFilePath(argv[1]);
      std::string destDirPath(argv[2]);
      std::string appName(argv[3]);
      Utils::Logger::setup(srcFilePath); //Setup static logger.
      Utils::Logger::log("\n--------- TEBNF Code Generator v" + Utils::getTEBNFVersion() + " ---------\n");
      Reader::read(srcFilePath);
      Scanner::scan(Reader::getFileText());
      Parser::parse();
      Generator::generate(destDirPath, appName);
      return EXIT_SUCCESS;
    }
    catch(std::exception& ex)
    {
      std::cout << std::string(ex.what()) << std::endl;
    }
  }
  return EXIT_FAILURE;
}
Exemplo n.º 3
0
int compile(int debug, int printSource, int printAssembly, int optimisation, char *fname)
{
    wchar_t *InputBuf;
    struct TokenStruct *Token;
    struct ContextStruct Context;
    int Result;

    /* Load the inputfile into memory. */
    InputBuf = LoadInputFile(fname);
    if (InputBuf == NULL) exit(1);
    
    if(printSource)
        printf("Input MAlice program:\n-- %s --\n%ls---\n\n",fname,InputBuf);

    /* Run the Parser. */
    Result = Parse(InputBuf,wcslen(InputBuf),TRIMREDUCTIONS,DEBUG,&Token);
    /* Interpret the results. */
    if (Result != PARSEACCEPT) {
        ShowErrorMessage(Token,Result);
        
        printf("\n[EXIT] Syntax Error\n");
        return 255;
    } else {
        /* Initialize the Context. */
        Context.Debug = DEBUG;
        Context.Indent = 0;
        Context.ReturnValue = NULL;

        /* Start execution by calling the subroutine of the first Token on
           the TokenStack. It's the "Start Symbol" that is defined in the
           grammar. */
        RuleJumpTable[Token->ReductionRule](Token,&Context);
    }
    
    //printTree(Token,0);
    
    int error = debug; // Used as debug flag and error reporting during CFG gen
    CFG * cfg = makeCFG(Token, optimisation, &error);
    if(error || cfg == 0)
    {
        printf("\n[EXIT] Compilation Error\n");
        return 255;
    }
    char* asmfname;
    asmfname = asmName(fname);
    
    genCode(cfg, asmfname);
    if(printAssembly)
    {
        printf("Generated assembly code:\n-- %s --\n",asmfname);
        printFile(asmfname);
        printf("---\n\n");
    }
    free(asmfname);

    char* exefname;
    exefname = exeName(fname);
    char fc[128];
    char sc[128];

    sprintf(fc ,"nasm -f elf32 %s.s -o %s.o", exefname, exefname);
    sprintf(sc ,"gcc -m32 %s.o -o %s", exefname, exefname);

    char * firstCommand = (char *)malloc(sizeof(char) * strlen(fc));
    char * secondCommand = (char *)malloc(sizeof(char) * strlen(sc));
    strcpy(firstCommand, fc);
    strcpy(secondCommand, sc);

    system(firstCommand);
    system(secondCommand);
    
    /* Cleanup. */
    DeleteTokens(Token);
    free(InputBuf);
    return 0;
}