Skip to content

Commit

Permalink
Merge pull request #84 from shubhamnarlawar77/csmith-gcc-c-extension
Browse files Browse the repository at this point in the history
Added support for Common Type Attributes, Label Attributes and Variable Attributes
  • Loading branch information
jxyang committed May 13, 2020
2 parents efdc1a3 + 48f4da9 commit 5039909
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/CGOptions.cpp
Expand Up @@ -197,6 +197,9 @@ DEFINE_GETTER_SETTER_BOOL(fast_execution);

//GCC C Extensions
DEFINE_GETTER_SETTER_BOOL(func_attr_flag);
DEFINE_GETTER_SETTER_BOOL(type_attr_flag);
DEFINE_GETTER_SETTER_BOOL(label_attr_flag);
DEFINE_GETTER_SETTER_BOOL(var_attr_flag);

void
CGOptions::set_default_builtin_kinds()
Expand Down
12 changes: 12 additions & 0 deletions src/CGOptions.h
Expand Up @@ -460,6 +460,15 @@ class CGOptions {
//GCC C Extensions
static bool func_attr_flag(void);
static bool func_attr_flag(bool p);

static bool type_attr_flag(void);
static bool type_attr_flag(bool p);

static bool label_attr_flag(void);
static bool label_attr_flag(bool p);

static bool var_attr_flag(void);
static bool var_attr_flag(bool p);
private:
static bool enabled_builtin_kind(const string &kind);

Expand Down Expand Up @@ -604,6 +613,9 @@ class CGOptions {

//GCC C Extensions
static bool func_attr_flag_;
static bool type_attr_flag_;
static bool label_attr_flag_;
static bool var_attr_flag_;
private:
CGOptions(void);
CGOptions(CGOptions &cgo);
Expand Down
12 changes: 12 additions & 0 deletions src/Probabilities.cpp
Expand Up @@ -504,6 +504,15 @@ Probabilities::set_single_name_maps()

//for choosing function attributes
set_single_name("func_attr_flag", pFuncAttrProb);

//for choosing type attributes
set_single_name("type_attr_flag", pTypeAttrProb);

//for choosing label attributes
set_single_name("label_attr_flag", pLabelAttrProb);

//for choosing variable attributes
set_single_name("var_attr_flag", pVarAttrProb);
}

void
Expand Down Expand Up @@ -546,6 +555,9 @@ Probabilities::initialize_single_probs()

//GCC C Extensions
m[pFuncAttrProb] = 30;
m[pTypeAttrProb] = 50;
m[pLabelAttrProb] = 30;
m[pVarAttrProb] = 30;

if (CGOptions::volatiles())
m[pRegularVolatileProb] = 50;
Expand Down
13 changes: 13 additions & 0 deletions src/Probabilities.h
Expand Up @@ -149,6 +149,10 @@ enum ProbName {

//for function attributes
pFuncAttrProb,
pTypeAttrProb,
pLabelAttrProb,
pVarAttrProb,

};

#define MAX_PROB_NAME ((ProbName)(pStatementProb+1))
Expand Down Expand Up @@ -228,6 +232,15 @@ enum ProbName {
#define FuncAttrProb \
Probabilities::get_prob(pFuncAttrProb)

#define TypeAttrProb \
Probabilities::get_prob(pTypeAttrProb)

#define LabelAttrProb \
Probabilities::get_prob(pLabelAttrProb)

#define VarAttrProb \
Probabilities::get_prob(pVarAttrProb)

//////////////////////////////////////////////////
#define UNARY_OPS_PROB_FILTER \
Probabilities::get_prob_filter(pUnaryOpsProb)
Expand Down
50 changes: 50 additions & 0 deletions src/RandomProgramGenerator.cpp
Expand Up @@ -211,6 +211,10 @@ static void print_help()
//--------------------------GCC C Extensions--------------------------
cout<< "------------------------------GCC C Extensions------------------------------" << endl << endl;
cout << " --function-attributes | --no-func-attributes: enable | disable generate common function attributes (disabled by default)." << endl << endl;
cout << " --type-attributes | --no-type-attributes: enable | disable generate common type attributes (disabled by default)." << endl << endl;
cout << " --label-attributes | --no-label-attributes: enable | disable generate common label attributes (disabled by default)." << endl << endl;
cout << " --variable-attributes | --no-variable-attributes: enable | disable generate common variable attributes (disabled by default)." << endl << endl;
cout << " --compiler-attributes | --no-compiler-attributes: enable | disable generate function, type, label and variable attributes (disabled by default)." << endl << endl;

}

Expand Down Expand Up @@ -809,6 +813,52 @@ main(int argc, char **argv)
continue;
}

if (strcmp (argv[i], "--type-attributes") == 0) {
CGOptions::type_attr_flag(true);
continue;
}

if (strcmp (argv[i], "--no-type-attributes") == 0) {
CGOptions::type_attr_flag(false);
continue;
}

if (strcmp (argv[i], "--label-attributes") == 0) {
CGOptions::label_attr_flag(true);
continue;
}

if (strcmp (argv[i], "--no-label-attributes") == 0) {
CGOptions::label_attr_flag(false);
continue;
}

if (strcmp (argv[i], "--variable-attributes") == 0) {
CGOptions::var_attr_flag(true);
continue;
}

if (strcmp (argv[i], "--no-variable-attributes") == 0) {
CGOptions::var_attr_flag(false);
continue;
}

if (strcmp (argv[i], "--compiler-attributes") == 0) {
CGOptions::func_attr_flag(true);
CGOptions::type_attr_flag(true);
CGOptions::label_attr_flag(true);
CGOptions::var_attr_flag(true);
continue;
}

if (strcmp (argv[i], "--no-compiler-attributes") == 0) {
CGOptions::func_attr_flag(false);
CGOptions::type_attr_flag(false);
CGOptions::label_attr_flag(false);
CGOptions::var_attr_flag(false);
continue;
}

if (strcmp (argv[i], "--max-array-dim") ==0 ) {
unsigned long dim;
i++;
Expand Down
26 changes: 24 additions & 2 deletions src/Statement.cpp
Expand Up @@ -84,11 +84,24 @@
#include "util.h"
#include "StringUtils.h"
#include "VariableSelector.h"
#include "Attribute.h"

using namespace std;
const Statement* Statement::failed_stm;

AttributeGenerator Statement::label_attr_generator;

///////////////////////////////////////////////////////////////////////////////

void
InitializeLabelAttributes()
{
if(CGOptions::label_attr_flag()){
Statement::label_attr_generator.attributes.push_back(new BooleanAttribute("hot", TypeAttrProb));
Statement::label_attr_generator.attributes.push_back(new BooleanAttribute("cold", TypeAttrProb));
}
}

class StatementFilter : public Filter
{
public:
Expand All @@ -97,21 +110,28 @@ class StatementFilter : public Filter
virtual ~StatementFilter(void);

virtual bool filter(int v) const;

static bool label_attr_generate;
private:
const CGContext &cg_context_;
};

StatementFilter::StatementFilter(const CGContext &cg_context)
: cg_context_(cg_context)
{

if(!label_attr_generate){
InitializeLabelAttributes();
label_attr_generate = true;
}
}

StatementFilter::~StatementFilter(void)
{

}

bool StatementFilter::label_attr_generate = false;

// use a table to define probabilities of different kinds of statements
// Must initialize it before use
ProbabilityTable<unsigned int, ProbName> *Statement::stmtTable_ = NULL;
Expand Down Expand Up @@ -951,7 +971,9 @@ Statement::pre_output(std::ostream &out, FactMgr* /* fm */, int indent) const
vector<const StatementGoto*> gotos;
if (find_jump_sources(gotos)) {
assert(gotos.size() > 0);
out << gotos[0]->label << ":" << endl;
out << gotos[0]->label << ":";
label_attr_generator.Output(out);
out << endl;
return 1;
//for (j=0; j<gotos.size(); j++) {
// gotos[j]->output_skipped_var_inits(out, indent);
Expand Down
3 changes: 3 additions & 0 deletions src/Statement.h
Expand Up @@ -66,6 +66,7 @@ class ProbabilityTable;
class StatementGoto;
class Variable;
class Expression;
class AttributeGenerator;

enum eStatementType
{
Expand Down Expand Up @@ -178,6 +179,8 @@ class Statement

int get_blk_depth(void) const;

static AttributeGenerator label_attr_generator;

// unique id for each statement
int stm_id;
Function* func;
Expand Down
29 changes: 28 additions & 1 deletion src/Type.cpp
Expand Up @@ -54,6 +54,7 @@
#include "Probabilities.h"
#include "DepthSpec.h"
#include "Enumerator.h"
#include "Attribute.h"

using namespace std;

Expand All @@ -71,7 +72,27 @@ Type *Type::void_type = NULL;
static vector<Type *> AllTypes;
static vector<Type *> derived_types;

AttributeGenerator struct_type_attr_generator;
AttributeGenerator union_type_attr_generator;

//////////////////////////////////////////////////////////////////////

void
InitializeTypeAttributes()
{
if(CGOptions::type_attr_flag()){
struct_type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8));
struct_type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8));
struct_type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb));
struct_type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb));
union_type_attr_generator.attributes.push_back(new AlignedAttribute("aligned", TypeAttrProb, 8));
union_type_attr_generator.attributes.push_back(new AlignedAttribute("warn_if_not_aligned", TypeAttrProb, 8));
union_type_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", TypeAttrProb));
union_type_attr_generator.attributes.push_back(new BooleanAttribute("unused", TypeAttrProb));
union_type_attr_generator.attributes.push_back(new BooleanAttribute("transparent_union", TypeAttrProb));
}
}

class NonVoidTypeFilter : public Filter
{
public:
Expand Down Expand Up @@ -1218,6 +1239,7 @@ Type::GenerateSimpleTypes(void)
void
GenerateAllTypes(void)
{
InitializeTypeAttributes();
// In the exhaustive mode, we want to generate all type first.
// We don't support struct for now
if (CGOptions::dfs_exhaustive()) {
Expand Down Expand Up @@ -1911,7 +1933,12 @@ void OutputStructUnion(Type* type, std::ostream &out)
OutputUnionAssignOps(type, out, true);
}

out << "};";
out << "}";
if(type->eType == eStruct || type->eType == eUnion){
struct_type_attr_generator.Output(out);
union_type_attr_generator.Output(out);
}
out << ";";
really_outputln(out);
if (type->packed_) {
if (CGOptions::ccomp()) {
Expand Down
26 changes: 25 additions & 1 deletion src/Variable.cpp
Expand Up @@ -69,6 +69,7 @@
#include "Error.h"
#include "ArrayVariable.h"
#include "StringUtils.h"
#include "Attribute.h"


using namespace std;
Expand All @@ -77,8 +78,25 @@ unsigned long Variable::ctrl_vars_count;

const char Variable::sink_var_name[] = "csmith_sink_";

bool Variable::var_attr_generate = false;
AttributeGenerator Variable::var_attr_generator;

//////////////////////////////////////////////////////////////////////////////

void
InitializeVariableAttributes()
{
if(CGOptions::var_attr_flag()){
Variable::var_attr_generator.attributes.push_back(new MultiChoiceAttribute("visibility", VarAttrProb, {"default", "hidden", "protected", "internal"}));
Variable::var_attr_generator.attributes.push_back(new AlignedAttribute("aligned", VarAttrProb, 8));
Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("common", VarAttrProb));
Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("uncommon", VarAttrProb));
Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("deprecated", VarAttrProb));
Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("unused", VarAttrProb));
Variable::var_attr_generator.attributes.push_back(new BooleanAttribute("used", VarAttrProb));
}
}

int find_variable_in_set(const vector<const Variable*>& set, const Variable* v)
{
size_t i;
Expand Down Expand Up @@ -409,6 +427,10 @@ Variable::CreateVariable(const std::string &name, const Type *type,
Variable *
Variable::CreateVariable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer)
{
if(!Variable::var_attr_generate){
InitializeVariableAttributes();
Variable::var_attr_generate = true;
}
assert(type);
if (type->eType == eSimple)
assert(type->simple_type != eVoid);
Expand Down Expand Up @@ -695,7 +717,9 @@ Variable::OutputDef(std::ostream &out, int indent) const
out << "static ";
}
output_qualified_type(out);
out << get_actual_name() << " = ";
out << get_actual_name();
var_attr_generator.Output(out);
out << " = ";
assert(init);
init->Output(out);
out << ";";
Expand Down
4 changes: 4 additions & 0 deletions src/Variable.h
Expand Up @@ -58,6 +58,7 @@ class Function;
class Block;
class Lhs;
class ArrayVariable;
class AttributeGenerator;

class Variable
{
Expand Down Expand Up @@ -161,6 +162,9 @@ class Variable

static const char sink_var_name[];

static bool var_attr_generate;
static AttributeGenerator var_attr_generator;

private:
Variable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer);
Variable(const std::string &name, const Type *type, const Expression* init, const CVQualifiers* qfer, const Variable* isFieldVarOf, bool isArray);
Expand Down

0 comments on commit 5039909

Please sign in to comment.