From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id EA72B385276A; Wed, 15 Jun 2022 09:03:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA72B385276A Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: unify two dis_asm_read_memory functions in disasm.c X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: 8b39b1e7ab20609ced6a224cae440f19e6ae02c1 X-Git-Newrev: 75033d08412577fb8ffcf76971e8d0393d14a8aa Message-Id: <20220615090357.EA72B385276A@sourceware.org> Date: Wed, 15 Jun 2022 09:03:57 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Jun 2022 09:03:58 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D75033d084125= 77fb8ffcf76971e8d0393d14a8aa commit 75033d08412577fb8ffcf76971e8d0393d14a8aa Author: Andrew Burgess Date: Mon Apr 4 22:52:58 2022 +0100 gdb: unify two dis_asm_read_memory functions in disasm.c =20 After the recent restructuring of the disassembler code, GDB has ended up with two identical class static functions, both called dis_asm_read_memory, with identical implementations. =20 My first thought was to move these out of their respective classes, and just make them global functions, then I'd only need a single copy. =20 And maybe that's the right way to go. But I disliked that by doing that I loose the encapsulation of the method with the corresponding disassembler class. =20 So, instead, I placed the static method into its own class, and had both the gdb_non_printing_memory_disassembler and gdb_disassembler classes inherit from this new class as an additional base-class. =20 In terms of code generated, I don't think there's any significant difference with this approach, but I think this better reflects how the function is closely tied to the disassembler. =20 There should be no user visible changes after this commit. Diff: --- gdb/disasm.c | 16 +++------------- gdb/disasm.h | 29 +++++++++++++++++------------ 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/gdb/disasm.c b/gdb/disasm.c index 53cd6f5b6bb..c6edc92930d 100644 --- a/gdb/disasm.c +++ b/gdb/disasm.c @@ -132,9 +132,9 @@ line_has_code_p (htab_t table, struct symtab *symtab, i= nt line) /* Wrapper of target_read_code. */ =20 int -gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, - unsigned int len, - struct disassemble_info *info) +gdb_disassembler_memory_reader::dis_asm_read_memory + (bfd_vma memaddr, gdb_byte *myaddr, unsigned int len, + struct disassemble_info *info) { return target_read_code (memaddr, myaddr, len); } @@ -1021,16 +1021,6 @@ gdb_non_printing_disassembler::null_fprintf_styled_f= unc return 0; } =20 -/* See disasm.h. */ - -int -gdb_non_printing_memory_disassembler::dis_asm_read_memory - (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, - struct disassemble_info *dinfo) -{ - return target_read_code (memaddr, myaddr, length); -} - /* A non-printing disassemble_info management class. The disassemble_info setup by this class will not print anything to the output stream (there is no output stream), and the instruction to be disassembled will be diff --git a/gdb/disasm.h b/gdb/disasm.h index ec5120351a1..da03e130526 100644 --- a/gdb/disasm.h +++ b/gdb/disasm.h @@ -165,31 +165,39 @@ private: ATTRIBUTE_PRINTF(3,4); }; =20 +/* This is a helper class, for use as an additional base-class, by some of + the disassembler classes below. This class just defines a static method + for reading from target memory, which can then be used by the various + disassembler sub-classes. */ + +struct gdb_disassembler_memory_reader +{ + /* Implements the read_memory_func disassemble_info callback. */ + static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, + unsigned int len, + struct disassemble_info *info); +}; + /* A non-printing disassemble_info management class. The disassemble_info setup by this class will not print anything to the output stream (there is no output stream), and the instruction to be disassembled will be read from target memory. */ =20 struct gdb_non_printing_memory_disassembler - : public gdb_non_printing_disassembler + : public gdb_non_printing_disassembler, + private gdb_disassembler_memory_reader { /* Constructor. GDBARCH is the architecture to disassemble for. */ gdb_non_printing_memory_disassembler (struct gdbarch *gdbarch) :gdb_non_printing_disassembler (gdbarch, dis_asm_read_memory) { /* Nothing. */ } - -private: - - /* Implements the read_memory_func disassemble_info callback. */ - static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, - unsigned int len, - struct disassemble_info *info); }; =20 /* A dissassembler class that provides 'print_insn', a method for disassembling a single instruction to the output stream. */ =20 -struct gdb_disassembler : public gdb_printing_disassembler +struct gdb_disassembler : public gdb_printing_disassembler, + private gdb_disassembler_memory_reader { gdb_disassembler (struct gdbarch *gdbarch, struct ui_file *file) : gdb_disassembler (gdbarch, file, dis_asm_read_memory) @@ -239,9 +247,6 @@ private: (currently just to addresses and symbols) as it goes. */ static bool use_ext_lang_colorization_p; =20 - static int dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, - unsigned int len, - struct disassemble_info *info); static void dis_asm_memory_error (int err, bfd_vma memaddr, struct disassemble_info *info); static void dis_asm_print_address (bfd_vma addr,