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