2 Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P.
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
30 #include "uwx_scoreboard.h"
31 #include "uwx_trace.h"
34 static unsigned short uwx_allocated;
35 static struct uwx_scoreboard uwx_scoreboard[sizeof(uwx_allocated) << 3];
38 free(struct uwx_scoreboard *p)
40 int idx = p - uwx_scoreboard;
41 uwx_allocated &= ~(1 << idx);
44 static struct uwx_scoreboard *
48 if (sz != sizeof(struct uwx_scoreboard))
50 for (idx = 0; idx < (sizeof(uwx_allocated) << 3); idx++) {
51 if ((uwx_allocated & (1 << idx)) == 0) {
52 uwx_allocated |= 1 << idx;
53 return (uwx_scoreboard + idx);
61 void uwx_prealloc_scoreboard(struct uwx_env *env, struct uwx_scoreboard *sb)
63 sb->id = env->nscoreboards++;
64 sb->nextused = env->used_scoreboards;
66 env->used_scoreboards = sb;
67 TRACE_B_PREALLOC(sb->id)
70 struct uwx_scoreboard *uwx_alloc_scoreboard(struct uwx_env *env)
72 struct uwx_scoreboard *sb;
75 if (env->free_scoreboards != 0) {
76 sb = env->free_scoreboards;
77 env->free_scoreboards = sb->nextfree;
81 if (env->allocate_cb == 0)
82 sb = (struct uwx_scoreboard *)
83 malloc(sizeof(struct uwx_scoreboard));
85 sb = (struct uwx_scoreboard *)
86 (*env->allocate_cb)(sizeof(struct uwx_scoreboard));
89 sb->id = env->nscoreboards++;
90 sb->nextused = env->used_scoreboards;
92 env->used_scoreboards = sb;
98 for (i = 0; i < env->nsbreg; i++)
99 sb->rstate[i] = UWX_DISP_NONE;
100 sb->rstate[SBREG_RP] = UWX_DISP_REG(UWX_REG_BR(0));
101 sb->rstate[SBREG_PSP] = UWX_DISP_SPPLUS(0);
102 sb->rstate[SBREG_PFS] = UWX_DISP_REG(UWX_REG_AR_PFS);
103 sb->rstate[SBREG_PRIUNAT] = UWX_DISP_REG(UWX_REG_AR_UNAT);
109 void uwx_reclaim_scoreboards(struct uwx_env *env)
111 struct uwx_scoreboard *sb;
113 env->free_scoreboards = 0;
114 for (sb = env->used_scoreboards; sb != 0; sb = sb->nextused) {
115 sb->nextfree = env->free_scoreboards;
116 env->free_scoreboards = sb;
118 env->labeled_scoreboards = 0;
121 struct uwx_scoreboard *uwx_init_scoreboards(struct uwx_env *env)
123 struct uwx_scoreboard *sb;
125 uwx_reclaim_scoreboards(env);
126 sb = uwx_alloc_scoreboard(env);
130 struct uwx_scoreboard *uwx_new_scoreboard(
132 struct uwx_scoreboard *prevsb)
135 struct uwx_scoreboard *sb;
137 sb = uwx_alloc_scoreboard(env);
140 sb->nextstack = prevsb;
141 for (i = 0; i < env->nsbreg; i++)
142 sb->rstate[i] = prevsb->rstate[i];
146 struct uwx_scoreboard *uwx_pop_scoreboards(
148 struct uwx_scoreboard *sb,
151 struct uwx_scoreboard *next;
154 next = sb->nextstack;
157 sb->nextfree = env->free_scoreboards;
158 env->free_scoreboards = sb;
167 int uwx_label_scoreboard(
169 struct uwx_scoreboard *sb,
172 struct uwx_scoreboard *new;
173 struct uwx_scoreboard *back;
174 struct uwx_scoreboard *next;
179 /* Copy the current stack, storing reverse links */
180 /* in the "nextstack" field. */
185 TRACE_B_LABEL_COPY(sb->id)
186 new = uwx_alloc_scoreboard(env);
188 return UWX_ERR_NOMEM;
189 new->nextstack = back;
190 for (i = 0; i < env->nsbreg; i++)
191 new->rstate[i] = sb->rstate[i];
196 /* The "new" pointer now points to the bottom of the new stack, */
197 /* and the "nextstack" links lead towards the top. */
198 /* Now go back down the stack, reversing the stack links to their */
199 /* proper direction. */
203 next = new->nextstack;
204 new->nextstack = back;
205 TRACE_B_LABEL_REVERSE(back, new)
210 /* The "back" pointer now points to the top of the stack. */
213 back->nextlabel = env->labeled_scoreboards;
214 env->labeled_scoreboards = back;
218 int uwx_copy_scoreboard(
220 struct uwx_scoreboard *sb,
223 struct uwx_scoreboard *next;
224 struct uwx_scoreboard *next2;
225 struct uwx_scoreboard *lsb;
226 struct uwx_scoreboard *new;
227 struct uwx_scoreboard *back;
230 TRACE_B_COPY(label, sb->id)
232 /* Free the existing stack. */
234 next = sb->nextstack;
236 TRACE_B_COPY_FREE(next->id)
237 next2 = next->nextstack;
239 next->nextfree = env->free_scoreboards;
240 env->free_scoreboards = next;
244 /* Find the scoreboard with the requested label. */
246 for (lsb = env->labeled_scoreboards; lsb != 0; lsb = lsb->nextlabel) {
247 if (lsb->label == label)
252 return UWX_ERR_UNDEFLABEL;
254 TRACE_B_COPY_FOUND(lsb->id)
256 /* Copy the labeled scoreboard. */
260 for (i = 0; i < env->nsbreg; i++)
261 sb->rstate[i] = lsb->rstate[i];
264 /* Now copy its stack, storing reverse links in the nextstack field. */
268 for (next = lsb->nextstack; next != 0; next = next->nextstack) {
269 TRACE_B_COPY_COPY(next->id)
270 new = uwx_alloc_scoreboard(env);
272 return UWX_ERR_NOMEM;
273 new->nextstack = back;
274 for (i = 0; i < env->nsbreg; i++)
275 new->rstate[i] = next->rstate[i];
279 /* The "new" pointer now points to the bottom of the new stack, */
280 /* and the "nextstack" links lead towards the top. */
281 /* Now go back down the stack, reversing the nextstack links to their */
282 /* proper direction. */
286 next = new->nextstack;
287 new->nextstack = back;
288 TRACE_B_COPY_REVERSE(back, new)
296 void uwx_free_scoreboards(struct uwx_env *env)
298 struct uwx_scoreboard *sb;
299 struct uwx_scoreboard *next;
301 for (sb = env->used_scoreboards; sb != 0; sb = next) {
305 if (env->free_cb == 0)
308 (*env->free_cb)((void *)sb);
311 env->free_scoreboards = 0;
312 env->used_scoreboards = 0;
313 env->labeled_scoreboards = 0;