Summary

Topics: make_unique() - Bits in Boolean context - All-zero bit string literals and Register set specifiers in assembler and disassembler

This week was spent on bug fixes, and enhancing memory tightness of the core VM.


make_unique()

There were many places in the VM where the following construction was used:

unique_ptr<viua::something::Something>(new viua::something::Something(42));
    

It was unfortunate, since this construction can lead to memory leaks if an exception is thrown between the construction of new viua::something::Something and the moment it becomes guarded by the unique_ptr. Viua strives to correctly manage its memory, and account for every resource it allocates, so this was clearly unacceptable.

The solution came in the form of std::make_unique(). This function safely allocates memory, constructs an object, and wraps it in a unique_ptr. It also leads to shorter code and less repetition:

/*
 * Type is repeated two times, and
 * memory can be leaked.
 */
auto a = unique_ptr<viua::something::Something>(new viua::something::Something(42));

/*
 * No repetition of 'viua::something::Something', and
 * it is much less likely that memory will be leaked.
 */
auto b = make_unique<viua::something::Something>(42);
    

Bits in Boolean context

An embarrassing omission managed to slip past tests: bit strings always evaluated to false when used a Boolean values. This obviously incorrect behaviour was fixed. Bit strings containing at least one enabled bit evaluate to true, bit strings with all zeroes evaluate to false.

All-zero bit string literals

Another bug managed to sneak into the assembler: literal bit strings that were all zeroes were reduced to zero-width strings. This is fixed now, and literal all-zero bit strings are (correctly) reduced to one-zero strings. Examples:

0b00000000  ; is reduced to 0b0
0o00000000  ; is reduced to 0o0
0x00000000  ; is reduced to 0x0
    

After stripping leading zeroes the width of the literal bit string is expanded to the nearest multiple of 8. This behaviour did not change.

Register set specifiers in assembler and disassembler

Disassembly was fixed for if, streq, insert, and remove instructions. Their target registers were not disassembled with register set specifiers, which could change the meaning of the program being disassembled.

The if instruction also did not support register set specifiers in source code at all. This is now fixed.