Mica 7.7.10 is a small release that closes open ends of the 7.7.x road. A var among statements now declares several groups, as a variable section does. A loop or a branch whose body is written as the empty statement, one semicolon too many behind do, then or else, compiles with a warning that names begin end as the spelling of a body meant to be empty. The export’s reaching definitions see every write: the runtime’s writes through a variable’s address, on the GPU and on the host, and a store to one element or one field, which ends no other definition. A device scalar assigned from another is a value of its own, where 7.7.9 handed the source’s handle on.

A var among statements declares several groups

Since 7.2 a compound statement declares variables of its own where a statement may stand, and they belong to the compound. Until now such a var read one group: a second line, high : int64 := 4;, was read as a statement and refused. A var among statements now declares what a variable section declares, several groups of one name or of several names of one type, and a name alone on its line may carry its initial value.

{
    Groups - a 'var' among statements declares several groups, as a variable section does, and a named loop follows it.
}
program Groups;

imp
    WriteLn : std;

const
    Format = "low=%lld high=%lld sum=%lld squares=%lld";

begin
    var
        low : int64 := 1;
        high : int64 := 4;
        i, sum, squares : int64;

    scan : for i := low to high do
    begin
        sum := sum + i;
        squares := squares + i * i;

        if squares > 20 then
            leave scan
    end;

    WriteLn(Format, low, high, sum, squares)
end.

It prints:

low=1 high=4 sum=10 squares=30

The section ends where what follows a name can only begin a statement. A name and a colon with for, while or repeat behind it name the loop that follows, scan : for above; a name and a colon with any other word behind it, or names separated by commas and closed by a colon, begin a further group; and anything else, sum := 0 or WriteLn(...), is the statement behind the section. The grammar settles the rule, since no statement begins with a name and a comma, and the one statement that begins with a name and a colon is a named loop. The formatter lays such a section out as it lays out a declaration section, and the editor colours every name it declares.

An empty body draws a warning

One semicolon too many behind do is the classic slip of the Pascal family. for i := 1 to 3 do; is a loop that repeats nothing three times, and the compound meant as its body runs once, behind the loop. The program compiles and runs exactly as written, so nothing stopped it. Now the build says what it reads:

{
    Slip - one semicolon too many behind 'do': the loop repeats nothing, and the compound meant as its body runs once.
}
program Slip;

imp
    WriteLn : std;

const
    Format = "rounds counted: %lld";

var
    i, rounds : int64;

begin
    for i := 1 to 3 do;
    begin
        rounds := rounds + 1
    end;

    WriteLn(Format, rounds)
end.

The build warns at the semicolon, and the program prints one round where three were meant:

Warnings:
    16: for i := 1 to 3 do;
                          ^ analyzer warning 5803 [16,23]: the loop's body is empty: write 'begin end' if an empty body is meant
rounds counted: 1

A branch behind then or else left as the empty statement draws warning 5804 in the same words, the branch behind 'then' is empty: write 'begin end' if an empty branch is meant. A body written begin end is the spelling of an empty body that is meant and draws nothing, and so do a case arm that does nothing and a never-entered loop the optimizer folds away, since the warning is raised before the fold, at debug and at O2 alike. The editor shows the warning as it shows every diagnostic, at the semicolon. GCC and Clang say the same for C under -Wempty-body.

The reaching definitions see every write

7.7.9’s export states, for every read of a variable, the writes that may reach it, the reaching definitions, and the editor’s follow one value walks that listing. Two kinds of writes were missing from it. A write of a device variable is a call of the floor through the variable’s address, and so is the host runtime’s write of a tensor’s result, c := a * b over host matrices, or of a value handed over whole, h := ToHost(d); the listing named the routine’s entry for every such read. Every one of the runtime’s 305 labeled functions now states, in one table of the platform, the cells it writes through its arguments or through a table of cell addresses, and the compiler stops at its start for a function without a row.

The second kind is the textbook’s. A store to one element of an array or one field of a record was counted as a definition of the whole name that ends every other, so the read of c[0, 0] behind c := a + b, lowered as four element stores, listed the store to c[1, 1] alone. A store to a part of a name is a partial definition: it reaches every later read of the name together with the definitions before it, and only a definition of the whole name, a store of the whole value or a runtime call that writes the name’s storage, ends them. The read now lists the four stores, and a variable stored only in part also lists the routine’s entry.

A device scalar is a value of its own

kept := loss between two variables declared float32 on gpu copies the cell. In 7.7.9 the assignment handed the source’s handle on instead, so the copy read its source’s later value, and two further assignments ended the program with a segmentation fault; every read of a device scalar also handed the floor a copy of the handle cell, so a never-assigned one received a fresh zero buffer at each read. The assignment now copies the buffer, every device entry reads the variable through its own cell, and a never-assigned device scalar reads as zero and receives one buffer. Device tensors were never affected.

Fixed

  • A device scalar assigned from another device scalar copies the cell, where it shared the source’s handle and could end the program with a segmentation fault; a never-assigned device scalar reads as zero and receives one buffer.
  • The export’s reaching definitions list the runtime’s writes of a device variable and of a host tensor’s result, where they listed the routine’s entry.
  • The export’s reaching definitions count a store to one element or one field as a partial definition that ends no other, where it ended every other definition of the name.

Upgrading

Things a 7.7.9 build meets differently in 7.7.10:

  • A loop or a branch whose body is the empty statement draws warning 5803 or 5804. The program compiles and runs as before; write begin end where an empty body is meant, or move the semicolon.
  • A var among statements reads every group behind it. No program 7.7.9 compiled reads differently: the spellings that are groups now were refused before. Two refusals move with it: outer : begin directly behind such a section is a further group whose type is missing and draws 2065 where it drew 2045, and a name and a colon typed halfway with the next group behind them draw 2015 at that group’s comma.
  • The Mica extension for VS Code needs no new version for this release: the warning reaches the editor through the language server, and the grammar of the editor reads a var section as before.

The packages and the commands that install them are on the install page.