From patchwork Tue Nov 15 04:52:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 20189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a40e:b0:83:7221:86ba with SMTP id ck14csp3424327dyb; Mon, 14 Nov 2022 21:00:59 -0800 (PST) X-Google-Smtp-Source: AA0mqf5MVE8n/GEckWDOcOgI3mmiK/plsVfcnU+cW1bzMP4R0AfTiM7fl+T5CE/gwDuArQ65hK6G X-Received: by 2002:a05:6402:5cf:b0:462:845:ba98 with SMTP id n15-20020a05640205cf00b004620845ba98mr13343544edx.12.1668488459057; Mon, 14 Nov 2022 21:00:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668488459; cv=none; d=google.com; s=arc-20160816; b=pk4dens6A7J3o6thGRqhUNMQs0J7Oqdv8jxfBcFnQ4G9TQAeBfVMvE5crJFvR6NFGA OxQcxUuzxuoFpxV+1ezZ/BpvQJQPxex0YnCOBCsRVIisnRs1r9ZGUz+bvMoWgES+ZKFY bIgJLeMz0af+dLa+VK76WLpYH2JQtaos47fcPrCt6U+o8hlNhjCBVtkd4QOPJuQ0x3YT fv9oeRTCuez9s8+Kv47AqLdD1hzGy3fg9mw5jilXSYXZSkGHkWXwkQmtcf5SeDiYqAUc id906impAv2S0y/SeUPFWSfAFTTZGGTDP7PyhchtT0NnhYcwH2+dJZFXdc26bsZH+fLU hNqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=MwhMjvwqPE8K+BtqsEf7oahoAH6kD0V9tM9JHXWR+t0=; b=NKKYEDg+KIom4b35eMHuDpre5FYBBHvanPoPXUndwhZNa75mGLQ2mVg47PFEIwj8Ky sZ1f8eN4LM48yclaiCfUNgN4EjuYy8HBZG8Unxu8MFuJFWhwLVpoKC0EqK4ZqeBq8UZ0 UyxXVO4vDnT1to39756i07BcMmLsEhTKSyJA42E2AWX2KzXENuQEXqiw8a7gg0cD5aan zm3bM/Cjg7glqFaZilim2NZpHbRlMq7yGedbTUeB2RE3SciWGRQs/UU64ZuTz/p/fFp9 qsXEA11W7MfrwWuJ1L2Y/fPMMrjWq99fvvlygjEs6Ym+uEsYCpC5cUUBI6DmL1GcjeYa Uv8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OhjfAH2v; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v14-20020a056402348e00b00461e63fe88fsi12538452edc.596.2022.11.14.21.00.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Nov 2022 21:00:59 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=OhjfAH2v; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 88B8E389839F for ; Tue, 15 Nov 2022 04:59:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 88B8E389839F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668488358; bh=MwhMjvwqPE8K+BtqsEf7oahoAH6kD0V9tM9JHXWR+t0=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=OhjfAH2vPjVY2cADbxsE/A3WXhRxCIX1lbE4SzeBYPxmzPR3eHL6uSfWz4gACFNSe NOwHrFUq7iJmisX4lJgsit3izTvIsYpe1rdmXrEX3w3MUPBQlfL19lF+buIQHhO6LI sZeIKx8DVD+eI3rzUM0FvxAE1yT6G0weqzH+shZo= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-sender-0.a4lg.com (mail-sender.a4lg.com [153.120.152.154]) by sourceware.org (Postfix) with ESMTPS id 7ED223885C36 for ; Tue, 15 Nov 2022 04:59:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7ED223885C36 Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id D700A300089; Tue, 15 Nov 2022 04:59:07 +0000 (UTC) To: Tsukasa OI , Nelson Chu , Kito Cheng , Palmer Dabbelt Cc: binutils@sourceware.org Subject: [PATCH 09/11] RISC-V: Reorganize disassembler state initialization Date: Tue, 15 Nov 2022 04:52:52 +0000 Message-Id: <12cd8820841f695708875206b6461b6322c74428.1668487922.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tsukasa OI via Binutils From: Tsukasa OI Reply-To: Tsukasa OI Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749536954487615344?= X-GMAIL-MSGID: =?utf-8?q?1749536954487615344?= The current disassembler repeatedly checks current state per instruction: - Whether riscv_gpr_names is initialized to a non-NULL value - Whether the Zfinx extension is available - Whether the hash table is initialized ... but they are not frequently changed. riscv_gpr_names is initialized to a non-NULL value when - The first disassembler option is specified, or - Not initialized with disassembler options (in the first print_insn_riscv function call). We can safely initialize the default disassembler options prior to the print_insn_riscv function call and this per-instruction checking of riscv_gpr_names can be safely removed. The opcode hash table initialization will be taken care with a later commit. To group disassembler state initialization, this commit utilizes the disassemble_init_for_target function. As a side effect, this will also fix a potential issue when disassembler options is set and then unset on GDB. This idea is based on opcodes/ppc-dis.c and opcodes/wasm32-dis.c. New callback function init_riscv_dis_state_for_arch_and_options is called when either the architecture string or an option is possibly changed. We can now group the disassembler state initialization together. It makes state initialization clearer and makes further changes easier. In performance perspective, this commit has various effects (-5% to 6%). However, this commit makes implementing large optimizations easier as well as "RISC-V: Split match/print steps on disassembler". include/ChangeLog: * dis-asm.h (disassemble_init_riscv): Add declaration of disassemble_init_riscv. opcodes/ChangeLog: * disassemble.c (disassemble_init_for_target): Call disassemble_init_riscv to group state initialization together. * riscv-dis.c (xlen_by_mach, xlen_by_elf): New variables to store environment-inferred XLEN. (is_numeric): New. Instead of directly setting riscv_{gpr,fpr}_names, use this to store an option. (update_riscv_dis_xlen): New function to set actual XLEN from xlen_by_mach and xlen_by_elf variables. (init_riscv_dis_state_for_arch_and_options): New callback function called when either the architecture or an option is changed. Set riscv_{gpr,fpr}_names here. (set_default_riscv_dis_options): Initialize is_numeric instead of riscv_gpr_names and riscv_fpr_names. (parse_riscv_dis_option_without_args): When the "numeric" option is specified, write to is_numeric instead of register names. (parse_riscv_dis_options): Suppress setting the default options here and let disassemble_init_riscv to initialize them. (riscv_disassemble_insn): Move probing Zfinx and setting XLEN portions to init_riscv_dis_state_for_arch_and_options and update_riscv_dis_xlen. (riscv_get_map_state): If a mapping symbol with ISA string is suitable, call init_riscv_dis_state_for_arch_and_options function to update disassembler state. (print_insn_riscv): Update XLEN only if we haven't guessed correct XLEN for the disassembler. Stop checking disassembler options for every instruction and let disassemble_init_riscv to parse options. (riscv_get_disassembler): Call init_riscv_dis_state_for_arch_and_options because the architecture string is updated here. (disassemble_init_riscv): New function to initialize the structure, reset/guess correct XLEN and reset/parse disassembler options. --- include/dis-asm.h | 1 + opcodes/disassemble.c | 2 +- opcodes/riscv-dis.c | 112 +++++++++++++++++++++++++++++------------- 3 files changed, 79 insertions(+), 36 deletions(-) diff --git a/include/dis-asm.h b/include/dis-asm.h index 4921c040710..11537b432ff 100644 --- a/include/dis-asm.h +++ b/include/dis-asm.h @@ -393,6 +393,7 @@ extern bool arm_symbol_is_valid (asymbol *, struct disassemble_info *); extern bool csky_symbol_is_valid (asymbol *, struct disassemble_info *); extern bool riscv_symbol_is_valid (asymbol *, struct disassemble_info *); extern void disassemble_init_powerpc (struct disassemble_info *); +extern void disassemble_init_riscv (struct disassemble_info *); extern void disassemble_init_s390 (struct disassemble_info *); extern void disassemble_init_wasm32 (struct disassemble_info *); extern void disassemble_init_nds32 (struct disassemble_info *); diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c index 0a8f2da629f..704fb476ea9 100644 --- a/opcodes/disassemble.c +++ b/opcodes/disassemble.c @@ -717,7 +717,7 @@ disassemble_init_for_target (struct disassemble_info * info) #endif #ifdef ARCH_riscv case bfd_arch_riscv: - info->symbol_is_valid = riscv_symbol_is_valid; + disassemble_init_riscv (info); info->created_styled_output = true; break; #endif diff --git a/opcodes/riscv-dis.c b/opcodes/riscv-dis.c index 316c6c97607..76387efbe4b 100644 --- a/opcodes/riscv-dis.c +++ b/opcodes/riscv-dis.c @@ -35,6 +35,12 @@ /* Current XLEN for the disassembler. */ static unsigned xlen = 0; +/* XLEN as inferred by the machine architecture. */ +static unsigned xlen_by_mach = 0; + +/* XLEN as inferred by ELF header. */ +static unsigned xlen_by_elf = 0; + /* Default ISA specification version (constant as of now). */ static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1; @@ -72,6 +78,48 @@ static const char * const *riscv_fpr_names; /* If set, disassemble as most general instruction. */ static bool no_aliases = false; + +/* If set, disassemble with numeric register names. */ +static bool is_numeric = false; + + +/* Guess and update current XLEN. */ + +static void +update_riscv_dis_xlen (struct disassemble_info *info) +{ + /* Set XLEN with following precedence rules: + 1. BFD machine architecture set by either: + a. -m riscv:rv[32|64] option (GDB: set arch riscv:rv[32|64]) + b. ELF class in actual ELF header (only on RISC-V ELF) + This is only effective if XLEN-specific BFD machine architecture is + chosen. If XLEN-neutral (like riscv), BFD machine architecture is + ignored on XLEN selection. + 2. ELF class in dummy ELF header. */ + if (xlen_by_mach != 0) + xlen = xlen_by_mach; + else if (xlen_by_elf != 0) + xlen = xlen_by_elf; + else if (info != NULL && info->section != NULL) + { + Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner); + xlen = xlen_by_elf = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32; + } +} + +/* Initialization (for arch and options). */ + +static void +init_riscv_dis_state_for_arch_and_options (void) +{ + /* Set GPR register names to disassemble. */ + riscv_gpr_names = is_numeric ? riscv_gpr_names_numeric : riscv_gpr_names_abi; + /* Set FPR register names to disassemble. */ + riscv_fpr_names + = !riscv_subset_supports (&riscv_rps_dis, "zfinx") + ? (is_numeric ? riscv_fpr_names_numeric : riscv_fpr_names_abi) + : riscv_gpr_names; +} /* Set default RISC-V disassembler options. */ @@ -79,9 +127,8 @@ static bool no_aliases = false; static void set_default_riscv_dis_options (void) { - riscv_gpr_names = riscv_gpr_names_abi; - riscv_fpr_names = riscv_fpr_names_abi; no_aliases = false; + is_numeric = false; } /* Parse RISC-V disassembler option (without arguments). */ @@ -92,10 +139,7 @@ parse_riscv_dis_option_without_args (const char *option) if (strcmp (option, "no-aliases") == 0) no_aliases = true; else if (strcmp (option, "numeric") == 0) - { - riscv_gpr_names = riscv_gpr_names_numeric; - riscv_fpr_names = riscv_fpr_names_numeric; - } + is_numeric = true; else return false; return true; @@ -163,8 +207,6 @@ parse_riscv_dis_options (const char *opts_in) { char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts; - set_default_riscv_dis_options (); - for ( ; opt_end != NULL; opt = opt_end + 1) { if ((opt_end = strchr (opt, ',')) != NULL) @@ -705,25 +747,6 @@ riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info) matched_op = NULL; op = riscv_hash[OP_HASH_IDX (word)]; - /* If XLEN is not known, get its value from the ELF class. */ - if (info->mach == bfd_mach_riscv64) - xlen = 64; - else if (info->mach == bfd_mach_riscv32) - xlen = 32; - else if (info->section != NULL) - { - Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner); - xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32; - } - - /* If arch has the Zfinx extension, replace FPR with GPR. */ - if (riscv_subset_supports (&riscv_rps_dis, "zfinx")) - riscv_fpr_names = riscv_gpr_names; - else - riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi - ? riscv_fpr_names_abi - : riscv_fpr_names_numeric; - for (; op && op->name; op++) { /* Does the opcode match? */ @@ -867,6 +890,7 @@ riscv_get_map_state (int n, { riscv_release_subset_list (&riscv_subsets); riscv_parse_subset (&riscv_rps_dis, arch); + init_riscv_dis_state_for_arch_and_options (); } return true; } @@ -1063,14 +1087,9 @@ print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) enum riscv_seg_mstate mstate; int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *); - if (info->disassembler_options != NULL) - { - parse_riscv_dis_options (info->disassembler_options); - /* Avoid repeatedly parsing the options. */ - info->disassembler_options = NULL; - } - else if (riscv_gpr_names == NULL) - set_default_riscv_dis_options (); + /* Guess and update XLEN if we haven't determined it yet. */ + if (xlen == 0) + update_riscv_dis_xlen (info); mstate = riscv_search_mapping_symbol (memaddr, info); @@ -1132,9 +1151,32 @@ riscv_get_disassembler (bfd *abfd) riscv_release_subset_list (&riscv_subsets); riscv_parse_subset (&riscv_rps_dis, default_arch); + init_riscv_dis_state_for_arch_and_options (); return print_insn_riscv; } +/* Initialize disassemble_info and parse options. */ + +void +disassemble_init_riscv (struct disassemble_info *info) +{ + info->symbol_is_valid = riscv_symbol_is_valid; + /* Clear previous XLEN and guess by mach. */ + xlen = 0; + xlen_by_mach = 0; + xlen_by_elf = 0; + if (info->mach == bfd_mach_riscv64) + xlen_by_mach = 64; + else if (info->mach == bfd_mach_riscv32) + xlen_by_mach = 32; + update_riscv_dis_xlen (info); + /* Parse disassembler options. */ + set_default_riscv_dis_options (); + if (info->disassembler_options != NULL) + parse_riscv_dis_options (info->disassembler_options); + init_riscv_dis_state_for_arch_and_options (); +} + /* Prevent use of the fake labels that are generated as part of the DWARF and for relaxable relocations in the assembler. */