blob: b5210ecdc398cf606a440bfc0a47ab45b6b9cfb9 [file] [log] [blame]
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_UTILS_H_
4#define ART_SRC_UTILS_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Brian Carlstromdb4d5402011-08-09 12:18:28 -07007#include "logging.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07008#include "stringpiece.h"
Elliott Hughesc7ac37f2011-08-12 12:21:58 -07009#include "stringprintf.h"
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070010
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070011namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070012
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070013class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070014class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070015class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Carl Shapiroa2e18e12011-06-21 18:57:55 -070017template<typename T>
18static inline bool IsPowerOfTwo(T x) {
19 return (x & (x - 1)) == 0;
20}
21
Carl Shapiroa2e18e12011-06-21 18:57:55 -070022template<typename T>
23static inline bool IsAligned(T x, int n) {
24 CHECK(IsPowerOfTwo(n));
25 return (x & (n - 1)) == 0;
26}
27
Carl Shapiroa2e18e12011-06-21 18:57:55 -070028template<typename T>
29static inline bool IsAligned(T* x, int n) {
30 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
31}
32
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070033// Check whether an N-bit two's-complement representation can hold value.
34static inline bool IsInt(int N, word value) {
35 CHECK_LT(0, N);
36 CHECK_LT(N, kBitsPerWord);
37 word limit = static_cast<word>(1) << (N - 1);
38 return (-limit <= value) && (value < limit);
39}
40
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070041static inline bool IsUint(int N, word value) {
42 CHECK_LT(0, N);
43 CHECK_LT(N, kBitsPerWord);
44 word limit = static_cast<word>(1) << N;
45 return (0 <= value) && (value < limit);
46}
47
Carl Shapiroa2e18e12011-06-21 18:57:55 -070048static inline bool IsAbsoluteUint(int N, word value) {
49 CHECK_LT(0, N);
50 CHECK_LT(N, kBitsPerWord);
51 if (value < 0) value = -value;
52 return IsUint(N, value);
53}
54
Ian Rogersb033c752011-07-20 12:22:35 -070055static inline int32_t Low16Bits(int32_t value) {
56 return static_cast<int32_t>(value & 0xffff);
57}
58
59static inline int32_t High16Bits(int32_t value) {
60 return static_cast<int32_t>(value >> 16);
61}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070062
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070063static inline int32_t Low32Bits(int64_t value) {
64 return static_cast<int32_t>(value);
65}
66
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070067static inline int32_t High32Bits(int64_t value) {
68 return static_cast<int32_t>(value >> 32);
69}
70
Carl Shapiro61e019d2011-07-14 16:53:09 -070071template<typename T>
72static inline T RoundDown(T x, int n) {
73 CHECK(IsPowerOfTwo(n));
74 return (x & -n);
75}
76
77template<typename T>
78static inline T RoundUp(T x, int n) {
79 return RoundDown(x + n - 1, n);
80}
81
Carl Shapiroa2e18e12011-06-21 18:57:55 -070082// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070083// figure 3-3, page 48, where the function is called clp2.
84static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
85 x = x - 1;
86 x = x | (x >> 1);
87 x = x | (x >> 2);
88 x = x | (x >> 4);
89 x = x | (x >> 8);
90 x = x | (x >> 16);
91 return x + 1;
92}
93
94// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070095// figure 5-2, page 66, where the function is called pop.
96static inline int CountOneBits(uint32_t x) {
97 x = x - ((x >> 1) & 0x55555555);
98 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
99 x = (x + (x >> 4)) & 0x0F0F0F0F;
100 x = x + (x >> 8);
101 x = x + (x >> 16);
102 return static_cast<int>(x & 0x0000003F);
103}
104
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700105#define CLZ(x) __builtin_clz(x)
106
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700107static inline bool NeedsEscaping(uint16_t ch) {
108 return (ch < ' ' || ch > '~');
109}
110
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700111static inline std::string PrintableChar(uint16_t ch) {
112 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700113 result += '\'';
114 if (NeedsEscaping(ch)) {
115 StringAppendF(&result, "\\u%04x", ch);
116 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700117 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700118 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700119 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700120 return result;
121}
122
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700123// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700124template<typename StringT>
125static inline std::string PrintableString(const StringT& s) {
126 std::string result;
127 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700128 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700129 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700130 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700131 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700132 } else {
133 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700134 }
135 }
136 result += '"';
137 return result;
138}
139
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700140// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
141// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
142// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700143std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700144
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700145// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
146// "a.b.C.m(II)V" (depending on the value of 'with_signature').
147std::string PrettyMethod(const Method* m, bool with_signature);
148
149// Returns a human-readable form of the name of the *class* of the given object.
150// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700151// be "java.lang.String". Given an array of int, the output would be "int[]".
152// Given String.class, the output would be "java.lang.Class<java.lang.String>".
153std::string PrettyType(const Object* obj);
154
Elliott Hughes79082e32011-08-25 12:07:32 -0700155// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
156// of the JNI spec.
157std::string MangleForJni(const std::string& s);
158
159// Returns the JNI native function name for the non-overloaded method 'm'.
160std::string JniShortName(const Method* m);
161// Returns the JNI native function name for the overloaded method 'm'.
162std::string JniLongName(const Method* m);
163
buzbeec143c552011-08-20 17:38:58 -0700164std::string ReadFileToString(const char* file_name);
165
Elliott Hughese27955c2011-08-26 15:21:24 -0700166// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
167std::string GetIsoDate();
168
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700169} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700170
171#endif // ART_SRC_UTILS_H_