ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/OpenCL/gengetinfo
Revision: 1.14
Committed: Fri May 4 14:46:02 2012 UTC (12 years ago) by root
Branch: MAIN
Changes since 1.13: +6 -0 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     # almost every "if" or "?" in this file indicates a place where the OpenCL
4     # designers fucked it up - each time you have to remember an exception to
5     # the naming rules.
6    
7     use common::sense;
8    
9     my %classmap = qw(
10     platform Platform
11     device Device
12     context Context
13     event Event
14     profiling Event
15     mem Memory
16     image Image
17 root 1.7 gl_texture Image
18 root 1.1 sampler Sampler
19     program Program
20     program_build Program
21     kernel Kernel
22     kernel_work_group Kernel
23     command_queue Queue
24     );
25    
26     my %typemap = (
27     cl_bool => ['cl_bool', 'value [i] ? &PL_sv_yes : &PL_sv_no', 'boolean'],
28     #char => ['char', 'newSVpvn (value, size)', 'string'],
29     char => ['char', 'newSVpv (value, 0)', 'string'], # all these are 0-terminated strings, and the driver often appends a \0
30     size_t => ['size_t', 'newSVuv (value [i])', 'int'],
31     "void*" => ['void *', 'newSVuv ((IV)(intptr_t)value [i])', 'ptr_value'],
32     cl_platform_id => ['cl_platform_id', 'OpenCL::Platform'],
33     Context => ['cl_context', 'OpenCL::Context', 'ctx'],
34     Device => ['cl_device_id', 'OpenCL::Device', 'device'],
35     cl_device_id => ['cl_device_id', 'OpenCL::Device', 'device'],
36     Memory => ['cl_mem', 'OpenCL::Memory', 'mem'],
37     Program => ['cl_program', 'OpenCL::Program', 'program'],
38     CommandQueue => ['cl_command_queue', 'OpenCL::Queue', 'queue'],
39     cl_context_properties => ['cl_context_properties', 'newSVuv ((UV)value [i])', 'property_int'],
40     );
41    
42     {
43     my %tmap = (
44     T_IV => "newSViv (value [i])",
45     T_UV => "newSVuv (value [i])",
46     );
47    
48     open my $fh, "<typemap"
49     or die "typemap: $!";
50    
51     while (<$fh>) {
52     next if /^INPUT$/;
53     my ($name, $type) = split /\s+/, $_;
54     if ($tmap{$type}) {
55     $typemap{$name} = [$name, $tmap{$type}, substr $name, 3];
56     }
57     }
58     }
59    
60     sub patch($$$$) {
61     my ($file, $beg, $end, $contents) = @_;
62    
63     {
64     local $/;
65    
66     open my $fh, "<$file"
67     or die "$file: $!";
68    
69     my $data = <$fh>;
70     $data =~ s/^(\Q$beg\E\n).*?\n(\Q$end\E\n)/$1\n$contents$2/sm
71     or die "$file: couldn't find $beg/$end";
72    
73     open my $fh2, ">$file~"
74     or die "$file~: $!";
75    
76     syswrite $fh2, $data;
77     }
78    
79     rename "$file~", $file;
80     }
81    
82 root 1.7 for my $CLASS (qw(platform device context command_queue mem image sampler program program_build kernel kernel_work_group event profiling gl_texture)) {
83 root 1.1 open my $fh, "<getinfo.txt"
84     or die "getinfo.txt: $!";
85    
86     my $POD;
87 root 1.5 my @funcs;
88     my %alias;
89 root 1.1
90     while (<$fh>) {
91     chomp;
92 root 1.10 my ($class, $name, $ctype) = split /\s*,\s*/, $_, 3;
93 root 1.1 next unless $class eq "cl_$CLASS\_info";
94     next if $name eq "CL_IMAGE_FORMAT"; # struct
95     next if $name eq "CL_PROGRAM_BINARIES"; # needs multiple calls
96    
97     $ctype =~ s/cl:://g;
98     $ctype =~ s/::size_t/size_t/g;
99    
100     my $cbase = $class;
101     $cbase =~ s/_(.)/\U$1/g;
102 root 1.9 $cbase =~ s/Gl(?=[A-Z])/GL/g;
103 root 1.1 $cbase =~ s/^cl//;
104     $cbase =~ s/Info$//;
105     $cbase = "MemObject" if $cbase eq "Mem";
106     $cbase = "EventProfiling" if $cbase eq "Profiling";
107    
108     my $real_class = $CLASS;
109     $real_class = "program" if $real_class eq "program_build";
110     $real_class = "kernel" if $real_class eq "kernel_work_group";
111     $real_class = "event" if $real_class eq "profiling";
112    
113     my $perl_name = lc $name;
114     $perl_name =~ s/^cl_//;
115     $perl_name =~ s/^$real_class\_//;
116     $perl_name =~ s/^queue\_//;
117    
118     my $extra_args;
119     my $extra_perl_args;
120     my $extra_xs_args;
121    
122     if ($CLASS eq "program_build" || $CLASS eq "kernel_work_group") {
123     $extra_args = ', device';
124     $extra_perl_args = ' ($device)';
125     $extra_xs_args = ', OpenCL::Device device';
126     }
127    
128 root 1.14 if ($CLASS eq "kernel_arg") {
129     $extra_args = ', idx';
130     $extra_perl_args = ' ($idx)';
131     $extra_xs_args = ', cl_uint idx';
132     }
133    
134 root 1.1 my $dynamic;
135 root 1.3 my $nelem = "size / sizeof (*value)";
136 root 1.1
137     if ($ctype eq "STRING_CLASS") {
138     $ctype = "VECTOR_CLASS<char>";
139     $nelem = "1";
140     $dynamic = 1;
141     }
142    
143     my $type = $ctype;
144     my $array = 0;
145    
146     if ($type =~ s/^VECTOR_CLASS<\s*(.*)>$/$1/) {
147     $dynamic = 1;
148     $array = 1;
149     } elsif ($type =~ s/<(\d+)>$//) {
150     $dynamic = 1;
151     $array = 1;
152     }
153    
154     $type = $typemap{$type}
155     or die "$name: no mapping for $ctype";
156    
157     my $perltype = $type->[2];
158    
159     if ($array && $nelem ne "1") {
160     $perltype = "\@${perltype}s";
161     } else {
162     $perltype = "\$$perltype";
163     }
164    
165 root 1.13 (my $perlenum = $name) =~ s/^CL_/OpenCL::/ or die;
166    
167     $POD .= "=item $perltype = \$$real_class->$perl_name$extra_perl_args\n\nCalls C<clGet${cbase}Info> with C<$perlenum> and returns the result.\n\n";
168 root 1.1
169 root 1.5 # XS1 contains the function before ALIAS, XS2 the function afterwards (the body)
170     # after we generate the bdoy we look for an identical body generated earlier
171     # and simply alias us to the earlier xs function, to save text size.
172     my ($XS1, $XS2);
173    
174     $XS1 = "void\n"
175 root 1.6 . "XXXNAMEXXX (OpenCL::$classmap{$real_class} self$extra_xs_args)\n";
176 root 1.5 $XS2 = " PPCODE:\n";
177 root 1.1
178     if ($dynamic) {
179 root 1.5 $XS2 .= " size_t size;\n"
180 root 1.6 . " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, 0, 0, &size));\n"
181 root 1.5 . " $type->[0] *value = tmpbuf (size);\n"
182 root 1.6 . " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, size, value, 0));\n";
183 root 1.1 } else {
184 root 1.5 $XS2 .= " $type->[0] value [1];\n"
185 root 1.6 . " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, sizeof (value), value, 0));\n";
186 root 1.1 }
187    
188 root 1.5 if ($array && $nelem ne "1") {
189     $XS2 .= " int i, n = $nelem;\n"
190     . " EXTEND (SP, n);\n"
191     . " for (i = 0; i < n; ++i)\n";
192 root 1.1 } else {
193 root 1.5 $XS2 .= " EXTEND (SP, 1);\n"
194     . " const int i = 0;\n"
195 root 1.1 }
196    
197     if ($type->[1] =~ /^OpenCL::(\S+)$/) {
198     my $oclass = $1;
199     $oclass = "MemObject" if $oclass eq "Memory";
200     $oclass = "CommandQueue" if $oclass eq "Queue";
201    
202 root 1.12 my $stash = lc $type->[1];
203     $stash =~ s/opencl:://;
204     $stash =~ s/::/_/g;
205    
206 root 1.5 $XS2 .= " {\n";
207 root 1.12 $XS2 .= " NEED_SUCCESS (Retain$oclass, (value [i]));\n" unless $oclass eq "Platform" || $oclass eq "Device";
208     $XS2 .= " PUSH_CLOBJ (stash_$stash, value [i]);\n";
209 root 1.5 $XS2 .= " }\n";
210 root 1.1 } else {
211 root 1.5 $XS2 .= " PUSHs (sv_2mortal ($type->[1]));\n";
212 root 1.1 }
213    
214 root 1.5 $XS2 .= "\n";
215    
216     if (my $alias = $alias{"$XS1$XS2"}) {
217     push @$alias, [$perl_name, $name];
218     } else {
219     push @funcs, [$XS1, (my $alias = [[$perl_name, $name]]), $XS2];
220     $alias{"$XS1$XS2"} = $alias;
221     }
222     }
223    
224     my $XS;
225    
226     # this very dirty and ugly code is a very dirty and ugly code size optimisation.
227     for (@funcs) {
228     $_->[0] =~s /^XXXNAMEXXX/$_->[1][0][0]/m;
229    
230     if (@{ $_->[1] } == 1) { # undo ALIAS
231     $_->[2] =~ s/\bix\b/$_->[1][0][1]/g;
232     $_->[1] = "";
233     } else {
234     $_->[1] = " ALIAS:\n" . join "", map " $_->[0] = $_->[1]\n", @{ $_->[1] };
235     }
236     $XS .= join "", @$_;
237 root 1.1 }
238    
239 root 1.7 warn "patching class $CLASS\n";
240    
241 root 1.1 patch "OpenCL.xs", "#BEGIN:$CLASS" , "#END:$CLASS" , $XS;
242 root 1.2 patch "OpenCL.pm", "=for gengetinfo begin $CLASS", "=for gengetinfo end $CLASS", $POD;
243 root 1.1 }
244