blob: 4b3e57ec507eb586ca08af92bbd687e6c121c558 [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 Hughesa2501992011-08-26 19:39:54 -070013class Field;
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070014class Method;
Elliott Hughes11e45072011-08-16 17:40:46 -070015class Object;
Elliott Hughes5174fe62011-08-23 15:12:35 -070016class String;
Elliott Hughes11e45072011-08-16 17:40:46 -070017
Carl Shapiroa2e18e12011-06-21 18:57:55 -070018template<typename T>
19static inline bool IsPowerOfTwo(T x) {
20 return (x & (x - 1)) == 0;
21}
22
Carl Shapiroa2e18e12011-06-21 18:57:55 -070023template<typename T>
24static inline bool IsAligned(T x, int n) {
25 CHECK(IsPowerOfTwo(n));
26 return (x & (n - 1)) == 0;
27}
28
Carl Shapiroa2e18e12011-06-21 18:57:55 -070029template<typename T>
30static inline bool IsAligned(T* x, int n) {
31 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
32}
33
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070034// Check whether an N-bit two's-complement representation can hold value.
35static 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 Shapiroa5d5cfd2011-06-21 12:46:59 -070042static 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 Shapiroa2e18e12011-06-21 18:57:55 -070049static 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 Rogersb033c752011-07-20 12:22:35 -070056static inline int32_t Low16Bits(int32_t value) {
57 return static_cast<int32_t>(value & 0xffff);
58}
59
60static inline int32_t High16Bits(int32_t value) {
61 return static_cast<int32_t>(value >> 16);
62}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070063
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070064static inline int32_t Low32Bits(int64_t value) {
65 return static_cast<int32_t>(value);
66}
67
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070068static inline int32_t High32Bits(int64_t value) {
69 return static_cast<int32_t>(value >> 32);
70}
71
Carl Shapiro61e019d2011-07-14 16:53:09 -070072template<typename T>
73static inline T RoundDown(T x, int n) {
74 CHECK(IsPowerOfTwo(n));
75 return (x & -n);
76}
77
78template<typename T>
79static inline T RoundUp(T x, int n) {
80 return RoundDown(x + n - 1, n);
81}
82
Carl Shapiroa2e18e12011-06-21 18:57:55 -070083// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070084// figure 3-3, page 48, where the function is called clp2.
85static 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 Shapiroa2e18e12011-06-21 18:57:55 -070096// figure 5-2, page 66, where the function is called pop.
97static 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 Carlstromdb4d5402011-08-09 12:18:28 -0700106#define CLZ(x) __builtin_clz(x)
107
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700108static inline bool NeedsEscaping(uint16_t ch) {
109 return (ch < ' ' || ch > '~');
110}
111
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700112static inline std::string PrintableChar(uint16_t ch) {
113 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700114 result += '\'';
115 if (NeedsEscaping(ch)) {
116 StringAppendF(&result, "\\u%04x", ch);
117 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700118 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700119 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700120 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700121 return result;
122}
123
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700124// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700125template<typename StringT>
126static inline std::string PrintableString(const StringT& s) {
127 std::string result;
128 result += '"';
Elliott Hughesb465ab02011-08-24 11:21:21 -0700129 for (typename StringT::const_iterator it = s.begin(); it != s.end(); ++it) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700130 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700131 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700132 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700133 } else {
134 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700135 }
136 }
137 result += '"';
138 return result;
139}
140
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700141// 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 Hughes5174fe62011-08-23 15:12:35 -0700144std::string PrettyDescriptor(const String* descriptor);
Elliott Hughes11e45072011-08-16 17:40:46 -0700145
Elliott Hughesa2501992011-08-26 19:39:54 -0700146// Returns a human-readable signature for 'f'. Something like "a.b.C.f".
147std::string PrettyField(const Field* f);
148
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700149// 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').
buzbeedfd3d702011-08-28 12:56:51 -0700151std::string PrettyMethod(const Method* m, bool with_signature = true);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700152
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 Hughes11e45072011-08-16 17:40:46 -0700155// 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>".
157std::string PrettyType(const Object* obj);
158
Elliott Hughes79082e32011-08-25 12:07:32 -0700159// Performs JNI name mangling as described in section 11.3 "Linking Native Methods"
160// of the JNI spec.
161std::string MangleForJni(const std::string& s);
162
163// Returns the JNI native function name for the non-overloaded method 'm'.
164std::string JniShortName(const Method* m);
165// Returns the JNI native function name for the overloaded method 'm'.
166std::string JniLongName(const Method* m);
167
buzbeec143c552011-08-20 17:38:58 -0700168std::string ReadFileToString(const char* file_name);
169
Elliott Hughese27955c2011-08-26 15:21:24 -0700170// Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
171std::string GetIsoDate();
172
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700173} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700174
175#endif // ART_SRC_UTILS_H_