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