Mercurial > hg-old > index.cgi
annotate src/symtab.c @ 25:3b818f05dc2a
imported instruction table from older version to development version
author | lost |
---|---|
date | Fri, 02 Jan 2009 02:03:48 +0000 |
parents | 34568fab6058 |
children |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 symtab.c |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
20 Implements code for handling the symbol table. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 */ |
0 | 22 |
23 #include <ctype.h> | |
24 #include <errno.h> | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 #define __symtab_c_seen__ | |
29 #include "lwasm.h" | |
30 | |
31 void register_symbol(asmstate_t *as, sourceline_t *cl, char *symstr, int val, int flags) | |
32 { | |
33 symtab_t *st; | |
34 symtab_t *prev; | |
35 int cv = -2; | |
36 | |
37 for (prev = NULL, st = as -> symbol_table; st; st = st -> next) | |
38 { | |
39 cv = strcasecmp(st -> symbol, symstr); | |
40 if (cv == 0) | |
41 { | |
42 cv = strcmp(st -> symbol, symstr); | |
43 } | |
44 if (cv >= 0) | |
45 break; | |
46 prev = st; | |
47 } | |
48 // cv is 0 if we found the symbol, > 0 if we didn't and found one | |
49 // later in order, or -2 if we never did a comparison | |
50 // if st is NULL, the value of cv is irrelevant as it means | |
51 // we fell off the end of the list | |
52 // if st is NULL and prev is not, then prev is the tail of the list | |
53 // if both are NULL, the list is empty | |
54 | |
55 // handle adding the symbol if needed | |
56 if (!st || cv != 0) | |
57 { | |
58 symtab_t *st2; | |
59 // register the symbol | |
60 st2 = malloc(sizeof(symtab_t)); | |
61 st2 -> symbol = strdup(symstr); | |
62 st2 -> addr = val; | |
63 st2 -> flags = 0; | |
64 if (flags & SYMFLAG_SET) | |
65 st2 -> flags |= SYMFLAG_SET; | |
66 | |
67 if (prev) | |
68 prev -> next = st2; | |
69 else | |
70 as -> symbol_table = st2; | |
71 | |
72 st2 -> next = st; | |
73 return; | |
74 } | |
75 | |
76 // st is NOT NULL here and cv IS 0 | |
77 if ((flags & SYMFLAG_SET) && ((st -> flags) & SYMFLAG_SET)) | |
78 { | |
79 // symbol already exists but it is a "SET" symbol so reset the value | |
80 st -> addr = val; | |
81 return; | |
82 } | |
83 if (st && as -> passnum == 1) | |
84 { | |
85 // duplicate symbol, flag error | |
86 errorp1(ERR_DUPSYM); | |
87 } | |
88 if (st -> addr != val) | |
89 errorp2(ERR_PHASE); | |
90 } | |
91 | |
92 int lookup_symbol(asmstate_t *as, char *symstr) | |
93 { | |
94 symtab_t *st; | |
95 | |
96 for (st = as -> symbol_table; st; st = st -> next) | |
97 { | |
98 if (!strcmp(symstr, st -> symbol)) | |
99 break; | |
100 } | |
101 if (st) | |
102 return st -> addr; | |
103 return -1; | |
104 } | |
105 | |
106 void list_symbols(asmstate_t *as, FILE *f) | |
107 { | |
108 symtab_t *st; | |
109 for (st = as -> symbol_table; st; st = st -> next) | |
110 { | |
111 fprintf(f, "%04X %s%s\n", st -> addr, st -> symbol, (st -> flags & SYMFLAG_SET) ? "(S)" : ""); | |
112 } | |
113 } |