expr.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 1,924 行 · 第 1/4 页
C
1,924 行
/* expr.c -operands, expressions- Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 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 2, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */#include <ctype.h>#include <string.h>#define min(a, b) ((a) < (b) ? (a) : (b))#include "as.h"#include "obstack.h"static void floating_constant PARAMS ((expressionS * expressionP));static valueT generic_bignum_to_int32 PARAMS ((void));#ifdef BFD64static valueT generic_bignum_to_int64 PARAMS ((void));#endifstatic void integer_constant PARAMS ((int radix, expressionS * expressionP));static void mri_char_constant PARAMS ((expressionS *));static void current_location PARAMS ((expressionS *));static void clean_up_expression PARAMS ((expressionS * expressionP));static segT operand PARAMS ((expressionS *));static operatorT operator PARAMS ((int *));extern const char EXP_CHARS[], FLT_CHARS[];/* 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; char *file; unsigned int line;};static struct expr_symbol_line *expr_symbol_lines;/* 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 (expressionP) expressionS *expressionP;{ expressionS zero; const char *fake; 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; zero assumed")); else as_bad (_("floating point number invalid; zero assumed")); zero.X_op = O_constant; zero.X_add_number = 0; zero.X_unsigned = 0; clean_up_expression (&zero); expressionP = &zero; } fake = FAKE_LABEL_NAME; /* 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, (expressionP->X_op == O_constant ? absolute_section : expr_section), 0, &zero_address_frag); symbol_set_value_expression (symbolP, expressionP); if (expressionP->X_op == O_constant) resolve_symbol_value (symbolP, 1); n = (struct expr_symbol_line *) xmalloc (sizeof *n); n->sym = symbolP; as_where (&n->file, &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 (sym, pfile, pline) symbolS *sym; char **pfile; unsigned int *pline;{ register 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;}/* 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 (value) offsetT value;{ expressionS e; e.X_op = O_constant; e.X_add_number = value; e.X_unsigned = 1; return make_expr_symbol (&e);}/* Build an expression for OP s1. */symbolS *expr_build_unary (op, s1) operatorT op; symbolS *s1;{ expressionS e; e.X_op = op; e.X_add_symbol = s1; e.X_add_number = 0; return make_expr_symbol (&e);}/* Build an expression for s1 OP s2. */symbolS *expr_build_binary (op, s1, s2) operatorT op; symbolS *s1; symbolS *s2;{ expressionS e; e.X_op = op; e.X_add_symbol = s1; e.X_op_symbol = s2; e.X_add_number = 0; return make_expr_symbol (&e);}/* Build an expression for the current location ('.'). */symbolS *expr_build_dot (){ expressionS e; current_location (&e); 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. */};/* If nonzero, we've been asked to assemble nan, +inf or -inf. */int generic_floating_point_magic;static voidfloating_constant (expressionP) 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, probably assembling junk")); } 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;}static valueTgeneric_bignum_to_int32 (){ valueT number = ((generic_bignum[1] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS) | (generic_bignum[0] & LITTLENUM_MASK); number &= 0xffffffff; return number;}#ifdef BFD64static valueTgeneric_bignum_to_int64 (){ valueT number = ((((((((valueT) generic_bignum[3] & LITTLENUM_MASK) << LITTLENUM_NUMBER_OF_BITS) | ((valueT) generic_bignum[2] & LITTLENUM_MASK)) << LITTLENUM_NUMBER_OF_BITS) | ((valueT) generic_bignum[1] & LITTLENUM_MASK)) << LITTLENUM_NUMBER_OF_BITS) | ((valueT) generic_bignum[0] & LITTLENUM_MASK)); return number;}#endifstatic voidinteger_constant (radix, expressionP) 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. */ short int maxdig = 0; /* Highest permitted digit value. */ int too_many_digits = 0; /* If we see >= this number of. */ char *name; /* Points to name of symbol. */ symbolS *symbolP; /* Points to symbol. */ int small; /* True if fits in 32 bits. */ /* 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 ((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 ((unsigned char) *suffix); suffix++) { if (*suffix == 'e' || *suffix == 'E') flt = 1; } if (suffix == input_line_pointer) { radix = 10; suffix = NULL; } else { c = *--suffix; if (islower ((unsigned char) c)) c = toupper (c); if (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: maxdig = 2; too_many_digits = valuesize + 1; break; case 8: maxdig = radix = 8; too_many_digits = (valuesize + 2) / 3 + 1; break; case 16: maxdig = radix = 16; too_many_digits = (valuesize + 3) / 4 + 1; break; case 10: maxdig = radix = 10; too_many_digits = (valuesize + 11) / 4; /* Very rough. */ }#undef valuesize start = input_line_pointer; c = *input_line_pointer++; for (number = 0; (digit = hex_value (c)) < maxdig; 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)) < maxdig; 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; 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; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?