From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id DA27A3857809 for ; Wed, 16 Feb 2022 20:53:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DA27A3857809 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-505-IhwwqBt-Oi-sinURhwPzFQ-1; Wed, 16 Feb 2022 15:53:43 -0500 X-MC-Unique: IhwwqBt-Oi-sinURhwPzFQ-1 Received: by mail-wm1-f71.google.com with SMTP id v130-20020a1cac88000000b0037e3d70e7e1so596434wme.1 for ; Wed, 16 Feb 2022 12:53:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5BLvDEcmlqRVnRGEpRC04j9nFeCcdtMtvl0A8lhF0Yc=; b=bAx56/jnXkYQk22TrbmfFtm3SrXmQwJka7uaUbSdY3ONKc0JkgJrWOhHqvy29unJLw YhTJrOVanVpGbslXOSEj5x6vGZpr1VGyZBRZwsx1fNyRWSOtlFlQTUpCh4j4G7tQRIR5 Nof9VzfbwzsDzDxGKlDG0/RGoe1iXWOQPjnQXRW28/lp/FtVB/Q4nRXrm0FYQHzlKHFx 2GyHKj/x/cdcmACqDesMYvJFFgCj/imvzDnSFWuAXtsu5V3J9ToGHJ0MTsQv8JzDRzQj VxsoUFaR3SBQ57KumqO3iYaCym3f7h7z1VFqA198+EKMkERSZ+oFKuE4YFtfAAMKIRbA k0CQ== X-Gm-Message-State: AOAM53070c4EWmdWdSbbyj4NHpW1dR2giKvI+zlNQROdx2j1Z6R1dJoY /5PiMXPrw+NvnudOEKteKPAoy0YWNjuk2CX2Of1dunH9R+OkZn4rUTZ9ytH6IqzezGhUexf4gl2 0BRo9SfdP+0EAMgJtQ5NgzPL5BFac+5p3mYUO+31OgmgtW+eBZbvtdVNAI8wOLMkf7hbiPA== X-Received: by 2002:a05:6000:156c:b0:1e2:eb89:9a82 with SMTP id 12-20020a056000156c00b001e2eb899a82mr3504849wrz.397.1645044821737; Wed, 16 Feb 2022 12:53:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJy+uNBtXKZvB5k0hxU/Pej87UlS9CHllIJu5YX68VVpWQ9bJIcVq95JnxJgRDOxigthECIm/w== X-Received: by 2002:a05:6000:156c:b0:1e2:eb89:9a82 with SMTP id 12-20020a056000156c00b001e2eb899a82mr3504831wrz.397.1645044821334; Wed, 16 Feb 2022 12:53:41 -0800 (PST) Received: from localhost (host86-134-151-224.range86-134.btcentralplus.com. [86.134.151.224]) by smtp.gmail.com with ESMTPSA id i2sm24613323wmq.45.2022.02.16.12.53.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 12:53:40 -0800 (PST) From: Andrew Burgess To: binutils@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/3] opcodes/i386: partially implement disassembler style support Date: Wed, 16 Feb 2022 20:53:31 +0000 Message-Id: <11996f886e69218629abf81f8041269e8740a60e.1645043588.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Wed, 16 Feb 2022 20:53:46 -0000 This commit adds partial support for disassembler styling in the i386 disassembler. The i386 disassembler collects the instruction arguments into an array of strings, and then loops over the array printing the arguments out later on. The problem is that by the time we print the arguments out it's not obvious what the type of each argument is. Obviously this can be fixed, but I'd like to not do that as part of this commit, rather, I'd prefer to keep this commit as small as possible to get the basic infrastructure in place, then we can improve on this, to add additional styling, in later commits. For now then, I think this commit should correctly style mnemonics, some immediates, and comments. Everything else will be printed as plain text, which will include most instruction arguments, unless the argument is printed as a symbol, by calling the print_address_func callback. Ignoring colours, there should be no other user visible changes in the output of the disassembler in either objdump or gdb. opcodes/ChangeLog: * disassembler.c (disassemble_init_for_target): Set created_styled_output for i386 based targets. * i386-dis.c: Changed throughout to use fprintf_styled_func instead of fprintf_func. --- opcodes/disassemble.c | 9 +++++- opcodes/i386-dis.c | 71 +++++++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c index b8eed9695ea..81a90f20a9c 100644 --- a/opcodes/disassemble.c +++ b/opcodes/disassemble.c @@ -632,7 +632,14 @@ disassemble_init_for_target (struct disassemble_info * info) info->disassembler_needs_relocs = true; break; #endif - +#ifdef ARCH_i386 + case bfd_arch_i386: + case bfd_arch_iamcu: + case bfd_arch_l1om: + case bfd_arch_k1om: + info->created_styled_output = true; + break; +#endif #ifdef ARCH_ia64 case bfd_arch_ia64: info->skip_zeroes = 16; diff --git a/opcodes/i386-dis.c b/opcodes/i386-dis.c index a30bda0633b..f9a2e3d6357 100644 --- a/opcodes/i386-dis.c +++ b/opcodes/i386-dis.c @@ -9402,8 +9402,8 @@ print_insn (bfd_vma pc, instr_info *ins) if (ins->address_mode == mode_64bit && sizeof (bfd_vma) < 8) { - (*ins->info->fprintf_func) (ins->info->stream, - _("64-bit address is disabled")); + (*ins->info->fprintf_styled_func) (ins->info->stream, dis_style_text, + _("64-bit address is disabled")); return -1; } @@ -9456,12 +9456,18 @@ print_insn (bfd_vma pc, instr_info *ins) { name = prefix_name (ins, priv.the_buffer[0], priv.orig_sizeflag); if (name != NULL) - (*ins->info->fprintf_func) (ins->info->stream, "%s", name); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "%s", name); else { /* Just print the first byte as a .byte instruction. */ - (*ins->info->fprintf_func) (ins->info->stream, ".byte 0x%x", - (unsigned int) priv.the_buffer[0]); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, ".byte"); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_text, " "); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_immediate, "0x%x", + (unsigned int) priv.the_buffer[0]); } return 1; @@ -9479,10 +9485,10 @@ print_insn (bfd_vma pc, instr_info *ins) for (i = 0; i < (int) ARRAY_SIZE (ins->all_prefixes) && ins->all_prefixes[i]; i++) - (*ins->info->fprintf_func) (ins->info->stream, "%s%s", - i == 0 ? "" : " ", - prefix_name (ins, ins->all_prefixes[i], - sizeflag)); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "%s%s", + (i == 0 ? "" : " "), prefix_name (ins, ins->all_prefixes[i], + sizeflag)); return i; } @@ -9497,10 +9503,15 @@ print_insn (bfd_vma pc, instr_info *ins) /* Handle ins->prefixes before fwait. */ for (i = 0; i < ins->fwait_prefix && ins->all_prefixes[i]; i++) - (*ins->info->fprintf_func) (ins->info->stream, "%s ", - prefix_name (ins, ins->all_prefixes[i], - sizeflag)); - (*ins->info->fprintf_func) (ins->info->stream, "fwait"); + { + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "%s", + prefix_name (ins, ins->all_prefixes[i], sizeflag)); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, " "); + } + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "fwait"); return i + 1; } @@ -9649,14 +9660,16 @@ print_insn (bfd_vma pc, instr_info *ins) are all 0s in inverted form. */ if (ins->need_vex && ins->vex.register_specifier != 0) { - (*ins->info->fprintf_func) (ins->info->stream, "(bad)"); + (*ins->info->fprintf_styled_func) (ins->info->stream, dis_style_text, + "(bad)"); return ins->end_codep - priv.the_buffer; } /* If EVEX.z is set, there must be an actual mask register in use. */ if (ins->vex.zeroing && ins->vex.mask_register_specifier == 0) { - (*ins->info->fprintf_func) (ins->info->stream, "(bad)"); + (*ins->info->fprintf_styled_func) (ins->info->stream, dis_style_text, + "(bad)"); return ins->end_codep - priv.the_buffer; } @@ -9667,7 +9680,8 @@ print_insn (bfd_vma pc, instr_info *ins) the encoding invalid. Most other PREFIX_OPCODE rules still apply. */ if (ins->need_vex ? !ins->vex.prefix : !(ins->prefixes & PREFIX_DATA)) { - (*ins->info->fprintf_func) (ins->info->stream, "(bad)"); + (*ins->info->fprintf_styled_func) (ins->info->stream, + dis_style_text, "(bad)"); return ins->end_codep - priv.the_buffer; } ins->used_prefixes |= PREFIX_DATA; @@ -9694,7 +9708,8 @@ print_insn (bfd_vma pc, instr_info *ins) || (ins->vex.evex && dp->prefix_requirement != PREFIX_DATA && !ins->vex.w != !(ins->used_prefixes & PREFIX_DATA))) { - (*ins->info->fprintf_func) (ins->info->stream, "(bad)"); + (*ins->info->fprintf_styled_func) (ins->info->stream, + dis_style_text, "(bad)"); return ins->end_codep - priv.the_buffer; } break; @@ -9744,13 +9759,17 @@ print_insn (bfd_vma pc, instr_info *ins) if (name == NULL) abort (); prefix_length += strlen (name) + 1; - (*ins->info->fprintf_func) (ins->info->stream, "%s ", name); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "%s", name); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_text, " "); } /* Check maximum code length. */ if ((ins->codep - ins->start_codep) > MAX_CODE_LENGTH) { - (*ins->info->fprintf_func) (ins->info->stream, "(bad)"); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_text, "(bad)"); return MAX_CODE_LENGTH; } @@ -9758,7 +9777,8 @@ print_insn (bfd_vma pc, instr_info *ins) for (i = strlen (ins->obuf) + prefix_length; i < 6; i++) oappend (ins, " "); oappend (ins, " "); - (*ins->info->fprintf_func) (ins->info->stream, "%s", ins->obuf); + (*ins->info->fprintf_styled_func) + (ins->info->stream, dis_style_mnemonic, "%s", ins->obuf); /* The enter and bound instructions are printed with operands in the same order as the intel book; everything else is printed in reverse order. */ @@ -9797,7 +9817,8 @@ print_insn (bfd_vma pc, instr_info *ins) if (*op_txt[i]) { if (needcomma) - (*ins->info->fprintf_func) (ins->info->stream, ","); + (*ins->info->fprintf_styled_func) (ins->info->stream, + dis_style_text, ","); if (ins->op_index[i] != -1 && !ins->op_riprel[i]) { bfd_vma target = (bfd_vma) ins->op_address[ins->op_index[i]]; @@ -9813,14 +9834,18 @@ print_insn (bfd_vma pc, instr_info *ins) (*ins->info->print_address_func) (target, ins->info); } else - (*ins->info->fprintf_func) (ins->info->stream, "%s", op_txt[i]); + (*ins->info->fprintf_styled_func) (ins->info->stream, + dis_style_text, "%s", + op_txt[i]); needcomma = 1; } for (i = 0; i < MAX_OPERANDS; i++) if (ins->op_index[i] != -1 && ins->op_riprel[i]) { - (*ins->info->fprintf_func) (ins->info->stream, " # "); + (*ins->info->fprintf_styled_func) (ins->info->stream, + dis_style_comment_start, + " # "); (*ins->info->print_address_func) ((bfd_vma) (ins->start_pc + (ins->codep - ins->start_codep) + ins->op_address[ins->op_index[i]]), ins->info); -- 2.25.4