Demangler.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Ryujinx.HLE.OsHle.Diagnostics
  5. {
  6. static class Demangler
  7. {
  8. private static readonly Dictionary<string, string> BuiltinTypes = new Dictionary<string, string>
  9. {
  10. { "v", "void" },
  11. { "w", "wchar_t" },
  12. { "b", "bool" },
  13. { "c", "char" },
  14. { "a", "signed char" },
  15. { "h", "unsigned char" },
  16. { "s", "short" },
  17. { "t", "unsigned short" },
  18. { "i", "int" },
  19. { "j", "unsigned int" },
  20. { "l", "long" },
  21. { "m", "unsigned long" },
  22. { "x", "long long" },
  23. { "y", "unsigned long long" },
  24. { "n", "__int128" },
  25. { "o", "unsigned __int128" },
  26. { "f", "float" },
  27. { "d", "double" },
  28. { "e", "long double" },
  29. { "g", "__float128" },
  30. { "z", "..." },
  31. { "Dd", "__iec559_double" },
  32. { "De", "__iec559_float128" },
  33. { "Df", "__iec559_float" },
  34. { "Dh", "__iec559_float16" },
  35. { "Di", "char32_t" },
  36. { "Ds", "char16_t" },
  37. { "Da", "decltype(auto)" },
  38. { "Dn", "std::nullptr_t" },
  39. };
  40. private static readonly Dictionary<string, string> SubstitutionExtra = new Dictionary<string, string>
  41. {
  42. {"Sa", "std::allocator"},
  43. {"Sb", "std::basic_string"},
  44. {"Ss", "std::basic_string<char, ::std::char_traits<char>, ::std::allocator<char>>"},
  45. {"Si", "std::basic_istream<char, ::std::char_traits<char>>"},
  46. {"So", "std::basic_ostream<char, ::std::char_traits<char>>"},
  47. {"Sd", "std::basic_iostream<char, ::std::char_traits<char>>"}
  48. };
  49. private static int FromBase36(string encoded)
  50. {
  51. string base36 = "0123456789abcdefghijklmnopqrstuvwxyz";
  52. char[] reversedEncoded = encoded.ToLower().ToCharArray().Reverse().ToArray();
  53. int result = 0;
  54. for (int i = 0; i < reversedEncoded.Length; i++)
  55. {
  56. char c = reversedEncoded[i];
  57. int value = base36.IndexOf(c);
  58. if (value == -1)
  59. return -1;
  60. result += value * (int)Math.Pow(36, i);
  61. }
  62. return result;
  63. }
  64. private static string GetCompressedValue(string compression, List<string> compressionData, out int pos)
  65. {
  66. string res = null;
  67. bool canHaveUnqualifiedName = false;
  68. pos = -1;
  69. if (compressionData.Count == 0 || !compression.StartsWith("S"))
  70. return null;
  71. if (compression.Length >= 2 && SubstitutionExtra.TryGetValue(compression.Substring(0, 2), out string substitutionValue))
  72. {
  73. pos = 1;
  74. res = substitutionValue;
  75. compression = compression.Substring(2);
  76. }
  77. else if (compression.StartsWith("St"))
  78. {
  79. pos = 1;
  80. canHaveUnqualifiedName = true;
  81. res = "std";
  82. compression = compression.Substring(2);
  83. }
  84. else if (compression.StartsWith("S_"))
  85. {
  86. pos = 1;
  87. res = compressionData[0];
  88. canHaveUnqualifiedName = true;
  89. compression = compression.Substring(2);
  90. }
  91. else
  92. {
  93. int id = -1;
  94. int underscorePos = compression.IndexOf('_');
  95. if (underscorePos == -1)
  96. return null;
  97. string partialId = compression.Substring(1, underscorePos - 1);
  98. id = FromBase36(partialId);
  99. if (id == -1 || compressionData.Count <= (id + 1))
  100. {
  101. return null;
  102. }
  103. res = compressionData[id + 1];
  104. pos = partialId.Length + 1;
  105. canHaveUnqualifiedName= true;
  106. compression = compression.Substring(pos);
  107. }
  108. if (res != null)
  109. {
  110. if (canHaveUnqualifiedName)
  111. {
  112. List<string> type = ReadName(compression, compressionData, out int endOfNameType);
  113. if (endOfNameType != -1 && type != null)
  114. {
  115. pos += endOfNameType;
  116. res = res + "::" + type[type.Count - 1];
  117. }
  118. }
  119. }
  120. return res;
  121. }
  122. private static List<string> ReadName(string mangled, List<string> compressionData, out int pos, bool isNested = true)
  123. {
  124. List<string> res = new List<string>();
  125. string charCountString = null;
  126. int charCount = 0;
  127. int i;
  128. pos = -1;
  129. for (i = 0; i < mangled.Length; i++)
  130. {
  131. char chr = mangled[i];
  132. if (charCountString == null)
  133. {
  134. if (ReadCVQualifiers(chr) != null)
  135. {
  136. continue;
  137. }
  138. if (chr == 'S')
  139. {
  140. string data = GetCompressedValue(mangled.Substring(i), compressionData, out pos);
  141. if (pos == -1)
  142. {
  143. return null;
  144. }
  145. if (res.Count == 0)
  146. res.Add(data);
  147. else
  148. res.Add(res[res.Count - 1] + "::" + data);
  149. i += pos;
  150. if (i < mangled.Length && mangled[i] == 'E')
  151. {
  152. break;
  153. }
  154. continue;
  155. }
  156. else if (chr == 'E')
  157. {
  158. break;
  159. }
  160. }
  161. if (Char.IsDigit(chr))
  162. {
  163. charCountString += chr;
  164. }
  165. else
  166. {
  167. if (!int.TryParse(charCountString, out charCount))
  168. {
  169. return null;
  170. }
  171. string demangledPart = mangled.Substring(i, charCount);
  172. if (res.Count == 0)
  173. res.Add(demangledPart);
  174. else
  175. res.Add(res[res.Count - 1] + "::" + demangledPart);
  176. i = i + charCount - 1;
  177. charCount = 0;
  178. charCountString = null;
  179. if (!isNested)
  180. break;
  181. }
  182. }
  183. if (res.Count == 0)
  184. {
  185. return null;
  186. }
  187. pos = i;
  188. return res;
  189. }
  190. private static string ReadBuiltinType(string mangledType, out int pos)
  191. {
  192. string res = null;
  193. string possibleBuiltinType;
  194. pos = -1;
  195. possibleBuiltinType = mangledType[0].ToString();
  196. if (!BuiltinTypes.TryGetValue(possibleBuiltinType, out res))
  197. {
  198. if (mangledType.Length >= 2)
  199. {
  200. // Try to match the first 2 chars if the first call failed
  201. possibleBuiltinType = mangledType.Substring(0, 2);
  202. BuiltinTypes.TryGetValue(possibleBuiltinType, out res);
  203. }
  204. }
  205. if (res != null)
  206. pos = possibleBuiltinType.Length;
  207. return res;
  208. }
  209. private static string ReadCVQualifiers(char qualifier)
  210. {
  211. if (qualifier == 'r')
  212. return "restricted";
  213. else if (qualifier == 'V')
  214. return "volatile";
  215. else if (qualifier == 'K')
  216. return "const";
  217. return null;
  218. }
  219. private static string ReadRefQualifiers(char qualifier)
  220. {
  221. if (qualifier == 'R')
  222. return "&";
  223. else if (qualifier == 'O')
  224. return "&&";
  225. return null;
  226. }
  227. private static string ReadSpecialQualifiers(char qualifier)
  228. {
  229. if (qualifier == 'P')
  230. return "*";
  231. else if (qualifier == 'C')
  232. return "complex";
  233. else if (qualifier == 'G')
  234. return "imaginary";
  235. return null;
  236. }
  237. private static List<string> ReadParameters(string mangledParams, List<string> compressionData, out int pos)
  238. {
  239. List<string> res = new List<string>();
  240. List<string> refQualifiers = new List<string>();
  241. string parsedTypePart = null;
  242. string currentRefQualifiers = null;
  243. string currentBuiltinType = null;
  244. string currentSpecialQualifiers = null;
  245. string currentCompressedValue = null;
  246. int i = 0;
  247. pos = -1;
  248. for (i = 0; i < mangledParams.Length; i++)
  249. {
  250. if (currentBuiltinType != null)
  251. {
  252. string currentCVQualifier = String.Join(" ", refQualifiers);
  253. // Try to mimic the compression indexing
  254. if (currentRefQualifiers != null)
  255. {
  256. compressionData.Add(currentBuiltinType + currentRefQualifiers);
  257. }
  258. if (refQualifiers.Count != 0)
  259. {
  260. compressionData.Add(currentBuiltinType + " " + currentCVQualifier + currentRefQualifiers);
  261. }
  262. if (currentSpecialQualifiers != null)
  263. {
  264. compressionData.Add(currentBuiltinType + " " + currentCVQualifier + currentRefQualifiers + currentSpecialQualifiers);
  265. }
  266. if (currentRefQualifiers == null && currentCVQualifier == null && currentSpecialQualifiers == null)
  267. {
  268. compressionData.Add(currentBuiltinType);
  269. }
  270. currentBuiltinType = null;
  271. currentCompressedValue = null;
  272. currentCVQualifier = null;
  273. currentRefQualifiers = null;
  274. refQualifiers.Clear();
  275. currentSpecialQualifiers = null;
  276. }
  277. char chr = mangledParams[i];
  278. string part = mangledParams.Substring(i);
  279. // Try to read qualifiers
  280. parsedTypePart = ReadCVQualifiers(chr);
  281. if (parsedTypePart != null)
  282. {
  283. refQualifiers.Add(parsedTypePart);
  284. // need more data
  285. continue;
  286. }
  287. parsedTypePart = ReadRefQualifiers(chr);
  288. if (parsedTypePart != null)
  289. {
  290. currentRefQualifiers = parsedTypePart;
  291. // need more data
  292. continue;
  293. }
  294. parsedTypePart = ReadSpecialQualifiers(chr);
  295. if (parsedTypePart != null)
  296. {
  297. currentSpecialQualifiers = parsedTypePart;
  298. // need more data
  299. continue;
  300. }
  301. // TODO: extended-qualifier?
  302. if (part.StartsWith("S"))
  303. {
  304. parsedTypePart = GetCompressedValue(part, compressionData, out pos);
  305. if (pos != -1 && parsedTypePart != null)
  306. {
  307. currentCompressedValue = parsedTypePart;
  308. i += pos;
  309. res.Add(currentCompressedValue + " " + String.Join(" ", refQualifiers) + currentRefQualifiers + currentSpecialQualifiers);
  310. currentBuiltinType = null;
  311. currentCompressedValue = null;
  312. currentRefQualifiers = null;
  313. refQualifiers.Clear();
  314. currentSpecialQualifiers = null;
  315. continue;
  316. }
  317. pos = -1;
  318. return null;
  319. }
  320. else if (part.StartsWith("N"))
  321. {
  322. part = part.Substring(1);
  323. List<string> name = ReadName(part, compressionData, out pos);
  324. if (pos != -1 && name != null)
  325. {
  326. i += pos + 1;
  327. res.Add(name[name.Count - 1] + " " + String.Join(" ", refQualifiers) + currentRefQualifiers + currentSpecialQualifiers);
  328. currentBuiltinType = null;
  329. currentCompressedValue = null;
  330. currentRefQualifiers = null;
  331. refQualifiers.Clear();
  332. currentSpecialQualifiers = null;
  333. continue;
  334. }
  335. }
  336. // Try builting
  337. parsedTypePart = ReadBuiltinType(part, out pos);
  338. if (pos == -1)
  339. {
  340. return null;
  341. }
  342. currentBuiltinType = parsedTypePart;
  343. res.Add(currentBuiltinType + " " + String.Join(" ", refQualifiers) + currentRefQualifiers + currentSpecialQualifiers);
  344. i = i + pos -1;
  345. }
  346. pos = i;
  347. return res;
  348. }
  349. private static string ParseFunctionName(string mangled)
  350. {
  351. List<string> compressionData = new List<string>();
  352. int pos = 0;
  353. string res;
  354. bool isNested = mangled.StartsWith("N");
  355. // If it's start with "N" it must be a nested function name
  356. if (isNested)
  357. mangled = mangled.Substring(1);
  358. compressionData = ReadName(mangled, compressionData, out pos, isNested);
  359. if (pos == -1)
  360. return null;
  361. res = compressionData[compressionData.Count - 1];
  362. compressionData.Remove(res);
  363. mangled = mangled.Substring(pos + 1);
  364. // more data? maybe not a data name so...
  365. if (mangled != String.Empty)
  366. {
  367. List<string> parameters = ReadParameters(mangled, compressionData, out pos);
  368. // parameters parsing error, we return the original data to avoid information loss.
  369. if (pos == -1)
  370. return null;
  371. parameters = parameters.Select(outer => outer.Trim()).ToList();
  372. res += "(" + String.Join(", ", parameters) + ")";
  373. }
  374. return res;
  375. }
  376. public static string Parse(string originalMangled)
  377. {
  378. if (originalMangled.StartsWith("_Z"))
  379. {
  380. // We assume that we have a name (TOOD: support special names)
  381. string res = ParseFunctionName(originalMangled.Substring(2));
  382. if (res == null)
  383. return originalMangled;
  384. return res;
  385. }
  386. return originalMangled;
  387. }
  388. }
  389. }