Short descriptionThe source language is RZ. RZ is a descendant of R1: a concurrent language for small control applications. (Have a look at full report and implementation from my research work web page). RZ is aimed to be a teaching language for system programming and computer architecture subjects, which emphasises a small language that can be used to illustrate all the "inner" working parts of a computer system (compilation, code generation, ISA simulation), in other words from a high level language down to logic gates and bits of a processor. RZ
Examples
RZ-bytecode
// find max in an arrayThe variables a[], N are globals, max, i are locals. For an array, the size must be known at compile time. (A note of C user, there is no ++, --, and no "break", "print" is not "printf"). "print" knows only integer and string. The size of basic unit (integer) depends on the target machine (16-bit or 32-bit).a[10], N;
init(){
i = 0;
while ( i < N ) {
a[i] = i;
i = i + 1;
}
}main(){
N = 10;
init();
max = a[0];
i = 1;
while( i < N ) {
if( max < a[i] ) max = a[i];
i = i + 1;
}
print( "the max value is ", max );
}
Another example: a matrix multiplication. A matrix (2 dimensions) is converted into one dimension array by the index calculation i*N + j where i,j are 2 dimension index 0..N-1 and N are the size of matrix NxN.
// matrix multiplicationThe call by reference can be achived using the * and & operators just like in C. In short, you can think of RZ syntax as C without type declaration (writing a translator to convert RZ to C is a trivial task).// N = 4, simulate matrix by (i,j) = i*N + j
N, a[16], b[16], c[16];index(i,j){
return i*N + j;
}matmul() {
i = 0;
while( i<N ){
j = 0;
while( j<N ){
s = 0;
k = 0;
* while( k<N ){
* s = s + a[index(i,k)] * b[index(k,j)];
* k = k + 1;
* }
c[index(i,j)] = s;
j = j + 1;
}
i = i + 1;
}
}
address byte-code comment
270 Rval 1 ;; i
273 Rvalg 1 ;; N
276 Lt ;;
while i<N
277 Jz 331 ;; exit while loop
280 Lval 2 ;; &s,
for s = ...
283 Rval 2 ;; s
286 Lvalg 2 ;; &a[]
289 Rval 4 ;; i
292 Rval 1 ;; k
295 Call 200 ;; index(i,k)
298 Index ;;
&a[index(i,k)]
299 Fetch ;;
a[index(i,k)]
300 Lvalg 18 ;; &b
303 Rval 1 ;; k
306 Rval 3 ;; j
309 Call 200
312 Index ;;
similar to a[]
313 Fetch ;;
b[index(k,j)]
314 Mul
315 Add
316 Set
;; s=s+a[..]*b[..]
317 Lval 1
320 Rval 1
323 Lit 1
326 Add
327 Set
;; k = k + 1
328 Jmp 270 ;; end while loop
331 . . .
A bit of explanation is in order. Rval, Rvalg is the right value of a local (global) variable, similarly Lval, Lvalg the left value (or the address). Lt, Mul, Add, Set are binary operators with obvious meaning (Set takes an address and a value and assign the value to that address). Fetch takes an address and gets the value. Index is just adding the offset with the base address to get the address of the element in an array. Jz ads, jumps if top of stack is zero, Jmp is an unconditional jump.
last update 9 January 2003