Mercurial > hg-old > index.cgi
comparison lwasm/parse.c @ 151:427e268e876b
renamed src to lwasm to better reflect its purpose
author | lost |
---|---|
date | Fri, 30 Jan 2009 04:01:55 +0000 |
parents | src/parse.c@f59c0916753d |
children | 745721e13970 |
comparison
equal
deleted
inserted
replaced
150:f0881c115010 | 151:427e268e876b |
---|---|
1 /* | |
2 parse.c | |
3 Copyright © 2008 William Astle | |
4 | |
5 This file is part of LWASM. | |
6 | |
7 LWASM is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 */ | |
20 | |
21 /* | |
22 Contains the general parser | |
23 */ | |
24 | |
25 #define __parse_c_seen__ | |
26 | |
27 #include <ctype.h> | |
28 #include <string.h> | |
29 | |
30 #include "lwasm.h" | |
31 #include "instab.h" | |
32 #include "util.h" | |
33 | |
34 // parse a line and despatch to the appropriate handlers for opcodes | |
35 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l) | |
36 { | |
37 char *p, *p2; | |
38 char *opc; | |
39 int opnum; | |
40 char *sym = NULL; | |
41 | |
42 // if this was a bad op first pass (or otherwise a no-process line) | |
43 // ignore it | |
44 if (l -> badop) | |
45 return; | |
46 | |
47 p = l -> text; | |
48 l -> sect = as -> csect; | |
49 | |
50 // blank lines are a no brainer | |
51 if (!*p) | |
52 { | |
53 as -> context = lwasm_next_context(as); | |
54 return 0; | |
55 } | |
56 | |
57 // for output generation later but only on pass 1 | |
58 // also used by some pseudo ops on pass 2 | |
59 if (as -> passnum == 1) | |
60 l -> codeaddr = as -> addr; | |
61 | |
62 // if it's a comment, return (this doesn't cause a context change) | |
63 if (*p == '*' || *p == ';') | |
64 return; | |
65 | |
66 // if we start with a non-space character, it's a symbol | |
67 if (!isspace(*p)) | |
68 { | |
69 // we have a symbol specified here | |
70 // parse it out and record it for later use | |
71 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
72 /* do nothing */ ; | |
73 | |
74 sym = lwasm_alloc((p2 - p) + 1); | |
75 sym[p2 - p] = '\0'; | |
76 memcpy(sym, p, p2 - p); | |
77 | |
78 p = p2; | |
79 } | |
80 l -> sym = sym; | |
81 | |
82 // now skip any whitespace to find the opcode | |
83 while (*p && isspace(*p)) | |
84 p++; | |
85 | |
86 // is the line blank? | |
87 if (!*p && !sym) | |
88 { | |
89 // nothing but white space *is* a context break | |
90 as -> context = lwasm_next_context(as); | |
91 return; | |
92 } | |
93 | |
94 // parse the opcode | |
95 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
96 /* do nothing */ ; | |
97 | |
98 opc = lwasm_alloc((p2 - p) + 1); | |
99 memcpy(opc, p, p2 - p); | |
100 opc[p2 - p] = '\0'; | |
101 | |
102 debug_message(2, "Found operation code: '%s'", opc); | |
103 | |
104 // skip intervening whitespace if present | |
105 while (*p2 && isspace(*p2)) | |
106 p2++; | |
107 | |
108 // look up instruction in insn table | |
109 for (opnum = 0; instab[opnum].opcode; opnum++) | |
110 { | |
111 if (!strcasecmp(instab[opnum].opcode, opc)) | |
112 break; | |
113 } | |
114 | |
115 // if we found no operation, check if we had a comment | |
116 // the reason this check is here is to allow for "private" | |
117 // operation codes like "*pragma" which will be ignored by | |
118 // other assemblers | |
119 // also skip empty ops | |
120 if (!(instab[opnum].opcode)) | |
121 { | |
122 if (*opc == '*' || *opc == ';' || !*opc) | |
123 goto done_line; | |
124 } | |
125 | |
126 // now we have the opcode and the symbol, we can decide if we're | |
127 // actually going to do anything with this line | |
128 | |
129 // we will NOT call the function if any of the following are true: | |
130 | |
131 // - we are skipping a condition and the operation code is not a conditional | |
132 // - we are defining a macro and the operation code is not ENDM | |
133 | |
134 // we will call the function in any other circumstance | |
135 | |
136 // first condition above | |
137 if (as -> inmacro && instab[opnum].endm == 0) | |
138 { | |
139 add_macro_line(as, l -> text); | |
140 goto done_line; | |
141 } | |
142 | |
143 // second condition above | |
144 if (as -> skipcond && instab[opnum].iscond == 0) | |
145 goto done_line; | |
146 | |
147 // we've registered the symbol as needed | |
148 // now we need to check for a macro call IFF we don't collide with | |
149 // an operation code; otherwise, call the operation function | |
150 if (instab[opnum].opcode) | |
151 { | |
152 if (instab[opnum].fn) | |
153 { | |
154 (instab[opnum].fn)(as, l, &p2, opnum); | |
155 } | |
156 else | |
157 { | |
158 // carp about unimplemented operation | |
159 register_error(as, l, 1, "Unimplemented operation code: %s", opc); | |
160 } | |
161 } | |
162 else | |
163 { | |
164 if (expand_macro(as, l, &p2, opc) == 0) | |
165 goto done_line; | |
166 | |
167 // carp about an unknown operation code and note that fact for | |
168 // pass 2 in case a macro appears later with the same name! | |
169 register_error(as, l, 1, "Uknown operation code: %s", opc); | |
170 l -> badop = 1; | |
171 } | |
172 | |
173 done_line: | |
174 if (!(as -> skipcond || as -> inmacro)) | |
175 { | |
176 // register symbol if the operation didn't | |
177 if (sym && instab[opnum].setsym == 0) | |
178 { | |
179 if (as -> passnum == 1) | |
180 { | |
181 debug_message(1, "Registering symbol '%s' at %04X", sym, l -> codeaddr); | |
182 if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0) | |
183 l -> sym = NULL; | |
184 else | |
185 l -> addrset = 1; | |
186 } | |
187 } | |
188 } | |
189 | |
190 l -> sect = as -> csect; | |
191 l -> context = as -> context; | |
192 | |
193 lwasm_free(opc); | |
194 if (sym) | |
195 lwasm_free(sym); | |
196 } |