From: dje@google.com (Doug Evans)
To: gcc-patches@gcc.gnu.org, binutils@sourceware.org
Cc: gdb-patches@sourceware.org
Subject: [RFA] leb128.h: New file.
Date: Tue, 15 May 2012 05:02:00 -0000 [thread overview]
Message-ID: <20120515050228.3CAD62461B0@ruffy.mtv.corp.google.com> (raw)
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 <dje@google.com>
* 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 <stdio.h>
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h>
+#endif
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#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 */
next reply other threads:[~2012-05-15 5:02 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-15 5:02 Doug Evans [this message]
2012-05-17 18:29 Doug Evans
2012-05-21 0:31 ` Doug Evans
2012-05-21 14:21 ` Jan Kratochvil
2012-05-21 17:09 ` Ian Lance Taylor
2012-05-21 17:36 ` Doug Evans
2012-05-21 18:22 ` Ian Lance Taylor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120515050228.3CAD62461B0@ruffy.mtv.corp.google.com \
--to=dje@google.com \
--cc=binutils@sourceware.org \
--cc=gcc-patches@gcc.gnu.org \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox