aicasm_symbol.c revision 1.2 1 1.2 lukem /* $NetBSD: aicasm_symbol.c,v 1.2 2002/06/02 01:20:36 lukem Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
5 1.1 fvdl *
6 1.1 fvdl * Copyright (c) 1997 Justin T. Gibbs.
7 1.1 fvdl * All rights reserved.
8 1.1 fvdl *
9 1.1 fvdl * Redistribution and use in source and binary forms, with or without
10 1.1 fvdl * modification, are permitted provided that the following conditions
11 1.1 fvdl * are met:
12 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
13 1.1 fvdl * notice, this list of conditions, and the following disclaimer,
14 1.1 fvdl * without modification.
15 1.1 fvdl * 2. The name of the author may not be used to endorse or promote products
16 1.1 fvdl * derived from this software without specific prior written permission.
17 1.1 fvdl *
18 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 1.1 fvdl * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 fvdl * SUCH DAMAGE.
29 1.1 fvdl *
30 1.1 fvdl * $FreeBSD: src/sys/dev/aic7xxx/aicasm_symbol.c,v 1.8 1999/12/06 18:23:30 gibbs Exp $
31 1.1 fvdl */
32 1.1 fvdl
33 1.1 fvdl
34 1.1 fvdl #include <sys/types.h>
35 1.1 fvdl
36 1.1 fvdl #include <db.h>
37 1.1 fvdl #include <fcntl.h>
38 1.1 fvdl #include <stdio.h>
39 1.1 fvdl #include <stdlib.h>
40 1.1 fvdl #include <string.h>
41 1.1 fvdl #include <sysexits.h>
42 1.1 fvdl
43 1.1 fvdl #include "aicasm.h"
44 1.1 fvdl #include "aicasm_symbol.h"
45 1.1 fvdl
46 1.1 fvdl static DB *symtable;
47 1.1 fvdl
48 1.1 fvdl symbol_t *
49 1.1 fvdl symbol_create(name)
50 1.1 fvdl char *name;
51 1.1 fvdl {
52 1.1 fvdl symbol_t *new_symbol;
53 1.1 fvdl
54 1.1 fvdl new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
55 1.1 fvdl if (new_symbol == NULL) {
56 1.1 fvdl perror("Unable to create new symbol");
57 1.1 fvdl exit(EX_SOFTWARE);
58 1.1 fvdl }
59 1.1 fvdl memset(new_symbol, 0, sizeof(*new_symbol));
60 1.1 fvdl new_symbol->name = strdup(name);
61 1.1 fvdl new_symbol->type = UNINITIALIZED;
62 1.1 fvdl return (new_symbol);
63 1.1 fvdl }
64 1.1 fvdl
65 1.1 fvdl void
66 1.1 fvdl symbol_delete(symbol)
67 1.1 fvdl symbol_t *symbol;
68 1.1 fvdl {
69 1.1 fvdl if (symtable != NULL) {
70 1.1 fvdl DBT key;
71 1.1 fvdl
72 1.1 fvdl key.data = symbol->name;
73 1.1 fvdl key.size = strlen(symbol->name);
74 1.1 fvdl symtable->del(symtable, &key, /*flags*/0);
75 1.1 fvdl }
76 1.1 fvdl switch(symbol->type) {
77 1.1 fvdl case SCBLOC:
78 1.1 fvdl case SRAMLOC:
79 1.1 fvdl case REGISTER:
80 1.1 fvdl if (symbol->info.rinfo != NULL)
81 1.1 fvdl free(symbol->info.rinfo);
82 1.1 fvdl break;
83 1.1 fvdl case ALIAS:
84 1.1 fvdl if (symbol->info.ainfo != NULL)
85 1.1 fvdl free(symbol->info.ainfo);
86 1.1 fvdl break;
87 1.1 fvdl case MASK:
88 1.1 fvdl case BIT:
89 1.1 fvdl if (symbol->info.minfo != NULL) {
90 1.1 fvdl symlist_free(&symbol->info.minfo->symrefs);
91 1.1 fvdl free(symbol->info.minfo);
92 1.1 fvdl }
93 1.1 fvdl break;
94 1.1 fvdl case DOWNLOAD_CONST:
95 1.1 fvdl case CONST:
96 1.1 fvdl if (symbol->info.cinfo != NULL)
97 1.1 fvdl free(symbol->info.cinfo);
98 1.1 fvdl break;
99 1.1 fvdl case LABEL:
100 1.1 fvdl if (symbol->info.linfo != NULL)
101 1.1 fvdl free(symbol->info.linfo);
102 1.1 fvdl break;
103 1.1 fvdl case UNINITIALIZED:
104 1.1 fvdl default:
105 1.1 fvdl break;
106 1.1 fvdl }
107 1.1 fvdl free(symbol->name);
108 1.1 fvdl free(symbol);
109 1.1 fvdl }
110 1.1 fvdl
111 1.1 fvdl void
112 1.1 fvdl symtable_open()
113 1.1 fvdl {
114 1.1 fvdl symtable = dbopen(/*filename*/NULL,
115 1.1 fvdl O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
116 1.1 fvdl /*openinfo*/NULL);
117 1.1 fvdl
118 1.1 fvdl if (symtable == NULL) {
119 1.1 fvdl perror("Symbol table creation failed");
120 1.1 fvdl exit(EX_SOFTWARE);
121 1.1 fvdl /* NOTREACHED */
122 1.1 fvdl }
123 1.1 fvdl }
124 1.1 fvdl
125 1.1 fvdl void
126 1.1 fvdl symtable_close()
127 1.1 fvdl {
128 1.1 fvdl if (symtable != NULL) {
129 1.1 fvdl DBT key;
130 1.1 fvdl DBT data;
131 1.1 fvdl
132 1.1 fvdl while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
133 1.1 fvdl symbol_t *stored_ptr;
134 1.1 fvdl
135 1.1 fvdl memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
136 1.1 fvdl symbol_delete(stored_ptr);
137 1.1 fvdl }
138 1.1 fvdl symtable->close(symtable);
139 1.1 fvdl }
140 1.1 fvdl }
141 1.1 fvdl
142 1.1 fvdl /*
143 1.1 fvdl * The semantics of get is to return an uninitialized symbol entry
144 1.1 fvdl * if a lookup fails.
145 1.1 fvdl */
146 1.1 fvdl symbol_t *
147 1.1 fvdl symtable_get(name)
148 1.1 fvdl char *name;
149 1.1 fvdl {
150 1.1 fvdl symbol_t *stored_ptr;
151 1.1 fvdl DBT key;
152 1.1 fvdl DBT data;
153 1.1 fvdl int retval;
154 1.1 fvdl
155 1.1 fvdl key.data = (void *)name;
156 1.1 fvdl key.size = strlen(name);
157 1.1 fvdl
158 1.1 fvdl if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
159 1.1 fvdl if (retval == -1) {
160 1.1 fvdl perror("Symbol table get operation failed");
161 1.1 fvdl exit(EX_SOFTWARE);
162 1.1 fvdl /* NOTREACHED */
163 1.1 fvdl } else if (retval == 1) {
164 1.1 fvdl /* Symbol wasn't found, so create a new one */
165 1.1 fvdl symbol_t *new_symbol;
166 1.1 fvdl
167 1.1 fvdl new_symbol = symbol_create(name);
168 1.1 fvdl data.data = &new_symbol;
169 1.1 fvdl data.size = sizeof(new_symbol);
170 1.1 fvdl if (symtable->put(symtable, &key, &data,
171 1.1 fvdl /*flags*/0) !=0) {
172 1.1 fvdl perror("Symtable put failed");
173 1.1 fvdl exit(EX_SOFTWARE);
174 1.1 fvdl }
175 1.1 fvdl return (new_symbol);
176 1.1 fvdl } else {
177 1.1 fvdl perror("Unexpected return value from db get routine");
178 1.1 fvdl exit(EX_SOFTWARE);
179 1.1 fvdl /* NOTREACHED */
180 1.1 fvdl }
181 1.1 fvdl }
182 1.1 fvdl memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
183 1.1 fvdl return (stored_ptr);
184 1.1 fvdl }
185 1.1 fvdl
186 1.1 fvdl symbol_node_t *
187 1.1 fvdl symlist_search(symlist, symname)
188 1.1 fvdl symlist_t *symlist;
189 1.1 fvdl char *symname;
190 1.1 fvdl {
191 1.1 fvdl symbol_node_t *curnode;
192 1.1 fvdl
193 1.2 lukem curnode = SLIST_FIRST(symlist);
194 1.1 fvdl while(curnode != NULL) {
195 1.1 fvdl if (strcmp(symname, curnode->symbol->name) == 0)
196 1.1 fvdl break;
197 1.2 lukem curnode = SLIST_NEXT(curnode, links);
198 1.1 fvdl }
199 1.1 fvdl return (curnode);
200 1.1 fvdl }
201 1.1 fvdl
202 1.1 fvdl void
203 1.1 fvdl symlist_add(symlist, symbol, how)
204 1.1 fvdl symlist_t *symlist;
205 1.1 fvdl symbol_t *symbol;
206 1.1 fvdl int how;
207 1.1 fvdl {
208 1.1 fvdl symbol_node_t *newnode;
209 1.1 fvdl
210 1.1 fvdl newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
211 1.1 fvdl if (newnode == NULL) {
212 1.1 fvdl stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
213 1.1 fvdl /* NOTREACHED */
214 1.1 fvdl }
215 1.1 fvdl newnode->symbol = symbol;
216 1.1 fvdl if (how == SYMLIST_SORT) {
217 1.1 fvdl symbol_node_t *curnode;
218 1.1 fvdl int mask;
219 1.1 fvdl
220 1.1 fvdl mask = FALSE;
221 1.1 fvdl switch(symbol->type) {
222 1.1 fvdl case REGISTER:
223 1.1 fvdl case SCBLOC:
224 1.1 fvdl case SRAMLOC:
225 1.1 fvdl break;
226 1.1 fvdl case BIT:
227 1.1 fvdl case MASK:
228 1.1 fvdl mask = TRUE;
229 1.1 fvdl break;
230 1.1 fvdl default:
231 1.1 fvdl stop("symlist_add: Invalid symbol type for sorting",
232 1.1 fvdl EX_SOFTWARE);
233 1.1 fvdl /* NOTREACHED */
234 1.1 fvdl }
235 1.1 fvdl
236 1.2 lukem curnode = SLIST_FIRST(symlist);
237 1.1 fvdl if (curnode == NULL
238 1.1 fvdl || (mask && (curnode->symbol->info.minfo->mask >
239 1.1 fvdl newnode->symbol->info.minfo->mask))
240 1.1 fvdl || (!mask && (curnode->symbol->info.rinfo->address >
241 1.1 fvdl newnode->symbol->info.rinfo->address))) {
242 1.1 fvdl SLIST_INSERT_HEAD(symlist, newnode, links);
243 1.1 fvdl return;
244 1.1 fvdl }
245 1.1 fvdl
246 1.1 fvdl while (1) {
247 1.2 lukem if (SLIST_NEXT(curnode, links) == NULL) {
248 1.1 fvdl SLIST_INSERT_AFTER(curnode, newnode,
249 1.1 fvdl links);
250 1.1 fvdl break;
251 1.1 fvdl } else {
252 1.1 fvdl symbol_t *cursymbol;
253 1.1 fvdl
254 1.2 lukem cursymbol = SLIST_NEXT(curnode, links)->symbol;
255 1.1 fvdl if ((mask && (cursymbol->info.minfo->mask >
256 1.1 fvdl symbol->info.minfo->mask))
257 1.1 fvdl || (!mask &&(cursymbol->info.rinfo->address >
258 1.1 fvdl symbol->info.rinfo->address))){
259 1.1 fvdl SLIST_INSERT_AFTER(curnode, newnode,
260 1.1 fvdl links);
261 1.1 fvdl break;
262 1.1 fvdl }
263 1.1 fvdl }
264 1.2 lukem curnode = SLIST_NEXT(curnode, links);
265 1.1 fvdl }
266 1.1 fvdl } else {
267 1.1 fvdl SLIST_INSERT_HEAD(symlist, newnode, links);
268 1.1 fvdl }
269 1.1 fvdl }
270 1.1 fvdl
271 1.1 fvdl void
272 1.1 fvdl symlist_free(symlist)
273 1.1 fvdl symlist_t *symlist;
274 1.1 fvdl {
275 1.1 fvdl symbol_node_t *node1, *node2;
276 1.1 fvdl
277 1.2 lukem node1 = SLIST_FIRST(symlist);
278 1.1 fvdl while (node1 != NULL) {
279 1.2 lukem node2 = SLIST_NEXT(node1, links);
280 1.1 fvdl free(node1);
281 1.1 fvdl node1 = node2;
282 1.1 fvdl }
283 1.1 fvdl SLIST_INIT(symlist);
284 1.1 fvdl }
285 1.1 fvdl
286 1.1 fvdl void
287 1.1 fvdl symlist_merge(symlist_dest, symlist_src1, symlist_src2)
288 1.1 fvdl symlist_t *symlist_dest;
289 1.1 fvdl symlist_t *symlist_src1;
290 1.1 fvdl symlist_t *symlist_src2;
291 1.1 fvdl {
292 1.1 fvdl symbol_node_t *node;
293 1.1 fvdl
294 1.1 fvdl *symlist_dest = *symlist_src1;
295 1.2 lukem while((node = SLIST_FIRST(symlist_src2)) != NULL) {
296 1.1 fvdl SLIST_REMOVE_HEAD(symlist_src2, links);
297 1.1 fvdl SLIST_INSERT_HEAD(symlist_dest, node, links);
298 1.1 fvdl }
299 1.1 fvdl
300 1.1 fvdl /* These are now empty */
301 1.1 fvdl SLIST_INIT(symlist_src1);
302 1.1 fvdl SLIST_INIT(symlist_src2);
303 1.1 fvdl }
304 1.1 fvdl
305 1.1 fvdl void
306 1.1 fvdl symtable_dump(ofile)
307 1.1 fvdl FILE *ofile;
308 1.1 fvdl {
309 1.1 fvdl /*
310 1.1 fvdl * Sort the registers by address with a simple insertion sort.
311 1.1 fvdl * Put bitmasks next to the first register that defines them.
312 1.1 fvdl * Put constants at the end.
313 1.1 fvdl */
314 1.1 fvdl symlist_t registers;
315 1.1 fvdl symlist_t masks;
316 1.1 fvdl symlist_t constants;
317 1.1 fvdl symlist_t download_constants;
318 1.1 fvdl symlist_t aliases;
319 1.1 fvdl
320 1.1 fvdl SLIST_INIT(®isters);
321 1.1 fvdl SLIST_INIT(&masks);
322 1.1 fvdl SLIST_INIT(&constants);
323 1.1 fvdl SLIST_INIT(&download_constants);
324 1.1 fvdl SLIST_INIT(&aliases);
325 1.1 fvdl
326 1.1 fvdl if (symtable != NULL) {
327 1.1 fvdl DBT key;
328 1.1 fvdl DBT data;
329 1.1 fvdl int flag = R_FIRST;
330 1.1 fvdl
331 1.1 fvdl while (symtable->seq(symtable, &key, &data, flag) == 0) {
332 1.1 fvdl symbol_t *cursym;
333 1.1 fvdl
334 1.1 fvdl memcpy(&cursym, data.data, sizeof(cursym));
335 1.1 fvdl switch(cursym->type) {
336 1.1 fvdl case REGISTER:
337 1.1 fvdl case SCBLOC:
338 1.1 fvdl case SRAMLOC:
339 1.1 fvdl symlist_add(®isters, cursym, SYMLIST_SORT);
340 1.1 fvdl break;
341 1.1 fvdl case MASK:
342 1.1 fvdl case BIT:
343 1.1 fvdl symlist_add(&masks, cursym, SYMLIST_SORT);
344 1.1 fvdl break;
345 1.1 fvdl case CONST:
346 1.1 fvdl if (cursym->info.cinfo->define == FALSE) {
347 1.1 fvdl symlist_add(&constants, cursym,
348 1.1 fvdl SYMLIST_INSERT_HEAD);
349 1.1 fvdl }
350 1.1 fvdl break;
351 1.1 fvdl case DOWNLOAD_CONST:
352 1.1 fvdl symlist_add(&download_constants, cursym,
353 1.1 fvdl SYMLIST_INSERT_HEAD);
354 1.1 fvdl break;
355 1.1 fvdl case ALIAS:
356 1.1 fvdl symlist_add(&aliases, cursym,
357 1.1 fvdl SYMLIST_INSERT_HEAD);
358 1.1 fvdl break;
359 1.1 fvdl default:
360 1.1 fvdl break;
361 1.1 fvdl }
362 1.1 fvdl flag = R_NEXT;
363 1.1 fvdl }
364 1.1 fvdl
365 1.1 fvdl /* Put in the masks and bits */
366 1.2 lukem while (! SLIST_EMPTY(&masks)) {
367 1.1 fvdl symbol_node_t *curnode;
368 1.1 fvdl symbol_node_t *regnode;
369 1.1 fvdl char *regname;
370 1.1 fvdl
371 1.2 lukem curnode = SLIST_FIRST(&masks);
372 1.1 fvdl SLIST_REMOVE_HEAD(&masks, links);
373 1.1 fvdl
374 1.1 fvdl regnode =
375 1.2 lukem SLIST_FIRST(&curnode->symbol->info.minfo->symrefs);
376 1.1 fvdl regname = regnode->symbol->name;
377 1.1 fvdl regnode = symlist_search(®isters, regname);
378 1.1 fvdl SLIST_INSERT_AFTER(regnode, curnode, links);
379 1.1 fvdl }
380 1.1 fvdl
381 1.1 fvdl /* Add the aliases */
382 1.2 lukem while (! SLIST_EMPTY(&aliases)) {
383 1.1 fvdl symbol_node_t *curnode;
384 1.1 fvdl symbol_node_t *regnode;
385 1.1 fvdl char *regname;
386 1.1 fvdl
387 1.2 lukem curnode = SLIST_FIRST(&aliases);
388 1.1 fvdl SLIST_REMOVE_HEAD(&aliases, links);
389 1.1 fvdl
390 1.1 fvdl regname = curnode->symbol->info.ainfo->parent->name;
391 1.1 fvdl regnode = symlist_search(®isters, regname);
392 1.1 fvdl SLIST_INSERT_AFTER(regnode, curnode, links);
393 1.1 fvdl }
394 1.1 fvdl
395 1.1 fvdl /* Output what we have */
396 1.1 fvdl fprintf(ofile,
397 1.1 fvdl "/*
398 1.1 fvdl * DO NOT EDIT - This file is automatically generated.
399 1.1 fvdl */\n");
400 1.2 lukem while (! SLIST_EMPTY(®isters)) {
401 1.1 fvdl symbol_node_t *curnode;
402 1.1 fvdl u_int8_t value;
403 1.1 fvdl char *tab_str;
404 1.1 fvdl char *tab_str2;
405 1.1 fvdl
406 1.2 lukem curnode = SLIST_FIRST(®isters);
407 1.1 fvdl SLIST_REMOVE_HEAD(®isters, links);
408 1.1 fvdl switch(curnode->symbol->type) {
409 1.1 fvdl case REGISTER:
410 1.1 fvdl case SCBLOC:
411 1.1 fvdl case SRAMLOC:
412 1.1 fvdl fprintf(ofile, "\n");
413 1.1 fvdl value = curnode->symbol->info.rinfo->address;
414 1.1 fvdl tab_str = "\t";
415 1.1 fvdl tab_str2 = "\t\t";
416 1.1 fvdl break;
417 1.1 fvdl case ALIAS:
418 1.1 fvdl {
419 1.1 fvdl symbol_t *parent;
420 1.1 fvdl
421 1.1 fvdl parent = curnode->symbol->info.ainfo->parent;
422 1.1 fvdl value = parent->info.rinfo->address;
423 1.1 fvdl tab_str = "\t";
424 1.1 fvdl tab_str2 = "\t\t";
425 1.1 fvdl break;
426 1.1 fvdl }
427 1.1 fvdl case MASK:
428 1.1 fvdl case BIT:
429 1.1 fvdl value = curnode->symbol->info.minfo->mask;
430 1.1 fvdl tab_str = "\t\t";
431 1.1 fvdl tab_str2 = "\t";
432 1.1 fvdl break;
433 1.1 fvdl default:
434 1.1 fvdl value = 0; /* Quiet compiler */
435 1.1 fvdl tab_str = NULL;
436 1.1 fvdl tab_str2 = NULL;
437 1.1 fvdl stop("symtable_dump: Invalid symbol type "
438 1.1 fvdl "encountered", EX_SOFTWARE);
439 1.1 fvdl break;
440 1.1 fvdl }
441 1.1 fvdl fprintf(ofile, "#define%s%-16s%s0x%02x\n",
442 1.1 fvdl tab_str, curnode->symbol->name, tab_str2,
443 1.1 fvdl value);
444 1.1 fvdl free(curnode);
445 1.1 fvdl }
446 1.1 fvdl fprintf(ofile, "\n\n");
447 1.1 fvdl
448 1.2 lukem while (! SLIST_EMPTY(&constants)) {
449 1.1 fvdl symbol_node_t *curnode;
450 1.1 fvdl
451 1.2 lukem curnode = SLIST_FIRST(&constants);
452 1.1 fvdl SLIST_REMOVE_HEAD(&constants, links);
453 1.1 fvdl fprintf(ofile, "#define\t%-8s\t0x%02x\n",
454 1.1 fvdl curnode->symbol->name,
455 1.1 fvdl curnode->symbol->info.cinfo->value);
456 1.1 fvdl free(curnode);
457 1.1 fvdl }
458 1.1 fvdl
459 1.1 fvdl
460 1.1 fvdl fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
461 1.1 fvdl
462 1.2 lukem while (! SLIST_EMPTY(&download_constants)) {
463 1.1 fvdl symbol_node_t *curnode;
464 1.1 fvdl
465 1.2 lukem curnode = SLIST_FIRST(&download_constants);
466 1.1 fvdl SLIST_REMOVE_HEAD(&download_constants, links);
467 1.1 fvdl fprintf(ofile, "#define\t%-8s\t0x%02x\n",
468 1.1 fvdl curnode->symbol->name,
469 1.1 fvdl curnode->symbol->info.cinfo->value);
470 1.1 fvdl free(curnode);
471 1.1 fvdl }
472 1.1 fvdl }
473 1.1 fvdl }
474 1.1 fvdl
475