OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [lto-compress.c] - Diff between revs 816 and 826

Only display areas with differences | Details | Blame | View Log

Rev 816 Rev 826
/* LTO IL compression streams.
/* LTO IL compression streams.
 
 
   Copyright 2009 Free Software Foundation, Inc.
   Copyright 2009 Free Software Foundation, Inc.
   Contributed by Simon Baldwin <simonb@google.com>
   Contributed by Simon Baldwin <simonb@google.com>
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify it
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful, but WITHOUT
GCC is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
License for more details.
License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
/* zlib.h includes other system headers.  Those headers may test feature
/* zlib.h includes other system headers.  Those headers may test feature
   test macros.  config.h may define feature test macros.  For this reason,
   test macros.  config.h may define feature test macros.  For this reason,
   zlib.h needs to be included after, rather than before, config.h and
   zlib.h needs to be included after, rather than before, config.h and
   system.h.  */
   system.h.  */
#include <zlib.h>
#include <zlib.h>
#include "coretypes.h"
#include "coretypes.h"
#include "tree.h"
#include "tree.h"
#include "diagnostic.h"
#include "diagnostic.h"
#include "errors.h"
#include "errors.h"
#include "langhooks.h"
#include "langhooks.h"
#include "lto-streamer.h"
#include "lto-streamer.h"
#include "lto-compress.h"
#include "lto-compress.h"
 
 
/* Compression stream structure, holds the flush callback and opaque token,
/* Compression stream structure, holds the flush callback and opaque token,
   the buffered data, and a note of whether compressing or uncompressing.  */
   the buffered data, and a note of whether compressing or uncompressing.  */
 
 
struct lto_compression_stream
struct lto_compression_stream
{
{
  void (*callback) (const char *, unsigned, void *);
  void (*callback) (const char *, unsigned, void *);
  void *opaque;
  void *opaque;
  char *buffer;
  char *buffer;
  size_t bytes;
  size_t bytes;
  size_t allocation;
  size_t allocation;
  bool is_compression;
  bool is_compression;
};
};
 
 
/* Overall compression constants for zlib.  */
/* Overall compression constants for zlib.  */
 
 
static const size_t Z_BUFFER_LENGTH = 4096;
static const size_t Z_BUFFER_LENGTH = 4096;
static const size_t MIN_STREAM_ALLOCATION = 1024;
static const size_t MIN_STREAM_ALLOCATION = 1024;
 
 
/* For zlib, allocate SIZE count of ITEMS and return the address, OPAQUE
/* For zlib, allocate SIZE count of ITEMS and return the address, OPAQUE
   is unused.  */
   is unused.  */
 
 
static void *
static void *
lto_zalloc (void *opaque, unsigned items, unsigned size)
lto_zalloc (void *opaque, unsigned items, unsigned size)
{
{
  gcc_assert (opaque == Z_NULL);
  gcc_assert (opaque == Z_NULL);
  return xmalloc (items * size);
  return xmalloc (items * size);
}
}
 
 
/* For zlib, free memory at ADDRESS, OPAQUE is unused.  */
/* For zlib, free memory at ADDRESS, OPAQUE is unused.  */
 
 
static void
static void
lto_zfree (void *opaque, void *address)
lto_zfree (void *opaque, void *address)
{
{
  gcc_assert (opaque == Z_NULL);
  gcc_assert (opaque == Z_NULL);
  free (address);
  free (address);
}
}
 
 
/* Return a zlib compression level that zlib will not reject.  Normalizes
/* Return a zlib compression level that zlib will not reject.  Normalizes
   the compression level from the command line flag, clamping non-default
   the compression level from the command line flag, clamping non-default
   values to the appropriate end of their valid range.  */
   values to the appropriate end of their valid range.  */
 
 
static int
static int
lto_normalized_zlib_level (void)
lto_normalized_zlib_level (void)
{
{
  int level = flag_lto_compression_level;
  int level = flag_lto_compression_level;
 
 
  if (level != Z_DEFAULT_COMPRESSION)
  if (level != Z_DEFAULT_COMPRESSION)
    {
    {
      if (level < Z_NO_COMPRESSION)
      if (level < Z_NO_COMPRESSION)
        level = Z_NO_COMPRESSION;
        level = Z_NO_COMPRESSION;
      else if (level > Z_BEST_COMPRESSION)
      else if (level > Z_BEST_COMPRESSION)
        level = Z_BEST_COMPRESSION;
        level = Z_BEST_COMPRESSION;
    }
    }
 
 
  return level;
  return level;
}
}
 
 
/* Create a new compression stream, with CALLBACK flush function passed
/* Create a new compression stream, with CALLBACK flush function passed
   OPAQUE token, IS_COMPRESSION indicates if compressing or uncompressing.  */
   OPAQUE token, IS_COMPRESSION indicates if compressing or uncompressing.  */
 
 
static struct lto_compression_stream *
static struct lto_compression_stream *
lto_new_compression_stream (void (*callback) (const char *, unsigned, void *),
lto_new_compression_stream (void (*callback) (const char *, unsigned, void *),
                            void *opaque, bool is_compression)
                            void *opaque, bool is_compression)
{
{
  struct lto_compression_stream *stream
  struct lto_compression_stream *stream
    = (struct lto_compression_stream *) xmalloc (sizeof (*stream));
    = (struct lto_compression_stream *) xmalloc (sizeof (*stream));
 
 
  memset (stream, 0, sizeof (*stream));
  memset (stream, 0, sizeof (*stream));
  stream->callback = callback;
  stream->callback = callback;
  stream->opaque = opaque;
  stream->opaque = opaque;
  stream->is_compression = is_compression;
  stream->is_compression = is_compression;
 
 
  return stream;
  return stream;
}
}
 
 
/* Append NUM_CHARS from address BASE to STREAM.  */
/* Append NUM_CHARS from address BASE to STREAM.  */
 
 
static void
static void
lto_append_to_compression_stream (struct lto_compression_stream *stream,
lto_append_to_compression_stream (struct lto_compression_stream *stream,
                                  const char *base, size_t num_chars)
                                  const char *base, size_t num_chars)
{
{
  size_t required = stream->bytes + num_chars;
  size_t required = stream->bytes + num_chars;
 
 
  if (stream->allocation < required)
  if (stream->allocation < required)
    {
    {
      if (stream->allocation == 0)
      if (stream->allocation == 0)
        stream->allocation = MIN_STREAM_ALLOCATION;
        stream->allocation = MIN_STREAM_ALLOCATION;
      while (stream->allocation < required)
      while (stream->allocation < required)
        stream->allocation *= 2;
        stream->allocation *= 2;
 
 
      stream->buffer = (char *) xrealloc (stream->buffer, stream->allocation);
      stream->buffer = (char *) xrealloc (stream->buffer, stream->allocation);
    }
    }
 
 
  memcpy (stream->buffer + stream->bytes, base, num_chars);
  memcpy (stream->buffer + stream->bytes, base, num_chars);
  stream->bytes += num_chars;
  stream->bytes += num_chars;
}
}
 
 
/* Free the buffer and memory associated with STREAM.  */
/* Free the buffer and memory associated with STREAM.  */
 
 
static void
static void
lto_destroy_compression_stream (struct lto_compression_stream *stream)
lto_destroy_compression_stream (struct lto_compression_stream *stream)
{
{
  free (stream->buffer);
  free (stream->buffer);
  free (stream);
  free (stream);
}
}
 
 
/* Return a new compression stream, with CALLBACK flush function passed
/* Return a new compression stream, with CALLBACK flush function passed
   OPAQUE token.  */
   OPAQUE token.  */
 
 
struct lto_compression_stream *
struct lto_compression_stream *
lto_start_compression (void (*callback) (const char *, unsigned, void *),
lto_start_compression (void (*callback) (const char *, unsigned, void *),
                       void *opaque)
                       void *opaque)
{
{
  return lto_new_compression_stream (callback, opaque, true);
  return lto_new_compression_stream (callback, opaque, true);
}
}
 
 
/* Append NUM_CHARS from address BASE to STREAM.  */
/* Append NUM_CHARS from address BASE to STREAM.  */
 
 
void
void
lto_compress_block (struct lto_compression_stream *stream,
lto_compress_block (struct lto_compression_stream *stream,
                    const char *base, size_t num_chars)
                    const char *base, size_t num_chars)
{
{
  gcc_assert (stream->is_compression);
  gcc_assert (stream->is_compression);
 
 
  lto_append_to_compression_stream (stream, base, num_chars);
  lto_append_to_compression_stream (stream, base, num_chars);
  lto_stats.num_output_il_bytes += num_chars;
  lto_stats.num_output_il_bytes += num_chars;
}
}
 
 
/* Finalize STREAM compression, and free stream allocations.  */
/* Finalize STREAM compression, and free stream allocations.  */
 
 
void
void
lto_end_compression (struct lto_compression_stream *stream)
lto_end_compression (struct lto_compression_stream *stream)
{
{
  unsigned char *cursor = (unsigned char *) stream->buffer;
  unsigned char *cursor = (unsigned char *) stream->buffer;
  size_t remaining = stream->bytes;
  size_t remaining = stream->bytes;
  const size_t outbuf_length = Z_BUFFER_LENGTH;
  const size_t outbuf_length = Z_BUFFER_LENGTH;
  unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  z_stream out_stream;
  z_stream out_stream;
  size_t compressed_bytes = 0;
  size_t compressed_bytes = 0;
  int status;
  int status;
 
 
  gcc_assert (stream->is_compression);
  gcc_assert (stream->is_compression);
 
 
  out_stream.next_out = outbuf;
  out_stream.next_out = outbuf;
  out_stream.avail_out = outbuf_length;
  out_stream.avail_out = outbuf_length;
  out_stream.next_in = cursor;
  out_stream.next_in = cursor;
  out_stream.avail_in = remaining;
  out_stream.avail_in = remaining;
  out_stream.zalloc = lto_zalloc;
  out_stream.zalloc = lto_zalloc;
  out_stream.zfree = lto_zfree;
  out_stream.zfree = lto_zfree;
  out_stream.opaque = Z_NULL;
  out_stream.opaque = Z_NULL;
 
 
  status = deflateInit (&out_stream, lto_normalized_zlib_level ());
  status = deflateInit (&out_stream, lto_normalized_zlib_level ());
  if (status != Z_OK)
  if (status != Z_OK)
    internal_error ("compressed stream: %s", zError (status));
    internal_error ("compressed stream: %s", zError (status));
 
 
  do
  do
    {
    {
      size_t in_bytes, out_bytes;
      size_t in_bytes, out_bytes;
 
 
      status = deflate (&out_stream, Z_FINISH);
      status = deflate (&out_stream, Z_FINISH);
      if (status != Z_OK && status != Z_STREAM_END)
      if (status != Z_OK && status != Z_STREAM_END)
        internal_error ("compressed stream: %s", zError (status));
        internal_error ("compressed stream: %s", zError (status));
 
 
      in_bytes = remaining - out_stream.avail_in;
      in_bytes = remaining - out_stream.avail_in;
      out_bytes = outbuf_length - out_stream.avail_out;
      out_bytes = outbuf_length - out_stream.avail_out;
 
 
      stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
      stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
      lto_stats.num_compressed_il_bytes += out_bytes;
      lto_stats.num_compressed_il_bytes += out_bytes;
      compressed_bytes += out_bytes;
      compressed_bytes += out_bytes;
 
 
      cursor += in_bytes;
      cursor += in_bytes;
      remaining -= in_bytes;
      remaining -= in_bytes;
 
 
      out_stream.next_out = outbuf;
      out_stream.next_out = outbuf;
      out_stream.avail_out = outbuf_length;
      out_stream.avail_out = outbuf_length;
      out_stream.next_in = cursor;
      out_stream.next_in = cursor;
      out_stream.avail_in = remaining;
      out_stream.avail_in = remaining;
    }
    }
  while (status != Z_STREAM_END);
  while (status != Z_STREAM_END);
 
 
  status = deflateEnd (&out_stream);
  status = deflateEnd (&out_stream);
  if (status != Z_OK)
  if (status != Z_OK)
    internal_error ("compressed stream: %s", zError (status));
    internal_error ("compressed stream: %s", zError (status));
 
 
  lto_destroy_compression_stream (stream);
  lto_destroy_compression_stream (stream);
  free (outbuf);
  free (outbuf);
}
}
 
 
/* Return a new uncompression stream, with CALLBACK flush function passed
/* Return a new uncompression stream, with CALLBACK flush function passed
   OPAQUE token.  */
   OPAQUE token.  */
 
 
struct lto_compression_stream *
struct lto_compression_stream *
lto_start_uncompression (void (*callback) (const char *, unsigned, void *),
lto_start_uncompression (void (*callback) (const char *, unsigned, void *),
                         void *opaque)
                         void *opaque)
{
{
  return lto_new_compression_stream (callback, opaque, false);
  return lto_new_compression_stream (callback, opaque, false);
}
}
 
 
/* Append NUM_CHARS from address BASE to STREAM.  */
/* Append NUM_CHARS from address BASE to STREAM.  */
 
 
void
void
lto_uncompress_block (struct lto_compression_stream *stream,
lto_uncompress_block (struct lto_compression_stream *stream,
                      const char *base, size_t num_chars)
                      const char *base, size_t num_chars)
{
{
  gcc_assert (!stream->is_compression);
  gcc_assert (!stream->is_compression);
 
 
  lto_append_to_compression_stream (stream, base, num_chars);
  lto_append_to_compression_stream (stream, base, num_chars);
  lto_stats.num_input_il_bytes += num_chars;
  lto_stats.num_input_il_bytes += num_chars;
}
}
 
 
/* Finalize STREAM uncompression, and free stream allocations.
/* Finalize STREAM uncompression, and free stream allocations.
 
 
   Because of the way LTO IL streams are compressed, there may be several
   Because of the way LTO IL streams are compressed, there may be several
   concatenated compressed segments in the accumulated data, so for this
   concatenated compressed segments in the accumulated data, so for this
   function we iterate decompressions until no data remains.  */
   function we iterate decompressions until no data remains.  */
 
 
void
void
lto_end_uncompression (struct lto_compression_stream *stream)
lto_end_uncompression (struct lto_compression_stream *stream)
{
{
  unsigned char *cursor = (unsigned char *) stream->buffer;
  unsigned char *cursor = (unsigned char *) stream->buffer;
  size_t remaining = stream->bytes;
  size_t remaining = stream->bytes;
  const size_t outbuf_length = Z_BUFFER_LENGTH;
  const size_t outbuf_length = Z_BUFFER_LENGTH;
  unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  unsigned char *outbuf = (unsigned char *) xmalloc (outbuf_length);
  size_t uncompressed_bytes = 0;
  size_t uncompressed_bytes = 0;
 
 
  gcc_assert (!stream->is_compression);
  gcc_assert (!stream->is_compression);
 
 
  while (remaining > 0)
  while (remaining > 0)
    {
    {
      z_stream in_stream;
      z_stream in_stream;
      size_t out_bytes;
      size_t out_bytes;
      int status;
      int status;
 
 
      in_stream.next_out = outbuf;
      in_stream.next_out = outbuf;
      in_stream.avail_out = outbuf_length;
      in_stream.avail_out = outbuf_length;
      in_stream.next_in = cursor;
      in_stream.next_in = cursor;
      in_stream.avail_in = remaining;
      in_stream.avail_in = remaining;
      in_stream.zalloc = lto_zalloc;
      in_stream.zalloc = lto_zalloc;
      in_stream.zfree = lto_zfree;
      in_stream.zfree = lto_zfree;
      in_stream.opaque = Z_NULL;
      in_stream.opaque = Z_NULL;
 
 
      status = inflateInit (&in_stream);
      status = inflateInit (&in_stream);
      if (status != Z_OK)
      if (status != Z_OK)
        internal_error ("compressed stream: %s", zError (status));
        internal_error ("compressed stream: %s", zError (status));
 
 
      do
      do
        {
        {
          size_t in_bytes;
          size_t in_bytes;
 
 
          status = inflate (&in_stream, Z_SYNC_FLUSH);
          status = inflate (&in_stream, Z_SYNC_FLUSH);
          if (status != Z_OK && status != Z_STREAM_END)
          if (status != Z_OK && status != Z_STREAM_END)
            internal_error ("compressed stream: %s", zError (status));
            internal_error ("compressed stream: %s", zError (status));
 
 
          in_bytes = remaining - in_stream.avail_in;
          in_bytes = remaining - in_stream.avail_in;
          out_bytes = outbuf_length - in_stream.avail_out;
          out_bytes = outbuf_length - in_stream.avail_out;
 
 
          stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
          stream->callback ((const char *) outbuf, out_bytes, stream->opaque);
          lto_stats.num_uncompressed_il_bytes += out_bytes;
          lto_stats.num_uncompressed_il_bytes += out_bytes;
          uncompressed_bytes += out_bytes;
          uncompressed_bytes += out_bytes;
 
 
          cursor += in_bytes;
          cursor += in_bytes;
          remaining -= in_bytes;
          remaining -= in_bytes;
 
 
          in_stream.next_out = outbuf;
          in_stream.next_out = outbuf;
          in_stream.avail_out = outbuf_length;
          in_stream.avail_out = outbuf_length;
          in_stream.next_in = cursor;
          in_stream.next_in = cursor;
          in_stream.avail_in = remaining;
          in_stream.avail_in = remaining;
        }
        }
      while (!(status == Z_STREAM_END && out_bytes == 0));
      while (!(status == Z_STREAM_END && out_bytes == 0));
 
 
      status = inflateEnd (&in_stream);
      status = inflateEnd (&in_stream);
      if (status != Z_OK)
      if (status != Z_OK)
        internal_error ("compressed stream: %s", zError (status));
        internal_error ("compressed stream: %s", zError (status));
    }
    }
 
 
  lto_destroy_compression_stream (stream);
  lto_destroy_compression_stream (stream);
  free (outbuf);
  free (outbuf);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.