OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COURGETTE_IMAGE_UTILS_H_ | 5 #ifndef COURGETTE_IMAGE_UTILS_H_ |
6 #define COURGETTE_IMAGE_UTILS_H_ | 6 #define COURGETTE_IMAGE_UTILS_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 | 9 |
10 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently | 10 // COURGETTE_HISTOGRAM_TARGETS prints out a histogram of how frequently |
11 // different target addresses are referenced. Purely for debugging. | 11 // different target addresses are referenced. Purely for debugging. |
12 #define COURGETTE_HISTOGRAM_TARGETS 0 | 12 #define COURGETTE_HISTOGRAM_TARGETS 0 |
13 | 13 |
14 namespace courgette { | 14 namespace courgette { |
15 | 15 |
16 typedef uint32 RVA; | 16 typedef uint32 RVA; |
17 | 17 |
| 18 // A Label is a symbolic reference to an address. Unlike a conventional |
| 19 // assembly language, we always know the address. The address will later be |
| 20 // stored in a table and the Label will be replaced with the index into the |
| 21 // table. |
| 22 // TODO(huangs): Make this a struct, and remove "_" from member names. |
| 23 class Label { |
| 24 public: |
| 25 enum : int { kNoIndex = -1 }; |
| 26 explicit Label(RVA rva) : rva_(rva) {} |
| 27 |
| 28 bool operator==(const Label& other) const { |
| 29 return rva_ == other.rva_ && index_ == other.index_ && |
| 30 count_ == other.count_; |
| 31 } |
| 32 |
| 33 RVA rva_ = 0; // Address referred to by the label. |
| 34 int index_ = kNoIndex; // Index of address in address table. |
| 35 int32 count_ = 0; |
| 36 }; |
| 37 |
18 // These helper functions avoid the need for casts in the main code. | 38 // These helper functions avoid the need for casts in the main code. |
19 inline uint16 ReadU16(const uint8* address, size_t offset) { | 39 inline uint16 ReadU16(const uint8* address, size_t offset) { |
20 return *reinterpret_cast<const uint16*>(address + offset); | 40 return *reinterpret_cast<const uint16*>(address + offset); |
21 } | 41 } |
22 | 42 |
23 inline uint32 ReadU32(const uint8* address, size_t offset) { | 43 inline uint32 ReadU32(const uint8* address, size_t offset) { |
24 return *reinterpret_cast<const uint32*>(address + offset); | 44 return *reinterpret_cast<const uint32*>(address + offset); |
25 } | 45 } |
26 | 46 |
27 inline uint64 ReadU64(const uint8* address, size_t offset) { | 47 inline uint64 ReadU64(const uint8* address, size_t offset) { |
28 return *reinterpret_cast<const uint64*>(address + offset); | 48 return *reinterpret_cast<const uint64*>(address + offset); |
29 } | 49 } |
30 | 50 |
31 inline uint16 Read16LittleEndian(const void* address) { | 51 inline uint16 Read16LittleEndian(const void* address) { |
32 return *reinterpret_cast<const uint16*>(address); | 52 return *reinterpret_cast<const uint16*>(address); |
33 } | 53 } |
34 | 54 |
35 inline uint32 Read32LittleEndian(const void* address) { | 55 inline uint32 Read32LittleEndian(const void* address) { |
36 return *reinterpret_cast<const uint32*>(address); | 56 return *reinterpret_cast<const uint32*>(address); |
37 } | 57 } |
38 | 58 |
39 inline uint64 Read64LittleEndian(const void* address) { | 59 inline uint64 Read64LittleEndian(const void* address) { |
40 return *reinterpret_cast<const uint64*>(address); | 60 return *reinterpret_cast<const uint64*>(address); |
41 } | 61 } |
42 | 62 |
43 } // namespace courgette | 63 } // namespace courgette |
44 | 64 |
45 #endif // COURGETTE_IMAGE_UTILS_H_ | 65 #endif // COURGETTE_IMAGE_UTILS_H_ |
OLD | NEW |