daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | /****************************************************************************\ |
| 2 | Copyright (c) 2002, NVIDIA Corporation. |
| 3 | |
| 4 | NVIDIA Corporation("NVIDIA") supplies this software to you in |
| 5 | consideration of your agreement to the following terms, and your use, |
| 6 | installation, modification or redistribution of this NVIDIA software |
| 7 | constitutes acceptance of these terms. If you do not agree with these |
| 8 | terms, please do not use, install, modify or redistribute this NVIDIA |
| 9 | software. |
| 10 | |
| 11 | In consideration of your agreement to abide by the following terms, and |
| 12 | subject to these terms, NVIDIA grants you a personal, non-exclusive |
| 13 | license, under NVIDIA's copyrights in this original NVIDIA software (the |
| 14 | "NVIDIA Software"), to use, reproduce, modify and redistribute the |
| 15 | NVIDIA Software, with or without modifications, in source and/or binary |
| 16 | forms; provided that if you redistribute the NVIDIA Software, you must |
| 17 | retain the copyright notice of NVIDIA, this notice and the following |
| 18 | text and disclaimers in all such redistributions of the NVIDIA Software. |
| 19 | Neither the name, trademarks, service marks nor logos of NVIDIA |
| 20 | Corporation may be used to endorse or promote products derived from the |
| 21 | NVIDIA Software without specific prior written permission from NVIDIA. |
| 22 | Except as expressly stated in this notice, no other rights or licenses |
| 23 | express or implied, are granted by NVIDIA herein, including but not |
| 24 | limited to any patent rights that may be infringed by your derivative |
| 25 | works or by other works in which the NVIDIA Software may be |
| 26 | incorporated. No hardware is licensed hereunder. |
| 27 | |
| 28 | THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT |
| 29 | WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, |
| 30 | INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE, |
| 31 | NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR |
| 32 | ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER |
| 33 | PRODUCTS. |
| 34 | |
| 35 | IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, |
| 36 | INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 37 | TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 38 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY |
| 39 | OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE |
| 40 | NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, |
| 41 | TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF |
| 42 | NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 43 | \****************************************************************************/ |
| 44 | // |
| 45 | // symbols.c |
| 46 | // |
| 47 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | #include <stdio.h> |
| 50 | #include <string.h> |
| 51 | |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 52 | #include "compiler/preprocessor/slglobals.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 53 | |
apatrick@chromium.org | a1d8059 | 2012-01-25 21:52:10 +0000 | [diff] [blame^] | 54 | #if defined(_MSC_VER) |
| 55 | #pragma warning(disable: 4706) |
| 56 | #endif |
| 57 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 58 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 59 | /////////////////////////////////// Symbol Table Variables: /////////////////////////////////// |
| 60 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | Scope *ScopeList = NULL; |
| 63 | Scope *CurrentScope = NULL; |
| 64 | Scope *GlobalScope = NULL; |
| 65 | |
| 66 | static void unlinkScope(void *_scope) { |
| 67 | Scope *scope = _scope; |
| 68 | |
| 69 | if (scope->next) |
| 70 | scope->next->prev = scope->prev; |
| 71 | if (scope->prev) |
| 72 | scope->prev->next = scope->next; |
| 73 | else |
| 74 | ScopeList = scope->next; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * NewScope() |
| 79 | * |
| 80 | */ |
| 81 | Scope *NewScopeInPool(MemoryPool *pool) |
| 82 | { |
| 83 | Scope *lScope; |
| 84 | |
| 85 | lScope = mem_Alloc(pool, sizeof(Scope)); |
| 86 | lScope->pool = pool; |
| 87 | lScope->parent = NULL; |
| 88 | lScope->funScope = NULL; |
| 89 | lScope->symbols = NULL; |
| 90 | |
| 91 | lScope->level = 0; |
| 92 | |
| 93 | lScope->programs = NULL; |
| 94 | if ((lScope->next = ScopeList)) |
| 95 | ScopeList->prev = lScope; |
| 96 | lScope->prev = 0; |
| 97 | ScopeList = lScope; |
| 98 | mem_AddCleanup(pool, unlinkScope, lScope); |
| 99 | return lScope; |
| 100 | } // NewScope |
| 101 | |
| 102 | /* |
| 103 | * PushScope() |
| 104 | * |
| 105 | */ |
| 106 | |
| 107 | void PushScope(Scope *fScope) |
| 108 | { |
| 109 | Scope *lScope; |
| 110 | |
| 111 | if (CurrentScope) { |
| 112 | fScope->level = CurrentScope->level + 1; |
| 113 | if (fScope->level == 1) { |
| 114 | if (!GlobalScope) { |
| 115 | /* HACK - CTD -- if GlobalScope==NULL and level==1, we're |
| 116 | * defining a function in the superglobal scope. Things |
| 117 | * will break if we leave the level as 1, so we arbitrarily |
| 118 | * set it to 2 */ |
| 119 | fScope->level = 2; |
| 120 | } |
| 121 | } |
| 122 | if (fScope->level >= 2) { |
| 123 | lScope = fScope; |
| 124 | while (lScope->level > 2) |
| 125 | lScope = lScope->next; |
| 126 | fScope->funScope = lScope; |
| 127 | } |
| 128 | } else { |
| 129 | fScope->level = 0; |
| 130 | } |
| 131 | fScope->parent = CurrentScope; |
| 132 | CurrentScope = fScope; |
| 133 | } // PushScope |
| 134 | |
| 135 | /* |
| 136 | * PopScope() |
| 137 | * |
| 138 | */ |
| 139 | |
| 140 | Scope *PopScope(void) |
| 141 | { |
| 142 | Scope *lScope; |
| 143 | |
| 144 | lScope = CurrentScope; |
| 145 | if (CurrentScope) |
| 146 | CurrentScope = CurrentScope->parent; |
| 147 | return lScope; |
| 148 | } // PopScope |
| 149 | |
| 150 | /* |
| 151 | * NewSymbol() - Allocate a new symbol node; |
| 152 | * |
| 153 | */ |
| 154 | |
| 155 | Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, symbolkind kind) |
| 156 | { |
| 157 | Symbol *lSymb; |
| 158 | char *pch; |
alokp@chromium.org | bcfba4c | 2010-08-09 22:30:49 +0000 | [diff] [blame] | 159 | unsigned int ii; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 160 | |
| 161 | lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol)); |
| 162 | lSymb->left = NULL; |
| 163 | lSymb->right = NULL; |
| 164 | lSymb->next = NULL; |
| 165 | lSymb->name = name; |
| 166 | lSymb->loc = *loc; |
| 167 | lSymb->kind = kind; |
| 168 | |
| 169 | // Clear union area: |
| 170 | |
| 171 | pch = (char *) &lSymb->details; |
| 172 | for (ii = 0; ii < sizeof(lSymb->details); ii++) |
| 173 | *pch++ = 0; |
| 174 | return lSymb; |
| 175 | } // NewSymbol |
| 176 | |
| 177 | /* |
| 178 | * lAddToTree() - Using a binary tree is not a good idea for basic atom values because they |
| 179 | * are generated in order. We'll fix this later (by reversing the bit pattern). |
| 180 | */ |
| 181 | |
| 182 | static void lAddToTree(Symbol **fSymbols, Symbol *fSymb) |
| 183 | { |
| 184 | Symbol *lSymb; |
| 185 | int lrev, frev; |
| 186 | |
| 187 | lSymb = *fSymbols; |
| 188 | if (lSymb) { |
| 189 | frev = GetReversedAtom(atable, fSymb->name); |
| 190 | while (lSymb) { |
| 191 | lrev = GetReversedAtom(atable, lSymb->name); |
| 192 | if (lrev == frev) { |
| 193 | CPPErrorToInfoLog("GetAtomString(atable, fSymb->name)"); |
| 194 | break; |
| 195 | } else { |
| 196 | if (lrev > frev) { |
| 197 | if (lSymb->left) { |
| 198 | lSymb = lSymb->left; |
| 199 | } else { |
| 200 | lSymb->left = fSymb; |
| 201 | break; |
| 202 | } |
| 203 | } else { |
| 204 | if (lSymb->right) { |
| 205 | lSymb = lSymb->right; |
| 206 | } else { |
| 207 | lSymb->right = fSymb; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } else { |
| 214 | *fSymbols = fSymb; |
| 215 | } |
| 216 | } // lAddToTree |
| 217 | |
| 218 | |
| 219 | /* |
| 220 | * AddSymbol() - Add a variable, type, or function name to a scope. |
| 221 | * |
| 222 | */ |
| 223 | |
| 224 | Symbol *AddSymbol(SourceLoc *loc, Scope *fScope, int atom, symbolkind kind) |
| 225 | { |
| 226 | Symbol *lSymb; |
| 227 | |
| 228 | if (!fScope) |
| 229 | fScope = CurrentScope; |
| 230 | lSymb = NewSymbol(loc, fScope, atom, kind); |
| 231 | lAddToTree(&fScope->symbols, lSymb); |
| 232 | return lSymb; |
| 233 | } // AddSymbol |
| 234 | |
| 235 | |
| 236 | /*********************************************************************************************/ |
| 237 | /************************************ Symbol Semantic Functions ******************************/ |
| 238 | /*********************************************************************************************/ |
| 239 | |
| 240 | /* |
| 241 | * LookUpLocalSymbol() |
| 242 | * |
| 243 | */ |
| 244 | |
| 245 | Symbol *LookUpLocalSymbol(Scope *fScope, int atom) |
| 246 | { |
| 247 | Symbol *lSymb; |
| 248 | int rname, ratom; |
| 249 | |
| 250 | ratom = GetReversedAtom(atable, atom); |
| 251 | if (!fScope) |
| 252 | fScope = CurrentScope; |
| 253 | lSymb = fScope->symbols; |
| 254 | while (lSymb) { |
| 255 | rname = GetReversedAtom(atable, lSymb->name); |
| 256 | if (rname == ratom) { |
| 257 | return lSymb; |
| 258 | } else { |
| 259 | if (rname > ratom) { |
| 260 | lSymb = lSymb->left; |
| 261 | } else { |
| 262 | lSymb = lSymb->right; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | return NULL; |
| 267 | } // LookUpLocalSymbol |
| 268 | |
| 269 | /* |
| 270 | * LookUpSymbol() |
| 271 | * |
| 272 | */ |
| 273 | |
| 274 | Symbol *LookUpSymbol(Scope *fScope, int atom) |
| 275 | { |
| 276 | Symbol *lSymb; |
| 277 | |
| 278 | if (!fScope) |
| 279 | fScope = CurrentScope; |
| 280 | while (fScope) { |
| 281 | lSymb = LookUpLocalSymbol(fScope, atom); |
| 282 | if (lSymb) |
| 283 | return lSymb; |
| 284 | fScope = fScope->parent; |
| 285 | } |
| 286 | return NULL; |
| 287 | } // LookUpSymbol |
| 288 | |