Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
util.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010-2019, 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 #pragma once
40 
41 #include "ispc.h"
42 #ifdef ISPC_HOST_IS_WINDOWS
43 #include <stdarg.h>
44 #endif
45 
46 struct SourcePos;
47 
48 /** Rounds up the given value to the next power of two, if it isn't a power
49  of two already. */
50 inline uint32_t RoundUpPow2(uint32_t v) {
51  v--;
52  v |= v >> 1;
53  v |= v >> 2;
54  v |= v >> 4;
55  v |= v >> 8;
56  v |= v >> 16;
57  return v + 1;
58 }
59 
60 #ifdef __GNUG__
61 #define PRINTF_FUNC __attribute__((format(printf, 2, 3)))
62 #else
63 #define PRINTF_FUNC
64 #endif // __GNUG__
65 
66 // for cross-platform compatibility
67 #ifdef ISPC_HOST_IS_WINDOWS
68 int vasprintf(char **sptr, const char *fmt, va_list argv);
69 int asprintf(char **sptr, const char *fmt, ...);
70 #endif
71 
72 /** Prints a debugging message. These messages are only printed if
73  g->debugPrint is \c true. In addition to a program source code
74  position to associate with the message, a printf()-style format string
75  is passed along with any values needed for items in the format
76  string.
77 */
78 void Debug(SourcePos p, const char *format, ...) PRINTF_FUNC;
79 
80 /** Prints a warning about an issue found during compilation. Compilation
81  can still continue after warnings; they are purely informative for the
82  user. In addition to a program source code position to associate with
83  the message, a printf()-style format string is passed along with any
84  values needed for items in the format string.
85 */
86 void Warning(SourcePos p, const char *format, ...) PRINTF_FUNC;
87 
88 /** Prints an error message. It is assumd that compilation can not be
89  successfully completed after an error has been issued, though the
90  system tries to continue compiling as much as possible, in order to be
91  able to issue any subsequent error messages. In addition to a program
92  source code position to associate with the message, a printf()-style
93  format string is passed along with any values needed for items in the
94  format string.
95 */
96 void Error(SourcePos p, const char *format, ...) PRINTF_FUNC;
97 
98 /** Prints a message about a potential performance issue in the user's
99  code. These messages are purely informative and don't affect the
100  completion of compilation. In addition to a program source code
101  position to associate with the message, a printf()-style format string
102  is passed along with any values needed for items in the format
103  string.
104 */
105 void PerformanceWarning(SourcePos p, const char *format, ...) PRINTF_FUNC;
106 
107 /** Reports that unreachable location is reached. This is a kind of fatal error
108  that causes the program to terminate.
109  */
110 #define UNREACHABLE() FatalError(__FILE__, __LINE__, "unreachable code")
111 
112 /** Reports a fatal error that causes the program to terminate. This
113  should only be used for cases where there is an internal error in the
114  compiler.
115  */
116 #define FATAL(message) FatalError(__FILE__, __LINE__, message)
117 
118 /** This function generally shouldn't be called directly, but should be
119  used via the FATAL macro, which includes the file and line number where
120  the error was issued.
121  */
122 [[noreturn]] void FatalError(const char *file, int line, const char *message);
123 
124 /** Asserts that expr parameter is not equal to zero. Otherwise the program is
125  terminated with propper error message and with file and line number where
126  the assertion happend.
127  */
128 #define Assert(expr) ((void)((expr) ? 0 : ((void)DoAssert(__FILE__, __LINE__, #expr), 0)))
129 
130 /** This function generally shouldn't be called directly, but should be
131  used via the Assert macro, which includes the file and line number where
132  the assertion happens.
133  Note: avoid adding [[noreturn]] as VS2017 treats Assert macros as never returning.
134  */
135 void DoAssert(const char *file, int line, const char *expr);
136 
137 /** Asserts that expr parameter is not equal to zero. Otherwise the program is
138  terminated with propper error message and with file and line number where
139  the assertion happend and the information about source position in the user
140  program, which has triggered the problem.
141  */
142 #define AssertPos(pos, expr) ((void)((expr) ? 0 : ((void)DoAssertPos(pos, __FILE__, __LINE__, #expr), 0)))
143 
144 /** This function generally shouldn't be called directly, but should be
145  used via the AssertPos macro, which includes the file and line number where
146  the assertion happens.
147  Note: avoid adding [[noreturn]] as VS2017 treats AssertPos macros as never returning.
148  */
149 void DoAssertPos(SourcePos pos, const char *file, int line, const char *expr);
150 
151 /** Returns the number of single-character edits needed to transform
152  between the two strings.
153 
154  @param str1 First string
155  @param str2 Second string
156  @param maxDist Maximum number of single-character edits allowed
157  @return Number of single-character edits to transform from str1
158  to str2, or maxDist+1 if it's not psosible to do so
159  in fewer than maxDist steps
160 */
161 int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist);
162 
163 /** Given a string and a set of candidate strings, returns the set of
164  candidates that are "close" to the given string, where distance is
165  measured by the number of single-character changes needed to transform
166  between the two. An empty vector may be returned if none of the
167  options is close to \c str.
168  */
169 std::vector<std::string> MatchStrings(const std::string &str, const std::vector<std::string> &options);
170 
171 /** Given the current working directory and a filename relative to that
172  directory, this function returns the final directory that the resulting
173  file is in and the base name of the file itself. */
174 void GetDirectoryAndFileName(const std::string &currentDir, const std::string &relativeName, std::string *directory,
175  std::string *filename);
176 
177 /** Verification routine, which ensures that DataLayout of the module being
178  compiled is compatible with DataLayout of the library. At the moment we
179  allow the library DataLayout to a subset of the module DataLayout (and
180  extra floating point and vector types to be defined for module) or
181  empty library DataLayout.
182  */
183 bool VerifyDataLayoutCompatibility(const std::string &module_dl, const std::string &lib_dl);
184 
185 /** Print the given string to the given FILE, assuming the given output
186  column width. Break words as needed to avoid words spilling past the
187  last column. */
188 void PrintWithWordBreaks(const char *buf, int indent, int columnWidth, FILE *out);
189 
190 /** Returns the width of the terminal where the compiler is running.
191  Finding this out may fail in a variety of reasonable situations (piping
192  compiler output to 'less', redirecting output to a file, running the
193  compiler under a debuffer; in this case, just return a reasonable
194  default.
195  */
196 int TerminalWidth();
197 
198 /** Returns true is the filepath represents stdin, otherwise false.
199  */
200 bool IsStdin(const char *);
void PerformanceWarning(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:397
void GetDirectoryAndFileName(const std::string &currentDir, const std::string &relativeName, std::string *directory, std::string *filename)
Definition: util.cpp:516
void DoAssert(const char *file, int line, const char *expr)
Definition: util.cpp:440
int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist)
Definition: util.cpp:455
void Error(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:351
bool VerifyDataLayoutCompatibility(const std::string &module_dl, const std::string &lib_dl)
Definition: util.cpp:568
void PrintWithWordBreaks(const char *buf, int indent, int columnWidth, FILE *out)
Definition: util.cpp:210
int TerminalWidth()
Definition: util.cpp:76
uint32_t RoundUpPow2(uint32_t v)
Definition: util.h:50
Representation of a range of positions in a source file.
Definition: ispc.h:123
void Warning(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:378
void DoAssertPos(SourcePos pos, const char *file, int line, const char *expr)
Definition: util.cpp:446
#define PRINTF_FUNC
Definition: util.h:63
void FatalError(const char *file, int line, const char *message)
Definition: util.cpp:434
bool IsStdin(const char *)
Definition: util.cpp:621
std::vector< std::string > MatchStrings(const std::string &str, const std::vector< std::string > &options)
Definition: util.cpp:490
Main ispc.header file. Defines Target, Globals and Opt classes.
void Debug(SourcePos p, const char *format,...) PRINTF_FUNC
Definition: util.cpp:366