annotate src/symtab.c @ 0:57495da01900

Initial checking of LWASM
author lost
date Fri, 03 Oct 2008 02:44:20 +0000
parents
children 34568fab6058
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
1 /*
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
2 * symtab.c
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
3 *
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
4 * main code for lwasm
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
5 */
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
6
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
7 #include <ctype.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
8 #include <errno.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
9 #include <stdio.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
10 #include <stdlib.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
11 #include <string.h>
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
12 #define __symtab_c_seen__
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
13 #include "lwasm.h"
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
14
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
15 void register_symbol(asmstate_t *as, sourceline_t *cl, char *symstr, int val, int flags)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
16 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
17 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
18 symtab_t *prev;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
19 int cv = -2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
20
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
21 for (prev = NULL, st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
22 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
23 cv = strcasecmp(st -> symbol, symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
24 if (cv == 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
25 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
26 cv = strcmp(st -> symbol, symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
27 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
28 if (cv >= 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
29 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
30 prev = st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
31 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
32 // 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
33 // later in order, or -2 if we never did a comparison
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
34 // if st is NULL, the value of cv is irrelevant as it means
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
35 // we fell off the end of the list
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
36 // 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
37 // if both are NULL, the list is empty
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
38
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
39 // handle adding the symbol if needed
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
40 if (!st || cv != 0)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
41 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
42 symtab_t *st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
43 // register the symbol
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
44 st2 = malloc(sizeof(symtab_t));
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
45 st2 -> symbol = strdup(symstr);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
46 st2 -> addr = val;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
47 st2 -> flags = 0;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
48 if (flags & SYMFLAG_SET)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
49 st2 -> flags |= SYMFLAG_SET;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
50
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
51 if (prev)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
52 prev -> next = st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
53 else
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
54 as -> symbol_table = st2;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
55
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
56 st2 -> next = st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
57 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
58 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
59
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
60 // st is NOT NULL here and cv IS 0
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
61 if ((flags & SYMFLAG_SET) && ((st -> flags) & SYMFLAG_SET))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
62 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
63 // symbol already exists but it is a "SET" symbol so reset the value
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
64 st -> addr = val;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
65 return;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
66 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
67 if (st && as -> passnum == 1)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
68 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
69 // duplicate symbol, flag error
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
70 errorp1(ERR_DUPSYM);
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
71 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
72 if (st -> addr != val)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
73 errorp2(ERR_PHASE);
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 int lookup_symbol(asmstate_t *as, char *symstr)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
77 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
78 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
79
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
80 for (st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
81 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
82 if (!strcmp(symstr, st -> symbol))
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
83 break;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
84 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
85 if (st)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
86 return st -> addr;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
87 return -1;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
88 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
89
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
90 void list_symbols(asmstate_t *as, FILE *f)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
91 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
92 symtab_t *st;
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
93 for (st = as -> symbol_table; st; st = st -> next)
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
94 {
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
95 fprintf(f, "%04X %s%s\n", st -> addr, st -> symbol, (st -> flags & SYMFLAG_SET) ? "(S)" : "");
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
96 }
57495da01900 Initial checking of LWASM
lost
parents:
diff changeset
97 }