00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __SIGNATURE_H_
00021 #define __SIGNATURE_H_
00022
00023 #if defined(_MSC_VER)
00024 #pragma warning(disable:4290)
00025 #endif
00026
00027 #include <string>
00028 #include "exp.h"
00029 #include "type.h"
00030 #include "sigenum.h"
00031 #include "memo.h"
00032 #include "statement.h"
00033
00034 class Statement;
00035 class StatementList;
00036 class BinaryFile;
00037 class XMLProgParser;
00038
00039 class Parameter {
00040 private:
00041 Type *type;
00042 std::string name;
00043 Exp *exp;
00044 std::string boundMax;
00045
00046 public:
00047 Parameter(Type *type, const char *name, Exp *exp = NULL, const char *boundMax = "") :
00048 type(type), name(name), exp(exp), boundMax(boundMax) { }
00049 virtual ~Parameter() { delete type; delete exp; }
00050 bool operator==(Parameter& other);
00051 Parameter* clone();
00052
00053 Type *getType() { return type; }
00054 void setType(Type *ty) { type = ty; }
00055 const char *getName() { return name.c_str(); }
00056 void setName(const char *nam) { name = nam; }
00057 Exp *getExp() { return exp; }
00058 void setExp(Exp *e) { exp = e; }
00059
00060
00061 const char *getBoundMax() { return boundMax.c_str(); }
00062 void setBoundMax(const char *nam);
00063
00064 protected:
00065 friend class XMLProgParser;
00066 Parameter() : type(NULL), name(""), exp(NULL) { }
00067 };
00068
00069 class Return {
00070 public:
00071 Type *type;
00072 Exp *exp;
00073
00074 Return(Type *type, Exp *exp) : type(type), exp(exp) { }
00075 virtual ~Return() { }
00076 bool operator==(Return& other);
00077 Return* clone();
00078
00079 Return() : type(NULL), exp(NULL) { }
00080 friend class XMLProgParser;
00081 };
00082
00083 typedef std::vector<Return*> Returns;
00084
00085
00086 class Signature {
00087 protected:
00088 std::string name;
00089 std::string sigFile;
00090 std::vector<Parameter*> params;
00091
00092 Returns returns;
00093 Type *rettype;
00094 bool ellipsis;
00095 bool unknown;
00096
00097
00098 bool forced;
00099 Type *preferedReturn;
00100 std::string preferedName;
00101 std::vector<int> preferedParams;
00102
00103
00104 bool usesNewParam(UserProc *p, Statement *stmt, bool checkreach, int &n);
00105
00106
00107
00108
00109 public:
00110 Signature(const char *nam);
00111
00112
00113 static Signature *instantiate(platform plat, callconv cc, const char *nam);
00114 virtual ~Signature() { }
00115
00116 virtual bool operator==(Signature& other);
00117
00118
00119 virtual Signature *clone();
00120
00121 bool isUnknown() { return unknown; }
00122 void setUnknown(bool b) { unknown = b; }
00123
00124 bool isForced() {return forced; }
00125 void setForced(bool f) {forced = f; }
00126
00127
00128 virtual void addReturn(Type *type, Exp *e = NULL);
00129 virtual void addReturn(Exp *e);
00130 virtual void addReturn(Return *ret) { returns.push_back(ret); }
00131 virtual void removeReturn(Exp *e);
00132 virtual unsigned getNumReturns() {return returns.size();}
00133 virtual Exp *getReturnExp(int n) {return returns[n]->exp;}
00134 void setReturnExp(int n, Exp* e) {returns[n]->exp = e;}
00135 virtual Type *getReturnType(int n) {return returns[n]->type;}
00136 virtual void setReturnType(int n, Type *ty);
00137 int findReturn(Exp *e);
00138
00139 void setRetType(Type *t) { rettype = t; }
00140 Returns& getReturns() {return returns;}
00141 Type* getTypeFor(Exp* e);
00142
00143
00144 virtual const char *getName();
00145 virtual void setName(const char *nam);
00146
00147 const char *getSigFile() { return sigFile.c_str(); }
00148 void setSigFile(const char *nam) { sigFile = nam; }
00149
00150
00151 virtual void addParameter(const char *nam = NULL);
00152 virtual void addParameter(Type *type, const char *nam = NULL, Exp *e = NULL, const char *boundMax = "");
00153 virtual void addParameter(Exp *e, Type* ty);
00154 virtual void addParameter(Parameter *param);
00155 void addEllipsis() { ellipsis = true; }
00156 void killEllipsis() {ellipsis = false; }
00157 virtual void removeParameter(Exp *e);
00158 virtual void removeParameter(int i);
00159
00160 virtual void setNumParams(int n);
00161
00162
00163 virtual unsigned getNumParams() {return params.size();}
00164 virtual const char *getParamName(int n);
00165 virtual Exp *getParamExp(int n);
00166 virtual Type *getParamType(int n);
00167 virtual const char *getParamBoundMax(int n);
00168 virtual void setParamType(int n, Type *ty);
00169 virtual void setParamType(const char* nam, Type *ty);
00170 virtual void setParamType(Exp* e, Type *ty);
00171 virtual void setParamName(int n, const char *nam);
00172 virtual void setParamExp(int n, Exp *e);
00173 virtual int findParam(Exp *e);
00174 virtual int findParam(const char *nam);
00175
00176 virtual Exp *getArgumentExp(int n);
00177 virtual bool hasEllipsis() { return ellipsis; }
00178
00179 void renameParam(const char *oldName, const char *newName);
00180
00181
00182
00183
00184
00185 bool dfaTypeAnalysis(Cfg* cfg);
00186
00187
00188 virtual Signature *promote(UserProc *p);
00189 void print(std::ostream &out, bool html = false);
00190 char* prints();
00191 void printToLog();
00192
00193
00194
00195 Exp* getFirstArgLoc(Prog* prog);
00196
00197
00198
00199 Exp* getEarlyParamExp(int n, Prog* prog);
00200
00201
00202 virtual Exp *getStackWildcard() { return NULL; }
00203 class StackRegisterNotDefinedException : public std::exception {
00204 public:
00205 StackRegisterNotDefinedException() { }
00206 };
00207 virtual int getStackRegister( ) throw(StackRegisterNotDefinedException);
00208 static int getStackRegister(Prog* prog) throw(StackRegisterNotDefinedException);
00209
00210
00211
00212
00213 bool isStackLocal(Prog* prog, Exp *e);
00214
00215 virtual bool isAddrOfStackLocal(Prog* prog, Exp* e);
00216
00217 virtual bool isLocalOffsetNegative() {return true;}
00218
00219 virtual bool isLocalOffsetPositive() {return false;}
00220
00221 bool isOpCompatStackLocal(OPER op);
00222
00223
00224 static Exp* getReturnExp2(BinaryFile* pBF);
00225 static StatementList& getStdRetStmt(Prog* prog);
00226
00227
00228 virtual Exp *getProven(Exp *left) { return NULL; }
00229 virtual bool isPreserved(Exp* e) { return false; }
00230 virtual void setLibraryDefines(StatementList* defs) {}
00231 static void setABIdefines(Prog* prog, StatementList* defs);
00232
00233
00234 virtual bool isPromoted() { return false; }
00235
00236
00237
00238
00239
00240 static char* platformName(platform plat);
00241 static char* conventionName(callconv cc);
00242 virtual platform getPlatform() { return PLAT_GENERIC; }
00243 virtual callconv getConvention() { return CONV_NONE; }
00244
00245
00246 void setPreferedReturn(Type *ty) { preferedReturn = ty; }
00247 void setPreferedName(const char *nam) { preferedName = nam; }
00248 void addPreferedParameter(int n) { preferedParams.push_back(n); }
00249 Type *getPreferedReturn() { return preferedReturn; }
00250 const char *getPreferedName() { return preferedName.c_str(); }
00251 unsigned int getNumPreferedParams() { return preferedParams.size(); }
00252 int getPreferedParam(int n) { return preferedParams[n]; }
00253
00254
00255 virtual bool argumentCompare(Assignment& a, Assignment& b);
00256 virtual bool returnCompare(Assignment& a, Assignment& b);
00257
00258
00259 protected:
00260 friend class XMLProgParser;
00261 Signature() : name(""), rettype(NULL), ellipsis(false), preferedReturn(NULL), preferedName("") { }
00262 void appendParameter(Parameter *p) { params.push_back(p); }
00263
00264 void appendReturn(Return *r) { returns.push_back(r); }
00265 };
00266
00267 class CustomSignature : public Signature {
00268 protected:
00269 int sp;
00270 public:
00271 CustomSignature(const char *nam);
00272 virtual ~CustomSignature() { }
00273 virtual bool isPromoted() { return true; }
00274 virtual Signature *clone();
00275 void setSP(int nsp);
00276 virtual int getStackRegister() throw(StackRegisterNotDefinedException) {return sp; };
00277 };
00278
00279 #endif