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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
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
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
22
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23 #include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 #include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27 #include <string.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 #define __symtab_c_seen__
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 void register_symbol(asmstate_t *as, sourceline_t *cl, char *symstr, int val, int flags)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
33 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 symtab_t *prev;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35 int cv = -2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
37 for (prev = NULL, st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
38 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39 cv = strcasecmp(st -> symbol, symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40 if (cv == 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
41 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42 cv = strcmp(st -> symbol, symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
43 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44 if (cv >= 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 prev = st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48 // cv is 0 if we found the symbol, > 0 if we didn't and found one
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 // later in order, or -2 if we never did a comparison
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50 // if st is NULL, the value of cv is irrelevant as it means
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 // we fell off the end of the list
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 // if st is NULL and prev is not, then prev is the tail of the list
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 // if both are NULL, the list is empty
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55 // handle adding the symbol if needed
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 if (!st || cv != 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 symtab_t *st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59 // register the symbol
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 st2 = malloc(sizeof(symtab_t));
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 st2 -> symbol = strdup(symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 st2 -> addr = val;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 st2 -> flags = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 if (flags & SYMFLAG_SET)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65 st2 -> flags |= SYMFLAG_SET;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 if (prev)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 prev -> next = st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 as -> symbol_table = st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 st2 -> next = st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
74 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
75
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
76 // st is NOT NULL here and cv IS 0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 if ((flags & SYMFLAG_SET) && ((st -> flags) & SYMFLAG_SET))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79 // symbol already exists but it is a "SET" symbol so reset the value
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
80 st -> addr = val;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
81 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
82 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
83 if (st && as -> passnum == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85 // duplicate symbol, flag error
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 errorp1(ERR_DUPSYM);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 if (st -> addr != val)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89 errorp2(ERR_PHASE);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 int lookup_symbol(asmstate_t *as, char *symstr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 for (st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
98 if (!strcmp(symstr, st -> symbol))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
99 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
100 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
101 if (st)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
102 return st -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
103 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
104 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
105
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
106 void list_symbols(asmstate_t *as, FILE *f)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
107 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
108 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
109 for (st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
110 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
111 fprintf(f, "%04X %s%s\n", st -> addr, st -> symbol, (st -> flags & SYMFLAG_SET) ? "(S)" : "");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
112 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
113 }