From patchwork Thu May 17 18:29:20 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Evans X-Patchwork-Id: 160002 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id D8559B6F6E for ; Fri, 18 May 2012 04:30:04 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1337884205; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Received:Received:To:cc:Subject:Message-Id: Date:From:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=5+4Y0hH Yatm7rbDH7HjoMcVZmzQ=; b=jmiQDRzIlgTN/ii264M0xaiQaZiILrGQyVIGo7Z c/OKDUd9bYkO4d2b9gMwxcipeP72PJxLnzFiPW28RozNM/aM9g1LvVR2Sie9s7rc KlwKzpOLwMJ8JLhScWAq15GqqWxrIvOaapCK8BcMCN0jsaa2aI3XzgKjTxAWfPI4 0y1A= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:Received:Received:Received:To:cc:Subject:Message-Id:Date:From:X-Gm-Message-State:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=alN5v/qQ0E77DO0a2sgfgpgJA3i7ZcFrZBgESYL3uT00PH/Eed/1nGTLK9u8Lq yJR58m8do7lhqR9eeDOVSnfcxPbRY5ozKFqDPyzxOzhxLtASu42YMuNixKc1TNAX GvLMe7ajVgEtySbcRRf76GNF21gGoPyQ3dSyMA3bnGF6I=; Received: (qmail 26022 invoked by alias); 17 May 2012 18:29:48 -0000 Received: (qmail 25794 invoked by uid 22791); 17 May 2012 18:29:43 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mail-we0-f201.google.com (HELO mail-we0-f201.google.com) (74.125.82.201) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 17 May 2012 18:29:23 +0000 Received: by wefh52 with SMTP id h52so111568wef.2 for ; Thu, 17 May 2012 11:29:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=to:cc:subject:message-id:date:from:x-gm-message-state; bh=3kXYOTJ0CqRbeyrG7ETcu3md//WXPkN/E4X+LyBe0PU=; b=AiFRJxPGnq09kd54Roxn4Ke8SulAi8gU4PnqxkGakg5kcv/Owoz1zTFYENmpxeUjiy 4g3RZcH0w+SzA8fyxV5lr//kUKUThP2xmiikvgbT/TJUI97+rERDWy4xib+0riIwQGG3 vDeCdDT2i0NNtqWbzj5+MFJPVhFO0BswzJ0cNS8knbq1Rhj/YoVdUgkeN1GVSadf64lS bN1iYoJqnV9mZxCrbBEW32hvz7BMMS4oGQF7aA9SYWHFflK7a/swFPNAZHU+ifBOswMZ eQYydA55FXzFoC5ymyvtfOdKCeYGpt3sqb7SfI2rM0aPRgjmS3TCT69SCtP/XdU3zu+r 3UyQ== Received: by 10.14.185.140 with SMTP id u12mr1715620eem.0.1337279361638; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: by 10.14.185.140 with SMTP id u12mr1715611eem.0.1337279361508; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id d52si6285442eei.1.2012.05.17.11.29.21 (version=TLSv1/SSLv3 cipher=AES128-SHA); Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.18.110.50]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 20F7220004E; Thu, 17 May 2012 11:29:21 -0700 (PDT) Received: by ruffy.mtv.corp.google.com (Postfix, from userid 67641) id 64FAE2461B2; Thu, 17 May 2012 11:29:20 -0700 (PDT) To: gcc-patches@gcc.gnu.org, binutils@sourceware.org cc: gdb-patches@sourceware.org Subject: Re: [RFA] leb128.h: New file. Message-Id: <20120517182920.64FAE2461B2@ruffy.mtv.corp.google.com> Date: Thu, 17 May 2012 11:29:20 -0700 (PDT) From: dje@google.com (Doug Evans) X-Gm-Message-State: ALoCoQnNmU+gnCDgKT2RAPXrbDHcCae8a/5zE9/Vetl4wZ9jby5N6IhXimEnL9YQqtzYCZBuCO46IGm5iol+6O8lf4dBAm/i0EQqf9XOYHnX9fWeGsIb3fwRtuvvkx6A8rlAQe556HM3nVTdqBv4cFsXzrbvhiGNjFReyqCL+nfSWvxI4vU7iwQ= Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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 Doug Evans * leb128.h: New file. Index: leb128.h =================================================================== RCS file: leb128.h diff -N leb128.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ leb128.h 17 May 2012 18:23:29 -0000 @@ -0,0 +1,130 @@ +/* Utilities for reading leb128 values. + 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. See 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. If not, write +to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ + +/* The functions defined here can be speed critical. + Since they are all pretty small we keep things simple and just define + them all as "static inline". */ + +#ifndef LEB128_H +#define LEB128_H + +#include "ansidecl.h" + +/* Get a definition for NULL. */ +#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 + by R, and return the number of bytes read. + If we read off the end of the buffer, zero is returned, + and nothing is stored in R. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +read_uleb128 (const unsigned char *buf, const unsigned char *buf_end, + uint64_t *r) +{ + const unsigned char *p = buf; + unsigned int shift = 0; + uint64_t result = 0; + unsigned char byte; + + while (1) + { + if (p >= buf_end) + return 0; + + byte = *p++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + if ((byte & 0x80) == 0) + break; + shift += 7; + } + + *r = result; + return p - buf; +} + +/* Decode the signed LEB128 constant at BUF into the variable pointed to + by R, and return the number of bytes read. + If we read off the end of the buffer, zero is returned, + and nothing is stored in R. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +read_sleb128 (const unsigned char *buf, const unsigned char *buf_end, + int64_t *r) +{ + const unsigned char *p = buf; + unsigned int shift = 0; + int64_t result = 0; + unsigned char byte; + + while (1) + { + if (p >= buf_end) + return 0; + + byte = *p++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + shift += 7; + if ((byte & 0x80) == 0) + break; + } + if (shift < (sizeof (*r) * 8) && (byte & 0x40) != 0) + result |= -(((uint64_t) 1) << shift); + + *r = result; + return p - buf; +} + +/* Return the number of bytes to read to skip past an LEB128 number in BUF. + If the end isn't found before reaching BUF_END, return zero. + + Note: The result is an int instead of a pointer to the next byte to be + read to avoid const-vs-non-const problems. */ + +static inline int +skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) +{ + const unsigned char *p = buf; + unsigned char byte; + + while (1) + { + if (p == buf_end) + return 0; + + byte = *p++; + if ((byte & 128) == 0) + return p - buf; + } +} + +#endif /* LEB128_H */