Topics: Bit manipulation
This week was spent on a single issue: bit manipulation instructions.
Viua is now able to manipulate bit strings. New instructions allow programs to perform bitwise logic operations (and, or, xor, not), shifts, and set and test single bits inside bit strings.
The full set of new instructions introduced is presented below, with short explanations of what they do.
This instruction is used to create bit string values. It can either take a register index and create a bit string with width specified by the integer found in the register it received, or a bit string (or binary) literal.
Bit string literals may be written as binary, octal, or hexadecimal numbers.
One caveat that must be taken into account when using literals is that their width is expanded to the nearest multiple of eigth. For example, if a literal in source code might be encoded using 13 bits it will be actually encoded on 16 bits. The expansion is performed by padding the bit string with zeroes on the left (i.e. the higher bits will be zeroes).
Literals are a handy syntax when a bit-pattern is known at write- or compile time, and can be used to avoid creating an empty bit strings and enabling bits one-by-one.
bitat instruction fetches value of the bit at a specified index from a bit string.
It needs a target register, a bit string value, and an index:
bitset instruction sets value of the bit at a specified index inside a bit string,
modifying the bit string in-place.
The four operations available: and, or, xor, and not are implemented by
The first three instructions share the three-operand structure: output-lhs-rhs, while
bitnot has only two operands: output and source.
Viua provides instructions to perform arithmetic and logical shifts, in both directions (left and right). All shift instructions have the same operand structure: output-source-offset. Shifted bit strings are modified in-place.
The output operand (also known as "target register") stores the bits that were "shifted out". For example, if an 8 bit wide bit string is shifted to the left by 3 bits, the target register will store a 3 bit wide bit string.
It is also possible to use
void target register as the output of shift instructions to
discard the "shifted out" bits if they are not interesting.
Viua provides instructions to perform left and right bit rotations. Rotations are performed on bit strings in-place.