File: typegen.pl

package info (click to toggle)
icheck 0.9.7-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,352 kB
  • sloc: perl: 12,152; makefile: 202; ansic: 100
file content (153 lines) | stat: -rwxr-xr-x 3,536 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl

use strict;
use 5.6.0;
use warnings;

# This perl script generates a C program which generates a perl
# script. It's gonna get ugly.

my @types = ('char', 'signed char', 'unsigned char', 'short',
             'unsigned short', 'int', 'unsigned', 'long', 'unsigned long',
             'long long', 'unsigned long long', 'float', 'double', 'long double',
             '_Bool', 'void *', 'enum null_enum');

sub mangle_typename
  {
    my $name = shift;
    $name =~ s/\*/ptr/g;
    $name =~ s/[^\w]/_/g;
    return $name;
  }

sub print_align_struct
  {
    my $type = shift;
    my $name = mangle_typename($type);
    print <<"END";
struct align_$name
{
  char pad;
  $type object;
};

END
  }

print <<END;
enum null_enum {enumerator};

END

print_align_struct($_) foreach @types;

print <<END;
#include <stddef.h>
#include <limits.h>
#include <stdio.h>

int
main(void)
{
  printf("package CType::Native;\\n");
  printf("\\n");
  printf("use 5.6.0;\\n");
  printf("use strict;\\n");
  printf("use warnings;\\n");
  printf("\\n");
  printf("use bigint;\\n");
  printf("use CType::Fundamental qw/register_type_attrs/;\\n");
  printf("\\n");
END

# We can't use gcc's __alignof__ function because it's
# broken. __alignof__(double) returns 8 on i686-linux-gnu, but the
# correct value is 4. Instead we'll examine how a struct is laid out,
# to deduce the alignment actually used.

sub alignof
  {
    my $type = shift;
    my $name = mangle_typename($type);
    return "offsetof(struct align_$name, object) * 8";
  }

sub signof
  {
    my $type = shift;
    return "((($type) -1) < 0)";
  }

sub sizeof
  {
    my $type = shift;
    return "sizeof($type) * 8";
  }

my %printf_limit_values = ('long' => '%ld',
                           'unsigned long' => '%lu',
                           'long long' => '%lld',
                           'unsigned long long' => '%llu',
                          );

my %limit_values = ('char' => 'CHAR',
                    'signed char' => 'SCHAR',
                    'unsigned char' => 'UCHAR',
                    'short' => 'SHRT',
                    'unsigned short' => 'USHRT',
                    'int' => 'INT',
                    'unsigned' => 'UINT',
                    'long' => 'LONG',
                    'unsigned long' => 'ULONG',
                    'long long' => 'LLONG',
                    'unsigned long long' => 'ULLONG'
                   );

sub printf_limit_min
  {
    my $type = shift;
    return '\%d' unless $printf_limit_values{$type};
    return '\%d' if $type =~ /unsigned/;
    return $printf_limit_values{$type};
  }

sub printf_limit_max
  {
    my $type = shift;
    return '\%d' unless $printf_limit_values{$type};
    return $printf_limit_values{$type};
  }

sub minof
  {
    my $type = shift;
    return '0' unless $limit_values{$type};
    return '0' if $type =~ /unsigned/;
    return $limit_values{$type} . '_MIN';
  }

sub maxof
  {
    my $type = shift;
    return '0' unless $limit_values{$type};
    return $limit_values{$type} . '_MAX';
  }

sub print_register_attrs
  {
    my $type = shift;
    my $ln = printf_limit_min($type);
    my $lx = printf_limit_max($type);
    print "  printf(\"register_type_attrs('$type', \%d, \%d, \%d, $ln, $lx);\\n\",\n";
    print "         " . sizeof($type) . ", " . alignof($type) . ", " . signof($type) . ",\n";
    print "         " . minof($type) . ", " . maxof($type) . ");\n";
  }

print_register_attrs($_) foreach @types;

print <<END;
  printf("\\n");
  printf("1;\\n");
  return 0;
}
END