From: Dmitry Selyutin Date: Tue, 12 Jul 2022 09:24:54 +0000 (+0300) Subject: ppc: support register names in macros X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=31701d310797478c8652dc638a6ca66b08f34f7b;p=binutils-gdb.git ppc: support register names in macros This patch is a follow-up of discussion: https://sourceware.org/pipermail/binutils/2022-July/121719.html Form now on, PPC assembly becomes capable of compiling such code: .set VREG, %r0 .set REG, VREG extsw REG, 2 extsw %r1, 1 The custom register_name() code path, as well as checks for CRs, are deprecated; from now on, we use md_operand() routine everywhere. Whilst we're here, let's also reuse register lookup for CFI. --- diff --git a/gas/config/tc-ppc.c b/gas/config/tc-ppc.c index 5015777d600..44f3ac292fd 100644 --- a/gas/config/tc-ppc.c +++ b/gas/config/tc-ppc.c @@ -317,7 +317,7 @@ struct pd_reg The table is sorted. Suitable for searching by a binary search. */ -static const struct pd_reg pre_defined_registers[] = +static const struct pd_reg reg_names[] = { /* VSX accumulators. */ { "a0", 0, PPC_OPERAND_ACC }, @@ -784,8 +784,25 @@ static const struct pd_reg pre_defined_registers[] = { "xer", 1, PPC_OPERAND_SPR } }; +#define REG_NAME_CNT (sizeof (reg_names) / sizeof (struct pd_reg)) -#define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct pd_reg)) +static const struct pd_reg cr_names[] = +{ + { "cr0", 0, PPC_OPERAND_CR_REG }, + { "cr1", 1, PPC_OPERAND_CR_REG }, + { "cr2", 2, PPC_OPERAND_CR_REG }, + { "cr3", 3, PPC_OPERAND_CR_REG }, + { "cr4", 4, PPC_OPERAND_CR_REG }, + { "cr5", 5, PPC_OPERAND_CR_REG }, + { "cr6", 6, PPC_OPERAND_CR_REG }, + { "cr7", 7, PPC_OPERAND_CR_REG }, + { "eq", 2, PPC_OPERAND_CR_BIT }, + { "gt", 1, PPC_OPERAND_CR_BIT }, + { "lt", 0, PPC_OPERAND_CR_BIT }, + { "so", 3, PPC_OPERAND_CR_BIT }, + { "un", 3, PPC_OPERAND_CR_BIT } +}; +#define CR_NAME_CNT (sizeof (cr_names) / sizeof (struct pd_reg)) /* Given NAME, find the register number associated with that name, return the integer value associated with the given name or -1 on failure. */ @@ -815,106 +832,68 @@ reg_name_search (const struct pd_reg *regs, int regcount, const char *name) return NULL; } -/* - * Summary of register_name. - * - * in: Input_line_pointer points to 1st char of operand. - * - * out: A expressionS. - * The operand may have been a register: in this case, X_op == O_register, - * X_add_number is set to the register number, and truth is returned. - * Input_line_pointer->(next non-blank) char after operand, or is in its - * original state. - */ + +static bool cr_operand; -static bool -register_name (expressionS *expressionP) +int +ppc_parse_name (const char *name, expressionS *exp) { + symbolS *symbol; const struct pd_reg *reg; - char *name; - char *start; - char c; /* Find the spelling of the operand. */ - start = name = input_line_pointer; if (name[0] == '%' && ISALPHA (name[1])) - name = ++input_line_pointer; - - else if (!reg_names_p || !ISALPHA (name[0])) - return false; - - c = get_symbol_name (&name); - reg = reg_name_search (pre_defined_registers, REG_NAME_CNT, name); - - /* Put back the delimiting char. */ - *input_line_pointer = c; + ++name; + else if (!cr_operand && (!reg_names_p || !ISALPHA (name[0]))) + return 0; /* Look to see if it's in the register table. */ + if (cr_operand) + reg = reg_name_search (cr_names, CR_NAME_CNT, name); + else + reg = reg_name_search (reg_names, REG_NAME_CNT, name); if (reg != NULL) { - expressionP->X_op = O_register; - expressionP->X_add_number = reg->value; - expressionP->X_md = reg->flags; + exp->X_op = O_register; + exp->X_add_number = reg->value; + exp->X_md = reg->flags; - /* Make the rest nice. */ - expressionP->X_add_symbol = NULL; - expressionP->X_op_symbol = NULL; - return true; - } + if (! cr_operand) + { + /* Make the rest nice. */ + exp->X_add_symbol = NULL; + exp->X_op_symbol = NULL; + } - /* Reset the line as if we had not done anything. */ - input_line_pointer = start; - return false; -} - -/* This function is called for each symbol seen in an expression. It - handles the special parsing which PowerPC assemblers are supposed - to use for condition codes. */ + return 1; + } -/* Whether to do the special parsing. */ -static bool cr_operand; + /* Look to see if it's a macro. */ + symbol = symbol_find_or_make (name); + if (symbol != NULL) + { + expressionS symbol_exp = *symbol_get_value_expression (symbol); -/* Names to recognize in a condition code. This table is sorted. */ -static const struct pd_reg cr_names[] = -{ - { "cr0", 0, PPC_OPERAND_CR_REG }, - { "cr1", 1, PPC_OPERAND_CR_REG }, - { "cr2", 2, PPC_OPERAND_CR_REG }, - { "cr3", 3, PPC_OPERAND_CR_REG }, - { "cr4", 4, PPC_OPERAND_CR_REG }, - { "cr5", 5, PPC_OPERAND_CR_REG }, - { "cr6", 6, PPC_OPERAND_CR_REG }, - { "cr7", 7, PPC_OPERAND_CR_REG }, - { "eq", 2, PPC_OPERAND_CR_BIT }, - { "gt", 1, PPC_OPERAND_CR_BIT }, - { "lt", 0, PPC_OPERAND_CR_BIT }, - { "so", 3, PPC_OPERAND_CR_BIT }, - { "un", 3, PPC_OPERAND_CR_BIT } -}; + if ((symbol_exp.X_op == O_register) + || (symbol_exp.X_op == O_constant)) + { + *exp = symbol_exp; + return 1; + } + } -/* Parsing function. This returns non-zero if it recognized an - expression. */ + return 0; +} -int -ppc_parse_name (const char *name, expressionS *exp) +void +ppc_operand (expressionS *exp) { - const struct pd_reg *reg; - - if (! cr_operand) - return 0; - - if (*name == '%') - ++name; - reg = reg_name_search (cr_names, sizeof cr_names / sizeof cr_names[0], - name); - if (reg == NULL) - return 0; - - exp->X_op = O_register; - exp->X_add_number = reg->value; - exp->X_md = reg->flags; + char endc; + char *name = input_line_pointer; - return 1; + endc = get_symbol_name (&name); + if (!ppc_parse_name (name, exp)) + restore_line_pointer (endc); } /* Propagate X_md and check register expressions. This is to support @@ -3274,7 +3253,6 @@ parse_tls_arg (char **str, const expressionS *exp, struct ppc_fixup *tls_fix) #endif /* This routine is called for each instruction to be assembled. */ - void md_assemble (char *str) { @@ -3437,25 +3415,11 @@ md_assemble (char *str) /* Gather the operand. */ hold = input_line_pointer; input_line_pointer = str; - - if ((reg_names_p - && (((operand->flags & PPC_OPERAND_CR_BIT) != 0) - || ((operand->flags & PPC_OPERAND_CR_REG) != 0))) - || !register_name (&ex)) - { - char save_lex = lex_type['%']; - - if (((operand->flags & PPC_OPERAND_CR_REG) != 0) - || (operand->flags & PPC_OPERAND_CR_BIT) != 0) - { - cr_operand = true; - lex_type['%'] |= LEX_BEGIN_NAME; - } - expression (&ex); - cr_operand = false; - lex_type['%'] = save_lex; - } - + memset (&ex, 0, sizeof (expressionS)); + if (operand->flags & (PPC_OPERAND_CR_BIT | PPC_OPERAND_CR_REG)) + cr_operand = true; + expression (&ex); + cr_operand = false; str = input_line_pointer; input_line_pointer = hold; @@ -7734,10 +7698,8 @@ ppc_cfi_frame_initial_instructions (void) int tc_ppc_regname_to_dw2regnum (char *regname) { - unsigned int regnum = -1; unsigned int i; - const char *p; - char *q; + const struct pd_reg *reg; static struct { const char *name; int dw2regnum; } regnames[] = { { "sp", 1 }, { "r.sp", 1 }, { "rtoc", 2 }, { "r.toc", 2 }, @@ -7750,23 +7712,15 @@ tc_ppc_regname_to_dw2regnum (char *regname) if (strcmp (regnames[i].name, regname) == 0) return regnames[i].dw2regnum; - if (regname[0] == 'r' || regname[0] == 'f' || regname[0] == 'v') - { - p = regname + 1 + (regname[1] == '.'); - regnum = strtoul (p, &q, 10); - if (p == q || *q || regnum >= 32) - return -1; - if (regname[0] == 'f') - regnum += 32; - else if (regname[0] == 'v') - regnum += 77; - } - else if (regname[0] == 'c' && regname[1] == 'r') - { - p = regname + 2 + (regname[2] == '.'); - if (p[0] < '0' || p[0] > '7' || p[1]) - return -1; - regnum = p[0] - '0' + 68; - } - return regnum; + if (regname[0] == '%' && ISALPHA (regname[1])) + ++regname; + + reg = reg_name_search (reg_names, REG_NAME_CNT, regname); + if (reg == NULL) + reg = reg_name_search (cr_names, CR_NAME_CNT, regname); + + if (reg != NULL) + return reg->value; + + return -1; } diff --git a/gas/config/tc-ppc.h b/gas/config/tc-ppc.h index ed06a296382..da3c427c4ae 100644 --- a/gas/config/tc-ppc.h +++ b/gas/config/tc-ppc.h @@ -322,14 +322,17 @@ extern void ppc_frob_label (symbolS *); /* call md_pcrel_from_section, not md_pcrel_from */ #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from_section(FIX, SEC) +#define LEX_PCT (LEX_BEGIN_NAME) + +extern int ppc_parse_name (const char *name, struct expressionS *exp); #define md_parse_name(name, exp, mode, c) ppc_parse_name (name, exp) -extern int ppc_parse_name (const char *, struct expressionS *); + +extern void ppc_operand (expressionS *exp); +#define md_operand ppc_operand #define md_optimize_expr(left, op, right) ppc_optimize_expr (left, op, right) extern int ppc_optimize_expr (expressionS *, operatorT, expressionS *); -#define md_operand(x) - #define md_cleanup() ppc_cleanup () extern void ppc_cleanup (void);