Mercurial > hg-old > index.cgi
annotate src/macro.c @ 112:a567dbb3f1d4
command line options
author | lost |
---|---|
date | Sat, 17 Jan 2009 16:55:53 +0000 |
parents | 918be0c02239 |
children |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 macro.c |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
20 Contains stuff associated with macro processing |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 */ |
0 | 22 |
23 #include <ctype.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include "lwasm.h" | |
57 | 27 #include "instab.h" |
28 #include "util.h" | |
0 | 29 |
57 | 30 OPFUNC(pseudo_macro) |
0 | 31 { |
32 macrotab_t *m; | |
33 | |
57 | 34 // if skipping a condition, flag in a macro |
35 if (as -> skipcond) | |
36 { | |
37 as -> skipmacro = 1; | |
38 return; | |
39 } | |
40 | |
41 // actually define a macro | |
0 | 42 if (as -> inmacro) |
43 { | |
57 | 44 register_error(as, l, 1, "Attempt to define a macro inside a macro"); |
0 | 45 return; |
46 } | |
57 | 47 |
0 | 48 as -> inmacro = 1; |
57 | 49 |
50 // don't actually do anything if not pass 1 | |
0 | 51 if (as -> passnum != 1) |
52 return; | |
57 | 53 |
54 | |
55 if (!l -> sym) | |
56 { | |
57 register_error(as, l, 1, "Macro definition with no effect - no symbol"); | |
58 return; | |
59 } | |
0 | 60 |
57 | 61 // search for macro by same name... |
0 | 62 for (m = as -> macros; m; m = m -> next) |
63 { | |
57 | 64 if (!strcmp(m -> name, l -> sym)) |
0 | 65 break; |
66 } | |
67 if (m) | |
68 { | |
57 | 69 register_error(as, l, 1, "Duplicate macro definition"); |
0 | 70 return; |
71 } | |
57 | 72 |
73 m = lwasm_alloc(sizeof(macrotab_t)); | |
74 m -> name = lwasm_strdup(l -> sym); | |
0 | 75 m -> next = as -> macros; |
57 | 76 m -> lines = NULL; |
77 m -> numlines = 0; | |
0 | 78 as -> macros = m; |
57 | 79 |
80 while (**p && !isspace(**p)) | |
81 (*p)++; | |
0 | 82 } |
83 | |
57 | 84 OPFUNC(pseudo_endm) |
0 | 85 { |
57 | 86 if (as -> skipcond) |
87 { | |
88 as -> skipmacro = 0; | |
89 return; | |
90 } | |
91 | |
0 | 92 if (!as -> inmacro) |
93 { | |
57 | 94 register_error(as, l, 1, "ENDM without MACRO"); |
0 | 95 return; |
96 } | |
97 | |
98 as -> inmacro = 0; | |
57 | 99 |
100 // a macro definition counts as a context break for local symbols | |
101 as -> context = lwasm_next_context(as); | |
0 | 102 } |
103 | |
57 | 104 // the current macro will ALWAYS be the first one in the table |
105 int add_macro_line(asmstate_t *as, char *optr) | |
0 | 106 { |
107 if (!as -> inmacro) | |
108 return 0; | |
109 | |
110 if (as -> passnum == 2) | |
111 return 1; | |
112 | |
68 | 113 as -> macros -> lines = lwasm_realloc(as -> macros -> lines, sizeof(char *) * (as -> macros -> numlines + 1)); |
57 | 114 as -> macros -> lines[as -> macros -> numlines] = lwasm_strdup(optr); |
115 as -> macros -> numlines += 1; | |
0 | 116 return 1; |
117 } | |
118 | |
119 void macro_add_to_buff(char **buff, int *loc, int *len, char c) | |
120 { | |
121 if (*loc == *len) | |
122 { | |
69 | 123 *buff = lwasm_realloc(*buff, *len + 32); |
0 | 124 *len += 32; |
125 } | |
126 (*buff)[(*loc)++] = c; | |
127 } | |
128 | |
129 // this is just like a regular operation function | |
130 /* | |
57 | 131 macro args are referenced by "\n" where 1 <= n <= 9 |
0 | 132 or by "\{n}"; a \ can be included by writing \\ |
57 | 133 a comma separates argument but one can be included with "\," |
134 whitespace ends argument list but can be included with "\ " or the like | |
135 | |
136 In pass 1, actually add the lines to the system, in pass 2, do not | |
137 In pass 2, track the number of lines to skip because they already will be | |
138 processed by this function - this will be in as -> skiplines | |
139 | |
0 | 140 */ |
57 | 141 int expand_macro(asmstate_t *as, lwasm_line_t *l, char **p, char *opc) |
0 | 142 { |
57 | 143 int lc; |
144 lwasm_line_t *cl, *nl; | |
145 int oldcontext; | |
0 | 146 macrotab_t *m; |
57 | 147 |
148 char **args = NULL; // macro arguments | |
67 | 149 int nargs = 0; // number of arguments |
57 | 150 |
151 char *p2, *p3; | |
0 | 152 |
57 | 153 int bloc, blen; |
154 char *linebuff; | |
155 | |
156 for (m = as -> macros; m; m = m -> next) | |
157 { | |
158 if (!strcmp(opc, m -> name)) | |
159 break; | |
160 } | |
161 // signal no macro expansion | |
162 if (!m) | |
163 return -1; | |
0 | 164 |
57 | 165 |
166 // save current symbol context for after macro expansion | |
167 oldcontext = as -> context; | |
168 | |
169 cl = l; | |
170 | |
171 as -> context = lwasm_next_context(as); | |
172 | |
173 // step 1: parse arguments (pass 1 only) | |
174 if (as -> passnum == 1) | |
0 | 175 { |
57 | 176 while (**p && !isspace(**p) && **p != ',') |
0 | 177 { |
57 | 178 p2 = *p; |
179 while (*p2 && !isspace(*p2) && *p2 != ',') | |
180 { | |
181 if (*p2 == '\\') | |
182 { | |
183 if (p2[1]) | |
184 p2++; | |
185 } | |
67 | 186 p2++; |
57 | 187 } |
188 | |
189 // have arg here | |
68 | 190 args = lwasm_realloc(args, sizeof(char *) * (nargs + 1)); |
57 | 191 args[nargs] = lwasm_alloc(p2 - *p + 1); |
192 args[nargs][p2 - *p] = '\0'; | |
193 memcpy(args[nargs], *p, p2 - *p); | |
194 *p = p2; | |
195 | |
196 // now collapse out "\" characters | |
197 for (p3 = p2 = args[nargs]; *p2; p2++, p3++) | |
198 { | |
199 if (*p2 == '\\' && p2[1]) | |
200 { | |
201 p2++; | |
202 } | |
203 *p3 = *p2; | |
204 } | |
205 *p3 = '\0'; | |
206 | |
207 nargs++; | |
68 | 208 if (**p == ',') |
209 (*p)++; | |
0 | 210 } |
211 } | |
212 | |
69 | 213 { |
214 int i; | |
215 for (i = 0; i < nargs; i++) | |
216 { | |
217 debug_message(10, "Macro (%s) arg %d: %s", m -> name, i + 1, args[i]); | |
218 } | |
219 } | |
220 | |
57 | 221 // step 2: iterate over the lines |
222 if (as -> passnum == 2) | |
223 { | |
224 // pass 2 only - parse the lines and count them | |
225 for (lc = 0; lc < m -> numlines; lc++) | |
226 { | |
227 cl = cl -> next; | |
228 as -> skiplines++; | |
229 lwasm_parse_line(as, cl); | |
230 } | |
231 } | |
232 else | |
0 | 233 { |
57 | 234 // pass 1 only - construct the lines and parse them |
235 for (lc = 0; lc < m -> numlines; lc++) | |
236 { | |
237 nl = lwasm_alloc(sizeof(lwasm_line_t)); | |
238 nl -> lineno = lc + 1; | |
239 nl -> filename = m -> name; | |
240 nl -> next = NULL; | |
241 nl -> prev = as -> linestail; | |
242 nl -> err = NULL; | |
243 nl -> fsize = 0; | |
244 nl -> sym = NULL; | |
245 nl -> bytes = NULL; | |
246 nl -> codelen = 0; | |
247 nl -> codesize = 0; | |
248 nl -> nocodelen = 0; | |
249 nl -> addrset = 0; | |
250 nl -> symaddr = -1; | |
67 | 251 nl -> badop = 0; |
85 | 252 nl -> relocoff = -1; |
57 | 253 if (as -> linestail) |
254 as -> linestail -> next = nl; | |
255 as -> linestail = nl; | |
256 if (!(as -> lineshead)) | |
257 as -> lineshead = nl; | |
0 | 258 |
57 | 259 bloc = blen = 0; |
260 linebuff = NULL; | |
261 for (p2 = m -> lines[lc]; *p2; p2++) | |
0 | 262 { |
57 | 263 if (*p2 == '\\' && isdigit(p2[1])) |
0 | 264 { |
57 | 265 int n; |
266 | |
267 p2++; | |
268 n = *p2 - '0'; | |
269 if (n == 0) | |
270 { | |
67 | 271 for (p3 = m -> name; *p3; p3++) |
57 | 272 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
273 continue; | |
274 } | |
275 if (n < 1 || n > nargs) | |
276 continue; | |
67 | 277 for (p3 = args[n - 1]; *p3; p3++) |
57 | 278 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
0 | 279 continue; |
280 } | |
57 | 281 else if (*p2 == '{') |
0 | 282 { |
57 | 283 int n = 0, n2; |
284 p2++; | |
285 while (*p2 && isdigit(*p2)) | |
0 | 286 { |
57 | 287 n2 = *p2 - '0'; |
288 if (n2 < 0 || n2 > 9) | |
289 n2 = 0; | |
290 n = n * 10 + n2; | |
291 p2++; | |
0 | 292 } |
57 | 293 if (*p2 == '}') |
294 p2++; | |
295 | |
296 if (n == 0) | |
297 { | |
67 | 298 for (p3 = m -> name; *p3; p3++) |
57 | 299 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
300 continue; | |
301 } | |
302 if (n < 1 || n > nargs) | |
303 continue; | |
67 | 304 for (p3 = args[n - 1]; *p3; p3++) |
57 | 305 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
0 | 306 continue; |
307 } | |
308 else | |
309 { | |
57 | 310 macro_add_to_buff(&linebuff, &bloc, &blen, *p2); |
0 | 311 } |
312 } | |
57 | 313 |
69 | 314 macro_add_to_buff(&linebuff, &bloc, &blen, 0); |
315 | |
57 | 316 nl -> text = linebuff; |
317 | |
318 lwasm_parse_line(as, nl); | |
319 if (as -> endseen) | |
320 break; | |
321 | |
0 | 322 } |
57 | 323 } |
324 | |
325 // restore context from before the macro was called | |
326 as -> context = oldcontext; | |
0 | 327 |
57 | 328 // clean up |
67 | 329 if (args) |
57 | 330 { |
67 | 331 while (nargs) |
332 { | |
333 lwasm_free(args[--nargs]); | |
334 } | |
335 lwasm_free(args); | |
0 | 336 } |
57 | 337 |
338 // indicate a macro was expanded | |
339 return 0; | |
0 | 340 } |