Skip to content

Commit

Permalink
Merge pull request #14 from ZhuJunhua1104/type_restriction
Browse files Browse the repository at this point in the history
Type restriction
  • Loading branch information
jxyang committed Sep 8, 2015
2 parents 2e9b962 + 69dd6a0 commit aa48386
Show file tree
Hide file tree
Showing 132 changed files with 127,021 additions and 120,117 deletions.
108 changes: 58 additions & 50 deletions src/AggregateType.cpp
Expand Up @@ -50,6 +50,7 @@
#include "util.h"
#include "Bookkeeper.h"
#include "Probabilities.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -156,7 +157,7 @@ AggregateType::make_full_bitfields_struct_fields(size_t field_cnt, vector<const
for (size_t i=0; i<field_cnt; i++) {
bool is_non_bitfield = rnd_flipcoin(ScalarFieldInFullBitFieldsProb);
if (is_non_bitfield) {
make_one_struct_field(random_fields, qualifiers, fields_length);
make_one_struct_field(random_fields, qualifiers, fields_length, TypeConfig::get_filter_for_request(asStructMember));
}
else {
make_one_bitfield(random_fields, qualifiers, fields_length);
Expand All @@ -167,27 +168,32 @@ AggregateType::make_full_bitfields_struct_fields(size_t field_cnt, vector<const
void
AggregateType::make_one_struct_field(vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length)
vector<int> &fields_length,
Filter * additional_filter)
{
VectorFilter f;
VectorFilter * f = NULL;
if (additional_filter)
f = dynamic_cast<VectorFilter *>(additional_filter);
if (!f)
f = new VectorFilter();
unsigned int type_index = 0;
Type *typ = 0;
for( type_index = 0; type_index < AllTypes.size(); type_index++) {
typ = AllTypes[type_index];
assert(typ);
if (typ->eType == eSimple) {
vector<Type *>::iterator iter;
for(iter = AllTypes.begin(); iter != AllTypes.end(); ++iter)
{
if ((*iter)->eType == eSimple) {
Filter *filter = SIMPLE_TYPES_PROB_FILTER;
if(filter->filter(typ->simple_type))
f.add(type_index);
if(filter->filter((*iter)->simple_type))
f->add((*iter)->type_index);
}
else if ((typ->eType == eStruct) && (!CGOptions::return_structs())) {
f.add(type_index);
else if (((*iter)->eType == eStruct) && (!CGOptions::return_structs())) {
f->add((*iter)->type_index);
}
if (typ->get_struct_depth() >= CGOptions::max_nested_struct_level()) {
f.add(type_index);
if ((*iter)->get_struct_depth() >= CGOptions::max_nested_struct_level()) {
f->add((*iter)->type_index);
}
}
type_index = rnd_upto(AllTypes.size(), &f);
type_index = rnd_upto(AllTypes.size(), f);
delete f;
const Type* type = AllTypes[type_index];
random_fields.push_back(type);
CVQualifiers qual = CVQualifiers::random_qualifiers(type, FieldConstProb, FieldVolatileProb);
Expand All @@ -208,7 +214,7 @@ AggregateType::make_one_union_field(vector<const Type*> &fields, vector<CVQualif
// filter out struct types containing bit-fields. Their layout is implementation
// defined, we don't want to mess with them in unions for now
for (i=0; i<AllTypes.size(); i++) {
if (!AllTypes[i]->has_bitfields()) {
if (!AllTypes[i]->has_bitfields() && ! TypeConfig::check_exclude_by_request( AllTypes[i], asUnionMember)) {
ok_types.push_back(AllTypes[i]);
}
}
Expand Down Expand Up @@ -262,7 +268,7 @@ AggregateType::make_normal_struct_fields(size_t field_cnt, vector<const Type*> &
make_one_bitfield(random_fields, qualifiers, fields_length);
}
else {
make_one_struct_field(random_fields, qualifiers, fields_length);
make_one_struct_field(random_fields, qualifiers, fields_length, TypeConfig::get_filter_for_request(asStructMember));
}
}
}
Expand Down Expand Up @@ -466,38 +472,39 @@ unsigned long
AggregateType::SizeInBytes(void) const
{
size_t i;
switch (eType) {
case eUnion: {
unsigned int max_size = 0;
for (i=0; i<fields.size(); i++) {
unsigned int sz = 0;
if (is_bitfield(i)) {
assert(i < bitfields_length_.size());
sz = (int)(ceil(bitfields_length_[i] / 8.0) * 8);
} else {
sz = fields[i]->SizeInBytes();
}
if (sz == SIZE_UNKNOWN) return sz;
if (sz > max_size) {
max_size = sz;
}
}
return max_size;
}
case eStruct: {
if (!this->packed_) return SIZE_UNKNOWN;
// give up if there are bitfields, too much compiler-dependence and machine-dependence
if (this->has_bitfields()) return SIZE_UNKNOWN;
unsigned int total_size = 0;
for (i=0; i<fields.size(); i++) {
unsigned int sz = fields[i]->SizeInBytes();
if (sz == SIZE_UNKNOWN) return sz;
total_size += sz;
}
return total_size;
}
}
return 0;
switch (eType) {
case eUnion: {
unsigned int max_size = 0;
for (i=0; i<fields.size(); i++) {
unsigned int sz = 0;
if (is_bitfield(i)) {
assert(i < bitfields_length_.size());
sz = (int)(ceil(bitfields_length_[i] / 8.0) * 8);
} else {
sz = fields[i]->SizeInBytes();
}
if (sz == SIZE_UNKNOWN) return sz;
if (sz > max_size) {
max_size = sz;
}
}
return max_size;
}
case eStruct: {
if (!this->packed_) return SIZE_UNKNOWN;
// give up if there are bitfields, too much compiler-dependence and machine-dependence
if (this->has_bitfields()) return SIZE_UNKNOWN;
unsigned int total_size = 0;
for (i=0; i<fields.size(); i++) {
unsigned int sz = fields[i]->SizeInBytes();
if (sz == SIZE_UNKNOWN) return sz;
total_size += sz;
}
return total_size;
}
default:
return 0;
}
}


Expand All @@ -510,7 +517,8 @@ AggregateType::get_int_subfield_names(string prefix, vector<string>& names, cons
for (i=0; i<fields.size(); i++) {
if (is_unamed_padding(i)) continue; // skip 0 length bitfields
// skip excluded fields
if (std::find(excluded_fields.begin(), excluded_fields.end(), j) != excluded_fields.end()) {
if (std::find(excluded_fields.begin(), excluded_fields.end(), j) != excluded_fields.end() ||
is_bitfield(i)) {
j++;
continue;
}
Expand Down
3 changes: 2 additions & 1 deletion src/AggregateType.h
Expand Up @@ -113,7 +113,8 @@ class AggregateType : public Type

static void make_one_struct_field(vector<const Type*> &random_fields,
vector<CVQualifiers> &qualifiers,
vector<int> &fields_length);
vector<int> &fields_length,
Filter * additional_filter = NULL);

static void make_one_union_field(vector<const Type*> &fields, vector<CVQualifiers> &qfers, vector<int> &lens);

Expand Down
3 changes: 2 additions & 1 deletion src/ArrayVariable.cpp 100644 → 100755
Expand Up @@ -57,6 +57,7 @@
#include "SafeOpFlags.h"
#include "OutputMgr.h"
#include "StringUtils.h"
#include "TypeConfig.h"

using namespace std;

Expand Down Expand Up @@ -504,7 +505,7 @@ ArrayVariable::OutputDef(std::ostream &out, int indent) const
{
if (collective == 0) {
output_tab(out, indent);
if (!no_loop_initializer() ) {
if (!no_loop_initializer() || TypeConfig::check_exclude_by_request(type, asGlobalInit) && is_global() ) {
// don't print definition for array, rather use a loop initializer
OutputDecl(out);
out << ";";
Expand Down
6 changes: 4 additions & 2 deletions src/BuiltinConfig.cpp
Expand Up @@ -327,7 +327,8 @@ dump_intrin_info(TiXmlNode* pParent, unsigned int indent = 0) {

void BuiltinConfig::build_builtin_list_from_xml(std::string file_path) {
// read XML configuration file
TiXmlDocument builtinDoc(file_path.c_str());
std::string file_name = file_path + "/Builtin.xml";
TiXmlDocument builtinDoc(file_name.c_str());
bool loadOkay = builtinDoc.LoadFile();
if (loadOkay) {
dump_intrin_info(&builtinDoc);
Expand All @@ -337,7 +338,8 @@ void BuiltinConfig::build_builtin_list_from_xml(std::string file_path) {
}

// config all Types CVQualifiers
for (int i = 0; i < BuiltinConfig::builtin_config_list.size(); i++) {
unsigned int i = 0;
for (; i < BuiltinConfig::builtin_config_list.size(); i++) {
Function::make_builtin_function(BuiltinConfig::builtin_config_list[i]);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/CGOptions.cpp
Expand Up @@ -182,7 +182,7 @@ DEFINE_GETTER_SETTER_BOOL(use_comma_exprs);
DEFINE_GETTER_SETTER_BOOL(take_union_field_addr);
DEFINE_GETTER_SETTER_BOOL(vol_struct_union_fields);
DEFINE_GETTER_SETTER_BOOL(lang_cpp);
DEFINE_GETTER_SETTER_STRING_REF(init_builtin_config_filepath);
DEFINE_GETTER_SETTER_STRING_REF(init_config_filepath);

void
CGOptions::set_default_builtin_kinds()
Expand Down Expand Up @@ -292,7 +292,7 @@ CGOptions::set_default_settings(void)

set_default_builtin_kinds();

init_builtin_config_filepath("");
init_config_filepath("");
}

/*
Expand Down
6 changes: 3 additions & 3 deletions src/CGOptions.h
Expand Up @@ -80,8 +80,8 @@ class CGOptions {
static int max_split_files(void);
static int max_split_files(int p);

static std::string init_builtin_config_filepath(void);
static std::string init_builtin_config_filepath(std::string p);
static std::string init_config_filepath(void);
static std::string init_config_filepath(std::string p);

static std::string split_files_dir(void);
static std::string split_files_dir(std::string p);
Expand Down Expand Up @@ -555,7 +555,7 @@ class CGOptions {
// flag to indicate language
static bool lang_cpp_;

static std::string init_builtin_config_filepath_;
static std::string init_config_filepath_;
private:
CGOptions(void);
CGOptions(CGOptions &cgo);
Expand Down
79 changes: 10 additions & 69 deletions src/Constant.cpp
Expand Up @@ -96,65 +96,19 @@ Constant::clone() const

// --------------------------------------------------------------
static string
GenerateRandomCharConstant(void)
GenerateRandomConstant(int size)
{
string ch;
string suffix = "L";
if (size > 4)
suffix = "LL";
if (CGOptions::ccomp() || !CGOptions::longlong())
ch = string("0x") + RandomHexDigits(2);
ch = string("0x") + RandomHexDigits(2 * size);
else
ch = string("0x") + RandomHexDigits(2) + "L";
ch = string("0x") + RandomHexDigits(2 * size) + suffix;
return ch;
}

// --------------------------------------------------------------
static string
GenerateRandomIntConstant(void)
{
string val;
// Int constant - Max 8 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";

return val;
}

// --------------------------------------------------------------
static string
GenerateRandomShortConstant(void)
{
string val;
// Short constant - Max 4 Hex digits on 32-bit platforms
if (CGOptions::ccomp() || !CGOptions::longlong())
val = "0x" + RandomHexDigits( 4 );
else
val = "0x" + RandomHexDigits( 4 ) + "L";

return val;
}

// --------------------------------------------------------------
static string
GenerateRandomLongConstant(void)
{
string val;
// Long constant - Max 8 Hex digits on 32-bit platforms
if (!CGOptions::longlong())
val = "0x" + RandomHexDigits( 8 );
else
val = "0x" + RandomHexDigits( 8 ) + "L";
return val;
}

// --------------------------------------------------------------
static string
GenerateRandomLongLongConstant(void)
{
// Long constant - Max 8 Hex digits on 32-bit platforms
string val = "0x" + RandomHexDigits( 16 ) + "LL";
return val;
}

// --------------------------------------------------------------
#if 0
Expand Down Expand Up @@ -358,23 +312,10 @@ GenerateRandomConstant(const Type* type)
v = oss.str() + (type->is_signed() ? "L" : "UL");
}
} else {
switch (st) {
case eVoid: v = "/* void */"; break;
case eChar: v = GenerateRandomCharConstant(); break;
case eInt: v = GenerateRandomIntConstant(); break;
case eShort: v = GenerateRandomShortConstant(); break;
case eLong: v = GenerateRandomLongConstant(); break;
case eLongLong: v = GenerateRandomLongLongConstant(); break;
case eUChar: v = GenerateRandomCharConstant(); break;
case eUInt: v = GenerateRandomIntConstant(); break;
case eUShort: v = GenerateRandomShortConstant(); break;
case eULong: v = GenerateRandomLongConstant(); break;
case eULongLong: v = GenerateRandomLongLongConstant(); break;
case eFloat: v = GenerateRandomFloatHexConstant(); break;
// case eDouble: v = GenerateRandomFloatConstant(); break;
default:
assert(0 && "Unsupported type!");
}
if(st == eVoid)
v = "/* void */";
else
v = GenerateRandomConstant(Type::get_simple_type(st).SizeInBytes());
}
} else {
assert(0); // no support for types other than integers and structs for now
Expand Down
8 changes: 6 additions & 2 deletions src/Expression.cpp
Expand Up @@ -59,6 +59,7 @@
#include "random.h"
#include "CVQualifiers.h"
#include "Parameter.h"
#include "TypeConfig.h"

int eid = 0;

Expand Down Expand Up @@ -171,7 +172,9 @@ Expression::make_random(CGContext &cg_context, const Type* type, const CVQualifi
VectorFilter filter(&Expression::exprTable_);
if (no_func ||
(!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
(!CGOptions::return_unions() && type->eType == eUnion) ||
TypeConfig::check_exclude_by_request(type, asReturn))
{
filter.add(eFunction);
}
// struct constants can't be subexpressions (union constant can't either?)
Expand Down Expand Up @@ -266,7 +269,8 @@ Expression::make_random_param_value(CGContext &cg_context, const Parameter* para
VectorFilter filter(&Expression::paramTable_);
filter.add(eConstant); // don't call functions with constant parameters because it is not interesting
if ((!CGOptions::return_structs() && type->eType == eStruct) ||
(!CGOptions::return_unions() && type->eType == eUnion)) {
(!CGOptions::return_unions() && type->eType == eUnion) ||
TypeConfig::check_exclude_by_request(type, asReturn)) {
filter.add(eFunction);
}
if (type->is_const_struct_union()) {
Expand Down
5 changes: 4 additions & 1 deletion src/ExpressionFuncall.cpp 100644 → 100755
Expand Up @@ -44,7 +44,7 @@
#include "StringUtils.h"
#include "Block.h"
#include "random.h"

#include "TypeConfig.h"
///////////////////////////////////////////////////////////////////////////////

/*
Expand All @@ -70,6 +70,9 @@ ExpressionFuncall::make_random(CGContext &cg_context, const Type* type, const CV
// unary/binary "functions" produce scalar types only
if (type && (type->eType != eSimple || type->simple_type == eVoid))
std_func = false;
if (type && (TypeConfig::check_exclude_by_request(type, asBinaryExprRv) ||
TypeConfig::check_exclude_by_request(type, asUnaryExprRv)))
std_func = false;

Effect effect_accum = cg_context.get_accum_effect();
Effect effect_stm = cg_context.get_effect_stm();
Expand Down

0 comments on commit aa48386

Please sign in to comment.