Intel SPMD Program Compiler  1.9.1
util.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2013, Intel Corporation
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above copyright
13  notice, this list of conditions and the following disclaimer in the
14  documentation and/or other materials provided with the distribution.
15 
16  * Neither the name of Intel Corporation nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 /** @file util.h
35 
36  @brief
37 */
38 
39 #ifndef ISPC_UTIL_H
40 #define ISPC_UTIL_H
41 
42 #include "ispc.h"
43 #ifdef ISPC_IS_WINDOWS
44 #include <stdarg.h>
45 #endif
46 
47 struct SourcePos;
48 
49 /** Rounds up the given value to the next power of two, if it isn't a power
50  of two already. */
51 inline uint32_t RoundUpPow2(uint32_t v) {
52  v--;
53  v |= v >> 1;
54  v |= v >> 2;
55  v |= v >> 4;
56  v |= v >> 8;
57  v |= v >> 16;
58  return v+1;
59 }
60 
61 #ifdef __GNUG__
62 #define PRINTF_FUNC __attribute__ \
63  ((__format__ (__printf__, 2, 3)))
64 #else
65 #define PRINTF_FUNC
66 #endif // __GNUG__
67 
68 // for cross-platform compatibility
69 #ifdef ISPC_IS_WINDOWS
70 int vasprintf(char **sptr, const char *fmt, va_list argv);
71 int asprintf(char **sptr, const char *fmt, ...);
72 #endif
73 
74 /** Prints a debugging message. These messages are only printed if
75  g->debugPrint is \c true. In addition to a program source code
76  position to associate with the message, a printf()-style format string
77  is passed along with any values needed for items in the format
78  string.
79 */
80 void Debug(SourcePos p, const char *format, ...) PRINTF_FUNC;
81 
82 /** Prints a warning about an issue found during compilation. Compilation
83  can still continue after warnings; they are purely informative for the
84  user. In addition to a program source code position to associate with
85  the message, a printf()-style format string is passed along with any
86  values needed for items in the format string.
87 */
88 void Warning(SourcePos p, const char *format, ...) PRINTF_FUNC;
89 
90 /** Prints an error message. It is assumd that compilation can not be
91  successfully completed after an error has been issued, though the
92  system tries to continue compiling as much as possible, in order to be
93  able to issue any subsequent error messages. In addition to a program
94  source code position to associate with the message, a printf()-style
95  format string is passed along with any values needed for items in the
96  format string.
97 */
98 void Error(SourcePos p, const char *format, ...) PRINTF_FUNC;
99 
100 /** Prints a message about a potential performance issue in the user's
101  code. These messages are purely informative and don't affect the
102  completion of compilation. In addition to a program source code
103  position to associate with the message, a printf()-style format string
104  is passed along with any values needed for items in the format
105  string.
106 */
107 void PerformanceWarning(SourcePos p, const char *format, ...) PRINTF_FUNC;
108 
109 /** Reports a fatal error that causes the program to terminate. This
110  should only be used for cases where there is an internal error in the
111  compiler.
112  */
113 #define FATAL(message) FatalError(__FILE__, __LINE__, message)
114 
115 /** This function generally shouldn't be called directly, but should be
116  used via the FATAL macro, which includes the file and line number where
117  the error was issued.
118  */
119 void FatalError(const char *file, int line, const char *message);
120 
121 /** Returns the number of single-character edits needed to transform
122  between the two strings.
123 
124  @param str1 First string
125  @param str2 Second string
126  @param maxDist Maximum number of single-character edits allowed
127  @return Number of single-character edits to transform from str1
128  to str2, or maxDist+1 if it's not psosible to do so
129  in fewer than maxDist steps
130 */
131 int StringEditDistance(const std::string &str1, const std::string &str2,
132  int maxDist);
133 
134 /** Given a string and a set of candidate strings, returns the set of
135  candidates that are "close" to the given string, where distance is
136  measured by the number of single-character changes needed to transform
137  between the two. An empty vector may be returned if none of the
138  options is close to \c str.
139  */
140 std::vector<std::string> MatchStrings(const std::string &str,
141  const std::vector<std::string> &options);
142 
143 /** Given the current working directory and a filename relative to that
144  directory, this function returns the final directory that the resulting
145  file is in and the base name of the file itself. */
146 void GetDirectoryAndFileName(const std::string &currentDir,
147  const std::string &relativeName,
148  std::string *directory, std::string *filename);
149 
150 /** Verification routine, which ensures that DataLayout of the module being
151  compiled is compatible with DataLayout of the library. At the moment we
152  allow the library DataLayout to a subset of the module DataLayout (and
153  extra floating point and vector types to be defined for module) or
154  empty library DataLayout.
155  */
156 bool VerifyDataLayoutCompatibility(const std::string &module_dl,
157  const std::string &lib_dl);
158 
159 /** Print the given string to the given FILE, assuming the given output
160  column width. Break words as needed to avoid words spilling past the
161  last column. */
162 void PrintWithWordBreaks(const char *buf, int indent, int columnWidth,
163  FILE *out);
164 
165 /** Returns the width of the terminal where the compiler is running.
166  Finding this out may fail in a variety of reasonable situations (piping
167  compiler output to 'less', redirecting output to a file, running the
168  compiler under a debuffer; in this case, just return a reasonable
169  default.
170  */
171 int TerminalWidth();
172 
173 #endif // ISPC_UTIL_H
void PerformanceWarning(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:426
void GetDirectoryAndFileName(const std::string &currentDir, const std::string &relativeName, std::string *directory, std::string *filename)
Definition: util.cpp:547
int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist)
Definition: util.cpp:482
void Error(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:385
bool VerifyDataLayoutCompatibility(const std::string &module_dl, const std::string &lib_dl)
Definition: util.cpp:603
void PrintWithWordBreaks(const char *buf, int indent, int columnWidth, FILE *out)
Definition: util.cpp:230
int TerminalWidth()
Definition: util.cpp:81
uint32_t RoundUpPow2(uint32_t v)
Definition: util.h:51
Representation of a range of positions in a source file.
Definition: ispc.h:134
void Warning(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:410
#define PRINTF_FUNC
Definition: util.h:65
void FatalError(const char *file, int line, const char *message)
Definition: util.cpp:455
std::vector< std::string > MatchStrings(const std::string &str, const std::vector< std::string > &options)
Definition: util.cpp:519
Main ispc.header file. Defines Target, Globals and Opt classes.
void Debug(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:398