zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 1 | // |
zmo@google.com | 89c9813 | 2012-01-06 02:17:03 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | #include "compiler/MapLongVariableNames.h" |
| 8 | |
| 9 | namespace { |
| 10 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 11 | TString mapLongName(int id, const TString& name, bool isGlobal) |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 12 | { |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 13 | ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 14 | TStringStream stream; |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 15 | stream << "webgl_"; |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 16 | if (isGlobal) |
| 17 | stream << "g"; |
zmo@google.com | 571fe34 | 2012-04-17 17:40:29 +0000 | [diff] [blame^] | 18 | stream << id; |
| 19 | if (name[0] != '_') |
| 20 | stream << "_"; |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 21 | stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size()); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 22 | return stream.str(); |
| 23 | } |
| 24 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 25 | LongNameMap* gLongNameMapInstance = NULL; |
| 26 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 27 | } // anonymous namespace |
| 28 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 29 | LongNameMap::LongNameMap() |
| 30 | : refCount(0) |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 34 | LongNameMap::~LongNameMap() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | // static |
| 39 | LongNameMap* LongNameMap::GetInstance() |
| 40 | { |
| 41 | if (gLongNameMapInstance == NULL) |
| 42 | gLongNameMapInstance = new LongNameMap; |
| 43 | gLongNameMapInstance->refCount++; |
| 44 | return gLongNameMapInstance; |
| 45 | } |
| 46 | |
| 47 | void LongNameMap::Release() |
| 48 | { |
| 49 | ASSERT(gLongNameMapInstance == this); |
| 50 | ASSERT(refCount > 0); |
| 51 | refCount--; |
| 52 | if (refCount == 0) { |
| 53 | delete gLongNameMapInstance; |
| 54 | gLongNameMapInstance = NULL; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | const char* LongNameMap::Find(const char* originalName) const |
| 59 | { |
| 60 | std::map<std::string, std::string>::const_iterator it = mLongNameMap.find( |
| 61 | originalName); |
| 62 | if (it != mLongNameMap.end()) |
| 63 | return (*it).second.c_str(); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | void LongNameMap::Insert(const char* originalName, const char* mappedName) |
| 68 | { |
| 69 | mLongNameMap.insert(std::map<std::string, std::string>::value_type( |
| 70 | originalName, mappedName)); |
| 71 | } |
| 72 | |
| 73 | int LongNameMap::Size() const |
| 74 | { |
| 75 | return mLongNameMap.size(); |
| 76 | } |
| 77 | |
| 78 | MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap) |
| 79 | { |
| 80 | ASSERT(globalMap); |
| 81 | mGlobalMap = globalMap; |
| 82 | } |
| 83 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 84 | void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) |
| 85 | { |
| 86 | ASSERT(symbol != NULL); |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 87 | if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) { |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 88 | switch (symbol->getQualifier()) { |
| 89 | case EvqVaryingIn: |
| 90 | case EvqVaryingOut: |
| 91 | case EvqInvariantVaryingIn: |
| 92 | case EvqInvariantVaryingOut: |
zmo@google.com | 89c9813 | 2012-01-06 02:17:03 +0000 | [diff] [blame] | 93 | case EvqUniform: |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 94 | symbol->setSymbol( |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 95 | mapGlobalLongName(symbol->getSymbol())); |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 96 | break; |
| 97 | default: |
| 98 | symbol->setSymbol( |
| 99 | mapLongName(symbol->getId(), symbol->getSymbol(), false)); |
| 100 | break; |
| 101 | }; |
| 102 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
zmo@google.com | 216aa5e | 2011-06-17 22:31:32 +0000 | [diff] [blame] | 105 | bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node) |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 106 | { |
zmo@google.com | 216aa5e | 2011-06-17 22:31:32 +0000 | [diff] [blame] | 107 | if (node->getInit()) |
| 108 | node->getInit()->traverse(this); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 112 | TString MapLongVariableNames::mapGlobalLongName(const TString& name) |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 113 | { |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 114 | ASSERT(mGlobalMap); |
| 115 | const char* mappedName = mGlobalMap->Find(name.c_str()); |
| 116 | if (mappedName != NULL) |
| 117 | return mappedName; |
| 118 | int id = mGlobalMap->Size(); |
| 119 | TString rt = mapLongName(id, name, true); |
| 120 | mGlobalMap->Insert(name.c_str(), rt.c_str()); |
| 121 | return rt; |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 122 | } |