Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* cond.c - conditional assembly pseudo-ops, and .include |
abd63a32 | 2 | Copyright (C) 1990, 91, 92, 93, 95, 96, 97, 98, 99, 2000 |
252b5132 RH |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | #include "as.h" | |
23 | #include "macro.h" | |
24 | ||
25 | #include "obstack.h" | |
26 | ||
76b0a8c0 KH |
27 | /* This is allocated to grow and shrink as .ifdef/.endif pairs are |
28 | scanned. */ | |
252b5132 RH |
29 | struct obstack cond_obstack; |
30 | ||
31 | struct file_line | |
32 | { | |
33 | char *file; | |
34 | unsigned int line; | |
35 | }; | |
36 | ||
37 | /* We push one of these structures for each .if, and pop it at the | |
38 | .endif. */ | |
39 | ||
40 | struct conditional_frame | |
41 | { | |
42 | /* The source file & line number of the "if". */ | |
43 | struct file_line if_file_line; | |
44 | /* The source file & line of the "else". */ | |
45 | struct file_line else_file_line; | |
46 | /* The previous conditional. */ | |
47 | struct conditional_frame *previous_cframe; | |
48 | /* Have we seen an else yet? */ | |
49 | int else_seen; | |
50 | /* Whether we are currently ignoring input. */ | |
51 | int ignoring; | |
52 | /* Whether a conditional at a higher level is ignoring input. */ | |
53 | int dead_tree; | |
54 | /* Macro nesting level at which this conditional was created. */ | |
55 | int macro_nest; | |
56 | }; | |
57 | ||
58 | static void initialize_cframe PARAMS ((struct conditional_frame *cframe)); | |
59 | static char *get_mri_string PARAMS ((int, int *)); | |
60 | ||
61 | static struct conditional_frame *current_cframe = NULL; | |
62 | ||
76b0a8c0 | 63 | void |
252b5132 RH |
64 | s_ifdef (arg) |
65 | int arg; | |
66 | { | |
76b0a8c0 KH |
67 | /* Points to name of symbol. */ |
68 | register char *name; | |
69 | /* Points to symbol. */ | |
70 | register symbolS *symbolP; | |
252b5132 RH |
71 | struct conditional_frame cframe; |
72 | ||
76b0a8c0 KH |
73 | /* Leading whitespace is part of operand. */ |
74 | SKIP_WHITESPACE (); | |
252b5132 RH |
75 | name = input_line_pointer; |
76 | ||
77 | if (!is_name_beginner (*name)) | |
78 | { | |
79 | as_bad (_("invalid identifier for \".ifdef\"")); | |
80 | obstack_1grow (&cond_obstack, 0); | |
81 | ignore_rest_of_line (); | |
82 | } | |
83 | else | |
84 | { | |
85 | char c; | |
86 | ||
87 | c = get_symbol_end (); | |
88 | symbolP = symbol_find (name); | |
89 | *input_line_pointer = c; | |
90 | ||
91 | initialize_cframe (&cframe); | |
92 | cframe.ignoring = cframe.dead_tree || !((symbolP != 0) ^ arg); | |
93 | current_cframe = ((struct conditional_frame *) | |
94 | obstack_copy (&cond_obstack, &cframe, | |
95 | sizeof (cframe))); | |
96 | ||
97 | if (LISTING_SKIP_COND () | |
98 | && cframe.ignoring | |
99 | && (cframe.previous_cframe == NULL | |
100 | || ! cframe.previous_cframe->ignoring)) | |
101 | listing_list (2); | |
102 | ||
103 | demand_empty_rest_of_line (); | |
104 | } /* if a valid identifyer name */ | |
76b0a8c0 | 105 | } |
252b5132 | 106 | |
76b0a8c0 | 107 | void |
252b5132 RH |
108 | s_if (arg) |
109 | int arg; | |
110 | { | |
111 | expressionS operand; | |
112 | struct conditional_frame cframe; | |
113 | int t; | |
114 | char *stop = NULL; | |
115 | char stopc; | |
116 | ||
117 | if (flag_mri) | |
118 | stop = mri_comment_field (&stopc); | |
119 | ||
76b0a8c0 KH |
120 | /* Leading whitespace is part of operand. */ |
121 | SKIP_WHITESPACE (); | |
252b5132 RH |
122 | |
123 | if (current_cframe != NULL && current_cframe->ignoring) | |
124 | { | |
125 | operand.X_add_number = 0; | |
126 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
127 | ++input_line_pointer; | |
128 | } | |
129 | else | |
130 | { | |
131 | expression (&operand); | |
132 | if (operand.X_op != O_constant) | |
133 | as_bad (_("non-constant expression in \".if\" statement")); | |
134 | } | |
135 | ||
136 | switch ((operatorT) arg) | |
137 | { | |
138 | case O_eq: t = operand.X_add_number == 0; break; | |
139 | case O_ne: t = operand.X_add_number != 0; break; | |
140 | case O_lt: t = operand.X_add_number < 0; break; | |
141 | case O_le: t = operand.X_add_number <= 0; break; | |
142 | case O_ge: t = operand.X_add_number >= 0; break; | |
143 | case O_gt: t = operand.X_add_number > 0; break; | |
144 | default: | |
145 | abort (); | |
146 | return; | |
147 | } | |
148 | ||
149 | /* If the above error is signaled, this will dispatch | |
150 | using an undefined result. No big deal. */ | |
151 | initialize_cframe (&cframe); | |
152 | cframe.ignoring = cframe.dead_tree || ! t; | |
153 | current_cframe = ((struct conditional_frame *) | |
154 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
155 | ||
156 | if (LISTING_SKIP_COND () | |
157 | && cframe.ignoring | |
158 | && (cframe.previous_cframe == NULL | |
159 | || ! cframe.previous_cframe->ignoring)) | |
160 | listing_list (2); | |
161 | ||
162 | if (flag_mri) | |
163 | mri_comment_end (stop, stopc); | |
164 | ||
165 | demand_empty_rest_of_line (); | |
76b0a8c0 | 166 | } |
252b5132 RH |
167 | |
168 | /* Get a string for the MRI IFC or IFNC pseudo-ops. */ | |
169 | ||
170 | static char * | |
171 | get_mri_string (terminator, len) | |
172 | int terminator; | |
173 | int *len; | |
174 | { | |
175 | char *ret; | |
176 | char *s; | |
177 | ||
178 | SKIP_WHITESPACE (); | |
179 | s = ret = input_line_pointer; | |
180 | if (*input_line_pointer == '\'') | |
181 | { | |
182 | ++s; | |
183 | ++input_line_pointer; | |
184 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
185 | { | |
186 | *s++ = *input_line_pointer++; | |
187 | if (s[-1] == '\'') | |
188 | { | |
189 | if (*input_line_pointer != '\'') | |
190 | break; | |
191 | ++input_line_pointer; | |
192 | } | |
193 | } | |
194 | SKIP_WHITESPACE (); | |
195 | } | |
196 | else | |
197 | { | |
198 | while (*input_line_pointer != terminator | |
199 | && ! is_end_of_line[(unsigned char) *input_line_pointer]) | |
200 | ++input_line_pointer; | |
201 | s = input_line_pointer; | |
202 | while (s > ret && (s[-1] == ' ' || s[-1] == '\t')) | |
203 | --s; | |
204 | } | |
205 | ||
206 | *len = s - ret; | |
207 | return ret; | |
208 | } | |
209 | ||
210 | /* The MRI IFC and IFNC pseudo-ops. */ | |
211 | ||
212 | void | |
213 | s_ifc (arg) | |
214 | int arg; | |
215 | { | |
216 | char *stop = NULL; | |
217 | char stopc; | |
218 | char *s1, *s2; | |
219 | int len1, len2; | |
220 | int res; | |
221 | struct conditional_frame cframe; | |
222 | ||
223 | if (flag_mri) | |
224 | stop = mri_comment_field (&stopc); | |
225 | ||
226 | s1 = get_mri_string (',', &len1); | |
227 | ||
228 | if (*input_line_pointer != ',') | |
229 | as_bad (_("bad format for ifc or ifnc")); | |
230 | else | |
231 | ++input_line_pointer; | |
232 | ||
233 | s2 = get_mri_string (';', &len2); | |
234 | ||
235 | res = len1 == len2 && strncmp (s1, s2, len1) == 0; | |
236 | ||
237 | initialize_cframe (&cframe); | |
238 | cframe.ignoring = cframe.dead_tree || ! (res ^ arg); | |
239 | current_cframe = ((struct conditional_frame *) | |
240 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
241 | ||
242 | if (LISTING_SKIP_COND () | |
243 | && cframe.ignoring | |
244 | && (cframe.previous_cframe == NULL | |
245 | || ! cframe.previous_cframe->ignoring)) | |
246 | listing_list (2); | |
247 | ||
248 | if (flag_mri) | |
249 | mri_comment_end (stop, stopc); | |
250 | ||
251 | demand_empty_rest_of_line (); | |
252 | } | |
253 | ||
76b0a8c0 | 254 | void |
3fd9f047 TW |
255 | s_elseif (arg) |
256 | int arg; | |
257 | { | |
258 | expressionS operand; | |
259 | int t; | |
260 | ||
261 | if (current_cframe == NULL) | |
262 | { | |
263 | as_bad (_("\".elseif\" without matching \".if\" - ignored")); | |
264 | ||
265 | } | |
266 | else if (current_cframe->else_seen) | |
267 | { | |
268 | as_bad (_("\".elseif\" after \".else\" - ignored")); | |
269 | as_bad_where (current_cframe->else_file_line.file, | |
270 | current_cframe->else_file_line.line, | |
271 | _("here is the previous \"else\"")); | |
272 | as_bad_where (current_cframe->if_file_line.file, | |
273 | current_cframe->if_file_line.line, | |
274 | _("here is the previous \"if\"")); | |
275 | } | |
276 | else | |
277 | { | |
278 | as_where (¤t_cframe->else_file_line.file, | |
279 | ¤t_cframe->else_file_line.line); | |
280 | ||
281 | if (!current_cframe->dead_tree) | |
282 | { | |
283 | current_cframe->ignoring = !current_cframe->ignoring; | |
284 | if (LISTING_SKIP_COND ()) | |
285 | { | |
286 | if (! current_cframe->ignoring) | |
287 | listing_list (1); | |
288 | else | |
289 | listing_list (2); | |
290 | } | |
291 | } /* if not a dead tree */ | |
292 | } /* if error else do it */ | |
293 | ||
76b0a8c0 KH |
294 | /* Leading whitespace is part of operand. */ |
295 | SKIP_WHITESPACE (); | |
3fd9f047 TW |
296 | |
297 | if (current_cframe != NULL && current_cframe->ignoring) | |
298 | { | |
299 | operand.X_add_number = 0; | |
300 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
301 | ++input_line_pointer; | |
302 | } | |
303 | else | |
304 | { | |
305 | expression (&operand); | |
306 | if (operand.X_op != O_constant) | |
307 | as_bad (_("non-constant expression in \".elseif\" statement")); | |
308 | } | |
76b0a8c0 | 309 | |
3fd9f047 TW |
310 | switch ((operatorT) arg) |
311 | { | |
312 | case O_eq: t = operand.X_add_number == 0; break; | |
313 | case O_ne: t = operand.X_add_number != 0; break; | |
314 | case O_lt: t = operand.X_add_number < 0; break; | |
315 | case O_le: t = operand.X_add_number <= 0; break; | |
316 | case O_ge: t = operand.X_add_number >= 0; break; | |
317 | case O_gt: t = operand.X_add_number > 0; break; | |
318 | default: | |
319 | abort (); | |
320 | return; | |
321 | } | |
322 | ||
323 | current_cframe->ignoring = current_cframe->dead_tree || ! t; | |
324 | ||
325 | if (LISTING_SKIP_COND () | |
326 | && current_cframe->ignoring | |
327 | && (current_cframe->previous_cframe == NULL | |
328 | || ! current_cframe->previous_cframe->ignoring)) | |
329 | listing_list (2); | |
330 | ||
331 | demand_empty_rest_of_line (); | |
332 | } | |
333 | ||
76b0a8c0 | 334 | void |
252b5132 | 335 | s_endif (arg) |
ab9da554 | 336 | int arg ATTRIBUTE_UNUSED; |
252b5132 RH |
337 | { |
338 | struct conditional_frame *hold; | |
339 | ||
340 | if (current_cframe == NULL) | |
341 | { | |
342 | as_bad (_("\".endif\" without \".if\"")); | |
343 | } | |
344 | else | |
345 | { | |
346 | if (LISTING_SKIP_COND () | |
347 | && current_cframe->ignoring | |
348 | && (current_cframe->previous_cframe == NULL | |
349 | || ! current_cframe->previous_cframe->ignoring)) | |
350 | listing_list (1); | |
351 | ||
352 | hold = current_cframe; | |
353 | current_cframe = current_cframe->previous_cframe; | |
354 | obstack_free (&cond_obstack, hold); | |
355 | } /* if one pop too many */ | |
356 | ||
357 | if (flag_mri) | |
358 | { | |
359 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
360 | ++input_line_pointer; | |
361 | } | |
362 | ||
363 | demand_empty_rest_of_line (); | |
76b0a8c0 | 364 | } |
252b5132 | 365 | |
76b0a8c0 | 366 | void |
252b5132 | 367 | s_else (arg) |
ab9da554 | 368 | int arg ATTRIBUTE_UNUSED; |
252b5132 RH |
369 | { |
370 | if (current_cframe == NULL) | |
371 | { | |
372 | as_bad (_(".else without matching .if - ignored")); | |
373 | ||
374 | } | |
375 | else if (current_cframe->else_seen) | |
376 | { | |
377 | as_bad (_("duplicate \"else\" - ignored")); | |
378 | as_bad_where (current_cframe->else_file_line.file, | |
379 | current_cframe->else_file_line.line, | |
380 | _("here is the previous \"else\"")); | |
381 | as_bad_where (current_cframe->if_file_line.file, | |
382 | current_cframe->if_file_line.line, | |
383 | _("here is the previous \"if\"")); | |
384 | } | |
385 | else | |
386 | { | |
387 | as_where (¤t_cframe->else_file_line.file, | |
388 | ¤t_cframe->else_file_line.line); | |
389 | ||
390 | if (!current_cframe->dead_tree) | |
391 | { | |
392 | current_cframe->ignoring = !current_cframe->ignoring; | |
393 | if (LISTING_SKIP_COND ()) | |
394 | { | |
395 | if (! current_cframe->ignoring) | |
396 | listing_list (1); | |
397 | else | |
398 | listing_list (2); | |
399 | } | |
400 | } /* if not a dead tree */ | |
401 | ||
402 | current_cframe->else_seen = 1; | |
403 | } /* if error else do it */ | |
404 | ||
405 | if (flag_mri) | |
406 | { | |
407 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
408 | ++input_line_pointer; | |
409 | } | |
410 | ||
411 | demand_empty_rest_of_line (); | |
76b0a8c0 | 412 | } |
252b5132 | 413 | |
76b0a8c0 | 414 | void |
252b5132 RH |
415 | s_ifeqs (arg) |
416 | int arg; | |
417 | { | |
418 | char *s1, *s2; | |
419 | int len1, len2; | |
420 | int res; | |
421 | struct conditional_frame cframe; | |
422 | ||
423 | s1 = demand_copy_C_string (&len1); | |
424 | ||
425 | SKIP_WHITESPACE (); | |
426 | if (*input_line_pointer != ',') | |
427 | { | |
428 | as_bad (_(".ifeqs syntax error")); | |
429 | ignore_rest_of_line (); | |
430 | return; | |
431 | } | |
432 | ||
433 | ++input_line_pointer; | |
434 | ||
435 | s2 = demand_copy_C_string (&len2); | |
436 | ||
437 | res = len1 == len2 && strncmp (s1, s2, len1) == 0; | |
438 | ||
439 | initialize_cframe (&cframe); | |
440 | cframe.ignoring = cframe.dead_tree || ! (res ^ arg); | |
441 | current_cframe = ((struct conditional_frame *) | |
442 | obstack_copy (&cond_obstack, &cframe, sizeof (cframe))); | |
443 | ||
444 | if (LISTING_SKIP_COND () | |
445 | && cframe.ignoring | |
446 | && (cframe.previous_cframe == NULL | |
447 | || ! cframe.previous_cframe->ignoring)) | |
448 | listing_list (2); | |
449 | ||
450 | demand_empty_rest_of_line (); | |
76b0a8c0 | 451 | } |
252b5132 | 452 | |
76b0a8c0 | 453 | int |
252b5132 RH |
454 | ignore_input () |
455 | { | |
456 | char *s; | |
457 | ||
458 | s = input_line_pointer; | |
459 | ||
abd63a32 | 460 | if (NO_PSEUDO_DOT || flag_m68k_mri) |
252b5132 RH |
461 | { |
462 | if (s[-1] != '.') | |
463 | --s; | |
464 | } | |
465 | else | |
466 | { | |
467 | if (s[-1] != '.') | |
468 | return (current_cframe != NULL) && (current_cframe->ignoring); | |
469 | } | |
470 | ||
471 | /* We cannot ignore certain pseudo ops. */ | |
472 | if (((s[0] == 'i' | |
473 | || s[0] == 'I') | |
474 | && (!strncasecmp (s, "if", 2) | |
475 | || !strncasecmp (s, "ifdef", 5) | |
476 | || !strncasecmp (s, "ifndef", 6))) | |
477 | || ((s[0] == 'e' | |
478 | || s[0] == 'E') | |
479 | && (!strncasecmp (s, "else", 4) | |
480 | || !strncasecmp (s, "endif", 5) | |
481 | || !strncasecmp (s, "endc", 4)))) | |
482 | return 0; | |
483 | ||
484 | return (current_cframe != NULL) && (current_cframe->ignoring); | |
76b0a8c0 | 485 | } |
252b5132 | 486 | |
76b0a8c0 | 487 | static void |
252b5132 RH |
488 | initialize_cframe (cframe) |
489 | struct conditional_frame *cframe; | |
490 | { | |
491 | memset (cframe, 0, sizeof (*cframe)); | |
492 | as_where (&cframe->if_file_line.file, | |
493 | &cframe->if_file_line.line); | |
494 | cframe->previous_cframe = current_cframe; | |
495 | cframe->dead_tree = current_cframe != NULL && current_cframe->ignoring; | |
496 | cframe->macro_nest = macro_nest; | |
497 | } | |
498 | ||
499 | /* Give an error if a conditional is unterminated inside a macro or | |
500 | the assembly as a whole. If NEST is non negative, we are being | |
501 | called because of the end of a macro expansion. If NEST is | |
502 | negative, we are being called at the of the input files. */ | |
503 | ||
504 | void | |
505 | cond_finish_check (nest) | |
506 | int nest; | |
507 | { | |
508 | if (current_cframe != NULL && current_cframe->macro_nest >= nest) | |
509 | { | |
510 | if (nest >= 0) | |
511 | as_bad (_("end of macro inside conditional")); | |
512 | else | |
513 | as_bad (_("end of file inside conditional")); | |
514 | as_bad_where (current_cframe->if_file_line.file, | |
515 | current_cframe->if_file_line.line, | |
516 | _("here is the start of the unterminated conditional")); | |
517 | if (current_cframe->else_seen) | |
518 | as_bad_where (current_cframe->else_file_line.file, | |
519 | current_cframe->else_file_line.line, | |
520 | _("here is the \"else\" of the unterminated conditional")); | |
521 | } | |
522 | } | |
523 | ||
524 | /* This function is called when we exit out of a macro. We assume | |
525 | that any conditionals which began within the macro are correctly | |
526 | nested, and just pop them off the stack. */ | |
527 | ||
528 | void | |
529 | cond_exit_macro (nest) | |
530 | int nest; | |
531 | { | |
532 | while (current_cframe != NULL && current_cframe->macro_nest >= nest) | |
533 | { | |
534 | struct conditional_frame *hold; | |
535 | ||
536 | hold = current_cframe; | |
537 | current_cframe = current_cframe->previous_cframe; | |
538 | obstack_free (&cond_obstack, hold); | |
539 | } | |
540 | } |