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