2 Copyright (c) 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 package gnu.classpath.tools.rmic;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
30 private final HashSet free = new HashSet();
31 private final HashMap names = new HashMap();
32 private final HashSet wides = new HashSet();
33 private final HashSet declared = new HashSet();
34 private boolean allocated = false;
36 public void declare(Object name)
41 public void declareWide(Object name)
46 public void declare(Object name, int size)
49 throw new IllegalStateException("cannot declare after allocating");
50 if (size != 1 && size != 2)
51 throw new IllegalArgumentException("size must be 1 or 2");
52 if (names.containsKey(name))
53 throw new IllegalStateException("already allocated " + name);
55 allocateNew(name, size);
59 private int allocateNew(Object name, int size)
61 // total allocation size is first unallocated slot
62 int i = free.size() + names.size() + wides.size();
63 names.put(name, new Integer(i));
64 if (size == 2) wides.add(name);
68 public int allocate(Object name)
70 return allocate(name, 1);
73 public int allocateWide(Object name)
75 return allocate(name, 2);
78 public int allocate(Object name, int size)
81 if (size != 1 && size != 2)
82 throw new IllegalArgumentException("size must be 1 or 2");
83 if (names.containsKey(name))
84 throw new IllegalStateException("already allocated " + name);
88 // look for consecutive free slots
89 for (Iterator it = free.iterator(); it.hasNext(); )
91 Integer i = (Integer) it.next();
92 Integer next = new Integer(i.intValue() + 1);
93 if (free.contains(next))
103 else if (free.size() > 0)
105 Integer i = (Integer) free.iterator().next();
111 return allocateNew(name, size);
114 public int deallocate(Object name)
116 if (! names.containsKey(name))
117 throw new IllegalArgumentException("no variable " + name);
119 if (declared.contains(name))
120 throw new IllegalStateException(name + " can't be deallocated");
122 Integer i = (Integer) names.get(name);
125 if (wides.remove(name))
126 free.add(new Integer(i.intValue() + 1));
130 public int get(Object name)
132 if (! names.containsKey(name))
133 throw new IllegalArgumentException("no variable " + name);
135 return ((Integer) names.get(name)).intValue();