blob: 5c2a44ae5e6fd81418fb997ab5b8c92005e963fa [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;
15
Carl Shapiroa2e18e12011-06-21 18:57:55 -070016template<typename T>
17static inline bool IsPowerOfTwo(T x) {
18 return (x & (x - 1)) == 0;
19}
20
Carl Shapiroa2e18e12011-06-21 18:57:55 -070021template<typename T>
22static inline bool IsAligned(T x, int n) {
23 CHECK(IsPowerOfTwo(n));
24 return (x & (n - 1)) == 0;
25}
26
Carl Shapiroa2e18e12011-06-21 18:57:55 -070027template<typename T>
28static inline bool IsAligned(T* x, int n) {
29 return IsAligned(reinterpret_cast<uintptr_t>(x), n);
30}
31
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070032// Check whether an N-bit two's-complement representation can hold value.
33static inline bool IsInt(int N, word value) {
34 CHECK_LT(0, N);
35 CHECK_LT(N, kBitsPerWord);
36 word limit = static_cast<word>(1) << (N - 1);
37 return (-limit <= value) && (value < limit);
38}
39
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070040static inline bool IsUint(int N, word value) {
41 CHECK_LT(0, N);
42 CHECK_LT(N, kBitsPerWord);
43 word limit = static_cast<word>(1) << N;
44 return (0 <= value) && (value < limit);
45}
46
Carl Shapiroa2e18e12011-06-21 18:57:55 -070047static inline bool IsAbsoluteUint(int N, word value) {
48 CHECK_LT(0, N);
49 CHECK_LT(N, kBitsPerWord);
50 if (value < 0) value = -value;
51 return IsUint(N, value);
52}
53
Ian Rogersb033c752011-07-20 12:22:35 -070054static inline int32_t Low16Bits(int32_t value) {
55 return static_cast<int32_t>(value & 0xffff);
56}
57
58static inline int32_t High16Bits(int32_t value) {
59 return static_cast<int32_t>(value >> 16);
60}
Carl Shapiroa2e18e12011-06-21 18:57:55 -070061
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070062static inline int32_t Low32Bits(int64_t value) {
63 return static_cast<int32_t>(value);
64}
65
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -070066static inline int32_t High32Bits(int64_t value) {
67 return static_cast<int32_t>(value >> 32);
68}
69
Carl Shapiro61e019d2011-07-14 16:53:09 -070070template<typename T>
71static inline T RoundDown(T x, int n) {
72 CHECK(IsPowerOfTwo(n));
73 return (x & -n);
74}
75
76template<typename T>
77static inline T RoundUp(T x, int n) {
78 return RoundDown(x + n - 1, n);
79}
80
Carl Shapiroa2e18e12011-06-21 18:57:55 -070081// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiro1fb86202011-06-27 17:43:13 -070082// figure 3-3, page 48, where the function is called clp2.
83static inline uint32_t RoundUpToPowerOfTwo(uint32_t x) {
84 x = x - 1;
85 x = x | (x >> 1);
86 x = x | (x >> 2);
87 x = x | (x >> 4);
88 x = x | (x >> 8);
89 x = x | (x >> 16);
90 return x + 1;
91}
92
93// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
Carl Shapiroa2e18e12011-06-21 18:57:55 -070094// figure 5-2, page 66, where the function is called pop.
95static inline int CountOneBits(uint32_t x) {
96 x = x - ((x >> 1) & 0x55555555);
97 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
98 x = (x + (x >> 4)) & 0x0F0F0F0F;
99 x = x + (x >> 8);
100 x = x + (x >> 16);
101 return static_cast<int>(x & 0x0000003F);
102}
103
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700104#define CLZ(x) __builtin_clz(x)
105
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700106static inline bool NeedsEscaping(uint16_t ch) {
107 return (ch < ' ' || ch > '~');
108}
109
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700110static inline std::string PrintableChar(uint16_t ch) {
111 std::string result;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700112 result += '\'';
113 if (NeedsEscaping(ch)) {
114 StringAppendF(&result, "\\u%04x", ch);
115 } else {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700116 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700117 }
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700118 result += '\'';
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700119 return result;
120}
121
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700122// TODO: assume the content is UTF-8, and show code point escapes?
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700123template<typename StringT>
124static inline std::string PrintableString(const StringT& s) {
125 std::string result;
126 result += '"';
127 for (typename StringT::iterator it = s.begin(); it != s.end(); ++it) {
128 char ch = *it;
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700129 if (NeedsEscaping(ch)) {
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700130 StringAppendF(&result, "\\x%02x", ch & 0xff);
Elliott Hughes46b92ba2011-08-12 17:57:34 -0700131 } else {
132 result += ch;
Elliott Hughesc7ac37f2011-08-12 12:21:58 -0700133 }
134 }
135 result += '"';
136 return result;
137}
138
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700139// Returns a human-readable equivalent of 'descriptor'. So "I" would be "int",
140// "[[I" would be "int[][]", "[Ljava/lang/String;" would be
141// "java.lang.String[]", and so forth.
Elliott Hughes11e45072011-08-16 17:40:46 -0700142std::string PrettyDescriptor(const StringPiece& descriptor);
143
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700144// Returns a human-readable signature for 'm'. Something like "a.b.C.m" or
145// "a.b.C.m(II)V" (depending on the value of 'with_signature').
146std::string PrettyMethod(const Method* m, bool with_signature);
147
148// Returns a human-readable form of the name of the *class* of the given object.
149// So given an instance of java.lang.String, the output would
Elliott Hughes11e45072011-08-16 17:40:46 -0700150// be "java.lang.String". Given an array of int, the output would be "int[]".
151// Given String.class, the output would be "java.lang.Class<java.lang.String>".
152std::string PrettyType(const Object* obj);
153
buzbeec143c552011-08-20 17:38:58 -0700154std::string ReadFileToString(const char* file_name);
155
Carl Shapiro6b6b5f02011-06-21 15:05:09 -0700156} // namespace art
Carl Shapiroa5d5cfd2011-06-21 12:46:59 -0700157
158#endif // ART_SRC_UTILS_H_