001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package vm;
028:
029: import jcc.Str2ID;
030: import components.StringConstant;
031: import components.UnicodeConstant;
032: import java.util.Hashtable;
033: import java.util.Enumeration;
034:
035: /**
036: *
037: * There are two string-like data types in today's JDK:
038: * 1) zero-terminated, C-language, ASCII strings
039: * 2) Java Strings.
040: *
041: * The former arise from:
042: * (a) UTF encoding of Java class, method, field names and type
043: * signatures, used for linkage
044: * (b) UTF encoded forms of Java String constants, used as keys
045: * for the intern-ing of said constants.
046: * See the class AsciiStrings where these are manipulated
047: * to achieve some sharing of runtime data structures.
048: *
049: * In this, the StringTable class, we keep track of Java Strings, in
050: * the form of StringConstant's.
051: * We enter them in a Str2ID structure, which will be wanted
052: * at runtime. And we assign layout of the runtime char[] data.
053: * Much aliasing of data is possible, since this is read-only and
054: * not usually zero-terminated. We won't do any of that, for now.
055: * There is much potential here.
056: */
057:
058: public class StringTable {
059: public Str2ID stringHash = new Str2ID();
060: public Hashtable htable = new Hashtable();
061: public StringBuffer data;
062: private int aggregateSize;
063: private int stringIndex = 0;
064:
065: public void intern(StringConstant s) {
066: StringConstant t = (StringConstant) htable.get(s);
067: if (t == null) {
068: htable.put(s, s);
069: stringHash.getID(s.str, s);
070: aggregateSize += s.str.string.length();
071: s.unicodeIndex = stringIndex++;
072: } else {
073: s.unicodeIndex = t.unicodeIndex;
074: }
075: }
076:
077: public Enumeration allStrings() {
078: return htable.elements();
079: }
080:
081: public int internedStringCount() {
082: return stringIndex;
083: }
084:
085: /*
086: * Arrange for the "data" buffer to hold all the string bodies
087: * in some form.
088: * Arrange for the unicodeOffset field of each StringConstant
089: * to be set to the index of the beginning of the representation
090: * of its data in this array.
091: */
092: public int arrangeStringData() {
093: /*
094: * Our initial guess is simply to concatenate all the data.
095: * Later, we can try to be cleverer.
096: */
097: data = new StringBuffer(aggregateSize);
098: int curOffset = 0;
099: Enumeration s = allStrings();
100: while (s.hasMoreElements()) {
101: StringConstant t = (StringConstant) s.nextElement();
102: t.unicodeOffset = curOffset;
103: data.append(t.str.string);
104: curOffset += t.str.string.length();
105: }
106: return curOffset;
107: }
108: }
|