00001 #include <assert.h>
00002 #include <stdlib.h>
00003 #include "recursivness.hh"
00004 #include "occurences.hh"
00005 #include "sigtype.hh"
00006 #include "sigtyperules.hh"
00007 #include <iostream>
00008
00009 using namespace std;
00010
00014 static int xVariability (int v, int r)
00015 {
00016
00017
00018
00019 if (r>1) r=1;
00020 return min(3, v + r);
00021 }
00022
00023
00024
00025
00026
00027 Occurences::Occurences(int v, int r) : fXVariability(xVariability(v,r)) {
00028 for (int i=0; i<4; i++) fOccurences[i]=0;
00029 fMultiOcc = false;
00030 fMaxDelay = 0;
00031 fOutDelayOcc = false;
00032 }
00033
00034 Occurences* Occurences::incOccurences(int v, int r, int d) {
00035 int ctxt = xVariability(v,r);
00036
00037 fOccurences[ctxt] += 1;
00038 fMultiOcc = fMultiOcc | (ctxt > fXVariability) | (fOccurences[ctxt] > 1);
00039 if (d == 0) {
00040
00041 fOutDelayOcc = true;
00042 }
00043 if (d > fMaxDelay) {
00044
00045 fMaxDelay = d;
00046 }
00047 return this;
00048 }
00049
00050 bool Occurences::hasMultiOccurences() const { return fMultiOcc; }
00051
00052 bool Occurences::hasOutDelayOccurences() const { return fOutDelayOcc; }
00053
00054 int Occurences::getMaxDelay() const
00055 {
00056 return fMaxDelay;
00057 }
00058
00059
00060
00061
00062
00063 void OccMarkup::mark(Tree root)
00064 {
00065 fRootTree = root;
00066 fPropKey = tree(unique("OCCURENCES"));
00067
00068 if (isList(root)) {
00069 while (isList(root)) {
00070
00071 incOcc(nil, kSamp, 0, 0, hd(root));
00072 root = tl(root);
00073 }
00074
00075 } else {
00076
00077 incOcc(nil, kSamp, 0, 0, root);
00078 }
00079 }
00080
00081 Occurences* OccMarkup::retrieve(Tree t)
00082 {
00083 Occurences* p = getOcc(t);
00084 if (p == 0) {
00085
00086
00087 }
00088 return p;
00089 }
00090
00091
00092
00093
00094
00095 void OccMarkup::incOcc(Tree env, int v, int r, int d, Tree t)
00096 {
00097
00098
00099
00100 Occurences* occ = getOcc(t);
00101
00102 if (occ==0) {
00103
00104 Type ty = getSigType(t);
00105 int v0 = ty->variability();
00106 int r0 = getRecursivness(t);
00107
00108 occ = new Occurences(v0,r0);
00109 setOcc(t, occ);
00110
00111
00112 Tree x, y;
00113 if (isSigFixDelay(t,x,y)) {
00114 Type g2 = getSigType(y);
00115 int d2 = checkDelayInterval(g2);
00116 assert(d2>=0);
00117 incOcc(env, v0, r0, d2, x);
00118 incOcc(env, v0, r0, 0, y);
00119 } else {
00120 vector<Tree> br;
00121 int n = getSubSignals(t, br);
00122 if (n>0 && ! isSigGen(t)) {
00123 for (int i=0; i<n; i++) incOcc(env, v0, r0, 0, br[i]);
00124 }
00125 }
00126 }
00127
00128 occ->incOccurences(v,r,d);
00129
00130 }
00131
00132
00133
00134 Occurences* OccMarkup::getOcc(Tree t)
00135 {
00136 Tree p = t->getProperty(fPropKey);
00137 if (p) {
00138 return (Occurences*) tree2ptr(p);
00139 } else {
00140 return 0;
00141 }
00142 }
00143
00144
00145 void OccMarkup::setOcc(Tree t, Occurences* occ)
00146 {
00147 t->setProperty(fPropKey, tree(occ));
00148 }
00149
00150
00151
00158 static int position (Tree env, Tree t, int p)
00159 {
00160 if (isNil(env)) return 0;
00161 if (hd(env) == t) return p;
00162 else return position (tl(env), t, p+1);
00163 }