blib/lib/SPVM/Builder/Exe.pm

filter: Statementstmt 91.6% 338 / 369 Branchbran 52.2% 23 / 44 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 97.6% 41 / 42 Podpod 0.0% 0 / 17 totaltotal 84.6% 402 / 475 CC 41 SCAR 17.0
CC
41
Cov
87%
parse_option_values_native_class
30.0
build_exe_file
9.0
create_bootstrap_header_source
7.4
CRAP 5.5
« blib/lib/SPVM/Builder/CompileInfo.pm ↑ Summary blib/lib/SPVM/Dist.pm »
1 package SPVM::Builder::Exe;
2  
3 79 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 79 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 79 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 79 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 79 ▶use File::Basename 'basename', 'dirname', 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 79 ▶use File::Path 'mkpath', 'rmtree';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 79 ▶use File::Find 'find';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 79 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 79 ▶use File::Copy 'copy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 79 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 79 ▶use Digest::SHA 'sha1_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 79 ▶use SPVM::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 79 ▶use SPVM::Builder::CC;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 79 ▶use SPVM::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 79 ▶use SPVM::Builder::Config::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 79 ▶use SPVM::Builder::Config::Exe;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 79 ▶use SPVM::Builder::Config::DLL;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 79 ▶use SPVM::Builder::Native::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 79 ▶use SPVM::Builder::Native::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 79 ▶use SPVM::Builder::Native::BasicType;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 79 ▶use SPVM::Builder::Native::ClassFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 79 ▶use SPVM::Builder::ScriptInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 79 ▶use SPVM::Builder::ObjectFileInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 79 ▶use SPVM::Builder::Config::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 79 ▶use SPVM::Builder::Accessor 'has';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # Fields
33 79 my $field_names = [qw(
34 builder
35 script_name
36 class_name
37 output_file
38 config
39 compiler
40 allow_no_config_file
41 mode
42 runtime
43 )];
44 79 has $field_names;
45  
46 # Class Methods
47  
48 78 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod undocumented
49 78 my $class = shift;
50
51 78 my %options = @_;
52
53 78 my $self = bless {
54 external_object_files => [],
55 %options
56 }, $class;
57
58 78 my $script_name = $self->{script_name};
59
60 78 $self->check_script_name;
61
62 # Excutable file name
63 78 my $output_file = $self->{output_file};
64 78 ▶ unless (defined $output_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output_file) 0 78 78
65 0 Carp::confess("A output file must be defined.");
66 }
67
68 78 my $app_name = $self->app_name;
69
70 # Config mode
71 78 my $config_mode = $self->{mode};
72
73 # New SPVM::Builder object
74 my $builder = SPVM::Builder->new(
75 78 ▶ exists $self->{build_dir} ? (build_dir => $self->{build_dir}) : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'build_dir'} ? : 0 78 78
76 );
77
78 # Config file settings
79 78 my $allow_no_config_file = $self->{allow_no_config_file};
80
81 # Config
82 78 my $config_file = SPVM::Builder::Config::Util::create_config_file_path_from_class_file_path($script_name);
83
84 78 my $config;
85 78 ▶ if ($allow_no_config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_no_config_file) { } 6 72 78
86 6 $config = SPVM::Builder::Config::Exe->new;
87 }
88 else {
89 72 $config = SPVM::Builder::Config::Util::load_mode_config($config_file, $config_mode);
90 }
91
92 78 $self->{builder} = $builder;
93
94 # Override config settings with command line options if defined
95 {
96 78 ▶ if (defined $self->{external_object_files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"external_object_files"}) 78 0 78
97 78 $config->external_object_files($self->{external_object_files});
98 }
99 }
100
101 78 $self->config($config);
102
103 78 my $compiler = SPVM::Builder::Native::Compiler->new;
104
105 78 for my $class_search_dir (@{$builder->class_search_dirs}) {
106 1788 $compiler->add_class_search_dir($class_search_dir);
107 }
108
109 78 $self->{compiler} = $compiler;
110
111 78 $self->prepare_compile;
112
113 77 return $self;
114 }
115  
116 # Instance Methods
117 77 ▶sub build_exe_file {
Subroutine build_exe_file: called CC=9 SCAR=22.0
Pod undocumented
118 77 my ($self) = @_;
119
120 77 my $builder = $self->builder;
121
122 77 my $config = $self->config;
123
124 77 my $output_file = $self->{output_file};
125 77 $config->output_file($output_file);
126
127 77 my $class_name = $self->{class_name};
128
129 77 my $class = $self->runtime->get_basic_type_by_name($class_name);
130
131 77 my $class_file = $class->get_class_file;
132
133 77 my $class_names_include_spvm_script_class_name = $self->get_user_defined_basic_type_names;
134 77 my $class_names = [];
135 77 my $spvm_script_class_name;
136 my $spvm_script_config_file;
137 77 for my $class_name (@$class_names_include_spvm_script_class_name) {
138 1729 my $script_name = $self->script_name;
139 1729 my $config_file;
140 1729 ▶ if ($class_name eq $self->class_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class_name eq $self->class_name) { } 77 1652 1729
141 77 $spvm_script_class_name = $class_name;
142 77 $spvm_script_config_file = SPVM::Builder::Config::Util::create_config_file_path_from_class_file_path($script_name);
143 }
144 else {
145 1652 push @$class_names, $class_name;
146 }
147 }
148 77 my $no_compile_resources = 1;
149
150 77 my $builder_cc = SPVM::Builder::CC->new(builder => $builder, runtime => $self->runtime);
151
152 77 my $compile_infos = [];
153
154 77 my $bootstrap_link_info = $self->prepare_compile_bootstrap_source_file;
155 77 push @$compile_infos, @{$bootstrap_link_info->compile_infos};
156
157 77 my $spvm_core_link_info = $builder->prepare_compile_spvm_core_source_files;
158 77 push @$compile_infos, @{$spvm_core_link_info->compile_infos};
159
160 77 for my $class_name (@$class_names) {
161 1652 my $native_link_info = $builder_cc->prepare_compile_native_class($class_name, {no_compile_resources => $no_compile_resources});
162 1652 ▶ if ($native_link_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($native_link_info) 0 1652 1652
163 0 push @$compile_infos, @{$native_link_info->compile_infos};
164 }
165 }
166
167 77 for my $class_name (@$class_names) {
168 1652 my $precompile_link_info = $builder_cc->prepare_compile_precompile_class($class_name);
169 1652 ▶ if ($precompile_link_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($precompile_link_info) 1652 0 1652
170 1652 push @$compile_infos, @{$precompile_link_info->compile_infos};
171 }
172 }
173
174 77 my $spvm_scritp_native_link_info = $builder_cc->prepare_compile_native_class($spvm_script_class_name, {config_file => $spvm_script_config_file});
175 77 ▶ if ($spvm_scritp_native_link_info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spvm_scritp_native_link_info) 71 6 77
176 71 push @$compile_infos, @{$spvm_scritp_native_link_info->compile_infos};
177 }
178
179 77 for my $compile_info (@$compile_infos) {
180 4543 $compile_info->config->output_type('exe');
181 }
182
183 77 my $link_info = SPVM::Builder::LinkInfo->new(config => $config, compile_infos => $compile_infos);
184
185 77 my $diagnostic_message = "[Generate Executable File:\$output_file='$output_file']";
186 77 $link_info->diagnostic_message($diagnostic_message);
187
188 77 $builder->build_parallel_with_link_infos([$link_info], {config_global => $config->global});
189 }
190  
191 78 ▶sub prepare_compile {
Subroutine prepare_compile: called CC=2 SCAR=6.9
Pod undocumented
192 78 my ($self) = @_;
193
194 78 my $script_name = $self->{script_name};
195
196 78 my $compiler = $self->compiler;
197
198 78 ▶ open my $script_fh, '<', $script_name
Branch: 1 branch
BranchTrueFalseTotal
open my $script_fh, "<", $script_name or die "Can't open file '${script_name}':$!" 78 0 78
199 or die "Can't open file '$script_name':$!";
200
201 78 my $source = do { local $/; <$script_fh> };
202
203 78 $source = "#file \"$script_name\"\x{A}$source";
204
205 78 my $class_name = $compiler->compile_anon_class_with_exit($source, __FILE__, __LINE__);
206
207 77 $self->class_name($class_name);
208
209 77 $self->config->class_name($class_name);
210
211 77 my $runtime = $compiler->get_runtime;
212
213 77 $self->runtime($runtime);
214 }
215  
216 77 ▶sub create_bootstrap_header_source {
Subroutine create_bootstrap_header_source: called CC=7 SCAR=20.0
Pod undocumented
217 77 my ($self) = @_;
218
219 77 my $class_name = $self->class_name;
220
221 77 my $class_names = $self->get_user_defined_basic_type_names;
222
223 77 my $source = '';
224
225 77 $source .= <<'EOS';
226  
227 #include <stdio.h>
228 #include <stdlib.h>
229 #include <string.h>
230 #include <inttypes.h>
231 #include <time.h>
232 #include <assert.h>
233  
234 #include "spvm_native.h"
235  
236 EOS
237
238 77 $source .= "// precompile functions declaration\n";
239 77 for my $class_name (@$class_names) {
240 1729 my $class = $self->runtime->get_basic_type_by_name($class_name);
241 1729 my $precompile_method_names = $class->get_precompile_method_names;
242 1729 for my $method_name (@$precompile_method_names) {
243 0 my $class_cname = $class_name;
244 0 $class_cname =~ s/::/__/g;
245 0 $source .= <<"EOS";
246 int32_t SPVMPRECOMPILE__${class_cname}__$method_name(SPVM_ENV* env, SPVM_VALUE* stack);
247 EOS
248 }
249
250 1729 my $basic_type = $self->runtime->get_basic_type_by_name($class_name);
251 1729 my $anon_basic_type_names = $basic_type->get_anon_basic_type_names;
252 1729 for my $anon_basic_type_name (@$anon_basic_type_names) {
253 20 my $anon_basic_type_cname = $anon_basic_type_name;
254 20 $anon_basic_type_cname =~ s/::/__/g;
255
256 20 my $anon_basic_type = $self->runtime->get_basic_type_by_name($anon_basic_type_name);
257 20 my $anon_method = $anon_basic_type->get_method_by_name("");
258 20 ▶ if ($anon_method->is_precompile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anon_method->is_precompile) 0 20 20
259 0 $source .= <<"EOS";
260 int32_t SPVMPRECOMPILE__${anon_basic_type_cname}__(SPVM_ENV* env, SPVM_VALUE* stack);
261 EOS
262 }
263 }
264 }
265
266 77 my $boostrap_name_space = $self->create_boostrap_name_space;
267
268 77 $source .= "void ${boostrap_name_space}build_runtime(SPVM_ENV* env);\n\n";
269
270 77 $source .= "static void set_precompile_method_addresses(SPVM_ENV* env);\n";
271
272 77 $source .= "// native functions declaration\n";
273 77 for my $class_name (@$class_names) {
274 1729 my $class = $self->runtime->get_basic_type_by_name($class_name);
275 1729 my $native_method_names = $class->get_native_method_names;
276 1729 for my $method_name (@$native_method_names) {
277 0 my $class_name = $class_name;
278 0 $class_name =~ s/::/__/g;
279 0 $source .= <<"EOS";
280 int32_t SPVM__${class_name}__$method_name(SPVM_ENV* env, SPVM_VALUE* stack);
281 EOS
282 }
283 }
284
285 77 $source .= "static void set_native_method_addresses(SPVM_ENV* env);\n\n";
286
287 77 $source .= "static void compile_all_classes(SPVM_ENV* env);\n\n";
288
289 77 $source .= <<"EOS";
290 static void set_precompile_method_address(SPVM_ENV* env, const char* class_name, const char* method_name, void* precompile_address);
291  
292 EOS
293  
294 77 $source .= <<"EOS";
295 static void set_native_method_address(SPVM_ENV* env, const char* class_name, const char* method_name, void* native_address);
296  
297 EOS
298  
299 77 return $source;
300 }
301  
302 77 ▶sub create_bootstrap_main_func_source {
Subroutine create_bootstrap_main_func_source: called CC=2 SCAR=7.0
Pod undocumented
303 77 my ($self) = @_;
304
305 77 my $class_name = $self->class_name;
306
307 77 my $class_names = $self->get_user_defined_basic_type_names;
308
309 77 ▶ my $warning = $^W ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^W ? : 0 77 77
310
311 77 my $boostrap_name_space = $self->create_boostrap_name_space;
312
313 77 my $source = '';
314
315 77 $source .= <<"EOS";
316 int main(int argc, char* argv[]);
317 int SPVMCC__main(int argc, char* argv[]);
318  
319 int SPVMCC__main(int argc, char* argv[]) {
320
321 int32_t error_id = 0;
322
323 SPVM_ENV* boot_env = SPVM_NATIVE_new_env();
324
325 void* compiler = boot_env->api->compiler->new_instance();
326
327 SPVM_ENV* env = boot_env->new_env();
328
329 error_id = env->api->compiler->compile(compiler, NULL);
330 assert(error_id == 0);
331
332 env->runtime = env->api->compiler->get_runtime(compiler);
333
334 ${boostrap_name_space}build_runtime(env);
335
336 FILE* spvm_stderr = env->api->runtime->get_spvm_stderr(env->runtime);
337
338 SPVM_VALUE* stack = env->new_stack(env);
339
340 // Set the program name and the command line arguments
341 {
342 int32_t mortal_stack_top = env->enter_scope(env, stack);
343
344 void* obj_program_name = env->new_string(env, stack, argv[0], strlen(argv[0]));
345
346 // ARGV - string[]
347 void* obj_argv = env->new_string_array(env, stack, argc - 1);
348 for (int32_t arg_index = 1; arg_index < argc; arg_index++) {
349 void* obj_arg = env->new_string(env, stack, argv[arg_index], strlen(argv[arg_index]));
350 env->set_elem_object(env, stack, obj_argv, arg_index - 1, obj_arg);
351 }
352
353 int64_t base_time = time(NULL);
354
355 // Set command line info
356 {
357 error_id = env->set_command_info_program_name(env, stack, obj_program_name);
358
359 if (error_id) {
360 env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
361 goto END_OF_FUNC;
362 }
363
364 error_id = env->set_command_info_argv(env, stack, obj_argv);
365
366 if (error_id) {
367 env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
368 goto END_OF_FUNC;
369 }
370
371 error_id = env->set_command_info_basetime(env, stack, base_time);
372 if (error_id) {
373 env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
374 goto END_OF_FUNC;
375 }
376
377 error_id = env->set_command_info_warning(env, stack, $warning);
378 if (error_id) {
379 env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
380 goto END_OF_FUNC;
381 }
382 }
383
384 env->leave_scope(env, stack, mortal_stack_top);
385 }
386
387 const char* class_name = "$class_name";
388
389 error_id = env->check_bootstrap_method(env, stack, class_name);
390
391 if (error_id) {
392 env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
393 goto END_OF_FUNC;
394 }
395
396 error_id = env->call_init_methods(env, stack);
397 if (error_id) {
398 goto END_OF_FUNC;
399 }
400
401 void* class_basic_type = env->api->runtime->get_basic_type_by_name(env->runtime, class_name);
402 void* method = env->api->basic_type->get_method_by_name(env->runtime, class_basic_type, "main");
403
404 int32_t args_width = 0;
405 error_id = env->call_method(env, stack, method, args_width, __func__, __FILE__, __LINE__);
406 if (error_id) {
407 error_id = env->die(env, stack, env->get_chars(env, stack, env->get_exception(env, stack)), __func__, __FILE__, __LINE__);
408 goto END_OF_FUNC;
409 }
410
411 END_OF_FUNC:
412
413 if (error_id) {
414 /* Enter a new scope to handle the exception message object safely */
415 int32_t scope_id = env->enter_scope(env, stack);
416
417 /* Reconstruct the exception message including the SPVM stack trace */
418 void* obj_exception_with_stack_trace = env->build_exception_message(env, stack, 0);
419
420 /* Print the full exception message to stderr */
421 env->print_stderr(env, stack, obj_exception_with_stack_trace);
422 fprintf(spvm_stderr, "\\n");
423
424 /* Leave the scope to free the exception message object */
425 env->leave_scope(env, stack, scope_id);
426 }
427
428 env->call_end_methods(env, stack);
429
430 env->destroy_class_vars(env, stack);
431
432 env->free_stack(env, stack);
433
434 env->free_env(env);
435
436 boot_env->api->compiler->free_instance(compiler);
437
438 boot_env->free_env(boot_env);
439
440 return error_id;
441 }
442  
443 int main(int argc, char* argv[]) {
444 return SPVMCC__main(argc, argv);
445 }
446  
447 EOS
448  
449 77 return $source;
450 }
451  
452 77 ▶sub create_bootstrap_build_runtime_source {
Subroutine create_bootstrap_build_runtime_source: called CC=1 SCAR=0.0
Pod undocumented
453 77 my ($self) = @_;
454
455 77 my $boostrap_name_space = $self->create_boostrap_name_space;
456
457 77 my $source = '';
458
459 77 $source .= <<"EOS";
460  
461 void ${boostrap_name_space}build_runtime(SPVM_ENV* env) {
462
463 compile_all_classes(env);
464
465 set_precompile_method_addresses(env);
466
467 set_native_method_addresses(env);
468 }
469  
470 EOS
471
472 77 return $source;
473 }
474  
475 77 ▶sub create_bootstrap_compile_source {
Subroutine create_bootstrap_compile_source: called CC=4 SCAR=13.9
Pod undocumented
476 77 my ($self) = @_;
477
478 # Namespace
479 77 my $boostrap_name_space = $self->create_boostrap_name_space;
480
481 77 my $source = '';
482
483 # Start function
484 77 $source .= <<"EOS";
485 static void compile_all_classes(SPVM_ENV* env) {
486
487 /* Get compiler */
488 void* compiler = env->api->runtime->get_compiler(env->runtime);
489
490 EOS
491
492 # User defined classes
493 77 my $class_names = $self->get_user_defined_basic_type_names;
494
495 77 my $compiler = $self->compiler;
496
497 77 for my $class_name (@$class_names) {
498
499 1729 my $class = $self->runtime->get_basic_type_by_name($class_name);
500 1729 my $class_file = $compiler->get_class_file($class_name);
501 1729 my $class_file_rel_file = $class_file->get_rel_file;
502 1729 my $class_file_content = $class_file->get_content;
503 1729 my $class_file_content_length = $class_file->get_content_length;
504 1729 my $source_class_file = '';
505
506 1729 $source_class_file .= qq| {\n|;
507
508 # Add class file
509 1729 $source_class_file .= qq| env->api->compiler->add_class_file(compiler, "$class_name");\n|;
510
511 1729 $source_class_file .= qq| void* class_file = env->api->compiler->get_class_file(compiler, "$class_name");\n|;
512
513 # Set relative file path
514 1729 ▶ if (defined $class_file_rel_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class_file_rel_file) 1729 0 1729
515 1729 $source_class_file .= qq| env->api->class_file->set_rel_file(compiler, class_file, "$class_file_rel_file");\n|;
516 }
517
518 # Set content as C string literal
519 1729 ▶ if (defined $class_file_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $class_file_content) 1729 0 1729
520 1729 my $content_literal = $compiler->create_c_string_literal($class_file_content);
521 1729 $source_class_file .= qq| env->api->class_file->set_content(compiler, class_file, \n$content_literal );\n|;
522 }
523
524 # Set content length
525 1729 $source_class_file .= qq| env->api->class_file->set_content_length(compiler, class_file, $class_file_content_length);\n|;
526
527 1729 $source_class_file .= qq| }\n|;
528
529 1729 $source .= $source_class_file;
530 }
531
532 # Set debug info
533 77 $source .= qq| env->api->compiler->set_start_file(compiler, __FILE__);\n|;
534 77 $source .= qq| env->api->compiler->set_start_line(compiler, __LINE__ + 1);\n|;
535
536 # Compile
537 77 my $start_class_name = $self->{class_name};
538 77 $source .= qq| int32_t error_id = env->api->compiler->compile(compiler, \"$start_class_name\");\n|;
539
540 # Error handling
541 77 $source .= qq| void* runtime = env->api->compiler->get_runtime(compiler);\n|;
542 77 $source .= qq| FILE* spvm_stderr = env->api->runtime->get_spvm_stderr(runtime);\n|;
543 77 $source .= qq| if (error_id != 0) {\n|;
544 77 $source .= qq| fprintf(spvm_stderr, "[Unexpected Compile Error]%s.", env->api->compiler->get_error_message(compiler, 0));\n|;
545 77 $source .= qq| exit(255);\n|;
546 77 $source .= qq| }\n|;
547
548 77 $source .= <<"EOS";
549 }
550 EOS
551
552 77 return $source;
553 }
554  
555 77 ▶sub create_bootstrap_set_precompile_method_addresses_func_source {
Subroutine create_bootstrap_set_precompile_method_addresses_func_source: called CC=5 SCAR=16.2
Pod undocumented
556 77 my ($self) = @_;
557
558 77 my $class_names = $self->get_user_defined_basic_type_names;
559
560 77 my $boostrap_name_space = $self->create_boostrap_name_space;
561
562 77 my $source = '';
563
564 77 $source .= <<"EOS";
565 static void set_precompile_method_address(SPVM_ENV* env, const char* class_name, const char* method_name, void* precompile_address) {
566 void* class_basic_type = env->api->runtime->get_basic_type_by_name(env->runtime, class_name);
567 void* method = env->api->basic_type->get_method_by_name(env->runtime, class_basic_type, method_name);
568 env->api->method->set_precompile_address(env->runtime, method, precompile_address);
569 }
570  
571 EOS
572
573 77 $source .= <<"EOS";
574 static void set_native_method_address(SPVM_ENV* env, const char* class_name, const char* method_name, void* native_address) {
575 void* class_basic_type = env->api->runtime->get_basic_type_by_name(env->runtime, class_name);
576 void* method = env->api->basic_type->get_method_by_name(env->runtime, class_basic_type, method_name);
577 env->api->method->set_native_address(env->runtime, method, native_address);
578 }
579  
580 EOS
581  
582 77 $source .= "static void set_precompile_method_addresses(SPVM_ENV* env){\n";
583
584 77 for my $class_name (@$class_names) {
585 1729 my $class = $self->runtime->get_basic_type_by_name($class_name);
586
587 1729 my $class_cname = $class_name;
588 1729 $class_cname =~ s/::/__/g;
589
590 1729 my $precompile_method_names = $class->get_precompile_method_names;
591
592 1729 for my $precompile_method_name (@$precompile_method_names) {
593 0 $source .= <<"EOS";
594 set_precompile_method_address(env, "$class_name", "$precompile_method_name", &SPVMPRECOMPILE__${class_cname}__$precompile_method_name);
595
596 EOS
597 }
598
599 1729 my $basic_type = $self->runtime->get_basic_type_by_name($class_name);
600 1729 my $anon_basic_type_names = $basic_type->get_anon_basic_type_names;
601 1729 for my $anon_basic_type_name (@$anon_basic_type_names) {
602 20 my $anon_basic_type_cname = $anon_basic_type_name;
603 20 $anon_basic_type_cname =~ s/::/__/g;
604
605 20 my $anon_basic_type = $self->runtime->get_basic_type_by_name($anon_basic_type_name);
606 20 my $anon_method = $anon_basic_type->get_method_by_name("");
607 20 ▶ if ($anon_method->is_precompile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anon_method->is_precompile) 0 20 20
608 0 $source .= <<"EOS";
609 set_precompile_method_address(env, "$anon_basic_type_name", "", &SPVMPRECOMPILE__${anon_basic_type_cname}__);
610
611 EOS
612 }
613 }
614 }
615
616 77 $source .= "}\n";
617
618 77 return $source;
619 }
620  
621 77 ▶sub create_bootstrap_set_native_method_addresses_func_source {
Subroutine create_bootstrap_set_native_method_addresses_func_source: called CC=3 SCAR=11.0
Pod undocumented
622 77 my ($self) = @_;
623
624 77 my $class_names = $self->get_user_defined_basic_type_names;
625
626 77 my $boostrap_name_space = $self->create_boostrap_name_space;
627
628 77 my $source = '';
629
630 77 $source .= "static void set_native_method_addresses(SPVM_ENV* env){\n";
631
632 77 for my $class_name (@$class_names) {
633 1729 my $class = $self->runtime->get_basic_type_by_name($class_name);
634
635 1729 my $class_cname = $class_name;
636 1729 $class_cname =~ s/::/__/g;
637
638 1729 my $native_method_names = $class->get_native_method_names;
639
640 1729 for my $native_method_name (@$native_method_names) {
641 0 $source .= <<"EOS";
642 set_native_method_address(env, "$class_name", "$native_method_name", &SPVM__${class_cname}__$native_method_name);
643
644 EOS
645 }
646 }
647  
648 77 $source .= "}\n";
649
650 77 return $source;
651 }
652  
653 77 ▶sub create_bootstrap_source {
Subroutine create_bootstrap_source: called CC=1 SCAR=0.0
Pod undocumented
654 77 my ($self) = @_;
655
656 # Build bootstrap source content
657 77 my $bootstrap_source = '';
658
659 77 $bootstrap_source .= $self->create_bootstrap_header_source;
660
661 77 $bootstrap_source .= $self->create_bootstrap_main_func_source;
662
663 77 $bootstrap_source .= $self->create_bootstrap_build_runtime_source;
664
665 77 $bootstrap_source .= $self->create_bootstrap_compile_source;
666
667 77 $bootstrap_source .= $self->create_bootstrap_set_precompile_method_addresses_func_source;
668
669 77 $bootstrap_source .= $self->create_bootstrap_set_native_method_addresses_func_source;
670
671 77 $bootstrap_source .= "\n";
672
673 77 return $bootstrap_source;
674 }
675  
676 77 ▶sub prepare_compile_bootstrap_source_file {
Subroutine prepare_compile_bootstrap_source_file: called CC=1 SCAR=0.0
Pod undocumented
677 77 my ($self) = @_;
678
679 77 my $builder = $self->builder;
680
681 77 my $bootstrap_source = $self->create_bootstrap_source;
682
683 77 my $source_rel_file = $self->create_bootstrap_source_rel_file_path($bootstrap_source);
684
685 77 my $bootstrap_source_file = $builder->create_build_src_path($source_rel_file);
686
687 77 SPVM::Builder::Util::spurt_binary($bootstrap_source_file, $bootstrap_source, $builder->global_lock_fh);
688
689 77 my $config = SPVM::Builder::Util::API::create_default_config();
690 77 my $source_dir = $builder->create_build_src_path;
691
692 77 my $compile_info = SPVM::Builder::CompileInfo->new(
693 source_dir => $source_dir,
694 source_rel_file => $source_rel_file,
695 config => $config,
696 category => 'bootstrap',
697 diagnostic_message => "[Compile Bootstrap File]",
698 );
699
700 77 my $link_info = SPVM::Builder::LinkInfo->new(config => $config, compile_infos => [$compile_info]);
701
702 77 return $link_info;
703 }
704  
705 462 ▶sub get_user_defined_basic_type_names {
Subroutine get_user_defined_basic_type_names: called CC=1 SCAR=0.0
Pod undocumented
706 462 my ($self) = @_;
707
708 462 my $runtime = $self->runtime;
709
710 462 my $category = [
711 4, # SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_MULNUM
712 6, # SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_CLASS,
713 7, # SPVM_NATIVE_C_BASIC_TYPE_CATEGORY_INTERFACE,
714 ];
715
716 # Classes for anon methods are contained in another class.
717 462 my $basic_types = [grep { $_->get_name !~ /::anon_method::/ } @{$runtime->get_basic_types({category => $category})}];
718
719 462 my $class_names = [map { $_->get_name } @$basic_types];
720
721 462 return $class_names;
722 }
723  
724 0 ▶sub parse_option_values_native_class {
Subroutine parse_option_values_native_class: not called CC=5 SCAR=34.0
Pod undocumented
725 0 my ($class, $option_name, $option_values, $value_is_array) = @_;
726
727 0 my $hash = {};
728 0 for my $option_value (@$option_values) {
729
730 0 ▶ if ($option_value =~ /^([\w:]+)\@(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option_value =~ /^([\w:]+)\@(.+)/) { } 0 0 0
731 0 my $class_name = $1;
732
733 0 my $value = $2;
734
735 0 ▶ if ($value_is_array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_is_array) { } 0 0 0
736 0 ▶ $hash->{$class_name} //= [];
Condition0%0/2
$hash->{$class_name} //= []
l!l
00
MC/DC0%0/1
$hash->{$class_name} //= []
$hash->{$class_name}
Truth table0%0/2
A
A= $hash->{$class_name}
Aresult
11
00
737 0 push @{$hash->{$class_name}}, $value;
738 }
739 else {
740 0 $hash->{$class_name} = $value;
741 }
742 }
743 else {
744 0 Carp::confess "Invalid option value '$option_value' for $option_name option.";
745 }
746 }
747
748 0 return $hash;
749 }
750  
751 77 ▶sub create_bootstrap_source_rel_file_path {
Subroutine create_bootstrap_source_rel_file_path: called CC=2 SCAR=7.0
Pod undocumented
752 77 my ($self, $bootstrap_source) = @_;
753
754 77 ▶ unless (defined $bootstrap_source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bootstrap_source) 0 77 77
755 0 confess("A bootstrap source must be defined.");
756 }
757
758 # Create a SHA-1 hex digest from the bootstrap source content
759 77 my $digest = sha1_hex($bootstrap_source);
760 77 my $dir_1 = substr($digest, 0, 2);
761 77 my $dir_2 = substr($digest, 2);
762
763 # Get the base filename without extension
764 77 my $script_name = $self->script_name;
765 77 my $file_base = basename $script_name;
766 77 $file_base =~ s/\..*$//;
767
768 # Final relative path: bootstrap/[xx]/[xxxx...]/[script_name].c
769 77 my $bootstrap_source_rel_file = "bootstrap/$dir_1/$dir_2/$file_base.c";
770
771 77 return $bootstrap_source_rel_file;
772 }
773  
774 78 ▶sub check_script_name {
Subroutine check_script_name: called CC=5 SCAR=19.3
Pod undocumented
775 78 my ($self) = @_;
776
777 78 my $script_name = $self->{script_name};
778
779 78 ▶ unless (defined $script_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $script_name) 0 78 78
780 0 Carp::confess("A script name must be defined.");
781 }
782
783 78 my ($script_name_base) = fileparse $script_name;
784
785 78 ▶ unless ($script_name_base =~ /^[a-zA-Z_][a-zA-Z_0-9\-]*\.spvm$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($script_name_base =~ /^[a-zA-Z_][a-zA-Z_0-9\-]*\.spvm$/) 0 78 78
786 0 Carp::confess "The script base name must match the regex qr/^[a-zA-Z_][a-zA-Z_0-9\\-]*\\.spvm\$/. SCRIPT_NAME='$script_name'.";
787 }
788
789 78 ▶ if ($script_name_base =~ /--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_name_base =~ /--/) 0 78 78
790 0 Carp::confess "The script base name cannnot contain '--'. SCRIPT_NAME=$script_name.";
791 }
792
793 78 ▶ if ($script_name_base =~ /__/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($script_name_base =~ /__/) 0 78 78
794 0 Carp::confess "The script base name cannot contain '__'. SCRIPT_NAME=$script_name.";
795 }
796
797 }
798  
799 540 ▶sub app_name {
Subroutine app_name: called CC=1 SCAR=0.0
Pod undocumented
800 540 my ($self) = @_;
801
802 540 my $script_name = $self->script_name;
803
804 540 my $app_name = basename $script_name;
805 540 $app_name =~ s/\.spvm$//;
806
807 540 return $app_name;
808 }
809  
810 462 ▶sub create_boostrap_name_space {
Subroutine create_boostrap_name_space: called CC=2 SCAR=7.0
Pod undocumented
811 462 my ($self, $app_name) = @_;
812
813 462 ▶ unless (defined $app_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $app_name) 462 0 462
814 462 $app_name = $self->app_name;
815 }
816
817 462 $app_name =~ s/-/____/g;
818
819 462 my $name_space = "SPVM_BOOTSTRAP_${app_name}______";
820
821 462 return $name_space;
822 }
823  
824 79 1;
825  
826 =head1 Name
827  
828 SPVM::Builder::Exe - Creating Executable File
829  
830 =head1 Copyright & License
831  
832 Copyright (c) 2023 Yuki Kimoto
833  
834 MIT License
« blib/lib/SPVM/Builder/CompileInfo.pm ↑ Summary blib/lib/SPVM/Dist.pm »