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