2 * Copyright (C) 2014 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef UnlinkedInstructionStream_h
28 #define UnlinkedInstructionStream_h
30 #include "UnlinkedCodeBlock.h"
31 #include <wtf/RefCountedArray.h>
35 class UnlinkedInstructionStream
{
36 WTF_MAKE_FAST_ALLOCATED
;
38 explicit UnlinkedInstructionStream(const Vector
<UnlinkedInstruction
>&);
40 unsigned count() const { return m_instructionCount
; }
44 explicit Reader(const UnlinkedInstructionStream
&);
46 const UnlinkedInstruction
* next();
47 bool atEnd() const { return m_index
== m_stream
.m_data
.size(); }
50 unsigned char read8();
53 const UnlinkedInstructionStream
& m_stream
;
54 UnlinkedInstruction m_unpackedBuffer
[16];
59 const RefCountedArray
<UnlinkedInstruction
>& unpackForDebugging() const;
66 mutable RefCountedArray
<UnlinkedInstruction
> m_unpackedInstructionsForDebugging
;
69 RefCountedArray
<unsigned char> m_data
;
70 unsigned m_instructionCount
;
73 // Unlinked instructions are packed in a simple stream format.
75 // The first byte is always the opcode.
76 // It's followed by an opcode-dependent number of argument values.
77 // The first 3 bits of each value determines the format:
79 // 5-bit positive integer (1 byte total)
80 // 5-bit negative integer (1 byte total)
81 // 13-bit positive integer (2 bytes total)
82 // 13-bit negative integer (2 bytes total)
83 // 5-bit constant register index, based at 0x40000000 (1 byte total)
84 // 13-bit constant register index, based at 0x40000000 (2 bytes total)
85 // 32-bit raw value (5 bytes total)
87 enum PackedValueType
{
93 ConstantRegister13Bit
,
97 ALWAYS_INLINE
UnlinkedInstructionStream::Reader::Reader(const UnlinkedInstructionStream
& stream
)
103 ALWAYS_INLINE
unsigned char UnlinkedInstructionStream::Reader::read8()
105 return m_stream
.m_data
.data()[m_index
++];
108 ALWAYS_INLINE
unsigned UnlinkedInstructionStream::Reader::read32()
110 const unsigned char* data
= &m_stream
.m_data
.data()[m_index
];
111 unsigned char type
= data
[0] >> 5;
119 return 0xffffffe0 | data
[0];
122 return ((data
[0] & 0x1F) << 8) | data
[1];
125 return 0xffffe000 | ((data
[0] & 0x1F) << 8) | data
[1];
126 case ConstantRegister5Bit
:
128 return 0x40000000 | (data
[0] & 0x1F);
129 case ConstantRegister13Bit
:
131 return 0x40000000 | ((data
[0] & 0x1F) << 8) | data
[1];
133 ASSERT(type
== Full32Bit
);
135 return data
[1] | data
[2] << 8 | data
[3] << 16 | data
[4] << 24;
139 ALWAYS_INLINE
const UnlinkedInstruction
* UnlinkedInstructionStream::Reader::next()
141 m_unpackedBuffer
[0].u
.opcode
= static_cast<OpcodeID
>(read8());
142 unsigned opLength
= opcodeLength(m_unpackedBuffer
[0].u
.opcode
);
143 for (unsigned i
= 1; i
< opLength
; ++i
)
144 m_unpackedBuffer
[i
].u
.index
= read32();
145 return m_unpackedBuffer
;
150 #endif // UnlinkedInstructionStream_h