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