Import GNU Classpath (20121202).
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / rmic / Variables.java
blob14ba6493ae08bdc47ac8b2947cfebc713e64d52c
1 /* Variables.java --
2 Copyright (c) 2004, 2005, 2012 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.classpath.tools.rmic;
40 import java.util.HashMap;
41 import java.util.HashSet;
42 import java.util.Iterator;
44 class Variables
46 private final HashSet<Integer> free = new HashSet<Integer>();
47 private final HashMap<Object,Integer> names = new HashMap<Object,Integer>();
48 private final HashSet<Object> wides = new HashSet<Object>();
49 private final HashSet<Object> declared = new HashSet<Object>();
50 private boolean allocated = false;
52 public void declare(Object name)
54 declare(name, 1);
57 public void declareWide(Object name)
59 declare(name, 2);
62 public void declare(Object name, int size)
64 if (allocated)
65 throw new IllegalStateException("cannot declare after allocating");
66 if (size != 1 && size != 2)
67 throw new IllegalArgumentException("size must be 1 or 2");
68 if (names.containsKey(name))
69 throw new IllegalStateException("already allocated " + name);
71 allocateNew(name, size);
72 declared.add(name);
75 private int allocateNew(Object name, int size)
77 // total allocation size is first unallocated slot
78 int i = free.size() + names.size() + wides.size();
79 names.put(name, Integer.valueOf(i));
80 if (size == 2) wides.add(name);
81 return i;
84 public int allocate(Object name)
86 return allocate(name, 1);
89 public int allocateWide(Object name)
91 return allocate(name, 2);
94 public int allocate(Object name, int size)
96 allocated = true;
97 if (size != 1 && size != 2)
98 throw new IllegalArgumentException("size must be 1 or 2");
99 if (names.containsKey(name))
100 throw new IllegalStateException("already allocated " + name);
102 if (size == 2)
104 // look for consecutive free slots
105 for (Iterator<Integer> it = free.iterator(); it.hasNext(); )
107 Integer i = it.next();
108 Integer next = Integer.valueOf(i.intValue() + 1);
109 if (free.contains(next))
111 free.remove(i);
112 free.remove(next);
113 wides.add(name);
114 names.put(name, i);
115 return i.intValue();
119 else if (free.size() > 0)
121 Integer i = free.iterator().next();
122 free.remove(i);
123 names.put(name, i);
124 return i.intValue();
127 return allocateNew(name, size);
130 public int deallocate(Object name)
132 if (! names.containsKey(name))
133 throw new IllegalArgumentException("no variable " + name);
135 if (declared.contains(name))
136 throw new IllegalStateException(name + " can't be deallocated");
138 Integer i = names.get(name);
139 names.remove(name);
140 free.add(i);
141 if (wides.remove(name))
142 free.add(Integer.valueOf(i.intValue() + 1));
143 return i.intValue();
146 public int get(Object name)
148 if (! names.containsKey(name))
149 throw new IllegalArgumentException("no variable " + name);
151 return names.get(name).intValue();