OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [rmic/] [Variables.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* Variables.java --
2
   Copyright (c) 2004, 2005
3
   Free Software Foundation, Inc.
4
 
5
This file is part of GNU Classpath.
6
 
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)
10
any later version.
11
 
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.
16
 
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
20
02111-1307 USA.
21
 
22
Linking this library statically or dynamically with other modules is
23
making a combined work based on this library.  Thus, the terms and
24
conditions of the GNU General Public License cover the whole
25
combination.
26
 
27
As a special exception, the copyright holders of this library give you
28
permission to link this library with independent modules to produce an
29
executable, regardless of the license terms of these independent
30
modules, and to copy and distribute the resulting executable under
31
terms of your choice, provided that you also meet, for each linked
32
independent module, the terms and conditions of the license of that
33
module.  An independent module is a module which is not derived from
34
or based on this library.  If you modify this library, you may extend
35
this exception to your version of the library, but you are not
36
obligated to do so.  If you do not wish to do so, delete this
37
exception statement from your version. */
38
 
39
package gnu.classpath.tools.rmic;
40
 
41
import java.util.HashMap;
42
import java.util.HashSet;
43
import java.util.Iterator;
44
 
45
class Variables
46
{
47
  private final HashSet free = new HashSet();
48
  private final HashMap names = new HashMap();
49
  private final HashSet wides = new HashSet();
50
  private final HashSet declared = new HashSet();
51
  private boolean allocated = false;
52
 
53
  public void declare(Object name)
54
  {
55
    declare(name, 1);
56
  }
57
 
58
  public void declareWide(Object name)
59
  {
60
    declare(name, 2);
61
  }
62
 
63
  public void declare(Object name, int size)
64
  {
65
    if (allocated)
66
      throw new IllegalStateException("cannot declare after allocating");
67
    if (size != 1 && size != 2)
68
      throw new IllegalArgumentException("size must be 1 or 2");
69
    if (names.containsKey(name))
70
      throw new IllegalStateException("already allocated " + name);
71
 
72
    allocateNew(name, size);
73
    declared.add(name);
74
  }
75
 
76
  private int allocateNew(Object name, int size)
77
  {
78
    // total allocation size is first unallocated slot
79
    int i = free.size() + names.size() + wides.size();
80
    names.put(name, new Integer(i));
81
    if (size == 2) wides.add(name);
82
    return i;
83
  }
84
 
85
  public int allocate(Object name)
86
  {
87
    return allocate(name, 1);
88
  }
89
 
90
  public int allocateWide(Object name)
91
  {
92
    return allocate(name, 2);
93
  }
94
 
95
  public int allocate(Object name, int size)
96
  {
97
    allocated = true;
98
    if (size != 1 && size != 2)
99
      throw new IllegalArgumentException("size must be 1 or 2");
100
    if (names.containsKey(name))
101
      throw new IllegalStateException("already allocated " + name);
102
 
103
    if (size == 2)
104
      {
105
        // look for consecutive free slots
106
        for (Iterator it = free.iterator(); it.hasNext(); )
107
          {
108
            Integer i = (Integer) it.next();
109
            Integer next = new Integer(i.intValue() + 1);
110
            if (free.contains(next))
111
              {
112
                free.remove(i);
113
                free.remove(next);
114
                wides.add(name);
115
                names.put(name, i);
116
                return i.intValue();
117
              }
118
          }
119
      }
120
    else if (free.size() > 0)
121
      {
122
        Integer i = (Integer) free.iterator().next();
123
        free.remove(i);
124
        names.put(name, i);
125
        return i.intValue();
126
      }
127
 
128
    return allocateNew(name, size);
129
  }
130
 
131
  public int deallocate(Object name)
132
  {
133
    if (! names.containsKey(name))
134
      throw new IllegalArgumentException("no variable " + name);
135
 
136
    if (declared.contains(name))
137
      throw new IllegalStateException(name + " can't be deallocated");
138
 
139
    Integer i = (Integer) names.get(name);
140
    names.remove(name);
141
    free.add(i);
142
    if (wides.remove(name))
143
      free.add(new Integer(i.intValue() + 1));
144
    return i.intValue();
145
  }
146
 
147
  public int get(Object name)
148
  {
149
    if (! names.containsKey(name))
150
      throw new IllegalArgumentException("no variable " + name);
151
 
152
    return ((Integer) names.get(name)).intValue();
153
  }
154
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.