1 /*
2  * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 #ifndef CPU_ARM_VM_BYTES_ARM_HPP
26 #define CPU_ARM_VM_BYTES_ARM_HPP
27 
28 #include "memory/allocation.hpp"
29 #include "utilities/macros.hpp"
30 
31 #ifndef VM_LITTLE_ENDIAN
32 #define VM_LITTLE_ENDIAN  1
33 #endif
34 
35 class Bytes: AllStatic {
36 
37  public:
get_Java_u2(address p)38   static inline u2 get_Java_u2(address p) {
39     return (u2(p[0]) << 8) | u2(p[1]);
40   }
41 
get_Java_u4(address p)42   static inline u4 get_Java_u4(address p) {
43     return u4(p[0]) << 24 |
44            u4(p[1]) << 16 |
45            u4(p[2]) <<  8 |
46            u4(p[3]);
47   }
48 
get_Java_u8(address p)49   static inline u8 get_Java_u8(address p) {
50     return u8(p[0]) << 56 |
51            u8(p[1]) << 48 |
52            u8(p[2]) << 40 |
53            u8(p[3]) << 32 |
54            u8(p[4]) << 24 |
55            u8(p[5]) << 16 |
56            u8(p[6]) <<  8 |
57            u8(p[7]);
58   }
59 
put_Java_u2(address p,u2 x)60   static inline void put_Java_u2(address p, u2 x) {
61     p[0] = x >> 8;
62     p[1] = x;
63   }
64 
put_Java_u4(address p,u4 x)65   static inline void put_Java_u4(address p, u4 x) {
66     ((u1*)p)[0] = x >> 24;
67     ((u1*)p)[1] = x >> 16;
68     ((u1*)p)[2] = x >>  8;
69     ((u1*)p)[3] = x;
70   }
71 
put_Java_u8(address p,u8 x)72   static inline void put_Java_u8(address p, u8 x) {
73     ((u1*)p)[0] = x >> 56;
74     ((u1*)p)[1] = x >> 48;
75     ((u1*)p)[2] = x >> 40;
76     ((u1*)p)[3] = x >> 32;
77     ((u1*)p)[4] = x >> 24;
78     ((u1*)p)[5] = x >> 16;
79     ((u1*)p)[6] = x >>  8;
80     ((u1*)p)[7] = x;
81   }
82 
83 #ifdef VM_LITTLE_ENDIAN
84 
get_native_u2(address p)85   static inline u2 get_native_u2(address p) {
86     return (intptr_t(p) & 1) == 0 ? *(u2*)p : u2(p[0]) | (u2(p[1]) << 8);
87   }
88 
get_native_u4(address p)89   static inline u4 get_native_u4(address p) {
90     switch (intptr_t(p) & 3) {
91       case 0:  return *(u4*)p;
92       case 2:  return u4(((u2*)p)[0]) |
93                       u4(((u2*)p)[1]) << 16;
94       default: return u4(p[0])       |
95                       u4(p[1]) <<  8 |
96                       u4(p[2]) << 16 |
97                       u4(p[3]) << 24;
98     }
99   }
100 
get_native_u8(address p)101   static inline u8 get_native_u8(address p) {
102     switch (intptr_t(p) & 7) {
103       case 0:  return *(u8*)p;
104       case 4:  return u8(((u4*)p)[0]) |
105                       u8(((u4*)p)[1]) << 32;
106       case 2:  return u8(((u2*)p)[0])       |
107                       u8(((u2*)p)[1]) << 16 |
108                       u8(((u2*)p)[2]) << 32 |
109                       u8(((u2*)p)[3]) << 48;
110       default: return u8(p[0])       |
111                       u8(p[1]) <<  8 |
112                       u8(p[2]) << 16 |
113                       u8(p[3]) << 24 |
114                       u8(p[4]) << 32 |
115                       u8(p[5]) << 40 |
116                       u8(p[6]) << 48 |
117                       u8(p[7]) << 56;
118     }
119   }
120 
put_native_u2(address p,u2 x)121   static inline void put_native_u2(address p, u2 x) {
122     if ((intptr_t(p) & 1) == 0) {
123       *(u2*)p = x;
124     } else {
125       p[0] = x;
126       p[1] = x >> 8;
127     }
128   }
129 
put_native_u4(address p,u4 x)130   static inline void put_native_u4(address p, u4 x) {
131     switch (intptr_t(p) & 3) {
132       case 0:  *(u4*)p = x;
133                break;
134       case 2:  ((u2*)p)[0] = x;
135                ((u2*)p)[1] = x >> 16;
136                break;
137       default: ((u1*)p)[0] = x;
138                ((u1*)p)[1] = x >>  8;
139                ((u1*)p)[2] = x >> 16;
140                ((u1*)p)[3] = x >> 24;
141                break;
142     }
143   }
144 
put_native_u8(address p,u8 x)145   static inline void put_native_u8(address p, u8 x) {
146     switch (intptr_t(p) & 7) {
147       case 0:  *(u8*)p = x;
148                break;
149       case 4:  ((u4*)p)[0] = x;
150                ((u4*)p)[1] = x >> 32;
151                break;
152       case 2:  ((u2*)p)[0] = x;
153                ((u2*)p)[1] = x >> 16;
154                ((u2*)p)[2] = x >> 32;
155                ((u2*)p)[3] = x >> 48;
156                break;
157       default: ((u1*)p)[0] = x;
158                ((u1*)p)[1] = x >>  8;
159                ((u1*)p)[2] = x >> 16;
160                ((u1*)p)[3] = x >> 24;
161                ((u1*)p)[4] = x >> 32;
162                ((u1*)p)[5] = x >> 40;
163                ((u1*)p)[6] = x >> 48;
164                ((u1*)p)[7] = x >> 56;
165     }
166   }
167 
168 #else
169 
get_native_u2(address p)170   static inline u2 get_native_u2(address p) { return get_Java_u2(p); }
get_native_u4(address p)171   static inline u4 get_native_u4(address p) { return get_Java_u4(p); }
get_native_u8(address p)172   static inline u8 get_native_u8(address p) { return get_Java_u8(p); }
put_native_u2(address p,u2 x)173   static inline void put_native_u2(address p, u2 x) { put_Java_u2(p, x); }
put_native_u4(address p,u4 x)174   static inline void put_native_u4(address p, u4 x) { put_Java_u4(p, x); }
put_native_u8(address p,u8 x)175   static inline void put_native_u8(address p, u8 x) { put_Java_u8(p, x); }
176 
177 #endif // VM_LITTLE_ENDIAN
178 
179   // Efficient swapping of byte ordering
180   static inline u2 swap_u2(u2 x);
181   static inline u4 swap_u4(u4 x);
182   static inline u8 swap_u8(u8 x);
183 };
184 
185 
186 // The following header contains the implementations of swap_u2, swap_u4, and swap_u8
187 #include OS_CPU_HEADER_INLINE(bytes)
188 
189 #endif // CPU_ARM_VM_BYTES_ARM_HPP
190