From 48fdc39ca13508d2392f9f5e1e15892a04ad59f9 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Wed, 21 Jun 2017 23:57:05 -0700 Subject: [PATCH] Inline Encoding Structs into appropriate AST Nodes --- src/AST/boolean.c | 15 ++++++++++----- src/AST/boolean.h | 2 ++ src/AST/element.c | 18 +++++++++++++++--- src/AST/element.h | 6 +++++- src/Encoders/elementencoding.c | 7 ++----- src/Encoders/elementencoding.h | 4 ++-- src/Encoders/functionencoding.c | 15 +++++---------- src/Encoders/functionencoding.h | 6 +++--- .../{naiveelementencoder.c => naiveencoder.c} | 5 +++-- .../{naiveelementencoder.h => naiveencoder.h} | 0 10 files changed, 47 insertions(+), 31 deletions(-) rename src/Encoders/{naiveelementencoder.c => naiveencoder.c} (88%) rename src/Encoders/{naiveelementencoder.h => naiveencoder.h} (100%) diff --git a/src/AST/boolean.c b/src/AST/boolean.c index 8a47f72..0432b43 100644 --- a/src/AST/boolean.c +++ b/src/AST/boolean.c @@ -32,6 +32,8 @@ Boolean * allocBooleanPredicate(Predicate * predicate, Element ** inputs, uint n for(uint i=0;iencoding, (Boolean *) This); + return & This->base; } @@ -45,11 +47,14 @@ Boolean * allocBooleanLogicArray(CSolver *solver, LogicOp op, Boolean ** array, void deleteBoolean(Boolean * This) { switch(GETBOOLEANTYPE(This)){ - case PREDICATEOP: - deleteInlineArrayElement(& ((BooleanPredicate*)This)->inputs ); - break; - default: - break; + case PREDICATEOP: { + BooleanPredicate *bp=(BooleanPredicate *)This; + deleteInlineArrayElement(& bp->inputs ); + deleteFunctionEncoding(& bp->encoding); + break; + } + default: + break; } deleteVectorArrayBoolean(GETBOOLEANPARENTS(This)); ourfree(This); diff --git a/src/AST/boolean.h b/src/AST/boolean.h index a9d0702..411af26 100644 --- a/src/AST/boolean.h +++ b/src/AST/boolean.h @@ -5,6 +5,7 @@ #include "ops.h" #include "structs.h" #include "astnode.h" +#include "functionencoding.h" /** This is a little sketchy, but apparently legit. @@ -40,6 +41,7 @@ struct BooleanLogic { struct BooleanPredicate { Boolean base; Predicate * predicate; + FunctionEncoding encoding; ArrayElement inputs; }; diff --git a/src/AST/element.c b/src/AST/element.c index 7401173..a94db4b 100644 --- a/src/AST/element.c +++ b/src/AST/element.c @@ -5,8 +5,8 @@ Element *allocElementSet(Set * s) { ElementSet * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet)); GETELEMENTTYPE(tmp)= ELEMSET; tmp->set=s; - tmp->encoding=NULL; allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp)); + initElementEncoding(&tmp->encoding, (Element *) tmp); return &tmp->base; } @@ -19,17 +19,29 @@ Element* allocElementFunction(Function * function, Element ** array, uint numArr allocInlineDefVectorASTNode(GETELEMENTPARENTS(tmp)); for(uint i=0;idomainencoding, (Element *) tmp); + initFunctionEncoding(&tmp->functionencoding, (Element *) tmp); return &tmp->base; } void deleteElement(Element *This) { switch(GETELEMENTTYPE(This)) { - case ELEMFUNCRETURN: - deleteInlineArrayElement(&((ElementFunction *)This)->inputs); + case ELEMFUNCRETURN: { + ElementFunction *ef = (ElementFunction *) This; + deleteInlineArrayElement(&ef->inputs); + deleteElementEncoding(&ef->domainencoding); + deleteFunctionEncoding(&ef->functionencoding); break; + } + case ELEMSET: { + ElementSet *es = (ElementSet *) This; + deleteElementEncoding(&es->encoding); + break; + } default: ; } deleteVectorArrayASTNode(GETELEMENTPARENTS(This)); + ourfree(This); } diff --git a/src/AST/element.h b/src/AST/element.h index 7886e4c..b5bc9c1 100644 --- a/src/AST/element.h +++ b/src/AST/element.h @@ -4,6 +4,8 @@ #include "mymemory.h" #include "structs.h" #include "astnode.h" +#include "functionencoding.h" +#include "elementencoding.h" #define GETELEMENTTYPE(o) GETASTNODETYPE(o) #define GETELEMENTPARENTS(o) (&((Element*)o)->parents) @@ -16,7 +18,7 @@ struct Element { struct ElementSet { Element base; Set * set; - ElementEncoding * encoding; + ElementEncoding encoding; }; struct ElementFunction { @@ -24,6 +26,8 @@ struct ElementFunction { Function * function; ArrayElement inputs; Boolean * overflowstatus; + FunctionEncoding functionencoding; + ElementEncoding domainencoding; }; Element * allocElementSet(Set *s); diff --git a/src/Encoders/elementencoding.c b/src/Encoders/elementencoding.c index 08385db..ecabd1b 100644 --- a/src/Encoders/elementencoding.c +++ b/src/Encoders/elementencoding.c @@ -1,13 +1,11 @@ #include "elementencoding.h" -ElementEncoding * allocElementEncoding(ElementEncodingType type, Element *element) { - ElementEncoding * This=ourmalloc(sizeof(ElementEncoding)); +void initElementEncoding(ElementEncoding * This, Element *element) { This->element=element; - This->type=type; + This->type=ELEM_UNASSIGNED; This->variables=NULL; This->encodingArray=NULL; This->numVars=0; - return This; } void deleteElementEncoding(ElementEncoding *This) { @@ -17,7 +15,6 @@ void deleteElementEncoding(ElementEncoding *This) { ourfree(This->encodingArray); if (This->inUseArray!=NULL) ourfree(This->inUseArray); - ourfree(This); } void allocEncodingArrayElement(ElementEncoding *This, uint size) { diff --git a/src/Encoders/elementencoding.h b/src/Encoders/elementencoding.h index bc173ea..3ade03b 100644 --- a/src/Encoders/elementencoding.h +++ b/src/Encoders/elementencoding.h @@ -3,7 +3,7 @@ #include "classlist.h" enum ElementEncodingType { - ONEHOT, UNARY, BINARYINDEX, ONEHOTBINARY, BINARYVAL + ELEM_UNASSIGNED, ONEHOT, UNARY, BINARYINDEX, ONEHOTBINARY, BINARYVAL }; typedef enum ElementEncodingType ElementEncodingType; @@ -17,7 +17,7 @@ struct ElementEncoding { uint numVars; /* Number of variables */ }; -ElementEncoding * allocElementEncoding(ElementEncodingType type, Element *element); +void initElementEncoding(ElementEncoding *This, Element *element); void deleteElementEncoding(ElementEncoding *This); void baseBinaryIndexElementAssign(ElementEncoding *This); void allocEncodingArrayElement(ElementEncoding *This, uint size); diff --git a/src/Encoders/functionencoding.c b/src/Encoders/functionencoding.c index d0f8c3d..32be4ab 100644 --- a/src/Encoders/functionencoding.c +++ b/src/Encoders/functionencoding.c @@ -1,19 +1,14 @@ #include "functionencoding.h" -FunctionEncoding * allocFunctionEncoding(FunctionEncodingType type, Element *function) { - FunctionEncoding * This=ourmalloc(sizeof(FunctionEncoding)); +void initFunctionEncoding(FunctionEncoding *This, Element *function) { This->op.function=function; - This->type=type; - return This; + This->type=FUNC_UNASSIGNED; } -FunctionEncoding * allocPredicateEncoding(FunctionEncodingType type, Boolean *predicate) { - FunctionEncoding * This=ourmalloc(sizeof(FunctionEncoding)); +void initPredicateEncoding(FunctionEncoding *This, Boolean *predicate) { This->op.predicate=predicate; - This->type=type; - return This; + This->type=FUNC_UNASSIGNED; } -void deleteFunctionEncoding(FunctionEncoding *fe) { - ourfree(fe); +void deleteFunctionEncoding(FunctionEncoding *This) { } diff --git a/src/Encoders/functionencoding.h b/src/Encoders/functionencoding.h index 63583b6..486d5eb 100644 --- a/src/Encoders/functionencoding.h +++ b/src/Encoders/functionencoding.h @@ -3,7 +3,7 @@ #include "classlist.h" enum FunctionEncodingType { - ENUMERATEIMPLICATIONS, CIRCUIT + FUNC_UNASSIGNED, ENUMERATEIMPLICATIONS, CIRCUIT }; typedef enum FunctionEncodingType FunctionEncodingType; @@ -21,7 +21,7 @@ struct FunctionEncoding { ElementPredicate op; }; -FunctionEncoding * allocFunctionEncoding(FunctionEncodingType type, Element *function); -FunctionEncoding * allocPredicateEncoding(FunctionEncodingType type, Boolean *predicate); +void initFunctionEncoding(FunctionEncoding *encoding, Element *function); +void initPredicateEncoding(FunctionEncoding *encoding, Boolean *predicate); void deleteFunctionEncoding(FunctionEncoding *This); #endif diff --git a/src/Encoders/naiveelementencoder.c b/src/Encoders/naiveencoder.c similarity index 88% rename from src/Encoders/naiveelementencoder.c rename to src/Encoders/naiveencoder.c index 242a4e7..ad8a650 100644 --- a/src/Encoders/naiveelementencoder.c +++ b/src/Encoders/naiveencoder.c @@ -1,6 +1,8 @@ -#include "naiveelementencoder.h" +#include "naiveencoder.h" #include "elementencoding.h" #include "element.h" +#include "functionencoding.h" +#include "function.h" #include "set.h" #include "common.h" #include "structs.h" @@ -21,4 +23,3 @@ void baseBinaryIndexElementAssign(ElementEncoding *This) { setInUseElement(This, i); } } - diff --git a/src/Encoders/naiveelementencoder.h b/src/Encoders/naiveencoder.h similarity index 100% rename from src/Encoders/naiveelementencoder.h rename to src/Encoders/naiveencoder.h -- 2.34.1