blib/lib/XS/TCC.pm

filter: Statementstmt 32.3% 64 / 198 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 69.5% 16 / 23 Podpod 0.0% 0 / 1 totaltotal 26.1% 80 / 306 SCAR 66.7
CC
41
Cov
24%
tcc_inline
382.1
_gen_single_function_xs_wrapper
210.0
_get_core_typemap
56.0
CRAP 790.2
↑ Summary blib/lib/XS/TCC/Parser.pm »
1 package XS::TCC;
2 2 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.04';
7  
8 use constant {
9 2 TCC_OUTPUT_MEMORY => 0,
10 TCC_OUTPUT_EXE => 1,
11 TCC_OUTPUT_DLL => 2,
12 TCC_OUTPUT_OBJ => 3,
13 TCC_OUTPUT_PREPROCESS => 4,
14 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use ExtUtils::Embed ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use ExtUtils::Typemaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use ExtUtils::ParseXS::Eval;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Alien::TinyCC;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 our $RuntimeIncludeDir = File::ShareDir::dist_dir('XS-TCC');
28 2 our $TinyCCIncludeDir = Alien::TinyCC->libtcc_include_path;
29 2 our $TinyCCLibDir = File::Spec->catdir( Alien::TinyCC->libtcc_library_path, 'tcc' );
30  
31 2 ▶use XS::TCC::Typemaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use XS::TCC::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 XSLoader::load('XS::TCC', $VERSION);
35  
36 2 our @EXPORT_OK = qw(
37 tcc_inline
38 TCC_OUTPUT_MEMORY
39 TCC_OUTPUT_EXE
40 TCC_OUTPUT_DLL
41 TCC_OUTPUT_OBJ
42 TCC_OUTPUT_PREPROCESS
43 );
44 2 our %EXPORT_TAGS = (all => \@EXPORT_OK);
45  
46 2 our $CCOPTS;
47 {
48 2 local $0 = "NOT A -e LINE!"; # ExtUtils::Embed is daft
49 2 $CCOPTS = ExtUtils::Embed::ccopts;
50 }
51  
52 2 my $CodeHeader = <<'HERE';
53 #ifndef XS_TCC_INIT
54 #define XS_TCC_INIT
55 /* #define PERL_NO_GET_CONTEXT */
56  
57 #ifdef __XS_TCC_DARWIN__
58 /* http://comments.gmane.org/gmane.comp.compilers.tinycc.devel/325 */
59 typedef unsigned short __uint16_t, uint16_t;
60 typedef unsigned int __uint32_t, uint32_t;
61 typedef unsigned long __uint64_t, uint64_t;
62 #endif
63  
64 #include <EXTERN.h>
65 #include <perl.h>
66 #include <XSUB.h>
67  
68 #ifdef HAS_BUILTIN_EXPECT
69 # undef HAS_BUILTIN_EXPECT
70 # ifdef EXPECT
71 # undef EXPECT
72 # define EXPECT(expr, val) (expr)
73 # endif
74 #endif
75  
76 #include <typemap_func.h>
77  
78 /* The XS_EXTERNAL macro is used for functions that must not be static
79 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
80 * macro defined, the best we can do is assume XS is the same.
81 * Dito for XS_INTERNAL.
82 */
83 #ifndef XS_EXTERNAL
84 # define XS_EXTERNAL(name) XS(name)
85 #endif
86 #ifndef XS_INTERNAL
87 # define XS_INTERNAL(name) XS(name)
88 #endif
89  
90 #ifndef PERL_UNUSED_VAR
91 # define PERL_UNUSED_VAR(var) if (0) var = var
92 #endif
93  
94 #ifndef dVAR
95 # define dVAR dNOOP
96 #endif
97  
98  
99 #ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
100 #define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)
101  
102 /* prototype to pass -Wmissing-prototypes */
103 STATIC void
104 S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params);
105  
106 STATIC void
107 S_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
108 {
109 const GV *const gv = CvGV(cv);
110  
111 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
112  
113 if (gv) {
114 const char *const gvname = GvNAME(gv);
115 const HV *const stash = GvSTASH(gv);
116 const char *const hvname = stash ? HvNAME(stash) : NULL;
117  
118 if (hvname)
119 Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
120 else
121 Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
122 } else {
123 /* Pants. I don't think that it should be possible to get here. */
124 Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
125 }
126 }
127 #undef PERL_ARGS_ASSERT_CROAK_XS_USAGE
128  
129 #ifdef PERL_IMPLICIT_CONTEXT
130 # define croak_xs_usage(a,b) S_croak_xs_usage(aTHX_ a,b)
131 #else
132 # define croak_xs_usage S_croak_xs_usage
133 #endif
134  
135 #endif
136  
137 #endif /* XS_TCC_INIT */
138 HERE
139  
140 SCOPE: {
141 2 my @compilers; # never die...
142 #my $compiler;
143 sub _get_compiler {
144 #return $compiler if $compiler;
145 0 ▶ my $compiler = XS::TCC::TCCState->new;
Subroutine _get_compiler: not called CC=2 SCAR=17.9
146 0 $compiler->set_lib_path($TinyCCLibDir);
147 0 $compiler->add_sysinclude_path($TinyCCIncludeDir);
148 0 $compiler->add_sysinclude_path($RuntimeIncludeDir);
149 0 ▶ if ($^O eq 'darwin') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "darwin") 0 0 0
150 0 $compiler->define_symbol("__XS_TCC_DARWIN__", 1);
151 }
152 #push @compilers, $compiler;
153 0 return $compiler;
154 } # end _get_compiler
155 } # end SCOPE
156  
157  
158 2 SCOPE: {
159 2 my $core_typemap;
160 sub _get_core_typemap {
161 0 ▶ return $core_typemap if $core_typemap;
Subroutine _get_core_typemap: not called CC=7 SCAR=40.3
Branch: 1 branch
BranchTrueFalseTotal
if $core_typemap 0 0 0
162  
163 0 my @tm;
164 0 foreach my $dir (@INC) {
165 0 my $file = File::Spec->catfile($dir, ExtUtils => 'typemap');
166 0 ▶ unshift @tm, $file if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 0 0 0
167 }
168  
169 0 $core_typemap = ExtUtils::Typemaps->new();
170 0 foreach my $typemap_loc (@tm) {
171 0 ▶ next unless -f $typemap_loc;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $typemap_loc 0 0 0
172 # skip directories, binary files etc.
173 0 ▶ warn("Warning: ignoring non-text typemap file '$typemap_loc'\n"), next
Branch: 1 branch
BranchTrueFalseTotal
unless -T $typemap_loc 0 0 0
174 unless -T $typemap_loc;
175  
176 0 $core_typemap->merge(file => $typemap_loc, replace => 1);
177 }
178  
179 # Override core typemaps with custom function-based replacements.
180 # This is because GCC compiled functions are likely faster than inlined code in TCC.
181 0 $core_typemap->merge(replace => 1, typemap => $XS::TCC::Typemaps::Typemap);
182  
183 0 return $core_typemap;
184 } # end _get_core_typemap
185 } # end SCOPE
186  
187 # current options:
188 # code, warn_code, package, typemap, add_files, ccopts
189 2 sub tcc_inline (@) {
190 0 ▶ my $code;
Subroutine tcc_inline: not called CC=20 SCAR=59.5
Pod undocumented
191  
192 0 ▶ $code = pop @_ if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
193 0 my %args = @_;
194  
195 0 ▶ if (defined $code and defined $args{code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $code and defined $args{"code"}) 0 0 0
Condition0%0/3
defined $code and defined $args{"code"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $code and defined $args{"code"}
defined $code defined $args{"code"}
Truth table0%0/3
A and B
A= defined $codeB= defined $args{"code"}
ABresult
0X0
100
111
196 0 Carp::croak("Can't specify code both as a named and as a positional parameter");
197 }
198 0 ▶ $code //= $args{code};
Condition0%0/3
$code //= $args{"code"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$code //= $args{"code"}
$code $args{"code"}
Truth table0%0/3
A //= B
A= $codeB= $args{"code"}
ABresult
1X1
011
000
199 0 ▶ Carp::croak("Need code to compile") if not defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 0 0
200  
201 0 ▶ my $package = $args{package} // (caller())[0];
Condition0%0/3
$args{"package"} // (caller())[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"package"} // (caller())[0]
$args{"package"} (caller())[0]
Truth table0%0/3
A // B
A= $args{"package"}B= (caller())[0]
ABresult
1X1
011
000
202  
203 # Set up the typemap object if any (defaulting to core typemaps)
204 0 my $typemap;
205 0 my $typemap_arg = $args{typemap};
206 0 ▶ if (not defined($typemap_arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $typemap_arg) { } 0 0 0
elsif (ref $typemap_arg) { } 0 0 0
207 0 $typemap = _get_core_typemap();
208 }
209 elsif (ref($typemap_arg)) {
210 0 $typemap = _get_core_typemap()->clone(shallow => 1);
211 0 $typemap->merge(typemap => $typemap_arg);
212 }
213 else {
214 0 $typemap = _get_core_typemap()->clone(shallow => 1);
215 0 $typemap->add_string(string => $typemap_arg);
216 }
217  
218 # Function signature parsing
219 0 my $parse_result = XS::TCC::Parser::extract_function_metadata($code);
220 return
221 if not $parse_result
222 0 ▶ or not @{$parse_result->{function_names}};
Branch: 1 branch
BranchTrueFalseTotal
unless $parse_result and @{$parse_result->{"function_names"};} 0 0 0
Condition0%0/3
$parse_result and @{$parse_result->{"function_names"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$parse_result and @{$parse_result->{"function_names"};}
$parse_result @{$parse_result->{"function_names"};}
Truth table0%0/3
A and B
A= $parse_resultB= @{$parse_result->{"function_names"};}
ABresult
0X0
100
111
223  
224 # eval the typemaps for the function sig
225 0 my @code = ($CodeHeader, $code);
226 0 foreach my $cfun_name (@{$parse_result->{function_names}}) {
227 0 my $fun_info = $parse_result->{functions}{$cfun_name};
228 0 my $xs_fun = _gen_single_function_xs_wrapper($package, $cfun_name, $fun_info, $typemap, \@code);
229 0 $fun_info->{xs_function_name} = $xs_fun;
230 }
231  
232 0 my $final_code = join "\n", @code;
233  
234 0 ▶ warn _add_line_nums($final_code) if $args{warn_code};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"warn_code"} 0 0 0
235  
236 0 my $compiler = _get_compiler();
237  
238 # Code to catch compile errors
239 0 my $errmsg;
240 0 ▶ my $err_hook = sub { $errmsg = $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
241  
242 0 $compiler->set_error_callback($err_hook);
243  
244 # Add user-specified files
245 0 my @add_files;
246 0 @add_files = ref($args{add_files}) ? @{$args{add_files}} : $args{add_files}
247 0 ▶ if defined $args{add_files};
Branch: 2 branches
BranchTrueFalseTotal
if defined $args{"add_files"} 0 0 0
ref $args{'add_files'} ? : 0 0 0
248 0 $compiler->add_file($_) for @add_files;
249  
250 # Do the compilation
251 0 ▶ $compiler->set_options(($args{ccopts} // $CCOPTS));
Condition0%0/3
$args{"ccopts"} // $XS::TCC::CCOPTS
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"ccopts"} // $XS::TCC::CCOPTS
$args{"ccopts"} $XS::TCC::CCOPTS
Truth table0%0/3
A // B
A= $args{"ccopts"}B= $XS::TCC::CCOPTS
ABresult
1X1
011
000
252 # compile_string() returns 0 if succeeded, -1 otherwise.
253 0 my $fatal = $compiler->compile_string($final_code);
254 0 $compiler->relocate();
255  
256 0 ▶ if (defined $errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errmsg) 0 0 0
257 0 $errmsg = _build_compile_error_msg($errmsg, 1);
258 0 ▶ if ($fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) { } 0 0 0
259 0 Carp::croak($errmsg);
260 } else {
261 0 Carp::carp($errmsg);
262 }
263 }
264  
265 # install the XSUBs
266 0 foreach my $cfun_name (@{$parse_result->{function_names}}) {
267 0 my $fun_info = $parse_result->{functions}{$cfun_name};
268 0 my $sym = $compiler->get_symbol($fun_info->{xs_function_name});
269 0 my $perl_name = $package . "::" . $cfun_name;
270 0 my $sub = $sym->as_xsub();
271 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
272 0 *{"$perl_name"} = $sub;
273 }
274  
275 }
276  
277  
278 sub _build_compile_error_msg {
279 0 ▶ my ($msg, $caller_level) = @_;
Subroutine _build_compile_error_msg: not called CC=1 SCAR=6.9
280 0 $caller_level++;
281 # TODO write code to emit file/line info
282 0 return $msg;
283 }
284  
285 sub _gen_single_function_xs_wrapper {
286 0 ▶ my ($package, $cfun_name, $fun_info, $typemap, $code_ary) = @_;
Subroutine _gen_single_function_xs_wrapper: not called CC=14 SCAR=53.5
287  
288 0 my $arg_names = $fun_info->{arg_names};
289 0 my $nparams = scalar(@$arg_names);
290 0 my $arg_names_str = join ", ", map {s/\W/_/; $_} @$arg_names;
291  
292 # Return type and output typemap preparation
293 0 my $ret_type = $fun_info->{return_type};
294 0 my $is_void_function = $ret_type eq 'void';
295 0 ▶ my $retval_decl = $is_void_function ? '' : "$ret_type RETVAL;";
Branch: 1 branch
BranchTrueFalseTotal
$is_void_function ? : 0 0 0
296  
297 0 my $out_typemap;
298 my $outputmap;
299 0 my $dxstarg = "";
300 0 ▶ if (not $is_void_function) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_void_function) 0 0 0
301 0 $out_typemap = $typemap->get_typemap(ctype => $ret_type);
302 0 ▶ $outputmap = $out_typemap
Branch: 1 branch
BranchTrueFalseTotal
$out_typemap ? : 0 0 0
303 ? $typemap->get_outputmap(xstype => $out_typemap->xstype)
304 : undef;
305 0 ▶ Carp::croak("No output typemap found for return type '$ret_type'")
Branch: 1 branch
BranchTrueFalseTotal
unless $outputmap 0 0 0
306 if not $outputmap;
307 # TODO implement TARG optimization below
308 #$dxstarg = $outputmap->targetable ? " dXSTARG;" : "";
309 }
310  
311 # Emit function header and declarations
312 0 (my $xs_pkg_name = $package) =~ s/:+/_/g;
313 0 my $xs_fun_name = "XS_${xs_pkg_name}_$cfun_name";
314 0 push @$code_ary, <<FUN_HEADER;
315 XS_EXTERNAL($xs_fun_name); /* prototype to pass -Wmissing-prototypes */
316 XS_EXTERNAL($xs_fun_name)
317 {
318 dVAR; dXSARGS;$dxstarg
319 if (items != $nparams)
320 croak_xs_usage(cv, "$arg_names_str");
321 /* PERL_UNUSED_VAR(ax); */ /* -Wall */
322 /* SP -= items; */
323 {
324 $retval_decl
325  
326  
327 FUN_HEADER
328  
329 0 my $do_pass_threading_context = $fun_info->{need_threading_context};
330  
331 # emit input typemaps
332 0 my @input_decl;
333 my @input_assign;
334 0 for my $argno (0..$#{$fun_info->{arg_names}}) {
335 0 my $aname = $fun_info->{arg_names}[$argno];
336 0 my $atype = $fun_info->{arg_types}[$argno];
337 0 (my $decl_type = $atype) =~ s/^\s*const\b\s*//;
338  
339 0 my $tm = $typemap->get_typemap(ctype => $atype);
340 0 ▶ my $im = !$tm ? undef : $typemap->get_inputmap(xstype => $tm->xstype);
Branch: 1 branch
BranchTrueFalseTotal
!$tm ? : 0 0 0
341  
342 0 ▶ Carp::croak("No input typemap found for type '$atype'")
Branch: 1 branch
BranchTrueFalseTotal
unless $im 0 0 0
343 if not $im;
344 0 my $imcode = $im->cleaned_code;
345  
346 0 my $vars = {
347 Package => $package,
348 ALIAS => $cfun_name,
349 func_name => $cfun_name,
350 Full_func_name => $cfun_name,
351 pname => $package . "::" . $cfun_name,
352 type => $decl_type,
353 ntype => $decl_type,
354 arg => "ST($argno)",
355 var => $aname,
356 init => undef,
357 # FIXME some of these are guesses at their true meaning. Validate in EU::PXS
358 num => $argno,
359 printed_name => $aname,
360 argoff => $argno,
361 };
362  
363 # FIXME do we want to support the obscure ARRAY/Ptr logic (subtype, ntype)?
364 0 my $out = ExtUtils::ParseXS::Eval::eval_input_typemap_code(
365 $vars, qq{"$imcode"}, $vars
366 );
367  
368 0 $out =~ s/;\s*$//;
369 0 ▶ if ($out =~ /^\s*\Q$aname\E\s*=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /^\s*\Q$aname\E\s*=/) { } 0 0 0
370 0 push @input_decl, " $decl_type $out;";
371 }
372 else {
373 0 push @input_decl, " $decl_type $aname;";
374 0 push @input_assign, " $out;";
375 }
376 }
377 0 push @$code_ary, @input_decl, @input_assign;
378  
379 # emit function call
380 0 ▶ my $fun_call_assignment = $is_void_function ? "" : "RETVAL = ";
Branch: 1 branch
BranchTrueFalseTotal
$is_void_function ? : 0 0 0
381 0 my $arglist = join ", ", @{ $fun_info->{arg_names} };
382 0 my $threading_context = "";
383 0 ▶ if ($do_pass_threading_context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_pass_threading_context) 0 0 0
384 0 ▶ $threading_context = scalar(@{ $fun_info->{arg_names} }) == 0
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$fun_info->{'arg_names'};} == 0 ? : 0 0 0
385 ? "aTHX " : "aTHX_ ";
386 }
387 0 push @$code_ary, " ${fun_call_assignment}$cfun_name($threading_context$arglist);\n";
388  
389 # emit output typemap
390 0 ▶ if (not $is_void_function) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_void_function) 0 0 0
391 0 my $omcode = $outputmap->cleaned_code;
392 0 my $vars = {
393 Package => $package,
394 ALIAS => $cfun_name,
395 func_name => $cfun_name,
396 Full_func_name => $cfun_name,
397 pname => $package . "::" . $cfun_name,
398 type => $ret_type,
399 ntype => $ret_type,
400 arg => "ST(0)",
401 var => "RETVAL",
402 };
403  
404 # FIXME do we want to support the obscure ARRAY/Ptr logic (subtype, ntype)?
405  
406 # TODO TARG ($om->targetable) optimization!
407 0 my $out = ExtUtils::ParseXS::Eval::eval_output_typemap_code(
408 $vars, qq{"$omcode"}, $vars
409 );
410 0 push @$code_ary, " ST(0) = sv_newmortal();";
411 0 push @$code_ary, " " . $out;
412 }
413  
414  
415 0 ▶ my $nreturnvalues = $is_void_function ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$is_void_function ? : 0 0 0
416 0 push @$code_ary, <<FUN_FOOTER;
417 }
418 XSRETURN($nreturnvalues);
419 }
420 FUN_FOOTER
421  
422 0 return($xs_fun_name);
423 }
424  
425 # just for debugging
426 sub _add_line_nums {
427 0 ▶ my $code = shift;
Subroutine _add_line_nums: not called CC=2 SCAR=17.9
428 0 ▶ my $i = shift || 1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
429 0 my @l = split /\n/, $code;
430 0 my $n = @l + $i - 1;
431 0 my $len = length($n);
432 0 return join("\n", map sprintf("% ${len}u: %s", $i++, $_), @l);
433 }
434  
435 2 1;
436  
437 __END__
↑ Summary blib/lib/XS/TCC/Parser.pm »