Skip to content

Commit

Permalink
Merge pull request #90 from shubhamnarlawar77/csmith-gcc-c-extension
Browse files Browse the repository at this point in the history
Added support for signed & unsigned integer of size 128 bits and generation of binary constants
  • Loading branch information
jxyang committed May 31, 2020
2 parents 5039909 + 25267c5 commit d0b585a
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/CGOptions.cpp
Expand Up @@ -200,6 +200,9 @@ 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);
DEFINE_GETTER_SETTER_BOOL(Int128);
DEFINE_GETTER_SETTER_BOOL(UInt128);
DEFINE_GETTER_SETTER_BOOL(binary_constant);

void
CGOptions::set_default_builtin_kinds()
Expand Down Expand Up @@ -311,6 +314,9 @@ CGOptions::set_default_settings(void)
fast_execution(false);

set_default_builtin_kinds();
Int128(false);
UInt128(false);
binary_constant(false);
}

// Add options necessary for cpp
Expand Down
12 changes: 12 additions & 0 deletions src/CGOptions.h
Expand Up @@ -469,6 +469,15 @@ class CGOptions {

static bool var_attr_flag(void);
static bool var_attr_flag(bool p);

static bool Int128(void);
static bool Int128(bool p);

static bool UInt128(void);
static bool UInt128(bool p);

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

Expand Down Expand Up @@ -616,6 +625,9 @@ class CGOptions {
static bool type_attr_flag_;
static bool label_attr_flag_;
static bool var_attr_flag_;
static bool Int128_;
static bool UInt128_;
static bool binary_constant_;
private:
CGOptions(void);
CGOptions(CGOptions &cgo);
Expand Down
50 changes: 45 additions & 5 deletions src/Constant.cpp
Expand Up @@ -96,12 +96,28 @@ Constant::clone() const
return new Constant(*this);
}

//--------------------------------------------------------------
string HexToBinary(string val)
{
std::ostringstream oss;
string ToBinary[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
for (unsigned int i = 0; i < val.size(); i++) {
if ((val[i] - '0') < 16)
oss << ToBinary[val[i] - '0'];
else
oss << ToBinary[val[i] - '7'];
}
return oss.str();
}

// --------------------------------------------------------------
static string
GenerateRandomCharConstant(void)
{
string ch;
if (CGOptions::ccomp() || !CGOptions::longlong())
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
ch = string("0b") + HexToBinary(RandomHexDigits(2));
} else if (CGOptions::ccomp() || !CGOptions::longlong())
ch = string("0x") + RandomHexDigits(2);
else
ch = string("0x") + RandomHexDigits(2) + "L";
Expand All @@ -114,21 +130,37 @@ GenerateRandomIntConstant(void)
{
string val;
// Int constant - Max 8 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
val = string("0b") + HexToBinary(RandomHexDigits( 8 ));
} else if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";

return val;
}

// --------------------------------------------------------------
static string
GenerateRandomInt128Constant(void)
{
string val;
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
val = string("0b") + HexToBinary(RandomHexDigits( 16 ));
} else
val = "0x" + RandomHexDigits( 16 ) ;
return val;
}

// --------------------------------------------------------------
static string
GenerateRandomShortConstant(void)
{
string val;
// Short constant - Max 4 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
val = string("0b") + HexToBinary(RandomHexDigits( 4 ));
} else if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 4 );
else
val = "0x" + RandomHexDigits( 4 ) + "L";
Expand All @@ -142,7 +174,9 @@ GenerateRandomLongConstant(void)
{
string val;
// Long constant - Max 8 Hex digits on 32-bit platforms
if (!CGOptions::longlong())
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
val = string("0b") + HexToBinary(RandomHexDigits( 8 ));
} else if (!CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";
Expand All @@ -153,8 +187,12 @@ GenerateRandomLongConstant(void)
static string
GenerateRandomLongLongConstant(void)
{
string val;
// Long constant - Max 8 Hex digits on 32-bit platforms
string val = "0x" + RandomHexDigits( 16 ) + "LL";
if (CGOptions::binary_constant() && rnd_flipcoin(pBinaryConstProb)) {
val = string("0b") + HexToBinary(RandomHexDigits( 16 )) + "LL";
} else
val = "0x" + RandomHexDigits( 16 ) + "LL";
return val;
}

Expand Down Expand Up @@ -380,6 +418,8 @@ GenerateRandomConstant(const Type* type)
case eULong: v = GenerateRandomLongConstant(); break;
case eULongLong: v = GenerateRandomLongLongConstant(); break;
case eFloat: v = GenerateRandomFloatHexConstant(); break;
case eInt128: v = GenerateRandomInt128Constant(); break;
case eUInt128: v = GenerateRandomInt128Constant(); break;
// case eDouble: v = GenerateRandomFloatConstant(); break;
default:
assert(0 && "Unsupported type!");
Expand Down
18 changes: 18 additions & 0 deletions src/Probabilities.cpp
Expand Up @@ -513,6 +513,9 @@ Probabilities::set_single_name_maps()

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

//for choosing binary constants
set_single_name("binary_constant", pBinaryConstProb);
}

void
Expand Down Expand Up @@ -558,6 +561,7 @@ Probabilities::initialize_single_probs()
m[pTypeAttrProb] = 50;
m[pLabelAttrProb] = 30;
m[pVarAttrProb] = 30;
m[pBinaryConstProb] = 3;

if (CGOptions::volatiles())
m[pRegularVolatileProb] = 50;
Expand Down Expand Up @@ -699,6 +703,20 @@ Probabilities::set_default_simple_types_prob()
SET_SINGLE_NAME("char_prob", Char, 0);
}

if (CGOptions::Int128()) {
SET_SINGLE_NAME("Int128_prob", Int128, 1);
}
else {
SET_SINGLE_NAME("Int128_prob", Int128, 0);
}

if (CGOptions::UInt128()) {
SET_SINGLE_NAME("UInt128_prob", UInt128, 1);
}
else {
SET_SINGLE_NAME("UInt128_prob", UInt128, 0);
}

SET_SINGLE_NAME("int_prob", Int, 1);
SET_SINGLE_NAME("short_prob", Short, 1);

Expand Down
12 changes: 12 additions & 0 deletions src/Probabilities.h
Expand Up @@ -139,6 +139,8 @@ enum ProbName {
pULongProb,
pULongLongProb,
pFloatProb,
pInt128Prob,
pUInt128Prob,

// for safe math ops
pSafeOpsSizeProb,
Expand All @@ -152,6 +154,7 @@ enum ProbName {
pTypeAttrProb,
pLabelAttrProb,
pVarAttrProb,
pBinaryConstProb,

};

Expand Down Expand Up @@ -241,6 +244,15 @@ enum ProbName {
#define VarAttrProb \
Probabilities::get_prob(pVarAttrProb)

#define Int128Prob \
Probabilities::get_prob(pInt128Prob)

#define UInt128Prob \
Probabilities::get_prob(pUInt128Prob)

#define BinaryConstProb \
Probabilities::get_prob(pBinaryConstProb)

//////////////////////////////////////////////////
#define UNARY_OPS_PROB_FILTER \
Probabilities::get_prob_filter(pUnaryOpsProb)
Expand Down
33 changes: 33 additions & 0 deletions src/RandomProgramGenerator.cpp
Expand Up @@ -215,6 +215,9 @@ static void print_help()
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;
cout << " --int128 | --no-int128: enable | disable generate __int128 as datatype extension (disabled by default)." << endl << endl;
cout << " --uint128 | --no-uint128: enable | disable generate unsigned __int128 as datatype extension (disabled by default)." << endl << endl;
cout << " --binary-constant | --no-binary-constant: enable | disable generate binary constant (disabled by default)." << endl << endl;

}

Expand Down Expand Up @@ -859,6 +862,36 @@ main(int argc, char **argv)
continue;
}

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

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

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

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

if (strcmp (argv[i], "--binary-constant") == 0) {
CGOptions::binary_constant(true);
continue;
}

if (strcmp (argv[i], "--no-binary-constant") == 0) {
CGOptions::binary_constant(false);
continue;
}

if (strcmp (argv[i], "--max-array-dim") ==0 ) {
unsigned long dim;
i++;
Expand Down
16 changes: 15 additions & 1 deletion src/Type.cpp
Expand Up @@ -451,6 +451,12 @@ Type::get_type_from_string(const string &type_string)
else if (type_string == "Float") {
return &Type::get_simple_type(eFloat);
}
else if (type_string == "Int128") {
return &Type::get_simple_type(eInt128);
}
else if (type_string == "UInt128") {
return &Type::get_simple_type(eUInt128);
}

assert(0 && "Unsupported type string!");
return NULL;
Expand Down Expand Up @@ -1446,6 +1452,8 @@ Type::to_unsigned(void) const
case eShort: return &get_simple_type(eUShort);
case eLong: return &get_simple_type(eULong);
case eLongLong: return &get_simple_type(eULongLong);
case eInt128: return &get_simple_type(eInt128);
case eUInt128: return &get_simple_type(eUInt128);
default:
break;
}
Expand Down Expand Up @@ -1613,7 +1621,9 @@ Type::SizeInBytes(void) const
case eULong: return 4;
case eULongLong:return 8;
case eFloat: return 4;
// case eDouble: return 8;
case eInt128: return 16;
case eUInt128: return 16;
// case eDouble: return 8;
}
break;
case eUnion: {
Expand Down Expand Up @@ -1742,6 +1752,10 @@ Type::Output(std::ostream &out) const
out << "void";
} else if (this->simple_type == eFloat) {
out << "float";
} else if (this->simple_type == eInt128) {
out << "__int" << (SizeInBytes() * 8);
} else if (this->simple_type == eUInt128) {
out << "unsigned __int" << (SizeInBytes() * 8);
} else {
out << (is_signed() ? "int" : "uint");
out << (SizeInBytes() * 8);
Expand Down
4 changes: 3 additions & 1 deletion src/Type.h
Expand Up @@ -83,8 +83,10 @@ enum eSimpleType
eFloat,
// eDouble,
eULongLong,
eInt128,
eUInt128,
};
#define MAX_SIMPLE_TYPES ((eSimpleType) (eULongLong+1))
#define MAX_SIMPLE_TYPES ((eSimpleType) (eUInt128+1))

enum eMatchType
{
Expand Down

0 comments on commit d0b585a

Please sign in to comment.