HPCToolkit
ProcNameMgr.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************* System Include Files ****************************
61 
62 #include <string>
63 using std::string;
64 
65 
66 //*************************** User Include Files ****************************
67 
68 #include "ProcNameMgr.hpp"
69 
70 #include "diagnostics.h"
71 
72 
73 //*************************** Forward Declarations **************************
74 
75 //***************************************************************************
76 
77 //***************************************************************************
78 // ProcNameMgr
79 //***************************************************************************
80 
81 // Canonicalize C++ templates and overloading: Compute a 'generic'
82 // name for a templated function type:
83 // f<...> --> f<>
84 // f<...>() --> f<>()
85 // f<...>(T<...>* x) --> f<>(T<>* x)
86 // f<...>::foo() --> f<>::foo()
87 // f<resultT(objT::*)(a1,a2)>::foo() --> f<>::foo()
88 // f(T<...>* x) --> f(T<>* x)
89 //
90 // Be careful!
91 // operator<<() --> (no change)
92 // operator>>() --> (no change)
93 
94 // Invariants about function names:
95 // Templates may have multiple nested < >, but have at least one pair
96 // Excluding function pointer types, functions have at most one pair of ( )
97 
98 std::string
99 ProcNameMgr::canonicalizeCppTemplate(const std::string& name)
100 {
101  size_t posLangle = name.find_first_of('<'); // returns valid pos or npos
102 
103  // Ensure we have a name of the form: "...<..." but not "...<<..."
104  if ((0 < posLangle && posLangle < (name.length() - 1))
105  && name[posLangle+1] != '<') {
106 
107  string x = name.substr(0, posLangle /*len*/); // exclude '<'
108 
109  int nesting = 0;
110  for (uint i = posLangle; i < name.size(); i++) {
111  char c = name[i];
112 
113  bool save_c = (nesting == 0);
114  if (c == '<') {
115  nesting++;
116  }
117  else if (c == '>') {
118  nesting--;
119  }
120  save_c = (save_c || (nesting == 0));
121 
122  if (save_c) {
123  x += c;
124  }
125  }
126 
127  return x;
128  }
129 
130  return name;
131 }
132 
133 
134 //***************************************************************************
135 // CilkNameMgr
136 //***************************************************************************
137 
138 const string CilkNameMgr::cilkmain = "cilk_main";
139 
140 // Cilk creates four specializations of each procedure:
141 // fast: <x>
142 // slow: _cilk_<x>_slow
143 // import: _cilk_<x>_import
144 // export: mt_<x>
145 
146 const string CilkNameMgr::s_procSlow_pfx = "_cilk_";
147 const string CilkNameMgr::s_procSlow_sfx = "_slow";
148 
149 const string CilkNameMgr::s_procImport_pfx = "_cilk_";
150 const string CilkNameMgr::s_procImport_sfx = "_import";
151 
152 const string CilkNameMgr::s_procExport_pfx = "mt_";
153 const string CilkNameMgr::s_procExport_sfx = "";
154 
155 
156 // Cilk 'outlines' an inlet <x> within a procedure <proc>, creating
157 // three specializations:
158 // norm: _cilk_<proc>_<x>_inlet
159 // fast: _cilk_<proc>_<x>_inlet_fast
160 // slow: _cilk_<proc>_<x>_inlet_slow
161 
162 const string CilkNameMgr::s_inletNorm_pfx = "_cilk_";
163 const string CilkNameMgr::s_inletNorm_sfx = "_inlet";
164 
165 const string CilkNameMgr::s_inletFast_pfx = "_cilk_";
166 const string CilkNameMgr::s_inletFast_sfx = "_inlet_fast";
167 
168 const string CilkNameMgr::s_inletSlow_pfx = "_cilk_";
169 const string CilkNameMgr::s_inletSlow_sfx = "_inlet_slow";
170 
171 
172 string
173 CilkNameMgr::canonicalize(const string& name)
174 {
175  // ------------------------------------------------------------
176  // inlets: must test before procedures because of _slow suffix
177  // ------------------------------------------------------------
178  if (isGenerated(name, s_inletNorm_pfx, s_inletNorm_sfx)) {
179  return basename(name, s_inletNorm_pfx, s_inletNorm_sfx);
180  }
181  else if (isGenerated(name, s_inletFast_pfx, s_inletFast_sfx)) {
182  return basename(name, s_inletFast_pfx, s_inletFast_sfx);
183  }
184  else if (isGenerated(name, s_inletSlow_pfx, s_inletSlow_sfx)) {
185  return basename(name, s_inletSlow_pfx, s_inletSlow_sfx);
186  }
187 
188  // ------------------------------------------------------------
189  // procedures
190  // ------------------------------------------------------------
191  else if (isGenerated(name, s_procSlow_pfx, s_procSlow_sfx)) {
192  return basename(name, s_procSlow_pfx, s_procSlow_sfx);
193  }
194  else if (isGenerated(name, s_procImport_pfx, s_procImport_sfx)) {
195  return basename(name, s_procImport_pfx, s_procImport_sfx);
196  }
197  else if (isGenerated(name, s_procExport_pfx, s_procExport_sfx)) {
198  return basename(name, s_procExport_pfx, s_procExport_sfx);
199  }
200 
201  // ------------------------------------------------------------
202  // special case
203  // ------------------------------------------------------------
204  else if (name == "_cilk_cilk_main_import") {
205  // _cilk_cilk_main_import --> invoke_main_slow
206  return "invoke_main_slow";
207  }
208 
209  // ------------------------------------------------------------
210  // default
211  // ------------------------------------------------------------
212  else {
213  return name;
214  }
215 }
216 
217 
218 //***************************************************************************
219 
220 
static const std::string s_procImport_sfx
static const std::string cilkmain
static const std::string s_procSlow_sfx
static const std::string s_inletFast_pfx
static const std::string s_procExport_pfx
static const std::string s_procExport_sfx
virtual std::string canonicalize(const std::string &name)
unsigned int uint
Definition: uint.h:124
std::string canonicalizeCppTemplate(const std::string &name)
Definition: ProcNameMgr.cpp:99
static const std::string s_inletNorm_pfx
static const std::string s_procSlow_pfx
static const std::string s_inletSlow_sfx
static const std::string s_inletSlow_pfx
string basename(const char *fName)
Definition: FileUtil.cpp:90
static const std::string s_inletFast_sfx
static const std::string s_inletNorm_sfx
static const std::string s_procImport_pfx