From 9b9e61c7cf49abdf9c2703c4d1f555d762c6e49f Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Wed, 31 Aug 2022 13:32:59 +0100 Subject: [PATCH] gdb: final cleanup of various gdbarch_register_name methods Building on the previous commits, this commit goes through the various gdbarch_register_name methods and removes all the remaining 'return NULL' cases, I claim that these either couldn't be hit, or should be returning the empty string. In all cases the return of NULL was used if the register number being passed to gdbarch_register_name was "invalid", i.e. negative, or greater than the total number of declared registers. I don't believe either of these cases can occur, and the previous commit asserts that this is the case. As a result we can simplify the code by removing these checks. In many cases, where the register names are held in an array, I was able to add a static assert that the array contains the correct number of strings, after that, a direct access into the array is fine. I don't have any means of testing these changes. --- gdb/alpha-tdep.c | 5 +---- gdb/avr-tdep.c | 6 ++---- gdb/bpf-tdep.c | 5 ++--- gdb/cris-tdep.c | 3 ++- gdb/frv-tdep.c | 6 ------ gdb/ft32-tdep.c | 5 +---- gdb/h8300-tdep.c | 9 ++------- gdb/hppa-tdep.c | 12 ++++-------- gdb/iq2000-tdep.c | 3 +-- gdb/lm32-tdep.c | 6 ++---- gdb/m32r-tdep.c | 5 +---- gdb/m68hc11-tdep.c | 5 +---- gdb/m68k-tdep.c | 9 +++------ gdb/microblaze-tdep.c | 6 +++--- gdb/mn10300-tdep.c | 14 ++++++-------- gdb/moxie-tdep.c | 5 +---- gdb/msp430-tdep.c | 2 ++ gdb/nds32-tdep.c | 7 ++----- gdb/nios2-tdep.c | 2 +- gdb/sh-tdep.c | 33 +++++++++++---------------------- gdb/sparc-tdep.c | 8 ++------ gdb/sparc64-tdep.c | 8 ++------ gdb/tic6x-tdep.c | 3 --- gdb/tilegx-tdep.c | 8 ++------ gdb/v850-tdep.c | 12 +++++------- gdb/vax-tdep.c | 6 ++---- gdb/xstormy16-tdep.c | 9 ++------- gdb/xtensa-tdep.c | 6 +----- gdb/z80-tdep.c | 4 ++-- 29 files changed, 66 insertions(+), 146 deletions(-) diff --git a/gdb/alpha-tdep.c b/gdb/alpha-tdep.c index 1dc9b98c7c2..d30e0c369cc 100644 --- a/gdb/alpha-tdep.c +++ b/gdb/alpha-tdep.c @@ -99,10 +99,7 @@ alpha_register_name (struct gdbarch *gdbarch, int regno) "pc", "", "unique" }; - if (regno < 0) - return NULL; - if (regno >= ARRAY_SIZE(register_names)) - return NULL; + gdb_static_assert (ALPHA_NUM_REGS == ARRAY_SIZE (register_names)); return register_names[regno]; } diff --git a/gdb/avr-tdep.c b/gdb/avr-tdep.c index fa66d8380c5..2d8f91f3240 100644 --- a/gdb/avr-tdep.c +++ b/gdb/avr-tdep.c @@ -216,10 +216,8 @@ avr_register_name (struct gdbarch *gdbarch, int regnum) "SREG", "SP", "PC2", "pc" }; - if (regnum < 0) - return NULL; - if (regnum >= (sizeof (register_names) / sizeof (*register_names))) - return NULL; + gdb_static_assert (ARRAY_SIZE (register_names) + == (AVR_NUM_REGS + AVR_NUM_PSEUDO_REGS)); return register_names[regnum]; } diff --git a/gdb/bpf-tdep.c b/gdb/bpf-tdep.c index b50988f0344..64c9f03512b 100644 --- a/gdb/bpf-tdep.c +++ b/gdb/bpf-tdep.c @@ -93,9 +93,8 @@ static const char *bpf_register_names[] = static const char * bpf_register_name (struct gdbarch *gdbarch, int reg) { - if (reg >= 0 && reg < BPF_NUM_REGS) - return bpf_register_names[reg]; - return NULL; + gdb_static_assert (ARRAY_SIZE (bpf_register_names) == BPF_NUM_REGS); + return bpf_register_names[reg]; } /* Return the GDB type of register REGNUM. */ diff --git a/gdb/cris-tdep.c b/gdb/cris-tdep.c index 93dcc920946..51c4e30e12b 100644 --- a/gdb/cris-tdep.c +++ b/gdb/cris-tdep.c @@ -1666,9 +1666,10 @@ cris_register_name (struct gdbarch *gdbarch, int regno) "r8", "r9", "r10", "r11", \ "r12", "r13", "sp", "pc" }; - if (regno >= 0 && regno < NUM_GENREGS) + if (regno < NUM_GENREGS) { /* General register. */ + gdb_static_assert (ARRAY_SIZE (cris_genreg_names) == NUM_GENREGS); return cris_genreg_names[regno]; } else if (regno >= NUM_GENREGS && regno < gdbarch_num_regs (gdbarch)) diff --git a/gdb/frv-tdep.c b/gdb/frv-tdep.c index a7e3405eea4..a11277cf7d0 100644 --- a/gdb/frv-tdep.c +++ b/gdb/frv-tdep.c @@ -269,12 +269,6 @@ set_variant_scratch_registers (frv_gdbarch_tdep *var) static const char * frv_register_name (struct gdbarch *gdbarch, int reg) { - if (reg < 0) - return "?toosmall?"; - - if (reg >= frv_num_regs + frv_num_pseudo_regs) - return "?toolarge?"; - frv_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); return tdep->register_names[reg]; } diff --git a/gdb/ft32-tdep.c b/gdb/ft32-tdep.c index 4ba3e9f3cbc..4105f650b2d 100644 --- a/gdb/ft32-tdep.c +++ b/gdb/ft32-tdep.c @@ -96,10 +96,7 @@ static const char *const ft32_register_names[] = static const char * ft32_register_name (struct gdbarch *gdbarch, int reg_nr) { - if (reg_nr < 0) - return NULL; - if (reg_nr >= FT32_NUM_REGS) - return NULL; + gdb_static_assert (ARRAY_SIZE (ft32_register_names) == FT32_NUM_REGS); return ft32_register_names[reg_nr]; } diff --git a/gdb/h8300-tdep.c b/gdb/h8300-tdep.c index 0bf1dab9402..ed401ee45f8 100644 --- a/gdb/h8300-tdep.c +++ b/gdb/h8300-tdep.c @@ -933,13 +933,8 @@ static const char * h8300_register_name_common (const char *regnames[], int numregs, struct gdbarch *gdbarch, int regno) { - if (regno < 0 - || regno >= numregs) - internal_error (__FILE__, __LINE__, - _("h8300_register_name_common: illegal register number %d"), - regno); - else - return regnames[regno]; + gdb_assert (numregs == gdbarch_num_cooked_regs (gdbarch)); + return regnames[regno]; } static const char * diff --git a/gdb/hppa-tdep.c b/gdb/hppa-tdep.c index b9e3cecf0f5..2ab3095359b 100644 --- a/gdb/hppa-tdep.c +++ b/gdb/hppa-tdep.c @@ -632,10 +632,8 @@ hppa32_register_name (struct gdbarch *gdbarch, int i) "fr28", "fr28R", "fr29", "fr29R", "fr30", "fr30R", "fr31", "fr31R" }; - if (i < 0 || i >= (sizeof (names) / sizeof (*names))) - return NULL; - else - return names[i]; + gdb_static_assert (ARRAY_SIZE (names) == hppa32_num_regs); + return names[i]; } static const char * @@ -667,10 +665,8 @@ hppa64_register_name (struct gdbarch *gdbarch, int i) "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", "fr30", "fr31" }; - if (i < 0 || i >= (sizeof (names) / sizeof (*names))) - return NULL; - else - return names[i]; + gdb_static_assert (ARRAY_SIZE (names) == hppa64_num_regs); + return names[i]; } /* Map dwarf DBX register numbers to GDB register numbers. */ diff --git a/gdb/iq2000-tdep.c b/gdb/iq2000-tdep.c index 236362cc5a8..ea0c129b85e 100644 --- a/gdb/iq2000-tdep.c +++ b/gdb/iq2000-tdep.c @@ -135,8 +135,7 @@ iq2000_register_name (struct gdbarch *gdbarch, int regnum) "r30", "r31", "pc" }; - if (regnum < 0 || regnum >= E_NUM_REGS) - return NULL; + gdb_static_assert (ARRAY_SIZE (names) == E_NUM_REGS); return names[regnum]; } diff --git a/gdb/lm32-tdep.c b/gdb/lm32-tdep.c index 9565d0afe23..7d3888afd7c 100644 --- a/gdb/lm32-tdep.c +++ b/gdb/lm32-tdep.c @@ -87,10 +87,8 @@ lm32_register_name (struct gdbarch *gdbarch, int reg_nr) "PC", "EID", "EBA", "DEBA", "IE", "IM", "IP" }; - if ((reg_nr < 0) || (reg_nr >= ARRAY_SIZE (register_names))) - return NULL; - else - return register_names[reg_nr]; + gdb_static_assert (ARRAY_SIZE (register_names) == SIM_LM32_NUM_REGS); + return register_names[reg_nr]; } /* Return type of register. */ diff --git a/gdb/m32r-tdep.c b/gdb/m32r-tdep.c index 4c2ecd3cfa7..aa65f5ad3ee 100644 --- a/gdb/m32r-tdep.c +++ b/gdb/m32r-tdep.c @@ -212,10 +212,7 @@ static const char * const m32r_register_names[] = { static const char * m32r_register_name (struct gdbarch *gdbarch, int reg_nr) { - if (reg_nr < 0) - return NULL; - if (reg_nr >= M32R_NUM_REGS) - return NULL; + gdb_static_assert (ARRAY_SIZE (m32r_register_names) == M32R_NUM_REGS); return m32r_register_names[reg_nr]; } diff --git a/gdb/m68hc11-tdep.c b/gdb/m68hc11-tdep.c index 64e82cfaf18..f5e33e7a0f1 100644 --- a/gdb/m68hc11-tdep.c +++ b/gdb/m68hc11-tdep.c @@ -388,12 +388,9 @@ m68hc11_register_name (struct gdbarch *gdbarch, int reg_nr) if (reg_nr == HARD_PC_REGNUM && use_page_register (gdbarch)) return "ppc"; - - if (reg_nr < 0) - return NULL; if (reg_nr >= M68HC11_ALL_REGS) - return NULL; + return ""; m68hc11_initialize_register_info (); diff --git a/gdb/m68k-tdep.c b/gdb/m68k-tdep.c index f16e1d3b32f..d34dfb37d0f 100644 --- a/gdb/m68k-tdep.c +++ b/gdb/m68k-tdep.c @@ -173,12 +173,9 @@ m68k_register_name (struct gdbarch *gdbarch, int regnum) { m68k_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); - if (regnum < 0 || regnum >= ARRAY_SIZE (m68k_register_names)) - internal_error (__FILE__, __LINE__, - _("m68k_register_name: illegal register number %d"), - regnum); - else if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM - && tdep->fpregs_present == 0) + gdb_static_assert (ARRAY_SIZE (m68k_register_names) == M68K_NUM_REGS); + if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM + && tdep->fpregs_present == 0) return ""; else return m68k_register_names[regnum]; diff --git a/gdb/microblaze-tdep.c b/gdb/microblaze-tdep.c index ad8d0fdaca3..722de579868 100644 --- a/gdb/microblaze-tdep.c +++ b/gdb/microblaze-tdep.c @@ -92,9 +92,9 @@ static unsigned int microblaze_debug_flag = 0; static const char * microblaze_register_name (struct gdbarch *gdbarch, int regnum) { - if (regnum >= 0 && regnum < MICROBLAZE_NUM_REGS) - return microblaze_register_names[regnum]; - return NULL; + gdb_static_assert (ARRAY_SIZE (microblaze_register_names) + == MICROBLAZE_NUM_REGS); + return microblaze_register_names[regnum]; } static struct type * diff --git a/gdb/mn10300-tdep.c b/gdb/mn10300-tdep.c index 9f690a40b44..25c207a738f 100644 --- a/gdb/mn10300-tdep.c +++ b/gdb/mn10300-tdep.c @@ -250,12 +250,10 @@ mn10300_return_value (struct gdbarch *gdbarch, struct value *function, } static const char * -register_name (int reg, const char **regs, long sizeof_regs) +register_name (int reg, const char **regs, long num_regs) { - if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0])) - return NULL; - else - return regs[reg]; + gdb_assert (reg < num_regs); + return regs[reg]; } static const char * @@ -267,7 +265,7 @@ mn10300_generic_register_name (struct gdbarch *gdbarch, int reg) "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "fp" }; - return register_name (reg, regs, sizeof regs); + return register_name (reg, regs, ARRAY_SIZE (regs)); } @@ -280,7 +278,7 @@ am33_register_name (struct gdbarch *gdbarch, int reg) "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", "" }; - return register_name (reg, regs, sizeof regs); + return register_name (reg, regs, ARRAY_SIZE (regs)); } static const char * @@ -297,7 +295,7 @@ am33_2_register_name (struct gdbarch *gdbarch, int reg) "fs16", "fs17", "fs18", "fs19", "fs20", "fs21", "fs22", "fs23", "fs24", "fs25", "fs26", "fs27", "fs28", "fs29", "fs30", "fs31" }; - return register_name (reg, regs, sizeof regs); + return register_name (reg, regs, ARRAY_SIZE (regs)); } static struct type * diff --git a/gdb/moxie-tdep.c b/gdb/moxie-tdep.c index c11cb0f935d..29bb6458f80 100644 --- a/gdb/moxie-tdep.c +++ b/gdb/moxie-tdep.c @@ -81,10 +81,7 @@ static const char * const moxie_register_names[] = { static const char * moxie_register_name (struct gdbarch *gdbarch, int reg_nr) { - if (reg_nr < 0) - return NULL; - if (reg_nr >= MOXIE_NUM_REGS) - return NULL; + gdb_static_assert (ARRAY_SIZE (moxie_register_names) == MOXIE_NUM_REGS); return moxie_register_names[reg_nr]; } diff --git a/gdb/msp430-tdep.c b/gdb/msp430-tdep.c index 2abc7cf15a9..6c729affdef 100644 --- a/gdb/msp430-tdep.c +++ b/gdb/msp430-tdep.c @@ -196,6 +196,8 @@ msp430_register_name (struct gdbarch *gdbarch, int regnr) "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }; + gdb_static_assert (ARRAY_SIZE (reg_names) == (MSP430_NUM_REGS + + MSP430_NUM_PSEUDO_REGS)); return reg_names[regnr]; } diff --git a/gdb/nds32-tdep.c b/gdb/nds32-tdep.c index 23e9adae2f1..77eb5da863e 100644 --- a/gdb/nds32-tdep.c +++ b/gdb/nds32-tdep.c @@ -418,11 +418,8 @@ nds32_pseudo_register_name (struct gdbarch *gdbarch, int regnum) regnum -= gdbarch_num_regs (gdbarch); /* Currently, only FSRs could be defined as pseudo registers. */ - if (regnum < gdbarch_num_pseudo_regs (gdbarch)) - return nds32_fsr_register_names[regnum]; - - warning (_("Unknown nds32 pseudo register %d."), regnum); - return NULL; + gdb_assert (regnum < gdbarch_num_pseudo_regs (gdbarch)); + return nds32_fsr_register_names[regnum]; } /* Implement the "pseudo_register_read" gdbarch method. */ diff --git a/gdb/nios2-tdep.c b/gdb/nios2-tdep.c index ca03b95ccf4..2d8049032dc 100644 --- a/gdb/nios2-tdep.c +++ b/gdb/nios2-tdep.c @@ -174,7 +174,7 @@ static const char * nios2_register_name (struct gdbarch *gdbarch, int regno) { /* Use mnemonic aliases for GPRs. */ - if (regno >= 0 && regno < NIOS2_NUM_REGS) + if (regno < NIOS2_NUM_REGS) return nios2_reg_names[regno]; else return tdesc_register_name (gdbarch, regno); diff --git a/gdb/sh-tdep.c b/gdb/sh-tdep.c index e4c631b73ec..90f8aa4185c 100644 --- a/gdb/sh-tdep.c +++ b/gdb/sh-tdep.c @@ -115,8 +115,7 @@ sh_sh_register_name (struct gdbarch *gdbarch, int reg_nr) "pc", "pr", "gbr", "vbr", "mach", "macl", "sr" }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -136,8 +135,7 @@ sh_sh3_register_name (struct gdbarch *gdbarch, int reg_nr) "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -156,8 +154,7 @@ sh_sh3e_register_name (struct gdbarch *gdbarch, int reg_nr) "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0", "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -173,8 +170,7 @@ sh_sh2e_register_name (struct gdbarch *gdbarch, int reg_nr) "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7", "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -212,8 +208,7 @@ sh_sh2a_register_name (struct gdbarch *gdbarch, int reg_nr) /* double precision (pseudo) 68 - 75 */ "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -250,8 +245,7 @@ sh_sh2a_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr) "bank", /* double precision (pseudo) 68 - 75: report blank, see below. */ }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -269,8 +263,7 @@ sh_sh_dsp_register_name (struct gdbarch *gdbarch, int reg_nr) "", "", "rs", "re", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -289,8 +282,7 @@ sh_sh3_dsp_register_name (struct gdbarch *gdbarch, int reg_nr) "rs", "re", "", "", "", "", "", "", "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -326,8 +318,7 @@ sh_sh4_register_name (struct gdbarch *gdbarch, int reg_nr) /* FIXME: missing XF */ /* FIXME: missing XD */ }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -361,8 +352,7 @@ sh_sh4_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr) /* vectors (pseudo) 76 - 79 -- not for nofpu target: report blank below. */ }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } @@ -381,8 +371,7 @@ sh_sh4al_dsp_register_name (struct gdbarch *gdbarch, int reg_nr) "rs", "re", "", "", "", "", "", "", "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b", }; - gdb_assert (reg_nr >= 0); - if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) + if (reg_nr >= ARRAY_SIZE (register_names)) return ""; return register_names[reg_nr]; } diff --git a/gdb/sparc-tdep.c b/gdb/sparc-tdep.c index 5f1d9efabea..320f898eb98 100644 --- a/gdb/sparc-tdep.c +++ b/gdb/sparc-tdep.c @@ -398,12 +398,8 @@ sparc32_pseudo_register_name (struct gdbarch *gdbarch, int regnum) { regnum -= gdbarch_num_regs (gdbarch); - if (regnum < SPARC32_NUM_PSEUDO_REGS) - return sparc32_pseudo_register_names[regnum]; - - internal_error (__FILE__, __LINE__, - _("sparc32_pseudo_register_name: bad register number %d"), - regnum); + gdb_assert (regnum < SPARC32_NUM_PSEUDO_REGS); + return sparc32_pseudo_register_names[regnum]; } /* Return the name of register REGNUM. */ diff --git a/gdb/sparc64-tdep.c b/gdb/sparc64-tdep.c index 5e696579f1f..6b9d9eaa957 100644 --- a/gdb/sparc64-tdep.c +++ b/gdb/sparc64-tdep.c @@ -810,12 +810,8 @@ sparc64_pseudo_register_name (struct gdbarch *gdbarch, int regnum) { regnum -= gdbarch_num_regs (gdbarch); - if (regnum < SPARC64_NUM_PSEUDO_REGS) - return sparc64_pseudo_register_names[regnum]; - - internal_error (__FILE__, __LINE__, - _("sparc64_pseudo_register_name: bad register number %d"), - regnum); + gdb_assert (regnum < SPARC64_NUM_PSEUDO_REGS); + return sparc64_pseudo_register_names[regnum]; } /* Return the name of register REGNUM. */ diff --git a/gdb/tic6x-tdep.c b/gdb/tic6x-tdep.c index 5dd84f8ea0d..27e575d4176 100644 --- a/gdb/tic6x-tdep.c +++ b/gdb/tic6x-tdep.c @@ -102,9 +102,6 @@ static const int arg_regs[] = { 4, 20, 6, 22, 8, 24, 10, 26, 12, 28 }; static const char * tic6x_register_name (struct gdbarch *gdbarch, int regno) { - if (regno < 0) - return NULL; - if (tdesc_has_registers (gdbarch_target_desc (gdbarch))) return tdesc_register_name (gdbarch, regno); else if (regno >= ARRAY_SIZE (tic6x_register_names)) diff --git a/gdb/tilegx-tdep.c b/gdb/tilegx-tdep.c index 2888251079a..3554b503e8b 100644 --- a/gdb/tilegx-tdep.c +++ b/gdb/tilegx-tdep.c @@ -142,7 +142,7 @@ template_reverse_regs[TILEGX_NUM_PHYS_REGS] = static const char * tilegx_register_name (struct gdbarch *gdbarch, int regnum) { - static const char *const register_names[TILEGX_NUM_REGS] = + static const char *const register_names[] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", @@ -155,11 +155,7 @@ tilegx_register_name (struct gdbarch *gdbarch, int regnum) "pc", "faultnum", }; - if (regnum < 0 || regnum >= TILEGX_NUM_REGS) - internal_error (__FILE__, __LINE__, - "tilegx_register_name: invalid register number %d", - regnum); - + gdb_static_assert (TILEGX_NUM_REGS == ARRAY_SIZE (register_names)); return register_names[regnum]; } diff --git a/gdb/v850-tdep.c b/gdb/v850-tdep.c index b7f3d8a9a18..7b5e42929d7 100644 --- a/gdb/v850-tdep.c +++ b/gdb/v850-tdep.c @@ -312,8 +312,7 @@ v850_register_name (struct gdbarch *gdbarch, int regnum) "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31", "pc", "fp" }; - if (regnum < 0 || regnum > E_NUM_OF_V850_REGS) - return ""; + gdb_static_assert (E_NUM_OF_V850_REGS == ARRAY_SIZE (v850_reg_names)); return v850_reg_names[regnum]; } @@ -332,8 +331,7 @@ v850e_register_name (struct gdbarch *gdbarch, int regnum) "sr24", "sr25", "sr26", "sr27", "sr28", "sr29", "sr30", "sr31", "pc", "fp" }; - if (regnum < 0 || regnum > E_NUM_OF_V850E_REGS) - return ""; + gdb_static_assert (E_NUM_OF_V850E_REGS == ARRAY_SIZE (v850e_reg_names)); return v850e_reg_names[regnum]; } @@ -376,7 +374,7 @@ v850e2_register_name (struct gdbarch *gdbarch, int regnum) "", "", "", "", "", "", "", "", "", "", "", "fpspc" }; - if (regnum < 0 || regnum >= E_NUM_OF_V850E2_REGS) + if (regnum >= E_NUM_OF_V850E2_REGS) return ""; return v850e2_reg_names[regnum]; } @@ -479,8 +477,8 @@ v850e3v5_register_name (struct gdbarch *gdbarch, int regnum) "vr24", "vr25", "vr26", "vr27", "vr28", "vr29", "vr30", "vr31", }; - if (regnum < 0 || regnum >= E_NUM_OF_V850E3V5_REGS) - return ""; + gdb_static_assert (E_NUM_OF_V850E3V5_REGS + == ARRAY_SIZE (v850e3v5_reg_names)); return v850e3v5_reg_names[regnum]; } diff --git a/gdb/vax-tdep.c b/gdb/vax-tdep.c index 633575d0df6..3befa8051d9 100644 --- a/gdb/vax-tdep.c +++ b/gdb/vax-tdep.c @@ -45,10 +45,8 @@ vax_register_name (struct gdbarch *gdbarch, int regnum) "ps", }; - if (regnum >= 0 && regnum < ARRAY_SIZE (register_names)) - return register_names[regnum]; - - return NULL; + gdb_static_assert (VAX_NUM_REGS == ARRAY_SIZE (register_names)); + return register_names[regnum]; } /* Return the GDB type object for the "standard" data type of data in diff --git a/gdb/xstormy16-tdep.c b/gdb/xstormy16-tdep.c index 17036370203..3504a0b6eaa 100644 --- a/gdb/xstormy16-tdep.c +++ b/gdb/xstormy16-tdep.c @@ -108,13 +108,8 @@ xstormy16_register_name (struct gdbarch *gdbarch, int regnum) "psw", "sp", "pc" }; - if (regnum < 0 || regnum >= E_NUM_REGS) - internal_error (__FILE__, __LINE__, - _("xstormy16_register_name: illegal register number %d"), - regnum); - else - return register_names[regnum]; - + gdb_static_assert (ARRAY_SIZE (register_names) == E_NUM_REGS); + return register_names[regnum]; } static struct type * diff --git a/gdb/xtensa-tdep.c b/gdb/xtensa-tdep.c index 58fdb40b90d..32311035ef5 100644 --- a/gdb/xtensa-tdep.c +++ b/gdb/xtensa-tdep.c @@ -242,11 +242,7 @@ xtensa_register_name (struct gdbarch *gdbarch, int regnum) xtensa_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); /* Return the name stored in the register map. */ - if (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch)) - return tdep->regmap[regnum].name; - - internal_error (__FILE__, __LINE__, _("invalid register %d"), regnum); - return 0; + return tdep->regmap[regnum].name; } /* Return the type of a register. Create a new type, if necessary. */ diff --git a/gdb/z80-tdep.c b/gdb/z80-tdep.c index d30f2302824..a25610aab6b 100644 --- a/gdb/z80-tdep.c +++ b/gdb/z80-tdep.c @@ -167,10 +167,10 @@ static const char *z80_reg_names[] = static const char * z80_register_name (struct gdbarch *gdbarch, int regnum) { - if (regnum >= 0 && regnum < ARRAY_SIZE (z80_reg_names)) + if (regnum < ARRAY_SIZE (z80_reg_names)) return z80_reg_names[regnum]; - return NULL; + return ""; } /* Return the type of a register specified by the architecture. Only -- 2.30.2