Mercurial > hg > truffle
comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/InlineDataDecoder.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, 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 package com.sun.max.asm; | |
24 | |
25 import java.io.*; | |
26 import java.util.*; | |
27 | |
28 import com.sun.max.asm.InlineDataDescriptor.*; | |
29 import com.sun.max.io.*; | |
30 import com.sun.max.program.*; | |
31 | |
32 /** | |
33 * A decoder for a sequence of {@linkplain InlineDataDescriptor inline data descriptors} associated with an | |
34 * instruction stream. The decoder is initialized either from an encoded or inflated sequence of descriptors. | |
35 * | |
36 * Once initialized, a inline data decoder can be {@linkplain #decode(int, BufferedInputStream) queried} for the | |
37 * inline data descriptor describing the inline data at a given position in an instruction stream. | |
38 */ | |
39 public class InlineDataDecoder { | |
40 | |
41 protected final Map<Integer, InlineDataDescriptor> positionToDescriptorMap; | |
42 | |
43 /** | |
44 * Creates a decoder from an encoded sequence of inline data descriptors. | |
45 * | |
46 * @param encodedDescriptors a sequence of descriptors encoded in a byte array whose format complies with that used | |
47 * by {@link InlineDataRecorder#encodedDescriptors()}. This value can be null. | |
48 * @return null if {@code encodedDescriptors} is null | |
49 */ | |
50 public static InlineDataDecoder createFrom(byte[] encodedDescriptors) { | |
51 if (encodedDescriptors != null) { | |
52 return new InlineDataDecoder(encodedDescriptors); | |
53 } | |
54 return null; | |
55 } | |
56 | |
57 /** | |
58 * Creates a decoder based on the descriptors in a given recorder. | |
59 * | |
60 * @param inlineDataRecorder | |
61 * @return null if {@code inlineDataRecorder} does not contain any entries | |
62 */ | |
63 public static InlineDataDecoder createFrom(InlineDataRecorder inlineDataRecorder) { | |
64 final List<InlineDataDescriptor> descriptors = inlineDataRecorder.descriptors(); | |
65 if (descriptors != null) { | |
66 return new InlineDataDecoder(descriptors); | |
67 } | |
68 return null; | |
69 } | |
70 | |
71 public InlineDataDecoder(List<InlineDataDescriptor> descriptors) { | |
72 positionToDescriptorMap = new TreeMap<Integer, InlineDataDescriptor>(); | |
73 for (InlineDataDescriptor descriptor : descriptors) { | |
74 positionToDescriptorMap.put(descriptor.startPosition(), descriptor); | |
75 } | |
76 } | |
77 | |
78 /** | |
79 * Creates a decoder from an encoded sequence of inline data descriptors. | |
80 * | |
81 * @param encodedDescriptors a sequence of descriptors encoded in a byte array whose format complies with that used | |
82 * by {@link InlineDataRecorder#encodedDescriptors()} | |
83 */ | |
84 public InlineDataDecoder(byte[] encodedDescriptors) { | |
85 try { | |
86 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encodedDescriptors); | |
87 final DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream); | |
88 final int numberOfEntries = dataInputStream.readInt(); | |
89 positionToDescriptorMap = new TreeMap<Integer, InlineDataDescriptor>(); | |
90 for (int i = 0; i < numberOfEntries; ++i) { | |
91 final Tag tag = InlineDataDescriptor.Tag.VALUES.get(dataInputStream.readByte()); | |
92 final InlineDataDescriptor inlineDataDescriptor = tag.decode(dataInputStream); | |
93 positionToDescriptorMap.put(inlineDataDescriptor.startPosition(), inlineDataDescriptor); | |
94 } | |
95 assert byteArrayInputStream.available() == 0; | |
96 } catch (IOException ioException) { | |
97 throw ProgramError.unexpected(ioException); | |
98 } | |
99 } | |
100 | |
101 /** | |
102 * Decodes the data (if any) from the current read position of a given stream. | |
103 * | |
104 * @param currentPosition the stream's current read position with respect to the start of the stream | |
105 * @param stream the instruction stream being disassembled | |
106 * @return the inline data decoded from the stream or null if there is no inline data at {@code currentPosition} | |
107 */ | |
108 public InlineData decode(int currentPosition, BufferedInputStream stream) throws IOException { | |
109 final InlineDataDescriptor inlineDataDescriptor = positionToDescriptorMap.get(currentPosition); | |
110 if (inlineDataDescriptor != null) { | |
111 final int size = inlineDataDescriptor.size(); | |
112 final byte[] data = new byte[size]; | |
113 Streams.readFully(stream, data); | |
114 return new InlineData(inlineDataDescriptor, data); | |
115 } | |
116 return null; | |
117 } | |
118 } |