]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/cond.c
* cond.c (get_mri_string): New static function.
[thirdparty/binutils-gdb.git] / gas / cond.c
1 /* cond.c - conditional assembly pseudo-ops, and .include
2 Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "as.h"
21
22 #include "obstack.h"
23
24 /* This is allocated to grow and shrink as .ifdef/.endif pairs are scanned. */
25 struct obstack cond_obstack;
26
27 struct file_line
28 {
29 char *file;
30 unsigned int line;
31 }; /* file_line */
32
33 /* This is what we push and pop. */
34 struct conditional_frame
35 {
36 struct file_line if_file_line; /* the source file & line number of the "if" */
37 struct file_line else_file_line; /* the source file & line of the "else" */
38 struct conditional_frame *previous_cframe;
39 int else_seen; /* have we seen an else yet? */
40 int ignoring; /* if we are currently ignoring input. */
41 int dead_tree; /* if a conditional at a higher level is ignoring input. */
42 }; /* conditional_frame */
43
44 static void initialize_cframe PARAMS ((struct conditional_frame *cframe));
45 static char *get_mri_string PARAMS ((int, int *));
46
47 static struct conditional_frame *current_cframe = NULL;
48
49 void
50 s_ifdef (arg)
51 int arg;
52 {
53 register char *name; /* points to name of symbol */
54 register struct symbol *symbolP; /* Points to symbol */
55 struct conditional_frame cframe;
56
57 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
58 name = input_line_pointer;
59
60 if (!is_name_beginner (*name))
61 {
62 as_bad ("invalid identifier for \".ifdef\"");
63 obstack_1grow (&cond_obstack, 0);
64 }
65 else
66 {
67 get_symbol_end ();
68 ++input_line_pointer;
69 symbolP = symbol_find (name);
70
71 initialize_cframe (&cframe);
72 cframe.ignoring = cframe.dead_tree || !((symbolP != 0) ^ arg);
73 current_cframe = (struct conditional_frame *) obstack_copy (&cond_obstack, &cframe, sizeof (cframe));
74 } /* if a valid identifyer name */
75
76 return;
77 } /* s_ifdef() */
78
79 void
80 s_if (arg)
81 int arg;
82 {
83 expressionS operand;
84 struct conditional_frame cframe;
85 int t;
86
87 SKIP_WHITESPACE (); /* Leading whitespace is part of operand. */
88 expression (&operand);
89
90 if (operand.X_op != O_constant)
91 as_bad ("non-constant expression in \".if\" statement");
92
93 switch ((operatorT) arg)
94 {
95 case O_eq: t = operand.X_add_number == 0; break;
96 case O_ne: t = operand.X_add_number != 0; break;
97 case O_lt: t = operand.X_add_number < 0; break;
98 case O_le: t = operand.X_add_number <= 0; break;
99 case O_ge: t = operand.X_add_number >= 0; break;
100 case O_gt: t = operand.X_add_number > 0; break;
101 default:
102 abort ();
103 }
104
105 /* If the above error is signaled, this will dispatch
106 using an undefined result. No big deal. */
107 initialize_cframe (&cframe);
108 cframe.ignoring = cframe.dead_tree || ! t;
109 current_cframe = ((struct conditional_frame *)
110 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
111 return;
112 } /* s_if() */
113
114 /* Get a string for the MRI IFC or IFNC pseudo-ops. */
115
116 static char *
117 get_mri_string (terminator, len)
118 int terminator;
119 int *len;
120 {
121 char *ret;
122 char *s;
123
124 SKIP_WHITESPACE ();
125 s = ret = input_line_pointer;
126 if (*input_line_pointer == '\'')
127 {
128 ++s;
129 ++input_line_pointer;
130 while (! is_end_of_line[(unsigned char) *input_line_pointer])
131 {
132 *s++ = *input_line_pointer++;
133 if (s[-1] == '\'')
134 {
135 if (*input_line_pointer != '\'')
136 break;
137 ++input_line_pointer;
138 }
139 }
140 SKIP_WHITESPACE ();
141 }
142 else
143 {
144 while (*input_line_pointer != terminator
145 && ! is_end_of_line[(unsigned char) *input_line_pointer])
146 ++input_line_pointer;
147 s = input_line_pointer;
148 while (s > ret && (s[-1] == ' ' || s[-1] == '\t'))
149 --s;
150 }
151
152 *len = s - ret;
153 return ret;
154 }
155
156 /* The MRI IFC and IFNC pseudo-ops. */
157
158 void
159 s_ifc (arg)
160 int arg;
161 {
162 char *s1, *s2;
163 int len1, len2;
164 int res;
165 struct conditional_frame cframe;
166
167 s1 = get_mri_string (',', &len1);
168
169 if (*input_line_pointer != ',')
170 as_bad ("bad format for ifc or ifnc");
171 else
172 ++input_line_pointer;
173
174 s2 = get_mri_string (';', &len2);
175
176 res = len1 == len2 && strncmp (s1, s2, len1) == 0;
177
178 initialize_cframe (&cframe);
179 cframe.ignoring = cframe.dead_tree || ! (res ^ arg);
180 current_cframe = ((struct conditional_frame *)
181 obstack_copy (&cond_obstack, &cframe, sizeof (cframe)));
182 }
183
184 void
185 s_endif (arg)
186 int arg;
187 {
188 struct conditional_frame *hold;
189
190 if (current_cframe == NULL)
191 {
192 as_bad ("\".endif\" without \".if\"");
193 }
194 else
195 {
196 hold = current_cframe;
197 current_cframe = current_cframe->previous_cframe;
198 obstack_free (&cond_obstack, hold);
199 } /* if one pop too many */
200
201 return;
202 } /* s_endif() */
203
204 void
205 s_else (arg)
206 int arg;
207 {
208 if (current_cframe == NULL)
209 {
210 as_bad (".else without matching .if - ignored");
211
212 }
213 else if (current_cframe->else_seen)
214 {
215 as_bad ("duplicate \"else\" - ignored");
216 as_bad_where (current_cframe->else_file_line.file,
217 current_cframe->else_file_line.line,
218 "here is the previous \"else\"");
219 as_bad_where (current_cframe->if_file_line.file,
220 current_cframe->if_file_line.line,
221 "here is the previous \"if\"");
222 }
223 else
224 {
225 as_where (&current_cframe->else_file_line.file,
226 &current_cframe->else_file_line.line);
227
228 if (!current_cframe->dead_tree)
229 {
230 current_cframe->ignoring = !current_cframe->ignoring;
231 } /* if not a dead tree */
232
233 current_cframe->else_seen = 1;
234 } /* if error else do it */
235
236 return;
237 } /* s_else() */
238
239 void
240 s_ifeqs (arg)
241 int arg;
242 {
243 as_bad ("ifeqs not implemented.");
244
245 return;
246 } /* s_ifeqs() */
247
248 int
249 ignore_input ()
250 {
251 char *s;
252
253 s = input_line_pointer;
254
255 if (flag_mri
256 #ifdef NO_PSEUDO_DOT
257 || 1
258 #endif
259 )
260 {
261 if (s[-1] != '.')
262 --s;
263 }
264 else
265 {
266 if (s[-1] != '.')
267 return (current_cframe != NULL) && (current_cframe->ignoring);
268 }
269
270 /* We cannot ignore certain pseudo ops. */
271 if ((s[0] == 'i'
272 && (!strncmp (s, "if", 2)
273 || !strncmp (s, "ifdef", 5)
274 || !strncmp (s, "ifndef", 6)))
275 || (s[0] == 'e'
276 && (!strncmp (s, "else", 4)
277 || !strncmp (s, "endif", 5)
278 || !strncmp (s, "endc", 4))))
279 return 0;
280
281 return (current_cframe != NULL) && (current_cframe->ignoring);
282 } /* ignore_input() */
283
284 static void
285 initialize_cframe (cframe)
286 struct conditional_frame *cframe;
287 {
288 memset (cframe, 0, sizeof (*cframe));
289 as_where (&cframe->if_file_line.file,
290 &cframe->if_file_line.line);
291 cframe->previous_cframe = current_cframe;
292 cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring;
293
294 return;
295 } /* initialize_cframe() */
296
297 /*
298 * Local Variables:
299 * fill-column: 131
300 * comment-column: 0
301 * End:
302 */
303
304 /* end of cond.c */