Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5222 serge 1
/* expr.c -operands, expressions-
2
   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3
   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011,
4
   2012 Free Software Foundation, Inc.
5
 
6
   This file is part of GAS, the GNU Assembler.
7
 
8
   GAS is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3, or (at your option)
11
   any later version.
12
 
13
   GAS is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with GAS; see the file COPYING.  If not, write to the Free
20
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21
   02110-1301, USA.  */
22
 
23
/* This is really a branch office of as-read.c. I split it out to clearly
24
   distinguish the world of expressions from the world of statements.
25
   (It also gives smaller files to re-compile.)
26
   Here, "operand"s are of expressions, not instructions.  */
27
 
28
#define min(a, b)       ((a) < (b) ? (a) : (b))
29
 
30
#include "as.h"
31
#include "safe-ctype.h"
32
#include "obstack.h"
33
 
34
#ifdef HAVE_LIMITS_H
35
#include 
36
#endif
37
#ifndef CHAR_BIT
38
#define CHAR_BIT 8
39
#endif
40
 
41
static void floating_constant (expressionS * expressionP);
42
static valueT generic_bignum_to_int32 (void);
43
#ifdef BFD64
44
static valueT generic_bignum_to_int64 (void);
45
#endif
46
static void integer_constant (int radix, expressionS * expressionP);
47
static void mri_char_constant (expressionS *);
48
static void clean_up_expression (expressionS * expressionP);
49
static segT operand (expressionS *, enum expr_mode);
50
static operatorT operatorf (int *);
51
 
52
extern const char EXP_CHARS[], FLT_CHARS[];
53
 
54
/* We keep a mapping of expression symbols to file positions, so that
55
   we can provide better error messages.  */
56
 
57
struct expr_symbol_line {
58
  struct expr_symbol_line *next;
59
  symbolS *sym;
60
  char *file;
61
  unsigned int line;
62
};
63
 
64
static struct expr_symbol_line *expr_symbol_lines;
65
 
66
/* Build a dummy symbol to hold a complex expression.  This is how we
67
   build expressions up out of other expressions.  The symbol is put
68
   into the fake section expr_section.  */
69
 
70
symbolS *
71
make_expr_symbol (expressionS *expressionP)
72
{
73
  expressionS zero;
74
  symbolS *symbolP;
75
  struct expr_symbol_line *n;
76
 
77
  if (expressionP->X_op == O_symbol
78
      && expressionP->X_add_number == 0)
79
    return expressionP->X_add_symbol;
80
 
81
  if (expressionP->X_op == O_big)
82
    {
83
      /* This won't work, because the actual value is stored in
84
	 generic_floating_point_number or generic_bignum, and we are
85
	 going to lose it if we haven't already.  */
86
      if (expressionP->X_add_number > 0)
87
	as_bad (_("bignum invalid"));
88
      else
89
	as_bad (_("floating point number invalid"));
90
      zero.X_op = O_constant;
91
      zero.X_add_number = 0;
92
      zero.X_unsigned = 0;
93
      zero.X_extrabit = 0;
94
      clean_up_expression (&zero);
95
      expressionP = &zero;
96
    }
97
 
98
  /* Putting constant symbols in absolute_section rather than
99
     expr_section is convenient for the old a.out code, for which
100
     S_GET_SEGMENT does not always retrieve the value put in by
101
     S_SET_SEGMENT.  */
102
  symbolP = symbol_create (FAKE_LABEL_NAME,
103
			   (expressionP->X_op == O_constant
104
			    ? absolute_section
105
			    : expressionP->X_op == O_register
106
			      ? reg_section
107
			      : expr_section),
108
			   0, &zero_address_frag);
109
  symbol_set_value_expression (symbolP, expressionP);
110
 
111
  if (expressionP->X_op == O_constant)
112
    resolve_symbol_value (symbolP);
113
 
114
  n = (struct expr_symbol_line *) xmalloc (sizeof *n);
115
  n->sym = symbolP;
116
  as_where (&n->file, &n->line);
117
  n->next = expr_symbol_lines;
118
  expr_symbol_lines = n;
119
 
120
  return symbolP;
121
}
122
 
123
/* Return the file and line number for an expr symbol.  Return
124
   non-zero if something was found, 0 if no information is known for
125
   the symbol.  */
126
 
127
int
128
expr_symbol_where (symbolS *sym, char **pfile, unsigned int *pline)
129
{
130
  register struct expr_symbol_line *l;
131
 
132
  for (l = expr_symbol_lines; l != NULL; l = l->next)
133
    {
134
      if (l->sym == sym)
135
	{
136
	  *pfile = l->file;
137
	  *pline = l->line;
138
	  return 1;
139
	}
140
    }
141
 
142
  return 0;
143
}
144
 
145
/* Utilities for building expressions.
146
   Since complex expressions are recorded as symbols for use in other
147
   expressions these return a symbolS * and not an expressionS *.
148
   These explicitly do not take an "add_number" argument.  */
149
/* ??? For completeness' sake one might want expr_build_symbol.
150
   It would just return its argument.  */
151
 
152
/* Build an expression for an unsigned constant.
153
   The corresponding one for signed constants is missing because
154
   there's currently no need for it.  One could add an unsigned_p flag
155
   but that seems more clumsy.  */
156
 
157
symbolS *
158
expr_build_uconstant (offsetT value)
159
{
160
  expressionS e;
161
 
162
  e.X_op = O_constant;
163
  e.X_add_number = value;
164
  e.X_unsigned = 1;
165
  e.X_extrabit = 0;
166
  return make_expr_symbol (&e);
167
}
168
 
169
/* Build an expression for the current location ('.').  */
170
 
171
symbolS *
172
expr_build_dot (void)
173
{
174
  expressionS e;
175
 
176
  current_location (&e);
177
  return symbol_clone_if_forward_ref (make_expr_symbol (&e));
178
}
179
 
180
/* Build any floating-point literal here.
181
   Also build any bignum literal here.  */
182
 
183
/* Seems atof_machine can backscan through generic_bignum and hit whatever
184
   happens to be loaded before it in memory.  And its way too complicated
185
   for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,
186
   and never write into the early words, thus they'll always be zero.
187
   I hate Dean's floating-point code.  Bleh.  */
188
LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6];
189
 
190
FLONUM_TYPE generic_floating_point_number = {
191
  &generic_bignum[6],		/* low.  (JF: Was 0)  */
192
  &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high.  JF: (added +6)  */
193
  0,				/* leader.  */
194
  0,				/* exponent.  */
195
 
196
};
197
 
198
 
199
static void
200
floating_constant (expressionS *expressionP)
201
{
202
  /* input_line_pointer -> floating-point constant.  */
203
  int error_code;
204
 
205
  error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,
206
			     &generic_floating_point_number);
207
 
208
  if (error_code)
209
    {
210
      if (error_code == ERROR_EXPONENT_OVERFLOW)
211
	{
212
	  as_bad (_("bad floating-point constant: exponent overflow"));
213
	}
214
      else
215
	{
216
	  as_bad (_("bad floating-point constant: unknown error code=%d"),
217
		  error_code);
218
	}
219
    }
220
  expressionP->X_op = O_big;
221
  /* input_line_pointer -> just after constant, which may point to
222
     whitespace.  */
223
  expressionP->X_add_number = -1;
224
}
225
 
226
static valueT
227
generic_bignum_to_int32 (void)
228
{
229
  valueT number =
230
	   ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS)
231
	   | (generic_bignum[0] & LITTLENUM_MASK);
232
  number &= 0xffffffff;
233
  return number;
234
}
235
 
236
#ifdef BFD64
237
static valueT
238
generic_bignum_to_int64 (void)
239
{
240
  valueT number =
241
    ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK)
242
	  << LITTLENUM_NUMBER_OF_BITS)
243
	 | ((valueT) generic_bignum[2] & LITTLENUM_MASK))
244
	<< LITTLENUM_NUMBER_OF_BITS)
245
       | ((valueT) generic_bignum[1] & LITTLENUM_MASK))
246
      << LITTLENUM_NUMBER_OF_BITS)
247
     | ((valueT) generic_bignum[0] & LITTLENUM_MASK));
248
  return number;
249
}
250
#endif
251
 
252
static void
253
integer_constant (int radix, expressionS *expressionP)
254
{
255
  char *start;		/* Start of number.  */
256
  char *suffix = NULL;
257
  char c;
258
  valueT number;	/* Offset or (absolute) value.  */
259
  short int digit;	/* Value of next digit in current radix.  */
260
  short int maxdig = 0;	/* Highest permitted digit value.  */
261
  int too_many_digits = 0;	/* If we see >= this number of.  */
262
  char *name;		/* Points to name of symbol.  */
263
  symbolS *symbolP;	/* Points to symbol.  */
264
 
265
  int small;			/* True if fits in 32 bits.  */
266
 
267
  /* May be bignum, or may fit in 32 bits.  */
268
  /* Most numbers fit into 32 bits, and we want this case to be fast.
269
     so we pretend it will fit into 32 bits.  If, after making up a 32
270
     bit number, we realise that we have scanned more digits than
271
     comfortably fit into 32 bits, we re-scan the digits coding them
272
     into a bignum.  For decimal and octal numbers we are
273
     conservative: Some numbers may be assumed bignums when in fact
274
     they do fit into 32 bits.  Numbers of any radix can have excess
275
     leading zeros: We strive to recognise this and cast them back
276
     into 32 bits.  We must check that the bignum really is more than
277
     32 bits, and change it back to a 32-bit number if it fits.  The
278
     number we are looking for is expected to be positive, but if it
279
     fits into 32 bits as an unsigned number, we let it be a 32-bit
280
     number.  The cavalier approach is for speed in ordinary cases.  */
281
  /* This has been extended for 64 bits.  We blindly assume that if
282
     you're compiling in 64-bit mode, the target is a 64-bit machine.
283
     This should be cleaned up.  */
284
 
285
#ifdef BFD64
286
#define valuesize 64
287
#else /* includes non-bfd case, mostly */
288
#define valuesize 32
289
#endif
290
 
291
  if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)
292
    {
293
      int flt = 0;
294
 
295
      /* In MRI mode, the number may have a suffix indicating the
296
	 radix.  For that matter, it might actually be a floating
297
	 point constant.  */
298
      for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)
299
	{
300
	  if (*suffix == 'e' || *suffix == 'E')
301
	    flt = 1;
302
	}
303
 
304
      if (suffix == input_line_pointer)
305
	{
306
	  radix = 10;
307
	  suffix = NULL;
308
	}
309
      else
310
	{
311
	  c = *--suffix;
312
	  c = TOUPPER (c);
313
	  /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,
314
	     we distinguish between 'B' and 'b'.  This is the case for
315
	     Z80.  */
316
	  if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')
317
	    radix = 2;
318
	  else if (c == 'D')
319
	    radix = 10;
320
	  else if (c == 'O' || c == 'Q')
321
	    radix = 8;
322
	  else if (c == 'H')
323
	    radix = 16;
324
	  else if (suffix[1] == '.' || c == 'E' || flt)
325
	    {
326
	      floating_constant (expressionP);
327
	      return;
328
	    }
329
	  else
330
	    {
331
	      radix = 10;
332
	      suffix = NULL;
333
	    }
334
	}
335
    }
336
 
337
  switch (radix)
338
    {
339
    case 2:
340
      maxdig = 2;
341
      too_many_digits = valuesize + 1;
342
      break;
343
    case 8:
344
      maxdig = radix = 8;
345
      too_many_digits = (valuesize + 2) / 3 + 1;
346
      break;
347
    case 16:
348
      maxdig = radix = 16;
349
      too_many_digits = (valuesize + 3) / 4 + 1;
350
      break;
351
    case 10:
352
      maxdig = radix = 10;
353
      too_many_digits = (valuesize + 11) / 4; /* Very rough.  */
354
    }
355
#undef valuesize
356
  start = input_line_pointer;
357
  c = *input_line_pointer++;
358
  for (number = 0;
359
       (digit = hex_value (c)) < maxdig;
360
       c = *input_line_pointer++)
361
    {
362
      number = number * radix + digit;
363
    }
364
  /* c contains character after number.  */
365
  /* input_line_pointer->char after c.  */
366
  small = (input_line_pointer - start - 1) < too_many_digits;
367
 
368
  if (radix == 16 && c == '_')
369
    {
370
      /* This is literal of the form 0x333_0_12345678_1.
371
	 This example is equivalent to 0x00000333000000001234567800000001.  */
372
 
373
      int num_little_digits = 0;
374
      int i;
375
      input_line_pointer = start;	/* -> 1st digit.  */
376
 
377
      know (LITTLENUM_NUMBER_OF_BITS == 16);
378
 
379
      for (c = '_'; c == '_'; num_little_digits += 2)
380
	{
381
 
382
	  /* Convert one 64-bit word.  */
383
	  int ndigit = 0;
384
	  number = 0;
385
	  for (c = *input_line_pointer++;
386
	       (digit = hex_value (c)) < maxdig;
387
	       c = *(input_line_pointer++))
388
	    {
389
	      number = number * radix + digit;
390
	      ndigit++;
391
	    }
392
 
393
	  /* Check for 8 digit per word max.  */
394
	  if (ndigit > 8)
395
	    as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word"));
396
 
397
	  /* Add this chunk to the bignum.
398
	     Shift things down 2 little digits.  */
399
	  know (LITTLENUM_NUMBER_OF_BITS == 16);
400
	  for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);
401
	       i >= 2;
402
	       i--)
403
	    generic_bignum[i] = generic_bignum[i - 2];
404
 
405
	  /* Add the new digits as the least significant new ones.  */
406
	  generic_bignum[0] = number & 0xffffffff;
407
	  generic_bignum[1] = number >> 16;
408
	}
409
 
410
      /* Again, c is char after number, input_line_pointer->after c.  */
411
 
412
      if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)
413
	num_little_digits = SIZE_OF_LARGE_NUMBER - 1;
414
 
415
      gas_assert (num_little_digits >= 4);
416
 
417
      if (num_little_digits != 8)
418
	as_bad (_("a bignum with underscores must have exactly 4 words"));
419
 
420
      /* We might have some leading zeros.  These can be trimmed to give
421
	 us a change to fit this constant into a small number.  */
422
      while (generic_bignum[num_little_digits - 1] == 0
423
	     && num_little_digits > 1)
424
	num_little_digits--;
425
 
426
      if (num_little_digits <= 2)
427
	{
428
	  /* will fit into 32 bits.  */
429
	  number = generic_bignum_to_int32 ();
430
	  small = 1;
431
	}
432
#ifdef BFD64
433
      else if (num_little_digits <= 4)
434
	{
435
	  /* Will fit into 64 bits.  */
436
	  number = generic_bignum_to_int64 ();
437
	  small = 1;
438
	}
439
#endif
440
      else
441
	{
442
	  small = 0;
443
 
444
	  /* Number of littlenums in the bignum.  */
445
	  number = num_little_digits;
446
	}
447
    }
448
  else if (!small)
449
    {
450
      /* We saw a lot of digits. manufacture a bignum the hard way.  */
451
      LITTLENUM_TYPE *leader;	/* -> high order littlenum of the bignum.  */
452
      LITTLENUM_TYPE *pointer;	/* -> littlenum we are frobbing now.  */
453
      long carry;
454
 
455
      leader = generic_bignum;
456
      generic_bignum[0] = 0;
457
      generic_bignum[1] = 0;
458
      generic_bignum[2] = 0;
459
      generic_bignum[3] = 0;
460
      input_line_pointer = start;	/* -> 1st digit.  */
461
      c = *input_line_pointer++;
462
      for (; (carry = hex_value (c)) < maxdig; c = *input_line_pointer++)
463
	{
464
	  for (pointer = generic_bignum; pointer <= leader; pointer++)
465
	    {
466
	      long work;
467
 
468
	      work = carry + radix * *pointer;
469
	      *pointer = work & LITTLENUM_MASK;
470
	      carry = work >> LITTLENUM_NUMBER_OF_BITS;
471
	    }
472
	  if (carry)
473
	    {
474
	      if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)
475
		{
476
		  /* Room to grow a longer bignum.  */
477
		  *++leader = carry;
478
		}
479
	    }
480
	}
481
      /* Again, c is char after number.  */
482
      /* input_line_pointer -> after c.  */
483
      know (LITTLENUM_NUMBER_OF_BITS == 16);
484
      if (leader < generic_bignum + 2)
485
	{
486
	  /* Will fit into 32 bits.  */
487
	  number = generic_bignum_to_int32 ();
488
	  small = 1;
489
	}
490
#ifdef BFD64
491
      else if (leader < generic_bignum + 4)
492
	{
493
	  /* Will fit into 64 bits.  */
494
	  number = generic_bignum_to_int64 ();
495
	  small = 1;
496
	}
497
#endif
498
      else
499
	{
500
	  /* Number of littlenums in the bignum.  */
501
	  number = leader - generic_bignum + 1;
502
	}
503
    }
504
 
505
  if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
506
      && suffix != NULL
507
      && input_line_pointer - 1 == suffix)
508
    c = *input_line_pointer++;
509
 
510
  if (small)
511
    {
512
      /* Here with number, in correct radix. c is the next char.
513
	 Note that unlike un*x, we allow "011f" "0x9f" to both mean
514
	 the same as the (conventional) "9f".
515
	 This is simply easier than checking for strict canonical
516
	 form.  Syntax sux!  */
517
 
518
      if (LOCAL_LABELS_FB && c == 'b')
519
	{
520
	  /* Backward ref to local label.
521
	     Because it is backward, expect it to be defined.  */
522
	  /* Construct a local label.  */
523
	  name = fb_label_name ((int) number, 0);
524
 
525
	  /* Seen before, or symbol is defined: OK.  */
526
	  symbolP = symbol_find (name);
527
	  if ((symbolP != NULL) && (S_IS_DEFINED (symbolP)))
528
	    {
529
	      /* Local labels are never absolute.  Don't waste time
530
		 checking absoluteness.  */
531
	      know (SEG_NORMAL (S_GET_SEGMENT (symbolP)));
532
 
533
	      expressionP->X_op = O_symbol;
534
	      expressionP->X_add_symbol = symbolP;
535
	    }
536
	  else
537
	    {
538
	      /* Either not seen or not defined.  */
539
	      /* @@ Should print out the original string instead of
540
		 the parsed number.  */
541
	      as_bad (_("backward ref to unknown label \"%d:\""),
542
		      (int) number);
543
	      expressionP->X_op = O_constant;
544
	    }
545
 
546
	  expressionP->X_add_number = 0;
547
	}			/* case 'b' */
548
      else if (LOCAL_LABELS_FB && c == 'f')
549
	{
550
	  /* Forward reference.  Expect symbol to be undefined or
551
	     unknown.  undefined: seen it before.  unknown: never seen
552
	     it before.
553
 
554
	     Construct a local label name, then an undefined symbol.
555
	     Don't create a xseg frag for it: caller may do that.
556
	     Just return it as never seen before.  */
557
	  name = fb_label_name ((int) number, 1);
558
	  symbolP = symbol_find_or_make (name);
559
	  /* We have no need to check symbol properties.  */
560
#ifndef many_segments
561
	  /* Since "know" puts its arg into a "string", we
562
	     can't have newlines in the argument.  */
563
	  know (S_GET_SEGMENT (symbolP) == undefined_section || S_GET_SEGMENT (symbolP) == text_section || S_GET_SEGMENT (symbolP) == data_section);
564
#endif
565
	  expressionP->X_op = O_symbol;
566
	  expressionP->X_add_symbol = symbolP;
567
	  expressionP->X_add_number = 0;
568
	}			/* case 'f' */
569
      else if (LOCAL_LABELS_DOLLAR && c == '$')
570
	{
571
	  /* If the dollar label is *currently* defined, then this is just
572
	     another reference to it.  If it is not *currently* defined,
573
	     then this is a fresh instantiation of that number, so create
574
	     it.  */
575
 
576
	  if (dollar_label_defined ((long) number))
577
	    {
578
	      name = dollar_label_name ((long) number, 0);
579
	      symbolP = symbol_find (name);
580
	      know (symbolP != NULL);
581
	    }
582
	  else
583
	    {
584
	      name = dollar_label_name ((long) number, 1);
585
	      symbolP = symbol_find_or_make (name);
586
	    }
587
 
588
	  expressionP->X_op = O_symbol;
589
	  expressionP->X_add_symbol = symbolP;
590
	  expressionP->X_add_number = 0;
591
	}			/* case '$' */
592
      else
593
	{
594
	  expressionP->X_op = O_constant;
595
	  expressionP->X_add_number = number;
596
	  input_line_pointer--;	/* Restore following character.  */
597
	}			/* Really just a number.  */
598
    }
599
  else
600
    {
601
      /* Not a small number.  */
602
      expressionP->X_op = O_big;
603
      expressionP->X_add_number = number;	/* Number of littlenums.  */
604
      input_line_pointer--;	/* -> char following number.  */
605
    }
606
}
607
 
608
/* Parse an MRI multi character constant.  */
609
 
610
static void
611
mri_char_constant (expressionS *expressionP)
612
{
613
  int i;
614
 
615
  if (*input_line_pointer == '\''
616
      && input_line_pointer[1] != '\'')
617
    {
618
      expressionP->X_op = O_constant;
619
      expressionP->X_add_number = 0;
620
      return;
621
    }
622
 
623
  /* In order to get the correct byte ordering, we must build the
624
     number in reverse.  */
625
  for (i = SIZE_OF_LARGE_NUMBER - 1; i >= 0; i--)
626
    {
627
      int j;
628
 
629
      generic_bignum[i] = 0;
630
      for (j = 0; j < CHARS_PER_LITTLENUM; j++)
631
	{
632
	  if (*input_line_pointer == '\'')
633
	    {
634
	      if (input_line_pointer[1] != '\'')
635
		break;
636
	      ++input_line_pointer;
637
	    }
638
	  generic_bignum[i] <<= 8;
639
	  generic_bignum[i] += *input_line_pointer;
640
	  ++input_line_pointer;
641
	}
642
 
643
      if (i < SIZE_OF_LARGE_NUMBER - 1)
644
	{
645
	  /* If there is more than one littlenum, left justify the
646
	     last one to make it match the earlier ones.  If there is
647
	     only one, we can just use the value directly.  */
648
	  for (; j < CHARS_PER_LITTLENUM; j++)
649
	    generic_bignum[i] <<= 8;
650
	}
651
 
652
      if (*input_line_pointer == '\''
653
	  && input_line_pointer[1] != '\'')
654
	break;
655
    }
656
 
657
  if (i < 0)
658
    {
659
      as_bad (_("character constant too large"));
660
      i = 0;
661
    }
662
 
663
  if (i > 0)
664
    {
665
      int c;
666
      int j;
667
 
668
      c = SIZE_OF_LARGE_NUMBER - i;
669
      for (j = 0; j < c; j++)
670
	generic_bignum[j] = generic_bignum[i + j];
671
      i = c;
672
    }
673
 
674
  know (LITTLENUM_NUMBER_OF_BITS == 16);
675
  if (i > 2)
676
    {
677
      expressionP->X_op = O_big;
678
      expressionP->X_add_number = i;
679
    }
680
  else
681
    {
682
      expressionP->X_op = O_constant;
683
      if (i < 2)
684
	expressionP->X_add_number = generic_bignum[0] & LITTLENUM_MASK;
685
      else
686
	expressionP->X_add_number =
687
	  (((generic_bignum[1] & LITTLENUM_MASK)
688
	    << LITTLENUM_NUMBER_OF_BITS)
689
	   | (generic_bignum[0] & LITTLENUM_MASK));
690
    }
691
 
692
  /* Skip the final closing quote.  */
693
  ++input_line_pointer;
694
}
695
 
696
/* Return an expression representing the current location.  This
697
   handles the magic symbol `.'.  */
698
 
699
void
700
current_location (expressionS *expressionp)
701
{
702
  if (now_seg == absolute_section)
703
    {
704
      expressionp->X_op = O_constant;
705
      expressionp->X_add_number = abs_section_offset;
706
    }
707
  else
708
    {
709
      expressionp->X_op = O_symbol;
710
      expressionp->X_add_symbol = &dot_symbol;
711
      expressionp->X_add_number = 0;
712
    }
713
}
714
 
715
/* In:	Input_line_pointer points to 1st char of operand, which may
716
	be a space.
717
 
718
   Out:	An expressionS.
719
	The operand may have been empty: in this case X_op == O_absent.
720
	Input_line_pointer->(next non-blank) char after operand.  */
721
 
722
static segT
723
operand (expressionS *expressionP, enum expr_mode mode)
724
{
725
  char c;
726
  symbolS *symbolP;	/* Points to symbol.  */
727
  char *name;		/* Points to name of symbol.  */
728
  segT segment;
729
 
730
  /* All integers are regarded as unsigned unless they are negated.
731
     This is because the only thing which cares whether a number is
732
     unsigned is the code in emit_expr which extends constants into
733
     bignums.  It should only sign extend negative numbers, so that
734
     something like ``.quad 0x80000000'' is not sign extended even
735
     though it appears negative if valueT is 32 bits.  */
736
  expressionP->X_unsigned = 1;
737
  expressionP->X_extrabit = 0;
738
 
739
  /* Digits, assume it is a bignum.  */
740
 
741
  SKIP_WHITESPACE ();		/* Leading whitespace is part of operand.  */
742
  c = *input_line_pointer++;	/* input_line_pointer -> past char in c.  */
743
 
744
  if (is_end_of_line[(unsigned char) c])
745
    goto eol;
746
 
747
  switch (c)
748
    {
749
    case '1':
750
    case '2':
751
    case '3':
752
    case '4':
753
    case '5':
754
    case '6':
755
    case '7':
756
    case '8':
757
    case '9':
758
      input_line_pointer--;
759
 
760
      integer_constant ((NUMBERS_WITH_SUFFIX || flag_m68k_mri)
761
			? 0 : 10,
762
			expressionP);
763
      break;
764
 
765
#ifdef LITERAL_PREFIXDOLLAR_HEX
766
    case '$':
767
      /* $L is the start of a local label, not a hex constant.  */
768
      if (* input_line_pointer == 'L')
769
      goto isname;
770
      integer_constant (16, expressionP);
771
      break;
772
#endif
773
 
774
#ifdef LITERAL_PREFIXPERCENT_BIN
775
    case '%':
776
      integer_constant (2, expressionP);
777
      break;
778
#endif
779
 
780
    case '0':
781
      /* Non-decimal radix.  */
782
 
783
      if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
784
	{
785
	  char *s;
786
 
787
	  /* Check for a hex or float constant.  */
788
	  for (s = input_line_pointer; hex_p (*s); s++)
789
	    ;
790
	  if (*s == 'h' || *s == 'H' || *input_line_pointer == '.')
791
	    {
792
	      --input_line_pointer;
793
	      integer_constant (0, expressionP);
794
	      break;
795
	    }
796
	}
797
      c = *input_line_pointer;
798
      switch (c)
799
	{
800
	case 'o':
801
	case 'O':
802
	case 'q':
803
	case 'Q':
804
	case '8':
805
	case '9':
806
	  if (NUMBERS_WITH_SUFFIX || flag_m68k_mri)
807
	    {
808
	      integer_constant (0, expressionP);
809
	      break;
810
	    }
811
	  /* Fall through.  */
812
	default:
813
	default_case:
814
	  if (c && strchr (FLT_CHARS, c))
815
	    {
816
	      input_line_pointer++;
817
	      floating_constant (expressionP);
818
	      expressionP->X_add_number = - TOLOWER (c);
819
	    }
820
	  else
821
	    {
822
	      /* The string was only zero.  */
823
	      expressionP->X_op = O_constant;
824
	      expressionP->X_add_number = 0;
825
	    }
826
 
827
	  break;
828
 
829
	case 'x':
830
	case 'X':
831
	  if (flag_m68k_mri)
832
	    goto default_case;
833
	  input_line_pointer++;
834
	  integer_constant (16, expressionP);
835
	  break;
836
 
837
	case 'b':
838
	  if (LOCAL_LABELS_FB && ! (flag_m68k_mri || NUMBERS_WITH_SUFFIX))
839
	    {
840
	      /* This code used to check for '+' and '-' here, and, in
841
		 some conditions, fall through to call
842
		 integer_constant.  However, that didn't make sense,
843
		 as integer_constant only accepts digits.  */
844
	      /* Some of our code elsewhere does permit digits greater
845
		 than the expected base; for consistency, do the same
846
		 here.  */
847
	      if (input_line_pointer[1] < '0'
848
		  || input_line_pointer[1] > '9')
849
		{
850
		  /* Parse this as a back reference to label 0.  */
851
		  input_line_pointer--;
852
		  integer_constant (10, expressionP);
853
		  break;
854
		}
855
	      /* Otherwise, parse this as a binary number.  */
856
	    }
857
	  /* Fall through.  */
858
	case 'B':
859
	  input_line_pointer++;
860
	  if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
861
	    goto default_case;
862
	  integer_constant (2, expressionP);
863
	  break;
864
 
865
	case '0':
866
	case '1':
867
	case '2':
868
	case '3':
869
	case '4':
870
	case '5':
871
	case '6':
872
	case '7':
873
	  integer_constant ((flag_m68k_mri || NUMBERS_WITH_SUFFIX)
874
			    ? 0 : 8,
875
			    expressionP);
876
	  break;
877
 
878
	case 'f':
879
	  if (LOCAL_LABELS_FB)
880
	    {
881
	      /* If it says "0f" and it could possibly be a floating point
882
		 number, make it one.  Otherwise, make it a local label,
883
		 and try to deal with parsing the rest later.  */
884
	      if (!input_line_pointer[1]
885
		  || (is_end_of_line[0xff & input_line_pointer[1]])
886
		  || strchr (FLT_CHARS, 'f') == NULL)
887
		goto is_0f_label;
888
	      {
889
		char *cp = input_line_pointer + 1;
890
		int r = atof_generic (&cp, ".", EXP_CHARS,
891
				      &generic_floating_point_number);
892
		switch (r)
893
		  {
894
		  case 0:
895
		  case ERROR_EXPONENT_OVERFLOW:
896
		    if (*cp == 'f' || *cp == 'b')
897
		      /* Looks like a difference expression.  */
898
		      goto is_0f_label;
899
		    else if (cp == input_line_pointer + 1)
900
		      /* No characters has been accepted -- looks like
901
			 end of operand.  */
902
		      goto is_0f_label;
903
		    else
904
		      goto is_0f_float;
905
		  default:
906
		    as_fatal (_("expr.c(operand): bad atof_generic return val %d"),
907
			      r);
908
		  }
909
	      }
910
 
911
	      /* Okay, now we've sorted it out.  We resume at one of these
912
		 two labels, depending on what we've decided we're probably
913
		 looking at.  */
914
	    is_0f_label:
915
	      input_line_pointer--;
916
	      integer_constant (10, expressionP);
917
	      break;
918
 
919
	    is_0f_float:
920
	      /* Fall through.  */
921
	      ;
922
	    }
923
 
924
	case 'd':
925
	case 'D':
926
	  if (flag_m68k_mri || NUMBERS_WITH_SUFFIX)
927
	    {
928
	      integer_constant (0, expressionP);
929
	      break;
930
	    }
931
	  /* Fall through.  */
932
	case 'F':
933
	case 'r':
934
	case 'e':
935
	case 'E':
936
	case 'g':
937
	case 'G':
938
	  input_line_pointer++;
939
	  floating_constant (expressionP);
940
	  expressionP->X_add_number = - TOLOWER (c);
941
	  break;
942
 
943
	case '$':
944
	  if (LOCAL_LABELS_DOLLAR)
945
	    {
946
	      integer_constant (10, expressionP);
947
	      break;
948
	    }
949
	  else
950
	    goto default_case;
951
	}
952
 
953
      break;
954
 
955
#ifndef NEED_INDEX_OPERATOR
956
    case '[':
957
# ifdef md_need_index_operator
958
      if (md_need_index_operator())
959
	goto de_fault;
960
# endif
961
      /* FALLTHROUGH */
962
#endif
963
    case '(':
964
      /* Didn't begin with digit & not a name.  */
965
      segment = expr (0, expressionP, mode);
966
      /* expression () will pass trailing whitespace.  */
967
      if ((c == '(' && *input_line_pointer != ')')
968
	  || (c == '[' && *input_line_pointer != ']'))
969
	as_bad (_("missing '%c'"), c == '(' ? ')' : ']');
970
      else
971
	input_line_pointer++;
972
      SKIP_WHITESPACE ();
973
      /* Here with input_line_pointer -> char after "(...)".  */
974
      return segment;
975
 
976
#ifdef TC_M68K
977
    case 'E':
978
      if (! flag_m68k_mri || *input_line_pointer != '\'')
979
	goto de_fault;
980
      as_bad (_("EBCDIC constants are not supported"));
981
      /* Fall through.  */
982
    case 'A':
983
      if (! flag_m68k_mri || *input_line_pointer != '\'')
984
	goto de_fault;
985
      ++input_line_pointer;
986
      /* Fall through.  */
987
#endif
988
    case '\'':
989
      if (! flag_m68k_mri)
990
	{
991
	  /* Warning: to conform to other people's assemblers NO
992
	     ESCAPEMENT is permitted for a single quote.  The next
993
	     character, parity errors and all, is taken as the value
994
	     of the operand.  VERY KINKY.  */
995
	  expressionP->X_op = O_constant;
996
	  expressionP->X_add_number = *input_line_pointer++;
997
	  break;
998
	}
999
 
1000
      mri_char_constant (expressionP);
1001
      break;
1002
 
1003
#ifdef TC_M68K
1004
    case '"':
1005
      /* Double quote is the bitwise not operator in MRI mode.  */
1006
      if (! flag_m68k_mri)
1007
	goto de_fault;
1008
      /* Fall through.  */
1009
#endif
1010
    case '~':
1011
      /* '~' is permitted to start a label on the Delta.  */
1012
      if (is_name_beginner (c))
1013
	goto isname;
1014
    case '!':
1015
    case '-':
1016
    case '+':
1017
      {
1018
#ifdef md_operator
1019
      unary:
1020
#endif
1021
	operand (expressionP, mode);
1022
	if (expressionP->X_op == O_constant)
1023
	  {
1024
	    /* input_line_pointer -> char after operand.  */
1025
	    if (c == '-')
1026
	      {
1027
		expressionP->X_add_number = - expressionP->X_add_number;
1028
		/* Notice: '-' may overflow: no warning is given.
1029
		   This is compatible with other people's
1030
		   assemblers.  Sigh.  */
1031
		expressionP->X_unsigned = 0;
1032
		if (expressionP->X_add_number)
1033
		  expressionP->X_extrabit ^= 1;
1034
	      }
1035
	    else if (c == '~' || c == '"')
1036
	      expressionP->X_add_number = ~ expressionP->X_add_number;
1037
	    else if (c == '!')
1038
	      expressionP->X_add_number = ! expressionP->X_add_number;
1039
	  }
1040
	else if (expressionP->X_op == O_big
1041
		 && expressionP->X_add_number <= 0
1042
		 && c == '-'
1043
		 && (generic_floating_point_number.sign == '+'
1044
		     || generic_floating_point_number.sign == 'P'))
1045
	  {
1046
	    /* Negative flonum (eg, -1.000e0).  */
1047
	    if (generic_floating_point_number.sign == '+')
1048
	      generic_floating_point_number.sign = '-';
1049
	    else
1050
	      generic_floating_point_number.sign = 'N';
1051
	  }
1052
	else if (expressionP->X_op == O_big
1053
		 && expressionP->X_add_number > 0)
1054
	  {
1055
	    int i;
1056
 
1057
	    if (c == '~' || c == '-')
1058
	      {
1059
		for (i = 0; i < expressionP->X_add_number; ++i)
1060
		  generic_bignum[i] = ~generic_bignum[i];
1061
 
1062
		/* Extend the bignum to at least the size of .octa.  */
1063
		if (expressionP->X_add_number < SIZE_OF_LARGE_NUMBER)
1064
		  {
1065
		    expressionP->X_add_number = SIZE_OF_LARGE_NUMBER;
1066
		    for (; i < expressionP->X_add_number; ++i)
1067
		      generic_bignum[i] = ~(LITTLENUM_TYPE) 0;
1068
		  }
1069
 
1070
		if (c == '-')
1071
		  for (i = 0; i < expressionP->X_add_number; ++i)
1072
		    {
1073
		      generic_bignum[i] += 1;
1074
		      if (generic_bignum[i])
1075
			break;
1076
		    }
1077
	      }
1078
	    else if (c == '!')
1079
	      {
1080
		for (i = 0; i < expressionP->X_add_number; ++i)
1081
		  if (generic_bignum[i] != 0)
1082
		    break;
1083
		expressionP->X_add_number = i >= expressionP->X_add_number;
1084
		expressionP->X_op = O_constant;
1085
		expressionP->X_unsigned = 1;
1086
		expressionP->X_extrabit = 0;
1087
	      }
1088
	  }
1089
	else if (expressionP->X_op != O_illegal
1090
		 && expressionP->X_op != O_absent)
1091
	  {
1092
	    if (c != '+')
1093
	      {
1094
		expressionP->X_add_symbol = make_expr_symbol (expressionP);
1095
		if (c == '-')
1096
		  expressionP->X_op = O_uminus;
1097
		else if (c == '~' || c == '"')
1098
		  expressionP->X_op = O_bit_not;
1099
		else
1100
		  expressionP->X_op = O_logical_not;
1101
		expressionP->X_add_number = 0;
1102
	      }
1103
	  }
1104
	else
1105
	  as_warn (_("Unary operator %c ignored because bad operand follows"),
1106
		   c);
1107
      }
1108
      break;
1109
 
1110
#if defined (DOLLAR_DOT) || defined (TC_M68K)
1111
    case '$':
1112
      /* '$' is the program counter when in MRI mode, or when
1113
	 DOLLAR_DOT is defined.  */
1114
#ifndef DOLLAR_DOT
1115
      if (! flag_m68k_mri)
1116
	goto de_fault;
1117
#endif
1118
      if (DOLLAR_AMBIGU && hex_p (*input_line_pointer))
1119
	{
1120
	  /* In MRI mode and on Z80, '$' is also used as the prefix
1121
	     for a hexadecimal constant.  */
1122
	  integer_constant (16, expressionP);
1123
	  break;
1124
	}
1125
 
1126
      if (is_part_of_name (*input_line_pointer))
1127
	goto isname;
1128
 
1129
      current_location (expressionP);
1130
      break;
1131
#endif
1132
 
1133
    case '.':
1134
      if (!is_part_of_name (*input_line_pointer))
1135
	{
1136
	  current_location (expressionP);
1137
	  break;
1138
	}
1139
      else if ((strncasecmp (input_line_pointer, "startof.", 8) == 0
1140
		&& ! is_part_of_name (input_line_pointer[8]))
1141
	       || (strncasecmp (input_line_pointer, "sizeof.", 7) == 0
1142
		   && ! is_part_of_name (input_line_pointer[7])))
1143
	{
1144
	  int start;
1145
 
1146
	  start = (input_line_pointer[1] == 't'
1147
		   || input_line_pointer[1] == 'T');
1148
	  input_line_pointer += start ? 8 : 7;
1149
	  SKIP_WHITESPACE ();
1150
	  if (*input_line_pointer != '(')
1151
	    as_bad (_("syntax error in .startof. or .sizeof."));
1152
	  else
1153
	    {
1154
	      char *buf;
1155
 
1156
	      ++input_line_pointer;
1157
	      SKIP_WHITESPACE ();
1158
	      name = input_line_pointer;
1159
	      c = get_symbol_end ();
1160
 
1161
	      buf = (char *) xmalloc (strlen (name) + 10);
1162
	      if (start)
1163
		sprintf (buf, ".startof.%s", name);
1164
	      else
1165
		sprintf (buf, ".sizeof.%s", name);
1166
	      symbolP = symbol_make (buf);
1167
	      free (buf);
1168
 
1169
	      expressionP->X_op = O_symbol;
1170
	      expressionP->X_add_symbol = symbolP;
1171
	      expressionP->X_add_number = 0;
1172
 
1173
	      *input_line_pointer = c;
1174
	      SKIP_WHITESPACE ();
1175
	      if (*input_line_pointer != ')')
1176
		as_bad (_("syntax error in .startof. or .sizeof."));
1177
	      else
1178
		++input_line_pointer;
1179
	    }
1180
	  break;
1181
	}
1182
      else
1183
	{
1184
	  goto isname;
1185
	}
1186
 
1187
    case ',':
1188
    eol:
1189
      /* Can't imagine any other kind of operand.  */
1190
      expressionP->X_op = O_absent;
1191
      input_line_pointer--;
1192
      break;
1193
 
1194
#ifdef TC_M68K
1195
    case '%':
1196
      if (! flag_m68k_mri)
1197
	goto de_fault;
1198
      integer_constant (2, expressionP);
1199
      break;
1200
 
1201
    case '@':
1202
      if (! flag_m68k_mri)
1203
	goto de_fault;
1204
      integer_constant (8, expressionP);
1205
      break;
1206
 
1207
    case ':':
1208
      if (! flag_m68k_mri)
1209
	goto de_fault;
1210
 
1211
      /* In MRI mode, this is a floating point constant represented
1212
	 using hexadecimal digits.  */
1213
 
1214
      ++input_line_pointer;
1215
      integer_constant (16, expressionP);
1216
      break;
1217
 
1218
    case '*':
1219
      if (! flag_m68k_mri || is_part_of_name (*input_line_pointer))
1220
	goto de_fault;
1221
 
1222
      current_location (expressionP);
1223
      break;
1224
#endif
1225
 
1226
    default:
1227
#if defined(md_need_index_operator) || defined(TC_M68K)
1228
    de_fault:
1229
#endif
1230
      if (is_name_beginner (c))	/* Here if did not begin with a digit.  */
1231
	{
1232
	  /* Identifier begins here.
1233
	     This is kludged for speed, so code is repeated.  */
1234
	isname:
1235
	  name = --input_line_pointer;
1236
	  c = get_symbol_end ();
1237
 
1238
#ifdef md_operator
1239
	  {
1240
	    operatorT op = md_operator (name, 1, &c);
1241
 
1242
	    switch (op)
1243
	      {
1244
	      case O_uminus:
1245
		*input_line_pointer = c;
1246
		c = '-';
1247
		goto unary;
1248
	      case O_bit_not:
1249
		*input_line_pointer = c;
1250
		c = '~';
1251
		goto unary;
1252
	      case O_logical_not:
1253
		*input_line_pointer = c;
1254
		c = '!';
1255
		goto unary;
1256
	      case O_illegal:
1257
		as_bad (_("invalid use of operator \"%s\""), name);
1258
		break;
1259
	      default:
1260
		break;
1261
	      }
1262
	    if (op != O_absent && op != O_illegal)
1263
	      {
1264
		*input_line_pointer = c;
1265
		expr (9, expressionP, mode);
1266
		expressionP->X_add_symbol = make_expr_symbol (expressionP);
1267
		expressionP->X_op_symbol = NULL;
1268
		expressionP->X_add_number = 0;
1269
		expressionP->X_op = op;
1270
		break;
1271
	      }
1272
	  }
1273
#endif
1274
 
1275
#ifdef md_parse_name
1276
	  /* This is a hook for the backend to parse certain names
1277
	     specially in certain contexts.  If a name always has a
1278
	     specific value, it can often be handled by simply
1279
	     entering it in the symbol table.  */
1280
	  if (md_parse_name (name, expressionP, mode, &c))
1281
	    {
1282
	      *input_line_pointer = c;
1283
	      break;
1284
	    }
1285
#endif
1286
 
1287
#ifdef TC_I960
1288
	  /* The MRI i960 assembler permits
1289
	         lda sizeof code,g13
1290
	     FIXME: This should use md_parse_name.  */
1291
	  if (flag_mri
1292
	      && (strcasecmp (name, "sizeof") == 0
1293
		  || strcasecmp (name, "startof") == 0))
1294
	    {
1295
	      int start;
1296
	      char *buf;
1297
 
1298
	      start = (name[1] == 't'
1299
		       || name[1] == 'T');
1300
 
1301
	      *input_line_pointer = c;
1302
	      SKIP_WHITESPACE ();
1303
 
1304
	      name = input_line_pointer;
1305
	      c = get_symbol_end ();
1306
 
1307
	      buf = (char *) xmalloc (strlen (name) + 10);
1308
	      if (start)
1309
		sprintf (buf, ".startof.%s", name);
1310
	      else
1311
		sprintf (buf, ".sizeof.%s", name);
1312
	      symbolP = symbol_make (buf);
1313
	      free (buf);
1314
 
1315
	      expressionP->X_op = O_symbol;
1316
	      expressionP->X_add_symbol = symbolP;
1317
	      expressionP->X_add_number = 0;
1318
 
1319
	      *input_line_pointer = c;
1320
	      SKIP_WHITESPACE ();
1321
 
1322
	      break;
1323
	    }
1324
#endif
1325
 
1326
	  symbolP = symbol_find_or_make (name);
1327
 
1328
	  /* If we have an absolute symbol or a reg, then we know its
1329
	     value now.  */
1330
	  segment = S_GET_SEGMENT (symbolP);
1331
	  if (mode != expr_defer
1332
	      && segment == absolute_section
1333
	      && !S_FORCE_RELOC (symbolP, 0))
1334
	    {
1335
	      expressionP->X_op = O_constant;
1336
	      expressionP->X_add_number = S_GET_VALUE (symbolP);
1337
	    }
1338
	  else if (mode != expr_defer && segment == reg_section)
1339
	    {
1340
	      expressionP->X_op = O_register;
1341
	      expressionP->X_add_number = S_GET_VALUE (symbolP);
1342
	    }
1343
	  else
1344
	    {
1345
	      expressionP->X_op = O_symbol;
1346
	      expressionP->X_add_symbol = symbolP;
1347
	      expressionP->X_add_number = 0;
1348
	    }
1349
	  *input_line_pointer = c;
1350
	}
1351
      else
1352
	{
1353
	  /* Let the target try to parse it.  Success is indicated by changing
1354
	     the X_op field to something other than O_absent and pointing
1355
	     input_line_pointer past the expression.  If it can't parse the
1356
	     expression, X_op and input_line_pointer should be unchanged.  */
1357
	  expressionP->X_op = O_absent;
1358
	  --input_line_pointer;
1359
	  md_operand (expressionP);
1360
	  if (expressionP->X_op == O_absent)
1361
	    {
1362
	      ++input_line_pointer;
1363
	      as_bad (_("bad expression"));
1364
	      expressionP->X_op = O_constant;
1365
	      expressionP->X_add_number = 0;
1366
	    }
1367
	}
1368
      break;
1369
    }
1370
 
1371
  /* It is more 'efficient' to clean up the expressionS when they are
1372
     created.  Doing it here saves lines of code.  */
1373
  clean_up_expression (expressionP);
1374
  SKIP_WHITESPACE ();		/* -> 1st char after operand.  */
1375
  know (*input_line_pointer != ' ');
1376
 
1377
  /* The PA port needs this information.  */
1378
  if (expressionP->X_add_symbol)
1379
    symbol_mark_used (expressionP->X_add_symbol);
1380
 
1381
  if (mode != expr_defer)
1382
    {
1383
      expressionP->X_add_symbol
1384
	= symbol_clone_if_forward_ref (expressionP->X_add_symbol);
1385
      expressionP->X_op_symbol
1386
	= symbol_clone_if_forward_ref (expressionP->X_op_symbol);
1387
    }
1388
 
1389
  switch (expressionP->X_op)
1390
    {
1391
    default:
1392
      return absolute_section;
1393
    case O_symbol:
1394
      return S_GET_SEGMENT (expressionP->X_add_symbol);
1395
    case O_register:
1396
      return reg_section;
1397
    }
1398
}
1399
 
1400
/* Internal.  Simplify a struct expression for use by expr ().  */
1401
 
1402
/* In:	address of an expressionS.
1403
	The X_op field of the expressionS may only take certain values.
1404
	Elsewise we waste time special-case testing. Sigh. Ditto SEG_ABSENT.
1405
 
1406
   Out:	expressionS may have been modified:
1407
	Unused fields zeroed to help expr ().  */
1408
 
1409
static void
1410
clean_up_expression (expressionS *expressionP)
1411
{
1412
  switch (expressionP->X_op)
1413
    {
1414
    case O_illegal:
1415
    case O_absent:
1416
      expressionP->X_add_number = 0;
1417
      /* Fall through.  */
1418
    case O_big:
1419
    case O_constant:
1420
    case O_register:
1421
      expressionP->X_add_symbol = NULL;
1422
      /* Fall through.  */
1423
    case O_symbol:
1424
    case O_uminus:
1425
    case O_bit_not:
1426
      expressionP->X_op_symbol = NULL;
1427
      break;
1428
    default:
1429
      break;
1430
    }
1431
}
1432
 
1433
/* Expression parser.  */
1434
 
1435
/* We allow an empty expression, and just assume (absolute,0) silently.
1436
   Unary operators and parenthetical expressions are treated as operands.
1437
   As usual, Q==quantity==operand, O==operator, X==expression mnemonics.
1438
 
1439
   We used to do an aho/ullman shift-reduce parser, but the logic got so
1440
   warped that I flushed it and wrote a recursive-descent parser instead.
1441
   Now things are stable, would anybody like to write a fast parser?
1442
   Most expressions are either register (which does not even reach here)
1443
   or 1 symbol. Then "symbol+constant" and "symbol-symbol" are common.
1444
   So I guess it doesn't really matter how inefficient more complex expressions
1445
   are parsed.
1446
 
1447
   After expr(RANK,resultP) input_line_pointer->operator of rank <= RANK.
1448
   Also, we have consumed any leading or trailing spaces (operand does that)
1449
   and done all intervening operators.
1450
 
1451
   This returns the segment of the result, which will be
1452
   absolute_section or the segment of a symbol.  */
1453
 
1454
#undef __
1455
#define __ O_illegal
1456
#ifndef O_SINGLE_EQ
1457
#define O_SINGLE_EQ O_illegal
1458
#endif
1459
 
1460
/* Maps ASCII -> operators.  */
1461
static const operatorT op_encoding[256] = {
1462
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1463
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1464
 
1465
  __, O_bit_or_not, __, __, __, O_modulus, O_bit_and, __,
1466
  __, __, O_multiply, O_add, __, O_subtract, __, O_divide,
1467
  __, __, __, __, __, __, __, __,
1468
  __, __, __, __, O_lt, O_SINGLE_EQ, O_gt, __,
1469
  __, __, __, __, __, __, __, __,
1470
  __, __, __, __, __, __, __, __,
1471
  __, __, __, __, __, __, __, __,
1472
  __, __, __,
1473
#ifdef NEED_INDEX_OPERATOR
1474
  O_index,
1475
#else
1476
  __,
1477
#endif
1478
  __, __, O_bit_exclusive_or, __,
1479
  __, __, __, __, __, __, __, __,
1480
  __, __, __, __, __, __, __, __,
1481
  __, __, __, __, __, __, __, __,
1482
  __, __, __, __, O_bit_inclusive_or, __, __, __,
1483
 
1484
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1485
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1486
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1487
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1488
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1489
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1490
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __,
1491
  __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __
1492
};
1493
 
1494
/* Rank	Examples
1495
 
1496
   1	||
1497
   2	&&
1498
   3	== <> < <= >= >
1499
   4	+ -
1500
   5	used for * / % in MRI mode
1501
   6	& ^ ! |
1502
   7	* / % << >>
1503
   8	unary - unary ~
1504
*/
1505
static operator_rankT op_rank[O_max] = {
1506
  0,	/* O_illegal */
1507
  0,	/* O_absent */
1508
  0,	/* O_constant */
1509
  0,	/* O_symbol */
1510
  0,	/* O_symbol_rva */
1511
  0,	/* O_register */
1512
  0,	/* O_big */
1513
  9,	/* O_uminus */
1514
  9,	/* O_bit_not */
1515
  9,	/* O_logical_not */
1516
  8,	/* O_multiply */
1517
  8,	/* O_divide */
1518
  8,	/* O_modulus */
1519
  8,	/* O_left_shift */
1520
  8,	/* O_right_shift */
1521
  7,	/* O_bit_inclusive_or */
1522
  7,	/* O_bit_or_not */
1523
  7,	/* O_bit_exclusive_or */
1524
  7,	/* O_bit_and */
1525
  5,	/* O_add */
1526
  5,	/* O_subtract */
1527
  4,	/* O_eq */
1528
  4,	/* O_ne */
1529
  4,	/* O_lt */
1530
  4,	/* O_le */
1531
  4,	/* O_ge */
1532
  4,	/* O_gt */
1533
  3,	/* O_logical_and */
1534
  2,	/* O_logical_or */
1535
  1,	/* O_index */
1536
};
1537
 
1538
/* Unfortunately, in MRI mode for the m68k, multiplication and
1539
   division have lower precedence than the bit wise operators.  This
1540
   function sets the operator precedences correctly for the current
1541
   mode.  Also, MRI uses a different bit_not operator, and this fixes
1542
   that as well.  */
1543
 
1544
#define STANDARD_MUL_PRECEDENCE 8
1545
#define MRI_MUL_PRECEDENCE 6
1546
 
1547
void
1548
expr_set_precedence (void)
1549
{
1550
  if (flag_m68k_mri)
1551
    {
1552
      op_rank[O_multiply] = MRI_MUL_PRECEDENCE;
1553
      op_rank[O_divide] = MRI_MUL_PRECEDENCE;
1554
      op_rank[O_modulus] = MRI_MUL_PRECEDENCE;
1555
    }
1556
  else
1557
    {
1558
      op_rank[O_multiply] = STANDARD_MUL_PRECEDENCE;
1559
      op_rank[O_divide] = STANDARD_MUL_PRECEDENCE;
1560
      op_rank[O_modulus] = STANDARD_MUL_PRECEDENCE;
1561
    }
1562
}
1563
 
1564
void
1565
expr_set_rank (operatorT op, operator_rankT rank)
1566
{
1567
  gas_assert (op >= O_md1 && op < ARRAY_SIZE (op_rank));
1568
  op_rank[op] = rank;
1569
}
1570
 
1571
/* Initialize the expression parser.  */
1572
 
1573
void
1574
expr_begin (void)
1575
{
1576
  expr_set_precedence ();
1577
 
1578
  /* Verify that X_op field is wide enough.  */
1579
  {
1580
    expressionS e;
1581
    e.X_op = O_max;
1582
    gas_assert (e.X_op == O_max);
1583
  }
1584
}
1585
 
1586
/* Return the encoding for the operator at INPUT_LINE_POINTER, and
1587
   sets NUM_CHARS to the number of characters in the operator.
1588
   Does not advance INPUT_LINE_POINTER.  */
1589
 
1590
static inline operatorT
1591
operatorf (int *num_chars)
1592
{
1593
  int c;
1594
  operatorT ret;
1595
 
1596
  c = *input_line_pointer & 0xff;
1597
  *num_chars = 1;
1598
 
1599
  if (is_end_of_line[c])
1600
    return O_illegal;
1601
 
1602
#ifdef md_operator
1603
  if (is_name_beginner (c))
1604
    {
1605
      char *name = input_line_pointer;
1606
      char ec = get_symbol_end ();
1607
 
1608
      ret = md_operator (name, 2, &ec);
1609
      switch (ret)
1610
	{
1611
	case O_absent:
1612
	  *input_line_pointer = ec;
1613
	  input_line_pointer = name;
1614
	  break;
1615
	case O_uminus:
1616
	case O_bit_not:
1617
	case O_logical_not:
1618
	  as_bad (_("invalid use of operator \"%s\""), name);
1619
	  ret = O_illegal;
1620
	  /* FALLTHROUGH */
1621
	default:
1622
	  *input_line_pointer = ec;
1623
	  *num_chars = input_line_pointer - name;
1624
	  input_line_pointer = name;
1625
	  return ret;
1626
	}
1627
    }
1628
#endif
1629
 
1630
  switch (c)
1631
    {
1632
    default:
1633
      ret = op_encoding[c];
1634
#ifdef md_operator
1635
      if (ret == O_illegal)
1636
	{
1637
	  char *start = input_line_pointer;
1638
 
1639
	  ret = md_operator (NULL, 2, NULL);
1640
	  if (ret != O_illegal)
1641
	    *num_chars = input_line_pointer - start;
1642
	  input_line_pointer = start;
1643
	}
1644
#endif
1645
      return ret;
1646
 
1647
    case '+':
1648
    case '-':
1649
      return op_encoding[c];
1650
 
1651
    case '<':
1652
      switch (input_line_pointer[1])
1653
	{
1654
	default:
1655
	  return op_encoding[c];
1656
	case '<':
1657
	  ret = O_left_shift;
1658
	  break;
1659
	case '>':
1660
	  ret = O_ne;
1661
	  break;
1662
	case '=':
1663
	  ret = O_le;
1664
	  break;
1665
	}
1666
      *num_chars = 2;
1667
      return ret;
1668
 
1669
    case '=':
1670
      if (input_line_pointer[1] != '=')
1671
	return op_encoding[c];
1672
 
1673
      *num_chars = 2;
1674
      return O_eq;
1675
 
1676
    case '>':
1677
      switch (input_line_pointer[1])
1678
	{
1679
	default:
1680
	  return op_encoding[c];
1681
	case '>':
1682
	  ret = O_right_shift;
1683
	  break;
1684
	case '=':
1685
	  ret = O_ge;
1686
	  break;
1687
	}
1688
      *num_chars = 2;
1689
      return ret;
1690
 
1691
    case '!':
1692
      switch (input_line_pointer[1])
1693
	{
1694
	case '!':
1695
	  /* We accept !! as equivalent to ^ for MRI compatibility. */
1696
	  *num_chars = 2;
1697
	  return O_bit_exclusive_or;
1698
	case '=':
1699
	  /* We accept != as equivalent to <>.  */
1700
	  *num_chars = 2;
1701
	  return O_ne;
1702
	default:
1703
	  if (flag_m68k_mri)
1704
	    return O_bit_inclusive_or;
1705
	  return op_encoding[c];
1706
	}
1707
 
1708
    case '|':
1709
      if (input_line_pointer[1] != '|')
1710
	return op_encoding[c];
1711
 
1712
      *num_chars = 2;
1713
      return O_logical_or;
1714
 
1715
    case '&':
1716
      if (input_line_pointer[1] != '&')
1717
	return op_encoding[c];
1718
 
1719
      *num_chars = 2;
1720
      return O_logical_and;
1721
    }
1722
 
1723
  /* NOTREACHED  */
1724
}
1725
 
1726
/* Implement "word-size + 1 bit" addition for
1727
   {resultP->X_extrabit:resultP->X_add_number} + {rhs_highbit:amount}.  This
1728
   is used so that the full range of unsigned word values and the full range of
1729
   signed word values can be represented in an O_constant expression, which is
1730
   useful e.g. for .sleb128 directives.  */
1731
 
1732
void
1733
add_to_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1734
{
1735
  valueT ures = resultP->X_add_number;
1736
  valueT uamount = amount;
1737
 
1738
  resultP->X_add_number += amount;
1739
 
1740
  resultP->X_extrabit ^= rhs_highbit;
1741
 
1742
  if (ures + uamount < ures)
1743
    resultP->X_extrabit ^= 1;
1744
}
1745
 
1746
/* Similarly, for subtraction.  */
1747
 
1748
void
1749
subtract_from_result (expressionS *resultP, offsetT amount, int rhs_highbit)
1750
{
1751
  valueT ures = resultP->X_add_number;
1752
  valueT uamount = amount;
1753
 
1754
  resultP->X_add_number -= amount;
1755
 
1756
  resultP->X_extrabit ^= rhs_highbit;
1757
 
1758
  if (ures < uamount)
1759
    resultP->X_extrabit ^= 1;
1760
}
1761
 
1762
/* Parse an expression.  */
1763
 
1764
segT
1765
expr (int rankarg,		/* Larger # is higher rank.  */
1766
      expressionS *resultP,	/* Deliver result here.  */
1767
      enum expr_mode mode	/* Controls behavior.  */)
1768
{
1769
  operator_rankT rank = (operator_rankT) rankarg;
1770
  segT retval;
1771
  expressionS right;
1772
  operatorT op_left;
1773
  operatorT op_right;
1774
  int op_chars;
1775
 
1776
  know (rankarg >= 0);
1777
 
1778
  /* Save the value of dot for the fixup code.  */
1779
  if (rank == 0)
1780
    {
1781
      dot_value = frag_now_fix ();
1782
      dot_frag = frag_now;
1783
    }
1784
 
1785
  retval = operand (resultP, mode);
1786
 
1787
  /* operand () gobbles spaces.  */
1788
  know (*input_line_pointer != ' ');
1789
 
1790
  op_left = operatorf (&op_chars);
1791
  while (op_left != O_illegal && op_rank[(int) op_left] > rank)
1792
    {
1793
      segT rightseg;
1794
      offsetT frag_off;
1795
 
1796
      input_line_pointer += op_chars;	/* -> after operator.  */
1797
 
1798
      right.X_md = 0;
1799
      rightseg = expr (op_rank[(int) op_left], &right, mode);
1800
      if (right.X_op == O_absent)
1801
	{
1802
	  as_warn (_("missing operand; zero assumed"));
1803
	  right.X_op = O_constant;
1804
	  right.X_add_number = 0;
1805
	  right.X_add_symbol = NULL;
1806
	  right.X_op_symbol = NULL;
1807
	}
1808
 
1809
      know (*input_line_pointer != ' ');
1810
 
1811
      if (op_left == O_index)
1812
	{
1813
	  if (*input_line_pointer != ']')
1814
	    as_bad ("missing right bracket");
1815
	  else
1816
	    {
1817
	      ++input_line_pointer;
1818
	      SKIP_WHITESPACE ();
1819
	    }
1820
	}
1821
 
1822
      op_right = operatorf (&op_chars);
1823
 
1824
      know (op_right == O_illegal || op_left == O_index
1825
	    || op_rank[(int) op_right] <= op_rank[(int) op_left]);
1826
      know ((int) op_left >= (int) O_multiply);
1827
#ifndef md_operator
1828
      know ((int) op_left <= (int) O_index);
1829
#else
1830
      know ((int) op_left < (int) O_max);
1831
#endif
1832
 
1833
      /* input_line_pointer->after right-hand quantity.  */
1834
      /* left-hand quantity in resultP.  */
1835
      /* right-hand quantity in right.  */
1836
      /* operator in op_left.  */
1837
 
1838
      if (resultP->X_op == O_big)
1839
	{
1840
	  if (resultP->X_add_number > 0)
1841
	    as_warn (_("left operand is a bignum; integer 0 assumed"));
1842
	  else
1843
	    as_warn (_("left operand is a float; integer 0 assumed"));
1844
	  resultP->X_op = O_constant;
1845
	  resultP->X_add_number = 0;
1846
	  resultP->X_add_symbol = NULL;
1847
	  resultP->X_op_symbol = NULL;
1848
	}
1849
      if (right.X_op == O_big)
1850
	{
1851
	  if (right.X_add_number > 0)
1852
	    as_warn (_("right operand is a bignum; integer 0 assumed"));
1853
	  else
1854
	    as_warn (_("right operand is a float; integer 0 assumed"));
1855
	  right.X_op = O_constant;
1856
	  right.X_add_number = 0;
1857
	  right.X_add_symbol = NULL;
1858
	  right.X_op_symbol = NULL;
1859
	}
1860
 
1861
      /* Optimize common cases.  */
1862
#ifdef md_optimize_expr
1863
      if (md_optimize_expr (resultP, op_left, &right))
1864
	{
1865
	  /* Skip.  */
1866
	  ;
1867
	}
1868
      else
1869
#endif
1870
#ifndef md_register_arithmetic
1871
# define md_register_arithmetic 1
1872
#endif
1873
      if (op_left == O_add && right.X_op == O_constant
1874
	  && (md_register_arithmetic || resultP->X_op != O_register))
1875
	{
1876
	  /* X + constant.  */
1877
	  add_to_result (resultP, right.X_add_number, right.X_extrabit);
1878
	}
1879
      /* This case comes up in PIC code.  */
1880
      else if (op_left == O_subtract
1881
	       && right.X_op == O_symbol
1882
	       && resultP->X_op == O_symbol
1883
	       && retval == rightseg
1884
#ifdef md_allow_local_subtract
1885
	       && md_allow_local_subtract (resultP, & right, rightseg)
1886
#endif
1887
	       && ((SEG_NORMAL (rightseg)
1888
		    && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
1889
		    && !S_FORCE_RELOC (right.X_add_symbol, 0))
1890
		   || right.X_add_symbol == resultP->X_add_symbol)
1891
	       && frag_offset_fixed_p (symbol_get_frag (resultP->X_add_symbol),
1892
				       symbol_get_frag (right.X_add_symbol),
1893
				       &frag_off))
1894
	{
1895
	  offsetT symval_diff = S_GET_VALUE (resultP->X_add_symbol)
1896
				- S_GET_VALUE (right.X_add_symbol);
1897
	  subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1898
	  subtract_from_result (resultP, frag_off / OCTETS_PER_BYTE, 0);
1899
	  add_to_result (resultP, symval_diff, symval_diff < 0);
1900
	  resultP->X_op = O_constant;
1901
	  resultP->X_add_symbol = 0;
1902
	}
1903
      else if (op_left == O_subtract && right.X_op == O_constant
1904
	       && (md_register_arithmetic || resultP->X_op != O_register))
1905
	{
1906
	  /* X - constant.  */
1907
	  subtract_from_result (resultP, right.X_add_number, right.X_extrabit);
1908
	}
1909
      else if (op_left == O_add && resultP->X_op == O_constant
1910
	       && (md_register_arithmetic || right.X_op != O_register))
1911
	{
1912
	  /* Constant + X.  */
1913
	  resultP->X_op = right.X_op;
1914
	  resultP->X_add_symbol = right.X_add_symbol;
1915
	  resultP->X_op_symbol = right.X_op_symbol;
1916
	  add_to_result (resultP, right.X_add_number, right.X_extrabit);
1917
	  retval = rightseg;
1918
	}
1919
      else if (resultP->X_op == O_constant && right.X_op == O_constant)
1920
	{
1921
	  /* Constant OP constant.  */
1922
	  offsetT v = right.X_add_number;
1923
	  if (v == 0 && (op_left == O_divide || op_left == O_modulus))
1924
	    {
1925
	      as_warn (_("division by zero"));
1926
	      v = 1;
1927
	    }
1928
	  if ((valueT) v >= sizeof(valueT) * CHAR_BIT
1929
	      && (op_left == O_left_shift || op_left == O_right_shift))
1930
	    {
1931
	      as_warn_value_out_of_range (_("shift count"), v, 0,
1932
					  sizeof(valueT) * CHAR_BIT - 1,
1933
					  NULL, 0);
1934
	      resultP->X_add_number = v = 0;
1935
	    }
1936
	  switch (op_left)
1937
	    {
1938
	    default:			goto general;
1939
	    case O_multiply:		resultP->X_add_number *= v; break;
1940
	    case O_divide:		resultP->X_add_number /= v; break;
1941
	    case O_modulus:		resultP->X_add_number %= v; break;
1942
	    case O_left_shift:		resultP->X_add_number <<= v; break;
1943
	    case O_right_shift:
1944
	      /* We always use unsigned shifts, to avoid relying on
1945
		 characteristics of the compiler used to compile gas.  */
1946
	      resultP->X_add_number =
1947
		(offsetT) ((valueT) resultP->X_add_number >> (valueT) v);
1948
	      break;
1949
	    case O_bit_inclusive_or:	resultP->X_add_number |= v; break;
1950
	    case O_bit_or_not:		resultP->X_add_number |= ~v; break;
1951
	    case O_bit_exclusive_or:	resultP->X_add_number ^= v; break;
1952
	    case O_bit_and:		resultP->X_add_number &= v; break;
1953
	      /* Constant + constant (O_add) is handled by the
1954
		 previous if statement for constant + X, so is omitted
1955
		 here.  */
1956
	    case O_subtract:
1957
	      subtract_from_result (resultP, v, 0);
1958
	      break;
1959
	    case O_eq:
1960
	      resultP->X_add_number =
1961
		resultP->X_add_number == v ? ~ (offsetT) 0 : 0;
1962
	      break;
1963
	    case O_ne:
1964
	      resultP->X_add_number =
1965
		resultP->X_add_number != v ? ~ (offsetT) 0 : 0;
1966
	      break;
1967
	    case O_lt:
1968
	      resultP->X_add_number =
1969
		resultP->X_add_number <  v ? ~ (offsetT) 0 : 0;
1970
	      break;
1971
	    case O_le:
1972
	      resultP->X_add_number =
1973
		resultP->X_add_number <= v ? ~ (offsetT) 0 : 0;
1974
	      break;
1975
	    case O_ge:
1976
	      resultP->X_add_number =
1977
		resultP->X_add_number >= v ? ~ (offsetT) 0 : 0;
1978
	      break;
1979
	    case O_gt:
1980
	      resultP->X_add_number =
1981
		resultP->X_add_number >  v ? ~ (offsetT) 0 : 0;
1982
	      break;
1983
	    case O_logical_and:
1984
	      resultP->X_add_number = resultP->X_add_number && v;
1985
	      break;
1986
	    case O_logical_or:
1987
	      resultP->X_add_number = resultP->X_add_number || v;
1988
	      break;
1989
	    }
1990
	}
1991
      else if (resultP->X_op == O_symbol
1992
	       && right.X_op == O_symbol
1993
	       && (op_left == O_add
1994
		   || op_left == O_subtract
1995
		   || (resultP->X_add_number == 0
1996
		       && right.X_add_number == 0)))
1997
	{
1998
	  /* Symbol OP symbol.  */
1999
	  resultP->X_op = op_left;
2000
	  resultP->X_op_symbol = right.X_add_symbol;
2001
	  if (op_left == O_add)
2002
	    add_to_result (resultP, right.X_add_number, right.X_extrabit);
2003
	  else if (op_left == O_subtract)
2004
	    {
2005
	      subtract_from_result (resultP, right.X_add_number,
2006
				    right.X_extrabit);
2007
	      if (retval == rightseg
2008
		  && SEG_NORMAL (retval)
2009
		  && !S_FORCE_RELOC (resultP->X_add_symbol, 0)
2010
		  && !S_FORCE_RELOC (right.X_add_symbol, 0))
2011
		{
2012
		  retval = absolute_section;
2013
		  rightseg = absolute_section;
2014
		}
2015
	    }
2016
	}
2017
      else
2018
	{
2019
        general:
2020
	  /* The general case.  */
2021
	  resultP->X_add_symbol = make_expr_symbol (resultP);
2022
	  resultP->X_op_symbol = make_expr_symbol (&right);
2023
	  resultP->X_op = op_left;
2024
	  resultP->X_add_number = 0;
2025
	  resultP->X_unsigned = 1;
2026
	  resultP->X_extrabit = 0;
2027
	}
2028
 
2029
      if (retval != rightseg)
2030
	{
2031
	  if (retval == undefined_section)
2032
	    ;
2033
	  else if (rightseg == undefined_section)
2034
	    retval = rightseg;
2035
	  else if (retval == expr_section)
2036
	    ;
2037
	  else if (rightseg == expr_section)
2038
	    retval = rightseg;
2039
	  else if (retval == reg_section)
2040
	    ;
2041
	  else if (rightseg == reg_section)
2042
	    retval = rightseg;
2043
	  else if (rightseg == absolute_section)
2044
	    ;
2045
	  else if (retval == absolute_section)
2046
	    retval = rightseg;
2047
#ifdef DIFF_EXPR_OK
2048
	  else if (op_left == O_subtract)
2049
	    ;
2050
#endif
2051
	  else
2052
	    as_bad (_("operation combines symbols in different segments"));
2053
	}
2054
 
2055
      op_left = op_right;
2056
    }				/* While next operator is >= this rank.  */
2057
 
2058
  /* The PA port needs this information.  */
2059
  if (resultP->X_add_symbol)
2060
    symbol_mark_used (resultP->X_add_symbol);
2061
 
2062
  if (rank == 0 && mode == expr_evaluate)
2063
    resolve_expression (resultP);
2064
 
2065
  return resultP->X_op == O_constant ? absolute_section : retval;
2066
}
2067
 
2068
/* Resolve an expression without changing any symbols/sub-expressions
2069
   used.  */
2070
 
2071
int
2072
resolve_expression (expressionS *expressionP)
2073
{
2074
  /* Help out with CSE.  */
2075
  valueT final_val = expressionP->X_add_number;
2076
  symbolS *add_symbol = expressionP->X_add_symbol;
2077
  symbolS *orig_add_symbol = add_symbol;
2078
  symbolS *op_symbol = expressionP->X_op_symbol;
2079
  operatorT op = expressionP->X_op;
2080
  valueT left, right;
2081
  segT seg_left, seg_right;
2082
  fragS *frag_left, *frag_right;
2083
  offsetT frag_off;
2084
 
2085
  switch (op)
2086
    {
2087
    default:
2088
      return 0;
2089
 
2090
    case O_constant:
2091
    case O_register:
2092
      left = 0;
2093
      break;
2094
 
2095
    case O_symbol:
2096
    case O_symbol_rva:
2097
      if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2098
	return 0;
2099
 
2100
      break;
2101
 
2102
    case O_uminus:
2103
    case O_bit_not:
2104
    case O_logical_not:
2105
      if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left))
2106
	return 0;
2107
 
2108
      if (seg_left != absolute_section)
2109
	return 0;
2110
 
2111
      if (op == O_logical_not)
2112
	left = !left;
2113
      else if (op == O_uminus)
2114
	left = -left;
2115
      else
2116
	left = ~left;
2117
      op = O_constant;
2118
      break;
2119
 
2120
    case O_multiply:
2121
    case O_divide:
2122
    case O_modulus:
2123
    case O_left_shift:
2124
    case O_right_shift:
2125
    case O_bit_inclusive_or:
2126
    case O_bit_or_not:
2127
    case O_bit_exclusive_or:
2128
    case O_bit_and:
2129
    case O_add:
2130
    case O_subtract:
2131
    case O_eq:
2132
    case O_ne:
2133
    case O_lt:
2134
    case O_le:
2135
    case O_ge:
2136
    case O_gt:
2137
    case O_logical_and:
2138
    case O_logical_or:
2139
      if (!snapshot_symbol (&add_symbol, &left, &seg_left, &frag_left)
2140
	  || !snapshot_symbol (&op_symbol, &right, &seg_right, &frag_right))
2141
	return 0;
2142
 
2143
      /* Simplify addition or subtraction of a constant by folding the
2144
	 constant into X_add_number.  */
2145
      if (op == O_add)
2146
	{
2147
	  if (seg_right == absolute_section)
2148
	    {
2149
	      final_val += right;
2150
	      op = O_symbol;
2151
	      break;
2152
	    }
2153
	  else if (seg_left == absolute_section)
2154
	    {
2155
	      final_val += left;
2156
	      left = right;
2157
	      seg_left = seg_right;
2158
	      add_symbol = op_symbol;
2159
	      orig_add_symbol = expressionP->X_op_symbol;
2160
	      op = O_symbol;
2161
	      break;
2162
	    }
2163
	}
2164
      else if (op == O_subtract)
2165
	{
2166
	  if (seg_right == absolute_section)
2167
	    {
2168
	      final_val -= right;
2169
	      op = O_symbol;
2170
	      break;
2171
	    }
2172
	}
2173
 
2174
      /* Equality and non-equality tests are permitted on anything.
2175
	 Subtraction, and other comparison operators are permitted if
2176
	 both operands are in the same section.
2177
	 Shifts by constant zero are permitted on anything.
2178
	 Multiplies, bit-ors, and bit-ands with constant zero are
2179
	 permitted on anything.
2180
	 Multiplies and divides by constant one are permitted on
2181
	 anything.
2182
	 Binary operations with both operands being the same register
2183
	 or undefined symbol are permitted if the result doesn't depend
2184
	 on the input value.
2185
	 Otherwise, both operands must be absolute.  We already handled
2186
	 the case of addition or subtraction of a constant above.  */
2187
      frag_off = 0;
2188
      if (!(seg_left == absolute_section
2189
	       && seg_right == absolute_section)
2190
	  && !(op == O_eq || op == O_ne)
2191
	  && !((op == O_subtract
2192
		|| op == O_lt || op == O_le || op == O_ge || op == O_gt)
2193
	       && seg_left == seg_right
2194
	       && (finalize_syms
2195
		   || frag_offset_fixed_p (frag_left, frag_right, &frag_off))
2196
	       && (seg_left != reg_section || left == right)
2197
	       && (seg_left != undefined_section || add_symbol == op_symbol)))
2198
	{
2199
	  if ((seg_left == absolute_section && left == 0)
2200
	      || (seg_right == absolute_section && right == 0))
2201
	    {
2202
	      if (op == O_bit_exclusive_or || op == O_bit_inclusive_or)
2203
		{
2204
		  if (!(seg_right == absolute_section && right == 0))
2205
		    {
2206
		      seg_left = seg_right;
2207
		      left = right;
2208
		      add_symbol = op_symbol;
2209
		      orig_add_symbol = expressionP->X_op_symbol;
2210
		    }
2211
		  op = O_symbol;
2212
		  break;
2213
		}
2214
	      else if (op == O_left_shift || op == O_right_shift)
2215
		{
2216
		  if (!(seg_left == absolute_section && left == 0))
2217
		    {
2218
		      op = O_symbol;
2219
		      break;
2220
		    }
2221
		}
2222
	      else if (op != O_multiply
2223
		       && op != O_bit_or_not && op != O_bit_and)
2224
	        return 0;
2225
	    }
2226
	  else if (op == O_multiply
2227
		   && seg_left == absolute_section && left == 1)
2228
	    {
2229
	      seg_left = seg_right;
2230
	      left = right;
2231
	      add_symbol = op_symbol;
2232
	      orig_add_symbol = expressionP->X_op_symbol;
2233
	      op = O_symbol;
2234
	      break;
2235
	    }
2236
	  else if ((op == O_multiply || op == O_divide)
2237
		   && seg_right == absolute_section && right == 1)
2238
	    {
2239
	      op = O_symbol;
2240
	      break;
2241
	    }
2242
	  else if (!(left == right
2243
		     && ((seg_left == reg_section && seg_right == reg_section)
2244
			 || (seg_left == undefined_section
2245
			     && seg_right == undefined_section
2246
			     && add_symbol == op_symbol))))
2247
	    return 0;
2248
	  else if (op == O_bit_and || op == O_bit_inclusive_or)
2249
	    {
2250
	      op = O_symbol;
2251
	      break;
2252
	    }
2253
	  else if (op != O_bit_exclusive_or && op != O_bit_or_not)
2254
	    return 0;
2255
	}
2256
 
2257
      right += frag_off / OCTETS_PER_BYTE;
2258
      switch (op)
2259
	{
2260
	case O_add:			left += right; break;
2261
	case O_subtract:		left -= right; break;
2262
	case O_multiply:		left *= right; break;
2263
	case O_divide:
2264
	  if (right == 0)
2265
	    return 0;
2266
	  left = (offsetT) left / (offsetT) right;
2267
	  break;
2268
	case O_modulus:
2269
	  if (right == 0)
2270
	    return 0;
2271
	  left = (offsetT) left % (offsetT) right;
2272
	  break;
2273
	case O_left_shift:		left <<= right; break;
2274
	case O_right_shift:		left >>= right; break;
2275
	case O_bit_inclusive_or:	left |= right; break;
2276
	case O_bit_or_not:		left |= ~right; break;
2277
	case O_bit_exclusive_or:	left ^= right; break;
2278
	case O_bit_and:			left &= right; break;
2279
	case O_eq:
2280
	case O_ne:
2281
	  left = (left == right
2282
		  && seg_left == seg_right
2283
		  && (finalize_syms || frag_left == frag_right)
2284
		  && (seg_left != undefined_section
2285
		      || add_symbol == op_symbol)
2286
		  ? ~ (valueT) 0 : 0);
2287
	  if (op == O_ne)
2288
	    left = ~left;
2289
	  break;
2290
	case O_lt:
2291
	  left = (offsetT) left <  (offsetT) right ? ~ (valueT) 0 : 0;
2292
	  break;
2293
	case O_le:
2294
	  left = (offsetT) left <= (offsetT) right ? ~ (valueT) 0 : 0;
2295
	  break;
2296
	case O_ge:
2297
	  left = (offsetT) left >= (offsetT) right ? ~ (valueT) 0 : 0;
2298
	  break;
2299
	case O_gt:
2300
	  left = (offsetT) left >  (offsetT) right ? ~ (valueT) 0 : 0;
2301
	  break;
2302
	case O_logical_and:	left = left && right; break;
2303
	case O_logical_or:	left = left || right; break;
2304
	default:		abort ();
2305
	}
2306
 
2307
      op = O_constant;
2308
      break;
2309
    }
2310
 
2311
  if (op == O_symbol)
2312
    {
2313
      if (seg_left == absolute_section)
2314
	op = O_constant;
2315
      else if (seg_left == reg_section && final_val == 0)
2316
	op = O_register;
2317
      else if (!symbol_same_p (add_symbol, orig_add_symbol))
2318
	final_val += left;
2319
      expressionP->X_add_symbol = add_symbol;
2320
    }
2321
  expressionP->X_op = op;
2322
 
2323
  if (op == O_constant || op == O_register)
2324
    final_val += left;
2325
  expressionP->X_add_number = final_val;
2326
 
2327
  return 1;
2328
}
2329
 
2330
/* This lives here because it belongs equally in expr.c & read.c.
2331
   expr.c is just a branch office read.c anyway, and putting it
2332
   here lessens the crowd at read.c.
2333
 
2334
   Assume input_line_pointer is at start of symbol name.
2335
   Advance input_line_pointer past symbol name.
2336
   Turn that character into a '\0', returning its former value.
2337
   This allows a string compare (RMS wants symbol names to be strings)
2338
   of the symbol name.
2339
   There will always be a char following symbol name, because all good
2340
   lines end in end-of-line.  */
2341
 
2342
char
2343
get_symbol_end (void)
2344
{
2345
  char c;
2346
 
2347
  /* We accept \001 in a name in case this is being called with a
2348
     constructed string.  */
2349
  if (is_name_beginner (c = *input_line_pointer++) || c == '\001')
2350
    {
2351
      while (is_part_of_name (c = *input_line_pointer++)
2352
	     || c == '\001')
2353
	;
2354
      if (is_name_ender (c))
2355
	c = *input_line_pointer++;
2356
    }
2357
  *--input_line_pointer = 0;
2358
  return (c);
2359
}
2360
 
2361
unsigned int
2362
get_single_number (void)
2363
{
2364
  expressionS exp;
2365
  operand (&exp, expr_normal);
2366
  return exp.X_add_number;
2367
}