#include "element.h"
#include "structs.h"
+#include "set.h"
+#include "constraint.h"
Element *allocElementSet(Set * s) {
ElementSet * tmp=(ElementSet *)ourmalloc(sizeof(ElementSet));
return &tmp->base;
}
+uint getElementSize(Element* This){
+ switch(GETELEMENTTYPE(This)){
+ case ELEMSET:
+ return getSetSize( ((ElementSet*)This)->set );
+ break;
+ case ELEMFUNCRETURN:
+ ASSERT(0);
+ default:
+ ASSERT(0);
+ }
+ return -1;
+}
+
+
+Constraint * getElementValueConstraint(Element* This, uint64_t value) {
+ switch(GETELEMENTTYPE(This)){
+ case ELEMSET:
+ ; //Statement is needed for a label and This is a NOPE
+ ElementSet* elemSet= ((ElementSet*)This);
+ uint size = getSetSize(elemSet->set);
+ for(uint i=0; i<size; i++){
+ if( GETELEMENTENCODING(elemSet)->encodingArray[i]==value){
+ return generateBinaryConstraint(GETELEMENTENCODING(elemSet)->numVars,
+ GETELEMENTENCODING(elemSet)->variables, i);
+ }
+ }
+ break;
+ case ELEMFUNCRETURN:
+ break;
+ default:
+ ASSERT(0);
+ }
+ ASSERT(0);
+ return NULL;
+}
+
+Constraint * generateBinaryConstraint(uint numvars, Constraint ** vars, uint value) {
+ Constraint *carray[numvars];
+ for(uint j=0;j<numvars;j++) {
+ carray[j]=((value&1)==1) ? vars[j] : negateConstraint(vars[j]);
+ value=value>>1;
+ }
+
+ return allocArrayConstraint(AND, numvars, carray);
+}
+
void deleteElement(Element *This) {
switch(GETELEMENTTYPE(This)) {
case ELEMFUNCRETURN: {
#include "astnode.h"
#include "functionencoding.h"
#include "elementencoding.h"
+#include "boolean.h"
#define GETELEMENTTYPE(o) GETASTNODETYPE(o)
#define GETELEMENTPARENTS(o) (&((Element*)o)->parents)
-
+#define GETELEMENTENCODING(e) (GETELEMENTTYPE(e)==ELEMSET? \
+ &((ElementSet*)e)->encoding: \
+ GETELEMENTTYPE(e)==ELEMFUNCRETURN? \
+ &((ElementFunction*)e)->domainencoding: NULL)
+// Should be called on the element or boolean
+#define GETFUNCTIONENCODING(f) (GETASTNODETYPE(f) == ELEMFUNCRETURN? \
+ &((ElementFunction*)f)->functionencoding: \
+ GETASTNODETYPE(f) == PREDICATEOP? \
+ &((BooleanPredicate*)f)->encoding: NULL)
+
struct Element {
ASTNode base;
VectorASTNode parents;
Element * allocElementSet(Set *s);
Element* allocElementFunction(Function * function, Element ** array, uint numArrays, Boolean * overflowstatus);
void deleteElement(Element *This);
+
+uint getElementSize(Element* This);
+Constraint * getElementValueConstraint(Element* This, uint64_t value);
#endif
return tmp;
}
+uint getSetSize(Set* set){
+ if(set->isRange){
+ return set->high- set->low+1;
+ }else{
+ return getSizeVectorInt(set->members);
+ }
+}
+
void deleteSet(Set * set) {
if (!set->isRange)
deleteVectorInt(set->members);
Set *allocSet(VarType t, uint64_t * elements, uint num);
Set * allocSetRange(VarType t, uint64_t lowrange, uint64_t highrange);
+uint getSetSize(Set* set);
void deleteSet(Set *set);
#endif/* SET_H */
#include "structs.h"
#include "tableentry.h"
#include "set.h"
+#include "mutableset.h"
-Table * allocTable(Set **domains, uint numDomain, Set * range){
- Table* table = (Table*) ourmalloc(sizeof(Table));
- allocInlineArrayInitSet(&table->domains, domains, numDomain);
- allocInlineDefVectorTableEntry(&table->entries);
- table->range =range;
- return table;
+Table * allocTable(Element **domains, uint numDomain, Element * range){
+ Table* table = (Table*) ourmalloc(sizeof(Table));
+ allocInlineArrayInitElement(&table->domains, domains, numDomain);
+ allocInlineDefVectorTableEntry(&table->entries);
+ table->range =range;
+ return table;
}
void addNewTableEntry(Table* table, uint64_t* inputs, uint inputSize, uint64_t result){
- ASSERT(getSizeVectorSet( table->domains) == inputSize);
- pushVectorTableEntry(&table->entries, allocTableEntry(inputs, inputSize, result));
+ ASSERT(getSizeArrayElement( &table->domains) == inputSize);
+ pushVectorTableEntry(&table->entries, allocTableEntry(inputs, inputSize, result));
}
void deleteTable(Table* table){
- deleteInlineArraySet(&table->domains);
+ deleteInlineArrayElement(&table->domains);
uint size = getSizeVectorTableEntry(&table->entries);
for(uint i=0; i<size; i++){
deleteTableEntry(getVectorTableEntry(&table->entries, i));
#include "structs.h"
struct Table {
- ArraySet domains;
- Set * range;
+ ArrayElement domains;
+ Element * range;
VectorTableEntry entries;
};
-Table * allocTable(Set **domains, uint numDomain, Set * range);
+Table * allocTable(Element **domains, uint numDomain, Element * range);
void addNewTableEntry(Table* table, uint64_t* inputs, uint inputSize, uint64_t result);
void deleteTable(Table* table);
#endif
}
}
-Constraint * generateConstraint(uint numvars, Constraint ** vars, uint value) {
- Constraint *carray[numvars];
- for(uint j=0;j<numvars;j++) {
- carray[j]=((value&1)==1) ? vars[j] : negateConstraint(vars[j]);
- value=value>>1;
- }
-
- return allocArrayConstraint(AND, numvars, carray);
-}
-
/** Generates a constraint to ensure that all encodings are less than value */
Constraint * generateLTConstraint(uint numvars, Constraint ** vars, uint value) {
Constraint *orarray[numvars];
extern Constraint ctrue;
extern Constraint cfalse;
-Constraint * generateConstraint(uint numvars, Constraint ** vars, uint value);
+Constraint * generateBinaryConstraint(uint numvars, Constraint ** vars, uint value);
Constraint * generateLTConstraint(uint numvars, Constraint ** vars, uint value);
Constraint * generateEquivNVConstraint(uint numvars, Constraint **var1, Constraint **var2);
Constraint * generateEquivConstraint(Constraint *var1, Constraint *var2);
#include "elementencoding.h"
+#include "common.h"
+#include "naiveencoder.h"
+#include "element.h"
void initElementEncoding(ElementEncoding * This, Element *element) {
This->element=element;
This->inUseArray=ourcalloc(1, size >> 6);
}
+void allocElementConstraintVariables(ElementEncoding* This, uint numVars){
+ This->numVars = numVars;
+ This->variables = ourmalloc(sizeof(Constraint*) * numVars);
+}
+
void setElementEncodingType(ElementEncoding* This, ElementEncodingType type){
This->type = type;
}
+
+void generateBinaryIndexEncodingVars(NaiveEncoder* encoder, ElementEncoding* This){
+ ASSERT(This->type==BINARYINDEX);
+ uint size = getElementSize(This->element);
+ allocElementConstraintVariables(This, NUMBITS(size-1));
+ getArrayNewVars(encoder, This->numVars, This->variables);
+}
+
+void generateElementEncodingVariables(NaiveEncoder* encoder, ElementEncoding* This){
+ ASSERT(This->type!=ELEM_UNASSIGNED);
+ ASSERT(This->variables==NULL);
+ switch(This->type){
+ case BINARYINDEX:
+ generateBinaryIndexEncodingVars(encoder, This);
+ break;
+ default:
+ ASSERT(0);
+ }
+}
\ No newline at end of file
#ifndef ELEMENTENCODING_H
#define ELEMENTENCODING_H
#include "classlist.h"
+#include "naiveencoder.h"
enum ElementEncodingType {
ELEM_UNASSIGNED, ONEHOT, UNARY, BINARYINDEX, ONEHOTBINARY, BINARYVAL
static inline void setInUseElement(ElementEncoding *This, uint offset) {
This->inUseArray[(offset>>6)] |= 1 << (offset & 63);
}
+
+void generateBinaryIndexEncodingVars(NaiveEncoder* encode, ElementEncoding* This);
+void generateElementEncodingVariables(NaiveEncoder* encoder, ElementEncoding* This);
+
#endif
#include "csolver.h"
#include "boolean.h"
#include "table.h"
+#include "tableentry.h"
+#include "constraint.h"
#include <strings.h>
-void naiveEncodingDecision(CSolver* csolver){
+NaiveEncoder* allocNaiveEncoder(){
+ NaiveEncoder* encoder = (NaiveEncoder*) ourmalloc(sizeof(NaiveEncoder));
+ allocInlineDefVectorConstraint(GETNAIVEENCODERALLCONSTRAINTS(encoder));
+ allocInlineDefVectorConstraint(GETNAIVEENCODERVARS(encoder));
+ encoder->varindex=0;
+ return encoder;
+}
+
+void naiveEncodingDecision(CSolver* csolver, NaiveEncoder* encoder){
uint size = getSizeVectorElement(csolver->allElements);
for(uint i=0; i<size; i++){
Element* element = getVectorElement(csolver->allElements, i);
switch(GETELEMENTTYPE(element)){
case ELEMSET:
- setElementEncodingType(&((ElementSet*)element)->encoding, BINARYINDEX);
- baseBinaryIndexElementAssign(&((ElementSet*)element)->encoding);
+ setElementEncodingType(GETELEMENTENCODING(element), BINARYINDEX);
+ baseBinaryIndexElementAssign(GETELEMENTENCODING(element));
+ generateElementEncodingVariables(encoder,GETELEMENTENCODING(element));
break;
case ELEMFUNCRETURN:
- setFunctionEncodingType(&((ElementFunction*)element)->functionencoding, ENUMERATEIMPLICATIONS);
+ setFunctionEncodingType(GETFUNCTIONENCODING(element), ENUMERATEIMPLICATIONS);
break;
default:
ASSERT(0);
Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
switch(GETBOOLEANTYPE(predicate)){
case PREDICATEOP:
- setFunctionEncodingType(&((BooleanPredicate*)predicate)->encoding, ENUMERATEIMPLICATIONS);
+ setFunctionEncodingType(GETFUNCTIONENCODING(predicate), ENUMERATEIMPLICATIONS);
break;
default:
continue;
}
}
+
+void getArrayNewVars(NaiveEncoder* encoder, uint num, Constraint **carray) {
+ for(uint i=0;i<num;i++)
+ carray[i]=getNewVar(encoder);
+}
+
+Constraint * getNewVar(NaiveEncoder* encoder) {
+ Constraint* var = allocVarConstraint(VAR, encoder->varindex);
+ Constraint* notVar = allocVarConstraint(NOTVAR, encoder->varindex);
+ setNegConstraint(var, notVar);
+ setNegConstraint(notVar, var);
+ pushVectorConstraint(GETNAIVEENCODERVARS(encoder), var);
+ encoder->varindex++;
+ return var;
+}
+
void baseBinaryIndexElementAssign(ElementEncoding *This) {
Element * element=This->element;
ASSERT(element->type == ELEMSET);
This->encodingArray[i]=getVectorInt(set->members, i);
setInUseElement(This, i);
}
+ This->numVars = NUMBITS(size-1);
+ This->variables = ourmalloc(sizeof(Constraint*)* This->numVars);
+
+
}
void encode(CSolver* csolver){
+ NaiveEncoder* encoder = allocNaiveEncoder();
+ naiveEncodingDecision( csolver, encoder);
uint size = getSizeVectorElement(csolver->allElements);
for(uint i=0; i<size; i++){
Element* element = getVectorElement(csolver->allElements, i);
switch(GETELEMENTTYPE(element)){
case ELEMFUNCRETURN:
- naiveEncodeFunctionPredicate(&((ElementFunction*)element)->functionencoding);
+ naiveEncodeFunctionPredicate(encoder, GETFUNCTIONENCODING(element));
break;
default:
continue;
Boolean* predicate = getVectorBoolean(csolver->allBooleans, i);
switch(GETBOOLEANTYPE(predicate)){
case PREDICATEOP:
- naiveEncodeFunctionPredicate(&((BooleanPredicate*)predicate)->encoding);
+ naiveEncodeFunctionPredicate(encoder, GETFUNCTIONENCODING(predicate));
break;
default:
continue;
}
}
-void naiveEncodeFunctionPredicate(FunctionEncoding *This){
+void naiveEncodeFunctionPredicate(NaiveEncoder* encoder, FunctionEncoding *This){
if(This->isFunction) {
ASSERT(GETELEMENTTYPE(This->op.function)==ELEMFUNCRETURN);
switch(This->type){
case ENUMERATEIMPLICATIONS:
- naiveEncodeEnumeratedFunction(This);
+ naiveEncodeEnumeratedFunction(encoder, This);
break;
case CIRCUIT:
- naiveEncodeCircuitFunction(This);
+ naiveEncodeCircuitFunction(encoder, This);
break;
default:
ASSERT(0);
}
}
-
-void naiveEncodeCircuitFunction(FunctionEncoding* This){
-
-}
-
-void naiveEncodeEnumeratedFunction(FunctionEncoding* This){
+void naiveEncodeEnumeratedFunction(NaiveEncoder* encoder, FunctionEncoding* This){
ElementFunction* ef =(ElementFunction*)This->op.function;
- Function * function = ef->function;
- switch(GETFUNCTIONTYPE(function)){
+ switch(GETFUNCTIONTYPE(ef->function)){
case TABLEFUNC:
- naiveEncodeEnumTableFunc(ef);
+ naiveEncodeEnumTableFunc(encoder, ef);
break;
case OPERATORFUNC:
- naiveEncodeEnumOperatingFunc(ef);
+ naiveEncodeEnumOperatingFunc(encoder, ef);
break;
default:
ASSERT(0);
}
}
-void naiveEncodeEnumTableFunc(ElementFunction* This){
+void naiveEncodeEnumTableFunc(NaiveEncoder* encoder, ElementFunction* This){
ASSERT(GETFUNCTIONTYPE(This->function)==TABLEFUNC);
ArrayElement* elements= &This->inputs;
Table* table = ((FunctionTable*) (This->function))->table;
uint size = getSizeVectorTableEntry(&table->entries);
for(uint i=0; i<size; i++){
TableEntry* entry = getVectorTableEntry(&table->entries, i);
- //FIXME: generate Constraints
+ uint inputNum =getSizeArrayElement(elements);
+ Element* el= getArrayElement(elements, i);
+ Constraint* carray[inputNum];
+ for(uint j=0; j<inputNum; j++){
+ carray[inputNum] = getElementValueConstraint(el, entry->inputs[j]);
+ }
+ Constraint* row= allocConstraint(IMPLIES, allocArrayConstraint(AND, inputNum, carray),
+ getElementValueConstraint(table->range, entry->output));
+ pushVectorConstraint( GETNAIVEENCODERALLCONSTRAINTS(encoder), row);
}
}
-void naiveEncodeEnumOperatingFunc(ElementFunction* This){
+void naiveEncodeEnumOperatingFunc(NaiveEncoder* encoder, ElementFunction* This){
+
+}
+
+
+void naiveEncodeCircuitFunction(NaiveEncoder* encoder, FunctionEncoding* This){
+}
+
+void deleteNaiveEncoder(NaiveEncoder* encoder){
+ deleteVectorArrayConstraint(&encoder->vars);
}
\ No newline at end of file
#ifndef NAIVEELEMENTENCODER_H
#define NAIVEELEMENTENCODER_H
#include "classlist.h"
+#include "structs.h"
+#define GETNAIVEENCODERVARS(ne) (&((NaiveEncoder*)ne)->vars)
+#define GETNAIVEENCODERALLCONSTRAINTS(ne) (&((NaiveEncoder*)ne)->allConstraints)
+
+struct NaiveEncoder{
+ uint varindex;
+ VectorConstraint vars;
+ VectorConstraint allConstraints;
+};
+
+NaiveEncoder* allocNaiveEncoder();
+Constraint* getNewVar(NaiveEncoder* encoder);
+void getArrayNewVars(NaiveEncoder* encoder, uint num, Constraint **carray);
//For now, This function just simply goes through elements/functions and
//assigns a predefined Encoding to each of them
-void naiveEncodingDecision(CSolver* csolver);
+void naiveEncodingDecision(CSolver* csolver, NaiveEncoder* encoder);
void encode(CSolver* csolver);
void baseBinaryIndexElementAssign(ElementEncoding *This);
-void naiveEncodeFunctionPredicate( FunctionEncoding *This);
-void naiveEncodeCircuitFunction(FunctionEncoding* This);
-void naiveEncodeEnumeratedFunction(FunctionEncoding* This);
-void naiveEncodeEnumTableFunc(ElementFunction* This);
-void naiveEncodeEnumOperatingFunc(ElementFunction* This);
+void naiveEncodeFunctionPredicate(NaiveEncoder* encoder, FunctionEncoding *This);
+void naiveEncodeCircuitFunction(NaiveEncoder* encoder, FunctionEncoding* This);
+void naiveEncodeEnumeratedFunction(NaiveEncoder* encoder, FunctionEncoding* This);
+void naiveEncodeEnumTableFunc(NaiveEncoder* encoder, ElementFunction* This);
+void naiveEncodeEnumOperatingFunc(NaiveEncoder* encoder, ElementFunction* This);
+
+void deleteNaiveEncoder(NaiveEncoder* encoder);
#endif
struct TableEntry;
typedef struct TableEntry TableEntry;
+struct NaiveEncoder;
+typedef struct NaiveEncoder NaiveEncoder;
typedef unsigned int uint;
typedef uint64_t VarType;
#endif
#define model_print printf
#define NEXTPOW2(x) (1<<(sizeof(uint)*8-__builtin_clz(x-1)))
+#define NUMBITS(x) ((x==0) ? 0 : 8*sizeof(x)-__builtin_clz(x))
#ifdef CONFIG_DEBUG
#define DEBUG(fmt, ...) do { model_print("*** %15s:%-4d %25s() *** " fmt, __FILE__, __LINE__, __func__, ## __VA_ARGS__); } while (0)
return predicate;
}
-Table * createTable(CSolver *solver, Set **domains, uint numDomain, Set * range) {
+Table * createTable(CSolver *solver, Element **domains, uint numDomain, Element * range) {
Table* table= allocTable(domains,numDomain,range);
pushVectorTable(solver->allTables, table);
return table;
/** This function creates an empty instance table.*/
-Table * createTable(CSolver *solver, Set **domains, uint numDomain, Set * range);
+Table * createTable(CSolver *solver, Element **domains, uint numDomain, Element * range);
/** This function adds an input output relation to a table. */