From: Brian Demsky Date: Fri, 25 Aug 2017 00:46:12 +0000 (-0700) Subject: edits X-Git-Url: http://plrg.eecs.uci.edu/git/?p=satune.git;a=commitdiff_plain;h=1a31b121641ff45d7ec3e272b7a0c476d2cb57e7;hp=-c edits --- 1a31b121641ff45d7ec3e272b7a0c476d2cb57e7 diff --git a/src/AST/astnode.h b/src/AST/astnode.h index 9aaee86..8cc126a 100644 --- a/src/AST/astnode.h +++ b/src/AST/astnode.h @@ -3,9 +3,11 @@ #include "classlist.h" #include "ops.h" -struct ASTNode { +class ASTNode { + public: + ASTNode(ASTNodeType _type) : type(_type) {} ASTNodeType type; + MEMALLOC; }; -#define GETASTNODETYPE(o) (((ASTNode *) o)->type) #endif diff --git a/src/AST/boolean.cc b/src/AST/boolean.cc index 256ab5a..b349c9a 100644 --- a/src/AST/boolean.cc +++ b/src/AST/boolean.cc @@ -4,75 +4,40 @@ #include "element.h" #include "order.h" -Boolean *allocBooleanVar(VarType t) { - BooleanVar *This = (BooleanVar *) ourmalloc(sizeof (BooleanVar)); - GETBOOLEANTYPE(This) = BOOLEANVAR; - GETBOOLEANVALUE(This) = BV_UNDEFINED; - GETBOOLEANPOLARITY(This) = P_UNDEFINED; - This->vtype = t; - This->var = E_NULL; - initDefVectorBoolean(GETBOOLEANPARENTS(This)); - return &This->base; +Boolean::Boolean(ASTNodeType _type) : ASTNode(_type), polarity(P_UNDEFINED), boolVal(BV_UNDEFINED) { + initDefVectorBoolean(GETBOOLEANPARENTS(this)); } -Boolean *allocBooleanOrder(Order *order, uint64_t first, uint64_t second) { - BooleanOrder *This = (BooleanOrder *) ourmalloc(sizeof (BooleanOrder)); - GETBOOLEANTYPE(This) = ORDERCONST; - GETBOOLEANVALUE(This) = BV_UNDEFINED; - GETBOOLEANPOLARITY(This) = P_UNDEFINED; - This->order = order; - This->first = first; - This->second = second; - pushVectorBooleanOrder(&order->constraints, This); - initDefVectorBoolean(GETBOOLEANPARENTS(This)); - return &This->base; +BooleanVar::BooleanVar(VarType t) : Boolean(BOOLEANVAR), vtype(t), var(E_NULL) { } -Boolean *allocBooleanPredicate(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) { - BooleanPredicate *This = (BooleanPredicate *) ourmalloc(sizeof(BooleanPredicate)); - GETBOOLEANTYPE(This) = PREDICATEOP; - GETBOOLEANVALUE(This) = BV_UNDEFINED; - GETBOOLEANPOLARITY(This) = P_UNDEFINED; - This->predicate = predicate; - initArrayInitElement(&This->inputs, inputs, numInputs); - initDefVectorBoolean(GETBOOLEANPARENTS(This)); +BooleanOrder::BooleanOrder(Order *_order, uint64_t _first, uint64_t _second) : Boolean(ORDERCONST), order(_order), first(_first), second(_second) { + pushVectorBooleanOrder(&order->constraints, this); +} + +BooleanPredicate::BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, Boolean *_undefinedStatus) : Boolean(PREDICATEOP), predicate(_predicate), undefStatus(_undefinedStatus) { + initArrayInitElement(&inputs, _inputs, _numInputs); - for (uint i = 0; i < numInputs; i++) { - pushVectorASTNode(GETELEMENTPARENTS(inputs[i]), (ASTNode *)This); + for (uint i = 0; i < _numInputs; i++) { + pushVectorASTNode(GETELEMENTPARENTS(_inputs[i]), this); } - initPredicateEncoding(&This->encoding, (Boolean *) This); - This->undefStatus = undefinedStatus; - return &This->base; + initPredicateEncoding(&encoding, this); } -Boolean *allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean **array, uint asize) { - BooleanLogic *This = (BooleanLogic *) ourmalloc(sizeof(BooleanLogic)); - GETBOOLEANTYPE(This) = LOGICOP; - GETBOOLEANVALUE(This) = BV_UNDEFINED; - GETBOOLEANPOLARITY(This) = P_UNDEFINED; - This->op = op; - initDefVectorBoolean(GETBOOLEANPARENTS(This)); - initArrayInitBoolean(&This->inputs, array, asize); - pushVectorBoolean(solver->allBooleans, (Boolean *) This); - return &This->base; +BooleanLogic::BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint asize) : Boolean(LOGICOP), op(_op) { + initArrayInitBoolean(&inputs, array, asize); + pushVectorBoolean(solver->allBooleans, (Boolean *) this); } -void deleteBoolean(Boolean *This) { - switch (GETBOOLEANTYPE(This)) { - case PREDICATEOP: { - BooleanPredicate *bp = (BooleanPredicate *)This; - deleteInlineArrayElement(&bp->inputs ); - deleteFunctionEncoding(&bp->encoding); - break; - } - case LOGICOP: { - BooleanLogic *bl = (BooleanLogic *) This; - deleteInlineArrayBoolean(&bl->inputs); - break; - } - default: - break; - } - deleteVectorArrayBoolean(GETBOOLEANPARENTS(This)); - ourfree(This); +Boolean::~Boolean() { + deleteVectorArrayBoolean(GETBOOLEANPARENTS(this)); +} + +BooleanPredicate::~BooleanPredicate() { + deleteInlineArrayElement(&inputs ); + deleteFunctionEncoding(&encoding); +} + +BooleanLogic::~BooleanLogic() { + deleteInlineArrayBoolean(&inputs); } diff --git a/src/AST/boolean.h b/src/AST/boolean.h index cdc05a6..db1bd7d 100644 --- a/src/AST/boolean.h +++ b/src/AST/boolean.h @@ -12,45 +12,61 @@ This is a little sketchy, but apparently legit. https://www.python.org/dev/peps/pep-3123/ */ -#define GETBOOLEANTYPE(o) GETASTNODETYPE(o) -#define GETBOOLEANPARENTS(o) (&((Boolean *)(o))->parents) -#define GETBOOLEANPOLARITY(b) (((Boolean *)b)->polarity) -#define GETBOOLEANVALUE(b) (((Boolean *)b)->boolVal) +#define GETBOOLEANTYPE(o) (o->type) +#define GETBOOLEANPARENTS(o) (&(o->parents)) +#define GETBOOLEANPOLARITY(b) (b->polarity) +#define GETBOOLEANVALUE(b) (b->boolVal) -struct Boolean { - ASTNode base; +class Boolean : public ASTNode { + public: + Boolean(ASTNodeType _type); Polarity polarity; BooleanValue boolVal; VectorBoolean parents; + ~Boolean(); + MEMALLOC; }; -struct BooleanOrder { - Boolean base; - Order *order; - uint64_t first; - uint64_t second; -}; - -struct BooleanVar { - Boolean base; +class BooleanVar : public Boolean { + public: + BooleanVar(VarType t); VarType vtype; Edge var; + MEMALLOC; }; -struct BooleanLogic { - Boolean base; - LogicOp op; - ArrayBoolean inputs; +class BooleanOrder : public Boolean { + public: + BooleanOrder(Order *_order, uint64_t _first, uint64_t _second); + Order *order; + uint64_t first; + uint64_t second; + MEMALLOC; }; -struct BooleanPredicate { - Boolean base; +class BooleanPredicate : public Boolean { + public: + BooleanPredicate(Predicate *_predicate, Element **_inputs, uint _numInputs, Boolean *_undefinedStatus); + ~BooleanPredicate(); Predicate *predicate; FunctionEncoding encoding; ArrayElement inputs; Boolean *undefStatus; + MEMALLOC; +}; + + +class BooleanLogic : public Boolean { + public: + BooleanLogic(CSolver *solver, LogicOp _op, Boolean **array, uint asize); + ~BooleanLogic(); + LogicOp op; + ArrayBoolean inputs; + MEMALLOC; }; + + Boolean *allocBooleanVar(VarType t); Boolean *allocBooleanOrder(Order *order, uint64_t first, uint64_t second); Boolean *allocBooleanPredicate(Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus); diff --git a/src/AST/element.cc b/src/AST/element.cc index 91551e5..3b0f6e4 100644 --- a/src/AST/element.cc +++ b/src/AST/element.cc @@ -5,39 +5,24 @@ #include "function.h" #include "table.h" -Element *allocElementSet(Set *s) { - ElementSet *This = (ElementSet *)ourmalloc(sizeof(ElementSet)); - GETELEMENTTYPE(This) = ELEMSET; - This->set = s; - initDefVectorASTNode(GETELEMENTPARENTS(This)); - initElementEncoding(&This->encoding, (Element *) This); - return &This->base; +Element::Element(ASTNodeType _type) : ASTNode(_type) { + initDefVectorASTNode(GETELEMENTPARENTS(this)); + initElementEncoding(&encoding, (Element *) this); } -Element *allocElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus) { - ElementFunction *This = (ElementFunction *) ourmalloc(sizeof(ElementFunction)); - GETELEMENTTYPE(This) = ELEMFUNCRETURN; - This->function = function; - ASSERT(GETBOOLEANTYPE(overflowstatus) == BOOLEANVAR); - This->overflowstatus = overflowstatus; - initArrayInitElement(&This->inputs, array, numArrays); - initDefVectorASTNode(GETELEMENTPARENTS(This)); +ElementSet::ElementSet(Set *s) : Element(ELEMSET), set(s) { +} + +ElementFunction::ElementFunction(Function *_function, Element **array, uint numArrays, Boolean *_overflowstatus) : Element(ELEMFUNCRETURN), function(_function), overflowstatus(_overflowstatus) { + initArrayInitElement(&inputs, array, numArrays); for (uint i = 0; i < numArrays; i++) - pushVectorASTNode(GETELEMENTPARENTS(array[i]), (ASTNode *) This); - initElementEncoding(&This->rangeencoding, (Element *) This); - initFunctionEncoding(&This->functionencoding, (Element *) This); - return &This->base; + pushVectorASTNode(GETELEMENTPARENTS(array[i]), this); + initFunctionEncoding(&functionencoding, this); } -Element *allocElementConst(uint64_t value, VarType type) { - ElementConst *This = (ElementConst *)ourmalloc(sizeof(ElementConst)); - GETELEMENTTYPE(This) = ELEMCONST; - This->value = value; +ElementConst::ElementConst(uint64_t _value, VarType _type) : Element(ELEMCONST), value(_value) { uint64_t array[]={value}; - This->set = allocSet(type, array, 1); - initDefVectorASTNode(GETELEMENTPARENTS(This)); - initElementEncoding(&This->encoding, (Element *) This); - return &This->base; + set = allocSet(_type, array, 1); } Set *getElementSet(Element *This) { @@ -64,29 +49,16 @@ Set *getElementSet(Element *This) { return NULL; } -void deleteElement(Element *This) { - switch (GETELEMENTTYPE(This)) { - case ELEMFUNCRETURN: { - ElementFunction *ef = (ElementFunction *) This; - deleteInlineArrayElement(&ef->inputs); - deleteElementEncoding(&ef->rangeencoding); - deleteFunctionEncoding(&ef->functionencoding); - break; - } - case ELEMSET: { - ElementSet *es = (ElementSet *) This; - deleteElementEncoding(&es->encoding); - break; - } - case ELEMCONST: { - ElementConst *ec = (ElementConst *) This; - deleteSet(ec->set);//Client did not create, so we free it - deleteElementEncoding(&ec->encoding); - break; - } - default: - ASSERT(0); - } - deleteVectorArrayASTNode(GETELEMENTPARENTS(This)); - ourfree(This); +ElementFunction::~ElementFunction() { + deleteInlineArrayElement(&inputs); + deleteFunctionEncoding(&functionencoding); +} + +ElementConst::~ElementConst() { + deleteSet(set); +} + +Element::~Element() { + deleteElementEncoding(&encoding); + deleteVectorArrayASTNode(GETELEMENTPARENTS(this)); } diff --git a/src/AST/element.h b/src/AST/element.h index 8640a47..df3cf62 100644 --- a/src/AST/element.h +++ b/src/AST/element.h @@ -8,53 +8,48 @@ #include "elementencoding.h" #include "boolean.h" -#define GETELEMENTTYPE(o) GETASTNODETYPE(o) +#define GETELEMENTTYPE(o) (o->type) #define GETELEMENTPARENTS(o) (&((Element *)o)->parents) -struct Element { - ASTNode base; +class Element : public ASTNode { + public: + Element(ASTNodeType type); + ~Element(); VectorASTNode parents; + ElementEncoding encoding; + MEMALLOC; }; -struct ElementConst { - Element base; +class ElementConst : public Element { + public: + ElementConst(uint64_t value, VarType type); + ~ElementConst(); Set *set; uint64_t value; - ElementEncoding encoding; + MEMALLOC; }; -struct ElementSet { - Element base; +class ElementSet : public Element { + public: + ElementSet(Set *s); Set *set; - ElementEncoding encoding; + MEMALLOC; }; -struct ElementFunction { - Element base; +class ElementFunction : public Element { + public: + ElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus); + ~ElementFunction(); Function *function; ArrayElement inputs; Boolean *overflowstatus; FunctionEncoding functionencoding; - ElementEncoding rangeencoding; + MEMALLOC; }; -Element *allocElementConst(uint64_t value, VarType type); -Element *allocElementSet(Set *s); -Element *allocElementFunction(Function *function, Element **array, uint numArrays, Boolean *overflowstatus); -void deleteElement(Element *This); Set *getElementSet(Element *This); -static inline ElementEncoding *getElementEncoding(Element *This) { - switch (GETELEMENTTYPE(This)) { - case ELEMSET: - return &((ElementSet *)This)->encoding; - case ELEMFUNCRETURN: - return &((ElementFunction *)This)->rangeencoding; - case ELEMCONST: - return &((ElementConst *)This)->encoding; - default: - ASSERT(0); - } - return NULL; +static inline ElementEncoding *getElementEncoding(Element *e) { + return &e->encoding; } static inline FunctionEncoding *getElementFunctionEncoding(ElementFunction *func) { diff --git a/src/Backend/satfuncopencoder.cc b/src/Backend/satfuncopencoder.cc index 8dd7532..8e66c9b 100644 --- a/src/Backend/satfuncopencoder.cc +++ b/src/Backend/satfuncopencoder.cc @@ -129,7 +129,7 @@ void encodeOperatorElementFunctionSATEncoder(SATEncoder *This, ElementFunction * carray[i] = getElementValueConstraint(This, elem, vals[i]); } if (isInRange) { - carray[numDomains] = getElementValueConstraint(This, &func->base, result); + carray[numDomains] = getElementValueConstraint(This, func, result); } Edge clause; diff --git a/src/classlist.h b/src/classlist.h index 7e904e8..8baae7a 100644 --- a/src/classlist.h +++ b/src/classlist.h @@ -21,16 +21,12 @@ typedef struct CSolver CSolver; struct SATEncoder; typedef struct SATEncoder SATEncoder; -typedef struct BooleanOrder BooleanOrder; -typedef struct BooleanVar BooleanVar; -typedef struct BooleanLogic BooleanLogic; -typedef struct BooleanPredicate BooleanPredicate; - -struct ASTNode; -typedef struct ASTNode ASTNode; - -struct Boolean; -typedef struct Boolean Boolean; +class Boolean; +class BooleanOrder; +class BooleanVar; +class BooleanLogic; +class BooleanPredicate; +class ASTNode; struct IncrementalSolver; typedef struct IncrementalSolver IncrementalSolver; @@ -39,12 +35,10 @@ struct Set; typedef struct Set Set; typedef struct Set MutableSet; -typedef struct ElementFunction ElementFunction; -typedef struct ElementSet ElementSet; -typedef struct ElementConst ElementConst; - -struct Element; -typedef struct Element Element; +class ElementFunction; +class ElementSet; +class ElementConst; +class Element; typedef struct FunctionOperator FunctionOperator; typedef struct FunctionTable FunctionTable; diff --git a/src/csolver.cc b/src/csolver.cc index b4c8208..51d8c7f 100644 --- a/src/csolver.cc +++ b/src/csolver.cc @@ -48,7 +48,7 @@ void deleteSolver(CSolver *This) { size = getSizeVectorElement(This->allElements); for (uint i = 0; i < size; i++) { - deleteElement(getVectorElement(This->allElements, i)); + delete getVectorElement(This->allElements, i); } deleteVectorElement(This->allElements); @@ -109,13 +109,13 @@ uint64_t createUniqueItem(CSolver *This, MutableSet *set) { } Element *getElementVar(CSolver *This, Set *set) { - Element *element = allocElementSet(set); + Element *element = new ElementSet(set); pushVectorElement(This->allElements, element); return element; } Element *getElementConst(CSolver *This, VarType type, uint64_t value) { - Element *element = allocElementConst(value, type); + Element *element = new ElementConst(value, type); pushVectorElement(This->allElements, element); return element; } @@ -165,7 +165,7 @@ Function *completeTable(CSolver *This, Table *table, UndefinedBehavior behavior) } Element *applyFunction(CSolver *This, Function *function, Element **array, uint numArrays, Boolean *overflowstatus) { - Element *element = allocElementFunction(function,array,numArrays,overflowstatus); + Element *element = new ElementFunction(function,array,numArrays,overflowstatus); pushVectorElement(This->allElements, element); return element; } @@ -174,13 +174,13 @@ Boolean *applyPredicate(CSolver *This, Predicate *predicate, Element **inputs, u return applyPredicateTable(This, predicate, inputs, numInputs, NULL); } Boolean *applyPredicateTable(CSolver *This, Predicate *predicate, Element **inputs, uint numInputs, Boolean *undefinedStatus) { - Boolean *boolean = allocBooleanPredicate(predicate, inputs, numInputs, undefinedStatus); + Boolean *boolean = new BooleanPredicate(predicate, inputs, numInputs, undefinedStatus); pushVectorBoolean(This->allBooleans, boolean); return boolean; } Boolean *applyLogicalOperation(CSolver *This, LogicOp op, Boolean **array, uint asize) { - return allocBooleanLogicArray(This, op, array, asize); + return new BooleanLogic(This, op, array, asize); } void addConstraint(CSolver *This, Boolean *constraint) { @@ -194,7 +194,7 @@ Order *createOrder(CSolver *This, OrderType type, Set *set) { } Boolean *orderConstraint(CSolver *This, Order *order, uint64_t first, uint64_t second) { - Boolean *constraint = allocBooleanOrder(order, first, second); + Boolean *constraint = new BooleanOrder(order, first, second); pushVectorBoolean(This->allBooleans,constraint); return constraint; }