From patchwork Tue May 15 05:02:28 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Evans X-Patchwork-Id: 159227 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 04DD5B6FB7 for ; Tue, 15 May 2012 15:02:56 +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=1337662978; 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=9/Nhx4d KHZHCfMzkIi2FvDRe+QA=; b=pi1k3LSCXECJthSCRfEAzl+C9zxkizoZa2mIZ22 qC12dp32SdE79S6dx9qtfQ5mpCgKf8PyDJzB3Sdm8q2OQ3LwC9s4KRvpX9YtijwH ZHEDGTuD0/d1YXC2WSttq7V9XLWqer2Sj9Q8YRV64H+1p+3K4i2ZBFmaOgY/hJkQ cg+c= 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=D72jGgx09en2D857hXj7tHCASzGWMlELMxeLyd0TF2Zg8vlqdQx7HZNiY3yL2a 6IX8FUanOz+nVXWFPZ2QFvCZSSvEdq45DAn5I061Cx6zw7df604mqaFT2OFXVGmY bhUKWJ7nVHJN1EwQNERAmS/GVzqzcovjpb8ZBXP1Rd8qQ=; Received: (qmail 25406 invoked by alias); 15 May 2012 05:02:46 -0000 Received: (qmail 25385 invoked by uid 22791); 15 May 2012 05:02:44 -0000 X-SWARE-Spam-Status: No, hits=-4.1 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-gg0-f201.google.com (HELO mail-gg0-f201.google.com) (209.85.161.201) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 15 May 2012 05:02:30 +0000 Received: by ggmi2 with SMTP id i2so581452ggm.2 for ; Mon, 14 May 2012 22:02:29 -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=pUJM+4y/kZIxgrOksVhgpElsg8evEctJkAgOAXVqJ70=; b=SI2oUp8iBdZdFj9Vav+7nwqBmDuNiANZ4I3l5Mv2rqKKo4i2wIxqoRMxXHxElGeAb2 6+SY5kWYBokpQMROIgaoP634KyLIk4GyVlFd8wyqmmBxf0FQW9MnIcimr6btaI3vRKtm jLiiyoCAkJzgIO3jo8IOaXe+1YTfqtRCHuGRKH1CA3FZywEvYNGsR/5IMn5V3ETMEc8A PUtHqIrJ/X8bgmGZ/jba2WT1Tu4Ky+pFt3LGgUgHrJ8Hm3CzACpI3nCHrQehTZhNsa+j gezvHYqqIvhtpAy27wwFWL4xUCmiUWnPRS+QGlsRy/4hI5os4aH2AWJj5BHMvte7/mab N4Ww== Received: by 10.101.2.30 with SMTP id e30mr5032771ani.27.1337058149160; Mon, 14 May 2012 22:02:29 -0700 (PDT) Received: by 10.101.2.30 with SMTP id e30mr5032752ani.27.1337058148944; Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: from wpzn4.hot.corp.google.com (216-239-44-65.google.com [216.239.44.65]) by gmr-mx.google.com with ESMTPS id v25si20664455yhm.0.2012.05.14.22.02.28 (version=TLSv1/SSLv3 cipher=AES128-SHA); Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: from ruffy.mtv.corp.google.com (ruffy.mtv.corp.google.com [172.18.110.50]) by wpzn4.hot.corp.google.com (Postfix) with ESMTP id 8C3F51E004D; Mon, 14 May 2012 22:02:28 -0700 (PDT) Received: by ruffy.mtv.corp.google.com (Postfix, from userid 67641) id 3CAD62461B0; Mon, 14 May 2012 22:02:28 -0700 (PDT) To: gcc-patches@gcc.gnu.org, binutils@sourceware.org cc: gdb-patches@sourceware.org Subject: [RFA] leb128.h: New file. Message-Id: <20120515050228.3CAD62461B0@ruffy.mtv.corp.google.com> Date: Mon, 14 May 2012 22:02:28 -0700 (PDT) From: dje@google.com (Doug Evans) X-Gm-Message-State: ALoCoQl3G0PyPN0Ci/L1HRnJESzlDMb3T1zTRNDTQyhmw4NiI+3wJeTKmBml0ooKX9pGyt4NQiyodtLOyxv+sYnOlZztHSsZIQIBcBFB+9o5+j2F0P2zgcyBMgCWArINwLi5oBv+AuuUKoc2DWIX+CyJMbo9LX1oFiUeSFoScRsvnIOAWTtMBB4= 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 patch creates leb128.h which contains a few functions for reading leb128 values. They're in a header as static inlines because they can be speed critical. I've only included what I need in GDB. Ok to check in? 2012-05-14 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 15 May 2012 04:56:33 -0000 @@ -0,0 +1,116 @@ +/* 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 new value of BUF. + If we read off the end of the buffer, NULL is returned. */ + +static inline const unsigned char * +read_uleb128 (const unsigned char *buf, const unsigned char *buf_end, + uint64_t *r) +{ + unsigned int shift = 0; + uint64_t result = 0; + unsigned char byte; + + while (1) + { + if (buf >= buf_end) + return NULL; + + byte = *buf++; + result |= ((uint64_t) (byte & 0x7f)) << shift; + if ((byte & 0x80) == 0) + break; + shift += 7; + } + + *r = result; + return buf; +} + +/* Decode the signed LEB128 constant at BUF into the variable pointed to + by R, and return the new value of BUF. + If we read off the end of the buffer, NULL is returned. */ + +static inline const unsigned char * +read_sleb128 (const unsigned char *buf, const unsigned char *buf_end, + int64_t *r) +{ + unsigned int shift = 0; + int64_t result = 0; + unsigned char byte; + + while (1) + { + if (buf >= buf_end) + return NULL; + + byte = *buf++; + 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 buf; +} + +/* Return a pointer to just past the end of an LEB128 number in BUF. + If the end isn't found before reaching BUF_END, return NULL. */ + +static inline const unsigned char * +skip_leb128 (const unsigned char *buf, const unsigned char *buf_end) +{ + unsigned char byte; + + while (1) + { + if (buf == buf_end) + return NULL; + + byte = *buf++; + if ((byte & 128) == 0) + return buf; + } +} + +#endif /* LEB128_H */