Home | History | Annotate | Line # | Download | only in gas
      1   1.1  christos /* expr.h -> header file for expr.c
      2  1.10  christos    Copyright (C) 1987-2025 Free Software Foundation, Inc.
      3   1.1  christos 
      4   1.1  christos    This file is part of GAS, the GNU Assembler.
      5   1.1  christos 
      6   1.1  christos    GAS is free software; you can redistribute it and/or modify
      7   1.1  christos    it under the terms of the GNU General Public License as published by
      8   1.1  christos    the Free Software Foundation; either version 3, or (at your option)
      9   1.1  christos    any later version.
     10   1.1  christos 
     11   1.1  christos    GAS is distributed in the hope that it will be useful,
     12   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14   1.1  christos    GNU General Public License for more details.
     15   1.1  christos 
     16   1.1  christos    You should have received a copy of the GNU General Public License
     17   1.1  christos    along with GAS; see the file COPYING.  If not, write to the Free
     18   1.1  christos    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
     19   1.1  christos    02110-1301, USA.  */
     20   1.1  christos 
     21   1.8  christos /* By popular demand, we define a struct to represent an expression.
     22   1.8  christos    This will no doubt mutate as expressions become baroque.
     23   1.8  christos 
     24   1.8  christos    Currently, we support expressions like "foo OP bar + 42".  In other
     25   1.8  christos    words we permit a (possibly undefined) symbol, a (possibly
     26   1.8  christos    undefined) symbol and the operation used to combine the symbols,
     27   1.8  christos    and an (absolute) augend.  RMS says this is so we can have 1-pass
     28   1.8  christos    assembly for any compiler emissions, and a 'case' statement might
     29   1.8  christos    emit 'undefined1 - undefined2'.
     30   1.8  christos 
     31   1.8  christos    The type of an expression used to be stored as a segment.  That got
     32   1.8  christos    confusing because it overloaded the concept of a segment.  I added
     33   1.8  christos    an operator field, instead.  */
     34   1.1  christos 
     35   1.1  christos /* This is the type of an expression.  The operator types are also
     36   1.1  christos    used while parsing an expression.
     37   1.1  christos 
     38   1.1  christos    NOTE: This enumeration must match the op_rank array in expr.c.  */
     39   1.1  christos 
     40   1.8  christos typedef enum
     41   1.8  christos {
     42   1.1  christos   /* An illegal expression.  */
     43   1.1  christos   O_illegal,
     44   1.1  christos   /* A nonexistent expression.  */
     45   1.1  christos   O_absent,
     46   1.1  christos   /* X_add_number (a constant expression).  */
     47   1.1  christos   O_constant,
     48   1.1  christos   /* X_add_symbol + X_add_number.  */
     49   1.1  christos   O_symbol,
     50   1.1  christos   /* X_add_symbol + X_add_number - the base address of the image.  */
     51   1.1  christos   O_symbol_rva,
     52   1.8  christos   /* The section index of X_add_symbol.  */
     53   1.8  christos   O_secidx,
     54   1.1  christos   /* A register (X_add_number is register number).  */
     55   1.1  christos   O_register,
     56   1.1  christos   /* A big value.  If X_add_number is negative or 0, the value is in
     57   1.1  christos      generic_floating_point_number.  Otherwise the value is in
     58   1.1  christos      generic_bignum, and X_add_number is the number of LITTLENUMs in
     59   1.1  christos      the value.  */
     60   1.1  christos   O_big,
     61   1.1  christos   /* (- X_add_symbol) + X_add_number.  */
     62   1.1  christos   O_uminus,
     63   1.1  christos   /* (~ X_add_symbol) + X_add_number.  */
     64   1.1  christos   O_bit_not,
     65   1.1  christos   /* (! X_add_symbol) + X_add_number.  */
     66   1.1  christos   O_logical_not,
     67   1.1  christos   /* (X_add_symbol * X_op_symbol) + X_add_number.  */
     68   1.1  christos   O_multiply,
     69   1.1  christos   /* (X_add_symbol / X_op_symbol) + X_add_number.  */
     70   1.1  christos   O_divide,
     71   1.1  christos   /* (X_add_symbol % X_op_symbol) + X_add_number.  */
     72   1.1  christos   O_modulus,
     73   1.1  christos   /* (X_add_symbol << X_op_symbol) + X_add_number.  */
     74   1.1  christos   O_left_shift,
     75   1.1  christos   /* (X_add_symbol >> X_op_symbol) + X_add_number.  */
     76   1.1  christos   O_right_shift,
     77   1.1  christos   /* (X_add_symbol | X_op_symbol) + X_add_number.  */
     78   1.1  christos   O_bit_inclusive_or,
     79   1.1  christos   /* (X_add_symbol |~ X_op_symbol) + X_add_number.  */
     80   1.1  christos   O_bit_or_not,
     81   1.1  christos   /* (X_add_symbol ^ X_op_symbol) + X_add_number.  */
     82   1.1  christos   O_bit_exclusive_or,
     83   1.1  christos   /* (X_add_symbol & X_op_symbol) + X_add_number.  */
     84   1.1  christos   O_bit_and,
     85   1.1  christos   /* (X_add_symbol + X_op_symbol) + X_add_number.  */
     86   1.1  christos   O_add,
     87   1.1  christos   /* (X_add_symbol - X_op_symbol) + X_add_number.  */
     88   1.1  christos   O_subtract,
     89   1.1  christos   /* (X_add_symbol == X_op_symbol) + X_add_number.  */
     90   1.1  christos   O_eq,
     91   1.1  christos   /* (X_add_symbol != X_op_symbol) + X_add_number.  */
     92   1.1  christos   O_ne,
     93   1.1  christos   /* (X_add_symbol < X_op_symbol) + X_add_number.  */
     94   1.1  christos   O_lt,
     95   1.1  christos   /* (X_add_symbol <= X_op_symbol) + X_add_number.  */
     96   1.1  christos   O_le,
     97   1.1  christos   /* (X_add_symbol >= X_op_symbol) + X_add_number.  */
     98   1.1  christos   O_ge,
     99   1.1  christos   /* (X_add_symbol > X_op_symbol) + X_add_number.  */
    100   1.1  christos   O_gt,
    101   1.1  christos   /* (X_add_symbol && X_op_symbol) + X_add_number.  */
    102   1.1  christos   O_logical_and,
    103   1.1  christos   /* (X_add_symbol || X_op_symbol) + X_add_number.  */
    104   1.1  christos   O_logical_or,
    105   1.1  christos   /* X_op_symbol [ X_add_symbol ] */
    106   1.1  christos   O_index,
    107   1.1  christos   /* machine dependent operators */
    108   1.1  christos   O_md1,  O_md2,  O_md3,  O_md4,  O_md5,  O_md6,  O_md7,  O_md8,
    109   1.1  christos   O_md9,  O_md10, O_md11, O_md12, O_md13, O_md14, O_md15, O_md16,
    110   1.1  christos   O_md17, O_md18, O_md19, O_md20, O_md21, O_md22, O_md23, O_md24,
    111   1.1  christos   O_md25, O_md26, O_md27, O_md28, O_md29, O_md30, O_md31, O_md32,
    112   1.1  christos   /* this must be the largest value */
    113   1.1  christos   O_max
    114   1.1  christos } operatorT;
    115   1.1  christos 
    116   1.8  christos typedef struct expressionS
    117   1.8  christos {
    118   1.1  christos   /* The main symbol.  */
    119   1.1  christos   symbolS *X_add_symbol;
    120   1.1  christos   /* The second symbol, if needed.  */
    121   1.1  christos   symbolS *X_op_symbol;
    122   1.1  christos   /* A number to add.  */
    123   1.1  christos   offsetT X_add_number;
    124   1.1  christos 
    125   1.1  christos   /* The type of the expression.  We can't assume that an arbitrary
    126   1.1  christos      compiler can handle a bitfield of enum type.  FIXME: We could
    127   1.1  christos      check this using autoconf.  */
    128   1.1  christos #ifdef __GNUC__
    129   1.1  christos   operatorT X_op : 8;
    130   1.1  christos #else
    131   1.1  christos   unsigned char X_op;
    132   1.1  christos #endif
    133   1.1  christos 
    134  1.10  christos   /* Non-zero if the expression value should be regarded as unsigned.  This is
    135  1.10  christos      only valid for
    136  1.10  christos      - O_constant expressions, where it is only used when an O_constant must be
    137  1.10  christos        extended into a bignum (i.e., it is not used when performing arithmetic
    138  1.10  christos        on these values),
    139  1.10  christos      - O_big integer expressions, i.e. when X_add_number is positive.
    140   1.1  christos      FIXME: This field is not set very reliably.  */
    141   1.1  christos   unsigned int X_unsigned : 1;
    142   1.3  christos   /* This is used to implement "word size + 1 bit" arithmetic, so that e.g.
    143   1.3  christos      expressions used with .sleb128 directives can use the full range available
    144   1.3  christos      for an unsigned word, but can also properly represent all values of a
    145   1.3  christos      signed word.  */
    146   1.3  christos   unsigned int X_extrabit : 1;
    147   1.1  christos 
    148   1.1  christos   /* 7 additional bits can be defined if needed.  */
    149   1.1  christos 
    150   1.1  christos   /* Machine dependent field */
    151   1.1  christos   unsigned short X_md;
    152   1.1  christos } expressionS;
    153   1.1  christos 
    154   1.1  christos enum expr_mode
    155   1.1  christos {
    156   1.1  christos   expr_evaluate,
    157   1.1  christos   expr_normal,
    158  1.10  christos   expr_defer,
    159  1.10  christos   expr_defer_incl_dot,
    160   1.1  christos };
    161   1.1  christos 
    162  1.10  christos #define expr_defer_p(m) ((m) >= expr_defer)
    163  1.10  christos 
    164   1.1  christos /* "result" should be type (expressionS *).  */
    165   1.1  christos #define expression(result) expr (0, result, expr_normal)
    166   1.1  christos #define expression_and_evaluate(result) expr (0, result, expr_evaluate)
    167   1.1  christos #define deferred_expression(result) expr (0, result, expr_defer)
    168   1.1  christos 
    169   1.1  christos /* If an expression is O_big, look here for its value. These common
    170   1.1  christos    data may be clobbered whenever expr() is called.  */
    171   1.1  christos /* Flonums returned here.  Big enough to hold most precise flonum.  */
    172   1.1  christos extern FLONUM_TYPE generic_floating_point_number;
    173   1.1  christos /* Bignums returned here.  */
    174   1.1  christos extern LITTLENUM_TYPE generic_bignum[];
    175   1.1  christos /* Number of littlenums in above.  */
    176   1.1  christos #define SIZE_OF_LARGE_NUMBER (20)
    177   1.1  christos 
    178   1.1  christos typedef char operator_rankT;
    179   1.1  christos 
    180   1.3  christos extern char get_symbol_name (char **);
    181   1.3  christos extern char restore_line_pointer (char);
    182   1.1  christos extern void expr_begin (void);
    183   1.9  christos extern void expr_end (void);
    184   1.1  christos extern void expr_set_precedence (void);
    185   1.1  christos extern void expr_set_rank (operatorT, operator_rankT);
    186   1.3  christos extern void add_to_result (expressionS *, offsetT, int);
    187   1.3  christos extern void subtract_from_result (expressionS *, offsetT, int);
    188   1.1  christos extern segT expr (int, expressionS *, enum expr_mode);
    189   1.1  christos extern unsigned int get_single_number (void);
    190   1.9  christos extern symbolS *make_expr_symbol (const expressionS * expressionP);
    191   1.5  christos extern int expr_symbol_where (symbolS *, const char **, unsigned int *);
    192  1.10  christos extern void current_location (expressionS *, enum expr_mode);
    193   1.1  christos extern symbolS *expr_build_uconstant (offsetT);
    194   1.1  christos extern symbolS *expr_build_dot (void);
    195   1.8  christos extern uint32_t generic_bignum_to_int32 (void);
    196   1.8  christos extern uint64_t generic_bignum_to_int64 (void);
    197   1.8  christos extern int resolve_expression (expressionS *);
    198   1.9  christos extern void resolve_register (expressionS *);
    199   1.1  christos 
    200   1.8  christos extern bool literal_prefix_dollar_hex;
    201