blob: 0c7e1a9cb809b2cd39ea86dc9d6fa3148da28b89 [file] [log] [blame]
zmo@google.comfd747b82011-04-23 01:30:07 +00001//
zmo@google.com89c98132012-01-06 02:17:03 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
zmo@google.comfd747b82011-04-23 01:30:07 +00003// 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
9namespace {
10
zmo@google.comb9f64aa2012-01-20 00:35:15 +000011TString mapLongName(int id, const TString& name, bool isGlobal)
zmo@google.comfd747b82011-04-23 01:30:07 +000012{
kbr@chromium.org22152112011-10-26 01:18:28 +000013 ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
zmo@google.comfd747b82011-04-23 01:30:07 +000014 TStringStream stream;
zmo@google.com24c08c42011-05-27 17:40:48 +000015 stream << "webgl_";
zmo@google.comb9f64aa2012-01-20 00:35:15 +000016 if (isGlobal)
17 stream << "g";
zmo@google.com24c08c42011-05-27 17:40:48 +000018 stream << id << "_";
kbr@chromium.org22152112011-10-26 01:18:28 +000019 stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
zmo@google.comfd747b82011-04-23 01:30:07 +000020 return stream.str();
21}
22
zmo@google.comb9f64aa2012-01-20 00:35:15 +000023LongNameMap* gLongNameMapInstance = NULL;
24
zmo@google.comfd747b82011-04-23 01:30:07 +000025} // anonymous namespace
26
zmo@google.comb9f64aa2012-01-20 00:35:15 +000027LongNameMap::LongNameMap()
28 : refCount(0)
zmo@google.com24c08c42011-05-27 17:40:48 +000029{
30}
31
zmo@google.comb9f64aa2012-01-20 00:35:15 +000032LongNameMap::~LongNameMap()
33{
34}
35
36// static
37LongNameMap* LongNameMap::GetInstance()
38{
39 if (gLongNameMapInstance == NULL)
40 gLongNameMapInstance = new LongNameMap;
41 gLongNameMapInstance->refCount++;
42 return gLongNameMapInstance;
43}
44
45void 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
56const 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
65void 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
71int LongNameMap::Size() const
72{
73 return mLongNameMap.size();
74}
75
76MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
77{
78 ASSERT(globalMap);
79 mGlobalMap = globalMap;
80}
81
zmo@google.comfd747b82011-04-23 01:30:07 +000082void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
83{
84 ASSERT(symbol != NULL);
kbr@chromium.org22152112011-10-26 01:18:28 +000085 if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) {
zmo@google.com24c08c42011-05-27 17:40:48 +000086 switch (symbol->getQualifier()) {
87 case EvqVaryingIn:
88 case EvqVaryingOut:
89 case EvqInvariantVaryingIn:
90 case EvqInvariantVaryingOut:
zmo@google.com89c98132012-01-06 02:17:03 +000091 case EvqUniform:
zmo@google.com24c08c42011-05-27 17:40:48 +000092 symbol->setSymbol(
zmo@google.comb9f64aa2012-01-20 00:35:15 +000093 mapGlobalLongName(symbol->getSymbol()));
zmo@google.com24c08c42011-05-27 17:40:48 +000094 break;
95 default:
96 symbol->setSymbol(
97 mapLongName(symbol->getId(), symbol->getSymbol(), false));
98 break;
99 };
100 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000101}
102
zmo@google.com216aa5e2011-06-17 22:31:32 +0000103bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node)
zmo@google.comfd747b82011-04-23 01:30:07 +0000104{
zmo@google.com216aa5e2011-06-17 22:31:32 +0000105 if (node->getInit())
106 node->getInit()->traverse(this);
zmo@google.comfd747b82011-04-23 01:30:07 +0000107 return true;
108}
109
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000110TString MapLongVariableNames::mapGlobalLongName(const TString& name)
zmo@google.com24c08c42011-05-27 17:40:48 +0000111{
zmo@google.comb9f64aa2012-01-20 00:35:15 +0000112 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.com24c08c42011-05-27 17:40:48 +0000120}