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
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.
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
49 kEncodeDefault
= kAMF3
58 REALLY_INLINE
void byteSwapU16(uint16_t& a
)
61 a
= _byteswap_ushort(a
);
63 // OPTIMIZEME: GCC 4.3+ have intrinsics we should use
64 a
= ((a
& 0x00ffU
)<<8)|
69 REALLY_INLINE
void byteSwapU32(uint32_t& a
)
72 a
= _byteswap_ulong(a
);
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);
82 REALLY_INLINE
void byteSwapU64(uint64_t& a
)
85 a
= _byteswap_uint64(a
);
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);
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())
126 REALLY_INLINE
void ConvertU32(uint32_t& value
)
128 if (GetEndian() != GetNativeEndian())
134 REALLY_INLINE
void ConvertU64(uint64_t& value
)
136 if (GetEndian() != GetNativeEndian())
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
150 struct { uint32_t a
, b
; }
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)
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
; }
182 // OPTIMIZEME these will fit into bytes, might be a tiny worthwhile savings
183 ObjectEncoding m_objectEncoding
;
187 class DataInput
: virtual public DataIOBase
190 REALLY_INLINE
DataInput() : DataIOBase() {}
192 virtual uint32_t Available() = 0;
193 virtual void Read(void *buffer
, uint32_t count
) = 0;
201 String
* ReadMultiByte(uint32_t length
, String
*charSet
);
203 String
* ReadUTFBytes(uint32_t length
);
204 void ReadByteArray(ByteArray
& buffer
, uint32_t offset
, uint32_t count
);
208 void CheckEOF(uint32_t count
);
210 REALLY_INLINE
void gcTrace(MMgc::GC
* gc
)
212 DataIOBase::gcTrace(gc
);
216 class DataOutput
: virtual public DataIOBase
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
);
236 REALLY_INLINE
void gcTrace(MMgc::GC
* gc
)
238 DataIOBase::gcTrace(gc
);
243 MMGC_DECLARE_SPECIALIZED_DESTRUCTORCALL_TEMPLATES(avmplus::DataIOBase
)
245 #endif /* DATAIO_INCLUDED */