annotate src/macro.c @ 97:2e8dda44027c

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