From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 121952 invoked by alias); 15 Jun 2017 11:59:20 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 121929 invoked by uid 89); 15 Jun 2017 11:59:19 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-io0-f195.google.com Received: from mail-io0-f195.google.com (HELO mail-io0-f195.google.com) (209.85.223.195) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 15 Jun 2017 11:59:15 +0000 Received: by mail-io0-f195.google.com with SMTP id j200so1646418ioe.0 for ; Thu, 15 Jun 2017 04:59:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=GY7LMu8DZr3P4a9roVoB64/JcNfF2wy7WPU7CgsP/q8=; b=b/0rXjN8X/Zkm846j5GyUpfqaFJnxzZX0qvSJTTp32GgSnAVTdUE00sKGGvG6XvXy2 hRDWKyW+8Dxjf6Pdyaqv03dTitF5FTRBGQ5twBzWa1AQxYnhV+SYNoXPXXQE1sTIP99v /z283ZSqUTtHSFi78gzF5ScvgC6Uoni/4UCQx46NlY55f4dZfskZvLyGDx6kETJEOXFB 5PpBGZ+PWmWA7636ZEVXPwxDvaZO+eyi/WiEUAcMW9Jb2jp21cRC4P0+hf7UFdtl3Bx9 gf+fYS4YDci38fOVZFQGzFz5o+qyG7JOCdU3r+unFXPMAwYkOg0ndVQ8j8cxvjVTLmBL fJ/Q== X-Gm-Message-State: AKS2vOwnckaFqB1M7rvC556ma2cAa2Sx2Fw3JIhVntUCYWB5gQwoAe5f bKhOiZv75d0igw/i X-Received: by 10.107.37.19 with SMTP id l19mr4522855iol.216.1497527958756; Thu, 15 Jun 2017 04:59:18 -0700 (PDT) Received: from E107787-LIN (static.42.136.251.148.clients.your-server.de. [148.251.136.42]) by smtp.gmail.com with ESMTPSA id e72sm1769893itd.29.2017.06.15.04.59.16 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 15 Jun 2017 04:59:18 -0700 (PDT) From: Yao Qi To: Pedro Alves Cc: Alan Hayward , gdb-patches@sourceware.org Subject: Re: [RFC] LONGEST and ULONGEST function template instantiation References: <1497353362-14441-1-git-send-email-yao.qi@linaro.org> <3038a7b4-d090-7614-1eb0-54c427f95169@redhat.com> Date: Thu, 15 Jun 2017 11:59:00 -0000 In-Reply-To: <3038a7b4-d090-7614-1eb0-54c427f95169@redhat.com> (Pedro Alves's message of "Wed, 14 Jun 2017 17:11:50 +0100") Message-ID: <8660fxv4vi.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2017-06/txt/msg00444.txt.bz2 Pedro Alves writes: Hi Pedro, I pick up your patch, and update the ChangeLog entry. > The version below has no impact on code size: > > $ size gdb.before gdb.after > text data bss dec hex filename > 7535236 125008 181184 7841428 77a694 gdb.before > 7535236 125008 181184 7841428 77a694 gdb.after I can't reproduce your result with gcc 5.4.0 on Ubuntu 16.04. The patched GDB size increased by 112, but it doesn't matter. before: $ size ./gdb text data bss dec hex filename 7733505 144208 180768 8058481 7af671 ./gdb after: $ size ./gdb text data bss dec hex filename 7733617 144208 180768 8058593 7af6e1 ./gdb --=20 Yao (=E9=BD=90=E5=B0=A7) =46rom cb61b39bb2a35e6ac81dce31019e3dabbc111292 Mon Sep 17 00:00:00 2001 From: Yao Qi Date: Thu, 15 Jun 2017 11:06:58 +0100 Subject: [PATCH] extract/store integer function template This patch converts functions extract_{unsigned,signed}_integer to a function template extract_integer, which has two instantiations. It also does the similar changes to store__{unsigned,signed}_integer, regcache::raw_read_{unsigned,signed}, regcache::raw_write_{unsigned,signed}, regcache::cooked_read_{unsigned,signed}, regcache::cooked_write_{unsigned,signed}. This patch was posted here https://sourceware.org/ml/gdb-patches/2017-05/msg00492.html but the problem was fixed in a different way. However, I think the patch is still useful to shorten the code. gdb: 2017-06-15 Alan Hayward Pedro Alves Yao Qi * defs.h (RequireLongest): New. (extract_integer): Declare function template. (extract_signed_integer): Remove the declaration, but define it static inline. (extract_unsigned_integer): Likewise. (store_integer): Declare function template. (store_signed_integer): Remove the declaration, but define it static inline. (store_unsigned_integer): Likewise. * findvar.c (extract_integer): New function template. (extract_signed_integer): Remove. (extract_unsigned_integer): Remove. (extract_integer, extract_integer): Explicit instantiations. (store_integer): New function template. (store_signed_integer): Remove. (store_unsigned_integer): Remove. (store_integer): Explicit instantiations. * regcache.c (regcache_raw_read_signed): Update. (regcache::raw_read): New function. (regcache::raw_read_signed): Remove. (regcache::raw_read_unsigned): Remove. (regcache_raw_read_unsigned): Update. (regcache_raw_write_unsigned): Update. (regcache::raw_write_signed): Remove. (regcache::raw_write): New function. (regcache_cooked_read_signed): Update. (regcache::raw_write_unsigned): Remove. (regcache::cooked_read_signed): Remove. (regcache_cooked_read_unsigned): Update. (regcache::cooked_read_unsigned): Remove. (regcache_cooked_write_signed): Update. (regcache_cooked_write_unsigned): Update. * regcache.h (regcache) : Remove. : Remove. : New. : Remove. : Remove. : New. * sh64-tdep.c (sh64_pseudo_register_read): Update. (sh64_pseudo_register_write): Update. diff --git a/gdb/defs.h b/gdb/defs.h index a1a97bb..55d16d1 100644 --- a/gdb/defs.h +++ b/gdb/defs.h @@ -82,6 +82,11 @@ enum compile_i_scope_types COMPILE_I_PRINT_VALUE_SCOPE, }; =20 + +template +using RequireLongest =3D gdb::Requires, + std::is_same>>; + /* Just in case they're not defined in stdio.h. */ =20 #ifndef SEEK_SET @@ -637,11 +642,22 @@ enum { MAX_REGISTER_SIZE =3D 64 }; =20 /* In findvar.c. */ =20 -extern LONGEST extract_signed_integer (const gdb_byte *, int, - enum bfd_endian); +template> +T extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_ord= er); =20 -extern ULONGEST extract_unsigned_integer (const gdb_byte *, int, - enum bfd_endian); +static inline LONGEST +extract_signed_integer (const gdb_byte *addr, int len, + enum bfd_endian byte_order) +{ + return extract_integer (addr, len, byte_order); +} + +static inline ULONGEST +extract_unsigned_integer (const gdb_byte *addr, int len, + enum bfd_endian byte_order) +{ + return extract_integer (addr, len, byte_order); +} =20 extern int extract_long_unsigned_integer (const gdb_byte *, int, enum bfd_endian, LONGEST *); @@ -649,11 +665,26 @@ extern int extract_long_unsigned_integer (const gdb_b= yte *, int, extern CORE_ADDR extract_typed_address (const gdb_byte *buf, struct type *type); =20 -extern void store_signed_integer (gdb_byte *, int, - enum bfd_endian, LONGEST); +/* All 'store' functions accept a host-format integer and store a + target-format integer at ADDR which is LEN bytes long. */ =20 -extern void store_unsigned_integer (gdb_byte *, int, - enum bfd_endian, ULONGEST); +template> +extern void store_integer (gdb_byte *addr, int len, enum bfd_endian byte_o= rder, + T val); + +static inline void +store_signed_integer (gdb_byte *addr, int len, + enum bfd_endian byte_order, LONGEST val) +{ + return store_integer (addr, len, byte_order, val); +} + +static inline void +store_unsigned_integer (gdb_byte *addr, int len, + enum bfd_endian byte_order, ULONGEST val) +{ + return store_integer (addr, len, byte_order, val); +} =20 extern void store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr); diff --git a/gdb/findvar.c b/gdb/findvar.c index 6c18e25..beb127e 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -47,70 +47,54 @@ you lose #endif =20 -LONGEST -extract_signed_integer (const gdb_byte *addr, int len, - enum bfd_endian byte_order) +template +T +extract_integer (const gdb_byte *addr, int len, enum bfd_endian byte_order) { - LONGEST retval; + T retval =3D 0; const unsigned char *p; const unsigned char *startaddr =3D addr; const unsigned char *endaddr =3D startaddr + len; =20 - if (len > (int) sizeof (LONGEST)) + if (len > (int) sizeof (T)) error (_("\ That operation is not available on integers of more than %d bytes."), - (int) sizeof (LONGEST)); + (int) sizeof (T)); =20 /* Start at the most significant end of the integer, and work towards the least significant. */ if (byte_order =3D=3D BFD_ENDIAN_BIG) { p =3D startaddr; - /* Do the sign extension once at the start. */ - retval =3D ((LONGEST) * p ^ 0x80) - 0x80; - for (++p; p < endaddr; ++p) + if (std::is_signed::value) + { + /* Do the sign extension once at the start. */ + retval =3D ((LONGEST) * p ^ 0x80) - 0x80; + ++p; + } + for (; p < endaddr; ++p) retval =3D (retval << 8) | *p; } else { p =3D endaddr - 1; - /* Do the sign extension once at the start. */ - retval =3D ((LONGEST) * p ^ 0x80) - 0x80; - for (--p; p >=3D startaddr; --p) + if (std::is_signed::value) + { + /* Do the sign extension once at the start. */ + retval =3D ((LONGEST) * p ^ 0x80) - 0x80; + --p; + } + for (; p >=3D startaddr; --p) retval =3D (retval << 8) | *p; } return retval; } =20 -ULONGEST -extract_unsigned_integer (const gdb_byte *addr, int len, - enum bfd_endian byte_order) -{ - ULONGEST retval; - const unsigned char *p; - const unsigned char *startaddr =3D addr; - const unsigned char *endaddr =3D startaddr + len; - - if (len > (int) sizeof (ULONGEST)) - error (_("\ -That operation is not available on integers of more than %d bytes."), - (int) sizeof (ULONGEST)); - - /* Start at the most significant end of the integer, and work towards - the least significant. */ - retval =3D 0; - if (byte_order =3D=3D BFD_ENDIAN_BIG) - { - for (p =3D startaddr; p < endaddr; ++p) - retval =3D (retval << 8) | *p; - } - else - { - for (p =3D endaddr - 1; p >=3D startaddr; --p) - retval =3D (retval << 8) | *p; - } - return retval; -} +/* Explicit instantiations. */ +template LONGEST extract_integer (const gdb_byte *addr, int len, + enum bfd_endian byte_order); +template ULONGEST extract_integer (const gdb_byte *addr, int len, + enum bfd_endian byte_order); =20 /* Sometimes a long long unsigned integer can be extracted as a LONGEST value. This is done so that we can print these values @@ -180,10 +164,10 @@ extract_typed_address (const gdb_byte *buf, struct ty= pe *type) =20 /* All 'store' functions accept a host-format integer and store a target-format integer at ADDR which is LEN bytes long. */ - +template void -store_signed_integer (gdb_byte *addr, int len, - enum bfd_endian byte_order, LONGEST val) +store_integer (gdb_byte *addr, int len, enum bfd_endian byte_order, + T val) { gdb_byte *p; gdb_byte *startaddr =3D addr; @@ -209,33 +193,14 @@ store_signed_integer (gdb_byte *addr, int len, } } =20 -void -store_unsigned_integer (gdb_byte *addr, int len, - enum bfd_endian byte_order, ULONGEST val) -{ - unsigned char *p; - unsigned char *startaddr =3D (unsigned char *) addr; - unsigned char *endaddr =3D startaddr + len; +/* Explicit instantiations. */ +template void store_integer (gdb_byte *addr, int len, + enum bfd_endian byte_order, + LONGEST val); =20 - /* Start at the least significant end of the integer, and work towards - the most significant. */ - if (byte_order =3D=3D BFD_ENDIAN_BIG) - { - for (p =3D endaddr - 1; p >=3D startaddr; --p) - { - *p =3D val & 0xff; - val >>=3D 8; - } - } - else - { - for (p =3D startaddr; p < endaddr; ++p) - { - *p =3D val & 0xff; - val >>=3D 8; - } - } -} +template void store_integer (gdb_byte *addr, int len, + enum bfd_endian byte_order, + ULONGEST val); =20 /* Store the address ADDR as a pointer of type TYPE at BUF, in target form. */ diff --git a/gdb/regcache.c b/gdb/regcache.c index 43ea430..7eeb737 100644 --- a/gdb/regcache.c +++ b/gdb/regcache.c @@ -662,11 +662,12 @@ enum register_status regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *= val) { gdb_assert (regcache !=3D NULL); - return regcache->raw_read_signed (regnum, val); + return regcache->raw_read (regnum, val); } =20 +template enum register_status -regcache::raw_read_signed (int regnum, LONGEST *val) +regcache::raw_read (int regnum, T *val) { gdb_byte *buf; enum register_status status; @@ -675,9 +676,9 @@ regcache::raw_read_signed (int regnum, LONGEST *val) buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); status =3D raw_read (regnum, buf); if (status =3D=3D REG_VALID) - *val =3D extract_signed_integer - (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch)); + *val =3D extract_integer (buf, + m_descr->sizeof_register[regnum], + gdbarch_byte_order (m_descr->gdbarch)); else *val =3D 0; return status; @@ -688,44 +689,26 @@ regcache_raw_read_unsigned (struct regcache *regcache= , int regnum, ULONGEST *val) { gdb_assert (regcache !=3D NULL); - return regcache->raw_read_unsigned (regnum, val); -} - - -enum register_status -regcache::raw_read_unsigned (int regnum, ULONGEST *val) -{ - gdb_byte *buf; - enum register_status status; - - gdb_assert (regnum >=3D 0 && regnum < m_descr->nr_raw_registers); - buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - status =3D raw_read (regnum, buf); - if (status =3D=3D REG_VALID) - *val =3D extract_unsigned_integer - (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch)); - else - *val =3D 0; - return status; + return regcache->raw_read (regnum, val); } =20 void regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST = val) { gdb_assert (regcache !=3D NULL); - regcache->raw_write_signed (regnum, val); + regcache->raw_write (regnum, val); } =20 +template void -regcache::raw_write_signed (int regnum, LONGEST val) +regcache::raw_write (int regnum, T val) { gdb_byte *buf; =20 gdb_assert (regnum >=3D0 && regnum < m_descr->nr_raw_registers); buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - store_signed_integer (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch), val); + store_integer (buf, m_descr->sizeof_register[regnum], + gdbarch_byte_order (m_descr->gdbarch), val); raw_write (regnum, buf); } =20 @@ -734,19 +717,7 @@ regcache_raw_write_unsigned (struct regcache *regcache= , int regnum, ULONGEST val) { gdb_assert (regcache !=3D NULL); - regcache->raw_write_unsigned (regnum, val); -} - -void -regcache::raw_write_unsigned (int regnum, ULONGEST val) -{ - gdb_byte *buf; - - gdb_assert (regnum >=3D0 && regnum < m_descr->nr_raw_registers); - buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - store_unsigned_integer (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch), val); - raw_write (regnum, buf); + regcache->raw_write (regnum, val); } =20 LONGEST @@ -857,11 +828,12 @@ regcache_cooked_read_signed (struct regcache *regcach= e, int regnum, LONGEST *val) { gdb_assert (regcache !=3D NULL); - return regcache->cooked_read_signed (regnum, val); + return regcache->cooked_read (regnum, val); } =20 +template enum register_status -regcache::cooked_read_signed (int regnum, LONGEST *val) +regcache::cooked_read (int regnum, T *val) { enum register_status status; gdb_byte *buf; @@ -870,9 +842,8 @@ regcache::cooked_read_signed (int regnum, LONGEST *val) buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); status =3D cooked_read (regnum, buf); if (status =3D=3D REG_VALID) - *val =3D extract_signed_integer - (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch)); + *val =3D extract_integer (buf, m_descr->sizeof_register[regnum], + gdbarch_byte_order (m_descr->gdbarch)); else *val =3D 0; return status; @@ -883,25 +854,7 @@ regcache_cooked_read_unsigned (struct regcache *regcac= he, int regnum, ULONGEST *val) { gdb_assert (regcache !=3D NULL); - return regcache->cooked_read_unsigned (regnum, val); -} - -enum register_status -regcache::cooked_read_unsigned (int regnum, ULONGEST *val) -{ - enum register_status status; - gdb_byte *buf; - - gdb_assert (regnum >=3D 0 && regnum < m_descr->nr_cooked_registers); - buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - status =3D cooked_read (regnum, buf); - if (status =3D=3D REG_VALID) - *val =3D extract_unsigned_integer - (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch)); - else - *val =3D 0; - return status; + return regcache->cooked_read (regnum, val); } =20 void @@ -909,18 +862,19 @@ regcache_cooked_write_signed (struct regcache *regcac= he, int regnum, LONGEST val) { gdb_assert (regcache !=3D NULL); - regcache->cooked_write_signed (regnum, val); + regcache->cooked_write (regnum, val); } =20 +template void -regcache::cooked_write_signed (int regnum, LONGEST val) +regcache::cooked_write (int regnum, T val) { gdb_byte *buf; =20 gdb_assert (regnum >=3D0 && regnum < m_descr->nr_cooked_registers); buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - store_signed_integer (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch), val); + store_integer (buf, m_descr->sizeof_register[regnum], + gdbarch_byte_order (m_descr->gdbarch), val); cooked_write (regnum, buf); } =20 @@ -929,19 +883,7 @@ regcache_cooked_write_unsigned (struct regcache *regca= che, int regnum, ULONGEST val) { gdb_assert (regcache !=3D NULL); - regcache->cooked_write_unsigned (regnum, val); -} - -void -regcache::cooked_write_unsigned (int regnum, ULONGEST val) -{ - gdb_byte *buf; - - gdb_assert (regnum >=3D0 && regnum < m_descr->nr_cooked_registers); - buf =3D (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); - store_unsigned_integer (buf, m_descr->sizeof_register[regnum], - gdbarch_byte_order (m_descr->gdbarch), val); - cooked_write (regnum, buf); + regcache->cooked_write (regnum, val); } =20 /* See regcache.h. */ diff --git a/gdb/regcache.h b/gdb/regcache.h index 4cf27a0..3f3f823 100644 --- a/gdb/regcache.h +++ b/gdb/regcache.h @@ -282,23 +282,19 @@ public: #endif void raw_write (int regnum, const gdb_byte *buf); =20 - enum register_status raw_read_signed (int regnum, LONGEST *val); + template> + enum register_status raw_read (int regnum, T *val); =20 - void raw_write_signed (int regnum, LONGEST val); - - enum register_status raw_read_unsigned (int regnum, ULONGEST *val); - - void raw_write_unsigned (int regnum, ULONGEST val); + template> + void raw_write (int regnum, T val); =20 struct value *cooked_read_value (int regnum); =20 - enum register_status cooked_read_signed (int regnum, LONGEST *val); - - void cooked_write_signed (int regnum, LONGEST val); - - enum register_status cooked_read_unsigned (int regnum, ULONGEST *val); + template> + enum register_status cooked_read (int regnum, T *val); =20 - void cooked_write_unsigned (int regnum, ULONGEST val); + template> + void cooked_write (int regnum, T val); =20 void raw_update (int regnum); =20 diff --git a/gdb/sh64-tdep.c b/gdb/sh64-tdep.c index dd18e9f..5aeb235 100644 --- a/gdb/sh64-tdep.c +++ b/gdb/sh64-tdep.c @@ -1665,11 +1665,11 @@ sh64_pseudo_register_read (struct gdbarch *gdbarch,= struct regcache *regcache, */ /* *INDENT-ON* */ /* Get FPSCR as an int. */ - status =3D regcache->raw_read_unsigned (fpscr_base_regnum, &fpscr_va= lue); + status =3D regcache->raw_read (fpscr_base_regnum, &fpscr_value); if (status !=3D REG_VALID) return status; /* Get SR as an int. */ - status =3D regcache->raw_read_unsigned (sr_base_regnum, &sr_value); + status =3D regcache->raw_read (sr_base_regnum, &sr_value); if (status !=3D REG_VALID) return status; /* Build the new value. */ @@ -1847,15 +1847,15 @@ sh64_pseudo_register_write (struct gdbarch *gdbarch= , struct regcache *regcache, fpscr_value =3D fpscr_c_value & fpscr_mask; sr_value =3D (fpscr_value & sr_mask) >> 6; =20=20=20=20=20=20=20 - regcache->raw_read_unsigned (fpscr_base_regnum, &old_fpscr_value); + regcache->raw_read (fpscr_base_regnum, &old_fpscr_value); old_fpscr_value &=3D 0xfffc0002; fpscr_value |=3D old_fpscr_value; - regcache->raw_write_unsigned (fpscr_base_regnum, fpscr_value); + regcache->raw_write (fpscr_base_regnum, fpscr_value); =20 - regcache->raw_read_unsigned (sr_base_regnum, &old_sr_value); + regcache->raw_read (sr_base_regnum, &old_sr_value); old_sr_value &=3D 0xffff8fff; sr_value |=3D old_sr_value; - regcache->raw_write_unsigned (sr_base_regnum, sr_value); + regcache->raw_write (sr_base_regnum, sr_value); } =20 else if (reg_nr =3D=3D FPUL_C_REGNUM)