-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.c
127 lines (110 loc) · 3.64 KB
/
symbol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/****************************************************************************
*
* Symbol table management
* Copyright (C) 2023 Sean Conner
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Comments, questions and criticisms can be sent to: sean@conman.org
*
****************************************************************************/
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "a09.h"
/**************************************************************************/
int symstrcmp(void const *restrict needle,void const *restrict haystack)
{
label const *key = needle;
struct symbol const *value = haystack;
int rc = memcmp(key->text,value->name.text,min(key->s,value->name.s));
if (rc == 0)
{
if (key->s < value->name.s)
rc = -1;
else if (key->s > value->name.s)
rc = 1;
}
return rc;
}
/**************************************************************************/
static int symtreecmp(void const *restrict needle,void const *restrict haystack)
{
struct symbol const *key = needle;
struct symbol const *value = haystack;
int rc = memcmp(key->name.text,value->name.text,min(key->name.s,value->name.s));
if (rc == 0)
{
if (key->name.s < value->name.s)
rc = -1;
else if (key->name.s > value->name.s)
rc = 1;
}
return rc;
}
/**************************************************************************/
struct symbol *symbol_add(struct a09 *a09,label const *name,uint16_t value)
{
assert(a09 != NULL);
assert(name != NULL);
if (
((name->s == 1) && (toupper(name->text[0]) == 'A'))
|| ((name->s == 1) && (toupper(name->text[0]) == 'B'))
|| ((name->s == 1) && (toupper(name->text[0]) == 'D'))
)
message(a09,MSG_WARNING,"W0013: label '%.*s' could be mistaken for register in index",name->s,name->text);
struct symbol *sym = symbol_find(a09,name);
if (sym == NULL)
{
sym = malloc(sizeof(struct symbol));
if (sym != NULL)
{
sym->tree.left = NULL;
sym->tree.right = NULL;
sym->tree.height = 0;
sym->name = *name;
sym->type = SYM_ADDRESS;
sym->value = value;
sym->filename = a09->infile;
sym->ldef = a09->lnum;
sym->bits = a09->dp == value >> 8 ? 8 : 16;
sym->refs = 0;
a09->symtab = tree_insert(a09->symtab,&sym->tree,symtreecmp);
}
}
else if (sym->type == SYM_SET)
{
sym->value = value;
sym->filename = a09->infile;
sym->ldef = a09->lnum;
sym->bits = value < 256 ? 8 : 16;
}
else
{
message(a09,MSG_ERROR,"E0049: '%.*s' already defined on line %zu",name->s,name->text,sym->ldef);
return NULL;
}
return sym;
}
/**************************************************************************/
void symbol_freetable(tree__s *tree)
{
if (tree != NULL)
{
symbol_freetable(tree->left);
symbol_freetable(tree->right);
free(tree);
}
}
/**************************************************************************/