• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- subzero/src/IceMangling.cpp - Cross test name mangling --*- C++ -*-===//
2 //
3 //                        The Subzero Code Generator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief Defines utility functions for name mangling for cross tests.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "IceDefs.h"
16 #include "IceGlobalContext.h"
17 #include "IceMangling.h"
18 
19 #include <cctype> // isdigit(), isupper()
20 #include <locale> // locale
21 
22 namespace Ice {
23 
24 using ManglerVector = llvm::SmallVector<char, 32>;
25 
26 namespace {
27 
28 // Scan a string for S[0-9A-Z]*_ patterns and replace them with
29 // S<num>_ where <num> is the next base-36 value.  If a type name
30 // legitimately contains that pattern, then the substitution will be
31 // made in error and most likely the link will fail.  In this case,
32 // the test classes can be rewritten not to use that pattern, which is
33 // much simpler and more reliable than implementing a full demangling
34 // parser.  Another substitution-in-error may occur if a type
35 // identifier ends with the pattern S[0-9A-Z]*, because an immediately
36 // following substitution string like "S1_" or "PS1_" may be combined
37 // with the previous type.
incrementSubstitutions(ManglerVector & OldName)38 void incrementSubstitutions(ManglerVector &OldName) {
39   const std::locale CLocale("C");
40   // Provide extra space in case the length of <num> increases.
41   ManglerVector NewName(OldName.size() * 2);
42   size_t OldPos = 0;
43   size_t NewPos = 0;
44   const size_t OldLen = OldName.size();
45   for (; OldPos < OldLen; ++OldPos, ++NewPos) {
46     if (OldName[OldPos] == '\0')
47       break;
48     if (OldName[OldPos] == 'S') {
49       // Search forward until we find _ or invalid character (including \0).
50       bool AllZs = true;
51       bool Found = false;
52       size_t Last;
53       for (Last = OldPos + 1; Last < OldLen; ++Last) {
54         char Ch = OldName[Last];
55         if (Ch == '_') {
56           Found = true;
57           break;
58         } else if (std::isdigit(Ch) || std::isupper(Ch, CLocale)) {
59           if (Ch != 'Z')
60             AllZs = false;
61         } else {
62           // Invalid character, stop searching.
63           break;
64         }
65       }
66       if (Found) {
67         NewName[NewPos++] = OldName[OldPos++]; // 'S'
68         size_t Length = Last - OldPos;
69         // NewPos and OldPos point just past the 'S'.
70         assert(NewName[NewPos - 1] == 'S');
71         assert(OldName[OldPos - 1] == 'S');
72         assert(OldName[OldPos + Length] == '_');
73         if (AllZs) {
74           // Replace N 'Z' characters with a '0' (if N=0) or '1' (if N>0)
75           // followed by N '0' characters.
76           NewName[NewPos++] = (Length ? '1' : '0');
77           for (size_t i = 0; i < Length; ++i) {
78             NewName[NewPos++] = '0';
79           }
80         } else {
81           // Iterate right-to-left and increment the base-36 number.
82           bool Carry = true;
83           for (size_t i = 0; i < Length; ++i) {
84             size_t Offset = Length - 1 - i;
85             char Ch = OldName[OldPos + Offset];
86             if (Carry) {
87               Carry = false;
88               switch (Ch) {
89               case '9':
90                 Ch = 'A';
91                 break;
92               case 'Z':
93                 Ch = '0';
94                 Carry = true;
95                 break;
96               default:
97                 ++Ch;
98                 break;
99               }
100             }
101             NewName[NewPos + Offset] = Ch;
102           }
103           NewPos += Length;
104         }
105         OldPos = Last;
106         // Fall through and let the '_' be copied across.
107       }
108     }
109     NewName[NewPos] = OldName[OldPos];
110   }
111   assert(NewName[NewPos] == '\0');
112   OldName = NewName;
113 }
114 
115 } // end of anonymous namespace
116 
117 // In this context, name mangling means to rewrite a symbol using a given
118 // prefix. For a C++ symbol, nest the original symbol inside the "prefix"
119 // namespace. For other symbols, just prepend the prefix.
mangleName(const std::string & Name)120 std::string mangleName(const std::string &Name) {
121   // An already-nested name like foo::bar() gets pushed down one level, making
122   // it equivalent to Prefix::foo::bar().
123   //   _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
124   // A non-nested but mangled name like bar() gets nested, making it equivalent
125   // to Prefix::bar().
126   //   _Z3barxyz ==> ZN6Prefix3barExyz
127   // An unmangled, extern "C" style name, gets a simple prefix:
128   //   bar ==> Prefixbar
129   if (!BuildDefs::dump() || getFlags().getTestPrefix().empty())
130     return Name;
131 
132   const std::string TestPrefix = getFlags().getTestPrefix();
133   unsigned PrefixLength = TestPrefix.length();
134   ManglerVector NameBase(1 + Name.length());
135   const size_t BufLen = 30 + Name.length() + PrefixLength;
136   ManglerVector NewName(BufLen);
137   uint32_t BaseLength = 0; // using uint32_t due to sscanf format string
138 
139   int ItemsParsed = sscanf(Name.c_str(), "_ZN%s", NameBase.data());
140   if (ItemsParsed == 1) {
141     // Transform _ZN3foo3barExyz ==> _ZN6Prefix3foo3barExyz
142     //   (splice in "6Prefix")          ^^^^^^^
143     snprintf(NewName.data(), BufLen, "_ZN%u%s%s", PrefixLength,
144              TestPrefix.c_str(), NameBase.data());
145     // We ignore the snprintf return value (here and below). If we somehow
146     // miscalculated the output buffer length, the output will be truncated,
147     // but it will be truncated consistently for all mangleName() calls on the
148     // same input string.
149     incrementSubstitutions(NewName);
150     return NewName.data();
151   }
152 
153   // Artificially limit BaseLength to 9 digits (less than 1 billion) because
154   // sscanf behavior is undefined on integer overflow. If there are more than 9
155   // digits (which we test by looking at the beginning of NameBase), then we
156   // consider this a failure to parse a namespace mangling, and fall back to
157   // the simple prefixing.
158   ItemsParsed = sscanf(Name.c_str(), "_Z%9u%s", &BaseLength, NameBase.data());
159   if (ItemsParsed == 2 && BaseLength <= strlen(NameBase.data()) &&
160       !isdigit(NameBase[0])) {
161     // Transform _Z3barxyz ==> _ZN6Prefix3barExyz
162     //                           ^^^^^^^^    ^
163     // (splice in "N6Prefix", and insert "E" after "3bar") But an "I" after the
164     // identifier indicates a template argument list terminated with "E";
165     // insert the new "E" before/after the old "E".  E.g.:
166     // Transform _Z3barIabcExyz ==> _ZN6Prefix3barIabcEExyz
167     //                                ^^^^^^^^         ^
168     // (splice in "N6Prefix", and insert "E" after "3barIabcE")
169     ManglerVector OrigName(Name.length());
170     ManglerVector OrigSuffix(Name.length());
171     uint32_t ActualBaseLength = BaseLength;
172     if (NameBase[ActualBaseLength] == 'I') {
173       ++ActualBaseLength;
174       while (NameBase[ActualBaseLength] != 'E' &&
175              NameBase[ActualBaseLength] != '\0')
176         ++ActualBaseLength;
177     }
178     strncpy(OrigName.data(), NameBase.data(), ActualBaseLength);
179     OrigName[ActualBaseLength] = '\0';
180     strcpy(OrigSuffix.data(), NameBase.data() + ActualBaseLength);
181     snprintf(NewName.data(), BufLen, "_ZN%u%s%u%sE%s", PrefixLength,
182              TestPrefix.c_str(), BaseLength, OrigName.data(),
183              OrigSuffix.data());
184     incrementSubstitutions(NewName);
185     return NewName.data();
186   }
187 
188   // Transform bar ==> Prefixbar
189   //                   ^^^^^^
190   return TestPrefix + Name;
191 }
192 
193 } // end of namespace Ice
194