From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id 4A0E13857C49 for ; Fri, 29 Apr 2022 18:16:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4A0E13857C49 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 23THwc3E015405 for ; Fri, 29 Apr 2022 18:16:48 GMT Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3fmb9aydwt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 29 Apr 2022 18:16:48 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.16.1.2/8.16.1.2) with SMTP id 23TIGfK7005793 for ; Fri, 29 Apr 2022 18:16:46 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2172.outbound.protection.outlook.com [104.47.58.172]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com with ESMTP id 3fp5yqfcam-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Fri, 29 Apr 2022 18:16:45 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IE4M3D/ZtWRFDKNxeHhB5DyNVRsLOEoaugB0zdT+MCCp2/Riuj3J6Sv3PbCoaR0zA0cWphgoEmHaLQGnQtXYFECfzyMePFmbO2DMotV6d0bd/B7bi3LyotU2ugWLDCxpyBi97JE4O00VxwYsktDd+7hufpg49NBPOdwEGJe2R3sJxGenw32lm2UKgOcCZY0zWtHQlFRvL5YL+x8aZFPLY9duR7p4/VFjXGtwN9GQu1i6MFuWUgvIxaJqmfBOOA00ZzmMmeMNemG312gGIkSoOXGpq3YW4KNJagB2VAyV3BQigrx0RxtJ3GmCoJzaUIHPPTc7b3s5BgG7/VqnfC+WJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Uykl3RtDmQTG1tVWljdtGLfyxrGpetPZK0BzgjJ1GZk=; b=erFd8Z+pks91S9uw2eQpvX+R1vSrXkzbU3NWT2dBwWTcN+AVADukZMxkhlEToJHf0Th+XY7L6Y2AMhhntWdoEXq3cXu/CUatIFwiX1OmKLMUQI8Jb3Qv0v8NgsmTzGTemzklvwC40GyFA3fl8A5oqK9dxXIYjwz3sZqEVYX6Ft/a3EVxWbVXsnKFbwu/H5xgCD/VforPUeUwXTuNjWc4S7PC3zMVm9gia91bf/wfpHVxlNOK6wfGVIVI0nSa5bLxrxhP3mq1mbnm7sr0A1gqQ1MQQIFi22B7ezkAn0LY7yuZ87H8yYzBhz6TlQRQ+nSvexvVEFH2F86318pRJ37/dQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none Received: from SA2PR10MB4636.namprd10.prod.outlook.com (2603:10b6:806:11e::10) by CY4PR1001MB2198.namprd10.prod.outlook.com (2603:10b6:910:42::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5186.15; Fri, 29 Apr 2022 18:16:43 +0000 Received: from SA2PR10MB4636.namprd10.prod.outlook.com ([fe80::c862:5f33:53fb:aa5]) by SA2PR10MB4636.namprd10.prod.outlook.com ([fe80::c862:5f33:53fb:aa5%6]) with mapi id 15.20.5206.014; Fri, 29 Apr 2022 18:16:43 +0000 Message-ID: <4e7afdd1-6721-2b47-6eab-53f8f0a0dd46@oracle.com> Date: Fri, 29 Apr 2022 11:16:40 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH 2/2] libopcodes: extend the styling within the i386 disassembler Content-Language: en-US To: binutils@sourceware.org References: <388c1dd1235a3c95aefc7caee5726b869b6894e0.1651239378.git.aburgess@redhat.com> From: Vladimir Mezentsev In-Reply-To: <388c1dd1235a3c95aefc7caee5726b869b6894e0.1651239378.git.aburgess@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: SJ0PR13CA0186.namprd13.prod.outlook.com (2603:10b6:a03:2c3::11) To SA2PR10MB4636.namprd10.prod.outlook.com (2603:10b6:806:11e::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9c743ce8-9eb4-4d71-ba3c-08da2a0c6a32 X-MS-TrafficTypeDiagnostic: CY4PR1001MB2198:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 23gP7XyfetgEqrEi+c3vhHOsJYuZw3Dlb55fXFk7JHUR8mcQph+3tI4Sk/g9gHpWf/KVR8UkgjpO4qQPtlNHmUJbvXfHkcW0h7+Xpp9yKI1Vgl+23j3OneKtoS7rOcQFI6xSU2cJIy81qqUvWdjFVuGWTAvcN3/MlzWdsRvqwBwSV6KAeNqDr+qEPs38fDl0ZKhBN0saSsUuqc4hqfaceBpN00gsfOHSjnIPRMKaUGAecvRIiyN8DAK5kKF3e6IQkXX1GnoIoOlvfCeYQVGh6ika4DHrqDoFUN5ObjUSkUnj2YDKnyOWZdap8HkIEaTgCL3o5Z3p29TECou7Rskx4dQtpgGm42UmzJ7KJGezLCvWuFORow0qWfDiDAqElWz239l3RtxIKhzApX4V7lDc8r2rq8qmsoO8GXYz5+LTSktJzRa8Geaj2I1Mak8VaNwQtxf9HzkOmNZ+nthWjl+2fPIKPlxsiv/uLgJR797987PhkloeOGUbE4GWjz9ntb1o18RJ40pPM+Jt0QckA5LZP3yqWuS3mhxR+2VYRq+tvNO18+yVifTy/TBRcXOQ+Zl3THTpPeczIcrBliLQwklVSoIy8PaQtRUW8b2YzF4Ii9cSwE4IzueuvbI/AWe0/bcFF01JWsrmF9fvYhx2k/8xMRvfGVTeQX6GOC8x0sprE3IpFObiXGYgIrUmnlcV6nQLTZjJx9zFXd1Ob9hpXUiCYvMyLKFAgeHiiGnQJ7Dx1sQ= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SA2PR10MB4636.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(366004)(31686004)(38100700002)(83380400001)(8676002)(5660300002)(66476007)(8936002)(31696002)(66946007)(66556008)(44832011)(2616005)(508600001)(86362001)(53546011)(2906002)(186003)(6512007)(6506007)(36756003)(316002)(6486002)(6916009)(43740500002)(45980500001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?NXU3THdoRUM4bEw5M3B5R0prdTc0cmF3UVJQN3E0Zmp0aEVoR2R1RmI0Q29K?= =?utf-8?B?NlNOalV6OUttbzl3dkVGVWw2UzN4bUxNQkI5bWttVk0xTmpnZjJmdG1WZ056?= =?utf-8?B?d0crd0twVDlJMHJqaFFFZzd3WnFJQUVMVTBWM3ZxWWxIcTE1ZWdnMzBWVHcw?= =?utf-8?B?eE0wSGdjWkVTKzJFWk5KeG83OHhGdW9nS3p4Q2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sRHBLNG5STEFpVDJONzJrakJpVFVpeFpSQlE3N3R5WWVKWXRwWWRuUzJM?= =?utf-8?B?WS9SZ2NzZkt0Q2ZuV0ViNzFpc3dmUVhrMDUxaUlRZmhuTUJ0anFveVdoZnhJ?= =?utf-8?B?YkY0cmVlaEtweHRsOGxHVXFGblhHV290TUxnam9KYzdscHFONm5naFdDanlo?= =?utf-8?B?Ti9HQXo5U3MrNFhHYldzcVBLWFd1Ylg2TjdhS1pzQm9paEpmL25ZM0gzWHJ4?= =?utf-8?B?SDk4NkpySTQyODBZYWJ4N2RNZFR3clFYclVnRzY5M0pncVkzN0R0R1AzZGYv?= =?utf-8?B?NHZDbDN0SXZPdjNBSFBOZGczVFVVU3VubWE0SGFscGN6WmxwRTN6ZjdCTEs4?= =?utf-8?Q?uCiodKQg+hFu8DtQv0DzznnVb57SNwN6neCeKYyeNy+ng?= X-MS-Exchange-AntiSpam-MessageData-1: nvNKClneVflWSw== X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c743ce8-9eb4-4d71-ba3c-08da2a0c6a32 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4636.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Apr 2022 18:16:43.7279 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: x41J8THnEAcLVRezVKZrKdlrB4HAMdyYX3JTE3MwATJKx50aXFoAYsjn492/fQpPF37u17VXZZYscwEGpuWtCZ/b0pUqgpT0flz/xEQoGEM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR1001MB2198 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.486, 18.0.858 definitions=2022-04-29_07:2022-04-28, 2022-04-29 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 mlxlogscore=999 mlxscore=0 suspectscore=0 malwarescore=0 spamscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2204290093 X-Proofpoint-ORIG-GUID: WO8SDaJ96ytKNCB7I-K_x9Gqcwgb3dul X-Proofpoint-GUID: WO8SDaJ96ytKNCB7I-K_x9Gqcwgb3dul X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP 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: Fri, 29 Apr 2022 18:16:51 -0000 On 4/29/22 06:42, Andrew Burgess via Binutils wrote: > The i386 disassembler is pretty complex. Most disassembly is done > indirectly; operands are built into buffers within a struct instr_info > instance, before finally being printed later in the disassembly > process. > > Sometimes the operand buffers are built in a different order to the > order in which they will eventually be printed. > > Each operand can contain multiple components, e.g. multiple registers, > immediates, other textual elements (commas, brackets, etc). > > When looking for how to apply styling I guess the ideal solution would > be to move away from the operands being a single string that is built > up, and instead have each operand be a list of "parts", where each > part is some text and a style. Then, when we eventually print the > operand we would loop over the parts and print each part with the > correct style. > > But it feels like a huge amount of work to move from where we are > now to that potentially ideal solution. Plus, the above solution > would be pretty complex. > > So, instead I propose a .... different solution here, one that works > with the existing infrastructure. > > As each operand is built up, piece be piece, we pass through style > information. This style information is then encoded into the operand > buffer (see below for details). After this the code can continue to > operate as it does right now in order to manage the set of operand > buffers. > > Then, as each operand is printed we can split the operand buffer into > chunks at the style marker boundaries, with each chunk being printed > in the correct style. > > For encoding the style information I use the format "~%x~". As far as > I can tell the '~' is not otherwise used in the i386 disassembler, so > this should serve as a unique marker. To speed up writing and then > reading the style markers, I take advantage of the fact that there are > less than 16 styles so I know the '%x' will only ever be a single hex > character. > > In some (not very scientific) benchmarking on my machine, > disassembling a reasonably large (142M) shared library, I'm not seeing > any significant slow down in disassembler speed with this change. > > Most instructions are now being fully syntax highlighted when I > disassemble using the --disassembler-color=extended-color option. I'm > sure that there are probably still a few corner cases that need fixing > up, but we can come back to them later I think. > > When disassembler syntax highlighting is not being used, then there > should be no user visible changes after this commit. > --- > opcodes/i386-dis.c | 571 ++++++++++++++++++++++++++------------------- > 1 file changed, 332 insertions(+), 239 deletions(-) > > diff --git a/opcodes/i386-dis.c b/opcodes/i386-dis.c > index 1e3266329c1..c94d316a03f 100644 > --- a/opcodes/i386-dis.c > +++ b/opcodes/i386-dis.c > @@ -42,12 +42,14 @@ > #include > typedef struct instr_info instr_info; > > +#define STYLE_BUFFER_SIZE 10 > + > static int print_insn (bfd_vma, instr_info *); > static void dofloat (instr_info *, int); > static void OP_ST (instr_info *, int, int); > static void OP_STi (instr_info *, int, int); > static int putop (instr_info *, const char *, int); > -static void oappend (instr_info *, const char *); > +static void oappend (instr_info *, const char *, enum disassembler_style); > static void append_seg (instr_info *); > static void OP_indirE (instr_info *, int, int); > static void print_operand_value (instr_info *, char *, int, bfd_vma); > @@ -166,6 +168,8 @@ struct instr_info > char *obufp; > char *mnemonicendp; > char scratchbuf[100]; > + char style_buffer[STYLE_BUFFER_SIZE]; I don't see where  style_buffer is used. It looks like style_buffer and  STYLE_BUFFER_SIZE are not needed. > + char staging_area[100];  staging_area is used only in i386_dis_printf(). Why this is not a local array inside i386_dis_printf() ? > unsigned char *start_codep; > unsigned char *insn_codep; > unsigned char *codep; > @@ -248,6 +252,8 @@ struct instr_info > > enum x86_64_isa isa64; > > + int (*printf) (instr_info *ins, enum disassembler_style style, > + const char *fmt, ...) ATTRIBUTE_FPTR_PRINTF_3; > }; > > /* Mark parts used in the REX prefix. When we are testing for > @@ -9300,9 +9306,73 @@ get_sib (instr_info *ins, int sizeflag) > /* Like oappend (below), but S is a string starting with '%'. > In Intel syntax, the '%' is elided. */ > static void > -oappend_maybe_intel (instr_info *ins, const char *s) > +oappend_maybe_intel (instr_info *ins, const char *s, > + enum disassembler_style style) > { > - oappend (ins, s + ins->intel_syntax); > + oappend (ins, s + ins->intel_syntax, style); > +} > + > +/* Wrap around a call to INS->info->fprintf_styled_func, printing FMT. > + STYLE is the default style to use in the fprintf_styled_func calls, > + however, FMT might include embedded style markers (see oappend_style), > + these embedded markers are not printed, but instead change the style > + used in the next fprintf_styled_func call. > + > + Return non-zero to indicate the print call was a success. */ > + > +static int ATTRIBUTE_PRINTF_3 > +i386_dis_printf (instr_info *ins, enum disassembler_style style, > + const char *fmt, ...) > +{ > + va_list ap; > + enum disassembler_style curr_style = style; > + char *start, *curr; > + > + va_start (ap, fmt); > + vsnprintf (ins->staging_area, 100, fmt, ap); Maybe sizeof (ins->staging_area) instead of 100 is better. As I wrote above,  staging_area  can be declared inside i386_dis_printf. -Vladimir