SPVM - Static Perl Virtual Machine. Fast Calculation, Fast Array Operation, and Easy C/C++ Binding.
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
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
Currently some ports of document are use Automatic translation, so not accurate and maybe difficult to read.
SPVM Document
SPVM Tutorial
SPVM Language Specification
SPVM Exchange API
SPVM Native API
SPVM Performance Benchmark
SPVM Core Modules.
SPVM::Byte
SPVM::ByteList
SPVM::Cloner
SPVM::Comparator
SPVM::Complex_2b
SPVM::Complex_2d
SPVM::Complex_2f
SPVM::Complex_2i
SPVM::Complex_2l
SPVM::Complex_2s
SPVM::Double
SPVM::DoubleList
SPVM::EqualityChecker
SPVM::EqualityChecker::SameObject
SPVM::Float
SPVM::FloatList
SPVM::Hash
SPVM::Hash::Entry
SPVM::Int
SPVM::IntList
SPVM::IO::File
SPVM::IO::FileHandle
SPVM::IO::Stderr
SPVM::IO::Stdout
SPVM::Long
SPVM::LongList
SPVM::Math
SPVM::NDArray::Byte
SPVM::NDArray::ComplexByte
SPVM::NDArray::ComplexDouble
SPVM::NDArray::ComplexFloat
SPVM::NDArray::ComplexInt
SPVM::NDArray::ComplexLong
SPVM::NDArray::ComplexShort
SPVM::NDArray::Double
SPVM::NDArray::Float
SPVM::NDArray::Int
SPVM::NDArray::Long
SPVM::NDArray::QuatByte
SPVM::NDArray::QuatDouble
SPVM::NDArray::QuatFloat
SPVM::NDArray::QuatInt
SPVM::NDArray::QuatLong
SPVM::NDArray::QuatShort
SPVM::NDArray::Short
SPVM::NDArray::String
SPVM::ObjectList
SPVM::Quat_4b
SPVM::Quat_4d
SPVM::Quat_4f
SPVM::Quat_4i
SPVM::Quat_4l
SPVM::Quat_4s
SPVM::Regex
SPVM::Regex::Pattern
SPVM::Regex::Replacer
SPVM::Short
SPVM::ShortList
SPVM::Sort
SPVM::StringBuffer
SPVM::Stringer
SPVM::StringList
SPVM::Time
SPVM::Time::Info
SPVM::Unicode
SPVM::Util
Get started with SPVM.
At first I will explain how to write for statement in SPVM.
Next, I will explain how to precompile SPVM subroutines so that they run at the same speed as in machine language.
Finally, I will explain how to write SPVM subroutines biding C language.
If you are interested in the SPVM tutorial, see SPVM Tutorial.
This is a simple example using the SPVM for statement.
You create "lib" direcotry and create "MyMath.spvm" file.
The extension of SPVM is ".spvm".
SPVM need package definition.
package MyMath { }
See SPVM subroutine definition.
package MyMath { sub sum : int ($nums : int[]) { } }
"int" after subroutine name "sum" is return value type.
"int" is signed 32bit numeric type.
"int[]" is argument type.
"int[]" is array type that element type is "int".
SPVM is static type language. You must specify subroutine return type and argument type.
See subroutine implementation.
sub sum : int ($nums : int[]) { my $total = 0; for (my $i = 0; $i < @$nums; $i++) { $total += $nums->[$i]; } return $total; }
"my" is lexical variable declaration.
my $total = 0;
There is no type declaration. Type inference is performed using the value on the right side. Same as the description below.
my $total :int = 0;
See for loop.
for (my $i = 0; $i < @$nums; $i++) { $total += $nums->[$i]; }
for loop is same as Perl syntax. You can get array length by @. @ is array length operator in all context instead of Perl.
Array access use arrow operator.
$nums->[$i]
At last, return value.
return $total;
If you want to know the syntax of SPVM, the SPVM Language Specification has a complete description.
You may be surprised to know that SPVM subroutines can be called directly from Perl.
use FindBin; use lib "$FindBin::Bin/lib"; use SPVM 'MyMath'; # Call subroutine my $total = MyMath->sum([3, 6, 8, 9]); print $total . "\n";
To load an SPVM module from Perl, use the following syntax.
use SPVM 'MyMath';
Call SPVM subroutine from Perl.
# Call subroutine my $total = MyMath->sum([3, 6, 8, 9]);
Perl array reference is converted to SPVM int array, and call sum method of MyMath, and SPVM int value of return value is converted to Perl Scalar.
If you want to know the rules for calling SPVM subroutines from Perl, and the rules for converting Perl and SPVM values, see the SPVM Exchagne API.
See how to speed up SPVM subroutines. SPVM subroutines can be converted into machine code by precompile descriptor.
Subroutine code is same as non precompile subroutine.
A build directory is required to precompile subroutines.
You set SPVM_BUILD_DIR. The following is bash example.
# ~/.bashrc export SPVM_BUILD_DIR=~/.spvm_build
How fast will it be? See SPVM Peformance Benchmark.
If you want, you can native subroutine for performance with native descriptor.
# lib/MyMath.spvm package MyMath { native sub sum_native : int ($nums : int[]); }
Native subrosutine is binding a function of C langage.
// 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; }
File name is "MyMath.c".
At first, include "spvm_native.h". This header provide SPVM Native API.
#include "spvm_native.h"
See the declaration of C function.
int32_t SPNATIVE__MyMath__sum_native(SPVM_ENV* env, SPVM_VALUE* stack) { return SPVM_SUCCESS; }
Return value type is int32_t. The return value indicates that the subroutine did not throw an exception.
SPVM_SUCCESS is macro that value is 0 defined in "spvm_native.h".
C function name start SPNATIVE__. Package name "MyMath" and subroutine name "sum_native" is joined by "__".
If package name contains double colon(Foo::Bar), double colon is replaced by "__".
First argument is a pointer to SPVM_ENV object. This variable has SPVM runtime information.
Second argument is SPVM_VALUE array. stack contains arguments of SPVM subroutine.
And stack is also used to set return value of SPVM subroutine.
See implementation of C function.
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; }
At first, get first argument of SPVM subroutine.
void* sv_nums = stack[0].oval;
"int[]" is array type. array type is also object type. You can get a object by oval field.
oval
Get array length.
int32_t length = env->length(env, sv_nums);
Get numeric int32_t array of C langage. SPVM int type is same as int32_t type of C language.
int32_t* nums = env->get_elems_int(env, sv_nums);
Calcuration.
int32_t total = 0; for (int32_t i = 0; i < length; i++) { total += nums[i]; }
At last, set return value to the first element of stack as int type. ival filed is used to get or set int32_t value.
ival
stack[0].ival = total;
Note that you do not return the SPVM return value with the return keyword of C langauge.
return value of C function indicates whether the subroutine throw an exception.
If you write a native subroutine, you must write a configuration file.
The configuration file for native subroutines is a Perl script.
# lib/MyMath.config use strict; use warnings; use SPVM::Builder::Config; my $bconf = SPVM::Builder::Config->new_c99; $bconf;
If you write SPVM subroutine using C language, you can use new_c99 method of SPVM::Builder::Config.
new_c99
If you want to see SPVM Native API like length and get_elems_int
int32_t length = env->length(env, sv_nums); int32_t* nums = env->get_elems_int(env, sv_nums);
See SPVM Native API.
A build directory is required to native subroutines.
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
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.
If you have problems or find bugs, comment to GitHub Issue.
SPVM(GitHub).
Yuki Kimoto <kimoto.yuki@gmail.com<gt>
moti<lt>motohiko.ave@gmail.com<gt>
Mohammad S Anwar
akinomyoga
NAGAYASU Shinya
Reini Urban
chromatic
Kazutake Hiramatsu
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:
You forgot a '=back' before '=head1'
To install SPVM, copy and paste the appropriate command in to your terminal.
cpanm
cpanm SPVM
CPAN shell
perl -MCPAN -e shell install SPVM
For more information on module installation, please visit the detailed CPAN module installation guide.