next up previous contents
Next: CSP and job distribution Up: The new language Previous: Language constructs   Contents

Indexing

Instructions that allow us to perform operations on entire vectors and matrices are not going to be enough for real-world problems. (In fact, it isn't even enough for the small testing problems I wanted to work with). It is very often necessary to work on a block of a vector or in a matrix.

In a language such as MatLab, this is handled elegantly in the language. One can write A[:,2:3] = 1, and the second and third columns of the A matrix will be assigned the value 1.

But what should we do in this virtual-machine-reflecting language ? It would be nice to represent everything like opcodes: An instruction, and a set of arguments. An opcode could be introduced to ``cut'' out the block we want to work with, assign it to some variable, and when we're done working on the variable we could call another opcode to have the results ``pasted'' in again.

That would not just be ugly, it would also be inefficient, since it would involve a larger number of opcodes which in turn would impose a greater overhead in the execution of the program. So that's not the way it is done.

Even though we do not allow normal mathematical expressions to be used as arguments to opcodes (e.g. you cannot enter add r0, r1*8), indexing has been implemented as a sort of ``modifiers'' to arguments. The second and third columns of a matrix register r0 can be assigned the value 1 with the following instruction: move r0[:][1:2], 1. This changes the way the virtual machine sees and treats the first argument to the move opcode. Instead of the first argument being a register (as it would be without the indexing), it is now an indexing. The base variable of the indexing is the register r0, and the indexing consists of two intervals, one (the [:] index) which is a ``full'' interval (one that resolves to the full length possible in the given base variable), the second which is a bounded interval (a block bounded by a lower and upper index value) resolves to the integer interval $[1;2]$. We use zero-based indexing, so r0[:][1:2] refers to the second and third column of r0. Optionally, one can also specify a step value, for example r0[0:5:50], which refers to the elements at positions $\{0, 5, 10, 15\ldots 50\}$ of the register r0.

A small program demonstrating this indexing could be:

;;
;; Indexing example
;;
entry "indexing"
        decl  matrix
        zero r0, 10
        move r0[3:6][3:6], 1
        move r0[:][0], 2
        move r0[:][9], 3
        move r0[0][:], 4
        move r0[9][:], 5
        return r0
end

This is also a simple example showing how dimensions of variables (registers) can change dynamically at run-time. The decl opcode only tells the virtual machine that r0 is a matrix. The zero r0, 10 opcode initializes r0 with a $10\times 10$ matrix holding zeros.

It returns the matrix

\begin{displaymath}
\left(
\begin{array}{cccccccccc}
4& 4& 4& 4& 4& 4& 4& 4& 4&...
...& 0& 0& 3 \\
5& 5& 5& 5& 5& 5& 5& 5& 5& 5
\end{array}\right)
\end{displaymath} (3.1)


next up previous contents
Next: CSP and job distribution Up: The new language Previous: Language constructs   Contents

1999-08-09