RZ language

Short description
Examples
RZ-bytecode
The 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
eliminates all the real-time concurrency language features and retains only the most basic language constructs.  This subset is aimed for ease of compilation and retargetting for a new instruction set, retaining the byte-code and interpreter.
 

Short description

The language is a small subset of C look alike language. It has no type (or having only one type which is "int").  Global variables must be declared but local variables do not have to be declared, they are automatic.  A variable can be either a scalar or an array.  There is no user defined data type.  An array is one dimension.  RZ language can be summarised as follows: for C programmer, please note, no: for, break, do, missing many operators especially ++, --
 

Examples

It is easier just to look at an example to know most of the syntax.  Here is an example of RZ
//  find max in an array

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 );
}

The 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).

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 multiplication

//  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;
  }
}

The 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).

RZ byte-code

rz byte-code is a post-fix zero address code.  Some code has parameters (so it can be called not a zero address, or one address, more likely to be a half address) for performance reason.  The full detail can be found in the R1 final report.  The following is the code from the inner loop of matrix multiplication program above (the lines marked with *).

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