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 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 11 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 12 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame^] | 13 | class Field; |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 14 | class Method; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 15 | class Object; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 16 | class String; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 17 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 18 | template<typename T> |
| 19 | static inline bool IsPowerOfTwo(T x) { |
| 20 | return (x & (x - 1)) == 0; |
| 21 | } |
| 22 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 23 | template<typename T> |
| 24 | static inline bool IsAligned(T x, int n) { |
| 25 | CHECK(IsPowerOfTwo(n)); |
| 26 | return (x & (n - 1)) == 0; |
| 27 | } |
| 28 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 29 | template<typename T> |
| 30 | static inline bool IsAligned(T* x, int n) { |
| 31 | return IsAligned(reinterpret_cast<uintptr_t>(x), n); |
| 32 | } |
| 33 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 34 | // Check whether an N-bit two's-complement representation can hold value. |
| 35 | static inline bool IsInt(int N, word value) { |
| 36 | CHECK_LT(0, N); |
| 37 | CHECK_LT(N, kBitsPerWord); |
| 38 | word limit = static_cast<word>(1) << (N - 1); |
| 39 | return (-limit <= value) && (value < limit); |
| 40 | } |
| 41 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 42 | static inline bool IsUint(int N, word value) { |
| 43 | CHECK_LT(0, N); |
| 44 | CHECK_LT(N, kBitsPerWord); |
| 45 | word limit = static_cast<word>(1) << N; |
| 46 | return (0 <= value) && (value < limit); |
| 47 | } |
| 48 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 49 | static inline bool IsAbsoluteUint(int N, word value) { |
| 50 | CHECK_LT(0, N); |
| 51 | CHECK_LT(N, kBitsPerWord); |
| 52 | if (value < 0) value = -value; |
| 53 | return IsUint(N, value); |
| 54 | } |
| 55 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 56 | static inline int32_t Low16Bits(int32_t value) { |
| 57 | return static_cast<int32_t>(value & 0xffff); |
| 58 | } |
| 59 | |
| 60 | static inline int32_t High16Bits(int32_t value) { |
| 61 | return static_cast<int32_t>(value >> 16); |
| 62 | } |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 63 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 64 | static inline int32_t Low32Bits(int64_t value) { |
| 65 | return static_cast<int32_t>(value); |
| 66 | } |
| 67 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 68 | static inline int32_t High32Bits(int64_t value) { |
| 69 | return static_cast<int32_t>(value >> 32); |
| 70 | } |
| 71 | |
Carl Shapiro | 61e019d | 2011-07-14 16:53:09 -0700 | [diff] [blame] | 72 | template<typename T> |
| 73 | static inline T RoundDown(T x, int n) { |
| 74 | CHECK(IsPowerOfTwo(n)); |
| 75 | return (x & -n); |
| 76 | } |
| 77 | |
| 78 | template<typename T> |
| 79 | static inline T RoundUp(T x, int n) { |
| 80 | return RoundDown(x + n - 1, n); |
| 81 | } |
| 82 | |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 83 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 84 | // figure 3-3, page 48, where the function is called clp2. |
| 85 | static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) { |
| 86 | x = x - 1; |
| 87 | x = x | (x >> 1); |
| 88 | x = x | (x >> 2); |
| 89 | x = x | (x >> 4); |
| 90 | x = x | (x >> 8); |
| 91 | x = x | (x >> 16); |
| 92 | return x + 1; |
| 93 | } |
| 94 | |
| 95 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
Carl Shapiro | a2e18e1 | 2011-06-21 18:57:55 -0700 | [diff] [blame] | 96 | // figure 5-2, page 66, where the function is called pop. |
| 97 | static inline int CountOneBits(uint32_t x) { |
| 98 | x = x - ((x >> 1) & 0x55555555); |
| 99 | x = (x & 0x33333333) + ((x >> 2) & 0x33333333); |
| 100 | x = (x + (x >> 4)) & 0x0F0F0F0F; |
| 101 | x = x + (x >> 8); |
| 102 | x = x + (x >> 16); |
| 103 | return static_cast<int>(x & 0x0000003F); |
| 104 | } |
| 105 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 106 | #define CLZ(x) __builtin_clz(x) |
| 107 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 108 | static inline bool NeedsEscaping(uint16_t ch) { |
| 109 | return (ch < ' ' || ch > '~'); |
| 110 | } |
| 111 | |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 112 | static inline std::string PrintableChar(uint16_t ch) { |
| 113 | std::string result; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 114 | result += '\''; |
| 115 | if (NeedsEscaping(ch)) { |
| 116 | StringAppendF(&result, "\\u%04x", ch); |
| 117 | } else { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 118 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 119 | } |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 120 | result += '\''; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 121 | return result; |
| 122 | } |
| 123 | |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 124 | // TODO: assume the content is UTF-8, and show code point escapes? |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 125 | template<typename StringT> |
| 126 | static inline std::string PrintableString(const StringT& s) { |
| 127 | std::string result; |
| 128 | result += '"'; |
Elliott Hughes | b465ab0 | 2011-08-24 11:21:21 -0700 | [diff] [blame] | 129 | for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 130 | char ch = *it; |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 131 | if (NeedsEscaping(ch)) { |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 132 | StringAppendF(&result, "\\x%02x", ch & 0xff); |
Elliott Hughes | 46b92ba | 2011-08-12 17:57:34 -0700 | [diff] [blame] | 133 | } else { |
| 134 | result += ch; |
Elliott Hughes | c7ac37f | 2011-08-12 12:21:58 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | result += '"'; |
| 138 | return result; |
| 139 | } |
| 140 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 141 | // Returns a human-readable equivalent of 'descriptor'. So "I" would be "int", |
| 142 | // "[[I" would be "int[][]", "[Ljava/lang/String;" would be |
| 143 | // "java.lang.String[]", and so forth. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 144 | std::string PrettyDescriptor(const String* descriptor); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 145 | |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame^] | 146 | // Returns a human-readable signature for 'f'. Something like "a.b.C.f". |
| 147 | std::string PrettyField(const Field* f); |
| 148 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 149 | // Returns a human-readable signature for 'm'. Something like "a.b.C.m" or |
| 150 | // "a.b.C.m(II)V" (depending on the value of 'with_signature'). |
buzbee | dfd3d70 | 2011-08-28 12:56:51 -0700 | [diff] [blame] | 151 | std::string PrettyMethod(const Method* m, bool with_signature = true); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 152 | |
| 153 | // Returns a human-readable form of the name of the *class* of the given object. |
| 154 | // So given an instance of java.lang.String, the output would |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 155 | // be "java.lang.String". Given an array of int, the output would be "int[]". |
| 156 | // Given String.class, the output would be "java.lang.Class<java.lang.String>". |
| 157 | std::string PrettyType(const Object* obj); |
| 158 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 159 | // Performs JNI name mangling as described in section 11.3 "Linking Native Methods" |
| 160 | // of the JNI spec. |
| 161 | std::string MangleForJni(const std::string& s); |
| 162 | |
| 163 | // Returns the JNI native function name for the non-overloaded method 'm'. |
| 164 | std::string JniShortName(const Method* m); |
| 165 | // Returns the JNI native function name for the overloaded method 'm'. |
| 166 | std::string JniLongName(const Method* m); |
| 167 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 168 | std::string ReadFileToString(const char* file_name); |
| 169 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 170 | // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format. |
| 171 | std::string GetIsoDate(); |
| 172 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 173 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 174 | |
| 175 | #endif // ART_SRC_UTILS_H_ |