comparison lwasm/macro.c @ 345:7416c3f9c321

Basic macro processor ported forward; added context break handling for local symbols
author lost@starbug
date Thu, 25 Mar 2010 23:17:54 -0600
parents old-trunk/lwasm/old/macro.c@eb230fa7d28e
children a82c55070624
comparison
equal deleted inserted replaced
344:0215a0fbf61b 345:7416c3f9c321
1 /*
2 macro.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 Contains stuff associated with macro processing
21 */
22
23 #include <config.h>
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 #include <lw_alloc.h>
31 #include <lw_string.h>
32
33 #include "lwasm.h"
34 #include "input.h"
35 #include "instab.h"
36
37 PARSEFUNC(pseudo_macro_parse)
38 {
39 macrotab_t *m;
40
41 if (as -> skipcond)
42 {
43 as -> skipmacro = 1;
44 return;
45 }
46
47 if (as -> inmacro)
48 {
49 lwasm_register_error(as, l, "Attempt to define a macro inside a macro");
50 return;
51 }
52
53 if (!(l -> sym))
54 {
55 lwasm_register_error(as, l, "Missing macro name");
56 return;
57 }
58
59 for (m = as -> macros; m; m = m -> next)
60 {
61 if (!strcmp(m -> name, l -> sym))
62 break;
63 }
64 if (m)
65 {
66 lwasm_register_error(as, l, "Duplicate macro definition");
67 return;
68 }
69
70 m = lw_alloc(sizeof(macrotab_t));
71 m -> name = lw_strdup(l -> sym);
72 m -> next = as -> macros;
73 m -> lines = NULL;
74 m -> numlines = 0;
75 as -> macros = m;
76
77 while (**p && !isspace(**p))
78 (*p)++;
79
80 as -> inmacro = 1;
81 }
82
83 PARSEFUNC(pseudo_endm_parse)
84 {
85 if (as -> skipcond)
86 {
87 as -> skipmacro = 0;
88 return;
89 }
90
91 if (!as -> inmacro)
92 {
93 lwasm_register_error(as, l, "ENDM without MACRO");
94 return;
95 }
96
97 as -> inmacro = 0;
98
99 // a macro definition counts as a context break for local symbols
100 as -> context = lwasm_next_context(as);
101 }
102
103 // the current macro will ALWAYS be the first one in the table
104 int add_macro_line(asmstate_t *as, char *optr)
105 {
106 if (!as -> inmacro)
107 return 0;
108
109 as -> macros -> lines = lw_realloc(as -> macros -> lines, sizeof(char *) * (as -> macros -> numlines + 1));
110 as -> macros -> lines[as -> macros -> numlines] = lw_strdup(optr);
111 as -> macros -> numlines += 1;
112 return 1;
113 }
114
115 void macro_add_to_buff(char **buff, int *loc, int *len, char c)
116 {
117 if (*loc == *len)
118 {
119 *buff = lw_realloc(*buff, *len + 32);
120 *len += 32;
121 }
122 (*buff)[(*loc)++] = c;
123 }
124
125 // this is just like a regular operation function
126 /*
127 macro args are referenced by "\n" where 1 <= n <= 9
128 or by "\{n}"; a \ can be included by writing \\
129 a comma separates argument but one can be included with "\,"
130 whitespace ends argument list but can be included with "\ " or the like
131
132 */
133 int expand_macro(asmstate_t *as, line_t *l, char **p, char *opc)
134 {
135 int lc;
136 line_t *cl, *nl;
137 int oldcontext;
138 macrotab_t *m;
139
140 char **args = NULL; // macro arguments
141 int nargs = 0; // number of arguments
142
143 char *p2, *p3;
144
145 int bloc, blen;
146 char *linebuff;
147
148 for (m = as -> macros; m; m = m -> next)
149 {
150 if (!strcmp(opc, m -> name))
151 break;
152 }
153 // signal no macro expansion
154 if (!m)
155 return -1;
156
157 // save current symbol context for after macro expansion
158 oldcontext = as -> context;
159
160 cl = l;
161
162 as -> context = lwasm_next_context(as);
163
164 while (**p && !isspace(**p) && **p != ',')
165 {
166 p2 = *p;
167 while (*p2 && !isspace(*p2) && *p2 != ',')
168 {
169 if (*p2 == '\\')
170 {
171 if (p2[1])
172 p2++;
173 }
174 p2++;
175 }
176
177 // have arg here
178 args = lw_realloc(args, sizeof(char *) * (nargs + 1));
179 args[nargs] = lw_alloc(p2 - *p + 1);
180 args[nargs][p2 - *p] = '\0';
181 memcpy(args[nargs], *p, p2 - *p);
182 *p = p2;
183
184 // now collapse out "\" characters
185 for (p3 = p2 = args[nargs]; *p2; p2++, p3++)
186 {
187 if (*p2 == '\\' && p2[1])
188 {
189 p2++;
190 }
191 *p3 = *p2;
192 }
193 *p3 = '\0';
194
195 nargs++;
196 if (**p == ',')
197 (*p)++;
198 }
199
200
201 // now create a string for the macro
202 // and push it into the front of the input stack
203 bloc = blen = 0;
204 linebuff = NULL;
205
206 for (lc = 0; lc < m -> numlines; lc++)
207 {
208 for (p2 = m -> lines[lc]; *p2; p2++)
209 {
210 if (*p2 == '\\' && isdigit(p2[1]))
211 {
212 int n;
213
214 p2++;
215 n = *p2 - '0';
216 if (n == 0)
217 {
218 for (p3 = m -> name; *p3; p3++)
219 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
220 continue;
221 }
222 if (n < 1 || n > nargs)
223 continue;
224 for (p3 = args[n - 1]; *p3; p3++)
225 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
226 continue;
227 }
228 else if (*p2 == '{')
229 {
230 int n = 0, n2;
231 p2++;
232 while (*p2 && isdigit(*p2))
233 {
234 n2 = *p2 - '0';
235 if (n2 < 0 || n2 > 9)
236 n2 = 0;
237 n = n * 10 + n2;
238 p2++;
239 }
240 if (*p2 == '}')
241 p2++;
242
243 if (n == 0)
244 {
245 for (p3 = m -> name; *p3; p3++)
246 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
247 continue;
248 }
249 if (n < 1 || n > nargs)
250 continue;
251 for (p3 = args[n - 1]; *p3; p3++)
252 macro_add_to_buff(&linebuff, &bloc, &blen, *p3);
253 continue;
254 }
255 else
256 {
257 macro_add_to_buff(&linebuff, &bloc, &blen, *p2);
258 }
259 }
260
261 macro_add_to_buff(&linebuff, &bloc, &blen, '\n');
262
263 }
264
265 {
266 char ctcbuf[100];
267 char *p;
268 snprintf(ctcbuf, 100, "\001\001SETCONTEXT %d\n", oldcontext);
269 for (p = ctcbuf; *p; p++)
270 macro_add_to_buff(&linebuff, &bloc, &blen, *p);
271 }
272
273 // push the macro into the front of the stream
274 input_openstring(as, opc, linebuff);
275
276 lw_free(linebuff);
277
278 // clean up
279 if (args)
280 {
281 while (nargs)
282 {
283 lw_free(args[--nargs]);
284 }
285 lw_free(args);
286 }
287
288 // indicate a macro was expanded
289 return 0;
290 }