From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13325 invoked by alias); 21 May 2012 00:31:17 -0000 Received: (qmail 13301 invoked by uid 22791); 21 May 2012 00:31:14 -0000 X-SWARE-Spam-Status: No, hits=-5.5 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,KHOP_RCVD_TRUST,KHOP_THREADED,RCVD_IN_DNSWL_LOW,RCVD_IN_HOSTKARMA_YE,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-vb0-f41.google.com (HELO mail-vb0-f41.google.com) (209.85.212.41) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 21 May 2012 00:30:59 +0000 Received: by vbbey12 with SMTP id ey12so4222031vbb.0 for ; Sun, 20 May 2012 17:30:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding:x-system-of-record :x-gm-message-state; bh=RQiDO89zA6juU/+PT/o9wu16kh9ZjSiml3OCebynxUs=; b=BOLwUG7+90XOalIcAf+JIrSfEGcc/Mfd9ib/hgWIFc1o+8Dt7vUPW8YqOok3kodmFc /hQ6whBB1oISVTYEwmxbJ4pqLeNXSFtciCANhdAY3q0rqlnZTocUjXoUw6QtgA1BCtxs 5W0n+FB4nR3S3thokumYVbryPi8uge8p7qLUP3mUmH4TkN/6bRKBY2jKKEljQIJ79XXg 0XDyHszxX2lXVh0FHVgsp18CEEo4lru8nmiDhn8dpWFkbD4N9Nve4Uh5V+fiomcwL2Q2 S8dHS35crOCeQf7BYGz8fz3N1/FPGYW63eYkZBYz9uDgOXy8nepSUOxmCGRB2Dcp9VO8 tcFw== Received: by 10.52.93.73 with SMTP id cs9mr8819069vdb.78.1337560258757; Sun, 20 May 2012 17:30:58 -0700 (PDT) MIME-Version: 1.0 Received: by 10.52.93.73 with SMTP id cs9mr8819062vdb.78.1337560258623; Sun, 20 May 2012 17:30:58 -0700 (PDT) Received: by 10.52.172.166 with HTTP; Sun, 20 May 2012 17:30:58 -0700 (PDT) In-Reply-To: <20120517182920.64FAE2461B2@ruffy.mtv.corp.google.com> References: <20120517182920.64FAE2461B2@ruffy.mtv.corp.google.com> Date: Mon, 21 May 2012 00:31:00 -0000 Message-ID: Subject: Re: [RFA] leb128.h: New file. From: Doug Evans To: gcc-patches@gcc.gnu.org, binutils@sourceware.org Cc: gdb-patches@sourceware.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-System-Of-Record: true X-Gm-Message-State: ALoCoQl/B8ZZr6AwuJjunhxQEMz0ZBSbo1jSPRbMq/EAsN/oemTlvKg3+Q9iORrPjHf3DlD8GJ8ba3LlE1tWpaOMz66BRAz8JduKBnS2mcQJIhYG3Dr5YZbknq16DNeIoAl1S7+MvyJt07uoNzkkmaABRckn/MFk+w== X-IsSubscribed: yes Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-05/txt/msg00757.txt.bz2 Ping. On Thu, May 17, 2012 at 11:29 AM, Doug Evans wrote: > Hi. > > This is a slightly modified version of my previous patch. > > ref: http://gcc.gnu.org/ml/gcc-patches/2012-05/msg00962.html > > The only change is to make the result of the functions an int > instead of a const pointer. > This lets them be used in places where the code is using > non-const pointers without having to apply ugly casts. > > Ok to check in? > > 2012-05-17 =A0Doug Evans =A0 > > =A0 =A0 =A0 =A0* leb128.h: New file. > > Index: leb128.h > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > RCS file: leb128.h > diff -N leb128.h > --- /dev/null =A0 1 Jan 1970 00:00:00 -0000 > +++ leb128.h =A0 =A017 May 2012 18:23:29 -0000 > @@ -0,0 +1,130 @@ > +/* Utilities for reading leb128 values. > + =A0 Copyright (C) 2012 Free Software Foundation, Inc. > + > +This file is part of the libiberty library. > +Libiberty is free software; you can redistribute it and/or > +modify it under the terms of the GNU Library General Public > +License as published by the Free Software Foundation; either > +version 2 of the License, or (at your option) any later version. > + > +Libiberty is distributed in the hope that it will be useful, > +but WITHOUT ANY WARRANTY; without even the implied warranty of > +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =A0See the GNU > +Library General Public License for more details. > + > +You should have received a copy of the GNU Library General Public > +License along with libiberty; see the file COPYING.LIB. =A0If not, write > +to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, > +Boston, MA 02110-1301, USA. =A0*/ > + > +/* The functions defined here can be speed critical. > + =A0 Since they are all pretty small we keep things simple and just defi= ne > + =A0 them all as "static inline". =A0*/ > + > +#ifndef LEB128_H > +#define LEB128_H > + > +#include "ansidecl.h" > + > +/* Get a definition for NULL. =A0*/ > +#include > + > +#ifdef HAVE_STDINT_H > +#include > +#endif > +#ifdef HAVE_INTTYPES_H > +#include > +#endif > + > +/* Decode the unsigned LEB128 constant at BUF into the variable pointed = to > + =A0 by R, and return the number of bytes read. > + =A0 If we read off the end of the buffer, zero is returned, > + =A0 and nothing is stored in R. > + > + =A0 Note: The result is an int instead of a pointer to the next byte to= be > + =A0 read to avoid const-vs-non-const problems. =A0*/ > + > +static inline int > +read_uleb128 (const unsigned char *buf, const unsigned char *buf_end, > + =A0 =A0 =A0 =A0 =A0 =A0 uint64_t *r) > +{ > + =A0const unsigned char *p =3D buf; > + =A0unsigned int shift =3D 0; > + =A0uint64_t result =3D 0; > + =A0unsigned char byte; > + > + =A0while (1) > + =A0 =A0{ > + =A0 =A0 =A0if (p >=3D buf_end) > + =A0 =A0 =A0 return 0; > + > + =A0 =A0 =A0byte =3D *p++; > + =A0 =A0 =A0result |=3D ((uint64_t) (byte & 0x7f)) << shift; > + =A0 =A0 =A0if ((byte & 0x80) =3D=3D 0) > + =A0 =A0 =A0 break; > + =A0 =A0 =A0shift +=3D 7; > + =A0 =A0} > + > + =A0*r =3D result; > + =A0return p - buf; > +} > + > +/* Decode the signed LEB128 constant at BUF into the variable pointed to > + =A0 by R, and return the number of bytes read. > + =A0 If we read off the end of the buffer, zero is returned, > + =A0 and nothing is stored in R. > + > + =A0 Note: The result is an int instead of a pointer to the next byte to= be > + =A0 read to avoid const-vs-non-const problems. =A0*/ > + > +static inline int > +read_sleb128 (const unsigned char *buf, const unsigned char *buf_end, > + =A0 =A0 =A0 =A0 =A0 =A0 int64_t *r) > +{ > + =A0const unsigned char *p =3D buf; > + =A0unsigned int shift =3D 0; > + =A0int64_t result =3D 0; > + =A0unsigned char byte; > + > + =A0while (1) > + =A0 =A0{ > + =A0 =A0 =A0if (p >=3D buf_end) > + =A0 =A0 =A0 return 0; > + > + =A0 =A0 =A0byte =3D *p++; > + =A0 =A0 =A0result |=3D ((uint64_t) (byte & 0x7f)) << shift; > + =A0 =A0 =A0shift +=3D 7; > + =A0 =A0 =A0if ((byte & 0x80) =3D=3D 0) > + =A0 =A0 =A0 break; > + =A0 =A0} > + =A0if (shift < (sizeof (*r) * 8) && (byte & 0x40) !=3D 0) > + =A0 =A0result |=3D -(((uint64_t) 1) << shift); > + > + =A0*r =3D result; > + =A0return p - buf; > +} > + > +/* Return the number of bytes to read to skip past an LEB128 number in B= UF. > + =A0 If the end isn't found before reaching BUF_END, return zero. > + > + =A0 Note: The result is an int instead of a pointer to the next byte to= be > + =A0 read to avoid const-vs-non-const problems. =A0*/ > + > +static inline int > +skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) > +{ > + =A0const unsigned char *p =3D buf; > + =A0unsigned char byte; > + > + =A0while (1) > + =A0 =A0{ > + =A0 =A0 =A0if (p =3D=3D buf_end) > + =A0 =A0 =A0 return 0; > + > + =A0 =A0 =A0byte =3D *p++; > + =A0 =A0 =A0if ((byte & 128) =3D=3D 0) > + =A0 =A0 =A0 return p - buf; > + =A0 =A0} > +} > + > +#endif /* LEB128_H */