summaryrefslogtreecommitdiff
path: root/parse-llvm-c-core-header.pl
blob: 1f8be9dce40f41b73b37b78e0be0ad26c3ded683 (plain)
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
#! /usr/bin/env perl

# Used to be a one-liner:
#
#  perl -0777pe ...
#

open (INPUT, "<$ARGV[0]") or die;
@input = <INPUT>;
close (INPUT);
$text = join ("", @input);

# Kill uninteresting code sections.
$text =~ s/#define.*//g;
$text =~ s/#include.*//g;
$text =~ s/#ifndef.*((.|[\r\n])*)#endif/\1/g;
$text =~ s/#if DEBUG(.|[\r\n])*?#endif//g;
$text =~ s/#ifdef __cplusplus(.|[\r\n])*?#endif//g;

# Kill comments.
$text =~ s{/\*(.|[\r\n])*?\*/}{}g;

# Convert type specifiers.
$text =~ s/(LLVM[^(\n\r]+?Ref)(?=[^(])/:pointer/g;
$text =~ s/(const )?char \*/:string /g;
$text =~ s/(\w|-|:)+ \*/:pointer /g;
$text =~ s/unsigned long long/:unsigned-long-long/g;
$text =~ s/(<!-)((void)|(unsigned)|(long)|(int)) /:\1 /g;

# Kill argument names.
#$text =~ s/([(,][^ ]+) .*?(?=[,)])/\1/g;

# Convert enums into something useful.
@statements = split /;/, $text;  #/
@enum_names = ();
@new_statements = ();

foreach $statement (@statements)
  {
    $statement =~ s/^[ \t\n\r]*//;
    $statement =~ s/[ \t\n\r]*$//;
    $statement =~ s/[ \t\n\r]+/ /g;
    if ($statement =~ /^typedef enum {(.*?)} (.*)/
        or $statement =~ /^enum {(.*?)}.*$/)
      {
        $values = $1;
        @values = split /,/, $values;  #/;

        $name = $2;
        unless ($name =~ /^$/)
          {
            push @enum_names, ($name);
          }

        $value_value = -1;
        foreach $value (@values)
          {
            $value =~ s/^ //;
            $value =~ s/ $//;
            if ($value =~ /(.*?) ?= ?(.+)/)
              {
                $value_name = $1;
                $value_value = eval $2;  # for stuff like `1 << 4'
              }
            else
              {
                $value_name = $value;
                $value_value = $value_value + 1;
              }
            push @new_statements, ("(setq +$value_name+ $value_value)");
          }
      }
    elsif ($statement =~ /^typedef/)
      {
        # Ignore.
      }
    else
      {
        unless ($statement =~ /^ ?$/)
          {
            $statement =~ /^((?:\w|:|-)+) (\w+) ?\((.*)\)$/ or die "Don't understand: $statement";
            $return_type = $1;
            $c_name = $2;
            $arguments = $3;

            @argtypes = ();
            foreach (split /,/, $arguments)   #/
              {
                unless (/^void$/)
                  {
                    /^ ?((?:\w|:|-)+)(?: (.*))?$/ or die "Don't understand: $_ (in: $statement)";
                    push @argtypes, ($1);
                  }
              }

            $argtypes = join (" ", @argtypes);

            @lisp_name_components = ();
            $_ = $c_name;
            s/^LLVM//;
            while (/^(.*?)([A-Z])(.*)$/)
              {
                $start = $1;
                $lower = lc $2;
                $rest = $3;
                unless ($start =~ /^$/)
                  {
                    $start =~ /^\w+$/ or die "Weird stuff here: $start";
                    push @lisp_name_components, ($start);
                  }
                $_ = "${lower}${rest}";
              }

            push @lisp_name_components, ($_);
            $lisp_name = join ("-", @lisp_name_components);

            push @new_statements, "(define-foreign-function $lisp_name \"$c_name\" nil $return_type $argtypes)";
          }
      }
  }

$text = join ("\n", @new_statements);

# Replace enum types with :int.
foreach $type (@enum_names)
  {
    $text =~ s/$type /:int /g;
  }

if ($ARGV[1] =~ /^$/)
  {
    print "$text\n";
  }
else
  {
    open (OUTPUT, ">$ARGV[1]") or die "Couldn't open file for writing: $ARGV[1]";
    print (OUTPUT "$text\n");
    close (OUTPUT);
  }