Leopold Toetsch <[EMAIL PROTECTED]> wrote:
> - $ perl c2str.pl src/objects.c > src/objects.str
> - enable #include "objects.str" near the top of the file
> - $ make

To make it compile cleanly with gcc 2.95.2 some of the consts need to go
- or I'm to dumb to do it right ;)

So here is another version below + 2 Makefile snippets to automate
processinng:

%.str : %.c c2str.pl
        perl c2str.pl $< > $@
...
$(SRC)/objects$(O) : $(GENERAL_H_FILES) $(SRC)/objects.str

We really need these constant strings badly (10-30% performance
increase). So please test it with your compiler.

BTW the string header needs probably live in the data segment - else we
might get troubles during e.g. setting the live flag on such strings.
Nethertheless we need something like this.

> see also "Constant strings - again" from yesterday.

> Thanks,
> leo


#! perl

use Text::Balanced qw(extract_bracketed);
use strict;

die "$0: Usage $0 FILE.c" unless $#ARGV == 0;

my $file = shift @ARGV;

$file =~ s/\.c$//;

my $infile = $file . '.c';
my $outfile = $file . '.str';

die "$0: $infile: $!" unless -e $infile;

print <<"HEADER";
/*
 * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
 *
 * This file is generated automatically from '$infile'
 * by $0.
 *
 * Any changes made here will be lost!
 *
 */

#define CONCAT(a,b) a##b
#define _S(name) (__PARROT_STATIC_STR(__LINE__))
#define __PARROT_STATIC_STR(line) CONCAT(&static_string_, line)

#if ! DISABLE_GC_DEBUG
#  define GC_DEBUG_VERSION ,0
#else
#  define GC_DEBUG_VERSION
#endif

HEADER

my %known_strings = ();
use Math::BigInt;
sub hash_val {
        my $s = substr shift, 1, -1;
        my $h = new Math::BigInt->bzero();
        for (my $i = 0; $i < length($s); ++$i) {
                $h += $h << 5;
                $h &= 0xffffffff;
                $h += ord substr($s, $i, 1);
                $h &= 0xffffffff;
        }
        return sprintf("0x%x", $h);
}

sub output_string {
  my ($text, $line) = @_;

  if (exists $known_strings{$text}) {
    <<"DATA";
#define static_string_${line} static_string_$known_strings{$text}

DATA
  }
  else {
    $known_strings{$text} = $line;
    my $h = hash_val($text);
    <<"DATA";
static /*const*/ char static_string_${line}_data\[\] = $text;
static const struct parrot_string_t static_string_${line} = {
  { /* pobj_t */
    {{
      static_string_${line}_data,
      sizeof(static_string_${line}_data)
    }},
    (PObj_constant_FLAG|PObj_external_FLAG)
    GC_DEBUG_VERSION
  },
  sizeof(static_string_${line}_data),
  static_string_${line}_data,
  sizeof(static_string_${line}_data) - 1,
  1,
  $h
};

DATA
  }
}

open IN, $infile;

my $line = 0;
while (<IN>) {
  $line++;
  next if m/^\s*#/; # ignore preprocessor
  next unless s/.*\b_S\b//;

  my $str = extract_bracketed $_, '(")';

  print output_string (substr($str,1,-1), $line);
}

Reply via email to