From patchwork Mon Mar 13 22:08:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 66341 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 779B1384F036 for ; Mon, 13 Mar 2023 22:09:45 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from outbound-ss-761.bluehost.com (outbound-ss-761.bluehost.com [74.220.211.250]) by sourceware.org (Postfix) with ESMTPS id 0B48C3858D32 for ; Mon, 13 Mar 2023 22:08:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0B48C3858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw13.mail.unifiedlayer.com (unknown [10.0.90.128]) by progateway8.mail.pro1.eigbox.com (Postfix) with ESMTP id C20AB1004CA47 for ; Mon, 13 Mar 2023 22:08:06 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id bqKspXv3xNX2abqKspEw58; Mon, 13 Mar 2023 22:08:06 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=NMAQR22g c=1 sm=1 tr=0 ts=640f9ec6 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10:nop_charset_1 a=k__wU0fu6RkA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=9lwuInUVEUyxk41ULTgA:9 a=QEXdDO2ut3YA:10:nop_charset_2 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding: Content-Type:MIME-Version:Subject:Date:From:Sender:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=fLf+OkbrWirWwuGVIdorsV1XLWuU/zXDPq6a92EyntM=; b=JWMJoJ6lDlKreEUmPBZKZn299d inkgky6qnQyTYZvgAqV0kW2DGVarazxFpsViyiS8gIN603TQ2N++CcGKWxV/EdZ1lNXszfE/Mn1ur uSRqgSh6Ml9LTeDWy/y5Q+/yM; Received: from 71-211-185-113.hlrn.qwest.net ([71.211.185.113]:52788 helo=[192.168.0.21]) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pbqKs-000u2m-Bn for gdb-patches@sourceware.org; Mon, 13 Mar 2023 16:08:06 -0600 From: Tom Tromey Date: Mon, 13 Mar 2023 16:08:01 -0600 Subject: [PATCH 01/20] Introduce type_allocator MIME-Version: 1.0 Message-Id: <20230313-split-objfile-type-allocator-2-v1-1-69ba773ac17b@tromey.com> References: <20230313-split-objfile-type-allocator-2-v1-0-69ba773ac17b@tromey.com> In-Reply-To: <20230313-split-objfile-type-allocator-2-v1-0-69ba773ac17b@tromey.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.1 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.185.113 X-Source-L: No X-Exim-ID: 1pbqKs-000u2m-Bn X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-185-113.hlrn.qwest.net ([192.168.0.21]) [71.211.185.113]:52788 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3026.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" This introduces a new type_allocator class. This class will be used to abstract out the placement of new types, so that type-creation code can be simplified and shared. --- gdb/gdbtypes.c | 118 ++++++++++++++++++++++++++++++++++++++++++++------------- gdb/gdbtypes.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 27 deletions(-) diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index cfd0a1b62c7..5bb49063d5a 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -173,6 +173,64 @@ show_strict_type_checking (struct ui_file *file, int from_tty, } +/* Helper function to initialize a newly allocated type. Set type code + to CODE and initialize the type-specific fields accordingly. */ + +static void +set_type_code (struct type *type, enum type_code code) +{ + type->set_code (code); + + switch (code) + { + case TYPE_CODE_STRUCT: + case TYPE_CODE_UNION: + case TYPE_CODE_NAMESPACE: + INIT_CPLUS_SPECIFIC (type); + break; + case TYPE_CODE_FLT: + TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT; + break; + case TYPE_CODE_FUNC: + INIT_FUNC_SPECIFIC (type); + break; + case TYPE_CODE_FIXED_POINT: + INIT_FIXED_POINT_SPECIFIC (type); + break; + } +} + +/* See gdbtypes.h. */ + +type * +type_allocator::new_type () +{ + if (m_smash) + return m_data.type; + + obstack *obstack = (m_is_objfile + ? &m_data.objfile->objfile_obstack + : gdbarch_obstack (m_data.gdbarch)); + + /* Alloc the structure and start off with all fields zeroed. */ + struct type *type = OBSTACK_ZALLOC (obstack, struct type); + TYPE_MAIN_TYPE (type) = OBSTACK_ZALLOC (obstack, struct main_type); + + if (m_is_objfile) + { + OBJSTAT (m_data.objfile, n_types++); + type->set_owner (m_data.objfile); + } + else + type->set_owner (m_data.gdbarch); + + /* Initialize the fields that might not be zero. */ + type->set_code (TYPE_CODE_UNDEF); + TYPE_CHAIN (type) = type; /* Chain back to itself. */ + + return type; +} + /* Allocate a new OBJFILE-associated type structure and fill it with some defaults. Space for the type structure is allocated on the objfile's objfile_obstack. */ @@ -241,6 +299,39 @@ alloc_type_copy (const struct type *type) /* See gdbtypes.h. */ +type * +type_allocator::new_type (enum type_code code, int bit, const char *name) +{ + struct type *type = new_type (); + set_type_code (type, code); + gdb_assert ((bit % TARGET_CHAR_BIT) == 0); + type->set_length (bit / TARGET_CHAR_BIT); + + if (name != nullptr) + { + obstack *obstack = (m_is_objfile + ? &m_data.objfile->objfile_obstack + : gdbarch_obstack (m_data.gdbarch)); + type->set_name (obstack_strdup (obstack, name)); + } + + return type; +} + +/* See gdbtypes.h. */ + +gdbarch * +type_allocator::arch () +{ + if (m_smash) + return m_data.type->arch (); + if (m_is_objfile) + return m_data.objfile->arch (); + return m_data.gdbarch; +} + +/* See gdbtypes.h. */ + gdbarch * type::arch () const { @@ -3344,33 +3435,6 @@ allocate_gnat_aux_type (struct type *type) *(TYPE_GNAT_SPECIFIC (type)) = gnat_aux_default; } -/* Helper function to initialize a newly allocated type. Set type code - to CODE and initialize the type-specific fields accordingly. */ - -static void -set_type_code (struct type *type, enum type_code code) -{ - type->set_code (code); - - switch (code) - { - case TYPE_CODE_STRUCT: - case TYPE_CODE_UNION: - case TYPE_CODE_NAMESPACE: - INIT_CPLUS_SPECIFIC (type); - break; - case TYPE_CODE_FLT: - TYPE_SPECIFIC_FIELD (type) = TYPE_SPECIFIC_FLOATFORMAT; - break; - case TYPE_CODE_FUNC: - INIT_FUNC_SPECIFIC (type); - break; - case TYPE_CODE_FIXED_POINT: - INIT_FIXED_POINT_SPECIFIC (type); - break; - } -} - /* Helper function to verify floating-point format and size. BIT is the type size in bits; if BIT equals -1, the size is determined by the floatformat. Returns size to be used. */ diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 52475d5afb4..613c22d676b 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -2205,6 +2205,104 @@ extern struct type *get_target_type (struct type *type); extern unsigned int type_length_units (struct type *type); +/* An object of this type is passed when allocating certain types. It + determines where the new type is allocated. Ultimately a type is + either allocated on a on an objfile obstack or on a gdbarch + obstack. However, it's also possible to request that a new type be + allocated on the same obstack as some existing type, or that a + "new" type instead overwrite a supplied type object. */ + +class type_allocator +{ +public: + + /* Create new types on OBJFILE. */ + explicit type_allocator (objfile *objfile) + : m_is_objfile (true) + { + m_data.objfile = objfile; + } + + /* Create new types on GDBARCH. */ + explicit type_allocator (gdbarch *gdbarch) + { + m_data.gdbarch = gdbarch; + } + + /* This determines whether a passed-in type should be rewritten in + place, or whether it should simply determine where the new type + is created. */ + enum type_allocator_kind + { + /* Allocate on same obstack as existing type. */ + SAME = 0, + /* Smash the existing type. */ + SMASH = 1, + }; + + /* Create new types either on the same obstack as TYPE; or if SMASH + is passed, overwrite TYPE. */ + explicit type_allocator (struct type *type, + type_allocator_kind kind = SAME) + { + if (kind == SAME) + { + if (type->is_objfile_owned ()) + { + m_data.objfile = type->objfile_owner (); + m_is_objfile = true; + } + else + m_data.gdbarch = type->arch_owner (); + } + else + { + m_smash = true; + m_data.type = type; + } + } + + /* Create new types on the same obstack as TYPE. */ + explicit type_allocator (const struct type *type) + : m_is_objfile (type->is_objfile_owned ()) + { + if (type->is_objfile_owned ()) + m_data.objfile = type->objfile_owner (); + else + m_data.gdbarch = type->arch_owner (); + } + + /* Create a new type on the desired obstack. Note that a "new" type + is not created if type-smashing was selected at construction. */ + type *new_type (); + + /* Create a new type on the desired obstack, and fill in its code, + length, and name. If NAME is non-null, it is copied to the + destination obstack first. Note that a "new" type is not created + if type-smashing was selected at construction. */ + type *new_type (enum type_code code, int bit, const char *name); + + /* Return the architecture associated with this allocator. This + comes from whatever object was supplied to the constructor. */ + gdbarch *arch (); + +private: + + /* Where the type should wind up. */ + union + { + struct objfile *objfile; + struct gdbarch *gdbarch; + struct type *type; + } m_data {}; + + /* True if this allocator uses the objfile field above. */ + bool m_is_objfile = false; + /* True if this allocator uses the type field above, indicating that + the "allocation" should be done in-place. */ + bool m_smash = false; +}; + /* * Helper function to construct objfile-owned types. */ extern struct type *init_type (struct objfile *, enum type_code, int,