annotate old-trunk/lwasm/old/macro.c @ 348:11a95c6414b4

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