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 | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 18 | stream << id << "_"; |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 19 | stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size()); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 20 | return stream.str(); |
| 21 | } |
| 22 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 23 | LongNameMap* gLongNameMapInstance = NULL; |
| 24 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 25 | } // anonymous namespace |
| 26 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 27 | LongNameMap::LongNameMap() |
| 28 | : refCount(0) |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 29 | { |
| 30 | } |
| 31 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 32 | LongNameMap::~LongNameMap() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | // static |
| 37 | LongNameMap* LongNameMap::GetInstance() |
| 38 | { |
| 39 | if (gLongNameMapInstance == NULL) |
| 40 | gLongNameMapInstance = new LongNameMap; |
| 41 | gLongNameMapInstance->refCount++; |
| 42 | return gLongNameMapInstance; |
| 43 | } |
| 44 | |
| 45 | void LongNameMap::Release() |
| 46 | { |
| 47 | ASSERT(gLongNameMapInstance == this); |
| 48 | ASSERT(refCount > 0); |
| 49 | refCount--; |
| 50 | if (refCount == 0) { |
| 51 | delete gLongNameMapInstance; |
| 52 | gLongNameMapInstance = NULL; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | const char* LongNameMap::Find(const char* originalName) const |
| 57 | { |
| 58 | std::map<std::string, std::string>::const_iterator it = mLongNameMap.find( |
| 59 | originalName); |
| 60 | if (it != mLongNameMap.end()) |
| 61 | return (*it).second.c_str(); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | void LongNameMap::Insert(const char* originalName, const char* mappedName) |
| 66 | { |
| 67 | mLongNameMap.insert(std::map<std::string, std::string>::value_type( |
| 68 | originalName, mappedName)); |
| 69 | } |
| 70 | |
| 71 | int LongNameMap::Size() const |
| 72 | { |
| 73 | return mLongNameMap.size(); |
| 74 | } |
| 75 | |
| 76 | MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap) |
| 77 | { |
| 78 | ASSERT(globalMap); |
| 79 | mGlobalMap = globalMap; |
| 80 | } |
| 81 | |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 82 | void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) |
| 83 | { |
| 84 | ASSERT(symbol != NULL); |
kbr@chromium.org | 2215211 | 2011-10-26 01:18:28 +0000 | [diff] [blame] | 85 | if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) { |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 86 | switch (symbol->getQualifier()) { |
| 87 | case EvqVaryingIn: |
| 88 | case EvqVaryingOut: |
| 89 | case EvqInvariantVaryingIn: |
| 90 | case EvqInvariantVaryingOut: |
zmo@google.com | 89c9813 | 2012-01-06 02:17:03 +0000 | [diff] [blame] | 91 | case EvqUniform: |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 92 | symbol->setSymbol( |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 93 | mapGlobalLongName(symbol->getSymbol())); |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 94 | break; |
| 95 | default: |
| 96 | symbol->setSymbol( |
| 97 | mapLongName(symbol->getId(), symbol->getSymbol(), false)); |
| 98 | break; |
| 99 | }; |
| 100 | } |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 101 | } |
| 102 | |
zmo@google.com | 216aa5e | 2011-06-17 22:31:32 +0000 | [diff] [blame] | 103 | bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node) |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 104 | { |
zmo@google.com | 216aa5e | 2011-06-17 22:31:32 +0000 | [diff] [blame] | 105 | if (node->getInit()) |
| 106 | node->getInit()->traverse(this); |
zmo@google.com | fd747b8 | 2011-04-23 01:30:07 +0000 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 110 | TString MapLongVariableNames::mapGlobalLongName(const TString& name) |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 111 | { |
zmo@google.com | b9f64aa | 2012-01-20 00:35:15 +0000 | [diff] [blame] | 112 | ASSERT(mGlobalMap); |
| 113 | const char* mappedName = mGlobalMap->Find(name.c_str()); |
| 114 | if (mappedName != NULL) |
| 115 | return mappedName; |
| 116 | int id = mGlobalMap->Size(); |
| 117 | TString rt = mapLongName(id, name, true); |
| 118 | mGlobalMap->Insert(name.c_str(), rt.c_str()); |
| 119 | return rt; |
zmo@google.com | 24c08c4 | 2011-05-27 17:40:48 +0000 | [diff] [blame] | 120 | } |