comparison graal/com.oracle.max.asmdis/src/com/sun/max/asm/InlineDataRecorder.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.program.*;
29
30 /**
31 * A facility for recording inline data descriptors associated with a sequence of assembled code.
32 * The recorded descriptors described the structure of some contiguous inline data encoded
33 * in the assembled code.
34 */
35 public class InlineDataRecorder {
36
37 private List<InlineDataDescriptor> descriptors;
38 private boolean normalized;
39
40 /**
41 * Adds an inline data descriptor to this object.
42 */
43 public void add(InlineDataDescriptor inlineData) {
44 if (inlineData.size() != 0) {
45 if (descriptors == null) {
46 descriptors = new ArrayList<InlineDataDescriptor>();
47 }
48 descriptors.add(inlineData);
49 normalized = false;
50 }
51 }
52
53 /**
54 * Gets the sequence of inline data descriptors derived from the descriptors that have been
55 * {@linkplain #add(InlineDataDescriptor) added} to this object. The returned sequence is comprised of
56 * non-overlapping descriptors (i.e. the range implied by each descriptor's
57 * {@linkplain InlineDataDescriptor#startPosition() start} and {@linkplain InlineDataDescriptor#size() size} is
58 * disjoint from all other descriptors) that are sorted in ascending order of their
59 * {@linkplain InlineDataDescriptor#startPosition() start} positions.
60 *
61 * @return null if no descriptors have been added to this object
62 */
63 public List<InlineDataDescriptor> descriptors() {
64 if (!normalized) {
65 normalize();
66 }
67 return descriptors;
68 }
69
70 /**
71 * Gets the result of {@link #descriptors()} encoded as a byte array in the format described
72 * {@linkplain InlineDataDescriptor here}.
73 */
74 public byte[] encodedDescriptors() {
75 if (descriptors == null) {
76 return null;
77 }
78 try {
79 normalize();
80 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
81 final DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
82 dataOutputStream.writeInt(descriptors.size());
83 for (InlineDataDescriptor inlineDataDescriptor : descriptors) {
84 inlineDataDescriptor.writeTo(dataOutputStream);
85 }
86 final byte[] result = byteArrayOutputStream.toByteArray();
87 return result;
88 } catch (IOException ioException) {
89 throw ProgramError.unexpected(ioException);
90 }
91 }
92
93 private void normalize() {
94 if (descriptors != null && !normalized) {
95 final SortedSet<InlineDataDescriptor> sortedEntries = new TreeSet<InlineDataDescriptor>(descriptors);
96 final List<InlineDataDescriptor> entries = new ArrayList<InlineDataDescriptor>(descriptors.size());
97 int lastEnd = 0;
98 for (InlineDataDescriptor inlineDataDescriptor : sortedEntries) {
99 if (inlineDataDescriptor.startPosition() >= lastEnd) {
100 entries.add(inlineDataDescriptor);
101 lastEnd = inlineDataDescriptor.endPosition();
102 }
103 }
104 descriptors = entries;
105 normalized = true;
106 }
107 }
108 }