[Assorted-commits] SF.net SVN: assorted:[1311] clamp/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-03-19 22:20:20
|
Revision: 1311 http://assorted.svn.sourceforge.net/assorted/?rev=1311&view=rev Author: yangzhang Date: 2009-03-19 22:19:57 +0000 (Thu, 19 Mar 2009) Log Message: ----------- - moved a bunch of the code gen helper functions into the CodeGen class as methods (so that they may know the prefix) - added the ability to specify a prefix for all the generated class names and file names - updated Makefile to remove all garbage and to use the prefix to avoid inter-test clobbering - added boost program_options for command-line argument parsing Modified Paths: -------------- clamp/trunk/src/CodeGen.cc clamp/trunk/src/CodeGen.hh clamp/trunk/src/Makefile clamp/trunk/src/clamp.y Modified: clamp/trunk/src/CodeGen.cc =================================================================== --- clamp/trunk/src/CodeGen.cc 2009-03-19 18:02:16 UTC (rev 1310) +++ clamp/trunk/src/CodeGen.cc 2009-03-19 22:19:57 UTC (rev 1311) @@ -113,12 +113,14 @@ // Constructors //////////////////////////////////////////////////////////////////////// - CodeGen::CodeGen () + CodeGen::CodeGen (const string &prefix) : mStack () , mLambdaCount (0) - , mImplStream ("lambda_impl.clamp_h") + , mImplStream ((prefix + "_lambda_impl.clamp_h").c_str()) + , mGenrStream ((prefix + "_lambda_genr.clamp_h").c_str()) , mLambdaMap () , mGeneratorStream () + , mPrefix (prefix) { } @@ -138,16 +140,17 @@ { } - void implTemplateName (ostream &strm, int ccount, int pcount) + void CodeGen::implTemplateName (ostream &strm, int ccount, int pcount) { strm - << "lambda_template_" + << mPrefix + << "_lambda_template_" << ccount << "_" << pcount; } - void tList (ostream &strm, int start, int end, bool first = true) + void CodeGen::tList (ostream &strm, int start, int end, bool first) { for (int count = start; count <= end; ++count) { @@ -162,7 +165,7 @@ } } - void implFunctionVar (ostream &strm, int ccount, int pcount, const char *n) + void CodeGen::implFunctionVar (ostream &strm, int ccount, int pcount, const char *n) { strm << "T1 ("; @@ -179,7 +182,7 @@ strm << ")"; } - void implInstance (ostream &strm, lambdaT const &lam) + void CodeGen::implInstance (ostream &strm, lambdaT const &lam) { unsigned ccount = count (lam.context.get()); unsigned pcount = count (lam.params.get()); @@ -229,14 +232,15 @@ int CodeGen::LambdaContext::sContextCount = 0; - stringT bodyFileName (int count) + stringT CodeGen::bodyFileName (int count) { char name[PATH_MAX]; - sprintf (name, "lambda_body_%d.clamp_h", count); + snprintf (name, PATH_MAX, "%s_lambda_body_%d.clamp_h", + mPrefix.c_str(), count); return stringT (name); } - void explicitConstructor (ostream &strm, int id, const lambdaT &lam) + void CodeGen::explicitConstructor (ostream &strm, int id, const lambdaT &lam) { if (lam.onHeap) { @@ -279,7 +283,7 @@ strm << ">"; } - void startExplicitImpl (ostream &strm, unsigned ccount, unsigned pcount) + void CodeGen::startExplicitImpl (ostream &strm, unsigned ccount, unsigned pcount) { implTemplateDecl (strm, ccount + pcount + 1); @@ -391,7 +395,7 @@ strm << ");\n }\n"; } - void declareMemberFn (ostream &strm + void CodeGen::declareMemberFn (ostream &strm , int id , unsigned ccount , unsigned pcount) @@ -410,7 +414,7 @@ strm << ");\n"; } - void defineMemberFn (ostream &strm + void CodeGen::defineMemberFn (ostream &strm , int id , unsigned ccount , unsigned pcount @@ -508,10 +512,11 @@ strm << "\n}\n"; } - void implicitConstructor (ostream &strm, int id, const lambdaT &lam) + void CodeGen::implicitConstructor (ostream &strm, int id, const lambdaT &lam) { strm - << "lambda_generator_" + << mPrefix + << "_lambda_generator_" << id << "<" << *lam.ret_type; @@ -525,7 +530,7 @@ strm <<")"; } - void implicitTemplateInstance (ostream &strm + void CodeGen::implicitTemplateInstance (ostream &strm , const lambdaT &lam , unsigned ccount , unsigned pcount) @@ -556,7 +561,7 @@ strm << ">"; } - void writeGenerator (ostream &strm, int id, const lambdaT &lam) + void CodeGen::writeGenerator (ostream &strm, int id, const lambdaT &lam) { unsigned ccount = count (lam.context.get()); unsigned pcount = count (lam.params.get()); @@ -567,7 +572,7 @@ typenameList (strm, 2 + ccount, 1 + ccount + pcount, false); strm - << ">\nstruct lambda_generator_" << id + << ">\nstruct " << mPrefix << "_lambda_generator_" << id << "\n{"; if (ccount >= 1) @@ -851,7 +856,7 @@ ; iter != list.end() ; ++iter) { - defineMemberFn (mImplStream + defineMemberFn (mGenrStream , iter->mLambdaNumber , group->first.first , group->first.second Modified: clamp/trunk/src/CodeGen.hh =================================================================== --- clamp/trunk/src/CodeGen.hh 2009-03-19 18:02:16 UTC (rev 1310) +++ clamp/trunk/src/CodeGen.hh 2009-03-19 22:19:57 UTC (rev 1311) @@ -32,13 +32,16 @@ #include <map> #include <sstream> #include "boost/smart_ptr.hpp" +#include <string> namespace clamp { + using namespace std; + class CodeGen { public: - CodeGen (); + CodeGen (const std::string &prefix); public: void lambdaType (FILE *, std::auto_ptr<lambdaT>); @@ -56,6 +59,30 @@ void spitItOut (); private: + void implTemplateName (ostream &strm, int ccount, int pcount); + void tList (ostream &strm, int start, int end, bool first = true); + void implFunctionVar (ostream &strm, int ccount, int pcount, const char *n); + void implInstance (ostream &strm, lambdaT const &lam); + stringT bodyFileName (int count); + void explicitConstructor (ostream &strm, int id, const lambdaT &lam); + void startExplicitImpl (ostream &strm, unsigned ccount, unsigned pcount); + void defineMemberFn (ostream &strm + , int id + , unsigned ccount + , unsigned pcount + , const lambdaT &lam); + void declareMemberFn (ostream &strm + , int id + , unsigned ccount + , unsigned pcount); + void implicitConstructor (ostream &strm, int id, const lambdaT &lam); + void implicitTemplateInstance (ostream &strm + , const lambdaT &lam + , unsigned ccount + , unsigned pcount); + void writeGenerator (ostream &strm, int id, const lambdaT &lam); + + private: struct LambdaContext { FILE *mPrevFile; int mOpenBracePos; @@ -75,6 +102,7 @@ private: std::ofstream mImplStream; + std::ofstream mGenrStream; private: // @@ -104,7 +132,12 @@ LambdaMap::iterator lambdaGroup (int ccount, int pcount); // Get the lambda group for the given number of parameters (starting // a new group if necessary) + + private: + std::string mPrefix; + }; + } #endif // CodeGen_rmg_20020211_included Modified: clamp/trunk/src/Makefile =================================================================== --- clamp/trunk/src/Makefile 2009-03-19 18:02:16 UTC (rev 1310) +++ clamp/trunk/src/Makefile 2009-03-19 22:19:57 UTC (rev 1311) @@ -35,7 +35,8 @@ LFLAGS = YFLAGS = -v -d -LDLIBS = -lstdc++ +LDLIBS = -lboost_program_options-gcc43-mt +LD = $(CXX) clamp.tab.h: clamp.cc mv y.tab.h clamp.tab.h @@ -61,10 +62,10 @@ clamp: LDLIBS := $(LDLIBS) -lfl clamp: clamp.o clamplex.o clamp_support.o CodeGen.o -clean: - -rm -f *.o clamp *.exe clamp.cc y.output clamp.tab.h lex.yy.cc lambda*.clamp_h test.cc +clean: + -rm -f *.o clamp *.exe clamp.cc y.output clamp.tab.h lex.yy.cc *_lambda_*.clamp_h test.cc curry test eg curry.cc test.cc eg.cc .PRECIOUS: %.cc %.cc: %.clamp clamp - ./clamp <$< >$@ + ./clamp --prefix $(basename $<) <$< >$@ Modified: clamp/trunk/src/clamp.y =================================================================== --- clamp/trunk/src/clamp.y 2009-03-19 18:02:16 UTC (rev 1310) +++ clamp/trunk/src/clamp.y 2009-03-19 22:19:57 UTC (rev 1311) @@ -27,12 +27,15 @@ #include "clamplex.h" #include "CodeGen.hh" +#include <string> #include <iostream> -#include <stdio.h> +#include <cstdio> #include <malloc.h> #include <set> +#include <boost/program_options.hpp> using namespace clamp; + using namespace std; CodeGen *gGenPtr = 0; @@ -79,7 +82,7 @@ ; lambda: lambda_type { - gGenPtr->lambdaType (yyout, std::auto_ptr<lambdaT>($1)); + gGenPtr->lambdaType (yyout, auto_ptr<lambdaT>($1)); // We read a lookahead token in lambda mode, and it wasn't // a left brace - put the char back and let the lexer reprocess @@ -99,7 +102,7 @@ lambda_def_start: lambda_type '{' { resume_initial = 1; - if (!gGenPtr->startLambda (yyout, openbraces, std::auto_ptr<lambdaT>($1))) + if (!gGenPtr->startLambda (yyout, openbraces, auto_ptr<lambdaT>($1))) { YYABORT; } @@ -152,7 +155,7 @@ | cvqualopt genericid cvqualopt { $$ = appendstr ($1, $2, $3); } | typename genericid { $$ = appendstr ($1, $2); } | lambda_type { - $$ = gGenPtr->lambdaTypeName (std::auto_ptr<lambdaT>($1)).release(); + $$ = gGenPtr->lambdaTypeName (auto_ptr<lambdaT>($1)).release(); } ; @@ -192,7 +195,7 @@ ; context: ctxorref '(' genericexpr ')' { - if (!gGenPtr->contextRef (yyout, std::auto_ptr<stringT> ($3), $1)) + if (!gGenPtr->contextRef (yyout, auto_ptr<stringT> ($3), $1)) { YYABORT; } @@ -231,13 +234,13 @@ BlockInfo *prev; BlockInfo *next; void *caller; - std::size_t size; + size_t size; }; struct Stats { - std::size_t blocks; - std::size_t bytes; + size_t blocks; + size_t bytes; }; Stats gAlloc = {0}; @@ -247,7 +250,7 @@ } #ifdef TRACK_MEMORY -void *operator new (std::size_t size) throw (std::bad_alloc) +void *operator new (size_t size) throw (bad_alloc) { ++gAlloc.blocks; gAlloc.bytes += size; @@ -299,7 +302,7 @@ } } -void *operator new[] (std::size_t size) throw (std::bad_alloc) +void *operator new[] (size_t size) throw (bad_alloc) { return operator new (size); } @@ -348,6 +351,39 @@ { int result; + string prefix; + + namespace po = boost::program_options; + po::options_description desc("Allowed options"); + desc.add_options() + ("help,h", "show this help message") + ("debug,d", "enable yydebug") +#if 0 + ("exit-on-recovery,x", po::bool_switch(&stop_on_recovery), + "exit after the joiner fully recovers (for leader)") + ("batch-size,b", po::value<int>(&batch_size)->default_value(100), + "number of txns to batch up in each msg (for leader)") +#endif + ("prefix,p", po::value<string>(&prefix)->default_value(""), + "hostname or address of the leader"); + + po::variables_map vm; + try { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + + yydebug = vm.count("debug"); + + if (vm.count("help")) { + cout << desc << endl; + return 0; + } + } catch (std::exception &ex) { + cerr << ex.what() << "\n\n" << desc << endl; + return 1; + } + +#if 0 if (argc > 1) { yyin = fopen (argv[1], "r"); @@ -362,15 +398,16 @@ ++argv; --argc; } +#endif - yydebug = argc - 1; + cout << "#include \"" << prefix << "_lambda_impl.clamp_h\"\n"; - printf ("#include \"lambda_impl.clamp_h\"\n"); + gGenPtr = new CodeGen(prefix); - gGenPtr = new CodeGen; - result = yyparse(); + cout << "#include \"" << prefix << "_lambda_genr.clamp_h\"\n"; + if (result == 0) { gGenPtr->spitItOut (); @@ -385,8 +422,6 @@ if (gAlloc.blocks != gDealloc.blocks) { - using namespace std; - fprintf (stderr , "Memory leak detected. Dumping still allocated blocks:\n"); @@ -396,7 +431,7 @@ while (scan) { - std::cerr + cerr << "Address " << static_cast<void *>(scan) << " size " << scan->size << " caller " << scan->caller This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |