Merge remote-tracking branch 'redux/master' into sh4-pool
[tamarin-stm.git] / core / DataIO.h
blobe4a96f2cf6418a2a188b5449051a17cdf0ec382b
1 /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
2 /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #ifndef DATAIO_INCLUDED
42 #define DATAIO_INCLUDED
44 namespace avmplus
46 enum ObjectEncoding {
47 kAMF0 = 0,
48 kAMF3 = 3,
49 kEncodeDefault = kAMF3
52 enum Endian
54 kBigEndian = 0,
55 kLittleEndian = 1
58 REALLY_INLINE void byteSwapU16(uint16_t& a)
60 #if defined(_MSC_VER)
61 a = _byteswap_ushort(a);
62 #else
63 // OPTIMIZEME: GCC 4.3+ have intrinsics we should use
64 a = ((a & 0x00ffU)<<8)|
65 ((a & 0xff00U)>>8);
66 #endif
69 REALLY_INLINE void byteSwapU32(uint32_t& a)
71 #if defined(_MSC_VER)
72 a = _byteswap_ulong(a);
73 #else
74 // OPTIMIZEME: GCC 4.3+ have intrinsics we should use
75 a = ((a & 0x000000ffUL)<<24)|
76 ((a & 0x0000ff00UL)<< 8)|
77 ((a & 0x00ff0000UL)>> 8)|
78 ((a & 0xff000000UL)>>24);
79 #endif
82 REALLY_INLINE void byteSwapU64(uint64_t& a)
84 #if defined(_MSC_VER)
85 a = _byteswap_uint64(a);
86 #else
87 // OPTIMIZEME: GCC 4.3+ have intrinsics we should use
88 a = ((a & 0x00000000000000ffULL)<<56)|
89 ((a & 0x000000000000ff00ULL)<<40)|
90 ((a & 0x0000000000ff0000ULL)<<24)|
91 ((a & 0x00000000ff000000ULL)<< 8)|
92 ((a & 0x000000ff00000000ULL)>> 8)|
93 ((a & 0x0000ff0000000000ULL)>>24)|
94 ((a & 0x00ff000000000000ULL)>>40)|
95 ((a & 0xff00000000000000ULL)>>56);
96 #endif
99 class DataIOBase
101 public:
102 MMGC_DECLARE_OPERATOR_DELETES_FOR_CLASS
104 REALLY_INLINE DataIOBase()
105 : m_objectEncoding(kEncodeDefault)
106 , m_endian(kBigEndian)
110 virtual ~DataIOBase() {}
112 REALLY_INLINE Endian GetEndian() const { return m_endian; }
113 REALLY_INLINE void SetEndian(Endian endian) { m_endian = endian; }
115 REALLY_INLINE ObjectEncoding GetObjectEncoding() const { return m_objectEncoding; }
116 REALLY_INLINE void SetObjectEncoding(ObjectEncoding objectEncoding) { m_objectEncoding = objectEncoding; }
118 REALLY_INLINE void ConvertU16(uint16_t& value)
120 if (GetEndian() != GetNativeEndian())
122 byteSwapU16(value);
126 REALLY_INLINE void ConvertU32(uint32_t& value)
128 if (GetEndian() != GetNativeEndian())
130 byteSwapU32(value);
134 REALLY_INLINE void ConvertU64(uint64_t& value)
136 if (GetEndian() != GetNativeEndian())
138 byteSwapU64(value);
142 REALLY_INLINE void ConvertD64(uint64_t& value)
144 #if defined(VMCFG_DOUBLE_MSW_FIRST)
145 // Swap the high and low words so that the datum is in "natural" endianness,
146 // this produces or consumes big-endian or little-endian external data
147 // as appropriate.
148 union {
149 uint64_t v;
150 struct { uint32_t a, b; }
152 v = value;
153 uint32_t tmp = a;
154 a = b;
155 b = tmp;
156 value = v;
157 #endif
158 return ConvertU64(value);
161 REALLY_INLINE Endian GetNativeEndian() const
163 #if defined(VMCFG_LITTLE_ENDIAN)
164 return kLittleEndian;
165 #elif defined(VMCFG_BIG_ENDIAN)
166 return kBigEndian;
167 #else
168 #error
169 #endif
172 protected:
173 virtual Toplevel* toplevel() const = 0;
175 void ThrowEOFError();
176 void ThrowMemoryError();
177 void ThrowRangeError();
179 REALLY_INLINE void gcTrace(MMgc::GC* gc) { (void)gc; }
181 private:
182 // OPTIMIZEME these will fit into bytes, might be a tiny worthwhile savings
183 ObjectEncoding m_objectEncoding;
184 Endian m_endian;
187 class DataInput : virtual public DataIOBase
189 public:
190 REALLY_INLINE DataInput() : DataIOBase() {}
192 virtual uint32_t Available() = 0;
193 virtual void Read(void *buffer, uint32_t count) = 0;
195 bool ReadBoolean();
196 uint8_t ReadU8();
197 uint16_t ReadU16();
198 uint32_t ReadU32();
199 float ReadFloat();
200 double ReadDouble();
201 String* ReadMultiByte(uint32_t length, String *charSet);
202 String* ReadUTF();
203 String* ReadUTFBytes(uint32_t length);
204 void ReadByteArray(ByteArray& buffer, uint32_t offset, uint32_t count);
205 Atom ReadObject();
207 protected:
208 void CheckEOF(uint32_t count);
210 REALLY_INLINE void gcTrace(MMgc::GC* gc)
212 DataIOBase::gcTrace(gc);
216 class DataOutput : virtual public DataIOBase
218 public:
219 REALLY_INLINE DataOutput() : DataIOBase() { }
221 virtual void Write(const void *buffer, uint32_t count) = 0;
223 void WriteBoolean(bool value);
224 void WriteU8(uint8_t value);
225 void WriteU16(uint16_t value);
226 void WriteU32(uint32_t value);
227 void WriteFloat(float value);
228 void WriteDouble(double value);
229 void WriteMultiByte(String *str, String *charSet);
230 void WriteUTF(String *str);
231 void WriteUTFBytes(String *str);
232 void WriteByteArray(ByteArray& buffer, uint32_t offset, uint32_t count);
233 void WriteObject(Atom atom);
235 protected:
236 REALLY_INLINE void gcTrace(MMgc::GC* gc)
238 DataIOBase::gcTrace(gc);
243 MMGC_DECLARE_SPECIALIZED_DESTRUCTORCALL_TEMPLATES(avmplus::DataIOBase)
245 #endif /* DATAIO_INCLUDED */