author | ihse |
Tue, 09 May 2017 12:57:30 +0200 | |
changeset 45028 | b0ea3c0bfb81 |
parent 35784 | ddeed53a1c5a |
permissions | -rw-r--r-- |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
1 |
/* |
45028
b0ea3c0bfb81
8179889: Fix typographic errors in copyright headers
ihse
parents:
35784
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
35784 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
7 |
* |
35784 | 8 |
* - Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
14 |
* |
35784 | 15 |
* - Neither the name of Oracle nor the names of its |
16 |
* contributors may be used to endorse or promote products derived |
|
17 |
* from this software without specific prior written permission. |
|
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
18 |
* |
35784 | 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
30 |
*/ |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
31 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
32 |
#ifndef LIBJIMAGE_ENDIAN_HPP |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
33 |
#define LIBJIMAGE_ENDIAN_HPP |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
34 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
35 |
#include "inttypes.hpp" |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
36 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
37 |
// Selectable endian handling. Endian handlers are used when accessing values |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
38 |
// that are of unknown (until runtime) endian. The only requirement of the values |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
39 |
// accessed are that they are aligned to proper size boundaries (no misalignment.) |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
40 |
// To select an endian handler, one should call Endian::get_handler(big_endian); |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
41 |
// Where big_endian is true if big endian is required and false otherwise. The |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
42 |
// native endian handler can be fetched with Endian::get_native_handler(); |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
43 |
// To retrieve a value using the approprate endian, use one of the overloaded |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
44 |
// calls to get. To set a value, then use one of the overloaded set calls. |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
45 |
// Ex. |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
46 |
// s4 value; // Imported value; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
47 |
// ... |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
48 |
// Endian* endian = Endian::get_handler(true); // Use big endian |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
49 |
// s4 corrected = endian->get(value); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
50 |
// endian->set(value, 1); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
51 |
// |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
52 |
class Endian { |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
53 |
public: |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
54 |
virtual u2 get(u2 x) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
55 |
virtual u4 get(u4 x) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
56 |
virtual u8 get(u8 x) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
57 |
virtual s2 get(s2 x) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
58 |
virtual s4 get(s4 x) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
59 |
virtual s8 get(s8 x) = 0; |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
60 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
61 |
virtual void set(u2& x, u2 y) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
62 |
virtual void set(u4& x, u4 y) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
63 |
virtual void set(u8& x, u8 y) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
64 |
virtual void set(s2& x, s2 y) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
65 |
virtual void set(s4& x, s4 y) = 0; |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
66 |
virtual void set(s8& x, s8 y) = 0; |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
67 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
68 |
// Quick little endian test. |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
69 |
static bool is_little_endian() { u4 x = 1; return *(u1 *)&x != 0; } |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
70 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
71 |
// Quick big endian test. |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
72 |
static bool is_big_endian() { return !is_little_endian(); } |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
73 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
74 |
// Select an appropriate endian handler. |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
75 |
static Endian* get_handler(bool big_endian); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
76 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
77 |
// Return the native endian handler. |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
78 |
static Endian* get_native_handler(); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
79 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
80 |
// get platform u2 from Java Big endian |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
81 |
static u2 get_java(u1* x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
82 |
// set platform u2 to Java Big endian |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
83 |
static void set_java(u1* p, u2 x); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
84 |
}; |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
85 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
86 |
// Normal endian handling. |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
87 |
class NativeEndian : public Endian { |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
88 |
private: |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
89 |
static NativeEndian _native; |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
90 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
91 |
public: |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
92 |
u2 get(u2 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
93 |
u4 get(u4 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
94 |
u8 get(u8 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
95 |
s2 get(s2 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
96 |
s4 get(s4 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
97 |
s8 get(s8 x); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
98 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
99 |
void set(u2& x, u2 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
100 |
void set(u4& x, u4 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
101 |
void set(u8& x, u8 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
102 |
void set(s2& x, s2 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
103 |
void set(s4& x, s4 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
104 |
void set(s8& x, s8 y); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
105 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
106 |
static Endian* get_native() { return &_native; } |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
107 |
}; |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
108 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
109 |
// Swapping endian handling. |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
110 |
class SwappingEndian : public Endian { |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
111 |
private: |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
112 |
static SwappingEndian _swapping; |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
113 |
|
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
114 |
public: |
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
115 |
u2 get(u2 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
116 |
u4 get(u4 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
117 |
u8 get(u8 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
118 |
s2 get(s2 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
119 |
s4 get(s4 x); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
120 |
s8 get(s8 x); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
121 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
122 |
void set(u2& x, u2 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
123 |
void set(u4& x, u4 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
124 |
void set(u8& x, u8 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
125 |
void set(s2& x, s2 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
126 |
void set(s4& x, s4 y); |
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
127 |
void set(s8& x, s8 y); |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
128 |
|
32757
79d34d4b9627
8135197: libjimage code needs translation from hotspot-ish to jdk-ish
jlaskey
parents:
32641
diff
changeset
|
129 |
static Endian* get_swapping() { return &_swapping; } |
32641
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
130 |
}; |
ac2c73b45253
8087181: Move native jimage code to its own library (maybe libjimage)
jlaskey
parents:
diff
changeset
|
131 |
#endif // LIBJIMAGE_ENDIAN_HPP |