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