<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>FAUST compiler: symbol.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>symbol.cpp</h1><a href="symbol_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/************************************************************************</span> <a name="l00002"></a>00002 <span class="comment"> ************************************************************************</span> <a name="l00003"></a>00003 <span class="comment"> FAUST compiler</span> <a name="l00004"></a>00004 <span class="comment"> Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale</span> <a name="l00005"></a>00005 <span class="comment"> ---------------------------------------------------------------------</span> <a name="l00006"></a>00006 <span class="comment"> This program is free software; you can redistribute it and/or modify</span> <a name="l00007"></a>00007 <span class="comment"> it under the terms of the GNU General Public License as published by</span> <a name="l00008"></a>00008 <span class="comment"> the Free Software Foundation; either version 2 of the License, or</span> <a name="l00009"></a>00009 <span class="comment"> (at your option) any later version.</span> <a name="l00010"></a>00010 <span class="comment"></span> <a name="l00011"></a>00011 <span class="comment"> This program is distributed in the hope that it will be useful,</span> <a name="l00012"></a>00012 <span class="comment"> but WITHOUT ANY WARRANTY; without even the implied warranty of</span> <a name="l00013"></a>00013 <span class="comment"> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the</span> <a name="l00014"></a>00014 <span class="comment"> GNU General Public License for more details.</span> <a name="l00015"></a>00015 <span class="comment"></span> <a name="l00016"></a>00016 <span class="comment"> You should have received a copy of the GNU General Public License</span> <a name="l00017"></a>00017 <span class="comment"> along with this program; if not, write to the Free Software</span> <a name="l00018"></a>00018 <span class="comment"> Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.</span> <a name="l00019"></a>00019 <span class="comment"> ************************************************************************</span> <a name="l00020"></a>00020 <span class="comment"> ************************************************************************/</span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="preprocessor">#include <stdlib.h></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <stdio.h></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <string.h></span> <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="symbol_8hh.html" title="A library to create and manipulate symbols with a unique name.">symbol.hh</a>"</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="compatibility_8hh.html">compatibility.hh</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include <iostream></span> <a name="l00028"></a>00028 <span class="preprocessor">#include <cstring></span> <a name="l00029"></a>00029 <span class="preprocessor">#include <assert.h></span> <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keyword">using namespace </span>std; <a name="l00032"></a>00032 <a name="l00037"></a>00037 <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* <a class="code" href="classSymbol.html#ad28a6fe0f613177e77bc29b98ad22ecd" title="Hash table used to store the symbols.">Symbol::gSymbolTable</a>[kHashTableSize]; <a name="l00038"></a>00038 <a name="l00039"></a>00039 <a name="l00046"></a><a class="code" href="classSymbol.html#af8badc954a2122bb8087e084224af76e">00046</a> <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* <a class="code" href="classSymbol.html#af8badc954a2122bb8087e084224af76e" title="Get the symbol of name str.">Symbol::get</a>(<span class="keyword">const</span> <span class="keywordtype">string</span>& str) <a name="l00047"></a>00047 { <a name="l00048"></a>00048 <span class="keywordtype">char</span> buf[1024]; <a name="l00049"></a>00049 <span class="keywordtype">int</span> i; <a name="l00050"></a>00050 <span class="keywordtype">int</span> n = str.length(); <a name="l00051"></a>00051 <a name="l00052"></a>00052 <span class="keywordflow">if</span> (n>1023) n = 1023; <a name="l00053"></a>00053 <span class="keywordflow">for</span> (i = 0; i < n; i++) { buf[i] = str[i]; } <a name="l00054"></a>00054 buf[i] = 0; <a name="l00055"></a>00055 <a name="l00056"></a>00056 <span class="keywordflow">return</span> <a class="code" href="classSymbol.html#af8badc954a2122bb8087e084224af76e" title="Get the symbol of name str.">Symbol::get</a>(buf); <a name="l00057"></a>00057 } <a name="l00058"></a>00058 <a name="l00059"></a>00059 <a name="l00060"></a>00060 <a name="l00067"></a><a class="code" href="classSymbol.html#a95ac2abd0de9839bb238e48a02e4d83a">00067</a> <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* <a class="code" href="classSymbol.html#af8badc954a2122bb8087e084224af76e" title="Get the symbol of name str.">Symbol::get</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hsh = <a class="code" href="classSymbol.html#a5c9ccccee95f2d09e34b060076d52a29" title="Compute the 32-bits hash key of string str.">calcHashKey</a>(str); <a name="l00070"></a>00070 <span class="keywordtype">int</span> bckt = hsh % <a class="code" href="classSymbol.html#abb8b44fb98513a5d545a684c6024be4d" title="Size of the hash table (a prime number is recommended).">kHashTableSize</a>; <a name="l00071"></a>00071 <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* item = <a class="code" href="classSymbol.html#ad28a6fe0f613177e77bc29b98ad22ecd" title="Hash table used to store the symbols.">gSymbolTable</a>[bckt]; <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="keywordflow">while</span> ( item && !item-><a class="code" href="classSymbol.html#ae5e90343699911affcc3372bcb131c50" title="Check if the name of the symbol is equal to string str.">equiv</a>(hsh,str) ) item = item-><a class="code" href="classSymbol.html#a2fc73a8c7c4a86ed96c48236e397bbf0" title="Next symbol in the hash table entry.">fNext</a>; <a name="l00074"></a>00074 <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* r = item ? item : <a class="code" href="classSymbol.html#ad28a6fe0f613177e77bc29b98ad22ecd" title="Hash table used to store the symbols.">gSymbolTable</a>[bckt] = <span class="keyword">new</span> <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>(str, hsh, <a class="code" href="classSymbol.html#ad28a6fe0f613177e77bc29b98ad22ecd" title="Hash table used to store the symbols.">gSymbolTable</a>[bckt]); <a name="l00075"></a>00075 <span class="keywordflow">return</span> r; <a name="l00076"></a>00076 } <a name="l00077"></a>00077 <a name="l00078"></a>00078 <a name="l00085"></a><a class="code" href="classSymbol.html#a2fd22ca0779942ebaafcdf24a0d758f6">00085</a> <span class="keywordtype">bool</span> <a class="code" href="classSymbol.html#a2fd22ca0779942ebaafcdf24a0d758f6" title="Returns true if no symbol of name str exists.">Symbol::isnew</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str) <a name="l00086"></a>00086 { <a name="l00087"></a>00087 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hsh = <a class="code" href="classSymbol.html#a5c9ccccee95f2d09e34b060076d52a29" title="Compute the 32-bits hash key of string str.">calcHashKey</a>(str); <a name="l00088"></a>00088 <span class="keywordtype">int</span> bckt = hsh % <a class="code" href="classSymbol.html#abb8b44fb98513a5d545a684c6024be4d" title="Size of the hash table (a prime number is recommended).">kHashTableSize</a>; <a name="l00089"></a>00089 <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* item = <a class="code" href="classSymbol.html#ad28a6fe0f613177e77bc29b98ad22ecd" title="Hash table used to store the symbols.">gSymbolTable</a>[bckt]; <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="keywordflow">while</span> ( item && !item-><a class="code" href="classSymbol.html#ae5e90343699911affcc3372bcb131c50" title="Check if the name of the symbol is equal to string str.">equiv</a>(hsh,str) ) item = item-><a class="code" href="classSymbol.html#a2fc73a8c7c4a86ed96c48236e397bbf0" title="Next symbol in the hash table entry.">fNext</a>; <a name="l00092"></a>00092 <span class="keywordflow">return</span> item == 0; <a name="l00093"></a>00093 } <a name="l00094"></a>00094 <a name="l00095"></a>00095 <a name="l00102"></a><a class="code" href="classSymbol.html#acab6dd6c1c62c49b03ca629f7e0dab36">00102</a> <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* <a class="code" href="classSymbol.html#acab6dd6c1c62c49b03ca629f7e0dab36" title="Creates a new symbol of name prefixed by str.">Symbol::prefix</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* str) <a name="l00103"></a>00103 { <a name="l00104"></a>00104 <span class="keywordtype">char</span> <a class="code" href="classSymbol.html#a8243134fbbd9d924ebbb3f833922e310" title="Returns the name of a symbol.">name</a>[256]; <a name="l00105"></a>00105 <a name="l00106"></a>00106 <span class="keyword">static</span> map<const char*, unsigned int> gPrefixCounters; <a name="l00107"></a>00107 <a name="l00108"></a>00108 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> n = 0; n<10000; n++) { <a name="l00109"></a>00109 snprintf(name, 256, <span class="stringliteral">"%s%d"</span>, str, gPrefixCounters[str]++); <a name="l00110"></a>00110 <span class="keywordflow">if</span> (<a class="code" href="classSymbol.html#a2fd22ca0779942ebaafcdf24a0d758f6" title="Returns true if no symbol of name str exists.">isnew</a>(name)) <span class="keywordflow">return</span> <span class="keyword">get</span>(name); <a name="l00111"></a>00111 } <a name="l00112"></a>00112 assert(<span class="keyword">false</span>); <a name="l00113"></a>00113 <span class="keywordflow">return</span> <span class="keyword">get</span>(<span class="stringliteral">"UNIQUEOVERFLOW"</span>); <a name="l00114"></a>00114 } <a name="l00115"></a>00115 <a name="l00116"></a>00116 <a name="l00127"></a><a class="code" href="classSymbol.html#ae5e90343699911affcc3372bcb131c50">00127</a> <span class="keywordtype">bool</span> <a class="code" href="classSymbol.html#ae5e90343699911affcc3372bcb131c50" title="Check if the name of the symbol is equal to string str.">Symbol::equiv</a> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hash, <span class="keyword">const</span> <span class="keywordtype">char</span> *str)<span class="keyword"> const</span> <a name="l00128"></a>00128 <span class="keyword"></span>{ <a name="l00129"></a>00129 <span class="keywordflow">return</span> (<a class="code" href="classSymbol.html#a96342ef5b450e2edbdb9149798c7d435" title="Hash key computed from the name and used to determine the hash table entry.">fHash</a> == hash) && (strcmp(<a class="code" href="classSymbol.html#a500120e613d60cba8faffc9544c06ad1" title="Name of the symbol.">fName</a>,str) == 0); <a name="l00130"></a>00130 } <a name="l00131"></a>00131 <a name="l00132"></a>00132 <a name="l00133"></a>00133 <a name="l00140"></a><a class="code" href="classSymbol.html#a5c9ccccee95f2d09e34b060076d52a29">00140</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <a class="code" href="classSymbol.html#a5c9ccccee95f2d09e34b060076d52a29" title="Compute the 32-bits hash key of string str.">Symbol::calcHashKey</a> (<span class="keyword">const</span> <span class="keywordtype">char</span>* str) <a name="l00141"></a>00141 { <a name="l00142"></a>00142 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> h = 0; <a name="l00143"></a>00143 <a name="l00144"></a>00144 <span class="keywordflow">while</span> (*str) h = (h << 1) ^ (h >> 20) ^ (*str++); <a name="l00145"></a>00145 <span class="keywordflow">return</span> h; <a name="l00146"></a>00146 } <a name="l00147"></a>00147 <a name="l00148"></a>00148 <a name="l00149"></a>00149 <a name="l00158"></a><a class="code" href="classSymbol.html#a28b3233002ab61fa790e1d3281bd5f4e">00158</a> <a class="code" href="classSymbol.html#a28b3233002ab61fa790e1d3281bd5f4e" title="Constructs a new symbol ready to be placed in the hash table.">Symbol::Symbol</a>(<span class="keyword">const</span> <span class="keywordtype">char</span>* str, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hsh, <a class="code" href="classSymbol.html" title="Symbols are unique objects with a name stored in a hash table.">Symbol</a>* nxt) <a name="l00159"></a>00159 { <a name="l00160"></a>00160 <span class="keywordtype">int</span> <a class="code" href="list_8cpp.html#a0b58ea1a5d8649e8afb0f70c48776347">len</a> = strlen(str); <a name="l00161"></a>00161 <a name="l00162"></a>00162 <a class="code" href="classSymbol.html#a500120e613d60cba8faffc9544c06ad1" title="Name of the symbol.">fName</a> = <span class="keyword">new</span> <span class="keywordtype">char</span> [len+1]; <a name="l00163"></a>00163 memcpy(<a class="code" href="classSymbol.html#a500120e613d60cba8faffc9544c06ad1" title="Name of the symbol.">fName</a>, str, len+1); <a name="l00164"></a>00164 <a class="code" href="classSymbol.html#a96342ef5b450e2edbdb9149798c7d435" title="Hash key computed from the name and used to determine the hash table entry.">fHash</a> = hsh; <a name="l00165"></a>00165 <a class="code" href="classSymbol.html#a2fc73a8c7c4a86ed96c48236e397bbf0" title="Next symbol in the hash table entry.">fNext</a> = nxt; <a name="l00166"></a>00166 <a class="code" href="classSymbol.html#a43a1fda1b5819fc200c19509a0eae03e" title="Field to user disposal to store additional data.">fData</a> = 0; <a name="l00167"></a>00167 } <a name="l00168"></a>00168 <a name="l00169"></a><a class="code" href="classSymbol.html#a505360ad4bd2e0bd1e3954eca1b05723">00169</a> <a class="code" href="classSymbol.html#a505360ad4bd2e0bd1e3954eca1b05723" title="The Destructor is never used.">Symbol::~Symbol</a> () <a name="l00170"></a>00170 { <a name="l00171"></a>00171 <span class="keyword">delete</span> [] <a class="code" href="classSymbol.html#a500120e613d60cba8faffc9544c06ad1" title="Name of the symbol.">fName</a>; <a name="l00172"></a>00172 } <a name="l00173"></a>00173 <a name="l00174"></a><a class="code" href="classSymbol.html#a80aa251523a4a1c24e9e9e679205f18e">00174</a> ostream& <a class="code" href="classSymbol.html#a80aa251523a4a1c24e9e9e679205f18e" title="print a symbol on a stream">Symbol::print</a> (ostream& fout) <span class="keyword">const</span> <a name="l00175"></a>00175 { <a name="l00176"></a>00176 <span class="keywordflow">return</span> fout << fName; <a name="l00177"></a>00177 } </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Wed Apr 28 23:59:59 2010 for FAUST compiler by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>