Rz language version 3.6

(This compiler is still a work in progress. )

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 it allows students to "play" with the system.   R1 is a concurrent language. Rz simplifies that by eliminating all the real-time concurrency language features and retains only the most basic language constructs.  In a way, Rz "looks like" C (without type).

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 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 ++, --  .  The syntax looks clean because it uses indentation instead of {} and using a newline to terminate a statement instead of ';'.

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(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.
// sum array

ax[10]

sum()
  i = 0
  s = 0
  while( ax[i] != 0 )
    s = s + ax[i]
    i = i + 1
  return s

main()
  ax[0] = 11
  ax[1] = 22
  ax[2] = 33
  ax[3] = 44
  ax[4] = 0
  print(sum())
The call by reference can be achieved using the * and & operators just like in C.  In short, you can think of Rz syntax as C without type declaration.

increment(x)
  *x = *x + 1

main()
  a = 1
  increment(&a)
  print(a)

With version 3.6, the compiler generates machine code for S2 version 3 (s23). A special syntax is introduced to enable a low level code generation. 
asm("...x ")
where x is an assembly statement.  The compiler will output this statement to the output file. To use this feature, understanding of S2.3 assembler is necessary.
s23 assembly language

Current state of implementation

Recursive call is not working.  (I still got code generation for recursive call incorrectly passing parameters). * and & operators have not been implemented yet. The output of the compiler is the s2.3 assembly language. It can be assemble and run under s2.3 simulator.

Session example

Here is hand-on how to use the compiler.  Compile the "sum array" program above. The screen will show:
c:> rz36 sum.txt
.symbol
 fp 30
 sp 29
 retval 28
 rads 27
 ax 2000
.code 0
 mov fp #4000
 mov sp #3000
 jal rads main
 trap r0 #0

:sum
  st r1 @1 fp
  st r2 @2 fp
  st r3 @3 fp
  st r4 @4 fp
  add fp fp #5
  st rads @0 fp
  mov r1 #0
  mov r2 #0
  jmp L102
:L103
  ld r3 @ax r1
  add r4 r2 r3
  mov r2 r4
  add r1 r1 #1
...
:main
...
  ld r1 @1 fp
  ret rads
.data 200
.end

Download

rz36.zip   compiler source that generates s2 assembly code
 
last update 13 Jan 2013