Rylik

Hello world

To get started, the only thing you need is an output file, which you set in the build function like this:

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",
        };
}

These are all the fields of the StcBuild struct. The block shows .out and .targets together only to list them: a real build gives one of the two, and the compiler refuses a build that gives both.

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",                                 /* required, unless .targets is used */
                .targets = .{                                   /* required, unless .out is used; one entry per binary */
                        .{.name = "hello", .out = "hello"},
                },
                .groups = .{                                    /* optional; without it every lane is in "main" */
                        .{.name = "main", .lanes = .REST},
                },
                .lane_bytes_reserved = @mib(64),                /* optional; set it when lanes need more memory */
                .lane_stack_pages = 16,                         /* can be ignored */
                .jit_bytes_reserved = @mib(16),                 /* can be ignored; only for #jit */
                .on_lane_startup_failure = .END,                /* can be ignored */
                .simd_level_max = .AVX2,                        /* can be ignored; caps the startup SIMD dispatch */
                .debug_information = .ON,                       /* optional; turn on while debugging */
                .date_unix_seconds = 0,                         /* unlikely to be used */
                .warning_emit = .EACH                           /* unlikely to be used */
        };
}

Notes:

  • a "lane" means the same thing as a "thread" as in multi-threading. The word "lane" will be used in stormc.
  • a "group" is a group of lanes that share a barrier and can work together, usually on the same subtask in a program (more on that later).

Hello world:

module("hello");

import {
        println :: "io".println
};

println :: config {
        .bytes_reserved = @kib(64),
        .bytes_committed = @kib(4),
};

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",
        };
}

main :: function_mutate(p: &StcProcess) -> Error.StcError
{
        lane main {
                err := try println("Hello world", .{}) onfail {
                        return err;
                };
        }

        return Error.OK;
}
$ ./hello
Hello world

Without the "lane main", the program would print once from all lanes before exiting. My CPU has 12 threads, so this will print 12 times.

module("hello");

import {
        println :: "io".println
};

println :: config {
        .bytes_reserved = @kib(64),
        .bytes_committed = @kib(4),
};

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",
        };
}

main :: function_mutate(p: &StcProcess) -> Error.StcError
{
        err := try println("Hello world", .{}) onfail {
                return err;
        };

        return Error.OK;
}
$ ./hello
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world
Hello world

An example printing from different lanes of different groups:

module("hello");

import {
        println :: "io".println
};

println :: config {
        .bytes_reserved = @kib(64),
        .bytes_committed = @kib(4),
};

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",
                .groups = .{
                        .{.name = "ui", .lanes = 2},
                        .{.name = "io", .lanes = 3},
                        .{.name = "work", .lanes = .REST},
                },
        };
}

main :: function_mutate(p: &StcProcess) -> Error.StcError
{
        group ui {
                err := try println("[Group#{}][Lane#{}] Hello from ui", .{@group_index, @lane_index}) onfail {
                        return err;
                };
        }

        group io {
                err := try println("[Group#{}][Lane#{}] Hello from io", .{@group_index, @lane_index}) onfail {
                        return err;
                };
        }

        group work {
                err := try println("[Group#{}][Lane#{}] Hello from work", .{@group_index, @lane_index}) onfail {
                        return err;
                };
        }

        return Error.OK;
}
$ ./hello
[Group#1][Lane#0] Hello from io
[Group#0][Lane#0] Hello from ui
[Group#2][Lane#6] Hello from work
[Group#0][Lane#1] Hello from ui
[Group#2][Lane#0] Hello from work
[Group#2][Lane#3] Hello from work
[Group#2][Lane#4] Hello from work
[Group#1][Lane#2] Hello from io
[Group#1][Lane#1] Hello from io
[Group#2][Lane#1] Hello from work
[Group#2][Lane#2] Hello from work
[Group#2][Lane#5] Hello from work

Note that for every group, the lanes start counting from 0. The [.REST] means "the rest of the remaining lanes" (which would be 7 for this machine, after partitioning 5 to the other 2 groups)

The error + try + onfail can be pretty annoying when you have a lot of code that can return errors. So, instead of having that curse stay like in other languages, in stormc you can implicitly handle an error, if you explicitly set it up to be handled automatically.

module("hello");

import {
        println :: "io".println
};

println :: config {
        .bytes_reserved = @kib(64),
        .bytes_committed = @kib(4),
        .onfail = .PASS,                /* on failure, return the error to the caller */
};

build :: function_build() -> StcBuild
{
        return .{
                .out = "hello",
        };
}

main :: function_mutate(p: &StcProcess) -> Error.StcError
{
        lane main {
                try println("Hello world", .{});
                try println("No onfail written here", .{});
        }

        return Error.OK;
}
$ ./hello
Hello world
No onfail written here

Even with this, if you still need to handle a special case somewhere, a try + onfail takes higher precedence over the automatic handling, so your try + onfail is taken for the lowered code rather than the automatic one. If you write [somefunc(...)] that can return an error, you will eventually forget that it returns an error and you won't be able to tell from code which can fail. Therefore, the [try] keyword is still kept. The two other ways to handle it, instead of [.PASS]:

  • .TRAP: a debug build prints the call's location and the error name, then traps. A release build traps without printing.
  • A named handler function: .onfail = handler_name.

This will conclude part 1.

Found this useful? Support the writing on Ko-fi.