Mercurial > hg-old > index.cgi
annotate lwasm/parse.c @ 213:c40960370fd8
Added autogen.sh
author | lost |
---|---|
date | Fri, 24 Apr 2009 21:42:06 +0000 |
parents | bae1e3ecdce1 |
children | 436b36214e35 |
rev | line source |
---|---|
36 | 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__ | |
212 | 26 #include <config.h> |
36 | 27 |
28 #include <ctype.h> | |
29 #include <string.h> | |
30 | |
31 #include "lwasm.h" | |
32 #include "instab.h" | |
33 #include "util.h" | |
34 | |
35 // parse a line and despatch to the appropriate handlers for opcodes | |
36 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l) | |
37 { | |
38 char *p, *p2; | |
39 char *opc; | |
40 int opnum; | |
44 | 41 char *sym = NULL; |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
42 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
43 // if this was a bad op first pass (or otherwise a no-process line) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
44 // ignore it |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
45 if (l -> badop) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
46 return; |
36 | 47 |
48 p = l -> text; | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
99
diff
changeset
|
49 l -> sect = as -> csect; |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
50 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
51 // blank lines are a no brainer |
36 | 52 if (!*p) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
53 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
54 as -> context = lwasm_next_context(as); |
36 | 55 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
56 } |
36 | 57 |
52 | 58 // for output generation later but only on pass 1 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
59 // also used by some pseudo ops on pass 2 |
52 | 60 if (as -> passnum == 1) |
61 l -> codeaddr = as -> addr; | |
44 | 62 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
63 // if it's a comment, return (this doesn't cause a context change) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
64 if (*p == '*' || *p == ';') |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
65 return; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
66 |
169 | 67 // if we have C pre-processor directives/output, ignore it |
68 if (*p == '#') | |
69 return; | |
70 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
71 // if we start with a non-space character, it's a symbol |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
72 if (!isspace(*p)) |
36 | 73 { |
74 // we have a symbol specified here | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
75 // parse it out and record it for later use |
157 | 76 for (p2 = p; *p2 && !isspace(*p2) && *p2 != ':'; p2++) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
77 /* do nothing */ ; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
78 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
79 sym = lwasm_alloc((p2 - p) + 1); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
80 sym[p2 - p] = '\0'; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
81 memcpy(sym, p, p2 - p); |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
82 |
44 | 83 p = p2; |
157 | 84 if (!*sym) |
85 { | |
86 register_error(as, l, 1, "Invalid symbol"); | |
87 lwasm_free(sym); | |
88 sym = NULL; | |
89 } | |
90 if (*p == ':') | |
91 p++; | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
92 } |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
93 l -> sym = sym; |
36 | 94 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
95 // now skip any whitespace to find the opcode |
36 | 96 while (*p && isspace(*p)) |
97 p++; | |
98 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
99 // is the line blank? |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
100 if (!*p && !sym) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
101 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
102 // nothing but white space *is* a context break |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
103 as -> context = lwasm_next_context(as); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
104 return; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
105 } |
36 | 106 |
107 // parse the opcode | |
108 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
109 /* do nothing */ ; | |
110 | |
111 opc = lwasm_alloc((p2 - p) + 1); | |
112 memcpy(opc, p, p2 - p); | |
113 opc[p2 - p] = '\0'; | |
114 | |
190 | 115 l -> forceglobal = 0; |
116 // if the opcode contains an =, treat it as "symbol = expr" | |
117 if (!sym && strchr(opc, '=')) | |
118 { | |
119 for (p2 = opc; *p2 && *p2 != '='; p2++) | |
120 /* do nothing */ ; | |
121 sym = lwasm_alloc((p2 - opc) + 1); | |
122 memcpy(sym, opc, p2 - opc); | |
123 sym[p2 - opc] = '\0'; | |
124 l -> sym = sym; | |
125 | |
126 p2 = p + (p2 - opc) + 1; | |
127 // p2++; | |
128 opc[0] = '='; | |
129 opc[1] = '\0'; | |
130 debug_message(2, "Found opcode = with symbol %s and operand %s", sym, p2); | |
131 l -> forceglobal = 1; | |
132 } | |
133 | |
38 | 134 debug_message(2, "Found operation code: '%s'", opc); |
135 | |
36 | 136 // skip intervening whitespace if present |
137 while (*p2 && isspace(*p2)) | |
138 p2++; | |
139 | |
140 // look up instruction in insn table | |
141 for (opnum = 0; instab[opnum].opcode; opnum++) | |
142 { | |
143 if (!strcasecmp(instab[opnum].opcode, opc)) | |
144 break; | |
145 } | |
146 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
147 // if we found no operation, check if we had a comment |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
148 // the reason this check is here is to allow for "private" |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
149 // operation codes like "*pragma" which will be ignored by |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
150 // other assemblers |
68 | 151 // also skip empty ops |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
152 if (!(instab[opnum].opcode)) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
153 { |
68 | 154 if (*opc == '*' || *opc == ';' || !*opc) |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
155 goto done_line; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
156 } |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
157 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
158 // now we have the opcode and the symbol, we can decide if we're |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
159 // actually going to do anything with this line |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
160 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
161 // we will NOT call the function if any of the following are true: |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
162 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
163 // - we are skipping a condition and the operation code is not a conditional |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
164 // - we are defining a macro and the operation code is not ENDM |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
165 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
166 // we will call the function in any other circumstance |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
167 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
168 // first condition above |
57 | 169 if (as -> inmacro && instab[opnum].endm == 0) |
170 { | |
171 add_macro_line(as, l -> text); | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
172 goto done_line; |
57 | 173 } |
174 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
175 // second condition above |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
176 if (as -> skipcond && instab[opnum].iscond == 0) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
177 goto done_line; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
178 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
179 // we've registered the symbol as needed |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
180 // now we need to check for a macro call IFF we don't collide with |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
181 // an operation code; otherwise, call the operation function |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
182 if (instab[opnum].opcode) |
36 | 183 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
184 if (instab[opnum].fn) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
185 { |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
186 (instab[opnum].fn)(as, l, &p2, opnum); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
187 } |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
188 else |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
189 { |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
190 // carp about unimplemented operation |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
191 register_error(as, l, 1, "Unimplemented operation code: %s", opc); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
192 } |
36 | 193 } |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
194 else |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
195 { |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
196 if (expand_macro(as, l, &p2, opc) == 0) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
197 goto done_line; |
36 | 198 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
199 // carp about an unknown operation code and note that fact for |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
200 // pass 2 in case a macro appears later with the same name! |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
201 register_error(as, l, 1, "Uknown operation code: %s", opc); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
202 l -> badop = 1; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
203 } |
57 | 204 |
205 done_line: | |
68 | 206 if (!(as -> skipcond || as -> inmacro)) |
207 { | |
208 // register symbol if the operation didn't | |
209 if (sym && instab[opnum].setsym == 0) | |
210 { | |
211 if (as -> passnum == 1) | |
212 { | |
71
90a5657d5408
Fixed problem with symbols being registered with the address of the NEXT instruction
lost
parents:
68
diff
changeset
|
213 debug_message(1, "Registering symbol '%s' at %04X", sym, l -> codeaddr); |
90a5657d5408
Fixed problem with symbols being registered with the address of the NEXT instruction
lost
parents:
68
diff
changeset
|
214 if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0) |
68 | 215 l -> sym = NULL; |
216 else | |
217 l -> addrset = 1; | |
218 } | |
219 } | |
220 } | |
99 | 221 |
85 | 222 l -> sect = as -> csect; |
91
718998b673ee
Added incomplete references to object output and added support for section base terms in expression handler
lost
parents:
85
diff
changeset
|
223 l -> context = as -> context; |
85 | 224 |
36 | 225 lwasm_free(opc); |
44 | 226 if (sym) |
227 lwasm_free(sym); | |
36 | 228 } |