Wolfgang Thallergcc16 merge934c3885 months agoHistory
2606 lines · 65.9 KB · C
RawDownload
/* expr.c -operands, expressions-   Copyright (C) 1987-2026 Free Software Foundation, Inc.    This file is part of GAS, the GNU Assembler.    GAS is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 3, or (at your option)   any later version.    GAS is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   GNU General Public License for more details.    You should have received a copy of the GNU General Public License   along with GAS; see the file COPYING.  If not, write to the Free   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA   02110-1301, USA.  */ /* This is really a branch office of as-read.c. I split it out to clearly   distinguish the world of expressions from the world of statements.   (It also gives smaller files to re-compile.)   Here, "operand"s are of expressions, not instructions.  */ #define min(a, b)       ((a) < (b) ? (a) : (b)) #include "as.h"#include "safe-ctype.h" #include <limits.h>#ifndef CHAR_BIT#define CHAR_BIT 8#endif bool literal_prefix_dollar_hex = false; /* We keep a mapping of expression symbols to file positions, so that   we can provide better error messages.  */ struct expr_symbol_line {  struct expr_symbol_line *next;  symbolS *sym;  const char *file;  unsigned int line;}; static struct expr_symbol_line *expr_symbol_lines; static const expressionS zero = { .X_op = O_constant };/* Build a dummy symbol to hold a complex expression.  This is how we   build expressions up out of other expressions.  The symbol is put   into the fake section expr_section.  */ symbolS *make_expr_symbol (const expressionS *expressionP){  symbolS *symbolP;  struct expr_symbol_line *n;   if (expressionP->X_op == O_symbol      && expressionP->X_add_number == 0)    return expressionP->X_add_symbol;   if (expressionP->X_op == O_big)    {      /* This won't work, because the actual value is stored in	 generic_floating_point_number or generic_bignum, and we are	 going to lose it if we haven't already.  */      if (expressionP->X_add_number > 0)	as_bad (_("bignum invalid"));      else	as_bad (_("floating point number invalid"));      expressionP = &zero;    }   /* Putting constant symbols in absolute_section rather than     expr_section is convenient for the old a.out code, for which     S_GET_SEGMENT does not always retrieve the value put in by     S_SET_SEGMENT.  */  symbolP = symbol_create (FAKE_LABEL_NAME,			   (expressionP->X_op == O_constant			    ? absolute_section			    : expressionP->X_op == O_register			      ? reg_section			      : expr_section),			   &zero_address_frag, 0);  symbol_set_value_expression (symbolP, expressionP);   if (expressionP->X_op == O_constant)    resolve_symbol_value (symbolP);   n = notes_alloc (sizeof (*n));  n->sym = symbolP;  n->file = as_where (&n->line);  n->next = expr_symbol_lines;  expr_symbol_lines = n;   return symbolP;} /* Return the file and line number for an expr symbol.  Return   non-zero if something was found, 0 if no information is known for   the symbol.  */ intexpr_symbol_where (symbolS *sym, const char **pfile, unsigned int *pline){  struct expr_symbol_line *l;   for (l = expr_symbol_lines; l != NULL; l = l->next)    {      if (l->sym == sym)	{	  *pfile = l->file;	  *pline = l->line;	  return 1;	}    }   return 0;} /* Look up a previously used .startof. / .sizeof. symbol, or make a fresh   one.  */static symbolS **seen[2];static unsigned int nr_seen[2]; static symbolS *symbol_lookup_or_make (const char *name, bool start){  char *buf = concat (start ? ".startof." : ".sizeof.", name, (char *) NULL);  symbolS *symbolP;  unsigned int i;   for (i = 0; i < nr_seen[start]; ++i)    {    symbolP = seen[start][i];     if (! symbolP)      break;     name = S_GET_NAME (symbolP);    if ((symbols_case_sensitive	 ? strcmp (buf, name)	 : strcasecmp (buf, name)) == 0)      {	free (buf);	return symbolP;      }    }   symbolP = symbol_make (buf);  free (buf);   if (i >= nr_seen[start])    {      unsigned int nr = (i + 1) * 2;       seen[start] = XRESIZEVEC (symbolS *, seen[start], nr);      nr_seen[start] = nr;      memset (&seen[start][i + 1], 0, (nr - i - 1) * sizeof(seen[0][0]));    }   seen[start][i] = symbolP;   return symbolP;}/* Utilities for building expressions.   Since complex expressions are recorded as symbols for use in other   expressions these return a symbolS * and not an expressionS *.   These explicitly do not take an "add_number" argument.  *//* ??? For completeness' sake one might want expr_build_symbol.   It would just return its argument.  */ /* Build an expression for an unsigned constant.   The corresponding one for signed constants is missing because   there's currently no need for it.  One could add an unsigned_p flag   but that seems more clumsy.  */ symbolS *expr_build_uconstant (offsetT value){  expressionS e = {    .X_op = O_constant,    .X_add_number = value,    .X_unsigned = 1  };  return make_expr_symbol (&e);}/* Build any floating-point literal here.   Also build any bignum literal here.  */ /* Seems atof_machine can backscan through generic_bignum and hit whatever   happens to be loaded before it in memory.  And its way too complicated   for me to fix right.  Thus a hack.  JF:  Just make generic_bignum bigger,   and never write into the early words, thus they'll always be zero.   I hate Dean's floating-point code.  Bleh.  */LITTLENUM_TYPE generic_bignum[SIZE_OF_LARGE_NUMBER + 6]; FLONUM_TYPE generic_floating_point_number = {  &generic_bignum[6],		/* low.  (JF: Was 0)  */  &generic_bignum[SIZE_OF_LARGE_NUMBER + 6 - 1], /* high.  JF: (added +6)  */  0,				/* leader.  */  0,				/* exponent.  */  0				/* sign.  */}; static voidfloating_constant (expressionS *expressionP){  /* input_line_pointer -> floating-point constant.  */  int error_code;   error_code = atof_generic (&input_line_pointer, ".", EXP_CHARS,			     &generic_floating_point_number);   if (error_code)    {      if (error_code == ERROR_EXPONENT_OVERFLOW)	{	  as_bad (_("bad floating-point constant: exponent overflow"));	}      else	{	  as_bad (_("bad floating-point constant: unknown error code=%d"),		  error_code);	}    }  expressionP->X_op = O_big;  /* input_line_pointer -> just after constant, which may point to     whitespace.  */  expressionP->X_add_number = -1;} uint32_tgeneric_bignum_to_int32 (void){  return ((((uint32_t) generic_bignum[1] & LITTLENUM_MASK)	   << LITTLENUM_NUMBER_OF_BITS)	  | ((uint32_t) generic_bignum[0] & LITTLENUM_MASK));} uint64_tgeneric_bignum_to_int64 (void){  return ((((((((uint64_t) generic_bignum[3] & LITTLENUM_MASK)	       << LITTLENUM_NUMBER_OF_BITS)	      | ((uint64_t) generic_bignum[2] & LITTLENUM_MASK))	     << LITTLENUM_NUMBER_OF_BITS)	    | ((uint64_t) generic_bignum[1] & LITTLENUM_MASK))	   << LITTLENUM_NUMBER_OF_BITS)	  | ((uint64_t) generic_bignum[0] & LITTLENUM_MASK));} static voidinteger_constant (int radix, expressionS *expressionP){  char *start;		/* Start of number.  */  char *suffix = NULL;  char c;  valueT number;	/* Offset or (absolute) value.  */  short int digit;	/* Value of next digit in current radix.  */  int too_many_digits = 0;	/* If we see >= this number of.  */  char *name;		/* Points to name of symbol.  */  symbolS *symbolP;	/* Points to symbol.  */   bool small;		/* True if fits in 32 bits (64 bits with BFD64).  */   /* May be bignum, or may fit in 32 bits.  */  /* Most numbers fit into 32 bits, and we want this case to be fast.     so we pretend it will fit into 32 bits.  If, after making up a 32     bit number, we realise that we have scanned more digits than     comfortably fit into 32 bits, we re-scan the digits coding them     into a bignum.  For decimal and octal numbers we are     conservative: Some numbers may be assumed bignums when in fact     they do fit into 32 bits.  Numbers of any radix can have excess     leading zeros: We strive to recognise this and cast them back     into 32 bits.  We must check that the bignum really is more than     32 bits, and change it back to a 32-bit number if it fits.  The     number we are looking for is expected to be positive, but if it     fits into 32 bits as an unsigned number, we let it be a 32-bit     number.  The cavalier approach is for speed in ordinary cases.  */  /* This has been extended for 64 bits.  We blindly assume that if     you're compiling in 64-bit mode, the target is a 64-bit machine.     This should be cleaned up.  */ #ifdef BFD64#define valuesize 64#else /* includes non-bfd case, mostly */#define valuesize 32#endif   if (is_end_of_stmt (*input_line_pointer))    {      expressionP->X_op = O_absent;      return;    }   if ((NUMBERS_WITH_SUFFIX || flag_m68k_mri) && radix == 0)    {      int flt = 0;       /* In MRI mode, the number may have a suffix indicating the	 radix.  For that matter, it might actually be a floating	 point constant.  */      for (suffix = input_line_pointer; ISALNUM (*suffix); suffix++)	{	  if (*suffix == 'e' || *suffix == 'E')	    flt = 1;	}       if (suffix == input_line_pointer)	{	  radix = 10;	  suffix = NULL;	}      else	{	  c = *--suffix;	  c = TOUPPER (c);	  /* If we have both NUMBERS_WITH_SUFFIX and LOCAL_LABELS_FB,	     we distinguish between 'B' and 'b'.  This is the case for	     Z80.  */	  if ((NUMBERS_WITH_SUFFIX && LOCAL_LABELS_FB ? *suffix : c) == 'B')	    radix = 2;	  else if (c == 'D')	    radix = 10;	  else if (c == 'O' || c == 'Q')	    radix = 8;	  else if (c == 'H')	    radix = 16;	  else if (suffix[1] == '.' || c == 'E' || flt)	    {	      floating_constant (expressionP);	      return;	    }	  else	    {	      radix = 10;	      suffix = NULL;	    }	}    }   switch (radix)    {    case 2:      too_many_digits = valuesize + 1;      break;    case 8:      too_many_digits = (valuesize + 2) / 3 + 1;      break;    case 16:      too_many_digits = (valuesize + 3) / 4 + 1;      break;    case 10:      too_many_digits = (valuesize + 11) / 4; /* Very rough.  */      break;    }#undef valuesize  start = input_line_pointer;  c = *input_line_pointer++;  for (number = 0;       (digit = hex_value (c)) < radix;       c = *input_line_pointer++)    {      number = number * radix + digit;    }  /* c contains character after number.  */  /* input_line_pointer->char after c.  */  small = (input_line_pointer - start - 1) < too_many_digits;   if (radix == 16 && c == '_')    {      /* This is literal of the form 0x333_0_12345678_1.	 This example is equivalent to 0x00000333000000001234567800000001.  */       int num_little_digits = 0;      int i;      input_line_pointer = start;	/* -> 1st digit.  */       know (LITTLENUM_NUMBER_OF_BITS == 16);       for (c = '_'; c == '_'; num_little_digits += 2)	{ 	  /* Convert one 64-bit word.  */	  int ndigit = 0;	  number = 0;	  for (c = *input_line_pointer++;	       (digit = hex_value (c)) < radix;	       c = *(input_line_pointer++))	    {	      number = number * radix + digit;	      ndigit++;	    } 	  /* Check for 8 digit per word max.  */	  if (ndigit > 8)	    as_bad (_("a bignum with underscores may not have more than 8 hex digits in any word")); 	  /* Add this chunk to the bignum.	     Shift things down 2 little digits.  */	  know (LITTLENUM_NUMBER_OF_BITS == 16);	  for (i = min (num_little_digits + 1, SIZE_OF_LARGE_NUMBER - 1);	       i >= 2;	       i--)	    generic_bignum[i] = generic_bignum[i - 2]; 	  /* Add the new digits as the least significant new ones.  */	  generic_bignum[0] = number & 0xffffffff;	  generic_bignum[1] = number >> 16;	}       /* Again, c is char after number, input_line_pointer->after c.  */       if (num_little_digits > SIZE_OF_LARGE_NUMBER - 1)	num_little_digits = SIZE_OF_LARGE_NUMBER - 1;       gas_assert (num_little_digits >= 4);       if (num_little_digits != 8)	as_bad (_("a bignum with underscores must have exactly 4 words"));       /* We might have some leading zeros.  These can be trimmed to give	 us a change to fit this constant into a small number.  */      while (generic_bignum[num_little_digits - 1] == 0	     && num_little_digits > 1)	num_little_digits--;       if (num_little_digits <= 2)	{	  /* will fit into 32 bits.  */	  number = generic_bignum_to_int32 ();	  small = 1;	}#ifdef BFD64      else if (num_little_digits <= 4)	{	  /* Will fit into 64 bits.  */	  number = generic_bignum_to_int64 ();	  small = 1;	}#endif      else	{	  small = 0; 	  /* Number of littlenums in the bignum.  */	  number = num_little_digits;	}    }  else if (!small)    {      /* We saw a lot of digits. manufacture a bignum the hard way.  */      LITTLENUM_TYPE *leader;	/* -> high order littlenum of the bignum.  */      LITTLENUM_TYPE *pointer;	/* -> littlenum we are frobbing now.  */      long carry;       leader = generic_bignum;      generic_bignum[0] = 0;      generic_bignum[1] = 0;      generic_bignum[2] = 0;      generic_bignum[3] = 0;      input_line_pointer = start;	/* -> 1st digit.  */      c = *input_line_pointer++;      for (; (carry = hex_value (c)) < radix; c = *input_line_pointer++)	{	  for (pointer = generic_bignum; pointer <= leader; pointer++)	    {	      long work; 	      work = carry + radix * *pointer;	      *pointer = work & LITTLENUM_MASK;	      carry = work >> LITTLENUM_NUMBER_OF_BITS;	    }	  if (carry)	    {	      if (leader < generic_bignum + SIZE_OF_LARGE_NUMBER - 1)		{		  /* Room to grow a longer bignum.  */		  *++leader = carry;		}	    }	}      /* Again, c is char after number.  */      /* input_line_pointer -> after c.  */      know (LITTLENUM_NUMBER_OF_BITS == 16);      if (leader < generic_bignum + 2)	{	  /* Will fit into 32 bits.  */	  number = generic_bignum_to_int32 ();	  small = 1;	}#ifdef BFD64

Lines 1–500 of 2606.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t