blob: 78d655e56a15f976f39478b02c31fc12354da87f [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
Elliott Hughes34023802011-08-30 12:06:17 -070011#include <string>
12#include <vector>
13
Carl Shapiro6b6b5f02011-06-21 15:05:09 -070014namespace art {
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070015
Elliott Hughesa2501992011-08-26 19:39:54 -070016class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070017class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070018class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070019class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070020
Carl Shapiroa2e18e12011-06-21 18:57:55 -070021template<typename T>
22static inline bool IsPowerOfTwo(T x) {
23 return (x & (x - 1)) == 0;
24}
25
Carl Shapiroa2e18e12011-06-21 18:57:55 -070026template<typename T>
27static inline bool IsAligned(T x, int n) {
28 CHECK(IsPowerOfTwo(n));
29 return (x & (n - 1)) == 0;
30}
31
Carl Shapiroa2e18e12011-06-21 18:57:55 -070032template<typename T>
33static inline bool IsAligned(T* x, int n) {
34 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
35}
36
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070037// Check whether an N-bit two's-complement representation can hold value.
38static inline bool IsInt(int N, word value) {
39 CHECK_LT(0, N);
40 CHECK_LT(N, kBitsPerWord);
41 word limit = static_cast<word>(1) << (N - 1);
42 return (-limit <= value) && (value < limit);
43}
44
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070045static inline bool IsUint(int N, word value) {
46 CHECK_LT(0, N);
47 CHECK_LT(N, kBitsPerWord);
48 word limit = static_cast<word>(1) << N;
49 return (0 <= value) && (value < limit);
50}
51
Carl Shapiroa2e18e12011-06-21 18:57:55 -070052static inline bool IsAbsoluteUint(int N, word value) {
53 CHECK_LT(0, N);
54 CHECK_LT(N, kBitsPerWord);
55 if (value < 0) value = -value;
56 return IsUint(N, value);
57}
58
Ian Rogersb033c752011-07-20 12:22:35 -070059static inline int32_t Low16Bits(int32_t value) {
60 return static_cast<int32_t>(value & 0xffff);
61}
62
63static inline int32_t High16Bits(int32_t value) {
64 return static_cast<int32_t>(value >> 16);
65}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070066
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070067static inline int32_t Low32Bits(int64_t value) {
68 return static_cast<int32_t>(value);
69}
70
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070071static inline int32_t High32Bits(int64_t value) {
72 return static_cast<int32_t>(value >> 32);
73}
74
Carl Shapiro61e019d2011-07-14 16:53:09 -070075template<typename T>
76static inline T RoundDown(T x, int n) {
77 CHECK(IsPowerOfTwo(n));
78 return (x & -n);
79}
80
81template<typename T>
82static inline T RoundUp(T x, int n) {
83 return RoundDown(x + n - 1, n);
84}
85
Carl Shapiroa2e18e12011-06-21 18:57:55 -070086// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070087// figure 3-3, page 48, where the function is called clp2.
88static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
89 x = x - 1;
90 x = x | (x >> 1);
91 x = x | (x >> 2);
92 x = x | (x >> 4);
93 x = x | (x >> 8);
94 x = x | (x >> 16);
95 return x + 1;
96}
97
98// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070099// figure 5-2, page 66, where the function is called pop.
100static inline int CountOneBits(uint32_t x) {
101 x = x - ((x >> 1) & 0x55555555);
102 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
103 x = (x + (x >> 4)) & 0x0F0F0F0F;
104 x = x + (x >> 8);
105 x = x + (x >> 16);
106 return static_cast<int>(x & 0x0000003F);
107}
108
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700109#define CLZ(x) __builtin_clz(x)
110
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700111static inline bool NeedsEscaping(uint16_t ch) {
112 return (ch < ' ' || ch > '~');
113}
114
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700115static inline std::string PrintableChar(uint16_t ch) {
116 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700117 result += '\'';
118 if (NeedsEscaping(ch)) {
119 StringAppendF(&result, "\\u%04x", ch);
120 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700121 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700122 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700123 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700124 return result;
125}
126
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700127// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700128template<typename StringT>
129static inline std::string PrintableString(const StringT& s) {
130 std::string result;
131 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700132 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700133 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700134 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700135 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700136 } else {
137 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700138 }
139 }
140 result += '"';
141 return result;
142}
143
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700144// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
145// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
146// "java.lang.String[]", and so forth.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700147std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700148
Elliott Hughesa2501992011-08-26 19:39:54 -0700149// Returns a human-readable signature for 'f'. Something like "a.b.C.f".
150std::string PrettyField(const Field* f);
151
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700152// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
153// "a.b.C.m(II)V" (depending on the value of 'with_signature').
buzbeedfd3d702011-08-28 12:56:51 -0700154std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700155
156// Returns a human-readable form of the name of the *class* of the given object.
157// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700158// be "java.lang.String". Given an array of int, the output would be "int[]".
159// Given String.class, the output would be "java.lang.Class<java.lang.String>".
160std::string PrettyType(const Object* obj);
161
Elliott Hughes79082e32011-08-25 12:07:32 -0700162// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
163// of the JNI spec.
164std::string MangleForJni(const std::string& s);
165
166// Returns the JNI native function name for the non-overloaded method 'm'.
167std::string JniShortName(const Method* m);
168// Returns the JNI native function name for the overloaded method 'm'.
169std::string JniLongName(const Method* m);
170
Elliott Hughesd92bec42011-09-02 17:04:36 -0700171bool ReadFileToString(const std::string& file_name, std::string* result);
buzbeec143c552011-08-20 17:38:58 -0700172
Elliott Hughese27955c2011-08-26 15:21:24 -0700173// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
174std::string GetIsoDate();
175
Elliott Hughes34023802011-08-30 12:06:17 -0700176// Splits a string using the given delimiter character into a vector of
177// strings. Empty strings will be omitted.
178void Split(const std::string& s, char delim, std::vector<std::string>& result);
179
Elliott Hughes42ee1422011-09-06 12:33:32 -0700180// Returns the calling thread's tid. (The C libraries don't expose this.)
181pid_t GetTid();
182
Elliott Hughesdcc24742011-09-07 14:02:44 -0700183// Sets the name of the current thread. The name may be truncated to an
184// implementation-defined limit.
185void SetThreadName(const char* name);
186
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700187} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700188
189#endif // ART_SRC_UTILS_H_