Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_UTILS_H_ |
| 4 | #define ART_SRC_UTILS_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "globals.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 7 | #include "logging.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 8 | #include "stringpiece.h" |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 9 | #include "stringprintf.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 10 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 11 | #include <pthread.h> |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 15 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 17 | class Class; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 18 | class Field; |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 19 | class Method; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 20 | class Object; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 21 | class String; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 22 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 23 | template<typename T> |
| 24 | static inline bool IsPowerOfTwo(T x) { |
| 25 | return (x & (x - 1)) == 0; |
| 26 | } |
| 27 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 28 | template<typename T> |
| 29 | static inline bool IsAligned(T x, int n) { |
| 30 | CHECK(IsPowerOfTwo(n)); |
| 31 | return (x & (n - 1)) == 0; |
| 32 | } |
| 33 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 34 | template<typename T> |
| 35 | static inline bool IsAligned(T* x, int n) { |
| 36 | return IsAligned(reinterpret_cast<uintptr_t>(x), n); |
| 37 | } |
| 38 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 39 | // Check whether an N-bit two's-complement representation can hold value. |
| 40 | static inline bool IsInt(int N, word value) { |
| 41 | CHECK_LT(0, N); |
| 42 | CHECK_LT(N, kBitsPerWord); |
| 43 | word limit = static_cast<word>(1) << (N - 1); |
| 44 | return (-limit <= value) && (value < limit); |
| 45 | } |
| 46 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 47 | static inline bool IsUint(int N, word value) { |
| 48 | CHECK_LT(0, N); |
| 49 | CHECK_LT(N, kBitsPerWord); |
| 50 | word limit = static_cast<word>(1) << N; |
| 51 | return (0 <= value) && (value < limit); |
| 52 | } |
| 53 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 54 | static inline bool IsAbsoluteUint(int N, word value) { |
| 55 | CHECK_LT(0, N); |
| 56 | CHECK_LT(N, kBitsPerWord); |
| 57 | if (value < 0) value = -value; |
| 58 | return IsUint(N, value); |
| 59 | } |
| 60 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 61 | static inline int32_t Low16Bits(int32_t value) { |
| 62 | return static_cast<int32_t>(value & 0xffff); |
| 63 | } |
| 64 | |
| 65 | static inline int32_t High16Bits(int32_t value) { |
| 66 | return static_cast<int32_t>(value >> 16); |
| 67 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 68 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 69 | static inline int32_t Low32Bits(int64_t value) { |
| 70 | return static_cast<int32_t>(value); |
| 71 | } |
| 72 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 73 | static inline int32_t High32Bits(int64_t value) { |
| 74 | return static_cast<int32_t>(value >> 32); |
| 75 | } |
| 76 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 77 | template<typename T> |
| 78 | static inline T RoundDown(T x, int n) { |
| 79 | CHECK(IsPowerOfTwo(n)); |
| 80 | return (x & -n); |
| 81 | } |
| 82 | |
| 83 | template<typename T> |
| 84 | static inline T RoundUp(T x, int n) { |
| 85 | return RoundDown(x + n - 1, n); |
| 86 | } |
| 87 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 88 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 89 | // figure 3-3, page 48, where the function is called clp2. |
| 90 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 91 | x = x - 1; |
| 92 | x = x | (x >> 1); |
| 93 | x = x | (x >> 2); |
| 94 | x = x | (x >> 4); |
| 95 | x = x | (x >> 8); |
| 96 | x = x | (x >> 16); |
| 97 | return x + 1; |
| 98 | } |
| 99 | |
| 100 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 101 | // figure 5-2, page 66, where the function is called pop. |
| 102 | static inline int CountOneBits(uint32_t x) { |
| 103 | x = x - ((x >> 1) & 0x55555555); |
| 104 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 105 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 106 | x = x + (x >> 8); |
| 107 | x = x + (x >> 16); |
| 108 | return static_cast<int>(x & 0x0000003F); |
| 109 | } |
| 110 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 111 | #define CLZ(x) __builtin_clz(x) |
| 112 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 113 | static inline bool NeedsEscaping(uint16_t ch) { |
| 114 | return (ch < ' ' || ch > '~'); |
| 115 | } |
| 116 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 117 | static inline std::string PrintableChar(uint16_t ch) { |
| 118 | std::string result; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 119 | result += '\''; |
| 120 | if (NeedsEscaping(ch)) { |
| 121 | StringAppendF(&result, "\\u%04x", ch); |
| 122 | } else { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 123 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 124 | } |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 125 | result += '\''; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 126 | return result; |
| 127 | } |
| 128 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 129 | // TODO: assume the content is UTF-8, and show code point escapes? |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 130 | template<typename StringT> |
| 131 | static inline std::string PrintableString(const StringT& s) { |
| 132 | std::string result; |
| 133 | result += '"'; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 134 | for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 135 | char ch = *it; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 136 | if (NeedsEscaping(ch)) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 137 | StringAppendF(&result, "\\x%02x", ch & 0xff); |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 138 | } else { |
| 139 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | result += '"'; |
| 143 | return result; |
| 144 | } |
| 145 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 146 | // Used to implement PrettyClass, PrettyField, PrettyMethod, and PrettyTypeOf, |
| 147 | // one of which is probably more useful to you. |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 148 | // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", |
| 149 | // "[[I" would be "int[][]", "[Ljava/lang/String;" would be |
| 150 | // "java.lang.String[]", and so forth. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 151 | std::string PrettyDescriptor(const String* descriptor); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame^] | 152 | std::string PrettyDescriptor(const std::string& descriptor); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 153 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 154 | // Returns a human-readable signature for 'f'. Something like "a.b.C.f" or |
| 155 | // "int a.b.C.f" (depending on the value of 'with_type'). |
| 156 | std::string PrettyField(const Field* f, bool with_type = true); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 157 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 158 | // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or |
| 159 | // "a.b.C.m(II)V" (depending on the value of 'with_signature'). |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 160 | std::string PrettyMethod(const Method* m, bool with_signature = true); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 161 | |
| 162 | // Returns a human-readable form of the name of the *class* of the given object. |
| 163 | // So given an instance of java.lang.String, the output would |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 164 | // be "java.lang.String". Given an array of int, the output would be "int[]". |
| 165 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 166 | std::string PrettyTypeOf(const Object* obj); |
| 167 | |
| 168 | // Returns a human-readable form of the name of the given class. |
| 169 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
| 170 | std::string PrettyClass(const Class* c); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 171 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 172 | // Performs JNI name mangling as described in section 11.3 "Linking Native Methods" |
| 173 | // of the JNI spec. |
| 174 | std::string MangleForJni(const std::string& s); |
| 175 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 176 | // Turn "java.lang.String" into "Ljava/lang/String;". |
| 177 | std::string DotToDescriptor(const char* class_name); |
| 178 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 179 | // Tests whether 's' is a valid class name. |
| 180 | // name_or_descriptor |
| 181 | // true => "java/lang/String" |
| 182 | // false => "Ljava/lang/String;" (i.e. "descriptor") |
| 183 | // dot_or_slash |
| 184 | // true => "java.lang.String" |
| 185 | // false => "java/lang/String" (i.e. "dot or slash") |
| 186 | bool IsValidClassName(const char* s, bool name_or_descriptor, bool dot_or_slash); |
| 187 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 188 | // Returns the JNI native function name for the non-overloaded method 'm'. |
| 189 | std::string JniShortName(const Method* m); |
| 190 | // Returns the JNI native function name for the overloaded method 'm'. |
| 191 | std::string JniLongName(const Method* m); |
| 192 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 193 | bool ReadFileToString(const std::string& file_name, std::string* result); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 194 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 195 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 196 | std::string GetIsoDate(); |
| 197 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 198 | // Splits a string using the given delimiter character into a vector of |
| 199 | // strings. Empty strings will be omitted. |
| 200 | void Split(const std::string& s, char delim, std::vector<std::string>& result); |
| 201 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 202 | // Returns the calling thread's tid. (The C libraries don't expose this.) |
| 203 | pid_t GetTid(); |
| 204 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 205 | // Sets the name of the current thread. The name may be truncated to an |
| 206 | // implementation-defined limit. |
| 207 | void SetThreadName(const char* name); |
| 208 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 209 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 210 | |
| 211 | #endif // ART_SRC_UTILS_H_ |