Skip to content

Commit

Permalink
add Comma operators and embedded assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuejun Yang committed Jul 11, 2011
1 parent cc2cae0 commit da55019
Show file tree
Hide file tree
Showing 7 changed files with 469 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Expression.h
Expand Up @@ -138,9 +138,9 @@ class Expression

static void record_dereference_level(int level);

virtual bool compatible(const Expression *) const = 0;
virtual bool compatible(const Expression *) const { return false;}

virtual bool compatible(const Variable *) const = 0;
virtual bool compatible(const Variable *) const { return false;}

enum eTermType term_type;
int expr_id;
Expand Down
146 changes: 146 additions & 0 deletions src/ExpressionAssign.cpp
@@ -0,0 +1,146 @@
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "ExpressionAssign.h"
#include <cassert>
#include "Common.h"
#include "CGContext.h"
#include "CGOptions.h"
#include "FactMgr.h"
#include "Bookkeeper.h"
#include "StringUtils.h"
#include "Block.h"

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

/*
*
*/
Expression *
ExpressionAssign::make_random(CGContext &cg_context, const Type* type, const CVQualifiers* qfer)
{
Expression *e = 0;
//++cg_context.stmt_depth;
//bool std_func = ExpressionFunctionProbability(cg_context);
//ERROR_GUARD(NULL);
// // unary/binary "functions" produce scalar types only
//if (type && (type->eType != eSimple || type->simple_type == eVoid))
// std_func = false;

//Effect effect_accum = cg_context.get_accum_effect();
//Effect effect_stm = cg_context.get_effect_stm();
//FactMgr* fm = get_fact_mgr(&cg_context);
//vector<const Fact*> facts_copy = fm->global_facts;
//FunctionInvocation *fi = FunctionInvocation::make_random(std_func, cg_context, type, qfer);
//ERROR_GUARD(NULL);

//if (fi->failed) {
// // if it's a invalid invocation, (see FunctionInvocationUser::revisit)
// // restore the env, and replace invocation with a simple var
// cg_context.reset_effect_accum(effect_accum);
// cg_context.reset_effect_stm(effect_stm);
// fm->restore_facts(facts_copy);
// e = ExpressionVariable::make_random(cg_context, type, qfer);
// delete fi;
//}
//else {
// e = new ExpressionAssign(*fi);
//}
//--cg_context.stmt_depth;
return e;
}

/*
*
*/
ExpressionAssign::~ExpressionAssign(void)
{
delete &assign;
}

CVQualifiers
ExpressionAssign::get_qualifiers(void) const
{
return assign.get_lhs()->get_qualifiers();
}

/*
* return if a variable is referenced in this expression
*/
bool
ExpressionAssign::use_var(const Variable* v) const
{
if (assign.get_lhs()->use_var(v) || assign.get_expr()->use_var(v)) {
return true;
}
return false;
}

bool
ExpressionAssign::equals(int num) const
{
return assign.is_simple_assign() && assign.get_expr()->equals(num);
}

bool
ExpressionAssign::is_0_or_1(void) const
{
return assign.is_simple_assign() && assign.get_expr()->is_0_or_1();
}

bool
ExpressionAssign::visit_facts(vector<const Fact*>& inputs, CGContext& cg_context) const
{
return assign.visit_facts(inputs, cg_context);
}

void
ExpressionAssign::Output(std::ostream &out) const
{
Reducer* reducer = CGOptions::get_reducer();
if (reducer && reducer->output_expr(this, out)) {
return;
}
assign.Output(out, 0);
}

void
ExpressionAssign::indented_output(std::ostream &out, int indent) const
{
assign.Output(out, 0, indent);
}

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

// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:

// End of file.
88 changes: 88 additions & 0 deletions src/ExpressionAssign.h
@@ -0,0 +1,88 @@
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef EXPRESSION_ASSIGN_H
#define EXPRESSION_ASSIGN_H

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

#include <ostream>
#include "Expression.h"
#include "StatementAssign.h"

class CGContext;
class ExpressionVariable;

/*
*
*/
class ExpressionAssign : public Expression
{
public:
// Factory method.
static Expression *make_random(CGContext &cg_context, const Type* type, const CVQualifiers* qfer=0);

virtual ~ExpressionAssign(void);

virtual CVQualifiers get_qualifiers(void) const;

virtual const Type &get_type(void) const { return assign.get_lhs()->get_type();}

virtual void get_called_funcs(std::vector<const FunctionInvocationUser*>& funcs) const { assign.get_called_funcs(funcs);}

virtual bool visit_facts(vector<const Fact*>& inputs, CGContext& cg_context) const;

virtual bool has_uncertain_call_recursive(void) const { return assign.has_uncertain_call_recursive();}

virtual bool use_var(const Variable* v) const;

virtual bool equals(int num) const;
virtual bool is_0_or_1(void) const;

virtual std::vector<const ExpressionVariable*> get_dereferenced_ptrs(void) const { return assign.get_dereferenced_ptrs();}
virtual void get_referenced_ptrs(std::vector<const Variable*>& ptrs) const { assign.get_referenced_ptrs(ptrs);}

void Output(std::ostream &) const;
virtual void indented_output(std::ostream &out, int indent) const;

private:
const StatementAssign& assign;
explicit ExpressionAssign(const ExpressionAssign &efun);
};

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

#endif // EXPRESSION_ASSIGN_H

// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:

// End of file.
131 changes: 131 additions & 0 deletions src/ExpressionComma.cpp
@@ -0,0 +1,131 @@
// -*- mode: C++ -*-
//
// Copyright (c) 2007, 2008, 2010, 2011 The University of Utah
// All rights reserved.
//
// This file is part of `csmith', a random generator of C programs.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#include "ExpressionComma.h"
#include <cassert>
#include "Common.h"
#include "CGContext.h"
#include "CGOptions.h"
#include "FactMgr.h"
#include "Bookkeeper.h"
#include "StringUtils.h"
#include "Block.h"

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

/*
*
*/
Expression *
ExpressionComma::make_random(CGContext &cg_context, const Type* type, const CVQualifiers* qfer)
{
Expression *e = 0;
//++cg_context.stmt_depth;
//bool std_func = ExpressionFunctionProbability(cg_context);
//ERROR_GUARD(NULL);
// // unary/binary "functions" produce scalar types only
//if (type && (type->eType != eSimple || type->simple_type == eVoid))
// std_func = false;

//Effect effect_accum = cg_context.get_accum_effect();
//Effect effect_stm = cg_context.get_effect_stm();
//FactMgr* fm = get_fact_mgr(&cg_context);
//vector<const Fact*> facts_copy = fm->global_facts;
//FunctionInvocation *fi = FunctionInvocation::make_random(std_func, cg_context, type, qfer);
//ERROR_GUARD(NULL);

//if (fi->failed) {
// // if it's a invalid invocation, (see FunctionInvocationUser::revisit)
// // restore the env, and replace invocation with a simple var
// cg_context.reset_effect_accum(effect_accum);
// cg_context.reset_effect_stm(effect_stm);
// fm->restore_facts(facts_copy);
// e = ExpressionVariable::make_random(cg_context, type, qfer);
// delete fi;
//}
//else {
// e = new ExpressionComma(*fi);
//}
//--cg_context.stmt_depth;
return e;
}

/*
*
*/
ExpressionComma::~ExpressionComma(void)
{
delete &lhs;
delete &rhs;
}

bool
ExpressionComma::visit_facts(vector<const Fact*>& inputs, CGContext& cg_context) const
{
if (!lhs.visit_facts(inputs, cg_context)) {
return false;
}
return rhs.visit_facts(inputs, cg_context);
}

vector<const ExpressionVariable*>
ExpressionComma::get_dereferenced_ptrs(void) const
{
vector<const ExpressionVariable*> ptrs1 = lhs.get_dereferenced_ptrs();
vector<const ExpressionVariable*> ptrs2 = rhs.get_dereferenced_ptrs();
ptrs1.insert(ptrs1.end(), ptrs2.begin(), ptrs2.end());
return ptrs1;
}

void
ExpressionComma::Output(std::ostream &out) const
{
Reducer* reducer = CGOptions::get_reducer();
if (reducer && reducer->output_expr(this, out)) {
return;
}
lhs.Output(out);
rhs.Output(out);
}

void
ExpressionComma::indented_output(std::ostream &out, int indent) const
{
output_tab(out, indent);
Output(out);
}

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

// Local Variables:
// c-basic-offset: 4
// tab-width: 4
// End:

// End of file.

0 comments on commit da55019

Please sign in to comment.