Skip to content

Commit bd0598e

Browse files
Added support for computed goto extension
1 parent 8ade51f commit bd0598e

File tree

8 files changed

+96
-3
lines changed

8 files changed

+96
-3
lines changed

src/Block.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
#include "Expression.h"
6666
#include "VectorFilter.h"
6767

68+
#include <string>
69+
#include <vector>
6870
using namespace std;
6971

7072
///////////////////////////////////////////////////////////////////////////////
@@ -188,14 +190,40 @@ Block::make_random(CGContext &cg_context, bool looping)
188190

189191
curr_func->stack.pop_back();
190192
if (Error::get_error() != SUCCESS) {
191-
//curr_func->stack.pop_back();
193+
curr_func->stack.pop_back();
192194
delete b;
193195
return NULL;
194196
}
195197

196198
// ISSUE: in the exhaustive mode, do we need a return statement here
197199
// if the last statement is not?
198200
Error::set_error(SUCCESS);
201+
202+
203+
if(CGOptions::computed_goto()){
204+
if(curr_func->blocks[0]->stm_id==b->stm_id){
205+
std::vector<string> labels;
206+
labels.clear();
207+
curr_func->blocks[0]->find_contained_labels(labels);
208+
string ss="";
209+
for (std::vector<string>::iterator itr=labels.begin();itr!=labels.end();itr++) {
210+
ss.clear();
211+
ss += "&&";
212+
ss += *itr;
213+
curr_func->blocks[0]->addr_labels.push_back(ss);//only adds in the main array related to function.
214+
}
215+
//__________________________________________________________
216+
for (size_t i=0; i<fm->cfg_edges.size();i++) {
217+
const CFGEdge* e = fm->cfg_edges[i];
218+
if(e->src->eType == eGoto) {
219+
const StatementGoto* sg = dynamic_cast<const StatementGoto* >(e->src);
220+
assert(sg);
221+
sg->change_label(curr_func->blocks[0]->addr_labels);
222+
}
223+
}
224+
225+
}
226+
}
199227
return b;
200228
}
201229

@@ -295,6 +323,10 @@ Block::Output(std::ostream &out, FactMgr* fm, int indent) const
295323
ss << "block id: " << stm_id;
296324
output_comment_line(out, ss.str());
297325

326+
if(CGOptions::computed_goto()){
327+
if(!this->addr_labels.empty())
328+
this->print_label_addr_array(out,indent);
329+
}
298330
if (CGOptions::depth_protect()) {
299331
out << "DEPTH++;" << endl;
300332
}
@@ -805,6 +837,19 @@ Block::post_creation_analysis(CGContext& cg_context, const Effect& pre_effect)
805837
}
806838
}
807839

840+
void
841+
Block::print_label_addr_array(std::ostream &out , int indent) const{
842+
ostringstream ss;
843+
output_tab (out,indent);
844+
cout << "/*\nNUMBER OF GOTO'S IN ABOVEE BLOCK:" << addr_labels.size() << "*\/";
845+
cout << "\nvoid *target[] = { ";
846+
for(unsigned int i=0; i < addr_labels.size();i++){
847+
i!=0 ? cout << ", " : cout << "";
848+
cout << addr_labels[i];
849+
}
850+
851+
cout << "};\n";
852+
}
808853
///////////////////////////////////////////////////////////////////////////////
809854

810855
// Local Variables:

src/Block.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class Block : public Statement
8080
Block* random_parent_block(void);
8181

8282
int block_size() { return block_size_; }
83+
std::vector<string> addr_labels;
84+
void print_label_addr_array(std::ostream&, int) const;
85+
8386
// These are currently accessed directly.
8487
std::vector<Statement *> stms;
8588
std::vector<Statement *> deleted_stms;

src/CGOptions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ DEFINE_GETTER_SETTER_BOOL(const_struct_union_fields);
199199
DEFINE_GETTER_SETTER_BOOL(lang_cpp);
200200
DEFINE_GETTER_SETTER_BOOL(cpp11);
201201
DEFINE_GETTER_SETTER_BOOL(fast_execution);
202+
DEFINE_GETTER_SETTER_BOOL(computed_goto);
202203

203204
void
204205
CGOptions::set_default_builtin_kinds()
@@ -313,6 +314,7 @@ CGOptions::set_default_settings(void)
313314
fast_execution(false);
314315

315316
set_default_builtin_kinds();
317+
computed_goto(false);
316318
}
317319

318320
// Add options necessary for cpp

src/CGOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ class CGOptions {
363363
static bool signed_char_index(void);
364364
static bool signed_char_index(bool p);
365365

366+
static bool computed_goto(void);
367+
static bool computed_goto(bool p);
366368
/////////////////////////////////////////////////////////
367369
static void set_default_settings(void);
368370

@@ -579,6 +581,7 @@ class CGOptions {
579581
static bool no_return_dead_ptr_;
580582
static bool hash_value_printf_;
581583
static bool signed_char_index_;
584+
static bool computed_goto_;
582585
static std::string dump_default_probabilities_;
583586
static std::string dump_random_probabilities_;
584587
static std::string probability_configuration_;

src/DefaultProgramGenerator.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ void
9191
DefaultProgramGenerator::goGenerator()
9292
{
9393
output_mgr_->OutputHeader(argc_, argv_, seed_);
94-
9594
GenerateAllTypes();
9695
GenerateFunctions();
9796
output_mgr_->Output();

src/RandomProgramGenerator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ static void print_help()
176176
cout << " --inline-function | --no-inline-function: enable | disable inline attributes on generated functions." << endl << endl;
177177
cout << " --inline-function-prob <num>: set the probability of each function being marked as inline (default is 50)." << endl << endl;
178178

179+
//GCC C extensions
180+
cout << " --computed-goto | --no-computed-goto: enable | disable computed goto extension (disable by default)." << endl << endl;
181+
179182
// numbered controls
180183
cout << " --max-array-dim <num>: limit array dimensions to <num>. (default 3)" << endl << endl;
181184
cout << " --max-array-len-per-dim <num>: limit array length per dimension to <num> (default 10)." << endl << endl;
@@ -850,6 +853,16 @@ main(int argc, char **argv)
850853
continue;
851854
}
852855

856+
if (strcmp (argv[i], "--computed-goto") == 0) {
857+
CGOptions::computed_goto(true);
858+
continue;
859+
}
860+
861+
if (strcmp (argv[i], "--no-computed-goto") == 0) {
862+
CGOptions::computed_goto(false);
863+
continue;
864+
}
865+
853866
if (strcmp (argv[i], "--no-jumps") == 0) {
854867
CGOptions::jumps(false);
855868
continue;

src/StatementGoto.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ StatementGoto::Output(std::ostream &out, FactMgr* /*fm*/, int indent) const
260260
out << ")";
261261
outputln(out);
262262
output_tab(out, indent+1);
263-
out << "goto " << label << ";";
263+
264+
if(CGOptions::computed_goto())
265+
out << "goto " << other_name_for_label << ";";
266+
else
267+
out << "goto " << label << ";";
264268
outputln(out);
265269
}
266270

@@ -416,6 +420,27 @@ StatementGoto::doFinalization(void)
416420
stm_labels.clear();
417421
}
418422

423+
void
424+
StatementGoto::change_label(std::vector<string> addr_labels) const{
425+
string find_label="";
426+
find_label+="&&";
427+
find_label+=label;
428+
auto it = std::find(addr_labels.begin(),addr_labels.end(),find_label);
429+
int index;
430+
if(it == addr_labels.end()){
431+
assert ("LABEL NOT FOUND");
432+
}
433+
else{
434+
index = std::distance (addr_labels.begin(),it);
435+
}
436+
std::stringstream ss;
437+
ss.clear();
438+
ss<< "*target[";
439+
ss<<index;
440+
ss<<"]";
441+
other_name_for_label="";
442+
other_name_for_label=ss.str();
443+
}
419444
///////////////////////////////////////////////////////////////////////////////
420445

421446
// Local Variables:

src/StatementGoto.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class StatementGoto : public Statement
7272
std::string label;
7373
std::vector<const Variable*> init_skipped_vars;
7474
static std::map<const Statement*, std::string> stm_labels;
75+
76+
mutable std::string other_name_for_label;
77+
void change_label(std::vector<string> addr_labels) const;
7578
};
7679

7780
///////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)