NAME
SPVM - Static Perl Virtual Machine. Fast Calculation, Fast Array Operation, and Easy C/C++ Binding.
SYNOPSIS
SPVM Module:
# lib/MyMath.spvm
package MyMath {
sub sum : int ($nums : int[]) {
my $total = 0;
for (my $i = 0; $i < @$nums; $i++) {
$total += $nums->[$i];
}
return $total;
}
}
Use SPVM Module from Perl
# spvm.pl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use SPVM 'MyMath';
# Call subroutine
my $total = MyMath->sum([3, 6, 8, 9]);
print "Total: $total\n";
# Call subroutine with packed data
my $nums_packed = pack('l*', 3, 6, 8, 9);
my $sv_nums = SPVM::new_int_array_from_bin($nums_packed);
my $total_packed = MyMath->sum($sv_nums);
print "Total Packed: $total_packed\n";
Precompiled SPVM Subroutine. This means SPVM code is converted to Machine Code:
# lib/MyMath.spvm
package MyMath : precompile {
sub sum_precompile : int ($nums : int[]) {
my $total = 0;
for (my $i = 0; $i < @$nums; $i++) {
$total += $nums->[$i];
}
return $total;
}
}
Call SPVM Precompile Subroutine from Perl
# spvm.pl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use SPVM 'MyMath';
# Call precompile subroutine
my $total_precompile = MyMath->sum_precompile([3, 6, 8, 9]);
print "Total Precompile: $total_precompile\n";
SPVM Native Subroutine. This means SPVM subroutine call C/C++ native subroutine:
# lib/MyMath.spvm
package MyMath {
native sub sum_native : int ($nums : int[]);
}
// lib/MyMath.c
#include "spvm_native.h"
int32_t SPNATIVE__MyMath__sum_native(SPVM_ENV* env, SPVM_VALUE* stack) {
void* sv_nums = stack[0].oval;
int32_t length = env->length(env, sv_nums);
int32_t* nums = env->get_elems_int(env, sv_nums);
int32_t total = 0;
for (int32_t i = 0; i < length; i++) {
total += nums[i];
}
stack[0].ival = total;
return SPVM_SUCCESS;
}
# lib/MyMath.config
use strict;
use warnings;
use SPVM::Builder::Config;
my $bconf = SPVM::Builder::Config->new_c99;
$bconf;
Use SPVM Native Subroutine from Perl
# spvm.pl
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use SPVM 'MyMath';
# Call native subroutine
my $total_native = MyMath->sum_native([3, 6, 8, 9]);
print "Total Native: $total_native\n";
Environment Variable "SPVM_BUILD_DIR" must be set for precompile and native subroutine
# bash example
export SPVM_BUILD_DIR=~/.spvm_build
DESCRIPTION
SPVM is Static Perl Virtual Machine. Provide fast calculation & easy C/C++ Binding.
Features:
Fast culcuration, Fast array operation, Small memory
Perl syntax, Static typing, Switch syntax, Have language specification
Enum, Type inference, Anon subroutine, Variable captures
Array initialization,
Reference count GC, Weaken reference, Module system
Exception, Package variable
Object oriented, Inteface, Value type, Value array type, Reference type
Easy way to C/C++ binding, Automatically Perl binding, C99 math functions
Shared Library, Precompile Subroutine into Machine code
Native API(C level api), C99 standard
DOCUMENT
Currently some ports of document are use Automatic translation, so not accurate and maybe difficult to read.
CORE MODULES
SPVM Core Modules.
FUNCTIONS
Function names and examples is only listed.
See SPVM Exchange API about the details.
new_string
my $spvm_string = SPVM::new_string("あいう");
New SPVM string from decoded string.
Return value is SPVM::BlessedObject::String object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::String.
new_string_from_bin
my $spvm_string = SPVM::new_string_from_bin("abc");
New SPVM string from binary data.
Return value is SPVM::BlessedObject::String object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::String.
new_byte_array
my $spvm_nums = SPVM::new_byte_array([ 1, -5, 100]);
New SPVM byte array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_byte_array_unsigned
my $spvm_nums = SPVM::new_byte_array_unsigned([1, 2, 255]);
New SPVM byte array from Perl array reference. Each element in Perl array reference is interpreted an unsigned 8-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_byte_array_len
my $spvm_nums = SPVM::new_byte_array_len(3)
New SPVM byte array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_byte_array_from_bin
# Pack singed 8-bit integers
my $bin = pack('c*', 1, -5, 100);
my $spvm_nums = SPVM::new_byte_array_from_bin($bin);
# Pack unsigned 8-bit integers
my $bin = pack('C*', 1, 2, 255);
my $spvm_nums = SPVM::new_byte_array_from_bin($bin);
New SPVM byte array with packed binary data. The packed binary data is interpreted a sequence of signed singed 8-bit integers or unsigned 8-bit intergers.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_byte_array_from_string
use utf8;
my $string = "あいう";
my $spvm_nums = new_byte_array_from_string($string);
New SPVM byte array from decoded Perl string. The decoded Perl string is encoded to UTF-8.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_short_array
my $spvm_nums = SPVM::new_short_array([1, 2, 3]);
New SPVM short array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_short_array_unsigned
my $spvm_nums = SPVM::new_short_array_unsigned([1, 2, 65535]);
New SPVM short array from Perl array reference. Each element in Perl array reference is interpreted an unsigned 16-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_short_array_len
my $spvm_nums = SPVM::new_short_array_len(3)
New SPVM short array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_short_array_from_bin
# Pack signed 16-bit intergers
my $bin = pack('s*', 1, -5, 100);
my $spvm_nums = SPVM::new_short_array_from_bin($bin);
# Pack unsigned 16-bit intergers
my $bin = pack('S*', 1, 2, 65535);
my $spvm_nums = SPVM::new_short_array_from_bin($bin);
New SPVM short array with packed binary data. The packed binary data is interpreted a sequence of signed 16-bit interger or unsinged 16-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_int_array
my $spvm_nums = SPVM::new_int_array([1, 2, 3]);
New SPVM int array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_int_array_unsigned
my $spvm_nums = SPVM::new_int_array_unsigned([1, 2, 4294967295]);
New SPVM int array from Perl array reference. Each element in Perl array reference is interpreted an unsigned 32-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_int_array_len
my $spvm_nums = SPVM::new_int_array_len(3)
New SPVM int array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_int_array_from_bin
# Pack signed 32-bit intergers
my $bin = pack('l*', 1, -5, 100);
my $spvm_nums = SPVM::new_int_array_from_bin($bin);
# Pack unsigned 32-bit intergers
my $bin = pack('L*', 1, 2, 65535);
my $spvm_nums = SPVM::new_int_array_from_bin($bin);
New SPVM int array with packed binary data. The packed binary data is interpreted a sequence of signed 32-bit interger or unsinged 32-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_long_array
my $spvm_nums = SPVM::new_long_array([1, 2, 3]);
New SPVM long array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_long_array_unsigned
my $spvm_nums = SPVM::new_long_array_unsigned([1, 2, 18446744073709551615]);
New SPVM long array from Perl array reference. Each element in Perl array reference is interpreted an unsigned 64-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_long_array_len
my $spvm_nums = SPVM::new_long_array_len(3)
New SPVM long array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_long_array_from_bin
# Pack signed 64-bit intergers
my $bin = pack('l*', 1, -5, 100);
my $spvm_nums = SPVM::new_long_array_from_bin($bin);
# Pack unsigned 64-bit intergers
my $bin = pack('L*', 1, 2, 65535);
my $spvm_nums = SPVM::new_long_array_from_bin($bin);
New SPVM long array with packed binary data. The packed binary data is interpreted a sequence of signed 64-bit interger or unsinged 64-bit integer.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_float_array
my $spvm_nums = SPVM::new_float_array([1.5, 2.5, 3.0]);
New SPVM float array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_float_array_len
my $spvm_nums = SPVM::new_float_array_len(3)
New SPVM float array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_float_array_from_bin
# Pack float value
my $bin = pack('f*', 1, -5.5, 4.5);
my $spvm_nums = SPVM::new_float_array_from_bin($bin);
New SPVM float array with packed binary data. The packed binary data is interpreted a sequence of 32-bit floating points.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_double_array
my $spvm_nums = SPVM::new_double_array([1.5, 2.5, 3.0]);
New SPVM double array from Perl array reference.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_double_array_len
my $spvm_nums = SPVM::new_double_array_len(3)
New SPVM double array with array length.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_double_array_from_bin
# Pack double value
my $bin = pack('d*', 1, -5.5, 4.5);
my $spvm_nums = SPVM::new_double_array_from_bin($bin);
New SPVM double array with packed binary data. The packed binary data is interpreted a sequence of 64-bit floating points.
Return value is SPVM::BlessedObject::Array object. If you want to convert SPVM array to Perl data structure, use the methods of SPVM::BlessedObject::Array.
new_object_array
new_string_array
new_mulnum_array
new_mulnum_array_from_bin
get_exception
set_exception
array_to_bin
array_to_elems
get_memory_blocks_count
call_sub
ENVIRONMENT VARIABLE
SPVM_BUILD_DIR
SPVM build directory for precompile and native subroutine.
If SPVM_BUILD_DIR environment variable is not set, SPVM can't compile precompile subroutine and native subroutine, and a exception occur. You see error message "SPVM_BUILD_DIR environment variable must be set ...".
In bash, you can set SPVM_BUILD_DIR to the following.
export SPVM_BUILD_DIR=~/.spvm_build
CAUTION
This release is beta release before SPVM 1.0. Features is changed without warnings.
SPVM 1.0 is First Major Release
But Full backward compatibility is not guaranteed because SPVM is not used much in corporate work yet.
If SPVM has fatal bugs in the specification or implementation, the backward compatibility is broken and the bug will be fixed after discussion.
SUPPORT
If you have problems or find bugs, comment to GitHub Issue.
AUTHOR
Yuki Kimoto <kimoto.yuki@gmail.com<gt>
CORE DEVELOPERS
moti<lt>motohiko.ave@gmail.com<gt>
CONTRIBUTERS
Mohammad S Anwar
akinomyoga
NAGAYASU Shinya
Reini Urban
chromatic
Kazutake Hiramatsu
Yasuaki Omokawa
COPYRIGHT & LICENSE
Copyright 2018-2020 Yuki Kimoto, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 469:
You forgot a '=back' before '=head1'