summaryrefslogtreecommitdiff
path: root/auto/bin/parse_spec.pl
blob: 00fdde9cc310d3d4149fb09a03169ebe70905e11 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/perl
##
## Copyright (C) 2002-2008, Marcelo E. Magallon <mmagallo[]debian org>
## Copyright (C) 2002-2008, Milan Ikits <milan ikits[]ieee org>
##
## This program is distributed under the terms and conditions of the GNU
## General Public License Version 2 as published by the Free Software
## Foundation or, at your option, any later version.

use strict;
use warnings;

sub compile_regex
{
    my $regex = join('', @_);
    return qr/$regex/
}

my @sections = (
    "Name",
    "Name Strings?",
    "New Procedures and Functions",
    "New Tokens.*",                    # Optional (GL/WGL/GLX/...) suffix
    "Additions to Chapter.*",
);

my %typemap = (
    bitfield => "GLbitfield",
    boolean  => "GLboolean",
    # fsck up in EXT_vertex_array
    Boolean  => "GLboolean",
    byte     => "GLbyte",
    clampd   => "GLclampd",
    clampf   => "GLclampf",
    double   => "GLdouble",
    enum     => "GLenum",
    # Intel fsck up
    Glenum   => "GLenum",
    float    => "GLfloat",
    half     => "GLhalf",
    int      => "GLint",
    short    => "GLshort",
    sizei    => "GLsizei",
    ubyte    => "GLubyte",
    uint     => "GLuint",
    ushort   => "GLushort",
    DMbuffer => "void *",
    # Nvidia video output fsck up
    int64EXT => "GLint64EXT",
    uint64EXT=> "GLuint64EXT",

    # ARB VBO introduces these.

    sizeiptr => "GLsizeiptr",
    intptr   => "GLintptr",
    sizeiptrARB => "GLsizeiptrARB",
    intptrARB   => "GLintptrARB",

    # ARB shader objects introduces these, charARB is at least 8 bits,
    # handleARB is at least 32 bits
    charARB => "GLcharARB",
    handleARB => "GLhandleARB",

    char => "GLchar",

    # OpenGL 3.2 and GL_ARB_sync

    int64  => "GLint64",
    uint64 => "GLuint64",
    sync   => "GLsync",

    # AMD_debug_output

    DEBUGPROCAMD => "GLDEBUGPROCAMD",

    # ARB_debug_output

    DEBUGPROCARB => "GLDEBUGPROCARB",

    # KHR_debug

    DEBUGPROC => "GLDEBUGPROC",

    vdpauSurfaceNV => "GLvdpauSurfaceNV",
    
    # GLX 1.3 defines new types which might not be available at compile time

    #GLXFBConfig   => "void*",
    #GLXFBConfigID => "XID",
    #GLXContextID  => "XID",
    #GLXWindow     => "XID",
    #GLXPbuffer    => "XID",

    # Weird stuff to some SGIX extension

    #GLXFBConfigSGIX   => "void*",
    #GLXFBConfigIDSGIX => "XID",

);

my %voidtypemap = (
    void    => "GLvoid",
);

my %taboo_tokens = (
    GL_ZERO => 1,
);

# list of function definitions to be ignored, unless they are being defined in
# the given spec.  This is an ugly hack arround the fact that people writing
# spec files seem to shut down all brain activity while they are at this task.
#
# This will be moved to its own file eventually.
#
# (mem, 2003-03-19)

my %fnc_ignore_list = (
    "BindProgramARB"                => "ARB_vertex_program",
    "ColorSubTableEXT"              => "EXT_color_subtable",
    "DeleteProgramsARB"             => "ARB_vertex_program",
    "GenProgramsARB"                => "ARB_vertex_program",
    "GetProgramEnvParameterdvARB"   => "ARB_vertex_program",
    "GetProgramEnvParameterfvARB"   => "ARB_vertex_program",
    "GetProgramLocalParameterdvARB" => "ARB_vertex_program",
    "GetProgramLocalParameterfvARB" => "ARB_vertex_program",
    "GetProgramStringARB"           => "ARB_vertex_program",
    "GetProgramivARB"               => "ARB_vertex_program",
    "IsProgramARB"                  => "ARB_vertex_program",
    "ProgramEnvParameter4dARB"      => "ARB_vertex_program",
    "ProgramEnvParameter4dvARB"     => "ARB_vertex_program",
    "ProgramEnvParameter4fARB"      => "ARB_vertex_program",
    "ProgramEnvParameter4fvARB"     => "ARB_vertex_program",
    "ProgramLocalParameter4dARB"    => "ARB_vertex_program",
    "ProgramLocalParameter4dvARB"   => "ARB_vertex_program",
    "ProgramLocalParameter4fARB"    => "ARB_vertex_program",
    "ProgramLocalParameter4fvARB"   => "ARB_vertex_program",
    "ProgramStringARB"              => "ARB_vertex_program",
    "glXCreateContextAttribsARB"    => "ARB_create_context_profile",
    "wglCreateContextAttribsARB"    => "WGL_ARB_create_context_profile",
);

my %regex = (
    eofnc    => qr/(?:\);?$|^$)/, # )$ | );$ | ^$
    extname  => qr/^[A-Z][A-Za-z0-9_]+$/,
    none     => qr/^\(none\)$/,
    function => qr/^(.+) ([a-z][a-z0-9_]*) \((.+)\)$/i,
    prefix   => qr/^(?:[aw]?gl|glX)/, # gl | agl | wgl | glX
    tprefix  => qr/^(?:[AW]?GL|GLX)_/, # GL_ | AGL_ | WGL_ | GLX_
    section  => compile_regex('^(', join('|', @sections), ')$'), # sections in spec
    token    => qr/^([A-Z0-9][A-Z0-9_x]*):?\s+((?:0x)?[0-9A-F]+)([^\?]*)$/, # define tokens
    types    => compile_regex('\b(', join('|', keys %typemap), ')\b'), # var types
    voidtype => compile_regex('\b(', keys %voidtypemap, ')\b '), # void type
);

# reshapes the the function declaration from multiline to single line form
sub normalize_prototype
{
    local $_ = join(" ", @_);
    s/\s+/ /g;                # multiple whitespace -> single space
    s/\<.*\>//g;              # remove <comments> from direct state access extension
    s/\<.*$//g;               # remove incomplete <comments> from direct state access extension
    s#/\*.*\*/##g;            # remove /* ... */ comments
    s/\s*\(\s*/ \(/;          # exactly one space before ( and none after
    s/\s*\)\s*/\)/;           # no space before or after )
    s/\s*\*([a-zA-Z])/\* $1/; # "* identifier"
    s/\*wgl/\* wgl/;          # "* wgl"
    s/\*glX/\* glX/;          # "* glX"
    s/\.\.\./void/;           # ... -> void
    s/;$//;                   # remove ; at the end of the line
    return $_;
}

# Ugly hack to work arround the fact that functions are declared in more
# than one spec file.
sub ignore_function($$)
{
    return exists($fnc_ignore_list{$_[0]}) && $fnc_ignore_list{$_[0]} ne $_[1]
}

sub parse_spec($)
{
    my $filename = shift;
    my $extname = "";
    my $vendortag = "";
    my @extnames = ();
    my %functions = ();
    my %tokens = ();

    my $section = "";
    my @fnc = ();

    my %proc = (
        "Name" => sub {
            if (/^([a-z0-9]+)_([a-z0-9_]+)/i)
            {
                $extname = "$1_$2";
                $vendortag = $1;
            }
        },

        "Name Strings" => sub {
            # Add extension name to extension list
        
           # Initially use $extname if (none) specified
            if (/$regex{none}/)
            {
                $_ = $extname;
            }

            if (/$regex{extname}/)
            {
                # prefix with "GL_" if prefix not present
                s/^/GL_/ unless /$regex{tprefix}/o;
                # Add extension name to extension list
                push @extnames, $_;
            }
        },

        "New Procedures and Functions" => sub {
            # if line matches end of function
            if (/$regex{eofnc}/)
            {
                # add line to function declaration
                push @fnc, $_;

                # if normalized version of function looks like a function
                if (normalize_prototype(@fnc) =~ /$regex{function}/)
                {
                    # get return type, name, and arguments from regex
                    my ($return, $name, $parms) = ($1, $2, $3);
                    if (!ignore_function($name, $extname))
                    {
                        # prefix with "gl" if prefix not present
                        $name =~ s/^/gl/ unless $name =~ /$regex{prefix}/;
                        # is this a pure GL function?
                        if ($name =~ /^gl/ && $name !~ /^glX/)
                        {
                            # apply typemaps
                            $return =~ s/$regex{types}/$typemap{$1}/og;
                            $return =~ s/GLvoid/void/og;
                            $return =~ s/void\*/void */og;
                            $parms =~ s/$regex{types}/$typemap{$1}/og;
                            $parms =~ s/$regex{voidtype}/$voidtypemap{$1}/og;
                            $parms =~ s/GLvoid/void/og;
                            $parms =~ s/ void\* / void */og;
                        }
                        # add to functions hash
                        $functions{$name} = {
                            rtype => $return,
                            parms => $parms,
                        };
                    }
                }
                # reset function declaration
                @fnc = ();
            } elsif ($_ ne "" and $_ ne "None") {
                # if not eof, add line to function declaration
                push @fnc, $_
            }
        },

        "New Tokens" => sub {
            if (/$regex{token}/)
            {
                my ($name, $value) = ($1, $2);
                # prefix with "GL_" if prefix not present
                $name =~ s/^/GL_/ unless $name =~ /$regex{tprefix}/;
                # Add (name, value) pair to tokens hash, unless it's taboo
                $tokens{$name} = $value unless exists $taboo_tokens{$name};
            }
        },
    );

    # Some people can't read, the template clearly says "Name String_s_"
    $proc{"Name String"} = $proc{"Name Strings"};

    # Open spec file
    open SPEC, "<$filename" or return;

    # For each line of SPEC
    while(<SPEC>)
    {
        # Delete trailing newline character
        chomp;
        # Remove trailing white spaces
        s/\s+$//;
        # If starts with a capital letter, it must be a new section
        if (/^[A-Z]/)
        {
            # Match section name with one of the predefined names 
            $section = /$regex{section}/o ? $1 : "default";
        } else {
            # Line is internal to a section
            # Remove leading whitespace
            s/^\s+//;
            # Call appropriate section processing function if it exists
            &{$proc{$section}} if exists $proc{$section};
        }
    }

    close SPEC;

    return ($extname, \@extnames, \%tokens, \%functions);
}

#----------------------------------------------------------------------------------------

my @speclist = ();
my %extensions = ();

my $ext_dir = shift;
my $reg_http = "http://www.opengl.org/registry/specs/";

# Take command line arguments or read list from file
if (@ARGV)
{
    @speclist = @ARGV;
} else {
    local $/; #???
    @speclist = split "\n", (<>);
}

foreach my $spec (sort @speclist)
{
    my ($extname, $extnames, $tokens, $functions) = parse_spec($spec);

    foreach my $ext (@{$extnames})
    {
        my $info = "$ext_dir/" . $ext;
        open EXT, ">$info";
        print EXT $ext . "\n";                       # Extension name
        my $specname = $spec;
        $specname =~ s/registry\/gl\/specs\///;
        print EXT $reg_http . $specname . "\n";      # Extension info URL
        print EXT $ext . "\n";                       # Extension string

        my $prefix = $ext;
        $prefix =~ s/^(.+?)(_.+)$/$1/;
        foreach my $token (sort { 
                if (${$tokens}{$a} eq ${$tokens}{$b}) {
                        $a cmp $b
                } else {
                    if (${$tokens}{$a} =~ /_/) {
                        if (${$tokens}{$b} =~ /_/) {
                            $a cmp $b
                        } else {
                            -1
                        }
                    } else {
                        if (${$tokens}{$b} =~ /_/) {
                            1
                        } else {
                            if (hex ${$tokens}{$a} eq hex ${$tokens}{$b})
                            {
                                $a cmp $b
                            } else {
                                hex ${$tokens}{$a} <=> hex ${$tokens}{$b}
                            }
                        }                    
                    }
                }
            } keys %{$tokens})
            {
            if ($token =~ /^$prefix\_.*/i)
            {
                print EXT "\t" . $token . " " . ${\%{$tokens}}{$token} . "\n";
            }
        }
        foreach my $function (sort keys %{$functions})
        {
            if ($function =~ /^$prefix.*/i)
            {
                print EXT "\t" . ${$functions}{$function}{rtype} . " " . $function . " (" . ${$functions}{$function}{parms} . ")" . "\n";
            }
        }
        close EXT;
    }
}