summaryrefslogtreecommitdiff
path: root/builtins/tools/generate_builtins.pl
blob: 71c0a4d8c0072d70e64fb165f8f1da98016197c7 (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
#!/usr/bin/env perl


sub process_version {
   my ($version) = @_;
   my @vars;
   print "/* Version $version builtins */\n\n";

   my @files = <builtins/$version/*>;
   foreach $file (@files) {
      push(@vars, process_file($file));
   }

   print "static const char *functions_for_$version [] = {\n";
   foreach $var (@vars) {
      print "   $var,\n";
   }
   print "};\n\n"
}

sub process_file {
   my ($file) = @_;

   # Change from builtins/110/foo to builtins_110_foo
   my $var = $file; $var =~ s!/!_!g;

   print "static const char *$var = {\n";
   open SRC, "<", "$file" or die $!;
   while (<SRC>) {
      s/\\/\\\\/g;
      s/\"/\\\"/g;
      s/\n/\\n/g;
      print "   \"$_\"\n";
   }
   print "};\n\n";
   close SRC or die $!;
   return $var;
}

print << 'EOF';
/* DO NOT MODIFY - automatically generated by generate_builtins.pl */
/*
 * Copyright © 2010 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include "glsl_parser_extras.h"
#include "ir_reader.h"

void
read_builtins(_mesa_glsl_parse_state *st, exec_list *instructions,
	      const char **functions, unsigned count)
{
   if (st->error)
      return;

   for (unsigned i = 0; i < count; i++) {
      _mesa_glsl_read_ir(st, instructions, functions[i]);

      if (st->error) {
	 printf("error reading builtin: %.35s ...\n", functions[i]);
         return;
      }
   }
}

EOF

@versions = sort(<builtins/[1-9][0-9][0-9]*>);
foreach $version (@versions) {
   $version =~ s!builtins/!!g;
   process_version($version);
}

print << 'EOF';
void
_mesa_glsl_initialize_functions(exec_list *instructions,
			        struct _mesa_glsl_parse_state *state)
{
EOF

foreach $version (@versions) {
    $version_number = $version;
   if ($version =~ m/_vs/) {
       $version_check = " && state->target == vertex_shader";
   } elsif ($version =~ m/_fs/) {
       $version_check = " && state->target == fragment_shader";
   } else {
       $version_check = "";
   }
   $version_number =~ s/_[vf]s//;
   print "   if (state->language_version >= $version_number$version_check)\n";
   print "      read_builtins(state, instructions, functions_for_$version,\n";
   print "                    sizeof(functions_for_$version) / ";
   print "sizeof(const char *));\n\n"
}

print "}\n";