#include #include "helping_functions.h" #include #include #include #include #include #include #include const string UNKNOWN_MEM_SYMBOL("InternalUnknownMem$"); bool useMalloc = false; string applySuffix(const string& name, const char* suffix, int ub, int lb, bool isOneBit, int instance_num) { stringstream name_stream; name_stream << name << "_" << ub << "_" << lb << suffix; if (isOneBit) { name_stream << "_0_0"; } else { name_stream << "_" << ub << "_" << lb; } if (instance_num != -1) { name_stream << "_" << instance_num; } return name_stream.str(); } //using namespace std; /** Synopsis: [A helper that allows translation of strings representing integer values to numeric integers] Description: [This function uses C's atoi for conversion. Returns 0 for empty string] SideEffects:[] SeeAlso:[] Arguments:[Valid string representing integer like "1234"] PostCondition: [Ensures correct conversion of valid input] */ int stringToInteger(string s) { //returns 0 if the string is empty //Put a check to see whether input string is valid, i.e. does not contain alphabet chars for (int i = 0; i < s.size(); i++) { if (isalpha(s[i])) { //cerr<<"Invalid string in stringToInteger:"<= str.length() || str[i] != find[i]) return false; } return true; } string charpAsString(const char* p){return string(p);} long int binaryStringToInteger(string str) { //return binaryStringToUL(str); int length = str.size(); assert(length<=(sizeof(long)*8)); long int num = 0; long int base = 1; for (int i = length - 1; i >= 0; i--) { if ((str[i] != '1') && (str[i] != '0')) { throw std::runtime_error("Unable to convert binary string to integer: " + str); //exit(-1); } if (str[i] == '1') { num = num + base; } base = base * 2; } return num; } unsigned getBitsRequired(unsigned long val) { if(val == 0) return 1; unsigned bits=0; while(val) { bits++; val = val>>1; } return bits; } unsigned long binaryStringToUL(const string &str) { unsigned long num=0; int len = str.length(); //assert(len<=(sizeof(unsigned long)*8)); static const unsigned long mask = 1ul<<((sizeof(unsigned long)*8)-1); //MSB for(int i=0;i expected in " + input); //return input; } return input.substr(0, index); } /** Synopsis: [A helper that returns binary string of an integer] Description: [Implements general algorithm for converting a integer to binary] SideEffects:[] SeeAlso:[] Arguments:[Integer >=0] PostCondition: [Ensures correct conversion of valid input, exits on invalid input] */ string integerToBinaryString(int i) { if (i < 0) cerr << "ERROR!! Cannot convert " << i << " to binary " << endl; if (i == 0) return "0"; string bin_str = ""; int j = i; while (j != 0) { if (j % 2 == 0) bin_str = bin_str + "0"; else bin_str = bin_str + "1"; j = j / 2; } string str = ""; //Reversing the binary string to bring it to final format for (int k = bin_str.size() - 1; k >= 0; k--) str += bin_str[k]; return str; } string hexStringToBinaryString(string str) { string bin = ""; while (str != "") { string hex = str.substr(str.length() - 1, 1); if (hex == "0") bin = "0000" + bin; else if (hex == "1") bin = "0001" + bin; else if (hex == "2") bin = "0010" + bin; else if (hex == "3") bin = "0011" + bin; else if (hex == "4") bin = "0100" + bin; else if (hex == "5") bin = "0101" + bin; else if (hex == "6") bin = "0110" + bin; else if (hex == "7") bin = "0111" + bin; else if (hex == "8") bin = "1000" + bin; else if (hex == "9") bin = "1001" + bin; else if ((hex == "a") || (hex == "A")) bin = "1010" + bin; else if ((hex == "b") || (hex == "B")) bin = "1011" + bin; else if ((hex == "c") || (hex == "C")) bin = "1100" + bin; else if ((hex == "d") || (hex == "D")) bin = "1101" + bin; else if ((hex == "e") || (hex == "E")) bin = "1110" + bin; else if ((hex == "f") || (hex == "F")) bin = "1111" + bin; else cout << "ERROR!!! Input not in hex format...in hexStringToBinaryString" << endl; str = str.substr(0, str.length() - 1); } return bin; } string integerToString(int i) //converts 123 to "123" { //return toString(i); //char* number = new char[10]; static char number[25]; if (i == 0) return "0"; int pos = 0; string ret_val; int j = i; while (j != 0) { switch (j % 10) { case 0: { number[pos++] = '0'; break; } case 1: { number[pos++] = '1'; break; } case 2: { number[pos++] = '2'; break; } case 3: { number[pos++] = '3'; break; } case 4: { number[pos++] = '4'; break; } case 5: { number[pos++] = '5'; break; } case 6: { number[pos++] = '6'; break; } case 7: { number[pos++] = '7'; break; } case 8: { number[pos++] = '8'; break; } case 9: { number[pos++] = '9'; break; } } j = j / 10; } number[pos++] = '\0'; ret_val = string(number); reverse(ret_val.begin(), ret_val.end()); //delete[] number; return ret_val; } vector split(const string& s, const string& delim, const bool keep_empty) { vector result; if (delim.empty()) { result.push_back(s); return result; } string::const_iterator substart = s.begin(), subend; while (true) { subend = search(substart, s.end(), delim.begin(), delim.end()); string temp(substart, subend); if (keep_empty || !temp.empty()) { result.push_back(temp); } if (subend == s.end()) { break; } substart = subend + delim.size(); } return result; } float stringToFloat(const string &float_representation_of_number) { float result; istringstream ss(float_representation_of_number); ss >> result; if (!ss) { throw std::invalid_argument("Read a bad value in stringToFloat :" + float_representation_of_number + "\n"); } return result; } bool stringToBoolean(const string &boolean_string) { if (boolean_string == "false") return false; else if (boolean_string == "true") return true; else if (boolean_string == "0") return false; else if (boolean_string == "1") return true; else if (boolean_string == "0b0") return false; else if (boolean_string == "0b1") return true; else throw std::invalid_argument("Read a bad value in stringToBoolean:" + boolean_string + "\n"); } int stringToInteger(const string &s) { istringstream ss(s); int result; ss >> result; if (!ss) { throw std::invalid_argument("Read a bad value in stringToInteger :" + s + "\n"); } return result; } bool isABinaryNumberString(string binaryString) { // cout<<"Binary String is:"<= 0; i--) { if ((binaryString[i] != '1') && (binaryString[i] != '0') && (binaryString[i] != 'x') && binaryString[i] != 'z') { return false; } } return true; } /** * It will replace the part of string source before delim by desReplace if the flag is true else it will replace string at delim at the last * by desReplace * * @param source * @param desReplace * @param delim * @return */ string replaceStringAtDelimOnce(const string& source, const string& desReplace, const string& delim, bool firstOrLast) { int index; string res; if (firstOrLast == true) { index = (int) source.find(delim); if (index != string::npos) { res = desReplace + source.substr(index + delim.length()); return res; } //cout<<"returning... "<=0;k--) str += bin_str[k]; int NoOfZeros = width - str.length(); string ret_str=""; for(int i=0; i= 0; i--) { if ((str[i] != '1') && (str[i] != '0')) { cerr << "Unable to convert binary string to integer in binaryStringToULLInteger: " << str << endl; exit(-1); } if (str[i] == '1') { num = num + base; } base = base * 2; } return num; } #endif //#endif added by Ajith John on 9 June 2013 //----------------------------------- MEMORY INFO ----------------------------------------------- map m_memVarNameToMemVarInfoMap; //made static since data added here by CDFG. t_MemoryVariableInfoVector m_allMemoryVariablesInDesign; t_MemoryArrayBoundsInfo* getMemoryVariableDimensions(string memoryVariableName) { /* Begin Commented by Supratik: Nov 27, 2012 vector::iterator it = m_allMemoryVariablesInDesign.begin(); while (it != m_allMemoryVariablesInDesign.end()) { t_MemoryArrayBoundsInfo* currentEntry = (*it); if (currentEntry->m_variable_name == memoryVariableName) { return currentEntry; } it++; } return NULL; * End Commented by Supratik: Nov 27, 2012 */ // Begin Added by Supratik: Nov 27, 2012 int num = 0; memoryVariableName = removeNewNamePrefixFromSignal(memoryVariableName,num); map ::iterator m_it = ::m_memVarNameToMemVarInfoMap.find(memoryVariableName); if (m_it != m_memVarNameToMemVarInfoMap.end()) { return m_it->second; } else { return NULL; } // End Added by Supratik: Nov 27, 2012 } bool addMemoryBoundsInfoToMemoryVariableInfo(t_MemoryArrayBoundsInfo* mem_bounds, bool performSLASHReplacement) { if(performSLASHReplacement) convertInstNameToSSName(mem_bounds->m_variable_name,true); #define VERIFY_BOUNDS #ifdef VERIFY_BOUNDS vector bounds = mem_bounds->m_bounds; for(int i=0;igetVariableName()); } #endif m_allMemoryVariablesInDesign.push_back(mem_bounds); // Begin Added by Supratik: Nov 27, 2012 ::m_memVarNameToMemVarInfoMap.insert(make_pair(mem_bounds->m_variable_name, mem_bounds)); // End Added by Supratik: Nov 27, 2012 return true; }