Mercurial > hg-old > index.cgi
annotate src/macro.c @ 55:8e32696380f3
added expression evaluation and checking function
author | lost |
---|---|
date | Sun, 04 Jan 2009 21:42:54 +0000 |
parents | 34568fab6058 |
children | 035b95a3690f |
rev | line source |
---|---|
0 | 1 /* |
4
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
2 macro.c |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
3 Copyright © 2008 William Astle |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
4 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
5 This file is part of LWASM. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
6 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
7 LWASM is free software: you can redistribute it and/or modify it under the |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
8 terms of the GNU General Public License as published by the Free Software |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
9 Foundation, either version 3 of the License, or (at your option) any later |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
10 version. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
11 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
12 This program is distributed in the hope that it will be useful, but WITHOUT |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
15 more details. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
16 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
17 You should have received a copy of the GNU General Public License along with |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
18 this program. If not, see <http://www.gnu.org/licenses/>. |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
19 |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
20 Contains stuff associated with macro processing |
34568fab6058
Fixed package to include all required files; also added copyright preamble to all source files
lost
parents:
0
diff
changeset
|
21 */ |
0 | 22 |
23 #include <ctype.h> | |
24 #include <stdlib.h> | |
25 #include <string.h> | |
26 #include "lwasm.h" | |
27 | |
28 extern void resolve_insn(asmstate_t *as, sourceline_t *cl); | |
29 | |
30 void pseudo_macro(asmstate_t *as, sourceline_t *cl, char **optr) | |
31 { | |
32 macrotab_t *m; | |
33 | |
34 if (as -> inmacro) | |
35 { | |
36 errorp1(ERR_MACRO); | |
37 return; | |
38 } | |
39 as -> inmacro = 1; | |
40 if (as -> passnum != 1) | |
41 return; | |
42 | |
43 for (m = as -> macros; m; m = m -> next) | |
44 { | |
45 if (!strcmp(m -> name, cl -> symstr)) | |
46 break; | |
47 } | |
48 if (m) | |
49 { | |
50 errorp1(ERR_DUPSYM); | |
51 return; | |
52 } | |
53 m = calloc(sizeof(macrotab_t), 1); | |
54 m -> name = strdup(cl -> symstr); | |
55 m -> next = as -> macros; | |
56 as -> macros = m; | |
57 cl -> hassym = 0; | |
58 while (**optr && !isspace(**optr)) | |
59 (*optr)++; | |
60 cl -> macrodef = 1; | |
61 } | |
62 | |
63 void pseudo_endm(asmstate_t *as, sourceline_t *cl, char **optr) | |
64 { | |
65 if (!as -> inmacro) | |
66 { | |
67 errorp1(ERR_ENDM); | |
68 return; | |
69 } | |
70 | |
71 as -> inmacro = 0; | |
72 cl -> macrodef = 1; | |
73 } | |
74 | |
75 int add_macro_line(asmstate_t *as, sourceline_t *cl, char *optr) | |
76 { | |
77 macroline_t *l; | |
78 | |
79 if (!as -> inmacro) | |
80 return 0; | |
81 | |
82 if (as -> passnum == 2) | |
83 return 1; | |
84 | |
85 l = calloc(sizeof(macroline_t), 1); | |
86 l -> linetext = strdup(optr); | |
87 if (as -> macros -> linetail) | |
88 as -> macros -> linetail -> next = l; | |
89 as -> macros -> linetail = l; | |
90 if (!(as -> macros -> linehead)) | |
91 as -> macros -> linehead = l; | |
92 return 1; | |
93 } | |
94 | |
95 void macro_add_to_buff(char **buff, int *loc, int *len, char c) | |
96 { | |
97 if (*loc == *len) | |
98 { | |
99 *buff = realloc(*buff, *len + 32); | |
100 *len += 32; | |
101 } | |
102 (*buff)[(*loc)++] = c; | |
103 } | |
104 | |
105 // this is just like a regular operation function | |
106 /* | |
107 macro args are references by "\n" where 1 <= n <= 9 | |
108 or by "\{n}"; a \ can be included by writing \\ | |
109 */ | |
110 void expand_macro(asmstate_t *as, sourceline_t *cl, char **optr) | |
111 { | |
112 char **args = NULL; | |
113 int nargs = 0; | |
114 int c; | |
115 sourceline_t *nl; | |
116 int nline = 1; | |
117 macrotab_t *m; | |
118 macroline_t *ml; | |
119 char *buff = NULL; | |
120 int bufflen = 0, buffloc; | |
121 | |
122 m = cl -> macro; | |
123 | |
124 // step the first: parse arguments | |
125 while (**optr && !isspace(**optr)) | |
126 { | |
127 c = 0; | |
128 while ((*optr)[c] && !isspace((*optr)[c]) && (*optr)[c] != ',') | |
129 { | |
130 c++; | |
131 } | |
132 args = realloc(args, sizeof(char *) * (nargs + 1)); | |
133 args[nargs] = malloc(c + 1); | |
134 strncpy(args[nargs], *optr, c); | |
135 args[nargs][c] = '\0'; | |
136 nargs++; | |
137 *optr += c; | |
138 if (**optr == ',') | |
139 (*optr)++; | |
140 } | |
141 | |
142 // step the second: iterate over the lines and expand arguments and add | |
143 // them after "cl" | |
144 for (ml = m -> linehead; ml; ml = ml -> next) | |
145 { | |
146 nl = calloc(sizeof(sourceline_t), 1); | |
147 | |
148 nl -> lineno = nline++; | |
149 nl -> sourcefile = m -> name; | |
150 nl -> opcode = -1; | |
151 nl -> addrmode = -1; | |
152 nl -> addr = as -> addr; | |
153 nl -> dpval = as -> dpval; | |
154 nl -> prev = cl; | |
155 if (!(cl -> next)) | |
156 as -> source_tail = nl; | |
157 nl -> next = cl -> next; | |
158 cl -> next = nl; | |
159 | |
160 buffloc = 0; | |
161 c = 0; | |
162 while (ml -> linetext[c]) | |
163 { | |
164 int ch; | |
165 ch = ml -> linetext[c++]; | |
166 if (ch == '{') | |
167 { | |
168 int v = 0; | |
169 again: | |
170 ch = ml -> linetext[c++]; | |
171 if (!ch) | |
172 { | |
173 c--; | |
174 continue; | |
175 } | |
176 if (ch >= '0' && ch <= '9') | |
177 { | |
178 v = v * 10 + (ch - '0'); | |
179 goto again; | |
180 } | |
181 if (ch == '}') | |
182 { | |
183 v--; | |
184 if (v < nargs) | |
185 { | |
186 char *t; | |
187 for (t = args[v]; *t; t++) | |
188 { | |
189 macro_add_to_buff(&buff, &buffloc, &bufflen, *t); | |
190 } | |
191 } | |
192 continue; | |
193 } | |
194 else | |
195 continue; | |
196 } | |
197 else if (ch == '\\' && ml -> linetext[c]) | |
198 { | |
199 ch = ml -> linetext[c++]; | |
200 if (ch >= '1' && ch <= '9') | |
201 { | |
202 ch -= '1'; | |
203 if (ch < nargs) | |
204 { | |
205 char *t; | |
206 for (t = args[ch]; *t; t++) | |
207 { | |
208 macro_add_to_buff(&buff, &buffloc, &bufflen, *t); | |
209 } | |
210 } | |
211 } | |
212 else | |
213 { | |
214 c--; | |
215 macro_add_to_buff(&buff, &buffloc, &bufflen, '\\'); | |
216 } | |
217 } | |
218 else | |
219 { | |
220 macro_add_to_buff(&buff, &buffloc, &bufflen, ch); | |
221 } | |
222 } | |
223 macro_add_to_buff(&buff, &buffloc, &bufflen, 0); | |
224 nl -> line = strdup(buff); | |
225 | |
226 resolve_insn(as, nl); | |
227 cl = nl; | |
228 } | |
229 if (buff) | |
230 free(buff); | |
231 } |