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 |
|
|
my $dynamic; |
129 |
root |
1.3 |
my $nelem = "size / sizeof (*value)"; |
130 |
root |
1.1 |
|
131 |
|
|
if ($ctype eq "STRING_CLASS") { |
132 |
|
|
$ctype = "VECTOR_CLASS<char>"; |
133 |
|
|
$nelem = "1"; |
134 |
|
|
$dynamic = 1; |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
my $type = $ctype; |
138 |
|
|
my $array = 0; |
139 |
|
|
|
140 |
|
|
if ($type =~ s/^VECTOR_CLASS<\s*(.*)>$/$1/) { |
141 |
|
|
$dynamic = 1; |
142 |
|
|
$array = 1; |
143 |
|
|
} elsif ($type =~ s/<(\d+)>$//) { |
144 |
|
|
$dynamic = 1; |
145 |
|
|
$array = 1; |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
$type = $typemap{$type} |
149 |
|
|
or die "$name: no mapping for $ctype"; |
150 |
|
|
|
151 |
|
|
my $perltype = $type->[2]; |
152 |
|
|
|
153 |
|
|
if ($array && $nelem ne "1") { |
154 |
|
|
$perltype = "\@${perltype}s"; |
155 |
|
|
} else { |
156 |
|
|
$perltype = "\$$perltype"; |
157 |
|
|
} |
158 |
|
|
|
159 |
root |
1.4 |
$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"; |
160 |
root |
1.1 |
|
161 |
root |
1.5 |
# XS1 contains the function before ALIAS, XS2 the function afterwards (the body) |
162 |
|
|
# after we generate the bdoy we look for an identical body generated earlier |
163 |
|
|
# and simply alias us to the earlier xs function, to save text size. |
164 |
|
|
my ($XS1, $XS2); |
165 |
|
|
|
166 |
|
|
$XS1 = "void\n" |
167 |
root |
1.6 |
. "XXXNAMEXXX (OpenCL::$classmap{$real_class} self$extra_xs_args)\n"; |
168 |
root |
1.5 |
$XS2 = " PPCODE:\n"; |
169 |
root |
1.1 |
|
170 |
|
|
if ($dynamic) { |
171 |
root |
1.5 |
$XS2 .= " size_t size;\n" |
172 |
root |
1.6 |
. " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, 0, 0, &size));\n" |
173 |
root |
1.5 |
. " $type->[0] *value = tmpbuf (size);\n" |
174 |
root |
1.6 |
. " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, size, value, 0));\n"; |
175 |
root |
1.1 |
} else { |
176 |
root |
1.5 |
$XS2 .= " $type->[0] value [1];\n" |
177 |
root |
1.6 |
. " NEED_SUCCESS (Get${cbase}Info, (self$extra_args, ix, sizeof (value), value, 0));\n"; |
178 |
root |
1.1 |
} |
179 |
|
|
|
180 |
root |
1.5 |
if ($array && $nelem ne "1") { |
181 |
|
|
$XS2 .= " int i, n = $nelem;\n" |
182 |
|
|
. " EXTEND (SP, n);\n" |
183 |
|
|
. " for (i = 0; i < n; ++i)\n"; |
184 |
root |
1.1 |
} else { |
185 |
root |
1.5 |
$XS2 .= " EXTEND (SP, 1);\n" |
186 |
|
|
. " const int i = 0;\n" |
187 |
root |
1.1 |
} |
188 |
|
|
|
189 |
|
|
if ($type->[1] =~ /^OpenCL::(\S+)$/) { |
190 |
|
|
my $oclass = $1; |
191 |
|
|
$oclass = "MemObject" if $oclass eq "Memory"; |
192 |
|
|
$oclass = "CommandQueue" if $oclass eq "Queue"; |
193 |
|
|
|
194 |
root |
1.5 |
$XS2 .= " {\n"; |
195 |
|
|
$XS2 .= " NEED_SUCCESS (Retain$oclass, (value [i]));\n" unless $1 eq "Platform" || $1 eq "Device"; |
196 |
root |
1.11 |
$XS2 .= " PUSH_CLOBJ (\"$type->[1]\", value [i]);\n"; |
197 |
root |
1.5 |
$XS2 .= " }\n"; |
198 |
root |
1.1 |
} else { |
199 |
root |
1.5 |
$XS2 .= " PUSHs (sv_2mortal ($type->[1]));\n"; |
200 |
root |
1.1 |
} |
201 |
|
|
|
202 |
root |
1.5 |
$XS2 .= "\n"; |
203 |
|
|
|
204 |
|
|
if (my $alias = $alias{"$XS1$XS2"}) { |
205 |
|
|
push @$alias, [$perl_name, $name]; |
206 |
|
|
} else { |
207 |
|
|
push @funcs, [$XS1, (my $alias = [[$perl_name, $name]]), $XS2]; |
208 |
|
|
$alias{"$XS1$XS2"} = $alias; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
my $XS; |
213 |
|
|
|
214 |
|
|
# this very dirty and ugly code is a very dirty and ugly code size optimisation. |
215 |
|
|
for (@funcs) { |
216 |
|
|
$_->[0] =~s /^XXXNAMEXXX/$_->[1][0][0]/m; |
217 |
|
|
|
218 |
|
|
if (@{ $_->[1] } == 1) { # undo ALIAS |
219 |
|
|
$_->[2] =~ s/\bix\b/$_->[1][0][1]/g; |
220 |
|
|
$_->[1] = ""; |
221 |
|
|
} else { |
222 |
|
|
$_->[1] = " ALIAS:\n" . join "", map " $_->[0] = $_->[1]\n", @{ $_->[1] }; |
223 |
|
|
} |
224 |
|
|
$XS .= join "", @$_; |
225 |
root |
1.1 |
} |
226 |
|
|
|
227 |
root |
1.7 |
warn "patching class $CLASS\n"; |
228 |
|
|
|
229 |
root |
1.1 |
patch "OpenCL.xs", "#BEGIN:$CLASS" , "#END:$CLASS" , $XS; |
230 |
root |
1.2 |
patch "OpenCL.pm", "=for gengetinfo begin $CLASS", "=for gengetinfo end $CLASS", $POD; |
231 |
root |
1.1 |
} |
232 |
|
|
|