Esempio n. 1
0
File: main.c Progetto: kidxin/Denovo
int main(int argc, char** argv) {
    if (argc <= 2) return 1;
    FILE *fin = fopen(argv[1], "r");
    if (!fin) {
    	perror(argv[1]);
    	return 1;
    }
    
    FILE *fout = fopen(argv[2], "w");
    if (!fout) {
    	perror(argv[2]);
    	return 1;
    }
    
    yyrestart(fin);
    yyparse();
    if (syntaxError) return 0;
   // printTree(root);
    checkSemantic(root);
    if (semanticError) return 0;
    generateIR(root);
	generateMIPS(icsHead, fout);
	fclose(fout);
    return 0;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	std::string outfilename;
	po::options_description desc("qvod_crack options");
	desc.add_options()
		("help,h", "display this help")
		(",c", "compile only")
		("ir", "generate llvm-IR and stop")
		("outfile,o", po::value<std::string>(&outfilename), "set outputname")
		;

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm);

	if (vm.count("help")) {
        std::cout << desc;
        return 0;
    }

    std::list<std::string> inputs = argv_getinputfiles(argc,argv);
	if(inputs.empty()){
		std::cerr <<  "no input file" << std::endl;
		return 1;
	}

	std::string input = inputs.begin()->c_str();
	
	// usage llvmqbc input.bas -o a.out
	// ./a.out
	std::cout << "openning: " << input << std::endl;
	yyin = std::fopen(input.c_str(),"r");
	
	if(!yyin)
	{
		printf("open %s failed\n",input.c_str());
		return 1;
	}

	qb::parser parser;

	parser.parse();
	
	std::cout << "parse done, no errors, generating llvm IR..." << std::endl;

	// Initialize targets first, so that --version shows registered targets.
	llvm::InitializeAllTargets();
	llvm::InitializeAllTargetMCs();
	llvm::InitializeAllAsmPrinters();
	llvm::InitializeAllAsmParsers();

	// 如果没有指定输出文件路径, 设置输出文件的路径为输入文件相同的路径下.
	if(outfilename.empty())
		outfilename = (fs::path(input).parent_path() / fs::basename(fs::path(input))).string();

	llvm::Module *module = new llvm::Module( fs::basename(fs::path(input)).c_str(), llvm::getGlobalContext());
	generateIR(program,module);

	// ir to generate llvm IR
	if(vm.count("ir")){
		module->dump();
		return 0;
	}
	
#if _WIN32
	std::string outobjname = outfilename + ".obj";
#else
	std::string outobjname = outfilename + ".o";
#endif
	std::string Err;

	boost::shared_ptr<llvm::tool_output_file> Out( new
		llvm::tool_output_file(outobjname.c_str(), Err, llvm::raw_fd_ostream::F_Binary) );

	if(generateobj(Out,module)==0)
		printf("======== object file writed to %s ===========\n", outobjname.c_str());

	if(!vm.count("-c"))
	{
		// compile to excuteable
#if _WIN32
		std::string cmd = boost::str(boost::format("link.exe /out:%s %s msvcrt.lib")
			% std::string(outfilename + ".exe") %  outobjname );
#else
		// invoke  gcc
		std::string libdir = fs::path(argv[0]).parent_path().string();
		std::string cmd = boost::str(boost::format("gcc -o %s %s -L%s -lbrt")
							% outfilename %  (outfilename + ".o") % libdir.c_str());
#endif
		printf("run linker: %s\n",cmd.c_str());
		if(std::system(cmd.c_str())==0)
			Out->keep(); // keep the file if linker runs fine

	} else {
		// no delete obj file
		Out->keep();
	}

	return 0;
}