annotate lwasm/symbol.c @ 355:981e34165e97

Added os9 directives to instruction table
author lost@starbug
date Tue, 30 Mar 2010 23:12:41 -0600
parents a82c55070624
children 7d91ab7ac7d6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
1 /*
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
2 symbol.c
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
3
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
4 Copyright © 2010 William Astle
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
5
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
6 This file is part of LWTOOLS.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
7
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
11 version.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
12
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
16 more details.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
17
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
20 */
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
21
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
22 #include <config.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
23
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
24 #include <stdlib.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
25 #include <string.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
26
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
27 #include <lw_alloc.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
28 #include <lw_expr.h>
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
29
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
30 #include "lwasm.h"
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
31
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
32 struct symtabe *register_symbol(asmstate_t *as, line_t *cl, char *sym, lw_expr_t val, int flags)
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
33 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
34 struct symtabe *se;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
35 int islocal = 0;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
36 int context = -1;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
37 int version = -1;
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
38 char *cp;
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
39
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
40 if (*sym < 0x80 && !strchr(SSYMCHARS, *sym))
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
41 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
42 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
43 return NULL;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
44 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
45
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
46 if ((*sym == '$' || *sym == '@') && (sym[1] >= '0' && sym[1] <= '9'))
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
47 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
48 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
49 return NULL;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
50 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
51
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
52 for (cp = sym; *cp; cp++)
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
53 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
54 if (*cp == '@' || *cp == '?')
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
55 islocal = 1;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
56 if (*cp == '$' && !(as -> pragmas & PRAGMA_DOLLARNOTLOCAL))
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
57 islocal = 1;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
58
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
59 // bad symbol
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
60 if (*cp < 0x80 && !strchr(SYMCHARS, *cp))
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
61 {
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
62 lwasm_register_error(as, cl, "Bad symbol (%s)", sym);
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
63 return NULL;
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
64 }
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
65 }
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
66
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
67 if (islocal)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
68 context = as -> context;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
69
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
70 // first, look up symbol to see if it is already defined
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
71 for (se = as -> symtab.head; se; se = se -> next)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
72 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
73 if (!strcmp(sym, se -> symbol))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
74 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
75 if (se -> context != context)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
76 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
77 if ((flags & symbol_flag_set) && (se -> flags & symbol_flag_set))
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
78 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
79 if (version < se -> version)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
80 version = se -> version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
81 continue;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
82 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
83 break;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
84 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
85 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
86 if (se)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
87 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
88 // multiply defined symbol
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
89 lwasm_register_error(as, cl, "Multiply defined symbol (%s)", sym);
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
90 return NULL;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
91 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
92
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
93 if (flags & symbol_flag_set)
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
94 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
95 version++;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
96 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
97
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
98 se = lw_alloc(sizeof(struct symtabe));
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
99 se -> next = as -> symtab.head;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
100 as -> symtab.head = se;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
101 se -> context = context;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
102 se -> version = version;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
103 se -> flags = flags;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
104 se -> value = lw_expr_copy(val);
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
105 return se;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
106 }
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
107
344
0215a0fbf61b Added assembly error system and additional checks for symbol syntax
lost@starbug
parents: 342
diff changeset
108 struct symtabe * lookup_symbol(asmstate_t *as, line_t *cl, char *sym, int context, int version)
342
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
109 {
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
110 return NULL;
7b4123dce741 Added basic symbol registration
lost@starbug
parents:
diff changeset
111 }