Mercurial > hg-old > index.cgi
annotate lwasm/parse.c @ 165:566943f98f8d
Made pragma actually take multiple pragmas on one line
author | lost |
---|---|
date | Sat, 31 Jan 2009 18:22:02 +0000 |
parents | 745721e13970 |
children | 99300be2d3bd |
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__ | |
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; | |
44 | 40 char *sym = NULL; |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
41 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
42 // 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
|
43 // ignore it |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
44 if (l -> badop) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
45 return; |
36 | 46 |
47 p = l -> text; | |
101
f59c0916753d
Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents:
99
diff
changeset
|
48 l -> sect = as -> csect; |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
49 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
50 // blank lines are a no brainer |
36 | 51 if (!*p) |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
52 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
53 as -> context = lwasm_next_context(as); |
36 | 54 return 0; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
55 } |
36 | 56 |
52 | 57 // 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
|
58 // also used by some pseudo ops on pass 2 |
52 | 59 if (as -> passnum == 1) |
60 l -> codeaddr = as -> addr; | |
44 | 61 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
62 // 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
|
63 if (*p == '*' || *p == ';') |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
64 return; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
65 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
66 // 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
|
67 if (!isspace(*p)) |
36 | 68 { |
69 // we have a symbol specified here | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
70 // parse it out and record it for later use |
157 | 71 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
|
72 /* do nothing */ ; |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
73 |
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
74 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
|
75 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
|
76 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
|
77 |
44 | 78 p = p2; |
157 | 79 if (!*sym) |
80 { | |
81 register_error(as, l, 1, "Invalid symbol"); | |
82 lwasm_free(sym); | |
83 sym = NULL; | |
84 } | |
85 if (*p == ':') | |
86 p++; | |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
87 } |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
88 l -> sym = sym; |
36 | 89 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
90 // now skip any whitespace to find the opcode |
36 | 91 while (*p && isspace(*p)) |
92 p++; | |
93 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
94 // is the line blank? |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
95 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
|
96 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
97 // nothing but white space *is* a context break |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
98 as -> context = lwasm_next_context(as); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
99 return; |
37
538e15927776
Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents:
36
diff
changeset
|
100 } |
36 | 101 |
102 // parse the opcode | |
103 for (p2 = p; *p2 && !isspace(*p2); p2++) | |
104 /* do nothing */ ; | |
105 | |
106 opc = lwasm_alloc((p2 - p) + 1); | |
107 memcpy(opc, p, p2 - p); | |
108 opc[p2 - p] = '\0'; | |
109 | |
38 | 110 debug_message(2, "Found operation code: '%s'", opc); |
111 | |
36 | 112 // skip intervening whitespace if present |
113 while (*p2 && isspace(*p2)) | |
114 p2++; | |
115 | |
116 // look up instruction in insn table | |
117 for (opnum = 0; instab[opnum].opcode; opnum++) | |
118 { | |
119 if (!strcasecmp(instab[opnum].opcode, opc)) | |
120 break; | |
121 } | |
122 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
123 // 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
|
124 // 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
|
125 // 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
|
126 // other assemblers |
68 | 127 // also skip empty ops |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
128 if (!(instab[opnum].opcode)) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
129 { |
68 | 130 if (*opc == '*' || *opc == ';' || !*opc) |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
131 goto done_line; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
132 } |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
133 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
134 // 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
|
135 // actually going to do anything with this line |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
136 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
137 // 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
|
138 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
139 // - 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
|
140 // - 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
|
141 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
142 // 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
|
143 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
144 // first condition above |
57 | 145 if (as -> inmacro && instab[opnum].endm == 0) |
146 { | |
147 add_macro_line(as, l -> text); | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
148 goto done_line; |
57 | 149 } |
150 | |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
151 // second condition above |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
152 if (as -> skipcond && instab[opnum].iscond == 0) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
153 goto done_line; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
154 |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
155 // we've registered the symbol as needed |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
156 // 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
|
157 // an operation code; otherwise, call the operation function |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
158 if (instab[opnum].opcode) |
36 | 159 { |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
160 if (instab[opnum].fn) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
161 { |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
162 (instab[opnum].fn)(as, l, &p2, opnum); |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
163 } |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
164 else |
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 // carp about unimplemented operation |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
167 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
|
168 } |
36 | 169 } |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
170 else |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
171 { |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
172 if (expand_macro(as, l, &p2, opc) == 0) |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
173 goto done_line; |
36 | 174 |
66
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
175 // 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
|
176 // 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
|
177 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
|
178 l -> badop = 1; |
aa9d9fedfdf4
Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents:
64
diff
changeset
|
179 } |
57 | 180 |
181 done_line: | |
68 | 182 if (!(as -> skipcond || as -> inmacro)) |
183 { | |
184 // register symbol if the operation didn't | |
185 if (sym && instab[opnum].setsym == 0) | |
186 { | |
187 if (as -> passnum == 1) | |
188 { | |
71
90a5657d5408
Fixed problem with symbols being registered with the address of the NEXT instruction
lost
parents:
68
diff
changeset
|
189 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
|
190 if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0) |
68 | 191 l -> sym = NULL; |
192 else | |
193 l -> addrset = 1; | |
194 } | |
195 } | |
196 } | |
99 | 197 |
85 | 198 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
|
199 l -> context = as -> context; |
85 | 200 |
36 | 201 lwasm_free(opc); |
44 | 202 if (sym) |
203 lwasm_free(sym); | |
36 | 204 } |