SPVM Getting Started

Let's take SPVM for the first time. Then, let's improve the performance of the SPVM code.

First SPVM Program

Let's take SPVM for the first time. This is a first simple example. Let's calcurate the sum of numbers.

Create SPVM module

Create SPVM module. The extension is "spvm". In this example, the name of SPVM module is "MyMath.spvm".

Create "MyMath.spvm" in the "lib" directory, and you write the following code.

# 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;
  }
}

Package Definition

Write Package Definition by package keyword. Unlike Perl, SPVM always need package. The whole SPVM grammar is a set of packages.

# Package Definition
package MyMath {

}

See also Package - SPVM Language Specification about Package.

Subroutine Definition

Write Subroutine Definition by sub keyword. Unlike Perl, SPVM Subroutine Definition have return type and argument types.

package MyMath {
  # Subroutine Definition
  sub sum : int ($nums : int[]) {
    
  }
}

See also Subroutine - SPVM Language Specification about Subroutine.

Numeric Types

int type in sum return value is signed 32bit integer type. This is same as C99 int32_t.

int

SPVM has the following types as other numeric types.

SPVM numric type C99 type
byte int8_t
short int16_t
int int32_t
long int64_t
float float
double double

See also Type - SPVM Language Specification about Type.

Array Types

int[] type in sum arguments is array of int type.

int[]

The numeric array type guarantees that the values are consecutive in memory.

In this point, unlike Perl where all value are assigned to the dynamic type SV, SPVM has a static type, so it can represent a efficient sequence of numbers.

See also Type - SPVM Language Specification about Type.

Lexical Variable Declaration

Write Lexical Variable Declaration by my keyword. You can initialize variables at the same time you declare variables.

my $total = 0;

The above declaration has the same meaning as:

my $total : int = 0;

0 on the right side is a signed 32-bit integer, so the type of the variable is automatically determined by type inference.

See also Lexical Variabe - SPVM Language Specification about Lexical Variabe Declaration.

See also Type Inference - SPVM Language Specification about Type Inference.

Integer Literal

0 is Integer Literal.

my $total : int = 0;

SPVM has the following Literal.

  • Integer Literal
  • Floating Point Literal
  • Charater Literal
  • String Literal
# Integer Literal
123
123_456_789

# Floating Point Literal
1.32
1.32f

# Character Literal
'a'
'c'

# String Literal
"Hello World!"

See also Literal - SPVM Language Specification about Literal.

Get Array Length

Let's look at the part where the sum of the arrays is calculated.

for (my $i = 0; $i < @$nums; $i++) {
  $total += $nums->[$i];
}

See @$nums.

@$nums

@ is Array Length Operator to get array length.

Unlike Perl, which is context sensitive, Array Length Operator of SPVM always returns the length of the array.

Note that SPVM has no context.

See Array Length Operator - SPVM Language Specification about Array Length Operator

Increment Operator

Incremental Operator increment the value.

$i++

See Increment Operator - SPVM Language Specification about Incremental Operator.

See Decrement Operator - SPVM Language Specification about Decrement Operator.

Array Access

Array Access can be done by "->" Arrow Operator.

$nums->[$i]

See Array - SPVM Language Specification about Array operation.

Addition Operator

See the following code.

$total += $nums->[$i];

This is same as the following Additon Operator and Assignment Operator.

$total = $total + $nums->[$i];

SPVM has many operators.

See Operator - SPVM Language Specification about Opeartor.

for Statement

Next, see for Statement.

for (my $i = 0; $i < @$nums; $i++) {
  $total += $nums->[$i];
}

See for Statement - SPVM Language Specification about for Statement.

See Statement - SPVM Language Specification about Statement like if Statement, while Statement, and switch Statement.

return Statement

At last, return Statement.

return $total;

See return Statement - SPVM Language Specification about return Statement.

Call SPVM Subroutine from Perl

Create "sum.pl" file and write the following code. This is Perl program.

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";

Add library path

The followings are the conventions and add current script directry's "lib" directory to library path .

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";

use SPVM module

use SPVM module.

use SPVM 'MyMath';

In this place, compilation is not done. Collect SPVM modules.

Call SPVM Subroutine

Call SPVM Subroutine. It's amazing that SPVM subroutine can be called as Perl subroutine.

# Call subroutine
my $total = MyMath->sum([3, 6, 8, 9]);

Perl array reference is converted to SPVM int array.

See Convert Perl Data to SPVM Value - SPVM Exchange API about Conversion of Perl Data to SPVM Value.

SPVM int return value is converted to Perl Scalar.

See Converting SPVM Value to Perl Data - SPVM Exchange API about Conversion of Perl Data to SPVM Value.

Call SPVM Subroutine with packed data

you can pass packed binary data. SPVM::new_int_array_from_bin create SPVM int array from packed binary data. This is efficient.

# 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);

See SPVM Exchange API about SPVM Exchange API like SPVM::new_int_array_from_bin.

How to improve SPVM Performance

See How to improve SPVM Performance.

If you're searching SPVM for performance reasons, here's what you really want to see.