[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[csmith-dev] Computed goto patch



Hi,
Below is the patch file for the GCC extension of Labels vs Values mentioned here
We have added the command line extension as ,
--computed-goto = to generate extension code
--no-computed-goto = to generate goto as being generated now

Please find the file attached below and suggest necessary changes if any.
Thanks,
Sameeran Joshi
diff --git a/goto_testing/src/AbsProgramGenerator.cpp b/goto_testing/src/AbsProgramGenerator.cpp
index 93014ac..89212c8 100644
--- a/goto_testing/src/AbsProgramGenerator.cpp
+++ b/goto_testing/src/AbsProgramGenerator.cpp
@@ -59,10 +59,11 @@ AbsProgramGenerator::CreateInstance(int argc, char *argv[], unsigned long seed)
 	}
 	else
 	{
+		//returned value is of class DefaultProgramGenerator,pointer now points to DefaultProgramGenerator class
 		AbsProgramGenerator::current_generator_ = new DefaultProgramGenerator(argc, argv, seed);
 	}
 
-	AbsProgramGenerator::current_generator_->initialize();
+	AbsProgramGenerator::current_generator_->initialize();//of class DefaultProgramGenerator
 
 	if (!(CGOptions::dump_default_probabilities().empty())) {
 		CGOptions::random_random(false);
diff --git a/goto_testing/src/Block.cpp b/goto_testing/src/Block.cpp
index c57473b..9aba52d 100644
--- a/goto_testing/src/Block.cpp
+++ b/goto_testing/src/Block.cpp
@@ -131,7 +131,7 @@ Block::make_random(CGContext &cg_context, bool looping)
 
 	Function *curr_func = cg_context.get_current_func();
 	assert(curr_func);
-
+	//max_block_size was set in CGOptions during CLI argument passing 
 	Block *b = new Block(cg_context.get_current_block(), CGOptions::max_block_size());
 	b->func = curr_func;
 	b->looping = looping;
@@ -203,36 +203,32 @@ Block::make_random(CGContext &cg_context, bool looping)
 	// if the last statement is not?
 	Error::set_error(SUCCESS);
 
-//*changehere*//
-	if(curr_func->blocks[0]->stm_id==b->stm_id){
-		std::vector<string> labels;
-		labels.clear();//vimp otherwise gives UB,gives unknown labels
-		int count=0;
-		count=curr_func->blocks[0]->find_contained_labels(labels);//*this 'b' is the one of the main block of function and not of inner blocks as we checked the condition above*//
-		string ss="";
-		for (std::vector<string>::iterator itr=labels.begin();itr!=labels.end();itr++) {
-			ss.clear();
-			ss += "&&";
-			ss += *itr;
-			curr_func->blocks[0]->addr_labels.push_back(ss);//only adds in the main array related to function.
-		}
-//__________________________________________________________
-//cout << "\n************************";
-//cout << "\nfunction " << curr_func->name;
-for (size_t i=0; i<fm->cfg_edges.size();i++) {
-	const CFGEdge* e = fm->cfg_edges[i];
-	if(e->src->eType == eGoto) {
-		const StatementGoto* sg = dynamic_cast<const StatementGoto* >(e->src);
-		assert(sg);
-		sg->change_label(curr_func->blocks[0]->addr_labels);
-	//	e->src = dynamic_cast<const Statement*>(sg);
-	//	cout << "\n cfgedges " << i << "   "<< sg->label;
-	}
-}
-//cout << "\n************************";
+	//*changehere*//
+	if(CGOptions::computed_goto()){
+		if(curr_func->blocks[0]->stm_id==b->stm_id){
+			std::vector<string> labels;
+			labels.clear();//vimp otherwise gives UB,gives unknown labels
+			int count=0;
+			count=curr_func->blocks[0]->find_contained_labels(labels);//*this 'b' is the one of the main block of function and not of inner blocks as we checked the condition above*//
+			string ss="";
+			for (std::vector<string>::iterator itr=labels.begin();itr!=labels.end();itr++) {
+				ss.clear();
+				ss += "&&";
+				ss += *itr;
+				curr_func->blocks[0]->addr_labels.push_back(ss);//only adds in the main array related to function.
+			}
+			//__________________________________________________________
+			for (size_t i=0; i<fm->cfg_edges.size();i++) {
+				const CFGEdge* e = fm->cfg_edges[i];
+				if(e->src->eType == eGoto) {
+					const StatementGoto* sg = dynamic_cast<const StatementGoto* >(e->src);
+					assert(sg);
+					sg->change_label(curr_func->blocks[0]->addr_labels);
+				}
+			}
 
+		}
 	}
-
 	return b;
 }
 
@@ -332,9 +328,10 @@ Block::Output(std::ostream &out, FactMgr* fm, int indent) const
 	ss << "block id: " << stm_id;
 	output_comment_line(out, ss.str());
 //*changehere*//
-	if(!this->addr_labels.empty())
-        	this->print_label_addr_array(out,indent);
-
+	if(CGOptions::computed_goto()){
+		if(!this->addr_labels.empty())
+        		this->print_label_addr_array(out,indent);
+	}
 	if (CGOptions::depth_protect()) {
 		out << "DEPTH++;" << endl;
 	}
diff --git a/goto_testing/src/CGOptions.cpp b/goto_testing/src/CGOptions.cpp
index 6c9d3dc..84b5d9a 100644
--- a/goto_testing/src/CGOptions.cpp
+++ b/goto_testing/src/CGOptions.cpp
@@ -199,6 +199,7 @@ DEFINE_GETTER_SETTER_BOOL(const_struct_union_fields);
 DEFINE_GETTER_SETTER_BOOL(lang_cpp);
 DEFINE_GETTER_SETTER_BOOL(cpp11);
 DEFINE_GETTER_SETTER_BOOL(fast_execution);
+DEFINE_GETTER_SETTER_BOOL(computed_goto);
 
 void
 CGOptions::set_default_builtin_kinds()
@@ -313,6 +314,7 @@ CGOptions::set_default_settings(void)
   fast_execution(false);
 
 	set_default_builtin_kinds();
+	computed_goto(false);
 }
 
 // Add options necessary for cpp 
diff --git a/goto_testing/src/CGOptions.h b/goto_testing/src/CGOptions.h
index ee7bb5e..003b2c5 100644
--- a/goto_testing/src/CGOptions.h
+++ b/goto_testing/src/CGOptions.h
@@ -363,6 +363,8 @@ public:
 	static bool signed_char_index(void);
 	static bool signed_char_index(bool p);
 
+	static bool computed_goto(void);
+	static bool computed_goto(bool p);
 	/////////////////////////////////////////////////////////
 	static void set_default_settings(void);
 
@@ -579,6 +581,7 @@ private:
 	static bool no_return_dead_ptr_;
 	static bool hash_value_printf_;
 	static bool signed_char_index_;
+	static bool computed_goto_;
 	static std::string	dump_default_probabilities_;
 	static std::string	dump_random_probabilities_;
 	static std::string	probability_configuration_;
@@ -596,6 +599,7 @@ private:
 	static int builtin_function_prob_;
 	static int null_pointer_dereference_prob_;
 	static int dead_pointer_dereference_prob_;
+
 	// flag that indicate the comformance level to C99. true means relaxed
 	static bool union_read_type_sensitive_;
 	static bool pre_incr_operator_;
@@ -631,3 +635,4 @@ private:
 // End:
 
 // End of file.
+
diff --git a/goto_testing/src/DefaultOutputMgr.cpp b/goto_testing/src/DefaultOutputMgr.cpp
index c0a364f..7f0cc04 100644
--- a/goto_testing/src/DefaultOutputMgr.cpp
+++ b/goto_testing/src/DefaultOutputMgr.cpp
@@ -196,7 +196,9 @@ DefaultOutputMgr::OutputHeader(int argc, char *argv[], unsigned long seed)
 {
 	OutputMgr::OutputHeader(argc, argv, seed);
 }
-
+/*
+*Function actually outputs the random generated program,except the comment part of header section
+*/
 void
 DefaultOutputMgr::Output()
 {
diff --git a/goto_testing/src/DefaultProgramGenerator.cpp b/goto_testing/src/DefaultProgramGenerator.cpp
index f355e9c..40aa9b8 100644
--- a/goto_testing/src/DefaultProgramGenerator.cpp
+++ b/goto_testing/src/DefaultProgramGenerator.cpp
@@ -73,7 +73,7 @@ DefaultProgramGenerator::initialize()
 	if (CGOptions::get_reducer()) {
 		output_mgr_ = new ReducerOutputMgr();
 	} else {
-		output_mgr_ = DefaultOutputMgr::CreateInstance();
+		output_mgr_ = DefaultOutputMgr::CreateInstance();//the pointer points to DefaultOutputMgr class.
 	}
 	assert(output_mgr_);
 
@@ -90,11 +90,10 @@ DefaultProgramGenerator::get_count_prefix(const std::string &)
 void
 DefaultProgramGenerator::goGenerator()
 {
-	output_mgr_->OutputHeader(argc_, argv_, seed_);
-
+	output_mgr_->OutputHeader(argc_, argv_, seed_);//output_mgr points to DefaultOutputMgr class,so function definition in that class and *NOT OF Output_mgr class*
 	GenerateAllTypes();
 	GenerateFunctions();
-	output_mgr_->Output();
+	output_mgr_->Output();//this actually outputs the program generated from above
 	if (CGOptions::identify_wrappers()) {
 		ofstream ofile;
 		ofile.open("wrapper.h");
diff --git a/goto_testing/src/Function.cpp b/goto_testing/src/Function.cpp
index f80a499..45a2c89 100644
--- a/goto_testing/src/Function.cpp
+++ b/goto_testing/src/Function.cpp
@@ -456,7 +456,7 @@ Function::make_first(void)
 	const Type *ty = RandomReturnType();
 	ERROR_GUARD(NULL);
 
-	Function *f = new Function(RandomFunctionName(), ty);
+	Function *f = new Function(RandomFunctionName(), ty);//pushes into the FuncList
 	// dummy variable representing return variable, we don't care about the type, so use 0
 	string rvname = f->name + "_" + "rv";
 	CVQualifiers ret_qfer = CVQualifiers::random_qualifiers(ty);
@@ -464,7 +464,7 @@ Function::make_first(void)
 	f->rv = Variable::CreateVariable(rvname, ty, NULL, &ret_qfer);
 
 	// create a fact manager for this function, with empty global facts
-	FactMgr* fm = new FactMgr(f);
+	FactMgr* fm = new FactMgr(f);//assignes 'f' to 'Function*' in FactMgr class
 	FMList.push_back(fm);
 
 	ExtensionMgr::GenerateFirstParameterList(*f);
diff --git a/goto_testing/src/RandomProgramGenerator.cpp b/goto_testing/src/RandomProgramGenerator.cpp
index a8eb615..994c741 100644
--- a/goto_testing/src/RandomProgramGenerator.cpp
+++ b/goto_testing/src/RandomProgramGenerator.cpp
@@ -175,6 +175,7 @@ static void print_help()
 	cout << "  --math64 | --no-math64: enable | disable 64-bit math ops (enabled by default)." << endl << endl;
 	cout << "  --inline-function | --no-inline-function: enable | disable inline attributes on generated functions." << endl << endl;
 	cout << "  --inline-function-prob <num>: set the probability of each function being marked as inline (default is 50)." << endl << endl;
+	cout << "  --computed-goto | --no-computed-goto: enable | disable computed goto extension (disable by default)." << endl << endl;
 
 	// numbered controls
 	cout << "  --max-array-dim <num>: limit array dimensions to <num>. (default 3)" << endl << endl;
@@ -354,7 +355,6 @@ main(int argc, char **argv)
 	g_Seed = platform_gen_seed();
 
 	CGOptions::set_default_settings();
-
 	for (int i=1; i<argc; i++) {
 
 		if (strcmp (argv[i], "--help") == 0 ||
@@ -850,6 +850,16 @@ main(int argc, char **argv)
 			continue;
 		}
 
+		if (strcmp (argv[i], "--computed-goto") == 0) {
+			CGOptions::computed_goto(true);
+			continue;
+		}
+
+		if (strcmp (argv[i], "--no-computed-goto") == 0) {
+			CGOptions::computed_goto(false);
+			continue;
+		}
+
 		if (strcmp (argv[i], "--no-jumps") == 0) {
 			CGOptions::jumps(false);
 			continue;
@@ -997,7 +1007,7 @@ main(int argc, char **argv)
 
 		if (strcmp (argv[i], "--no-muls") == 0) {
 			CGOptions::muls(false);
-			continue;
+				continue;
 		}
 
 		if (strcmp (argv[i], "--checksum") == 0) {
@@ -1459,12 +1469,13 @@ main(int argc, char **argv)
 		cout << "error: options conflict - " << CGOptions::conflict_msg() << std::endl;
 		exit(-1);
 	}
-
+//generates instance of DFS/Default progarm gerarator and initializes it(i.e creates OutputMgr object ) and dumps the probabilities and returns the object
 	AbsProgramGenerator *generator = AbsProgramGenerator::CreateInstance(argc, argv, g_Seed);
 	if (!generator) {
 		cout << "error: can't create generator!" << std::endl;
 		exit(-1);
 	}
+//actual generation of CFG and traversing the tree
 	generator->goGenerator();
 	delete generator;
 
diff --git a/goto_testing/src/Statement.cpp b/goto_testing/src/Statement.cpp
index da13114..95b86f0 100644
--- a/goto_testing/src/Statement.cpp
+++ b/goto_testing/src/Statement.cpp
@@ -234,7 +234,7 @@ int Statement::sid = 0;
  */
 Statement *
 Statement::make_random(CGContext &cg_context,
-					   eStatementType t)
+					   eStatementType t)//the second parameter is passed by default(MAX_STATEMENT_TYPE) in the header file
 {
 	DEPTH_GUARD_BY_TYPE_RETURN_WITH_FLAG(dtStatement, t, NULL);
 	// Should initialize table first
diff --git a/goto_testing/src/StatementGoto.cpp b/goto_testing/src/StatementGoto.cpp
index d4ea301..8485123 100644
--- a/goto_testing/src/StatementGoto.cpp
+++ b/goto_testing/src/StatementGoto.cpp
@@ -262,7 +262,10 @@ StatementGoto::Output(std::ostream &out, FactMgr* /*fm*/, int indent) const
 	output_tab(out, indent+1);
 
 //**changehere** some other name**//
-	out << "goto " << other_name_for_label << ";";
+	if(CGOptions::computed_goto())
+		out << "goto " << other_name_for_label << ";";
+	else
+		out << "goto " << label << ";";
 	outputln(out);
 }
 
@@ -417,7 +420,7 @@ StatementGoto::doFinalization(void)
 {
 	stm_labels.clear();
 }
-
+//*changehere*//
 void 
 StatementGoto::change_label(std::vector<string> addr_labels) const {
 		string find_label="";