Mercurial > hg-old > index.cgi
annotate src/macro.c @ 81:b6b1e79cc277
Moved indexed modes to new expression framework
author | lost |
---|---|
date | Sat, 10 Jan 2009 19:05:15 +0000 |
parents | b7550988b97c |
children | 918be0c02239 |
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; |
57 | 252 if (as -> linestail) |
253 as -> linestail -> next = nl; | |
254 as -> linestail = nl; | |
255 if (!(as -> lineshead)) | |
256 as -> lineshead = nl; | |
0 | 257 |
57 | 258 bloc = blen = 0; |
259 linebuff = NULL; | |
260 for (p2 = m -> lines[lc]; *p2; p2++) | |
0 | 261 { |
57 | 262 if (*p2 == '\\' && isdigit(p2[1])) |
0 | 263 { |
57 | 264 int n; |
265 | |
266 p2++; | |
267 n = *p2 - '0'; | |
268 if (n == 0) | |
269 { | |
67 | 270 for (p3 = m -> name; *p3; p3++) |
57 | 271 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
272 continue; | |
273 } | |
274 if (n < 1 || n > nargs) | |
275 continue; | |
67 | 276 for (p3 = args[n - 1]; *p3; p3++) |
57 | 277 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
0 | 278 continue; |
279 } | |
57 | 280 else if (*p2 == '{') |
0 | 281 { |
57 | 282 int n = 0, n2; |
283 p2++; | |
284 while (*p2 && isdigit(*p2)) | |
0 | 285 { |
57 | 286 n2 = *p2 - '0'; |
287 if (n2 < 0 || n2 > 9) | |
288 n2 = 0; | |
289 n = n * 10 + n2; | |
290 p2++; | |
0 | 291 } |
57 | 292 if (*p2 == '}') |
293 p2++; | |
294 | |
295 if (n == 0) | |
296 { | |
67 | 297 for (p3 = m -> name; *p3; p3++) |
57 | 298 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
299 continue; | |
300 } | |
301 if (n < 1 || n > nargs) | |
302 continue; | |
67 | 303 for (p3 = args[n - 1]; *p3; p3++) |
57 | 304 macro_add_to_buff(&linebuff, &bloc, &blen, *p3); |
0 | 305 continue; |
306 } | |
307 else | |
308 { | |
57 | 309 macro_add_to_buff(&linebuff, &bloc, &blen, *p2); |
0 | 310 } |
311 } | |
57 | 312 |
69 | 313 macro_add_to_buff(&linebuff, &bloc, &blen, 0); |
314 | |
57 | 315 nl -> text = linebuff; |
316 | |
317 lwasm_parse_line(as, nl); | |
318 if (as -> endseen) | |
319 break; | |
320 | |
0 | 321 } |
57 | 322 } |
323 | |
324 // restore context from before the macro was called | |
325 as -> context = oldcontext; | |
0 | 326 |
57 | 327 // clean up |
67 | 328 if (args) |
57 | 329 { |
67 | 330 while (nargs) |
331 { | |
332 lwasm_free(args[--nargs]); | |
333 } | |
334 lwasm_free(args); | |
0 | 335 } |
57 | 336 |
337 // indicate a macro was expanded | |
338 return 0; | |
0 | 339 } |